* created a wrapper over LauncherItem (helps to embrace functionality that is necess...
[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 = 0;
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 inline std::string getStringWrapper(GKeyFile *keyFile, const gchar *group, const gchar *itemName) {
35   gchar *tempo = g_key_file_get_string(keyFile, group, itemName, 0);
36   std::string result;
37
38   if (tempo != 0) {
39     result.assign(tempo);
40
41     g_free(tempo);
42   }
43
44   return result;
45 }
46
47 inline std::string getLocaleStringWrapper(GKeyFile *keyFile, const gchar *group, const gchar *itemName) {
48   gchar *tempo = g_key_file_get_locale_string(keyFile, group, itemName, 0, 0);
49   std::string result;
50
51   if (tempo != 0) {
52     result.assign(tempo);
53
54     g_free(tempo);
55   }
56
57   return result;
58 }
59
60 LauncherItem::LauncherItem() {
61 }
62
63 LauncherItem::~LauncherItem() {
64 }
65
66 bool LauncherItem::load(const std::string& filename) {
67   GKeyFile *key_file = 0;
68   GError *error = 0;
69
70   for (;;) {
71     if ((key_file = g_key_file_new()) == 0) {
72       break;
73     }
74
75     if (!g_key_file_load_from_file(key_file, filename.c_str(), G_KEY_FILE_NONE, &error)) {
76       break;
77     }
78
79     if (getStringWrapper(key_file, DESKTOP_ENTRY_GROUP, DESKTOP_ENTRY_TYPE_FIELD) != "Application") {
80       break;
81     }
82
83     myName = getStringWrapper(key_file, DESKTOP_ENTRY_GROUP, DESKTOP_ENTRY_NAME_FIELD);
84     myComment = getLocaleStringWrapper(key_file, DESKTOP_ENTRY_GROUP, DESKTOP_ENTRY_COMMENT_FIELD);
85     myIcon = getStringWrapper(key_file, DESKTOP_ENTRY_GROUP, DESKTOP_ENTRY_ICON_FIELD);
86     myService = getStringWrapper(key_file, DESKTOP_ENTRY_GROUP, DESKTOP_ENTRY_SERVICE_FIELD);
87
88     break;
89   }
90
91   if (error != 0) {
92     g_error_free(error);
93     error = 0;
94   }
95
96   if (key_file != 0) {
97     g_key_file_free(key_file);
98   }
99
100   return !(myName.empty() || myIcon.empty() || myService.empty());
101 }
102
103 GdkPixbuf *LauncherItem::getIcon(int icon_size) const {
104   GdkPixbuf *pixbuf = 0;
105
106   if (!myIcon.empty()) {
107     if (ourTheme == 0) {
108       ourTheme = gtk_icon_theme_get_default();
109     }
110
111     GError *error = 0;
112
113     pixbuf = gtk_icon_theme_load_icon(ourTheme, myIcon.c_str(), icon_size, GTK_ICON_LOOKUP_NO_SVG, &error);
114
115     if (error != 0) {
116       g_error_free(error);
117       error = NULL;
118     }
119   }
120
121   return pixbuf;
122 }
123
124 // vim:ts=2:sw=2:et