do not remove test applications: they are removed in their Makefile
[simple-launcher] / launcher-item.cc
1 // This file is a part of Simple Launcher
2 //
3 // Copyright (C) 2006, 2007, Mikhail Sobolev
4 //
5 // Simple Launcher is free software; you can redistribute it and/or modify it
6 // under the terms of the GNU General Public License version 2 as published by
7 // the Free Software Foundation.
8 //
9 // This program is distributed in the hope that it will be useful, but WITHOUT
10 // ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 // FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
12 // more details.
13 //
14 // You should have received a copy of the GNU General Public License along with
15 // this program; if not, write to the Free Software Foundation, Inc., 51
16 // Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
17
18 #include <string>
19
20 #include <libintl.h>
21
22 #include <glib/gmem.h>
23 #include <glib/gkeyfile.h>
24
25 #include "launcher-item.h"
26
27 GtkIconTheme *LauncherItem::ourTheme = NULL;
28
29 static const char *DESKTOP_ENTRY_GROUP = "Desktop Entry",
30                   *DESKTOP_ENTRY_TYPE_FIELD = "Type",
31                   *DESKTOP_ENTRY_ICON_FIELD = "Icon",
32                   *DESKTOP_ENTRY_NAME_FIELD = "Name",
33                   *DESKTOP_ENTRY_COMMENT_FIELD = "Comment",
34                   *DESKTOP_ENTRY_EXEC_FIELD = "Exec",
35                   *DESKTOP_ENTRY_SERVICE_FIELD = "X-Osso-Service",
36                   *DESKTOP_ENTRY_TEXT_DOMAIN = "X-Text-Domain";
37
38 static const char *DEFAULT_TEXT_DOMAIN = "maemo-af-desktop";
39 static const char *DEFAULT_APP_ICON = "qgn_list_gene_default_app";
40
41 class GKeyFileWrapper {
42 public:
43   GKeyFileWrapper() {
44     myKeyFile = g_key_file_new();
45   }
46
47  ~GKeyFileWrapper() {
48     if (myKeyFile != NULL) {
49       g_key_file_free(myKeyFile);
50     }
51   }
52
53   bool load(const std::string& name) {
54     GError *error = NULL;
55     bool result = g_key_file_load_from_file(myKeyFile, name.c_str(), G_KEY_FILE_NONE, &error);
56
57     if (error != NULL) {
58       g_error_free(error);
59     }
60
61     return result;
62   }
63
64   std::string getString(const gchar *group, const gchar *itemName) {
65     gchar *tempo = g_key_file_get_string(myKeyFile, group, itemName, NULL);
66     std::string result;
67
68     if (tempo != NULL) {
69       result.assign(tempo);
70
71       g_free(tempo);
72     }
73
74     return result;
75   }
76
77   std::string getLocaleString(const gchar *group, const gchar *itemName) {
78     gchar *tempo = g_key_file_get_locale_string(myKeyFile, group, itemName, NULL, NULL);
79     std::string result;
80
81     if (tempo != NULL) {
82       result.assign(tempo);
83
84       g_free(tempo);
85     }
86
87     return result;
88   }
89
90 private:
91   GKeyFile *myKeyFile;
92 };
93
94 LauncherItem::LauncherItem(): myEnabled(false) {
95 }
96
97 LauncherItem::~LauncherItem() {
98 }
99
100 std::string LauncherItem::translateString(const std::string& what) const {
101   if (what.empty()) {
102     return what;
103   } else {
104     return dgettext(myTextDomain.empty() ? DEFAULT_TEXT_DOMAIN : myTextDomain.c_str(), what.c_str());
105   }
106 }
107
108 bool LauncherItem::load(const std::string& filename) {
109   GKeyFileWrapper key_file;
110
111   for (;;) {
112     myFileName = filename;
113
114     if (!key_file.load(filename)) {
115       break;
116     }
117
118     if (key_file.getString(DESKTOP_ENTRY_GROUP, DESKTOP_ENTRY_TYPE_FIELD) != "Application") {
119       break;
120     }
121
122     myName = key_file.getLocaleString(DESKTOP_ENTRY_GROUP, DESKTOP_ENTRY_NAME_FIELD);
123     myComment = key_file.getLocaleString(DESKTOP_ENTRY_GROUP, DESKTOP_ENTRY_COMMENT_FIELD);
124     myIcon = key_file.getString(DESKTOP_ENTRY_GROUP, DESKTOP_ENTRY_ICON_FIELD);
125     myService = key_file.getString(DESKTOP_ENTRY_GROUP, DESKTOP_ENTRY_SERVICE_FIELD);
126     myExec = key_file.getString(DESKTOP_ENTRY_GROUP, DESKTOP_ENTRY_EXEC_FIELD);
127     myTextDomain = key_file.getString(DESKTOP_ENTRY_GROUP, DESKTOP_ENTRY_TEXT_DOMAIN);
128
129     break;
130   }
131
132   return (myEnabled = checkSanity());
133 }
134
135 GdkPixbuf *LauncherItem::getIcon(int icon_size) const {
136   if (ourTheme == NULL) {
137     ourTheme = gtk_icon_theme_get_default();
138   }
139
140   GdkPixbuf *pixbuf = NULL;
141   GError *error = NULL;
142
143   if (!myIcon.empty()) {
144     pixbuf = gtk_icon_theme_load_icon(ourTheme, myIcon.c_str(), icon_size, GTK_ICON_LOOKUP_NO_SVG, &error);
145
146     if (error != NULL) {
147       g_error_free(error);
148       error = NULL;
149     }
150   }
151
152   if (pixbuf == NULL) {
153     pixbuf = gtk_icon_theme_load_icon(ourTheme, DEFAULT_APP_ICON, icon_size, GTK_ICON_LOOKUP_NO_SVG, &error);
154
155     if (error != NULL) {
156       g_error_free(error);
157       error = NULL;
158     }
159   }
160
161   if (pixbuf != NULL) {
162     GdkPixbuf *tempo = gdk_pixbuf_copy(pixbuf);
163
164     g_object_unref(pixbuf);
165
166     pixbuf = tempo;
167   }
168
169   return pixbuf;
170 }
171
172 // vim:ts=2:sw=2:et