mirror of
https://github.com/status-im/snorenotify.git
synced 2025-02-10 07:26:22 +00:00
fixed netsnarl
This commit is contained in:
parent
9d711a2b0a
commit
aaf7e00a5b
@ -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()
|
||||
|
@ -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 );
|
||||
}
|
||||
|
@ -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();
|
||||
|
@ -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"<<msg;
|
||||
sNotification.httpClient=true;
|
||||
isHttp = true;
|
||||
splitted = tmp.split("#?");
|
||||
}
|
||||
else
|
||||
{
|
||||
splitted = msg.split("#?");
|
||||
}
|
||||
|
||||
QString appName;
|
||||
QString alertName;
|
||||
QString title;
|
||||
QString text;
|
||||
QString icon;
|
||||
QString alertName;
|
||||
int timeout=10;
|
||||
|
||||
QString key;
|
||||
QString value;
|
||||
QStringList splitted=msg.split("#?");
|
||||
foreach(QString s,splitted){
|
||||
|
||||
foreach(QString s,splitted)
|
||||
{
|
||||
key = s.mid(0,s.indexOf("=")).toLower();
|
||||
value = s.mid(s.indexOf("=")+1);
|
||||
switch(getSnpType.value(key)){
|
||||
switch(getSnpType.value(key))
|
||||
{
|
||||
case APP:
|
||||
appName = value;
|
||||
break;
|
||||
case ACTION:
|
||||
action = getSnpType.value(value);
|
||||
sNotification.action = value;
|
||||
break;
|
||||
case TITLE:
|
||||
title = value;
|
||||
@ -104,6 +108,7 @@ SnarlNotification Parser::parse(QString &msg,QTcpSocket* client){
|
||||
break;
|
||||
case CLASS:
|
||||
alertName = value;
|
||||
break;
|
||||
case TIMEOUT:
|
||||
timeout = value.toInt();
|
||||
break;
|
||||
@ -115,81 +120,78 @@ SnarlNotification Parser::parse(QString &msg,QTcpSocket* client){
|
||||
|
||||
Application app;
|
||||
Alert alert;
|
||||
if(snarl->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()<<sNotification.notification.application()<<"already registred";
|
||||
alert = Alert(alertName, title, icon);
|
||||
}
|
||||
app.addAlert(alert);
|
||||
break;
|
||||
case REGISTER:
|
||||
if(!snarl->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;
|
||||
}
|
||||
|
||||
|
@ -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;
|
||||
|
@ -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<QTcpSocket*>(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<QTcpSocket*>(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()<<out;
|
||||
}
|
||||
}
|
||||
|
||||
void SnarlNetworkFrontend::callback(const SnarlNotification &sn,QString msg)
|
||||
void SnarlNetworkFrontend::callback(Notification &sn, const QString msg)
|
||||
{
|
||||
notifications.remove(sn.notification.id());
|
||||
if(sn.clientSocket!=NULL&&!msg.isEmpty()){
|
||||
msg+=QString::number(sn.notification.id());
|
||||
qDebug()<<msg;
|
||||
sn.clientSocket->write(msg.toAscii()+"\r\n");
|
||||
sn.clientSocket->flush();
|
||||
if(sn.hints().contains("snarl_clientSocket") &&!msg.isEmpty())
|
||||
{
|
||||
QTcpSocket *client = qvariant_cast<QTcpSocket*>(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();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -23,22 +23,12 @@
|
||||
#include "parser.h"
|
||||
|
||||
#include <QPointer>
|
||||
#include <QTcpSocket>
|
||||
#include <QTcpServer>
|
||||
|
||||
namespace Snore{
|
||||
class Notification;
|
||||
class SnoreCore;
|
||||
}
|
||||
|
||||
struct SnarlNotification{
|
||||
Snore::Notification notification;
|
||||
QString action;
|
||||
bool httpClient;
|
||||
bool vailid;
|
||||
bool isNotification;
|
||||
QPointer<class QTcpSocket> 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<uint,SnarlNotification> notifications;
|
||||
QHash<QString,Snore::Application> m_applications;
|
||||
QHash<QTcpSocket*,Snore::Application> 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());
|
||||
}
|
||||
|
||||
};
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user