1 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
3 * Copyright (C) 2002-2007 Imendio AB
4 * Copyright (C) 2007-2008 Collabora Ltd.
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License as
8 * published by the Free Software Foundation; either version 2 of the
9 * License, or (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * General Public License for more details.
16 * You should have received a copy of the GNU General Public
17 * License along with this program; if not, write to the
18 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
19 * Boston, MA 02110-1301 USA
21 * Authors: Xavier Claessens <xclaesse@gmail.com>
28 #include <glib/gi18n.h>
30 #include <telepathy-glib/account-manager.h>
32 #include <libempathy/empathy-contact.h>
33 #include <libempathy/empathy-idle.h>
34 #include <libempathy/empathy-utils.h>
35 #include <libempathy/empathy-dispatcher.h>
36 #include <libempathy/empathy-chatroom-manager.h>
37 #include <libempathy/empathy-chatroom.h>
38 #include <libempathy/empathy-contact-list.h>
39 #include <libempathy/empathy-contact-manager.h>
40 #include <libempathy/empathy-status-presets.h>
42 #include <libempathy-gtk/empathy-conf.h>
43 #include <libempathy-gtk/empathy-contact-dialogs.h>
44 #include <libempathy-gtk/empathy-contact-list-store.h>
45 #include <libempathy-gtk/empathy-contact-list-view.h>
46 #include <libempathy-gtk/empathy-geometry.h>
47 #include <libempathy-gtk/empathy-gtk-enum-types.h>
48 #include <libempathy-gtk/empathy-new-message-dialog.h>
49 #include <libempathy-gtk/empathy-new-call-dialog.h>
50 #include <libempathy-gtk/empathy-log-window.h>
51 #include <libempathy-gtk/empathy-presence-chooser.h>
52 #include <libempathy-gtk/empathy-sound.h>
53 #include <libempathy-gtk/empathy-ui-utils.h>
55 #include "empathy-accounts-dialog.h"
56 #include "empathy-main-window.h"
57 #include "ephy-spinner.h"
58 #include "empathy-preferences.h"
59 #include "empathy-about-dialog.h"
60 #include "empathy-debug-window.h"
61 #include "empathy-new-chatroom-dialog.h"
62 #include "empathy-map-view.h"
63 #include "empathy-chatrooms-window.h"
64 #include "empathy-event-manager.h"
65 #include "empathy-ft-manager.h"
67 #define DEBUG_FLAG EMPATHY_DEBUG_OTHER
68 #include <libempathy/empathy-debug.h>
70 /* Flashing delay for icons (milliseconds). */
71 #define FLASH_TIMEOUT 500
73 /* Minimum width of roster window if something goes wrong. */
76 /* Accels (menu shortcuts) can be configured and saved */
77 #define ACCELS_FILENAME "accels.txt"
79 /* Name in the geometry file */
80 #define GEOMETRY_NAME "main-window"
83 EmpathyContactListView *list_view;
84 EmpathyContactListStore *list_store;
85 TpAccountManager *account_manager;
86 EmpathyChatroomManager *chatroom_manager;
87 EmpathyEventManager *event_manager;
88 guint flash_timeout_id;
94 GtkWidget *presence_toolbar;
95 GtkWidget *presence_chooser;
96 GtkWidget *errors_vbox;
98 GtkToggleAction *show_protocols;
99 GtkRadioAction *sort_by_name;
100 GtkRadioAction *sort_by_status;
101 GtkRadioAction *normal_with_avatars;
102 GtkRadioAction *normal_size;
103 GtkRadioAction *compact_size;
105 GtkUIManager *ui_manager;
106 GtkAction *view_history;
107 GtkAction *room_join_favorites;
108 GtkWidget *room_menu;
109 GtkWidget *room_separator;
110 GtkWidget *edit_context;
111 GtkWidget *edit_context_separator;
113 guint size_timeout_id;
116 /* stores a mapping from TpAccount to Handler ID to prevent
117 * to listen more than once to the status-changed signal */
118 GHashTable *status_changed_handlers;
120 /* Actions that are enabled when there are connected accounts */
121 GList *actions_connected;
124 static EmpathyMainWindow *main_window = NULL;
127 main_window_flash_stop (EmpathyMainWindow *window)
129 if (window->flash_timeout_id == 0) {
133 DEBUG ("Stop flashing");
134 g_source_remove (window->flash_timeout_id);
135 window->flash_timeout_id = 0;
136 window->flash_on = FALSE;
142 EmpathyMainWindow *window;
146 main_window_flash_foreach (GtkTreeModel *model,
151 FlashForeachData *data = (FlashForeachData *) user_data;
152 EmpathyContact *contact;
153 const gchar *icon_name;
154 GtkTreePath *parent_path = NULL;
155 GtkTreeIter parent_iter;
156 GdkPixbuf *pixbuf = NULL;
158 /* To be used with gtk_tree_model_foreach, update the status icon
159 * of the contact to show the event icon (on=TRUE) or the presence
161 gtk_tree_model_get (model, iter,
162 EMPATHY_CONTACT_LIST_STORE_COL_CONTACT, &contact,
165 if (contact != data->event->contact) {
167 g_object_unref (contact);
173 icon_name = data->event->icon_name;
174 pixbuf = empathy_pixbuf_from_icon_name (icon_name, GTK_ICON_SIZE_MENU);
176 pixbuf = contact_list_store_get_contact_status_icon (
177 data->window->list_store,
181 gtk_tree_store_set (GTK_TREE_STORE (model), iter,
182 EMPATHY_CONTACT_LIST_STORE_COL_ICON_STATUS, pixbuf,
185 /* To make sure the parent is shown correctly, we emit
186 * the row-changed signal on the parent so it prompts
187 * it to be refreshed by the filter func.
189 if (gtk_tree_model_iter_parent (model, &parent_iter, iter)) {
190 parent_path = gtk_tree_model_get_path (model, &parent_iter);
193 gtk_tree_model_row_changed (model, parent_path, &parent_iter);
194 gtk_tree_path_free (parent_path);
197 g_object_unref (contact);
203 main_window_flash_cb (EmpathyMainWindow *window)
207 gboolean found_event = FALSE;
208 FlashForeachData data;
210 window->flash_on = !window->flash_on;
211 data.on = window->flash_on;
212 model = GTK_TREE_MODEL (window->list_store);
214 events = empathy_event_manager_get_events (window->event_manager);
215 for (l = events; l; l = l->next) {
216 data.event = l->data;
217 data.window = window;
218 if (!data.event->contact || !data.event->must_ack) {
223 gtk_tree_model_foreach (model,
224 main_window_flash_foreach,
229 main_window_flash_stop (window);
236 main_window_flash_start (EmpathyMainWindow *window)
238 if (window->flash_timeout_id != 0) {
242 DEBUG ("Start flashing");
243 window->flash_timeout_id = g_timeout_add (FLASH_TIMEOUT,
244 (GSourceFunc) main_window_flash_cb,
246 main_window_flash_cb (window);
250 main_window_event_added_cb (EmpathyEventManager *manager,
252 EmpathyMainWindow *window)
254 if (event->contact) {
255 main_window_flash_start (window);
260 main_window_event_removed_cb (EmpathyEventManager *manager,
262 EmpathyMainWindow *window)
264 FlashForeachData data;
266 if (!event->contact) {
272 data.window = window;
273 gtk_tree_model_foreach (GTK_TREE_MODEL (window->list_store),
274 main_window_flash_foreach,
279 main_window_row_activated_cb (EmpathyContactListView *view,
281 GtkTreeViewColumn *col,
282 EmpathyMainWindow *window)
284 EmpathyContact *contact;
289 model = GTK_TREE_MODEL (window->list_store);
290 gtk_tree_model_get_iter (model, &iter, path);
291 gtk_tree_model_get (model, &iter,
292 EMPATHY_CONTACT_LIST_STORE_COL_CONTACT, &contact,
299 /* If the contact has an event activate it, otherwise the
300 * default handler of row-activated will be called. */
301 events = empathy_event_manager_get_events (window->event_manager);
302 for (l = events; l; l = l->next) {
303 EmpathyEvent *event = l->data;
305 if (event->contact == contact) {
306 DEBUG ("Activate event");
307 empathy_event_activate (event);
309 /* We don't want the default handler of this signal
310 * (e.g. open a chat) */
311 g_signal_stop_emission_by_name (view, "row-activated");
316 g_object_unref (contact);
320 main_window_remove_error (EmpathyMainWindow *window,
323 GtkWidget *error_widget;
325 error_widget = g_hash_table_lookup (window->errors, account);
326 if (error_widget != NULL) {
327 gtk_widget_destroy (error_widget);
328 g_hash_table_remove (window->errors, account);
333 main_window_account_disabled_cb (TpAccountManager *manager,
335 EmpathyMainWindow *window)
337 main_window_remove_error (window, account);
341 main_window_error_retry_clicked_cb (GtkButton *button,
342 EmpathyMainWindow *window)
346 account = g_object_get_data (G_OBJECT (button), "account");
347 tp_account_reconnect_async (account, NULL, NULL);
349 main_window_remove_error (window, account);
353 main_window_error_edit_clicked_cb (GtkButton *button,
354 EmpathyMainWindow *window)
358 account = g_object_get_data (G_OBJECT (button), "account");
359 empathy_accounts_dialog_show (GTK_WINDOW (window->window), account);
361 main_window_remove_error (window, account);
365 main_window_error_close_clicked_cb (GtkButton *button,
366 EmpathyMainWindow *window)
370 account = g_object_get_data (G_OBJECT (button), "account");
371 main_window_remove_error (window, account);
375 main_window_error_display (EmpathyMainWindow *window,
377 const gchar *message)
380 GtkWidget *content_area;
383 GtkWidget *retry_button;
384 GtkWidget *edit_button;
385 GtkWidget *close_button;
386 GtkWidget *action_area;
387 GtkWidget *action_table;
389 const gchar *icon_name;
391 str = g_markup_printf_escaped ("<b>%s</b>\n%s",
392 tp_account_get_display_name (account),
395 info_bar = g_hash_table_lookup (window->errors, account);
397 label = g_object_get_data (G_OBJECT (info_bar), "label");
399 /* Just set the latest error and return */
400 gtk_label_set_markup (GTK_LABEL (label), str);
406 info_bar = gtk_info_bar_new ();
407 gtk_info_bar_set_message_type (GTK_INFO_BAR (info_bar), GTK_MESSAGE_WARNING);
409 gtk_widget_set_no_show_all (info_bar, TRUE);
410 gtk_box_pack_start (GTK_BOX (window->errors_vbox), info_bar, FALSE, TRUE, 0);
411 gtk_widget_show (info_bar);
413 icon_name = tp_account_get_icon_name (account);
414 image = gtk_image_new_from_icon_name (icon_name, GTK_ICON_SIZE_SMALL_TOOLBAR);
415 gtk_widget_show (image);
417 label = gtk_label_new (str);
418 gtk_label_set_use_markup (GTK_LABEL (label), TRUE);
419 gtk_label_set_ellipsize (GTK_LABEL (label), PANGO_ELLIPSIZE_END);
420 gtk_widget_show (label);
423 content_area = gtk_info_bar_get_content_area (GTK_INFO_BAR (info_bar));
424 gtk_box_pack_start (GTK_BOX (content_area), image, FALSE, FALSE, 0);
425 gtk_box_pack_start (GTK_BOX (content_area), label, TRUE, TRUE, 0);
427 image = gtk_image_new_from_stock (GTK_STOCK_REFRESH, GTK_ICON_SIZE_BUTTON);
428 retry_button = gtk_button_new ();
429 gtk_button_set_image (GTK_BUTTON (retry_button), image);
430 gtk_widget_set_tooltip_text (retry_button, _("Reconnect"));
431 gtk_widget_show (retry_button);
433 image = gtk_image_new_from_stock (GTK_STOCK_EDIT, GTK_ICON_SIZE_BUTTON);
434 edit_button = gtk_button_new ();
435 gtk_button_set_image (GTK_BUTTON (edit_button), image);
436 gtk_widget_set_tooltip_text (edit_button, _("Edit Account"));
437 gtk_widget_show (edit_button);
439 image = gtk_image_new_from_stock (GTK_STOCK_CLOSE, GTK_ICON_SIZE_BUTTON);
440 close_button = gtk_button_new ();
441 gtk_button_set_image (GTK_BUTTON (close_button), image);
442 gtk_widget_set_tooltip_text (close_button, _("Close"));
443 gtk_widget_show (close_button);
445 action_table = gtk_table_new (1, 3, FALSE);
446 gtk_table_set_col_spacings (GTK_TABLE (action_table), 2);
447 gtk_widget_show (action_table);
449 action_area = gtk_info_bar_get_action_area (GTK_INFO_BAR (info_bar));
450 gtk_box_pack_start (GTK_BOX (action_area), action_table, FALSE, FALSE, 0);
452 gtk_table_attach (GTK_TABLE (action_table), retry_button, 0, 1, 0, 1,
453 (GtkAttachOptions) (GTK_SHRINK),
454 (GtkAttachOptions) (GTK_SHRINK), 0, 0);
455 gtk_table_attach (GTK_TABLE (action_table), edit_button, 1, 2, 0, 1,
456 (GtkAttachOptions) (GTK_SHRINK),
457 (GtkAttachOptions) (GTK_SHRINK), 0, 0);
458 gtk_table_attach (GTK_TABLE (action_table), close_button, 2, 3, 0, 1,
459 (GtkAttachOptions) (GTK_SHRINK),
460 (GtkAttachOptions) (GTK_SHRINK), 0, 0);
462 g_object_set_data (G_OBJECT (info_bar), "label", label);
463 g_object_set_data_full (G_OBJECT (info_bar),
464 "account", g_object_ref (account),
466 g_object_set_data_full (G_OBJECT (edit_button),
467 "account", g_object_ref (account),
469 g_object_set_data_full (G_OBJECT (close_button),
470 "account", g_object_ref (account),
472 g_object_set_data_full (G_OBJECT (retry_button),
473 "account", g_object_ref (account),
476 g_signal_connect (edit_button, "clicked",
477 G_CALLBACK (main_window_error_edit_clicked_cb),
479 g_signal_connect (close_button, "clicked",
480 G_CALLBACK (main_window_error_close_clicked_cb),
482 g_signal_connect (retry_button, "clicked",
483 G_CALLBACK (main_window_error_retry_clicked_cb),
486 gtk_widget_show (window->errors_vbox);
488 g_hash_table_insert (window->errors, g_object_ref (account), info_bar);
492 main_window_update_status (EmpathyMainWindow *window)
494 gboolean connected, connecting;
497 connected = empathy_account_manager_get_accounts_connected (&connecting);
499 /* Update the spinner state */
501 ephy_spinner_start (EPHY_SPINNER (window->throbber));
503 ephy_spinner_stop (EPHY_SPINNER (window->throbber));
506 /* Update widgets sensibility */
507 for (l = window->actions_connected; l; l = l->next) {
508 gtk_action_set_sensitive (l->data, connected);
513 main_window_connection_changed_cb (TpAccount *account,
517 gchar *dbus_error_name,
519 EmpathyMainWindow *window)
521 main_window_update_status (window);
523 if (current == TP_CONNECTION_STATUS_DISCONNECTED &&
524 reason != TP_CONNECTION_STATUS_REASON_REQUESTED) {
525 const gchar *message;
527 message = empathy_status_reason_get_default_message (reason);
529 main_window_error_display (window, account, message);
532 if (current == TP_CONNECTION_STATUS_DISCONNECTED) {
533 empathy_sound_play (GTK_WIDGET (window->window),
534 EMPATHY_SOUND_ACCOUNT_DISCONNECTED);
537 if (current == TP_CONNECTION_STATUS_CONNECTED) {
538 empathy_sound_play (GTK_WIDGET (window->window),
539 EMPATHY_SOUND_ACCOUNT_CONNECTED);
541 /* Account connected without error, remove error message if any */
542 main_window_remove_error (window, account);
547 main_window_contact_presence_changed_cb (EmpathyContactMonitor *monitor,
548 EmpathyContact *contact,
549 TpConnectionPresenceType current,
550 TpConnectionPresenceType previous,
551 EmpathyMainWindow *window)
554 gboolean should_play = FALSE;
557 account = empathy_contact_get_account (contact);
558 idle = empathy_idle_dup_singleton ();
560 should_play = !empathy_idle_account_is_just_connected (idle, account);
565 if (tp_connection_presence_type_cmp_availability (previous,
566 TP_CONNECTION_PRESENCE_TYPE_OFFLINE) > 0)
568 /* contact was online */
569 if (tp_connection_presence_type_cmp_availability (current,
570 TP_CONNECTION_PRESENCE_TYPE_OFFLINE) <= 0)
571 /* someone is logging off */
572 empathy_sound_play (GTK_WIDGET (window->window),
573 EMPATHY_SOUND_CONTACT_DISCONNECTED);
577 /* contact was offline */
578 if (tp_connection_presence_type_cmp_availability (current,
579 TP_CONNECTION_PRESENCE_TYPE_OFFLINE) > 0)
580 /* someone is logging in */
581 empathy_sound_play (GTK_WIDGET (window->window),
582 EMPATHY_SOUND_CONTACT_CONNECTED);
586 g_object_unref (idle);
590 main_window_accels_load (void)
594 filename = g_build_filename (g_get_user_config_dir (), PACKAGE_NAME, ACCELS_FILENAME, NULL);
595 if (g_file_test (filename, G_FILE_TEST_EXISTS)) {
596 DEBUG ("Loading from:'%s'", filename);
597 gtk_accel_map_load (filename);
604 main_window_accels_save (void)
607 gchar *file_with_path;
609 dir = g_build_filename (g_get_user_config_dir (), PACKAGE_NAME, NULL);
610 g_mkdir_with_parents (dir, S_IRUSR | S_IWUSR | S_IXUSR);
611 file_with_path = g_build_filename (dir, ACCELS_FILENAME, NULL);
614 DEBUG ("Saving to:'%s'", file_with_path);
615 gtk_accel_map_save (file_with_path);
617 g_free (file_with_path);
621 main_window_destroy_cb (GtkWidget *widget,
622 EmpathyMainWindow *window)
627 /* Save user-defined accelerators. */
628 main_window_accels_save ();
630 g_list_free (window->actions_connected);
632 g_object_unref (window->account_manager);
633 g_object_unref (window->list_store);
634 g_hash_table_destroy (window->errors);
636 /* disconnect all handlers of status-changed signal */
637 g_hash_table_iter_init (&iter, window->status_changed_handlers);
638 while (g_hash_table_iter_next (&iter, &key, &value))
639 g_signal_handler_disconnect (TP_ACCOUNT (key),
640 GPOINTER_TO_UINT (value));
642 g_hash_table_destroy (window->status_changed_handlers);
644 g_signal_handlers_disconnect_by_func (window->event_manager,
645 main_window_event_added_cb,
647 g_signal_handlers_disconnect_by_func (window->event_manager,
648 main_window_event_removed_cb,
650 g_object_unref (window->event_manager);
651 g_object_unref (window->ui_manager);
657 main_window_chat_quit_cb (GtkAction *action,
658 EmpathyMainWindow *window)
664 main_window_view_history_cb (GtkAction *action,
665 EmpathyMainWindow *window)
667 empathy_log_window_show (NULL, NULL, FALSE, GTK_WINDOW (window->window));
671 main_window_chat_new_message_cb (GtkAction *action,
672 EmpathyMainWindow *window)
674 empathy_new_message_dialog_show (GTK_WINDOW (window->window));
678 main_window_chat_new_call_cb (GtkAction *action,
679 EmpathyMainWindow *window)
681 empathy_new_call_dialog_show (GTK_WINDOW (window->window));
685 main_window_chat_add_contact_cb (GtkAction *action,
686 EmpathyMainWindow *window)
688 empathy_new_contact_dialog_show (GTK_WINDOW (window->window));
692 main_window_view_show_ft_manager (GtkAction *action,
693 EmpathyMainWindow *window)
695 empathy_ft_manager_show ();
699 main_window_view_show_offline_cb (GtkToggleAction *action,
700 EmpathyMainWindow *window)
704 current = gtk_toggle_action_get_active (action);
705 empathy_conf_set_bool (empathy_conf_get (),
706 EMPATHY_PREFS_CONTACTS_SHOW_OFFLINE,
709 /* Turn off sound just while we alter the contact list. */
710 // FIXME: empathy_sound_set_enabled (FALSE);
711 empathy_contact_list_store_set_show_offline (window->list_store, current);
712 //empathy_sound_set_enabled (TRUE);
716 main_window_notify_sort_contact_cb (EmpathyConf *conf,
718 EmpathyMainWindow *window)
722 if (empathy_conf_get_string (conf, key, &str) && str) {
724 GEnumClass *enum_class;
725 GEnumValue *enum_value;
727 type = empathy_contact_list_store_sort_get_type ();
728 enum_class = G_ENUM_CLASS (g_type_class_peek (type));
729 enum_value = g_enum_get_value_by_nick (enum_class, str);
731 /* By changing the value of the GtkRadioAction,
732 it emits a signal that calls main_window_view_sort_contacts_cb
733 which updates the contacts list */
734 gtk_radio_action_set_current_value (window->sort_by_name,
737 g_warning ("Wrong value for sort_criterium configuration : %s", str);
744 main_window_view_sort_contacts_cb (GtkRadioAction *action,
745 GtkRadioAction *current,
746 EmpathyMainWindow *window)
748 EmpathyContactListStoreSort value;
751 GEnumClass *enum_class;
752 GEnumValue *enum_value;
754 value = gtk_radio_action_get_current_value (action);
755 group = gtk_radio_action_get_group (action);
757 /* Get string from index */
758 type = empathy_contact_list_store_sort_get_type ();
759 enum_class = G_ENUM_CLASS (g_type_class_peek (type));
760 enum_value = g_enum_get_value (enum_class, g_slist_index (group, current));
763 g_warning ("No GEnumValue for EmpathyContactListSort with GtkRadioAction index:%d",
764 g_slist_index (group, action));
766 empathy_conf_set_string (empathy_conf_get (),
767 EMPATHY_PREFS_CONTACTS_SORT_CRITERIUM,
768 enum_value->value_nick);
770 empathy_contact_list_store_set_sort_criterium (window->list_store, value);
774 main_window_view_show_protocols_cb (GtkToggleAction *action,
775 EmpathyMainWindow *window)
779 value = gtk_toggle_action_get_active (action);
781 empathy_conf_set_bool (empathy_conf_get (),
782 EMPATHY_PREFS_UI_SHOW_PROTOCOLS,
784 empathy_contact_list_store_set_show_protocols (window->list_store,
788 /* Matches GtkRadioAction values set in empathy-main-window.ui */
789 #define CONTACT_LIST_NORMAL_SIZE_WITH_AVATARS 0
790 #define CONTACT_LIST_NORMAL_SIZE 1
791 #define CONTACT_LIST_COMPACT_SIZE 2
794 main_window_view_contacts_list_size_cb (GtkRadioAction *action,
795 GtkRadioAction *current,
796 EmpathyMainWindow *window)
800 value = gtk_radio_action_get_current_value (action);
802 empathy_conf_set_bool (empathy_conf_get (),
803 EMPATHY_PREFS_UI_SHOW_AVATARS,
804 value == CONTACT_LIST_NORMAL_SIZE_WITH_AVATARS);
805 empathy_conf_set_bool (empathy_conf_get (),
806 EMPATHY_PREFS_UI_COMPACT_CONTACT_LIST,
807 value == CONTACT_LIST_COMPACT_SIZE);
809 empathy_contact_list_store_set_show_avatars (window->list_store,
810 value == CONTACT_LIST_NORMAL_SIZE_WITH_AVATARS);
811 empathy_contact_list_store_set_is_compact (window->list_store,
812 value == CONTACT_LIST_COMPACT_SIZE);
814 gtk_action_set_sensitive (GTK_ACTION (window->show_protocols),
815 value != CONTACT_LIST_COMPACT_SIZE );
818 static void main_window_notify_show_protocols_cb (EmpathyConf *conf,
820 EmpathyMainWindow *window)
822 gboolean show_protocols;
824 if (empathy_conf_get_bool (conf,
825 EMPATHY_PREFS_UI_SHOW_PROTOCOLS,
827 gtk_toggle_action_set_active (window->show_protocols,
834 main_window_notify_contact_list_size_cb (EmpathyConf *conf,
836 EmpathyMainWindow *window)
838 gboolean show_avatars;
839 gboolean compact_contact_list;
840 gint value = CONTACT_LIST_NORMAL_SIZE_WITH_AVATARS;
842 if (empathy_conf_get_bool (conf,
843 EMPATHY_PREFS_UI_SHOW_AVATARS,
845 && empathy_conf_get_bool (conf,
846 EMPATHY_PREFS_UI_COMPACT_CONTACT_LIST,
847 &compact_contact_list)) {
848 if (compact_contact_list) {
849 value = CONTACT_LIST_COMPACT_SIZE;
850 } else if (show_avatars) {
851 value = CONTACT_LIST_NORMAL_SIZE_WITH_AVATARS;
853 value = CONTACT_LIST_NORMAL_SIZE;
856 /* By changing the value of the GtkRadioAction,
857 it emits a signal that calls main_window_view_contacts_list_size_cb
858 which updates the contacts list */
859 gtk_radio_action_set_current_value (window->normal_with_avatars, value);
863 main_window_view_show_map_cb (GtkCheckMenuItem *item,
864 EmpathyMainWindow *window)
866 #if HAVE_LIBCHAMPLAIN
867 empathy_map_view_show ();
872 main_window_favorite_chatroom_join (EmpathyChatroom *chatroom)
875 TpConnection *connection;
878 account = empathy_chatroom_get_account (chatroom);
879 connection = tp_account_get_connection (account);
880 room = empathy_chatroom_get_room (chatroom);
882 if (connection != NULL) {
883 DEBUG ("Requesting channel for '%s'", room);
884 empathy_dispatcher_join_muc (connection, room, NULL, NULL);
889 main_window_favorite_chatroom_menu_activate_cb (GtkMenuItem *menu_item,
890 EmpathyChatroom *chatroom)
892 main_window_favorite_chatroom_join (chatroom);
896 main_window_favorite_chatroom_menu_add (EmpathyMainWindow *window,
897 EmpathyChatroom *chatroom)
899 GtkWidget *menu_item;
902 if (g_object_get_data (G_OBJECT (chatroom), "menu_item")) {
906 name = empathy_chatroom_get_name (chatroom);
907 menu_item = gtk_menu_item_new_with_label (name);
909 g_object_set_data (G_OBJECT (chatroom), "menu_item", menu_item);
910 g_signal_connect (menu_item, "activate",
911 G_CALLBACK (main_window_favorite_chatroom_menu_activate_cb),
914 gtk_menu_shell_insert (GTK_MENU_SHELL (window->room_menu),
917 gtk_widget_show (menu_item);
921 main_window_favorite_chatroom_menu_added_cb (EmpathyChatroomManager *manager,
922 EmpathyChatroom *chatroom,
923 EmpathyMainWindow *window)
925 main_window_favorite_chatroom_menu_add (window, chatroom);
926 gtk_widget_show (window->room_separator);
927 gtk_action_set_sensitive (window->room_join_favorites, TRUE);
931 main_window_favorite_chatroom_menu_removed_cb (EmpathyChatroomManager *manager,
932 EmpathyChatroom *chatroom,
933 EmpathyMainWindow *window)
935 GtkWidget *menu_item;
938 menu_item = g_object_get_data (G_OBJECT (chatroom), "menu_item");
939 g_object_set_data (G_OBJECT (chatroom), "menu_item", NULL);
940 gtk_widget_destroy (menu_item);
942 chatrooms = empathy_chatroom_manager_get_chatrooms (window->chatroom_manager, NULL);
944 gtk_widget_show (window->room_separator);
946 gtk_widget_hide (window->room_separator);
949 gtk_action_set_sensitive (window->room_join_favorites, chatrooms != NULL);
950 g_list_free (chatrooms);
954 main_window_favorite_chatroom_menu_setup (EmpathyMainWindow *window)
956 GList *chatrooms, *l;
959 window->chatroom_manager = empathy_chatroom_manager_dup_singleton (NULL);
960 chatrooms = empathy_chatroom_manager_get_chatrooms (window->chatroom_manager, NULL);
961 room = gtk_ui_manager_get_widget (window->ui_manager,
963 window->room_menu = gtk_menu_item_get_submenu (GTK_MENU_ITEM (room));
964 window->room_separator = gtk_ui_manager_get_widget (window->ui_manager,
965 "/menubar/room/room_separator");
967 for (l = chatrooms; l; l = l->next) {
968 main_window_favorite_chatroom_menu_add (window, l->data);
972 gtk_widget_hide (window->room_separator);
975 gtk_action_set_sensitive (window->room_join_favorites, chatrooms != NULL);
977 g_signal_connect (window->chatroom_manager, "chatroom-added",
978 G_CALLBACK (main_window_favorite_chatroom_menu_added_cb),
980 g_signal_connect (window->chatroom_manager, "chatroom-removed",
981 G_CALLBACK (main_window_favorite_chatroom_menu_removed_cb),
984 g_list_free (chatrooms);
988 main_window_room_join_new_cb (GtkAction *action,
989 EmpathyMainWindow *window)
991 empathy_new_chatroom_dialog_show (GTK_WINDOW (window->window));
995 main_window_room_join_favorites_cb (GtkAction *action,
996 EmpathyMainWindow *window)
998 GList *chatrooms, *l;
1000 chatrooms = empathy_chatroom_manager_get_chatrooms (window->chatroom_manager, NULL);
1001 for (l = chatrooms; l; l = l->next) {
1002 main_window_favorite_chatroom_join (l->data);
1004 g_list_free (chatrooms);
1008 main_window_room_manage_favorites_cb (GtkAction *action,
1009 EmpathyMainWindow *window)
1011 empathy_chatrooms_window_show (GTK_WINDOW (window->window));
1015 main_window_edit_cb (GtkAction *action,
1016 EmpathyMainWindow *window)
1020 /* FIXME: It should use the UIManager to merge the contact/group submenu */
1021 submenu = empathy_contact_list_view_get_contact_menu (window->list_view);
1026 item = GTK_MENU_ITEM (window->edit_context);
1027 label = gtk_bin_get_child (GTK_BIN (item));
1028 gtk_label_set_text (GTK_LABEL (label), _("Contact"));
1030 gtk_widget_show (window->edit_context);
1031 gtk_widget_show (window->edit_context_separator);
1033 gtk_menu_item_set_submenu (item, submenu);
1038 submenu = empathy_contact_list_view_get_group_menu (window->list_view);
1043 item = GTK_MENU_ITEM (window->edit_context);
1044 label = gtk_bin_get_child (GTK_BIN (item));
1045 gtk_label_set_text (GTK_LABEL (label), _("Group"));
1047 gtk_widget_show (window->edit_context);
1048 gtk_widget_show (window->edit_context_separator);
1050 gtk_menu_item_set_submenu (item, submenu);
1055 gtk_widget_hide (window->edit_context);
1056 gtk_widget_hide (window->edit_context_separator);
1062 main_window_edit_accounts_cb (GtkAction *action,
1063 EmpathyMainWindow *window)
1065 empathy_accounts_dialog_show (GTK_WINDOW (window->window), NULL);
1069 main_window_edit_personal_information_cb (GtkAction *action,
1070 EmpathyMainWindow *window)
1072 empathy_contact_personal_dialog_show (GTK_WINDOW (window->window));
1076 main_window_edit_preferences_cb (GtkAction *action,
1077 EmpathyMainWindow *window)
1079 empathy_preferences_show (GTK_WINDOW (window->window));
1083 main_window_help_about_cb (GtkAction *action,
1084 EmpathyMainWindow *window)
1086 empathy_about_dialog_new (GTK_WINDOW (window->window));
1090 main_window_help_debug_cb (GtkAction *action,
1091 EmpathyMainWindow *window)
1093 empathy_debug_window_new (NULL);
1097 main_window_help_contents_cb (GtkAction *action,
1098 EmpathyMainWindow *window)
1100 empathy_url_show (window->window, "ghelp:empathy");
1104 main_window_throbber_button_press_event_cb (GtkWidget *throbber_ebox,
1105 GdkEventButton *event,
1106 EmpathyMainWindow *window)
1108 if (event->type != GDK_BUTTON_PRESS ||
1109 event->button != 1) {
1113 empathy_accounts_dialog_show (GTK_WINDOW (window->window), NULL);
1119 main_window_account_removed_cb (TpAccountManager *manager,
1121 EmpathyMainWindow *window)
1125 a = tp_account_manager_get_valid_accounts (manager);
1127 gtk_action_set_sensitive (window->view_history,
1128 g_list_length (a) > 0);
1132 /* remove errors if any */
1133 main_window_remove_error (window, account);
1137 main_window_account_validity_changed_cb (TpAccountManager *manager,
1140 EmpathyMainWindow *window)
1144 handler_id = GPOINTER_TO_UINT (g_hash_table_lookup (
1145 window->status_changed_handlers, account));
1147 /* connect signal only if it was not connected yet */
1148 if (handler_id == 0) {
1149 handler_id = g_signal_connect (account,
1151 G_CALLBACK (main_window_connection_changed_cb),
1153 g_hash_table_insert (window->status_changed_handlers,
1154 account, GUINT_TO_POINTER (handler_id));
1158 main_window_account_removed_cb (manager, account, window);
1162 main_window_notify_show_offline_cb (EmpathyConf *conf,
1164 gpointer toggle_action)
1166 gboolean show_offline;
1168 if (empathy_conf_get_bool (conf, key, &show_offline)) {
1169 gtk_toggle_action_set_active (toggle_action, show_offline);
1174 main_window_connection_items_setup (EmpathyMainWindow *window,
1180 const gchar *actions_connected[] = {
1185 "edit_personal_information"
1188 for (i = 0, list = NULL; i < G_N_ELEMENTS (actions_connected); i++) {
1189 action = gtk_builder_get_object (gui, actions_connected[i]);
1190 list = g_list_prepend (list, action);
1193 window->actions_connected = list;
1197 empathy_main_window_get (void)
1199 return main_window != NULL ? main_window->window : NULL;
1203 account_manager_prepared_cb (GObject *source_object,
1204 GAsyncResult *result,
1207 GList *accounts, *j;
1208 TpAccountManager *manager = TP_ACCOUNT_MANAGER (source_object);
1209 EmpathyMainWindow *window = user_data;
1210 GError *error = NULL;
1212 if (!tp_account_manager_prepare_finish (manager, result, &error)) {
1213 DEBUG ("Failed to prepare account manager: %s", error->message);
1214 g_error_free (error);
1218 accounts = tp_account_manager_get_valid_accounts (window->account_manager);
1219 for (j = accounts; j != NULL; j = j->next) {
1220 TpAccount *account = TP_ACCOUNT (j->data);
1223 handler_id = g_signal_connect (account, "status-changed",
1224 G_CALLBACK (main_window_connection_changed_cb),
1226 g_hash_table_insert (window->status_changed_handlers,
1227 account, GUINT_TO_POINTER (handler_id));
1230 g_signal_connect (manager, "account-validity-changed",
1231 G_CALLBACK (main_window_account_validity_changed_cb),
1234 main_window_update_status (window);
1236 /* Disable the "Previous Conversations" menu entry if there is no account */
1237 gtk_action_set_sensitive (window->view_history,
1238 g_list_length (accounts) > 0);
1240 g_list_free (accounts);
1244 empathy_main_window_show (void)
1246 EmpathyMainWindow *window;
1247 EmpathyContactList *list_iface;
1248 EmpathyContactMonitor *monitor;
1252 GtkToggleAction *show_offline_widget;
1254 GtkAction *show_map_widget;
1256 gboolean show_offline;
1261 empathy_window_present (GTK_WINDOW (main_window->window), TRUE);
1262 return main_window->window;
1265 main_window = g_new0 (EmpathyMainWindow, 1);
1266 window = main_window;
1268 /* Set up interface */
1269 filename = empathy_file_lookup ("empathy-main-window.ui", "src");
1270 gui = empathy_builder_get_file (filename,
1271 "main_window", &window->window,
1272 "main_vbox", &window->main_vbox,
1273 "errors_vbox", &window->errors_vbox,
1274 "ui_manager", &window->ui_manager,
1275 "view_show_offline", &show_offline_widget,
1276 "view_show_protocols", &window->show_protocols,
1277 "view_sort_by_name", &window->sort_by_name,
1278 "view_sort_by_status", &window->sort_by_status,
1279 "view_normal_size_with_avatars", &window->normal_with_avatars,
1280 "view_normal_size", &window->normal_size,
1281 "view_compact_size", &window->compact_size,
1282 "view_history", &window->view_history,
1283 "view_show_map", &show_map_widget,
1284 "room_join_favorites", &window->room_join_favorites,
1285 "presence_toolbar", &window->presence_toolbar,
1286 "roster_scrolledwindow", &sw,
1290 empathy_builder_connect (gui, window,
1291 "main_window", "destroy", main_window_destroy_cb,
1292 "chat_quit", "activate", main_window_chat_quit_cb,
1293 "chat_new_message", "activate", main_window_chat_new_message_cb,
1294 "chat_new_call", "activate", main_window_chat_new_call_cb,
1295 "view_history", "activate", main_window_view_history_cb,
1296 "room_join_new", "activate", main_window_room_join_new_cb,
1297 "room_join_favorites", "activate", main_window_room_join_favorites_cb,
1298 "room_manage_favorites", "activate", main_window_room_manage_favorites_cb,
1299 "chat_add_contact", "activate", main_window_chat_add_contact_cb,
1300 "view_show_ft_manager", "activate", main_window_view_show_ft_manager,
1301 "view_show_offline", "toggled", main_window_view_show_offline_cb,
1302 "view_show_protocols", "toggled", main_window_view_show_protocols_cb,
1303 "view_sort_by_name", "changed", main_window_view_sort_contacts_cb,
1304 "view_normal_size_with_avatars", "changed", main_window_view_contacts_list_size_cb,
1305 "view_show_map", "activate", main_window_view_show_map_cb,
1306 "edit", "activate", main_window_edit_cb,
1307 "edit_accounts", "activate", main_window_edit_accounts_cb,
1308 "edit_personal_information", "activate", main_window_edit_personal_information_cb,
1309 "edit_preferences", "activate", main_window_edit_preferences_cb,
1310 "help_about", "activate", main_window_help_about_cb,
1311 "help_debug", "activate", main_window_help_debug_cb,
1312 "help_contents", "activate", main_window_help_contents_cb,
1315 /* Set up connection related widgets. */
1316 main_window_connection_items_setup (window, gui);
1318 g_object_ref (window->ui_manager);
1319 g_object_unref (gui);
1321 #if !HAVE_LIBCHAMPLAIN
1322 gtk_action_set_visible (show_map_widget, FALSE);
1325 window->account_manager = tp_account_manager_dup ();
1327 tp_account_manager_prepare_async (window->account_manager, NULL,
1328 account_manager_prepared_cb, window);
1330 window->errors = g_hash_table_new_full (g_direct_hash,
1335 window->status_changed_handlers = g_hash_table_new_full (g_direct_hash,
1341 main_window_favorite_chatroom_menu_setup (window);
1343 window->edit_context = gtk_ui_manager_get_widget (window->ui_manager,
1344 "/menubar/edit/edit_context");
1345 window->edit_context_separator = gtk_ui_manager_get_widget (window->ui_manager,
1346 "/menubar/edit/edit_context_separator");
1347 gtk_widget_hide (window->edit_context);
1348 gtk_widget_hide (window->edit_context_separator);
1350 /* Set up contact list. */
1351 empathy_status_presets_get_all ();
1353 /* Set up presence chooser */
1354 window->presence_chooser = empathy_presence_chooser_new ();
1355 gtk_widget_show (window->presence_chooser);
1356 item = gtk_tool_item_new ();
1357 gtk_widget_show (GTK_WIDGET (item));
1358 gtk_container_add (GTK_CONTAINER (item), window->presence_chooser);
1359 gtk_tool_item_set_is_important (item, TRUE);
1360 gtk_tool_item_set_expand (item, TRUE);
1361 gtk_toolbar_insert (GTK_TOOLBAR (window->presence_toolbar), item, -1);
1363 /* Set up the throbber */
1364 ebox = gtk_event_box_new ();
1365 gtk_event_box_set_visible_window (GTK_EVENT_BOX (ebox), FALSE);
1366 gtk_widget_set_tooltip_text (ebox, _("Show and edit accounts"));
1367 g_signal_connect (ebox,
1368 "button-press-event",
1369 G_CALLBACK (main_window_throbber_button_press_event_cb),
1371 gtk_widget_show (ebox);
1373 window->throbber = ephy_spinner_new ();
1374 ephy_spinner_set_size (EPHY_SPINNER (window->throbber), GTK_ICON_SIZE_LARGE_TOOLBAR);
1375 gtk_container_add (GTK_CONTAINER (ebox), window->throbber);
1376 gtk_widget_show (window->throbber);
1378 item = gtk_tool_item_new ();
1379 gtk_container_add (GTK_CONTAINER (item), ebox);
1380 gtk_toolbar_insert (GTK_TOOLBAR (window->presence_toolbar), item, -1);
1381 gtk_widget_show (GTK_WIDGET (item));
1383 list_iface = EMPATHY_CONTACT_LIST (empathy_contact_manager_dup_singleton ());
1384 monitor = empathy_contact_list_get_monitor (list_iface);
1385 window->list_store = empathy_contact_list_store_new (list_iface);
1386 window->list_view = empathy_contact_list_view_new (window->list_store,
1387 EMPATHY_CONTACT_LIST_FEATURE_ALL,
1388 EMPATHY_CONTACT_FEATURE_ALL);
1389 g_signal_connect (monitor, "contact-presence-changed",
1390 G_CALLBACK (main_window_contact_presence_changed_cb), window);
1391 g_object_unref (list_iface);
1393 gtk_widget_show (GTK_WIDGET (window->list_view));
1394 gtk_container_add (GTK_CONTAINER (sw),
1395 GTK_WIDGET (window->list_view));
1396 g_signal_connect (window->list_view, "row-activated",
1397 G_CALLBACK (main_window_row_activated_cb),
1400 /* Load user-defined accelerators. */
1401 main_window_accels_load ();
1403 /* Set window size. */
1404 empathy_geometry_bind (GTK_WINDOW (window->window), GEOMETRY_NAME);
1406 /* Enable event handling */
1407 window->event_manager = empathy_event_manager_dup_singleton ();
1408 g_signal_connect (window->event_manager, "event-added",
1409 G_CALLBACK (main_window_event_added_cb),
1411 g_signal_connect (window->event_manager, "event-removed",
1412 G_CALLBACK (main_window_event_removed_cb),
1415 g_signal_connect (window->account_manager, "account-validity-changed",
1416 G_CALLBACK (main_window_account_validity_changed_cb),
1418 g_signal_connect (window->account_manager, "account-removed",
1419 G_CALLBACK (main_window_account_removed_cb),
1421 g_signal_connect (window->account_manager, "account-disabled",
1422 G_CALLBACK (main_window_account_disabled_cb),
1425 l = empathy_event_manager_get_events (window->event_manager);
1427 main_window_event_added_cb (window->event_manager,
1432 conf = empathy_conf_get ();
1434 /* Show offline ? */
1435 empathy_conf_get_bool (conf,
1436 EMPATHY_PREFS_CONTACTS_SHOW_OFFLINE,
1438 empathy_conf_notify_add (conf,
1439 EMPATHY_PREFS_CONTACTS_SHOW_OFFLINE,
1440 main_window_notify_show_offline_cb,
1441 show_offline_widget);
1443 gtk_toggle_action_set_active (show_offline_widget, show_offline);
1445 /* Show protocol ? */
1446 empathy_conf_notify_add (conf,
1447 EMPATHY_PREFS_UI_SHOW_PROTOCOLS,
1448 (EmpathyConfNotifyFunc) main_window_notify_show_protocols_cb,
1451 main_window_notify_show_protocols_cb (conf,
1452 EMPATHY_PREFS_UI_SHOW_PROTOCOLS,
1455 /* Sort by name / by status ? */
1456 empathy_conf_notify_add (conf,
1457 EMPATHY_PREFS_CONTACTS_SORT_CRITERIUM,
1458 (EmpathyConfNotifyFunc) main_window_notify_sort_contact_cb,
1461 main_window_notify_sort_contact_cb (conf,
1462 EMPATHY_PREFS_CONTACTS_SORT_CRITERIUM,
1465 /* Contacts list size */
1466 empathy_conf_notify_add (conf,
1467 EMPATHY_PREFS_UI_COMPACT_CONTACT_LIST,
1468 (EmpathyConfNotifyFunc) main_window_notify_contact_list_size_cb,
1470 empathy_conf_notify_add (conf,
1471 EMPATHY_PREFS_UI_SHOW_AVATARS,
1472 (EmpathyConfNotifyFunc) main_window_notify_contact_list_size_cb,
1475 main_window_notify_contact_list_size_cb (conf,
1476 EMPATHY_PREFS_UI_SHOW_AVATARS,
1479 return window->window;