~ubuntu-branches/debian/jessie/mate-control-center/jessie

« back to all changes in this revision

Viewing changes to capplets/common/capplet-util.c

  • Committer: Package Import Robot
  • Author(s): Mike Gabriel
  • Date: 2014-05-02 23:20:06 UTC
  • Revision ID: package-import@ubuntu.com-20140502232006-9626h14ggaz2hc9h
Tags: upstream-1.8.1+dfsg1
ImportĀ upstreamĀ versionĀ 1.8.1+dfsg1

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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
 
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
 
70
capplet_help (GtkWindow *parent, char const *section)
 
71
{
 
72
        GError *error = NULL;
 
73
        char *uri;
 
74
        GdkScreen *screen;
 
75
 
 
76
        g_return_if_fail (section != NULL);
 
77
 
 
78
        if (!parent)
 
79
                screen = gdk_screen_get_default ();
 
80
        else
 
81
                screen = gtk_widget_get_screen (GTK_WIDGET (parent));
 
82
 
 
83
        uri = g_strdup_printf ("help:mate-user-guide/%s", section);
 
84
 
 
85
        if (!gtk_show_uri (screen, uri, gtk_get_current_event_time (), &error)) {
 
86
                capplet_error_dialog (
 
87
                        parent,
 
88
                        _("There was an error displaying help: %s"),
 
89
                        error);
 
90
        }
 
91
 
 
92
        g_free (uri);
 
93
}
 
94
 
 
95
/**
 
96
 * capplet_set_icon :
 
97
 * @window :
 
98
 * @file_name  :
 
99
 *
 
100
 * A quick utility routine to avoid the cut-n-paste of bogus code
 
101
 * that caused several bugs.
 
102
 **/
 
103
void
 
104
capplet_set_icon (GtkWidget *window, char const *icon_file_name)
 
105
{
 
106
        /* Make sure that every window gets an icon */
 
107
        gtk_window_set_default_icon_name (icon_file_name);
 
108
        gtk_window_set_icon_name (GTK_WINDOW (window), icon_file_name);
 
109
}
 
110
 
 
111
static gboolean
 
112
directory_delete_recursive (GFile *directory, GError **error)
 
113
{
 
114
        GFileEnumerator *enumerator;
 
115
        GFileInfo *info;
 
116
        gboolean success = TRUE;
 
117
 
 
118
        enumerator = g_file_enumerate_children (directory,
 
119
                                                G_FILE_ATTRIBUTE_STANDARD_NAME ","
 
120
                                                G_FILE_ATTRIBUTE_STANDARD_TYPE,
 
121
                                                G_FILE_QUERY_INFO_NONE,
 
122
                                                NULL, error);
 
123
        if (enumerator == NULL)
 
124
                return FALSE;
 
125
 
 
126
        while (success &&
 
127
               (info = g_file_enumerator_next_file (enumerator, NULL, NULL))) {
 
128
                GFile *child;
 
129
 
 
130
                child = g_file_get_child (directory, g_file_info_get_name (info));
 
131
 
 
132
                if (g_file_info_get_file_type (info) == G_FILE_TYPE_DIRECTORY) {
 
133
                        success = directory_delete_recursive (child, error);
 
134
                } else {
 
135
                        success = g_file_delete (child, NULL, error);
 
136
                }
 
137
                g_object_unref (info);
 
138
        }
 
139
        g_file_enumerator_close (enumerator, NULL, NULL);
 
140
 
 
141
        if (success)
 
142
                success = g_file_delete (directory, NULL, error);
 
143
 
 
144
        return success;
 
145
}
 
146
 
 
147
/**
 
148
 * capplet_file_delete_recursive :
 
149
 * @file :
 
150
 * @error  :
 
151
 *
 
152
 * A utility routine to delete files and/or directories,
 
153
 * including non-empty directories.
 
154
 **/
 
155
gboolean
 
156
capplet_file_delete_recursive (GFile *file, GError **error)
 
157
{
 
158
        GFileInfo *info;
 
159
        GFileType type;
 
160
 
 
161
        g_return_val_if_fail (error == NULL || *error == NULL, FALSE);
 
162
 
 
163
        info = g_file_query_info (file,
 
164
                                  G_FILE_ATTRIBUTE_STANDARD_TYPE,
 
165
                                  G_FILE_QUERY_INFO_NONE,
 
166
                                  NULL, error);
 
167
        if (info == NULL)
 
168
                return FALSE;
 
169
 
 
170
        type = g_file_info_get_file_type (info);
 
171
        g_object_unref (info);
 
172
 
 
173
        if (type == G_FILE_TYPE_DIRECTORY)
 
174
                return directory_delete_recursive (file, error);
 
175
        else
 
176
                return g_file_delete (file, NULL, error);
 
177
}
 
178
 
 
179
void
 
180
capplet_init (GOptionContext *context,
 
181
              int *argc,
 
182
              char ***argv)
 
183
{
 
184
        GError *err = NULL;
 
185
 
 
186
#ifdef ENABLE_NLS
 
187
        bindtextdomain (GETTEXT_PACKAGE, MATELOCALEDIR);
 
188
        bind_textdomain_codeset (GETTEXT_PACKAGE, "UTF-8");
 
189
        textdomain (GETTEXT_PACKAGE);
 
190
#endif
 
191
 
 
192
        if (context) {
 
193
#if GLIB_CHECK_VERSION (2, 12, 0)
 
194
                g_option_context_set_translation_domain (context, GETTEXT_PACKAGE);
 
195
#endif
 
196
                g_option_context_add_group (context, gtk_get_option_group (TRUE));
 
197
 
 
198
                if (!g_option_context_parse (context, argc, argv, &err)) {
 
199
                        g_printerr ("%s\n", err->message);
 
200
                        exit (1);
 
201
                }
 
202
        }
 
203
 
 
204
        gtk_init (argc, argv);
 
205
}