on xenial help sign in
Don't require that apps be stored to look up information about them
commit6c4f4d631895023ea33bfe4cbad201572708c172 authorMichael Terry <michael.terry@canonical.com> dateThu 22 Aug 2013, 17:56 (13 years ago) parent8db3b13
3 files changed+38−43
plugins/Unity/Launcher/backend/launcherbackend.cpp+26−34 plugins/Unity/Launcher/backend/launcherbackend.h+10−1 tests/plugins/Unity/Launcher/launcherbackendtest.cpp+2−8
plugins/Unity/Launcher/backend/launcherbackend.cpp+26−34
@@ -65,7 +65,7 @@ void LauncherBackend::setStoredApplications(const QStringList &appIds)
6565 if (m_storedApps[i].pinned) {
6666 auto found = false;
6767 for (int j = 0; j < appIds.size(); j++) {
68− auto fullAppId = resolveAppId(appIds[j]);
68+ auto fullAppId = desktopFile(appIds[j]);
6969 if (m_storedApps[i].settings->fileName() == fullAppId) {
7070 found = true;
7171 break;
@@ -81,7 +81,7 @@ void LauncherBackend::setStoredApplications(const QStringList &appIds)
8181 clearItems();
8282
8383 for (auto appId: appIds) {
84− loadDesktopFile(appId, pinnedItems.contains(resolveAppId(appId)));
84+ loadDesktopFile(appId, pinnedItems.contains(desktopFile(appId)));
8585 }
8686
8787 if (needToSync) {
@@ -91,39 +91,32 @@ void LauncherBackend::setStoredApplications(const QStringList &appIds)
9191
9292QString LauncherBackend::desktopFile(const QString &appId) const
9393{
94− auto index = findItem(appId);
95− if (index < 0) {
96− return "";
97− } else {
98− return resolveAppId(appId);
99− }
94+ QFileInfo fileInfo(QDir("/usr/share/applications"), appId);
95+ return fileInfo.absoluteFilePath();
10096}
10197
10298QString LauncherBackend::displayName(const QString &appId) const
10399{
104− auto index = findItem(appId);
105− if (index < 0) {
106− return "";
107− } else {
108− return m_storedApps[index].settings->value("Desktop Entry/Name").toString();
109− }
100+ auto desktopFile = parseDesktopFile(appId);
101+ auto displayName = desktopFile->value("Desktop Entry/Name").toString();
102+ delete desktopFile;
103+ return displayName;
110104}
111105
112106QString LauncherBackend::icon(const QString &appId) const
113107{
114− auto index = findItem(appId);
115− if (index < 0) {
116− return "";
117− } else {
118− auto iconName = m_storedApps[index].settings->value("Desktop Entry/Icon").toString();
119− if (!iconName.isEmpty()) {
120− QFileInfo iconFileInfo(iconName);
121− if (iconFileInfo.isRelative()) {
122− iconName = "image://gicon/" + iconName;
123− }
108+ auto desktopFile = parseDesktopFile(appId);
109+ auto iconName = desktopFile->value("Desktop Entry/Icon").toString();
110+ delete desktopFile;
111+
112+ if (!iconName.isEmpty()) {
113+ QFileInfo iconFileInfo(iconName);
114+ if (iconFileInfo.isRelative()) {
115+ iconName = "image://gicon/" + iconName;
124116 }
125− return iconName;
126117 }
118+
119+ return iconName;
127120}
128121
129122bool LauncherBackend::isPinned(const QString &appId) const
@@ -184,12 +177,6 @@ void LauncherBackend::triggerQuickListAction(const QString &appId, const QString
184177 Q_UNUSED(quickListId)
185178}
186179
187−QString LauncherBackend::resolveAppId(const QString &appId) const
188−{
189− QFileInfo fileInfo(QDir("/usr/share/applications"), appId);
190− return fileInfo.absoluteFilePath();
191−}
192−
193180void LauncherBackend::syncFromAccounts()
194181{
195182 auto appIds = QStringList();
@@ -235,11 +222,16 @@ void LauncherBackend::syncToAccounts()
235222 }
236223}
237224
225+QSettings *LauncherBackend::parseDesktopFile(const QString &appId) const
226+{
227+ auto fullAppId = desktopFile(appId);
228+ return new QSettings(fullAppId, QSettings::IniFormat);
229+}
230+
238231bool LauncherBackend::loadDesktopFile(const QString &appId, bool isPinned)
239232{
240233 auto item = LauncherBackendItem();
241− auto fullAppId = resolveAppId(appId);
242− item.settings = new QSettings(fullAppId, QSettings::IniFormat);
234+ item.settings = parseDesktopFile(appId);
243235 item.pinned = isPinned;
244236 m_storedApps.append(item);
245237 return true; // QSettings doesn't really indicate a failure mode right now
@@ -247,7 +239,7 @@ bool LauncherBackend::loadDesktopFile(const QString &appId, bool isPinned)
247239
248240int LauncherBackend::findItem(const QString &appId) const
249241{
250− auto fullAppId = resolveAppId(appId);
242+ auto fullAppId = desktopFile(appId);
251243 for (int i = 0; i < m_storedApps.size(); i++) {
252244 if (m_storedApps[i].settings->fileName() == fullAppId) {
253245 return i;
plugins/Unity/Launcher/backend/launcherbackend.h+10−1
@@ -59,12 +59,18 @@ public:
5959
6060 /**
6161 * @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+ *
6265 * @returns The full path to the .dekstop file.
6366 */
6467 QString desktopFile(const QString &appId) const;
6568
6669 /**
6770 * @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+ *
6874 * @param appId The ID of the application.
6975 * @returns The user friendly name of the application.
7076 */
@@ -72,6 +78,9 @@ public:
7278
7379 /**
7480 * @brief Get the icon of an application.
81+ *
82+ * The application does not need to be in the list of stored applications.
83+ *
7584 * @param appId The ID of the application.
7685 * @returns The full path to the icon for the application.
7786 */
@@ -132,7 +141,7 @@ Q_SIGNALS:
132141 void countChanged(const QString &appId, int count);
133142
134143private:
135− QString resolveAppId(const QString &appId) const;
144+ QSettings *parseDesktopFile(const QString &appId) const;
136145 bool loadDesktopFile(const QString &appId, bool isPinned);
137146 int findItem(const QString &appId) const;
138147 void syncFromAccounts();
tests/plugins/Unity/Launcher/launcherbackendtest.cpp+2−8
@@ -31,15 +31,12 @@ private Q_SLOTS:
3131 {
3232 LauncherBackend backend(false);
3333
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");
3636
3737 QCOMPARE(backend.desktopFile("one.desktop"), QString("/usr/share/applications/one.desktop"));
3838 QCOMPARE(backend.desktopFile("two"), QString("/usr/share/applications/two"));
3939 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(""));
4340
4441 QCOMPARE(backend.desktopFile("/usr/share/applications/one.desktop"), QString("/usr/share/applications/one.desktop"));
4542 }
@@ -48,9 +45,6 @@ private Q_SLOTS:
4845 {
4946 LauncherBackend backend(false);
5047
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−
5448 QCOMPARE(backend.displayName(SRCDIR "/rel-icon.desktop"), QString("Relative Icon"));
5549 QCOMPARE(backend.icon(SRCDIR "/rel-icon.desktop"), QString("image://gicon/rel-icon"));
5650