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