KDEUI
knotificationmanager.cpp
Go to the documentation of this file.
00001 /* This file is part of the KDE libraries 00002 Copyright (C) 2005 Olivier Goffart <ogoffart at kde.org> 00003 00004 This library is free software; you can redistribute it and/or 00005 modify it under the terms of the GNU Library General Public 00006 License version 2 as published by the Free Software Foundation. 00007 00008 This library is distributed in the hope that it will be useful, 00009 but WITHOUT ANY WARRANTY; without even the implied warranty of 00010 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 00011 Library General Public License for more details. 00012 00013 You should have received a copy of the GNU Library General Public License 00014 along with this library; see the file COPYING.LIB. If not, write to 00015 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, 00016 Boston, MA 02110-1301, USA. 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 #include "knotify_interface.h" 00035 00036 typedef QHash<QString,QString> Dict; 00037 00038 struct KNotificationManager::Private 00039 { 00040 QHash<int , KNotification*> notifications; 00041 org::kde::KNotify *knotify; 00042 }; 00043 00044 KNotificationManager * KNotificationManager::self() 00045 { 00046 K_GLOBAL_STATIC(KNotificationManager, s_self) 00047 return s_self; 00048 } 00049 00050 00051 KNotificationManager::KNotificationManager() 00052 : d(new Private) 00053 { 00054 if (!QDBusConnection::sessionBus().interface()->isServiceRegistered("org.kde.knotify")) { 00055 QString error; 00056 int ret = KToolInvocation::startServiceByDesktopPath("knotify4.desktop", 00057 QStringList(), &error); 00058 if (ret > 0) { 00059 kError() << "Couldn't start knotify from knotify4.desktop: " << error << endl; 00060 } 00061 } 00062 d->knotify = 00063 new org::kde::KNotify(QLatin1String("org.kde.knotify"), QLatin1String("/Notify"), QDBusConnection::sessionBus(), this); 00064 connect(d->knotify, SIGNAL(notificationClosed(int)), 00065 this, SLOT(notificationClosed(int))); 00066 connect(d->knotify, SIGNAL(notificationActivated(int,int)), 00067 this, SLOT(notificationActivated(int,int))); 00068 } 00069 00070 00071 KNotificationManager::~KNotificationManager() 00072 { 00073 delete d->knotify; 00074 delete d; 00075 } 00076 00077 void KNotificationManager::notificationActivated( int id, int action ) 00078 { 00079 if(d->notifications.contains(id)) 00080 { 00081 kDebug(299) << id << " " << action; 00082 KNotification *n = d->notifications[id]; 00083 d->notifications.remove(id); 00084 n->activate( action ); 00085 } 00086 } 00087 00088 void KNotificationManager::notificationClosed( int id ) 00089 { 00090 if(d->notifications.contains(id)) 00091 { 00092 kDebug( 299 ) << id; 00093 KNotification *n = d->notifications[id]; 00094 d->notifications.remove(id); 00095 n->close(); 00096 } 00097 } 00098 00099 00100 void KNotificationManager::close( int id, bool force ) 00101 { 00102 if(force || d->notifications.contains(id)) { 00103 d->notifications.remove(id); 00104 kDebug( 299 ) << id; 00105 d->knotify->closeNotification(id); 00106 } 00107 } 00108 00109 bool KNotificationManager::notify( KNotification* n, const QPixmap &pix, 00110 const QStringList &actions, 00111 const KNotification::ContextList & contexts, 00112 const QString &appname) 00113 { 00114 WId winId=n->widget() ? n->widget()->topLevelWidget()->winId() : 0; 00115 00116 QByteArray pixmapData; 00117 { 00118 QBuffer buffer(&pixmapData); 00119 buffer.open(QIODevice::WriteOnly); 00120 pix.save(&buffer, "PNG"); 00121 } 00122 00123 QVariantList contextList; 00124 typedef QPair<QString,QString> Context; 00125 foreach (const Context& ctx, contexts) 00126 { 00127 QVariantList vl; 00128 vl << ctx.first << ctx.second; 00129 contextList << vl; 00130 } 00131 00132 // Persistent => 0 == infinite timeout 00133 // CloseOnTimeout => -1 == let the server decide 00134 int timeout = (n->flags() & KNotification::Persistent) ? 0 : -1; 00135 00136 QList<QVariant> args; 00137 args << n->eventId() << (appname.isEmpty() ? KGlobal::mainComponent().componentName() : appname); 00138 args.append(QVariant(contextList)); 00139 args << n->title() << n->text() << pixmapData << QVariant(actions) << timeout << qlonglong(winId) ; 00140 return d->knotify->callWithCallback( "event", args, n, SLOT(slotReceivedId(int)), SLOT(slotReceivedIdError(QDBusError))); 00141 } 00142 00143 void KNotificationManager::insert(KNotification *n, int id) 00144 { 00145 d->notifications.insert(id, n); 00146 } 00147 00148 void KNotificationManager::update(KNotification * n, int id) 00149 { 00150 if(id <= 0) 00151 return; 00152 00153 QByteArray pixmapData; 00154 if(!n->pixmap().isNull()) 00155 { 00156 QBuffer buffer(&pixmapData); 00157 buffer.open(QIODevice::WriteOnly); 00158 n->pixmap().save(&buffer, "PNG"); 00159 } 00160 00161 d->knotify->update(id, n->title(), n->text(), pixmapData , n->actions() ); 00162 } 00163 00164 void KNotificationManager::reemit(KNotification * n, int id) 00165 { 00166 QVariantList contextList; 00167 typedef QPair<QString,QString> Context; 00168 foreach (const Context& ctx, n->contexts()) 00169 { 00170 // kDebug(299) << "add context " << ctx.first << "-" << ctx.second; 00171 QVariantList vl; 00172 vl << ctx.first << ctx.second; 00173 contextList << vl; 00174 } 00175 00176 d->knotify->reemit(id, contextList); 00177 } 00178 00179 00180 #include "knotificationmanager_p.moc"
KDE 4.6 API Reference