add more spacing
[personal-kdebase.git] / workspace / plasma / applets / systemtray / protocols / notifications / dbusnotificationprotocol.cpp
blob5c4f164b0e2195c0e5ef47b305584fe71f664afe
1 /***************************************************************************
2 * fdoprotocol.cpp *
3 * *
4 * Copyright (C) 2008 Jason Stubbs <jasonbstubbs@gmail.com> *
5 * *
6 * This program is free software; you can redistribute it and/or modify *
7 * it under the terms of the GNU General Public License as published by *
8 * the Free Software Foundation; either version 2 of the License, or *
9 * (at your option) any later version. *
10 * *
11 * This program is distributed in the hope that it will be useful, *
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
14 * GNU General Public License for more details. *
15 * *
16 * You should have received a copy of the GNU General Public License *
17 * along with this program; if not, write to the *
18 * Free Software Foundation, Inc., *
19 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA . *
20 ***************************************************************************/
22 #include "dbusnotification.h"
23 #include "dbusnotificationprotocol.h"
25 #include <KConfigGroup>
26 #include <KIcon>
28 #include <plasma/dataenginemanager.h>
29 #include <plasma/service.h>
32 namespace SystemTray
35 static const char *engineName = "notifications";
37 DBusNotificationProtocol::DBusNotificationProtocol(QObject *parent)
38 : Protocol(parent),
39 m_engine(0)
44 DBusNotificationProtocol::~DBusNotificationProtocol()
46 if (m_engine) {
47 Plasma::DataEngineManager::self()->unloadEngine(engineName);
52 void DBusNotificationProtocol::init()
54 m_engine = Plasma::DataEngineManager::self()->loadEngine(engineName);
56 if (!m_engine->isValid()) {
57 m_engine = 0;
58 return;
61 connect(m_engine, SIGNAL(sourceAdded(const QString&)),
62 this, SLOT(prepareNotification(const QString&)));
63 connect(m_engine, SIGNAL(sourceRemoved(const QString&)),
64 this, SLOT(removeNotification(const QString&)));
68 void DBusNotificationProtocol::prepareNotification(const QString &source)
70 m_engine->connectSource(source, this);
74 void DBusNotificationProtocol::dataUpdated(const QString &source, const Plasma::DataEngine::Data &data)
76 bool isNew = !m_notifications.contains(source);
78 if (isNew) {
79 m_notifications[source] = new DBusNotification(source, this);
80 connect(m_notifications[source], SIGNAL(notificationDeleted(const QString&)),
81 this, SLOT(removeNotification(const QString&)));
82 connect(m_notifications[source], SIGNAL(actionTriggered(const QString&, const QString&)),
83 this, SLOT(relayAction(const QString&, const QString&)));
86 DBusNotification* notification = m_notifications[source];
87 notification->setApplicationName(data.value("appName").toString());
88 notification->setApplicationIcon(KIcon(data.value("appIcon").toString()));
89 notification->setEventId(data.value("eventId").toString());
90 notification->setSummary(data.value("summary").toString());
91 notification->setMessage(data.value("body").toString());
92 notification->setTimeout(data.value("expireTimeout").toInt());
94 QStringList codedActions = data.value("actions").toStringList();
95 if (codedActions.count() % 2 != 0) {
96 kDebug() << "Invalid actions" << codedActions << "from" << notification->applicationName();
97 codedActions.clear();
100 QHash<QString, QString> actions;
101 QStringList actionOrder;
103 while (!codedActions.isEmpty()) {
104 QString actionId = codedActions.takeFirst();
105 QString actionName = codedActions.takeFirst();
106 actions.insert(actionId, actionName);
107 actionOrder.append(actionId);
110 notification->setActions(actions);
111 notification->setActionOrder(actionOrder);
113 if (isNew) {
114 emit notificationCreated(notification);
115 } else {
116 emit notification->changed(notification);
121 void DBusNotificationProtocol::relayAction(const QString &source, const QString &actionId)
123 Plasma::Service *service = m_engine->serviceForSource(source);
124 KConfigGroup op = service->operationDescription("invokeAction");
126 if (op.isValid()) {
127 op.writeEntry("actionId", actionId);
128 service->startOperationCall(op);
129 } else {
130 kDebug() << "invalid operation";
135 void DBusNotificationProtocol::removeNotification(const QString &source)
137 if (m_notifications.contains(source)) {
138 m_notifications.take(source)->deleteLater();
144 #include "dbusnotificationprotocol.moc"