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