summaryrefslogtreecommitdiff
path: root/capplets/common/capplet-util.c
blob: b9fde6bb2bb572b0218406068f99cee0db5b8fe7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
/* -*- mode: c; style: linux -*- */

/* capplet-util.c
 * Copyright (C) 2001 Ximian, Inc.
 *
 * Written by Bradford Hovinen <hovinen@ximian.com>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2, or (at your option)
 * any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
 * 02110-1301, USA.
 */

#ifdef HAVE_CONFIG_H
#  include <config.h>
#endif

#include <ctype.h>

/* For stat */
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <glib/gi18n.h>
#include <stdlib.h>

#include "capplet-util.h"

static void
capplet_error_dialog (GtkWindow *parent, char const *msg, GError *err)
{
	if (err != NULL) {
		GtkWidget *dialog;

		dialog = gtk_message_dialog_new (GTK_WINDOW (parent),
			GTK_DIALOG_DESTROY_WITH_PARENT,
			GTK_MESSAGE_ERROR,
			GTK_BUTTONS_CLOSE,
			msg, err->message);

		g_signal_connect (G_OBJECT (dialog),
			"response",
			G_CALLBACK (gtk_widget_destroy), NULL);
		gtk_window_set_resizable (GTK_WINDOW (dialog), FALSE);
		gtk_widget_show (dialog);
		g_error_free (err);
	}
}

/**
 * capplet_help :
 * @parent :
 * @helpfile :
 * @section  :
 *
 * A quick utility routine to display help for capplets, and handle errors in a
 * Havoc happy way.
 **/
void
capplet_help (GtkWindow *parent, char const *section)
{
	GError *error = NULL;
	char *uri;

	g_return_if_fail (section != NULL);

	uri = g_strdup_printf ("help:mate-user-guide/%s", section);

	if (!gtk_show_uri_on_window (parent , uri, gtk_get_current_event_time (), &error)) {
		capplet_error_dialog (
			parent,
			_("There was an error displaying help: %s"),
			error);
	}

	g_free (uri);
}

/**
 * capplet_set_icon :
 * @window :
 * @file_name  :
 *
 * A quick utility routine to avoid the cut-n-paste of bogus code
 * that caused several bugs.
 **/
void
capplet_set_icon (GtkWidget *window, char const *icon_file_name)
{
	/* Make sure that every window gets an icon */
	gtk_window_set_default_icon_name (icon_file_name);
	gtk_window_set_icon_name (GTK_WINDOW (window), icon_file_name);
}

static gboolean
directory_delete_recursive (GFile *directory, GError **error)
{
	GFileEnumerator *enumerator;
	GFileInfo *info;
	gboolean success = TRUE;

	enumerator = g_file_enumerate_children (directory,
						G_FILE_ATTRIBUTE_STANDARD_NAME ","
						G_FILE_ATTRIBUTE_STANDARD_TYPE,
						G_FILE_QUERY_INFO_NONE,
						NULL, error);
	if (enumerator == NULL)
		return FALSE;

	while (success &&
	       (info = g_file_enumerator_next_file (enumerator, NULL, NULL))) {
		GFile *child;

		child = g_file_get_child (directory, g_file_info_get_name (info));

		if (g_file_info_get_file_type (info) == G_FILE_TYPE_DIRECTORY) {
			success = directory_delete_recursive (child, error);
		} else {
			success = g_file_delete (child, NULL, error);
		}
		g_object_unref (info);
	}
	g_file_enumerator_close (enumerator, NULL, NULL);

	if (success)
		success = g_file_delete (directory, NULL, error);

	return success;
}

/**
 * capplet_file_delete_recursive :
 * @file :
 * @error  :
 *
 * A utility routine to delete files and/or directories,
 * including non-empty directories.
 **/
gboolean
capplet_file_delete_recursive (GFile *file, GError **error)
{
	GFileInfo *info;
	GFileType type;

	g_return_val_if_fail (error == NULL || *error == NULL, FALSE);

	info = g_file_query_info (file,
				  G_FILE_ATTRIBUTE_STANDARD_TYPE,
				  G_FILE_QUERY_INFO_NONE,
				  NULL, error);
	if (info == NULL)
		return FALSE;

	type = g_file_info_get_file_type (info);
	g_object_unref (info);

	if (type == G_FILE_TYPE_DIRECTORY)
		return directory_delete_recursive (file, error);
	else
		return g_file_delete (file, NULL, error);
}

gboolean
capplet_dialog_page_scroll_event_cb (GtkWidget *widget, GdkEventScroll *event, GtkWindow *window)
{
    GtkNotebook *notebook = GTK_NOTEBOOK (widget);
    GtkWidget *child, *event_widget, *action_widget;

    child = gtk_notebook_get_nth_page (notebook, gtk_notebook_get_current_page (notebook));
    if (child == NULL)
        return FALSE;

    event_widget = gtk_get_event_widget ((GdkEvent *) event);

    /* Ignore scroll events from the content of the page */
    if (event_widget == NULL ||
        event_widget == child ||
        gtk_widget_is_ancestor (event_widget, child))
        return FALSE;

    /* And also from the action widgets */
    action_widget = gtk_notebook_get_action_widget (notebook, GTK_PACK_START);
    if (event_widget == action_widget ||
        (action_widget != NULL && gtk_widget_is_ancestor (event_widget, action_widget)))
        return FALSE;
    action_widget = gtk_notebook_get_action_widget (notebook, GTK_PACK_END);
    if (event_widget == action_widget ||
        (action_widget != NULL && gtk_widget_is_ancestor (event_widget, action_widget)))
        return FALSE;

    switch (event->direction) {
    case GDK_SCROLL_RIGHT:
    case GDK_SCROLL_DOWN:
        gtk_notebook_next_page (notebook);
        break;
    case GDK_SCROLL_LEFT:
    case GDK_SCROLL_UP:
        gtk_notebook_prev_page (notebook);
        break;
    case GDK_SCROLL_SMOOTH:
        switch (gtk_notebook_get_tab_pos (notebook)) {
            case GTK_POS_LEFT:
            case GTK_POS_RIGHT:
                if (event->delta_y > 0)
                    gtk_notebook_next_page (notebook);
                else if (event->delta_y < 0)
                    gtk_notebook_prev_page (notebook);
                break;
            case GTK_POS_TOP:
            case GTK_POS_BOTTOM:
                if (event->delta_x > 0)
                    gtk_notebook_next_page (notebook);
                else if (event->delta_x < 0)
                    gtk_notebook_prev_page (notebook);
                break;
            }
        break;
    }

    return TRUE;
}

void
capplet_init (GOptionContext *context,
	      int *argc,
	      char ***argv)
{
	GError *err = NULL;

#ifdef ENABLE_NLS
	bindtextdomain (GETTEXT_PACKAGE, MATELOCALEDIR);
	bind_textdomain_codeset (GETTEXT_PACKAGE, "UTF-8");
	textdomain (GETTEXT_PACKAGE);
#endif

	if (context) {
		g_option_context_set_translation_domain (context, GETTEXT_PACKAGE);
		g_option_context_add_group (context, gtk_get_option_group (TRUE));

		if (!g_option_context_parse (context, argc, argv, &err)) {
			g_printerr ("%s\n", err->message);
			exit (1);
		}
	}

	gtk_init (argc, argv);
}