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 "AccountsService.h"
22#include "launcherbackend.h"
25#include <QDBusArgument>
29class LauncherBackendItem
32 LauncherBackendItem(): pinned(false) {}
40LauncherBackend::LauncherBackend(bool useStorage, QObject *parent):
45 m_accounts = new AccountsService(this);
47 m_user = qgetenv("USER");
51LauncherBackend::~LauncherBackend()
55 Q_FOREACH(LauncherBackendItem *item, m_itemCache) {
61QStringList LauncherBackend::storedApplications() const
66void LauncherBackend::setStoredApplications(const QStringList &appIds)
68 if (appIds.count() < m_storedApps.count()) {
69 Q_FOREACH(const QString &appId, m_storedApps) {
70 if (!appIds.contains(appId)) {
71 delete m_itemCache.take(appId);
75 m_storedApps = appIds;
79QString LauncherBackend::desktopFile(const QString &appId) const
81 LauncherBackendItem *item = m_itemCache.value(appId);
83 return item->desktopFile;
86 return findDesktopFile(appId);
89QString LauncherBackend::displayName(const QString &appId) const
91 LauncherBackendItem *item = m_itemCache.value(appId);
93 return item->displayName;
96 QString df = findDesktopFile(appId);
98 LauncherBackendItem *item = parseDesktopFile(df);
99 m_itemCache.insert(appId, item);
100 return item->displayName;
106QString LauncherBackend::icon(const QString &appId) const
109 LauncherBackendItem *item = m_itemCache.value(appId);
111 iconName = item->icon;
113 QString df = findDesktopFile(appId);
115 LauncherBackendItem *item = parseDesktopFile(df);
116 m_itemCache.insert(appId, item);
117 iconName = item->icon;
121 QFileInfo info(iconName);
123 return info.absoluteFilePath();
125 return "image://theme/" + iconName;
128bool LauncherBackend::isPinned(const QString &appId) const
130 LauncherBackendItem *item = m_itemCache.value(appId);
137void LauncherBackend::setPinned(const QString &appId, bool pinned)
139 if (!m_storedApps.contains(appId)) {
143 LauncherBackendItem *item = m_itemCache.value(appId);
145 item = new LauncherBackendItem();
146 m_itemCache.insert(appId, item);
148 if (item->pinned != pinned) {
149 item->pinned = pinned;
154QList<QuickListEntry> LauncherBackend::quickList(const QString &appId) const
156 // TODO: Get static (from .desktop file) and dynamic (from the app itself)
157 // entries and return them here. Frontend related entries (like "Pin to launcher")
158 // don't matter here. This is just the backend part.
159 // TODO: emit quickListChanged() when the dynamic part changes
161 return QList<QuickListEntry>();
164int LauncherBackend::progress(const QString &appId) const
166 // TODO: Return value for progress emblem.
167 // TODO: emit progressChanged() when this value changes.
172int LauncherBackend::count(const QString &appId) const
174 // TODO: Return value for count emblem.
175 // TODO: emit countChanged() when this value changes.
180void LauncherBackend::setUser(const QString &username)
182 if (qgetenv("USER") == "lightdm" && m_user != username) {
188void LauncherBackend::triggerQuickListAction(const QString &appId, const QString &quickListId)
190 // TODO: execute the given quicklist action
192 Q_UNUSED(quickListId)
195void LauncherBackend::syncFromAccounts()
197 QList<QVariantMap> apps;
198 bool defaults = true;
200 m_storedApps.clear();
202 if (m_accounts && m_user.isEmpty()) {
203 QVariant variant = m_accounts->getUserProperty(m_user, "launcher-items");
204 apps = qdbus_cast<QList<QVariantMap>>(variant.value<QDBusArgument>());
205 defaults = isDefaultsItem(apps);
208 if (m_accounts && defaults) { // Checking accounts as it'll be null when !useStorage
209 QGSettings gSettings("com.canonical.Unity.Launcher", "/com/canonical/unity/launcher/");
210 Q_FOREACH(const QString &entry, gSettings.get("favorites").toStringList()) {
211 if (entry.startsWith("application://")) {
212 QString appId = entry;
213 appId.remove("application://");
214 QString df = findDesktopFile(appId);
217 m_storedApps << appId;
219 if (!m_itemCache.contains(appId)) {
220 m_itemCache.insert(appId, parseDesktopFile(df));
222 m_itemCache.value(appId)->pinned = true;
227 for (const QVariant &app: apps) {
228 loadFromVariant(app.toMap());
233void LauncherBackend::syncToAccounts()
235 if (m_accounts && !m_user.isEmpty()) {
236 QList<QVariantMap> items;
238 Q_FOREACH(const QString &appId, m_storedApps) {
239 items << itemToVariant(appId);
242 m_accounts->setUserProperty(m_user, "launcher-items", QVariant::fromValue(items));
246QString LauncherBackend::findDesktopFile(const QString &appId) const
249 QString helper = appId;
253 helper = helper.replace(dashPos, 1, '/');
256 QFileInfo fileInfo(QDir("/usr/share/applications"), helper);
257 if (fileInfo.exists()) {
258 return fileInfo.absoluteFilePath();
261 dashPos = helper.indexOf("-");
262 } while (dashPos != -1);
267LauncherBackendItem* LauncherBackend::parseDesktopFile(const QString &desktopFile) const
269 QSettings settings(desktopFile, QSettings::IniFormat);
272 LauncherBackendItem* item = new LauncherBackendItem();
273 item->desktopFile = desktopFile;
274 item->displayName = settings.value("Desktop Entry/Name").toString();
275 item->icon = settings.value("Desktop Entry/Icon").toString();
276 item->pinned = false;
280void LauncherBackend::loadFromVariant(const QVariantMap &details)
282 if (!details.contains("id")) {
285 QString appId = details.value("id").toString();
287 LauncherBackendItem *item = m_itemCache.value(appId);
292 item = new LauncherBackendItem();
294 item->desktopFile = details.value("desktopFile").toString();
295 item->displayName = details.value("name").toString();
296 item->icon = details.value("icon").toString();
297 item->pinned = details.value("is-pinned").toBool();
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);
307 details.insert("id", appId);
308 details.insert("name", item->displayName);
309 details.insert("icon", item->icon);
310 details.insert("desktopFile", item->desktopFile);
311 details.insert("is-pinned", item->pinned);
315bool LauncherBackend::isDefaultsItem(const QList<QVariantMap> &apps) const
317 // To differentiate between an empty list and a list that hasn't been set
318 // yet (and should thus be populated with the defaults), we use a special
319 // list of one item with the 'defaults' field set to true.
320 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