on dda4ff5 help sign in
unity8/plugins/Unity/Launcher/backend/launcherbackend.cpp
224 lines 6.0 kB history raw
1/*
2 * Copyright (C) 2013 Canonical, Ltd.
3 *
4 * Authors:
5 * Michael Zanetti <michael.zanetti@canonical.com>
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; version 3.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see <http://www.gnu.org/licenses/>.
18 */
20#include "launcherbackend.h"
22#include <QDir>
23#include <QFileInfo>
25LauncherBackend::LauncherBackendItem::~LauncherBackendItem()
27 delete settings;
30LauncherBackend::LauncherBackend(QObject *parent):
31 QObject(parent)
33 m_accounts = new AccountsService(this);
34 setUser(qgetenv("USER"));
37LauncherBackend::~LauncherBackend()
42QStringList LauncherBackend::storedApplications() const
44 auto files = QStringList();
45 for (int i = 0; i < m_storedApps.size(); i++) {
46 files << m_storedApps[i].settings->fileName();
47 }
48 return files;
51void LauncherBackend::setStoredApplications(const QStringList &appIds)
53 // Are we dropping any pinned apps?
54 auto needToSync = false;
55 for (int i = 0; i < m_storedApps.size(); i++) {
56 if (m_storedApps[i].pinned) {
57 auto found = false;
58 for (int j = 0; j < appIds.size(); j++) {
59 auto fullAppId = desktopFile(appIds[j]);
60 if (m_storedApps[i].settings->fileName() == fullAppId) {
61 found = true;
62 break;
63 }
64 }
65 if (!found) {
66 needToSync = true;
67 break;
68 }
69 }
70 }
72 m_storedApps.clear();
73 for (auto appId: appIds) {
74 loadDesktopFile(appId);
75 }
77 if (needToSync) {
78 syncToAccounts();
79 }
82QString LauncherBackend::desktopFile(const QString &appId) const
84 auto fileInfo = QFileInfo(QDir("/usr/share/applications"), appId);
85 return fileInfo.canonicalFilePath();
88QString LauncherBackend::displayName(const QString &appId) const
90 auto index = findItem(appId);
91 if (index < 0) {
92 return "";
93 } else {
94 return m_storedApps[index].settings->value("Desktop Entry/Name").toString();
95 }
98QString LauncherBackend::icon(const QString &appId) const
100 auto index = findItem(appId);
101 if (index < 0) {
102 return "";
103 } else {
104 return m_storedApps[index].settings->value("Desktop Entry/Icon").toString();
105 }
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 != "") {
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);
192void LauncherBackend::syncToAccounts()
194 auto pinnedItems = QStringList();
195 for (int i = 0; i < m_storedApps.size(); i++) {
196 if (m_storedApps[i].pinned) {
197 pinnedItems.append(m_storedApps[i].settings->fileName());
198 }
199 }
201 if (m_user != "") {
202 m_accounts->setUserProperty(m_user, "launcher-items", QVariant(pinnedItems));
203 }
206bool LauncherBackend::loadDesktopFile(const QString &appId)
208 auto item = LauncherBackendItem();
209 auto fullAppId = desktopFile(appId);
210 item.settings = new QSettings(fullAppId, QSettings::IniFormat);
211 m_storedApps.append(item);
212 return true; // QSettings doesn't really indicate a failure mode right now
215int LauncherBackend::findItem(const QString &appId) const
217 auto fullAppId = desktopFile(appId);
218 for (int i = 0; i < m_storedApps.size(); i++) {
219 if (m_storedApps[i].settings->fileName() == fullAppId) {
220 return i;
221 }
222 }
223 return -1;