Version bump (0.2-2)
[cl-launcher] / src / cl-utils.c
1 /*
2  *  Camera Launcher for Maemo.
3  *  Copyright (C) 2010 Roman Moravcik
4  *
5  *  This program is free software; you can redistribute it and/or modify
6  *  it under the terms of the GNU General Public License as published by
7  *  the Free Software Foundation; either version 2 of the License, or
8  *  (at your option) any later version.
9  *
10  *  This program is distributed in the hope that it will be useful,
11  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  *  GNU General Public License for more details.
14  *
15  *  You should have received a copy of the GNU General Public License
16  *  along with this program; if not, write to the Free Software
17  *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
18  */
19
20 #ifdef HAVE_CONFIG_H
21 #include <config.h>
22 #endif
23
24 #include <stdio.h>
25 #include <stdlib.h>
26 #include <string.h>
27
28 #include <gconf/gconf-client.h>
29
30 #include <glib.h>
31 #include <glib/gi18n-lib.h>
32
33 #include <gtk/gtk.h>
34 #include <hildon/hildon.h>
35
36 #include "cl-utils.h"
37
38 DesktopFileInfo *
39 get_desktop_file_info (const gchar *desktop_file)
40 {
41         DesktopFileInfo *dfinfo = NULL;
42         GKeyFile *key_file;
43
44         if (desktop_file) {
45                 key_file = g_key_file_new ();
46                 if (g_key_file_load_from_file (key_file, desktop_file, 0, NULL)) {
47                         gchar *type = NULL;
48
49                         /* exclude non application desktop files */
50                         if (g_key_file_has_key (key_file, "Desktop Entry", "Type", NULL)) {
51                                 type = g_key_file_get_string (key_file, "Desktop Entry", "Type", NULL);
52                                 if (type) {
53                                         if (strcmp (type, "Application")) {
54                                                 g_free (type);
55                                                 return NULL;
56                                         }
57                                         g_free (type);
58                                 }
59                         }
60
61                         dfinfo = g_new0 (DesktopFileInfo, 1);
62
63                         /* get application icon */
64                         if (g_key_file_has_key (key_file, "Desktop Entry", "Icon", NULL)) {
65                                 dfinfo->icon = g_key_file_get_string (key_file, "Desktop Entry", "Icon", NULL);
66                         } else {
67                                 dfinfo->icon = NULL;
68                         }
69
70                         /* get application name */
71                         if (g_key_file_has_key (key_file, "Desktop Entry", "X-Text-Domain", NULL)) {
72                                 textdomain (g_key_file_get_string (key_file, "Desktop Entry", "X-Text-Domain", NULL));
73                                 dfinfo->name = gettext (g_key_file_get_string (key_file, "Desktop Entry", "Name", NULL));
74                         } else if (g_key_file_has_key (key_file, "Desktop Entry", "Name", NULL)) {
75                                 textdomain ("maemo-af-desktop");
76                                 dfinfo->name = gettext (g_key_file_get_string (key_file, "Desktop Entry", "Name", NULL));
77                         } else {
78                                 dfinfo->name = NULL;
79                         }
80
81                         /* get application osso_service */
82                         if (g_key_file_has_key (key_file, "Desktop Entry", "X-Osso-Service", NULL)) {
83                                 dfinfo->osso_service = g_key_file_get_string (key_file, "Desktop Entry", "X-Osso-Service", NULL);
84                         } else {
85                                 dfinfo->osso_service = NULL;
86                         }
87
88                         /* get application exec */
89                         if (g_key_file_has_key (key_file, "Desktop Entry", "Exec", NULL)) {
90                                 dfinfo->exec = g_key_file_get_string (key_file, "Desktop Entry", "Exec", NULL);
91                         } else {
92                                 dfinfo->exec = NULL;
93                         }
94                 }
95
96                 if (key_file)
97                         g_key_file_free (key_file);
98         }
99         return dfinfo;
100 }
101
102 gboolean
103 get_application_list (GtkListStore *store)
104 {
105         gboolean is_empty = TRUE;
106         GtkIconTheme *icon_theme;
107         GtkTreeIter iter;
108         GDir *dir;
109
110         icon_theme = gtk_icon_theme_get_default ();
111
112         dir = g_dir_open (CL_LAUNCHER_DESKTOP_DATADIR, 0, NULL);
113         if (dir) {
114                 const gchar *filename;
115
116                 while ((filename = g_dir_read_name (dir)) != NULL) {
117                         if (g_str_has_suffix (filename, ".desktop")) {
118                                 DesktopFileInfo *dfinfo = NULL;
119                                 GtkIconInfo *icon_info;
120                                 GdkPixbuf *icon_pixbuf = NULL;
121                                 gchar *desktop_file = NULL;
122
123                                 desktop_file = g_strdup_printf ("%s/%s", CL_LAUNCHER_DESKTOP_DATADIR, filename);
124                                 if (desktop_file) {
125                                         dfinfo = get_desktop_file_info (desktop_file);
126                                         if (dfinfo) {
127                                                 if (!dfinfo->icon) {
128                                                         dfinfo->icon = g_strdup ("tasklaunch_default_application");
129                                                 }
130
131                                                 icon_info = gtk_icon_theme_lookup_icon (icon_theme, dfinfo->icon, 48,
132                                                                                         GTK_ICON_LOOKUP_NO_SVG);
133                                                 if (icon_info) {
134                                                         const gchar *fname = gtk_icon_info_get_filename (icon_info);
135                                                         icon_pixbuf = gdk_pixbuf_new_from_file_at_size (fname, 48, 48, 0);
136                                                         gtk_icon_info_free (icon_info);
137                                                 }
138
139                                                 /* fill application store */
140                                                 gtk_list_store_append (store, &iter);
141                                                 gtk_list_store_set (store, &iter,
142                                                                     SELECTOR_COLUMN_NAME, dfinfo->name,
143                                                                     SELECTOR_COLUMN_ICON, icon_pixbuf,
144                                                                     SELECTOR_COLUMN_FILENAME, filename,
145                                                                     SELECTOR_COLUMN_OSSO_SERVICE, dfinfo->osso_service,
146                                                                     SELECTOR_COLUMN_EXEC, dfinfo->exec,
147                                                                     -1);
148
149                                                 if (is_empty == TRUE)
150                                                         is_empty = FALSE;
151
152                                                 if (icon_pixbuf)
153                                                         g_object_unref (icon_pixbuf);
154
155                                                 g_free (dfinfo);
156                                         }
157                                         g_free (desktop_file);
158                                 }
159                         }
160                 }
161                 g_dir_close (dir);
162         }
163
164         return is_empty;
165 }
166
167 gboolean
168 get_application_list_from_list (GtkListStore *store, const GSList *list)
169 {
170         const GSList *application;
171         gboolean is_empty = TRUE;
172         GtkIconTheme *icon_theme;
173         GtkTreeIter iter;
174
175         icon_theme = gtk_icon_theme_get_default ();
176
177         for (application = list; application != NULL; application = application->next) {
178                 const gchar *filename;
179
180                 filename = gconf_value_get_string (application->data);
181
182                 if (g_str_has_suffix (filename, ".desktop")) {
183                         DesktopFileInfo *dfinfo = NULL;
184                         GtkIconInfo *icon_info;
185                         GdkPixbuf *icon_pixbuf = NULL;
186                         gchar *desktop_file = NULL;
187
188                         desktop_file = g_strdup_printf ("%s/%s", CL_LAUNCHER_DESKTOP_DATADIR, filename);
189                         if (desktop_file) {
190                                 dfinfo = get_desktop_file_info (desktop_file);
191                                 if (dfinfo) {
192                                         if (!dfinfo->icon) {
193                                                 dfinfo->icon = g_strdup ("tasklaunch_default_application");
194                                         }
195
196                                         icon_info = gtk_icon_theme_lookup_icon (icon_theme, dfinfo->icon, 48,
197                                                                                 GTK_ICON_LOOKUP_NO_SVG);
198                                         if (icon_info) {
199                                                 const gchar *fname = gtk_icon_info_get_filename (icon_info);
200                                                 icon_pixbuf = gdk_pixbuf_new_from_file_at_size (fname, 48, 48, 0);
201                                                 gtk_icon_info_free (icon_info);
202                                         }
203
204                                         /* fill application store */
205                                         gtk_list_store_append (store, &iter);
206                                         gtk_list_store_set (store, &iter,
207                                                             SELECTOR_COLUMN_NAME, dfinfo->name,
208                                                             SELECTOR_COLUMN_ICON, icon_pixbuf,
209                                                             SELECTOR_COLUMN_FILENAME, filename,
210                                                             SELECTOR_COLUMN_OSSO_SERVICE, dfinfo->osso_service,
211                                                             SELECTOR_COLUMN_EXEC, dfinfo->exec,
212                                                             -1);
213
214                                         if (is_empty == TRUE)
215                                                 is_empty = FALSE;
216
217                                         if (icon_pixbuf)
218                                                 g_object_unref (icon_pixbuf);
219
220                                         g_free (dfinfo);
221                                 }
222                                 g_free (desktop_file);
223                         }
224                 }
225         }
226
227         return is_empty;
228 }