on cbdd179 help sign in
unity8/plugins/Unity/Launcher/backend/launcherbackend.cpp
320 lines 9.4 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 QString desktopFile;
34 QString displayName;
35 QString icon;
36};
38LauncherBackend::LauncherBackend(QObject *parent):
39 QObject(parent),
40 m_accounts(nullptr)
42#ifndef LAUNCHER_TESTING
43 m_accounts = new AccountsServiceDBusAdaptor(this);
44#endif
45 m_user = qgetenv("USER");
46 syncFromAccounts();
49LauncherBackend::~LauncherBackend()
51 m_storedApps.clear();
53 Q_FOREACH(LauncherBackendItem *item, m_itemCache) {
54 delete item;
55 }
56 m_itemCache.clear();
59QStringList LauncherBackend::storedApplications() const
61 return m_storedApps;
64void LauncherBackend::setStoredApplications(const QStringList &appIds)
66 if (appIds.count() < m_storedApps.count()) {
67 Q_FOREACH(const QString &appId, m_storedApps) {
68 if (!appIds.contains(appId)) {
69 delete m_itemCache.take(appId);
70 }
71 }
72 }
73 m_storedApps = appIds;
74 Q_FOREACH(const QString &appId, appIds) {
75 if (!m_itemCache.contains(appId)) {
76 QString df = findDesktopFile(appId);
77 if (!df.isEmpty()) {
78 LauncherBackendItem *item = parseDesktopFile(df);
79 m_itemCache.insert(appId, item);
80 } else {
81 // Cannot find any data for that app... ignoring it.
82 qWarning() << "cannot find desktop file for" << appId << ". discarding app.";
83 m_storedApps.removeAll(appId);
84 }
85 }
86 }
87 syncToAccounts();
90QString LauncherBackend::desktopFile(const QString &appId) const
92 LauncherBackendItem *item = m_itemCache.value(appId);
93 if (item) {
94 return item->desktopFile;
95 }
97 return findDesktopFile(appId);
100QString LauncherBackend::displayName(const QString &appId) const
102 LauncherBackendItem *item = m_itemCache.value(appId);
103 if (item) {
104 return item->displayName;
105 }
107 QString df = findDesktopFile(appId);
108 if (!df.isEmpty()) {
109 LauncherBackendItem *item = parseDesktopFile(df);
110 m_itemCache.insert(appId, item);
111 return item->displayName;
112 }
114 return QString();
117QString LauncherBackend::icon(const QString &appId) const
119 QString iconName;
120 LauncherBackendItem *item = m_itemCache.value(appId);
121 if (item) {
122 iconName = item->icon;
123 } else {
124 QString df = findDesktopFile(appId);
125 if (!df.isEmpty()) {
126 LauncherBackendItem *item = parseDesktopFile(df);
127 m_itemCache.insert(appId, item);
128 iconName = item->icon;
129 }
130 }
132 return iconName;
135QList<QuickListEntry> LauncherBackend::quickList(const QString &appId) const
137 // TODO: Get static (from .desktop file) and dynamic (from the app itself)
138 // entries and return them here. Frontend related entries (like "Pin to launcher")
139 // don't matter here. This is just the backend part.
140 // TODO: emit quickListChanged() when the dynamic part changes
141 Q_UNUSED(appId)
142 return QList<QuickListEntry>();
145int LauncherBackend::progress(const QString &appId) const
147 // TODO: Return value for progress emblem.
148 // TODO: emit progressChanged() when this value changes.
149 Q_UNUSED(appId)
150 return -1;
153int LauncherBackend::count(const QString &appId) const
155 // TODO: Return value for count emblem.
156 // TODO: emit countChanged() when this value changes.
157 Q_UNUSED(appId)
158 return 0;
161void LauncherBackend::setUser(const QString &username)
163 if (qgetenv("USER") == "lightdm" && m_user != username) {
164 m_user = username;
165 syncFromAccounts();
166 }
169void LauncherBackend::triggerQuickListAction(const QString &appId, const QString &quickListId)
171 // TODO: execute the given quicklist action
172 Q_UNUSED(appId)
173 Q_UNUSED(quickListId)
176void LauncherBackend::syncFromAccounts()
178 QList<QVariantMap> apps;
179 bool defaults = true;
181 m_storedApps.clear();
183 if (m_accounts && !m_user.isEmpty()) {
184 QVariant variant = m_accounts->getUserProperty(m_user, "com.canonical.unity.AccountsService", "launcher-items");
185 apps = qdbus_cast<QList<QVariantMap>>(variant.value<QDBusArgument>());
186 defaults = isDefaultsItem(apps);
187 }
189 if (m_accounts && defaults) { // Checking accounts as it'll be null when !useStorage
190 QGSettings gSettings("com.canonical.Unity.Launcher", "/com/canonical/unity/launcher/");
191 Q_FOREACH(const QString &entry, gSettings.get("favorites").toStringList()) {
192 if (entry.startsWith("application://")) {
193 QString appId = entry;
194 // Transform "application://foobar.desktop" to "foobar"
195 appId.remove("application://");
196 if (appId.endsWith(".desktop")) {
197 appId.chop(8);
198 }
199 QString df = findDesktopFile(appId);
201 if (!df.isEmpty()) {
202 m_storedApps << appId;
204 if (!m_itemCache.contains(appId)) {
205 m_itemCache.insert(appId, parseDesktopFile(df));
206 }
207 }
208 }
209 }
210 } else {
211 for (const QVariant &app: apps) {
212 loadFromVariant(app.toMap());
213 }
214 }
217void LauncherBackend::syncToAccounts()
219 if (m_accounts && !m_user.isEmpty()) {
220 QList<QVariantMap> items;
222 Q_FOREACH(const QString &appId, m_storedApps) {
223 items << itemToVariant(appId);
224 }
226 m_accounts->setUserProperty(m_user, "com.canonical.unity.AccountsService", "launcher-items", QVariant::fromValue(items));
227 }
230QString LauncherBackend::findDesktopFile(const QString &appId) const
232 int dashPos = -1;
233 QString helper = appId;
235 QStringList searchDirs;
236 searchDirs << QDir::homePath() + "/.local/share/applications";
237 searchDirs << "/usr/share/applications";
239#ifdef LAUNCHER_TESTING
240 searchDirs << "";
241#endif
243 do {
244 if (dashPos != -1) {
245 helper = helper.replace(dashPos, 1, '/');
246 }
248 Q_FOREACH(const QString &searchDir, searchDirs) {
249 QFileInfo fileInfo(QDir(searchDir), helper + ".desktop");
250 if (fileInfo.exists()) {
251 return fileInfo.absoluteFilePath();
252 }
253 }
255 dashPos = helper.indexOf("-");
256 } while (dashPos != -1);
258 return QString();
261LauncherBackendItem* LauncherBackend::parseDesktopFile(const QString &desktopFile) const
263 QSettings settings(desktopFile, QSettings::IniFormat);
265 LauncherBackendItem* item = new LauncherBackendItem();
266 item->desktopFile = desktopFile;
267 item->displayName = settings.value("Desktop Entry/Name").toString();
269 QString iconString = settings.value("Desktop Entry/Icon").toString();
270 QString pathString = settings.value("Desktop Entry/Path").toString();
271 if (QFileInfo(iconString).exists()) {
272 item->icon = QFileInfo(iconString).absoluteFilePath();
273 } else if (QFileInfo(pathString + '/' + iconString).exists()) {
274 item->icon = pathString + '/' + iconString;
275 } else {
276 item->icon = "image://theme/" + iconString;
277 }
278 return item;
281void LauncherBackend::loadFromVariant(const QVariantMap &details)
283 if (!details.contains("id")) {
284 return;
285 }
286 QString appId = details.value("id").toString();
288 LauncherBackendItem *item = m_itemCache.value(appId);
289 if (item) {
290 delete item;
291 }
293 item = new LauncherBackendItem();
295 item->desktopFile = details.value("desktopFile").toString();
296 item->displayName = details.value("name").toString();
297 item->icon = details.value("icon").toString();
299 m_itemCache.insert(appId, item);
300 m_storedApps.append(appId);
303QVariantMap LauncherBackend::itemToVariant(const QString &appId) const
305 LauncherBackendItem *item = m_itemCache.value(appId);
306 QVariantMap details;
307 details.insert("id", appId);
308 details.insert("name", item->displayName);
309 details.insert("icon", item->icon);
310 details.insert("desktopFile", item->desktopFile);
311 return details;
314bool LauncherBackend::isDefaultsItem(const QList<QVariantMap> &apps) const
316 // To differentiate between an empty list and a list that hasn't been set
317 // yet (and should thus be populated with the defaults), we use a special
318 // list of one item with the 'defaults' field set to true.
319 return (apps.size() == 1 && apps[0].value("defaults").toBool());