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>
30class LauncherBackendItem
33 LauncherBackendItem(): pinned(false) {}
41LauncherBackend::LauncherBackend(bool useStorage, QObject *parent):
46 m_accounts = new AccountsServiceDBusAdaptor(this);
48 m_user = qgetenv("USER");
52LauncherBackend::~LauncherBackend()
56 Q_FOREACH(LauncherBackendItem *item, m_itemCache) {
62QStringList LauncherBackend::storedApplications() const
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);
76 m_storedApps = appIds;
77 Q_FOREACH(const QString &appId, appIds) {
78 if (!m_itemCache.contains(appId)) {
79 QString df = findDesktopFile(appId);
81 LauncherBackendItem *item = parseDesktopFile(df);
82 m_itemCache.insert(appId, item);
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);
93QString LauncherBackend::desktopFile(const QString &appId) const
95 LauncherBackendItem *item = m_itemCache.value(appId);
97 return item->desktopFile;
100 return findDesktopFile(appId);
103QString LauncherBackend::displayName(const QString &appId) const
105 LauncherBackendItem *item = m_itemCache.value(appId);
107 return item->displayName;
110 QString df = findDesktopFile(appId);
112 LauncherBackendItem *item = parseDesktopFile(df);
113 m_itemCache.insert(appId, item);
114 return item->displayName;
120QString LauncherBackend::icon(const QString &appId) const
123 LauncherBackendItem *item = m_itemCache.value(appId);
125 iconName = item->icon;
127 QString df = findDesktopFile(appId);
129 LauncherBackendItem *item = parseDesktopFile(df);
130 m_itemCache.insert(appId, item);
131 iconName = item->icon;
138bool LauncherBackend::isPinned(const QString &appId) const
140 LauncherBackendItem *item = m_itemCache.value(appId);
147void LauncherBackend::setPinned(const QString &appId, bool pinned)
149 if (!m_storedApps.contains(appId)) {
153 LauncherBackendItem *item = m_itemCache.value(appId);
155 item = new LauncherBackendItem();
156 m_itemCache.insert(appId, item);
158 if (item->pinned != pinned) {
159 item->pinned = pinned;
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
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.
182int LauncherBackend::count(const QString &appId) const
184 // TODO: Return value for count emblem.
185 // TODO: emit countChanged() when this value changes.
190void LauncherBackend::setUser(const QString &username)
192 if (qgetenv("USER") == "lightdm" && m_user != username) {
198void LauncherBackend::triggerQuickListAction(const QString &appId, const QString &quickListId)
200 // TODO: execute the given quicklist action
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);
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")) {
228 QString df = findDesktopFile(appId);
231 m_storedApps << appId;
233 if (!m_itemCache.contains(appId)) {
234 m_itemCache.insert(appId, parseDesktopFile(df));
236 m_itemCache.value(appId)->pinned = true;
241 for (const QVariant &app: apps) {
242 loadFromVariant(app.toMap());
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);
256 m_accounts->setUserProperty(m_user, "com.canonical.unity.AccountsService", "launcher-items", QVariant::fromValue(items));
260QString LauncherBackend::findDesktopFile(const QString &appId) const
263 QString helper = appId;
265 QStringList searchDirs;
266 searchDirs << QDir::homePath() + "/.local/share/applications";
267 searchDirs << "/usr/share/applications";
269#ifdef LAUNCHER_TESTING
275 helper = helper.replace(dashPos, 1, '/');
278 Q_FOREACH(const QString &searchDir, searchDirs) {
279 QFileInfo fileInfo(QDir(searchDir), helper + ".desktop");
280 if (fileInfo.exists()) {
281 return fileInfo.absoluteFilePath();
285 dashPos = helper.indexOf("-");
286 } while (dashPos != -1);
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;
306 item->icon = "image://theme/" + iconString;
308 item->pinned = false;
312void LauncherBackend::loadFromVariant(const QVariantMap &details)
314 if (!details.contains("id")) {
317 QString appId = details.value("id").toString();
319 LauncherBackendItem *item = m_itemCache.value(appId);
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);
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);
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());
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