~cjcurran/+junk/sound-nua-ui-tweaks-fri-16

« back to all changes in this revision

Viewing changes to soundnua/sound-theme-file-utils.c

  • Committer: Sebastien Bacher
  • Date: 2012-03-16 10:09:38 UTC
  • Revision ID: seb128@ubuntu.com-20120316100938-nt63xa0x58zfsen3
initialĀ sourceĀ import

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 8 -*-
 
2
 * Copyright (C) 2008 Bastien Nocera <hadess@hadess.net>
 
3
 *
 
4
 * This program is free software; you can redistribute it and/or modify
 
5
 * it under the terms of the GNU General Public License as published by
 
6
 * the Free Software Foundation; either version 2, or (at your option)
 
7
 * any later version.
 
8
 *
 
9
 * This program is distributed in the hope that it will be useful,
 
10
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 
11
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
12
 * GNU General Public License for more details.
 
13
 *
 
14
 * You should have received a copy of the GNU General Public License
 
15
 * along with this program; if not, write to the Free Software
 
16
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
 
17
 * 02111-1307, USA.
 
18
 */
 
19
 
 
20
#include <config.h>
 
21
#include <glib/gstdio.h>
 
22
#include <glib/gi18n-lib.h>
 
23
#include <gio/gio.h>
 
24
#include <utime.h>
 
25
#include <strings.h>
 
26
 
 
27
#include "sound-theme-file-utils.h"
 
28
 
 
29
#define CUSTOM_THEME_NAME       "__custom"
 
30
 
 
31
/* This function needs to be called after each individual
 
32
 * changeset to the theme */
 
33
void
 
34
custom_theme_update_time (void)
 
35
{
 
36
        char *path;
 
37
 
 
38
        path = custom_theme_dir_path (NULL);
 
39
        utime (path, NULL);
 
40
        g_free (path);
 
41
}
 
42
 
 
43
char *
 
44
custom_theme_dir_path (const char *child)
 
45
{
 
46
        static char *dir = NULL;
 
47
        const char *data_dir;
 
48
 
 
49
        if (dir == NULL) {
 
50
                data_dir = g_get_user_data_dir ();
 
51
                dir = g_build_filename (data_dir, "sounds", CUSTOM_THEME_NAME, NULL);
 
52
        }
 
53
        if (child == NULL)
 
54
                return g_strdup (dir);
 
55
 
 
56
        return g_build_filename (dir, child, NULL);
 
57
}
 
58
 
 
59
static gboolean
 
60
directory_delete_recursive (GFile *directory, GError **error)
 
61
{
 
62
        GFileEnumerator *enumerator;
 
63
        GFileInfo *info;
 
64
        gboolean success = TRUE;
 
65
 
 
66
        enumerator = g_file_enumerate_children (directory,
 
67
                                                G_FILE_ATTRIBUTE_STANDARD_NAME ","
 
68
                                                G_FILE_ATTRIBUTE_STANDARD_TYPE,
 
69
                                                G_FILE_QUERY_INFO_NONE,
 
70
                                                NULL, error);
 
71
        if (enumerator == NULL)
 
72
                return FALSE;
 
73
 
 
74
        while (success &&
 
75
               (info = g_file_enumerator_next_file (enumerator, NULL, NULL))) {
 
76
                GFile *child;
 
77
 
 
78
                child = g_file_get_child (directory, g_file_info_get_name (info));
 
79
 
 
80
                if (g_file_info_get_file_type (info) == G_FILE_TYPE_DIRECTORY) {
 
81
                        success = directory_delete_recursive (child, error);
 
82
                }
 
83
                g_object_unref (info);
 
84
 
 
85
                if (success)
 
86
                        success = g_file_delete (child, NULL, error);
 
87
        }
 
88
        g_file_enumerator_close (enumerator, NULL, NULL);
 
89
 
 
90
        if (success)
 
91
                success = g_file_delete (directory, NULL, error);
 
92
 
 
93
        return success;
 
94
}
 
95
 
 
96
/**
 
97
 * capplet_file_delete_recursive :
 
98
 * @file :
 
99
 * @error  :
 
100
 *
 
101
 * A utility routine to delete files and/or directories,
 
102
 * including non-empty directories.
 
103
 **/
 
104
static gboolean
 
105
capplet_file_delete_recursive (GFile *file, GError **error)
 
106
{
 
107
        GFileInfo *info;
 
108
        GFileType type;
 
109
 
 
110
        g_return_val_if_fail (error == NULL || *error == NULL, FALSE);
 
111
 
 
112
        info = g_file_query_info (file,
 
113
                                  G_FILE_ATTRIBUTE_STANDARD_TYPE,
 
114
                                  G_FILE_QUERY_INFO_NONE,
 
115
                                  NULL, error);
 
116
        if (info == NULL)
 
117
                return FALSE;
 
118
 
 
119
        type = g_file_info_get_file_type (info);
 
120
        g_object_unref (info);
 
121
 
 
122
        if (type == G_FILE_TYPE_DIRECTORY)
 
123
                return directory_delete_recursive (file, error);
 
124
        else
 
125
                return g_file_delete (file, NULL, error);
 
126
}
 
127
 
 
128
void
 
129
delete_custom_theme_dir (void)
 
130
{
 
131
        char *dir;
 
132
        GFile *file;
 
133
 
 
134
        dir = custom_theme_dir_path (NULL);
 
135
        file = g_file_new_for_path (dir);
 
136
        g_free (dir);
 
137
        capplet_file_delete_recursive (file, NULL);
 
138
        g_object_unref (file);
 
139
 
 
140
        g_debug ("deleted the custom theme dir");
 
141
}
 
142
 
 
143
gboolean
 
144
custom_theme_dir_is_empty (void)
 
145
{
 
146
        char            *dir;
 
147
        GFile           *file;
 
148
        gboolean         is_empty;
 
149
        GFileEnumerator *enumerator;
 
150
        GFileInfo       *info;
 
151
        GError          *error = NULL;
 
152
 
 
153
        dir = custom_theme_dir_path (NULL);
 
154
        file = g_file_new_for_path (dir);
 
155
        g_free (dir);
 
156
 
 
157
        is_empty = TRUE;
 
158
 
 
159
        enumerator = g_file_enumerate_children (file,
 
160
                                                G_FILE_ATTRIBUTE_STANDARD_NAME ","
 
161
                                                G_FILE_ATTRIBUTE_STANDARD_TYPE,
 
162
                                                G_FILE_QUERY_INFO_NONE,
 
163
                                                NULL, &error);
 
164
        if (enumerator == NULL) {
 
165
                g_warning ("Unable to enumerate files: %s", error->message);
 
166
                g_error_free (error);
 
167
                goto out;
 
168
        }
 
169
 
 
170
        while (is_empty &&
 
171
               (info = g_file_enumerator_next_file (enumerator, NULL, NULL))) {
 
172
 
 
173
                if (strcmp ("index.theme", g_file_info_get_name (info)) != 0) {
 
174
                        is_empty = FALSE;
 
175
                }
 
176
 
 
177
                g_object_unref (info);
 
178
        }
 
179
        g_file_enumerator_close (enumerator, NULL, NULL);
 
180
 
 
181
 out:
 
182
        g_object_unref (file);
 
183
 
 
184
        return is_empty;
 
185
}
 
186
 
 
187
static void
 
188
delete_one_file (const char *sound_name, const char *pattern)
 
189
{
 
190
        GFile *file;
 
191
        char *name, *filename;
 
192
 
 
193
        name = g_strdup_printf (pattern, sound_name);
 
194
        filename = custom_theme_dir_path (name);
 
195
        g_free (name);
 
196
        file = g_file_new_for_path (filename);
 
197
        g_free (filename);
 
198
        capplet_file_delete_recursive (file, NULL);
 
199
        g_object_unref (file);
 
200
}
 
201
 
 
202
void
 
203
delete_old_files (const char **sounds)
 
204
{
 
205
        guint i;
 
206
 
 
207
        for (i = 0; sounds[i] != NULL; i++) {
 
208
                delete_one_file (sounds[i], "%s.ogg");
 
209
        }
 
210
}
 
211
 
 
212
void
 
213
delete_disabled_files (const char **sounds)
 
214
{
 
215
        guint i;
 
216
 
 
217
        for (i = 0; sounds[i] != NULL; i++)
 
218
                delete_one_file (sounds[i], "%s.disabled");
 
219
}
 
220
 
 
221
static void
 
222
create_one_file (GFile *file)
 
223
{
 
224
        GFileOutputStream* stream;
 
225
 
 
226
        stream = g_file_create (file, G_FILE_CREATE_NONE, NULL, NULL);
 
227
        if (stream != NULL) {
 
228
                g_output_stream_close (G_OUTPUT_STREAM (stream), NULL, NULL);
 
229
                g_object_unref (stream);
 
230
        }
 
231
}
 
232
 
 
233
void
 
234
add_disabled_file (const char **sounds)
 
235
{
 
236
        guint i;
 
237
 
 
238
        for (i = 0; sounds[i] != NULL; i++) {
 
239
                GFile *file;
 
240
                char *name, *filename;
 
241
 
 
242
                name = g_strdup_printf ("%s.disabled", sounds[i]);
 
243
                filename = custom_theme_dir_path (name);
 
244
                g_free (name);
 
245
                file = g_file_new_for_path (filename);
 
246
                g_free (filename);
 
247
 
 
248
                create_one_file (file);
 
249
                g_object_unref (file);
 
250
        }
 
251
}
 
252
 
 
253
void
 
254
add_custom_file (const char **sounds, const char *filename)
 
255
{
 
256
        guint i;
 
257
 
 
258
        for (i = 0; sounds[i] != NULL; i++) {
 
259
                GFile *file;
 
260
                char *name, *path;
 
261
 
 
262
                /* We use *.ogg because it's the first type of file that
 
263
                 * libcanberra looks at */
 
264
                name = g_strdup_printf ("%s.ogg", sounds[i]);
 
265
                path = custom_theme_dir_path (name);
 
266
                g_free (name);
 
267
                /* In case there's already a link there, delete it */
 
268
                g_unlink (path);
 
269
                file = g_file_new_for_path (path);
 
270
                g_free (path);
 
271
 
 
272
                /* Create the link */
 
273
                g_file_make_symbolic_link (file, filename, NULL, NULL);
 
274
                g_object_unref (file);
 
275
        }
 
276
}
 
277
 
 
278
void
 
279
create_custom_theme (const char *parent)
 
280
{
 
281
        GKeyFile *keyfile;
 
282
        char     *data;
 
283
        char     *path;
 
284
 
 
285
        /* Create the custom directory */
 
286
        path = custom_theme_dir_path (NULL);
 
287
        g_mkdir_with_parents (path, 0755);
 
288
        g_free (path);
 
289
 
 
290
        /* Set the data for index.theme */
 
291
        keyfile = g_key_file_new ();
 
292
        g_key_file_set_string (keyfile, "Sound Theme", "Name", _("Custom"));
 
293
        g_key_file_set_string (keyfile, "Sound Theme", "Inherits", parent);
 
294
        g_key_file_set_string (keyfile, "Sound Theme", "Directories", ".");
 
295
        data = g_key_file_to_data (keyfile, NULL, NULL);
 
296
        g_key_file_free (keyfile);
 
297
 
 
298
        /* Save the index.theme */
 
299
        path = custom_theme_dir_path ("index.theme");
 
300
        g_file_set_contents (path, data, -1, NULL);
 
301
        g_free (path);
 
302
        g_free (data);
 
303
 
 
304
        custom_theme_update_time ();
 
305
}