cleanups refactoring, added supportsRichtext to backends

This commit is contained in:
Patrick von Reth 2013-07-22 21:12:24 +02:00
parent bb98537310
commit 1af5f8d43c
19 changed files with 237 additions and 203 deletions

View File

@ -1,4 +1,4 @@
set ( SnoreNotify_SRCS ${SnoreNotify_SRCS}
set ( SnoreNotify_SRCS ${SnoreNotify_SRCS}
notification/notification.cpp
notification/icon.cpp
PARENT_SCOPE)

View File

@ -17,12 +17,9 @@
#include "icon.h"
#include "../snore.h"
#include <QCryptographicHash>
#include <QBuffer>
#include <QHash>
#include <QFile>
#include <QDebug>
namespace Snore{
#include "notification/icon_p.h"
using namespace Snore;
QHash<QString,QString> SnoreIcon::hasedImages;
@ -33,12 +30,12 @@ SnoreIcon::SnoreIcon() :
}
SnoreIcon::SnoreIcon(const QImage &img):
d(new SnoreIconData(img))
d(new IconData(img))
{
}
SnoreIcon::SnoreIcon(const QString &url):
d(new SnoreIconData(url))
d(new IconData(url))
{
}
@ -47,6 +44,12 @@ SnoreIcon::SnoreIcon(const SnoreIcon &other):
{
}
SnoreIcon &SnoreIcon::operator=(const SnoreIcon &other)
{
d = other.d;
return *this;
}
SnoreIcon::~SnoreIcon()
{
@ -89,7 +92,7 @@ QString SnoreIcon::hash() const{
return "";
}
if(d->m_hash.isEmpty() && !imageData().isNull()){
d->m_hash = computeHash(imageData());
d->m_hash = IconData::computeHash(imageData());
}
return d->m_hash;
}
@ -108,55 +111,7 @@ bool SnoreIcon::isValid() const
return d;
}
QString SnoreIcon::computeHash(const QByteArray &data)
{
QCryptographicHash h(QCryptographicHash::Md5);
h.addData(data);
return h.result().toHex();
}
QString SnoreIcon::url() const
{
return d->m_url;
}
SnoreIcon::SnoreIcon::SnoreIconData::SnoreIconData(const QString &url):
m_url(url),
m_isLocalFile(false)
{
if(m_url.startsWith(":/"))
{
m_hash = computeHash(m_url.toLatin1());
}
else if(QFile(url).exists())
{
m_isLocalFile = true;
m_localUrl = url;
}
}
SnoreIcon::SnoreIcon::SnoreIconData::SnoreIconData(const QImage &img):
m_img(img),
m_isLocalFile(false)
{
setImageData();
}
SnoreIcon::SnoreIcon::SnoreIconData::~SnoreIconData()
{
}
void SnoreIcon::SnoreIconData::setImageData()
{
QBuffer buffer( &m_data );
buffer.open( QBuffer::WriteOnly );
m_img.save( &buffer, "PNG" );
if(m_hash.isEmpty())
{
m_hash = computeHash(m_data);
}
}
}

View File

@ -18,13 +18,12 @@
#define NOTIFICATION_ICON_H
#include "../snore_exports.h"
#include <QImage>
#include <QSharedPointer>
#include <QSharedData>
#include <QDebug>
namespace Snore{
class IconData;
class SNORE_EXPORT SnoreIcon
{
@ -33,6 +32,7 @@ public:
SnoreIcon(const QImage &img);
SnoreIcon(const class QString &url);
SnoreIcon(const SnoreIcon &other);
SnoreIcon &operator=(const SnoreIcon &other);
~SnoreIcon();
const QImage &image() const;
@ -47,28 +47,8 @@ public:
private:
static QHash<QString,QString> hasedImages;
static QString computeHash(const QByteArray &data);
class SNORE_EXPORT SnoreIconData : public QSharedData
{
public:
SnoreIconData(const QImage &img);
SnoreIconData(const QString &url);
~SnoreIconData();
void setImageData();
QImage m_img;
QByteArray m_data;
QString m_localUrl;
QString m_url;
QString m_hash;
bool m_isLocalFile;
};
QExplicitlySharedDataPointer<SnoreIconData> d;
QExplicitlySharedDataPointer<IconData> d;
};
}

View File

@ -0,0 +1,76 @@
#ifndef ICONDATA_H
#define ICONDATA_H
#include "notification.h"
#include <QImage>
#include <QSharedData>
#include <QBuffer>
#include <QHash>
#include <QFile>
#include <QDebug>
#include <QCryptographicHash>
namespace Snore{
class IconData : public QSharedData
{
public:
IconData(const QString &url):
m_url(url),
m_isLocalFile(false)
{
if(m_url.startsWith(":/"))
{
m_hash = computeHash(m_url.toLatin1());
}
else if(QFile(url).exists())
{
m_isLocalFile = true;
m_localUrl = url;
}
}
IconData(const QImage &img):
m_img(img),
m_isLocalFile(false)
{
setImageData();
}
~IconData()
{
}
void setImageData()
{
QBuffer buffer( &m_data );
buffer.open( QBuffer::WriteOnly );
m_img.save( &buffer, "PNG" );
if(m_hash.isEmpty())
{
m_hash = computeHash(m_data);
}
}
static QString computeHash(const QByteArray &data)
{
QCryptographicHash h(QCryptographicHash::Md5);
h.addData(data);
return h.result().toHex();
}
QImage m_img;
QByteArray m_data;
QString m_localUrl;
QString m_url;
QString m_hash;
bool m_isLocalFile;
};
}
#endif // ICONDATA_H

View File

@ -20,34 +20,20 @@
#include "notification.h"
#include "snore.h"
#include "notification/icon.h"
#include "notification/notification_p.h"
#include "plugins/plugincontainer.h"
#include <QDebug>
#include <QTcpSocket>
#include <Qt>
#include <QTextDocumentFragment>
#include <QTextDocument>
#include <QSharedData>
namespace Snore{
int Notification::notificationMetaID = qRegisterMetaType<Notification>();
uint Notification::notificationCount = 0;
uint Notification::m_idCount = 1;
using namespace Snore;
int Notification::DefaultTimeout = 10;
QString Notification::toPlainText ( const QString &string )
{
if( Qt::mightBeRichText(string))
return QTextDocumentFragment::fromHtml(string).toPlainText();
return QString(string);
}
uint NotificationData::notificationCount = 0;
uint NotificationData::m_idCount = 1;
int NotificationData::notificationMetaID = qRegisterMetaType<Notification>();
Notification::Notification () :
d(NULL)
@ -64,6 +50,12 @@ Notification::Notification ( const Notification &other ) :
{
}
Notification &Notification::operator=(const Notification &other)
{
d = other.d;
return *this;
}
Notification::~Notification()
{
}
@ -214,28 +206,3 @@ QDataStream &operator<< ( QDataStream &stream, const Notification::Action &a)
return stream;
}
Snore::Notification::Notification::NotificationData::NotificationData(const QString &application, const QString &alert, const QString &title, const QString &text, const SnoreIcon &icon, int timeout, NotificationEnums::Prioritys::prioritys priority):
m_id ( m_idCount++ ),
m_updateID(0),
m_timeout ( timeout ),
m_source ( NULL),
m_application ( application ),
m_alert ( alert ),
m_title ( title ),
m_text ( text ),
m_icon ( icon ),
m_priority(priority),
m_closeReason(NotificationEnums::CloseReasons::NONE)
{
notificationCount++;
qDebug()<< "Creating Notification: ActiveNotifications" << notificationCount << "id" << m_id;
}
Snore::Notification::Notification::NotificationData::~NotificationData()
{
notificationCount--;
qDebug() << "Deleting Notification: ActiveNotifications" << notificationCount << "id" << m_id;
}
}

View File

@ -26,14 +26,19 @@
#include <QVariant>
#include <QDebug>
#include <QTextDocumentFragment>
#include <QTextDocument>
namespace Snore{
class NotificationData;
class SNORE_EXPORT Notification
{
public:
static int DefaultTimeout;
static QString toPlainText ( const QString &string );
class Action
{
@ -43,10 +48,12 @@ public:
QString name;
};
public:
Notification ();
Notification (const QString &application,const QString &alert,const QString &title,const QString &text,const SnoreIcon &icon,int timeout=10, NotificationEnums::Prioritys::prioritys priority = NotificationEnums::Prioritys::NORMAL );
Notification ( const Notification &other );
Notification &operator=(const Notification &other);
~Notification();
const uint &id() const;
@ -86,39 +93,8 @@ public:
private:
static uint m_idCount;
class SNORE_EXPORT NotificationData : public QSharedData
{
public:
NotificationData ( const QString &application,const QString &alert,const QString &title,const QString &text,const SnoreIcon &icon,
int timeout,NotificationEnums::Prioritys::prioritys priority );
~NotificationData();
uint m_id;
uint m_updateID;
int m_timeout;
Notification::Action *m_actionInvoked;
SnoreFrontend *m_source;
QString m_application;
QString m_alert;
QString m_title;
QString m_text;
SnoreIcon m_icon;
NotificationEnums::Prioritys::prioritys m_priority;
NotificationEnums::CloseReasons::closeReasons m_closeReason;
QHash<int,Notification::Action*> m_actions;
QVariantHash m_hints;
};
QExplicitlySharedDataPointer<NotificationData> d;
static uint notificationCount;
static int notificationMetaID;
};

View File

@ -0,0 +1,67 @@
#ifndef NOTIFICATIONDATA_H
#define NOTIFICATIONDATA_H
#include "notification/icon.h"
#include "notification/NotificationEnums.h"
#include <QSharedData>
namespace Snore{
class NotificationData : public QSharedData
{
public:
NotificationData ( const QString &application,const QString &alert,const QString &title,const QString &text,const SnoreIcon &icon,
int timeout,NotificationEnums::Prioritys::prioritys priority ):
m_id ( m_idCount++ ),
m_updateID(0),
m_timeout ( timeout ),
m_source ( NULL),
m_application ( application ),
m_alert ( alert ),
m_title ( title ),
m_text ( text ),
m_icon ( icon ),
m_priority(priority),
m_closeReason(NotificationEnums::CloseReasons::NONE)
{
notificationCount++;
qDebug()<< "Creating Notification: ActiveNotifications" << notificationCount << "id" << m_id;
}
~NotificationData()
{
notificationCount--;
qDebug() << "Deleting Notification: ActiveNotifications" << notificationCount << "id" << m_id;
}
uint m_id;
uint m_updateID;
int m_timeout;
Notification::Action *m_actionInvoked;
SnoreFrontend *m_source;
QString m_application;
QString m_alert;
QString m_title;
QString m_text;
SnoreIcon m_icon;
NotificationEnums::Prioritys::prioritys m_priority;
NotificationEnums::CloseReasons::closeReasons m_closeReason;
QHash<int,Notification::Action*> m_actions;
QVariantHash m_hints;
static uint notificationCount;
static uint m_idCount;
static int notificationMetaID;
private:
Q_DISABLE_COPY(NotificationData)
};
}
#endif // NOTIFICATIONDATA_H

View File

@ -29,8 +29,10 @@
namespace Snore{
SnoreBackend::SnoreBackend ( const QString &name ) :
SnorePlugin ( name )
SnoreBackend::SnoreBackend (const QString &name , bool canCloseNotification, bool supportsRichtext) :
SnorePlugin ( name ),
m_canCloseNotification(canCloseNotification),
m_supportsRichtext(supportsRichtext)
{
}
@ -85,8 +87,8 @@ void SnoreBackend::slotCloseNotification(Notification notification)
Q_UNUSED(notification)
}
SnoreSecondaryBackend::SnoreSecondaryBackend(const QString &name)
:SnoreBackend(name)
SnoreSecondaryBackend::SnoreSecondaryBackend(const QString &name, bool supportsRhichtext)
:SnoreBackend(name,false,supportsRhichtext)
{
}
@ -107,6 +109,16 @@ Snore::Notification SnoreBackend::getActiveNotificationByID(uint id)
return m_activeNotifications[id];
}
bool SnoreBackend::canCloseNotification()
{
return m_canCloseNotification;
}
bool SnoreBackend::supportsRichtext()
{
return m_supportsRichtext;
}
void SnoreBackend::addActiveNotification(Notification n)
{
m_activeNotifications[n.id()] = n;

View File

@ -35,7 +35,7 @@ class SNORE_EXPORT SnoreBackend : public SnorePlugin
Q_OBJECT
Q_INTERFACES(Snore::SnorePlugin)
public:
SnoreBackend(const QString &name );
SnoreBackend(const QString &name, bool canCloseNotification, bool supportsRichtext );
virtual ~SnoreBackend();
virtual bool init(SnoreCore *snore);
@ -43,7 +43,8 @@ public:
Snore::Notification getActiveNotificationByID(uint id);
virtual bool canCloseNotification() = 0;
bool canCloseNotification();
bool supportsRichtext();
signals:
void closeNotification( Snore::Notification );
@ -56,6 +57,8 @@ public slots:
virtual void slotCloseNotification ( Snore::Notification notification );
protected:
bool m_canCloseNotification;
bool m_supportsRichtext;
void closeNotification(Snore::Notification,Snore::NotificationEnums::CloseReasons::closeReasons);
@ -80,7 +83,7 @@ class SNORE_EXPORT SnoreSecondaryBackend:public SnoreBackend
Q_OBJECT
Q_INTERFACES(Snore::SnorePlugin Snore::SnoreBackend)
public:
SnoreSecondaryBackend(const QString &name);
SnoreSecondaryBackend(const QString &name, bool supportsRhichtext);
virtual ~SnoreSecondaryBackend();
virtual bool init(SnoreCore *snore);

View File

@ -368,4 +368,9 @@ void SnoreCore::requestCloseNotification(Notification n, NotificationEnums::Clos
m_notificationBackend->requestCloseNotification(n,r);
}
bool SnoreCore::primaryBackendSupportsRichtext()
{
return m_notificationBackend->supportsRichtext();
}
}

View File

@ -69,6 +69,8 @@ public:
void requestCloseNotification(Notification,NotificationEnums::CloseReasons::closeReasons);
bool primaryBackendSupportsRichtext();
@ -104,6 +106,19 @@ private slots:
};
static inline QString toPlainText ( const QString &string)
{
if(Qt::mightBeRichText(string))
{
return QTextDocumentFragment::fromHtml(string).toPlainText();
}
else
{
return string;
}
}
}
#endif // SNORESERVER_H

View File

@ -32,7 +32,7 @@ Q_EXPORT_PLUGIN2(growl,Growl)
Growl *Growl::s_instance = NULL;
Growl::Growl():
SnoreBackend("Growl"),
SnoreBackend("Growl",false,false),
m_id(0)
{
s_instance = this;
@ -79,8 +79,8 @@ void Growl::slotNotify(Notification notification){
//qDebug()<<"Notify Growl:"<<notification.application()<<Notification.toPlainText(notification.title());
try{
growl->notify(notification.alert().toUtf8().constData(),notification.id(),
Notification::toPlainText(notification.title()).toUtf8().constData(),
Notification::toPlainText(notification.text()).toUtf8().constData(),
Snore::toPlainText(notification.title()).toUtf8().constData(),
Snore::toPlainText(notification.text()).toUtf8().constData(),
notification.icon().localUrl().isEmpty()?NULL:notification.icon().localUrl().toUtf8().constData(),NULL,"1");
}catch(const std::exception& e){
@ -104,7 +104,3 @@ void Growl::gntpCallback(const int &id,const std::string &reason,const std::stri
s_instance->closeNotification(n,r);
}
bool Growl::canCloseNotification()
{
return false;
}

View File

@ -31,7 +31,6 @@ public:
Growl();
~Growl();
static void gntpCallback(const int &id,const std::string &reason,const std::string &data);
bool canCloseNotification();
private:
//a static instance for the static callback methode

View File

@ -130,7 +130,7 @@ private:
SnarlBackend::SnarlBackend():
SnoreBackend("Snarl"),
SnoreBackend("Snarl",true,false),
m_defautSnarlinetrface(NULL)
{
@ -158,11 +158,6 @@ bool SnarlBackend::init(SnoreCore *snore){
return SnoreBackend::init(snore);
}
bool SnarlBackend::canCloseNotification()
{
return true;
}
void SnarlBackend::slotRegisterApplication(Application *application){
SnarlInterface *snarlInterface = NULL;
if(m_applications.contains(application->name())){
@ -221,8 +216,8 @@ void SnarlBackend::slotNotify(Notification notification){
if(notification.updateID() == 0){
ULONG32 id = snarlInterface->Notify(notification.alert().toUtf8().constData(),
Notification::toPlainText(notification.title()).toUtf8().constData(),
Notification::toPlainText(notification.text()).toUtf8().constData(),
Snore::toPlainText(notification.title()).toUtf8().constData(),
Snore::toPlainText(notification.text()).toUtf8().constData(),
notification.timeout(),
notification.icon().isLocalFile()?notification.icon().localUrl().toUtf8().constData():0,
!notification.icon().isLocalFile()?notification.icon().imageData().toBase64().constData():0,
@ -239,8 +234,8 @@ void SnarlBackend::slotNotify(Notification notification){
//update message
snarlInterface->Update(m_idMap[notification.updateID()],
notification.alert().toUtf8().constData(),
Notification::toPlainText(notification.title()).toUtf8().constData(),
Notification::toPlainText(notification.text()).toUtf8().constData(),
Snore::toPlainText(notification.title()).toUtf8().constData(),
Snore::toPlainText(notification.text()).toUtf8().constData(),
notification.timeout(),
notification.icon().isLocalFile()?notification.icon().localUrl().toUtf8().constData():0,
!notification.icon().isLocalFile()?notification.icon().imageData().toBase64().constData():0,

View File

@ -32,7 +32,6 @@ public:
SnarlBackend();
~SnarlBackend();
virtual bool init(Snore::SnoreCore *snore);
bool canCloseNotification();
private:
class SnarlWidget;

View File

@ -16,7 +16,7 @@ Q_EXPORT_PLUGIN2(snoretoast,SnoreToast)
SnoreToast::SnoreToast():
SnoreBackend("SnoreToast")
SnoreBackend("SnoreToast",false,false)
{
}
@ -52,11 +52,6 @@ bool SnoreToast::init(SnoreCore *snore)
return SnoreBackend::init(snore);
}
bool SnoreToast::canCloseNotification()
{
return false;
}
void SnoreToast::slotRegisterApplication(Application *application)
{
Q_UNUSED(application)
@ -77,9 +72,9 @@ void SnoreToast::slotNotify(Notification notification)
QStringList arguements;
arguements << "-t"
<< Notification::toPlainText(notification.title())
<< Snore::toPlainText(notification.title())
<< "-m"
<< Notification::toPlainText(notification.text())
<< Snore::toPlainText(notification.text())
<< "-p"
// << notification.icon().isLocalFile()?QDir::toNativeSeparators(notification.icon().localUrl()):notification.icon().url()
<< QDir::toNativeSeparators(notification.icon().localUrl())

View File

@ -11,7 +11,6 @@ public:
SnoreToast();
~SnoreToast();
bool init(Snore::SnoreCore *snore);
bool canCloseNotification();
// SnoreBackend interface

View File

@ -11,7 +11,7 @@ using namespace Snore;
Q_EXPORT_PLUGIN2(trayicon,TrayIconNotifer)
TrayIconNotifer::TrayIconNotifer () :
SnoreBackend ( "SystemTray" ),
SnoreBackend ( "SystemTray",false,false ),
m_trayIcon(NULL),
m_displayed(-1)
{
@ -26,10 +26,6 @@ bool TrayIconNotifer::init(SnoreCore *snore){
return SnoreBackend::init(snore);
}
bool TrayIconNotifer::canCloseNotification()
{
return false;
}
void TrayIconNotifer::slotRegisterApplication ( Application *application )
{
@ -57,7 +53,7 @@ void TrayIconNotifer::displayNotification(){
qDebug()<<"taking"<<notification.title();
m_displayed = notification.id();
m_trayIcon->showMessage ( Notification::toPlainText(notification.title()),Notification::toPlainText(notification.text()),QSystemTrayIcon::NoIcon,notification.timeout() *1000 );
m_trayIcon->showMessage ( Snore::toPlainText(notification.title()),Snore::toPlainText(notification.text()),QSystemTrayIcon::NoIcon,notification.timeout() *1000 );
m_lastNotify.restart();
}

View File

@ -18,7 +18,6 @@ class TrayIconNotifer:public Snore::SnoreBackend
public:
TrayIconNotifer ();
virtual bool init(Snore::SnoreCore *snore);
bool canCloseNotification();
public slots:
void slotRegisterApplication ( Snore::Application *application );