on fe64332 help sign in
unity8/plugins/Unity/Launcher/dbusinterface.cpp
209 lines 7.0 kB history raw
1/*
2 * Copyright 2014 Canonical Ltd.
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU Lesser General Public License as published by
6 * the Free Software Foundation; version 3.
7 *
8 * This program 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
11 * GNU Lesser General Public License for more details.
12 *
13 * You should have received a copy of the GNU Lesser General Public License
14 * along with this program. If not, see <http://www.gnu.org/licenses/>.
15 *
16 * Authors:
17 * Michael Zanetti <michael.zanetti@canonical.com>
18 */
20#include "dbusinterface.h"
21#include "launchermodel.h"
22#include "launcheritem.h"
24#include <QDBusArgument>
25#include <QDBusConnection>
26#include <QDBusMessage>
27#include <QDebug>
29DBusInterface::DBusInterface(LauncherModel *parent):
30 UnityDBusVirtualObject("/com/canonical/Unity/Launcher", "com.canonical.Unity.Launcher", true, parent),
31 m_launcherModel(parent)
35QString DBusInterface::introspect(const QString &path) const
37 /* This case we should just list the nodes */
38 if (path == "/com/canonical/Unity/Launcher/" || path == "/com/canonical/Unity/Launcher") {
39 QString nodes;
41 // Add Refresh to introspect
42 nodes = "<interface name=\"com.canonical.Unity.Launcher\">"
43 "<method name=\"Refresh\"/>"
44 "</interface>";
46 // Add dynamic properties for launcher emblems
47 for (int i = 0; i < m_launcherModel->rowCount(); i++) {
48 nodes.append("<node name=\"");
49 nodes.append(encodeAppId(m_launcherModel->get(i)->appId()));
50 nodes.append("\"/>\n");
51 }
52 return nodes;
53 }
55 /* Should not happen, but let's handle it */
56 if (!path.startsWith("/com/canonical/Unity/Launcher")) {
57 return "";
58 }
60 /* Now we should be looking at a node */
61 QString nodeiface =
62 "<interface name=\"com.canonical.Unity.Launcher.Item\">"
63 "<property name=\"count\" type=\"i\" access=\"readwrite\" />"
64 "<property name=\"countVisible\" type=\"b\" access=\"readwrite\" />"
65 "<method name=\"Alert\" />"
66 "</interface>";
67 return nodeiface;
71QString DBusInterface::decodeAppId(const QString& path)
73 QByteArray bytes = path.toUtf8();
74 QByteArray decoded;
76 for (int i = 0; i < bytes.size(); ++i) {
77 char chr = bytes.at(i);
79 if (chr == '_') {
80 QString number;
81 number.append(bytes.at(i+1));
82 number.append(bytes.at(i+2));
84 bool okay;
85 char newchar = number.toUInt(&okay, 16);
86 if (okay)
87 decoded.append(newchar);
89 i += 2;
90 } else {
91 decoded.append(chr);
92 }
93 }
95 return QString::fromUtf8(decoded);
98QString DBusInterface::encodeAppId(const QString& appId)
100 QByteArray bytes = appId.toUtf8();
101 QString encoded;
103 for (int i = 0; i < bytes.size(); ++i) {
104 uchar chr = bytes.at(i);
106 if ((chr >= 'a' && chr <= 'z') ||
107 (chr >= 'A' && chr <= 'Z') ||
108 (chr >= '0' && chr <= '9'&& i != 0)) {
109 encoded.append(chr);
110 } else {
111 QString hexval = QString("_%1").arg(chr, 2, 16, QChar('0'));
112 encoded.append(hexval.toUpper());
113 }
114 }
116 return encoded;
119bool DBusInterface::handleMessage(const QDBusMessage& message, const QDBusConnection& connection)
121 /* Check to make sure we're getting properties on our interface */
122 if (message.type() != QDBusMessage::MessageType::MethodCallMessage) {
123 return false;
124 }
126 /* Break down the path to just the app id */
127 bool validpath = true;
128 QString pathtemp = message.path();
129 if (!pathtemp.startsWith("/com/canonical/Unity/Launcher/")) {
130 validpath = false;
131 }
132 pathtemp.remove("/com/canonical/Unity/Launcher/");
133 if (pathtemp.indexOf('/') >= 0) {
134 validpath = false;
135 }
137 /* Find ourselves an appid */
138 QString appid = decodeAppId(pathtemp);
140 // First handle methods of the Launcher interface
141 if (message.interface() == "com.canonical.Unity.Launcher") {
142 if (message.member() == "Refresh") {
143 QDBusMessage reply = message.createReply();
144 Q_EMIT refreshCalled();
145 return connection.send(reply);
146 }
147 } else if (message.interface() == "com.canonical.Unity.Launcher.Item") {
148 // Handle methods of the Launcher-Item interface
149 if (message.member() == "Alert" && validpath) {
150 QDBusMessage reply = message.createReply();
151 Q_EMIT alertCalled(appid);
152 return connection.send(reply);
153 }
154 }
156 // Now handle dynamic properties (for launcher emblems)
157 if (message.interface() != "org.freedesktop.DBus.Properties") {
158 return false;
159 }
161 if (message.member() != "GetAll" && message.arguments()[0].toString() != "com.canonical.Unity.Launcher.Item") {
162 return false;
163 }
165 if (!validpath) {
166 return false;
167 }
169 int index = m_launcherModel->findApplication(appid);
170 LauncherItem *item = static_cast<LauncherItem*>(m_launcherModel->get(index));
172 QVariantList retval;
173 if (message.member() == "Get") {
174 if (!item) {
175 return false;
176 }
177 if (message.arguments()[1].toString() == "count") {
178 retval.append(QVariant::fromValue(QDBusVariant(item->count())));
179 } else if (message.arguments()[1].toString() == "countVisible") {
180 retval.append(QVariant::fromValue(QDBusVariant(item->countVisible())));
181 }
182 } else if (message.member() == "Set") {
183 if (message.arguments()[1].toString() == "count") {
184 int newCount = message.arguments()[2].value<QDBusVariant>().variant().toInt();
185 if (!item || newCount != item->count()) {
186 Q_EMIT countChanged(appid, newCount);
187 notifyPropertyChanged("com.canonical.Unity.Launcher.Item", encodeAppId(appid), "count", QVariant(newCount));
188 }
189 } else if (message.arguments()[1].toString() == "countVisible") {
190 bool newVisible = message.arguments()[2].value<QDBusVariant>().variant().toBool();
191 if (!item || newVisible != item->countVisible()) {
192 Q_EMIT countVisibleChanged(appid, newVisible);
193 notifyPropertyChanged("com.canonical.Unity.Launcher.Item", encodeAppId(appid), "countVisible", newVisible);
194 }
195 }
196 } else if (message.member() == "GetAll") {
197 if (item) {
198 QVariantMap all;
199 all.insert("count", item->count());
200 all.insert("countVisible", item->countVisible());
201 retval.append(all);
202 }
203 } else {
204 return false;
205 }
207 QDBusMessage reply = message.createReply(retval);
208 return connection.send(reply);