* read text domain from the .desktop file (if available)
[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 = "osso-applet-tasknavigator";
38
39 class GKeyFileWrapper {
40 public:
41   GKeyFileWrapper() {
42     myKeyFile = g_key_file_new();
43   }
44
45  ~GKeyFileWrapper() {
46     if (myKeyFile != NULL) {
47       g_key_file_free(myKeyFile);
48     }
49   }
50
51   bool load(const std::string& name) {
52     GError *error = NULL;
53     bool result = g_key_file_load_from_file(myKeyFile, name.c_str(), G_KEY_FILE_NONE, &error);
54
55     if (error != NULL) {
56       g_error_free(error);
57     }
58
59     return result;
60   }
61
62   std::string getString(const gchar *group, const gchar *itemName) {
63     gchar *tempo = g_key_file_get_string(myKeyFile, group, itemName, NULL);
64     std::string result;
65
66     if (tempo != NULL) {
67       result.assign(tempo);
68
69       g_free(tempo);
70     }
71
72     return result;
73   }
74
75   std::string getLocaleString(const gchar *group, const gchar *itemName) {
76     gchar *tempo = g_key_file_get_locale_string(myKeyFile, group, itemName, NULL, NULL);
77     std::string result;
78
79     if (tempo != NULL) {
80       result.assign(tempo);
81
82       g_free(tempo);
83     }
84
85     return result;
86   }
87
88 private:
89   GKeyFile *myKeyFile;
90 };
91
92 LauncherItem::LauncherItem(): myEnabled(false) {
93 }
94
95 LauncherItem::~LauncherItem() {
96 }
97
98 std::string LauncherItem::translateString(const std::string& what) const {
99   if (what.empty()) {
100     return what;
101   } else {
102     return dgettext(myTextDomain.empty() ? DEFAULT_TEXT_DOMAIN : myTextDomain.c_str(), what.c_str());
103   }
104 }
105
106 bool LauncherItem::load(const std::string& filename) {
107   GKeyFileWrapper key_file;
108
109   for (;;) {
110     myFileName = filename;
111
112     if (!key_file.load(filename)) {
113       break;
114     }
115
116     if (key_file.getString(DESKTOP_ENTRY_GROUP, DESKTOP_ENTRY_TYPE_FIELD) != "Application") {
117       break;
118     }
119
120     myName = key_file.getLocaleString(DESKTOP_ENTRY_GROUP, DESKTOP_ENTRY_NAME_FIELD);
121     myComment = key_file.getLocaleString(DESKTOP_ENTRY_GROUP, DESKTOP_ENTRY_COMMENT_FIELD);
122     myIcon = key_file.getString(DESKTOP_ENTRY_GROUP, DESKTOP_ENTRY_ICON_FIELD);
123     myService = key_file.getString(DESKTOP_ENTRY_GROUP, DESKTOP_ENTRY_SERVICE_FIELD);
124     myTextDomain = key_file.getString(DESKTOP_ENTRY_GROUP, DESKTOP_ENTRY_TEXT_DOMAIN);
125
126     break;
127   }
128
129   return (myEnabled = checkSanity());
130 }
131
132 GdkPixbuf *LauncherItem::getIcon(int icon_size) const {
133   GdkPixbuf *pixbuf = NULL;
134
135   if (!myIcon.empty()) {
136     if (ourTheme == NULL) {
137       ourTheme = gtk_icon_theme_get_default();
138     }
139
140     GError *error = NULL;
141
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   return pixbuf;
151 }
152
153 // vim:ts=2:sw=2:et