From 18adeb032688a3e18e2e8cfc3f3739581139f051 Mon Sep 17 00:00:00 2001 From: theonering Date: Sun, 28 Feb 2010 15:29:17 +0100 Subject: [PATCH] fixed snarl network --- src/core/notification.cpp | 11 +++- src/core/notification.h | 17 +++-- src/core/snoreserver.cpp | 7 ++- src/core/snoreserver.h | 2 +- .../freedesktopnotificationfrontend.cpp | 11 ++-- .../freedesktopnotification_backend.cpp | 10 +-- .../freedesktopnotification_backend.h | 2 +- src/plugins/snarl/SnarlInterface.h | 2 +- src/plugins/snarl/snarl_backend.cpp | 10 +-- src/plugins/snarlnetwork/parser.cpp | 63 ++++++++++--------- src/plugins/snarlnetwork/parser.h | 2 +- src/plugins/snarlnetwork/snarlnetwork.cpp | 20 +++--- 12 files changed, 84 insertions(+), 73 deletions(-) diff --git a/src/core/notification.cpp b/src/core/notification.cpp index 0bc16b5..b8dc160 100644 --- a/src/core/notification.cpp +++ b/src/core/notification.cpp @@ -5,9 +5,10 @@ +int Notification::DefaultTimeout=10; -Notification::Notification():source("none"),timeout(10),notification(true){} -Notification::Notification(QString source,QString title,QString text,QString icon,int timeout):source(source),title(title),text(text),timeout(timeout),icon(icon),notification(true) +Notification::Notification(uint id):source("none"),timeout(10),id(id),notification(true){} +Notification::Notification(QString source,QString title,QString text,QString icon,int timeout,uint id):source(source),title(title),text(text),timeout(timeout),id(id),icon(icon),notification(true) { } @@ -19,6 +20,12 @@ bool Notification::isNotification(){ return notification; } +void Notification::setIsNotification(bool b){ + notification=b; +} +uint Notification::getID(){ + return id; +} QString Notification::toSnalrString()const{ QString out("type=SNP#?version=1.1"); diff --git a/src/core/notification.h b/src/core/notification.h index ed7e8ce..24aa904 100644 --- a/src/core/notification.h +++ b/src/core/notification.h @@ -13,7 +13,9 @@ class SNORE_EXPORT Notification:public QObject { Q_OBJECT + friend class SnoreServer; public: + static int DefaultTimeout; static inline QString toPlainText(const QString &string){ if(!Qt::mightBeRichText ( string))return string; QTextEdit te; @@ -21,9 +23,10 @@ public: return te.toPlainText(); }; public: - Notification(); - Notification(QString source,QString title,QString text,QString icon,int timeout); + Notification(uint id=0); + Notification(QString source,QString title,QString text,QString icon,int timeout=10,uint id=0); bool isNotification(); + void setIsNotification(bool b); QString toSnalrString()const; enum actions{ @@ -44,17 +47,13 @@ public: int timeout; void setIcon(const QString &icon){this->icon=icon; } QString getIcon(); - QVariantHash hints; - - int id; + uint getID(); - - - -private: +private: + uint id; QString icon; bool notification; diff --git a/src/core/snoreserver.cpp b/src/core/snoreserver.cpp index 49908f6..fb258d8 100644 --- a/src/core/snoreserver.cpp +++ b/src/core/snoreserver.cpp @@ -2,6 +2,7 @@ #include #include "notification.h" #include +#include QString const SnoreServer::snoreTMP=QDir::temp().path()+"/SnoreNotify/"; @@ -57,7 +58,9 @@ int SnoreServer::broadcastNotification(QSharedPointer notification emit notify(notification); qDebug()<<"Broadcasting notification:"<toSnalrString(); if(primaryNotificationBackend!=NULL){ - return primaryNotificationBackend->notify(notification); + notification->id=primaryNotificationBackend->notify(notification); + std::cout<<"Notification ID: "<id).toLatin1().data()<id; } return -1; } @@ -83,7 +86,7 @@ void SnoreServer::addApplication(QSharedPointer application){ } -bool SnoreServer::applicationListConontainsAlert(const QString &applicationName,const QString &alertName){ +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; } diff --git a/src/core/snoreserver.h b/src/core/snoreserver.h index 17ae383..c89fd16 100644 --- a/src/core/snoreserver.h +++ b/src/core/snoreserver.h @@ -31,7 +31,7 @@ public: void notificationActionInvoked(QSharedPointer notification); void addApplication(QSharedPointer application); - bool applicationListConontainsAlert(const QString &applicationName,const QString &alertName); + bool applicationListAlertIsActive(const QString &applicationName,const QString &alertName); void addAlert(const QString &appName,const QString &alertName, const QString &alertTitle); void removeApplication(const QString& appName); diff --git a/src/plugins/freedesktopfrontend/freedesktopnotificationfrontend.cpp b/src/plugins/freedesktopfrontend/freedesktopnotificationfrontend.cpp index 3415305..6052059 100644 --- a/src/plugins/freedesktopfrontend/freedesktopnotificationfrontend.cpp +++ b/src/plugins/freedesktopfrontend/freedesktopnotificationfrontend.cpp @@ -22,7 +22,7 @@ FreedesktopNotification_Frontend::~FreedesktopNotification_Frontend(){ } void FreedesktopNotification_Frontend::actionInvoked(QSharedPointernotification){ - emit ActionInvoked(notification->id,QString::number(notification->actionInvoked)); + emit ActionInvoked(notification->getID(),QString::number(notification->actionInvoked)); } void FreedesktopNotification_Frontend::notificationClosed(QSharedPointernotification){ @@ -40,7 +40,7 @@ void FreedesktopNotification_Frontend::notificationClosed(QSharedPointerid,reason); + emit NotificationClosed(notification->getID(),reason); } QString FreedesktopNotification_Frontend::getImagefromHint(const FreedesktopImageHint &img){ @@ -68,16 +68,15 @@ uint FreedesktopNotification_Frontend::Notify(const QString &app_name, uint repl hints["image_data"].value()>>image; icon=getImagefromHint(image); } - QSharedPointer noti(new Notification(property("name").value(),summary,body,icon,timeout)); - noti->id=replaces_id; + + QSharedPointer noti(new Notification(property("name").value(),summary,body,icon,timeout==-1?Notification::DefaultTimeout:timeout/1000,replaces_id)); return getSnore()->broadcastNotification(noti); } void FreedesktopNotification_Frontend::CloseNotification(uint id){ - QSharedPointer n(new Notification()); - n->id=id; + QSharedPointer n(new Notification(id)); getSnore()->closeNotification(n); } diff --git a/src/plugins/freedesktopnotification/freedesktopnotification_backend.cpp b/src/plugins/freedesktopnotification/freedesktopnotification_backend.cpp index b3a2798..acdcf68 100644 --- a/src/plugins/freedesktopnotification/freedesktopnotification_backend.cpp +++ b/src/plugins/freedesktopnotification/freedesktopnotification_backend.cpp @@ -35,11 +35,11 @@ fNotification::fNotification(FreedesktopNotification_Backend* parent):parent(par -int fNotification::send(){ +uint fNotification::send(){ qDebug("Sending a notification"); FreedesktopNotification n(notification.data()); QDBusMessage recive=notificationInterface.call("Notify", QVariant::fromValue(n)); - n.notification->id=recive.arguments().last().toInt(); + uint id=recive.arguments().last().toInt(); selfdistruct.setParent(this); selfdistruct.setSingleShot(true); @@ -48,11 +48,11 @@ int fNotification::send(){ QDBusConnection::sessionBus().connect("org.freedesktop.Notifications","/org/freedesktop/Notifications","org.freedesktop.Notifications","ActionInvoked",this,SLOT(action(uint,QString))); if(getVendor()=="GNOME") QDBusConnection::sessionBus().connect("org.freedesktop.Notifications","/org/freedesktop/Notifications","org.freedesktop.Notifications","NotificationClosed",this,SLOT(closed(uint,uint))); - return n.notification->id; + return id; } void fNotification::action(const uint &id, const QString &action_key){ - if(id!=notification->id)return; + if(id!=notification->getID())return; close(); qDebug()<id)return; + if(id!=notification->getID())return; close(); if(reason==1) notification->actionInvoked=Notification::TIMED_OUT; diff --git a/src/plugins/freedesktopnotification/freedesktopnotification_backend.h b/src/plugins/freedesktopnotification/freedesktopnotification_backend.h index a64f89b..fac6ff8 100644 --- a/src/plugins/freedesktopnotification/freedesktopnotification_backend.h +++ b/src/plugins/freedesktopnotification/freedesktopnotification_backend.h @@ -34,7 +34,7 @@ private: public: fNotification(FreedesktopNotification_Backend* parent); - int send(); + uint send(); QSharedPointer notification; private: diff --git a/src/plugins/snarl/SnarlInterface.h b/src/plugins/snarl/SnarlInterface.h index 137d597..adcded5 100644 --- a/src/plugins/snarl/SnarlInterface.h +++ b/src/plugins/snarl/SnarlInterface.h @@ -46,7 +46,7 @@ namespace Snarl { M_OK = 0x00000000, M_OUT_OF_MEMORY = 0x80000002, M_TIMED_OUT = 0x8000000A - }; + } ; enum SNARL_COMMANDS { SNARL_SHOW = 1, diff --git a/src/plugins/snarl/snarl_backend.cpp b/src/plugins/snarl/snarl_backend.cpp index 587c194..5a7b16d 100644 --- a/src/plugins/snarl/snarl_backend.cpp +++ b/src/plugins/snarl/snarl_backend.cpp @@ -19,16 +19,16 @@ Snarl_Backend::Snarl_Backend() int Snarl_Backend::notify(QSharedPointernotification){ - int timeout=notification->timeout>=0?notification->timeout/1000:10; - if(notification->id==0){ + int timeout=notification->timeout>=0?notification->timeout:10; + if(notification->getID()==0){ QString title=Notification::toPlainText(notification->title); QString text=Notification::toPlainText(notification->text); - qDebug()<<"Calling Snarl"<getIcon(); + std::cout<<"Calling Snarl"<getIcon().toLocal8Bit().data()<ShowMessage(title.toLocal8Bit().data(), text.toLocal8Bit().data(),timeout, notification->getIcon().toLocal8Bit().data()); }else{ //update message - snarlInterface->UpdateMessage(LONG32(notification->id),notification->title.toLocal8Bit().data(), notification->text.toLocal8Bit().data(),notification->getIcon().toLocal8Bit().data()); - return notification->id; + snarlInterface->UpdateMessage(LONG32(notification->getID()),notification->title.toLocal8Bit().data(), notification->text.toLocal8Bit().data(),notification->getIcon().toLocal8Bit().data()); + return notification->getID(); } } diff --git a/src/plugins/snarlnetwork/parser.cpp b/src/plugins/snarlnetwork/parser.cpp index 379bf51..2f8f1fc 100644 --- a/src/plugins/snarlnetwork/parser.cpp +++ b/src/plugins/snarlnetwork/parser.cpp @@ -28,7 +28,7 @@ Parser::Parser(SnarlNetworkFrontend *snarl):snarl(snarl) } - SnarlNotification Parser::parse(QString &msg,QTcpSocket* client){ +SnarlNotification Parser::parse(QString &msg,QTcpSocket* client){ msg=msg.trimmed(); SnarlNotification sNotification; @@ -36,6 +36,7 @@ Parser::Parser(SnarlNetworkFrontend *snarl):snarl(snarl) sNotification.vailid=true; sNotification.notification=QSharedPointer(new Notification()); sNotification.clientSocket=client; + sNotification.notification->setIsNotification(false); snpTypes action(ERROR); if(msg.startsWith("GET ")){ @@ -84,8 +85,9 @@ Parser::Parser(SnarlNetworkFrontend *snarl):snarl(snarl) switch(action){ case NOTIFICATION: - if(snarl->getSnore()->applicationListConontainsAlert(sNotification.notification->app,sNotification.notification->alert)) + if(snarl->getSnore()->applicationListAlertIsActive(sNotification.notification->app,sNotification.notification->alert)) break; + sNotification.notification->setIsNotification(true); return sNotification; break; case ADD_CLASS: @@ -118,40 +120,39 @@ Parser::Parser(SnarlNetworkFrontend *snarl):snarl(snarl) return sNotification; } - QString Parser::downloadIcon(const QString &address){ - if(address=="") - return ""; - if(address.startsWith("file://")) - return QString(address.mid(7)); - QByteArray arr=address.toUtf8(); - QUrl url=QUrl::fromEncoded(arr); +QString Parser::downloadIcon(const QString &address){ + if(address=="") + return ""; + if(address.startsWith("file://")) + return QString(address.mid(7)); + QByteArray arr=address.toUtf8(); + QUrl url=QUrl::fromEncoded(arr); - QCryptographicHash hash(QCryptographicHash::Md5); - hash.addData(arr); - QString filename=QDir::temp().path()+"/SnoreNotify/"+hash.result().toHex()+address.mid(address.lastIndexOf(".")-1); - QFile file(filename); - if(file.exists()) - return filename; + QCryptographicHash hash(QCryptographicHash::Md5); + hash.addData(arr); + QString filename=QDir::temp().path()+"/SnoreNotify/"+hash.result().toHex()+address.mid(address.lastIndexOf(".")-1); + QFile file(filename); + if(file.exists()) + return filename; - QNetworkReply * reply=download(url); + QByteArray reply=download(url); - file.open(QIODevice::WriteOnly); - file.write(reply->readAll()); + file.open(QIODevice::WriteOnly); + file.write(reply); - reply->deleteLater(); - return filename; + return filename; - } +} - QNetworkReply* Parser::download(const QUrl &address){ - QNetworkAccessManager manager; - QEventLoop loop; - QNetworkRequest request(address); - request.setRawHeader("User-Agent", "SnoreNotify"); - QNetworkReply *reply=manager.get(request); - QObject::connect(reply, SIGNAL(finished()), &loop, SLOT(quit())); - loop.exec(); - return reply; - } +QByteArray Parser::download(const QUrl &address){ + QNetworkAccessManager manager; + QEventLoop loop; + QNetworkRequest request(address); + request.setRawHeader("User-Agent", "SnoreNotify"); + QNetworkReply *reply=manager.get(request); + QObject::connect(reply, SIGNAL(finished()), &loop, SLOT(quit())); + loop.exec(); + return reply->readAll(); +} //#include "parser.moc" diff --git a/src/plugins/snarlnetwork/parser.h b/src/plugins/snarlnetwork/parser.h index 6a38d3d..8e18525 100644 --- a/src/plugins/snarlnetwork/parser.h +++ b/src/plugins/snarlnetwork/parser.h @@ -11,7 +11,7 @@ class Parser{ public: - static class QNetworkReply* download(const QUrl &address); + static class QByteArray download(const QUrl &address); public: Parser(class SnarlNetworkFrontend* snarl); diff --git a/src/plugins/snarlnetwork/snarlnetwork.cpp b/src/plugins/snarlnetwork/snarlnetwork.cpp index 1a40936..1fee85d 100644 --- a/src/plugins/snarlnetwork/snarlnetwork.cpp +++ b/src/plugins/snarlnetwork/snarlnetwork.cpp @@ -18,14 +18,14 @@ SnarlNetworkFrontend::SnarlNetworkFrontend():parser(this){ void SnarlNetworkFrontend::actionInvoked(QSharedPointernotification){ //TODO:fix callback - SnarlNotification sn=notifications.value(notification->id); + SnarlNotification sn=notifications.value(notification->getID()); if(notification->actionInvoked==1) callback(sn,"SNP/1.1/304/Notification acknowledged/"); else if(notification->actionInvoked==2) callback(sn,"SNP/1.1/302/Notification cancelled/"); } void SnarlNetworkFrontend::notificationClosed(QSharedPointernotification){ - SnarlNotification sn=notifications.value(notification->id); + SnarlNotification sn=notifications.value(notification->getID()); if(notification->actionInvoked==Notification::TIMED_OUT) callback(sn,"SNP/1.1/303/Notification timed out/"); else @@ -44,16 +44,18 @@ void SnarlNetworkFrontend::handleMessages(){ QStringList incommings(QString::fromUtf8(client->readAll()).split("\r\n")); foreach(QString s,incommings){ SnarlNotification noti=parser.parse(s,client); - notifications.insert(noti.notification->id,noti); + notifications.insert(noti.notification->getID(),noti); if(!noti.vailid) continue; - int notificationNR=getSnore()->broadcastNotification(noti.notification); - if(notificationNR!=-1){ - out+="/"+QString::number(notificationNR)+"\r\n"; + if(noti.notification->isNotification()){ + getSnore()->broadcastNotification(noti.notification); + if(noti.notification->getID()!=0){ + out+="/"+QString::number(noti.notification->getID())+"\r\n"; + } }else{ out+="\r\n"; } - client->write(out.toUtf8()); + client->write(out.toLatin1()); if(noti.httpClient){ client->disconnectFromHost(); client->waitForDisconnected(); @@ -68,9 +70,9 @@ void SnarlNetworkFrontend::clientDisconnecd(){ } void SnarlNetworkFrontend::callback(const SnarlNotification &sn,QString msg){ - notifications.remove(sn.notification->id); + notifications.remove(sn.notification->getID()); if(sn.clientSocket!=NULL&&!msg.isEmpty()){ - msg+=QString::number(sn.notification->id); + msg+=QString::number(sn.notification->getID()); qDebug()<write(msg.toAscii()+"\n"); sn.clientSocket->flush();