big refactoring, cleaning up,added register for notification backends

This commit is contained in:
Patrick von Reth 2010-07-22 15:19:48 +02:00
parent 97c405df58
commit 6652fd1add
19 changed files with 224 additions and 118 deletions

View File

@ -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<Alert>(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"

View File

@ -19,31 +19,46 @@
#include "snore_exports.h"
#include <QHash>
#include <QSharedPointer>
typedef QHash<QString,QSharedPointer<class Application> > ApplicationsList ;
typedef QHash<QString,QSharedPointer<class Alert> > AlertList;
typedef QHash<QString,class Application*> ApplicationsList ;
typedef QHash<QString,class Alert*> 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;
};

View File

@ -19,6 +19,8 @@
#include "snore_exports.h"
#include "notification.h"
#include <QSharedPointer>
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> notification)=0;
virtual void closeNotification(int id)=0;

View File

@ -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),

View File

@ -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;

View File

@ -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<Notification>)),_notificationBackend,SLOT(notify(QSharedPointer<Notification>)));
_notificationBackend->notify(QSharedPointer<Notification>(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: "<<loader.errorString();
return;
}
// SnorePlugin *sp = qobject_cast<SnorePlugin*>(plugin);
SnorePlugin *sp = dynamic_cast<SnorePlugin*>(plugin);
if(sp==NULL){
std::cerr<<"Error:"<<fileName.toLatin1().data()<<"is not a snarl plugin"<<std::endl ;
return;
}
QString pluginName(sp->name());
publicatePlugin(sp);
}
void SnoreServer::publicatePlugin(SnorePlugin *plugin){
QString pluginName(plugin->name());
qDebug()<<"Loading plugin: "<<pluginName;
plugins.insert(pluginName,sp);
plugins.insert(pluginName,plugin);
qDebug()<<pluginName<<"is a SnorePlugin";
sp->setSnore(this);
plugin->setSnore(this);
Notification_Frontend *nf=qobject_cast<Notification_Frontend*>(plugin);
if(nf){
@ -87,20 +86,18 @@ void SnoreServer::publicatePlugin(const QString &fileName){
if(nb){
qDebug()<<pluginName<<"is a Notification_Backend";
if(nb->isPrimaryNotificationBackend()){
if(_notificationBackend){
_notyfier.insert(pluginName,nb);
_primaryNotificationBackends.insert(pluginName,nb);
connect(this,SIGNAL(notify(QSharedPointer<Notification>)),_notificationBackend,SLOT(notify(QSharedPointer<Notification>)));
_primaryNotificationBackends.insert(pluginName,nb);
if(_notificationBackend==NULL){
_notificationBackend=nb;
}
_notificationBackend=nb;
_notificationBackend->notify(QSharedPointer<Notification>(new Notification(NULL,"Welcome","Snore Notify succesfully registred "+pluginName,"")));
}else{
_notyfier.insert(pluginName,nb);
connect(this,SIGNAL(notify(QSharedPointer<Notification>)),nb,SLOT(notify(QSharedPointer<Notification>)));
}
_notyfier.insert(pluginName,nb);
connect(this,SIGNAL(notify(QSharedPointer<Notification>)),nb,SLOT(notify(QSharedPointer<Notification>)));
connect(this,SIGNAL(closeNotify(int)),nb,SLOT(closeNotification(int)));
connect(this,SIGNAL(applicationInitialized(Application*)),nb,SLOT(registerApplication(Application*)));
nb->setSnore(this);
nb->notify(QSharedPointer<Notification>(new Notification(NULL,"SnoreNotify","","Welcome","Snore Notify succesfully registred "+pluginName,"")));
}
}
@ -130,28 +127,21 @@ void SnoreServer::notificationActionInvoked(QSharedPointer<Notification> notific
}
}
void SnoreServer::addApplication(QSharedPointer<Application> 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;
}

View File

@ -32,17 +32,17 @@ public:
public:
SnoreServer(class QSystemTrayIcon *trayIcon=0);
void publicatePlugin(const QString &fileName);
void publicatePlugin(SnorePlugin *plugin);
int broadcastNotification(QSharedPointer<Notification> notification);
void closeNotification(QSharedPointer<Notification> notification);
void notificationActionInvoked(QSharedPointer<Notification> notification);
void addApplication(QSharedPointer<Application> 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<QString,Notification_Backend*> &primaryNotificationBackends() const;
void setNotificationBackend(Notification_Backend *backend);
@ -61,7 +61,8 @@ private:
signals:
void applicationListChanged();
void applicationInitialized(Application*);
void applicationRemoved(Application*);
void notify(QSharedPointer<Notification> noti);
void closeNotify(int id);

View File

@ -8,6 +8,9 @@ TrayIconNotifer::TrayIconNotifer(SnoreServer *snore, QSystemTrayIcon *icon):
_id(0)
{}
void TrayIconNotifer::registerApplication(Application *application){
}
int TrayIconNotifer::notify(QSharedPointer<Notification> notification){
_trayIcon->showMessage(notification->title(),notification->text(),QSystemTrayIcon::NoIcon,notification->timeout()*1000);

View File

@ -12,6 +12,7 @@ public:
bool isPrimaryNotificationBackend(){return true;}
public slots:
void registerApplication(Application *application);
int notify(QSharedPointer<Notification> notification);
void closeNotification(int id);

View File

@ -88,7 +88,7 @@ uint FreedesktopNotification_Frontend::Notify(const QString &app_name, uint repl
icon=getImagefromHint(image);
}
QSharedPointer<Notification> noti(new Notification(this,summary,body,icon,timeout==-1?Notification::DefaultTimeout:timeout/1000,replaces_id));
QSharedPointer<Notification> noti(new Notification(this,app_name,"",summary,body,icon,timeout==-1?Notification::DefaultTimeout:timeout/1000,replaces_id));
return snore()->broadcastNotification(noti);
}

View File

@ -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<Alert*> 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> 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()<<title<<text;
growl->Notify("SnoreNotification",title.toLatin1().data(),text.toLatin1().data(),NULL,notification->icon().toLatin1().data());
qDebug()<<notification->application()<<title<<text;
g->Notify(notification->application().toLatin1().data(),title.toLatin1().data(),text.toLatin1().data(),NULL,notification->icon().toLatin1().data());
return ++id;
}

View File

@ -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<QString,class Growl*> _applications;
public slots:
void registerApplication(Application *application);
int notify(QSharedPointer<Notification>notification);
void closeNotification(int nr);
};
#endif // GROWL_BACKEND_H

View File

@ -216,5 +216,4 @@ namespace Snarl {
};
}
#endif // SNARL_INTERFACE

View File

@ -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(QSharedPointer<Notification>notification){
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"

View File

@ -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(QSharedPointer<Notification>notification);
void closeNotification(int nr);
void registerApplication(Application *application);
int notify(QSharedPointer<Notification>notification);
void closeNotification(int nr);
};

View File

@ -50,7 +50,7 @@ Parser::Parser(SnarlNetworkFrontend *snarl):snarl(snarl)
SnarlNotification Parser::parse(QString &msg,QTcpSocket* client){
msg=msg.trimmed();
qDebug()<<msg;
SnarlNotification sNotification;
sNotification.httpClient=false;
@ -110,18 +110,25 @@ SnarlNotification Parser::parse(QString &msg,QTcpSocket* client){
}
}
sNotification.notification=QSharedPointer<Notification>(new Notification(snarl,title,text,icon,timeout));
sNotification.notification=QSharedPointer<Notification>(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()<<sNotification.notification->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()<<snarl->snore()->aplicationList().keys();
if(!snarl->snore()->aplicationList().contains(sNotification.notification->application())&&!sNotification.notification->application().isEmpty()){
snarl->snore()->addApplication(QSharedPointer<Application>(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()<<sNotification.notification->application()<<"already registred";
break;
@ -147,7 +153,6 @@ SnarlNotification Parser::parse(QString &msg,QTcpSocket* client){
sNotification.vailid=false;
break;
}
qDebug()<<Utils::notificationToSNTPString(sNotification.notification);
sNotification.notification->insertHint("SNaction",sNotification.action);
return sNotification;
}

View File

@ -69,7 +69,10 @@ void SnarlNetworkFrontend::handleMessages(){
QString out("SNP/1.1/0/OK");
QTcpSocket *client=qobject_cast<QTcpSocket*>(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)

View File

@ -29,6 +29,10 @@ Notification_Backend("WebPoster",snore)
manager=new QNetworkAccessManager(this);
}
void WebPoster::registerApplication(Application *application){
}
int WebPoster::notify(QSharedPointer<Notification>notification){
QByteArray byte(Utils::notificationToSNTPString(notification).toLatin1().data());
QUrl url("http://www.pro-zeit.ch/index.php");

View File

@ -29,6 +29,7 @@ public:
bool isPrimaryNotificationBackend(){return false;}
public slots:
void registerApplication(Application *application);
int notify(QSharedPointer<Notification>notification);
void closeNotification(int id);