on xenial help sign in
First cut at launcher-items support
0ee222dMichael Terry
plugins/AccountsService/com.canonical.unity.AccountsService.xml+2
@@ -13,5 +13,7 @@
13 <annotation name="org.freedesktop.Accounts.DefaultValue" value="true"/>
13 <annotation name="org.freedesktop.Accounts.DefaultValue" value="true"/>
14 </property>
14 </property>
15
15
16+ <property name="launcher-items" type="as" access="readwrite"/>
17+
16 </interface>
18 </interface>
17</node>
19</node>
plugins/Unity/Launcher/CMakeLists.txt+5−1
@@ -1,8 +1,11 @@
1include(FindPkgConfig)
1include(FindPkgConfig)
2pkg_check_modules(LAUNCHER_API REQUIRED unity-shell-launcher=2)
2pkg_check_modules(LAUNCHER_API REQUIRED unity-shell-launcher=2)
3
3
4+add_definitions(-DSM_BUSNAME=systemBus)
5+
4include_directories(
6include_directories(
5 ${CMAKE_CURRENT_SOURCE_DIR}
7 ${CMAKE_CURRENT_SOURCE_DIR}
8+ ${CMAKE_SOURCE_DIR}/plugins/AccountsService
6)
9)
7
10
8set(QMLLAUNCHERPLUGIN_SRC
11set(QMLLAUNCHERPLUGIN_SRC
@@ -12,6 +15,7 @@ set(QMLLAUNCHERPLUGIN_SRC
12 quicklistmodel.cpp
15 quicklistmodel.cpp
13 common/quicklistentry.cpp
16 common/quicklistentry.cpp
14 backend/launcherbackend.cpp
17 backend/launcherbackend.cpp
18+ ${CMAKE_SOURCE_DIR}/plugins/AccountsService/AccountsService.cpp
15 ${LAUNCHER_API_INCLUDEDIR}/unity/shell/launcher/LauncherItemInterface.h
19 ${LAUNCHER_API_INCLUDEDIR}/unity/shell/launcher/LauncherItemInterface.h
16 ${LAUNCHER_API_INCLUDEDIR}/unity/shell/launcher/LauncherModelInterface.h
20 ${LAUNCHER_API_INCLUDEDIR}/unity/shell/launcher/LauncherModelInterface.h
17 ${LAUNCHER_API_INCLUDEDIR}/unity/shell/launcher/QuickListModelInterface.h
21 ${LAUNCHER_API_INCLUDEDIR}/unity/shell/launcher/QuickListModelInterface.h
@@ -21,7 +25,7 @@ add_library(UnityLauncher-qml MODULE
21 ${QMLLAUNCHERPLUGIN_SRC}
25 ${QMLLAUNCHERPLUGIN_SRC}
22 )
26 )
23
27
24−qt5_use_modules(UnityLauncher-qml Qml)
28+qt5_use_modules(UnityLauncher-qml DBus Qml)
25
29
26# export the qmldir and qmltypes files
30# export the qmldir and qmltypes files
27export_qmlfiles(Unity.Launcher Unity/Launcher)
31export_qmlfiles(Unity.Launcher Unity/Launcher)
plugins/Unity/Launcher/backend/launcherbackend.cpp+123−51
@@ -19,50 +19,34 @@
19
19
20#include "launcherbackend.h"
20#include "launcherbackend.h"
21
21
22−#include <QHash>
22+#include <QDir>
23+#include <QFileInfo>
24+
25+LauncherBackend::LauncherBackendItem::~LauncherBackendItem()
26+{
27+ delete settings;
28+}
23
29
24LauncherBackend::LauncherBackend(QObject *parent):
30LauncherBackend::LauncherBackend(QObject *parent):
25 QObject(parent)
31 QObject(parent)
26{
32{
33+ m_accounts = new AccountsService(this);
34+ syncFromAccounts();
27
35
28 // TODO: load default pinned ones from default config, instead of hardcoding here...
36 // TODO: load default pinned ones from default config, instead of hardcoding here...
29−
30− m_storedApps <<
37+ if (m_storedApps.isEmpty()) {
38+ setStoredApplications(QStringList() <<
31− QLatin1String("phone-app.desktop") <<
39+ "phone-app.desktop" <<
32− QLatin1String("camera-app.desktop") <<
40+ "camera-app.desktop" <<
33− QLatin1String("gallery-app.desktop") <<
41+ "gallery-app.desktop" <<
34− QLatin1String("facebook-webapp.desktop") <<
42+ "facebook-webapp.desktop" <<
35− QLatin1String("webbrowser-app.desktop") <<
43+ "webbrowser-app.desktop" <<
36− QLatin1String("twitter-webapp.desktop") <<
44+ "twitter-webapp.desktop" <<
37− QLatin1String("gmail-webapp.desktop") <<
45+ "gmail-webapp.desktop" <<
38− QLatin1String("ubuntu-weather-app.desktop") <<
46+ "ubuntu-weather-app.desktop" <<
39− QLatin1String("notes-app.desktop") <<
47+ "notes-app.desktop" <<
40− QLatin1String("calendar-app.desktop");
48+ "calendar-app.desktop");
41−
49+ }
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−
66}
50}
67
51
68LauncherBackend::~LauncherBackend()
52LauncherBackend::~LauncherBackend()
@@ -72,43 +56,87 @@ LauncherBackend::~LauncherBackend()
72
56
73QStringList LauncherBackend::storedApplications() const
57QStringList LauncherBackend::storedApplications() const
74{
58{
59+ auto files = QStringList();
60+ for (int i = 0; i < m_storedApps.size(); i++) {
61+ files << m_storedApps[i].settings->fileName();
62+ }
75− return m_storedApps;
63+ return files;
76}
64}
77
65
78void LauncherBackend::setStoredApplications(const QStringList &appIds)
66void LauncherBackend::setStoredApplications(const QStringList &appIds)
79{
67{
80− m_storedApps = appIds;
68+ // Are we dropping any pinned apps?
81− // TODO: Cleanup pinned state from settings for apps not in list any more.
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+ }
82}
95}
83
96
84QString LauncherBackend::desktopFile(const QString &appId) const
97QString LauncherBackend::desktopFile(const QString &appId) const
85{
98{
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();
88}
101}
89
102
90QString LauncherBackend::displayName(const QString &appId) const
103QString LauncherBackend::displayName(const QString &appId) const
91{
104{
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+ }
93}
111}
94
112
95QString LauncherBackend::icon(const QString &appId) const
113QString LauncherBackend::icon(const QString &appId) const
96{
114{
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+ }
98}
121}
99
122
100bool LauncherBackend::isPinned(const QString &appId) const
123bool LauncherBackend::isPinned(const QString &appId) const
101{
124{
102− // TODO: return app's pinned state from settings
125+ auto index = findItem(appId);
103− Q_UNUSED(appId)
126+ if (index < 0) {
104− return true;
127+ return false;
128+ } else {
129+ return m_storedApps[index].pinned;
130+ }
105}
131}
106
132
107void LauncherBackend::setPinned(const QString &appId, bool pinned)
133void LauncherBackend::setPinned(const QString &appId, bool pinned)
108{
134{
109− // TODO: Store pinned state in settings.
135+ auto index = findItem(appId);
110− Q_UNUSED(appId)
136+ if (index >= 0 && !m_storedApps[index].pinned) {
111− Q_UNUSED(pinned)
137+ m_storedApps[index].pinned = pinned;
138+ syncToAccounts();
139+ }
112}
140}
113
141
114QList<QuickListEntry> LauncherBackend::quickList(const QString &appId) const
142QList<QuickListEntry> LauncherBackend::quickList(const QString &appId) const
@@ -143,3 +171,47 @@ void LauncherBackend::triggerQuickListAction(const QString &appId, const QString
143 Q_UNUSED(appId)
171 Q_UNUSED(appId)
144 Q_UNUSED(quickListId)
172 Q_UNUSED(quickListId)
145}
173}
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 @@
19#ifndef LAUNCHERBACKEND_H
19#ifndef LAUNCHERBACKEND_H
20#define LAUNCHERBACKEND_H
20#define LAUNCHERBACKEND_H
21
21
22+#include "AccountsService.h"
22#include "common/quicklistentry.h"
23#include "common/quicklistentry.h"
23
24
24#include <QObject>
25#include <QObject>
26+#include <QSettings>
25#include <QStringList>
27#include <QStringList>
26−#include <QHash>
27
28
28/**
29/**
29 * @brief An interface that provides all the data needed by the launcher.
30 * @brief An interface that provides all the data needed by the launcher.
@@ -124,9 +125,23 @@ Q_SIGNALS:
124 void countChanged(const QString &appId, int count);
125 void countChanged(const QString &appId, int count);
125
126
126private:
127private:
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+
127− QStringList m_storedApps;
143+ QList<LauncherBackendItem> m_storedApps;
128− QHash<QString, QString> m_displayNameMap;
144+ AccountsService *m_accounts;
129− QHash<QString, QString> m_iconMap;
130};
145};
131
146
132#endif // LAUNCHERBACKEND_H
147#endif // LAUNCHERBACKEND_H
plugins/Unity/Launcher/launchermodel.cpp+2
@@ -99,6 +99,7 @@ void LauncherModel::pin(const QString &appId, int index)
99 if (currentIndex >= 0) {
99 if (currentIndex >= 0) {
100 if (index == -1 || index == currentIndex) {
100 if (index == -1 || index == currentIndex) {
101 m_list.at(currentIndex)->setPinned(true);
101 m_list.at(currentIndex)->setPinned(true);
102+ m_backend->setPinned(appId, true);
102 QModelIndex modelIndex = this->index(currentIndex);
103 QModelIndex modelIndex = this->index(currentIndex);
103 Q_EMIT dataChanged(modelIndex, modelIndex);
104 Q_EMIT dataChanged(modelIndex, modelIndex);
104 } else {
105 } else {
@@ -114,6 +115,7 @@ void LauncherModel::pin(const QString &appId, int index)
114 m_backend->displayName(appId),
115 m_backend->displayName(appId),
115 m_backend->icon(appId));
116 m_backend->icon(appId));
116 item->setPinned(true);
117 item->setPinned(true);
118+ m_backend->setPinned(appId, true);
117 m_list.insert(index, item);
119 m_list.insert(index, item);
118 endInsertRows();
120 endInsertRows();
119 }
121 }