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