on 0ee222d help sign in
unity8/plugins/Unity/Launcher/backend/launcherbackend.cpp
217 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 syncFromAccounts();
36 // TODO: load default pinned ones from default config, instead of hardcoding here...
37 if (m_storedApps.isEmpty()) {
38 setStoredApplications(QStringList() <<
39 "phone-app.desktop" <<
40 "camera-app.desktop" <<
41 "gallery-app.desktop" <<
42 "facebook-webapp.desktop" <<
43 "webbrowser-app.desktop" <<
44 "twitter-webapp.desktop" <<
45 "gmail-webapp.desktop" <<
46 "ubuntu-weather-app.desktop" <<
47 "notes-app.desktop" <<
48 "calendar-app.desktop");
49 }
52LauncherBackend::~LauncherBackend()
57QStringList LauncherBackend::storedApplications() const
59 auto files = QStringList();
60 for (int i = 0; i < m_storedApps.size(); i++) {
61 files << m_storedApps[i].settings->fileName();
62 }
63 return files;
66void LauncherBackend::setStoredApplications(const QStringList &appIds)
68 // Are we dropping any pinned apps?
69 auto needToSync = false;
70 for (int i = 0; i < m_storedApps.size(); i++) {
71 if (m_storedApps[i].pinned) {
72 auto found = false;
73 for (int j = 0; j < appIds.size(); j++) {
74 auto fullAppId = desktopFile(appIds[j]);
75 if (m_storedApps[i].settings->fileName() == fullAppId) {
76 found = true;
77 break;
78 }
79 }
80 if (!found) {
81 needToSync = true;
82 break;
83 }
84 }
85 }
87 m_storedApps.clear();
88 for (auto appId: appIds) {
89 loadDesktopFile(appId);
90 }
92 if (needToSync) {
93 syncToAccounts();
94 }
97QString LauncherBackend::desktopFile(const QString &appId) const
99 auto fileInfo = QFileInfo(QDir("/usr/share/applications"), appId);
100 return fileInfo.canonicalFilePath();
103QString LauncherBackend::displayName(const QString &appId) const
105 auto index = findItem(appId);
106 if (index < 0) {
107 return "";
108 } else {
109 return m_storedApps[index].settings->value("Desktop Entry/Name").toString();
110 }
113QString LauncherBackend::icon(const QString &appId) const
115 auto index = findItem(appId);
116 if (index < 0) {
117 return "";
118 } else {
119 return m_storedApps[index].settings->value("Desktop Entry/Icon").toString();
120 }
123bool LauncherBackend::isPinned(const QString &appId) const
125 auto index = findItem(appId);
126 if (index < 0) {
127 return false;
128 } else {
129 return m_storedApps[index].pinned;
130 }
133void LauncherBackend::setPinned(const QString &appId, bool pinned)
135 auto index = findItem(appId);
136 if (index >= 0 && !m_storedApps[index].pinned) {
137 m_storedApps[index].pinned = pinned;
138 syncToAccounts();
139 }
142QList<QuickListEntry> LauncherBackend::quickList(const QString &appId) const
144 // TODO: Get static (from .desktop file) and dynamic (from the app itself)
145 // entries and return them here. Frontend related entries (like "Pin to launcher")
146 // don't matter here. This is just the backend part.
147 // TODO: emit quickListChanged() when the dynamic part changes
148 Q_UNUSED(appId)
149 return QList<QuickListEntry>();
152int LauncherBackend::progress(const QString &appId) const
154 // TODO: Return value for progress emblem.
155 // TODO: emit progressChanged() when this value changes.
156 Q_UNUSED(appId)
157 return -1;
160int LauncherBackend::count(const QString &appId) const
162 // TODO: Return value for count emblem.
163 // TODO: emit countChanged() when this value changes.
164 Q_UNUSED(appId)
165 return 0;
168void LauncherBackend::triggerQuickListAction(const QString &appId, const QString &quickListId)
170 // TODO: execute the given quicklist action
171 Q_UNUSED(appId)
172 Q_UNUSED(quickListId)
175void LauncherBackend::syncFromAccounts()
177 auto username = qgetenv("USER");
178 if (username != "") {
179 auto itemsVariant = m_accounts->getUserProperty(username, "launcher-items");
180 setStoredApplications(itemsVariant.toStringList());
181 }
184void LauncherBackend::syncToAccounts()
186 auto pinnedItems = QStringList();
187 for (int i = 0; i < m_storedApps.size(); i++) {
188 if (m_storedApps[i].pinned) {
189 pinnedItems.append(m_storedApps[i].settings->fileName());
190 }
191 }
193 auto username = qgetenv("USER");
194 if (username != "") {
195 m_accounts->setUserProperty(username, "launcher-items", QVariant(pinnedItems));
196 }
199bool LauncherBackend::loadDesktopFile(const QString &appId)
201 auto item = LauncherBackendItem();
202 auto fullAppId = desktopFile(appId);
203 item.settings = new QSettings(fullAppId, QSettings::IniFormat);
204 m_storedApps.append(item);
205 return true; // QSettings doesn't really indicate a failure mode right now
208int LauncherBackend::findItem(const QString &appId) const
210 auto fullAppId = desktopFile(appId);
211 for (int i = 0; i < m_storedApps.size(); i++) {
212 if (m_storedApps[i].settings->fileName() == fullAppId) {
213 return i;
214 }
215 }
216 return -1;