2 * Copyright (C) 2013 Canonical, Ltd.
5 * Michael Terry <michael.terry@canonical.com>
6 * Michael Zanetti <michael.zanetti@canonical.com>
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.
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.
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/>.
21#include "AccountsServiceDBusAdaptor.h"
22#include "launcherbackend.h"
25#include <QDBusArgument>
29#include <QStandardPaths>
31class LauncherBackendItem
39LauncherBackend::LauncherBackend(QObject *parent):
43#ifndef LAUNCHER_TESTING
44 m_accounts = new AccountsServiceDBusAdaptor(this);
46 m_user = qgetenv("USER");
50LauncherBackend::~LauncherBackend()
54 Q_FOREACH(LauncherBackendItem *item, m_itemCache) {
60QStringList LauncherBackend::storedApplications() const
65void LauncherBackend::setStoredApplications(const QStringList &appIds)
67 if (appIds.count() < m_storedApps.count()) {
68 Q_FOREACH(const QString &appId, m_storedApps) {
69 if (!appIds.contains(appId)) {
70 delete m_itemCache.take(appId);
74 m_storedApps = appIds;
75 Q_FOREACH(const QString &appId, appIds) {
76 if (!m_itemCache.contains(appId)) {
77 QString df = findDesktopFile(appId);
79 LauncherBackendItem *item = parseDesktopFile(df);
80 m_itemCache.insert(appId, item);
82 // Cannot find any data for that app... ignoring it.
83 qWarning() << "cannot find desktop file for" << appId << ". discarding app.";
84 m_storedApps.removeAll(appId);
91QString LauncherBackend::desktopFile(const QString &appId) const
93 LauncherBackendItem *item = m_itemCache.value(appId);
95 return item->desktopFile;
98 return findDesktopFile(appId);
101QString LauncherBackend::displayName(const QString &appId) const
103 LauncherBackendItem *item = m_itemCache.value(appId);
105 return item->displayName;
108 QString df = findDesktopFile(appId);
110 LauncherBackendItem *item = parseDesktopFile(df);
111 m_itemCache.insert(appId, item);
112 return item->displayName;
118QString LauncherBackend::icon(const QString &appId) const
121 LauncherBackendItem *item = m_itemCache.value(appId);
123 iconName = item->icon;
125 QString df = findDesktopFile(appId);
127 LauncherBackendItem *item = parseDesktopFile(df);
128 m_itemCache.insert(appId, item);
129 iconName = item->icon;
136QList<QuickListEntry> LauncherBackend::quickList(const QString &appId) const
138 // TODO: Get static (from .desktop file) and dynamic (from the app itself)
139 // entries and return them here. Frontend related entries (like "Pin to launcher")
140 // don't matter here. This is just the backend part.
141 // TODO: emit quickListChanged() when the dynamic part changes
143 return QList<QuickListEntry>();
146int LauncherBackend::progress(const QString &appId) const
148 // TODO: Return value for progress emblem.
149 // TODO: emit progressChanged() when this value changes.
154int LauncherBackend::count(const QString &appId) const
156 // TODO: Return value for count emblem.
157 // TODO: emit countChanged() when this value changes.
162void LauncherBackend::setUser(const QString &username)
164 if (qgetenv("USER") == "lightdm" && m_user != username) {
170void LauncherBackend::triggerQuickListAction(const QString &appId, const QString &quickListId)
172 // TODO: execute the given quicklist action
174 Q_UNUSED(quickListId)
177void LauncherBackend::syncFromAccounts()
179 QList<QVariantMap> apps;
180 bool defaults = true;
182 m_storedApps.clear();
184 if (m_accounts && !m_user.isEmpty()) {
185 QVariant variant = m_accounts->getUserProperty(m_user, "com.canonical.unity.AccountsService", "launcher-items");
186 apps = qdbus_cast<QList<QVariantMap>>(variant.value<QDBusArgument>());
187 defaults = isDefaultsItem(apps);
190 if (m_accounts && defaults) { // Checking accounts as it'll be null when !useStorage
191 QGSettings gSettings("com.canonical.Unity.Launcher", "/com/canonical/unity/launcher/");
192 Q_FOREACH(const QString &entry, gSettings.get("favorites").toStringList()) {
193 if (entry.startsWith("application://")) {
194 QString appId = entry;
195 // Transform "application://foobar.desktop" to "foobar"
196 appId.remove("application://");
197 if (appId.endsWith(".desktop")) {
200 QString df = findDesktopFile(appId);
203 m_storedApps << appId;
205 if (!m_itemCache.contains(appId)) {
206 m_itemCache.insert(appId, parseDesktopFile(df));
212 for (const QVariant &app: apps) {
213 loadFromVariant(app.toMap());
218void LauncherBackend::syncToAccounts()
220 if (m_accounts && !m_user.isEmpty()) {
221 QList<QVariantMap> items;
223 Q_FOREACH(const QString &appId, m_storedApps) {
224 items << itemToVariant(appId);
227 m_accounts->setUserProperty(m_user, "com.canonical.unity.AccountsService", "launcher-items", QVariant::fromValue(items));
231QString LauncherBackend::findDesktopFile(const QString &appId) const
234 QString helper = appId;
236 QStringList searchDirs = QStandardPaths::standardLocations(QStandardPaths::ApplicationsLocation);
237#ifdef LAUNCHER_TESTING
243 helper = helper.replace(dashPos, 1, '/');
246 Q_FOREACH(const QString &searchDir, searchDirs) {
247 QFileInfo fileInfo(QDir(searchDir), helper + ".desktop");
248 if (fileInfo.exists()) {
249 return fileInfo.absoluteFilePath();
253 dashPos = helper.indexOf("-");
254 } while (dashPos != -1);
259LauncherBackendItem* LauncherBackend::parseDesktopFile(const QString &desktopFile) const
261 QSettings settings(desktopFile, QSettings::IniFormat);
263 LauncherBackendItem* item = new LauncherBackendItem();
264 item->desktopFile = desktopFile;
265 item->displayName = settings.value("Desktop Entry/Name").toString();
267 QString iconString = settings.value("Desktop Entry/Icon").toString();
268 QString pathString = settings.value("Desktop Entry/Path").toString();
269 if (QFileInfo(iconString).exists()) {
270 item->icon = QFileInfo(iconString).absoluteFilePath();
271 } else if (QFileInfo(pathString + '/' + iconString).exists()) {
272 item->icon = pathString + '/' + iconString;
274 item->icon = "image://theme/" + iconString;
279void LauncherBackend::loadFromVariant(const QVariantMap &details)
281 if (!details.contains("id")) {
284 QString appId = details.value("id").toString();
286 LauncherBackendItem *item = m_itemCache.value(appId);
291 item = new LauncherBackendItem();
293 item->desktopFile = details.value("desktopFile").toString();
294 item->displayName = details.value("name").toString();
295 item->icon = details.value("icon").toString();
297 m_itemCache.insert(appId, item);
298 m_storedApps.append(appId);
301QVariantMap LauncherBackend::itemToVariant(const QString &appId) const
303 LauncherBackendItem *item = m_itemCache.value(appId);
305 details.insert("id", appId);
306 details.insert("name", item->displayName);
307 details.insert("icon", item->icon);
308 details.insert("desktopFile", item->desktopFile);
312bool LauncherBackend::isDefaultsItem(const QList<QVariantMap> &apps) const
314 // To differentiate between an empty list and a list that hasn't been set
315 // yet (and should thus be populated with the defaults), we use a special
316 // list of one item with the 'defaults' field set to true.
317 return (apps.size() == 1 && apps[0].value("defaults").toBool());
keysesc to close
- j k
- move down, up
- enter
- open
- esc
- back, or up a level
- 1 2 3 4
- readme, files, commits, refs
- /
- filter
- y
- copy the clone command
- c
- compare the chosen commit with another (or shift-click both)
- ?
- show or hide this list