on ebf4a8a help sign in
unity8/plugins/Unity/Launcher/backend/launcherbackend.cpp
354 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>
30class LauncherBackendItem
32public:
33 LauncherBackendItem(): pinned(false) {}
35 QString desktopFile;
36 QString displayName;
37 QString icon;
38 bool pinned;
39};
41LauncherBackend::LauncherBackend(bool useStorage, QObject *parent):
42 QObject(parent),
43 m_accounts(nullptr)
45 if (useStorage) {
46 m_accounts = new AccountsServiceDBusAdaptor(this);
47 }
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 QFileInfo info(iconName);
136 if (info.exists()) {
137 return info.absoluteFilePath();
138 }
139 return "image://theme/" + iconName;
142bool LauncherBackend::isPinned(const QString &appId) const
144 LauncherBackendItem *item = m_itemCache.value(appId);
145 if (item) {
146 return item->pinned;
147 }
148 return false;
151void LauncherBackend::setPinned(const QString &appId, bool pinned)
153 if (!m_storedApps.contains(appId)) {
154 return;
155 }
157 LauncherBackendItem *item = m_itemCache.value(appId);
158 if (!item) {
159 item = new LauncherBackendItem();
160 m_itemCache.insert(appId, item);
161 }
162 if (item->pinned != pinned) {
163 item->pinned = pinned;
164 syncToAccounts();
165 }
168QList<QuickListEntry> LauncherBackend::quickList(const QString &appId) const
170 // TODO: Get static (from .desktop file) and dynamic (from the app itself)
171 // entries and return them here. Frontend related entries (like "Pin to launcher")
172 // don't matter here. This is just the backend part.
173 // TODO: emit quickListChanged() when the dynamic part changes
174 Q_UNUSED(appId)
175 return QList<QuickListEntry>();
178int LauncherBackend::progress(const QString &appId) const
180 // TODO: Return value for progress emblem.
181 // TODO: emit progressChanged() when this value changes.
182 Q_UNUSED(appId)
183 return -1;
186int LauncherBackend::count(const QString &appId) const
188 // TODO: Return value for count emblem.
189 // TODO: emit countChanged() when this value changes.
190 Q_UNUSED(appId)
191 return 0;
194void LauncherBackend::setUser(const QString &username)
196 if (qgetenv("USER") == "lightdm" && m_user != username) {
197 m_user = username;
198 syncFromAccounts();
199 }
202void LauncherBackend::triggerQuickListAction(const QString &appId, const QString &quickListId)
204 // TODO: execute the given quicklist action
205 Q_UNUSED(appId)
206 Q_UNUSED(quickListId)
209void LauncherBackend::syncFromAccounts()
211 QList<QVariantMap> apps;
212 bool defaults = true;
214 m_storedApps.clear();
216 if (m_accounts && !m_user.isEmpty()) {
217 QVariant variant = m_accounts->getUserProperty(m_user, "com.canonical.unity.AccountsService", "launcher-items");
218 apps = qdbus_cast<QList<QVariantMap>>(variant.value<QDBusArgument>());
219 defaults = isDefaultsItem(apps);
220 }
222 if (m_accounts && defaults) { // Checking accounts as it'll be null when !useStorage
223 QGSettings gSettings("com.canonical.Unity.Launcher", "/com/canonical/unity/launcher/");
224 Q_FOREACH(const QString &entry, gSettings.get("favorites").toStringList()) {
225 if (entry.startsWith("application://")) {
226 QString appId = entry;
227 // Transform "application://foobar.desktop" to "foobar"
228 appId.remove("application://");
229 if (appId.endsWith(".desktop")) {
230 appId.chop(8);
231 }
232 QString df = findDesktopFile(appId);
234 if (!df.isEmpty()) {
235 m_storedApps << appId;
237 if (!m_itemCache.contains(appId)) {
238 m_itemCache.insert(appId, parseDesktopFile(df));
239 }
240 m_itemCache.value(appId)->pinned = true;
241 }
242 }
243 }
244 } else {
245 for (const QVariant &app: apps) {
246 loadFromVariant(app.toMap());
247 }
248 }
251void LauncherBackend::syncToAccounts()
253 if (m_accounts && !m_user.isEmpty()) {
254 QList<QVariantMap> items;
256 Q_FOREACH(const QString &appId, m_storedApps) {
257 items << itemToVariant(appId);
258 }
260 m_accounts->setUserProperty(m_user, "com.canonical.unity.AccountsService", "launcher-items", QVariant::fromValue(items));
261 }
264QString LauncherBackend::findDesktopFile(const QString &appId) const
266 int dashPos = -1;
267 QString helper = appId;
269 QStringList searchDirs;
270 searchDirs << QDir::homePath() + "/.local/share/applications";
271 searchDirs << "/usr/share/applications";
273#ifdef LAUNCHER_TESTING
274 searchDirs << "";
275#endif
277 do {
278 if (dashPos != -1) {
279 helper = helper.replace(dashPos, 1, '/');
280 }
282 Q_FOREACH(const QString &searchDir, searchDirs) {
283 QFileInfo fileInfo(QDir(searchDir), helper + ".desktop");
284 if (fileInfo.exists()) {
285 return fileInfo.absoluteFilePath();
286 }
287 }
289 dashPos = helper.indexOf("-");
290 } while (dashPos != -1);
292 return QString();
295LauncherBackendItem* LauncherBackend::parseDesktopFile(const QString &desktopFile) const
297 QSettings settings(desktopFile, QSettings::IniFormat);
300 LauncherBackendItem* item = new LauncherBackendItem();
301 item->desktopFile = desktopFile;
302 item->displayName = settings.value("Desktop Entry/Name").toString();
304 if (settings.contains("Desktop Entry/Path")) {
305 item->icon = settings.value("Desktop Entry/Path").toString() + "/" + settings.value("Desktop Entry/Icon").toString();
306 } else {
307 item->icon = settings.value("Desktop Entry/Icon").toString();
308 }
309 item->pinned = false;
310 return item;
313void LauncherBackend::loadFromVariant(const QVariantMap &details)
315 if (!details.contains("id")) {
316 return;
317 }
318 QString appId = details.value("id").toString();
320 LauncherBackendItem *item = m_itemCache.value(appId);
321 if (item) {
322 delete item;
323 }
325 item = new LauncherBackendItem();
327 item->desktopFile = details.value("desktopFile").toString();
328 item->displayName = details.value("name").toString();
329 item->icon = details.value("icon").toString();
330 item->pinned = details.value("is-pinned").toBool();
332 m_itemCache.insert(appId, item);
333 m_storedApps.append(appId);
336QVariantMap LauncherBackend::itemToVariant(const QString &appId) const
338 LauncherBackendItem *item = m_itemCache.value(appId);
339 QVariantMap details;
340 details.insert("id", appId);
341 details.insert("name", item->displayName);
342 details.insert("icon", item->icon);
343 details.insert("desktopFile", item->desktopFile);
344 details.insert("is-pinned", item->pinned);
345 return details;
348bool LauncherBackend::isDefaultsItem(const QList<QVariantMap> &apps) const
350 // To differentiate between an empty list and a list that hasn't been set
351 // yet (and should thus be populated with the defaults), we use a special
352 // list of one item with the 'defaults' field set to true.
353 return (apps.size() == 1 && apps[0].value("defaults").toBool());