[svn-buildpackage] Tagging simple-launcher (0.9.2)
[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_SERVICE_FIELD = "X-Osso-Service",
35                   *DESKTOP_ENTRY_TEXT_DOMAIN = "X-Text-Domain";
36
37 static const char *DEFAULT_TEXT_DOMAIN = "maemo-af-desktop";
38 static const char *DEFAULT_APP_ICON = "qgn_list_gene_default_app";
39
40 class GKeyFileWrapper {
41 public:
42   GKeyFileWrapper() {
43     myKeyFile = g_key_file_new();
44   }
45
46  ~GKeyFileWrapper() {
47     if (myKeyFile != NULL) {
48       g_key_file_free(myKeyFile);
49     }
50   }
51
52   bool load(const std::string& name) {
53     GError *error = NULL;
54     bool result = g_key_file_load_from_file(myKeyFile, name.c_str(), G_KEY_FILE_NONE, &error);
55
56     if (error != NULL) {
57       g_error_free(error);
58     }
59
60     return result;
61   }
62
63   std::string getString(const gchar *group, const gchar *itemName) {
64     gchar *tempo = g_key_file_get_string(myKeyFile, group, itemName, NULL);
65     std::string result;
66
67     if (tempo != NULL) {
68       result.assign(tempo);
69
70       g_free(tempo);
71     }
72
73     return result;
74   }
75
76   std::string getLocaleString(const gchar *group, const gchar *itemName) {
77     gchar *tempo = g_key_file_get_locale_string(myKeyFile, group, itemName, NULL, NULL);
78     std::string result;
79
80     if (tempo != NULL) {
81       result.assign(tempo);
82
83       g_free(tempo);
84     }
85
86     return result;
87   }
88
89 private:
90   GKeyFile *myKeyFile;
91 };
92
93 LauncherItem::LauncherItem(): myEnabled(false) {
94 }
95
96 LauncherItem::~LauncherItem() {
97 }
98
99 std::string LauncherItem::translateString(const std::string& what) const {
100   if (what.empty()) {
101     return what;
102   } else {
103     return dgettext(myTextDomain.empty() ? DEFAULT_TEXT_DOMAIN : myTextDomain.c_str(), what.c_str());
104   }
105 }
106
107 bool LauncherItem::load(const std::string& filename) {
108   GKeyFileWrapper key_file;
109
110   for (;;) {
111     myFileName = filename;
112
113     if (!key_file.load(filename)) {
114       break;
115     }
116
117     if (key_file.getString(DESKTOP_ENTRY_GROUP, DESKTOP_ENTRY_TYPE_FIELD) != "Application") {
118       break;
119     }
120
121     myName = key_file.getLocaleString(DESKTOP_ENTRY_GROUP, DESKTOP_ENTRY_NAME_FIELD);
122     myComment = key_file.getLocaleString(DESKTOP_ENTRY_GROUP, DESKTOP_ENTRY_COMMENT_FIELD);
123     myIcon = key_file.getString(DESKTOP_ENTRY_GROUP, DESKTOP_ENTRY_ICON_FIELD);
124     myService = key_file.getString(DESKTOP_ENTRY_GROUP, DESKTOP_ENTRY_SERVICE_FIELD);
125     myTextDomain = key_file.getString(DESKTOP_ENTRY_GROUP, DESKTOP_ENTRY_TEXT_DOMAIN);
126
127     break;
128   }
129
130   return (myEnabled = checkSanity());
131 }
132
133 GdkPixbuf *LauncherItem::getIcon(int icon_size) const {
134   if (ourTheme == NULL) {
135     ourTheme = gtk_icon_theme_get_default();
136   }
137
138   GdkPixbuf *pixbuf = NULL;
139   GError *error = NULL;
140
141   if (!myIcon.empty()) {
142     pixbuf = gtk_icon_theme_load_icon(ourTheme, myIcon.c_str(), icon_size, GTK_ICON_LOOKUP_NO_SVG, &error);
143
144     if (error != NULL) {
145       g_error_free(error);
146       error = NULL;
147     }
148   }
149
150   if (pixbuf == NULL) {
151     pixbuf = gtk_icon_theme_load_icon(ourTheme, DEFAULT_APP_ICON, icon_size, GTK_ICON_LOOKUP_NO_SVG, &error);
152
153     if (error != NULL) {
154       g_error_free(error);
155       error = NULL;
156     }
157   }
158
159   return pixbuf;
160 }
161
162 // vim:ts=2:sw=2:et