diff --git a/src/core/application.cpp b/src/core/application.cpp index b2e756d..97d5d9d 100644 --- a/src/core/application.cpp +++ b/src/core/application.cpp @@ -18,34 +18,73 @@ Application::Application(const QString &name): - name(name) -{} - -Application::Application(): - name("Error: Uninitialized Application") -{} - -void Application::addAlert(const QString &alert,const QString &title) + _name(name), + _initialized(false) { - alerts.insert(alert,QSharedPointer(new Alert(alert,title))); + _alerts.insert("",new Alert("Default Alert","Default Alert")); } +Application::Application(): + _name("Error: Uninitialized Application") +{} + +Application::~Application(){ + foreach(Alert *a,_alerts){ + a->deleteLater(); + } +} + +void Application::addAlert(Alert *alert) +{ + _alerts.insert(alert->name(),alert); +} + +const QString &Application::name() const{ + return _name; +} + +const AlertList &Application::alerts() const{ + return _alerts; +} + +bool Application::isInitialized(){ + return _initialized; +} + +void Application::setInitialized(bool b){ + _initialized = b; +} Alert::Alert(const QString &name,const QString &title): - name(name), - title(title), - active(true) + _name(name), + _title(title), + _active(true) {} Alert::Alert(const QString &name,const QString &title,bool active): - name(name), - title(title), - active(active) + _name(name), + _title(title), + _active(active) {} Alert::Alert(): - active(false) + _active(false) {} +const QString &Alert::name() const{ + return _name; +} + +const QString &Alert::title() const{ + return _title; +} + +bool Alert::isActive() const{ + return _active; +} + +#include "application.moc" + + diff --git a/src/core/application.h b/src/core/application.h index 9fbba4a..0ab80de 100644 --- a/src/core/application.h +++ b/src/core/application.h @@ -19,31 +19,46 @@ #include "snore_exports.h" #include -#include -typedef QHash > ApplicationsList ; -typedef QHash > AlertList; +typedef QHash ApplicationsList ; +typedef QHash AlertList; -class SNORE_EXPORT Application +class SNORE_EXPORT Application:public QObject { + Q_OBJECT public: Application(const QString &name); Application(); - AlertList alerts; - QString name; + ~Application(); + void addAlert(Alert *alert); + const QString &name() const; + const AlertList &alerts() const; + bool isInitialized(); + void setInitialized(bool b); - void addAlert(const QString &alert,const QString &title); +private: + QString _name; + AlertList _alerts; + bool _initialized; + }; -class SNORE_EXPORT Alert{ +class SNORE_EXPORT Alert:public QObject +{ + Q_OBJECT public: Alert(const QString &name,const QString &title); Alert(const QString &name,const QString &title,bool active); Alert(); - QString name; - QString title; - bool active; + + const QString &name() const; + const QString &title() const; + bool isActive() const; +private: + QString _name; + QString _title; + bool _active; }; diff --git a/src/core/interface.h b/src/core/interface.h index 99dd4a1..c29a9e8 100644 --- a/src/core/interface.h +++ b/src/core/interface.h @@ -19,6 +19,8 @@ #include "snore_exports.h" #include "notification.h" +#include + class SNORE_EXPORT SnorePlugin:public QObject{ Q_OBJECT @@ -44,6 +46,7 @@ public: virtual bool isPrimaryNotificationBackend()=0; public slots: + virtual void registerApplication(class Application *application)=0; virtual int notify(QSharedPointer notification)=0; virtual void closeNotification(int id)=0; diff --git a/src/core/notification.cpp b/src/core/notification.cpp index a8a75c4..850476a 100644 --- a/src/core/notification.cpp +++ b/src/core/notification.cpp @@ -42,10 +42,12 @@ Notification::Notification(uint id): _notification(true) {} -Notification::Notification(Notification_Frontend *source,QString title,QString text,QString icon,int timeout,uint id): +Notification::Notification(Notification_Frontend *source, const QString &application, const QString &alert, const QString &title, const QString &text, const QString &icon, int timeout, uint id): _id(id), _timeout(timeout), _source(source), + _app(application), + _alert(alert), _title(title), _text(text), _icon(icon), diff --git a/src/core/notification.h b/src/core/notification.h index 5a7a8dd..165a0bb 100644 --- a/src/core/notification.h +++ b/src/core/notification.h @@ -32,7 +32,7 @@ public: static QString toPlainText(const QString &string); public: Notification(uint id=0); - Notification(class Notification_Frontend *source,QString title,QString text,QString icon,int timeout=10,uint id=0); + Notification(class Notification_Frontend *source,const QString &application,const QString &alert,const QString &title,const QString &text,const QString &icon,int timeout=10,uint id=0); QString toString() const; bool isNotification(); void setIsNotification(bool b); @@ -67,10 +67,10 @@ private: actions _actionInvoked; class Notification_Frontend *_source; QString _app; + QString _alert; QString _title; QString _text; - QString _icon; - QString _alert; + QString _icon; QVariantHash _hints; bool _notification; diff --git a/src/core/snoreserver.cpp b/src/core/snoreserver.cpp index 77bbb42..f985661 100644 --- a/src/core/snoreserver.cpp +++ b/src/core/snoreserver.cpp @@ -46,14 +46,9 @@ SnoreServer::SnoreServer(QSystemTrayIcon *trayIcon): QDir::temp().mkpath("SnoreNotify"); if(trayIcon!=NULL){ - _notificationBackend=new TrayIconNotifer(this,trayIcon); - _notyfier.insert(_notificationBackend->name(),_notificationBackend); - _primaryNotificationBackends.insert(_notificationBackend->name(),_notificationBackend); - connect(this,SIGNAL(notify(QSharedPointer)),_notificationBackend,SLOT(notify(QSharedPointer))); - _notificationBackend->notify(QSharedPointer(new Notification(NULL,"Welcome","Snore Notify succesfully registred "+_notificationBackend->name(),""))); + publicatePlugin(new TrayIconNotifer(this,trayIcon)); } } - void SnoreServer::publicatePlugin(const QString &fileName){ QPluginLoader loader(fileName); QObject *plugin = loader.instance(); @@ -61,20 +56,24 @@ void SnoreServer::publicatePlugin(const QString &fileName){ qDebug()<<"Failed loading plugin: "<(plugin); SnorePlugin *sp = dynamic_cast(plugin); if(sp==NULL){ std::cerr<<"Error:"<name()); + publicatePlugin(sp); +} + +void SnoreServer::publicatePlugin(SnorePlugin *plugin){ + + + QString pluginName(plugin->name()); qDebug()<<"Loading plugin: "<setSnore(this); + plugin->setSnore(this); Notification_Frontend *nf=qobject_cast(plugin); if(nf){ @@ -87,20 +86,18 @@ void SnoreServer::publicatePlugin(const QString &fileName){ if(nb){ qDebug()<isPrimaryNotificationBackend()){ - if(_notificationBackend){ - _notyfier.insert(pluginName,nb); - _primaryNotificationBackends.insert(pluginName,nb); - connect(this,SIGNAL(notify(QSharedPointer)),_notificationBackend,SLOT(notify(QSharedPointer))); + _primaryNotificationBackends.insert(pluginName,nb); + if(_notificationBackend==NULL){ + _notificationBackend=nb; } - _notificationBackend=nb; - _notificationBackend->notify(QSharedPointer(new Notification(NULL,"Welcome","Snore Notify succesfully registred "+pluginName,""))); - - }else{ - _notyfier.insert(pluginName,nb); - connect(this,SIGNAL(notify(QSharedPointer)),nb,SLOT(notify(QSharedPointer))); } + _notyfier.insert(pluginName,nb); + connect(this,SIGNAL(notify(QSharedPointer)),nb,SLOT(notify(QSharedPointer))); connect(this,SIGNAL(closeNotify(int)),nb,SLOT(closeNotification(int))); + connect(this,SIGNAL(applicationInitialized(Application*)),nb,SLOT(registerApplication(Application*))); nb->setSnore(this); + nb->notify(QSharedPointer(new Notification(NULL,"SnoreNotify","","Welcome","Snore Notify succesfully registred "+pluginName,""))); + } } @@ -130,28 +127,21 @@ void SnoreServer::notificationActionInvoked(QSharedPointer notific } } -void SnoreServer::addApplication(QSharedPointer application){ - _applications.insert(application->name,application); - emit applicationListChanged(); +void SnoreServer::addApplication(Application *application){ + _applications.insert(application->name(),application); } - -bool SnoreServer::applicationListAlertIsActive(const QString &applicationName,const QString &alertName){ - return _applications.contains(applicationName)&&_applications.value(applicationName)->alerts.contains(alertName) - &&!_applications.value(applicationName)->alerts.value(alertName)->active; -} - -void SnoreServer::addAlert(const QString &appName,const QString &alertName, const QString &alertTitle){ - _applications.value(appName)->addAlert(alertName,alertTitle); - emit applicationListChanged(); +void SnoreServer::applicationIsInitialized(Application *application){ + application->setInitialized(true); + emit applicationInitialized(application); } void SnoreServer::removeApplication(const QString& appName){ - _applications.take(appName).clear(); - emit applicationListChanged(); + emit applicationRemoved(_applications.value(appName)); + _applications.take(appName)->deleteLater(); } -const ApplicationsList &SnoreServer::aplicationList() const{ +const ApplicationsList &SnoreServer::aplications() const{ return _applications; } diff --git a/src/core/snoreserver.h b/src/core/snoreserver.h index 9d9f3fd..1026944 100644 --- a/src/core/snoreserver.h +++ b/src/core/snoreserver.h @@ -32,17 +32,17 @@ public: public: SnoreServer(class QSystemTrayIcon *trayIcon=0); void publicatePlugin(const QString &fileName); + void publicatePlugin(SnorePlugin *plugin); int broadcastNotification(QSharedPointer notification); void closeNotification(QSharedPointer notification); void notificationActionInvoked(QSharedPointer notification); - void addApplication(QSharedPointer application); - bool applicationListAlertIsActive(const QString &applicationName,const QString &alertName); - void addAlert(const QString &appName,const QString &alertName, const QString &alertTitle); + void addApplication(Application *application); + void applicationIsInitialized(Application* application); void removeApplication(const QString& appName); - const ApplicationsList &aplicationList() const; + const ApplicationsList &aplications() const; const QHash &primaryNotificationBackends() const; void setNotificationBackend(Notification_Backend *backend); @@ -61,7 +61,8 @@ private: signals: - void applicationListChanged(); + void applicationInitialized(Application*); + void applicationRemoved(Application*); void notify(QSharedPointer noti); void closeNotify(int id); diff --git a/src/core/trayiconnotifer.cpp b/src/core/trayiconnotifer.cpp index 2080149..2fe4ed3 100644 --- a/src/core/trayiconnotifer.cpp +++ b/src/core/trayiconnotifer.cpp @@ -8,6 +8,9 @@ TrayIconNotifer::TrayIconNotifer(SnoreServer *snore, QSystemTrayIcon *icon): _id(0) {} +void TrayIconNotifer::registerApplication(Application *application){ + +} int TrayIconNotifer::notify(QSharedPointer notification){ _trayIcon->showMessage(notification->title(),notification->text(),QSystemTrayIcon::NoIcon,notification->timeout()*1000); diff --git a/src/core/trayiconnotifer.h b/src/core/trayiconnotifer.h index ad0656f..df4bf51 100644 --- a/src/core/trayiconnotifer.h +++ b/src/core/trayiconnotifer.h @@ -12,6 +12,7 @@ public: bool isPrimaryNotificationBackend(){return true;} public slots: + void registerApplication(Application *application); int notify(QSharedPointer notification); void closeNotification(int id); diff --git a/src/plugins/freedesktopfrontend/freedesktopnotificationfrontend.cpp b/src/plugins/freedesktopfrontend/freedesktopnotificationfrontend.cpp index 22d2a1b..e633619 100644 --- a/src/plugins/freedesktopfrontend/freedesktopnotificationfrontend.cpp +++ b/src/plugins/freedesktopfrontend/freedesktopnotificationfrontend.cpp @@ -88,7 +88,7 @@ uint FreedesktopNotification_Frontend::Notify(const QString &app_name, uint repl icon=getImagefromHint(image); } - QSharedPointer noti(new Notification(this,summary,body,icon,timeout==-1?Notification::DefaultTimeout:timeout/1000,replaces_id)); + QSharedPointer noti(new Notification(this,app_name,"",summary,body,icon,timeout==-1?Notification::DefaultTimeout:timeout/1000,replaces_id)); return snore()->broadcastNotification(noti); } diff --git a/src/plugins/growl/growl_backend.cpp b/src/plugins/growl/growl_backend.cpp index b9957e0..0797ae5 100644 --- a/src/plugins/growl/growl_backend.cpp +++ b/src/plugins/growl/growl_backend.cpp @@ -25,17 +25,40 @@ Notification_Backend("Growl",snore), id(0) { const char *n[1] = { "SnoreNotification"}; - growl=new Growl(GROWL_TCP,NULL,"SnoreNotify",n,1); + growl = new Growl(GROWL_TCP,NULL,"SnoreNotify",n,1); + _applications.insert("SnoreNotify",growl); + } Growl_Backend::~Growl_Backend(){ delete growl; } +void Growl_Backend::registerApplication(Application *application){ + QList aList = application->alerts().values(); + int alertCount = application->alerts().count(); + char **n = new char*[alertCount]; + for (int i = 0 ; i < alertCount; ++i){ + QString name = aList.at(i)->name(); + n[i] = new char[name.length()]; + n[i] = name.toLatin1().data(); + } + + _applications.insert(application->name(),new Growl(GROWL_TCP,NULL,application->name().toLatin1().data(),(const char**)n,application->alerts().count())); + + for (int i = 0 ; i < alertCount; ++i){ + delete [] n[i]; + } + delete [] n; +} + int Growl_Backend::notify(QSharedPointer notification){ + Growl *g = _applications.value(notification->application()); + if(g==NULL) + g=growl; QString title=Notification::toPlainText(notification->title()); QString text=Notification::toPlainText(notification->text()); - qDebug()<Notify("SnoreNotification",title.toLatin1().data(),text.toLatin1().data(),NULL,notification->icon().toLatin1().data()); + qDebug()<application()<Notify(notification->application().toLatin1().data(),title.toLatin1().data(),text.toLatin1().data(),NULL,notification->icon().toLatin1().data()); return ++id; } diff --git a/src/plugins/growl/growl_backend.h b/src/plugins/growl/growl_backend.h index bf9c365..7eb26dd 100644 --- a/src/plugins/growl/growl_backend.h +++ b/src/plugins/growl/growl_backend.h @@ -17,6 +17,9 @@ #ifndef GROWL_BACKEND_H #define GROWL_BACKEND_H #include "core/interface.h" + + + class Growl_Backend:public Notification_Backend { Q_OBJECT @@ -28,9 +31,13 @@ public: private: uint id; class Growl *growl; + QHash _applications; public slots: + void registerApplication(Application *application); int notify(QSharedPointernotification); void closeNotification(int nr); }; + + #endif // GROWL_BACKEND_H diff --git a/src/plugins/snarl/SnarlInterface.h b/src/plugins/snarl/SnarlInterface.h index 1591174..1a26ec0 100644 --- a/src/plugins/snarl/SnarlInterface.h +++ b/src/plugins/snarl/SnarlInterface.h @@ -216,5 +216,4 @@ namespace Snarl { }; } - #endif // SNARL_INTERFACE diff --git a/src/plugins/snarl/snarl_backend.cpp b/src/plugins/snarl/snarl_backend.cpp index 1b97a26..e4d2fdf 100644 --- a/src/plugins/snarl/snarl_backend.cpp +++ b/src/plugins/snarl/snarl_backend.cpp @@ -37,36 +37,38 @@ Snarl_Backend::~Snarl_Backend(){ delete snarlInterface; } +void Snarl_Backend::registerApplication(Application *application){ + wchar_t *appName = toWchar(application->name()); + snarlInterface->RegisterApp(appName,L"",L""); + wprintf(L"Registering %s with Snarl.",appName); + + foreach(Alert *alert,application->alerts()){ + wchar_t *alertName = toWchar(alert->name()); + wprintf(L"Registering %s als snarl alert classSnarl.",alertName); + snarlInterface->RegisterAlert(appName,alertName); + delete [] alertName; + } + delete [] appName; +} + int Snarl_Backend::notify(QSharedPointernotification){ - QString qtitle(Notification::toPlainText(notification->title())); - QString qtext( Notification::toPlainText(notification->text())); - QString qicon(notification->icon()); - - wchar_t *title = new wchar_t[qtitle.length()+1]; - wchar_t *text = new wchar_t[qtext.length()+1]; - wchar_t *icon = new wchar_t[qicon.length()+1]; - - int i=0; - i=qtitle.toWCharArray(title); - title[i+1]=0; - i=qtext.toWCharArray(text); - text[i+1]=0; - i=qicon.toWCharArray(icon); - icon[i+1]=0; + wchar_t *title = toWchar(Notification::toPlainText(notification->title())); + wchar_t *text = toWchar(Notification::toPlainText(notification->text())); + wchar_t *icon = toWchar(notification->icon()); if(notification->id()==0){ wprintf(L"Calling SnarlMessage\n" - L"Title: %s\n" - L"Text:%s\n" - L"Timeout: %i\n" - L"Icon: %s\n",title,text,notification->timeout(),icon); + L"Title: \"%s\"\n" + L"Text: \"%s\"\n" + L"Timeout: \"%i\"\n" + L"Icon: \"%s\"\n",title,text,notification->timeout(),icon); return snarlInterface->ShowMessage(title,text,notification->timeout(), icon); }else{ //update message - wprintf(L"Updating SnarlMessage ID: %i\n" - L"Title: %s\n" - L"Text:%s\n" - L"Icon: %s\n",notification->id(),title,text,icon); + wprintf(L"Updating SnarlMessage ID: \"%i\"\n" + L"Title: \"%s\"\n" + L"Text: \"%s\"\n" + L"Icon: \"%s\"\n",notification->id(),title,text,icon); snarlInterface->UpdateMessage(notification->id(),title, text,icon); return notification->id(); } @@ -84,5 +86,10 @@ bool Snarl_Backend::eventFilter(QObject *obj, QEvent *event){ return true; } +wchar_t *Snarl_Backend::toWchar(const QString &string){ + wchar_t *wc = new wchar_t[string.length()+1]; + wc[string.toWCharArray(wc)] = 0; + return wc; +} #include "snarl_backend.moc" diff --git a/src/plugins/snarl/snarl_backend.h b/src/plugins/snarl/snarl_backend.h index 3ff8974..6e56009 100644 --- a/src/plugins/snarl/snarl_backend.h +++ b/src/plugins/snarl/snarl_backend.h @@ -24,18 +24,21 @@ class Snarl_Backend:public Notification_Backend Q_OBJECT Q_INTERFACES(Notification_Backend) public: - Snarl_Backend(class SnoreServer *snore=0); + Snarl_Backend(class SnoreServer *snore=0); ~Snarl_Backend(); bool isPrimaryNotificationBackend(){return true;} protected: - bool eventFilter(QObject *obj, QEvent *event); + bool eventFilter(QObject *obj, QEvent *event); private: + //returns a wchart_t aray has to deleted after use + wchar_t *toWchar(const QString &string); Snarl::SnarlInterface *snarlInterface; public slots: - int notify(QSharedPointernotification); - void closeNotification(int nr); + void registerApplication(Application *application); + int notify(QSharedPointernotification); + void closeNotification(int nr); }; diff --git a/src/plugins/snarlnetwork/parser.cpp b/src/plugins/snarlnetwork/parser.cpp index 4f2e916..fcd3e9b 100644 --- a/src/plugins/snarlnetwork/parser.cpp +++ b/src/plugins/snarlnetwork/parser.cpp @@ -50,7 +50,7 @@ Parser::Parser(SnarlNetworkFrontend *snarl):snarl(snarl) SnarlNotification Parser::parse(QString &msg,QTcpSocket* client){ - msg=msg.trimmed(); + qDebug()<(new Notification(snarl,title,text,icon,timeout)); + sNotification.notification=QSharedPointer(new Notification(snarl,app,alert,title,text,icon,timeout)); sNotification.notification->setIsNotification(false); sNotification.notification->insertHint("SnarlIcon",sntpIcon); switch(action){ - case NOTIFICATION: - if(snarl->snore()->applicationListAlertIsActive(sNotification.notification->application(),sNotification.notification->alert())) + case NOTIFICATION:{ + qDebug()<application(); + Application * appl=snarl->snore()->aplications().value(sNotification.notification->application()); + if(!appl->isInitialized()){ + snarl->snore()->applicationIsInitialized(appl); + } + + if(!appl->alerts().value(sNotification.notification->alert())->isActive()) + break; + sNotification.notification->setIsNotification(true); + return sNotification; break; - sNotification.notification->setIsNotification(true); - return sNotification; - break; + } case ADD_CLASS: if(sNotification.notification->alert().isEmpty()){ qDebug()<<"Error registering alert with empty name"; @@ -129,13 +136,12 @@ SnarlNotification Parser::parse(QString &msg,QTcpSocket* client){ } if(title.isEmpty()) title = alert; - snarl->snore()->addAlert(sNotification.notification->application(),alert,title); + snarl->snore()->aplications().value(sNotification.notification->application())->addAlert(new Alert(alert,title)); break; case REGISTER: - qDebug()<snore()->aplicationList().keys(); - if(!snarl->snore()->aplicationList().contains(sNotification.notification->application())&&!sNotification.notification->application().isEmpty()){ - snarl->snore()->addApplication(QSharedPointer(new Application(sNotification.notification->application()))); - } + if(!snarl->snore()->aplications().contains(sNotification.notification->application())&&!sNotification.notification->application().isEmpty()){ + snarl->snore()->addApplication(new Application(sNotification.notification->application())); + } else qDebug()<application()<<"already registred"; break; @@ -147,7 +153,6 @@ SnarlNotification Parser::parse(QString &msg,QTcpSocket* client){ sNotification.vailid=false; break; } - qDebug()<insertHint("SNaction",sNotification.action); return sNotification; } diff --git a/src/plugins/snarlnetwork/snarlnetwork.cpp b/src/plugins/snarlnetwork/snarlnetwork.cpp index 0318689..28d962b 100644 --- a/src/plugins/snarlnetwork/snarlnetwork.cpp +++ b/src/plugins/snarlnetwork/snarlnetwork.cpp @@ -69,7 +69,10 @@ void SnarlNetworkFrontend::handleMessages(){ QString out("SNP/1.1/0/OK"); QTcpSocket *client=qobject_cast(sender()); QStringList incommings(QString::fromUtf8(client->readAll()).split("\r\n")); - foreach(QString s,incommings){ + foreach(const QString &msg,incommings){ + QString s=msg.trimmed(); + if(s == "") + continue; SnarlNotification noti=parser->parse(s,client); notifications.insert(noti.notification->id(),noti); if(!noti.vailid) diff --git a/src/plugins/webposter/webposter.cpp b/src/plugins/webposter/webposter.cpp index 7cb2b56..c962fc4 100644 --- a/src/plugins/webposter/webposter.cpp +++ b/src/plugins/webposter/webposter.cpp @@ -29,6 +29,10 @@ Notification_Backend("WebPoster",snore) manager=new QNetworkAccessManager(this); } +void WebPoster::registerApplication(Application *application){ + +} + int WebPoster::notify(QSharedPointernotification){ QByteArray byte(Utils::notificationToSNTPString(notification).toLatin1().data()); QUrl url("http://www.pro-zeit.ch/index.php"); diff --git a/src/plugins/webposter/webposter.h b/src/plugins/webposter/webposter.h index 961bd16..de89b78 100644 --- a/src/plugins/webposter/webposter.h +++ b/src/plugins/webposter/webposter.h @@ -29,6 +29,7 @@ public: bool isPrimaryNotificationBackend(){return false;} public slots: + void registerApplication(Application *application); int notify(QSharedPointernotification); void closeNotification(int id);