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

View File

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

View File

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

View File

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