1 // This file is a part of Simple Launcher
3 // Copyright (C) 2006, 2007, Mikhail Sobolev
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.
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
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
23 #include <glib/gmem.h>
24 #include <glib/gkeyfile.h>
26 #include "launcher-item.h"
28 GtkIconTheme *LauncherItem::ourTheme = NULL;
30 static const char *DESKTOP_ENTRY_GROUP = "Desktop Entry",
31 *DESKTOP_ENTRY_TYPE_FIELD = "Type",
32 *DESKTOP_ENTRY_ICON_FIELD = "Icon",
33 *DESKTOP_ENTRY_NAME_FIELD = "Name",
34 *DESKTOP_ENTRY_COMMENT_FIELD = "Comment",
35 *DESKTOP_ENTRY_EXEC_FIELD = "Exec",
36 *DESKTOP_ENTRY_SERVICE_FIELD = "X-Osso-Service",
37 *DESKTOP_ENTRY_TEXT_DOMAIN = "X-Text-Domain";
39 static const char *DEFAULT_TEXT_DOMAIN = "maemo-af-desktop";
40 static const char *DEFAULT_APP_ICON = "qgn_list_gene_default_app";
42 class GKeyFileWrapper {
45 myKeyFile = g_key_file_new();
49 if (myKeyFile != NULL) {
50 g_key_file_free(myKeyFile);
54 bool load(const std::string& name) {
56 bool result = g_key_file_load_from_file(myKeyFile, name.c_str(), G_KEY_FILE_NONE, &error);
65 std::string getString(const gchar *group, const gchar *itemName) {
66 gchar *tempo = g_key_file_get_string(myKeyFile, group, itemName, NULL);
78 std::string getLocaleString(const gchar *group, const gchar *itemName) {
79 gchar *tempo = g_key_file_get_locale_string(myKeyFile, group, itemName, NULL, NULL);
95 LauncherItem::LauncherItem(): myEnabled(false) {
98 LauncherItem::~LauncherItem() {
101 std::string LauncherItem::translateString(const std::string& what) const {
105 return dgettext(myTextDomain.empty() ? DEFAULT_TEXT_DOMAIN : myTextDomain.c_str(), what.c_str());
109 bool LauncherItem::load(const std::string& filename) {
110 GKeyFileWrapper key_file;
113 myFileName = filename;
115 if (!key_file.load(filename)) {
119 if (key_file.getString(DESKTOP_ENTRY_GROUP, DESKTOP_ENTRY_TYPE_FIELD) != "Application") {
123 myName = key_file.getLocaleString(DESKTOP_ENTRY_GROUP, DESKTOP_ENTRY_NAME_FIELD);
124 myComment = key_file.getLocaleString(DESKTOP_ENTRY_GROUP, DESKTOP_ENTRY_COMMENT_FIELD);
125 myIcon = key_file.getString(DESKTOP_ENTRY_GROUP, DESKTOP_ENTRY_ICON_FIELD);
126 myService = key_file.getString(DESKTOP_ENTRY_GROUP, DESKTOP_ENTRY_SERVICE_FIELD);
127 myExec = key_file.getString(DESKTOP_ENTRY_GROUP, DESKTOP_ENTRY_EXEC_FIELD);
128 myTextDomain = key_file.getString(DESKTOP_ENTRY_GROUP, DESKTOP_ENTRY_TEXT_DOMAIN);
133 return (myEnabled = checkSanity());
136 // The function below is taken verbatim from exo library -- xfce supporting library
137 static GdkPixbuf *exo_gdk_pixbuf_scale_ratio(GdkPixbuf *source, gint dest_size) {
138 gdouble wratio, hratio;
139 gint source_width, source_height;
140 gint dest_width, dest_height;
142 source_width = gdk_pixbuf_get_width(source);
143 source_height = gdk_pixbuf_get_height(source);
145 wratio = (gdouble)source_width / (gdouble)dest_size;
146 hratio = (gdouble)source_height / (gdouble)dest_size;
148 if (hratio > wratio) {
149 dest_width = (gint)rint(source_width / hratio);
150 dest_height = dest_size;
152 dest_width = dest_size;
153 dest_height = (gint)rint(source_height / wratio);
156 return gdk_pixbuf_scale_simple(source, MAX(dest_width, 1), MAX(dest_height, 1), GDK_INTERP_BILINEAR);
159 GdkPixbuf *LauncherItem::getIcon(int icon_size) const {
160 if (ourTheme == NULL) {
161 ourTheme = gtk_icon_theme_get_default();
164 GdkPixbuf *pixbuf = NULL;
165 GError *error = NULL;
167 if (!myIcon.empty()) {
168 pixbuf = gtk_icon_theme_load_icon(ourTheme, myIcon.c_str(), icon_size, GTK_ICON_LOOKUP_NO_SVG, &error);
176 if (pixbuf == NULL) {
177 pixbuf = gtk_icon_theme_load_icon(ourTheme, DEFAULT_APP_ICON, icon_size, GTK_ICON_LOOKUP_NO_SVG, &error);
185 if (pixbuf != NULL) {
188 if (gdk_pixbuf_get_width(pixbuf) > icon_size || gdk_pixbuf_get_height(pixbuf) > icon_size) {
189 tempo = exo_gdk_pixbuf_scale_ratio(pixbuf, icon_size);
191 tempo = gdk_pixbuf_copy(pixbuf);
194 g_object_unref(pixbuf);