on xenial help sign in
First cut at launcher-items support
commit0ee222d1b94f452a7acc786186ee6b22831b3313 authorMichael Terry <michael.terry@canonical.com> dateSun 18 Aug 2013, 21:35 (13 years ago) parent2338298
5 files changed+151−56
plugins/AccountsService/com.canonical.unity.AccountsService.xml+2 plugins/Unity/Launcher/CMakeLists.txt+5−1 plugins/Unity/Launcher/backend/launcherbackend.cpp+123−51 plugins/Unity/Launcher/backend/launcherbackend.h+19−4 plugins/Unity/Launcher/launchermodel.cpp+2
plugins/AccountsService/com.canonical.unity.AccountsService.xml+2
@@ -13,5 +13,7 @@
1313 <annotation name="org.freedesktop.Accounts.DefaultValue" value="true"/>
1414 </property>
1515
16+ <property name="launcher-items" type="as" access="readwrite"/>
17+
1618 </interface>
1719</node>
plugins/Unity/Launcher/CMakeLists.txt+5−1
@@ -1,8 +1,11 @@
11include(FindPkgConfig)
22pkg_check_modules(LAUNCHER_API REQUIRED unity-shell-launcher=2)
33
4+add_definitions(-DSM_BUSNAME=systemBus)
5+
46include_directories(
57 ${CMAKE_CURRENT_SOURCE_DIR}
8+ ${CMAKE_SOURCE_DIR}/plugins/AccountsService
69)
710
811set(QMLLAUNCHERPLUGIN_SRC
@@ -12,6 +15,7 @@ set(QMLLAUNCHERPLUGIN_SRC
1215 quicklistmodel.cpp
1316 common/quicklistentry.cpp
1417 backend/launcherbackend.cpp
18+ ${CMAKE_SOURCE_DIR}/plugins/AccountsService/AccountsService.cpp
1519 ${LAUNCHER_API_INCLUDEDIR}/unity/shell/launcher/LauncherItemInterface.h
1620 ${LAUNCHER_API_INCLUDEDIR}/unity/shell/launcher/LauncherModelInterface.h
1721 ${LAUNCHER_API_INCLUDEDIR}/unity/shell/launcher/QuickListModelInterface.h
@@ -21,7 +25,7 @@ add_library(UnityLauncher-qml MODULE
2125 ${QMLLAUNCHERPLUGIN_SRC}
2226 )
2327
24−qt5_use_modules(UnityLauncher-qml Qml)
28+qt5_use_modules(UnityLauncher-qml DBus Qml)
2529
2630# export the qmldir and qmltypes files
2731export_qmlfiles(Unity.Launcher Unity/Launcher)
plugins/Unity/Launcher/backend/launcherbackend.cpp+123−51
@@ -19,50 +19,34 @@
1919
2020#include "launcherbackend.h"
2121
22−#include <QHash>
22+#include <QDir>
23+#include <QFileInfo>
24+
25+LauncherBackend::LauncherBackendItem::~LauncherBackendItem()
26+{
27+ delete settings;
28+}
2329
2430LauncherBackend::LauncherBackend(QObject *parent):
2531 QObject(parent)
2632{
33+ m_accounts = new AccountsService(this);
34+ syncFromAccounts();
2735
2836 // TODO: load default pinned ones from default config, instead of hardcoding here...
29−
30− m_storedApps <<
31− QLatin1String("phone-app.desktop") <<
32− QLatin1String("camera-app.desktop") <<
33− QLatin1String("gallery-app.desktop") <<
34− QLatin1String("facebook-webapp.desktop") <<
35− QLatin1String("webbrowser-app.desktop") <<
36− QLatin1String("twitter-webapp.desktop") <<
37− QLatin1String("gmail-webapp.desktop") <<
38− QLatin1String("ubuntu-weather-app.desktop") <<
39− QLatin1String("notes-app.desktop") <<
40− QLatin1String("calendar-app.desktop");
41−
42− // TODO: get stuff from desktop files instead this hardcoded map
43− m_displayNameMap.insert("phone-app.desktop", "Phone");
44− m_displayNameMap.insert("camera-app.desktop", "Camera");
45− m_displayNameMap.insert("gallery-app.desktop", "Gallery");
46− m_displayNameMap.insert("facebook-webapp.desktop", "Facebook");
47− m_displayNameMap.insert("webbrowser-app.desktop", "Browser");
48− m_displayNameMap.insert("twitter-webapp.desktop", "Twitter");
49− m_displayNameMap.insert("gmail-webapp.desktop", "GMail");
50− m_displayNameMap.insert("ubuntu-weather-app.desktop", "Weather");
51− m_displayNameMap.insert("notes-app.desktop", "Notes");
52− m_displayNameMap.insert("calendar-app.desktop", "Calendar");
53−
54− // TODO: get stuff from desktop files instead this hardcoded map
55− m_iconMap.insert("phone-app.desktop", "phone-app");
56− m_iconMap.insert("camera-app.desktop", "camera");
57− m_iconMap.insert("gallery-app.desktop", "gallery");
58− m_iconMap.insert("facebook-webapp.desktop", "facebook");
59− m_iconMap.insert("webbrowser-app.desktop", "browser");
60− m_iconMap.insert("twitter-webapp.desktop", "twitter");
61− m_iconMap.insert("gmail-webapp.desktop", "gmail");
62− m_iconMap.insert("ubuntu-weather-app.desktop", "weather");
63− m_iconMap.insert("notes-app.desktop", "notepad");
64− m_iconMap.insert("calendar-app.desktop", "calendar");
65−
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+ }
6650}
6751
6852LauncherBackend::~LauncherBackend()
@@ -72,43 +56,87 @@ LauncherBackend::~LauncherBackend()
7256
7357QStringList LauncherBackend::storedApplications() const
7458{
75− return m_storedApps;
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;
7664}
7765
7866void LauncherBackend::setStoredApplications(const QStringList &appIds)
7967{
80− m_storedApps = appIds;
81− // TODO: Cleanup pinned state from settings for apps not in list any more.
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+ }
86+
87+ m_storedApps.clear();
88+ for (auto appId: appIds) {
89+ loadDesktopFile(appId);
90+ }
91+
92+ if (needToSync) {
93+ syncToAccounts();
94+ }
8295}
8396
8497QString LauncherBackend::desktopFile(const QString &appId) const
8598{
86− // TODO: return real path instead of this hardcoded one
87− return QLatin1String("/usr/share/applications/") + appId;
99+ auto fileInfo = QFileInfo(QDir("/usr/share/applications"), appId);
100+ return fileInfo.canonicalFilePath();
88101}
89102
90103QString LauncherBackend::displayName(const QString &appId) const
91104{
92− return m_displayNameMap.value(appId);
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+ }
93111}
94112
95113QString LauncherBackend::icon(const QString &appId) const
96114{
97− return m_iconMap.value(appId);
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+ }
98121}
99122
100123bool LauncherBackend::isPinned(const QString &appId) const
101124{
102− // TODO: return app's pinned state from settings
103− Q_UNUSED(appId)
104− return true;
125+ auto index = findItem(appId);
126+ if (index < 0) {
127+ return false;
128+ } else {
129+ return m_storedApps[index].pinned;
130+ }
105131}
106132
107133void LauncherBackend::setPinned(const QString &appId, bool pinned)
108134{
109− // TODO: Store pinned state in settings.
110− Q_UNUSED(appId)
111− Q_UNUSED(pinned)
135+ auto index = findItem(appId);
136+ if (index >= 0 && !m_storedApps[index].pinned) {
137+ m_storedApps[index].pinned = pinned;
138+ syncToAccounts();
139+ }
112140}
113141
114142QList<QuickListEntry> LauncherBackend::quickList(const QString &appId) const
@@ -143,3 +171,47 @@ void LauncherBackend::triggerQuickListAction(const QString &appId, const QString
143171 Q_UNUSED(appId)
144172 Q_UNUSED(quickListId)
145173}
174+
175+void LauncherBackend::syncFromAccounts()
176+{
177+ auto username = qgetenv("USER");
178+ if (username != "") {
179+ auto itemsVariant = m_accounts->getUserProperty(username, "launcher-items");
180+ setStoredApplications(itemsVariant.toStringList());
181+ }
182+}
183+
184+void LauncherBackend::syncToAccounts()
185+{
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+ }
192+
193+ auto username = qgetenv("USER");
194+ if (username != "") {
195+ m_accounts->setUserProperty(username, "launcher-items", QVariant(pinnedItems));
196+ }
197+}
198+
199+bool LauncherBackend::loadDesktopFile(const QString &appId)
200+{
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
206+}
207+
208+int LauncherBackend::findItem(const QString &appId) const
209+{
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;
217+}
plugins/Unity/Launcher/backend/launcherbackend.h+19−4
@@ -19,11 +19,12 @@
1919#ifndef LAUNCHERBACKEND_H
2020#define LAUNCHERBACKEND_H
2121
22+#include "AccountsService.h"
2223#include "common/quicklistentry.h"
2324
2425#include <QObject>
26+#include <QSettings>
2527#include <QStringList>
26−#include <QHash>
2728
2829/**
2930 * @brief An interface that provides all the data needed by the launcher.
@@ -124,9 +125,23 @@ Q_SIGNALS:
124125 void countChanged(const QString &appId, int count);
125126
126127private:
127− QStringList m_storedApps;
128− QHash<QString, QString> m_displayNameMap;
129− QHash<QString, QString> m_iconMap;
128+ QString resolveAppId(const QString &appId) const;
129+ bool loadDesktopFile(const QString &appId);
130+ int findItem(const QString &appId) const;
131+ void syncFromAccounts();
132+ void syncToAccounts();
133+
134+ class LauncherBackendItem
135+ {
136+ public:
137+ ~LauncherBackendItem();
138+
139+ QSettings *settings;
140+ bool pinned;
141+ };
142+
143+ QList<LauncherBackendItem> m_storedApps;
144+ AccountsService *m_accounts;
130145};
131146
132147#endif // LAUNCHERBACKEND_H
plugins/Unity/Launcher/launchermodel.cpp+2
@@ -99,6 +99,7 @@ void LauncherModel::pin(const QString &appId, int index)
9999 if (currentIndex >= 0) {
100100 if (index == -1 || index == currentIndex) {
101101 m_list.at(currentIndex)->setPinned(true);
102+ m_backend->setPinned(appId, true);
102103 QModelIndex modelIndex = this->index(currentIndex);
103104 Q_EMIT dataChanged(modelIndex, modelIndex);
104105 } else {
@@ -114,6 +115,7 @@ void LauncherModel::pin(const QString &appId, int index)
114115 m_backend->displayName(appId),
115116 m_backend->icon(appId));
116117 item->setPinned(true);
118+ m_backend->setPinned(appId, true);
117119 m_list.insert(index, item);
118120 endInsertRows();
119121 }