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(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");

View File

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

View File

@ -2,6 +2,7 @@
#include <QDebug>
#include "notification.h"
#include <QPluginLoader>
#include <iostream>
QString const SnoreServer::snoreTMP=QDir::temp().path()+"/SnoreNotify/";
@ -57,7 +58,9 @@ int SnoreServer::broadcastNotification(QSharedPointer<Notification> notification
emit notify(notification);
qDebug()<<"Broadcasting notification:"<<notification->toSnalrString();
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;
}
@ -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)
&&!applications.value(applicationName)->alerts.value(alertName)->active;
}

View File

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

View File

@ -22,7 +22,7 @@ FreedesktopNotification_Frontend::~FreedesktopNotification_Frontend(){
}
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){
@ -40,7 +40,7 @@ void FreedesktopNotification_Frontend::notificationClosed(QSharedPointer<Notific
reason=4;
}
emit NotificationClosed(notification->id,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<QDBusArgument>()>>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);
}
void FreedesktopNotification_Frontend::CloseNotification(uint id){
QSharedPointer<Notification> n(new Notification());
n->id=id;
QSharedPointer<Notification> n(new Notification(id));
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");
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<<"|"<<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){
qDebug()<<id<<"|"<<reason;;
if(id!=notification->id)return;
if(id!=notification->getID())return;
close();
if(reason==1)
notification->actionInvoked=Notification::TIMED_OUT;

View File

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

View File

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

View File

@ -19,16 +19,16 @@ Snarl_Backend::Snarl_Backend()
int Snarl_Backend::notify(QSharedPointer<Notification>notification){
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"<<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());
}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();
}
}

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();
SnarlNotification sNotification;
@ -36,6 +36,7 @@ Parser::Parser(SnarlNetworkFrontend *snarl):snarl(snarl)
sNotification.vailid=true;
sNotification.notification=QSharedPointer<Notification>(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"

View File

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

View File

@ -18,14 +18,14 @@ SnarlNetworkFrontend::SnarlNetworkFrontend():parser(this){
void SnarlNetworkFrontend::actionInvoked(QSharedPointer<Notification>notification){
//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(QSharedPointer<Notification>notification){
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()<<msg;
sn.clientSocket->write(msg.toAscii()+"\n");
sn.clientSocket->flush();