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