]> git.0d.be Git - empathy.git/blob - src/empathy-preferences.c
Merge remote-tracking branch 'pochu/misc-fixes'
[empathy.git] / src / empathy-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., 51 Franklin St, Fifth Floor,
18  * Boston, MA  02110-1301  USA
19  *
20  * Authors: Mikael Hallendal <micke@imendio.com>
21  *          Richard Hult <richard@imendio.com>
22  *          Martyn Russell <martyn@imendio.com>
23  *          Danielle Madeley <danielle.madeley@collabora.co.uk>
24  */
25
26 #include <config.h>
27
28 #include <string.h>
29 #include <stdio.h>
30
31 #include <gtk/gtk.h>
32 #include <glib/gi18n.h>
33 #include <telepathy-glib/dbus.h>
34 #include <telepathy-glib/util.h>
35
36 #include <libempathy/empathy-gsettings.h>
37 #include <libempathy/empathy-utils.h>
38
39 #include <libempathy-gtk/empathy-ui-utils.h>
40 #include <libempathy-gtk/empathy-theme-manager.h>
41 #include <libempathy-gtk/empathy-spell.h>
42 #include <libempathy-gtk/empathy-contact-list-store.h>
43 #include <libempathy-gtk/empathy-gtk-enum-types.h>
44 #include <libempathy-gtk/empathy-theme-adium.h>
45
46 #include "empathy-preferences.h"
47
48 #define DEBUG_FLAG EMPATHY_DEBUG_OTHER
49 #include <libempathy/empathy-debug.h>
50
51 G_DEFINE_TYPE (EmpathyPreferences, empathy_preferences, GTK_TYPE_DIALOG);
52
53 #define GET_PRIV(self) ((EmpathyPreferencesPriv *)((EmpathyPreferences *) self)->priv)
54
55 static const gchar * empathy_preferences_tabs[] =
56 {
57   "general",
58   "notifications",
59   "sounds",
60   "calls",
61   "location",
62   "spell",
63   "themes",
64 };
65
66 struct _EmpathyPreferencesPriv {
67         GtkWidget *notebook;
68
69         GtkWidget *checkbutton_show_smileys;
70         GtkWidget *checkbutton_show_contacts_in_rooms;
71         GtkWidget *checkbutton_separate_chat_windows;
72         GtkWidget *checkbutton_events_notif_area;
73         GtkWidget *checkbutton_autoconnect;
74         GtkWidget *checkbutton_logging;
75
76         GtkWidget *checkbutton_sounds_enabled;
77         GtkWidget *checkbutton_sounds_disabled_away;
78         GtkWidget *treeview_sounds;
79
80         GtkWidget *checkbutton_notifications_enabled;
81         GtkWidget *checkbutton_notifications_disabled_away;
82         GtkWidget *checkbutton_notifications_focus;
83         GtkWidget *checkbutton_notifications_contact_signin;
84         GtkWidget *checkbutton_notifications_contact_signout;
85
86         GtkWidget *scale_call_volume;
87         GtkWidget *adj_call_volume;
88         GtkWidget *echo_cancellation;
89
90         GtkWidget *treeview_spell_checker;
91
92         GtkWidget *checkbutton_location_publish;
93         GtkWidget *checkbutton_location_reduce_accuracy;
94         GtkWidget *checkbutton_location_resource_network;
95         GtkWidget *checkbutton_location_resource_cell;
96         GtkWidget *checkbutton_location_resource_gps;
97
98         GtkWidget *vbox_chat_theme;
99         GtkWidget *combobox_chat_theme;
100         GtkWidget *combobox_chat_theme_variant;
101         GtkWidget *hbox_chat_theme_variant;
102         GtkWidget *sw_chat_theme_preview;
103         EmpathyChatView *chat_theme_preview;
104         EmpathyThemeManager *theme_manager;
105
106         GSettings *gsettings;
107         GSettings *gsettings_chat;
108         GSettings *gsettings_call;
109         GSettings *gsettings_loc;
110         GSettings *gsettings_notify;
111         GSettings *gsettings_sound;
112         GSettings *gsettings_ui;
113         GSettings *gsettings_logger;
114 };
115
116 static void     preferences_setup_widgets                (EmpathyPreferences      *preferences);
117 static void     preferences_languages_setup              (EmpathyPreferences      *preferences);
118 static void     preferences_languages_add                (EmpathyPreferences      *preferences);
119 static void     preferences_languages_save               (EmpathyPreferences      *preferences);
120 static gboolean preferences_languages_save_foreach       (GtkTreeModel           *model,
121                                                           GtkTreePath            *path,
122                                                           GtkTreeIter            *iter,
123                                                           gchar                 **languages);
124 static void     preferences_languages_load               (EmpathyPreferences      *preferences);
125 static gboolean preferences_languages_load_foreach       (GtkTreeModel           *model,
126                                                           GtkTreePath            *path,
127                                                           GtkTreeIter            *iter,
128                                                           GList                  *languages);
129 static void     preferences_languages_cell_toggled_cb    (GtkCellRendererToggle  *cell,
130                                                           gchar                  *path_string,
131                                                           EmpathyPreferences      *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_THEME_VISIBLE_NAME,
142         COL_THEME_NAME,
143         COL_THEME_IS_ADIUM,
144         COL_THEME_ADIUM_PATH,
145         COL_THEME_ADIUM_INFO,
146         COL_THEME_COUNT
147 };
148
149 enum {
150         COL_VARIANT_NAME,
151         COL_VARIANT_DEFAULT,
152         COL_VARIANT_COUNT
153 };
154
155 enum {
156         COL_SOUND_ENABLED,
157         COL_SOUND_NAME,
158         COL_SOUND_KEY,
159         COL_SOUND_COUNT
160 };
161
162 typedef struct {
163         const char *name;
164         const char *key;
165 } SoundEventEntry;
166
167 /* TODO: add phone related sounds also? */
168 static SoundEventEntry sound_entries [] = {
169         { N_("Message received"), EMPATHY_PREFS_SOUNDS_INCOMING_MESSAGE },
170         { N_("Message sent"), EMPATHY_PREFS_SOUNDS_OUTGOING_MESSAGE },
171         { N_("New conversation"), EMPATHY_PREFS_SOUNDS_NEW_CONVERSATION },
172         { N_("Contact goes online"), EMPATHY_PREFS_SOUNDS_CONTACT_LOGIN },
173         { N_("Contact goes offline"), EMPATHY_PREFS_SOUNDS_CONTACT_LOGOUT },
174         { N_("Account connected"), EMPATHY_PREFS_SOUNDS_SERVICE_LOGIN },
175         { N_("Account disconnected"), EMPATHY_PREFS_SOUNDS_SERVICE_LOGOUT }
176 };
177
178 static void
179 preferences_setup_widgets (EmpathyPreferences *preferences)
180 {
181         EmpathyPreferencesPriv *priv = GET_PRIV (preferences);
182
183         g_settings_bind (priv->gsettings_notify,
184                          EMPATHY_PREFS_NOTIFICATIONS_ENABLED,
185                          priv->checkbutton_notifications_enabled,
186                          "active",
187                          G_SETTINGS_BIND_DEFAULT);
188         g_settings_bind (priv->gsettings_notify,
189                          EMPATHY_PREFS_NOTIFICATIONS_DISABLED_AWAY,
190                          priv->checkbutton_notifications_disabled_away,
191                          "active",
192                          G_SETTINGS_BIND_DEFAULT);
193         g_settings_bind (priv->gsettings_notify,
194                          EMPATHY_PREFS_NOTIFICATIONS_FOCUS,
195                          priv->checkbutton_notifications_focus,
196                          "active",
197                          G_SETTINGS_BIND_DEFAULT);
198         g_settings_bind (priv->gsettings_notify,
199                          EMPATHY_PREFS_NOTIFICATIONS_CONTACT_SIGNIN,
200                          priv->checkbutton_notifications_contact_signin,
201                          "active",
202                          G_SETTINGS_BIND_DEFAULT);
203         g_settings_bind (priv->gsettings_notify,
204                          EMPATHY_PREFS_NOTIFICATIONS_CONTACT_SIGNOUT,
205                          priv->checkbutton_notifications_contact_signout,
206                          "active",
207                          G_SETTINGS_BIND_DEFAULT);
208
209         g_settings_bind (priv->gsettings_notify,
210                          EMPATHY_PREFS_NOTIFICATIONS_ENABLED,
211                          priv->checkbutton_notifications_disabled_away,
212                          "sensitive",
213                          G_SETTINGS_BIND_GET);
214         g_settings_bind (priv->gsettings_notify,
215                          EMPATHY_PREFS_NOTIFICATIONS_ENABLED,
216                          priv->checkbutton_notifications_focus,
217                          "sensitive",
218                          G_SETTINGS_BIND_GET);
219         g_settings_bind (priv->gsettings_notify,
220                          EMPATHY_PREFS_NOTIFICATIONS_ENABLED,
221                          priv->checkbutton_notifications_contact_signin,
222                          "sensitive",
223                          G_SETTINGS_BIND_GET);
224         g_settings_bind (priv->gsettings_notify,
225                          EMPATHY_PREFS_NOTIFICATIONS_ENABLED,
226                          priv->checkbutton_notifications_contact_signout,
227                          "sensitive",
228                          G_SETTINGS_BIND_GET);
229
230         g_settings_bind (priv->gsettings_sound,
231                          EMPATHY_PREFS_SOUNDS_ENABLED,
232                          priv->checkbutton_sounds_enabled,
233                          "active",
234                          G_SETTINGS_BIND_DEFAULT);
235         g_settings_bind (priv->gsettings_sound,
236                          EMPATHY_PREFS_SOUNDS_DISABLED_AWAY,
237                          priv->checkbutton_sounds_disabled_away,
238                          "active",
239                          G_SETTINGS_BIND_DEFAULT);
240
241         g_settings_bind (priv->gsettings_sound,
242                          EMPATHY_PREFS_SOUNDS_ENABLED,
243                          priv->checkbutton_sounds_disabled_away,
244                          "sensitive",
245                          G_SETTINGS_BIND_GET);
246         g_settings_bind (priv->gsettings_sound,
247                         EMPATHY_PREFS_SOUNDS_ENABLED,
248                         priv->treeview_sounds,
249                         "sensitive",
250                         G_SETTINGS_BIND_GET);
251
252         g_settings_bind (priv->gsettings_ui,
253                          EMPATHY_PREFS_UI_SEPARATE_CHAT_WINDOWS,
254                          priv->checkbutton_separate_chat_windows,
255                          "active",
256                          G_SETTINGS_BIND_DEFAULT);
257
258         g_settings_bind (priv->gsettings_ui,
259                          EMPATHY_PREFS_UI_EVENTS_NOTIFY_AREA,
260                          priv->checkbutton_events_notif_area,
261                          "active",
262                          G_SETTINGS_BIND_DEFAULT);
263
264         g_settings_bind (priv->gsettings_chat,
265                          EMPATHY_PREFS_CHAT_SHOW_SMILEYS,
266                          priv->checkbutton_show_smileys,
267                          "active",
268                          G_SETTINGS_BIND_DEFAULT);
269         g_settings_bind (priv->gsettings_chat,
270                          EMPATHY_PREFS_CHAT_SHOW_CONTACTS_IN_ROOMS,
271                          priv->checkbutton_show_contacts_in_rooms,
272                          "active",
273                          G_SETTINGS_BIND_DEFAULT);
274
275         g_settings_bind (priv->gsettings_call,
276                          EMPATHY_PREFS_CALL_SOUND_VOLUME,
277                          priv->adj_call_volume,
278                          "value",
279                          G_SETTINGS_BIND_DEFAULT);
280
281         g_settings_bind (priv->gsettings_call,
282                          EMPATHY_PREFS_CALL_ECHO_CANCELLATION,
283                          priv->echo_cancellation,
284                          "active",
285                          G_SETTINGS_BIND_DEFAULT);
286
287         g_settings_bind (priv->gsettings,
288                          EMPATHY_PREFS_AUTOCONNECT,
289                          priv->checkbutton_autoconnect,
290                          "active",
291                          G_SETTINGS_BIND_DEFAULT);
292
293         g_settings_bind (priv->gsettings_loc,
294                          EMPATHY_PREFS_LOCATION_PUBLISH,
295                          priv->checkbutton_location_publish,
296                          "active",
297                          G_SETTINGS_BIND_DEFAULT);
298
299         g_settings_bind (priv->gsettings_loc,
300                          EMPATHY_PREFS_LOCATION_RESOURCE_NETWORK,
301                          priv->checkbutton_location_resource_network,
302                          "active",
303                          G_SETTINGS_BIND_DEFAULT);
304         g_settings_bind (priv->gsettings_loc,
305                          EMPATHY_PREFS_LOCATION_PUBLISH,
306                          priv->checkbutton_location_resource_network,
307                          "sensitive",
308                          G_SETTINGS_BIND_GET);
309
310         g_settings_bind (priv->gsettings_loc,
311                          EMPATHY_PREFS_LOCATION_RESOURCE_CELL,
312                          priv->checkbutton_location_resource_cell,
313                          "active",
314                          G_SETTINGS_BIND_DEFAULT);
315         g_settings_bind (priv->gsettings_loc,
316                          EMPATHY_PREFS_LOCATION_PUBLISH,
317                          priv->checkbutton_location_resource_cell,
318                          "sensitive",
319                          G_SETTINGS_BIND_GET);
320
321         g_settings_bind (priv->gsettings_loc,
322                          EMPATHY_PREFS_LOCATION_RESOURCE_GPS,
323                          priv->checkbutton_location_resource_gps,
324                          "active",
325                          G_SETTINGS_BIND_DEFAULT);
326         g_settings_bind (priv->gsettings_loc,
327                          EMPATHY_PREFS_LOCATION_PUBLISH,
328                          priv->checkbutton_location_resource_gps,
329                          "sensitive",
330                          G_SETTINGS_BIND_GET);
331
332         g_settings_bind (priv->gsettings_loc,
333                          EMPATHY_PREFS_LOCATION_REDUCE_ACCURACY,
334                          priv->checkbutton_location_reduce_accuracy,
335                          "active",
336                          G_SETTINGS_BIND_DEFAULT);
337         g_settings_bind (priv->gsettings_loc,
338                          EMPATHY_PREFS_LOCATION_PUBLISH,
339                          priv->checkbutton_location_reduce_accuracy,
340                          "sensitive",
341                          G_SETTINGS_BIND_GET);
342
343         g_settings_bind (priv->gsettings_logger,
344                          EMPATHY_PREFS_LOGGER_ENABLED,
345                          priv->checkbutton_logging,
346                          "active",
347                          G_SETTINGS_BIND_DEFAULT);
348 }
349
350 static void
351 preferences_sound_cell_toggled_cb (GtkCellRendererToggle *toggle,
352                                    char *path_string,
353                                    EmpathyPreferences *preferences)
354 {
355         EmpathyPreferencesPriv *priv = GET_PRIV (preferences);
356         GtkTreePath *path;
357         gboolean instore;
358         GtkTreeIter iter;
359         GtkTreeView *view;
360         GtkTreeModel *model;
361         char *key;
362
363         view = GTK_TREE_VIEW (priv->treeview_sounds);
364         model = gtk_tree_view_get_model (view);
365
366         path = gtk_tree_path_new_from_string (path_string);
367
368         gtk_tree_model_get_iter (model, &iter, path);
369         gtk_tree_model_get (model, &iter, COL_SOUND_KEY, &key,
370                             COL_SOUND_ENABLED, &instore, -1);
371
372         instore ^= 1;
373
374         gtk_list_store_set (GTK_LIST_STORE (model), &iter,
375                             COL_SOUND_ENABLED, instore, -1);
376
377         g_settings_set_boolean (priv->gsettings_sound, key, instore);
378
379         g_free (key);
380         gtk_tree_path_free (path);
381 }
382
383 static void
384 preferences_sound_load (EmpathyPreferences *preferences)
385 {
386         EmpathyPreferencesPriv *priv = GET_PRIV (preferences);
387         guint i;
388         GtkTreeView *view;
389         GtkListStore *store;
390         GtkTreeIter iter;
391         gboolean set;
392
393         view = GTK_TREE_VIEW (priv->treeview_sounds);
394         store = GTK_LIST_STORE (gtk_tree_view_get_model (view));
395
396         for (i = 0; i < G_N_ELEMENTS (sound_entries); i++) {
397                 set = g_settings_get_boolean (priv->gsettings_sound,
398                                               sound_entries[i].key);
399
400                 gtk_list_store_insert_with_values (store, &iter, i,
401                                                    COL_SOUND_NAME, gettext (sound_entries[i].name),
402                                                    COL_SOUND_KEY, sound_entries[i].key,
403                                                    COL_SOUND_ENABLED, set, -1);
404         }
405 }
406
407 static void
408 preferences_sound_setup (EmpathyPreferences *preferences)
409 {
410         EmpathyPreferencesPriv *priv = GET_PRIV (preferences);
411         GtkTreeView *view;
412         GtkListStore *store;
413         GtkCellRenderer *renderer;
414         GtkTreeViewColumn *column;
415
416         view = GTK_TREE_VIEW (priv->treeview_sounds);
417
418         store = gtk_list_store_new (COL_SOUND_COUNT,
419                                     G_TYPE_BOOLEAN, /* enabled */
420                                     G_TYPE_STRING,  /* name */
421                                     G_TYPE_STRING); /* key */
422
423         gtk_tree_view_set_model (view, GTK_TREE_MODEL (store));
424
425         renderer = gtk_cell_renderer_toggle_new ();
426         g_signal_connect (renderer, "toggled",
427                           G_CALLBACK (preferences_sound_cell_toggled_cb),
428                           preferences);
429
430         column = gtk_tree_view_column_new ();
431         gtk_tree_view_column_pack_start (column, renderer, FALSE);
432         gtk_tree_view_column_add_attribute (column, renderer,
433                                             "active", COL_SOUND_ENABLED);
434
435         renderer = gtk_cell_renderer_text_new ();
436         gtk_tree_view_column_pack_start (column, renderer, FALSE);
437         gtk_tree_view_column_add_attribute (column, renderer,
438                                             "text", COL_SOUND_NAME);
439
440         gtk_tree_view_append_column (view, column);
441
442         gtk_tree_view_column_set_resizable (column, FALSE);
443         gtk_tree_view_column_set_clickable (GTK_TREE_VIEW_COLUMN (column), TRUE);
444
445         g_object_unref (store);
446 }
447
448 static void
449 preferences_languages_setup (EmpathyPreferences *preferences)
450 {
451         EmpathyPreferencesPriv *priv = GET_PRIV (preferences);
452         GtkTreeView       *view;
453         GtkListStore      *store;
454         GtkTreeSelection  *selection;
455         GtkTreeViewColumn *column;
456         GtkCellRenderer   *renderer;
457         guint              col_offset;
458
459         view = GTK_TREE_VIEW (priv->treeview_spell_checker);
460
461         store = gtk_list_store_new (COL_LANG_COUNT,
462                                     G_TYPE_BOOLEAN,  /* enabled */
463                                     G_TYPE_STRING,   /* code */
464                                     G_TYPE_STRING);  /* name */
465
466         gtk_tree_view_set_model (view, GTK_TREE_MODEL (store));
467
468         selection = gtk_tree_view_get_selection (view);
469         gtk_tree_selection_set_mode (selection, GTK_SELECTION_SINGLE);
470
471         renderer = gtk_cell_renderer_toggle_new ();
472         g_signal_connect (renderer, "toggled",
473                           G_CALLBACK (preferences_languages_cell_toggled_cb),
474                           preferences);
475
476         column = gtk_tree_view_column_new_with_attributes (NULL, renderer,
477                                                            "active", COL_LANG_ENABLED,
478                                                            NULL);
479
480         gtk_tree_view_append_column (view, column);
481
482         renderer = gtk_cell_renderer_text_new ();
483         col_offset = gtk_tree_view_insert_column_with_attributes (view,
484                                                                   -1, _("Language"),
485                                                                   renderer,
486                                                                   "text", COL_LANG_NAME,
487                                                                   NULL);
488
489         g_object_set_data (G_OBJECT (renderer),
490                            "column", GINT_TO_POINTER (COL_LANG_NAME));
491
492         column = gtk_tree_view_get_column (view, col_offset - 1);
493         gtk_tree_view_column_set_sort_column_id (column, COL_LANG_NAME);
494         gtk_tree_view_column_set_resizable (column, FALSE);
495         gtk_tree_view_column_set_clickable (GTK_TREE_VIEW_COLUMN (column), TRUE);
496
497         g_object_unref (store);
498 }
499
500 static void
501 preferences_languages_add (EmpathyPreferences *preferences)
502 {
503         EmpathyPreferencesPriv *priv = GET_PRIV (preferences);
504         GtkTreeView  *view;
505         GtkListStore *store;
506         GList        *codes, *l;
507
508         view = GTK_TREE_VIEW (priv->treeview_spell_checker);
509         store = GTK_LIST_STORE (gtk_tree_view_get_model (view));
510
511         codes = empathy_spell_get_language_codes ();
512
513         if (!codes) {
514                 gtk_widget_set_sensitive (priv->treeview_spell_checker, FALSE);
515         }
516
517         for (l = codes; l; l = l->next) {
518                 GtkTreeIter  iter;
519                 const gchar *code;
520                 const gchar *name;
521
522                 code = l->data;
523                 name = empathy_spell_get_language_name (code);
524                 if (!name) {
525                         continue;
526                 }
527
528                 gtk_list_store_append (store, &iter);
529                 gtk_list_store_set (store, &iter,
530                                     COL_LANG_CODE, code,
531                                     COL_LANG_NAME, name,
532                                     -1);
533         }
534
535         empathy_spell_free_language_codes (codes);
536 }
537
538 static void
539 preferences_languages_save (EmpathyPreferences *preferences)
540 {
541         EmpathyPreferencesPriv *priv = GET_PRIV (preferences);
542         GtkTreeView       *view;
543         GtkTreeModel      *model;
544
545         gchar             *languages = NULL;
546
547         view = GTK_TREE_VIEW (priv->treeview_spell_checker);
548         model = gtk_tree_view_get_model (view);
549
550         gtk_tree_model_foreach (model,
551                                 (GtkTreeModelForeachFunc) preferences_languages_save_foreach,
552                                 &languages);
553
554         /* if user selects no languages, we don't want spell check */
555         g_settings_set_boolean (priv->gsettings_chat,
556                                 EMPATHY_PREFS_CHAT_SPELL_CHECKER_ENABLED,
557                                 languages != NULL);
558
559         g_settings_set_string (priv->gsettings_chat,
560                                EMPATHY_PREFS_CHAT_SPELL_CHECKER_LANGUAGES,
561                                languages != NULL ? languages : "");
562
563         g_free (languages);
564 }
565
566 static gboolean
567 preferences_languages_save_foreach (GtkTreeModel  *model,
568                                     GtkTreePath   *path,
569                                     GtkTreeIter   *iter,
570                                     gchar        **languages)
571 {
572         gboolean  enabled;
573         gchar    *code;
574
575         if (!languages) {
576                 return TRUE;
577         }
578
579         gtk_tree_model_get (model, iter, COL_LANG_ENABLED, &enabled, -1);
580         if (!enabled) {
581                 return FALSE;
582         }
583
584         gtk_tree_model_get (model, iter, COL_LANG_CODE, &code, -1);
585         if (!code) {
586                 return FALSE;
587         }
588
589         if (!(*languages)) {
590                 *languages = g_strdup (code);
591         } else {
592                 gchar *str = *languages;
593                 *languages = g_strdup_printf ("%s,%s", str, code);
594                 g_free (str);
595         }
596
597         g_free (code);
598
599         return FALSE;
600 }
601
602 static void
603 preferences_languages_load (EmpathyPreferences *preferences)
604 {
605         EmpathyPreferencesPriv *priv = GET_PRIV (preferences);
606         GtkTreeView   *view;
607         GtkTreeModel  *model;
608         GList         *enabled_codes;
609
610         enabled_codes = empathy_spell_get_enabled_language_codes ();
611
612         g_settings_set_boolean (priv->gsettings_chat,
613                                 EMPATHY_PREFS_CHAT_SPELL_CHECKER_ENABLED,
614                                 enabled_codes != NULL);
615
616         if (enabled_codes == NULL)
617                 return;
618
619         view = GTK_TREE_VIEW (priv->treeview_spell_checker);
620         model = gtk_tree_view_get_model (view);
621
622         gtk_tree_model_foreach (model,
623                                 (GtkTreeModelForeachFunc) preferences_languages_load_foreach,
624                                 enabled_codes);
625
626         g_list_free (enabled_codes);
627 }
628
629 static gboolean
630 preferences_languages_load_foreach (GtkTreeModel  *model,
631                                     GtkTreePath   *path,
632                                     GtkTreeIter   *iter,
633                                     GList         *languages)
634 {
635         gchar    *code;
636         gboolean  found = FALSE;
637
638         if (!languages) {
639                 return TRUE;
640         }
641
642         gtk_tree_model_get (model, iter, COL_LANG_CODE, &code, -1);
643         if (!code) {
644                 return FALSE;
645         }
646
647         if (g_list_find_custom (languages, code, (GCompareFunc) strcmp)) {
648                 found = TRUE;
649         }
650
651         g_free (code);
652         gtk_list_store_set (GTK_LIST_STORE (model), iter, COL_LANG_ENABLED, found, -1);
653         return FALSE;
654 }
655
656 static void
657 preferences_languages_cell_toggled_cb (GtkCellRendererToggle *cell,
658                                        gchar                 *path_string,
659                                        EmpathyPreferences     *preferences)
660 {
661         EmpathyPreferencesPriv *priv = GET_PRIV (preferences);
662         GtkTreeView  *view;
663         GtkTreeModel *model;
664         GtkListStore *store;
665         GtkTreePath  *path;
666         GtkTreeIter   iter;
667         gboolean      enabled;
668
669         view = GTK_TREE_VIEW (priv->treeview_spell_checker);
670         model = gtk_tree_view_get_model (view);
671         store = GTK_LIST_STORE (model);
672
673         path = gtk_tree_path_new_from_string (path_string);
674
675         gtk_tree_model_get_iter (model, &iter, path);
676         gtk_tree_model_get (model, &iter, COL_LANG_ENABLED, &enabled, -1);
677
678         enabled ^= 1;
679
680         gtk_list_store_set (store, &iter, COL_LANG_ENABLED, enabled, -1);
681         gtk_tree_path_free (path);
682
683         preferences_languages_save (preferences);
684 }
685
686 static void
687 preferences_preview_theme_append_message (EmpathyChatView *view,
688                                           EmpathyContact *sender,
689                                           EmpathyContact *receiver,
690                                           const gchar *text)
691 {
692         EmpathyMessage *message;
693
694         message = g_object_new (EMPATHY_TYPE_MESSAGE,
695                 "sender", sender,
696                 "receiver", receiver,
697                 "body", text,
698                 NULL);
699
700         empathy_chat_view_append_message (view, message);
701         g_object_unref (message);
702 }
703
704 static void
705 preferences_preview_theme_changed_cb (EmpathyThemeManager *manager,
706                                       EmpathyPreferences  *preferences)
707 {
708         EmpathyPreferencesPriv *priv = GET_PRIV (preferences);
709         TpDBusDaemon *dbus;
710         TpAccount *account;
711         EmpathyContact *juliet;
712         EmpathyContact *romeo;
713
714         DEBUG ("Theme changed, update preview widget");
715
716         if (priv->chat_theme_preview != NULL) {
717                 gtk_widget_destroy (GTK_WIDGET (priv->chat_theme_preview));
718         }
719         priv->chat_theme_preview = empathy_theme_manager_create_view (manager);
720         gtk_container_add (GTK_CONTAINER (priv->sw_chat_theme_preview),
721                            GTK_WIDGET (priv->chat_theme_preview));
722         gtk_widget_show (GTK_WIDGET (priv->chat_theme_preview));
723
724         /* FIXME: It is ugly to add a fake conversation like that.
725          * Would be cool if we could request a TplLogManager for a fake
726          * conversation */
727         dbus = tp_dbus_daemon_dup (NULL);
728         account = tp_account_new (dbus,
729                 TP_ACCOUNT_OBJECT_PATH_BASE "cm/jabber/account", NULL);
730         juliet = g_object_new (EMPATHY_TYPE_CONTACT,
731                 "account", account,
732                 "id", "juliet",
733                 /* translators: Contact name for the chat theme preview */
734                 "alias", _("Juliet"),
735                 "is-user", FALSE,
736                 NULL);
737         romeo = g_object_new (EMPATHY_TYPE_CONTACT,
738                 "account", account,
739                 "id", "romeo",
740                 /* translators: Contact name for the chat theme preview */
741                 "alias", _("Romeo"),
742                 "is-user", TRUE,
743                 NULL);
744
745         preferences_preview_theme_append_message (priv->chat_theme_preview,
746                 /* translators: Quote from Romeo & Julier, for chat theme preview */
747                 juliet, romeo, _("O Romeo, Romeo, wherefore art thou Romeo?"));
748         preferences_preview_theme_append_message (priv->chat_theme_preview,
749                 /* translators: Quote from Romeo & Julier, for chat theme preview */
750                 juliet, romeo, _("Deny thy father and refuse thy name;"));
751         preferences_preview_theme_append_message (priv->chat_theme_preview,
752                 /* translators: Quote from Romeo & Julier, for chat theme preview */
753                 juliet, romeo, _("Or if thou wilt not, be but sworn my love"));
754         preferences_preview_theme_append_message (priv->chat_theme_preview,
755                 /* translators: Quote from Romeo & Julier, for chat theme preview */
756                 juliet, romeo, _("And I'll no longer be a Capulet."));
757         preferences_preview_theme_append_message (priv->chat_theme_preview,
758                 /* translators: Quote from Romeo & Julier, for chat theme preview */
759                 romeo, juliet, _("Shall I hear more, or shall I speak at this?"));
760
761         /* translators: Quote from Romeo & Julier, for chat theme preview */
762         empathy_chat_view_append_event (priv->chat_theme_preview, _("Juliet has disconnected"));
763
764         g_object_unref (juliet);
765         g_object_unref (romeo);
766         g_object_unref (account);
767         g_object_unref (dbus);
768 }
769
770 static void
771 preferences_theme_variant_changed_cb (GtkComboBox        *combo,
772                                       EmpathyPreferences *preferences)
773 {
774         EmpathyPreferencesPriv *priv = GET_PRIV (preferences);
775         GtkTreeIter   iter;
776
777         if (gtk_combo_box_get_active_iter (combo, &iter)) {
778                 GtkTreeModel *model;
779                 gchar        *name;
780
781                 model = gtk_combo_box_get_model (combo);
782                 gtk_tree_model_get (model, &iter,
783                                     COL_VARIANT_NAME, &name,
784                                     -1);
785
786                 g_settings_set_string (priv->gsettings_chat,
787                                        EMPATHY_PREFS_CHAT_THEME_VARIANT,
788                                        name);
789
790                 g_free (name);
791         }
792 }
793
794 static void
795 preferences_theme_variant_notify_cb (GSettings   *gsettings,
796                                      const gchar *key,
797                                      gpointer     user_data)
798 {
799         EmpathyPreferences *preferences = user_data;
800         EmpathyPreferencesPriv *priv = GET_PRIV (preferences);
801         GtkComboBox        *combo;
802         gchar              *conf_name;
803         GtkTreeModel       *model;
804         GtkTreeIter         iter;
805         GtkTreeIter         default_iter;
806         gboolean            found_default = FALSE;
807         gboolean            found = FALSE;
808         gboolean            ok;
809
810         conf_name = g_settings_get_string (gsettings, EMPATHY_PREFS_CHAT_THEME_VARIANT);
811         combo = GTK_COMBO_BOX (priv->combobox_chat_theme_variant);
812         model = gtk_combo_box_get_model (combo);
813
814         for (ok = gtk_tree_model_get_iter_first (model, &iter);
815              ok && !found;
816              ok = gtk_tree_model_iter_next (model, &iter)) {
817                 gchar *name;
818                 gboolean is_default;
819
820                 gtk_tree_model_get (model, &iter,
821                                     COL_VARIANT_NAME, &name,
822                                     COL_VARIANT_DEFAULT, &is_default,
823                                     -1);
824
825                 if (!tp_strdiff (name, conf_name)) {
826                         found = TRUE;
827                         gtk_combo_box_set_active_iter (combo, &iter);
828                 }
829                 if (is_default) {
830                         found_default = TRUE;
831                         default_iter = iter;
832                 }
833
834                 g_free (name);
835         }
836
837         /* Fallback to the first one. */
838         if (!found) {
839                 if (found_default) {
840                         gtk_combo_box_set_active_iter (combo, &default_iter);
841                 } else if (gtk_tree_model_get_iter_first (model, &iter)) {
842                         gtk_combo_box_set_active_iter (combo, &iter);
843                 }
844         }
845
846         g_free (conf_name);
847 }
848
849 /* return TRUE if we added at least one variant */
850 static gboolean
851 preferences_theme_variants_fill (EmpathyPreferences *preferences,
852                                  GHashTable         *info)
853 {
854         EmpathyPreferencesPriv *priv = GET_PRIV (preferences);
855         GtkTreeModel *model;
856         GtkListStore *store;
857         GPtrArray    *variants;
858         const gchar  *default_variant;
859         guint         i;
860         gboolean      result = FALSE;
861
862         model = gtk_combo_box_get_model (GTK_COMBO_BOX (priv->combobox_chat_theme_variant));
863         store = GTK_LIST_STORE (model);
864         gtk_list_store_clear (store);
865
866         variants = empathy_adium_info_get_available_variants (info);
867         default_variant = empathy_adium_info_get_default_variant (info);
868         for (i = 0; i < variants->len; i++) {
869                 gchar *name = g_ptr_array_index (variants, i);
870
871                 gtk_list_store_insert_with_values (store, NULL, -1,
872                         COL_VARIANT_NAME, name,
873                         COL_VARIANT_DEFAULT, !tp_strdiff (name, default_variant),
874                         -1);
875
876                 result = TRUE;
877         }
878
879         /* Select the variant from the GSetting key */
880         preferences_theme_variant_notify_cb (priv->gsettings_chat,
881                                              EMPATHY_PREFS_CHAT_THEME_VARIANT,
882                                              preferences);
883
884         return result;
885 }
886
887 static void
888 preferences_theme_variants_setup (EmpathyPreferences *preferences)
889 {
890         EmpathyPreferencesPriv *priv = GET_PRIV (preferences);
891         GtkComboBox   *combo;
892         GtkCellLayout *cell_layout;
893         GtkCellRenderer *renderer;
894         GtkListStore  *store;
895
896         combo = GTK_COMBO_BOX (priv->combobox_chat_theme_variant);
897         cell_layout = GTK_CELL_LAYOUT (combo);
898
899         /* Create the model */
900         store = gtk_list_store_new (COL_VARIANT_COUNT,
901                                     G_TYPE_STRING,      /* name */
902                                     G_TYPE_BOOLEAN);    /* is default */
903         gtk_tree_sortable_set_sort_column_id (GTK_TREE_SORTABLE (store),
904                 COL_VARIANT_NAME, GTK_SORT_ASCENDING);
905
906         /* Add cell renderer */
907         renderer = gtk_cell_renderer_text_new ();
908         gtk_cell_layout_pack_start (cell_layout, renderer, TRUE);
909         gtk_cell_layout_set_attributes (cell_layout, renderer,
910                 "text", COL_VARIANT_NAME, NULL);
911
912         gtk_combo_box_set_model (combo, GTK_TREE_MODEL (store));
913         g_object_unref (store);
914
915         g_signal_connect (combo, "changed",
916                           G_CALLBACK (preferences_theme_variant_changed_cb),
917                           preferences);
918
919         /* Track changes of the GSetting key */
920         g_signal_connect (priv->gsettings_chat,
921                           "changed::" EMPATHY_PREFS_CHAT_THEME_VARIANT,
922                           G_CALLBACK (preferences_theme_variant_notify_cb),
923                           preferences);
924 }
925
926 static void
927 preferences_theme_changed_cb (GtkComboBox        *combo,
928                               EmpathyPreferences *preferences)
929 {
930         EmpathyPreferencesPriv *priv = GET_PRIV (preferences);
931         GtkTreeIter   iter;
932
933         if (gtk_combo_box_get_active_iter (combo, &iter)) {
934                 GtkTreeModel *model;
935                 gboolean      is_adium;
936                 gchar        *name;
937                 gchar        *path;
938                 GHashTable   *info;
939
940                 model = gtk_combo_box_get_model (combo);
941                 gtk_tree_model_get (model, &iter,
942                                     COL_THEME_IS_ADIUM, &is_adium,
943                                     COL_THEME_NAME, &name,
944                                     COL_THEME_ADIUM_PATH, &path,
945                                     COL_THEME_ADIUM_INFO, &info,
946                                     -1);
947
948                 g_settings_set_string (priv->gsettings_chat,
949                                        EMPATHY_PREFS_CHAT_THEME,
950                                        name);
951                 if (is_adium) {
952                         gboolean variant;
953
954                         g_settings_set_string (priv->gsettings_chat,
955                                                EMPATHY_PREFS_CHAT_ADIUM_PATH,
956                                                path);
957
958                         variant = preferences_theme_variants_fill (preferences, info);
959                         gtk_widget_set_visible (priv->hbox_chat_theme_variant, variant);
960                 } else {
961                         gtk_widget_hide (priv->hbox_chat_theme_variant);
962                 }
963                 g_free (name);
964                 g_free (path);
965                 tp_clear_pointer (&info, g_hash_table_unref);
966         }
967 }
968
969 static void
970 preferences_theme_notify_cb (GSettings   *gsettings,
971                              const gchar *key,
972                              gpointer     user_data)
973 {
974         EmpathyPreferences *preferences = user_data;
975         EmpathyPreferencesPriv *priv = GET_PRIV (preferences);
976         GtkComboBox        *combo;
977         gchar              *conf_name;
978         gchar              *conf_path;
979         GtkTreeModel       *model;
980         GtkTreeIter         iter;
981         gboolean            found = FALSE;
982         gboolean            ok;
983
984         conf_name = g_settings_get_string (gsettings, EMPATHY_PREFS_CHAT_THEME);
985         conf_path = g_settings_get_string (gsettings, EMPATHY_PREFS_CHAT_ADIUM_PATH);
986
987         combo = GTK_COMBO_BOX (priv->combobox_chat_theme);
988         model = gtk_combo_box_get_model (combo);
989         for (ok = gtk_tree_model_get_iter_first (model, &iter);
990              ok && !found;
991              ok = gtk_tree_model_iter_next (model, &iter)) {
992                 gboolean is_adium;
993                 gchar *name;
994                 gchar *path;
995
996                 gtk_tree_model_get (model, &iter,
997                                     COL_THEME_IS_ADIUM, &is_adium,
998                                     COL_THEME_NAME, &name,
999                                     COL_THEME_ADIUM_PATH, &path,
1000                                     -1);
1001
1002                 if (!tp_strdiff (name, conf_name) &&
1003                     (!is_adium || !tp_strdiff (path, conf_path))) {
1004                         found = TRUE;
1005                         gtk_combo_box_set_active_iter (combo, &iter);
1006                 }
1007
1008                 g_free (name);
1009                 g_free (path);
1010         }
1011
1012         /* Fallback to the first one. */
1013         if (!found) {
1014                 if (gtk_tree_model_get_iter_first (model, &iter)) {
1015                         gtk_combo_box_set_active_iter (combo, &iter);
1016                 }
1017         }
1018
1019         g_free (conf_name);
1020         g_free (conf_path);
1021 }
1022
1023 static void
1024 preferences_themes_setup (EmpathyPreferences *preferences)
1025 {
1026         EmpathyPreferencesPriv *priv = GET_PRIV (preferences);
1027         GtkComboBox   *combo;
1028         GtkCellLayout *cell_layout;
1029         GtkCellRenderer *renderer;
1030         GtkListStore  *store;
1031         const gchar  **themes;
1032         GList         *adium_themes;
1033         gint           i;
1034
1035         preferences_theme_variants_setup (preferences);
1036
1037         combo = GTK_COMBO_BOX (priv->combobox_chat_theme);
1038         cell_layout = GTK_CELL_LAYOUT (combo);
1039
1040         /* Create the model */
1041         store = gtk_list_store_new (COL_THEME_COUNT,
1042                                     G_TYPE_STRING,      /* Display name */
1043                                     G_TYPE_STRING,      /* Theme name */
1044                                     G_TYPE_BOOLEAN,     /* Is an Adium theme */
1045                                     G_TYPE_STRING,      /* Adium theme path */
1046                                     G_TYPE_HASH_TABLE); /* Adium theme info */
1047         gtk_tree_sortable_set_sort_column_id (GTK_TREE_SORTABLE (store),
1048                 COL_THEME_VISIBLE_NAME, GTK_SORT_ASCENDING);
1049
1050         /* Fill the model */
1051         themes = empathy_theme_manager_get_themes ();
1052         for (i = 0; themes[i]; i += 2) {
1053                 gtk_list_store_insert_with_values (store, NULL, -1,
1054                         COL_THEME_VISIBLE_NAME, _(themes[i + 1]),
1055                         COL_THEME_NAME, themes[i],
1056                         COL_THEME_IS_ADIUM, FALSE,
1057                         -1);
1058         }
1059
1060         adium_themes = empathy_theme_manager_get_adium_themes ();
1061         while (adium_themes != NULL) {
1062                 GHashTable *info;
1063                 const gchar *name;
1064                 const gchar *path;
1065
1066                 info = adium_themes->data;
1067                 name = tp_asv_get_string (info, "CFBundleName");
1068                 path = tp_asv_get_string (info, "path");
1069
1070                 if (name != NULL && path != NULL) {
1071                         gtk_list_store_insert_with_values (store, NULL, -1,
1072                                 COL_THEME_VISIBLE_NAME, name,
1073                                 COL_THEME_NAME, "adium",
1074                                 COL_THEME_IS_ADIUM, TRUE,
1075                                 COL_THEME_ADIUM_PATH, path,
1076                                 COL_THEME_ADIUM_INFO, info,
1077                                 -1);
1078                 }
1079                 g_hash_table_unref (info);
1080                 adium_themes = g_list_delete_link (adium_themes, adium_themes);
1081         }
1082
1083         /* Add cell renderer */
1084         renderer = gtk_cell_renderer_text_new ();
1085         gtk_cell_layout_pack_start (cell_layout, renderer, TRUE);
1086         gtk_cell_layout_set_attributes (cell_layout, renderer,
1087                 "text", COL_THEME_VISIBLE_NAME, NULL);
1088
1089         gtk_combo_box_set_model (combo, GTK_TREE_MODEL (store));
1090         g_object_unref (store);
1091
1092         g_signal_connect (combo, "changed",
1093                           G_CALLBACK (preferences_theme_changed_cb),
1094                           preferences);
1095
1096         /* Select the theme from the GSetting key and track changes */
1097         preferences_theme_notify_cb (priv->gsettings_chat,
1098                                      EMPATHY_PREFS_CHAT_THEME,
1099                                      preferences);
1100         g_signal_connect (priv->gsettings_chat,
1101                           "changed::" EMPATHY_PREFS_CHAT_THEME,
1102                           G_CALLBACK (preferences_theme_notify_cb),
1103                           preferences);
1104
1105         g_signal_connect (priv->gsettings_chat,
1106                           "changed::" EMPATHY_PREFS_CHAT_ADIUM_PATH,
1107                           G_CALLBACK (preferences_theme_notify_cb),
1108                           preferences);
1109 }
1110
1111 static gchar *
1112 preferences_call_format_volume_cb (GtkScale *scale,
1113                                    gdouble value)
1114 {
1115         return g_strdup_printf ("%g%%", value);
1116 }
1117
1118 static void
1119 empathy_preferences_response (GtkDialog *widget,
1120                               gint response)
1121 {
1122         gtk_widget_destroy (GTK_WIDGET (widget));
1123 }
1124
1125 static void
1126 empathy_preferences_finalize (GObject *self)
1127 {
1128         EmpathyPreferencesPriv *priv = GET_PRIV (self);
1129
1130         g_object_unref (priv->theme_manager);
1131
1132         g_object_unref (priv->gsettings);
1133         g_object_unref (priv->gsettings_chat);
1134         g_object_unref (priv->gsettings_call);
1135         g_object_unref (priv->gsettings_loc);
1136         g_object_unref (priv->gsettings_notify);
1137         g_object_unref (priv->gsettings_sound);
1138         g_object_unref (priv->gsettings_ui);
1139         g_object_unref (priv->gsettings_logger);
1140
1141         G_OBJECT_CLASS (empathy_preferences_parent_class)->finalize (self);
1142 }
1143
1144 static void
1145 empathy_preferences_class_init (EmpathyPreferencesClass *klass)
1146 {
1147         GtkDialogClass *dialog_class = GTK_DIALOG_CLASS (klass);
1148         GObjectClass *object_class = G_OBJECT_CLASS (klass);
1149
1150         dialog_class->response = empathy_preferences_response;
1151
1152         object_class->finalize = empathy_preferences_finalize;
1153
1154         g_type_class_add_private (object_class,
1155                                   sizeof (EmpathyPreferencesPriv));
1156 }
1157
1158 static void
1159 empathy_preferences_init (EmpathyPreferences *preferences)
1160 {
1161         EmpathyPreferencesPriv    *priv;
1162         GtkBuilder                *gui;
1163         gchar                     *filename;
1164         GtkWidget                 *page;
1165         GtkWidget                 *call_volume_scale_box;
1166         GtkWidget                 *call_volume_bar_box;
1167
1168         priv = preferences->priv = G_TYPE_INSTANCE_GET_PRIVATE (preferences,
1169                         EMPATHY_TYPE_PREFERENCES, EmpathyPreferencesPriv);
1170
1171         gtk_dialog_add_button (GTK_DIALOG (preferences),
1172                                GTK_STOCK_CLOSE, GTK_RESPONSE_CLOSE);
1173
1174         gtk_container_set_border_width (GTK_CONTAINER (preferences), 5);
1175         gtk_window_set_title (GTK_WINDOW (preferences), _("Preferences"));
1176         gtk_window_set_role (GTK_WINDOW (preferences), "preferences");
1177         gtk_window_set_position (GTK_WINDOW (preferences),
1178                                  GTK_WIN_POS_CENTER_ON_PARENT);
1179         gtk_window_set_icon_name (GTK_WINDOW (preferences), "gtk-preferences");
1180
1181         filename = empathy_file_lookup ("empathy-preferences.ui", "src");
1182         gui = empathy_builder_get_file (filename,
1183                 "notebook", &priv->notebook,
1184                 "checkbutton_show_smileys", &priv->checkbutton_show_smileys,
1185                 "checkbutton_show_contacts_in_rooms", &priv->checkbutton_show_contacts_in_rooms,
1186                 "vbox_chat_theme", &priv->vbox_chat_theme,
1187                 "combobox_chat_theme", &priv->combobox_chat_theme,
1188                 "combobox_chat_theme_variant", &priv->combobox_chat_theme_variant,
1189                 "hbox_chat_theme_variant", &priv->hbox_chat_theme_variant,
1190                 "sw_chat_theme_preview", &priv->sw_chat_theme_preview,
1191                 "checkbutton_separate_chat_windows", &priv->checkbutton_separate_chat_windows,
1192                 "checkbutton_events_notif_area", &priv->checkbutton_events_notif_area,
1193                 "checkbutton_autoconnect", &priv->checkbutton_autoconnect,
1194                 "checkbutton_logging", &priv->checkbutton_logging,
1195                 "checkbutton_notifications_enabled", &priv->checkbutton_notifications_enabled,
1196                 "checkbutton_notifications_disabled_away", &priv->checkbutton_notifications_disabled_away,
1197                 "checkbutton_notifications_focus", &priv->checkbutton_notifications_focus,
1198                 "checkbutton_notifications_contact_signin", &priv->checkbutton_notifications_contact_signin,
1199                 "checkbutton_notifications_contact_signout", &priv->checkbutton_notifications_contact_signout,
1200                 "checkbutton_sounds_enabled", &priv->checkbutton_sounds_enabled,
1201                 "checkbutton_sounds_disabled_away", &priv->checkbutton_sounds_disabled_away,
1202                 "treeview_sounds", &priv->treeview_sounds,
1203                 "treeview_spell_checker", &priv->treeview_spell_checker,
1204                 "checkbutton_location_publish", &priv->checkbutton_location_publish,
1205                 "checkbutton_location_reduce_accuracy", &priv->checkbutton_location_reduce_accuracy,
1206                 "checkbutton_location_resource_network", &priv->checkbutton_location_resource_network,
1207                 "checkbutton_location_resource_cell", &priv->checkbutton_location_resource_cell,
1208                 "checkbutton_location_resource_gps", &priv->checkbutton_location_resource_gps,
1209                 "call_volume_scale_box", &call_volume_scale_box,
1210                 "call_volume_bar_box", &call_volume_bar_box,
1211                 "call_volume_scale", &priv->scale_call_volume,
1212                 "call_volume_adjustment", &priv->adj_call_volume,
1213                 "call_echo_cancellation", &priv->echo_cancellation,
1214                 NULL);
1215         g_free (filename);
1216
1217         gtk_container_add (GTK_CONTAINER (gtk_dialog_get_content_area (GTK_DIALOG (preferences))), priv->notebook);
1218         gtk_widget_show (priv->notebook);
1219
1220         g_object_unref (gui);
1221
1222         priv->gsettings = g_settings_new (EMPATHY_PREFS_SCHEMA);
1223         priv->gsettings_chat = g_settings_new (EMPATHY_PREFS_CHAT_SCHEMA);
1224         priv->gsettings_call = g_settings_new (EMPATHY_PREFS_CALL_SCHEMA);
1225         priv->gsettings_loc = g_settings_new (EMPATHY_PREFS_LOCATION_SCHEMA);
1226         priv->gsettings_notify = g_settings_new (EMPATHY_PREFS_NOTIFICATIONS_SCHEMA);
1227         priv->gsettings_sound = g_settings_new (EMPATHY_PREFS_SOUNDS_SCHEMA);
1228         priv->gsettings_ui = g_settings_new (EMPATHY_PREFS_UI_SCHEMA);
1229         priv->gsettings_logger = g_settings_new (EMPATHY_PREFS_LOGGER_SCHEMA);
1230
1231         /* Create chat theme preview, and track changes */
1232         priv->theme_manager = empathy_theme_manager_dup_singleton ();
1233         tp_g_signal_connect_object (priv->theme_manager, "theme-changed",
1234                           G_CALLBACK (preferences_preview_theme_changed_cb),
1235                           preferences, 0);
1236         preferences_preview_theme_changed_cb (priv->theme_manager, preferences);
1237
1238         g_signal_connect (priv->scale_call_volume, "format-value",
1239                           G_CALLBACK (preferences_call_format_volume_cb),
1240                           preferences);
1241
1242 #ifndef HAVE_CALL
1243         gtk_widget_hide (call_volume_scale_box);
1244         gtk_widget_hide (call_volume_bar_box);
1245 #endif
1246
1247         preferences_themes_setup (preferences);
1248
1249         preferences_setup_widgets (preferences);
1250
1251         preferences_languages_setup (preferences);
1252         preferences_languages_add (preferences);
1253         preferences_languages_load (preferences);
1254
1255         preferences_sound_setup (preferences);
1256         preferences_sound_load (preferences);
1257
1258         if (empathy_spell_supported ()) {
1259                 page = gtk_notebook_get_nth_page (GTK_NOTEBOOK (priv->notebook), EMPATHY_PREFERENCES_TAB_SPELL);
1260                 gtk_widget_show (page);
1261         }
1262
1263         page = gtk_notebook_get_nth_page (GTK_NOTEBOOK (priv->notebook), EMPATHY_PREFERENCES_TAB_LOCATION);
1264 #ifdef HAVE_GEOCLUE
1265         gtk_widget_show (page);
1266 #else
1267         gtk_widget_hide (page);
1268 #endif
1269 }
1270
1271 static EmpathyPreferencesTab
1272 empathy_preferences_tab_from_string (const gchar *str)
1273 {
1274   guint i;
1275
1276   for (i = 0; i < G_N_ELEMENTS (empathy_preferences_tabs); i++)
1277     {
1278       if (!tp_strdiff (str, empathy_preferences_tabs[i]))
1279         return i;
1280     }
1281
1282   g_warn_if_reached ();
1283   return -1;
1284 }
1285
1286 const gchar *
1287 empathy_preferences_tab_to_string (EmpathyPreferencesTab tab)
1288 {
1289   g_return_val_if_fail (tab < G_N_ELEMENTS (empathy_preferences_tabs), NULL);
1290
1291   return empathy_preferences_tabs[tab];
1292 }
1293
1294 GtkWidget *
1295 empathy_preferences_new (GtkWindow *parent,
1296                          gboolean  shell_running)
1297 {
1298         GtkWidget              *self;
1299         EmpathyPreferencesPriv *priv;
1300         GtkWidget              *notif_page;
1301
1302         g_return_val_if_fail (parent == NULL || GTK_IS_WINDOW (parent), NULL);
1303
1304         self = g_object_new (EMPATHY_TYPE_PREFERENCES, NULL);
1305
1306         if (parent != NULL) {
1307                 gtk_window_set_transient_for (GTK_WINDOW (self),
1308                                               parent);
1309         }
1310
1311         /* when running in Gnome Shell we must hide these options since they
1312          * are meaningless in that context:
1313          * - 'Display incoming events in the notification area' (General->Behavior)
1314          * - 'Notifications' tab
1315          */
1316         priv = GET_PRIV (self);
1317         if (shell_running) {
1318                 gtk_widget_hide (priv->checkbutton_events_notif_area);
1319                 notif_page = gtk_notebook_get_nth_page (GTK_NOTEBOOK (priv->notebook),
1320                                                         EMPATHY_PREFERENCES_TAB_NOTIFICATIONS);
1321                 gtk_widget_hide (notif_page);
1322         }
1323
1324         return self;
1325 }
1326
1327 void
1328 empathy_preferences_show_tab (EmpathyPreferences *self,
1329                               const gchar *page)
1330 {
1331         EmpathyPreferencesPriv *priv = GET_PRIV (self);
1332
1333         gtk_notebook_set_current_page (GTK_NOTEBOOK (priv->notebook),
1334                                        empathy_preferences_tab_from_string (page));
1335 }