799ef5a98acdbdc5c15faaeac421bcf5819ad5cc
[cl-launcher] / src / cl-utils.c
1 /*
2  *  Camera Launcher for Maemo.
3  *  Copyright (C) 2009 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 <glib.h>
29 #include <glib/gi18n-lib.h>
30
31 #include <gtk/gtk.h>
32 #include <hildon/hildon.h>
33
34 #include "cl-utils.h"
35
36 DesktopFileInfo *
37 get_desktop_file_info (const gchar *desktop_file)
38 {
39         DesktopFileInfo *dfinfo = NULL;
40         GKeyFile *key_file;
41         gchar *filename = NULL;
42         GError *error;
43
44         filename = g_strdup_printf ("%s/%s", CL_LAUNCHER_DESKTOP_DATADIR, desktop_file);
45         if (filename) {
46                 key_file = g_key_file_new ();
47                 if (g_key_file_load_from_file (key_file, filename, 0, NULL)) {
48                         dfinfo = g_new0 (DesktopFileInfo, 1);
49
50                         /* get application icon */
51                         if (g_key_file_has_key (key_file, "Desktop Entry", "Icon", &error)) {
52                                 dfinfo->icon = g_key_file_get_string (key_file, "Desktop Entry", "Icon", &error);
53                         } else {
54                                 dfinfo->icon = NULL;
55                         }
56
57                         /* get application name */
58                         if (g_key_file_has_key (key_file, "Desktop Entry", "X-Text-Domain", &error)) {
59                                 textdomain (g_key_file_get_string (key_file, "Desktop Entry", "X-Text-Domain", &error));
60                                 dfinfo->name = gettext (g_key_file_get_string (key_file, "Desktop Entry", "Name", &error));
61                         } else if (g_key_file_has_key (key_file, "Desktop Entry", "Name", &error)) {
62                                 dfinfo->name = g_key_file_get_string (key_file, "Desktop Entry", "Name", &error);
63                         } else {
64                                 dfinfo->name = NULL;
65                         }
66                 }
67
68                         /* get application osso_service */
69                         if (g_key_file_has_key (key_file, "Desktop Entry", "X-Osso-Service", &error)) {
70                                 dfinfo->osso_service = g_key_file_get_string (key_file, "Desktop Entry", "X-Osso-Service", &error);
71                         } else {
72                                 dfinfo->osso_service = NULL;
73                         }
74
75                         /* get application exec */
76                         if (g_key_file_has_key (key_file, "Desktop Entry", "Exec", &error)) {
77                                 dfinfo->exec = g_key_file_get_string (key_file, "Desktop Entry", "Exec", &error);
78                         } else {
79                                 dfinfo->exec = NULL;
80                         }
81
82                 if (key_file)
83                         g_key_file_free (key_file);
84         }
85         return dfinfo;
86 }
87
88 gboolean
89 get_application_list (GtkListStore *list)
90 {
91         gboolean found = FALSE;
92         GtkIconTheme *icon_theme;
93         GtkTreeIter iter;
94         GDir *dir;
95
96         icon_theme = gtk_icon_theme_get_default ();
97
98         dir = g_dir_open (CL_LAUNCHER_DESKTOP_DATADIR, 0, NULL);
99         if (dir) {
100                 const gchar *filename;
101
102                 while ((filename = g_dir_read_name (dir)) != NULL) {
103                         if (g_str_has_suffix (filename, ".desktop")) {
104                                 DesktopFileInfo *dfinfo = NULL;
105                                 GtkIconInfo *icon_info;
106                                 GdkPixbuf *icon_pixbuf = NULL;
107
108                                 dfinfo = get_desktop_file_info (filename);
109                                 if (dfinfo) {
110                                         if (!dfinfo->icon) {
111                                                 dfinfo->icon = g_strdup ("tasklaunch_default_application");
112                                         }
113
114                                         icon_info = gtk_icon_theme_lookup_icon (icon_theme, dfinfo->icon, 48, GTK_ICON_LOOKUP_NO_SVG);
115                                         if (icon_info) {
116                                                 const gchar *fname = gtk_icon_info_get_filename (icon_info);
117                                                 icon_pixbuf = gdk_pixbuf_new_from_file_at_size (fname, 48, 48, 0);
118                                                 gtk_icon_info_free (icon_info);
119                                         }
120
121                                         /* fill application list */
122                                         gtk_list_store_append (list, &iter);
123                                         gtk_list_store_set (list, &iter,
124                                                             SELECTOR_COLUMN_ICON, icon_pixbuf,
125                                                             SELECTOR_COLUMN_NAME, dfinfo->name,
126                                                             SELECTOR_COLUMN_OSSO_SERVICE, dfinfo->osso_service,
127                                                             SELECTOR_COLUMN_EXEC, dfinfo->exec,
128                                                             -1);
129
130                                         if (found == FALSE)
131                                                 found = TRUE;
132
133                                         if (icon_pixbuf)
134                                                 g_object_unref (icon_pixbuf);
135
136                                         g_free (dfinfo);
137                                 }
138                         }
139                 }
140                 g_dir_close (dir);
141         }
142
143         return found;
144 }