on 5bc8421 help sign in
unity8/plugins/Unity/Launcher/backend/launcherbackend.cpp
353 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 return iconName;
138bool LauncherBackend::isPinned(const QString &appId) const
140 LauncherBackendItem *item = m_itemCache.value(appId);
141 if (item) {
142 return item->pinned;
143 }
144 return false;
147void LauncherBackend::setPinned(const QString &appId, bool pinned)
149 if (!m_storedApps.contains(appId)) {
150 return;
151 }
153 LauncherBackendItem *item = m_itemCache.value(appId);
154 if (!item) {
155 item = new LauncherBackendItem();
156 m_itemCache.insert(appId, item);
157 }
158 if (item->pinned != pinned) {
159 item->pinned = pinned;
160 syncToAccounts();
161 }
164QList<QuickListEntry> LauncherBackend::quickList(const QString &appId) const
166 // TODO: Get static (from .desktop file) and dynamic (from the app itself)
167 // entries and return them here. Frontend related entries (like "Pin to launcher")
168 // don't matter here. This is just the backend part.
169 // TODO: emit quickListChanged() when the dynamic part changes
170 Q_UNUSED(appId)
171 return QList<QuickListEntry>();
174int LauncherBackend::progress(const QString &appId) const
176 // TODO: Return value for progress emblem.
177 // TODO: emit progressChanged() when this value changes.
178 Q_UNUSED(appId)
179 return -1;
182int LauncherBackend::count(const QString &appId) const
184 // TODO: Return value for count emblem.
185 // TODO: emit countChanged() when this value changes.
186 Q_UNUSED(appId)
187 return 0;
190void LauncherBackend::setUser(const QString &username)
192 if (qgetenv("USER") == "lightdm" && m_user != username) {
193 m_user = username;
194 syncFromAccounts();
195 }
198void LauncherBackend::triggerQuickListAction(const QString &appId, const QString &quickListId)
200 // TODO: execute the given quicklist action
201 Q_UNUSED(appId)
202 Q_UNUSED(quickListId)
205void LauncherBackend::syncFromAccounts()
207 QList<QVariantMap> apps;
208 bool defaults = true;
210 m_storedApps.clear();
212 if (m_accounts && !m_user.isEmpty()) {
213 QVariant variant = m_accounts->getUserProperty(m_user, "com.canonical.unity.AccountsService", "launcher-items");
214 apps = qdbus_cast<QList<QVariantMap>>(variant.value<QDBusArgument>());
215 defaults = isDefaultsItem(apps);
216 }
218 if (m_accounts && defaults) { // Checking accounts as it'll be null when !useStorage
219 QGSettings gSettings("com.canonical.Unity.Launcher", "/com/canonical/unity/launcher/");
220 Q_FOREACH(const QString &entry, gSettings.get("favorites").toStringList()) {
221 if (entry.startsWith("application://")) {
222 QString appId = entry;
223 // Transform "application://foobar.desktop" to "foobar"
224 appId.remove("application://");
225 if (appId.endsWith(".desktop")) {
226 appId.chop(8);
227 }
228 QString df = findDesktopFile(appId);
230 if (!df.isEmpty()) {
231 m_storedApps << appId;
233 if (!m_itemCache.contains(appId)) {
234 m_itemCache.insert(appId, parseDesktopFile(df));
235 }
236 m_itemCache.value(appId)->pinned = true;
237 }
238 }
239 }
240 } else {
241 for (const QVariant &app: apps) {
242 loadFromVariant(app.toMap());
243 }
244 }
247void LauncherBackend::syncToAccounts()
249 if (m_accounts && !m_user.isEmpty()) {
250 QList<QVariantMap> items;
252 Q_FOREACH(const QString &appId, m_storedApps) {
253 items << itemToVariant(appId);
254 }
256 m_accounts->setUserProperty(m_user, "com.canonical.unity.AccountsService", "launcher-items", QVariant::fromValue(items));
257 }
260QString LauncherBackend::findDesktopFile(const QString &appId) const
262 int dashPos = -1;
263 QString helper = appId;
265 QStringList searchDirs;
266 searchDirs << QDir::homePath() + "/.local/share/applications";
267 searchDirs << "/usr/share/applications";
269#ifdef LAUNCHER_TESTING
270 searchDirs << "";
271#endif
273 do {
274 if (dashPos != -1) {
275 helper = helper.replace(dashPos, 1, '/');
276 }
278 Q_FOREACH(const QString &searchDir, searchDirs) {
279 QFileInfo fileInfo(QDir(searchDir), helper + ".desktop");
280 if (fileInfo.exists()) {
281 return fileInfo.absoluteFilePath();
282 }
283 }
285 dashPos = helper.indexOf("-");
286 } while (dashPos != -1);
288 return QString();
291LauncherBackendItem* LauncherBackend::parseDesktopFile(const QString &desktopFile) const
293 QSettings settings(desktopFile, QSettings::IniFormat);
295 LauncherBackendItem* item = new LauncherBackendItem();
296 item->desktopFile = desktopFile;
297 item->displayName = settings.value("Desktop Entry/Name").toString();
299 QString iconString = settings.value("Desktop Entry/Icon").toString();
300 QString pathString = settings.value("Desktop Entry/Path").toString();
301 if (QFileInfo(iconString).exists()) {
302 item->icon = QFileInfo(iconString).absoluteFilePath();
303 } else if (QFileInfo(pathString + '/' + iconString).exists()) {
304 item->icon = pathString + '/' + iconString;
305 } else {
306 item->icon = "image://theme/" + iconString;
307 }
308 item->pinned = false;
309 return item;
312void LauncherBackend::loadFromVariant(const QVariantMap &details)
314 if (!details.contains("id")) {
315 return;
316 }
317 QString appId = details.value("id").toString();
319 LauncherBackendItem *item = m_itemCache.value(appId);
320 if (item) {
321 delete item;
322 }
324 item = new LauncherBackendItem();
326 item->desktopFile = details.value("desktopFile").toString();
327 item->displayName = details.value("name").toString();
328 item->icon = details.value("icon").toString();
329 item->pinned = details.value("is-pinned").toBool();
331 m_itemCache.insert(appId, item);
332 m_storedApps.append(appId);
335QVariantMap LauncherBackend::itemToVariant(const QString &appId) const
337 LauncherBackendItem *item = m_itemCache.value(appId);
338 QVariantMap details;
339 details.insert("id", appId);
340 details.insert("name", item->displayName);
341 details.insert("icon", item->icon);
342 details.insert("desktopFile", item->desktopFile);
343 details.insert("is-pinned", item->pinned);
344 return details;
347bool LauncherBackend::isDefaultsItem(const QList<QVariantMap> &apps) const
349 // To differentiate between an empty list and a list that hasn't been set
350 // yet (and should thus be populated with the defaults), we use a special
351 // list of one item with the 'defaults' field set to true.
352 return (apps.size() == 1 && apps[0].value("defaults").toBool());