Releasing debian version 0.5.1-3.
[debian/lxappearance.git] / src / utils.c
CommitLineData
1469f737
DB
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
33static 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
40static 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
51static gboolean on_progress_timeout(GtkProgressBar* progress)
52{
53 gtk_progress_bar_pulse(progress);
54 return TRUE;
55}
56
57gboolean 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,
e7f9874d
DB
61#if GTK_CHECK_VERSION(3, 0, 0)
62 GTK_DIALOG_MODAL,
63#else
1469f737 64 GTK_DIALOG_NO_SEPARATOR|GTK_DIALOG_MODAL,
e7f9874d 65#endif
1469f737
DB
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);
e7f9874d 80 gtk_progress_bar_pulse(GTK_PROGRESS_BAR(progress));
1469f737
DB
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
92static 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
116static gboolean install_icon_theme_package(const char* package_path)
117{
118 GPid pid = -1;
119 const char* user_icons_dir = icon_theme_dirs[0];
120 char* tmp_dir = g_build_filename(user_icons_dir, "tmp.XXXXXX", NULL);
121 char* argv[]= {
122 "tar",
123 NULL,
124 "-C",
125 tmp_dir,
126 "-xf",
127 (char*)package_path,
128 NULL
129 };
130
131 if(g_mkdir_with_parents(user_icons_dir, 0700) == -1)
132 return FALSE;
133
134 if(!mkdtemp(tmp_dir))
135 return FALSE;
136
137 if(g_str_has_suffix(package_path, ".tar.gz"))
138 argv[1] = "--gzip";
139 else if(g_str_has_suffix(package_path, ".tar.bz2"))
140 argv[1] = "--bzip2";
141 else /* the file format is not supported */
142 goto _out;
143
144 char* cmd = g_strjoinv(" ", argv);
145 g_debug("extract: %s", cmd);
146 g_free(cmd);
147
148 if(g_spawn_async(NULL, argv, NULL, G_SPAWN_SEARCH_PATH|G_SPAWN_DO_NOT_REAP_CHILD, NULL, NULL, &pid, NULL))
149 {
150 g_debug("pid = %d", pid);
151 /* show progress UI for this pid */
152 if(show_progress_for_pid(GTK_WINDOW(app.dlg), "Install themes", "Installing...", pid))
153 {
154 /* move files in tmp_dir to user_icons_dir */
155 GDir* dir;
156 GKeyFile* kf = g_key_file_new();
157
158 /* convert the themes in the dir to IconTheme structs and add them to app.icon_themes list */
159 load_icon_themes_from_dir(user_icons_dir, tmp_dir, kf);
160 g_key_file_free(kf);
161
162 /* now really move this themes to ~/.icons dir and also update the GUI */
163 dir = g_dir_open(tmp_dir, 0, NULL);
164 if(dir)
165 {
166 char* name;
e7f9874d 167 while((name = (char*)g_dir_read_name(dir)) != NULL)
1469f737
DB
168 {
169 char* index_theme = g_build_filename(tmp_dir, name, "index.theme", NULL);
170 gboolean is_theme = g_file_test(index_theme, G_FILE_TEST_EXISTS);
171 g_free(index_theme);
172 if(is_theme)
173 {
174 char* theme_tmp = g_build_filename(tmp_dir, name, NULL);
175 char* theme_target = g_build_filename(user_icons_dir, name, NULL);
176 if(g_rename(theme_tmp, theme_target) == 0)
177 {
178 /* the theme is already installed to ~/.icons */
179 GSList* l= g_slist_find_custom(app.icon_themes, name, (GCompareFunc)icon_theme_cmp_name);
180 if(l)
181 {
182 IconTheme* theme = (IconTheme*)l->data;
183 g_debug("installed theme: %p, %s", theme, theme->name);
184 /* update UI */
185 insert_theme_to_models(theme);
186 }
187 }
188 else
189 {
190 /* errors happened */
191 }
192 g_free(theme_target);
193 g_free(theme_tmp);
194 }
e7f9874d 195 g_free(name);
1469f737
DB
196 }
197 g_dir_close(dir);
198
199 /* remove remaining files. FIXME: will this cause problems? */
200 name = g_strdup_printf("rm -rf '%s'", tmp_dir);
201 g_spawn_command_line_sync(name, NULL, NULL, NULL, NULL);
202 g_free(name);
203 }
204 }
205 }
206
207_out:
208 g_free(tmp_dir);
209 return (pid != -1);
210}
211
212gboolean install_icon_theme(GtkWindow* parent)
213{
214 GtkFileFilter* filter = gtk_file_filter_new();
215 char* file = NULL;
216 int res;
217 GtkWidget* fc = gtk_file_chooser_dialog_new( _("Select an icon theme"), NULL,
218 GTK_FILE_CHOOSER_ACTION_OPEN,
219 GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
220 GTK_STOCK_OPEN, GTK_RESPONSE_OK, NULL );
221 gtk_window_set_transient_for(GTK_WINDOW(fc), GTK_WINDOW(app.dlg));
222 gtk_file_filter_add_pattern( filter, "*.tar.gz" );
223 gtk_file_filter_add_pattern( filter, "*.tar.bz2" );
224 gtk_file_filter_set_name( filter, _("*.tar.gz, *.tar.bz2 (Icon Theme)") );
225
226 gtk_file_chooser_add_filter( GTK_FILE_CHOOSER(fc), filter );
227 gtk_file_chooser_set_filter( GTK_FILE_CHOOSER(fc), filter );
228
229 res = gtk_dialog_run( (GtkDialog*)fc );
230 file = gtk_file_chooser_get_filename( GTK_FILE_CHOOSER(fc) );
231 gtk_widget_destroy( fc );
232
233 if( res == GTK_RESPONSE_OK )
234 install_icon_theme_package(file);
235
236 g_free(file);
237 return TRUE;
238}
239
240gboolean remove_icon_theme(GtkWindow* parent, IconTheme* theme)
241{
242 gboolean ret = TRUE;
243 char* dir = g_build_filename(theme->base_dir, theme->name, NULL);
244 char* tmp_dir = g_build_filename(theme->base_dir, "tmp.XXXXXX", NULL);
245g_debug("tmp_dir = %s", tmp_dir);
246 /* move the theme to a tmp dir first. so we can make the
247 * removal atomic. */
248 if(mkdtemp(tmp_dir))
249 {
250 char* tmp_dest = g_build_filename(tmp_dir, theme->name, NULL);
251 if(g_rename(dir, tmp_dest) == 0)
252 {
253 char* argv[] = {
254 "rm",
255 "-rf",
256 tmp_dir,
257 NULL
258 };
259 GPid pid;
260 if(g_spawn_async(NULL, argv, NULL, G_SPAWN_SEARCH_PATH|G_SPAWN_DO_NOT_REAP_CHILD, NULL, NULL, &pid, NULL))
261 {
262 ret = show_progress_for_pid(GTK_WINDOW(app.dlg), "Remove icon theme", "Removing...", pid);
263 }
264 }
265 g_free(tmp_dest);
266 }
267 else
268 ret = FALSE;
269
270 g_free(dir);
271 return ret;
272}