improved icon class, only write icons out if needed (snarl)

This commit is contained in:
Patrick von Reth 2011-07-21 16:57:12 +02:00
parent 727cca7ba7
commit 7abeae216d
4 changed files with 85 additions and 40 deletions

View File

@ -20,52 +20,91 @@
#include <QCryptographichash>
#include <QBuffer>
#include <QHash>
#include <QDebug>
QHash<QString,QString> NotificationIcon::hasedImages;
NotificationIcon::NotificationIcon():
_data(NULL)
class NotificationIcon::NotificationIconData
{
public:
NotificationIconData():
_isLocalFile(false)
{ };
NotificationIconData(const QImage &img):
_img(img),
_isLocalFile(false)
{};
~NotificationIconData()
{ };
QImage _img;
QByteArray _data;
QString _hash;
bool _isLocalFile;
private:
NotificationIconData(const NotificationIconData &other)
{ };
};
NotificationIcon::NotificationIcon()
{
d = QSharedPointer<NotificationIconData>(new NotificationIconData());
}
NotificationIcon::NotificationIcon(const QImage &img):
_img(img)
NotificationIcon::NotificationIcon(const QImage &img)
{
QBuffer buffer( _data );
buffer.open( QBuffer::WriteOnly );
img.save( &buffer, "PNG" );
computeHash();
d = QSharedPointer<NotificationIconData>(new NotificationIconData(img));
}
NotificationIcon::NotificationIcon(const QByteArray &img)
{
computeHash();
}
NotificationIcon::NotificationIcon(const NotificationIcon &other):
d(other.d)
{ }
void NotificationIcon::computeHash(){
if(_hash.isEmpty()){
const QString &NotificationIcon::hash() const{
if(d->_hash.isEmpty()){
QCryptographicHash h(QCryptographicHash::Md5);
h.addData(*_data);
_hash = h.result().toHex();
h.addData(imageData());
d->_hash = h.result().toHex();
}
return d->_hash;
}
const QImage &NotificationIcon::image() const{
return _img;
return d->_img;
}
const QString &NotificationIcon::localUrl()const{
if(hasedImages.contains(_hash))
return hasedImages[_hash];
QString h = hash();
if(hasedImages.contains(h))
return hasedImages[h];
QString fp = SnoreServer::snoreTMP();
fp = fp.append("/").append(_hash).append(".png");
_img.save(fp,"PNG");
hasedImages[_hash] = fp;
return hasedImages[_hash];
fp = fp.append("/").append(h).append(".png");
d->_img.save(fp,"PNG");
hasedImages[h] = fp;
return hasedImages[h];
}
const QByteArray &NotificationIcon::imageData() const{
if(d->_data.isEmpty()){
QBuffer buffer( &d->_data );
buffer.open( QBuffer::WriteOnly );
d->_img.save( &buffer, "PNG" );
}
return d->_data;
}
const bool NotificationIcon::isLocalFile() const
{
return d->_isLocalFile;
}
#include "icon.moc"

View File

@ -20,7 +20,7 @@
#include <QImage>
#include <QSharedPointer>
class SNORE_EXPORT NotificationIcon
@ -29,18 +29,23 @@ public:
NotificationIcon();
NotificationIcon(const QImage &img);
NotificationIcon(const QByteArray &img);
NotificationIcon(const NotificationIcon &other);
const QImage &image() const;
const QString &localUrl() const;
const QByteArray &imageData() const;
const bool isLocalFile() const;
private:
static QHash<QString,QString> hasedImages;
private:
QImage _img;
QByteArray *_data;
QString _hash;
void computeHash();
class NotificationIconData;
QSharedPointer<NotificationIconData> d;
const QString &hash() const;
};

View File

@ -28,10 +28,6 @@ Growl_Backend::Growl_Backend(SnoreServer *snore):
Notification_Backend("Growl",snore),
id(0)
{
gntp *growl = new gntp("SnoreNotify");
growl->regist("Default Alert");
_applications.insert("SnoreNotify",growl);
}
Growl_Backend::~Growl_Backend(){
foreach(Application *a,this->snore()->aplications().values()){
@ -47,6 +43,7 @@ void Growl_Backend::registerApplication(Application *application){
try{
growl->regist(a->name().toUtf8().constData());
}catch(const std::exception& e){
qDebug()<<"Growl:"<<e.what();
}
}
_applications.insert(application->name(),growl);

View File

@ -94,13 +94,16 @@ int Snarl_Backend::notify(Notification notification){
snarlInterface = _defautSnarlinetrface;
}
uint id = notification.id();
qDebug()<<"Snarl is localfile"<<notification.icon().isLocalFile();
if(id == 0){
id = snarlInterface->Notify(notification.alert().toUtf8().constData(),
Notification::toPlainText(notification.title()).toUtf8().constData(),
Notification::toPlainText(notification.text()).toUtf8().constData(),
notification.timeout(),
notification.icon().localUrl().isEmpty()?0:notification.icon().localUrl().toUtf8().constData(),
0,notification.priority());
notification.icon().isLocalFile()?notification.icon().localUrl().toUtf8().constData():0,
!notification.icon().isLocalFile()?notification.icon().imageData().toBase64().constData():0,
notification.priority());
foreach(const Notification::Action *a, notification.actions()){
qDebug()<<"snarl add action"<<a->id<<a->name;
@ -115,8 +118,9 @@ int Snarl_Backend::notify(Notification notification){
Notification::toPlainText(notification.title()).toUtf8().constData(),
Notification::toPlainText(notification.text()).toUtf8().constData(),
notification.timeout(),
notification.icon().localUrl().toUtf8().constData(),
0,notification.priority());
notification.icon().isLocalFile()?notification.icon().localUrl().toUtf8().constData():0,
!notification.icon().isLocalFile()?notification.icon().imageData().toBase64().constData():0,
notification.priority());
}
return id;
}