fix tricky regression noticed by Vyacheslav Tokarev on Google Reader.
[kdelibs.git] / kdeui / util / knotificationmanager.cpp
blob52e9e87479cfebc6003b1309250196bffd198141
1 /* This file is part of the KDE libraries
2 Copyright (C) 2005 Olivier Goffart <ogoffart at kde.org>
4 This library is free software; you can redistribute it and/or
5 modify it under the terms of the GNU Library General Public
6 License version 2 as published by the Free Software Foundation.
8 This library is distributed in the hope that it will be useful,
9 but WITHOUT ANY WARRANTY; without even the implied warranty of
10 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 Library General Public License for more details.
13 You should have received a copy of the GNU Library General Public License
14 along with this library; see the file COPYING.LIB. If not, write to
15 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
16 Boston, MA 02110-1301, USA.
19 #include "knotificationmanager_p.h"
20 #include <ktoolinvocation.h>
21 #include "knotification.h"
23 #include <QHash>
24 #include <QWidget>
25 #include <QtDBus/QtDBus>
26 #include <QPointer>
28 #include <kdebug.h>
29 #include <kapplication.h>
30 #include <kiconloader.h>
31 #include <kconfig.h>
32 #include <klocale.h>
34 typedef QHash<QString,QString> Dict;
36 struct KNotificationManager::Private
38 QHash<int , KNotification*> notifications;
39 QDBusInterface *knotify;
42 KNotificationManager * KNotificationManager::self()
44 K_GLOBAL_STATIC(KNotificationManager, s_self)
45 return s_self;
49 KNotificationManager::KNotificationManager()
50 : d(new Private)
52 if (!QDBusConnection::sessionBus().interface()->isServiceRegistered("org.kde.knotify")) {
53 QString error;
54 int ret = KToolInvocation::startServiceByDesktopPath("knotify4.desktop",
55 QStringList(), &error);
56 if (ret > 0) {
57 kError() << "Couldn't start knotify from knotify4.desktop: " << error << endl;
60 d->knotify =
61 new QDBusInterface(QLatin1String("org.kde.knotify"), QLatin1String("/Notify"), QLatin1String("org.kde.KNotify"), QDBusConnection::sessionBus(), this);
62 d->knotify->connection().connect(QLatin1String("org.kde.knotify"), QLatin1String("/Notify"),
63 QLatin1String("org.kde.KNotify"),
64 QLatin1String("notificationClosed"),
65 this, SLOT(notificationClosed(int)));
66 d->knotify->connection().connect(QLatin1String("org.kde.knotify"), QLatin1String("/Notify"),
67 QLatin1String("org.kde.KNotify"),
68 QLatin1String("notificationActivated"),
69 this, SLOT(notificationActivated(int,int)));
74 KNotificationManager::~KNotificationManager()
76 delete d->knotify;
77 delete d;
80 void KNotificationManager::notificationActivated( int id, int action )
82 if(d->notifications.contains(id))
84 kDebug(299) << id << " " << action;
85 KNotification *n = d->notifications[id];
86 d->notifications.remove(id);
87 n->activate( action );
91 void KNotificationManager::notificationClosed( int id )
93 if(d->notifications.contains(id))
95 kDebug( 299 ) << id;
96 KNotification *n = d->notifications[id];
97 d->notifications.remove(id);
98 n->close();
103 void KNotificationManager::close( int id, bool force )
105 if(force || d->notifications.contains(id)) {
106 d->notifications.remove(id);
107 kDebug( 299 ) << id;
108 d->knotify->call(QDBus::NoBlock, "closeNotification", id);
112 bool KNotificationManager::notify( KNotification* n, const QPixmap &pix,
113 const QStringList &actions,
114 const KNotification::ContextList & contexts,
115 const QString &appname)
117 WId winId=n->widget() ? n->widget()->topLevelWidget()->winId() : 0;
119 QByteArray pixmapData;
122 QDataStream arg(&pixmapData, QIODevice::WriteOnly);
123 arg << pix;
126 QVariantList contextList;
127 typedef QPair<QString,QString> Context;
128 foreach (const Context& ctx, contexts)
130 QVariantList vl;
131 vl << ctx.first << ctx.second;
132 contextList << vl;
135 QList<QVariant> args;
136 args << n->eventId() << (appname.isEmpty() ? KGlobal::mainComponent().componentName() : appname);
137 args.append(QVariant(contextList));
138 args << n->text() << pixmapData << QVariant(actions) << qlonglong(winId) ;
139 return d->knotify->callWithCallback( "event", args, n, SLOT(slotReceivedId(int)), SLOT(slotReceivedIdError(QDBusError)));
142 void KNotificationManager::insert(KNotification *n, int id)
144 d->notifications.insert(id, n);
147 void KNotificationManager::update(KNotification * n, int id)
149 if(id <= 0)
150 return;
152 QByteArray pixmapData;
153 QDataStream arg(&pixmapData, QIODevice::WriteOnly);
154 arg << n->pixmap();
156 d->knotify->call(QDBus::NoBlock, "update", id, n->text(), pixmapData , n->actions() );
159 void KNotificationManager::reemit(KNotification * n, int id)
161 QVariantList contextList;
162 typedef QPair<QString,QString> Context;
163 foreach (const Context& ctx, n->contexts())
165 // kDebug(299) << "add context " << ctx.first << "-" << ctx.second;
166 QVariantList vl;
167 vl << ctx.first << ctx.second;
168 contextList << vl;
171 d->knotify->call(QDBus::NoBlock, "reemit", id, contextList);
175 #include "knotificationmanager_p.moc"