on xenial help sign in
change DesktopFileHandler back into a stateful class but more polished
577a2edMichael Zanetti
plugins/Unity/Launcher/desktopfilehandler.cpp+46−13
@@ -27,10 +27,46 @@
27
27
28#include <libintl.h>
28#include <libintl.h>
29
29
30−QString DesktopFileHandler::findDesktopFile(const QString &appId)
30+DesktopFileHandler::DesktopFileHandler(const QString &appId, QObject *parent):
31+ QObject(parent),
32+ m_appId(appId)
31{
33{
34+ load();
35+}
36+
37+QString DesktopFileHandler::appId() const
38+{
39+ return m_appId;
40+}
41+
42+void DesktopFileHandler::setAppId(const QString &appId)
43+{
44+ if (m_appId != appId) {
45+ m_appId = appId;
46+ load();
47+ }
48+}
49+
50+QString DesktopFileHandler::filename() const
51+{
52+ return m_filename;
53+}
54+
55+bool DesktopFileHandler::isValid() const
56+{
57+ return !m_filename.isEmpty();
58+}
59+
60+void DesktopFileHandler::load()
61+{
62+ m_filename.clear();
63+
64+ if (m_appId.isEmpty()) {
65+ return;
66+ }
67+
32 int dashPos = -1;
68 int dashPos = -1;
33− QString helper = appId;
69+ QString helper = m_appId;
34
70
35 QStringList searchDirs = QStandardPaths::standardLocations(QStandardPaths::ApplicationsLocation);
71 QStringList searchDirs = QStandardPaths::standardLocations(QStandardPaths::ApplicationsLocation);
36#ifdef LAUNCHER_TESTING
72#ifdef LAUNCHER_TESTING
@@ -53,25 +89,23 @@ QString DesktopFileHandler::findDesktopFile(const QString &appId)
53 Q_FOREACH(const QString &desktopFile, searchDir.entryList(QStringList() << "*.desktop")) {
89 Q_FOREACH(const QString &desktopFile, searchDir.entryList(QStringList() << "*.desktop")) {
54 if (desktopFile.startsWith(helper)) {
90 if (desktopFile.startsWith(helper)) {
55 QFileInfo fileInfo(searchDir, desktopFile);
91 QFileInfo fileInfo(searchDir, desktopFile);
56− return fileInfo.absoluteFilePath();
92+ m_filename = fileInfo.absoluteFilePath();
93+ return;
57 }
94 }
58 }
95 }
59 }
96 }
60
97
61 dashPos = helper.indexOf("-");
98 dashPos = helper.indexOf("-");
62 } while (dashPos != -1);
99 } while (dashPos != -1);
63−
64− return QString();
65}
100}
66
101
67−QString DesktopFileHandler::displayName(const QString &appId)
102+QString DesktopFileHandler::displayName() const
68{
103{
69− QString desktopFile = findDesktopFile(appId);
104+ if (!isValid()) {
70− if (desktopFile.isEmpty()) {
71 return QString();
105 return QString();
72 }
106 }
73
107
74− QSettings settings(desktopFile, QSettings::IniFormat);
108+ QSettings settings(m_filename, QSettings::IniFormat);
75 settings.beginGroup("Desktop Entry");
109 settings.beginGroup("Desktop Entry");
76
110
77 // First try to find Name[xx_YY] and Name[xx] in .desktop file
111 // First try to find Name[xx_YY] and Name[xx] in .desktop file
@@ -97,14 +131,13 @@ QString DesktopFileHandler::displayName(const QString &appId)
97 return displayName;
131 return displayName;
98}
132}
99
133
100−QString DesktopFileHandler::icon(const QString &appId)
134+QString DesktopFileHandler::icon() const
101{
135{
102− QString desktopFile = findDesktopFile(appId);
136+ if (!isValid()) {
103− if (desktopFile.isEmpty()) {
104 return QString();
137 return QString();
105 }
138 }
106
139
107− QSettings settings(desktopFile, QSettings::IniFormat);
140+ QSettings settings(m_filename, QSettings::IniFormat);
108 settings.beginGroup("Desktop Entry");
141 settings.beginGroup("Desktop Entry");
109 QString iconString = settings.value("Icon").toString();
142 QString iconString = settings.value("Icon").toString();
110 QString pathString = settings.value("Path").toString();
143 QString pathString = settings.value("Path").toString();
plugins/Unity/Launcher/desktopfilehandler.h+29−6
@@ -20,14 +20,37 @@
20#ifndef DESKTOPFILEHANDLER_H
20#ifndef DESKTOPFILEHANDLER_H
21#define DESKTOPFILEHANDLER_H
21#define DESKTOPFILEHANDLER_H
22
22
23−#include <QString>
23+#include <QObject>
24
24
25+/**
26+ * When an object of this class is created or whenever setAppId(appId) is called,
27+ * this will search for a .desktop file matching the give appId. If a file is
28+ * found, isValid() will return true and the other methods return the contents
29+ * of the .desktop file.
30+ *
31+ * Note that this class will consider the user's locale and do a best effort
32+ * to return localized values.
33+ */
34+
25−namespace DesktopFileHandler
35+class DesktopFileHandler: public QObject
26{
36{
37+ Q_OBJECT
38+public:
39+ DesktopFileHandler(const QString &appId = QString(), QObject *parent = nullptr);
40+
41+ QString appId() const;
27− QString findDesktopFile(const QString &appId);
42+ void setAppId(const QString &appId);
43+
44+ bool isValid() const;
45+ QString filename() const;
46+ QString displayName() const;
47+ QString icon() const;
48+
49+private:
50+ void load();
28
51
29− QString displayName(const QString &appId);
52+ QString m_appId;
30− QString icon(const QString &appId);
53+ QString m_filename;
31−}
54+};
32
55
33#endif
56#endif
plugins/Unity/Launcher/launchermodel.cpp+16−12
@@ -40,14 +40,15 @@ LauncherModel::LauncherModel(QObject *parent):
40 connect(m_dbusIface, &DBusInterface::refreshCalled, this, &LauncherModel::refresh);
40 connect(m_dbusIface, &DBusInterface::refreshCalled, this, &LauncherModel::refresh);
41
41
42 Q_FOREACH (const QString &entry, m_settings->storedApplications()) {
42 Q_FOREACH (const QString &entry, m_settings->storedApplications()) {
43− if (DesktopFileHandler::findDesktopFile(entry).isEmpty()) {
43+ DesktopFileHandler desktopFile(entry);
44+ if (!desktopFile.isValid()) {
44 qWarning() << "Couldn't find a .desktop file for" << entry << ". Skipping...";
45 qWarning() << "Couldn't find a .desktop file for" << entry << ". Skipping...";
45 continue;
46 continue;
46 }
47 }
47
48
48 LauncherItem *item = new LauncherItem(entry,
49 LauncherItem *item = new LauncherItem(entry,
49− DesktopFileHandler::displayName(entry),
50+ desktopFile.displayName(),
50− DesktopFileHandler::icon(entry),
51+ desktopFile.icon(),
51 this);
52 this);
52 item->setPinned(true);
53 item->setPinned(true);
53 m_list.append(item);
54 m_list.append(item);
@@ -150,15 +151,16 @@ void LauncherModel::pin(const QString &appId, int index)
150 index = m_list.count();
151 index = m_list.count();
151 }
152 }
152
153
153− if (DesktopFileHandler::findDesktopFile(appId).isEmpty()) {
154+ DesktopFileHandler desktopFile(appId);
155+ if (!desktopFile.isValid()) {
154 qWarning() << "Can't pin this application, there is no .destkop file available.";
156 qWarning() << "Can't pin this application, there is no .destkop file available.";
155 return;
157 return;
156 }
158 }
157
159
158 beginInsertRows(QModelIndex(), index, index);
160 beginInsertRows(QModelIndex(), index, index);
159 LauncherItem *item = new LauncherItem(appId,
161 LauncherItem *item = new LauncherItem(appId,
160− DesktopFileHandler::displayName(appId),
162+ desktopFile.displayName(),
161− DesktopFileHandler::icon(appId),
163+ desktopFile.icon(),
162 this);
164 this);
163 item->setPinned(true);
165 item->setPinned(true);
164 m_list.insert(index, item);
166 m_list.insert(index, item);
@@ -342,10 +344,11 @@ void LauncherModel::countVisibleChanged(const QString &appId, int countVisible)
342 }
344 }
343 } else {
345 } else {
344 // Need to create a new LauncherItem and show the highlight
346 // Need to create a new LauncherItem and show the highlight
345− if (countVisible && !DesktopFileHandler::findDesktopFile(appId).isEmpty()) {
347+ DesktopFileHandler desktopFile(appId);
348+ if (countVisible && desktopFile.isValid()) {
346 LauncherItem *item = new LauncherItem(appId,
349 LauncherItem *item = new LauncherItem(appId,
347− DesktopFileHandler::displayName(appId),
350+ desktopFile.displayName(),
348− DesktopFileHandler::icon(appId));
351+ desktopFile.icon());
349 item->setCountVisible(true);
352 item->setCountVisible(true);
350 beginInsertRows(QModelIndex(), m_list.count(), m_list.count());
353 beginInsertRows(QModelIndex(), m_list.count(), m_list.count());
351 m_list.append(item);
354 m_list.append(item);
@@ -358,12 +361,13 @@ void LauncherModel::refresh()
358{
361{
359 QList<LauncherItem*> toBeRemoved;
362 QList<LauncherItem*> toBeRemoved;
360 Q_FOREACH (LauncherItem* item, m_list) {
363 Q_FOREACH (LauncherItem* item, m_list) {
361− if (DesktopFileHandler::findDesktopFile(item->appId()).isEmpty()) {
364+ DesktopFileHandler desktopFile(item->appId());
365+ if (!desktopFile.isValid()) {
362 toBeRemoved << item;
366 toBeRemoved << item;
363 } else {
367 } else {
364 int idx = m_list.indexOf(item);
368 int idx = m_list.indexOf(item);
365− item->setName(DesktopFileHandler::displayName(item->appId()));
369+ item->setName(desktopFile.displayName());
366− item->setIcon(DesktopFileHandler::icon(item->appId()));
370+ item->setIcon(desktopFile.icon());
367 Q_EMIT dataChanged(index(idx), index(idx), QVector<int>() << RoleName << RoleIcon);
371 Q_EMIT dataChanged(index(idx), index(idx), QVector<int>() << RoleName << RoleIcon);
368 }
372 }
369 }
373 }