on b64ae7d 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 "AccountsService.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 AccountsService(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, "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 appId.remove("application://");
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, "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 << "/usr/share/applications";
268// FIXME: Right now the appId can be (or rather is) a full path
269// to a .desktop file. This will change in the future.
270// Once the ApplicationManager has been switched over to use appIds,
271// remove this chop() and enable the ifdef to only search the current
272// working dir for testing. Also, this is the place to add the search
273// paths for click apps in the next step.
274 if (helper.endsWith(".desktop")) {
275 helper.chop(8);
276 }
277//#ifdef LAUNCHER_TESTING
278 searchDirs << "";
279//#endif
281 do {
282 if (dashPos != -1) {
283 helper = helper.replace(dashPos, 1, '/');
284 }
286 Q_FOREACH(const QString &searchDir, searchDirs) {
287 QFileInfo fileInfo(QDir(searchDir), helper + ".desktop");
288 if (fileInfo.exists()) {
289 return fileInfo.absoluteFilePath();
290 }
291 }
293 dashPos = helper.indexOf("-");
294 } while (dashPos != -1);
296 return QString();
299LauncherBackendItem* LauncherBackend::parseDesktopFile(const QString &desktopFile) const
301 QSettings settings(desktopFile, QSettings::IniFormat);
304 LauncherBackendItem* item = new LauncherBackendItem();
305 item->desktopFile = desktopFile;
306 item->displayName = settings.value("Desktop Entry/Name").toString();
307 item->icon = settings.value("Desktop Entry/Icon").toString();
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());