on bef70da help sign in
unity8/plugins/Unity/Launcher/desktopfilehandler.cpp
118 lines 3.8 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>
30QString DesktopFileHandler::findDesktopFile(const QString &appId)
32 int dashPos = -1;
33 QString helper = appId;
35 QStringList searchDirs = QStandardPaths::standardLocations(QStandardPaths::ApplicationsLocation);
36#ifdef LAUNCHER_TESTING
37 searchDirs << "";
38#endif
40 QString path;
41 do {
42 if (dashPos != -1) {
43 helper.replace(dashPos, 1, '/');
44 }
46 if (helper.contains("/")) {
47 path += helper.split('/').first() + '/';
48 helper.remove(QRegExp("^" + path));
49 }
51 Q_FOREACH(const QString &searchDirName, searchDirs) {
52 QDir searchDir(searchDirName + "/" + path);
53 Q_FOREACH(const QString &desktopFile, searchDir.entryList(QStringList() << "*.desktop")) {
54 if (desktopFile.startsWith(helper)) {
55 QFileInfo fileInfo(searchDir, desktopFile);
56 return fileInfo.absoluteFilePath();
57 }
58 }
59 }
61 dashPos = helper.indexOf("-");
62 } while (dashPos != -1);
64 return QString();
67QString DesktopFileHandler::displayName(const QString &appId)
69 QString desktopFile = findDesktopFile(appId);
70 if (desktopFile.isEmpty()) {
71 return QString();
72 }
74 QSettings settings(desktopFile, QSettings::IniFormat);
75 settings.beginGroup("Desktop Entry");
77 // First try to find Name[xx_YY] and Name[xx] in .desktop file
78 QString locale = QLocale::system().name();
79 QString shortLocale = locale.split('_').first();
81 if (locale != shortLocale && settings.contains(QString("Name[%1]").arg(locale))) {
82 return settings.value(QString("Name[%1]").arg(locale)).toString();
83 }
85 if (settings.contains(QString("Name[%1]").arg(shortLocale))) {
86 return settings.value(QString("Name[%1]").arg(shortLocale)).toString();
87 }
89 // No translation found in desktop file. Get the untranslated one and have a go with gettext.
90 QString displayName = settings.value("Name").toString();
92 if (settings.contains("X-Ubuntu-Gettext-Domain")) {
93 const QString domain = settings.value("X-Ubuntu-Gettext-Domain").toString();
94 return dgettext(domain.toUtf8().constData(), displayName.toUtf8().constData());
95 }
97 return displayName;
100QString DesktopFileHandler::icon(const QString &appId)
102 QString desktopFile = findDesktopFile(appId);
103 if (desktopFile.isEmpty()) {
104 return QString();
105 }
107 QSettings settings(desktopFile, QSettings::IniFormat);
108 settings.beginGroup("Desktop Entry");
109 QString iconString = settings.value("Icon").toString();
110 QString pathString = settings.value("Path").toString();
112 if (QFileInfo(iconString).exists()) {
113 return QFileInfo(iconString).absoluteFilePath();
114 } else if (QFileInfo(pathString + '/' + iconString).exists()) {
115 return pathString + '/' + iconString;
116 }
117 return "image://theme/" + iconString;