@@ -1,591 +0,0 @@
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>
33−class LauncherBackendItem
42−LauncherBackend::LauncherBackend(QObject *parent):
43− QDBusVirtualObject(parent),
46−#ifndef LAUNCHER_TESTING
47− m_accounts = new AccountsServiceDBusAdaptor(this);
49− m_user = qgetenv("USER");
52− /* Set up ourselves on DBus */
53− QDBusConnection con = QDBusConnection::sessionBus();
54− if (!con.registerService("com.canonical.Unity.Launcher")) {
55− qDebug() << "Unable to register launcher name";
57− if (!con.registerVirtualObject("/com/canonical/Unity/Launcher", this, QDBusConnection::VirtualObjectRegisterOption::SubPath)) {
58− qDebug() << "Unable to register launcher object";
62−LauncherBackend::~LauncherBackend()
64− /* Remove oursevles from DBus */
65− QDBusConnection con = QDBusConnection::sessionBus();
66− con.unregisterService("com.canonical.Unity.Launcher");
67− con.unregisterObject("/com/canonical/Unity/Launcher");
70− m_storedApps.clear();
72− Q_FOREACH(LauncherBackendItem *item, m_itemCache) {
78−QStringList LauncherBackend::storedApplications() const
83−void LauncherBackend::setStoredApplications(const QStringList &appIds)
85− if (appIds.count() < m_storedApps.count()) {
86− Q_FOREACH(const QString &appId, m_storedApps) {
87− if (!appIds.contains(appId)) {
88− delete m_itemCache.take(appId);
92− m_storedApps = appIds;
93− Q_FOREACH(const QString &appId, appIds) {
94− if (!m_itemCache.contains(appId)) {
95− QString df = findDesktopFile(appId);
97− LauncherBackendItem *item = parseDesktopFile(df);
98− m_itemCache.insert(appId, item);
100− // Cannot find any data for that app... ignoring it.
101− qWarning() << "cannot find desktop file for" << appId << ". discarding app.";
102− m_storedApps.removeAll(appId);
109−QString LauncherBackend::desktopFile(const QString &appId) const
111− return findDesktopFile(appId);
114−QString LauncherBackend::displayName(const QString &appId) const
116− LauncherBackendItem *item = m_itemCache.value(appId, nullptr);
118− return item->displayName;
121− QString df = findDesktopFile(appId);
122− if (!df.isEmpty()) {
123− LauncherBackendItem *item = parseDesktopFile(df);
124− m_itemCache.insert(appId, item);
125− return item->displayName;
131−QString LauncherBackend::icon(const QString &appId) const
134− LauncherBackendItem *item = getItem(appId);
136− iconName = item->icon;
142−QList<QuickListEntry> LauncherBackend::quickList(const QString &appId) const
144− // TODO: Get static (from .desktop file) and dynamic (from the app itself)
145− // entries and return them here. Frontend related entries (like "Pin to launcher")
146− // don't matter here. This is just the backend part.
147− // TODO: emit quickListChanged() when the dynamic part changes
149− return QList<QuickListEntry>();
152−int LauncherBackend::progress(const QString &appId) const
154− // TODO: Return value for progress emblem.
155− // TODO: emit progressChanged() when this value changes.
160−int LauncherBackend::count(const QString &appId) const
163− LauncherBackendItem *item = getItem(appId);
166− if (item->countVisible) {
167− count = item->count;
174−void LauncherBackend::setCount(const QString &appId, int count) const
176− LauncherBackendItem *item = getItem(appId);
178− bool emitchange = false;
180− emitchange = (item->count != count);
181− item->count = count;
185− /* TODO: This needs to use the accessor to handle the visibility
186− correctly, but when we have the two properties we can just use
188− Q_EMIT countChanged(appId, this->count(appId));
189− QVariant vcount(item->count);
190− emitPropChangedDbus(appId, "count", vcount);
194−bool LauncherBackend::countVisible(const QString &appId) const
196− bool visible = false;
197− LauncherBackendItem *item = getItem(appId);
200− visible = item->countVisible;
206−void LauncherBackend::setCountVisible(const QString &appId, bool visible) const
208− LauncherBackendItem *item = getItem(appId);
210− bool emitchange = false;
212− emitchange = (item->countVisible != visible);
213− item->countVisible = visible;
215− qDebug() << "Unable to find:" << appId;
219− /* TODO: Because we're using visible in determining the
220− count we need to emit a count changed as well */
221− Q_EMIT countChanged(appId, this->count(appId));
222− Q_EMIT countVisibleChanged(appId, item->countVisible);
223− QVariant vCountVisible(item->countVisible);
224− emitPropChangedDbus(appId, "countVisible", vCountVisible);
228−void LauncherBackend::setUser(const QString &username)
230− if (qgetenv("USER") == "lightdm" && m_user != username) {
236−void LauncherBackend::triggerQuickListAction(const QString &appId, const QString &quickListId)
238− // TODO: execute the given quicklist action
240− Q_UNUSED(quickListId)
243−void LauncherBackend::syncFromAccounts()
245− QList<QVariantMap> apps;
246− bool defaults = true;
248− m_storedApps.clear();
250− if (m_accounts && !m_user.isEmpty()) {
251− QVariant variant = m_accounts->getUserProperty(m_user, "com.canonical.unity.AccountsService", "launcher-items");
252− if (variant.isValid() && variant.canConvert<QDBusArgument>()) {
253− apps = qdbus_cast<QList<QVariantMap>>(variant.value<QDBusArgument>());
254− defaults = isDefaultsItem(apps);
258− if (m_accounts && defaults) { // Checking accounts as it'll be null when !useStorage
259− QGSettings gSettings("com.canonical.Unity.Launcher", "/com/canonical/unity/launcher/");
260− Q_FOREACH(const QString &entry, gSettings.get("favorites").toStringList()) {
261− if (entry.startsWith("application://")) {
262− QString appId = entry;
263− // Transform "application://foobar.desktop" to "application://foobar"
264− appId.remove("application://");
265− if (appId.endsWith(".desktop")) {
268− QString df = findDesktopFile(appId);
270− if (!df.isEmpty()) {
271− m_storedApps << appId;
273− if (!m_itemCache.contains(appId)) {
274− m_itemCache.insert(appId, parseDesktopFile(df));
278− if (entry.startsWith("appid://")) {
279− QString appId = entry;
280− appId.remove("appid://");
281− // Strip hook name and current-user-version in case its there
283− if (appId.split('/').count() != 3) {
284− qWarning() << "ignoring entry " + appId + ". Not a valid appId.";
287− appId = appId.split('/').first() + "_" + appId.split('/').at(1);
288− QString df = findDesktopFile(appId);
290− if (!df.isEmpty()) {
291− m_storedApps << appId;
293− if (!m_itemCache.contains(appId)) {
294− m_itemCache.insert(appId, parseDesktopFile(df));
300− for (const QVariant &app: apps) {
301− loadFromVariant(app.toMap());
306−void LauncherBackend::syncToAccounts()
308− if (m_accounts && !m_user.isEmpty()) {
309− QList<QVariantMap> items;
311− Q_FOREACH(const QString &appId, m_storedApps) {
312− items << itemToVariant(appId);
315− m_accounts->setUserProperty(m_user, "com.canonical.unity.AccountsService", "launcher-items", QVariant::fromValue(items));
319−QString LauncherBackend::findDesktopFile(const QString &appId) const
322− QString helper = appId;
324− QStringList searchDirs = QStandardPaths::standardLocations(QStandardPaths::ApplicationsLocation);
325−#ifdef LAUNCHER_TESTING
330− if (dashPos != -1) {
331− helper = helper.replace(dashPos, 1, '/');
334− Q_FOREACH(const QString &searchDirName, searchDirs) {
335− QDir searchDir(searchDirName);
336− Q_FOREACH(const QString &desktopFile, searchDir.entryList(QStringList() << "*.desktop")) {
337− if (desktopFile.startsWith(helper)) {
338− QFileInfo fileInfo(searchDir, desktopFile);
339− return fileInfo.absoluteFilePath();
344− dashPos = helper.indexOf("-");
345− } while (dashPos != -1);
350−LauncherBackendItem* LauncherBackend::parseDesktopFile(const QString &desktopFile) const
352− QSettings settings(desktopFile, QSettings::IniFormat);
354− LauncherBackendItem* item = new LauncherBackendItem();
356− item->displayName = settings.value("Desktop Entry/Name").toString();
357− const QString domain = settings.value("Desktop Entry/X-Ubuntu-Gettext-Domain").toString();
358− if (!domain.isEmpty()) {
359− item->displayName = dgettext(domain.toUtf8().constData(), item->displayName.toUtf8().constData());
362− QString iconString = settings.value("Desktop Entry/Icon").toString();
363− QString pathString = settings.value("Desktop Entry/Path").toString();
364− if (QFileInfo(iconString).exists()) {
365− item->icon = QFileInfo(iconString).absoluteFilePath();
366− } else if (QFileInfo(pathString + '/' + iconString).exists()) {
367− item->icon = pathString + '/' + iconString;
369− item->icon = "image://theme/" + iconString;
372− /* TODO: These should be looked up in a cache somewhere */
374− item->countVisible = false;
379−/* Gets an item, and tries to create a new one if we need it to */
380−LauncherBackendItem* LauncherBackend::getItem(const QString &appId) const
382− LauncherBackendItem *item = m_itemCache.value(appId, nullptr);
384− QString df = findDesktopFile(appId);
385− if (!df.isEmpty()) {
386− item = parseDesktopFile(df);
388− m_itemCache[appId] = item;
390− qWarning() << "Unable to parse desktop file for" << appId << "path" << df;
393− qWarning() << "Unable to find desktop file for:" << appId;
398− qWarning() << "Unable to find item for: " << appId;
403−void LauncherBackend::loadFromVariant(const QVariantMap &details)
405− if (!details.contains("id")) {
408− QString appId = details.value("id").toString();
410− LauncherBackendItem *item = m_itemCache.value(appId, nullptr);
415− item = new LauncherBackendItem();
417− item->displayName = details.value("name").toString();
418− item->icon = details.value("icon").toString();
419− item->count = details.value("count").toInt();
420− item->countVisible = details.value("countVisible").toBool();
422− m_itemCache.insert(appId, item);
423− m_storedApps.append(appId);
426−QVariantMap LauncherBackend::itemToVariant(const QString &appId) const
428− LauncherBackendItem *item = m_itemCache.value(appId);
429− QVariantMap details;
430− details.insert("id", appId);
431− details.insert("name", item->displayName);
432− details.insert("icon", item->icon);
433− details.insert("count", item->count);
434− details.insert("countVisible", item->countVisible);
438−bool LauncherBackend::isDefaultsItem(const QList<QVariantMap> &apps) const
440− // To differentiate between an empty list and a list that hasn't been set
441− // yet (and should thus be populated with the defaults), we use a special
442− // list of one item with the 'defaults' field set to true.
443− return (apps.size() == 1 && apps[0].value("defaults").toBool());
446−bool LauncherBackend::handleMessage(const QDBusMessage& message, const QDBusConnection& connection)
448− /* Check to make sure we're getting properties on our interface */
449− if (message.type() != QDBusMessage::MessageType::MethodCallMessage) {
452− if (message.interface() != "org.freedesktop.DBus.Properties") {
455− if (message.arguments()[0].toString() != "com.canonical.Unity.Launcher.Item") {
459− /* Break down the path to just the app id */
460− QString pathtemp = message.path();
461− if (!pathtemp.startsWith("/com/canonical/Unity/Launcher/")) {
464− pathtemp.remove("/com/canonical/Unity/Launcher/");
465− if (pathtemp.indexOf('/') >= 0) {
469− /* Find ourselves an appid */
470− QString appid = decodeAppId(pathtemp);
471− QVariantList retval;
473− if (message.member() == "Get") {
474− if (message.arguments()[1].toString() == "count") {
475− retval.append(QVariant::fromValue(QDBusVariant(this->count(appid))));
476− } else if (message.arguments()[1].toString() == "countVisible") {
477− retval.append(QVariant::fromValue(QDBusVariant(this->countVisible(appid))));
479− } else if (message.member() == "Set") {
480− if (message.arguments()[1].toString() == "count") {
481− this->setCount(appid, message.arguments()[2].value<QDBusVariant>().variant().toInt());
482− } else if (message.arguments()[1].toString() == "countVisible") {
483− this->setCountVisible(appid, message.arguments()[2].value<QDBusVariant>().variant().toBool());
485− } else if (message.member() == "GetAll") {
486− retval.append(this->itemToVariant(appid));
491− QDBusMessage reply = message.createReply(retval);
492− return connection.send(reply);
495−QString LauncherBackend::introspect(const QString &path) const
497− /* This case we should just list the nodes */
498− if (path == "/com/canonical/Unity/Launcher/" || path == "/com/canonical/Unity/Launcher") {
501− Q_FOREACH(const QString &appId, m_itemCache.keys()) {
502− nodes.append("<node name=\"");
503− nodes.append(encodeAppId(appId));
504− nodes.append("\"/>\n");
510− /* Should not happen, but let's handle it */
511− if (!path.startsWith("/com/canonical/Unity/Launcher")) {
515− /* Now we should be looking at a node */
517− "<interface name=\"com.canonical.Unity.Launcher.Item\">"
518− "<property name=\"count\" type=\"i\" access=\"readwrite\" />"
519− "<property name=\"countVisible\" type=\"b\" access=\"readwrite\" />"
524−QString LauncherBackend::decodeAppId(const QString& path)
526− QByteArray bytes = path.toUtf8();
529− for (int i = 0; i < bytes.size(); ++i) {
530− char chr = bytes.at(i);
534− number.append(bytes.at(i+1));
535− number.append(bytes.at(i+2));
538− char newchar = number.toUInt(&okay, 16);
540− decoded.append(newchar);
544− decoded.append(chr);
548− return QString::fromUtf8(decoded);
551−QString LauncherBackend::encodeAppId(const QString& appId)
553− QByteArray bytes = appId.toUtf8();
556− for (int i = 0; i < bytes.size(); ++i) {
557− uchar chr = bytes.at(i);
559− if ((chr >= 'a' && chr <= 'z') ||
560− (chr >= 'A' && chr <= 'Z') ||
561− (chr >= '0' && chr <= '9'&& i != 0)) {
562− encoded.append(chr);
564− QString hexval = QString("_%1").arg(chr, 2, 16, QChar('0'));
565− encoded.append(hexval.toUpper());
572−void LauncherBackend::emitPropChangedDbus(const QString& appId, const QString& property, QVariant &value) const
574− QString path("/com/canonical/Unity/Launcher/");
575− path.append(encodeAppId(appId));
577− QDBusMessage message = QDBusMessage::createSignal(path, "org.freedesktop.DBus.Properties", "PropertiesChanged");
579− QList<QVariant> arguments;
580− QVariantHash changedprops;
581− changedprops[property] = QVariant::fromValue(QDBusVariant(value));
582− QVariantList deletedprops;
584− arguments.append(changedprops);
585− arguments.append(deletedprops);
587− message.setArguments(arguments);
589− QDBusConnection con = QDBusConnection::sessionBus();