diff --git a/src/core/application_p.cpp b/src/core/application_p.cpp index 5fd54dd..0e88f60 100644 --- a/src/core/application_p.cpp +++ b/src/core/application_p.cpp @@ -25,6 +25,7 @@ ApplicationData::ApplicationData(const QString &name, const Icon &icon): m_name(name), m_icon(icon) { + Q_ASSERT_X(!name.isEmpty(),Q_FUNC_INFO, "invalid name detected"); } ApplicationData::~ApplicationData() diff --git a/src/core/snore.cpp b/src/core/snore.cpp index cd023a6..f53bd6c 100644 --- a/src/core/snore.cpp +++ b/src/core/snore.cpp @@ -124,6 +124,7 @@ void SnoreCore::registerApplication(const Application &application) Q_D(SnoreCore); if(!d->m_applications.contains(application.name())) { + qDebug() << Q_FUNC_INFO << "Registering Application:" << application; d->m_applications.insert ( application.name(),application ); emit d->applicationRegistered ( application ); } diff --git a/src/plugins/backends/snarl/snarl.cpp b/src/plugins/backends/snarl/snarl.cpp index 9462be5..620a577 100644 --- a/src/plugins/backends/snarl/snarl.cpp +++ b/src/plugins/backends/snarl/snarl.cpp @@ -193,7 +193,8 @@ void SnarlBackend::slotRegisterApplication(const Application &application){ } } -void SnarlBackend::slotDeregisterApplication(const Application &application){ +void SnarlBackend::slotDeregisterApplication(const Application &application) +{ if(!m_applications.contains(application.name())) { qDebug() << Q_FUNC_INFO << "Unknown apllication: " << application.name(); diff --git a/src/plugins/frontends/snarlnetwork/parser.cpp b/src/plugins/frontends/snarlnetwork/parser.cpp index 58c7a59..7e1682f 100644 --- a/src/plugins/frontends/snarlnetwork/parser.cpp +++ b/src/plugins/frontends/snarlnetwork/parser.cpp @@ -54,44 +54,48 @@ Parser::Parser(SnarlNetworkFrontend *snarl): } -SnarlNotification Parser::parse(QString &msg,QTcpSocket* client){ - SnarlNotification sNotification; - sNotification.httpClient=false; - sNotification.vailid=true; - sNotification.clientSocket=client; - sNotification.isNotification = false; - +bool Parser::parse(Notification &sNotification,const QString &msg,QTcpSocket* client) +{ + qDebug() << Q_FUNC_INFO << msg; + QStringList splitted; + bool isHttp = false; snpTypes action(ERROR); - if(msg.startsWith("GET ")){ - msg=msg.mid(msg.indexOf("/")+1); - msg=msg.mid(0,msg.indexOf(" ")); - QByteArray dat(QByteArray::fromBase64(msg.toLatin1().data())); - msg=QString(dat); + if(msg.startsWith("GET ")) + { + QString tmp = msg.mid(msg.indexOf("/")+1); + tmp = tmp.mid(0,tmp.indexOf(" ")); + tmp = QString(QByteArray::fromBase64(tmp.toLatin1().data())); qDebug()<<"Notification from a browser"<m_applications.contains(appName)) + + if(snarl->m_applications.contains(client)) { - app = snarl->m_applications[appName]; - } - else - { - app = Application(appName, icon); + app = snarl->m_applications[client]; } - if(app.alerts().contains(alertName)) + + + if(!alertName.isEmpty() && app.isValid()) { - alert = app.alerts()[alertName]; - } - else - { - if(title.isEmpty()) + if(app.alerts().contains(alertName)) { - alert = Alert(alertName, alertName); - } - else - { - alert = Alert(alertName, title); + alert = app.alerts()[alertName]; } } - sNotification.notification = Notification(app,alert,title,text,icon,timeout); - sNotification.notification.data()->setSource(snarl); + switch(action) { case NOTIFICATION: { - qDebug() << sNotification.notification.application(); - const Application &appl = sNotification.notification.application(); - if(!snarl->snore()->aplications().contains(appl.name())) + if(!snarl->snore()->aplications().contains(app.name())) { - snarl->snore()->registerApplication(appl); + snarl->snore()->registerApplication(app); } - if(!sNotification.notification.alert().isActive()) + if(!alert.isActive()) { break; } - sNotification.isNotification = true; - return sNotification; + sNotification = Notification(app,alert,title,text,icon,timeout); + sNotification.data()->setSource(snarl); + sNotification.hints().setValue("snarl_clientSocket", qVariantFromValue(client)); + sNotification.hints().setValue("snarl_isHttpCLient", isHttp); break; } case ADD_CLASS: - if(!sNotification.notification.alert().isValid()) + if(alertName.isEmpty()) { qDebug()<<"Error registering alert with empty name"; break; } - snarl->m_applications[appName].addAlert(sNotification.notification.alert()); - break; - case REGISTER: - if(sNotification.notification.application().isValid() && !snarl->m_applications.contains(sNotification.notification.application().name())) + if(title.isEmpty()) { - snarl->m_applications.insert(sNotification.notification.application().name(), sNotification.notification.application()); + alert = Alert(alertName, alertName, icon); } else { - qDebug()<m_applications.contains(client)) + { + snarl->m_applications[client] = Application(appName, icon); + } + else + { + qDebug() << appName << "already registred"; } break; case UNREGISTER: - snarl->snore()->deregisterApplication( sNotification.notification.application()); + snarl->snore()->deregisterApplication( app ); + snarl->m_applications.take(client); break; case ERROR: default: - sNotification.vailid=false; break; } - sNotification.notification.hints().setValue("SnarlAction", sNotification.action); - return sNotification; + return isHttp; } diff --git a/src/plugins/frontends/snarlnetwork/parser.h b/src/plugins/frontends/snarlnetwork/parser.h index 8ad6015..b178d1a 100644 --- a/src/plugins/frontends/snarlnetwork/parser.h +++ b/src/plugins/frontends/snarlnetwork/parser.h @@ -27,12 +27,13 @@ -class Parser:public QObject{ +class Parser : public QObject +{ Q_OBJECT public: Parser(class SnarlNetworkFrontend* snarl); - struct SnarlNotification parse(QString &msg,class QTcpSocket* client); + bool parse(Snore::Notification &sNotification, const QString &msg, class QTcpSocket* client); private: class SnarlNetworkFrontend *snarl; diff --git a/src/plugins/frontends/snarlnetwork/snarlnetwork.cpp b/src/plugins/frontends/snarlnetwork/snarlnetwork.cpp index 42d04bd..d8de525 100644 --- a/src/plugins/frontends/snarlnetwork/snarlnetwork.cpp +++ b/src/plugins/frontends/snarlnetwork/snarlnetwork.cpp @@ -70,79 +70,82 @@ bool SnarlNetworkFrontend::deinitialize() } -void SnarlNetworkFrontend::actionInvoked(Notification notification) +void SnarlNetworkFrontend::actionInvoked(Snore::Notification notification) { //TODO:fix callback - SnarlNotification sn=notifications.value(notification.id()); if(notification.actionInvoked().id() == 1 ) { - callback(sn,"SNP/1.1/304/Notification acknowledged/"); + callback(notification,"SNP/1.1/304/Notification acknowledged/"); } else if(notification.actionInvoked().id() == 2) { - callback(sn,"SNP/1.1/302/Notification cancelled/"); + callback(notification,"SNP/1.1/302/Notification cancelled/"); } } -void SnarlNetworkFrontend::notificationClosed(Notification notification) +void SnarlNetworkFrontend::notificationClosed(Snore::Notification notification) { - SnarlNotification sn=notifications.value(notification.id()); if(notification.closeReason() == NotificationEnums::CloseReasons::TIMED_OUT) { - callback(sn,"SNP/1.1/303/Notification timed out/"); + callback(notification, "SNP/1.1/303/Notification timed out/"); } else { - callback(sn,"SNP/1.1/307/Notification closed/"); + callback(notification, "SNP/1.1/307/Notification closed/"); } } -void SnarlNetworkFrontend::handleConnection(){ +void SnarlNetworkFrontend::handleConnection() +{ QTcpSocket *client = tcpServer->nextPendingConnection(); connect(client,SIGNAL(readyRead()),this,SLOT(handleMessages())); connect(client,SIGNAL(disconnected()), client, SLOT(deleteLater())); } -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(const QString &msg,incommings){ - QString s=msg.trimmed(); - if(s == "") - continue; - SnarlNotification noti=parser->parse(s,client); - if(!noti.vailid) - continue; - if(noti.isNotification){ - snore()->broadcastNotification(noti.notification); - if(noti.notification.id()!=0){ - out+="/"+QString::number(noti.notification.id()); - notifications.insert(noti.notification.id(),noti); - } - } - out+="\r\n"; +void SnarlNetworkFrontend::handleMessages() +{ + const QString out("SNP/1.1/0/OK"); + QTcpSocket *client = qobject_cast(sender()); - client->write(out.toUtf8()); - if(noti.httpClient){ + QStringList messages(QString::fromAscii(client->readAll()).trimmed().split("\r\n")); + foreach(const QString &s, messages) + { + if(s.isEmpty()) + { + continue; + } + Notification noti; + bool isHttp = parser->parse(noti, s, client); + if(noti.isValid()) + { + snore()->broadcastNotification(noti); + write(client, QString("%1/%2\r\n").arg(out,QString::number(noti.id()))); + } + else + { + write(client, QString("%1\r\n").arg(out)); + } + + + if(isHttp) + { client->disconnectFromHost(); client->waitForDisconnected(); } - qDebug()<write(msg.toAscii()+"\r\n"); - sn.clientSocket->flush(); + if(sn.hints().contains("snarl_clientSocket") &&!msg.isEmpty()) + { + QTcpSocket *client = qvariant_cast(sn.hints().value("snarl_clientSocket")); + write(client, QString("%1%2\r\n").arg(msg, QString::number(sn.id()))); + client->flush(); - if(sn.httpClient){ - sn.clientSocket->waitForBytesWritten(-1); - sn.clientSocket->disconnectFromHost(); + if(sn.hints().value("snarl_isHttpClient",false).toBool()) + { + client->waitForBytesWritten(-1); + client->disconnectFromHost(); } } } diff --git a/src/plugins/frontends/snarlnetwork/snarlnetwork.h b/src/plugins/frontends/snarlnetwork/snarlnetwork.h index 2382c7b..b72a154 100644 --- a/src/plugins/frontends/snarlnetwork/snarlnetwork.h +++ b/src/plugins/frontends/snarlnetwork/snarlnetwork.h @@ -23,22 +23,12 @@ #include "parser.h" #include +#include +#include -namespace Snore{ - class Notification; - class SnoreCore; -} -struct SnarlNotification{ - Snore::Notification notification; - QString action; - bool httpClient; - bool vailid; - bool isNotification; - QPointer clientSocket; -}; - -class SnarlNetworkFrontend:public Snore::SnoreFrontend{ +class SnarlNetworkFrontend : public Snore::SnoreFrontend +{ Q_OBJECT Q_INTERFACES(Snore::SnoreFrontend) Q_PLUGIN_METADATA(IID "org.Snore.NotificationFrontend/1.0") @@ -60,12 +50,17 @@ private slots: void handleMessages(); private: - class QTcpServer *tcpServer; + QTcpServer *tcpServer; Parser *parser; - QHash notifications; - QHash m_applications; + QHash m_applications; - void callback(const SnarlNotification &sn,QString msg); + void callback(Snore::Notification &sn,QString msg); + + inline void write(QTcpSocket *dest,const QString &msg) + { + qDebug() << Q_FUNC_INFO << msg; + dest->write(msg.toAscii()); + } };