]> git.0d.be Git - empathy.git/blob - src/empathy-preferences.c
Remove calls to gtk_dialog_set_has_separator() if building against GTK+ 3
[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
30 #include <gtk/gtk.h>
31 #include <glib/gi18n.h>
32 #include <telepathy-glib/dbus.h>
33 #include <telepathy-glib/util.h>
34
35 #include <libempathy/empathy-gsettings.h>
36 #include <libempathy/empathy-utils.h>
37
38 #include <libempathy-gtk/empathy-ui-utils.h>
39 #include <libempathy-gtk/empathy-theme-manager.h>
40 #include <libempathy-gtk/empathy-spell.h>
41 #include <libempathy-gtk/empathy-contact-list-store.h>
42 #include <libempathy-gtk/empathy-gtk-enum-types.h>
43
44 #ifdef HAVE_WEBKIT
45 #include <libempathy-gtk/empathy-theme-adium.h>
46 #endif
47
48 #include "empathy-preferences.h"
49
50 G_DEFINE_TYPE (EmpathyPreferences, empathy_preferences, GTK_TYPE_DIALOG);
51
52 #define GET_PRIV(self) ((EmpathyPreferencesPriv *)((EmpathyPreferences *) self)->priv)
53
54 struct _EmpathyPreferencesPriv {
55         GtkWidget *notebook;
56
57         GtkWidget *checkbutton_show_smileys;
58         GtkWidget *checkbutton_show_contacts_in_rooms;
59         GtkWidget *combobox_chat_theme;
60         GtkWidget *checkbutton_separate_chat_windows;
61         GtkWidget *checkbutton_events_notif_area;
62         GtkWidget *checkbutton_autoconnect;
63         GtkWidget *checkbutton_logging;
64
65         GtkWidget *checkbutton_sounds_enabled;
66         GtkWidget *checkbutton_sounds_disabled_away;
67         GtkWidget *treeview_sounds;
68
69         GtkWidget *checkbutton_notifications_enabled;
70         GtkWidget *checkbutton_notifications_disabled_away;
71         GtkWidget *checkbutton_notifications_focus;
72         GtkWidget *checkbutton_notifications_contact_signin;
73         GtkWidget *checkbutton_notifications_contact_signout;
74
75         GtkWidget *treeview_spell_checker;
76
77         GtkWidget *checkbutton_location_publish;
78         GtkWidget *checkbutton_location_reduce_accuracy;
79         GtkWidget *checkbutton_location_resource_network;
80         GtkWidget *checkbutton_location_resource_cell;
81         GtkWidget *checkbutton_location_resource_gps;
82
83         GSettings *gsettings;
84         GSettings *gsettings_chat;
85         GSettings *gsettings_loc;
86         GSettings *gsettings_notify;
87         GSettings *gsettings_sound;
88         GSettings *gsettings_ui;
89         GSettings *gsettings_logger;
90 };
91
92 static void     preferences_setup_widgets                (EmpathyPreferences      *preferences);
93 static void     preferences_languages_setup              (EmpathyPreferences      *preferences);
94 static void     preferences_languages_add                (EmpathyPreferences      *preferences);
95 static void     preferences_languages_save               (EmpathyPreferences      *preferences);
96 static gboolean preferences_languages_save_foreach       (GtkTreeModel           *model,
97                                                           GtkTreePath            *path,
98                                                           GtkTreeIter            *iter,
99                                                           gchar                 **languages);
100 static void     preferences_languages_load               (EmpathyPreferences      *preferences);
101 static gboolean preferences_languages_load_foreach       (GtkTreeModel           *model,
102                                                           GtkTreePath            *path,
103                                                           GtkTreeIter            *iter,
104                                                           gchar                 **languages);
105 static void     preferences_languages_cell_toggled_cb    (GtkCellRendererToggle  *cell,
106                                                           gchar                  *path_string,
107                                                           EmpathyPreferences      *preferences);
108
109 enum {
110         COL_LANG_ENABLED,
111         COL_LANG_CODE,
112         COL_LANG_NAME,
113         COL_LANG_COUNT
114 };
115
116 enum {
117         COL_COMBO_IS_ADIUM,
118         COL_COMBO_VISIBLE_NAME,
119         COL_COMBO_NAME,
120         COL_COMBO_PATH,
121         COL_COMBO_COUNT
122 };
123
124 enum {
125         COL_SOUND_ENABLED,
126         COL_SOUND_NAME,
127         COL_SOUND_KEY,
128         COL_SOUND_COUNT
129 };
130
131 typedef struct {
132         const char *name;
133         const char *key;
134 } SoundEventEntry;
135
136 /* TODO: add phone related sounds also? */
137 static SoundEventEntry sound_entries [] = {
138         { N_("Message received"), EMPATHY_PREFS_SOUNDS_INCOMING_MESSAGE },
139         { N_("Message sent"), EMPATHY_PREFS_SOUNDS_OUTGOING_MESSAGE },
140         { N_("New conversation"), EMPATHY_PREFS_SOUNDS_NEW_CONVERSATION },
141         { N_("Contact goes online"), EMPATHY_PREFS_SOUNDS_CONTACT_LOGIN },
142         { N_("Contact goes offline"), EMPATHY_PREFS_SOUNDS_CONTACT_LOGOUT },
143         { N_("Account connected"), EMPATHY_PREFS_SOUNDS_SERVICE_LOGIN },
144         { N_("Account disconnected"), EMPATHY_PREFS_SOUNDS_SERVICE_LOGOUT }
145 };
146
147 static void
148 preferences_setup_widgets (EmpathyPreferences *preferences)
149 {
150         EmpathyPreferencesPriv *priv = GET_PRIV (preferences);
151
152         g_settings_bind (priv->gsettings_notify,
153                          EMPATHY_PREFS_NOTIFICATIONS_ENABLED,
154                          priv->checkbutton_notifications_enabled,
155                          "active",
156                          G_SETTINGS_BIND_DEFAULT);
157         g_settings_bind (priv->gsettings_notify,
158                          EMPATHY_PREFS_NOTIFICATIONS_DISABLED_AWAY,
159                          priv->checkbutton_notifications_disabled_away,
160                          "active",
161                          G_SETTINGS_BIND_DEFAULT);
162         g_settings_bind (priv->gsettings_notify,
163                          EMPATHY_PREFS_NOTIFICATIONS_FOCUS,
164                          priv->checkbutton_notifications_focus,
165                          "active",
166                          G_SETTINGS_BIND_DEFAULT);
167         g_settings_bind (priv->gsettings_notify,
168                          EMPATHY_PREFS_NOTIFICATIONS_CONTACT_SIGNIN,
169                          priv->checkbutton_notifications_contact_signin,
170                          "active",
171                          G_SETTINGS_BIND_DEFAULT);
172         g_settings_bind (priv->gsettings_notify,
173                          EMPATHY_PREFS_NOTIFICATIONS_CONTACT_SIGNOUT,
174                          priv->checkbutton_notifications_contact_signout,
175                          "active",
176                          G_SETTINGS_BIND_DEFAULT);
177
178         g_settings_bind (priv->gsettings_notify,
179                          EMPATHY_PREFS_NOTIFICATIONS_ENABLED,
180                          priv->checkbutton_notifications_disabled_away,
181                          "sensitive",
182                          G_SETTINGS_BIND_GET);
183         g_settings_bind (priv->gsettings_notify,
184                          EMPATHY_PREFS_NOTIFICATIONS_ENABLED,
185                          priv->checkbutton_notifications_focus,
186                          "sensitive",
187                          G_SETTINGS_BIND_GET);
188         g_settings_bind (priv->gsettings_notify,
189                          EMPATHY_PREFS_NOTIFICATIONS_ENABLED,
190                          priv->checkbutton_notifications_contact_signin,
191                          "sensitive",
192                          G_SETTINGS_BIND_GET);
193         g_settings_bind (priv->gsettings_notify,
194                          EMPATHY_PREFS_NOTIFICATIONS_ENABLED,
195                          priv->checkbutton_notifications_contact_signout,
196                          "sensitive",
197                          G_SETTINGS_BIND_GET);
198
199         g_settings_bind (priv->gsettings_sound,
200                          EMPATHY_PREFS_SOUNDS_ENABLED,
201                          priv->checkbutton_sounds_enabled,
202                          "active",
203                          G_SETTINGS_BIND_DEFAULT);
204         g_settings_bind (priv->gsettings_sound,
205                          EMPATHY_PREFS_SOUNDS_DISABLED_AWAY,
206                          priv->checkbutton_sounds_disabled_away,
207                          "active",
208                          G_SETTINGS_BIND_DEFAULT);
209
210         g_settings_bind (priv->gsettings_sound,
211                          EMPATHY_PREFS_SOUNDS_ENABLED,
212                          priv->checkbutton_sounds_disabled_away,
213                          "sensitive",
214                          G_SETTINGS_BIND_GET);
215         g_settings_bind (priv->gsettings_sound,
216                         EMPATHY_PREFS_SOUNDS_ENABLED,
217                         priv->treeview_sounds,
218                         "sensitive",
219                         G_SETTINGS_BIND_GET);
220
221         g_settings_bind (priv->gsettings_ui,
222                          EMPATHY_PREFS_UI_SEPARATE_CHAT_WINDOWS,
223                          priv->checkbutton_separate_chat_windows,
224                          "active",
225                          G_SETTINGS_BIND_DEFAULT);
226
227         g_settings_bind (priv->gsettings_ui,
228                          EMPATHY_PREFS_UI_EVENTS_NOTIFY_AREA,
229                          priv->checkbutton_events_notif_area,
230                          "active",
231                          G_SETTINGS_BIND_DEFAULT);
232
233         g_settings_bind (priv->gsettings_chat,
234                          EMPATHY_PREFS_CHAT_SHOW_SMILEYS,
235                          priv->checkbutton_show_smileys,
236                          "active",
237                          G_SETTINGS_BIND_DEFAULT);
238         g_settings_bind (priv->gsettings_chat,
239                          EMPATHY_PREFS_CHAT_SHOW_CONTACTS_IN_ROOMS,
240                          priv->checkbutton_show_contacts_in_rooms,
241                          "active",
242                          G_SETTINGS_BIND_DEFAULT);
243
244         g_settings_bind (priv->gsettings,
245                          EMPATHY_PREFS_AUTOCONNECT,
246                          priv->checkbutton_autoconnect,
247                          "active",
248                          G_SETTINGS_BIND_DEFAULT);
249
250         g_settings_bind (priv->gsettings_loc,
251                          EMPATHY_PREFS_LOCATION_PUBLISH,
252                          priv->checkbutton_location_publish,
253                          "active",
254                          G_SETTINGS_BIND_DEFAULT);
255
256         g_settings_bind (priv->gsettings_loc,
257                          EMPATHY_PREFS_LOCATION_RESOURCE_NETWORK,
258                          priv->checkbutton_location_resource_network,
259                          "active",
260                          G_SETTINGS_BIND_DEFAULT);
261         g_settings_bind (priv->gsettings_loc,
262                          EMPATHY_PREFS_LOCATION_PUBLISH,
263                          priv->checkbutton_location_resource_network,
264                          "sensitive",
265                          G_SETTINGS_BIND_GET);
266
267         g_settings_bind (priv->gsettings_loc,
268                          EMPATHY_PREFS_LOCATION_RESOURCE_CELL,
269                          priv->checkbutton_location_resource_cell,
270                          "active",
271                          G_SETTINGS_BIND_DEFAULT);
272         g_settings_bind (priv->gsettings_loc,
273                          EMPATHY_PREFS_LOCATION_PUBLISH,
274                          priv->checkbutton_location_resource_cell,
275                          "sensitive",
276                          G_SETTINGS_BIND_GET);
277
278         g_settings_bind (priv->gsettings_loc,
279                          EMPATHY_PREFS_LOCATION_RESOURCE_GPS,
280                          priv->checkbutton_location_resource_gps,
281                          "active",
282                          G_SETTINGS_BIND_DEFAULT);
283         g_settings_bind (priv->gsettings_loc,
284                          EMPATHY_PREFS_LOCATION_PUBLISH,
285                          priv->checkbutton_location_resource_gps,
286                          "sensitive",
287                          G_SETTINGS_BIND_GET);
288
289         g_settings_bind (priv->gsettings_loc,
290                          EMPATHY_PREFS_LOCATION_REDUCE_ACCURACY,
291                          priv->checkbutton_location_reduce_accuracy,
292                          "active",
293                          G_SETTINGS_BIND_DEFAULT);
294         g_settings_bind (priv->gsettings_loc,
295                          EMPATHY_PREFS_LOCATION_PUBLISH,
296                          priv->checkbutton_location_reduce_accuracy,
297                          "sensitive",
298                          G_SETTINGS_BIND_GET);
299
300         g_settings_bind (priv->gsettings_logger,
301                          EMPATHY_PREFS_LOGGER_ENABLED,
302                          priv->checkbutton_logging,
303                          "active",
304                          G_SETTINGS_BIND_DEFAULT);
305 }
306
307 static void
308 preferences_sound_cell_toggled_cb (GtkCellRendererToggle *toggle,
309                                    char *path_string,
310                                    EmpathyPreferences *preferences)
311 {
312         EmpathyPreferencesPriv *priv = GET_PRIV (preferences);
313         GtkTreePath *path;
314         gboolean toggled, instore;
315         GtkTreeIter iter;
316         GtkTreeView *view;
317         GtkTreeModel *model;
318         char *key;
319
320         view = GTK_TREE_VIEW (priv->treeview_sounds);
321         model = gtk_tree_view_get_model (view);
322
323         path = gtk_tree_path_new_from_string (path_string);
324         toggled = gtk_cell_renderer_toggle_get_active (toggle);
325
326         gtk_tree_model_get_iter (model, &iter, path);
327         gtk_tree_model_get (model, &iter, COL_SOUND_KEY, &key,
328                             COL_SOUND_ENABLED, &instore, -1);
329
330         instore ^= 1;
331
332         gtk_list_store_set (GTK_LIST_STORE (model), &iter,
333                             COL_SOUND_ENABLED, instore, -1);
334
335         g_settings_set_boolean (priv->gsettings_sound, key, instore);
336
337         g_free (key);
338         gtk_tree_path_free (path);
339 }
340
341 static void
342 preferences_sound_load (EmpathyPreferences *preferences)
343 {
344         EmpathyPreferencesPriv *priv = GET_PRIV (preferences);
345         guint i;
346         GtkTreeView *view;
347         GtkListStore *store;
348         GtkTreeIter iter;
349         gboolean set;
350
351         view = GTK_TREE_VIEW (priv->treeview_sounds);
352         store = GTK_LIST_STORE (gtk_tree_view_get_model (view));
353
354         for (i = 0; i < G_N_ELEMENTS (sound_entries); i++) {
355                 set = g_settings_get_boolean (priv->gsettings_sound,
356                                               sound_entries[i].key);
357
358                 gtk_list_store_insert_with_values (store, &iter, i,
359                                                    COL_SOUND_NAME, gettext (sound_entries[i].name),
360                                                    COL_SOUND_KEY, sound_entries[i].key,
361                                                    COL_SOUND_ENABLED, set, -1);
362         }
363 }
364
365 static void
366 preferences_sound_setup (EmpathyPreferences *preferences)
367 {
368         EmpathyPreferencesPriv *priv = GET_PRIV (preferences);
369         GtkTreeView *view;
370         GtkListStore *store;
371         GtkCellRenderer *renderer;
372         GtkTreeViewColumn *column;
373
374         view = GTK_TREE_VIEW (priv->treeview_sounds);
375
376         store = gtk_list_store_new (COL_SOUND_COUNT,
377                                     G_TYPE_BOOLEAN, /* enabled */
378                                     G_TYPE_STRING,  /* name */
379                                     G_TYPE_STRING); /* key */
380
381         gtk_tree_view_set_model (view, GTK_TREE_MODEL (store));
382
383         renderer = gtk_cell_renderer_toggle_new ();
384         g_signal_connect (renderer, "toggled",
385                           G_CALLBACK (preferences_sound_cell_toggled_cb),
386                           preferences);
387
388         column = gtk_tree_view_column_new ();
389         gtk_tree_view_column_pack_start (column, renderer, FALSE);
390         gtk_tree_view_column_add_attribute (column, renderer,
391                                             "active", COL_SOUND_ENABLED);
392
393         renderer = gtk_cell_renderer_text_new ();
394         gtk_tree_view_column_pack_start (column, renderer, FALSE);
395         gtk_tree_view_column_add_attribute (column, renderer,
396                                             "text", COL_SOUND_NAME);
397
398         gtk_tree_view_append_column (view, column);
399
400         gtk_tree_view_column_set_resizable (column, FALSE);
401         gtk_tree_view_column_set_clickable (GTK_TREE_VIEW_COLUMN (column), TRUE);
402
403         g_object_unref (store);
404 }
405
406 static void
407 preferences_languages_setup (EmpathyPreferences *preferences)
408 {
409         EmpathyPreferencesPriv *priv = GET_PRIV (preferences);
410         GtkTreeView       *view;
411         GtkListStore      *store;
412         GtkTreeSelection  *selection;
413         GtkTreeModel      *model;
414         GtkTreeViewColumn *column;
415         GtkCellRenderer   *renderer;
416         guint              col_offset;
417
418         view = GTK_TREE_VIEW (priv->treeview_spell_checker);
419
420         store = gtk_list_store_new (COL_LANG_COUNT,
421                                     G_TYPE_BOOLEAN,  /* enabled */
422                                     G_TYPE_STRING,   /* code */
423                                     G_TYPE_STRING);  /* name */
424
425         gtk_tree_view_set_model (view, GTK_TREE_MODEL (store));
426
427         selection = gtk_tree_view_get_selection (view);
428         gtk_tree_selection_set_mode (selection, GTK_SELECTION_SINGLE);
429
430         model = GTK_TREE_MODEL (store);
431
432         renderer = gtk_cell_renderer_toggle_new ();
433         g_signal_connect (renderer, "toggled",
434                           G_CALLBACK (preferences_languages_cell_toggled_cb),
435                           preferences);
436
437         column = gtk_tree_view_column_new_with_attributes (NULL, renderer,
438                                                            "active", COL_LANG_ENABLED,
439                                                            NULL);
440
441         gtk_tree_view_append_column (view, column);
442
443         renderer = gtk_cell_renderer_text_new ();
444         col_offset = gtk_tree_view_insert_column_with_attributes (view,
445                                                                   -1, _("Language"),
446                                                                   renderer,
447                                                                   "text", COL_LANG_NAME,
448                                                                   NULL);
449
450         g_object_set_data (G_OBJECT (renderer),
451                            "column", GINT_TO_POINTER (COL_LANG_NAME));
452
453         column = gtk_tree_view_get_column (view, col_offset - 1);
454         gtk_tree_view_column_set_sort_column_id (column, COL_LANG_NAME);
455         gtk_tree_view_column_set_resizable (column, FALSE);
456         gtk_tree_view_column_set_clickable (GTK_TREE_VIEW_COLUMN (column), TRUE);
457
458         g_object_unref (store);
459 }
460
461 static void
462 preferences_languages_add (EmpathyPreferences *preferences)
463 {
464         EmpathyPreferencesPriv *priv = GET_PRIV (preferences);
465         GtkTreeView  *view;
466         GtkListStore *store;
467         GList        *codes, *l;
468
469         view = GTK_TREE_VIEW (priv->treeview_spell_checker);
470         store = GTK_LIST_STORE (gtk_tree_view_get_model (view));
471
472         codes = empathy_spell_get_language_codes ();
473
474         g_settings_set_boolean (priv->gsettings_chat,
475                                 EMPATHY_PREFS_CHAT_SPELL_CHECKER_ENABLED,
476                                 codes != NULL);
477         if (!codes) {
478                 gtk_widget_set_sensitive (priv->treeview_spell_checker, FALSE);
479         }
480
481         for (l = codes; l; l = l->next) {
482                 GtkTreeIter  iter;
483                 const gchar *code;
484                 const gchar *name;
485
486                 code = l->data;
487                 name = empathy_spell_get_language_name (code);
488                 if (!name) {
489                         continue;
490                 }
491
492                 gtk_list_store_append (store, &iter);
493                 gtk_list_store_set (store, &iter,
494                                     COL_LANG_CODE, code,
495                                     COL_LANG_NAME, name,
496                                     -1);
497         }
498
499         empathy_spell_free_language_codes (codes);
500 }
501
502 static void
503 preferences_languages_save (EmpathyPreferences *preferences)
504 {
505         EmpathyPreferencesPriv *priv = GET_PRIV (preferences);
506         GtkTreeView       *view;
507         GtkTreeModel      *model;
508
509         gchar             *languages = NULL;
510
511         view = GTK_TREE_VIEW (priv->treeview_spell_checker);
512         model = gtk_tree_view_get_model (view);
513
514         gtk_tree_model_foreach (model,
515                                 (GtkTreeModelForeachFunc) preferences_languages_save_foreach,
516                                 &languages);
517
518         /* if user selects no languages, we don't want spell check */
519         g_settings_set_boolean (priv->gsettings_chat,
520                                 EMPATHY_PREFS_CHAT_SPELL_CHECKER_ENABLED,
521                                 languages != NULL);
522
523         g_settings_set_string (priv->gsettings_chat,
524                                EMPATHY_PREFS_CHAT_SPELL_CHECKER_LANGUAGES,
525                                languages != NULL ? languages : "");
526
527         g_free (languages);
528 }
529
530 static gboolean
531 preferences_languages_save_foreach (GtkTreeModel  *model,
532                                     GtkTreePath   *path,
533                                     GtkTreeIter   *iter,
534                                     gchar        **languages)
535 {
536         gboolean  enabled;
537         gchar    *code;
538
539         if (!languages) {
540                 return TRUE;
541         }
542
543         gtk_tree_model_get (model, iter, COL_LANG_ENABLED, &enabled, -1);
544         if (!enabled) {
545                 return FALSE;
546         }
547
548         gtk_tree_model_get (model, iter, COL_LANG_CODE, &code, -1);
549         if (!code) {
550                 return FALSE;
551         }
552
553         if (!(*languages)) {
554                 *languages = g_strdup (code);
555         } else {
556                 gchar *str = *languages;
557                 *languages = g_strdup_printf ("%s,%s", str, code);
558                 g_free (str);
559         }
560
561         g_free (code);
562
563         return FALSE;
564 }
565
566 static void
567 preferences_languages_load (EmpathyPreferences *preferences)
568 {
569         EmpathyPreferencesPriv *priv = GET_PRIV (preferences);
570         GtkTreeView   *view;
571         GtkTreeModel  *model;
572         gchar         *value;
573         gchar        **vlanguages;
574
575         value = g_settings_get_string (priv->gsettings_chat,
576                                        EMPATHY_PREFS_CHAT_SPELL_CHECKER_LANGUAGES);
577
578         if (value == NULL)
579                 return;
580
581         vlanguages = g_strsplit (value, ",", -1);
582         g_free (value);
583
584         view = GTK_TREE_VIEW (priv->treeview_spell_checker);
585         model = gtk_tree_view_get_model (view);
586
587         gtk_tree_model_foreach (model,
588                                 (GtkTreeModelForeachFunc) preferences_languages_load_foreach,
589                                 vlanguages);
590
591         g_strfreev (vlanguages);
592 }
593
594 static gboolean
595 preferences_languages_load_foreach (GtkTreeModel  *model,
596                                     GtkTreePath   *path,
597                                     GtkTreeIter   *iter,
598                                     gchar        **languages)
599 {
600         gchar    *code;
601         gchar    *lang;
602         gint      i;
603         gboolean  found = FALSE;
604
605         if (!languages) {
606                 return TRUE;
607         }
608
609         gtk_tree_model_get (model, iter, COL_LANG_CODE, &code, -1);
610         if (!code) {
611                 return FALSE;
612         }
613
614         for (i = 0, lang = languages[i]; lang; lang = languages[++i]) {
615                 if (!tp_strdiff (lang, code)) {
616                         found = TRUE;
617                 }
618         }
619
620         g_free (code);
621         gtk_list_store_set (GTK_LIST_STORE (model), iter, COL_LANG_ENABLED, found, -1);
622         return FALSE;
623 }
624
625 static void
626 preferences_languages_cell_toggled_cb (GtkCellRendererToggle *cell,
627                                        gchar                 *path_string,
628                                        EmpathyPreferences     *preferences)
629 {
630         EmpathyPreferencesPriv *priv = GET_PRIV (preferences);
631         GtkTreeView  *view;
632         GtkTreeModel *model;
633         GtkListStore *store;
634         GtkTreePath  *path;
635         GtkTreeIter   iter;
636         gboolean      enabled;
637
638         view = GTK_TREE_VIEW (priv->treeview_spell_checker);
639         model = gtk_tree_view_get_model (view);
640         store = GTK_LIST_STORE (model);
641
642         path = gtk_tree_path_new_from_string (path_string);
643
644         gtk_tree_model_get_iter (model, &iter, path);
645         gtk_tree_model_get (model, &iter, COL_LANG_ENABLED, &enabled, -1);
646
647         enabled ^= 1;
648
649         gtk_list_store_set (store, &iter, COL_LANG_ENABLED, enabled, -1);
650         gtk_tree_path_free (path);
651
652         preferences_languages_save (preferences);
653 }
654
655 static void
656 preferences_theme_notify_cb (GSettings   *gsettings,
657                              const gchar *key,
658                              gpointer     user_data)
659 {
660         EmpathyPreferences *preferences = user_data;
661         EmpathyPreferencesPriv *priv = GET_PRIV (preferences);
662         GtkComboBox        *combo;
663         gchar              *conf_name;
664         gchar              *conf_path;
665         GtkTreeModel       *model;
666         GtkTreeIter         iter;
667         gboolean            found = FALSE;
668
669         conf_name = g_settings_get_string (gsettings, EMPATHY_PREFS_CHAT_THEME);
670         conf_path = g_settings_get_string (gsettings, EMPATHY_PREFS_CHAT_ADIUM_PATH);
671
672         combo = GTK_COMBO_BOX (priv->combobox_chat_theme);
673         model = gtk_combo_box_get_model (combo);
674         if (gtk_tree_model_get_iter_first (model, &iter)) {
675                 gboolean is_adium;
676                 gchar *name;
677                 gchar *path;
678
679                 do {
680                         gtk_tree_model_get (model, &iter,
681                                             COL_COMBO_IS_ADIUM, &is_adium,
682                                             COL_COMBO_NAME, &name,
683                                             COL_COMBO_PATH, &path,
684                                             -1);
685
686                         if (!tp_strdiff (name, conf_name)) {
687                                 if (tp_strdiff (name, "adium") ||
688                                     !tp_strdiff (path, conf_path)) {
689                                         found = TRUE;
690                                         gtk_combo_box_set_active_iter (combo, &iter);
691                                         g_free (name);
692                                         g_free (path);
693                                         break;
694                                 }
695                         }
696
697                         g_free (name);
698                         g_free (path);
699                 } while (gtk_tree_model_iter_next (model, &iter));
700         }
701
702         /* Fallback to the first one. */
703         if (!found) {
704                 if (gtk_tree_model_get_iter_first (model, &iter)) {
705                         gtk_combo_box_set_active_iter (combo, &iter);
706                 }
707         }
708
709         g_free (conf_name);
710         g_free (conf_path);
711 }
712
713 static void
714 preferences_theme_changed_cb (GtkComboBox        *combo,
715                               EmpathyPreferences *preferences)
716 {
717         EmpathyPreferencesPriv *priv = GET_PRIV (preferences);
718         GtkTreeModel *model;
719         GtkTreeIter   iter;
720         gboolean      is_adium;
721         gchar        *name;
722         gchar        *path;
723
724         if (gtk_combo_box_get_active_iter (combo, &iter)) {
725                 model = gtk_combo_box_get_model (combo);
726
727                 gtk_tree_model_get (model, &iter,
728                                     COL_COMBO_IS_ADIUM, &is_adium,
729                                     COL_COMBO_NAME, &name,
730                                     COL_COMBO_PATH, &path,
731                                     -1);
732
733                 g_settings_set_string (priv->gsettings_chat,
734                                        EMPATHY_PREFS_CHAT_THEME,
735                                        name);
736                 if (is_adium == TRUE)
737                         g_settings_set_string (priv->gsettings_chat,
738                                                EMPATHY_PREFS_CHAT_ADIUM_PATH,
739                                                path);
740                 g_free (name);
741                 g_free (path);
742         }
743 }
744
745 static void
746 preferences_themes_setup (EmpathyPreferences *preferences)
747 {
748         EmpathyPreferencesPriv *priv = GET_PRIV (preferences);
749         GtkComboBox   *combo;
750         GtkCellLayout *cell_layout;
751         GtkCellRenderer *renderer;
752         GtkListStore  *store;
753         const gchar  **themes;
754         GList         *adium_themes;
755         gint           i;
756
757         combo = GTK_COMBO_BOX (priv->combobox_chat_theme);
758         cell_layout = GTK_CELL_LAYOUT (combo);
759
760         /* Create the model */
761         store = gtk_list_store_new (COL_COMBO_COUNT,
762                                     G_TYPE_BOOLEAN, /* Is an Adium theme */
763                                     G_TYPE_STRING,  /* Display name */
764                                     G_TYPE_STRING,  /* Theme name */
765                                     G_TYPE_STRING); /* Theme path */
766         gtk_tree_sortable_set_sort_column_id (GTK_TREE_SORTABLE (store),
767                 COL_COMBO_VISIBLE_NAME, GTK_SORT_ASCENDING);
768
769         /* Fill the model */
770         themes = empathy_theme_manager_get_themes ();
771         for (i = 0; themes[i]; i += 2) {
772                 gtk_list_store_insert_with_values (store, NULL, -1,
773                         COL_COMBO_IS_ADIUM, FALSE,
774                         COL_COMBO_VISIBLE_NAME, _(themes[i + 1]),
775                         COL_COMBO_NAME, themes[i],
776                         COL_COMBO_PATH, NULL,
777                         -1);
778         }
779
780         adium_themes = empathy_theme_manager_get_adium_themes ();
781         while (adium_themes != NULL) {
782                 GHashTable *info;
783                 const gchar *name;
784                 const gchar *path;
785
786                 info = adium_themes->data;
787                 name = tp_asv_get_string (info, "CFBundleName");
788                 path = tp_asv_get_string (info, "path");
789
790                 if (name != NULL && path != NULL) {
791                         gtk_list_store_insert_with_values (store, NULL, -1,
792                                 COL_COMBO_IS_ADIUM, TRUE,
793                                 COL_COMBO_VISIBLE_NAME, name,
794                                 COL_COMBO_NAME, "adium",
795                                 COL_COMBO_PATH, path,
796                                 -1);
797                 }
798                 g_hash_table_unref (info);
799                 adium_themes = g_list_delete_link (adium_themes, adium_themes);
800         }
801
802         /* Add cell renderer */
803         renderer = gtk_cell_renderer_text_new ();
804         gtk_cell_layout_pack_start (cell_layout, renderer, TRUE);
805         gtk_cell_layout_set_attributes (cell_layout, renderer,
806                 "text", COL_COMBO_VISIBLE_NAME, NULL);
807
808         gtk_combo_box_set_model (combo, GTK_TREE_MODEL (store));
809         g_object_unref (store);
810
811         g_signal_connect (combo, "changed",
812                           G_CALLBACK (preferences_theme_changed_cb),
813                           preferences);
814
815         /* Select the theme from the GSetting key and track changes */
816         preferences_theme_notify_cb (priv->gsettings_chat,
817                                      EMPATHY_PREFS_CHAT_THEME,
818                                      preferences);
819         g_signal_connect (priv->gsettings_chat,
820                           "changed::" EMPATHY_PREFS_CHAT_THEME,
821                           G_CALLBACK (preferences_theme_notify_cb),
822                           preferences);
823
824         g_signal_connect (priv->gsettings_chat,
825                           "changed::" EMPATHY_PREFS_CHAT_ADIUM_PATH,
826                           G_CALLBACK (preferences_theme_notify_cb),
827                           preferences);
828 }
829
830 static void
831 empathy_preferences_response (GtkDialog *widget,
832                               gint response)
833 {
834         gtk_widget_destroy (GTK_WIDGET (widget));
835 }
836
837 static void
838 empathy_preferences_finalize (GObject *self)
839 {
840         EmpathyPreferencesPriv *priv = GET_PRIV (self);
841
842         g_object_unref (priv->gsettings);
843         g_object_unref (priv->gsettings_chat);
844         g_object_unref (priv->gsettings_loc);
845         g_object_unref (priv->gsettings_notify);
846         g_object_unref (priv->gsettings_sound);
847         g_object_unref (priv->gsettings_ui);
848         g_object_unref (priv->gsettings_logger);
849
850         G_OBJECT_CLASS (empathy_preferences_parent_class)->finalize (self);
851 }
852
853 static void
854 empathy_preferences_class_init (EmpathyPreferencesClass *klass)
855 {
856         GtkDialogClass *dialog_class = GTK_DIALOG_CLASS (klass);
857         GObjectClass *object_class = G_OBJECT_CLASS (klass);
858
859         dialog_class->response = empathy_preferences_response;
860
861         object_class->finalize = empathy_preferences_finalize;
862
863         g_type_class_add_private (object_class,
864                                   sizeof (EmpathyPreferencesPriv));
865 }
866
867 static void
868 empathy_preferences_init (EmpathyPreferences *preferences)
869 {
870         EmpathyPreferencesPriv    *priv;
871         GtkBuilder                *gui;
872         gchar                     *filename;
873         GtkWidget                 *page;
874
875         priv = preferences->priv = G_TYPE_INSTANCE_GET_PRIVATE (preferences,
876                         EMPATHY_TYPE_PREFERENCES, EmpathyPreferencesPriv);
877
878         gtk_dialog_add_button (GTK_DIALOG (preferences),
879                                GTK_STOCK_CLOSE, GTK_RESPONSE_CLOSE);
880
881         gtk_container_set_border_width (GTK_CONTAINER (preferences), 5);
882         gtk_window_set_title (GTK_WINDOW (preferences), _("Preferences"));
883         gtk_window_set_role (GTK_WINDOW (preferences), "preferences");
884         gtk_window_set_position (GTK_WINDOW (preferences),
885                                  GTK_WIN_POS_CENTER_ON_PARENT);
886         gtk_window_set_icon_name (GTK_WINDOW (preferences), "gtk-preferences");
887
888         /* FIXME: Remove this once we unconditionally depend on GTK+ 3 */
889 #ifndef HAVE_GTK3
890         gtk_dialog_set_has_separator (GTK_DIALOG (preferences), FALSE);
891 #endif
892
893         filename = empathy_file_lookup ("empathy-preferences.ui", "src");
894         gui = empathy_builder_get_file (filename,
895                 "notebook", &priv->notebook,
896                 "checkbutton_show_smileys", &priv->checkbutton_show_smileys,
897                 "checkbutton_show_contacts_in_rooms", &priv->checkbutton_show_contacts_in_rooms,
898                 "combobox_chat_theme", &priv->combobox_chat_theme,
899                 "checkbutton_separate_chat_windows", &priv->checkbutton_separate_chat_windows,
900                 "checkbutton_events_notif_area", &priv->checkbutton_events_notif_area,
901                 "checkbutton_autoconnect", &priv->checkbutton_autoconnect,
902                 "checkbutton_logging", &priv->checkbutton_logging,
903                 "checkbutton_notifications_enabled", &priv->checkbutton_notifications_enabled,
904                 "checkbutton_notifications_disabled_away", &priv->checkbutton_notifications_disabled_away,
905                 "checkbutton_notifications_focus", &priv->checkbutton_notifications_focus,
906                 "checkbutton_notifications_contact_signin", &priv->checkbutton_notifications_contact_signin,
907                 "checkbutton_notifications_contact_signout", &priv->checkbutton_notifications_contact_signout,
908                 "checkbutton_sounds_enabled", &priv->checkbutton_sounds_enabled,
909                 "checkbutton_sounds_disabled_away", &priv->checkbutton_sounds_disabled_away,
910                 "treeview_sounds", &priv->treeview_sounds,
911                 "treeview_spell_checker", &priv->treeview_spell_checker,
912                 "checkbutton_location_publish", &priv->checkbutton_location_publish,
913                 "checkbutton_location_reduce_accuracy", &priv->checkbutton_location_reduce_accuracy,
914                 "checkbutton_location_resource_network", &priv->checkbutton_location_resource_network,
915                 "checkbutton_location_resource_cell", &priv->checkbutton_location_resource_cell,
916                 "checkbutton_location_resource_gps", &priv->checkbutton_location_resource_gps,
917                 NULL);
918         g_free (filename);
919
920         gtk_container_add (GTK_CONTAINER (gtk_dialog_get_content_area (GTK_DIALOG (preferences))), priv->notebook);
921         gtk_widget_show (priv->notebook);
922
923         g_object_unref (gui);
924
925         priv->gsettings = g_settings_new (EMPATHY_PREFS_SCHEMA);
926         priv->gsettings_chat = g_settings_new (EMPATHY_PREFS_CHAT_SCHEMA);
927         priv->gsettings_loc = g_settings_new (EMPATHY_PREFS_LOCATION_SCHEMA);
928         priv->gsettings_notify = g_settings_new (EMPATHY_PREFS_NOTIFICATIONS_SCHEMA);
929         priv->gsettings_sound = g_settings_new (EMPATHY_PREFS_SOUNDS_SCHEMA);
930         priv->gsettings_ui = g_settings_new (EMPATHY_PREFS_UI_SCHEMA);
931         priv->gsettings_logger = g_settings_new (EMPATHY_PREFS_LOGGER_SCHEMA);
932
933         preferences_themes_setup (preferences);
934
935         preferences_setup_widgets (preferences);
936
937         preferences_languages_setup (preferences);
938         preferences_languages_add (preferences);
939         preferences_languages_load (preferences);
940
941         preferences_sound_setup (preferences);
942         preferences_sound_load (preferences);
943
944         if (empathy_spell_supported ()) {
945                 page = gtk_notebook_get_nth_page (GTK_NOTEBOOK (priv->notebook), 2);
946                 gtk_widget_show (page);
947         }
948
949         page = gtk_notebook_get_nth_page (GTK_NOTEBOOK (priv->notebook), 3);
950 #ifdef HAVE_GEOCLUE
951         gtk_widget_show (page);
952 #else
953         gtk_widget_hide (page);
954 #endif
955 }
956
957 GtkWidget *
958 empathy_preferences_new (GtkWindow *parent)
959 {
960         GtkWidget *self;
961
962         g_return_val_if_fail (parent == NULL || GTK_IS_WINDOW (parent), NULL);
963
964         self = g_object_new (EMPATHY_TYPE_PREFERENCES, NULL);
965
966         if (parent != NULL) {
967                 gtk_window_set_transient_for (GTK_WINDOW (self),
968                                               parent);
969         }
970
971         return self;
972 }