fixed no-return-in-nonvoid-function utils.c:91, no-return-in-nonvoid-function color...
[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 <glib/gi18n.h>
24 #include <sys/types.h>
25 #include <signal.h>
26 #include <sys/wait.h>
27 #include <stdlib.h>
28
29 static void on_pid_exit(GPid pid, gint status, gpointer user_data)
30 {
31 GtkDialog* dlg = GTK_DIALOG(user_data);
32 gtk_dialog_response(dlg, GTK_RESPONSE_CLOSE);
33 g_debug("pid exit");
34 }
35
36 static void on_progress_dlg_response(GtkDialog* dlg, int res, gpointer user_data)
37 {
38 GPid* ppid = (GPid*)user_data;
39 int status;
40 kill(*ppid, SIGTERM);
41 waitpid(*ppid, &status, WNOHANG);
42 }
43
44 void show_progress_for_pid(GtkWindow* parent, const char* title, const char* msg, GPid pid)
45 {
46 GtkWidget* dlg = gtk_dialog_new_with_buttons(title, parent,
47 GTK_DIALOG_NO_SEPARATOR|GTK_DIALOG_MODAL,
48 GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL, NULL);
49 guint child_watch = g_child_watch_add(pid, on_pid_exit, dlg);
50 GtkWidget* vbox = gtk_dialog_get_content_area(dlg);
51 GtkWidget* label = gtk_label_new(msg);
52 gtk_widget_show(label);
53 gtk_box_pack_start(vbox, label, FALSE, TRUE, 0);
54 g_signal_connect(dlg, "response", G_CALLBACK(on_progress_dlg_response), &pid);
55 gtk_dialog_run(dlg);
56 g_source_remove(child_watch);
57 gtk_widget_destroy(dlg);
58 }
59
60 gboolean install_icon_theme(GtkWindow* parent)
61 {
62 GtkFileFilter* filter = gtk_file_filter_new();
63 char* file = NULL;
64 GPid pid = -1;
65 GtkWidget* fc = gtk_file_chooser_dialog_new( _("Select an icon theme"), NULL,
66 GTK_FILE_CHOOSER_ACTION_OPEN,
67 GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
68 GTK_STOCK_OPEN, GTK_RESPONSE_OK, NULL );
69
70 gtk_file_filter_add_pattern( filter, "*.tar.gz" );
71 gtk_file_filter_add_pattern( filter, "*.tar.bz2" );
72 gtk_file_filter_set_name( filter, _("*.tar.gz, *.tar.bz2 (Icon Theme)") );
73
74 gtk_file_chooser_add_filter( GTK_FILE_CHOOSER(fc), filter );
75 gtk_file_chooser_set_filter( GTK_FILE_CHOOSER(fc), filter );
76
77 if( gtk_dialog_run( (GtkDialog*)fc ) == GTK_RESPONSE_OK )
78 {
79 file = gtk_file_chooser_get_filename( GTK_FILE_CHOOSER(fc) );
80 char* argv[]={
81 PACKAGE_DATA_DIR"/install-icon-theme.sh",
82 file, NULL };
83 g_spawn_async(NULL, argv, NULL, G_SPAWN_DO_NOT_REAP_CHILD, NULL, NULL, &pid, NULL);
84 g_debug("%s\n%s\npid = %d", file, argv[0], pid);
85 }
86 gtk_widget_destroy( fc );
87
88 if(pid >=0)
89 show_progress_for_pid(parent, "Install themes", "Installing...", pid);
90 g_free(file);
91
92 return TRUE;
93 }
94