Adding upstream version 0.6.2.
[debian/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 #if GTK_CHECK_VERSION(3, 0, 0)
62 GTK_DIALOG_MODAL,
63 #else
64 GTK_DIALOG_NO_SEPARATOR|GTK_DIALOG_MODAL,
65 #endif
66 GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL, NULL);
67 GtkWidget* progress = gtk_progress_bar_new();
68 GtkWidget* vbox = gtk_dialog_get_content_area(GTK_DIALOG(dlg));
69 GtkWidget* label = gtk_label_new(msg);
70
71 guint child_watch = g_child_watch_add(pid, on_pid_exit, dlg);
72 guint timeout = g_timeout_add(300, (GSourceFunc)on_progress_timeout, progress);
73
74 gtk_window_set_default_size(GTK_WINDOW(dlg), 240, -1);
75 gtk_box_set_spacing(GTK_BOX(vbox), 6);
76 gtk_widget_show(label);
77 gtk_box_pack_start(GTK_BOX(vbox), label, FALSE, TRUE, 0);
78 gtk_widget_show(progress);
79 gtk_box_pack_start(GTK_BOX(vbox), progress, FALSE, TRUE, 0);
80 gtk_progress_bar_pulse(GTK_PROGRESS_BAR(progress));
81 g_signal_connect(dlg, "response", G_CALLBACK(on_progress_dlg_response), &pid);
82
83 res = gtk_dialog_run(GTK_DIALOG(dlg));
84
85 g_source_remove(child_watch);
86 g_source_remove(timeout);
87 gtk_widget_destroy(dlg);
88
89 return (res == GTK_RESPONSE_OK);
90 }
91
92 static void insert_theme_to_models(IconTheme* theme)
93 {
94 int icon_theme_pos = 0;
95 int cursor_theme_pos = 0;
96 GSList* l;
97 GtkTreeIter it;
98
99 for(l = app.icon_themes; l; l=l->next)
100 {
101 IconTheme* theme2 = (IconTheme*)l->data;
102 if(l->data == theme)
103 break;
104 if(theme2->has_icon)
105 ++icon_theme_pos;
106 if(theme2->has_cursor)
107 ++cursor_theme_pos;
108 }
109 if(theme->has_icon)
110 gtk_list_store_insert_with_values(app.icon_theme_store, &it, icon_theme_pos, 0, theme->disp_name, 1, theme, -1);
111
112 if(theme->has_cursor)
113 gtk_list_store_insert_with_values(app.cursor_theme_store, &it, cursor_theme_pos, 0, theme->disp_name, 1, theme, -1);
114 }
115
116 static gboolean install_icon_theme_package(const char* package_path)
117 {
118 GPid pid = -1;
119 /* Use new XDG path $XDG_DATA_HOME/icons to install icons theme */
120 char* user_icons_dir = g_build_filename(g_get_user_data_dir(), "icons", NULL);
121 char* tmp_dir = g_build_filename(user_icons_dir, "tmp.XXXXXX", NULL);
122 const char* argv[]= {
123 "tar",
124 NULL,
125 "-C",
126 tmp_dir,
127 "-xf",
128 (char*)package_path,
129 NULL
130 };
131
132 if(g_mkdir_with_parents(user_icons_dir, 0700) == -1)
133 goto _failed;
134
135 if(!mkdtemp(tmp_dir))
136 {
137 _failed:
138 g_free(user_icons_dir);
139 g_free(tmp_dir);
140 return FALSE;
141 }
142
143 if(g_str_has_suffix(package_path, ".tar.gz"))
144 argv[1] = "--gzip";
145 else if(g_str_has_suffix(package_path, ".tar.bz2"))
146 argv[1] = "--bzip2";
147 else /* the file format is not supported */
148 goto _out;
149
150 char* cmd = g_strjoinv(" ", (char**)argv);
151 g_debug("extract: %s", cmd);
152 g_free(cmd);
153
154 if(g_spawn_async(NULL, (char**)argv, NULL, G_SPAWN_SEARCH_PATH|G_SPAWN_DO_NOT_REAP_CHILD, NULL, NULL, &pid, NULL))
155 {
156 g_debug("pid = %d", pid);
157 /* show progress UI for this pid */
158 if(show_progress_for_pid(GTK_WINDOW(app.dlg), "Install themes", "Installing...", pid))
159 {
160 /* move files in tmp_dir to user_icons_dir */
161 GDir* dir;
162 GKeyFile* kf = g_key_file_new();
163
164 /* convert the themes in the dir to IconTheme structs and add them to app.icon_themes list */
165 load_icon_themes_from_dir(user_icons_dir, tmp_dir, kf);
166 g_key_file_free(kf);
167
168 /* now really move this themes to $XDG_DATA_HOME/icons dir and also update the GUI */
169 dir = g_dir_open(tmp_dir, 0, NULL);
170 if(dir)
171 {
172 const char* name;
173 while((name = g_dir_read_name(dir)) != NULL)
174 {
175 char* index_theme = g_build_filename(tmp_dir, name, "index.theme", NULL);
176 gboolean is_theme = g_file_test(index_theme, G_FILE_TEST_EXISTS);
177 g_free(index_theme);
178 if(is_theme)
179 {
180 char* theme_tmp = g_build_filename(tmp_dir, name, NULL);
181 char* theme_target = g_build_filename(user_icons_dir, name, NULL);
182 if(g_rename(theme_tmp, theme_target) == 0)
183 {
184 /* the theme is now installed to $XDG_DATA_HOME/icons */
185 GSList* l= g_slist_find_custom(app.icon_themes, name, (GCompareFunc)icon_theme_cmp_name);
186 if(l)
187 {
188 IconTheme* theme = (IconTheme*)l->data;
189 g_debug("installed theme: %p, %s", theme, theme->name);
190 /* update UI */
191 insert_theme_to_models(theme);
192 }
193 }
194 else
195 {
196 /* errors happened */
197 }
198 g_free(theme_target);
199 g_free(theme_tmp);
200 }
201 }
202 g_dir_close(dir);
203
204 /* remove remaining files. FIXME: will this cause problems? */
205 cmd = g_strdup_printf("rm -rf '%s'", tmp_dir);
206 g_spawn_command_line_sync(cmd, NULL, NULL, NULL, NULL);
207 g_free(cmd);
208 }
209 }
210 }
211
212 _out:
213 g_free(user_icons_dir);
214 g_free(tmp_dir);
215 return (pid != -1);
216 }
217
218 gboolean install_icon_theme(GtkWindow* parent)
219 {
220 GtkFileFilter* filter = gtk_file_filter_new();
221 char* file = NULL;
222 int res;
223 GtkWidget* fc = gtk_file_chooser_dialog_new( _("Select an icon theme"), NULL,
224 GTK_FILE_CHOOSER_ACTION_OPEN,
225 GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
226 GTK_STOCK_OPEN, GTK_RESPONSE_OK, NULL );
227 gtk_window_set_transient_for(GTK_WINDOW(fc), GTK_WINDOW(app.dlg));
228 gtk_file_filter_add_pattern( filter, "*.tar.gz" );
229 gtk_file_filter_add_pattern( filter, "*.tar.bz2" );
230 gtk_file_filter_set_name( filter, _("*.tar.gz, *.tar.bz2 (Icon Theme)") );
231
232 gtk_file_chooser_add_filter( GTK_FILE_CHOOSER(fc), filter );
233 gtk_file_chooser_set_filter( GTK_FILE_CHOOSER(fc), filter );
234
235 res = gtk_dialog_run( (GtkDialog*)fc );
236 file = gtk_file_chooser_get_filename( GTK_FILE_CHOOSER(fc) );
237 gtk_widget_destroy( fc );
238
239 if( res == GTK_RESPONSE_OK )
240 install_icon_theme_package(file);
241
242 g_free(file);
243 return TRUE;
244 }
245
246 gboolean remove_icon_theme(GtkWindow* parent, IconTheme* theme)
247 {
248 gboolean ret = TRUE;
249 char* dir = g_build_filename(theme->base_dir, theme->name, NULL);
250 char* tmp_dir = g_build_filename(theme->base_dir, "tmp.XXXXXX", NULL);
251 g_debug("tmp_dir = %s", tmp_dir);
252 /* move the theme to a tmp dir first. so we can make the
253 * removal atomic. */
254 if(mkdtemp(tmp_dir))
255 {
256 char* tmp_dest = g_build_filename(tmp_dir, theme->name, NULL);
257 if(g_rename(dir, tmp_dest) == 0)
258 {
259 const char* argv[] = {
260 "rm",
261 "-rf",
262 tmp_dir,
263 NULL
264 };
265 GPid pid;
266 if(g_spawn_async(NULL, (char**)argv, NULL, G_SPAWN_SEARCH_PATH|G_SPAWN_DO_NOT_REAP_CHILD, NULL, NULL, &pid, NULL))
267 {
268 ret = show_progress_for_pid(GTK_WINDOW(app.dlg), "Remove icon theme", "Removing...", pid);
269 }
270 }
271 g_free(tmp_dest);
272 }
273 else
274 ret = FALSE;
275
276 g_free(dir);
277 return ret;
278 }