fixed snarl network

This commit is contained in:
theonering 2010-02-28 15:29:17 +01:00
parent 598b72ffb3
commit 18adeb0326
12 changed files with 84 additions and 73 deletions

View File

@ -5,9 +5,10 @@
int Notification::DefaultTimeout=10;
Notification::Notification():source("none"),timeout(10),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):source(source),title(title),text(text),timeout(timeout),icon(icon),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; return notification;
} }
void Notification::setIsNotification(bool b){
notification=b;
}
uint Notification::getID(){
return id;
}
QString Notification::toSnalrString()const{ QString Notification::toSnalrString()const{
QString out("type=SNP#?version=1.1"); QString out("type=SNP#?version=1.1");

View File

@ -13,7 +13,9 @@
class SNORE_EXPORT Notification:public QObject class SNORE_EXPORT Notification:public QObject
{ {
Q_OBJECT Q_OBJECT
friend class SnoreServer;
public: public:
static int DefaultTimeout;
static inline QString toPlainText(const QString &string){ static inline QString toPlainText(const QString &string){
if(!Qt::mightBeRichText ( string))return string; if(!Qt::mightBeRichText ( string))return string;
QTextEdit te; QTextEdit te;
@ -21,9 +23,10 @@ public:
return te.toPlainText(); return te.toPlainText();
}; };
public: public:
Notification(); Notification(uint id=0);
Notification(QString source,QString title,QString text,QString icon,int timeout); Notification(QString source,QString title,QString text,QString icon,int timeout=10,uint id=0);
bool isNotification(); bool isNotification();
void setIsNotification(bool b);
QString toSnalrString()const; QString toSnalrString()const;
enum actions{ enum actions{
@ -44,17 +47,13 @@ public:
int timeout; int timeout;
void setIcon(const QString &icon){this->icon=icon; } void setIcon(const QString &icon){this->icon=icon; }
QString getIcon(); QString getIcon();
QVariantHash hints; QVariantHash hints;
uint getID();
int id;
private:
uint id;
private:
QString icon; QString icon;
bool notification; bool notification;

View File

@ -2,6 +2,7 @@
#include <QDebug> #include <QDebug>
#include "notification.h" #include "notification.h"
#include <QPluginLoader> #include <QPluginLoader>
#include <iostream>
QString const SnoreServer::snoreTMP=QDir::temp().path()+"/SnoreNotify/"; QString const SnoreServer::snoreTMP=QDir::temp().path()+"/SnoreNotify/";
@ -57,7 +58,9 @@ int SnoreServer::broadcastNotification(QSharedPointer<Notification> notification
emit notify(notification); emit notify(notification);
qDebug()<<"Broadcasting notification:"<<notification->toSnalrString(); qDebug()<<"Broadcasting notification:"<<notification->toSnalrString();
if(primaryNotificationBackend!=NULL){ if(primaryNotificationBackend!=NULL){
return primaryNotificationBackend->notify(notification); notification->id=primaryNotificationBackend->notify(notification);
std::cout<<"Notification ID: "<<QString::number(notification->id).toLatin1().data()<<std::endl;
return notification->id;
} }
return -1; return -1;
} }
@ -83,7 +86,7 @@ void SnoreServer::addApplication(QSharedPointer<Application> 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) return applications.contains(applicationName)&&applications.value(applicationName)->alerts.contains(alertName)
&&!applications.value(applicationName)->alerts.value(alertName)->active; &&!applications.value(applicationName)->alerts.value(alertName)->active;
} }

View File

@ -31,7 +31,7 @@ public:
void notificationActionInvoked(QSharedPointer<Notification> notification); void notificationActionInvoked(QSharedPointer<Notification> notification);
void addApplication(QSharedPointer<Application> application); void addApplication(QSharedPointer<Application> 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 addAlert(const QString &appName,const QString &alertName, const QString &alertTitle);
void removeApplication(const QString& appName); void removeApplication(const QString& appName);

View File

@ -22,7 +22,7 @@ FreedesktopNotification_Frontend::~FreedesktopNotification_Frontend(){
} }
void FreedesktopNotification_Frontend::actionInvoked(QSharedPointer<Notification>notification){ void FreedesktopNotification_Frontend::actionInvoked(QSharedPointer<Notification>notification){
emit ActionInvoked(notification->id,QString::number(notification->actionInvoked)); emit ActionInvoked(notification->getID(),QString::number(notification->actionInvoked));
} }
void FreedesktopNotification_Frontend::notificationClosed(QSharedPointer<Notification>notification){ void FreedesktopNotification_Frontend::notificationClosed(QSharedPointer<Notification>notification){
@ -40,7 +40,7 @@ void FreedesktopNotification_Frontend::notificationClosed(QSharedPointer<Notific
reason=4; reason=4;
} }
emit NotificationClosed(notification->id,reason); emit NotificationClosed(notification->getID(),reason);
} }
QString FreedesktopNotification_Frontend::getImagefromHint(const FreedesktopImageHint &img){ 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<QDBusArgument>()>>image; hints["image_data"].value<QDBusArgument>()>>image;
icon=getImagefromHint(image); icon=getImagefromHint(image);
} }
QSharedPointer<Notification> noti(new Notification(property("name").value<QString>(),summary,body,icon,timeout));
noti->id=replaces_id; QSharedPointer<Notification> noti(new Notification(property("name").value<QString>(),summary,body,icon,timeout==-1?Notification::DefaultTimeout:timeout/1000,replaces_id));
return getSnore()->broadcastNotification(noti); return getSnore()->broadcastNotification(noti);
} }
void FreedesktopNotification_Frontend::CloseNotification(uint id){ void FreedesktopNotification_Frontend::CloseNotification(uint id){
QSharedPointer<Notification> n(new Notification()); QSharedPointer<Notification> n(new Notification(id));
n->id=id;
getSnore()->closeNotification(n); getSnore()->closeNotification(n);
} }

View File

@ -35,11 +35,11 @@ fNotification::fNotification(FreedesktopNotification_Backend* parent):parent(par
int fNotification::send(){ uint fNotification::send(){
qDebug("Sending a notification"); qDebug("Sending a notification");
FreedesktopNotification n(notification.data()); FreedesktopNotification n(notification.data());
QDBusMessage recive=notificationInterface.call("Notify", QVariant::fromValue(n)); 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.setParent(this);
selfdistruct.setSingleShot(true); 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))); QDBusConnection::sessionBus().connect("org.freedesktop.Notifications","/org/freedesktop/Notifications","org.freedesktop.Notifications","ActionInvoked",this,SLOT(action(uint,QString)));
if(getVendor()=="GNOME") if(getVendor()=="GNOME")
QDBusConnection::sessionBus().connect("org.freedesktop.Notifications","/org/freedesktop/Notifications","org.freedesktop.Notifications","NotificationClosed",this,SLOT(closed(uint,uint))); 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){ void fNotification::action(const uint &id, const QString &action_key){
if(id!=notification->id)return; if(id!=notification->getID())return;
close(); close();
qDebug()<<id<<"|"<<action_key ; qDebug()<<id<<"|"<<action_key ;
@ -66,7 +66,7 @@ void fNotification::action(const uint &id, const QString &action_key){
} }
void fNotification::closed(const uint &id,const uint &reason){ void fNotification::closed(const uint &id,const uint &reason){
qDebug()<<id<<"|"<<reason;; qDebug()<<id<<"|"<<reason;;
if(id!=notification->id)return; if(id!=notification->getID())return;
close(); close();
if(reason==1) if(reason==1)
notification->actionInvoked=Notification::TIMED_OUT; notification->actionInvoked=Notification::TIMED_OUT;

View File

@ -34,7 +34,7 @@ private:
public: public:
fNotification(FreedesktopNotification_Backend* parent); fNotification(FreedesktopNotification_Backend* parent);
int send(); uint send();
QSharedPointer<Notification> notification; QSharedPointer<Notification> notification;
private: private:

View File

@ -46,7 +46,7 @@ namespace Snarl {
M_OK = 0x00000000, M_OK = 0x00000000,
M_OUT_OF_MEMORY = 0x80000002, M_OUT_OF_MEMORY = 0x80000002,
M_TIMED_OUT = 0x8000000A M_TIMED_OUT = 0x8000000A
}; } ;
enum SNARL_COMMANDS { enum SNARL_COMMANDS {
SNARL_SHOW = 1, SNARL_SHOW = 1,

View File

@ -19,16 +19,16 @@ Snarl_Backend::Snarl_Backend()
int Snarl_Backend::notify(QSharedPointer<Notification>notification){ int Snarl_Backend::notify(QSharedPointer<Notification>notification){
int timeout=notification->timeout>=0?notification->timeout/1000:10; int timeout=notification->timeout>=0?notification->timeout:10;
if(notification->id==0){ if(notification->getID()==0){
QString title=Notification::toPlainText(notification->title); QString title=Notification::toPlainText(notification->title);
QString text=Notification::toPlainText(notification->text); QString text=Notification::toPlainText(notification->text);
qDebug()<<"Calling Snarl"<<title<< text<<QString::number(timeout)<< notification->getIcon(); std::cout<<"Calling Snarl"<<title.toLocal8Bit().data()<< text.toLocal8Bit().data()<<" "<<QString::number(timeout).toLatin1().data()<< notification->getIcon().toLocal8Bit().data()<<std::endl;
return snarlInterface->ShowMessage(title.toLocal8Bit().data(), text.toLocal8Bit().data(),timeout, notification->getIcon().toLocal8Bit().data()); return snarlInterface->ShowMessage(title.toLocal8Bit().data(), text.toLocal8Bit().data(),timeout, notification->getIcon().toLocal8Bit().data());
}else{ }else{
//update message //update message
snarlInterface->UpdateMessage(LONG32(notification->id),notification->title.toLocal8Bit().data(), notification->text.toLocal8Bit().data(),notification->getIcon().toLocal8Bit().data()); snarlInterface->UpdateMessage(LONG32(notification->getID()),notification->title.toLocal8Bit().data(), notification->text.toLocal8Bit().data(),notification->getIcon().toLocal8Bit().data());
return notification->id; return notification->getID();
} }
} }

View File

@ -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(); msg=msg.trimmed();
SnarlNotification sNotification; SnarlNotification sNotification;
@ -36,6 +36,7 @@ Parser::Parser(SnarlNetworkFrontend *snarl):snarl(snarl)
sNotification.vailid=true; sNotification.vailid=true;
sNotification.notification=QSharedPointer<Notification>(new Notification()); sNotification.notification=QSharedPointer<Notification>(new Notification());
sNotification.clientSocket=client; sNotification.clientSocket=client;
sNotification.notification->setIsNotification(false);
snpTypes action(ERROR); snpTypes action(ERROR);
if(msg.startsWith("GET ")){ if(msg.startsWith("GET ")){
@ -84,8 +85,9 @@ Parser::Parser(SnarlNetworkFrontend *snarl):snarl(snarl)
switch(action){ switch(action){
case NOTIFICATION: case NOTIFICATION:
if(snarl->getSnore()->applicationListConontainsAlert(sNotification.notification->app,sNotification.notification->alert)) if(snarl->getSnore()->applicationListAlertIsActive(sNotification.notification->app,sNotification.notification->alert))
break; break;
sNotification.notification->setIsNotification(true);
return sNotification; return sNotification;
break; break;
case ADD_CLASS: case ADD_CLASS:
@ -118,40 +120,39 @@ Parser::Parser(SnarlNetworkFrontend *snarl):snarl(snarl)
return sNotification; return sNotification;
} }
QString Parser::downloadIcon(const QString &address){ QString Parser::downloadIcon(const QString &address){
if(address=="") if(address=="")
return ""; return "";
if(address.startsWith("file://")) if(address.startsWith("file://"))
return QString(address.mid(7)); return QString(address.mid(7));
QByteArray arr=address.toUtf8(); QByteArray arr=address.toUtf8();
QUrl url=QUrl::fromEncoded(arr); QUrl url=QUrl::fromEncoded(arr);
QCryptographicHash hash(QCryptographicHash::Md5); QCryptographicHash hash(QCryptographicHash::Md5);
hash.addData(arr); hash.addData(arr);
QString filename=QDir::temp().path()+"/SnoreNotify/"+hash.result().toHex()+address.mid(address.lastIndexOf(".")-1); QString filename=QDir::temp().path()+"/SnoreNotify/"+hash.result().toHex()+address.mid(address.lastIndexOf(".")-1);
QFile file(filename); QFile file(filename);
if(file.exists()) if(file.exists())
return filename; return filename;
QNetworkReply * reply=download(url); QByteArray reply=download(url);
file.open(QIODevice::WriteOnly); file.open(QIODevice::WriteOnly);
file.write(reply->readAll()); file.write(reply);
reply->deleteLater(); return filename;
return filename;
} }
QNetworkReply* Parser::download(const QUrl &address){ QByteArray Parser::download(const QUrl &address){
QNetworkAccessManager manager; QNetworkAccessManager manager;
QEventLoop loop; QEventLoop loop;
QNetworkRequest request(address); QNetworkRequest request(address);
request.setRawHeader("User-Agent", "SnoreNotify"); request.setRawHeader("User-Agent", "SnoreNotify");
QNetworkReply *reply=manager.get(request); QNetworkReply *reply=manager.get(request);
QObject::connect(reply, SIGNAL(finished()), &loop, SLOT(quit())); QObject::connect(reply, SIGNAL(finished()), &loop, SLOT(quit()));
loop.exec(); loop.exec();
return reply; return reply->readAll();
} }
//#include "parser.moc" //#include "parser.moc"

View File

@ -11,7 +11,7 @@
class Parser{ class Parser{
public: public:
static class QNetworkReply* download(const QUrl &address); static class QByteArray download(const QUrl &address);
public: public:
Parser(class SnarlNetworkFrontend* snarl); Parser(class SnarlNetworkFrontend* snarl);

View File

@ -18,14 +18,14 @@ SnarlNetworkFrontend::SnarlNetworkFrontend():parser(this){
void SnarlNetworkFrontend::actionInvoked(QSharedPointer<Notification>notification){ void SnarlNetworkFrontend::actionInvoked(QSharedPointer<Notification>notification){
//TODO:fix callback //TODO:fix callback
SnarlNotification sn=notifications.value(notification->id); SnarlNotification sn=notifications.value(notification->getID());
if(notification->actionInvoked==1) if(notification->actionInvoked==1)
callback(sn,"SNP/1.1/304/Notification acknowledged/"); callback(sn,"SNP/1.1/304/Notification acknowledged/");
else if(notification->actionInvoked==2) else if(notification->actionInvoked==2)
callback(sn,"SNP/1.1/302/Notification cancelled/"); callback(sn,"SNP/1.1/302/Notification cancelled/");
} }
void SnarlNetworkFrontend::notificationClosed(QSharedPointer<Notification>notification){ void SnarlNetworkFrontend::notificationClosed(QSharedPointer<Notification>notification){
SnarlNotification sn=notifications.value(notification->id); SnarlNotification sn=notifications.value(notification->getID());
if(notification->actionInvoked==Notification::TIMED_OUT) if(notification->actionInvoked==Notification::TIMED_OUT)
callback(sn,"SNP/1.1/303/Notification timed out/"); callback(sn,"SNP/1.1/303/Notification timed out/");
else else
@ -44,16 +44,18 @@ void SnarlNetworkFrontend::handleMessages(){
QStringList incommings(QString::fromUtf8(client->readAll()).split("\r\n")); QStringList incommings(QString::fromUtf8(client->readAll()).split("\r\n"));
foreach(QString s,incommings){ foreach(QString s,incommings){
SnarlNotification noti=parser.parse(s,client); SnarlNotification noti=parser.parse(s,client);
notifications.insert(noti.notification->id,noti); notifications.insert(noti.notification->getID(),noti);
if(!noti.vailid) if(!noti.vailid)
continue; continue;
int notificationNR=getSnore()->broadcastNotification(noti.notification); if(noti.notification->isNotification()){
if(notificationNR!=-1){ getSnore()->broadcastNotification(noti.notification);
out+="/"+QString::number(notificationNR)+"\r\n"; if(noti.notification->getID()!=0){
out+="/"+QString::number(noti.notification->getID())+"\r\n";
}
}else{ }else{
out+="\r\n"; out+="\r\n";
} }
client->write(out.toUtf8()); client->write(out.toLatin1());
if(noti.httpClient){ if(noti.httpClient){
client->disconnectFromHost(); client->disconnectFromHost();
client->waitForDisconnected(); client->waitForDisconnected();
@ -68,9 +70,9 @@ void SnarlNetworkFrontend::clientDisconnecd(){
} }
void SnarlNetworkFrontend::callback(const SnarlNotification &sn,QString msg){ void SnarlNetworkFrontend::callback(const SnarlNotification &sn,QString msg){
notifications.remove(sn.notification->id); notifications.remove(sn.notification->getID());
if(sn.clientSocket!=NULL&&!msg.isEmpty()){ if(sn.clientSocket!=NULL&&!msg.isEmpty()){
msg+=QString::number(sn.notification->id); msg+=QString::number(sn.notification->getID());
qDebug()<<msg; qDebug()<<msg;
sn.clientSocket->write(msg.toAscii()+"\n"); sn.clientSocket->write(msg.toAscii()+"\n");
sn.clientSocket->flush(); sn.clientSocket->flush();