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