[darcs-to-svn @ initial import]
[empathy.git] / libempathy-gtk / gossip-preferences.c
1 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
2 /*
3  * Copyright (C) 2003-2007 Imendio AB
4  *
5  * This program is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU General Public License as
7  * published by the Free Software Foundation; either version 2 of the
8  * License, or (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public
16  * License along with this program; if not, write to the
17  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18  * Boston, MA 02111-1307, USA.
19  *
20  * Authors: Mikael Hallendal <micke@imendio.com>
21  *          Richard Hult <richard@imendio.com>
22  *          Martyn Russell <martyn@imendio.com>
23  */
24
25 #include "config.h"
26
27 #include <string.h>
28
29 #include <gtk/gtk.h>
30 #include <glade/glade.h>
31 #include <glib/gi18n.h>
32
33 #include <libempathy/gossip-conf.h>
34
35 #include "gossip-preferences.h"
36 #include "gossip-stock.h"
37 #include "gossip-ui-utils.h"
38 #include "gossip-theme-manager.h"
39 #include "gossip-spell.h"
40
41 typedef struct {
42         GtkWidget *dialog;
43
44         GtkWidget *notebook;
45
46         GtkWidget *checkbutton_show_avatars;
47         GtkWidget *checkbutton_compact_contact_list;
48         GtkWidget *checkbutton_show_smileys;
49         GtkWidget *combobox_chat_theme;
50         GtkWidget *checkbutton_theme_chat_room;
51         GtkWidget *checkbutton_separate_chat_windows;
52
53         GtkWidget *checkbutton_sounds_for_messages;
54         GtkWidget *checkbutton_sounds_when_busy;
55         GtkWidget *checkbutton_sounds_when_away;
56         GtkWidget *checkbutton_popups_when_available;
57
58         GtkWidget *treeview_spell_checker;
59         GtkWidget *checkbutton_spell_checker;
60
61         GList     *notify_ids;
62 } GossipPreferences;
63
64 static void     preferences_setup_widgets                (GossipPreferences      *preferences);
65 static void     preferences_languages_setup              (GossipPreferences      *preferences);
66 static void     preferences_languages_add                (GossipPreferences      *preferences);
67 static void     preferences_languages_save               (GossipPreferences      *preferences);
68 static gboolean preferences_languages_save_foreach       (GtkTreeModel           *model,
69                                                           GtkTreePath            *path,
70                                                           GtkTreeIter            *iter,
71                                                           gchar                 **languages);
72 static void     preferences_languages_load               (GossipPreferences      *preferences);
73 static gboolean preferences_languages_load_foreach       (GtkTreeModel           *model,
74                                                           GtkTreePath            *path,
75                                                           GtkTreeIter            *iter,
76                                                           gchar                 **languages);
77 static void     preferences_languages_cell_toggled_cb    (GtkCellRendererToggle  *cell,
78                                                           gchar                  *path_string,
79                                                           GossipPreferences      *preferences);
80 static void     preferences_themes_setup                 (GossipPreferences      *preferences);
81 static void     preferences_widget_sync_bool             (const gchar            *key,
82                                                           GtkWidget              *widget);
83 static void     preferences_widget_sync_int              (const gchar            *key,
84                                                           GtkWidget              *widget);
85 static void     preferences_widget_sync_string           (const gchar            *key,
86                                                           GtkWidget              *widget);
87 static void     preferences_widget_sync_string_combo     (const gchar            *key,
88                                                           GtkWidget              *widget);
89 static void     preferences_notify_int_cb                (GossipConf             *conf,
90                                                           const gchar            *key,
91                                                           gpointer                user_data);
92 static void     preferences_notify_string_cb             (GossipConf             *conf,
93                                                           const gchar            *key,
94                                                           gpointer                user_data);
95 static void     preferences_notify_string_combo_cb       (GossipConf             *conf,
96                                                           const gchar            *key,
97                                                           gpointer                user_data);
98 static void     preferences_notify_bool_cb               (GossipConf             *conf,
99                                                           const gchar            *key,
100                                                           gpointer                user_data);
101 static void     preferences_notify_sensitivity_cb        (GossipConf             *conf,
102                                                           const gchar            *key,
103                                                           gpointer                user_data);
104 static void     preferences_hookup_spin_button           (GossipPreferences      *preferences,
105                                                           const gchar            *key,
106                                                           GtkWidget              *widget);
107 static void     preferences_hookup_entry                 (GossipPreferences      *preferences,
108                                                           const gchar            *key,
109                                                           GtkWidget              *widget);
110 static void     preferences_hookup_toggle_button         (GossipPreferences      *preferences,
111                                                           const gchar            *key,
112                                                           GtkWidget              *widget);
113 static void     preferences_hookup_string_combo          (GossipPreferences      *preferences,
114                                                           const gchar            *key,
115                                                           GtkWidget              *widget);
116 static void     preferences_hookup_sensitivity           (GossipPreferences      *preferences,
117                                                           const gchar            *key,
118                                                           GtkWidget              *widget);
119 static void     preferences_spin_button_value_changed_cb (GtkWidget              *button,
120                                                           gpointer                user_data);
121 static void     preferences_entry_value_changed_cb       (GtkWidget              *entry,
122                                                           gpointer                user_data);
123 static void     preferences_toggle_button_toggled_cb     (GtkWidget              *button,
124                                                           gpointer                user_data);
125 static void     preferences_string_combo_changed_cb      (GtkWidget *button,
126                                                           gpointer                user_data);
127 static void     preferences_destroy_cb                   (GtkWidget              *widget,
128                                                           GossipPreferences      *preferences);
129 static void     preferences_response_cb                  (GtkWidget              *widget,
130                                                           gint                    response,
131                                                           GossipPreferences      *preferences);
132
133 enum {
134         COL_LANG_ENABLED,
135         COL_LANG_CODE,
136         COL_LANG_NAME,
137         COL_LANG_COUNT
138 };
139
140 enum {
141         COL_COMBO_VISIBLE_NAME,
142         COL_COMBO_NAME,
143         COL_COMBO_COUNT
144 };
145
146 static void
147 preferences_setup_widgets (GossipPreferences *preferences)
148 {
149         preferences_hookup_toggle_button (preferences,
150                                           GOSSIP_PREFS_SOUNDS_FOR_MESSAGES,
151                                           preferences->checkbutton_sounds_for_messages);
152         preferences_hookup_toggle_button (preferences,
153                                           GOSSIP_PREFS_SOUNDS_WHEN_AWAY,
154                                           preferences->checkbutton_sounds_when_away);
155         preferences_hookup_toggle_button (preferences,
156                                           GOSSIP_PREFS_SOUNDS_WHEN_BUSY,
157                                           preferences->checkbutton_sounds_when_busy);
158         preferences_hookup_toggle_button (preferences,
159                                           GOSSIP_PREFS_POPUPS_WHEN_AVAILABLE,
160                                           preferences->checkbutton_popups_when_available);
161
162         preferences_hookup_sensitivity (preferences,
163                                         GOSSIP_PREFS_SOUNDS_FOR_MESSAGES,
164                                         preferences->checkbutton_sounds_when_away);
165         preferences_hookup_sensitivity (preferences,
166                                         GOSSIP_PREFS_SOUNDS_FOR_MESSAGES,
167                                         preferences->checkbutton_sounds_when_busy);
168
169         preferences_hookup_toggle_button (preferences,
170                                           GOSSIP_PREFS_UI_SEPARATE_CHAT_WINDOWS,
171                                           preferences->checkbutton_separate_chat_windows);
172
173         preferences_hookup_toggle_button (preferences,
174                                           GOSSIP_PREFS_UI_SHOW_AVATARS,
175                                           preferences->checkbutton_show_avatars);
176
177         preferences_hookup_toggle_button (preferences,
178                                           GOSSIP_PREFS_UI_COMPACT_CONTACT_LIST,
179                                           preferences->checkbutton_compact_contact_list);
180
181         preferences_hookup_toggle_button (preferences,
182                                           GOSSIP_PREFS_CHAT_SHOW_SMILEYS,
183                                           preferences->checkbutton_show_smileys);
184
185         preferences_hookup_string_combo (preferences,
186                                          GOSSIP_PREFS_CHAT_THEME,
187                                          preferences->combobox_chat_theme);
188
189         preferences_hookup_toggle_button (preferences,
190                                           GOSSIP_PREFS_CHAT_THEME_CHAT_ROOM,
191                                           preferences->checkbutton_theme_chat_room);
192
193         preferences_hookup_toggle_button (preferences,
194                                           GOSSIP_PREFS_CHAT_SPELL_CHECKER_ENABLED,
195                                           preferences->checkbutton_spell_checker);
196         preferences_hookup_sensitivity (preferences,
197                                         GOSSIP_PREFS_CHAT_SPELL_CHECKER_ENABLED,
198                                         preferences->treeview_spell_checker);
199 }
200
201 static void
202 preferences_languages_setup (GossipPreferences *preferences)
203 {
204         GtkTreeView       *view;
205         GtkListStore      *store;
206         GtkTreeSelection  *selection;
207         GtkTreeModel      *model;
208         GtkTreeViewColumn *column;
209         GtkCellRenderer   *renderer;
210         guint              col_offset;
211
212         view = GTK_TREE_VIEW (preferences->treeview_spell_checker);
213
214         store = gtk_list_store_new (COL_LANG_COUNT,
215                                     G_TYPE_BOOLEAN,  /* enabled */
216                                     G_TYPE_STRING,   /* code */
217                                     G_TYPE_STRING);  /* name */
218
219         gtk_tree_view_set_model (view, GTK_TREE_MODEL (store));
220
221         selection = gtk_tree_view_get_selection (view);
222         gtk_tree_selection_set_mode (selection, GTK_SELECTION_SINGLE);
223
224         model = GTK_TREE_MODEL (store);
225
226         renderer = gtk_cell_renderer_toggle_new ();
227         g_signal_connect (renderer, "toggled",
228                           G_CALLBACK (preferences_languages_cell_toggled_cb),
229                           preferences);
230
231         column = gtk_tree_view_column_new_with_attributes (NULL, renderer,
232                                                            "active", COL_LANG_ENABLED,
233                                                            NULL);
234
235         gtk_tree_view_append_column (view, column);
236
237         renderer = gtk_cell_renderer_text_new ();
238         col_offset = gtk_tree_view_insert_column_with_attributes (view,
239                                                                   -1, _("Language"),
240                                                                   renderer,
241                                                                   "text", COL_LANG_NAME,
242                                                                   NULL);
243
244         g_object_set_data (G_OBJECT (renderer),
245                            "column", GINT_TO_POINTER (COL_LANG_NAME));
246
247         column = gtk_tree_view_get_column (view, col_offset - 1);
248         gtk_tree_view_column_set_sort_column_id (column, COL_LANG_NAME);
249         gtk_tree_view_column_set_resizable (column, FALSE);
250         gtk_tree_view_column_set_clickable (GTK_TREE_VIEW_COLUMN (column), TRUE);
251
252         g_object_unref (store);
253 }
254
255 static void
256 preferences_languages_add (GossipPreferences *preferences)
257 {
258         GtkTreeView  *view;
259         GtkListStore *store;
260         GList        *codes, *l;
261
262         view = GTK_TREE_VIEW (preferences->treeview_spell_checker);
263         store = GTK_LIST_STORE (gtk_tree_view_get_model (view));
264
265         codes = gossip_spell_get_language_codes ();
266         for (l = codes; l; l = l->next) {
267                 GtkTreeIter  iter;
268                 const gchar *code;
269                 const gchar *name;
270
271                 code = l->data;
272                 name = gossip_spell_get_language_name (code);
273                 if (!name) {
274                         continue;
275                 }
276
277                 gtk_list_store_append (store, &iter);
278                 gtk_list_store_set (store, &iter,
279                                     COL_LANG_CODE, code,
280                                     COL_LANG_NAME, name,
281                                     -1);
282         }
283
284         gossip_spell_free_language_codes (codes);
285 }
286
287 static void
288 preferences_languages_save (GossipPreferences *preferences)
289 {
290         GtkTreeView       *view;
291         GtkTreeModel      *model;
292
293         gchar             *languages = NULL;
294
295         view = GTK_TREE_VIEW (preferences->treeview_spell_checker);
296         model = gtk_tree_view_get_model (view);
297
298         gtk_tree_model_foreach (model,
299                                 (GtkTreeModelForeachFunc) preferences_languages_save_foreach,
300                                 &languages);
301
302         if (!languages) {
303                 /* Default to english */
304                 languages = g_strdup ("en");
305         }
306
307         gossip_conf_set_string (gossip_conf_get (),
308                                  GOSSIP_PREFS_CHAT_SPELL_CHECKER_LANGUAGES,
309                                  languages);
310
311         g_free (languages);
312 }
313
314 static gboolean
315 preferences_languages_save_foreach (GtkTreeModel  *model,
316                                     GtkTreePath   *path,
317                                     GtkTreeIter   *iter,
318                                     gchar        **languages)
319 {
320         gboolean  enabled;
321         gchar    *code;
322
323         if (!languages) {
324                 return TRUE;
325         }
326
327         gtk_tree_model_get (model, iter, COL_LANG_ENABLED, &enabled, -1);
328         if (!enabled) {
329                 return FALSE;
330         }
331
332         gtk_tree_model_get (model, iter, COL_LANG_CODE, &code, -1);
333         if (!code) {
334                 return FALSE;
335         }
336
337         if (!(*languages)) {
338                 *languages = g_strdup (code);
339         } else {
340                 gchar *str = *languages;
341                 *languages = g_strdup_printf ("%s,%s", str, code);
342                 g_free (str);
343         }
344
345         g_free (code);
346
347         return FALSE;
348 }
349
350 static void
351 preferences_languages_load (GossipPreferences *preferences)
352 {
353         GtkTreeView   *view;
354         GtkTreeModel  *model;
355         gchar         *value;
356         gchar        **vlanguages;
357
358         if (!gossip_conf_get_string (gossip_conf_get (),
359                                       GOSSIP_PREFS_CHAT_SPELL_CHECKER_LANGUAGES,
360                                       &value) || !value) {
361                 return;
362         }
363
364         vlanguages = g_strsplit (value, ",", -1);
365         g_free (value);
366
367         view = GTK_TREE_VIEW (preferences->treeview_spell_checker);
368         model = gtk_tree_view_get_model (view);
369
370         gtk_tree_model_foreach (model,
371                                 (GtkTreeModelForeachFunc) preferences_languages_load_foreach,
372                                 vlanguages);
373
374         g_strfreev (vlanguages);
375 }
376
377 static gboolean
378 preferences_languages_load_foreach (GtkTreeModel  *model,
379                                     GtkTreePath   *path,
380                                     GtkTreeIter   *iter,
381                                     gchar        **languages)
382 {
383         gchar    *code;
384         gchar    *lang;
385         gint      i;
386         gboolean  found = FALSE;
387
388         if (!languages) {
389                 return TRUE;
390         }
391
392         gtk_tree_model_get (model, iter, COL_LANG_CODE, &code, -1);
393         if (!code) {
394                 return FALSE;
395         }
396
397         for (i = 0, lang = languages[i]; lang; lang = languages[++i]) {
398                 if (strcmp (lang, code) == 0) {
399                         found = TRUE;
400                 }
401         }
402
403         gtk_list_store_set (GTK_LIST_STORE (model), iter, COL_LANG_ENABLED, found, -1);
404         return FALSE;
405 }
406
407 static void
408 preferences_languages_cell_toggled_cb (GtkCellRendererToggle *cell,
409                                        gchar                 *path_string,
410                                        GossipPreferences     *preferences)
411 {
412         GtkTreeView  *view;
413         GtkTreeModel *model;
414         GtkListStore *store;
415         GtkTreePath  *path;
416         GtkTreeIter   iter;
417         gboolean      enabled;
418
419         view = GTK_TREE_VIEW (preferences->treeview_spell_checker);
420         model = gtk_tree_view_get_model (view);
421         store = GTK_LIST_STORE (model);
422
423         path = gtk_tree_path_new_from_string (path_string);
424
425         gtk_tree_model_get_iter (model, &iter, path);
426         gtk_tree_model_get (model, &iter, COL_LANG_ENABLED, &enabled, -1);
427
428         enabled ^= 1;
429
430         gtk_list_store_set (store, &iter, COL_LANG_ENABLED, enabled, -1);
431         gtk_tree_path_free (path);
432
433         preferences_languages_save (preferences);
434 }
435
436 static void
437 preferences_themes_setup (GossipPreferences *preferences)
438 {
439         GtkComboBox   *combo;
440         GtkListStore  *model;
441         GtkTreeIter    iter;
442         const gchar  **themes;
443         gint           i;
444
445         combo = GTK_COMBO_BOX (preferences->combobox_chat_theme);
446
447         model = gtk_list_store_new (COL_COMBO_COUNT,
448                                     G_TYPE_STRING,
449                                     G_TYPE_STRING);
450
451         themes = gossip_theme_manager_get_themes ();
452         for (i = 0; themes[i]; i += 2) {
453                 gtk_list_store_append (model, &iter);
454                 gtk_list_store_set (model, &iter,
455                                     COL_COMBO_VISIBLE_NAME, _(themes[i + 1]),
456                                     COL_COMBO_NAME, themes[i],
457                                     -1);
458         }
459
460         gtk_combo_box_set_model (combo, GTK_TREE_MODEL (model));
461         g_object_unref (model);
462 }
463
464 static void
465 preferences_widget_sync_bool (const gchar *key, GtkWidget *widget)
466 {
467         gboolean value;
468
469         if (gossip_conf_get_bool (gossip_conf_get (), key, &value)) {
470                 gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (widget), value);
471         }
472 }
473
474 static void
475 preferences_widget_sync_int (const gchar *key, GtkWidget *widget)
476 {
477         gint value;
478
479         if (gossip_conf_get_int (gossip_conf_get (), key, &value)) {
480                 gtk_spin_button_set_value (GTK_SPIN_BUTTON (widget), value);
481         }
482 }
483
484 static void
485 preferences_widget_sync_string (const gchar *key, GtkWidget *widget)
486 {
487         gchar *value;
488
489         if (gossip_conf_get_string (gossip_conf_get (), key, &value) && value) {
490                 gtk_entry_set_text (GTK_ENTRY (widget), value);
491                 g_free (value);
492         }
493 }
494
495 static void
496 preferences_widget_sync_string_combo (const gchar *key, GtkWidget *widget)
497 {
498         gchar        *value;
499         GtkTreeModel *model;
500         GtkTreeIter   iter;
501         gboolean      found;
502
503         if (!gossip_conf_get_string (gossip_conf_get (), key, &value)) {
504                 return;
505         }
506
507         model = gtk_combo_box_get_model (GTK_COMBO_BOX (widget));
508
509         found = FALSE;
510         if (value && gtk_tree_model_get_iter_first (model, &iter)) {
511                 gchar *name;
512
513                 do {
514                         gtk_tree_model_get (model, &iter,
515                                             COL_COMBO_NAME, &name,
516                                             -1);
517
518                         if (strcmp (name, value) == 0) {
519                                 found = TRUE;
520                                 gtk_combo_box_set_active_iter (GTK_COMBO_BOX (widget), &iter);
521                                 break;
522                         } else {
523                                 found = FALSE;
524                         }
525
526                         g_free (name);
527                 } while (gtk_tree_model_iter_next (model, &iter));
528         }
529
530         /* Fallback to the first one. */
531         if (!found) {
532                 if (gtk_tree_model_get_iter_first (model, &iter)) {
533                         gtk_combo_box_set_active_iter (GTK_COMBO_BOX (widget), &iter);
534                 }
535         }
536
537         g_free (value);
538 }
539
540 static void
541 preferences_notify_int_cb (GossipConf  *conf,
542                            const gchar *key,
543                            gpointer     user_data)
544 {
545         gint value;
546
547         if (gossip_conf_get_int (conf, key, &value)) {
548                 gtk_spin_button_set_value (GTK_SPIN_BUTTON (user_data), value);
549         }
550 }
551
552 static void
553 preferences_notify_string_cb (GossipConf  *conf,
554                               const gchar *key,
555                               gpointer     user_data)
556 {
557         gchar *value;
558
559         if (gossip_conf_get_string (conf, key, &value) && value) {
560                 gtk_entry_set_text (GTK_ENTRY (user_data), value);
561                 g_free (value);
562         }
563 }
564
565 static void
566 preferences_notify_string_combo_cb (GossipConf  *conf,
567                                     const gchar *key,
568                                     gpointer     user_data)
569 {
570         preferences_widget_sync_string_combo (key, user_data);
571 }
572
573 static void
574 preferences_notify_bool_cb (GossipConf  *conf,
575                             const gchar *key,
576                             gpointer     user_data)
577 {
578         preferences_widget_sync_bool (key, user_data);
579 /*
580         if (gossip_conf_get_bool (conf, key, &value)) {
581                 gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (user_data),
582                                               gconf_value_get_bool (value));
583                                               }
584 */
585 }
586
587 static void
588 preferences_notify_sensitivity_cb (GossipConf  *conf,
589                                    const gchar *key,
590                                    gpointer     user_data)
591 {
592         gboolean value;
593
594         if (gossip_conf_get_bool (conf, key, &value)) {
595                 gtk_widget_set_sensitive (GTK_WIDGET (user_data), value);
596         }
597 }
598
599 static void
600 preferences_add_id (GossipPreferences *preferences, guint id)
601 {
602         preferences->notify_ids = g_list_prepend (preferences->notify_ids,
603                                                   GUINT_TO_POINTER (id));
604 }
605
606 static void
607 preferences_hookup_spin_button (GossipPreferences *preferences,
608                                 const gchar       *key,
609                                 GtkWidget         *widget)
610 {
611         guint id;
612
613         /* Silence warning. */
614         if (0) {
615                 preferences_hookup_spin_button (preferences, key, widget);
616         }
617
618         preferences_widget_sync_int (key, widget);
619
620         g_object_set_data_full (G_OBJECT (widget), "key",
621                                 g_strdup (key), g_free);
622
623         g_signal_connect (widget,
624                           "value_changed",
625                           G_CALLBACK (preferences_spin_button_value_changed_cb),
626                           NULL);
627
628         id = gossip_conf_notify_add (gossip_conf_get (),
629                                       key,
630                                       preferences_notify_int_cb,
631                                       widget);
632         if (id) {
633                 preferences_add_id (preferences, id);
634         }
635 }
636
637 static void
638 preferences_hookup_entry (GossipPreferences *preferences,
639                           const gchar       *key,
640                           GtkWidget         *widget)
641 {
642         guint id;
643
644         if (0) {  /* Silent warning before we use this function. */
645                 preferences_hookup_entry (preferences, key, widget);
646         }
647
648         preferences_widget_sync_string (key, widget);
649
650         g_object_set_data_full (G_OBJECT (widget), "key",
651                                 g_strdup (key), g_free);
652
653         g_signal_connect (widget,
654                           "changed",
655                           G_CALLBACK (preferences_entry_value_changed_cb),
656                           NULL);
657
658         id = gossip_conf_notify_add (gossip_conf_get (),
659                                       key,
660                                       preferences_notify_string_cb,
661                                       widget);
662         if (id) {
663                 preferences_add_id (preferences, id);
664         }
665 }
666
667 static void
668 preferences_hookup_toggle_button (GossipPreferences *preferences,
669                                   const gchar       *key,
670                                   GtkWidget         *widget)
671 {
672         guint id;
673
674         preferences_widget_sync_bool (key, widget);
675
676         g_object_set_data_full (G_OBJECT (widget), "key",
677                                 g_strdup (key), g_free);
678
679         g_signal_connect (widget,
680                           "toggled",
681                           G_CALLBACK (preferences_toggle_button_toggled_cb),
682                           NULL);
683
684         id = gossip_conf_notify_add (gossip_conf_get (),
685                                       key,
686                                       preferences_notify_bool_cb,
687                                       widget);
688         if (id) {
689                 preferences_add_id (preferences, id);
690         }
691 }
692
693 static void
694 preferences_hookup_string_combo (GossipPreferences *preferences,
695                                  const gchar       *key,
696                                  GtkWidget         *widget)
697 {
698         guint id;
699
700         preferences_widget_sync_string_combo (key, widget);
701
702         g_object_set_data_full (G_OBJECT (widget), "key",
703                                 g_strdup (key), g_free);
704
705         g_signal_connect (widget,
706                           "changed",
707                           G_CALLBACK (preferences_string_combo_changed_cb),
708                           NULL);
709
710         id = gossip_conf_notify_add (gossip_conf_get (),
711                                       key,
712                                       preferences_notify_string_combo_cb,
713                                       widget);
714         if (id) {
715                 preferences_add_id (preferences, id);
716         }
717 }
718
719 static void
720 preferences_hookup_sensitivity (GossipPreferences *preferences,
721                                 const gchar       *key,
722                                 GtkWidget         *widget)
723 {
724         gboolean value;
725         guint    id;
726
727         if (gossip_conf_get_bool (gossip_conf_get (), key, &value)) {
728                 gtk_widget_set_sensitive (widget, value);
729         }
730
731         id = gossip_conf_notify_add (gossip_conf_get (),
732                                       key,
733                                       preferences_notify_sensitivity_cb,
734                                       widget);
735         if (id) {
736                 preferences_add_id (preferences, id);
737         }
738 }
739
740 static void
741 preferences_spin_button_value_changed_cb (GtkWidget *button,
742                                           gpointer   user_data)
743 {
744         const gchar *key;
745
746         key = g_object_get_data (G_OBJECT (button), "key");
747
748         gossip_conf_set_int (gossip_conf_get (),
749                               key,
750                               gtk_spin_button_get_value (GTK_SPIN_BUTTON (button)));
751 }
752
753 static void
754 preferences_entry_value_changed_cb (GtkWidget *entry,
755                                     gpointer   user_data)
756 {
757         const gchar *key;
758
759         key = g_object_get_data (G_OBJECT (entry), "key");
760
761         gossip_conf_set_string (gossip_conf_get (),
762                                  key,
763                                  gtk_entry_get_text (GTK_ENTRY (entry)));
764 }
765
766 static void
767 preferences_toggle_button_toggled_cb (GtkWidget *button,
768                                       gpointer   user_data)
769 {
770         const gchar *key;
771
772         key = g_object_get_data (G_OBJECT (button), "key");
773
774         gossip_conf_set_bool (gossip_conf_get (),
775                                key,
776                                gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (button)));
777 }
778
779 static void
780 preferences_string_combo_changed_cb (GtkWidget *combo,
781                                      gpointer   user_data)
782 {
783         const gchar  *key;
784         GtkTreeModel *model;
785         GtkTreeIter   iter;
786         gchar        *name;
787
788         key = g_object_get_data (G_OBJECT (combo), "key");
789
790         if (gtk_combo_box_get_active_iter (GTK_COMBO_BOX (combo), &iter)) {
791                 model = gtk_combo_box_get_model (GTK_COMBO_BOX (combo));
792
793                 gtk_tree_model_get (model, &iter,
794                                     COL_COMBO_NAME, &name,
795                                     -1);
796                 gossip_conf_set_string (gossip_conf_get (), key, name);
797                 g_free (name);
798         }
799 }
800
801 static void
802 preferences_response_cb (GtkWidget         *widget,
803                          gint               response,
804                          GossipPreferences *preferences)
805 {
806         gtk_widget_destroy (widget);
807 }
808
809 static void
810 preferences_destroy_cb (GtkWidget         *widget,
811                         GossipPreferences *preferences)
812 {
813         GList *l;
814
815         for (l = preferences->notify_ids; l; l = l->next) {
816                 guint id;
817
818                 id = GPOINTER_TO_UINT (l->data);
819                 gossip_conf_notify_remove (gossip_conf_get (), id);
820         }
821
822         g_list_free (preferences->notify_ids);
823         g_free (preferences);
824 }
825
826 void
827 gossip_preferences_show (void)
828 {
829         static GossipPreferences *preferences;
830         GladeXML                 *glade;
831
832         if (preferences) {
833                 gtk_window_present (GTK_WINDOW (preferences->dialog));
834                 return;
835         }
836
837         preferences = g_new0 (GossipPreferences, 1);
838
839         glade = gossip_glade_get_file (
840                 "main.glade",
841                 "preferences_dialog",
842                 NULL,
843                 "preferences_dialog", &preferences->dialog,
844                 "notebook", &preferences->notebook,
845                 "checkbutton_show_avatars", &preferences->checkbutton_show_avatars,
846                 "checkbutton_compact_contact_list", &preferences->checkbutton_compact_contact_list,
847                 "checkbutton_show_smileys", &preferences->checkbutton_show_smileys,
848                 "combobox_chat_theme", &preferences->combobox_chat_theme,
849                 "checkbutton_theme_chat_room", &preferences->checkbutton_theme_chat_room,
850                 "checkbutton_separate_chat_windows", &preferences->checkbutton_separate_chat_windows,
851                 "checkbutton_sounds_for_messages", &preferences->checkbutton_sounds_for_messages,
852                 "checkbutton_sounds_when_busy", &preferences->checkbutton_sounds_when_busy,
853                 "checkbutton_sounds_when_away", &preferences->checkbutton_sounds_when_away,
854                 "checkbutton_popups_when_available", &preferences->checkbutton_popups_when_available,
855                 "treeview_spell_checker", &preferences->treeview_spell_checker,
856                 "checkbutton_spell_checker", &preferences->checkbutton_spell_checker,
857                 NULL);
858
859         gossip_glade_connect (glade,
860                               preferences,
861                               "preferences_dialog", "destroy", preferences_destroy_cb,
862                               "preferences_dialog", "response", preferences_response_cb,
863                               NULL);
864
865         g_object_unref (glade);
866
867         g_object_add_weak_pointer (G_OBJECT (preferences->dialog), (gpointer) &preferences);
868
869         preferences_themes_setup (preferences);
870
871         preferences_setup_widgets (preferences);
872
873         preferences_languages_setup (preferences);
874         preferences_languages_add (preferences);
875         preferences_languages_load (preferences);
876
877         if (gossip_spell_supported ()) {
878                 GtkWidget *page;
879
880                 page = gtk_notebook_get_nth_page (GTK_NOTEBOOK (preferences->notebook), 2);
881                 gtk_widget_show (page);
882         }
883
884         gtk_widget_show (preferences->dialog);
885 }