20 #include "dbusinterface.h"
21 #include "launchermodel.h"
22 #include "launcheritem.h"
24 #include <QDBusArgument>
25 #include <QDBusConnection>
26 #include <QDBusMessage>
29 DBusInterface::DBusInterface(LauncherModel *parent):
30 UnityDBusVirtualObject(QStringLiteral(
"/com/canonical/Unity/Launcher"), QStringLiteral(
"com.canonical.Unity.Launcher"), true, parent),
31 m_launcherModel(parent)
35 QString DBusInterface::introspect(
const QString &path)
const
38 if (path == QLatin1String(
"/com/canonical/Unity/Launcher/") || path == QLatin1String(
"/com/canonical/Unity/Launcher")) {
42 nodes = QStringLiteral(
"<interface name=\"com.canonical.Unity.Launcher\">"
43 "<method name=\"Refresh\"/>"
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");
56 if (!path.startsWith(QLatin1String(
"/com/canonical/Unity/Launcher"))) {
57 return QLatin1String(
"");
62 QStringLiteral(
"<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\" />"
72 QString DBusInterface::decodeAppId(
const QString& path)
74 QByteArray bytes = path.toUtf8();
77 for (
int i = 0; i < bytes.size(); ++i) {
78 char chr = bytes.at(i);
82 number.append(bytes.at(i+1));
83 number.append(bytes.at(i+2));
86 char newchar = number.toUInt(&okay, 16);
88 decoded.append(newchar);
96 return QString::fromUtf8(decoded);
99 QString DBusInterface::encodeAppId(
const QString& appId)
101 QByteArray bytes = appId.toUtf8();
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)) {
112 QString hexval = QStringLiteral(
"_%1").arg(chr, 2, 16, QChar(
'0'));
113 encoded.append(hexval.toUpper());
120 bool DBusInterface::handleMessage(
const QDBusMessage& message,
const QDBusConnection& connection)
123 if (message.type() != QDBusMessage::MessageType::MethodCallMessage) {
128 bool validpath =
true;
129 QString pathtemp = message.path();
130 if (!pathtemp.startsWith(QLatin1String(
"/com/canonical/Unity/Launcher/"))) {
133 pathtemp.remove(QStringLiteral(
"/com/canonical/Unity/Launcher/"));
134 if (pathtemp.indexOf(
'/') >= 0) {
139 QString appid = decodeAppId(pathtemp);
142 if (message.interface() == QLatin1String(
"com.canonical.Unity.Launcher")) {
143 if (message.member() == QLatin1String(
"Refresh")) {
144 QDBusMessage reply = message.createReply();
145 Q_EMIT refreshCalled();
146 return connection.send(reply);
148 }
else if (message.interface() == QLatin1String(
"com.canonical.Unity.Launcher.Item")) {
150 if (message.member() == QLatin1String(
"Alert") && validpath) {
151 QDBusMessage reply = message.createReply();
152 Q_EMIT alertCalled(appid);
153 return connection.send(reply);
158 if (message.interface() != QLatin1String(
"org.freedesktop.DBus.Properties")) {
162 if (message.member() == QLatin1String(
"Get") && (message.arguments().count() != 2 || message.arguments()[0].toString() != QLatin1String(
"com.canonical.Unity.Launcher.Item"))) {
166 if (message.member() == QLatin1String(
"Set") && (message.arguments().count() != 3 || message.arguments()[0].toString() != QLatin1String(
"com.canonical.Unity.Launcher.Item"))) {
174 int index = m_launcherModel->findApplication(appid);
175 LauncherItem *item =
static_cast<LauncherItem*
>(m_launcherModel->get(index));
178 if (message.member() == QLatin1String(
"Get")) {
179 QString cachedString = message.arguments()[1].toString();
183 if (cachedString == QLatin1String(
"count")) {
184 retval.append(QVariant::fromValue(QDBusVariant(item->count())));
185 }
else if (cachedString == QLatin1String(
"countVisible")) {
186 retval.append(QVariant::fromValue(QDBusVariant(item->countVisible())));
187 }
else if (cachedString == QLatin1String(
"progress")) {
188 retval.append(QVariant::fromValue(QDBusVariant(item->progress())));
190 }
else if (message.member() == QLatin1String(
"Set")) {
191 QString cachedString = message.arguments()[1].toString();
192 if (cachedString == QLatin1String(
"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(QStringLiteral(
"com.canonical.Unity.Launcher.Item"), encodeAppId(appid), QStringLiteral(
"count"), QVariant(newCount));
198 }
else if (cachedString == QLatin1String(
"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(QStringLiteral(
"com.canonical.Unity.Launcher.Item"), encodeAppId(appid), QStringLiteral(
"countVisible"), newVisible);
204 }
else if (cachedString == QLatin1String(
"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(QStringLiteral(
"com.canonical.Unity.Launcher.Item"), encodeAppId(appid), QStringLiteral(
"progress"), QVariant(newProgress));
211 }
else if (message.member() == QLatin1String(
"GetAll")) {
214 all.insert(QStringLiteral(
"count"), item->count());
215 all.insert(QStringLiteral(
"countVisible"), item->countVisible());
216 all.insert(QStringLiteral(
"progress"), item->progress());
223 QDBusMessage reply = message.createReply(retval);
224 return connection.send(reply);