on xenial help sign in
change DesktopFileHandler back into a stateful class but more polished
commit577a2ed9ee4a8c6e9b3c72bfe1e76de705b125cb authorMichael Zanetti <michael.zanetti@canonical.com> dateTue 2 Sep 2014, 19:45 (12 years ago) parent1f0b8d1
3 files changed+91−31
plugins/Unity/Launcher/desktopfilehandler.cpp+46−13 plugins/Unity/Launcher/desktopfilehandler.h+29−6 plugins/Unity/Launcher/launchermodel.cpp+16−12
plugins/Unity/Launcher/desktopfilehandler.cpp+46−13
@@ -27,10 +27,46 @@
2727
2828#include <libintl.h>
2929
30−QString DesktopFileHandler::findDesktopFile(const QString &appId)
30+DesktopFileHandler::DesktopFileHandler(const QString &appId, QObject *parent):
31+ QObject(parent),
32+ m_appId(appId)
3133{
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+
3268 int dashPos = -1;
33− QString helper = appId;
69+ QString helper = m_appId;
3470
3571 QStringList searchDirs = QStandardPaths::standardLocations(QStandardPaths::ApplicationsLocation);
3672#ifdef LAUNCHER_TESTING
@@ -53,25 +89,23 @@ QString DesktopFileHandler::findDesktopFile(const QString &appId)
5389 Q_FOREACH(const QString &desktopFile, searchDir.entryList(QStringList() << "*.desktop")) {
5490 if (desktopFile.startsWith(helper)) {
5591 QFileInfo fileInfo(searchDir, desktopFile);
56− return fileInfo.absoluteFilePath();
92+ m_filename = fileInfo.absoluteFilePath();
93+ return;
5794 }
5895 }
5996 }
6097
6198 dashPos = helper.indexOf("-");
6299 } while (dashPos != -1);
63−
64− return QString();
65100}
66101
67−QString DesktopFileHandler::displayName(const QString &appId)
102+QString DesktopFileHandler::displayName() const
68103{
69− QString desktopFile = findDesktopFile(appId);
70− if (desktopFile.isEmpty()) {
104+ if (!isValid()) {
71105 return QString();
72106 }
73107
74− QSettings settings(desktopFile, QSettings::IniFormat);
108+ QSettings settings(m_filename, QSettings::IniFormat);
75109 settings.beginGroup("Desktop Entry");
76110
77111 // First try to find Name[xx_YY] and Name[xx] in .desktop file
@@ -97,14 +131,13 @@ QString DesktopFileHandler::displayName(const QString &appId)
97131 return displayName;
98132}
99133
100−QString DesktopFileHandler::icon(const QString &appId)
134+QString DesktopFileHandler::icon() const
101135{
102− QString desktopFile = findDesktopFile(appId);
103− if (desktopFile.isEmpty()) {
136+ if (!isValid()) {
104137 return QString();
105138 }
106139
107− QSettings settings(desktopFile, QSettings::IniFormat);
140+ QSettings settings(m_filename, QSettings::IniFormat);
108141 settings.beginGroup("Desktop Entry");
109142 QString iconString = settings.value("Icon").toString();
110143 QString pathString = settings.value("Path").toString();
plugins/Unity/Launcher/desktopfilehandler.h+29−6
@@ -20,14 +20,37 @@
2020#ifndef DESKTOPFILEHANDLER_H
2121#define DESKTOPFILEHANDLER_H
2222
23−#include <QString>
23+#include <QObject>
2424
25−namespace DesktopFileHandler
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+
35+class DesktopFileHandler: public QObject
2636{
27− QString findDesktopFile(const QString &appId);
37+ Q_OBJECT
38+public:
39+ DesktopFileHandler(const QString &appId = QString(), QObject *parent = nullptr);
40+
41+ QString appId() const;
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();
2851
29− QString displayName(const QString &appId);
30− QString icon(const QString &appId);
31−}
52+ QString m_appId;
53+ QString m_filename;
54+};
3255
3356#endif
plugins/Unity/Launcher/launchermodel.cpp+16−12
@@ -40,14 +40,15 @@ LauncherModel::LauncherModel(QObject *parent):
4040 connect(m_dbusIface, &DBusInterface::refreshCalled, this, &LauncherModel::refresh);
4141
4242 Q_FOREACH (const QString &entry, m_settings->storedApplications()) {
43− if (DesktopFileHandler::findDesktopFile(entry).isEmpty()) {
43+ DesktopFileHandler desktopFile(entry);
44+ if (!desktopFile.isValid()) {
4445 qWarning() << "Couldn't find a .desktop file for" << entry << ". Skipping...";
4546 continue;
4647 }
4748
4849 LauncherItem *item = new LauncherItem(entry,
49− DesktopFileHandler::displayName(entry),
50− DesktopFileHandler::icon(entry),
50+ desktopFile.displayName(),
51+ desktopFile.icon(),
5152 this);
5253 item->setPinned(true);
5354 m_list.append(item);
@@ -150,15 +151,16 @@ void LauncherModel::pin(const QString &appId, int index)
150151 index = m_list.count();
151152 }
152153
153− if (DesktopFileHandler::findDesktopFile(appId).isEmpty()) {
154+ DesktopFileHandler desktopFile(appId);
155+ if (!desktopFile.isValid()) {
154156 qWarning() << "Can't pin this application, there is no .destkop file available.";
155157 return;
156158 }
157159
158160 beginInsertRows(QModelIndex(), index, index);
159161 LauncherItem *item = new LauncherItem(appId,
160− DesktopFileHandler::displayName(appId),
161− DesktopFileHandler::icon(appId),
162+ desktopFile.displayName(),
163+ desktopFile.icon(),
162164 this);
163165 item->setPinned(true);
164166 m_list.insert(index, item);
@@ -342,10 +344,11 @@ void LauncherModel::countVisibleChanged(const QString &appId, int countVisible)
342344 }
343345 } else {
344346 // 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()) {
346349 LauncherItem *item = new LauncherItem(appId,
347− DesktopFileHandler::displayName(appId),
348− DesktopFileHandler::icon(appId));
350+ desktopFile.displayName(),
351+ desktopFile.icon());
349352 item->setCountVisible(true);
350353 beginInsertRows(QModelIndex(), m_list.count(), m_list.count());
351354 m_list.append(item);
@@ -358,12 +361,13 @@ void LauncherModel::refresh()
358361{
359362 QList<LauncherItem*> toBeRemoved;
360363 Q_FOREACH (LauncherItem* item, m_list) {
361− if (DesktopFileHandler::findDesktopFile(item->appId()).isEmpty()) {
364+ DesktopFileHandler desktopFile(item->appId());
365+ if (!desktopFile.isValid()) {
362366 toBeRemoved << item;
363367 } else {
364368 int idx = m_list.indexOf(item);
365− item->setName(DesktopFileHandler::displayName(item->appId()));
366− item->setIcon(DesktopFileHandler::icon(item->appId()));
369+ item->setName(desktopFile.displayName());
370+ item->setIcon(desktopFile.icon());
367371 Q_EMIT dataChanged(index(idx), index(idx), QVector<int>() << RoleName << RoleIcon);
368372 }
369373 }