Fix broken markup in Finnish user docs translation
[nijm-empathy.git] / src / empathy-preferences.c
blobc2a0fb046207e7b286aa145c339d1cd402b00c13
1 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
2 /*
3 * Copyright (C) 2003-2007 Imendio AB
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.
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.
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
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>
26 #include "config.h"
27 #include "empathy-preferences.h"
29 #include <glib/gi18n.h>
30 #include <tp-account-widgets/tpaw-builder.h>
31 #include <telepathy-glib/telepathy-glib-dbus.h>
33 #include "empathy-client-factory.h"
34 #include "empathy-gsettings.h"
35 #include "empathy-spell.h"
36 #include "empathy-theme-manager.h"
37 #include "empathy-ui-utils.h"
38 #include "empathy-utils.h"
40 #define DEBUG_FLAG EMPATHY_DEBUG_OTHER
41 #include "empathy-debug.h"
43 G_DEFINE_TYPE (EmpathyPreferences, empathy_preferences, GTK_TYPE_DIALOG);
45 #define GET_PRIV(self) ((EmpathyPreferencesPriv *)((EmpathyPreferences *) self)->priv)
47 static const gchar * empathy_preferences_tabs[] =
49 "general",
50 "notifications",
51 "sounds",
52 "calls",
53 "location",
54 "spell",
55 "themes",
58 struct _EmpathyPreferencesPriv {
59 GtkWidget *notebook;
61 GtkWidget *label_general_behavior;
62 GtkWidget *checkbutton_events_notif_area;
63 GtkWidget *checkbutton_autoconnect;
65 GtkWidget *treeview_sounds;
66 GtkWidget *treeview_spell_checker;
68 GtkWidget *vbox_chat_theme;
69 GtkWidget *combobox_chat_theme;
70 GtkWidget *combobox_chat_theme_variant;
71 GtkWidget *hbox_chat_theme_variant;
72 GtkWidget *sw_chat_theme_preview;
73 EmpathyThemeAdium *chat_theme_preview;
74 EmpathyThemeManager *theme_manager;
76 GSettings *gsettings;
77 GSettings *gsettings_chat;
78 GSettings *gsettings_call;
79 GSettings *gsettings_loc;
80 GSettings *gsettings_notify;
81 GSettings *gsettings_sound;
82 GSettings *gsettings_ui;
83 GSettings *gsettings_logger;
86 static void preferences_setup_widgets (EmpathyPreferences *preferences,
87 GtkBuilder *gui);
88 static void preferences_languages_setup (EmpathyPreferences *preferences);
89 static void preferences_languages_add (EmpathyPreferences *preferences);
90 static void preferences_languages_save (EmpathyPreferences *preferences);
91 static gboolean preferences_languages_save_foreach (GtkTreeModel *model,
92 GtkTreePath *path,
93 GtkTreeIter *iter,
94 gchar **languages);
95 static void preferences_languages_load (EmpathyPreferences *preferences);
96 static gboolean preferences_languages_load_foreach (GtkTreeModel *model,
97 GtkTreePath *path,
98 GtkTreeIter *iter,
99 GList *languages);
100 static void preferences_languages_cell_toggled_cb (GtkCellRendererToggle *cell,
101 gchar *path_string,
102 EmpathyPreferences *preferences);
104 enum {
105 COL_LANG_ENABLED,
106 COL_LANG_CODE,
107 COL_LANG_NAME,
108 COL_LANG_COUNT
111 enum {
112 COL_THEME_VISIBLE_NAME,
113 COL_THEME_ADIUM_NAME,
114 COL_THEME_ADIUM_INFO,
115 COL_THEME_COUNT
118 enum {
119 COL_VARIANT_NAME,
120 COL_VARIANT_DEFAULT,
121 COL_VARIANT_COUNT
124 enum {
125 COL_SOUND_ENABLED,
126 COL_SOUND_NAME,
127 COL_SOUND_KEY,
128 COL_SOUND_COUNT
131 typedef struct {
132 const char *name;
133 const char *key;
134 } SoundEventEntry;
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 comes 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 }
147 static void
148 preferences_setup_widgets (EmpathyPreferences *preferences,
149 GtkBuilder *gui)
151 EmpathyPreferencesPriv *priv = GET_PRIV (preferences);
152 #define BIND_ACTIVE(schema, key, widget) \
153 g_settings_bind (priv->gsettings_##schema, EMPATHY_PREFS_##key, \
154 gtk_builder_get_object (gui, widget), "active", \
155 G_SETTINGS_BIND_DEFAULT);
157 BIND_ACTIVE (notify, NOTIFICATIONS_ENABLED,
158 "checkbutton_notifications_enabled");
159 BIND_ACTIVE (notify, NOTIFICATIONS_DISABLED_AWAY,
160 "checkbutton_notifications_disabled_away");
161 BIND_ACTIVE (notify, NOTIFICATIONS_FOCUS,
162 "checkbutton_notifications_focus");
163 BIND_ACTIVE (notify, NOTIFICATIONS_CONTACT_SIGNIN,
164 "checkbutton_notifications_contact_signin");
165 BIND_ACTIVE (notify, NOTIFICATIONS_CONTACT_SIGNOUT,
166 "checkbutton_notifications_contact_signout");
168 BIND_ACTIVE (sound, SOUNDS_ENABLED,
169 "checkbutton_sounds_enabled");
170 BIND_ACTIVE (sound, SOUNDS_DISABLED_AWAY,
171 "checkbutton_sounds_disabled_away");
173 BIND_ACTIVE (ui, UI_SHOW_OFFLINE,
174 "checkbutton_show_offline");
175 BIND_ACTIVE (ui, UI_SHOW_GROUPS,
176 "checkbutton_show_groups");
177 BIND_ACTIVE (ui, UI_SEPARATE_CHAT_WINDOWS,
178 "radiobutton_chats_new_windows");
179 BIND_ACTIVE (ui, UI_EVENTS_NOTIFY_AREA,
180 "checkbutton_events_notif_area");
181 BIND_ACTIVE (ui, UI_SHOW_BALANCES,
182 "checkbutton_show_balances");
184 BIND_ACTIVE (chat, CHAT_SHOW_SMILEYS,
185 "checkbutton_show_smileys");
186 BIND_ACTIVE (chat, CHAT_SHOW_CONTACTS_IN_ROOMS,
187 "checkbutton_show_contacts_in_rooms");
189 BIND_ACTIVE (call, CALL_ECHO_CANCELLATION,
190 "call_echo_cancellation");
192 BIND_ACTIVE (loc, LOCATION_PUBLISH,
193 "checkbutton_location_publish");
194 BIND_ACTIVE (loc, LOCATION_REDUCE_ACCURACY,
195 "checkbutton_location_reduce_accuracy");
197 BIND_ACTIVE (logger, LOGGER_ENABLED,
198 "checkbutton_logging");
200 #undef BIND_ACTIVE
202 #define BIND_SENSITIVE(schema, key, widget) \
203 g_settings_bind (priv->gsettings_##schema, EMPATHY_PREFS_##key, \
204 gtk_builder_get_object (gui, widget), "sensitive", \
205 G_SETTINGS_BIND_GET);
206 /* N.B. BIND_SENSITIVE() is in addition to the sensitivity setting of
207 * BIND_ACTIVE() */
208 BIND_SENSITIVE (notify, NOTIFICATIONS_ENABLED,
209 "checkbutton_notifications_disabled_away");
210 BIND_SENSITIVE (notify, NOTIFICATIONS_ENABLED,
211 "checkbutton_notifications_focus");
212 BIND_SENSITIVE (notify, NOTIFICATIONS_ENABLED,
213 "checkbutton_notifications_contact_signin");
214 BIND_SENSITIVE (notify, NOTIFICATIONS_ENABLED,
215 "checkbutton_notifications_contact_signout");
217 BIND_SENSITIVE (sound, SOUNDS_ENABLED,
218 "checkbutton_sounds_disabled_away");
219 BIND_SENSITIVE (sound, SOUNDS_ENABLED,
220 "treeview_sounds");
222 BIND_SENSITIVE (loc, LOCATION_PUBLISH,
223 "checkbutton_location_reduce_accuracy");
225 #undef BIND_SENSITIVE
227 g_settings_bind (priv->gsettings,
228 EMPATHY_PREFS_AUTOCONNECT,
229 gtk_builder_get_object (gui,
230 "checkbutton_autoconnect"),
231 "active",
232 G_SETTINGS_BIND_DEFAULT);
235 static void
236 preferences_sound_cell_toggled_cb (GtkCellRendererToggle *toggle,
237 char *path_string,
238 EmpathyPreferences *preferences)
240 EmpathyPreferencesPriv *priv = GET_PRIV (preferences);
241 GtkTreePath *path;
242 gboolean instore;
243 GtkTreeIter iter;
244 GtkTreeView *view;
245 GtkTreeModel *model;
246 char *key;
248 view = GTK_TREE_VIEW (priv->treeview_sounds);
249 model = gtk_tree_view_get_model (view);
251 path = gtk_tree_path_new_from_string (path_string);
253 gtk_tree_model_get_iter (model, &iter, path);
254 gtk_tree_model_get (model, &iter, COL_SOUND_KEY, &key,
255 COL_SOUND_ENABLED, &instore, -1);
257 instore ^= 1;
259 gtk_list_store_set (GTK_LIST_STORE (model), &iter,
260 COL_SOUND_ENABLED, instore, -1);
262 g_settings_set_boolean (priv->gsettings_sound, key, instore);
264 g_free (key);
265 gtk_tree_path_free (path);
268 static void
269 preferences_sound_load (EmpathyPreferences *preferences)
271 EmpathyPreferencesPriv *priv = GET_PRIV (preferences);
272 guint i;
273 GtkTreeView *view;
274 GtkListStore *store;
275 GtkTreeIter iter;
276 gboolean set;
278 view = GTK_TREE_VIEW (priv->treeview_sounds);
279 store = GTK_LIST_STORE (gtk_tree_view_get_model (view));
281 for (i = 0; i < G_N_ELEMENTS (sound_entries); i++) {
282 set = g_settings_get_boolean (priv->gsettings_sound,
283 sound_entries[i].key);
285 gtk_list_store_insert_with_values (store, &iter, i,
286 COL_SOUND_NAME, gettext (sound_entries[i].name),
287 COL_SOUND_KEY, sound_entries[i].key,
288 COL_SOUND_ENABLED, set, -1);
292 static void
293 preferences_sound_setup (EmpathyPreferences *preferences)
295 EmpathyPreferencesPriv *priv = GET_PRIV (preferences);
296 GtkTreeView *view;
297 GtkListStore *store;
298 GtkCellRenderer *renderer;
299 GtkTreeViewColumn *column;
301 view = GTK_TREE_VIEW (priv->treeview_sounds);
303 store = gtk_list_store_new (COL_SOUND_COUNT,
304 G_TYPE_BOOLEAN, /* enabled */
305 G_TYPE_STRING, /* name */
306 G_TYPE_STRING); /* key */
308 gtk_tree_view_set_model (view, GTK_TREE_MODEL (store));
310 renderer = gtk_cell_renderer_toggle_new ();
311 g_signal_connect (renderer, "toggled",
312 G_CALLBACK (preferences_sound_cell_toggled_cb),
313 preferences);
315 column = gtk_tree_view_column_new ();
316 gtk_tree_view_column_pack_start (column, renderer, FALSE);
317 gtk_tree_view_column_add_attribute (column, renderer,
318 "active", COL_SOUND_ENABLED);
320 renderer = gtk_cell_renderer_text_new ();
321 gtk_tree_view_column_pack_start (column, renderer, FALSE);
322 gtk_tree_view_column_add_attribute (column, renderer,
323 "text", COL_SOUND_NAME);
325 gtk_tree_view_append_column (view, column);
327 gtk_tree_view_column_set_resizable (column, FALSE);
328 gtk_tree_view_column_set_clickable (GTK_TREE_VIEW_COLUMN (column), TRUE);
330 g_object_unref (store);
333 static void
334 preferences_languages_setup (EmpathyPreferences *preferences)
336 EmpathyPreferencesPriv *priv = GET_PRIV (preferences);
337 GtkTreeView *view;
338 GtkListStore *store;
339 GtkTreeSelection *selection;
340 GtkTreeViewColumn *column;
341 GtkCellRenderer *renderer;
342 guint col_offset;
344 view = GTK_TREE_VIEW (priv->treeview_spell_checker);
346 store = gtk_list_store_new (COL_LANG_COUNT,
347 G_TYPE_BOOLEAN, /* enabled */
348 G_TYPE_STRING, /* code */
349 G_TYPE_STRING); /* name */
351 gtk_tree_view_set_model (view, GTK_TREE_MODEL (store));
353 selection = gtk_tree_view_get_selection (view);
354 gtk_tree_selection_set_mode (selection, GTK_SELECTION_SINGLE);
356 renderer = gtk_cell_renderer_toggle_new ();
357 g_signal_connect (renderer, "toggled",
358 G_CALLBACK (preferences_languages_cell_toggled_cb),
359 preferences);
361 column = gtk_tree_view_column_new_with_attributes (NULL, renderer,
362 "active", COL_LANG_ENABLED,
363 NULL);
365 gtk_tree_view_append_column (view, column);
367 renderer = gtk_cell_renderer_text_new ();
368 col_offset = gtk_tree_view_insert_column_with_attributes (view,
369 -1, _("Language"),
370 renderer,
371 "text", COL_LANG_NAME,
372 NULL);
374 g_object_set_data (G_OBJECT (renderer),
375 "column", GINT_TO_POINTER (COL_LANG_NAME));
377 column = gtk_tree_view_get_column (view, col_offset - 1);
378 gtk_tree_view_column_set_sort_column_id (column, COL_LANG_NAME);
379 gtk_tree_view_column_set_resizable (column, FALSE);
380 gtk_tree_view_column_set_clickable (GTK_TREE_VIEW_COLUMN (column), TRUE);
382 g_object_unref (store);
385 static void
386 preferences_languages_add (EmpathyPreferences *preferences)
388 EmpathyPreferencesPriv *priv = GET_PRIV (preferences);
389 GtkTreeView *view;
390 GtkListStore *store;
391 GList *codes, *l;
393 view = GTK_TREE_VIEW (priv->treeview_spell_checker);
394 store = GTK_LIST_STORE (gtk_tree_view_get_model (view));
396 codes = empathy_spell_get_language_codes ();
398 if (!codes) {
399 gtk_widget_set_sensitive (priv->treeview_spell_checker, FALSE);
402 for (l = codes; l; l = l->next) {
403 const gchar *code;
404 const gchar *name;
406 code = l->data;
407 name = empathy_spell_get_language_name (code);
408 if (!name) {
409 continue;
412 gtk_list_store_insert_with_values (store, NULL, -1,
413 COL_LANG_CODE, code,
414 COL_LANG_NAME, name,
415 -1);
418 empathy_spell_free_language_codes (codes);
421 static void
422 preferences_languages_save (EmpathyPreferences *preferences)
424 EmpathyPreferencesPriv *priv = GET_PRIV (preferences);
425 GtkTreeView *view;
426 GtkTreeModel *model;
428 gchar *languages = NULL;
430 view = GTK_TREE_VIEW (priv->treeview_spell_checker);
431 model = gtk_tree_view_get_model (view);
433 gtk_tree_model_foreach (model,
434 (GtkTreeModelForeachFunc) preferences_languages_save_foreach,
435 &languages);
437 /* if user selects no languages, we don't want spell check */
438 g_settings_set_boolean (priv->gsettings_chat,
439 EMPATHY_PREFS_CHAT_SPELL_CHECKER_ENABLED,
440 languages != NULL);
442 g_settings_set_string (priv->gsettings_chat,
443 EMPATHY_PREFS_CHAT_SPELL_CHECKER_LANGUAGES,
444 languages != NULL ? languages : "");
446 g_free (languages);
449 static gboolean
450 preferences_languages_save_foreach (GtkTreeModel *model,
451 GtkTreePath *path,
452 GtkTreeIter *iter,
453 gchar **languages)
455 gboolean enabled;
456 gchar *code;
458 if (!languages) {
459 return TRUE;
462 gtk_tree_model_get (model, iter, COL_LANG_ENABLED, &enabled, -1);
463 if (!enabled) {
464 return FALSE;
467 gtk_tree_model_get (model, iter, COL_LANG_CODE, &code, -1);
468 if (!code) {
469 return FALSE;
472 if (!(*languages)) {
473 *languages = g_strdup (code);
474 } else {
475 gchar *str = *languages;
476 *languages = g_strdup_printf ("%s,%s", str, code);
477 g_free (str);
480 g_free (code);
482 return FALSE;
485 static void
486 preferences_languages_load (EmpathyPreferences *preferences)
488 EmpathyPreferencesPriv *priv = GET_PRIV (preferences);
489 GtkTreeView *view;
490 GtkTreeModel *model;
491 GList *enabled_codes;
493 enabled_codes = empathy_spell_get_enabled_language_codes ();
495 g_settings_set_boolean (priv->gsettings_chat,
496 EMPATHY_PREFS_CHAT_SPELL_CHECKER_ENABLED,
497 enabled_codes != NULL);
499 if (enabled_codes == NULL)
500 return;
502 view = GTK_TREE_VIEW (priv->treeview_spell_checker);
503 model = gtk_tree_view_get_model (view);
505 gtk_tree_model_foreach (model,
506 (GtkTreeModelForeachFunc) preferences_languages_load_foreach,
507 enabled_codes);
509 g_list_free (enabled_codes);
512 static gboolean
513 preferences_languages_load_foreach (GtkTreeModel *model,
514 GtkTreePath *path,
515 GtkTreeIter *iter,
516 GList *languages)
518 gchar *code;
519 gboolean found = FALSE;
521 if (!languages) {
522 return TRUE;
525 gtk_tree_model_get (model, iter, COL_LANG_CODE, &code, -1);
526 if (!code) {
527 return FALSE;
530 if (g_list_find_custom (languages, code, (GCompareFunc) strcmp)) {
531 found = TRUE;
534 g_free (code);
535 gtk_list_store_set (GTK_LIST_STORE (model), iter, COL_LANG_ENABLED, found, -1);
536 return FALSE;
539 static void
540 preferences_languages_cell_toggled_cb (GtkCellRendererToggle *cell,
541 gchar *path_string,
542 EmpathyPreferences *preferences)
544 EmpathyPreferencesPriv *priv = GET_PRIV (preferences);
545 GtkTreeView *view;
546 GtkTreeModel *model;
547 GtkListStore *store;
548 GtkTreePath *path;
549 GtkTreeIter iter;
550 gboolean enabled;
552 view = GTK_TREE_VIEW (priv->treeview_spell_checker);
553 model = gtk_tree_view_get_model (view);
554 store = GTK_LIST_STORE (model);
556 path = gtk_tree_path_new_from_string (path_string);
558 gtk_tree_model_get_iter (model, &iter, path);
559 gtk_tree_model_get (model, &iter, COL_LANG_ENABLED, &enabled, -1);
561 enabled ^= 1;
563 gtk_list_store_set (store, &iter, COL_LANG_ENABLED, enabled, -1);
564 gtk_tree_path_free (path);
566 preferences_languages_save (preferences);
569 static void
570 preferences_preview_theme_append_message (EmpathyThemeAdium *view,
571 EmpathyContact *sender,
572 EmpathyContact *receiver,
573 const gchar *text,
574 gboolean should_highlight)
576 EmpathyMessage *message;
578 message = g_object_new (EMPATHY_TYPE_MESSAGE,
579 "sender", sender,
580 "receiver", receiver,
581 "body", text,
582 NULL);
584 empathy_theme_adium_append_message (view, message, should_highlight);
585 g_object_unref (message);
588 static void
589 preferences_preview_theme_changed_cb (EmpathyThemeManager *manager,
590 EmpathyPreferences *preferences)
592 EmpathyPreferencesPriv *priv = GET_PRIV (preferences);
593 EmpathyClientFactory *factory;
594 TpAccount *account;
595 EmpathyContact *juliet;
596 EmpathyContact *romeo;
598 DEBUG ("Theme changed, update preview widget");
600 if (priv->chat_theme_preview != NULL) {
601 gtk_widget_destroy (GTK_WIDGET (priv->chat_theme_preview));
603 priv->chat_theme_preview = empathy_theme_manager_create_view (manager);
604 if (GTK_IS_SCROLLABLE (priv->chat_theme_preview))
606 gtk_container_add (GTK_CONTAINER (priv->sw_chat_theme_preview),
607 GTK_WIDGET (priv->chat_theme_preview));
609 else
611 gtk_widget_hide (priv->sw_chat_theme_preview);
612 gtk_box_pack_start (GTK_BOX (priv->vbox_chat_theme),
613 GTK_WIDGET (priv->chat_theme_preview),
614 TRUE, TRUE, 0);
616 gtk_widget_show (GTK_WIDGET (priv->chat_theme_preview));
618 /* FIXME: It is ugly to add a fake conversation like that.
619 * Would be cool if we could request a TplLogManager for a fake
620 * conversation */
621 factory = empathy_client_factory_dup ();
623 account = tp_simple_client_factory_ensure_account (
624 TP_SIMPLE_CLIENT_FACTORY (factory),
625 TP_ACCOUNT_OBJECT_PATH_BASE "cm/jabber/account", NULL, NULL);
626 juliet = g_object_new (EMPATHY_TYPE_CONTACT,
627 "account", account,
628 "id", "juliet",
629 /* translators: Contact name for the chat theme preview */
630 "alias", _("Juliet"),
631 "is-user", FALSE,
632 NULL);
633 romeo = g_object_new (EMPATHY_TYPE_CONTACT,
634 "account", account,
635 "id", "romeo",
636 /* translators: Contact name for the chat theme preview */
637 "alias", _("Romeo"),
638 "is-user", TRUE,
639 NULL);
641 preferences_preview_theme_append_message (priv->chat_theme_preview,
642 /* translators: Quote from Romeo & Julier, for chat theme preview */
643 juliet, romeo, _("O Romeo, Romeo, wherefore art thou Romeo?"),
644 TRUE /* this message mentions Romeo */);
645 preferences_preview_theme_append_message (priv->chat_theme_preview,
646 /* translators: Quote from Romeo & Julier, for chat theme preview */
647 juliet, romeo, _("Deny thy father and refuse thy name;"), FALSE);
648 preferences_preview_theme_append_message (priv->chat_theme_preview,
649 /* translators: Quote from Romeo & Julier, for chat theme preview */
650 juliet, romeo, _("Or if thou wilt not, be but sworn my love"), FALSE);
651 preferences_preview_theme_append_message (priv->chat_theme_preview,
652 /* translators: Quote from Romeo & Julier, for chat theme preview */
653 juliet, romeo, _("And I’ll no longer be a Capulet."), FALSE);
654 preferences_preview_theme_append_message (priv->chat_theme_preview,
655 /* translators: Quote from Romeo & Julier, for chat theme preview */
656 romeo, juliet, _("Shall I hear more, or shall I speak at this?"), FALSE);
658 /* translators: Quote from Romeo & Julier, for chat theme preview */
659 empathy_theme_adium_append_event (priv->chat_theme_preview, _("Juliet has disconnected"));
661 g_object_unref (juliet);
662 g_object_unref (romeo);
663 g_object_unref (account);
664 g_object_unref (factory);
667 static void
668 preferences_theme_variant_changed_cb (GtkComboBox *combo,
669 EmpathyPreferences *preferences)
671 EmpathyPreferencesPriv *priv = GET_PRIV (preferences);
672 GtkTreeIter iter;
674 if (gtk_combo_box_get_active_iter (combo, &iter)) {
675 GtkTreeModel *model;
676 gchar *name;
678 model = gtk_combo_box_get_model (combo);
679 gtk_tree_model_get (model, &iter,
680 COL_VARIANT_NAME, &name,
681 -1);
683 g_settings_set_string (priv->gsettings_chat,
684 EMPATHY_PREFS_CHAT_THEME_VARIANT,
685 name);
687 g_free (name);
691 static void
692 preferences_theme_variant_notify_cb (GSettings *gsettings,
693 const gchar *key,
694 gpointer user_data)
696 EmpathyPreferences *preferences = user_data;
697 EmpathyPreferencesPriv *priv = GET_PRIV (preferences);
698 GtkComboBox *combo;
699 gchar *conf_name;
700 GtkTreeModel *model;
701 GtkTreeIter iter;
702 GtkTreeIter default_iter;
703 gboolean found_default = FALSE;
704 gboolean found = FALSE;
705 gboolean ok;
707 conf_name = g_settings_get_string (gsettings, EMPATHY_PREFS_CHAT_THEME_VARIANT);
708 combo = GTK_COMBO_BOX (priv->combobox_chat_theme_variant);
709 model = gtk_combo_box_get_model (combo);
711 for (ok = gtk_tree_model_get_iter_first (model, &iter);
712 ok && !found;
713 ok = gtk_tree_model_iter_next (model, &iter)) {
714 gchar *name;
715 gboolean is_default;
717 gtk_tree_model_get (model, &iter,
718 COL_VARIANT_NAME, &name,
719 COL_VARIANT_DEFAULT, &is_default,
720 -1);
722 if (!tp_strdiff (name, conf_name)) {
723 found = TRUE;
724 gtk_combo_box_set_active_iter (combo, &iter);
726 if (is_default) {
727 found_default = TRUE;
728 default_iter = iter;
731 g_free (name);
734 /* Fallback to the first one. */
735 if (!found) {
736 if (found_default) {
737 gtk_combo_box_set_active_iter (combo, &default_iter);
738 } else if (gtk_tree_model_get_iter_first (model, &iter)) {
739 gtk_combo_box_set_active_iter (combo, &iter);
743 g_free (conf_name);
746 /* return TRUE if we added at least one variant */
747 static gboolean
748 preferences_theme_variants_fill (EmpathyPreferences *preferences,
749 GHashTable *info)
751 EmpathyPreferencesPriv *priv = GET_PRIV (preferences);
752 GtkTreeModel *model;
753 GtkListStore *store;
754 GPtrArray *variants;
755 const gchar *default_variant;
756 guint i;
757 gboolean result = FALSE;
759 model = gtk_combo_box_get_model (GTK_COMBO_BOX (priv->combobox_chat_theme_variant));
760 store = GTK_LIST_STORE (model);
761 gtk_list_store_clear (store);
763 variants = empathy_adium_info_get_available_variants (info);
764 default_variant = empathy_adium_info_get_default_variant (info);
765 for (i = 0; i < variants->len; i++) {
766 gchar *name = g_ptr_array_index (variants, i);
768 gtk_list_store_insert_with_values (store, NULL, -1,
769 COL_VARIANT_NAME, name,
770 COL_VARIANT_DEFAULT, !tp_strdiff (name, default_variant),
771 -1);
773 result = TRUE;
776 /* Select the variant from the GSetting key */
777 preferences_theme_variant_notify_cb (priv->gsettings_chat,
778 EMPATHY_PREFS_CHAT_THEME_VARIANT,
779 preferences);
781 return result;
784 static void
785 preferences_theme_variants_setup (EmpathyPreferences *preferences)
787 EmpathyPreferencesPriv *priv = GET_PRIV (preferences);
788 GtkComboBox *combo;
789 GtkCellLayout *cell_layout;
790 GtkCellRenderer *renderer;
791 GtkListStore *store;
793 combo = GTK_COMBO_BOX (priv->combobox_chat_theme_variant);
794 cell_layout = GTK_CELL_LAYOUT (combo);
796 /* Create the model */
797 store = gtk_list_store_new (COL_VARIANT_COUNT,
798 G_TYPE_STRING, /* name */
799 G_TYPE_BOOLEAN); /* is default */
800 gtk_tree_sortable_set_sort_column_id (GTK_TREE_SORTABLE (store),
801 COL_VARIANT_NAME, GTK_SORT_ASCENDING);
803 /* Add cell renderer */
804 renderer = gtk_cell_renderer_text_new ();
805 gtk_cell_layout_pack_start (cell_layout, renderer, TRUE);
806 gtk_cell_layout_set_attributes (cell_layout, renderer,
807 "text", COL_VARIANT_NAME, NULL);
809 gtk_combo_box_set_model (combo, GTK_TREE_MODEL (store));
810 g_object_unref (store);
812 g_signal_connect (combo, "changed",
813 G_CALLBACK (preferences_theme_variant_changed_cb),
814 preferences);
816 /* Track changes of the GSetting key */
817 g_signal_connect (priv->gsettings_chat,
818 "changed::" EMPATHY_PREFS_CHAT_THEME_VARIANT,
819 G_CALLBACK (preferences_theme_variant_notify_cb),
820 preferences);
823 static void
824 preferences_theme_changed_cb (GtkComboBox *combo,
825 EmpathyPreferences *preferences)
827 EmpathyPreferencesPriv *priv = GET_PRIV (preferences);
828 GtkTreeIter iter;
830 if (gtk_combo_box_get_active_iter (combo, &iter)) {
831 GtkTreeModel *model;
832 gchar *name;
833 GHashTable *info;
834 gboolean variant;
836 model = gtk_combo_box_get_model (combo);
837 gtk_tree_model_get (model, &iter,
838 COL_THEME_ADIUM_NAME, &name,
839 COL_THEME_ADIUM_INFO, &info,
840 -1);
842 g_settings_set_string (priv->gsettings_chat,
843 EMPATHY_PREFS_CHAT_THEME,
844 name);
846 variant = preferences_theme_variants_fill (preferences, info);
847 gtk_widget_set_visible (priv->hbox_chat_theme_variant, variant);
849 g_free (name);
850 tp_clear_pointer (&info, g_hash_table_unref);
854 static void
855 preferences_theme_notify_cb (GSettings *gsettings,
856 const gchar *key,
857 gpointer user_data)
859 EmpathyPreferences *preferences = user_data;
860 EmpathyPreferencesPriv *priv = GET_PRIV (preferences);
861 GtkComboBox *combo;
862 gchar *theme;
863 GtkTreeModel *model;
864 GtkTreeIter iter;
865 gboolean found = FALSE;
866 gboolean ok;
868 theme = g_settings_get_string (gsettings, key);
870 combo = GTK_COMBO_BOX (priv->combobox_chat_theme);
871 model = gtk_combo_box_get_model (combo);
872 for (ok = gtk_tree_model_get_iter_first (model, &iter);
873 ok && !found;
874 ok = gtk_tree_model_iter_next (model, &iter)) {
875 gchar *name;
877 gtk_tree_model_get (model, &iter,
878 COL_THEME_ADIUM_NAME, &name,
879 -1);
881 if (!tp_strdiff (name, theme)) {
882 found = TRUE;
883 gtk_combo_box_set_active_iter (combo, &iter);
886 g_free (name);
889 /* Fallback to the first one. */
890 if (!found) {
891 if (gtk_tree_model_get_iter_first (model, &iter)) {
892 gtk_combo_box_set_active_iter (combo, &iter);
896 g_free (theme);
899 static void
900 preferences_themes_setup (EmpathyPreferences *preferences)
902 EmpathyPreferencesPriv *priv = GET_PRIV (preferences);
903 GtkComboBox *combo;
904 GtkCellLayout *cell_layout;
905 GtkCellRenderer *renderer;
906 GtkListStore *store;
907 GList *adium_themes;
909 preferences_theme_variants_setup (preferences);
911 combo = GTK_COMBO_BOX (priv->combobox_chat_theme);
912 cell_layout = GTK_CELL_LAYOUT (combo);
914 /* Create the model */
915 store = gtk_list_store_new (COL_THEME_COUNT,
916 G_TYPE_STRING, /* Display name */
917 G_TYPE_STRING, /* Adium theme path */
918 G_TYPE_HASH_TABLE); /* Adium theme info */
919 gtk_tree_sortable_set_sort_column_id (GTK_TREE_SORTABLE (store),
920 COL_THEME_VISIBLE_NAME, GTK_SORT_ASCENDING);
922 /* Fill the model */
923 adium_themes = empathy_theme_manager_get_adium_themes ();
924 while (adium_themes != NULL) {
925 GHashTable *info;
926 const gchar *visible_name;
927 gchar *name;
929 info = adium_themes->data;
930 visible_name = tp_asv_get_string (info, "CFBundleName");
931 name = empathy_theme_manager_dup_theme_name_from_path (
932 tp_asv_get_string (info, "path"));
934 if (visible_name != NULL && name != NULL) {
935 gtk_list_store_insert_with_values (store, NULL, -1,
936 COL_THEME_VISIBLE_NAME, visible_name,
937 COL_THEME_ADIUM_NAME, name,
938 COL_THEME_ADIUM_INFO, info,
939 -1);
941 g_hash_table_unref (info);
942 g_free (name);
943 adium_themes = g_list_delete_link (adium_themes, adium_themes);
946 /* Add cell renderer */
947 renderer = gtk_cell_renderer_text_new ();
948 gtk_cell_layout_pack_start (cell_layout, renderer, TRUE);
949 gtk_cell_layout_set_attributes (cell_layout, renderer,
950 "text", COL_THEME_VISIBLE_NAME, NULL);
952 gtk_combo_box_set_model (combo, GTK_TREE_MODEL (store));
953 g_object_unref (store);
955 g_signal_connect (combo, "changed",
956 G_CALLBACK (preferences_theme_changed_cb),
957 preferences);
959 /* Select the theme from the GSetting key and track changes */
960 preferences_theme_notify_cb (priv->gsettings_chat,
961 EMPATHY_PREFS_CHAT_THEME,
962 preferences);
964 g_signal_connect (priv->gsettings_chat,
965 "changed::" EMPATHY_PREFS_CHAT_THEME,
966 G_CALLBACK (preferences_theme_notify_cb),
967 preferences);
970 static void
971 empathy_preferences_response (GtkDialog *widget,
972 gint response)
974 gtk_widget_destroy (GTK_WIDGET (widget));
977 static void
978 empathy_preferences_finalize (GObject *self)
980 EmpathyPreferencesPriv *priv = GET_PRIV (self);
982 g_object_unref (priv->theme_manager);
984 g_object_unref (priv->gsettings);
985 g_object_unref (priv->gsettings_chat);
986 g_object_unref (priv->gsettings_call);
987 g_object_unref (priv->gsettings_loc);
988 g_object_unref (priv->gsettings_notify);
989 g_object_unref (priv->gsettings_sound);
990 g_object_unref (priv->gsettings_ui);
991 g_object_unref (priv->gsettings_logger);
993 G_OBJECT_CLASS (empathy_preferences_parent_class)->finalize (self);
996 static void
997 empathy_preferences_class_init (EmpathyPreferencesClass *klass)
999 GtkDialogClass *dialog_class = GTK_DIALOG_CLASS (klass);
1000 GObjectClass *object_class = G_OBJECT_CLASS (klass);
1002 dialog_class->response = empathy_preferences_response;
1004 object_class->finalize = empathy_preferences_finalize;
1006 g_type_class_add_private (object_class,
1007 sizeof (EmpathyPreferencesPriv));
1010 static void
1011 empathy_preferences_init (EmpathyPreferences *preferences)
1013 EmpathyPreferencesPriv *priv;
1014 GtkBuilder *gui;
1015 gchar *filename;
1016 GtkWidget *page;
1018 priv = preferences->priv = G_TYPE_INSTANCE_GET_PRIVATE (preferences,
1019 EMPATHY_TYPE_PREFERENCES, EmpathyPreferencesPriv);
1021 gtk_dialog_add_button (GTK_DIALOG (preferences),
1022 GTK_STOCK_CLOSE, GTK_RESPONSE_CLOSE);
1024 gtk_container_set_border_width (GTK_CONTAINER (preferences), 5);
1025 gtk_window_set_title (GTK_WINDOW (preferences), _("Preferences"));
1026 gtk_window_set_role (GTK_WINDOW (preferences), "preferences");
1027 gtk_window_set_position (GTK_WINDOW (preferences),
1028 GTK_WIN_POS_CENTER_ON_PARENT);
1029 gtk_window_set_icon_name (GTK_WINDOW (preferences), "preferences-desktop");
1031 filename = empathy_file_lookup ("empathy-preferences.ui", "src");
1032 gui = tpaw_builder_get_file (filename,
1033 "notebook", &priv->notebook,
1034 "vbox_chat_theme", &priv->vbox_chat_theme,
1035 "combobox_chat_theme", &priv->combobox_chat_theme,
1036 "combobox_chat_theme_variant", &priv->combobox_chat_theme_variant,
1037 "hbox_chat_theme_variant", &priv->hbox_chat_theme_variant,
1038 "sw_chat_theme_preview", &priv->sw_chat_theme_preview,
1039 "treeview_sounds", &priv->treeview_sounds,
1040 "treeview_spell_checker", &priv->treeview_spell_checker,
1041 "label_general_behavior", &priv->label_general_behavior,
1042 "checkbutton_events_notif_area", &priv->checkbutton_events_notif_area,
1043 "checkbutton_autoconnect", &priv->checkbutton_autoconnect,
1044 NULL);
1045 g_free (filename);
1047 gtk_container_add (GTK_CONTAINER (gtk_dialog_get_content_area (GTK_DIALOG (preferences))), priv->notebook);
1048 gtk_widget_show (priv->notebook);
1050 priv->gsettings = g_settings_new (EMPATHY_PREFS_SCHEMA);
1051 priv->gsettings_chat = g_settings_new (EMPATHY_PREFS_CHAT_SCHEMA);
1052 priv->gsettings_call = g_settings_new (EMPATHY_PREFS_CALL_SCHEMA);
1053 priv->gsettings_loc = g_settings_new (EMPATHY_PREFS_LOCATION_SCHEMA);
1054 priv->gsettings_notify = g_settings_new (EMPATHY_PREFS_NOTIFICATIONS_SCHEMA);
1055 priv->gsettings_sound = g_settings_new (EMPATHY_PREFS_SOUNDS_SCHEMA);
1056 priv->gsettings_ui = g_settings_new (EMPATHY_PREFS_UI_SCHEMA);
1057 priv->gsettings_logger = g_settings_new (EMPATHY_PREFS_LOGGER_SCHEMA);
1059 /* Create chat theme preview, and track changes */
1060 priv->theme_manager = empathy_theme_manager_dup_singleton ();
1061 tp_g_signal_connect_object (priv->theme_manager, "theme-changed",
1062 G_CALLBACK (preferences_preview_theme_changed_cb),
1063 preferences, 0);
1064 preferences_preview_theme_changed_cb (priv->theme_manager, preferences);
1066 preferences_themes_setup (preferences);
1068 preferences_setup_widgets (preferences, gui);
1070 preferences_languages_setup (preferences);
1071 preferences_languages_add (preferences);
1072 preferences_languages_load (preferences);
1074 preferences_sound_setup (preferences);
1075 preferences_sound_load (preferences);
1077 g_object_unref (gui);
1079 if (empathy_spell_supported ()) {
1080 page = gtk_notebook_get_nth_page (GTK_NOTEBOOK (priv->notebook), EMPATHY_PREFERENCES_TAB_SPELL);
1081 gtk_widget_show (page);
1084 page = gtk_notebook_get_nth_page (GTK_NOTEBOOK (priv->notebook), EMPATHY_PREFERENCES_TAB_LOCATION);
1085 #ifdef HAVE_GEOCLUE
1086 gtk_widget_show (page);
1087 #else
1088 gtk_widget_hide (page);
1089 #endif
1092 static EmpathyPreferencesTab
1093 empathy_preferences_tab_from_string (const gchar *str)
1095 guint i;
1097 for (i = 0; i < G_N_ELEMENTS (empathy_preferences_tabs); i++)
1099 if (!tp_strdiff (str, empathy_preferences_tabs[i]))
1100 return i;
1103 g_warn_if_reached ();
1104 return -1;
1107 const gchar *
1108 empathy_preferences_tab_to_string (EmpathyPreferencesTab tab)
1110 g_return_val_if_fail (tab < G_N_ELEMENTS (empathy_preferences_tabs), NULL);
1112 return empathy_preferences_tabs[tab];
1115 GtkWidget *
1116 empathy_preferences_new (GtkWindow *parent,
1117 gboolean shell_running)
1119 GtkWidget *self;
1120 EmpathyPreferencesPriv *priv;
1121 GtkWidget *notif_page;
1123 g_return_val_if_fail (parent == NULL || GTK_IS_WINDOW (parent), NULL);
1125 self = g_object_new (EMPATHY_TYPE_PREFERENCES, NULL);
1127 if (parent != NULL) {
1128 gtk_window_set_transient_for (GTK_WINDOW (self),
1129 parent);
1132 /* when running in Gnome Shell we must hide these options since they
1133 * are meaningless in that context:
1134 * - General->Behavior label
1135 * - 'Display incoming events in the notification area' (General->Behavior)
1136 * - 'Automatically connect at startup' (General->Behavior)
1137 * - 'Notifications' tab
1139 priv = GET_PRIV (self);
1140 if (shell_running) {
1141 /* Behavior */
1142 gtk_widget_hide (priv->label_general_behavior);
1143 gtk_widget_hide (priv->checkbutton_events_notif_area);
1144 gtk_widget_hide (priv->checkbutton_autoconnect);
1146 /* Notifications tab */
1147 notif_page = gtk_notebook_get_nth_page (GTK_NOTEBOOK (priv->notebook),
1148 EMPATHY_PREFERENCES_TAB_NOTIFICATIONS);
1149 gtk_widget_hide (notif_page);
1152 return self;
1155 void
1156 empathy_preferences_show_tab (EmpathyPreferences *self,
1157 const gchar *page)
1159 EmpathyPreferencesPriv *priv = GET_PRIV (self);
1161 gtk_notebook_set_current_page (GTK_NOTEBOOK (priv->notebook),
1162 empathy_preferences_tab_from_string (page));