Remove obsolete contact-list-{store,view}
[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 {
683         EmpathyMessage *message;
684
685         message = g_object_new (EMPATHY_TYPE_MESSAGE,
686                 "sender", sender,
687                 "receiver", receiver,
688                 "body", text,
689                 NULL);
690
691         empathy_chat_view_append_message (view, message);
692         g_object_unref (message);
693 }
694
695 static void
696 preferences_preview_theme_changed_cb (EmpathyThemeManager *manager,
697                                       EmpathyPreferences  *preferences)
698 {
699         EmpathyPreferencesPriv *priv = GET_PRIV (preferences);
700         TpDBusDaemon *dbus;
701         TpAccount *account;
702         EmpathyContact *juliet;
703         EmpathyContact *romeo;
704
705         DEBUG ("Theme changed, update preview widget");
706
707         if (priv->chat_theme_preview != NULL) {
708                 gtk_widget_destroy (GTK_WIDGET (priv->chat_theme_preview));
709         }
710         priv->chat_theme_preview = empathy_theme_manager_create_view (manager);
711         gtk_container_add (GTK_CONTAINER (priv->sw_chat_theme_preview),
712                            GTK_WIDGET (priv->chat_theme_preview));
713         gtk_widget_show (GTK_WIDGET (priv->chat_theme_preview));
714
715         /* FIXME: It is ugly to add a fake conversation like that.
716          * Would be cool if we could request a TplLogManager for a fake
717          * conversation */
718         dbus = tp_dbus_daemon_dup (NULL);
719         account = tp_account_new (dbus,
720                 TP_ACCOUNT_OBJECT_PATH_BASE "cm/jabber/account", NULL);
721         juliet = g_object_new (EMPATHY_TYPE_CONTACT,
722                 "account", account,
723                 "id", "juliet",
724                 /* translators: Contact name for the chat theme preview */
725                 "alias", _("Juliet"),
726                 "is-user", FALSE,
727                 NULL);
728         romeo = g_object_new (EMPATHY_TYPE_CONTACT,
729                 "account", account,
730                 "id", "romeo",
731                 /* translators: Contact name for the chat theme preview */
732                 "alias", _("Romeo"),
733                 "is-user", TRUE,
734                 NULL);
735
736         preferences_preview_theme_append_message (priv->chat_theme_preview,
737                 /* translators: Quote from Romeo & Julier, for chat theme preview */
738                 juliet, romeo, _("O Romeo, Romeo, wherefore art thou Romeo?"));
739         preferences_preview_theme_append_message (priv->chat_theme_preview,
740                 /* translators: Quote from Romeo & Julier, for chat theme preview */
741                 juliet, romeo, _("Deny thy father and refuse thy name;"));
742         preferences_preview_theme_append_message (priv->chat_theme_preview,
743                 /* translators: Quote from Romeo & Julier, for chat theme preview */
744                 juliet, romeo, _("Or if thou wilt not, be but sworn my love"));
745         preferences_preview_theme_append_message (priv->chat_theme_preview,
746                 /* translators: Quote from Romeo & Julier, for chat theme preview */
747                 juliet, romeo, _("And I'll no longer be a Capulet."));
748         preferences_preview_theme_append_message (priv->chat_theme_preview,
749                 /* translators: Quote from Romeo & Julier, for chat theme preview */
750                 romeo, juliet, _("Shall I hear more, or shall I speak at this?"));
751
752         /* translators: Quote from Romeo & Julier, for chat theme preview */
753         empathy_chat_view_append_event (priv->chat_theme_preview, _("Juliet has disconnected"));
754
755         g_object_unref (juliet);
756         g_object_unref (romeo);
757         g_object_unref (account);
758         g_object_unref (dbus);
759 }
760
761 static void
762 preferences_theme_variant_changed_cb (GtkComboBox        *combo,
763                                       EmpathyPreferences *preferences)
764 {
765         EmpathyPreferencesPriv *priv = GET_PRIV (preferences);
766         GtkTreeIter   iter;
767
768         if (gtk_combo_box_get_active_iter (combo, &iter)) {
769                 GtkTreeModel *model;
770                 gchar        *name;
771
772                 model = gtk_combo_box_get_model (combo);
773                 gtk_tree_model_get (model, &iter,
774                                     COL_VARIANT_NAME, &name,
775                                     -1);
776
777                 g_settings_set_string (priv->gsettings_chat,
778                                        EMPATHY_PREFS_CHAT_THEME_VARIANT,
779                                        name);
780
781                 g_free (name);
782         }
783 }
784
785 static void
786 preferences_theme_variant_notify_cb (GSettings   *gsettings,
787                                      const gchar *key,
788                                      gpointer     user_data)
789 {
790         EmpathyPreferences *preferences = user_data;
791         EmpathyPreferencesPriv *priv = GET_PRIV (preferences);
792         GtkComboBox        *combo;
793         gchar              *conf_name;
794         GtkTreeModel       *model;
795         GtkTreeIter         iter;
796         GtkTreeIter         default_iter;
797         gboolean            found_default = FALSE;
798         gboolean            found = FALSE;
799         gboolean            ok;
800
801         conf_name = g_settings_get_string (gsettings, EMPATHY_PREFS_CHAT_THEME_VARIANT);
802         combo = GTK_COMBO_BOX (priv->combobox_chat_theme_variant);
803         model = gtk_combo_box_get_model (combo);
804
805         for (ok = gtk_tree_model_get_iter_first (model, &iter);
806              ok && !found;
807              ok = gtk_tree_model_iter_next (model, &iter)) {
808                 gchar *name;
809                 gboolean is_default;
810
811                 gtk_tree_model_get (model, &iter,
812                                     COL_VARIANT_NAME, &name,
813                                     COL_VARIANT_DEFAULT, &is_default,
814                                     -1);
815
816                 if (!tp_strdiff (name, conf_name)) {
817                         found = TRUE;
818                         gtk_combo_box_set_active_iter (combo, &iter);
819                 }
820                 if (is_default) {
821                         found_default = TRUE;
822                         default_iter = iter;
823                 }
824
825                 g_free (name);
826         }
827
828         /* Fallback to the first one. */
829         if (!found) {
830                 if (found_default) {
831                         gtk_combo_box_set_active_iter (combo, &default_iter);
832                 } else if (gtk_tree_model_get_iter_first (model, &iter)) {
833                         gtk_combo_box_set_active_iter (combo, &iter);
834                 }
835         }
836
837         g_free (conf_name);
838 }
839
840 /* return TRUE if we added at least one variant */
841 static gboolean
842 preferences_theme_variants_fill (EmpathyPreferences *preferences,
843                                  GHashTable         *info)
844 {
845         EmpathyPreferencesPriv *priv = GET_PRIV (preferences);
846         GtkTreeModel *model;
847         GtkListStore *store;
848         GPtrArray    *variants;
849         const gchar  *default_variant;
850         guint         i;
851         gboolean      result = FALSE;
852
853         model = gtk_combo_box_get_model (GTK_COMBO_BOX (priv->combobox_chat_theme_variant));
854         store = GTK_LIST_STORE (model);
855         gtk_list_store_clear (store);
856
857         variants = empathy_adium_info_get_available_variants (info);
858         default_variant = empathy_adium_info_get_default_variant (info);
859         for (i = 0; i < variants->len; i++) {
860                 gchar *name = g_ptr_array_index (variants, i);
861
862                 gtk_list_store_insert_with_values (store, NULL, -1,
863                         COL_VARIANT_NAME, name,
864                         COL_VARIANT_DEFAULT, !tp_strdiff (name, default_variant),
865                         -1);
866
867                 result = TRUE;
868         }
869
870         /* Select the variant from the GSetting key */
871         preferences_theme_variant_notify_cb (priv->gsettings_chat,
872                                              EMPATHY_PREFS_CHAT_THEME_VARIANT,
873                                              preferences);
874
875         return result;
876 }
877
878 static void
879 preferences_theme_variants_setup (EmpathyPreferences *preferences)
880 {
881         EmpathyPreferencesPriv *priv = GET_PRIV (preferences);
882         GtkComboBox   *combo;
883         GtkCellLayout *cell_layout;
884         GtkCellRenderer *renderer;
885         GtkListStore  *store;
886
887         combo = GTK_COMBO_BOX (priv->combobox_chat_theme_variant);
888         cell_layout = GTK_CELL_LAYOUT (combo);
889
890         /* Create the model */
891         store = gtk_list_store_new (COL_VARIANT_COUNT,
892                                     G_TYPE_STRING,      /* name */
893                                     G_TYPE_BOOLEAN);    /* is default */
894         gtk_tree_sortable_set_sort_column_id (GTK_TREE_SORTABLE (store),
895                 COL_VARIANT_NAME, GTK_SORT_ASCENDING);
896
897         /* Add cell renderer */
898         renderer = gtk_cell_renderer_text_new ();
899         gtk_cell_layout_pack_start (cell_layout, renderer, TRUE);
900         gtk_cell_layout_set_attributes (cell_layout, renderer,
901                 "text", COL_VARIANT_NAME, NULL);
902
903         gtk_combo_box_set_model (combo, GTK_TREE_MODEL (store));
904         g_object_unref (store);
905
906         g_signal_connect (combo, "changed",
907                           G_CALLBACK (preferences_theme_variant_changed_cb),
908                           preferences);
909
910         /* Track changes of the GSetting key */
911         g_signal_connect (priv->gsettings_chat,
912                           "changed::" EMPATHY_PREFS_CHAT_THEME_VARIANT,
913                           G_CALLBACK (preferences_theme_variant_notify_cb),
914                           preferences);
915 }
916
917 static void
918 preferences_theme_changed_cb (GtkComboBox        *combo,
919                               EmpathyPreferences *preferences)
920 {
921         EmpathyPreferencesPriv *priv = GET_PRIV (preferences);
922         GtkTreeIter   iter;
923
924         if (gtk_combo_box_get_active_iter (combo, &iter)) {
925                 GtkTreeModel *model;
926                 gboolean      is_adium;
927                 gchar        *name;
928                 gchar        *path;
929                 GHashTable   *info;
930
931                 model = gtk_combo_box_get_model (combo);
932                 gtk_tree_model_get (model, &iter,
933                                     COL_THEME_IS_ADIUM, &is_adium,
934                                     COL_THEME_NAME, &name,
935                                     COL_THEME_ADIUM_PATH, &path,
936                                     COL_THEME_ADIUM_INFO, &info,
937                                     -1);
938
939                 g_settings_set_string (priv->gsettings_chat,
940                                        EMPATHY_PREFS_CHAT_THEME,
941                                        name);
942                 if (is_adium) {
943                         gboolean variant;
944
945                         g_settings_set_string (priv->gsettings_chat,
946                                                EMPATHY_PREFS_CHAT_ADIUM_PATH,
947                                                path);
948
949                         variant = preferences_theme_variants_fill (preferences, info);
950                         gtk_widget_set_visible (priv->hbox_chat_theme_variant, variant);
951                 } else {
952                         gtk_widget_hide (priv->hbox_chat_theme_variant);
953                 }
954                 g_free (name);
955                 g_free (path);
956                 tp_clear_pointer (&info, g_hash_table_unref);
957         }
958 }
959
960 static void
961 preferences_theme_notify_cb (GSettings   *gsettings,
962                              const gchar *key,
963                              gpointer     user_data)
964 {
965         EmpathyPreferences *preferences = user_data;
966         EmpathyPreferencesPriv *priv = GET_PRIV (preferences);
967         GtkComboBox        *combo;
968         gchar              *conf_name;
969         gchar              *conf_path;
970         GtkTreeModel       *model;
971         GtkTreeIter         iter;
972         gboolean            found = FALSE;
973         gboolean            ok;
974
975         conf_name = g_settings_get_string (gsettings, EMPATHY_PREFS_CHAT_THEME);
976         conf_path = g_settings_get_string (gsettings, EMPATHY_PREFS_CHAT_ADIUM_PATH);
977
978         combo = GTK_COMBO_BOX (priv->combobox_chat_theme);
979         model = gtk_combo_box_get_model (combo);
980         for (ok = gtk_tree_model_get_iter_first (model, &iter);
981              ok && !found;
982              ok = gtk_tree_model_iter_next (model, &iter)) {
983                 gboolean is_adium;
984                 gchar *name;
985                 gchar *path;
986
987                 gtk_tree_model_get (model, &iter,
988                                     COL_THEME_IS_ADIUM, &is_adium,
989                                     COL_THEME_NAME, &name,
990                                     COL_THEME_ADIUM_PATH, &path,
991                                     -1);
992
993                 if (!tp_strdiff (name, conf_name) &&
994                     (!is_adium || !tp_strdiff (path, conf_path))) {
995                         found = TRUE;
996                         gtk_combo_box_set_active_iter (combo, &iter);
997                 }
998
999                 g_free (name);
1000                 g_free (path);
1001         }
1002
1003         /* Fallback to the first one. */
1004         if (!found) {
1005                 if (gtk_tree_model_get_iter_first (model, &iter)) {
1006                         gtk_combo_box_set_active_iter (combo, &iter);
1007                 }
1008         }
1009
1010         g_free (conf_name);
1011         g_free (conf_path);
1012 }
1013
1014 static void
1015 preferences_themes_setup (EmpathyPreferences *preferences)
1016 {
1017         EmpathyPreferencesPriv *priv = GET_PRIV (preferences);
1018         GtkComboBox   *combo;
1019         GtkCellLayout *cell_layout;
1020         GtkCellRenderer *renderer;
1021         GtkListStore  *store;
1022         const gchar  **themes;
1023         GList         *adium_themes;
1024         gint           i;
1025
1026         preferences_theme_variants_setup (preferences);
1027
1028         combo = GTK_COMBO_BOX (priv->combobox_chat_theme);
1029         cell_layout = GTK_CELL_LAYOUT (combo);
1030
1031         /* Create the model */
1032         store = gtk_list_store_new (COL_THEME_COUNT,
1033                                     G_TYPE_STRING,      /* Display name */
1034                                     G_TYPE_STRING,      /* Theme name */
1035                                     G_TYPE_BOOLEAN,     /* Is an Adium theme */
1036                                     G_TYPE_STRING,      /* Adium theme path */
1037                                     G_TYPE_HASH_TABLE); /* Adium theme info */
1038         gtk_tree_sortable_set_sort_column_id (GTK_TREE_SORTABLE (store),
1039                 COL_THEME_VISIBLE_NAME, GTK_SORT_ASCENDING);
1040
1041         /* Fill the model */
1042         themes = empathy_theme_manager_get_themes ();
1043         for (i = 0; themes[i]; i += 2) {
1044                 gtk_list_store_insert_with_values (store, NULL, -1,
1045                         COL_THEME_VISIBLE_NAME, _(themes[i + 1]),
1046                         COL_THEME_NAME, themes[i],
1047                         COL_THEME_IS_ADIUM, FALSE,
1048                         -1);
1049         }
1050
1051         adium_themes = empathy_theme_manager_get_adium_themes ();
1052         while (adium_themes != NULL) {
1053                 GHashTable *info;
1054                 const gchar *name;
1055                 const gchar *path;
1056
1057                 info = adium_themes->data;
1058                 name = tp_asv_get_string (info, "CFBundleName");
1059                 path = tp_asv_get_string (info, "path");
1060
1061                 if (name != NULL && path != NULL) {
1062                         gtk_list_store_insert_with_values (store, NULL, -1,
1063                                 COL_THEME_VISIBLE_NAME, name,
1064                                 COL_THEME_NAME, "adium",
1065                                 COL_THEME_IS_ADIUM, TRUE,
1066                                 COL_THEME_ADIUM_PATH, path,
1067                                 COL_THEME_ADIUM_INFO, info,
1068                                 -1);
1069                 }
1070                 g_hash_table_unref (info);
1071                 adium_themes = g_list_delete_link (adium_themes, adium_themes);
1072         }
1073
1074         /* Add cell renderer */
1075         renderer = gtk_cell_renderer_text_new ();
1076         gtk_cell_layout_pack_start (cell_layout, renderer, TRUE);
1077         gtk_cell_layout_set_attributes (cell_layout, renderer,
1078                 "text", COL_THEME_VISIBLE_NAME, NULL);
1079
1080         gtk_combo_box_set_model (combo, GTK_TREE_MODEL (store));
1081         g_object_unref (store);
1082
1083         g_signal_connect (combo, "changed",
1084                           G_CALLBACK (preferences_theme_changed_cb),
1085                           preferences);
1086
1087         /* Select the theme from the GSetting key and track changes */
1088         preferences_theme_notify_cb (priv->gsettings_chat,
1089                                      EMPATHY_PREFS_CHAT_THEME,
1090                                      preferences);
1091         g_signal_connect (priv->gsettings_chat,
1092                           "changed::" EMPATHY_PREFS_CHAT_THEME,
1093                           G_CALLBACK (preferences_theme_notify_cb),
1094                           preferences);
1095
1096         g_signal_connect (priv->gsettings_chat,
1097                           "changed::" EMPATHY_PREFS_CHAT_ADIUM_PATH,
1098                           G_CALLBACK (preferences_theme_notify_cb),
1099                           preferences);
1100 }
1101
1102 static void
1103 empathy_preferences_response (GtkDialog *widget,
1104                               gint response)
1105 {
1106         gtk_widget_destroy (GTK_WIDGET (widget));
1107 }
1108
1109 static void
1110 empathy_preferences_finalize (GObject *self)
1111 {
1112         EmpathyPreferencesPriv *priv = GET_PRIV (self);
1113
1114         g_object_unref (priv->theme_manager);
1115
1116         g_object_unref (priv->gsettings);
1117         g_object_unref (priv->gsettings_chat);
1118         g_object_unref (priv->gsettings_call);
1119         g_object_unref (priv->gsettings_loc);
1120         g_object_unref (priv->gsettings_notify);
1121         g_object_unref (priv->gsettings_sound);
1122         g_object_unref (priv->gsettings_ui);
1123         g_object_unref (priv->gsettings_logger);
1124
1125         G_OBJECT_CLASS (empathy_preferences_parent_class)->finalize (self);
1126 }
1127
1128 static void
1129 empathy_preferences_class_init (EmpathyPreferencesClass *klass)
1130 {
1131         GtkDialogClass *dialog_class = GTK_DIALOG_CLASS (klass);
1132         GObjectClass *object_class = G_OBJECT_CLASS (klass);
1133
1134         dialog_class->response = empathy_preferences_response;
1135
1136         object_class->finalize = empathy_preferences_finalize;
1137
1138         g_type_class_add_private (object_class,
1139                                   sizeof (EmpathyPreferencesPriv));
1140 }
1141
1142 static void
1143 empathy_preferences_init (EmpathyPreferences *preferences)
1144 {
1145         EmpathyPreferencesPriv    *priv;
1146         GtkBuilder                *gui;
1147         gchar                     *filename;
1148         GtkWidget                 *page;
1149
1150         priv = preferences->priv = G_TYPE_INSTANCE_GET_PRIVATE (preferences,
1151                         EMPATHY_TYPE_PREFERENCES, EmpathyPreferencesPriv);
1152
1153         gtk_dialog_add_button (GTK_DIALOG (preferences),
1154                                GTK_STOCK_CLOSE, GTK_RESPONSE_CLOSE);
1155
1156         gtk_container_set_border_width (GTK_CONTAINER (preferences), 5);
1157         gtk_window_set_title (GTK_WINDOW (preferences), _("Preferences"));
1158         gtk_window_set_role (GTK_WINDOW (preferences), "preferences");
1159         gtk_window_set_position (GTK_WINDOW (preferences),
1160                                  GTK_WIN_POS_CENTER_ON_PARENT);
1161         gtk_window_set_icon_name (GTK_WINDOW (preferences), "gtk-preferences");
1162
1163         filename = empathy_file_lookup ("empathy-preferences.ui", "src");
1164         gui = empathy_builder_get_file (filename,
1165                 "notebook", &priv->notebook,
1166                 "checkbutton_show_smileys", &priv->checkbutton_show_smileys,
1167                 "checkbutton_show_contacts_in_rooms", &priv->checkbutton_show_contacts_in_rooms,
1168                 "vbox_chat_theme", &priv->vbox_chat_theme,
1169                 "combobox_chat_theme", &priv->combobox_chat_theme,
1170                 "combobox_chat_theme_variant", &priv->combobox_chat_theme_variant,
1171                 "hbox_chat_theme_variant", &priv->hbox_chat_theme_variant,
1172                 "sw_chat_theme_preview", &priv->sw_chat_theme_preview,
1173                 "checkbutton_separate_chat_windows", &priv->checkbutton_separate_chat_windows,
1174                 "checkbutton_events_notif_area", &priv->checkbutton_events_notif_area,
1175                 "checkbutton_autoconnect", &priv->checkbutton_autoconnect,
1176                 "checkbutton_logging", &priv->checkbutton_logging,
1177                 "checkbutton_notifications_enabled", &priv->checkbutton_notifications_enabled,
1178                 "checkbutton_notifications_disabled_away", &priv->checkbutton_notifications_disabled_away,
1179                 "checkbutton_notifications_focus", &priv->checkbutton_notifications_focus,
1180                 "checkbutton_notifications_contact_signin", &priv->checkbutton_notifications_contact_signin,
1181                 "checkbutton_notifications_contact_signout", &priv->checkbutton_notifications_contact_signout,
1182                 "checkbutton_sounds_enabled", &priv->checkbutton_sounds_enabled,
1183                 "checkbutton_sounds_disabled_away", &priv->checkbutton_sounds_disabled_away,
1184                 "treeview_sounds", &priv->treeview_sounds,
1185                 "treeview_spell_checker", &priv->treeview_spell_checker,
1186                 "checkbutton_location_publish", &priv->checkbutton_location_publish,
1187                 "checkbutton_location_reduce_accuracy", &priv->checkbutton_location_reduce_accuracy,
1188                 "checkbutton_location_resource_network", &priv->checkbutton_location_resource_network,
1189                 "checkbutton_location_resource_cell", &priv->checkbutton_location_resource_cell,
1190                 "checkbutton_location_resource_gps", &priv->checkbutton_location_resource_gps,
1191                 "call_echo_cancellation", &priv->echo_cancellation,
1192                 NULL);
1193         g_free (filename);
1194
1195         gtk_container_add (GTK_CONTAINER (gtk_dialog_get_content_area (GTK_DIALOG (preferences))), priv->notebook);
1196         gtk_widget_show (priv->notebook);
1197
1198         g_object_unref (gui);
1199
1200         priv->gsettings = g_settings_new (EMPATHY_PREFS_SCHEMA);
1201         priv->gsettings_chat = g_settings_new (EMPATHY_PREFS_CHAT_SCHEMA);
1202         priv->gsettings_call = g_settings_new (EMPATHY_PREFS_CALL_SCHEMA);
1203         priv->gsettings_loc = g_settings_new (EMPATHY_PREFS_LOCATION_SCHEMA);
1204         priv->gsettings_notify = g_settings_new (EMPATHY_PREFS_NOTIFICATIONS_SCHEMA);
1205         priv->gsettings_sound = g_settings_new (EMPATHY_PREFS_SOUNDS_SCHEMA);
1206         priv->gsettings_ui = g_settings_new (EMPATHY_PREFS_UI_SCHEMA);
1207         priv->gsettings_logger = g_settings_new (EMPATHY_PREFS_LOGGER_SCHEMA);
1208
1209         /* Create chat theme preview, and track changes */
1210         priv->theme_manager = empathy_theme_manager_dup_singleton ();
1211         tp_g_signal_connect_object (priv->theme_manager, "theme-changed",
1212                           G_CALLBACK (preferences_preview_theme_changed_cb),
1213                           preferences, 0);
1214         preferences_preview_theme_changed_cb (priv->theme_manager, preferences);
1215
1216         preferences_themes_setup (preferences);
1217
1218         preferences_setup_widgets (preferences);
1219
1220         preferences_languages_setup (preferences);
1221         preferences_languages_add (preferences);
1222         preferences_languages_load (preferences);
1223
1224         preferences_sound_setup (preferences);
1225         preferences_sound_load (preferences);
1226
1227         if (empathy_spell_supported ()) {
1228                 page = gtk_notebook_get_nth_page (GTK_NOTEBOOK (priv->notebook), EMPATHY_PREFERENCES_TAB_SPELL);
1229                 gtk_widget_show (page);
1230         }
1231
1232         page = gtk_notebook_get_nth_page (GTK_NOTEBOOK (priv->notebook), EMPATHY_PREFERENCES_TAB_LOCATION);
1233 #ifdef HAVE_GEOCLUE
1234         gtk_widget_show (page);
1235 #else
1236         gtk_widget_hide (page);
1237 #endif
1238 }
1239
1240 static EmpathyPreferencesTab
1241 empathy_preferences_tab_from_string (const gchar *str)
1242 {
1243   guint i;
1244
1245   for (i = 0; i < G_N_ELEMENTS (empathy_preferences_tabs); i++)
1246     {
1247       if (!tp_strdiff (str, empathy_preferences_tabs[i]))
1248         return i;
1249     }
1250
1251   g_warn_if_reached ();
1252   return -1;
1253 }
1254
1255 const gchar *
1256 empathy_preferences_tab_to_string (EmpathyPreferencesTab tab)
1257 {
1258   g_return_val_if_fail (tab < G_N_ELEMENTS (empathy_preferences_tabs), NULL);
1259
1260   return empathy_preferences_tabs[tab];
1261 }
1262
1263 GtkWidget *
1264 empathy_preferences_new (GtkWindow *parent,
1265                          gboolean  shell_running)
1266 {
1267         GtkWidget              *self;
1268         EmpathyPreferencesPriv *priv;
1269         GtkWidget              *notif_page;
1270
1271         g_return_val_if_fail (parent == NULL || GTK_IS_WINDOW (parent), NULL);
1272
1273         self = g_object_new (EMPATHY_TYPE_PREFERENCES, NULL);
1274
1275         if (parent != NULL) {
1276                 gtk_window_set_transient_for (GTK_WINDOW (self),
1277                                               parent);
1278         }
1279
1280         /* when running in Gnome Shell we must hide these options since they
1281          * are meaningless in that context:
1282          * - 'Display incoming events in the notification area' (General->Behavior)
1283          * - 'Notifications' tab
1284          */
1285         priv = GET_PRIV (self);
1286         if (shell_running) {
1287                 gtk_widget_hide (priv->checkbutton_events_notif_area);
1288                 notif_page = gtk_notebook_get_nth_page (GTK_NOTEBOOK (priv->notebook),
1289                                                         EMPATHY_PREFERENCES_TAB_NOTIFICATIONS);
1290                 gtk_widget_hide (notif_page);
1291         }
1292
1293         return self;
1294 }
1295
1296 void
1297 empathy_preferences_show_tab (EmpathyPreferences *self,
1298                               const gchar *page)
1299 {
1300         EmpathyPreferencesPriv *priv = GET_PRIV (self);
1301
1302         gtk_notebook_set_current_page (GTK_NOTEBOOK (priv->notebook),
1303                                        empathy_preferences_tab_from_string (page));
1304 }