on b700d05 help sign in
unity8/plugins/Unity/Launcher/backend/launcherbackend.cpp
235 lines 6.5 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 <QFileInfo>
27LauncherBackend::LauncherBackend(bool useStorage, QObject *parent):
28 QObject(parent),
29 m_accounts(nullptr)
31 if (useStorage) {
32 m_accounts = new AccountsService(this);
33 }
34 setUser(qgetenv("USER"));
37LauncherBackend::~LauncherBackend()
39 clearItems();
42void LauncherBackend::clearItems()
44 for (LauncherBackendItem &app: m_storedApps) {
45 delete app.settings;
46 }
47 m_storedApps.clear();
50QStringList LauncherBackend::storedApplications() const
52 auto files = QStringList();
53 for (const LauncherBackendItem &app: m_storedApps) {
54 files << app.settings->fileName();
55 }
56 return files;
59void LauncherBackend::setStoredApplications(const QStringList &appIds)
61 // Record all existing pinned apps, so we can notice them in new list
62 auto pinnedItems = QStringList();
63 for (LauncherBackendItem &app: m_storedApps) {
64 if (app.pinned) {
65 pinnedItems.append(app.settings->fileName());
66 }
67 }
69 clearItems();
71 for (const QString &appId: appIds) {
72 loadDesktopFile(appId, pinnedItems.contains(desktopFile(appId)));
73 }
75 syncToAccounts();
78QString LauncherBackend::desktopFile(const QString &appId) const
80 QFileInfo fileInfo(QDir("/usr/share/applications"), appId);
81 return fileInfo.absoluteFilePath();
84QString LauncherBackend::displayName(const QString &appId) const
86 auto desktopFile = parseDesktopFile(appId);
87 auto displayName = desktopFile->value("Desktop Entry/Name").toString();
88 delete desktopFile;
89 return displayName;
92QString LauncherBackend::icon(const QString &appId) const
94 auto desktopFile = parseDesktopFile(appId);
95 auto iconName = desktopFile->value("Desktop Entry/Icon").toString();
96 delete desktopFile;
98 if (!iconName.isEmpty()) {
99 QFileInfo iconFileInfo(iconName);
100 if (iconFileInfo.isRelative()) {
101 iconName = "image://gicon/" + iconName;
102 }
103 }
105 return iconName;
108bool LauncherBackend::isPinned(const QString &appId) const
110 auto index = findItem(appId);
111 if (index < 0) {
112 return false;
113 } else {
114 return m_storedApps[index].pinned;
115 }
118void LauncherBackend::setPinned(const QString &appId, bool pinned)
120 auto index = findItem(appId);
121 if (index >= 0 && !m_storedApps[index].pinned) {
122 m_storedApps[index].pinned = pinned;
123 syncToAccounts();
124 }
127QList<QuickListEntry> LauncherBackend::quickList(const QString &appId) const
129 // TODO: Get static (from .desktop file) and dynamic (from the app itself)
130 // entries and return them here. Frontend related entries (like "Pin to launcher")
131 // don't matter here. This is just the backend part.
132 // TODO: emit quickListChanged() when the dynamic part changes
133 Q_UNUSED(appId)
134 return QList<QuickListEntry>();
137int LauncherBackend::progress(const QString &appId) const
139 // TODO: Return value for progress emblem.
140 // TODO: emit progressChanged() when this value changes.
141 Q_UNUSED(appId)
142 return -1;
145int LauncherBackend::count(const QString &appId) const
147 // TODO: Return value for count emblem.
148 // TODO: emit countChanged() when this value changes.
149 Q_UNUSED(appId)
150 return 0;
153void LauncherBackend::setUser(const QString &username)
155 m_user = username;
156 syncFromAccounts();
159void LauncherBackend::triggerQuickListAction(const QString &appId, const QString &quickListId)
161 // TODO: execute the given quicklist action
162 Q_UNUSED(appId)
163 Q_UNUSED(quickListId)
166void LauncherBackend::syncFromAccounts()
168 auto appIds = QStringList();
170 if (m_user != "" && m_accounts != nullptr) {
171 appIds = m_accounts->getUserProperty(m_user, "launcher-items").toStringList();
172 }
174 // TODO: load default pinned ones from default config, instead of hardcoding here...
175 if (appIds.isEmpty()) {
176 appIds <<
177 "phone-app.desktop" <<
178 "camera-app.desktop" <<
179 "gallery-app.desktop" <<
180 "facebook-webapp.desktop" <<
181 "webbrowser-app.desktop" <<
182 "twitter-webapp.desktop" <<
183 "gmail-webapp.desktop" <<
184 "ubuntu-weather-app.desktop" <<
185 "notes-app.desktop" <<
186 "calendar-app.desktop";
187 }
189 setStoredApplications(appIds);
191 // Mark all apps as pinned (since we just refreshed the set)
192 for (LauncherBackendItem &app: m_storedApps) {
193 app.pinned = true;
194 }
197void LauncherBackend::syncToAccounts()
199 auto pinnedItems = QStringList();
200 for (LauncherBackendItem &app: m_storedApps) {
201 if (app.pinned) {
202 pinnedItems.append(app.settings->fileName());
203 }
204 }
206 if (m_user != "" && m_accounts != nullptr) {
207 m_accounts->setUserProperty(m_user, "launcher-items", QVariant(pinnedItems));
208 }
211QSettings *LauncherBackend::parseDesktopFile(const QString &appId) const
213 auto fullAppId = desktopFile(appId);
214 return new QSettings(fullAppId, QSettings::IniFormat);
217bool LauncherBackend::loadDesktopFile(const QString &appId, bool isPinned)
219 auto item = LauncherBackendItem();
220 item.settings = parseDesktopFile(appId);
221 item.pinned = isPinned;
222 m_storedApps.append(item);
223 return true; // QSettings doesn't really indicate a failure mode right now
226int LauncherBackend::findItem(const QString &appId) const
228 auto fullAppId = desktopFile(appId);
229 for (int i = 0; i < m_storedApps.size(); ++i) {
230 if (m_storedApps[i].settings->fileName() == fullAppId) {
231 return i;
232 }
233 }
234 return -1;