1
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 8 -*-
2
* Copyright (C) 2008 Bastien Nocera <hadess@hadess.net>
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)
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.
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
21
#include <glib/gstdio.h>
22
#include <glib/gi18n-lib.h>
27
#include "sound-theme-file-utils.h"
29
#define CUSTOM_THEME_NAME "__custom"
31
/* This function needs to be called after each individual
32
* changeset to the theme */
34
custom_theme_update_time (void)
38
path = custom_theme_dir_path (NULL);
44
custom_theme_dir_path (const char *child)
46
static char *dir = NULL;
50
data_dir = g_get_user_data_dir ();
51
dir = g_build_filename (data_dir, "sounds", CUSTOM_THEME_NAME, NULL);
54
return g_strdup (dir);
56
return g_build_filename (dir, child, NULL);
60
directory_delete_recursive (GFile *directory, GError **error)
62
GFileEnumerator *enumerator;
64
gboolean success = TRUE;
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,
71
if (enumerator == NULL)
75
(info = g_file_enumerator_next_file (enumerator, NULL, NULL))) {
78
child = g_file_get_child (directory, g_file_info_get_name (info));
80
if (g_file_info_get_file_type (info) == G_FILE_TYPE_DIRECTORY) {
81
success = directory_delete_recursive (child, error);
83
g_object_unref (info);
86
success = g_file_delete (child, NULL, error);
88
g_file_enumerator_close (enumerator, NULL, NULL);
91
success = g_file_delete (directory, NULL, error);
97
* capplet_file_delete_recursive :
101
* A utility routine to delete files and/or directories,
102
* including non-empty directories.
105
capplet_file_delete_recursive (GFile *file, GError **error)
110
g_return_val_if_fail (error == NULL || *error == NULL, FALSE);
112
info = g_file_query_info (file,
113
G_FILE_ATTRIBUTE_STANDARD_TYPE,
114
G_FILE_QUERY_INFO_NONE,
119
type = g_file_info_get_file_type (info);
120
g_object_unref (info);
122
if (type == G_FILE_TYPE_DIRECTORY)
123
return directory_delete_recursive (file, error);
125
return g_file_delete (file, NULL, error);
129
delete_custom_theme_dir (void)
134
dir = custom_theme_dir_path (NULL);
135
file = g_file_new_for_path (dir);
137
capplet_file_delete_recursive (file, NULL);
138
g_object_unref (file);
140
g_debug ("deleted the custom theme dir");
144
custom_theme_dir_is_empty (void)
149
GFileEnumerator *enumerator;
151
GError *error = NULL;
153
dir = custom_theme_dir_path (NULL);
154
file = g_file_new_for_path (dir);
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,
164
if (enumerator == NULL) {
165
g_warning ("Unable to enumerate files: %s", error->message);
166
g_error_free (error);
171
(info = g_file_enumerator_next_file (enumerator, NULL, NULL))) {
173
if (strcmp ("index.theme", g_file_info_get_name (info)) != 0) {
177
g_object_unref (info);
179
g_file_enumerator_close (enumerator, NULL, NULL);
182
g_object_unref (file);
188
delete_one_file (const char *sound_name, const char *pattern)
191
char *name, *filename;
193
name = g_strdup_printf (pattern, sound_name);
194
filename = custom_theme_dir_path (name);
196
file = g_file_new_for_path (filename);
198
capplet_file_delete_recursive (file, NULL);
199
g_object_unref (file);
203
delete_old_files (const char **sounds)
207
for (i = 0; sounds[i] != NULL; i++) {
208
delete_one_file (sounds[i], "%s.ogg");
213
delete_disabled_files (const char **sounds)
217
for (i = 0; sounds[i] != NULL; i++)
218
delete_one_file (sounds[i], "%s.disabled");
222
create_one_file (GFile *file)
224
GFileOutputStream* stream;
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);
234
add_disabled_file (const char **sounds)
238
for (i = 0; sounds[i] != NULL; i++) {
240
char *name, *filename;
242
name = g_strdup_printf ("%s.disabled", sounds[i]);
243
filename = custom_theme_dir_path (name);
245
file = g_file_new_for_path (filename);
248
create_one_file (file);
249
g_object_unref (file);
254
add_custom_file (const char **sounds, const char *filename)
258
for (i = 0; sounds[i] != NULL; i++) {
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);
267
/* In case there's already a link there, delete it */
269
file = g_file_new_for_path (path);
272
/* Create the link */
273
g_file_make_symbolic_link (file, filename, NULL, NULL);
274
g_object_unref (file);
279
create_custom_theme (const char *parent)
285
/* Create the custom directory */
286
path = custom_theme_dir_path (NULL);
287
g_mkdir_with_parents (path, 0755);
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);
298
/* Save the index.theme */
299
path = custom_theme_dir_path ("index.theme");
300
g_file_set_contents (path, data, -1, NULL);
304
custom_theme_update_time ();