00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019 #include "knotificationmanager_p.h"
00020 #include <ktoolinvocation.h>
00021 #include "knotification.h"
00022
00023 #include <QHash>
00024 #include <QWidget>
00025 #include <QtDBus/QtDBus>
00026 #include <QPointer>
00027
00028 #include <kdebug.h>
00029 #include <kapplication.h>
00030 #include <kiconloader.h>
00031 #include <kconfig.h>
00032 #include <klocale.h>
00033
00034 typedef QHash<QString,QString> Dict;
00035
00036 struct KNotificationManager::Private
00037 {
00038 QHash<int , KNotification*> notifications;
00039 QDBusInterface *knotify;
00040 };
00041
00042 KNotificationManager * KNotificationManager::self()
00043 {
00044 K_GLOBAL_STATIC(KNotificationManager, s_self)
00045 return s_self;
00046 }
00047
00048
00049 KNotificationManager::KNotificationManager()
00050 : d(new Private)
00051 {
00052 if (!QDBusConnection::sessionBus().interface()->isServiceRegistered("org.kde.knotify")) {
00053 QString error;
00054 int ret = KToolInvocation::startServiceByDesktopPath("knotify4.desktop",
00055 QStringList(), &error);
00056 if (ret > 0) {
00057 kError() << "Couldn't start knotify from knotify4.desktop: " << error << endl;
00058 }
00059 }
00060 d->knotify =
00061 new QDBusInterface(QLatin1String("org.kde.knotify"), QLatin1String("/Notify"), QLatin1String("org.kde.KNotify"), QDBusConnection::sessionBus(), this);
00062 d->knotify->connection().connect(QLatin1String("org.kde.knotify"), QLatin1String("/Notify"),
00063 QLatin1String("org.kde.KNotify"),
00064 QLatin1String("notificationClosed"),
00065 this, SLOT(notificationClosed(int)));
00066 d->knotify->connection().connect(QLatin1String("org.kde.knotify"), QLatin1String("/Notify"),
00067 QLatin1String("org.kde.KNotify"),
00068 QLatin1String("notificationActivated"),
00069 this, SLOT(notificationActivated(int,int)));
00070
00071 }
00072
00073
00074 KNotificationManager::~KNotificationManager()
00075 {
00076 delete d->knotify;
00077 delete d;
00078 }
00079
00080 void KNotificationManager::notificationActivated( int id, int action )
00081 {
00082 if(d->notifications.contains(id))
00083 {
00084 kDebug(299) << id << " " << action;
00085 KNotification *n = d->notifications[id];
00086 d->notifications.remove(id);
00087 n->activate( action );
00088 }
00089 }
00090
00091 void KNotificationManager::notificationClosed( int id )
00092 {
00093 if(d->notifications.contains(id))
00094 {
00095 kDebug( 299 ) << id;
00096 KNotification *n = d->notifications[id];
00097 d->notifications.remove(id);
00098 n->close();
00099 }
00100 }
00101
00102
00103 void KNotificationManager::close( int id, bool force )
00104 {
00105 if(force || d->notifications.contains(id)) {
00106 d->notifications.remove(id);
00107 kDebug( 299 ) << id;
00108 d->knotify->call(QDBus::NoBlock, "closeNotification", id);
00109 }
00110 }
00111
00112 bool KNotificationManager::notify( KNotification* n, const QPixmap &pix,
00113 const QStringList &actions,
00114 const KNotification::ContextList & contexts,
00115 const QString &appname)
00116 {
00117 WId winId=n->widget() ? n->widget()->topLevelWidget()->winId() : 0;
00118
00119 QByteArray pixmapData;
00120 {
00121 QBuffer buffer(&pixmapData);
00122 buffer.open(QIODevice::WriteOnly);
00123 pix.save(&buffer, "PNG");
00124 }
00125
00126 QVariantList contextList;
00127 typedef QPair<QString,QString> Context;
00128 foreach (const Context& ctx, contexts)
00129 {
00130 QVariantList vl;
00131 vl << ctx.first << ctx.second;
00132 contextList << vl;
00133 }
00134
00135 QList<QVariant> args;
00136 args << n->eventId() << (appname.isEmpty() ? KGlobal::mainComponent().componentName() : appname);
00137 args.append(QVariant(contextList));
00138 args << n->title() << n->text() << pixmapData << QVariant(actions) << qlonglong(winId) ;
00139 return d->knotify->callWithCallback( "event", args, n, SLOT(slotReceivedId(int)), SLOT(slotReceivedIdError(QDBusError)));
00140 }
00141
00142 void KNotificationManager::insert(KNotification *n, int id)
00143 {
00144 d->notifications.insert(id, n);
00145 }
00146
00147 void KNotificationManager::update(KNotification * n, int id)
00148 {
00149 if(id <= 0)
00150 return;
00151
00152 QByteArray pixmapData;
00153 if(!n->pixmap().isNull())
00154 {
00155 QBuffer buffer(&pixmapData);
00156 buffer.open(QIODevice::WriteOnly);
00157 n->pixmap().save(&buffer, "PNG");
00158 }
00159
00160 d->knotify->call(QDBus::NoBlock, "update", id, n->title(), n->text(), pixmapData , n->actions() );
00161 }
00162
00163 void KNotificationManager::reemit(KNotification * n, int id)
00164 {
00165 QVariantList contextList;
00166 typedef QPair<QString,QString> Context;
00167 foreach (const Context& ctx, n->contexts())
00168 {
00169
00170 QVariantList vl;
00171 vl << ctx.first << ctx.second;
00172 contextList << vl;
00173 }
00174
00175 d->knotify->call(QDBus::NoBlock, "reemit", id, contextList);
00176 }
00177
00178
00179 #include "knotificationmanager_p.moc"