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
40LauncherBackend::LauncherBackend(bool greeterMode, QObject *parent):
41 QDBusVirtualObject(parent),
42 m_greeterMode(greeterMode),
45#ifndef LAUNCHER_TESTING
46 m_accounts = new AccountsServiceDBusAdaptor(this);
47 connect(m_accounts, SIGNAL(propertiesChanged(QString, QString, QStringList)),
48 this, SLOT(accountsListChanged(QString, QString, QStringList)));
50 m_user = qgetenv("USER");
53 /* Set up ourselves on DBus */
54 QDBusConnection con = QDBusConnection::sessionBus();
55 if (!con.registerService("com.canonical.Unity.Launcher")) {
56 qDebug() << "Unable to register launcher name";
58 if (!con.registerVirtualObject("/com/canonical/Unity/Launcher", this, QDBusConnection::VirtualObjectRegisterOption::SubPath)) {
59 qDebug() << "Unable to register launcher object";
63LauncherBackend::~LauncherBackend()
65 /* Remove oursevles from DBus */
66 QDBusConnection con = QDBusConnection::sessionBus();
67 con.unregisterService("com.canonical.Unity.Launcher");
68 con.unregisterObject("/com/canonical/Unity/Launcher");
73 Q_FOREACH(LauncherBackendItem *item, m_itemCache) {
79QStringList LauncherBackend::storedApplications() const
84void LauncherBackend::setStoredApplications(const QStringList &appIds)
86 if (appIds.count() < m_storedApps.count()) {
87 Q_FOREACH(const QString &appId, m_storedApps) {
88 if (!appIds.contains(appId)) {
89 delete m_itemCache.take(appId);
93 m_storedApps = appIds;
94 Q_FOREACH(const QString &appId, appIds) {
95 if (!m_itemCache.contains(appId)) {
96 QString df = findDesktopFile(appId);
98 LauncherBackendItem *item = parseDesktopFile(df);
99 m_itemCache.insert(appId, item);
101 // Cannot find any data for that app... ignoring it.
102 qWarning() << "cannot find desktop file for" << appId << ". discarding app.";
103 m_storedApps.removeAll(appId);
110QString LauncherBackend::desktopFile(const QString &appId) const
112 return findDesktopFile(appId);
115QString LauncherBackend::displayName(const QString &appId) const
117 LauncherBackendItem *item = m_itemCache.value(appId, nullptr);
119 return item->displayName;
122 QString df = findDesktopFile(appId);
124 LauncherBackendItem *item = parseDesktopFile(df);
125 m_itemCache.insert(appId, item);
126 return item->displayName;
132QString LauncherBackend::icon(const QString &appId) const
135 LauncherBackendItem *item = getItem(appId);
137 iconName = item->icon;
143QList<QuickListEntry> LauncherBackend::quickList(const QString &appId) const
145 // TODO: Get static (from .desktop file) and dynamic (from the app itself)
146 // entries and return them here. Frontend related entries (like "Pin to launcher")
147 // don't matter here. This is just the backend part.
148 // TODO: emit quickListChanged() when the dynamic part changes
150 return QList<QuickListEntry>();
153int LauncherBackend::progress(const QString &appId) const
155 // TODO: Return value for progress emblem.
156 // TODO: emit progressChanged() when this value changes.
161int LauncherBackend::count(const QString &appId) const
164 LauncherBackendItem *item = getItem(appId);
167 if (item->countVisible) {
175void LauncherBackend::setCount(const QString &appId, int count) const
177 LauncherBackendItem *item = getItem(appId);
179 bool emitchange = false;
181 emitchange = (item->count != count);
186 /* TODO: This needs to use the accessor to handle the visibility
187 correctly, but when we have the two properties we can just use
189 Q_EMIT countChanged(appId, this->count(appId));
190 QVariant vcount(item->count);
191 emitPropChangedDbus(appId, "count", vcount);
195bool LauncherBackend::countVisible(const QString &appId) const
197 bool visible = false;
198 LauncherBackendItem *item = getItem(appId);
201 visible = item->countVisible;
207void LauncherBackend::setCountVisible(const QString &appId, bool visible) const
209 LauncherBackendItem *item = getItem(appId);
211 bool emitchange = false;
213 emitchange = (item->countVisible != visible);
214 item->countVisible = visible;
216 qDebug() << "Unable to find:" << appId;
220 /* TODO: Because we're using visible in determining the
221 count we need to emit a count changed as well */
222 Q_EMIT countChanged(appId, this->count(appId));
223 Q_EMIT countVisibleChanged(appId, item->countVisible);
224 QVariant vCountVisible(item->countVisible);
225 emitPropChangedDbus(appId, "countVisible", vCountVisible);
229void LauncherBackend::setUser(const QString &username)
231 if (m_greeterMode && m_user != username) {
237void LauncherBackend::triggerQuickListAction(const QString &appId, const QString &quickListId)
239 // TODO: execute the given quicklist action
241 Q_UNUSED(quickListId)
244void LauncherBackend::accountsListChanged(const QString &user, const QString &interface, const QStringList &changed)
246 if (m_greeterMode && user == m_user &&
247 interface == "com.canonical.unity.AccountsService" &&
248 changed.contains("launcher-items")) {
253void LauncherBackend::syncFromAccounts()
255 QList<QVariantMap> apps;
256 bool defaults = true;
258 m_storedApps.clear();
260 if (m_accounts && !m_user.isEmpty()) {
261 QVariant variant = m_accounts->getUserProperty(m_user, "com.canonical.unity.AccountsService", "launcher-items");
262 if (variant.isValid() && variant.canConvert<QDBusArgument>()) {
263 apps = qdbus_cast<QList<QVariantMap>>(variant.value<QDBusArgument>());
264 defaults = isDefaultsItem(apps);
268 if (m_accounts && defaults) { // Checking accounts as it'll be null when !useStorage
269 QGSettings gSettings("com.canonical.Unity.Launcher", "/com/canonical/unity/launcher/");
270 Q_FOREACH(const QString &entry, gSettings.get("favorites").toStringList()) {
271 if (entry.startsWith("application://")) {
272 QString appId = entry;
273 // Transform "application://foobar.desktop" to "application://foobar"
274 appId.remove("application://");
275 if (appId.endsWith(".desktop")) {
278 QString df = findDesktopFile(appId);
281 m_storedApps << appId;
283 if (!m_itemCache.contains(appId)) {
284 m_itemCache.insert(appId, parseDesktopFile(df));
288 if (entry.startsWith("appid://")) {
289 QString appId = entry;
290 appId.remove("appid://");
291 // Strip hook name and current-user-version in case its there
293 if (appId.split('/').count() != 3) {
294 qWarning() << "ignoring entry " + appId + ". Not a valid appId.";
297 appId = appId.split('/').first() + "_" + appId.split('/').at(1);
298 QString df = findDesktopFile(appId);
301 m_storedApps << appId;
303 if (!m_itemCache.contains(appId)) {
304 m_itemCache.insert(appId, parseDesktopFile(df));
310 for (const QVariant &app: apps) {
311 loadFromVariant(app.toMap());
315 Q_EMIT refreshApplications();
318void LauncherBackend::syncToAccounts()
320 if (m_accounts && !m_user.isEmpty()) {
321 QList<QVariantMap> items;
323 Q_FOREACH(const QString &appId, m_storedApps) {
324 items << itemToVariant(appId);
327 m_accounts->setUserProperty(m_user, "com.canonical.unity.AccountsService", "launcher-items", QVariant::fromValue(items));
331QString LauncherBackend::findDesktopFile(const QString &appId) const
334 QString helper = appId;
336 QStringList searchDirs = QStandardPaths::standardLocations(QStandardPaths::ApplicationsLocation);
337#ifdef LAUNCHER_TESTING
343 helper = helper.replace(dashPos, 1, '/');
346 Q_FOREACH(const QString &searchDirName, searchDirs) {
347 QDir searchDir(searchDirName);
348 Q_FOREACH(const QString &desktopFile, searchDir.entryList(QStringList() << "*.desktop")) {
349 if (desktopFile.startsWith(helper)) {
350 QFileInfo fileInfo(searchDir, desktopFile);
351 return fileInfo.absoluteFilePath();
356 dashPos = helper.indexOf("-");
357 } while (dashPos != -1);
362LauncherBackendItem* LauncherBackend::parseDesktopFile(const QString &desktopFile) const
364 QSettings settings(desktopFile, QSettings::IniFormat);
366 LauncherBackendItem* item = new LauncherBackendItem();
367 item->displayName = settings.value("Desktop Entry/Name").toString();
369 QString iconString = settings.value("Desktop Entry/Icon").toString();
370 QString pathString = settings.value("Desktop Entry/Path").toString();
371 if (QFileInfo(iconString).exists()) {
372 item->icon = QFileInfo(iconString).absoluteFilePath();
373 } else if (QFileInfo(pathString + '/' + iconString).exists()) {
374 item->icon = pathString + '/' + iconString;
376 item->icon = "image://theme/" + iconString;
379 /* TODO: These should be looked up in a cache somewhere */
381 item->countVisible = false;
386/* Gets an item, and tries to create a new one if we need it to */
387LauncherBackendItem* LauncherBackend::getItem(const QString &appId) const
389 LauncherBackendItem *item = m_itemCache.value(appId, nullptr);
391 QString df = findDesktopFile(appId);
393 item = parseDesktopFile(df);
395 m_itemCache[appId] = item;
397 qWarning() << "Unable to parse desktop file for" << appId << "path" << df;
400 qWarning() << "Unable to find desktop file for:" << appId;
405 qWarning() << "Unable to find item for: " << appId;
410void LauncherBackend::loadFromVariant(const QVariantMap &details)
412 if (!details.contains("id")) {
415 QString appId = details.value("id").toString();
417 LauncherBackendItem *item = m_itemCache.value(appId, nullptr);
422 item = new LauncherBackendItem();
424 item->displayName = details.value("name").toString();
425 item->icon = details.value("icon").toString();
426 item->count = details.value("count").toInt();
427 item->countVisible = details.value("countVisible").toBool();
429 m_itemCache.insert(appId, item);
430 m_storedApps.append(appId);
433QVariantMap LauncherBackend::itemToVariant(const QString &appId) const
435 LauncherBackendItem *item = m_itemCache.value(appId);
437 details.insert("id", appId);
438 details.insert("name", item->displayName);
439 details.insert("icon", item->icon);
440 details.insert("count", item->count);
441 details.insert("countVisible", item->countVisible);
445bool LauncherBackend::isDefaultsItem(const QList<QVariantMap> &apps) const
447 // To differentiate between an empty list and a list that hasn't been set
448 // yet (and should thus be populated with the defaults), we use a special
449 // list of one item with the 'defaults' field set to true.
450 return (apps.size() == 1 && apps[0].value("defaults").toBool());
453bool LauncherBackend::handleMessage(const QDBusMessage& message, const QDBusConnection& connection)
455 /* Check to make sure we're getting properties on our interface */
456 if (message.type() != QDBusMessage::MessageType::MethodCallMessage) {
459 if (message.interface() != "org.freedesktop.DBus.Properties") {
462 if (message.arguments()[0].toString() != "com.canonical.Unity.Launcher.Item") {
466 /* Break down the path to just the app id */
467 QString pathtemp = message.path();
468 if (!pathtemp.startsWith("/com/canonical/Unity/Launcher/")) {
471 pathtemp.remove("/com/canonical/Unity/Launcher/");
472 if (pathtemp.indexOf('/') >= 0) {
476 /* Find ourselves an appid */
477 QString appid = decodeAppId(pathtemp);
480 if (message.member() == "Get") {
481 if (message.arguments()[1].toString() == "count") {
482 retval.append(QVariant::fromValue(QDBusVariant(this->count(appid))));
483 } else if (message.arguments()[1].toString() == "countVisible") {
484 retval.append(QVariant::fromValue(QDBusVariant(this->countVisible(appid))));
486 } else if (message.member() == "Set") {
487 if (message.arguments()[1].toString() == "count") {
488 this->setCount(appid, message.arguments()[2].value<QDBusVariant>().variant().toInt());
489 } else if (message.arguments()[1].toString() == "countVisible") {
490 this->setCountVisible(appid, message.arguments()[2].value<QDBusVariant>().variant().toBool());
492 } else if (message.member() == "GetAll") {
493 retval.append(this->itemToVariant(appid));
498 QDBusMessage reply = message.createReply(retval);
499 return connection.send(reply);
502QString LauncherBackend::introspect(const QString &path) const
504 /* This case we should just list the nodes */
505 if (path == "/com/canonical/Unity/Launcher/" || path == "/com/canonical/Unity/Launcher") {
508 Q_FOREACH(const QString &appId, m_itemCache.keys()) {
509 nodes.append("<node name=\"");
510 nodes.append(encodeAppId(appId));
511 nodes.append("\"/>\n");
517 /* Should not happen, but let's handle it */
518 if (!path.startsWith("/com/canonical/Unity/Launcher")) {
522 /* Now we should be looking at a node */
524 "<interface name=\"com.canonical.Unity.Launcher.Item\">"
525 "<property name=\"count\" type=\"i\" access=\"readwrite\" />"
526 "<property name=\"countVisible\" type=\"b\" access=\"readwrite\" />"
531QString LauncherBackend::decodeAppId(const QString& path)
533 QByteArray bytes = path.toUtf8();
536 for (int i = 0; i < bytes.size(); ++i) {
537 char chr = bytes.at(i);
541 number.append(bytes.at(i+1));
542 number.append(bytes.at(i+2));
545 char newchar = number.toUInt(&okay, 16);
547 decoded.append(newchar);
555 return QString::fromUtf8(decoded);
558QString LauncherBackend::encodeAppId(const QString& appId)
560 QByteArray bytes = appId.toUtf8();
563 for (int i = 0; i < bytes.size(); ++i) {
564 uchar chr = bytes.at(i);
566 if ((chr >= 'a' && chr <= 'z') ||
567 (chr >= 'A' && chr <= 'Z') ||
568 (chr >= '0' && chr <= '9'&& i != 0)) {
571 QString hexval = QString("_%1").arg(chr, 2, 16, QChar('0'));
572 encoded.append(hexval.toUpper());
579void LauncherBackend::emitPropChangedDbus(const QString& appId, const QString& property, QVariant &value) const
581 QString path("/com/canonical/Unity/Launcher/");
582 path.append(encodeAppId(appId));
584 QDBusMessage message = QDBusMessage::createSignal(path, "org.freedesktop.DBus.Properties", "PropertiesChanged");
586 QList<QVariant> arguments;
587 QVariantHash changedprops;
588 changedprops[property] = QVariant::fromValue(QDBusVariant(value));
589 QVariantList deletedprops;
591 arguments.append(changedprops);
592 arguments.append(deletedprops);
594 message.setArguments(arguments);
596 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