1 /* -*- mode: c; style: linux -*- */
2
3 /* capplet-util.c
4 * Copyright (C) 2001 Ximian, Inc.
5 *
6 * Written by Bradford Hovinen <hovinen@ximian.com>
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2, or (at your option)
11 * any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
21 * 02110-1301, USA.
22 */
23
24 #ifdef HAVE_CONFIG_H
25 #include <config.h>
26 #endif
27
28 #include <ctype.h>
29
30 /* For stat */
31 #include <sys/types.h>
32 #include <sys/stat.h>
33 #include <unistd.h>
34 #include <glib/gi18n.h>
35 #include <stdlib.h>
36
37 #include "capplet-util.h"
38
39 static void
capplet_error_dialog(GtkWindow * parent,char const * msg,GError * err)40 capplet_error_dialog (GtkWindow *parent, char const *msg, GError *err)
41 {
42 if (err != NULL) {
43 GtkWidget *dialog;
44
45 dialog = gtk_message_dialog_new (GTK_WINDOW (parent),
46 GTK_DIALOG_DESTROY_WITH_PARENT,
47 GTK_MESSAGE_ERROR,
48 GTK_BUTTONS_CLOSE,
49 msg, err->message);
50
51 g_signal_connect (G_OBJECT (dialog),
52 "response",
53 G_CALLBACK (gtk_widget_destroy), NULL);
54 gtk_window_set_resizable (GTK_WINDOW (dialog), FALSE);
55 gtk_widget_show (dialog);
56 g_error_free (err);
57 }
58 }
59
60 /**
61 * capplet_help :
62 * @parent :
63 * @helpfile :
64 * @section :
65 *
66 * A quick utility routine to display help for capplets, and handle errors in a
67 * Havoc happy way.
68 **/
69 void
capplet_help(GtkWindow * parent,char const * section)70 capplet_help (GtkWindow *parent, char const *section)
71 {
72 GError *error = NULL;
73 char *uri;
74
75 g_return_if_fail (section != NULL);
76
77 uri = g_strdup_printf ("help:mate-user-guide/%s", section);
78
79 if (!gtk_show_uri_on_window (parent , uri, gtk_get_current_event_time (), &error)) {
80 capplet_error_dialog (
81 parent,
82 _("There was an error displaying help: %s"),
83 error);
84 }
85
86 g_free (uri);
87 }
88
89 /**
90 * capplet_set_icon :
91 * @window :
92 * @file_name :
93 *
94 * A quick utility routine to avoid the cut-n-paste of bogus code
95 * that caused several bugs.
96 **/
97 void
capplet_set_icon(GtkWidget * window,char const * icon_file_name)98 capplet_set_icon (GtkWidget *window, char const *icon_file_name)
99 {
100 /* Make sure that every window gets an icon */
101 gtk_window_set_default_icon_name (icon_file_name);
102 gtk_window_set_icon_name (GTK_WINDOW (window), icon_file_name);
103 }
104
105 static gboolean
directory_delete_recursive(GFile * directory,GError ** error)106 directory_delete_recursive (GFile *directory, GError **error)
107 {
108 GFileEnumerator *enumerator;
109 GFileInfo *info;
110 gboolean success = TRUE;
111
112 enumerator = g_file_enumerate_children (directory,
113 G_FILE_ATTRIBUTE_STANDARD_NAME ","
114 G_FILE_ATTRIBUTE_STANDARD_TYPE,
115 G_FILE_QUERY_INFO_NONE,
116 NULL, error);
117 if (enumerator == NULL)
118 return FALSE;
119
120 while (success &&
121 (info = g_file_enumerator_next_file (enumerator, NULL, NULL))) {
122 GFile *child;
123
124 child = g_file_get_child (directory, g_file_info_get_name (info));
125
126 if (g_file_info_get_file_type (info) == G_FILE_TYPE_DIRECTORY) {
127 success = directory_delete_recursive (child, error);
128 } else {
129 success = g_file_delete (child, NULL, error);
130 }
131 g_object_unref (info);
132 }
133 g_file_enumerator_close (enumerator, NULL, NULL);
134
135 if (success)
136 success = g_file_delete (directory, NULL, error);
137
138 return success;
139 }
140
141 /**
142 * capplet_file_delete_recursive :
143 * @file :
144 * @error :
145 *
146 * A utility routine to delete files and/or directories,
147 * including non-empty directories.
148 **/
149 gboolean
capplet_file_delete_recursive(GFile * file,GError ** error)150 capplet_file_delete_recursive (GFile *file, GError **error)
151 {
152 GFileInfo *info;
153 GFileType type;
154
155 g_return_val_if_fail (error == NULL || *error == NULL, FALSE);
156
157 info = g_file_query_info (file,
158 G_FILE_ATTRIBUTE_STANDARD_TYPE,
159 G_FILE_QUERY_INFO_NONE,
160 NULL, error);
161 if (info == NULL)
162 return FALSE;
163
164 type = g_file_info_get_file_type (info);
165 g_object_unref (info);
166
167 if (type == G_FILE_TYPE_DIRECTORY)
168 return directory_delete_recursive (file, error);
169 else
170 return g_file_delete (file, NULL, error);
171 }
172
173 gboolean
capplet_notebook_scroll_event_cb(GtkWidget * widget,GdkEventScroll * event)174 capplet_notebook_scroll_event_cb (GtkWidget *widget,
175 GdkEventScroll *event)
176 {
177 GtkNotebook *notebook = GTK_NOTEBOOK (widget);
178 GtkWidget *child, *event_widget, *action_widget;
179
180 child = gtk_notebook_get_nth_page (notebook, gtk_notebook_get_current_page (notebook));
181 if (child == NULL)
182 return FALSE;
183
184 event_widget = gtk_get_event_widget ((GdkEvent *) event);
185
186 /* Ignore scroll events from the content of the page */
187 if (event_widget == NULL ||
188 event_widget == child ||
189 gtk_widget_is_ancestor (event_widget, child))
190 return FALSE;
191
192 /* And also from the action widgets */
193 action_widget = gtk_notebook_get_action_widget (notebook, GTK_PACK_START);
194 if (event_widget == action_widget ||
195 (action_widget != NULL && gtk_widget_is_ancestor (event_widget, action_widget)))
196 return FALSE;
197 action_widget = gtk_notebook_get_action_widget (notebook, GTK_PACK_END);
198 if (event_widget == action_widget ||
199 (action_widget != NULL && gtk_widget_is_ancestor (event_widget, action_widget)))
200 return FALSE;
201
202 switch (event->direction) {
203 case GDK_SCROLL_RIGHT:
204 case GDK_SCROLL_DOWN:
205 gtk_notebook_next_page (notebook);
206 break;
207 case GDK_SCROLL_LEFT:
208 case GDK_SCROLL_UP:
209 gtk_notebook_prev_page (notebook);
210 break;
211 case GDK_SCROLL_SMOOTH:
212 switch (gtk_notebook_get_tab_pos (notebook)) {
213 case GTK_POS_LEFT:
214 case GTK_POS_RIGHT:
215 if (event->delta_y > 0)
216 gtk_notebook_next_page (notebook);
217 else if (event->delta_y < 0)
218 gtk_notebook_prev_page (notebook);
219 break;
220 case GTK_POS_TOP:
221 case GTK_POS_BOTTOM:
222 if (event->delta_x > 0)
223 gtk_notebook_next_page (notebook);
224 else if (event->delta_x < 0)
225 gtk_notebook_prev_page (notebook);
226 break;
227 }
228 break;
229 }
230
231 return TRUE;
232 }
233
234 void
capplet_init(GOptionContext * context,int * argc,char *** argv)235 capplet_init (GOptionContext *context,
236 int *argc,
237 char ***argv)
238 {
239 GError *err = NULL;
240
241 #ifdef ENABLE_NLS
242 bindtextdomain (GETTEXT_PACKAGE, MATELOCALEDIR);
243 bind_textdomain_codeset (GETTEXT_PACKAGE, "UTF-8");
244 textdomain (GETTEXT_PACKAGE);
245 #endif /* ENABLE_NLS */
246
247 if (context) {
248 #ifdef ENABLE_NLS
249 g_option_context_set_translation_domain (context, GETTEXT_PACKAGE);
250 #endif /* ENABLE_NLS */
251 g_option_context_add_group (context, gtk_get_option_group (TRUE));
252
253 if (!g_option_context_parse (context, argc, argv, &err)) {
254 g_printerr ("%s\n", err->message);
255 exit (1);
256 }
257 }
258
259 gtk_init (argc, argv);
260 }
261