Debian Codegraph @

src/dbus.cpp

raw symbols SCIP
1 //
2 // C++ Implementation: DBus
3 //
4 // Description:
5 //
6 //
7 // Author: hooey <[email protected]>, (C) 2008
8 //
9 // Copyright: See COPYING file that comes with this distribution
10 //
11 //
12
13 #include "dbus.h"
14 #include "imageconverter.h"
15 #include <QtDBus>
16 #include <QtDebug>
17
18 namespace QTerm
19 {
20 DBus * DBus::m_instance = 0;
21 static const QString dbusServiceName = "org.freedesktop.Notifications";
22 static const QString dbusInterfaceName = "org.freedesktop.Notifications";
23 static const QString dbusPath = "/org/freedesktop/Notifications";
24
25 DBus * DBus::instance()
26 {
27 static QMutex mutex;
28 if (!m_instance) {
29 mutex.lock();
30
31 if (!m_instance)
32 m_instance = new DBus;
33
34 mutex.unlock();
35 }
36
37 return m_instance;
38 }
39
40 DBus::DBus()
41 :m_notificationAvailable(false),m_idList(),m_serverCapabilities()
42 {
43 m_idList.clear();
44 QDBusConnectionInterface* interface = QDBusConnection::sessionBus().interface();
45 m_notificationAvailable = interface && interface->isServiceRegistered(dbusServiceName);
46
47 if( m_notificationAvailable ) {
48 qDebug() << "using" << dbusServiceName << "for popups";
49 createConnection();
50 }
51
52 // to catch register/unregister events from service in runtime
53 connect(interface, SIGNAL(serviceOwnerChanged(const QString&, const QString&, const QString&)), this, SLOT(slotServiceOwnerChanged(const QString&, const QString&, const QString&)));
54
55 }
56
57 void DBus::createConnection()
58 {
59 bool connected = QDBusConnection::sessionBus().connect(QString(), // from any service
60 dbusPath,
61 dbusInterfaceName,
62 "ActionInvoked",
63 this,
64 SLOT(slotNotificationActionInvoked(uint,const QString&)));
65 if (!connected) {
66 qDebug() << "warning: failed to connect to ActionInvoked dbus signal";
67 }
68 connected = QDBusConnection::sessionBus().connect(QString(), // from any service
69 dbusPath,
70 dbusInterfaceName,
71 "NotificationClosed",
72 this,
73 SLOT(slotNotificationClosed(uint,uint)));
74 if (!connected) {
75 qDebug() << "warning: failed to connect to NotificationClosed dbus signal";
76 }
77 checkCapabilities();
78 }
79
80 void DBus::checkCapabilities()
81 {
82 QDBusMessage message = QDBusMessage::createMethodCall( dbusServiceName, dbusPath, dbusInterfaceName, "GetCapabilities" );
83 QDBusReply<QStringList> replyMsg = QDBusConnection::sessionBus().call(message);
84 m_serverCapabilities = replyMsg.value();
85 }
86
87 void DBus::slotServiceOwnerChanged( const QString & serviceName, const QString & oldOwner, const QString & newOwner )
88 {
89 if(serviceName == dbusServiceName)
90 {
91 if(oldOwner.isEmpty())
92 {
93 m_idList.clear();
94 m_notificationAvailable = true;
95 qDebug() << dbusServiceName << " was registered on bus, now using it to show popups";
96 // connect to action invocation signals
97 createConnection();
98 }
99 if(newOwner.isEmpty())
100 {
101 m_idList.clear();
102 m_notificationAvailable = false;
103 qDebug() << dbusServiceName << " was unregistered from bus, using passive popups from now on";
104 }
105 }
106 }
107
108 bool DBus::sendNotification(const QString & summary, const QString & body, const QImage & image, QList<DBus::Action> actions)
109 {
110 QDBusMessage message = QDBusMessage::createMethodCall( dbusServiceName, dbusPath, dbusInterfaceName, "Notify" );
111 uint id = 0;
112 QList<QVariant> args;
113 args.append("QTerm");
114 args.append(id); // If I send 0 directly, this will be an int32 instead of uin32, resulting an unknown method error.
115 // args.append(QString());
116 args.append("qterm"); // Icon name
117 args.append(summary); // Title
118 args.append(body); // Text
119 QStringList actionList;
120 if (m_serverCapabilities.contains("actions")) {
121 foreach (DBus::Action action, actions) {
122 if (action==DBus::Show_QTerm) {
123 actionList.append(QString::number(DBus::Show_QTerm));
124 actionList.append("Show QTerm");
125 }
126 }
127 }
128 args.append(actionList);
129 QVariantMap map;
130 if (!image.isNull())
131 map["image_data"] = ImageConverter::variantForImage(image);
132 args.append(map);
133 args.append(-1);//-1 means: notification server decides
134 message.setArguments(args);
135 QDBusMessage replyMsg = QDBusConnection::sessionBus().call(message);
136 if(replyMsg.type() == QDBusMessage::ReplyMessage) {
137 if (!replyMsg.arguments().isEmpty()) {
138 uint dbus_id = replyMsg.arguments().at(0).toUInt();
139 m_idList.append(dbus_id);
140 } else {
141 qDebug() << "error: received reply with no arguments";
142 }
143 } else if (replyMsg.type() == QDBusMessage::ErrorMessage) {
144 qDebug() << "error: failed to send dbus message";
145 } else {
146 qDebug() << "unexpected reply type";
147 }
148 //TODO: handle errors
149 return true;
150 }
151
152 void DBus::slotNotificationActionInvoked(uint id, const QString action)
153 {
154 if (m_idList.contains(id)) {
155 if (action.toInt() == DBus::Show_QTerm) {
156 closeNotification(id);
157 emit showQTerm();
158 }
159 }
160 }
161
162 void DBus::slotNotificationClosed(uint id, uint reason)
163 {
164 if (m_idList.contains(id))
165 m_idList.removeAll(id);
166 }
167
168 void DBus::closeNotification(uint id)
169 {
170 if (m_idList.contains(id)) {
171 m_idList.removeAll(id);
172 QDBusMessage m = QDBusMessage::createMethodCall( dbusServiceName, dbusPath, dbusInterfaceName, "CloseNotification" );
173 QList<QVariant> args;
174 args.append(id);
175 m.setArguments( args );
176 bool queued = QDBusConnection::sessionBus().send(m);
177 if (!queued) {
178 qDebug() << "warning: failed to queue dbus message";
179 }
180 }
181 }
182
183 bool DBus::notificationAvailable() const
184 {
185 return m_notificationAvailable;
186 }
187
188 } // namespace QTerm
189 #include <moc_dbus.cpp>