2 * Copyright (C) 2013 Canonical, Ltd.
5 * Michael Terry <michael.terry@canonical.com>
6 * Michael Zanetti <michael.zanetti@canonical.com>
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; version 3.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program. If not, see <http://www.gnu.org/licenses/>.
21#include "AccountsServiceDBusAdaptor.h"
22#include "launcherbackend.h"
25#include <QDBusArgument>
29#include <QStandardPaths>
31class LauncherBackendItem
41LauncherBackend::LauncherBackend(QObject *parent):
42 QDBusVirtualObject(parent),
45#ifndef LAUNCHER_TESTING
46 m_accounts = new AccountsServiceDBusAdaptor(this);
48 m_user = qgetenv("USER");
51 /* Set up ourselves on DBus */
52 QDBusConnection con = QDBusConnection::sessionBus();
53 if (!con.registerService("com.canonical.unity.launcher"))
54 qDebug() << "Unable to register launcher name";
55 if (!con.registerVirtualObject("/com/canonical/unity/launcher", this, QDBusConnection::VirtualObjectRegisterOption::SubPath))
56 qDebug() << "Unable to register launcher object";
59LauncherBackend::~LauncherBackend()
61 /* Remove oursevles from DBus */
62 QDBusConnection con = QDBusConnection::sessionBus();
63 con.unregisterService("com.canonical.unity.launcher");
64 con.unregisterObject("/com/canonical/unity/launcher");
69 Q_FOREACH(LauncherBackendItem *item, m_itemCache) {
75QStringList LauncherBackend::storedApplications() const
80void LauncherBackend::setStoredApplications(const QStringList &appIds)
82 if (appIds.count() < m_storedApps.count()) {
83 Q_FOREACH(const QString &appId, m_storedApps) {
84 if (!appIds.contains(appId)) {
85 delete m_itemCache.take(appId);
89 m_storedApps = appIds;
90 Q_FOREACH(const QString &appId, appIds) {
91 if (!m_itemCache.contains(appId)) {
92 QString df = findDesktopFile(appId);
94 LauncherBackendItem *item = parseDesktopFile(df);
95 m_itemCache.insert(appId, item);
97 // Cannot find any data for that app... ignoring it.
98 qWarning() << "cannot find desktop file for" << appId << ". discarding app.";
99 m_storedApps.removeAll(appId);
106QString LauncherBackend::desktopFile(const QString &appId) const
108 LauncherBackendItem *item = m_itemCache.value(appId);
110 return item->desktopFile;
113 return findDesktopFile(appId);
116QString LauncherBackend::displayName(const QString &appId) const
118 LauncherBackendItem *item = m_itemCache.value(appId);
120 return item->displayName;
123 QString df = findDesktopFile(appId);
125 LauncherBackendItem *item = parseDesktopFile(df);
126 m_itemCache.insert(appId, item);
127 return item->displayName;
133QString LauncherBackend::icon(const QString &appId) const
136 LauncherBackendItem *item = getItem(appId);
138 iconName = item->icon;
144QList<QuickListEntry> LauncherBackend::quickList(const QString &appId) const
146 // TODO: Get static (from .desktop file) and dynamic (from the app itself)
147 // entries and return them here. Frontend related entries (like "Pin to launcher")
148 // don't matter here. This is just the backend part.
149 // TODO: emit quickListChanged() when the dynamic part changes
151 return QList<QuickListEntry>();
154int LauncherBackend::progress(const QString &appId) const
156 // TODO: Return value for progress emblem.
157 // TODO: emit progressChanged() when this value changes.
162int LauncherBackend::count(const QString &appId) const
165 LauncherBackendItem *item = getItem(appId);
168 if (item->countVisible) {
176void LauncherBackend::setCount(const QString &appId, int count) const
178 LauncherBackendItem *item = getItem(appId);
180 bool emitchange = false;
182 emitchange = (item->count != count);
187 /* TODO: This needs to use the accessor to handle the visibility
188 correctly, but when we have the two properties we can just use
190 Q_EMIT countChanged(appId, this->count(appId));
191 QVariant vcount(item->count);
192 emitPropChangedDbus(appId, "count", vcount);
196bool LauncherBackend::countVisible(const QString &appId) const
198 bool visible = false;
199 LauncherBackendItem *item = getItem(appId);
202 visible = item->countVisible;
208void LauncherBackend::setCountVisible(const QString &appId, bool visible) const
210 LauncherBackendItem *item = getItem(appId);
212 bool emitchange = false;
214 emitchange = (item->countVisible != visible);
215 item->countVisible = visible;
217 qDebug() << "Unable to find:" << appId;
221 /* TODO: Because we're using visible in determining the
222 count we need to emit a count changed as well */
223 Q_EMIT countChanged(appId, this->count(appId));
224 Q_EMIT countVisibleChanged(appId, item->countVisible);
225 QVariant vCountVisible(item->countVisible);
226 emitPropChangedDbus(appId, "countVisible", vCountVisible);
230void LauncherBackend::setUser(const QString &username)
232 if (qgetenv("USER") == "lightdm" && m_user != username) {
238void LauncherBackend::triggerQuickListAction(const QString &appId, const QString &quickListId)
240 // TODO: execute the given quicklist action
242 Q_UNUSED(quickListId)
245void LauncherBackend::syncFromAccounts()
247 QList<QVariantMap> apps;
248 bool defaults = true;
250 m_storedApps.clear();
252 if (m_accounts && !m_user.isEmpty()) {
253 QVariant variant = m_accounts->getUserProperty(m_user, "com.canonical.unity.AccountsService", "launcher-items");
254 if (variant.isValid() && variant.canConvert<QDBusArgument>()) {
255 apps = qdbus_cast<QList<QVariantMap>>(variant.value<QDBusArgument>());
256 defaults = isDefaultsItem(apps);
260 if (m_accounts && defaults) { // Checking accounts as it'll be null when !useStorage
261 QGSettings gSettings("com.canonical.Unity.Launcher", "/com/canonical/unity/launcher/");
262 Q_FOREACH(const QString &entry, gSettings.get("favorites").toStringList()) {
263 if (entry.startsWith("application://")) {
264 QString appId = entry;
265 // Transform "application://foobar.desktop" to "foobar"
266 appId.remove("application://");
267 if (appId.endsWith(".desktop")) {
270 QString df = findDesktopFile(appId);
273 m_storedApps << appId;
275 if (!m_itemCache.contains(appId)) {
276 m_itemCache.insert(appId, parseDesktopFile(df));
282 for (const QVariant &app: apps) {
283 loadFromVariant(app.toMap());
288void LauncherBackend::syncToAccounts()
290 if (m_accounts && !m_user.isEmpty()) {
291 QList<QVariantMap> items;
293 Q_FOREACH(const QString &appId, m_storedApps) {
294 items << itemToVariant(appId);
297 m_accounts->setUserProperty(m_user, "com.canonical.unity.AccountsService", "launcher-items", QVariant::fromValue(items));
301QString LauncherBackend::findDesktopFile(const QString &appId) const
304 QString helper = appId;
306 QStringList searchDirs = QStandardPaths::standardLocations(QStandardPaths::ApplicationsLocation);
307#ifdef LAUNCHER_TESTING
313 helper = helper.replace(dashPos, 1, '/');
316 Q_FOREACH(const QString &searchDir, searchDirs) {
317 QFileInfo fileInfo(QDir(searchDir), helper + ".desktop");
318 if (fileInfo.exists()) {
319 return fileInfo.absoluteFilePath();
323 dashPos = helper.indexOf("-");
324 } while (dashPos != -1);
329LauncherBackendItem* LauncherBackend::parseDesktopFile(const QString &desktopFile) const
331 QSettings settings(desktopFile, QSettings::IniFormat);
333 LauncherBackendItem* item = new LauncherBackendItem();
334 item->desktopFile = desktopFile;
335 item->displayName = settings.value("Desktop Entry/Name").toString();
337 QString iconString = settings.value("Desktop Entry/Icon").toString();
338 QString pathString = settings.value("Desktop Entry/Path").toString();
339 if (QFileInfo(iconString).exists()) {
340 item->icon = QFileInfo(iconString).absoluteFilePath();
341 } else if (QFileInfo(pathString + '/' + iconString).exists()) {
342 item->icon = pathString + '/' + iconString;
344 item->icon = "image://theme/" + iconString;
347 /* TODO: These should be looked up in a cache somewhere */
349 item->countVisible = false;
354/* Gets an item, and tries to create a new one if we need it to */
355LauncherBackendItem* LauncherBackend::getItem (const QString &appId) const
357 LauncherBackendItem *item = m_itemCache[appId];
359 QString df = findDesktopFile(appId);
361 item = parseDesktopFile(df);
363 m_itemCache[appId] = item;
365 qDebug() << "Unable to parse desktop file for:" << appId;
368 qDebug() << "Unable to find desktop file for:" << appId;
373 qDebug() << "Unable to find item for: " << appId;
378void LauncherBackend::loadFromVariant(const QVariantMap &details)
380 if (!details.contains("id")) {
383 QString appId = details.value("id").toString();
385 LauncherBackendItem *item = m_itemCache.value(appId);
390 item = new LauncherBackendItem();
392 item->desktopFile = details.value("desktopFile").toString();
393 item->displayName = details.value("name").toString();
394 item->icon = details.value("icon").toString();
395 item->count = details.value("count").toInt();
396 item->countVisible = details.value("countVisible").toBool();
398 m_itemCache.insert(appId, item);
399 m_storedApps.append(appId);
402QVariantMap LauncherBackend::itemToVariant(const QString &appId) const
404 LauncherBackendItem *item = m_itemCache.value(appId);
406 details.insert("id", appId);
407 details.insert("name", item->displayName);
408 details.insert("icon", item->icon);
409 details.insert("desktopFile", item->desktopFile);
410 details.insert("count", item->count);
411 details.insert("countVisible", item->countVisible);
415bool LauncherBackend::isDefaultsItem(const QList<QVariantMap> &apps) const
417 // To differentiate between an empty list and a list that hasn't been set
418 // yet (and should thus be populated with the defaults), we use a special
419 // list of one item with the 'defaults' field set to true.
420 return (apps.size() == 1 && apps[0].value("defaults").toBool());
423bool LauncherBackend::handleMessage(const QDBusMessage& message, const QDBusConnection& connection)
425 /* Check to make sure we're getting properties on our interface */
426 if (message.type() != QDBusMessage::MessageType::MethodCallMessage)
428 if (message.interface() != "org.freedesktop.DBus.Properties")
430 if (message.arguments()[0].toString() != "com.canonical.unity.Launcher.Item")
433 /* Break down the path to just the app id */
434 QString pathtemp = message.path();
435 if (!pathtemp.startsWith("/com/canonical/unity/launcher/"))
437 pathtemp.remove("/com/canonical/unity/launcher/");
438 if (pathtemp.indexOf('/') >= 0)
441 /* Find ourselves an appid */
442 QString appid = decodeAppId(pathtemp);
445 if (message.member() == "Get") {
446 if (message.arguments()[1].toString() == "count")
447 retval.append(this->count(appid));
448 else if (message.arguments()[1].toString() == "countVisible")
449 retval.append(this->countVisible(appid));
450 } else if (message.member() == "Set") {
451 if (message.arguments()[1].toString() == "count")
452 this->setCount(appid, message.arguments()[2].value<QDBusVariant>().variant().toInt());
453 else if (message.arguments()[1].toString() == "countVisible")
454 this->setCountVisible(appid, message.arguments()[2].value<QDBusVariant>().variant().toBool());
455 } else if (message.member() == "GetAll") {
456 retval.append(this->itemToVariant(appid));
461 QDBusMessage reply = message.createReply(retval);
462 return connection.send(reply);
465QString LauncherBackend::introspect (const QString &path) const
467 /* This case we should just list the nodes */
468 if (path == "/com/canonical/unity/launcher/" || path == "/com/canonical/unity/launcher") {
471 Q_FOREACH(const QString &appId, m_itemCache.keys()) {
472 nodes.append("<node name=\"");
473 nodes.append(encodeAppId(appId));
474 nodes.append("\"/>\n");
480 /* Should not happen, but let's handle it */
481 if (!path.startsWith("/com/canonical/unity/launcher")) {
485 /* Now we should be looking at a node */
487 "<interface name=\"com.canonical.unity.Launcher.Item\">"
488 "<property name=\"count\" type=\"i\" access=\"readwrite\" />"
489 "<property name=\"countVisible\" type=\"b\" access=\"readwrite\" />"
494QString LauncherBackend::decodeAppId (const QString& path)
496 QByteArray bytes = path.toUtf8();
499 for (int i = 0; i < bytes.size(); ++i) {
500 char chr = bytes.at(i);
504 number.append(bytes.at(i+1));
505 number.append(bytes.at(i+2));
508 char newchar = number.toUInt(&okay, 16);
510 decoded.append(newchar);
521QString LauncherBackend::encodeAppId (const QString& appId)
523 QByteArray bytes = appId.toUtf8();
526 for (int i = 0; i < bytes.size(); ++i) {
527 char chr = bytes.at(i);
529 if ((chr >= 'a' && chr <= 'z') ||
530 (chr >= 'A' && chr <= 'Z') ||
531 (chr >= '0' && chr <= '9'&& i != 0)) {
534 QString hexval = QString("_%1").arg((ushort)chr, 2, 16, QChar('0'));
535 encoded.append(hexval);
542void LauncherBackend::emitPropChangedDbus (const QString& appId, const QString& property, QVariant &value) const
544 QString path("/com/canonical/unity/launcher/");
545 path.append(encodeAppId(appId));
547 QDBusMessage message = QDBusMessage::createSignal(path, "org.freedesktop.DBus.Properties", "PropertiesChanged");
549 QList<QVariant> arguments;
550 QVariantHash changedprops;
551 changedprops[property] = value;
552 QVariantList deletedprops;
554 arguments.append(changedprops);
555 arguments.append(deletedprops);
557 message.setArguments(arguments);
559 QDBusConnection con = QDBusConnection::sessionBus();
keysesc to close
- j k
- move down, up
- enter
- open
- esc
- back, or up a level
- 1 2 3 4
- readme, files, commits, refs
- /
- filter
- y
- copy the clone command
- c
- compare the chosen commit with another (or shift-click both)
- ?
- show or hide this list