on xenial help sign in
make the launcher backend read it's defaults from dconf
4af4357Michael Zanetti
Shell.qml+1−1
@@ -469,7 +469,7 @@ FocusScope {
469 onSelected: {
469 onSelected: {
470 // Update launcher items for new user
470 // Update launcher items for new user
471 var user = LightDM.Users.data(uid, LightDM.UserRoles.NameRole);
471 var user = LightDM.Users.data(uid, LightDM.UserRoles.NameRole);
472− LauncherModel.setUser(user);
472+ //LauncherModel.setUser(user);
473 }
473 }
474
474
475 onLeftTeaserPressedChanged: {
475 onLeftTeaserPressedChanged: {
debian/control+1
@@ -9,6 +9,7 @@ Build-Depends: cmake,
9 libgl1-mesa-dev[!armhf] | libgl-dev[!armhf],
9 libgl1-mesa-dev[!armhf] | libgl-dev[!armhf],
10 libgl1-mesa-dri,
10 libgl1-mesa-dri,
11 libgles2-mesa-dev[armhf],
11 libgles2-mesa-dev[armhf],
12+ libgsettings-qt-dev,
12 libhud-client2-dev,
13 libhud-client2-dev,
13 libjson-perl,
14 libjson-perl,
14 libnih-dbus-dev,
15 libnih-dbus-dev,
plugins/Unity/Launcher/CMakeLists.txt+4
@@ -1,11 +1,13 @@
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+pkg_check_modules(GSETTINGS_QT REQUIRED gsettings-qt)
3
4
4add_definitions(-DSM_BUSNAME=systemBus)
5add_definitions(-DSM_BUSNAME=systemBus)
5
6
6include_directories(
7include_directories(
7 ${CMAKE_CURRENT_SOURCE_DIR}
8 ${CMAKE_CURRENT_SOURCE_DIR}
8 ${CMAKE_SOURCE_DIR}/plugins/AccountsService
9 ${CMAKE_SOURCE_DIR}/plugins/AccountsService
10+ ${GSETTINGS_QT_INCLUDE_DIRS}
9)
11)
10
12
11set(QMLLAUNCHERPLUGIN_SRC
13set(QMLLAUNCHERPLUGIN_SRC
@@ -25,6 +27,8 @@ add_library(UnityLauncher-qml MODULE
25 ${QMLLAUNCHERPLUGIN_SRC}
27 ${QMLLAUNCHERPLUGIN_SRC}
26 )
28 )
27
29
30+target_link_libraries(UnityLauncher-qml ${GSETTINGS_QT_LDFLAGS})
31+
28qt5_use_modules(UnityLauncher-qml DBus Qml)
32qt5_use_modules(UnityLauncher-qml DBus Qml)
29
33
30# export the qmldir and qmltypes files
34# export the qmldir and qmltypes files
plugins/Unity/Launcher/backend/launcherbackend.cpp+165−93
@@ -24,6 +24,17 @@
24#include <QDir>
24#include <QDir>
25#include <QDBusArgument>
25#include <QDBusArgument>
26#include <QFileInfo>
26#include <QFileInfo>
27+#include <QGSettings>
28+#include <QDebug>
29+
30+class LauncherBackendItem
31+{
32+public:
33+ QString desktopFile;
34+ QString displayName;
35+ QString icon;
36+ bool pinned;
37+};
27
38
28LauncherBackend::LauncherBackend(bool useStorage, QObject *parent):
39LauncherBackend::LauncherBackend(bool useStorage, QObject *parent):
29 QObject(parent),
40 QObject(parent),
@@ -37,90 +48,106 @@ LauncherBackend::LauncherBackend(bool useStorage, QObject *parent):
37
48
38LauncherBackend::~LauncherBackend()
49LauncherBackend::~LauncherBackend()
39{
50{
40− clearItems();
51+ m_storedApps.clear();
41−}
42
52
43−void LauncherBackend::clearItems()
44−{
45− for (LauncherBackendItem &app: m_storedApps) {
53+ Q_FOREACH(LauncherBackendItem *item, m_itemCache) {
46− delete app.settings;
54+ delete item;
47 }
55 }
48− m_storedApps.clear();
56+ m_itemCache.clear();
49}
57}
50
58
51QStringList LauncherBackend::storedApplications() const
59QStringList LauncherBackend::storedApplications() const
52{
60{
53− auto files = QStringList();
54− for (const LauncherBackendItem &app: m_storedApps) {
55− files << app.settings->fileName();
56− }
57− return files;
61+ return m_storedApps;
58}
62}
59
63
60void LauncherBackend::setStoredApplications(const QStringList &appIds)
64void LauncherBackend::setStoredApplications(const QStringList &appIds)
61{
65{
62− // Record all existing pinned apps, so we can notice them in new list
66+ if (appIds.count() << m_storedApps.count()) {
63− auto pinnedItems = QStringList();
67+ Q_FOREACH(const QString &appId, m_storedApps) {
64− for (LauncherBackendItem &app: m_storedApps) {
68+ if (!appIds.contains(appId)) {
65− if (app.pinned) {
69+ delete m_itemCache.take(appId);
66− pinnedItems.append(app.settings->fileName());
70+ }
67 }
71 }
68 }
72 }
69−
73+ m_storedApps = appIds;
70− clearItems();
71−
72− for (const QString &appId: appIds) {
73− loadApp(makeAppDetails(appId, pinnedItems.contains(desktopFile(appId))));
74− }
75−
76 syncToAccounts();
74 syncToAccounts();
77}
75}
78
76
79QString LauncherBackend::desktopFile(const QString &appId) const
77QString LauncherBackend::desktopFile(const QString &appId) const
80{
78{
81− QFileInfo fileInfo(QDir("/usr/share/applications"), appId);
79+ if (m_itemCache.contains(appId)) {
82− return fileInfo.absoluteFilePath();
80+ return m_itemCache.value(appId)->desktopFile;
81+ }
82+
83+ return findDesktopFile(appId);
83}
84}
84
85
85QString LauncherBackend::displayName(const QString &appId) const
86QString LauncherBackend::displayName(const QString &appId) const
86{
87{
88+ if (m_itemCache.contains(appId)) {
89+ return m_itemCache.value(appId)->displayName;
90+ }
91+
92+ QString df = findDesktopFile(appId);
93+ if (!df.isEmpty()) {
87− auto desktopFile = parseDesktopFile(appId);
94+ LauncherBackendItem *item = parseDesktopFile(df);
88− auto displayName = desktopFile->value("Desktop Entry/Name").toString();
95+ m_itemCache.insert(appId, item);
89− delete desktopFile;
96+ return item->displayName;
97+ }
98+
90− return displayName;
99+ return QString();
91}
100}
92
101
93QString LauncherBackend::icon(const QString &appId) const
102QString LauncherBackend::icon(const QString &appId) const
94{
103{
95− auto desktopFile = parseDesktopFile(appId);
104+ qDebug() << "searching for icon >>>>>>>>>>>>>>>>>>>>" << appId;
96− auto iconName = desktopFile->value("Desktop Entry/Icon").toString();
105+ QString iconName;
97− delete desktopFile;
106+ if (m_itemCache.contains(appId)) {
98−
107+ qDebug() << "found icon in cache" << m_itemCache.value(appId)->icon;
108+ iconName = m_itemCache.value(appId)->icon;
109+ } else {
110+ QString df = findDesktopFile(appId);
99− if (!iconName.isEmpty()) {
111+ if (!df.isEmpty()) {
100− QFileInfo iconFileInfo(iconName);
112+ LauncherBackendItem *item = parseDesktopFile(df);
101− if (iconFileInfo.isRelative()) {
113+ m_itemCache.insert(appId, item);
102− iconName = "image://gicon/" + iconName;
114+ iconName = item->icon;
103 }
115 }
104 }
116 }
105
117
118+ QFileInfo info(iconName);
119+ if (info.exists()) {
120+ return info.absoluteFilePath();
121+ }
106− return iconName;
122+ return "image://theme/" + iconName;
107}
123}
108
124
109bool LauncherBackend::isPinned(const QString &appId) const
125bool LauncherBackend::isPinned(const QString &appId) const
110{
126{
111− auto index = findItem(appId);
127+ if (m_itemCache.contains(appId)) {
112− if (index < 0) {
128+ return m_itemCache.value(appId)->pinned;
113− return false;
114− } else {
115− return m_storedApps[index].pinned;
116 }
129 }
130+
131+ QString df = findDesktopFile(appId);
132+ if (!df.isEmpty()) {
133+ LauncherBackendItem *item = parseDesktopFile(df);
134+ m_itemCache.insert(appId, item);
135+ return item->pinned;
136+ }
137+
138+ return false;
117}
139}
118
140
119void LauncherBackend::setPinned(const QString &appId, bool pinned)
141void LauncherBackend::setPinned(const QString &appId, bool pinned)
120{
142{
121− auto index = findItem(appId);
122− if (index >= 0 && !m_storedApps[index].pinned) {
143+ if (!m_storedApps.contains(appId)) {
144+ qDebug() << "Cannot pin item. Unknown appId";
145+ return;
146+ }
147+
148+ LauncherBackendItem *item = m_itemCache.value(appId);
149+ if (item->pinned != pinned) {
123− m_storedApps[index].pinned = pinned;
150+ item->pinned = pinned;
124 syncToAccounts();
151 syncToAccounts();
125 }
152 }
126}
153}
@@ -169,75 +196,125 @@ void LauncherBackend::syncFromAccounts()
169 QList<QVariantMap> apps;
196 QList<QVariantMap> apps;
170 bool defaults = true;
197 bool defaults = true;
171
198
172− clearItems();
199+ m_storedApps.clear();
173
200
201+ qDebug() << "syncing from accounts" << m_user << m_accounts;
174 if (m_user != "" && m_accounts != nullptr) {
202 if (m_user != "" && m_accounts != nullptr) {
203+ qDebug() << "loading from user" << m_user << m_accounts;
175− auto variant = m_accounts->getUserProperty(m_user, "launcher-items");
204+ QVariant variant = m_accounts->getUserProperty(m_user, "launcher-items");
205+ qDebug() << "loaded launcher-items" << variant;
176 apps = qdbus_cast<QList<QVariantMap>>(variant.value<QDBusArgument>());
206 apps = qdbus_cast<QList<QVariantMap>>(variant.value<QDBusArgument>());
177 defaults = isDefaultsItem(apps);
207 defaults = isDefaultsItem(apps);
178 }
208 }
179
209
180− // TODO: load default pinned ones from default config, instead of hardcoding here...
181 if (defaults) {
210 if (defaults) {
182− apps <<
211+ QGSettings gSettings("com.canonical.Unity.Launcher", "/com/canonical/unity/launcher/");
183− makeAppDetails("dialer-app.desktop", true) <<
212+ qDebug() << "Got launcher gSettings" << gSettings.get("favorites");
184− makeAppDetails("messaging-app.desktop", true) <<
213+ Q_FOREACH(const QString &entry, gSettings.get("favorites").toStringList()) {
185− makeAppDetails("address-book-app.desktop", true) <<
214+ if (entry.startsWith("application://")) {
186− makeAppDetails("camera-app.desktop", true) <<
215+ QString appId = entry;
187− makeAppDetails("gallery-app.desktop", true) <<
216+ appId.remove("application://");
188− makeAppDetails("facebook-webapp.desktop", true) <<
217+ qDebug() << "searching for" << entry << appId;
189− makeAppDetails("webbrowser-app.desktop", true) <<
218+ QString df = findDesktopFile(appId);
190− makeAppDetails("twitter-webapp.desktop", true) <<
219+ qDebug() << "desktop file path" << df;
191− makeAppDetails("gmail-webapp.desktop", true) <<
220+
192− makeAppDetails("ubuntu-weather-app.desktop", true) <<
221+ if (!df.isEmpty()) {
193− makeAppDetails("notes-app.desktop", true) <<
222+ m_storedApps << appId;
194− makeAppDetails("calendar-app.desktop", true);
223+
224+ if (!m_itemCache.contains(appId)) {
225+ m_itemCache.insert(appId, parseDesktopFile(df));
226+ }
227+ m_itemCache.value(appId)->pinned = true;
228+ }
195− }
229+ }
196−
230+ }
231+ } else {
197− for (const QVariant &app: apps) {
232+ for (const QVariant &app: apps) {
198− loadApp(app.toMap());
233+ loadFromVariant(app.toMap());
234+ }
199 }
235 }
200}
236}
201
237
202void LauncherBackend::syncToAccounts()
238void LauncherBackend::syncToAccounts()
203{
239{
204− if (m_user != "" && m_accounts != nullptr) {
240+ qDebug() << "syncing to accounts";
241+ if (m_accounts && !m_user.isEmpty()) {
205 QList<QVariantMap> items;
242 QList<QVariantMap> items;
206
243
207− for (LauncherBackendItem &app: m_storedApps) {
244+ Q_FOREACH(const QString &appId, m_storedApps) {
208− items << makeAppDetails(app.settings->fileName(), app.pinned);
245+ items << itemToVariant(appId);
209 }
246 }
210
247
211 m_accounts->setUserProperty(m_user, "launcher-items", QVariant::fromValue(items));
248 m_accounts->setUserProperty(m_user, "launcher-items", QVariant::fromValue(items));
212 }
249 }
213}
250}
214
251
215−QSettings *LauncherBackend::parseDesktopFile(const QString &appId) const
252+QString LauncherBackend::findDesktopFile(const QString &appId) const
216{
253{
217− auto fullAppId = desktopFile(appId);
254+ int dashPos = -1;
218− return new QSettings(fullAppId, QSettings::IniFormat);
255+ QString helper = appId;
256+
257+ do {
258+ if (dashPos != -1) {
259+ helper = helper.replace(dashPos, 1, '/');
260+ }
261+
262+ QFileInfo fileInfo(QDir("/usr/share/applications"), helper);
263+ if (fileInfo.exists()) {
264+ return fileInfo.absoluteFilePath();
265+ }
266+
267+ dashPos = helper.indexOf("-");
268+ } while (dashPos != -1);
269+
270+ return QString();
219}
271}
220
272
221−void LauncherBackend::loadApp(const QVariantMap &details)
273+LauncherBackendItem* LauncherBackend::parseDesktopFile(const QString &desktopFile) const
222{
274{
223− auto appId = details.value("id").toString();
275+ QSettings settings(desktopFile, QSettings::IniFormat);
224− auto isPinned = details.value("is-pinned").toBool();
276+
225
277
226− if (appId.isEmpty()) {
278+ LauncherBackendItem* item = new LauncherBackendItem();
279+ item->desktopFile = desktopFile;
280+ item->displayName = settings.value("Desktop Entry/Name").toString();
281+ item->icon = settings.value("Desktop Entry/Icon").toString();
282+ item->pinned = false;
283+ qDebug() << "reading file" << desktopFile << settings.value("Desktop Entry/Name") << item->icon;
284+ return item;
285+}
286+
287+void LauncherBackend::loadFromVariant(const QVariantMap &details)
288+{
289+ if (!details.contains("id")) {
227 return;
290 return;
228 }
291 }
292+ QString appId = details.value("id").toString();
293+
294+ if (m_itemCache.contains(appId)) {
295+ delete m_itemCache.value(appId);
296+ }
229
297
230− LauncherBackendItem item;
298+ LauncherBackendItem *item = new LauncherBackendItem();
231− item.settings = parseDesktopFile(appId);
299+
232− item.pinned = isPinned;
300+ item->desktopFile = details.value("desktopFile").toString();
301+ item->displayName = details.value("name").toString();
302+ item->icon = details.value("icon").toString();
303+ item->pinned = details.value("is-pinned").toBool();
304+
305+ m_itemCache.insert(appId, item);
233− m_storedApps.append(item);
306+ m_storedApps.append(appId);
234}
307}
235
308
236−QVariantMap LauncherBackend::makeAppDetails(const QString &appId, bool pinned) const
309+QVariantMap LauncherBackend::itemToVariant(const QString &appId) const
237{
310{
311+ LauncherBackendItem *item = m_itemCache.value(appId);
238 QVariantMap details;
312 QVariantMap details;
239 details.insert("id", appId);
313 details.insert("id", appId);
314+ details.insert("name", item->displayName);
315+ details.insert("icon", item->icon);
316+ details.insert("desktopFile", item->desktopFile);
240− details.insert("is-pinned", pinned);
317+ details.insert("is-pinned", item->pinned);
241 return details;
318 return details;
242}
319}
243
320
@@ -246,16 +323,11 @@ bool LauncherBackend::isDefaultsItem(const QList<QVariantMap> &apps) const
246 // To differentiate between an empty list and a list that hasn't been set
323 // To differentiate between an empty list and a list that hasn't been set
247 // yet (and should thus be populated with the defaults), we use a special
324 // yet (and should thus be populated with the defaults), we use a special
248 // list of one item with the 'defaults' field set to true.
325 // list of one item with the 'defaults' field set to true.
326+ qDebug() << apps.size();
327+ if (apps.size() == 1)
328+ qDebug() << apps[0].value("defaults").toBool();
329+
249 return (apps.size() == 1 && apps[0].value("defaults").toBool());
330 return (apps.size() == 1 && apps[0].value("defaults").toBool());
250}
331}
251
332
252−int LauncherBackend::findItem(const QString &appId) const
333+
253−{
254− auto fullAppId = desktopFile(appId);
255− for (int i = 0; i < m_storedApps.size(); ++i) {
256− if (m_storedApps[i].settings->fileName() == fullAppId) {
257− return i;
258− }
259− }
260− return -1;
261−}
plugins/Unity/Launcher/backend/launcherbackend.h+10−12
@@ -31,6 +31,8 @@ class AccountsService;
31 * @brief An interface that provides all the data needed by the launcher.
31 * @brief An interface that provides all the data needed by the launcher.
32 */
32 */
33
33
34+class LauncherBackendItem;
35+
34class LauncherBackend : public QObject
36class LauncherBackend : public QObject
35{
37{
36 Q_OBJECT
38 Q_OBJECT
@@ -141,23 +143,19 @@ Q_SIGNALS:
141 void countChanged(const QString &appId, int count);
143 void countChanged(const QString &appId, int count);
142
144
143private:
145private:
146+ QString findDesktopFile(const QString &appId) const;
144− QSettings *parseDesktopFile(const QString &appId) const;
147+ LauncherBackendItem* parseDesktopFile(const QString &desktopFile) const;
148+
145− QVariantMap makeAppDetails(const QString &appId, bool pinned) const;
149+ QVariantMap itemToVariant(const QString &appId) const;
146− void loadApp(const QVariantMap &details);
150+ void loadFromVariant(const QVariantMap &details);
147− int findItem(const QString &appId) const;
151+
148 bool isDefaultsItem(const QList<QVariantMap> &apps) const;
152 bool isDefaultsItem(const QList<QVariantMap> &apps) const;
149 void syncFromAccounts();
153 void syncFromAccounts();
150 void syncToAccounts();
154 void syncToAccounts();
151− void clearItems();
152
155
156+ QList<QString> m_storedApps;
153− class LauncherBackendItem
157+ mutable QHash<QString, LauncherBackendItem*> m_itemCache;
154− {
155− public:
156− QSettings *settings;
157− bool pinned;
158− };
159
158
160− QList<LauncherBackendItem> m_storedApps;
161 AccountsService *m_accounts;
159 AccountsService *m_accounts;
162 QString m_user;
160 QString m_user;
163};
161};
plugins/Unity/Launcher/launchermodel.cpp+4
@@ -21,6 +21,8 @@
21#include "launcheritem.h"
21#include "launcheritem.h"
22#include "backend/launcherbackend.h"
22#include "backend/launcherbackend.h"
23
23
24+#include <QDebug>
25+
24LauncherModel::LauncherModel(QObject *parent):
26LauncherModel::LauncherModel(QObject *parent):
25 LauncherModelInterface(parent),
27 LauncherModelInterface(parent),
26 m_backend(new LauncherBackend(true, this))
28 m_backend(new LauncherBackend(true, this))
@@ -34,6 +36,7 @@ LauncherModel::LauncherModel(QObject *parent):
34 m_backend->displayName(entry),
36 m_backend->displayName(entry),
35 m_backend->icon(entry),
37 m_backend->icon(entry),
36 this);
38 this);
39+ qDebug() << "**************" << "got icon" << m_backend->icon(entry);
37 if (m_backend->isPinned(entry)) {
40 if (m_backend->isPinned(entry)) {
38 item->setPinned(true);
41 item->setPinned(true);
39 } else {
42 } else {
@@ -41,6 +44,7 @@ LauncherModel::LauncherModel(QObject *parent):
41 }
44 }
42 m_list.append(item);
45 m_list.append(item);
43 }
46 }
47+ qDebug() << "ctor done";
44}
48}
45
49
46LauncherModel::~LauncherModel()
50LauncherModel::~LauncherModel()
tests/plugins/Unity/Launcher/CMakeLists.txt+4
@@ -1,8 +1,11 @@
1+pkg_check_modules(GSETTINGS_QT REQUIRED gsettings-qt)
2+
1include_directories(
3include_directories(
2 ${CMAKE_CURRENT_BINARY_DIR}
4 ${CMAKE_CURRENT_BINARY_DIR}
3 ${CMAKE_SOURCE_DIR}/plugins/AccountsService
5 ${CMAKE_SOURCE_DIR}/plugins/AccountsService
4 ${CMAKE_SOURCE_DIR}/plugins/Unity/Launcher
6 ${CMAKE_SOURCE_DIR}/plugins/Unity/Launcher
5 ${CMAKE_SOURCE_DIR}/plugins/Unity/Launcher/backend
7 ${CMAKE_SOURCE_DIR}/plugins/Unity/Launcher/backend
8+ ${GSETTINGS_QT_INCLUDE_DIRS}
6 )
9 )
7
10
8add_definitions(-DSM_BUSNAME=sessionBus)
11add_definitions(-DSM_BUSNAME=sessionBus)
@@ -19,4 +22,5 @@ add_executable(launcherbackendtestExec
19 ${CMAKE_SOURCE_DIR}/plugins/Unity/Launcher/backend/launcherbackend.cpp
22 ${CMAKE_SOURCE_DIR}/plugins/Unity/Launcher/backend/launcherbackend.cpp
20 ${CMAKE_SOURCE_DIR}/plugins/Unity/Launcher/common/quicklistentry.cpp
23 ${CMAKE_SOURCE_DIR}/plugins/Unity/Launcher/common/quicklistentry.cpp
21 )
24 )
25+target_link_libraries(launcherbackendtestExec ${GSETTINGS_QT_LDFLAGS})
22qt5_use_modules(launcherbackendtestExec Test Core DBus)
26qt5_use_modules(launcherbackendtestExec Test Core DBus)