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-log-window.h>
50 #include <libempathy-gtk/empathy-presence-chooser.h>
51 #include <libempathy-gtk/empathy-sound.h>
52 #include <libempathy-gtk/empathy-ui-utils.h>
54 #include "empathy-accounts-dialog.h"
55 #include "empathy-main-window.h"
56 #include "ephy-spinner.h"
57 #include "empathy-preferences.h"
58 #include "empathy-about-dialog.h"
59 #include "empathy-debug-window.h"
60 #include "empathy-new-chatroom-dialog.h"
61 #include "empathy-map-view.h"
62 #include "empathy-chatrooms-window.h"
63 #include "empathy-event-manager.h"
64 #include "empathy-ft-manager.h"
66 #define DEBUG_FLAG EMPATHY_DEBUG_OTHER
67 #include <libempathy/empathy-debug.h>
69 /* Flashing delay for icons (milliseconds). */
70 #define FLASH_TIMEOUT 500
72 /* Minimum width of roster window if something goes wrong. */
75 /* Accels (menu shortcuts) can be configured and saved */
76 #define ACCELS_FILENAME "accels.txt"
78 /* Name in the geometry file */
79 #define GEOMETRY_NAME "main-window"
81 /* Response ID for GtkInfoBar-Edit Button */
82 #define INFO_BAR_RESPONSE_EDIT 8
84 /* Response ID for GtkInfoBar-Retry Button */
85 #define INFO_BAR_RESPONSE_RETRY 12
87 /* Response ID for GtkInfoBar-Disable Button */
88 #define INFO_BAR_RESPONSE_DISABLE 14
91 EmpathyContactListView *list_view;
92 EmpathyContactListStore *list_store;
93 TpAccountManager *account_manager;
94 EmpathyChatroomManager *chatroom_manager;
95 EmpathyEventManager *event_manager;
96 guint flash_timeout_id;
100 GtkWidget *main_vbox;
102 GtkWidget *presence_toolbar;
103 GtkWidget *presence_chooser;
104 GtkWidget *errors_vbox;
106 GtkRadioAction *sort_by_name;
107 GtkRadioAction *sort_by_status;
108 GtkRadioAction *normal_with_avatars;
109 GtkRadioAction *normal_size;
110 GtkRadioAction *compact_size;
112 GtkUIManager *ui_manager;
113 GtkAction *view_history;
114 GtkAction *room_join_favorites;
115 GtkWidget *room_menu;
116 GtkWidget *room_separator;
117 GtkWidget *edit_context;
118 GtkWidget *edit_context_separator;
120 guint size_timeout_id;
123 /* Actions that are enabled when there are connected accounts */
124 GList *actions_connected;
127 static EmpathyMainWindow *main_window = NULL;
129 static gboolean main_window_configure_event_timeout_cb (
130 EmpathyMainWindow *window);
133 main_window_flash_stop (EmpathyMainWindow *window)
135 if (window->flash_timeout_id == 0) {
139 DEBUG ("Stop flashing");
140 g_source_remove (window->flash_timeout_id);
141 window->flash_timeout_id = 0;
142 window->flash_on = FALSE;
151 main_window_flash_foreach (GtkTreeModel *model,
156 FlashForeachData *data = (FlashForeachData *) user_data;
157 EmpathyContact *contact;
158 const gchar *icon_name;
159 GtkTreePath *parent_path = NULL;
160 GtkTreeIter parent_iter;
162 /* To be used with gtk_tree_model_foreach, update the status icon
163 * of the contact to show the event icon (on=TRUE) or the presence
165 gtk_tree_model_get (model, iter,
166 EMPATHY_CONTACT_LIST_STORE_COL_CONTACT, &contact,
169 if (contact != data->event->contact) {
171 g_object_unref (contact);
177 icon_name = data->event->icon_name;
179 icon_name = empathy_icon_name_for_contact (contact);
182 gtk_tree_store_set (GTK_TREE_STORE (model), iter,
183 EMPATHY_CONTACT_LIST_STORE_COL_ICON_STATUS, icon_name,
186 /* To make sure the parent is shown correctly, we emit
187 * the row-changed signal on the parent so it prompts
188 * it to be refreshed by the filter func.
190 if (gtk_tree_model_iter_parent (model, &parent_iter, iter)) {
191 parent_path = gtk_tree_model_get_path (model, &parent_iter);
194 gtk_tree_model_row_changed (model, parent_path, &parent_iter);
195 gtk_tree_path_free (parent_path);
198 g_object_unref (contact);
204 main_window_flash_cb (EmpathyMainWindow *window)
208 gboolean found_event = FALSE;
209 FlashForeachData data;
211 window->flash_on = !window->flash_on;
212 data.on = window->flash_on;
213 model = GTK_TREE_MODEL (window->list_store);
215 events = empathy_event_manager_get_events (window->event_manager);
216 for (l = events; l; l = l->next) {
217 data.event = l->data;
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 gtk_tree_model_foreach (GTK_TREE_MODEL (window->list_store),
273 main_window_flash_foreach,
278 main_window_row_activated_cb (EmpathyContactListView *view,
280 GtkTreeViewColumn *col,
281 EmpathyMainWindow *window)
283 EmpathyContact *contact;
288 model = GTK_TREE_MODEL (window->list_store);
289 gtk_tree_model_get_iter (model, &iter, path);
290 gtk_tree_model_get (model, &iter,
291 EMPATHY_CONTACT_LIST_STORE_COL_CONTACT, &contact,
298 /* If the contact has an event activate it, otherwise the
299 * default handler of row-activated will be called. */
300 events = empathy_event_manager_get_events (window->event_manager);
301 for (l = events; l; l = l->next) {
302 EmpathyEvent *event = l->data;
304 if (event->contact == contact) {
305 DEBUG ("Activate event");
306 empathy_event_activate (event);
308 /* We don't want the default handler of this signal
309 * (e.g. open a chat) */
310 g_signal_stop_emission_by_name (view, "row-activated");
315 g_object_unref (contact);
319 main_window_error_infobar_button_clicked_cb (GtkInfoBar *info_bar,
321 EmpathyMainWindow *window)
324 GtkWidget *error_widget;
326 account = g_object_get_data (G_OBJECT (info_bar), "account");
327 switch (response_id) {
328 case INFO_BAR_RESPONSE_EDIT:
329 empathy_accounts_dialog_show (GTK_WINDOW (window->window), account);
331 case INFO_BAR_RESPONSE_DISABLE:
332 empathy_account_set_enabled_async (account, FALSE, NULL, NULL);
334 case INFO_BAR_RESPONSE_RETRY:
335 empathy_account_reconnect_async (account, NULL, NULL);
341 error_widget = g_hash_table_lookup (window->errors, account);
342 gtk_widget_destroy (error_widget);
343 g_hash_table_remove (window->errors, account);
347 main_window_error_display (EmpathyMainWindow *window,
349 const gchar *message)
352 GtkWidget *content_area;
356 const gchar *icon_name;
358 info_bar = g_hash_table_lookup (window->errors, account);
360 label = g_object_get_data (G_OBJECT (info_bar), "label");
362 /* Just set the latest error and return */
363 str = g_markup_printf_escaped ("<b>%s</b> - %s",
364 tp_account_get_display_name (account),
366 gtk_label_set_markup (GTK_LABEL (label), str);
372 info_bar = gtk_info_bar_new_with_buttons (GTK_STOCK_EDIT, INFO_BAR_RESPONSE_EDIT,
373 _("_Retry"), INFO_BAR_RESPONSE_RETRY,
374 _("_Disable"), INFO_BAR_RESPONSE_DISABLE, NULL);
375 gtk_info_bar_set_message_type (GTK_INFO_BAR (info_bar), GTK_MESSAGE_WARNING);
376 g_signal_connect (info_bar, "response",
377 G_CALLBACK (main_window_error_infobar_button_clicked_cb),
380 gtk_widget_set_no_show_all (info_bar, TRUE);
381 gtk_box_pack_start (GTK_BOX (window->errors_vbox), info_bar, FALSE, TRUE, 0);
382 gtk_container_set_border_width (GTK_CONTAINER (info_bar), 6);
383 gtk_widget_show (info_bar);
385 icon_name = empathy_account_get_icon_name (account);
386 image = gtk_image_new_from_icon_name (icon_name, GTK_ICON_SIZE_DIALOG);
387 gtk_widget_show (image);
389 str = g_markup_printf_escaped ("<b>%s</b> - %s",
390 tp_account_get_display_name (account),
392 label = gtk_label_new (str);
393 gtk_label_set_use_markup (GTK_LABEL (label), TRUE);
394 gtk_widget_show (label);
397 content_area = gtk_info_bar_get_content_area (GTK_INFO_BAR (info_bar));
398 gtk_box_pack_start (GTK_BOX (content_area), image, FALSE, FALSE, 0);
399 gtk_box_pack_start (GTK_BOX (content_area), label, FALSE, FALSE, 0);
401 g_object_set_data (G_OBJECT (info_bar), "label", label);
402 g_object_set_data_full (G_OBJECT (info_bar),
403 "account", g_object_ref (account),
406 gtk_widget_show (window->errors_vbox);
408 g_hash_table_insert (window->errors, g_object_ref (account), info_bar);
412 main_window_update_status (EmpathyMainWindow *window)
414 gboolean connected, connecting;
417 connected = empathy_account_manager_get_accounts_connected (&connecting);
419 /* Update the spinner state */
421 ephy_spinner_start (EPHY_SPINNER (window->throbber));
423 ephy_spinner_stop (EPHY_SPINNER (window->throbber));
426 /* Update widgets sensibility */
427 for (l = window->actions_connected; l; l = l->next) {
428 gtk_action_set_sensitive (l->data, connected);
433 main_window_connection_changed_cb (TpAccount *account,
437 gchar *dbus_error_name,
439 EmpathyMainWindow *window)
441 main_window_update_status (window);
443 if (current == TP_CONNECTION_STATUS_DISCONNECTED &&
444 reason != TP_CONNECTION_STATUS_REASON_REQUESTED) {
445 const gchar *message;
448 case TP_CONNECTION_STATUS_REASON_NONE_SPECIFIED:
449 message = _("No error specified");
451 case TP_CONNECTION_STATUS_REASON_NETWORK_ERROR:
452 message = _("Network error");
454 case TP_CONNECTION_STATUS_REASON_AUTHENTICATION_FAILED:
455 message = _("Authentication failed");
457 case TP_CONNECTION_STATUS_REASON_ENCRYPTION_ERROR:
458 message = _("Encryption error");
460 case TP_CONNECTION_STATUS_REASON_NAME_IN_USE:
461 message = _("Name in use");
463 case TP_CONNECTION_STATUS_REASON_CERT_NOT_PROVIDED:
464 message = _("Certificate not provided");
466 case TP_CONNECTION_STATUS_REASON_CERT_UNTRUSTED:
467 message = _("Certificate untrusted");
469 case TP_CONNECTION_STATUS_REASON_CERT_EXPIRED:
470 message = _("Certificate expired");
472 case TP_CONNECTION_STATUS_REASON_CERT_NOT_ACTIVATED:
473 message = _("Certificate not activated");
475 case TP_CONNECTION_STATUS_REASON_CERT_HOSTNAME_MISMATCH:
476 message = _("Certificate hostname mismatch");
478 case TP_CONNECTION_STATUS_REASON_CERT_FINGERPRINT_MISMATCH:
479 message = _("Certificate fingerprint mismatch");
481 case TP_CONNECTION_STATUS_REASON_CERT_SELF_SIGNED:
482 message = _("Certificate self-signed");
484 case TP_CONNECTION_STATUS_REASON_CERT_OTHER_ERROR:
485 message = _("Certificate error");
488 message = _("Unknown error");
492 main_window_error_display (window, account, message);
495 if (current == TP_CONNECTION_STATUS_DISCONNECTED) {
496 empathy_sound_play (GTK_WIDGET (window->window),
497 EMPATHY_SOUND_ACCOUNT_DISCONNECTED);
500 if (current == TP_CONNECTION_STATUS_CONNECTED) {
501 GtkWidget *error_widget;
503 empathy_sound_play (GTK_WIDGET (window->window),
504 EMPATHY_SOUND_ACCOUNT_CONNECTED);
506 /* Account connected without error, remove error message if any */
507 error_widget = g_hash_table_lookup (window->errors, account);
509 gtk_widget_destroy (error_widget);
510 g_hash_table_remove (window->errors, account);
516 main_window_contact_presence_changed_cb (EmpathyContactMonitor *monitor,
517 EmpathyContact *contact,
518 TpConnectionPresenceType current,
519 TpConnectionPresenceType previous,
520 EmpathyMainWindow *window)
523 gboolean should_play = FALSE;
526 account = empathy_contact_get_account (contact);
527 idle = empathy_idle_dup_singleton ();
529 should_play = !empathy_idle_account_is_just_connected (idle, account);
534 if (tp_connection_presence_type_cmp_availability (previous,
535 TP_CONNECTION_PRESENCE_TYPE_OFFLINE) > 0)
537 /* contact was online */
538 if (tp_connection_presence_type_cmp_availability (current,
539 TP_CONNECTION_PRESENCE_TYPE_OFFLINE) <= 0)
540 /* someone is logging off */
541 empathy_sound_play (GTK_WIDGET (window->window),
542 EMPATHY_SOUND_CONTACT_DISCONNECTED);
546 /* contact was offline */
547 if (tp_connection_presence_type_cmp_availability (current,
548 TP_CONNECTION_PRESENCE_TYPE_OFFLINE) > 0)
549 /* someone is logging in */
550 empathy_sound_play (GTK_WIDGET (window->window),
551 EMPATHY_SOUND_CONTACT_CONNECTED);
555 g_object_unref (idle);
559 main_window_accels_load (void)
563 filename = g_build_filename (g_get_user_config_dir (), PACKAGE_NAME, ACCELS_FILENAME, NULL);
564 if (g_file_test (filename, G_FILE_TEST_EXISTS)) {
565 DEBUG ("Loading from:'%s'", filename);
566 gtk_accel_map_load (filename);
573 main_window_accels_save (void)
576 gchar *file_with_path;
578 dir = g_build_filename (g_get_user_config_dir (), PACKAGE_NAME, NULL);
579 g_mkdir_with_parents (dir, S_IRUSR | S_IWUSR | S_IXUSR);
580 file_with_path = g_build_filename (dir, ACCELS_FILENAME, NULL);
583 DEBUG ("Saving to:'%s'", file_with_path);
584 gtk_accel_map_save (file_with_path);
586 g_free (file_with_path);
590 main_window_destroy_cb (GtkWidget *widget,
591 EmpathyMainWindow *window)
593 /* Save user-defined accelerators. */
594 main_window_accels_save ();
596 if (window->size_timeout_id) {
597 g_source_remove (window->size_timeout_id);
598 main_window_configure_event_timeout_cb (window);
601 g_list_free (window->actions_connected);
603 g_object_unref (window->account_manager);
604 g_object_unref (window->list_store);
605 g_hash_table_destroy (window->errors);
607 g_signal_handlers_disconnect_by_func (window->event_manager,
608 main_window_event_added_cb,
610 g_signal_handlers_disconnect_by_func (window->event_manager,
611 main_window_event_removed_cb,
613 g_object_unref (window->event_manager);
614 g_object_unref (window->ui_manager);
620 main_window_chat_quit_cb (GtkAction *action,
621 EmpathyMainWindow *window)
627 main_window_view_history_cb (GtkAction *action,
628 EmpathyMainWindow *window)
630 empathy_log_window_show (NULL, NULL, FALSE, GTK_WINDOW (window->window));
634 main_window_chat_new_message_cb (GtkAction *action,
635 EmpathyMainWindow *window)
637 empathy_new_message_dialog_show (GTK_WINDOW (window->window));
641 main_window_chat_add_contact_cb (GtkAction *action,
642 EmpathyMainWindow *window)
644 empathy_new_contact_dialog_show (GTK_WINDOW (window->window));
648 main_window_view_show_ft_manager (GtkAction *action,
649 EmpathyMainWindow *window)
651 empathy_ft_manager_show ();
655 main_window_view_show_offline_cb (GtkToggleAction *action,
656 EmpathyMainWindow *window)
660 current = gtk_toggle_action_get_active (action);
661 empathy_conf_set_bool (empathy_conf_get (),
662 EMPATHY_PREFS_CONTACTS_SHOW_OFFLINE,
665 /* Turn off sound just while we alter the contact list. */
666 // FIXME: empathy_sound_set_enabled (FALSE);
667 empathy_contact_list_store_set_show_offline (window->list_store, current);
668 //empathy_sound_set_enabled (TRUE);
672 main_window_notify_sort_contact_cb (EmpathyConf *conf,
674 EmpathyMainWindow *window)
678 if (empathy_conf_get_string (conf, key, &str) && str) {
680 GEnumClass *enum_class;
681 GEnumValue *enum_value;
683 type = empathy_contact_list_store_sort_get_type ();
684 enum_class = G_ENUM_CLASS (g_type_class_peek (type));
685 enum_value = g_enum_get_value_by_nick (enum_class, str);
687 /* By changing the value of the GtkRadioAction,
688 it emits a signal that calls main_window_view_sort_contacts_cb
689 which updates the contacts list */
690 gtk_radio_action_set_current_value (window->sort_by_name,
693 g_warning ("Wrong value for sort_criterium configuration : %s", str);
700 main_window_view_sort_contacts_cb (GtkRadioAction *action,
701 GtkRadioAction *current,
702 EmpathyMainWindow *window)
704 EmpathyContactListStoreSort value;
707 GEnumClass *enum_class;
708 GEnumValue *enum_value;
710 value = gtk_radio_action_get_current_value (action);
711 group = gtk_radio_action_get_group (action);
713 /* Get string from index */
714 type = empathy_contact_list_store_sort_get_type ();
715 enum_class = G_ENUM_CLASS (g_type_class_peek (type));
716 enum_value = g_enum_get_value (enum_class, g_slist_index (group, current));
719 g_warning ("No GEnumValue for EmpathyContactListSort with GtkRadioAction index:%d",
720 g_slist_index (group, action));
722 empathy_conf_set_string (empathy_conf_get (),
723 EMPATHY_PREFS_CONTACTS_SORT_CRITERIUM,
724 enum_value->value_nick);
726 empathy_contact_list_store_set_sort_criterium (window->list_store, value);
729 /* Matches GtkRadioAction values set in empathy-main-window.ui */
730 #define CONTACT_LIST_NORMAL_SIZE_WITH_AVATARS 0
731 #define CONTACT_LIST_NORMAL_SIZE 1
732 #define CONTACT_LIST_COMPACT_SIZE 2
735 main_window_view_contacts_list_size_cb (GtkRadioAction *action,
736 GtkRadioAction *current,
737 EmpathyMainWindow *window)
741 value = gtk_radio_action_get_current_value (action);
743 empathy_conf_set_bool (empathy_conf_get (),
744 EMPATHY_PREFS_UI_SHOW_AVATARS,
745 value == CONTACT_LIST_NORMAL_SIZE_WITH_AVATARS);
746 empathy_conf_set_bool (empathy_conf_get (),
747 EMPATHY_PREFS_UI_COMPACT_CONTACT_LIST,
748 value == CONTACT_LIST_COMPACT_SIZE);
750 empathy_contact_list_store_set_show_avatars (window->list_store,
751 value == CONTACT_LIST_NORMAL_SIZE_WITH_AVATARS);
752 empathy_contact_list_store_set_is_compact (window->list_store,
753 value == CONTACT_LIST_COMPACT_SIZE);
757 main_window_notify_contact_list_size_cb (EmpathyConf *conf,
759 EmpathyMainWindow *window)
761 gboolean show_avatars;
762 gboolean compact_contact_list;
763 gint value = CONTACT_LIST_NORMAL_SIZE_WITH_AVATARS;
765 if (empathy_conf_get_bool (conf,
766 EMPATHY_PREFS_UI_SHOW_AVATARS,
768 && empathy_conf_get_bool (conf,
769 EMPATHY_PREFS_UI_COMPACT_CONTACT_LIST,
770 &compact_contact_list)) {
771 if (compact_contact_list) {
772 value = CONTACT_LIST_COMPACT_SIZE;
773 } else if (show_avatars) {
774 value = CONTACT_LIST_NORMAL_SIZE_WITH_AVATARS;
776 value = CONTACT_LIST_NORMAL_SIZE;
779 /* By changing the value of the GtkRadioAction,
780 it emits a signal that calls main_window_view_contacts_list_size_cb
781 which updates the contacts list */
782 gtk_radio_action_set_current_value (window->normal_with_avatars, value);
786 main_window_view_show_map_cb (GtkCheckMenuItem *item,
787 EmpathyMainWindow *window)
789 #if HAVE_LIBCHAMPLAIN
790 empathy_map_view_show ();
795 main_window_favorite_chatroom_join (EmpathyChatroom *chatroom)
798 TpConnection *connection;
801 account = empathy_chatroom_get_account (chatroom);
802 connection = tp_account_get_connection (account);
803 room = empathy_chatroom_get_room (chatroom);
805 if (connection != NULL) {
806 DEBUG ("Requesting channel for '%s'", room);
807 empathy_dispatcher_join_muc (connection, room, NULL, NULL);
812 main_window_favorite_chatroom_menu_activate_cb (GtkMenuItem *menu_item,
813 EmpathyChatroom *chatroom)
815 main_window_favorite_chatroom_join (chatroom);
819 main_window_favorite_chatroom_menu_add (EmpathyMainWindow *window,
820 EmpathyChatroom *chatroom)
822 GtkWidget *menu_item;
825 if (g_object_get_data (G_OBJECT (chatroom), "menu_item")) {
829 name = empathy_chatroom_get_name (chatroom);
830 menu_item = gtk_menu_item_new_with_label (name);
832 g_object_set_data (G_OBJECT (chatroom), "menu_item", menu_item);
833 g_signal_connect (menu_item, "activate",
834 G_CALLBACK (main_window_favorite_chatroom_menu_activate_cb),
837 gtk_menu_shell_insert (GTK_MENU_SHELL (window->room_menu),
840 gtk_widget_show (menu_item);
844 main_window_favorite_chatroom_menu_added_cb (EmpathyChatroomManager *manager,
845 EmpathyChatroom *chatroom,
846 EmpathyMainWindow *window)
848 main_window_favorite_chatroom_menu_add (window, chatroom);
849 gtk_widget_show (window->room_separator);
850 gtk_action_set_sensitive (window->room_join_favorites, TRUE);
854 main_window_favorite_chatroom_menu_removed_cb (EmpathyChatroomManager *manager,
855 EmpathyChatroom *chatroom,
856 EmpathyMainWindow *window)
858 GtkWidget *menu_item;
861 menu_item = g_object_get_data (G_OBJECT (chatroom), "menu_item");
862 g_object_set_data (G_OBJECT (chatroom), "menu_item", NULL);
863 gtk_widget_destroy (menu_item);
865 chatrooms = empathy_chatroom_manager_get_chatrooms (window->chatroom_manager, NULL);
867 gtk_widget_show (window->room_separator);
869 gtk_widget_hide (window->room_separator);
872 gtk_action_set_sensitive (window->room_join_favorites, chatrooms != NULL);
873 g_list_free (chatrooms);
877 main_window_favorite_chatroom_menu_setup (EmpathyMainWindow *window)
879 GList *chatrooms, *l;
882 window->chatroom_manager = empathy_chatroom_manager_dup_singleton (NULL);
883 chatrooms = empathy_chatroom_manager_get_chatrooms (window->chatroom_manager, NULL);
884 room = gtk_ui_manager_get_widget (window->ui_manager,
886 window->room_menu = gtk_menu_item_get_submenu (GTK_MENU_ITEM (room));
887 window->room_separator = gtk_ui_manager_get_widget (window->ui_manager,
888 "/menubar/room/room_separator");
890 for (l = chatrooms; l; l = l->next) {
891 main_window_favorite_chatroom_menu_add (window, l->data);
895 gtk_widget_hide (window->room_separator);
898 gtk_action_set_sensitive (window->room_join_favorites, chatrooms != NULL);
900 g_signal_connect (window->chatroom_manager, "chatroom-added",
901 G_CALLBACK (main_window_favorite_chatroom_menu_added_cb),
903 g_signal_connect (window->chatroom_manager, "chatroom-removed",
904 G_CALLBACK (main_window_favorite_chatroom_menu_removed_cb),
907 g_list_free (chatrooms);
911 main_window_room_join_new_cb (GtkAction *action,
912 EmpathyMainWindow *window)
914 empathy_new_chatroom_dialog_show (GTK_WINDOW (window->window));
918 main_window_room_join_favorites_cb (GtkAction *action,
919 EmpathyMainWindow *window)
921 GList *chatrooms, *l;
923 chatrooms = empathy_chatroom_manager_get_chatrooms (window->chatroom_manager, NULL);
924 for (l = chatrooms; l; l = l->next) {
925 main_window_favorite_chatroom_join (l->data);
927 g_list_free (chatrooms);
931 main_window_room_manage_favorites_cb (GtkAction *action,
932 EmpathyMainWindow *window)
934 empathy_chatrooms_window_show (GTK_WINDOW (window->window));
938 main_window_edit_cb (GtkAction *action,
939 EmpathyMainWindow *window)
943 /* FIXME: It should use the UIManager to merge the contact/group submenu */
944 submenu = empathy_contact_list_view_get_contact_menu (window->list_view);
949 item = GTK_MENU_ITEM (window->edit_context);
950 label = gtk_bin_get_child (GTK_BIN (item));
951 gtk_label_set_text (GTK_LABEL (label), _("Contact"));
953 gtk_widget_show (window->edit_context);
954 gtk_widget_show (window->edit_context_separator);
956 gtk_menu_item_set_submenu (item, submenu);
961 submenu = empathy_contact_list_view_get_group_menu (window->list_view);
966 item = GTK_MENU_ITEM (window->edit_context);
967 label = gtk_bin_get_child (GTK_BIN (item));
968 gtk_label_set_text (GTK_LABEL (label), _("Group"));
970 gtk_widget_show (window->edit_context);
971 gtk_widget_show (window->edit_context_separator);
973 gtk_menu_item_set_submenu (item, submenu);
978 gtk_widget_hide (window->edit_context);
979 gtk_widget_hide (window->edit_context_separator);
985 main_window_edit_accounts_cb (GtkAction *action,
986 EmpathyMainWindow *window)
988 empathy_accounts_dialog_show (GTK_WINDOW (window->window), NULL);
992 main_window_edit_personal_information_cb (GtkAction *action,
993 EmpathyMainWindow *window)
995 empathy_contact_personal_dialog_show (GTK_WINDOW (window->window));
999 main_window_edit_preferences_cb (GtkAction *action,
1000 EmpathyMainWindow *window)
1002 empathy_preferences_show (GTK_WINDOW (window->window));
1006 main_window_help_about_cb (GtkAction *action,
1007 EmpathyMainWindow *window)
1009 empathy_about_dialog_new (GTK_WINDOW (window->window));
1013 main_window_help_debug_cb (GtkAction *action,
1014 EmpathyMainWindow *window)
1016 empathy_debug_window_new (NULL);
1020 main_window_help_contents_cb (GtkAction *action,
1021 EmpathyMainWindow *window)
1023 empathy_url_show (window->window, "ghelp:empathy");
1027 main_window_throbber_button_press_event_cb (GtkWidget *throbber_ebox,
1028 GdkEventButton *event,
1029 EmpathyMainWindow *window)
1031 if (event->type != GDK_BUTTON_PRESS ||
1032 event->button != 1) {
1036 empathy_accounts_dialog_show (GTK_WINDOW (window->window), NULL);
1042 main_window_configure_event_timeout_cb (EmpathyMainWindow *window)
1046 gtk_window_get_size (GTK_WINDOW (window->window), &w, &h);
1047 gtk_window_get_position (GTK_WINDOW (window->window), &x, &y);
1049 empathy_geometry_save (GEOMETRY_NAME, x, y, w, h);
1051 window->size_timeout_id = 0;
1057 main_window_configure_event_cb (GtkWidget *widget,
1058 GdkEventConfigure *event,
1059 EmpathyMainWindow *window)
1061 if (window->size_timeout_id) {
1062 g_source_remove (window->size_timeout_id);
1065 window->size_timeout_id = g_timeout_add_seconds (1,
1066 (GSourceFunc) main_window_configure_event_timeout_cb,
1073 main_window_account_removed_cb (TpAccountManager *manager,
1075 EmpathyMainWindow *window)
1079 a = tp_account_manager_get_valid_accounts (manager);
1081 gtk_action_set_sensitive (window->view_history,
1082 g_list_length (a) > 0);
1088 main_window_account_validity_changed_cb (TpAccountManager *manager,
1091 EmpathyMainWindow *window)
1093 main_window_account_removed_cb (manager, account, window);
1097 main_window_notify_show_offline_cb (EmpathyConf *conf,
1099 gpointer toggle_action)
1101 gboolean show_offline;
1103 if (empathy_conf_get_bool (conf, key, &show_offline)) {
1104 gtk_toggle_action_set_active (toggle_action, show_offline);
1109 main_window_connection_items_setup (EmpathyMainWindow *window,
1115 const gchar *actions_connected[] = {
1119 "edit_personal_information"
1122 for (i = 0, list = NULL; i < G_N_ELEMENTS (actions_connected); i++) {
1123 action = gtk_builder_get_object (gui, actions_connected[i]);
1124 list = g_list_prepend (list, action);
1127 window->actions_connected = list;
1131 empathy_main_window_get (void)
1133 return main_window != NULL ? main_window->window : NULL;
1137 account_manager_prepared_cb (GObject *source_object,
1138 GAsyncResult *result,
1141 GList *accounts, *j;
1142 TpAccountManager *manager = TP_ACCOUNT_MANAGER (source_object);
1143 EmpathyMainWindow *window = user_data;
1144 GError *error = NULL;
1146 if (!tp_account_manager_prepare_finish (manager, result, &error)) {
1147 DEBUG ("Failed to prepare account manager: %s", error->message);
1148 g_error_free (error);
1152 accounts = tp_account_manager_get_valid_accounts (window->account_manager);
1153 for (j = accounts; j != NULL; j = j->next) {
1154 TpAccount *account = TP_ACCOUNT (j->data);
1156 g_signal_connect (account, "status-changed",
1157 G_CALLBACK (main_window_connection_changed_cb),
1161 main_window_update_status (window);
1163 /* Disable the "Previous Conversations" menu entry if there is no account */
1164 gtk_action_set_sensitive (window->view_history,
1165 g_list_length (accounts) > 0);
1167 g_list_free (accounts);
1171 empathy_main_window_show (void)
1173 EmpathyMainWindow *window;
1174 EmpathyContactList *list_iface;
1175 EmpathyContactMonitor *monitor;
1179 GtkToggleAction *show_offline_widget;
1181 GtkAction *show_map_widget;
1183 gboolean show_offline;
1189 empathy_window_present (GTK_WINDOW (main_window->window), TRUE);
1190 return main_window->window;
1193 main_window = g_new0 (EmpathyMainWindow, 1);
1194 window = main_window;
1196 /* Set up interface */
1197 filename = empathy_file_lookup ("empathy-main-window.ui", "src");
1198 gui = empathy_builder_get_file (filename,
1199 "main_window", &window->window,
1200 "main_vbox", &window->main_vbox,
1201 "errors_vbox", &window->errors_vbox,
1202 "ui_manager", &window->ui_manager,
1203 "view_show_offline", &show_offline_widget,
1204 "view_sort_by_name", &window->sort_by_name,
1205 "view_sort_by_status", &window->sort_by_status,
1206 "view_normal_size_with_avatars", &window->normal_with_avatars,
1207 "view_normal_size", &window->normal_size,
1208 "view_compact_size", &window->compact_size,
1209 "view_history", &window->view_history,
1210 "view_show_map", &show_map_widget,
1211 "room_join_favorites", &window->room_join_favorites,
1212 "presence_toolbar", &window->presence_toolbar,
1213 "roster_scrolledwindow", &sw,
1217 empathy_builder_connect (gui, window,
1218 "main_window", "destroy", main_window_destroy_cb,
1219 "main_window", "configure_event", main_window_configure_event_cb,
1220 "chat_quit", "activate", main_window_chat_quit_cb,
1221 "chat_new_message", "activate", main_window_chat_new_message_cb,
1222 "view_history", "activate", main_window_view_history_cb,
1223 "room_join_new", "activate", main_window_room_join_new_cb,
1224 "room_join_favorites", "activate", main_window_room_join_favorites_cb,
1225 "room_manage_favorites", "activate", main_window_room_manage_favorites_cb,
1226 "chat_add_contact", "activate", main_window_chat_add_contact_cb,
1227 "view_show_ft_manager", "activate", main_window_view_show_ft_manager,
1228 "view_show_offline", "toggled", main_window_view_show_offline_cb,
1229 "view_sort_by_name", "changed", main_window_view_sort_contacts_cb,
1230 "view_normal_size_with_avatars", "changed", main_window_view_contacts_list_size_cb,
1231 "view_show_map", "activate", main_window_view_show_map_cb,
1232 "edit", "activate", main_window_edit_cb,
1233 "edit_accounts", "activate", main_window_edit_accounts_cb,
1234 "edit_personal_information", "activate", main_window_edit_personal_information_cb,
1235 "edit_preferences", "activate", main_window_edit_preferences_cb,
1236 "help_about", "activate", main_window_help_about_cb,
1237 "help_debug", "activate", main_window_help_debug_cb,
1238 "help_contents", "activate", main_window_help_contents_cb,
1241 /* Set up connection related widgets. */
1242 main_window_connection_items_setup (window, gui);
1244 g_object_ref (window->ui_manager);
1245 g_object_unref (gui);
1247 #if !HAVE_LIBCHAMPLAIN
1248 gtk_action_set_visible (show_map_widget, FALSE);
1251 window->account_manager = tp_account_manager_dup ();
1253 tp_account_manager_prepare_async (window->account_manager, NULL,
1254 account_manager_prepared_cb, window);
1256 window->errors = g_hash_table_new_full (g_direct_hash,
1262 main_window_favorite_chatroom_menu_setup (window);
1264 window->edit_context = gtk_ui_manager_get_widget (window->ui_manager,
1265 "/menubar/edit/edit_context");
1266 window->edit_context_separator = gtk_ui_manager_get_widget (window->ui_manager,
1267 "/menubar/edit/edit_context_separator");
1268 gtk_widget_hide (window->edit_context);
1269 gtk_widget_hide (window->edit_context_separator);
1271 /* Set up contact list. */
1272 empathy_status_presets_get_all ();
1274 /* Set up presence chooser */
1275 window->presence_chooser = empathy_presence_chooser_new ();
1276 gtk_widget_show (window->presence_chooser);
1277 item = gtk_tool_item_new ();
1278 gtk_widget_show (GTK_WIDGET (item));
1279 gtk_container_add (GTK_CONTAINER (item), window->presence_chooser);
1280 gtk_tool_item_set_is_important (item, TRUE);
1281 gtk_tool_item_set_expand (item, TRUE);
1282 gtk_toolbar_insert (GTK_TOOLBAR (window->presence_toolbar), item, -1);
1284 /* Set up the throbber */
1285 ebox = gtk_event_box_new ();
1286 gtk_event_box_set_visible_window (GTK_EVENT_BOX (ebox), FALSE);
1287 gtk_widget_set_tooltip_text (ebox, _("Show and edit accounts"));
1288 g_signal_connect (ebox,
1289 "button-press-event",
1290 G_CALLBACK (main_window_throbber_button_press_event_cb),
1292 gtk_widget_show (ebox);
1294 window->throbber = ephy_spinner_new ();
1295 ephy_spinner_set_size (EPHY_SPINNER (window->throbber), GTK_ICON_SIZE_LARGE_TOOLBAR);
1296 gtk_container_add (GTK_CONTAINER (ebox), window->throbber);
1297 gtk_widget_show (window->throbber);
1299 item = gtk_tool_item_new ();
1300 gtk_container_add (GTK_CONTAINER (item), ebox);
1301 gtk_toolbar_insert (GTK_TOOLBAR (window->presence_toolbar), item, -1);
1302 gtk_widget_show (GTK_WIDGET (item));
1304 list_iface = EMPATHY_CONTACT_LIST (empathy_contact_manager_dup_singleton ());
1305 monitor = empathy_contact_list_get_monitor (list_iface);
1306 window->list_store = empathy_contact_list_store_new (list_iface);
1307 window->list_view = empathy_contact_list_view_new (window->list_store,
1308 EMPATHY_CONTACT_LIST_FEATURE_ALL,
1309 EMPATHY_CONTACT_FEATURE_ALL);
1310 g_signal_connect (monitor, "contact-presence-changed",
1311 G_CALLBACK (main_window_contact_presence_changed_cb), window);
1312 g_object_unref (list_iface);
1314 gtk_widget_show (GTK_WIDGET (window->list_view));
1315 gtk_container_add (GTK_CONTAINER (sw),
1316 GTK_WIDGET (window->list_view));
1317 g_signal_connect (window->list_view, "row-activated",
1318 G_CALLBACK (main_window_row_activated_cb),
1321 /* Load user-defined accelerators. */
1322 main_window_accels_load ();
1324 /* Set window size. */
1325 empathy_geometry_load (GEOMETRY_NAME, &x, &y, &w, &h);
1327 if (w >= 1 && h >= 1) {
1328 /* Use the defaults from the ui file if we
1329 * don't have good w, h geometry.
1331 DEBUG ("Configuring window default size w:%d, h:%d", w, h);
1332 gtk_window_set_default_size (GTK_WINDOW (window->window), w, h);
1335 if (x >= 0 && y >= 0) {
1336 /* Let the window manager position it if we
1337 * don't have good x, y coordinates.
1339 DEBUG ("Configuring window default position x:%d, y:%d", x, y);
1340 gtk_window_move (GTK_WINDOW (window->window), x, y);
1343 /* Enable event handling */
1344 window->event_manager = empathy_event_manager_dup_singleton ();
1345 g_signal_connect (window->event_manager, "event-added",
1346 G_CALLBACK (main_window_event_added_cb),
1348 g_signal_connect (window->event_manager, "event-removed",
1349 G_CALLBACK (main_window_event_removed_cb),
1352 g_signal_connect (window->account_manager, "account-validity-changed",
1353 G_CALLBACK (main_window_account_validity_changed_cb),
1355 g_signal_connect (window->account_manager, "account-removed",
1356 G_CALLBACK (main_window_account_removed_cb),
1359 l = empathy_event_manager_get_events (window->event_manager);
1361 main_window_event_added_cb (window->event_manager,
1366 conf = empathy_conf_get ();
1368 /* Show offline ? */
1369 empathy_conf_get_bool (conf,
1370 EMPATHY_PREFS_CONTACTS_SHOW_OFFLINE,
1372 empathy_conf_notify_add (conf,
1373 EMPATHY_PREFS_CONTACTS_SHOW_OFFLINE,
1374 main_window_notify_show_offline_cb,
1375 show_offline_widget);
1377 gtk_toggle_action_set_active (show_offline_widget, show_offline);
1379 /* Sort by name / by status ? */
1380 empathy_conf_notify_add (conf,
1381 EMPATHY_PREFS_CONTACTS_SORT_CRITERIUM,
1382 (EmpathyConfNotifyFunc) main_window_notify_sort_contact_cb,
1385 main_window_notify_sort_contact_cb (conf,
1386 EMPATHY_PREFS_CONTACTS_SORT_CRITERIUM,
1389 /* Contacts list size */
1390 empathy_conf_notify_add (conf,
1391 EMPATHY_PREFS_UI_COMPACT_CONTACT_LIST,
1392 (EmpathyConfNotifyFunc) main_window_notify_contact_list_size_cb,
1394 empathy_conf_notify_add (conf,
1395 EMPATHY_PREFS_UI_SHOW_AVATARS,
1396 (EmpathyConfNotifyFunc) main_window_notify_contact_list_size_cb,
1399 main_window_notify_contact_list_size_cb (conf,
1400 EMPATHY_PREFS_UI_SHOW_AVATARS,
1403 return window->window;