on xenial help sign in
Don't require that apps be stored to look up information about them
6c4f4d6Michael Terry
plugins/Unity/Launcher/backend/launcherbackend.cpp+26−34
@@ -65,7 +65,7 @@ void LauncherBackend::setStoredApplications(const QStringList &appIds)
65 if (m_storedApps[i].pinned) {
65 if (m_storedApps[i].pinned) {
66 auto found = false;
66 auto found = false;
67 for (int j = 0; j < appIds.size(); j++) {
67 for (int j = 0; j < appIds.size(); j++) {
68− auto fullAppId = resolveAppId(appIds[j]);
68+ auto fullAppId = desktopFile(appIds[j]);
69 if (m_storedApps[i].settings->fileName() == fullAppId) {
69 if (m_storedApps[i].settings->fileName() == fullAppId) {
70 found = true;
70 found = true;
71 break;
71 break;
@@ -81,7 +81,7 @@ void LauncherBackend::setStoredApplications(const QStringList &appIds)
81 clearItems();
81 clearItems();
82
82
83 for (auto appId: appIds) {
83 for (auto appId: appIds) {
84− loadDesktopFile(appId, pinnedItems.contains(resolveAppId(appId)));
84+ loadDesktopFile(appId, pinnedItems.contains(desktopFile(appId)));
85 }
85 }
86
86
87 if (needToSync) {
87 if (needToSync) {
@@ -91,39 +91,32 @@ void LauncherBackend::setStoredApplications(const QStringList &appIds)
91
91
92QString LauncherBackend::desktopFile(const QString &appId) const
92QString LauncherBackend::desktopFile(const QString &appId) const
93{
93{
94− auto index = findItem(appId);
94+ QFileInfo fileInfo(QDir("/usr/share/applications"), appId);
95− if (index < 0) {
95+ return fileInfo.absoluteFilePath();
96− return "";
97− } else {
98− return resolveAppId(appId);
99− }
100}
96}
101
97
102QString LauncherBackend::displayName(const QString &appId) const
98QString LauncherBackend::displayName(const QString &appId) const
103{
99{
104− auto index = findItem(appId);
100+ auto desktopFile = parseDesktopFile(appId);
105− if (index < 0) {
106− return "";
107− } else {
108− return m_storedApps[index].settings->value("Desktop Entry/Name").toString();
101+ auto displayName = desktopFile->value("Desktop Entry/Name").toString();
109− }
102+ delete desktopFile;
103+ return displayName;
110}
104}
111
105
112QString LauncherBackend::icon(const QString &appId) const
106QString LauncherBackend::icon(const QString &appId) const
113{
107{
114− auto index = findItem(appId);
108+ auto desktopFile = parseDesktopFile(appId);
115− if (index < 0) {
116− return "";
117− } else {
118− auto iconName = m_storedApps[index].settings->value("Desktop Entry/Icon").toString();
109+ auto iconName = desktopFile->value("Desktop Entry/Icon").toString();
110+ delete desktopFile;
111+
119− if (!iconName.isEmpty()) {
112+ if (!iconName.isEmpty()) {
120− QFileInfo iconFileInfo(iconName);
113+ QFileInfo iconFileInfo(iconName);
121− if (iconFileInfo.isRelative()) {
114+ if (iconFileInfo.isRelative()) {
122− iconName = "image://gicon/" + iconName;
115+ iconName = "image://gicon/" + iconName;
123− }
124 }
116 }
125− return iconName;
126 }
117 }
118+
119+ return iconName;
127}
120}
128
121
129bool LauncherBackend::isPinned(const QString &appId) const
122bool LauncherBackend::isPinned(const QString &appId) const
@@ -184,12 +177,6 @@ void LauncherBackend::triggerQuickListAction(const QString &appId, const QString
184 Q_UNUSED(quickListId)
177 Q_UNUSED(quickListId)
185}
178}
186
179
187−QString LauncherBackend::resolveAppId(const QString &appId) const
188−{
189− QFileInfo fileInfo(QDir("/usr/share/applications"), appId);
190− return fileInfo.absoluteFilePath();
191−}
192−
193void LauncherBackend::syncFromAccounts()
180void LauncherBackend::syncFromAccounts()
194{
181{
195 auto appIds = QStringList();
182 auto appIds = QStringList();
@@ -235,11 +222,16 @@ void LauncherBackend::syncToAccounts()
235 }
222 }
236}
223}
237
224
225+QSettings *LauncherBackend::parseDesktopFile(const QString &appId) const
226+{
227+ auto fullAppId = desktopFile(appId);
228+ return new QSettings(fullAppId, QSettings::IniFormat);
229+}
230+
238bool LauncherBackend::loadDesktopFile(const QString &appId, bool isPinned)
231bool LauncherBackend::loadDesktopFile(const QString &appId, bool isPinned)
239{
232{
240 auto item = LauncherBackendItem();
233 auto item = LauncherBackendItem();
241− auto fullAppId = resolveAppId(appId);
234+ item.settings = parseDesktopFile(appId);
242− item.settings = new QSettings(fullAppId, QSettings::IniFormat);
243 item.pinned = isPinned;
235 item.pinned = isPinned;
244 m_storedApps.append(item);
236 m_storedApps.append(item);
245 return true; // QSettings doesn't really indicate a failure mode right now
237 return true; // QSettings doesn't really indicate a failure mode right now
@@ -247,7 +239,7 @@ bool LauncherBackend::loadDesktopFile(const QString &appId, bool isPinned)
247
239
248int LauncherBackend::findItem(const QString &appId) const
240int LauncherBackend::findItem(const QString &appId) const
249{
241{
250− auto fullAppId = resolveAppId(appId);
242+ auto fullAppId = desktopFile(appId);
251 for (int i = 0; i < m_storedApps.size(); i++) {
243 for (int i = 0; i < m_storedApps.size(); i++) {
252 if (m_storedApps[i].settings->fileName() == fullAppId) {
244 if (m_storedApps[i].settings->fileName() == fullAppId) {
253 return i;
245 return i;
plugins/Unity/Launcher/backend/launcherbackend.h+10−1
@@ -59,12 +59,18 @@ public:
59
59
60 /**
60 /**
61 * @brief Get the full path to the .desktop file.
61 * @brief Get the full path to the .desktop file.
62+ *
63+ * The application does not need to be in the list of stored applications.
64+ *
62 * @returns The full path to the .dekstop file.
65 * @returns The full path to the .dekstop file.
63 */
66 */
64 QString desktopFile(const QString &appId) const;
67 QString desktopFile(const QString &appId) const;
65
68
66 /**
69 /**
67 * @brief Get the user friendly name of an application.
70 * @brief Get the user friendly name of an application.
71+ *
72+ * The application does not need to be in the list of stored applications.
73+ *
68 * @param appId The ID of the application.
74 * @param appId The ID of the application.
69 * @returns The user friendly name of the application.
75 * @returns The user friendly name of the application.
70 */
76 */
@@ -72,6 +78,9 @@ public:
72
78
73 /**
79 /**
74 * @brief Get the icon of an application.
80 * @brief Get the icon of an application.
81+ *
82+ * The application does not need to be in the list of stored applications.
83+ *
75 * @param appId The ID of the application.
84 * @param appId The ID of the application.
76 * @returns The full path to the icon for the application.
85 * @returns The full path to the icon for the application.
77 */
86 */
@@ -132,7 +141,7 @@ Q_SIGNALS:
132 void countChanged(const QString &appId, int count);
141 void countChanged(const QString &appId, int count);
133
142
134private:
143private:
135− QString resolveAppId(const QString &appId) const;
144+ QSettings *parseDesktopFile(const QString &appId) const;
136 bool loadDesktopFile(const QString &appId, bool isPinned);
145 bool loadDesktopFile(const QString &appId, bool isPinned);
137 int findItem(const QString &appId) const;
146 int findItem(const QString &appId) const;
138 void syncFromAccounts();
147 void syncFromAccounts();
tests/plugins/Unity/Launcher/launcherbackendtest.cpp+2−8
@@ -31,15 +31,12 @@ private Q_SLOTS:
31 {
31 {
32 LauncherBackend backend(false);
32 LauncherBackend backend(false);
33
33
34− auto apps = QStringList() << "one.desktop" << "two" << "/full";
35− backend.setStoredApplications(apps);
34+ backend.setStoredApplications(QStringList() << "relative.path" << "/full/path");
35+ QCOMPARE(backend.storedApplications(), QStringList() << "/usr/share/applications/relative.path" << "/full/path");
36
36
37 QCOMPARE(backend.desktopFile("one.desktop"), QString("/usr/share/applications/one.desktop"));
37 QCOMPARE(backend.desktopFile("one.desktop"), QString("/usr/share/applications/one.desktop"));
38 QCOMPARE(backend.desktopFile("two"), QString("/usr/share/applications/two"));
38 QCOMPARE(backend.desktopFile("two"), QString("/usr/share/applications/two"));
39 QCOMPARE(backend.desktopFile("/full"), QString("/full"));
39 QCOMPARE(backend.desktopFile("/full"), QString("/full"));
40− QCOMPARE(backend.storedApplications(), QStringList() << "/usr/share/applications/one.desktop" << "/usr/share/applications/two" << "/full");
41−
42− QCOMPARE(backend.desktopFile("full"), QString(""));
43
40
44 QCOMPARE(backend.desktopFile("/usr/share/applications/one.desktop"), QString("/usr/share/applications/one.desktop"));
41 QCOMPARE(backend.desktopFile("/usr/share/applications/one.desktop"), QString("/usr/share/applications/one.desktop"));
45 }
42 }
@@ -48,9 +45,6 @@ private Q_SLOTS:
48 {
45 {
49 LauncherBackend backend(false);
46 LauncherBackend backend(false);
50
47
51− auto apps = QStringList() << SRCDIR "/rel-icon.desktop" << SRCDIR "/abs-icon.desktop" << SRCDIR "/no-icon.desktop" << SRCDIR "/no-name.desktop" << SRCDIR "/no-exist.desktop";
52− backend.setStoredApplications(apps);
53−
54 QCOMPARE(backend.displayName(SRCDIR "/rel-icon.desktop"), QString("Relative Icon"));
48 QCOMPARE(backend.displayName(SRCDIR "/rel-icon.desktop"), QString("Relative Icon"));
55 QCOMPARE(backend.icon(SRCDIR "/rel-icon.desktop"), QString("image://gicon/rel-icon"));
49 QCOMPARE(backend.icon(SRCDIR "/rel-icon.desktop"), QString("image://gicon/rel-icon"));
56
50