on b050452 help sign in
unity8/plugins/Unity/Launcher/backend/launcherbackend.cpp
359 lines 10 kB history raw
1/*
2 * Copyright (C) 2013 Canonical, Ltd.
3 *
4 * Authors:
5 * Michael Terry <michael.terry@canonical.com>
6 * Michael Zanetti <michael.zanetti@canonical.com>
7 *
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.
11 *
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.
16 *
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/>.
19 */
21#include "AccountsServiceDBusAdaptor.h"
22#include "launcherbackend.h"
24#include <QDir>
25#include <QDBusArgument>
26#include <QFileInfo>
27#include <QGSettings>
28#include <QDebug>
29#include <QStandardPaths>
31class LauncherBackendItem
33public:
34 QString desktopFile;
35 QString displayName;
36 QString icon;
37 int count;
38 bool countVisible;
39};
41LauncherBackend::LauncherBackend(QObject *parent):
42 QDBusVirtualObject(parent),
43 m_accounts(nullptr)
45#ifndef LAUNCHER_TESTING
46 m_accounts = new AccountsServiceDBusAdaptor(this);
47#endif
48 m_user = qgetenv("USER");
49 syncFromAccounts();
52LauncherBackend::~LauncherBackend()
54 m_storedApps.clear();
56 Q_FOREACH(LauncherBackendItem *item, m_itemCache) {
57 delete item;
58 }
59 m_itemCache.clear();
62QStringList LauncherBackend::storedApplications() const
64 return m_storedApps;
67void LauncherBackend::setStoredApplications(const QStringList &appIds)
69 if (appIds.count() < m_storedApps.count()) {
70 Q_FOREACH(const QString &appId, m_storedApps) {
71 if (!appIds.contains(appId)) {
72 delete m_itemCache.take(appId);
73 }
74 }
75 }
76 m_storedApps = appIds;
77 Q_FOREACH(const QString &appId, appIds) {
78 if (!m_itemCache.contains(appId)) {
79 QString df = findDesktopFile(appId);
80 if (!df.isEmpty()) {
81 LauncherBackendItem *item = parseDesktopFile(df);
82 m_itemCache.insert(appId, item);
83 } else {
84 // Cannot find any data for that app... ignoring it.
85 qWarning() << "cannot find desktop file for" << appId << ". discarding app.";
86 m_storedApps.removeAll(appId);
87 }
88 }
89 }
90 syncToAccounts();
93QString LauncherBackend::desktopFile(const QString &appId) const
95 LauncherBackendItem *item = m_itemCache.value(appId);
96 if (item) {
97 return item->desktopFile;
98 }
100 return findDesktopFile(appId);
103QString LauncherBackend::displayName(const QString &appId) const
105 LauncherBackendItem *item = m_itemCache.value(appId);
106 if (item) {
107 return item->displayName;
108 }
110 QString df = findDesktopFile(appId);
111 if (!df.isEmpty()) {
112 LauncherBackendItem *item = parseDesktopFile(df);
113 m_itemCache.insert(appId, item);
114 return item->displayName;
115 }
117 return QString();
120QString LauncherBackend::icon(const QString &appId) const
122 QString iconName;
123 LauncherBackendItem *item = m_itemCache.value(appId);
124 if (item) {
125 iconName = item->icon;
126 } else {
127 QString df = findDesktopFile(appId);
128 if (!df.isEmpty()) {
129 LauncherBackendItem *item = parseDesktopFile(df);
130 m_itemCache.insert(appId, item);
131 iconName = item->icon;
132 }
133 }
135 return iconName;
138QList<QuickListEntry> LauncherBackend::quickList(const QString &appId) const
140 // TODO: Get static (from .desktop file) and dynamic (from the app itself)
141 // entries and return them here. Frontend related entries (like "Pin to launcher")
142 // don't matter here. This is just the backend part.
143 // TODO: emit quickListChanged() when the dynamic part changes
144 Q_UNUSED(appId)
145 return QList<QuickListEntry>();
148int LauncherBackend::progress(const QString &appId) const
150 // TODO: Return value for progress emblem.
151 // TODO: emit progressChanged() when this value changes.
152 Q_UNUSED(appId)
153 return -1;
156int LauncherBackend::count(const QString &appId) const
158 // TODO: emit countChanged() when this value changes.
160 int count = 0;
161 LauncherBackendItem *item = m_itemCache.value(appId);
162 if (!item) {
163 QString df = findDesktopFile(appId);
164 if (!df.isEmpty()) {
165 item = parseDesktopFile(df);
166 m_itemCache.insert(appId, item);
167 }
168 }
170 if (!item) {
171 if (item->countVisible) {
172 count = item->count;
173 }
174 }
176 return count;
179void LauncherBackend::setUser(const QString &username)
181 if (qgetenv("USER") == "lightdm" && m_user != username) {
182 m_user = username;
183 syncFromAccounts();
184 }
187void LauncherBackend::triggerQuickListAction(const QString &appId, const QString &quickListId)
189 // TODO: execute the given quicklist action
190 Q_UNUSED(appId)
191 Q_UNUSED(quickListId)
194void LauncherBackend::syncFromAccounts()
196 QList<QVariantMap> apps;
197 bool defaults = true;
199 m_storedApps.clear();
201 if (m_accounts && !m_user.isEmpty()) {
202 QVariant variant = m_accounts->getUserProperty(m_user, "com.canonical.unity.AccountsService", "launcher-items");
203 if (variant.isValid() && variant.canConvert<QDBusArgument>()) {
204 apps = qdbus_cast<QList<QVariantMap>>(variant.value<QDBusArgument>());
205 defaults = isDefaultsItem(apps);
206 }
207 }
209 if (m_accounts && defaults) { // Checking accounts as it'll be null when !useStorage
210 QGSettings gSettings("com.canonical.Unity.Launcher", "/com/canonical/unity/launcher/");
211 Q_FOREACH(const QString &entry, gSettings.get("favorites").toStringList()) {
212 if (entry.startsWith("application://")) {
213 QString appId = entry;
214 // Transform "application://foobar.desktop" to "foobar"
215 appId.remove("application://");
216 if (appId.endsWith(".desktop")) {
217 appId.chop(8);
218 }
219 QString df = findDesktopFile(appId);
221 if (!df.isEmpty()) {
222 m_storedApps << appId;
224 if (!m_itemCache.contains(appId)) {
225 m_itemCache.insert(appId, parseDesktopFile(df));
226 }
227 }
228 }
229 }
230 } else {
231 for (const QVariant &app: apps) {
232 loadFromVariant(app.toMap());
233 }
234 }
237void LauncherBackend::syncToAccounts()
239 if (m_accounts && !m_user.isEmpty()) {
240 QList<QVariantMap> items;
242 Q_FOREACH(const QString &appId, m_storedApps) {
243 items << itemToVariant(appId);
244 }
246 m_accounts->setUserProperty(m_user, "com.canonical.unity.AccountsService", "launcher-items", QVariant::fromValue(items));
247 }
250QString LauncherBackend::findDesktopFile(const QString &appId) const
252 int dashPos = -1;
253 QString helper = appId;
255 QStringList searchDirs = QStandardPaths::standardLocations(QStandardPaths::ApplicationsLocation);
256#ifdef LAUNCHER_TESTING
257 searchDirs << "";
258#endif
260 do {
261 if (dashPos != -1) {
262 helper = helper.replace(dashPos, 1, '/');
263 }
265 Q_FOREACH(const QString &searchDir, searchDirs) {
266 QFileInfo fileInfo(QDir(searchDir), helper + ".desktop");
267 if (fileInfo.exists()) {
268 return fileInfo.absoluteFilePath();
269 }
270 }
272 dashPos = helper.indexOf("-");
273 } while (dashPos != -1);
275 return QString();
278LauncherBackendItem* LauncherBackend::parseDesktopFile(const QString &desktopFile) const
280 QSettings settings(desktopFile, QSettings::IniFormat);
282 LauncherBackendItem* item = new LauncherBackendItem();
283 item->desktopFile = desktopFile;
284 item->displayName = settings.value("Desktop Entry/Name").toString();
286 QString iconString = settings.value("Desktop Entry/Icon").toString();
287 QString pathString = settings.value("Desktop Entry/Path").toString();
288 if (QFileInfo(iconString).exists()) {
289 item->icon = QFileInfo(iconString).absoluteFilePath();
290 } else if (QFileInfo(pathString + '/' + iconString).exists()) {
291 item->icon = pathString + '/' + iconString;
292 } else {
293 item->icon = "image://theme/" + iconString;
294 }
296 /* TODO: These should be looked up in a cache somewhere */
297 item->count = 0;
298 item->countVisible = false;
300 return item;
303void LauncherBackend::loadFromVariant(const QVariantMap &details)
305 if (!details.contains("id")) {
306 return;
307 }
308 QString appId = details.value("id").toString();
310 LauncherBackendItem *item = m_itemCache.value(appId);
311 if (item) {
312 delete item;
313 }
315 item = new LauncherBackendItem();
317 item->desktopFile = details.value("desktopFile").toString();
318 item->displayName = details.value("name").toString();
319 item->icon = details.value("icon").toString();
320 item->count = details.value("count").toInt();
321 item->countVisible = details.value("countVisible").toBool();
323 m_itemCache.insert(appId, item);
324 m_storedApps.append(appId);
327QVariantMap LauncherBackend::itemToVariant(const QString &appId) const
329 LauncherBackendItem *item = m_itemCache.value(appId);
330 QVariantMap details;
331 details.insert("id", appId);
332 details.insert("name", item->displayName);
333 details.insert("icon", item->icon);
334 details.insert("desktopFile", item->desktopFile);
335 details.insert("count", item->count);
336 details.insert("countVisible", item->countVisible);
337 return details;
340bool LauncherBackend::isDefaultsItem(const QList<QVariantMap> &apps) const
342 // To differentiate between an empty list and a list that hasn't been set
343 // yet (and should thus be populated with the defaults), we use a special
344 // list of one item with the 'defaults' field set to true.
345 return (apps.size() == 1 && apps[0].value("defaults").toBool());
348bool LauncherBackend::handleMessage(const QDBusMessage& message, const QDBusConnection& connection)
350 Q_UNUSED(message)
351 Q_UNUSED(connection)
352 return false;
355QString LauncherBackend::introspect (const QString &path) const
357 Q_UNUSED(path)
358 return "";