@@ -1,621 +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>
31−class LauncherBackendItem
40−LauncherBackend::LauncherBackend(QObject *parent):
41− QDBusVirtualObject(parent),
44−#ifndef LAUNCHER_TESTING
45− m_accounts = new AccountsServiceDBusAdaptor(this);
47− m_user = qgetenv("USER");
50− /* Set up ourselves on DBus */
51− QDBusConnection con = QDBusConnection::sessionBus();
52− if (!con.registerService("com.canonical.Unity.Launcher")) {
53− 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";
60−LauncherBackend::~LauncherBackend()
62− /* Remove oursevles from DBus */
63− QDBusConnection con = QDBusConnection::sessionBus();
64− con.unregisterService("com.canonical.Unity.Launcher");
65− con.unregisterObject("/com/canonical/Unity/Launcher");
68− m_storedApps.clear();
70− Q_FOREACH(LauncherBackendItem *item, m_itemCache) {
76−QStringList LauncherBackend::storedApplications() const
81−void LauncherBackend::setStoredApplications(const QStringList &appIds)
83− if (appIds.count() < m_storedApps.count()) {
84− Q_FOREACH(const QString &appId, m_storedApps) {
85− if (!appIds.contains(appId)) {
86− delete m_itemCache.take(appId);
90− m_storedApps = appIds;
91− Q_FOREACH(const QString &appId, appIds) {
92− if (!m_itemCache.contains(appId)) {
93− QString df = findDesktopFile(appId);
95− LauncherBackendItem *item = parseDesktopFile(df);
96− m_itemCache.insert(appId, item);
98− // Cannot find any data for that app... ignoring it.
99− qWarning() << "cannot find desktop file for" << appId << ". discarding app.";
100− m_storedApps.removeAll(appId);
107−QString LauncherBackend::desktopFile(const QString &appId) const
109− return findDesktopFile(appId);
112−QString LauncherBackend::displayName(const QString &appId) const
114− LauncherBackendItem *item = m_itemCache.value(appId, nullptr);
116− return item->displayName;
119− QString df = findDesktopFile(appId);
120− if (!df.isEmpty()) {
121− LauncherBackendItem *item = parseDesktopFile(df);
122− m_itemCache.insert(appId, item);
123− return item->displayName;
129−QString LauncherBackend::icon(const QString &appId) const
132− LauncherBackendItem *item = getItem(appId);
134− iconName = item->icon;
140−QList<QuickListEntry> LauncherBackend::quickList(const QString &appId) const
142− // TODO: Get static (from .desktop file) and dynamic (from the app itself)
143− // entries and return them here. Frontend related entries (like "Pin to launcher")
144− // don't matter here. This is just the backend part.
145− // TODO: emit quickListChanged() when the dynamic part changes
147− return QList<QuickListEntry>();
150−int LauncherBackend::progress(const QString &appId) const
152− // TODO: Return value for progress emblem.
153− // TODO: emit progressChanged() when this value changes.
158−int LauncherBackend::count(const QString &appId) const
161− LauncherBackendItem *item = getItem(appId);
164− if (item->countVisible) {
165− count = item->count;
172−void LauncherBackend::setCount(const QString &appId, int count) const
174− LauncherBackendItem *item = getItem(appId);
176− bool emitchange = false;
178− emitchange = (item->count != count);
179− item->count = count;
183− /* TODO: This needs to use the accessor to handle the visibility
184− correctly, but when we have the two properties we can just use
186− Q_EMIT countChanged(appId, this->count(appId));
187− QVariant vcount(item->count);
188− emitPropChangedDbus(appId, "count", vcount);
192−bool LauncherBackend::countVisible(const QString &appId) const
194− bool visible = false;
195− LauncherBackendItem *item = getItem(appId);
198− visible = item->countVisible;
204−void LauncherBackend::setCountVisible(const QString &appId, bool visible) const
206− LauncherBackendItem *item = getItem(appId);
208− bool emitchange = false;
210− emitchange = (item->countVisible != visible);
211− item->countVisible = visible;
213− qDebug() << "Unable to find:" << appId;
217− /* TODO: Because we're using visible in determining the
218− count we need to emit a count changed as well */
219− Q_EMIT countChanged(appId, this->count(appId));
220− Q_EMIT countVisibleChanged(appId, item->countVisible);
221− QVariant vCountVisible(item->countVisible);
222− emitPropChangedDbus(appId, "countVisible", vCountVisible);
226−void LauncherBackend::setUser(const QString &username)
228− if (qgetenv("USER") == "lightdm" && m_user != username) {
234−void LauncherBackend::triggerQuickListAction(const QString &appId, const QString &quickListId)
236− // TODO: execute the given quicklist action
238− Q_UNUSED(quickListId)
241−void LauncherBackend::syncFromAccounts()
243− QList<QVariantMap> apps;
244− bool defaults = true;
246− m_storedApps.clear();
248− if (m_accounts && !m_user.isEmpty()) {
249− QVariant variant = m_accounts->getUserProperty(m_user, "com.canonical.unity.AccountsService", "launcher-items");
250− if (variant.isValid() && variant.canConvert<QDBusArgument>()) {
251− apps = qdbus_cast<QList<QVariantMap>>(variant.value<QDBusArgument>());
252− defaults = isDefaultsItem(apps);
256− if (m_accounts && defaults) { // Checking accounts as it'll be null when !useStorage
257− QGSettings gSettings("com.canonical.Unity.Launcher", "/com/canonical/unity/launcher/");
258− Q_FOREACH(const QString &entry, gSettings.get("favorites").toStringList()) {
259− if (entry.startsWith("application://")) {
260− QString appId = entry;
261− // Transform "application://foobar.desktop" to "application://foobar"
262− appId.remove("application://");
263− if (appId.endsWith(".desktop")) {
266− QString df = findDesktopFile(appId);
268− if (!df.isEmpty()) {
269− m_storedApps << appId;
271− if (!m_itemCache.contains(appId)) {
272− m_itemCache.insert(appId, parseDesktopFile(df));
276− if (entry.startsWith("appid://")) {
277− QString appId = entry;
278− appId.remove("appid://");
279− // Strip hook name and current-user-version in case its there
281− if (appId.split('/').count() != 3) {
282− qWarning() << "ignoring entry " + appId + ". Not a valid appId.";
285− appId = appId.split('/').first() + "_" + appId.split('/').at(1);
286− QString df = findDesktopFile(appId);
288− if (!df.isEmpty()) {
289− m_storedApps << appId;
291− if (!m_itemCache.contains(appId)) {
292− m_itemCache.insert(appId, parseDesktopFile(df));
298− for (const QVariant &app: apps) {
299− loadFromVariant(app.toMap());
304−void LauncherBackend::syncToAccounts()
306− if (m_accounts && !m_user.isEmpty()) {
307− QList<QVariantMap> items;
309− Q_FOREACH(const QString &appId, m_storedApps) {
310− items << itemToVariant(appId);
313− m_accounts->setUserProperty(m_user, "com.canonical.unity.AccountsService", "launcher-items", QVariant::fromValue(items));
317−void LauncherBackend::refresh()
319− QList<QString> removedApps;
320− Q_FOREACH (const QString &appId, m_storedApps) {
321− if (findDesktopFile(appId).isEmpty()) {
322− removedApps << appId;
325− Q_FOREACH (const QString &appId, removedApps) {
326− Q_EMIT appUninstalled(appId);
330−QString LauncherBackend::findDesktopFile(const QString &appId) const
333− QString helper = appId;
335− QStringList searchDirs = QStandardPaths::standardLocations(QStandardPaths::ApplicationsLocation);
336−#ifdef LAUNCHER_TESTING
341− if (dashPos != -1) {
342− helper = helper.replace(dashPos, 1, '/');
345− Q_FOREACH(const QString &searchDirName, searchDirs) {
346− QDir searchDir(searchDirName);
347− Q_FOREACH(const QString &desktopFile, searchDir.entryList(QStringList() << "*.desktop")) {
348− if (desktopFile.startsWith(helper)) {
349− QFileInfo fileInfo(searchDir, desktopFile);
350− return fileInfo.absoluteFilePath();
355− dashPos = helper.indexOf("-");
356− } while (dashPos != -1);
361−LauncherBackendItem* LauncherBackend::parseDesktopFile(const QString &desktopFile) const
363− QSettings settings(desktopFile, QSettings::IniFormat);
365− LauncherBackendItem* item = new LauncherBackendItem();
366− item->displayName = settings.value("Desktop Entry/Name").toString();
368− QString iconString = settings.value("Desktop Entry/Icon").toString();
369− QString pathString = settings.value("Desktop Entry/Path").toString();
370− if (QFileInfo(iconString).exists()) {
371− item->icon = QFileInfo(iconString).absoluteFilePath();
372− } else if (QFileInfo(pathString + '/' + iconString).exists()) {
373− item->icon = pathString + '/' + iconString;
375− item->icon = "image://theme/" + iconString;
378− /* TODO: These should be looked up in a cache somewhere */
380− item->countVisible = false;
385−/* Gets an item, and tries to create a new one if we need it to */
386−LauncherBackendItem* LauncherBackend::getItem(const QString &appId) const
388− LauncherBackendItem *item = m_itemCache.value(appId, nullptr);
390− QString df = findDesktopFile(appId);
391− if (!df.isEmpty()) {
392− item = parseDesktopFile(df);
394− m_itemCache[appId] = item;
396− qWarning() << "Unable to parse desktop file for" << appId << "path" << df;
399− qWarning() << "Unable to find desktop file for:" << appId;
404− qWarning() << "Unable to find item for: " << appId;
409−void LauncherBackend::loadFromVariant(const QVariantMap &details)
411− if (!details.contains("id")) {
414− QString appId = details.value("id").toString();
416− QString desktopFile = findDesktopFile(appId);
417− if (desktopFile.isEmpty()) {
421− LauncherBackendItem *item = m_itemCache.value(appId, nullptr);
426− if (qgetenv("USER") == "lightdm") {
427− item = new LauncherBackendItem();
428− item->displayName = details.value("name").toString();
429− item->icon = details.value("icon").toString();
431− item = parseDesktopFile(desktopFile);
433− item->count = details.value("count").toInt();
434− item->countVisible = details.value("countVisible").toBool();
436− m_itemCache.insert(appId, item);
437− m_storedApps.append(appId);
440−QVariantMap LauncherBackend::itemToVariant(const QString &appId) const
442− LauncherBackendItem *item = m_itemCache.value(appId);
443− QVariantMap details;
444− details.insert("id", appId);
445− details.insert("name", item->displayName);
446− details.insert("icon", item->icon);
447− details.insert("count", item->count);
448− details.insert("countVisible", item->countVisible);
452−bool LauncherBackend::isDefaultsItem(const QList<QVariantMap> &apps) const
454− // To differentiate between an empty list and a list that hasn't been set
455− // yet (and should thus be populated with the defaults), we use a special
456− // list of one item with the 'defaults' field set to true.
457− return (apps.size() == 1 && apps[0].value("defaults").toBool());
460−bool LauncherBackend::handleMessage(const QDBusMessage& message, const QDBusConnection& connection)
462− /* Check to make sure we're getting properties on our interface */
463− if (message.type() != QDBusMessage::MessageType::MethodCallMessage) {
466− // First handle methods of the Launcher interface
467− if (message.interface() == "com.canonical.Unity.Launcher") {
468− if (message.member() == "Refresh") {
469− QDBusMessage reply = message.createReply();
471− return connection.send(reply);
475− // Now handle dynamic properties (for launcher emblems)
476− if (message.interface() != "org.freedesktop.DBus.Properties") {
479− if (message.arguments()[0].toString() != "com.canonical.Unity.Launcher.Item") {
483− /* Break down the path to just the app id */
484− QString pathtemp = message.path();
485− if (!pathtemp.startsWith("/com/canonical/Unity/Launcher/")) {
488− pathtemp.remove("/com/canonical/Unity/Launcher/");
489− if (pathtemp.indexOf('/') >= 0) {
493− /* Find ourselves an appid */
494− QString appid = decodeAppId(pathtemp);
495− QVariantList retval;
497− if (message.member() == "Get") {
498− if (message.arguments()[1].toString() == "count") {
499− retval.append(QVariant::fromValue(QDBusVariant(this->count(appid))));
500− } else if (message.arguments()[1].toString() == "countVisible") {
501− retval.append(QVariant::fromValue(QDBusVariant(this->countVisible(appid))));
503− } else if (message.member() == "Set") {
504− if (message.arguments()[1].toString() == "count") {
505− this->setCount(appid, message.arguments()[2].value<QDBusVariant>().variant().toInt());
506− } else if (message.arguments()[1].toString() == "countVisible") {
507− this->setCountVisible(appid, message.arguments()[2].value<QDBusVariant>().variant().toBool());
509− } else if (message.member() == "GetAll") {
510− retval.append(this->itemToVariant(appid));
515− QDBusMessage reply = message.createReply(retval);
516− return connection.send(reply);
519−QString LauncherBackend::introspect(const QString &path) const
521− /* This case we should just list the nodes */
522− if (path == "/com/canonical/Unity/Launcher/" || path == "/com/canonical/Unity/Launcher") {
525− // Add Refresh to introspect
526− nodes = "<interface name=\"com.canonical.Unity.Launcher\">"
527− "<method name=\"Refresh\"/>"
530− // Add dynamic properties for launcher emblems
531− Q_FOREACH(const QString &appId, m_itemCache.keys()) {
532− nodes.append("<node name=\"");
533− nodes.append(encodeAppId(appId));
534− nodes.append("\"/>\n");
540− /* Should not happen, but let's handle it */
541− if (!path.startsWith("/com/canonical/Unity/Launcher")) {
545− /* Now we should be looking at a node */
547− "<interface name=\"com.canonical.Unity.Launcher.Item\">"
548− "<property name=\"count\" type=\"i\" access=\"readwrite\" />"
549− "<property name=\"countVisible\" type=\"b\" access=\"readwrite\" />"
554−QString LauncherBackend::decodeAppId(const QString& path)
556− QByteArray bytes = path.toUtf8();
559− for (int i = 0; i < bytes.size(); ++i) {
560− char chr = bytes.at(i);
564− number.append(bytes.at(i+1));
565− number.append(bytes.at(i+2));
568− char newchar = number.toUInt(&okay, 16);
570− decoded.append(newchar);
574− decoded.append(chr);
578− return QString::fromUtf8(decoded);
581−QString LauncherBackend::encodeAppId(const QString& appId)
583− QByteArray bytes = appId.toUtf8();
586− for (int i = 0; i < bytes.size(); ++i) {
587− uchar chr = bytes.at(i);
589− if ((chr >= 'a' && chr <= 'z') ||
590− (chr >= 'A' && chr <= 'Z') ||
591− (chr >= '0' && chr <= '9'&& i != 0)) {
592− encoded.append(chr);
594− QString hexval = QString("_%1").arg(chr, 2, 16, QChar('0'));
595− encoded.append(hexval.toUpper());
602−void LauncherBackend::emitPropChangedDbus(const QString& appId, const QString& property, QVariant &value) const
604− QString path("/com/canonical/Unity/Launcher/");
605− path.append(encodeAppId(appId));
607− QDBusMessage message = QDBusMessage::createSignal(path, "org.freedesktop.DBus.Properties", "PropertiesChanged");
609− QList<QVariant> arguments;
610− QVariantHash changedprops;
611− changedprops[property] = QVariant::fromValue(QDBusVariant(value));
612− QVariantList deletedprops;
614− arguments.append(changedprops);
615− arguments.append(deletedprops);
617− message.setArguments(arguments);
619− QDBusConnection con = QDBusConnection::sessionBus();