1
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 8 -*-
3
* Copyright (C) 2008 Bastien Nocera <hadess@hadess.net>
4
* Copyright (C) 2008 William Jon McCann
6
* This program is free software; you can redistribute it and/or modify
7
* it under the terms of the GNU General Public License as published by
8
* the Free Software Foundation; either version 2 of the License, or
9
* (at your option) any later version.
11
* This program is distributed in the hope that it will be useful,
12
* but WITHOUT ANY WARRANTY; without even the implied warranty of
13
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14
* GNU General Public License for more details.
16
* You should have received a copy of the GNU General Public License
17
* along with this program; if not, write to the Free Software
18
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
31
#include <glib/gi18n-lib.h>
33
#include <canberra-gtk.h>
34
#include <libxml/tree.h>
36
#include <gsettings-desktop-schemas/gdesktop-enums.h>
38
#include "gvc-sound-theme-chooser.h"
39
#include "sound-theme-file-utils.h"
41
#define GVC_SOUND_THEME_CHOOSER_GET_PRIVATE(o) (G_TYPE_INSTANCE_GET_PRIVATE ((o), GVC_TYPE_SOUND_THEME_CHOOSER, GvcSoundThemeChooserPrivate))
43
struct GvcSoundThemeChooserPrivate
46
GtkWidget *selection_box;
48
GSettings *sound_settings;
53
static void gvc_sound_theme_chooser_class_init (GvcSoundThemeChooserClass *klass);
54
static void gvc_sound_theme_chooser_init (GvcSoundThemeChooser *sound_theme_chooser);
55
static void gvc_sound_theme_chooser_finalize (GObject *object);
57
G_DEFINE_TYPE (GvcSoundThemeChooser, gvc_sound_theme_chooser, GTK_TYPE_VBOX)
59
#define KEY_SOUNDS_SCHEMA "org.gnome.desktop.sound"
60
#define EVENT_SOUNDS_KEY "event-sounds"
61
#define INPUT_SOUNDS_KEY "input-feedback-sounds"
62
#define SOUND_THEME_KEY "theme-name"
64
#define WM_SCHEMA "org.gnome.desktop.wm.preferences"
65
#define AUDIO_BELL_KEY "audible-bell"
67
#define DEFAULT_ALERT_ID "__default"
68
#define CUSTOM_THEME_NAME "__custom"
69
#define NO_SOUNDS_THEME_NAME "__no_sounds"
70
#define DEFAULT_THEME "ubuntu"
89
SOUND_TYPE_DEFAULT_FROM_THEME,
94
#define GVC_SOUND_SOUND (xmlChar *) "sound"
95
#define GVC_SOUND_NAME (xmlChar *) "name"
96
#define GVC_SOUND_FILENAME (xmlChar *) "filename"
98
/* Adapted from yelp-toc-pager.c */
100
xml_get_and_trim_names (xmlNodePtr node)
103
xmlChar *keep_lang = NULL;
105
int j, keep_pri = INT_MAX;
107
const gchar * const * langs = g_get_language_names ();
111
for (cur = node->children; cur; cur = cur->next) {
112
if (! xmlStrcmp (cur->name, GVC_SOUND_NAME)) {
113
xmlChar *cur_lang = NULL;
114
int cur_pri = INT_MAX;
116
cur_lang = xmlNodeGetLang (cur);
119
for (j = 0; langs[j]; j++) {
120
if (g_str_equal (cur_lang, langs[j])) {
126
cur_pri = INT_MAX - 1;
129
if (cur_pri <= keep_pri) {
135
value = xmlNodeGetContent (cur);
137
keep_lang = cur_lang;
146
/* Delete all GVC_SOUND_NAME nodes */
147
cur = node->children;
149
xmlNodePtr this = cur;
151
if (! xmlStrcmp (this->name, GVC_SOUND_NAME)) {
152
xmlUnlinkNode (this);
161
populate_model_from_node (GvcSoundThemeChooser *chooser,
170
name = xml_get_and_trim_names (node);
171
for (child = node->children; child; child = child->next) {
172
if (xmlNodeIsText (child)) {
176
if (xmlStrcmp (child->name, GVC_SOUND_FILENAME) == 0) {
177
filename = xmlNodeGetContent (child);
178
} else if (xmlStrcmp (child->name, GVC_SOUND_NAME) == 0) {
179
/* EH? should have been trimmed */
183
if (filename != NULL && name != NULL) {
184
gtk_list_store_insert_with_values (GTK_LIST_STORE (model),
187
ALERT_IDENTIFIER_COL, filename,
188
ALERT_DISPLAY_COL, name,
189
ALERT_SOUND_TYPE_COL, _("Built-in"),
198
populate_model_from_file (GvcSoundThemeChooser *chooser,
200
const char *filename)
207
exists = g_file_test (filename, G_FILE_TEST_EXISTS);
212
doc = xmlParseFile (filename);
217
root = xmlDocGetRootElement (doc);
219
for (child = root->children; child; child = child->next) {
220
if (xmlNodeIsText (child)) {
223
if (xmlStrcmp (child->name, GVC_SOUND_SOUND) != 0) {
227
populate_model_from_node (chooser, model, child);
234
populate_model_from_dir (GvcSoundThemeChooser *chooser,
241
d = g_dir_open (dirname, 0, NULL);
246
while ((name = g_dir_read_name (d)) != NULL) {
249
if (! g_str_has_suffix (name, ".xml")) {
253
path = g_build_filename (dirname, name, NULL);
254
populate_model_from_file (chooser, model, path);
260
save_alert_sounds (GvcSoundThemeChooser *chooser,
263
const char *sounds[3] = { "bell-terminal", "bell-window-system", NULL };
266
if (strcmp (id, DEFAULT_ALERT_ID) == 0) {
267
delete_old_files (sounds);
268
delete_disabled_files (sounds);
270
delete_old_files (sounds);
271
delete_disabled_files (sounds);
272
add_custom_file (sounds, id);
275
/* And poke the directory so the theme gets updated */
276
path = custom_theme_dir_path (NULL);
277
if (utime (path, NULL) != 0) {
278
g_warning ("Failed to update mtime for directory '%s': %s",
279
path, g_strerror (errno));
288
update_alert_model (GvcSoundThemeChooser *chooser,
294
model = gtk_tree_view_get_model (GTK_TREE_VIEW (chooser->priv->treeview));
295
gtk_tree_model_get_iter_first (model, &iter);
299
gtk_tree_model_get (model, &iter,
300
ALERT_IDENTIFIER_COL, &this_id,
303
if (strcmp (this_id, id) == 0) {
304
GtkTreeSelection *selection;
306
selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (chooser->priv->treeview));
307
gtk_tree_selection_select_iter (selection, &iter);
311
} while (gtk_tree_model_iter_next (model, &iter));
315
save_theme_name (GvcSoundThemeChooser *chooser,
316
const char *theme_name)
318
/* If the name is empty, use "freedesktop" */
319
if (theme_name == NULL || *theme_name == '\0') {
320
theme_name = DEFAULT_THEME;
323
/* special case for no sounds */
324
if (strcmp (theme_name, NO_SOUNDS_THEME_NAME) == 0) {
325
g_settings_set_boolean (chooser->priv->sound_settings, EVENT_SOUNDS_KEY, FALSE);
328
g_settings_set_boolean (chooser->priv->sound_settings, EVENT_SOUNDS_KEY, TRUE);
331
g_settings_set_string (chooser->priv->sound_settings, SOUND_THEME_KEY, theme_name);
335
load_theme_file (const char *path,
341
file = g_key_file_new ();
342
if (g_key_file_load_from_file (file, path, G_KEY_FILE_KEEP_TRANSLATIONS, NULL) == FALSE) {
343
g_key_file_free (file);
346
/* Don't add hidden themes to the list */
347
hidden = g_key_file_get_boolean (file, "Sound Theme", "Hidden", NULL);
349
/* Save the parent theme, if there's one */
350
if (parent != NULL) {
351
*parent = g_key_file_get_string (file,
358
g_key_file_free (file);
364
load_theme_name (const char *name,
367
const char * const *data_dirs;
368
const char *data_dir;
373
data_dir = g_get_user_data_dir ();
374
path = g_build_filename (data_dir, "sounds", name, "index.theme", NULL);
375
res = load_theme_file (path, parent);
380
data_dirs = g_get_system_data_dirs ();
381
for (i = 0; data_dirs[i] != NULL; i++) {
382
path = g_build_filename (data_dirs[i], "sounds", name, "index.theme", NULL);
383
res = load_theme_file (path, parent);
393
update_alert (GvcSoundThemeChooser *chooser,
394
const char *alert_id)
399
gboolean remove_custom;
401
is_custom = strcmp (chooser->priv->current_theme, CUSTOM_THEME_NAME) == 0;
402
is_default = strcmp (alert_id, DEFAULT_ALERT_ID) == 0;
404
/* So a few possibilities:
405
* 1. Named theme, default alert selected: noop
406
* 2. Named theme, alternate alert selected: create new custom with sound
407
* 3. Custom theme, default alert selected: remove sound and possibly custom
408
* 4. Custom theme, alternate alert selected: update custom sound
411
remove_custom = FALSE;
412
if (! is_custom && is_default) {
413
/* remove custom just in case */
414
remove_custom = TRUE;
415
} else if (! is_custom && ! is_default) {
416
if (chooser->priv->current_parent)
417
create_custom_theme (chooser->priv->current_parent);
419
create_custom_theme (DEFAULT_THEME);
420
save_alert_sounds (chooser, alert_id);
422
} else if (is_custom && is_default) {
423
save_alert_sounds (chooser, alert_id);
424
/* after removing files check if it is empty */
425
if (custom_theme_dir_is_empty ()) {
426
remove_custom = TRUE;
428
} else if (is_custom && ! is_default) {
429
save_alert_sounds (chooser, alert_id);
433
save_theme_name (chooser, CUSTOM_THEME_NAME);
434
} else if (remove_custom) {
435
delete_custom_theme_dir ();
437
save_theme_name (chooser, chooser->priv->current_parent);
441
update_alert_model (chooser, alert_id);
445
play_preview_for_id (GvcSoundThemeChooser *chooser,
448
g_return_if_fail (id != NULL);
450
/* special case: for the default item on custom themes
451
* play the alert for the parent theme */
452
if (strcmp (id, DEFAULT_ALERT_ID) == 0) {
453
if (chooser->priv->current_parent != NULL) {
454
ca_gtk_play_for_widget (GTK_WIDGET (chooser), 0,
455
CA_PROP_APPLICATION_NAME, _("Sound Preferences"),
456
CA_PROP_EVENT_ID, "bell-window-system",
457
CA_PROP_CANBERRA_XDG_THEME_NAME, chooser->priv->current_parent,
458
CA_PROP_EVENT_DESCRIPTION, _("Testing event sound"),
459
CA_PROP_CANBERRA_CACHE_CONTROL, "never",
460
CA_PROP_APPLICATION_ID, "org.gnome.VolumeControl",
461
#ifdef CA_PROP_CANBERRA_ENABLE
462
CA_PROP_CANBERRA_ENABLE, "1",
466
ca_gtk_play_for_widget (GTK_WIDGET (chooser), 0,
467
CA_PROP_APPLICATION_NAME, _("Sound Preferences"),
468
CA_PROP_EVENT_ID, "bell-window-system",
469
CA_PROP_EVENT_DESCRIPTION, _("Testing event sound"),
470
CA_PROP_CANBERRA_CACHE_CONTROL, "never",
471
CA_PROP_APPLICATION_ID, "org.gnome.VolumeControl",
472
#ifdef CA_PROP_CANBERRA_ENABLE
473
CA_PROP_CANBERRA_ENABLE, "1",
478
ca_gtk_play_for_widget (GTK_WIDGET (chooser), 0,
479
CA_PROP_APPLICATION_NAME, _("Sound Preferences"),
480
CA_PROP_MEDIA_FILENAME, id,
481
CA_PROP_EVENT_DESCRIPTION, _("Testing event sound"),
482
CA_PROP_CANBERRA_CACHE_CONTROL, "never",
483
CA_PROP_APPLICATION_ID, "org.gnome.VolumeControl",
484
#ifdef CA_PROP_CANBERRA_ENABLE
485
CA_PROP_CANBERRA_ENABLE, "1",
493
on_treeview_selection_changed (GtkTreeSelection *selection,
494
GvcSoundThemeChooser *chooser)
500
if (chooser->priv->treeview == NULL) {
504
model = gtk_tree_view_get_model (GTK_TREE_VIEW (chooser->priv->treeview));
506
if (gtk_tree_selection_get_selected (selection, &model, &iter) == FALSE) {
511
gtk_tree_model_get (model, &iter,
512
ALERT_IDENTIFIER_COL, &id,
518
play_preview_for_id (chooser, id);
519
update_alert (chooser, id);
524
on_treeview_button_pressed (GtkTreeView *treeview,
525
GdkEventButton *event,
526
GvcSoundThemeChooser *chooser)
528
GtkTreeSelection *selection;
531
selection = gtk_tree_view_get_selection (treeview);
532
if (gtk_tree_view_get_path_at_pos (GTK_TREE_VIEW (treeview),
533
event->x, event->y, &path, NULL, NULL, NULL) == FALSE) {
537
if (gtk_tree_selection_path_is_selected (selection, path) == FALSE) {
538
gtk_tree_path_free (path);
541
gtk_tree_path_free (path);
543
on_treeview_selection_changed (selection, chooser);
549
create_alert_treeview (GvcSoundThemeChooser *chooser)
553
GtkCellRenderer *renderer;
554
GtkTreeViewColumn *column;
555
GtkTreeSelection *selection;
557
treeview = gtk_tree_view_new ();
558
gtk_tree_view_set_headers_visible (GTK_TREE_VIEW (treeview), FALSE);
559
g_signal_connect (treeview,
560
"button-press-event",
561
G_CALLBACK (on_treeview_button_pressed),
564
selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (treeview));
565
gtk_tree_selection_set_mode (selection, GTK_SELECTION_SINGLE);
566
g_signal_connect (selection,
568
G_CALLBACK (on_treeview_selection_changed),
571
/* Setup the tree model, 3 columns:
576
store = gtk_list_store_new (ALERT_NUM_COLS,
581
gtk_list_store_insert_with_values (store,
584
ALERT_IDENTIFIER_COL, DEFAULT_ALERT_ID,
585
ALERT_DISPLAY_COL, _("Default"),
586
ALERT_SOUND_TYPE_COL, _("From theme"),
589
populate_model_from_dir (chooser, GTK_TREE_MODEL (store), SOUND_SET_DIR);
591
gtk_tree_view_set_model (GTK_TREE_VIEW (treeview),
592
GTK_TREE_MODEL (store));
594
renderer = gtk_cell_renderer_text_new ();
595
column = gtk_tree_view_column_new_with_attributes (_("Name"),
597
"text", ALERT_DISPLAY_COL,
599
gtk_tree_view_append_column (GTK_TREE_VIEW (treeview), column);
605
get_file_type (const char *sound_name,
608
char *name, *filename;
612
name = g_strdup_printf ("%s.disabled", sound_name);
613
filename = custom_theme_dir_path (name);
616
if (g_file_test (filename, G_FILE_TEST_IS_REGULAR) != FALSE) {
618
return SOUND_TYPE_OFF;
622
/* We only check for .ogg files because those are the
623
* only ones we create */
624
name = g_strdup_printf ("%s.ogg", sound_name);
625
filename = custom_theme_dir_path (name);
628
if (g_file_test (filename, G_FILE_TEST_IS_SYMLINK) != FALSE) {
629
*linked_name = g_file_read_link (filename, NULL);
631
return SOUND_TYPE_CUSTOM;
635
return SOUND_TYPE_BUILTIN;
639
update_alerts_from_theme_name (GvcSoundThemeChooser *chooser,
642
if (strcmp (name, CUSTOM_THEME_NAME) != 0) {
643
/* reset alert to default */
644
update_alert (chooser, DEFAULT_ALERT_ID);
650
sound_type = get_file_type ("bell-terminal", &linkname);
651
g_debug ("Found link: %s", linkname);
652
if (sound_type == SOUND_TYPE_CUSTOM) {
653
update_alert (chooser, linkname);
659
update_theme (GvcSoundThemeChooser *chooser)
661
gboolean events_enabled;
664
events_enabled = g_settings_get_boolean (chooser->priv->sound_settings, EVENT_SOUNDS_KEY);
666
last_theme = chooser->priv->current_theme;
667
if (events_enabled) {
668
chooser->priv->current_theme = g_settings_get_string (chooser->priv->sound_settings, SOUND_THEME_KEY);
670
chooser->priv->current_theme = g_strdup (NO_SOUNDS_THEME_NAME);
673
if (g_strcmp0 (last_theme, chooser->priv->current_theme) != 0) {
674
g_free (chooser->priv->current_parent);
675
if (load_theme_name (chooser->priv->current_theme,
676
&chooser->priv->current_parent) == FALSE) {
677
g_free (chooser->priv->current_theme);
678
chooser->priv->current_theme = g_strdup (DEFAULT_THEME);
679
load_theme_name (DEFAULT_THEME,
680
&chooser->priv->current_parent);
685
gtk_widget_set_sensitive (chooser->priv->selection_box, events_enabled);
687
update_alerts_from_theme_name (chooser, chooser->priv->current_theme);
691
gvc_sound_theme_chooser_constructor (GType type,
692
guint n_construct_properties,
693
GObjectConstructParam *construct_params)
696
GvcSoundThemeChooser *self;
698
object = G_OBJECT_CLASS (gvc_sound_theme_chooser_parent_class)->constructor (type, n_construct_properties, construct_params);
700
self = GVC_SOUND_THEME_CHOOSER (object);
708
gvc_sound_theme_chooser_class_init (GvcSoundThemeChooserClass *klass)
710
GObjectClass *object_class = G_OBJECT_CLASS (klass);
712
object_class->constructor = gvc_sound_theme_chooser_constructor;
713
object_class->finalize = gvc_sound_theme_chooser_finalize;
715
g_type_class_add_private (klass, sizeof (GvcSoundThemeChooserPrivate));
719
on_sound_settings_changed (GSettings *settings,
721
GvcSoundThemeChooser *chooser)
723
if (strcmp (key, EVENT_SOUNDS_KEY) == 0) {
724
update_theme (chooser);
725
} else if (strcmp (key, SOUND_THEME_KEY) == 0) {
726
update_theme (chooser);
727
} else if (strcmp (key, INPUT_SOUNDS_KEY) == 0) {
728
update_theme (chooser);
733
on_audible_bell_changed (GSettings *settings,
735
GvcSoundThemeChooser *chooser)
737
update_theme (chooser);
741
setup_list_size_constraint (GtkWidget *widget,
747
/* constrain height to be the tree height up to a max */
748
max_height = (gdk_screen_get_height (gtk_widget_get_screen (widget))) / 4;
750
gtk_widget_get_preferred_size (to_size, NULL, &req);
752
gtk_scrolled_window_set_min_content_height (GTK_SCROLLED_WINDOW (widget),
753
MIN (req.height, max_height));
757
gvc_sound_theme_chooser_init (GvcSoundThemeChooser *chooser)
761
GtkWidget *scrolled_window;
762
GtkWidget *alignment;
765
chooser->priv = GVC_SOUND_THEME_CHOOSER_GET_PRIVATE (chooser);
767
chooser->priv->settings = g_settings_new (WM_SCHEMA);
768
chooser->priv->sound_settings = g_settings_new (KEY_SOUNDS_SCHEMA);
770
str = g_strdup_printf ("<b>%s</b>", _("C_hoose an alert sound:"));
771
chooser->priv->selection_box = box = gtk_frame_new (str);
773
label = gtk_frame_get_label_widget (GTK_FRAME (box));
774
gtk_label_set_use_underline (GTK_LABEL (label), TRUE);
775
gtk_label_set_use_markup (GTK_LABEL (label), TRUE);
776
gtk_frame_set_shadow_type (GTK_FRAME (box), GTK_SHADOW_NONE);
778
alignment = gtk_alignment_new (0, 0, 1, 1);
779
gtk_alignment_set_padding (GTK_ALIGNMENT (alignment), 6, 0, 0, 0);
780
gtk_container_add (GTK_CONTAINER (alignment), box);
781
gtk_box_pack_start (GTK_BOX (chooser), alignment, TRUE, TRUE, 6);
783
alignment = gtk_alignment_new (0, 0, 1, 1);
784
gtk_alignment_set_padding (GTK_ALIGNMENT (alignment), 6, 0, 0, 0);
785
gtk_container_add (GTK_CONTAINER (box), alignment);
787
chooser->priv->treeview = create_alert_treeview (chooser);
788
gtk_label_set_mnemonic_widget (GTK_LABEL (label), chooser->priv->treeview);
790
scrolled_window = gtk_scrolled_window_new (NULL, NULL);
791
setup_list_size_constraint (scrolled_window, chooser->priv->treeview);
793
gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (scrolled_window),
795
GTK_POLICY_AUTOMATIC);
796
gtk_scrolled_window_set_shadow_type (GTK_SCROLLED_WINDOW (scrolled_window),
798
gtk_container_add (GTK_CONTAINER (scrolled_window), chooser->priv->treeview);
799
gtk_container_add (GTK_CONTAINER (alignment), scrolled_window);
801
g_signal_connect (G_OBJECT (chooser->priv->sound_settings), "changed",
802
G_CALLBACK (on_sound_settings_changed), chooser);
803
g_signal_connect (chooser->priv->settings, "changed::" AUDIO_BELL_KEY,
804
G_CALLBACK (on_audible_bell_changed), chooser);
808
gvc_sound_theme_chooser_finalize (GObject *object)
810
GvcSoundThemeChooser *sound_theme_chooser;
812
g_return_if_fail (object != NULL);
813
g_return_if_fail (GVC_IS_SOUND_THEME_CHOOSER (object));
815
sound_theme_chooser = GVC_SOUND_THEME_CHOOSER (object);
817
if (sound_theme_chooser->priv != NULL) {
818
g_object_unref (sound_theme_chooser->priv->settings);
819
g_object_unref (sound_theme_chooser->priv->sound_settings);
822
G_OBJECT_CLASS (gvc_sound_theme_chooser_parent_class)->finalize (object);
826
gvc_sound_theme_chooser_new (void)
829
chooser = g_object_new (GVC_TYPE_SOUND_THEME_CHOOSER,
832
return GTK_WIDGET (chooser);