on eb180d1 help sign in
unity8/plugins/Unity/Launcher/desktopfilehandler.cpp
152 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>
27#include <QDebug>
29#include <libintl.h>
31DesktopFileHandler::DesktopFileHandler(const QString &appId, QObject *parent):
32 QObject(parent),
33 m_appId(appId)
35 load();
38QString DesktopFileHandler::appId() const
40 return m_appId;
43void DesktopFileHandler::setAppId(const QString &appId)
45 if (m_appId != appId) {
46 m_appId = appId;
47 load();
48 }
51QString DesktopFileHandler::filename() const
53 return m_filename;
56bool DesktopFileHandler::isValid() const
58 return !m_filename.isEmpty();
61void DesktopFileHandler::load()
63 m_filename.clear();
65 if (m_appId.isEmpty()) {
66 return;
67 }
69 int dashPos = -1;
70 QString helper = m_appId;
72 QStringList searchDirs = QStandardPaths::standardLocations(QStandardPaths::ApplicationsLocation);
73#ifdef LAUNCHER_TESTING
74 searchDirs << ".";
75#endif
77 QString path;
78 do {
79 if (dashPos != -1) {
80 helper.replace(dashPos, 1, '/');
81 }
83 if (helper.contains("/")) {
84 path += helper.split('/').first() + '/';
85 helper.remove(QRegExp("^" + path));
86 }
88 Q_FOREACH(const QString &searchDirName, searchDirs) {
89 QDir searchDir(searchDirName + "/" + path);
90 Q_FOREACH(const QString &desktopFile, searchDir.entryList(QStringList() << "*.desktop")) {
91 if (desktopFile.startsWith(helper)) {
92 QFileInfo fileInfo(searchDir, desktopFile);
93 m_filename = fileInfo.absoluteFilePath();
94 return;
95 }
96 }
97 }
99 dashPos = helper.indexOf("-");
100 } while (dashPos != -1);
103QString DesktopFileHandler::displayName() const
105 if (!isValid()) {
106 return QString();
107 }
109 QSettings settings(m_filename, QSettings::IniFormat);
110 settings.beginGroup("Desktop Entry");
112 // First try to find Name[xx_YY] and Name[xx] in .desktop file
113 QString locale = QLocale::system().name();
114 QString shortLocale = locale.split('_').first();
116 if (locale != shortLocale && settings.contains(QString("Name[%1]").arg(locale))) {
117 return settings.value(QString("Name[%1]").arg(locale)).toString();
118 }
120 if (settings.contains(QString("Name[%1]").arg(shortLocale))) {
121 return settings.value(QString("Name[%1]").arg(shortLocale)).toString();
122 }
124 // No translation found in desktop file. Get the untranslated one and have a go with gettext.
125 QString displayName = settings.value("Name").toString();
127 if (settings.contains("X-Ubuntu-Gettext-Domain")) {
128 const QString domain = settings.value("X-Ubuntu-Gettext-Domain").toString();
129 return dgettext(domain.toUtf8().constData(), displayName.toUtf8().constData());
130 }
132 return displayName;
135QString DesktopFileHandler::icon() const
137 if (!isValid()) {
138 return QString();
139 }
141 QSettings settings(m_filename, QSettings::IniFormat);
142 settings.beginGroup("Desktop Entry");
143 QString iconString = settings.value("Icon").toString();
144 QString pathString = settings.value("Path").toString();
146 if (QFileInfo(iconString).exists()) {
147 return QFileInfo(iconString).absoluteFilePath();
148 } else if (QFileInfo(pathString + '/' + iconString).exists()) {
149 return pathString + '/' + iconString;
150 }
151 return "image://theme/" + iconString;