on dddfa87 help sign in
unity8/plugins/Unity/Launcher/desktopfilehandler.cpp
151 lines 4.2 kB history raw
1/*
2 * Copyright 2014 Canonical Ltd.
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU Lesser General Public License as published by
6 * the Free Software Foundation; version 3.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU Lesser General Public License for more details.
12 *
13 * You should have received a copy of the GNU Lesser General Public License
14 * along with this program. If not, see <http://www.gnu.org/licenses/>.
15 *
16 * Authors:
17 * Michael Zanetti <michael.zanetti@canonical.com>
18 */
20#include "desktopfilehandler.h"
22#include <QStringList>
23#include <QStandardPaths>
24#include <QDir>
25#include <QSettings>
26#include <QLocale>
28#include <libintl.h>
30DesktopFileHandler::DesktopFileHandler(const QString &appId, QObject *parent):
31 QObject(parent),
32 m_appId(appId)
34 load();
37QString DesktopFileHandler::appId() const
39 return m_appId;
42void DesktopFileHandler::setAppId(const QString &appId)
44 if (m_appId != appId) {
45 m_appId = appId;
46 load();
47 }
50QString DesktopFileHandler::filename() const
52 return m_filename;
55bool DesktopFileHandler::isValid() const
57 return !m_filename.isEmpty();
60void DesktopFileHandler::load()
62 m_filename.clear();
64 if (m_appId.isEmpty()) {
65 return;
66 }
68 int dashPos = -1;
69 QString helper = m_appId;
71 QStringList searchDirs = QStandardPaths::standardLocations(QStandardPaths::ApplicationsLocation);
72#ifdef LAUNCHER_TESTING
73 searchDirs << "";
74#endif
76 QString path;
77 do {
78 if (dashPos != -1) {
79 helper.replace(dashPos, 1, '/');
80 }
82 if (helper.contains("/")) {
83 path += helper.split('/').first() + '/';
84 helper.remove(QRegExp("^" + path));
85 }
87 Q_FOREACH(const QString &searchDirName, searchDirs) {
88 QDir searchDir(searchDirName + "/" + path);
89 Q_FOREACH(const QString &desktopFile, searchDir.entryList(QStringList() << "*.desktop")) {
90 if (desktopFile.startsWith(helper)) {
91 QFileInfo fileInfo(searchDir, desktopFile);
92 m_filename = fileInfo.absoluteFilePath();
93 return;
94 }
95 }
96 }
98 dashPos = helper.indexOf("-");
99 } while (dashPos != -1);
102QString DesktopFileHandler::displayName() const
104 if (!isValid()) {
105 return QString();
106 }
108 QSettings settings(m_filename, QSettings::IniFormat);
109 settings.beginGroup("Desktop Entry");
111 // First try to find Name[xx_YY] and Name[xx] in .desktop file
112 QString locale = QLocale::system().name();
113 QString shortLocale = locale.split('_').first();
115 if (locale != shortLocale && settings.contains(QString("Name[%1]").arg(locale))) {
116 return settings.value(QString("Name[%1]").arg(locale)).toString();
117 }
119 if (settings.contains(QString("Name[%1]").arg(shortLocale))) {
120 return settings.value(QString("Name[%1]").arg(shortLocale)).toString();
121 }
123 // No translation found in desktop file. Get the untranslated one and have a go with gettext.
124 QString displayName = settings.value("Name").toString();
126 if (settings.contains("X-Ubuntu-Gettext-Domain")) {
127 const QString domain = settings.value("X-Ubuntu-Gettext-Domain").toString();
128 return dgettext(domain.toUtf8().constData(), displayName.toUtf8().constData());
129 }
131 return displayName;
134QString DesktopFileHandler::icon() const
136 if (!isValid()) {
137 return QString();
138 }
140 QSettings settings(m_filename, QSettings::IniFormat);
141 settings.beginGroup("Desktop Entry");
142 QString iconString = settings.value("Icon").toString();
143 QString pathString = settings.value("Path").toString();
145 if (QFileInfo(iconString).exists()) {
146 return QFileInfo(iconString).absoluteFilePath();
147 } else if (QFileInfo(pathString + '/' + iconString).exists()) {
148 return pathString + '/' + iconString;
149 }
150 return "image://theme/" + iconString;