cleanup of icon stuff

This commit is contained in:
Patrick von Reth 2014-01-13 14:13:05 +01:00
parent 6745afa680
commit 88aae3d8d8
8 changed files with 61 additions and 65 deletions

View File

@ -34,7 +34,7 @@ class SNORE_EXPORT Alert
friend class AlertData;
public:
Alert();
Alert(const QString &name, const QString &title="", const Icon &icon = Icon(":/root/snore.png"), bool active=true );
explicit Alert(const QString &name, const QString &title="", const Icon &icon = Icon(":/root/snore.png"), bool active=true );
Alert(const Alert &other);
Alert &operator=(const Alert &other);
~Alert();

View File

@ -33,7 +33,7 @@ class SNORE_EXPORT Application
{
public:
Application();
Application ( const QString &name, const Icon &icon = Icon(":/root/snore.png"));
explicit Application ( const QString &name, const Icon &icon = Icon(":/root/snore.png"));
Application(const Application &other);
Application &operator=(const Application &other);
~Application();

View File

@ -25,9 +25,6 @@
using namespace Snore;
QHash<QString,QString> Icon::m_localImageCache;
Icon::Icon() :
d(NULL)
{
@ -64,41 +61,27 @@ const QImage &Icon::image() const{
{
if(isLocalFile())
{
d->m_img = QImage(d->m_url);
d->m_img = QImage(d->m_url);
}
else
{
d->m_img = QImage::fromData(d->m_data,"PNG");
d->m_img = QImage::fromData(imageData(),"PNG");
}
}
return d->m_img;
}
QString Icon::localUrl()const{
if(d->m_localUrl.isEmpty())
if(!QFile(d->m_localUrl).exists())
{
if(m_localImageCache.contains(d->m_hash))
{
d->m_localUrl = m_localImageCache[d->m_hash];
}else
{
if(isRemoteFile())
{
d->download();
}
d->m_localUrl = QString("%1%2.png").arg(SnoreCorePrivate::snoreTMP(), d->m_hash);
image().save(d->m_localUrl ,"PNG");
m_localImageCache[d->m_hash] = d->m_localUrl;
}
image().save(d->m_localUrl ,"PNG");
}
qDebug() << d->m_localUrl << QFile(d->m_localUrl).exists();
return d->m_localUrl;
}
const QByteArray &Icon::imageData() const{
if(d->m_data.isEmpty() && !image().isNull()){
d->setImageData();
}
return d->m_data;
return d->imageData();
}
bool Icon::isLocalFile() const
@ -106,13 +89,9 @@ bool Icon::isLocalFile() const
return d->m_isLocalFile;
}
bool Icon::isEmpty() const{
return d->m_hash.isEmpty() && d->m_img.isNull() && d->m_localUrl.isEmpty();
}
bool Icon::isValid() const
{
return d;
return d && !(d->m_img.isNull() && d->m_url.isEmpty());
}
QString Icon::url() const
@ -123,5 +102,11 @@ QString Icon::url() const
bool Snore::Icon::isRemoteFile() const
{
return !d->m_isResource && !isLocalFile();
return d->m_isRemoteFile;
}
QDebug operator<< ( QDebug debug, const Snore::Icon &icon )
{
debug << "Snore::Icon(" << icon.url() << ")" ;
return debug.maybeSpace();
}

View File

@ -44,30 +44,16 @@ public:
const QByteArray &imageData() const ;
bool isLocalFile() const;
bool isRemoteFile() const;
bool isEmpty() const;
bool isValid() const;
private:
static QHash<QString,QString> m_localImageCache;
QExplicitlySharedDataPointer<IconData> d;
};
}
inline QDebug operator<< ( QDebug debug, const Snore::Icon &icon )
{
if(icon.isValid())
{
debug << "Snore::Icon(" << icon.url() << ")" ;
}
else
{
debug << "Snore::Icon(0x00)" ;
}
return debug.maybeSpace();
}
SNORE_EXPORT QDebug operator<< ( QDebug, const Snore::Icon &);
#endif // NOTIFICATION_ICON_H

View File

@ -18,6 +18,7 @@
*/
#include "icon_p.h"
#include "../snore_p.h"
#include <QEventLoop>
#include <QNetworkAccessManager>
@ -27,23 +28,29 @@ using namespace Snore;
IconData::IconData(const QString &url):
m_url(url),
m_hash(computeHash(m_url.toLatin1())),
m_isLocalFile(false),
m_isResource(m_url.startsWith(":/")),
m_hash(computeHash(m_url.toLatin1()))
m_isResource(m_url.startsWith(":/"))
{
if(QFile(url).exists())
if(!m_isResource && QFile(url).exists())
{
m_isLocalFile = true;
m_localUrl = url;
}
else
{
m_localUrl = QString("%1%2.png").arg(SnoreCorePrivate::snoreTMP(), m_hash);
}
m_isRemoteFile = !m_isLocalFile && ! m_isResource;
}
IconData::IconData(const QImage &img):
m_img(img),
m_isLocalFile(false),
m_isResource(false)
m_isResource(false),
m_isRemoteFile(false)
{
setImageData();
imageData();
}
IconData::~IconData()
@ -52,16 +59,29 @@ IconData::~IconData()
}
void Snore::IconData::setImageData()
const QByteArray &Snore::IconData::imageData()
{
QBuffer buffer( &m_data );
buffer.open( QBuffer::WriteOnly );
m_img.save( &buffer, "PNG" );
if(m_hash.isEmpty())
if(m_data.isEmpty())
{
m_hash = computeHash(m_data);
if(!m_isLocalFile)
{
if(!m_img.isNull())
{
QBuffer buffer( &m_data );
buffer.open( QBuffer::WriteOnly );
m_img.save( &buffer, "PNG" );
if(m_hash.isEmpty())
{
m_hash = computeHash(m_data);
}
}
else if(m_isRemoteFile)
{
download();
}
}
}
return m_data;
}
QString IconData::computeHash(const QByteArray &data)

View File

@ -45,7 +45,7 @@ public:
static QString computeHash(const QByteArray &data);
void setImageData();
const QByteArray &imageData();
void download();
QImage m_img;
@ -55,6 +55,7 @@ public:
QString m_hash;
bool m_isLocalFile;
bool m_isResource;
bool m_isRemoteFile;
private:
Q_DISABLE_COPY(IconData)

View File

@ -72,11 +72,13 @@ void SnoreToast::slotNotify(Notification notification)
arguements << "-t"
<< Snore::toPlainText(notification.title())
<< "-m"
<< Snore::toPlainText(notification.text())
<< "-p"
// << notification.icon().isLocalFile()?QDir::toNativeSeparators(notification.icon().localUrl()):notification.icon().url()
<< QDir::toNativeSeparators(notification.icon().localUrl())
<< "-w"
<< Snore::toPlainText(notification.text());
if(notification.icon().isValid())
{
arguements << "-p"
<< QDir::toNativeSeparators(notification.icon().localUrl());
}
arguements << "-w"
<< "-appID"
<< m_appID;
;

View File

@ -142,10 +142,12 @@ SnarlNotification Parser::parse(QString &msg,QTcpSocket* client){
}
sNotification.notification = Notification(app,alert,title,text,icon,timeout);
qDebug() << sNotification.notification.title() << sNotification.notification.icon() << sNotification.notification.icon().isValid();
sNotification.notification.setSource(snarl);
switch(action){
switch(action)
{
case NOTIFICATION:
{
qDebug() << sNotification.notification.application();