replaced 0 with NULL where appropriate: shut the compiler up
[simple-launcher] / launcher-item.cc
1 // This file is a part of Simple Launcher
2 //
3 // Copyright (C) 2006, 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 <glib/gmem.h>
21 #include <glib/gkeyfile.h>
22
23 #include "launcher-item.h"
24
25 GtkIconTheme *LauncherItem::ourTheme = NULL;
26
27 static const char *DESKTOP_ENTRY_GROUP = "Desktop Entry",
28                   *DESKTOP_ENTRY_TYPE_FIELD = "Type",
29                   *DESKTOP_ENTRY_ICON_FIELD = "Icon",
30                   *DESKTOP_ENTRY_NAME_FIELD = "Name",
31                   *DESKTOP_ENTRY_COMMENT_FIELD = "Comment",
32                   *DESKTOP_ENTRY_SERVICE_FIELD = "X-Osso-Service";
33
34 class GKeyFileWrapper {
35 public:
36   GKeyFileWrapper() {
37     myKeyFile = g_key_file_new();
38   }
39
40  ~GKeyFileWrapper() {
41     if (myKeyFile != NULL) {
42       g_key_file_free(myKeyFile);
43     }
44   }
45
46   bool load(const std::string& name) {
47     GError *error = NULL;
48     bool result = g_key_file_load_from_file(myKeyFile, name.c_str(), G_KEY_FILE_NONE, &error);
49
50     if (error != NULL) {
51       g_error_free(error);
52     }
53
54     return result;
55   }
56
57   std::string getString(const gchar *group, const gchar *itemName) {
58     gchar *tempo = g_key_file_get_string(myKeyFile, group, itemName, NULL);
59     std::string result;
60
61     if (tempo != NULL) {
62       result.assign(tempo);
63
64       g_free(tempo);
65     }
66
67     return result;
68   }
69
70   std::string getLocaleString(const gchar *group, const gchar *itemName) {
71     gchar *tempo = g_key_file_get_locale_string(myKeyFile, group, itemName, NULL, NULL);
72     std::string result;
73
74     if (tempo != NULL) {
75       result.assign(tempo);
76
77       g_free(tempo);
78     }
79
80     return result;
81   }
82
83 private:
84   GKeyFile *myKeyFile;
85 };
86
87 LauncherItem::LauncherItem(): myEnabled(false) {
88 }
89
90 LauncherItem::~LauncherItem() {
91 }
92
93 bool LauncherItem::load(const std::string& filename) {
94   GKeyFileWrapper key_file;
95
96   for (;;) {
97     if (!key_file.load(filename)) {
98       break;
99     }
100
101     if (key_file.getString(DESKTOP_ENTRY_GROUP, DESKTOP_ENTRY_TYPE_FIELD) != "Application") {
102       break;
103     }
104
105     myName = key_file.getLocaleString(DESKTOP_ENTRY_GROUP, DESKTOP_ENTRY_NAME_FIELD);
106     myComment = key_file.getLocaleString(DESKTOP_ENTRY_GROUP, DESKTOP_ENTRY_COMMENT_FIELD);
107     myIcon = key_file.getString(DESKTOP_ENTRY_GROUP, DESKTOP_ENTRY_ICON_FIELD);
108     myService = key_file.getString(DESKTOP_ENTRY_GROUP, DESKTOP_ENTRY_SERVICE_FIELD);
109
110     break;
111   }
112
113   return !(myName.empty() || myIcon.empty() || myService.empty());
114 }
115
116 GdkPixbuf *LauncherItem::getIcon(int icon_size) const {
117   GdkPixbuf *pixbuf = NULL;
118
119   if (!myIcon.empty()) {
120     if (ourTheme == NULL) {
121       ourTheme = gtk_icon_theme_get_default();
122     }
123
124     GError *error = NULL;
125
126     pixbuf = gtk_icon_theme_load_icon(ourTheme, myIcon.c_str(), icon_size, GTK_ICON_LOOKUP_NO_SVG, &error);
127
128     if (error != NULL) {
129       g_error_free(error);
130       error = NULL;
131     }
132   }
133
134   return pixbuf;
135 }
136
137 // vim:ts=2:sw=2:et