Fix compiler warnings.
[lxde/lxappearance.git] / src / utils.c
1 /*
2 * utils.c
3 *
4 * Copyright 2010 PCMan <pcman.tw@gmail.com>
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
19 * MA 02110-1301, USA.
20 */
21
22 #include "utils.h"
23 #include "lxappearance.h"
24 #include <glib/gi18n.h>
25 #include <sys/types.h>
26 #include <signal.h>
27 #include <sys/wait.h>
28 #include <stdlib.h>
29 #include <glib/gstdio.h>
30
31 #include "icon-theme.h"
32
33 static void on_pid_exit(GPid pid, gint status, gpointer user_data)
34 {
35 GtkDialog* dlg = GTK_DIALOG(user_data);
36 gtk_dialog_response(dlg, GTK_RESPONSE_OK);
37 g_debug("pid exit");
38 }
39
40 static void on_progress_dlg_response(GtkDialog* dlg, int res, gpointer user_data)
41 {
42 if(res != GTK_RESPONSE_OK)
43 {
44 GPid* ppid = (GPid*)user_data;
45 int status;
46 kill(*ppid, SIGTERM);
47 waitpid(*ppid, &status, WNOHANG);
48 }
49 }
50
51 static gboolean on_progress_timeout(GtkProgressBar* progress)
52 {
53 gtk_progress_bar_pulse(progress);
54 return TRUE;
55 }
56
57 gboolean show_progress_for_pid(GtkWindow* parent, const char* title, const char* msg, GPid pid)
58 {
59 gint res;
60 GtkWidget* dlg = gtk_dialog_new_with_buttons(title, parent,
61 GTK_DIALOG_NO_SEPARATOR|GTK_DIALOG_MODAL,
62 GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL, NULL);
63 GtkWidget* progress = gtk_progress_bar_new();
64 GtkWidget* vbox = gtk_dialog_get_content_area(GTK_DIALOG(dlg));
65 GtkWidget* label = gtk_label_new(msg);
66
67 guint child_watch = g_child_watch_add(pid, on_pid_exit, dlg);
68 guint timeout = g_timeout_add(300, (GSourceFunc)on_progress_timeout, progress);
69
70 gtk_window_set_default_size(GTK_WINDOW(dlg), 240, -1);
71 gtk_box_set_spacing(GTK_BOX(vbox), 6);
72 gtk_widget_show(label);
73 gtk_box_pack_start(GTK_BOX(vbox), label, FALSE, TRUE, 0);
74 gtk_widget_show(progress);
75 gtk_box_pack_start(GTK_BOX(vbox), progress, FALSE, TRUE, 0);
76 gtk_progress_set_activity_mode(GTK_PROGRESS(progress), TRUE);
77 g_signal_connect(dlg, "response", G_CALLBACK(on_progress_dlg_response), &pid);
78
79 res = gtk_dialog_run(GTK_DIALOG(dlg));
80
81 g_source_remove(child_watch);
82 g_source_remove(timeout);
83 gtk_widget_destroy(dlg);
84
85 return (res == GTK_RESPONSE_OK);
86 }
87
88 static void insert_theme_to_models(IconTheme* theme)
89 {
90 int icon_theme_pos = 0;
91 int cursor_theme_pos = 0;
92 GSList* l;
93 GtkTreeIter it;
94
95 for(l = app.icon_themes; l; l=l->next)
96 {
97 IconTheme* theme2 = (IconTheme*)l->data;
98 if(l->data == theme)
99 break;
100 if(theme2->has_icon)
101 ++icon_theme_pos;
102 if(theme2->has_cursor)
103 ++cursor_theme_pos;
104 }
105 if(theme->has_icon)
106 gtk_list_store_insert_with_values(app.icon_theme_store, &it, icon_theme_pos, 0, theme->disp_name, 1, theme, -1);
107
108 if(theme->has_cursor)
109 gtk_list_store_insert_with_values(app.cursor_theme_store, &it, cursor_theme_pos, 0, theme->disp_name, 1, theme, -1);
110 }
111
112 static gboolean install_icon_theme_package(const char* package_path)
113 {
114 GPid pid = -1;
115 const char* user_icons_dir = icon_theme_dirs[0];
116 char* tmp_dir = g_build_filename(user_icons_dir, "tmp.XXXXXX", NULL);
117 char* argv[]= {
118 "tar",
119 NULL,
120 "-C",
121 tmp_dir,
122 "-xf",
123 (char*)package_path,
124 NULL
125 };
126
127 if(g_mkdir_with_parents(user_icons_dir, 0700) == -1)
128 return FALSE;
129
130 if(!mkdtemp(tmp_dir))
131 return FALSE;
132
133 if(g_str_has_suffix(package_path, ".tar.gz"))
134 argv[1] = "--gzip";
135 else if(g_str_has_suffix(package_path, ".tar.bz2"))
136 argv[1] = "--bzip2";
137 else /* the file format is not supported */
138 goto _out;
139
140 char* cmd = g_strjoinv(" ", argv);
141 g_debug("extract: %s", cmd);
142 g_free(cmd);
143
144 if(g_spawn_async(NULL, argv, NULL, G_SPAWN_SEARCH_PATH|G_SPAWN_DO_NOT_REAP_CHILD, NULL, NULL, &pid, NULL))
145 {
146 g_debug("pid = %d", pid);
147 /* show progress UI for this pid */
148 if(show_progress_for_pid(GTK_WINDOW(app.dlg), "Install themes", "Installing...", pid))
149 {
150 /* move files in tmp_dir to user_icons_dir */
151 GDir* dir;
152 GKeyFile* kf = g_key_file_new();
153
154 /* convert the themes in the dir to IconTheme structs and add them to app.icon_themes list */
155 load_icon_themes_from_dir(user_icons_dir, tmp_dir, kf);
156 g_key_file_free(kf);
157
158 /* now really move this themes to ~/.icons dir and also update the GUI */
159 dir = g_dir_open(tmp_dir, 0, NULL);
160 if(dir)
161 {
162 char* name;
163 while(name = (char*)g_dir_read_name(dir))
164 {
165 char* index_theme = g_build_filename(tmp_dir, name, "index.theme", NULL);
166 gboolean is_theme = g_file_test(index_theme, G_FILE_TEST_EXISTS);
167 g_free(index_theme);
168 if(is_theme)
169 {
170 char* theme_tmp = g_build_filename(tmp_dir, name, NULL);
171 char* theme_target = g_build_filename(user_icons_dir, name, NULL);
172 if(g_rename(theme_tmp, theme_target) == 0)
173 {
174 /* the theme is already installed to ~/.icons */
175 GSList* l= g_slist_find_custom(app.icon_themes, name, (GCompareFunc)icon_theme_cmp_name);
176 if(l)
177 {
178 IconTheme* theme = (IconTheme*)l->data;
179 g_debug("installed theme: %p, %s", theme, theme->name);
180 /* update UI */
181 insert_theme_to_models(theme);
182 }
183 }
184 else
185 {
186 /* errors happened */
187 }
188 g_free(theme_target);
189 g_free(theme_tmp);
190 }
191 }
192 g_dir_close(dir);
193
194 /* remove remaining files. FIXME: will this cause problems? */
195 name = g_strdup_printf("rm -rf '%s'", tmp_dir);
196 g_spawn_command_line_sync(name, NULL, NULL, NULL, NULL);
197 g_free(name);
198 }
199 }
200 }
201
202 _out:
203 g_free(tmp_dir);
204 return (pid != -1);
205 }
206
207 gboolean install_icon_theme(GtkWindow* parent)
208 {
209 GtkFileFilter* filter = gtk_file_filter_new();
210 char* file = NULL;
211 int res;
212 GtkWidget* fc = gtk_file_chooser_dialog_new( _("Select an icon theme"), NULL,
213 GTK_FILE_CHOOSER_ACTION_OPEN,
214 GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
215 GTK_STOCK_OPEN, GTK_RESPONSE_OK, NULL );
216 gtk_window_set_transient_for(GTK_WINDOW(fc), GTK_WINDOW(app.dlg));
217 gtk_file_filter_add_pattern( filter, "*.tar.gz" );
218 gtk_file_filter_add_pattern( filter, "*.tar.bz2" );
219 gtk_file_filter_set_name( filter, _("*.tar.gz, *.tar.bz2 (Icon Theme)") );
220
221 gtk_file_chooser_add_filter( GTK_FILE_CHOOSER(fc), filter );
222 gtk_file_chooser_set_filter( GTK_FILE_CHOOSER(fc), filter );
223
224 res = gtk_dialog_run( (GtkDialog*)fc );
225 file = gtk_file_chooser_get_filename( GTK_FILE_CHOOSER(fc) );
226 gtk_widget_destroy( fc );
227
228 if( res == GTK_RESPONSE_OK )
229 install_icon_theme_package(file);
230
231 g_free(file);
232 return TRUE;
233 }
234
235 gboolean remove_icon_theme(GtkWindow* parent, IconTheme* theme)
236 {
237 gboolean ret = TRUE;
238 char* dir = g_build_filename(theme->base_dir, theme->name, NULL);
239 char* tmp_dir = g_build_filename(theme->base_dir, "tmp.XXXXXX", NULL);
240 g_debug("tmp_dir = %s", tmp_dir);
241 /* move the theme to a tmp dir first. so we can make the
242 * removal atomic. */
243 if(mkdtemp(tmp_dir))
244 {
245 char* tmp_dest = g_build_filename(tmp_dir, theme->name, NULL);
246 if(g_rename(dir, tmp_dest) == 0)
247 {
248 char* argv[] = {
249 "rm",
250 "-rf",
251 tmp_dir,
252 NULL
253 };
254 GPid pid;
255 if(g_spawn_async(NULL, argv, NULL, G_SPAWN_SEARCH_PATH|G_SPAWN_DO_NOT_REAP_CHILD, NULL, NULL, &pid, NULL))
256 {
257 ret = show_progress_for_pid(GTK_WINDOW(app.dlg), "Remove icon theme", "Removing...", pid);
258 }
259 }
260 g_free(tmp_dest);
261 }
262 else
263 ret = FALSE;
264
265 g_free(dir);
266 return ret;
267 }