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_widget_show (label);
422 content_area = gtk_info_bar_get_content_area (GTK_INFO_BAR (info_bar));
423 gtk_box_pack_start (GTK_BOX (content_area), image, FALSE, FALSE, 0);
424 gtk_box_pack_start (GTK_BOX (content_area), label, FALSE, FALSE, 0);
426 image = gtk_image_new_from_stock (GTK_STOCK_REFRESH, GTK_ICON_SIZE_BUTTON);
427 retry_button = gtk_button_new ();
428 gtk_button_set_image (GTK_BUTTON (retry_button), image);
429 gtk_widget_set_tooltip_text (retry_button, _("Reconnect"));
430 gtk_widget_show (retry_button);
432 image = gtk_image_new_from_stock (GTK_STOCK_EDIT, GTK_ICON_SIZE_BUTTON);
433 edit_button = gtk_button_new ();
434 gtk_button_set_image (GTK_BUTTON (edit_button), image);
435 gtk_widget_set_tooltip_text (edit_button, _("Edit Account"));
436 gtk_widget_show (edit_button);
438 image = gtk_image_new_from_stock (GTK_STOCK_CLOSE, GTK_ICON_SIZE_BUTTON);
439 close_button = gtk_button_new ();
440 gtk_button_set_image (GTK_BUTTON (close_button), image);
441 gtk_widget_set_tooltip_text (close_button, _("Close"));
442 gtk_widget_show (close_button);
444 action_table = gtk_table_new (1, 3, FALSE);
445 gtk_table_set_col_spacings (GTK_TABLE (action_table), 2);
446 gtk_widget_show (action_table);
448 action_area = gtk_info_bar_get_action_area (GTK_INFO_BAR (info_bar));
449 gtk_box_pack_start (GTK_BOX (action_area), action_table, FALSE, FALSE, 0);
451 gtk_table_attach (GTK_TABLE (action_table), retry_button, 0, 1, 0, 1,
452 (GtkAttachOptions) (GTK_SHRINK),
453 (GtkAttachOptions) (GTK_SHRINK), 0, 0);
454 gtk_table_attach (GTK_TABLE (action_table), edit_button, 1, 2, 0, 1,
455 (GtkAttachOptions) (GTK_SHRINK),
456 (GtkAttachOptions) (GTK_SHRINK), 0, 0);
457 gtk_table_attach (GTK_TABLE (action_table), close_button, 2, 3, 0, 1,
458 (GtkAttachOptions) (GTK_SHRINK),
459 (GtkAttachOptions) (GTK_SHRINK), 0, 0);
461 g_object_set_data (G_OBJECT (info_bar), "label", label);
462 g_object_set_data_full (G_OBJECT (info_bar),
463 "account", g_object_ref (account),
465 g_object_set_data_full (G_OBJECT (edit_button),
466 "account", g_object_ref (account),
468 g_object_set_data_full (G_OBJECT (close_button),
469 "account", g_object_ref (account),
471 g_object_set_data_full (G_OBJECT (retry_button),
472 "account", g_object_ref (account),
475 g_signal_connect (edit_button, "clicked",
476 G_CALLBACK (main_window_error_edit_clicked_cb),
478 g_signal_connect (close_button, "clicked",
479 G_CALLBACK (main_window_error_close_clicked_cb),
481 g_signal_connect (retry_button, "clicked",
482 G_CALLBACK (main_window_error_retry_clicked_cb),
485 gtk_widget_show (window->errors_vbox);
487 g_hash_table_insert (window->errors, g_object_ref (account), info_bar);
491 main_window_update_status (EmpathyMainWindow *window)
493 gboolean connected, connecting;
496 connected = empathy_account_manager_get_accounts_connected (&connecting);
498 /* Update the spinner state */
500 ephy_spinner_start (EPHY_SPINNER (window->throbber));
502 ephy_spinner_stop (EPHY_SPINNER (window->throbber));
505 /* Update widgets sensibility */
506 for (l = window->actions_connected; l; l = l->next) {
507 gtk_action_set_sensitive (l->data, connected);
512 main_window_connection_changed_cb (TpAccount *account,
516 gchar *dbus_error_name,
518 EmpathyMainWindow *window)
520 main_window_update_status (window);
522 if (current == TP_CONNECTION_STATUS_DISCONNECTED &&
523 reason != TP_CONNECTION_STATUS_REASON_REQUESTED) {
524 const gchar *message;
526 message = empathy_status_reason_get_default_message (reason);
528 main_window_error_display (window, account, message);
531 if (current == TP_CONNECTION_STATUS_DISCONNECTED) {
532 empathy_sound_play (GTK_WIDGET (window->window),
533 EMPATHY_SOUND_ACCOUNT_DISCONNECTED);
536 if (current == TP_CONNECTION_STATUS_CONNECTED) {
537 empathy_sound_play (GTK_WIDGET (window->window),
538 EMPATHY_SOUND_ACCOUNT_CONNECTED);
540 /* Account connected without error, remove error message if any */
541 main_window_remove_error (window, account);
546 main_window_contact_presence_changed_cb (EmpathyContactMonitor *monitor,
547 EmpathyContact *contact,
548 TpConnectionPresenceType current,
549 TpConnectionPresenceType previous,
550 EmpathyMainWindow *window)
553 gboolean should_play = FALSE;
556 account = empathy_contact_get_account (contact);
557 idle = empathy_idle_dup_singleton ();
559 should_play = !empathy_idle_account_is_just_connected (idle, account);
564 if (tp_connection_presence_type_cmp_availability (previous,
565 TP_CONNECTION_PRESENCE_TYPE_OFFLINE) > 0)
567 /* contact was online */
568 if (tp_connection_presence_type_cmp_availability (current,
569 TP_CONNECTION_PRESENCE_TYPE_OFFLINE) <= 0)
570 /* someone is logging off */
571 empathy_sound_play (GTK_WIDGET (window->window),
572 EMPATHY_SOUND_CONTACT_DISCONNECTED);
576 /* contact was offline */
577 if (tp_connection_presence_type_cmp_availability (current,
578 TP_CONNECTION_PRESENCE_TYPE_OFFLINE) > 0)
579 /* someone is logging in */
580 empathy_sound_play (GTK_WIDGET (window->window),
581 EMPATHY_SOUND_CONTACT_CONNECTED);
585 g_object_unref (idle);
589 main_window_accels_load (void)
593 filename = g_build_filename (g_get_user_config_dir (), PACKAGE_NAME, ACCELS_FILENAME, NULL);
594 if (g_file_test (filename, G_FILE_TEST_EXISTS)) {
595 DEBUG ("Loading from:'%s'", filename);
596 gtk_accel_map_load (filename);
603 main_window_accels_save (void)
606 gchar *file_with_path;
608 dir = g_build_filename (g_get_user_config_dir (), PACKAGE_NAME, NULL);
609 g_mkdir_with_parents (dir, S_IRUSR | S_IWUSR | S_IXUSR);
610 file_with_path = g_build_filename (dir, ACCELS_FILENAME, NULL);
613 DEBUG ("Saving to:'%s'", file_with_path);
614 gtk_accel_map_save (file_with_path);
616 g_free (file_with_path);
620 main_window_destroy_cb (GtkWidget *widget,
621 EmpathyMainWindow *window)
626 /* Save user-defined accelerators. */
627 main_window_accels_save ();
629 g_list_free (window->actions_connected);
631 g_object_unref (window->account_manager);
632 g_object_unref (window->list_store);
633 g_hash_table_destroy (window->errors);
635 /* disconnect all handlers of status-changed signal */
636 g_hash_table_iter_init (&iter, window->status_changed_handlers);
637 while (g_hash_table_iter_next (&iter, &key, &value))
638 g_signal_handler_disconnect (TP_ACCOUNT (key),
639 GPOINTER_TO_UINT (value));
641 g_hash_table_destroy (window->status_changed_handlers);
643 g_signal_handlers_disconnect_by_func (window->event_manager,
644 main_window_event_added_cb,
646 g_signal_handlers_disconnect_by_func (window->event_manager,
647 main_window_event_removed_cb,
649 g_object_unref (window->event_manager);
650 g_object_unref (window->ui_manager);
656 main_window_chat_quit_cb (GtkAction *action,
657 EmpathyMainWindow *window)
663 main_window_view_history_cb (GtkAction *action,
664 EmpathyMainWindow *window)
666 empathy_log_window_show (NULL, NULL, FALSE, GTK_WINDOW (window->window));
670 main_window_chat_new_message_cb (GtkAction *action,
671 EmpathyMainWindow *window)
673 empathy_new_message_dialog_show (GTK_WINDOW (window->window));
677 main_window_chat_new_call_cb (GtkAction *action,
678 EmpathyMainWindow *window)
680 empathy_new_call_dialog_show (GTK_WINDOW (window->window));
684 main_window_chat_add_contact_cb (GtkAction *action,
685 EmpathyMainWindow *window)
687 empathy_new_contact_dialog_show (GTK_WINDOW (window->window));
691 main_window_view_show_ft_manager (GtkAction *action,
692 EmpathyMainWindow *window)
694 empathy_ft_manager_show ();
698 main_window_view_show_offline_cb (GtkToggleAction *action,
699 EmpathyMainWindow *window)
703 current = gtk_toggle_action_get_active (action);
704 empathy_conf_set_bool (empathy_conf_get (),
705 EMPATHY_PREFS_CONTACTS_SHOW_OFFLINE,
708 /* Turn off sound just while we alter the contact list. */
709 // FIXME: empathy_sound_set_enabled (FALSE);
710 empathy_contact_list_store_set_show_offline (window->list_store, current);
711 //empathy_sound_set_enabled (TRUE);
715 main_window_notify_sort_contact_cb (EmpathyConf *conf,
717 EmpathyMainWindow *window)
721 if (empathy_conf_get_string (conf, key, &str) && str) {
723 GEnumClass *enum_class;
724 GEnumValue *enum_value;
726 type = empathy_contact_list_store_sort_get_type ();
727 enum_class = G_ENUM_CLASS (g_type_class_peek (type));
728 enum_value = g_enum_get_value_by_nick (enum_class, str);
730 /* By changing the value of the GtkRadioAction,
731 it emits a signal that calls main_window_view_sort_contacts_cb
732 which updates the contacts list */
733 gtk_radio_action_set_current_value (window->sort_by_name,
736 g_warning ("Wrong value for sort_criterium configuration : %s", str);
743 main_window_view_sort_contacts_cb (GtkRadioAction *action,
744 GtkRadioAction *current,
745 EmpathyMainWindow *window)
747 EmpathyContactListStoreSort value;
750 GEnumClass *enum_class;
751 GEnumValue *enum_value;
753 value = gtk_radio_action_get_current_value (action);
754 group = gtk_radio_action_get_group (action);
756 /* Get string from index */
757 type = empathy_contact_list_store_sort_get_type ();
758 enum_class = G_ENUM_CLASS (g_type_class_peek (type));
759 enum_value = g_enum_get_value (enum_class, g_slist_index (group, current));
762 g_warning ("No GEnumValue for EmpathyContactListSort with GtkRadioAction index:%d",
763 g_slist_index (group, action));
765 empathy_conf_set_string (empathy_conf_get (),
766 EMPATHY_PREFS_CONTACTS_SORT_CRITERIUM,
767 enum_value->value_nick);
769 empathy_contact_list_store_set_sort_criterium (window->list_store, value);
773 main_window_view_show_protocols_cb (GtkToggleAction *action,
774 EmpathyMainWindow *window)
778 value = gtk_toggle_action_get_active (action);
780 empathy_conf_set_bool (empathy_conf_get (),
781 EMPATHY_PREFS_UI_SHOW_PROTOCOLS,
783 empathy_contact_list_store_set_show_protocols (window->list_store,
787 /* Matches GtkRadioAction values set in empathy-main-window.ui */
788 #define CONTACT_LIST_NORMAL_SIZE_WITH_AVATARS 0
789 #define CONTACT_LIST_NORMAL_SIZE 1
790 #define CONTACT_LIST_COMPACT_SIZE 2
793 main_window_view_contacts_list_size_cb (GtkRadioAction *action,
794 GtkRadioAction *current,
795 EmpathyMainWindow *window)
799 value = gtk_radio_action_get_current_value (action);
801 empathy_conf_set_bool (empathy_conf_get (),
802 EMPATHY_PREFS_UI_SHOW_AVATARS,
803 value == CONTACT_LIST_NORMAL_SIZE_WITH_AVATARS);
804 empathy_conf_set_bool (empathy_conf_get (),
805 EMPATHY_PREFS_UI_COMPACT_CONTACT_LIST,
806 value == CONTACT_LIST_COMPACT_SIZE);
808 empathy_contact_list_store_set_show_avatars (window->list_store,
809 value == CONTACT_LIST_NORMAL_SIZE_WITH_AVATARS);
810 empathy_contact_list_store_set_is_compact (window->list_store,
811 value == CONTACT_LIST_COMPACT_SIZE);
813 gtk_action_set_sensitive (GTK_ACTION (window->show_protocols),
814 value != CONTACT_LIST_COMPACT_SIZE );
817 static void main_window_notify_show_protocols_cb (EmpathyConf *conf,
819 EmpathyMainWindow *window)
821 gboolean show_protocols;
823 if (empathy_conf_get_bool (conf,
824 EMPATHY_PREFS_UI_SHOW_PROTOCOLS,
826 gtk_toggle_action_set_active (window->show_protocols,
833 main_window_notify_contact_list_size_cb (EmpathyConf *conf,
835 EmpathyMainWindow *window)
837 gboolean show_avatars;
838 gboolean compact_contact_list;
839 gint value = CONTACT_LIST_NORMAL_SIZE_WITH_AVATARS;
841 if (empathy_conf_get_bool (conf,
842 EMPATHY_PREFS_UI_SHOW_AVATARS,
844 && empathy_conf_get_bool (conf,
845 EMPATHY_PREFS_UI_COMPACT_CONTACT_LIST,
846 &compact_contact_list)) {
847 if (compact_contact_list) {
848 value = CONTACT_LIST_COMPACT_SIZE;
849 } else if (show_avatars) {
850 value = CONTACT_LIST_NORMAL_SIZE_WITH_AVATARS;
852 value = CONTACT_LIST_NORMAL_SIZE;
855 /* By changing the value of the GtkRadioAction,
856 it emits a signal that calls main_window_view_contacts_list_size_cb
857 which updates the contacts list */
858 gtk_radio_action_set_current_value (window->normal_with_avatars, value);
862 main_window_view_show_map_cb (GtkCheckMenuItem *item,
863 EmpathyMainWindow *window)
865 #if HAVE_LIBCHAMPLAIN
866 empathy_map_view_show ();
871 main_window_favorite_chatroom_join (EmpathyChatroom *chatroom)
874 TpConnection *connection;
877 account = empathy_chatroom_get_account (chatroom);
878 connection = tp_account_get_connection (account);
879 room = empathy_chatroom_get_room (chatroom);
881 if (connection != NULL) {
882 DEBUG ("Requesting channel for '%s'", room);
883 empathy_dispatcher_join_muc (connection, room, NULL, NULL);
888 main_window_favorite_chatroom_menu_activate_cb (GtkMenuItem *menu_item,
889 EmpathyChatroom *chatroom)
891 main_window_favorite_chatroom_join (chatroom);
895 main_window_favorite_chatroom_menu_add (EmpathyMainWindow *window,
896 EmpathyChatroom *chatroom)
898 GtkWidget *menu_item;
901 if (g_object_get_data (G_OBJECT (chatroom), "menu_item")) {
905 name = empathy_chatroom_get_name (chatroom);
906 menu_item = gtk_menu_item_new_with_label (name);
908 g_object_set_data (G_OBJECT (chatroom), "menu_item", menu_item);
909 g_signal_connect (menu_item, "activate",
910 G_CALLBACK (main_window_favorite_chatroom_menu_activate_cb),
913 gtk_menu_shell_insert (GTK_MENU_SHELL (window->room_menu),
916 gtk_widget_show (menu_item);
920 main_window_favorite_chatroom_menu_added_cb (EmpathyChatroomManager *manager,
921 EmpathyChatroom *chatroom,
922 EmpathyMainWindow *window)
924 main_window_favorite_chatroom_menu_add (window, chatroom);
925 gtk_widget_show (window->room_separator);
926 gtk_action_set_sensitive (window->room_join_favorites, TRUE);
930 main_window_favorite_chatroom_menu_removed_cb (EmpathyChatroomManager *manager,
931 EmpathyChatroom *chatroom,
932 EmpathyMainWindow *window)
934 GtkWidget *menu_item;
937 menu_item = g_object_get_data (G_OBJECT (chatroom), "menu_item");
938 g_object_set_data (G_OBJECT (chatroom), "menu_item", NULL);
939 gtk_widget_destroy (menu_item);
941 chatrooms = empathy_chatroom_manager_get_chatrooms (window->chatroom_manager, NULL);
943 gtk_widget_show (window->room_separator);
945 gtk_widget_hide (window->room_separator);
948 gtk_action_set_sensitive (window->room_join_favorites, chatrooms != NULL);
949 g_list_free (chatrooms);
953 main_window_favorite_chatroom_menu_setup (EmpathyMainWindow *window)
955 GList *chatrooms, *l;
958 window->chatroom_manager = empathy_chatroom_manager_dup_singleton (NULL);
959 chatrooms = empathy_chatroom_manager_get_chatrooms (window->chatroom_manager, NULL);
960 room = gtk_ui_manager_get_widget (window->ui_manager,
962 window->room_menu = gtk_menu_item_get_submenu (GTK_MENU_ITEM (room));
963 window->room_separator = gtk_ui_manager_get_widget (window->ui_manager,
964 "/menubar/room/room_separator");
966 for (l = chatrooms; l; l = l->next) {
967 main_window_favorite_chatroom_menu_add (window, l->data);
971 gtk_widget_hide (window->room_separator);
974 gtk_action_set_sensitive (window->room_join_favorites, chatrooms != NULL);
976 g_signal_connect (window->chatroom_manager, "chatroom-added",
977 G_CALLBACK (main_window_favorite_chatroom_menu_added_cb),
979 g_signal_connect (window->chatroom_manager, "chatroom-removed",
980 G_CALLBACK (main_window_favorite_chatroom_menu_removed_cb),
983 g_list_free (chatrooms);
987 main_window_room_join_new_cb (GtkAction *action,
988 EmpathyMainWindow *window)
990 empathy_new_chatroom_dialog_show (GTK_WINDOW (window->window));
994 main_window_room_join_favorites_cb (GtkAction *action,
995 EmpathyMainWindow *window)
997 GList *chatrooms, *l;
999 chatrooms = empathy_chatroom_manager_get_chatrooms (window->chatroom_manager, NULL);
1000 for (l = chatrooms; l; l = l->next) {
1001 main_window_favorite_chatroom_join (l->data);
1003 g_list_free (chatrooms);
1007 main_window_room_manage_favorites_cb (GtkAction *action,
1008 EmpathyMainWindow *window)
1010 empathy_chatrooms_window_show (GTK_WINDOW (window->window));
1014 main_window_edit_cb (GtkAction *action,
1015 EmpathyMainWindow *window)
1019 /* FIXME: It should use the UIManager to merge the contact/group submenu */
1020 submenu = empathy_contact_list_view_get_contact_menu (window->list_view);
1025 item = GTK_MENU_ITEM (window->edit_context);
1026 label = gtk_bin_get_child (GTK_BIN (item));
1027 gtk_label_set_text (GTK_LABEL (label), _("Contact"));
1029 gtk_widget_show (window->edit_context);
1030 gtk_widget_show (window->edit_context_separator);
1032 gtk_menu_item_set_submenu (item, submenu);
1037 submenu = empathy_contact_list_view_get_group_menu (window->list_view);
1042 item = GTK_MENU_ITEM (window->edit_context);
1043 label = gtk_bin_get_child (GTK_BIN (item));
1044 gtk_label_set_text (GTK_LABEL (label), _("Group"));
1046 gtk_widget_show (window->edit_context);
1047 gtk_widget_show (window->edit_context_separator);
1049 gtk_menu_item_set_submenu (item, submenu);
1054 gtk_widget_hide (window->edit_context);
1055 gtk_widget_hide (window->edit_context_separator);
1061 main_window_edit_accounts_cb (GtkAction *action,
1062 EmpathyMainWindow *window)
1064 empathy_accounts_dialog_show (GTK_WINDOW (window->window), NULL);
1068 main_window_edit_personal_information_cb (GtkAction *action,
1069 EmpathyMainWindow *window)
1071 empathy_contact_personal_dialog_show (GTK_WINDOW (window->window));
1075 main_window_edit_preferences_cb (GtkAction *action,
1076 EmpathyMainWindow *window)
1078 empathy_preferences_show (GTK_WINDOW (window->window));
1082 main_window_help_about_cb (GtkAction *action,
1083 EmpathyMainWindow *window)
1085 empathy_about_dialog_new (GTK_WINDOW (window->window));
1089 main_window_help_debug_cb (GtkAction *action,
1090 EmpathyMainWindow *window)
1092 empathy_debug_window_new (NULL);
1096 main_window_help_contents_cb (GtkAction *action,
1097 EmpathyMainWindow *window)
1099 empathy_url_show (window->window, "ghelp:empathy");
1103 main_window_throbber_button_press_event_cb (GtkWidget *throbber_ebox,
1104 GdkEventButton *event,
1105 EmpathyMainWindow *window)
1107 if (event->type != GDK_BUTTON_PRESS ||
1108 event->button != 1) {
1112 empathy_accounts_dialog_show (GTK_WINDOW (window->window), NULL);
1118 main_window_account_removed_cb (TpAccountManager *manager,
1120 EmpathyMainWindow *window)
1124 a = tp_account_manager_get_valid_accounts (manager);
1126 gtk_action_set_sensitive (window->view_history,
1127 g_list_length (a) > 0);
1131 /* remove errors if any */
1132 main_window_remove_error (window, account);
1136 main_window_account_validity_changed_cb (TpAccountManager *manager,
1139 EmpathyMainWindow *window)
1143 handler_id = GPOINTER_TO_UINT (g_hash_table_lookup (
1144 window->status_changed_handlers, account));
1146 /* connect signal only if it was not connected yet */
1147 if (handler_id == 0) {
1148 handler_id = g_signal_connect (account,
1150 G_CALLBACK (main_window_connection_changed_cb),
1152 g_hash_table_insert (window->status_changed_handlers,
1153 account, GUINT_TO_POINTER (handler_id));
1157 main_window_account_removed_cb (manager, account, window);
1161 main_window_notify_show_offline_cb (EmpathyConf *conf,
1163 gpointer toggle_action)
1165 gboolean show_offline;
1167 if (empathy_conf_get_bool (conf, key, &show_offline)) {
1168 gtk_toggle_action_set_active (toggle_action, show_offline);
1173 main_window_connection_items_setup (EmpathyMainWindow *window,
1179 const gchar *actions_connected[] = {
1184 "edit_personal_information"
1187 for (i = 0, list = NULL; i < G_N_ELEMENTS (actions_connected); i++) {
1188 action = gtk_builder_get_object (gui, actions_connected[i]);
1189 list = g_list_prepend (list, action);
1192 window->actions_connected = list;
1196 empathy_main_window_get (void)
1198 return main_window != NULL ? main_window->window : NULL;
1202 account_manager_prepared_cb (GObject *source_object,
1203 GAsyncResult *result,
1206 GList *accounts, *j;
1207 TpAccountManager *manager = TP_ACCOUNT_MANAGER (source_object);
1208 EmpathyMainWindow *window = user_data;
1209 GError *error = NULL;
1211 if (!tp_account_manager_prepare_finish (manager, result, &error)) {
1212 DEBUG ("Failed to prepare account manager: %s", error->message);
1213 g_error_free (error);
1217 accounts = tp_account_manager_get_valid_accounts (window->account_manager);
1218 for (j = accounts; j != NULL; j = j->next) {
1219 TpAccount *account = TP_ACCOUNT (j->data);
1222 handler_id = g_signal_connect (account, "status-changed",
1223 G_CALLBACK (main_window_connection_changed_cb),
1225 g_hash_table_insert (window->status_changed_handlers,
1226 account, GUINT_TO_POINTER (handler_id));
1229 g_signal_connect (manager, "account-validity-changed",
1230 G_CALLBACK (main_window_account_validity_changed_cb),
1233 main_window_update_status (window);
1235 /* Disable the "Previous Conversations" menu entry if there is no account */
1236 gtk_action_set_sensitive (window->view_history,
1237 g_list_length (accounts) > 0);
1239 g_list_free (accounts);
1243 empathy_main_window_show (void)
1245 EmpathyMainWindow *window;
1246 EmpathyContactList *list_iface;
1247 EmpathyContactMonitor *monitor;
1251 GtkToggleAction *show_offline_widget;
1253 GtkAction *show_map_widget;
1255 gboolean show_offline;
1260 empathy_window_present (GTK_WINDOW (main_window->window), TRUE);
1261 return main_window->window;
1264 main_window = g_new0 (EmpathyMainWindow, 1);
1265 window = main_window;
1267 /* Set up interface */
1268 filename = empathy_file_lookup ("empathy-main-window.ui", "src");
1269 gui = empathy_builder_get_file (filename,
1270 "main_window", &window->window,
1271 "main_vbox", &window->main_vbox,
1272 "errors_vbox", &window->errors_vbox,
1273 "ui_manager", &window->ui_manager,
1274 "view_show_offline", &show_offline_widget,
1275 "view_show_protocols", &window->show_protocols,
1276 "view_sort_by_name", &window->sort_by_name,
1277 "view_sort_by_status", &window->sort_by_status,
1278 "view_normal_size_with_avatars", &window->normal_with_avatars,
1279 "view_normal_size", &window->normal_size,
1280 "view_compact_size", &window->compact_size,
1281 "view_history", &window->view_history,
1282 "view_show_map", &show_map_widget,
1283 "room_join_favorites", &window->room_join_favorites,
1284 "presence_toolbar", &window->presence_toolbar,
1285 "roster_scrolledwindow", &sw,
1289 empathy_builder_connect (gui, window,
1290 "main_window", "destroy", main_window_destroy_cb,
1291 "chat_quit", "activate", main_window_chat_quit_cb,
1292 "chat_new_message", "activate", main_window_chat_new_message_cb,
1293 "chat_new_call", "activate", main_window_chat_new_call_cb,
1294 "view_history", "activate", main_window_view_history_cb,
1295 "room_join_new", "activate", main_window_room_join_new_cb,
1296 "room_join_favorites", "activate", main_window_room_join_favorites_cb,
1297 "room_manage_favorites", "activate", main_window_room_manage_favorites_cb,
1298 "chat_add_contact", "activate", main_window_chat_add_contact_cb,
1299 "view_show_ft_manager", "activate", main_window_view_show_ft_manager,
1300 "view_show_offline", "toggled", main_window_view_show_offline_cb,
1301 "view_show_protocols", "toggled", main_window_view_show_protocols_cb,
1302 "view_sort_by_name", "changed", main_window_view_sort_contacts_cb,
1303 "view_normal_size_with_avatars", "changed", main_window_view_contacts_list_size_cb,
1304 "view_show_map", "activate", main_window_view_show_map_cb,
1305 "edit", "activate", main_window_edit_cb,
1306 "edit_accounts", "activate", main_window_edit_accounts_cb,
1307 "edit_personal_information", "activate", main_window_edit_personal_information_cb,
1308 "edit_preferences", "activate", main_window_edit_preferences_cb,
1309 "help_about", "activate", main_window_help_about_cb,
1310 "help_debug", "activate", main_window_help_debug_cb,
1311 "help_contents", "activate", main_window_help_contents_cb,
1314 /* Set up connection related widgets. */
1315 main_window_connection_items_setup (window, gui);
1317 g_object_ref (window->ui_manager);
1318 g_object_unref (gui);
1320 #if !HAVE_LIBCHAMPLAIN
1321 gtk_action_set_visible (show_map_widget, FALSE);
1324 window->account_manager = tp_account_manager_dup ();
1326 tp_account_manager_prepare_async (window->account_manager, NULL,
1327 account_manager_prepared_cb, window);
1329 window->errors = g_hash_table_new_full (g_direct_hash,
1334 window->status_changed_handlers = g_hash_table_new_full (g_direct_hash,
1340 main_window_favorite_chatroom_menu_setup (window);
1342 window->edit_context = gtk_ui_manager_get_widget (window->ui_manager,
1343 "/menubar/edit/edit_context");
1344 window->edit_context_separator = gtk_ui_manager_get_widget (window->ui_manager,
1345 "/menubar/edit/edit_context_separator");
1346 gtk_widget_hide (window->edit_context);
1347 gtk_widget_hide (window->edit_context_separator);
1349 /* Set up contact list. */
1350 empathy_status_presets_get_all ();
1352 /* Set up presence chooser */
1353 window->presence_chooser = empathy_presence_chooser_new ();
1354 gtk_widget_show (window->presence_chooser);
1355 item = gtk_tool_item_new ();
1356 gtk_widget_show (GTK_WIDGET (item));
1357 gtk_container_add (GTK_CONTAINER (item), window->presence_chooser);
1358 gtk_tool_item_set_is_important (item, TRUE);
1359 gtk_tool_item_set_expand (item, TRUE);
1360 gtk_toolbar_insert (GTK_TOOLBAR (window->presence_toolbar), item, -1);
1362 /* Set up the throbber */
1363 ebox = gtk_event_box_new ();
1364 gtk_event_box_set_visible_window (GTK_EVENT_BOX (ebox), FALSE);
1365 gtk_widget_set_tooltip_text (ebox, _("Show and edit accounts"));
1366 g_signal_connect (ebox,
1367 "button-press-event",
1368 G_CALLBACK (main_window_throbber_button_press_event_cb),
1370 gtk_widget_show (ebox);
1372 window->throbber = ephy_spinner_new ();
1373 ephy_spinner_set_size (EPHY_SPINNER (window->throbber), GTK_ICON_SIZE_LARGE_TOOLBAR);
1374 gtk_container_add (GTK_CONTAINER (ebox), window->throbber);
1375 gtk_widget_show (window->throbber);
1377 item = gtk_tool_item_new ();
1378 gtk_container_add (GTK_CONTAINER (item), ebox);
1379 gtk_toolbar_insert (GTK_TOOLBAR (window->presence_toolbar), item, -1);
1380 gtk_widget_show (GTK_WIDGET (item));
1382 list_iface = EMPATHY_CONTACT_LIST (empathy_contact_manager_dup_singleton ());
1383 monitor = empathy_contact_list_get_monitor (list_iface);
1384 window->list_store = empathy_contact_list_store_new (list_iface);
1385 window->list_view = empathy_contact_list_view_new (window->list_store,
1386 EMPATHY_CONTACT_LIST_FEATURE_ALL,
1387 EMPATHY_CONTACT_FEATURE_ALL);
1388 g_signal_connect (monitor, "contact-presence-changed",
1389 G_CALLBACK (main_window_contact_presence_changed_cb), window);
1390 g_object_unref (list_iface);
1392 gtk_widget_show (GTK_WIDGET (window->list_view));
1393 gtk_container_add (GTK_CONTAINER (sw),
1394 GTK_WIDGET (window->list_view));
1395 g_signal_connect (window->list_view, "row-activated",
1396 G_CALLBACK (main_window_row_activated_cb),
1399 /* Load user-defined accelerators. */
1400 main_window_accels_load ();
1402 /* Set window size. */
1403 empathy_geometry_bind (GTK_WINDOW (window->window), GEOMETRY_NAME);
1405 /* Enable event handling */
1406 window->event_manager = empathy_event_manager_dup_singleton ();
1407 g_signal_connect (window->event_manager, "event-added",
1408 G_CALLBACK (main_window_event_added_cb),
1410 g_signal_connect (window->event_manager, "event-removed",
1411 G_CALLBACK (main_window_event_removed_cb),
1414 g_signal_connect (window->account_manager, "account-validity-changed",
1415 G_CALLBACK (main_window_account_validity_changed_cb),
1417 g_signal_connect (window->account_manager, "account-removed",
1418 G_CALLBACK (main_window_account_removed_cb),
1420 g_signal_connect (window->account_manager, "account-disabled",
1421 G_CALLBACK (main_window_account_disabled_cb),
1424 l = empathy_event_manager_get_events (window->event_manager);
1426 main_window_event_added_cb (window->event_manager,
1431 conf = empathy_conf_get ();
1433 /* Show offline ? */
1434 empathy_conf_get_bool (conf,
1435 EMPATHY_PREFS_CONTACTS_SHOW_OFFLINE,
1437 empathy_conf_notify_add (conf,
1438 EMPATHY_PREFS_CONTACTS_SHOW_OFFLINE,
1439 main_window_notify_show_offline_cb,
1440 show_offline_widget);
1442 gtk_toggle_action_set_active (show_offline_widget, show_offline);
1444 /* Show protocol ? */
1445 empathy_conf_notify_add (conf,
1446 EMPATHY_PREFS_UI_SHOW_PROTOCOLS,
1447 (EmpathyConfNotifyFunc) main_window_notify_show_protocols_cb,
1450 main_window_notify_show_protocols_cb (conf,
1451 EMPATHY_PREFS_UI_SHOW_PROTOCOLS,
1454 /* Sort by name / by status ? */
1455 empathy_conf_notify_add (conf,
1456 EMPATHY_PREFS_CONTACTS_SORT_CRITERIUM,
1457 (EmpathyConfNotifyFunc) main_window_notify_sort_contact_cb,
1460 main_window_notify_sort_contact_cb (conf,
1461 EMPATHY_PREFS_CONTACTS_SORT_CRITERIUM,
1464 /* Contacts list size */
1465 empathy_conf_notify_add (conf,
1466 EMPATHY_PREFS_UI_COMPACT_CONTACT_LIST,
1467 (EmpathyConfNotifyFunc) main_window_notify_contact_list_size_cb,
1469 empathy_conf_notify_add (conf,
1470 EMPATHY_PREFS_UI_SHOW_AVATARS,
1471 (EmpathyConfNotifyFunc) main_window_notify_contact_list_size_cb,
1474 main_window_notify_contact_list_size_cb (conf,
1475 EMPATHY_PREFS_UI_SHOW_AVATARS,
1478 return window->window;