on aa42683 help sign in
unity8/plugins/Unity/Launcher/dbusinterface.cpp
225 lines 7.9 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 "<property name=\"progress\" type=\"i\" access=\"readwrite\" />"
66 "<method name=\"Alert\" />"
67 "</interface>";
68 return nodeiface;
72QString DBusInterface::decodeAppId(const QString& path)
74 QByteArray bytes = path.toUtf8();
75 QByteArray decoded;
77 for (int i = 0; i < bytes.size(); ++i) {
78 char chr = bytes.at(i);
80 if (chr == '_') {
81 QString number;
82 number.append(bytes.at(i+1));
83 number.append(bytes.at(i+2));
85 bool okay;
86 char newchar = number.toUInt(&okay, 16);
87 if (okay)
88 decoded.append(newchar);
90 i += 2;
91 } else {
92 decoded.append(chr);
93 }
94 }
96 return QString::fromUtf8(decoded);
99QString DBusInterface::encodeAppId(const QString& appId)
101 QByteArray bytes = appId.toUtf8();
102 QString encoded;
104 for (int i = 0; i < bytes.size(); ++i) {
105 uchar chr = bytes.at(i);
107 if ((chr >= 'a' && chr <= 'z') ||
108 (chr >= 'A' && chr <= 'Z') ||
109 (chr >= '0' && chr <= '9'&& i != 0)) {
110 encoded.append(chr);
111 } else {
112 QString hexval = QString("_%1").arg(chr, 2, 16, QChar('0'));
113 encoded.append(hexval.toUpper());
114 }
115 }
117 return encoded;
120bool DBusInterface::handleMessage(const QDBusMessage& message, const QDBusConnection& connection)
122 /* Check to make sure we're getting properties on our interface */
123 if (message.type() != QDBusMessage::MessageType::MethodCallMessage) {
124 return false;
125 }
127 /* Break down the path to just the app id */
128 bool validpath = true;
129 QString pathtemp = message.path();
130 if (!pathtemp.startsWith("/com/canonical/Unity/Launcher/")) {
131 validpath = false;
132 }
133 pathtemp.remove("/com/canonical/Unity/Launcher/");
134 if (pathtemp.indexOf('/') >= 0) {
135 validpath = false;
136 }
138 /* Find ourselves an appid */
139 QString appid = decodeAppId(pathtemp);
141 // First handle methods of the Launcher interface
142 if (message.interface() == "com.canonical.Unity.Launcher") {
143 if (message.member() == "Refresh") {
144 QDBusMessage reply = message.createReply();
145 Q_EMIT refreshCalled();
146 return connection.send(reply);
147 }
148 } else if (message.interface() == "com.canonical.Unity.Launcher.Item") {
149 // Handle methods of the Launcher-Item interface
150 if (message.member() == "Alert" && validpath) {
151 QDBusMessage reply = message.createReply();
152 Q_EMIT alertCalled(appid);
153 return connection.send(reply);
154 }
155 }
157 // Now handle dynamic properties (for launcher emblems)
158 if (message.interface() != "org.freedesktop.DBus.Properties") {
159 return false;
160 }
162 if (message.member() == "Get" && (message.arguments().count() != 2 || message.arguments()[0].toString() != "com.canonical.Unity.Launcher.Item")) {
163 return false;
164 }
166 if (message.member() == "Set" && (message.arguments().count() != 3 || message.arguments()[0].toString() != "com.canonical.Unity.Launcher.Item")) {
167 return false;
168 }
170 if (!validpath) {
171 return false;
172 }
174 int index = m_launcherModel->findApplication(appid);
175 LauncherItem *item = static_cast<LauncherItem*>(m_launcherModel->get(index));
177 QVariantList retval;
178 if (message.member() == "Get") {
179 QString cachedString = message.arguments()[1].toString();
180 if (!item) {
181 return false;
182 }
183 if (cachedString == "count") {
184 retval.append(QVariant::fromValue(QDBusVariant(item->count())));
185 } else if (cachedString == "countVisible") {
186 retval.append(QVariant::fromValue(QDBusVariant(item->countVisible())));
187 } else if (cachedString == "progress") {
188 retval.append(QVariant::fromValue(QDBusVariant(item->progress())));
189 }
190 } else if (message.member() == "Set") {
191 QString cachedString = message.arguments()[1].toString();
192 if (cachedString == "count") {
193 int newCount = message.arguments()[2].value<QDBusVariant>().variant().toInt();
194 if (!item || newCount != item->count()) {
195 Q_EMIT countChanged(appid, newCount);
196 notifyPropertyChanged("com.canonical.Unity.Launcher.Item", encodeAppId(appid), "count", QVariant(newCount));
197 }
198 } else if (cachedString == "countVisible") {
199 bool newVisible = message.arguments()[2].value<QDBusVariant>().variant().toBool();
200 if (!item || newVisible != item->countVisible()) {
201 Q_EMIT countVisibleChanged(appid, newVisible);
202 notifyPropertyChanged("com.canonical.Unity.Launcher.Item", encodeAppId(appid), "countVisible", newVisible);
203 }
204 } else if (cachedString == "progress") {
205 int newProgress = message.arguments()[2].value<QDBusVariant>().variant().toInt();
206 if (!item || newProgress != item->progress()) {
207 Q_EMIT progressChanged(appid, newProgress);
208 notifyPropertyChanged("com.canonical.Unity.Launcher.Item", encodeAppId(appid), "progress", QVariant(newProgress));
209 }
210 }
211 } else if (message.member() == "GetAll") {
212 if (item) {
213 QVariantMap all;
214 all.insert("count", item->count());
215 all.insert("countVisible", item->countVisible());
216 all.insert("progress", item->progress());
217 retval.append(all);
218 }
219 } else {
220 return false;
221 }
223 QDBusMessage reply = message.createReply(retval);
224 return connection.send(reply);