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 EMPATHY_RESPONSE_EDIT 8
84 /* Response ID for GtkInfoBar-Retry Button */
85 #define EMPATHY_RESPONSE_RETRY 12
87 /* Response ID for GtkInfoBar-Disable Button */
88 #define EMPATHY_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 EMPATHY_RESPONSE_EDIT:
329 empathy_accounts_dialog_show (GTK_WINDOW (window->window), account);
331 case EMPATHY_RESPONSE_DISABLE:
332 empathy_account_set_enabled_async (account, FALSE, NULL, NULL);
334 case EMPATHY_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;
357 info_bar = g_hash_table_lookup (window->errors, account);
359 label = g_object_get_data (G_OBJECT (info_bar), "label");
361 /* Just set the latest error and return */
362 str = g_markup_printf_escaped ("<b>%s</b>\n%s",
363 tp_account_get_display_name (account),
365 gtk_label_set_markup (GTK_LABEL (label), str);
371 info_bar = gtk_info_bar_new ();
372 gtk_widget_set_no_show_all (info_bar, TRUE);
373 gtk_box_pack_start (GTK_BOX (window->errors_vbox), info_bar, FALSE, TRUE, 0);
374 gtk_container_set_border_width (GTK_CONTAINER (info_bar), 6);
375 gtk_widget_show (info_bar);
377 label = gtk_label_new ("");
378 gtk_widget_show (label);
380 image = gtk_image_new_from_stock (GTK_STOCK_DIALOG_WARNING,
381 GTK_ICON_SIZE_DIALOG);
382 gtk_widget_show (image);
384 content_area = gtk_info_bar_get_content_area (GTK_INFO_BAR (info_bar));
385 gtk_container_add (GTK_CONTAINER (content_area), image);
386 gtk_container_add (GTK_CONTAINER (content_area), label);
388 gtk_info_bar_add_button (GTK_INFO_BAR (info_bar),
389 GTK_STOCK_EDIT, EMPATHY_RESPONSE_EDIT);
390 gtk_info_bar_add_button (GTK_INFO_BAR (info_bar),
391 _("_Retry"), EMPATHY_RESPONSE_RETRY);
392 gtk_info_bar_add_button (GTK_INFO_BAR (info_bar),
393 _("_Disable"), EMPATHY_RESPONSE_DISABLE);
394 g_signal_connect (info_bar, "response",
395 G_CALLBACK (main_window_error_infobar_button_clicked_cb),
398 str = g_markup_printf_escaped ("<b>%s</b>\n\n%s",
399 tp_account_get_display_name (account),
401 gtk_label_set_markup (GTK_LABEL (label), str);
404 g_object_set_data (G_OBJECT (info_bar), "label", label);
405 g_object_set_data_full (G_OBJECT (info_bar),
406 "account", g_object_ref (account),
409 gtk_widget_show (window->errors_vbox);
411 g_hash_table_insert (window->errors, g_object_ref (account), info_bar);
415 main_window_update_status (EmpathyMainWindow *window)
417 gboolean connected, connecting;
420 connected = empathy_account_manager_get_accounts_connected (&connecting);
422 /* Update the spinner state */
424 ephy_spinner_start (EPHY_SPINNER (window->throbber));
426 ephy_spinner_stop (EPHY_SPINNER (window->throbber));
429 /* Update widgets sensibility */
430 for (l = window->actions_connected; l; l = l->next) {
431 gtk_action_set_sensitive (l->data, connected);
436 main_window_connection_changed_cb (TpAccount *account,
440 gchar *dbus_error_name,
442 EmpathyMainWindow *window)
444 main_window_update_status (window);
446 if (current == TP_CONNECTION_STATUS_DISCONNECTED &&
447 reason != TP_CONNECTION_STATUS_REASON_REQUESTED) {
448 const gchar *message;
451 case TP_CONNECTION_STATUS_REASON_NONE_SPECIFIED:
452 message = _("No error specified");
454 case TP_CONNECTION_STATUS_REASON_NETWORK_ERROR:
455 message = _("Network error");
457 case TP_CONNECTION_STATUS_REASON_AUTHENTICATION_FAILED:
458 message = _("Authentication failed");
460 case TP_CONNECTION_STATUS_REASON_ENCRYPTION_ERROR:
461 message = _("Encryption error");
463 case TP_CONNECTION_STATUS_REASON_NAME_IN_USE:
464 message = _("Name in use");
466 case TP_CONNECTION_STATUS_REASON_CERT_NOT_PROVIDED:
467 message = _("Certificate not provided");
469 case TP_CONNECTION_STATUS_REASON_CERT_UNTRUSTED:
470 message = _("Certificate untrusted");
472 case TP_CONNECTION_STATUS_REASON_CERT_EXPIRED:
473 message = _("Certificate expired");
475 case TP_CONNECTION_STATUS_REASON_CERT_NOT_ACTIVATED:
476 message = _("Certificate not activated");
478 case TP_CONNECTION_STATUS_REASON_CERT_HOSTNAME_MISMATCH:
479 message = _("Certificate hostname mismatch");
481 case TP_CONNECTION_STATUS_REASON_CERT_FINGERPRINT_MISMATCH:
482 message = _("Certificate fingerprint mismatch");
484 case TP_CONNECTION_STATUS_REASON_CERT_SELF_SIGNED:
485 message = _("Certificate self-signed");
487 case TP_CONNECTION_STATUS_REASON_CERT_OTHER_ERROR:
488 message = _("Certificate error");
491 message = _("Unknown error");
495 main_window_error_display (window, account, message);
498 if (current == TP_CONNECTION_STATUS_DISCONNECTED) {
499 empathy_sound_play (GTK_WIDGET (window->window),
500 EMPATHY_SOUND_ACCOUNT_DISCONNECTED);
503 if (current == TP_CONNECTION_STATUS_CONNECTED) {
504 GtkWidget *error_widget;
506 empathy_sound_play (GTK_WIDGET (window->window),
507 EMPATHY_SOUND_ACCOUNT_CONNECTED);
509 /* Account connected without error, remove error message if any */
510 error_widget = g_hash_table_lookup (window->errors, account);
512 gtk_widget_destroy (error_widget);
513 g_hash_table_remove (window->errors, account);
519 main_window_contact_presence_changed_cb (EmpathyContactMonitor *monitor,
520 EmpathyContact *contact,
521 TpConnectionPresenceType current,
522 TpConnectionPresenceType previous,
523 EmpathyMainWindow *window)
526 gboolean should_play = FALSE;
529 account = empathy_contact_get_account (contact);
530 idle = empathy_idle_dup_singleton ();
532 should_play = !empathy_idle_account_is_just_connected (idle, account);
537 if (tp_connection_presence_type_cmp_availability (previous,
538 TP_CONNECTION_PRESENCE_TYPE_OFFLINE) > 0)
540 /* contact was online */
541 if (tp_connection_presence_type_cmp_availability (current,
542 TP_CONNECTION_PRESENCE_TYPE_OFFLINE) <= 0)
543 /* someone is logging off */
544 empathy_sound_play (GTK_WIDGET (window->window),
545 EMPATHY_SOUND_CONTACT_DISCONNECTED);
549 /* contact was offline */
550 if (tp_connection_presence_type_cmp_availability (current,
551 TP_CONNECTION_PRESENCE_TYPE_OFFLINE) > 0)
552 /* someone is logging in */
553 empathy_sound_play (GTK_WIDGET (window->window),
554 EMPATHY_SOUND_CONTACT_CONNECTED);
558 g_object_unref (idle);
562 main_window_accels_load (void)
566 filename = g_build_filename (g_get_user_config_dir (), PACKAGE_NAME, ACCELS_FILENAME, NULL);
567 if (g_file_test (filename, G_FILE_TEST_EXISTS)) {
568 DEBUG ("Loading from:'%s'", filename);
569 gtk_accel_map_load (filename);
576 main_window_accels_save (void)
579 gchar *file_with_path;
581 dir = g_build_filename (g_get_user_config_dir (), PACKAGE_NAME, NULL);
582 g_mkdir_with_parents (dir, S_IRUSR | S_IWUSR | S_IXUSR);
583 file_with_path = g_build_filename (dir, ACCELS_FILENAME, NULL);
586 DEBUG ("Saving to:'%s'", file_with_path);
587 gtk_accel_map_save (file_with_path);
589 g_free (file_with_path);
593 main_window_destroy_cb (GtkWidget *widget,
594 EmpathyMainWindow *window)
596 /* Save user-defined accelerators. */
597 main_window_accels_save ();
599 if (window->size_timeout_id) {
600 g_source_remove (window->size_timeout_id);
601 main_window_configure_event_timeout_cb (window);
604 g_list_free (window->actions_connected);
606 g_object_unref (window->account_manager);
607 g_object_unref (window->list_store);
608 g_hash_table_destroy (window->errors);
610 g_signal_handlers_disconnect_by_func (window->event_manager,
611 main_window_event_added_cb,
613 g_signal_handlers_disconnect_by_func (window->event_manager,
614 main_window_event_removed_cb,
616 g_object_unref (window->event_manager);
617 g_object_unref (window->ui_manager);
623 main_window_chat_quit_cb (GtkAction *action,
624 EmpathyMainWindow *window)
630 main_window_view_history_cb (GtkAction *action,
631 EmpathyMainWindow *window)
633 empathy_log_window_show (NULL, NULL, FALSE, GTK_WINDOW (window->window));
637 main_window_chat_new_message_cb (GtkAction *action,
638 EmpathyMainWindow *window)
640 empathy_new_message_dialog_show (GTK_WINDOW (window->window));
644 main_window_chat_add_contact_cb (GtkAction *action,
645 EmpathyMainWindow *window)
647 empathy_new_contact_dialog_show (GTK_WINDOW (window->window));
651 main_window_view_show_ft_manager (GtkAction *action,
652 EmpathyMainWindow *window)
654 empathy_ft_manager_show ();
658 main_window_view_show_offline_cb (GtkToggleAction *action,
659 EmpathyMainWindow *window)
663 current = gtk_toggle_action_get_active (action);
664 empathy_conf_set_bool (empathy_conf_get (),
665 EMPATHY_PREFS_CONTACTS_SHOW_OFFLINE,
668 /* Turn off sound just while we alter the contact list. */
669 // FIXME: empathy_sound_set_enabled (FALSE);
670 empathy_contact_list_store_set_show_offline (window->list_store, current);
671 //empathy_sound_set_enabled (TRUE);
675 main_window_notify_sort_contact_cb (EmpathyConf *conf,
677 EmpathyMainWindow *window)
681 if (empathy_conf_get_string (conf, key, &str) && str) {
683 GEnumClass *enum_class;
684 GEnumValue *enum_value;
686 type = empathy_contact_list_store_sort_get_type ();
687 enum_class = G_ENUM_CLASS (g_type_class_peek (type));
688 enum_value = g_enum_get_value_by_nick (enum_class, str);
690 /* By changing the value of the GtkRadioAction,
691 it emits a signal that calls main_window_view_sort_contacts_cb
692 which updates the contacts list */
693 gtk_radio_action_set_current_value (window->sort_by_name,
696 g_warning ("Wrong value for sort_criterium configuration : %s", str);
703 main_window_view_sort_contacts_cb (GtkRadioAction *action,
704 GtkRadioAction *current,
705 EmpathyMainWindow *window)
707 EmpathyContactListStoreSort value;
710 GEnumClass *enum_class;
711 GEnumValue *enum_value;
713 value = gtk_radio_action_get_current_value (action);
714 group = gtk_radio_action_get_group (action);
716 /* Get string from index */
717 type = empathy_contact_list_store_sort_get_type ();
718 enum_class = G_ENUM_CLASS (g_type_class_peek (type));
719 enum_value = g_enum_get_value (enum_class, g_slist_index (group, current));
722 g_warning ("No GEnumValue for EmpathyContactListSort with GtkRadioAction index:%d",
723 g_slist_index (group, action));
725 empathy_conf_set_string (empathy_conf_get (),
726 EMPATHY_PREFS_CONTACTS_SORT_CRITERIUM,
727 enum_value->value_nick);
729 empathy_contact_list_store_set_sort_criterium (window->list_store, value);
732 /* Matches GtkRadioAction values set in empathy-main-window.ui */
733 #define CONTACT_LIST_NORMAL_SIZE_WITH_AVATARS 0
734 #define CONTACT_LIST_NORMAL_SIZE 1
735 #define CONTACT_LIST_COMPACT_SIZE 2
738 main_window_view_contacts_list_size_cb (GtkRadioAction *action,
739 GtkRadioAction *current,
740 EmpathyMainWindow *window)
744 value = gtk_radio_action_get_current_value (action);
746 empathy_conf_set_bool (empathy_conf_get (),
747 EMPATHY_PREFS_UI_SHOW_AVATARS,
748 value == CONTACT_LIST_NORMAL_SIZE_WITH_AVATARS);
749 empathy_conf_set_bool (empathy_conf_get (),
750 EMPATHY_PREFS_UI_COMPACT_CONTACT_LIST,
751 value == CONTACT_LIST_COMPACT_SIZE);
753 empathy_contact_list_store_set_show_avatars (window->list_store,
754 value == CONTACT_LIST_NORMAL_SIZE_WITH_AVATARS);
755 empathy_contact_list_store_set_is_compact (window->list_store,
756 value == CONTACT_LIST_COMPACT_SIZE);
760 main_window_notify_contact_list_size_cb (EmpathyConf *conf,
762 EmpathyMainWindow *window)
764 gboolean show_avatars;
765 gboolean compact_contact_list;
766 gint value = CONTACT_LIST_NORMAL_SIZE_WITH_AVATARS;
768 if (empathy_conf_get_bool (conf,
769 EMPATHY_PREFS_UI_SHOW_AVATARS,
771 && empathy_conf_get_bool (conf,
772 EMPATHY_PREFS_UI_COMPACT_CONTACT_LIST,
773 &compact_contact_list)) {
774 if (compact_contact_list) {
775 value = CONTACT_LIST_COMPACT_SIZE;
776 } else if (show_avatars) {
777 value = CONTACT_LIST_NORMAL_SIZE_WITH_AVATARS;
779 value = CONTACT_LIST_NORMAL_SIZE;
782 /* By changing the value of the GtkRadioAction,
783 it emits a signal that calls main_window_view_contacts_list_size_cb
784 which updates the contacts list */
785 gtk_radio_action_set_current_value (window->normal_with_avatars, value);
789 main_window_view_show_map_cb (GtkCheckMenuItem *item,
790 EmpathyMainWindow *window)
792 #if HAVE_LIBCHAMPLAIN
793 empathy_map_view_show ();
798 main_window_favorite_chatroom_join (EmpathyChatroom *chatroom)
801 TpConnection *connection;
804 account = empathy_chatroom_get_account (chatroom);
805 connection = tp_account_get_connection (account);
806 room = empathy_chatroom_get_room (chatroom);
808 if (connection != NULL) {
809 DEBUG ("Requesting channel for '%s'", room);
810 empathy_dispatcher_join_muc (connection, room, NULL, NULL);
815 main_window_favorite_chatroom_menu_activate_cb (GtkMenuItem *menu_item,
816 EmpathyChatroom *chatroom)
818 main_window_favorite_chatroom_join (chatroom);
822 main_window_favorite_chatroom_menu_add (EmpathyMainWindow *window,
823 EmpathyChatroom *chatroom)
825 GtkWidget *menu_item;
828 if (g_object_get_data (G_OBJECT (chatroom), "menu_item")) {
832 name = empathy_chatroom_get_name (chatroom);
833 menu_item = gtk_menu_item_new_with_label (name);
835 g_object_set_data (G_OBJECT (chatroom), "menu_item", menu_item);
836 g_signal_connect (menu_item, "activate",
837 G_CALLBACK (main_window_favorite_chatroom_menu_activate_cb),
840 gtk_menu_shell_insert (GTK_MENU_SHELL (window->room_menu),
843 gtk_widget_show (menu_item);
847 main_window_favorite_chatroom_menu_added_cb (EmpathyChatroomManager *manager,
848 EmpathyChatroom *chatroom,
849 EmpathyMainWindow *window)
851 main_window_favorite_chatroom_menu_add (window, chatroom);
852 gtk_widget_show (window->room_separator);
853 gtk_action_set_sensitive (window->room_join_favorites, TRUE);
857 main_window_favorite_chatroom_menu_removed_cb (EmpathyChatroomManager *manager,
858 EmpathyChatroom *chatroom,
859 EmpathyMainWindow *window)
861 GtkWidget *menu_item;
864 menu_item = g_object_get_data (G_OBJECT (chatroom), "menu_item");
865 g_object_set_data (G_OBJECT (chatroom), "menu_item", NULL);
866 gtk_widget_destroy (menu_item);
868 chatrooms = empathy_chatroom_manager_get_chatrooms (window->chatroom_manager, NULL);
870 gtk_widget_show (window->room_separator);
872 gtk_widget_hide (window->room_separator);
875 gtk_action_set_sensitive (window->room_join_favorites, chatrooms != NULL);
876 g_list_free (chatrooms);
880 main_window_favorite_chatroom_menu_setup (EmpathyMainWindow *window)
882 GList *chatrooms, *l;
885 window->chatroom_manager = empathy_chatroom_manager_dup_singleton (NULL);
886 chatrooms = empathy_chatroom_manager_get_chatrooms (window->chatroom_manager, NULL);
887 room = gtk_ui_manager_get_widget (window->ui_manager,
889 window->room_menu = gtk_menu_item_get_submenu (GTK_MENU_ITEM (room));
890 window->room_separator = gtk_ui_manager_get_widget (window->ui_manager,
891 "/menubar/room/room_separator");
893 for (l = chatrooms; l; l = l->next) {
894 main_window_favorite_chatroom_menu_add (window, l->data);
898 gtk_widget_hide (window->room_separator);
901 gtk_action_set_sensitive (window->room_join_favorites, chatrooms != NULL);
903 g_signal_connect (window->chatroom_manager, "chatroom-added",
904 G_CALLBACK (main_window_favorite_chatroom_menu_added_cb),
906 g_signal_connect (window->chatroom_manager, "chatroom-removed",
907 G_CALLBACK (main_window_favorite_chatroom_menu_removed_cb),
910 g_list_free (chatrooms);
914 main_window_room_join_new_cb (GtkAction *action,
915 EmpathyMainWindow *window)
917 empathy_new_chatroom_dialog_show (GTK_WINDOW (window->window));
921 main_window_room_join_favorites_cb (GtkAction *action,
922 EmpathyMainWindow *window)
924 GList *chatrooms, *l;
926 chatrooms = empathy_chatroom_manager_get_chatrooms (window->chatroom_manager, NULL);
927 for (l = chatrooms; l; l = l->next) {
928 main_window_favorite_chatroom_join (l->data);
930 g_list_free (chatrooms);
934 main_window_room_manage_favorites_cb (GtkAction *action,
935 EmpathyMainWindow *window)
937 empathy_chatrooms_window_show (GTK_WINDOW (window->window));
941 main_window_edit_cb (GtkAction *action,
942 EmpathyMainWindow *window)
946 /* FIXME: It should use the UIManager to merge the contact/group submenu */
947 submenu = empathy_contact_list_view_get_contact_menu (window->list_view);
952 item = GTK_MENU_ITEM (window->edit_context);
953 label = gtk_bin_get_child (GTK_BIN (item));
954 gtk_label_set_text (GTK_LABEL (label), _("Contact"));
956 gtk_widget_show (window->edit_context);
957 gtk_widget_show (window->edit_context_separator);
959 gtk_menu_item_set_submenu (item, submenu);
964 submenu = empathy_contact_list_view_get_group_menu (window->list_view);
969 item = GTK_MENU_ITEM (window->edit_context);
970 label = gtk_bin_get_child (GTK_BIN (item));
971 gtk_label_set_text (GTK_LABEL (label), _("Group"));
973 gtk_widget_show (window->edit_context);
974 gtk_widget_show (window->edit_context_separator);
976 gtk_menu_item_set_submenu (item, submenu);
981 gtk_widget_hide (window->edit_context);
982 gtk_widget_hide (window->edit_context_separator);
988 main_window_edit_accounts_cb (GtkAction *action,
989 EmpathyMainWindow *window)
991 empathy_accounts_dialog_show (GTK_WINDOW (window->window), NULL);
995 main_window_edit_personal_information_cb (GtkAction *action,
996 EmpathyMainWindow *window)
998 empathy_contact_personal_dialog_show (GTK_WINDOW (window->window));
1002 main_window_edit_preferences_cb (GtkAction *action,
1003 EmpathyMainWindow *window)
1005 empathy_preferences_show (GTK_WINDOW (window->window));
1009 main_window_help_about_cb (GtkAction *action,
1010 EmpathyMainWindow *window)
1012 empathy_about_dialog_new (GTK_WINDOW (window->window));
1016 main_window_help_debug_cb (GtkAction *action,
1017 EmpathyMainWindow *window)
1019 empathy_debug_window_new (NULL);
1023 main_window_help_contents_cb (GtkAction *action,
1024 EmpathyMainWindow *window)
1026 empathy_url_show (window->window, "ghelp:empathy");
1030 main_window_throbber_button_press_event_cb (GtkWidget *throbber_ebox,
1031 GdkEventButton *event,
1032 EmpathyMainWindow *window)
1034 if (event->type != GDK_BUTTON_PRESS ||
1035 event->button != 1) {
1039 empathy_accounts_dialog_show (GTK_WINDOW (window->window), NULL);
1045 main_window_configure_event_timeout_cb (EmpathyMainWindow *window)
1049 gtk_window_get_size (GTK_WINDOW (window->window), &w, &h);
1050 gtk_window_get_position (GTK_WINDOW (window->window), &x, &y);
1052 empathy_geometry_save (GEOMETRY_NAME, x, y, w, h);
1054 window->size_timeout_id = 0;
1060 main_window_configure_event_cb (GtkWidget *widget,
1061 GdkEventConfigure *event,
1062 EmpathyMainWindow *window)
1064 if (window->size_timeout_id) {
1065 g_source_remove (window->size_timeout_id);
1068 window->size_timeout_id = g_timeout_add_seconds (1,
1069 (GSourceFunc) main_window_configure_event_timeout_cb,
1076 main_window_account_removed_cb (TpAccountManager *manager,
1078 EmpathyMainWindow *window)
1082 a = tp_account_manager_get_valid_accounts (manager);
1084 gtk_action_set_sensitive (window->view_history,
1085 g_list_length (a) > 0);
1091 main_window_account_validity_changed_cb (TpAccountManager *manager,
1094 EmpathyMainWindow *window)
1096 main_window_account_removed_cb (manager, account, window);
1100 main_window_notify_show_offline_cb (EmpathyConf *conf,
1102 gpointer toggle_action)
1104 gboolean show_offline;
1106 if (empathy_conf_get_bool (conf, key, &show_offline)) {
1107 gtk_toggle_action_set_active (toggle_action, show_offline);
1112 main_window_connection_items_setup (EmpathyMainWindow *window,
1118 const gchar *actions_connected[] = {
1122 "edit_personal_information"
1125 for (i = 0, list = NULL; i < G_N_ELEMENTS (actions_connected); i++) {
1126 action = gtk_builder_get_object (gui, actions_connected[i]);
1127 list = g_list_prepend (list, action);
1130 window->actions_connected = list;
1134 empathy_main_window_get (void)
1136 return main_window != NULL ? main_window->window : NULL;
1140 account_manager_prepared_cb (GObject *source_object,
1141 GAsyncResult *result,
1144 GList *accounts, *j;
1145 TpAccountManager *manager = TP_ACCOUNT_MANAGER (source_object);
1146 EmpathyMainWindow *window = user_data;
1147 GError *error = NULL;
1149 if (!tp_account_manager_prepare_finish (manager, result, &error)) {
1150 DEBUG ("Failed to prepare account manager: %s", error->message);
1151 g_error_free (error);
1155 accounts = tp_account_manager_get_valid_accounts (window->account_manager);
1156 for (j = accounts; j != NULL; j = j->next) {
1157 TpAccount *account = TP_ACCOUNT (j->data);
1159 g_signal_connect (account, "status-changed",
1160 G_CALLBACK (main_window_connection_changed_cb),
1164 main_window_update_status (window);
1166 /* Disable the "Previous Conversations" menu entry if there is no account */
1167 gtk_action_set_sensitive (window->view_history,
1168 g_list_length (accounts) > 0);
1170 g_list_free (accounts);
1174 empathy_main_window_show (void)
1176 EmpathyMainWindow *window;
1177 EmpathyContactList *list_iface;
1178 EmpathyContactMonitor *monitor;
1182 GtkToggleAction *show_offline_widget;
1184 GtkAction *show_map_widget;
1186 gboolean show_offline;
1192 empathy_window_present (GTK_WINDOW (main_window->window), TRUE);
1193 return main_window->window;
1196 main_window = g_new0 (EmpathyMainWindow, 1);
1197 window = main_window;
1199 /* Set up interface */
1200 filename = empathy_file_lookup ("empathy-main-window.ui", "src");
1201 gui = empathy_builder_get_file (filename,
1202 "main_window", &window->window,
1203 "main_vbox", &window->main_vbox,
1204 "errors_vbox", &window->errors_vbox,
1205 "ui_manager", &window->ui_manager,
1206 "view_show_offline", &show_offline_widget,
1207 "view_sort_by_name", &window->sort_by_name,
1208 "view_sort_by_status", &window->sort_by_status,
1209 "view_normal_size_with_avatars", &window->normal_with_avatars,
1210 "view_normal_size", &window->normal_size,
1211 "view_compact_size", &window->compact_size,
1212 "view_history", &window->view_history,
1213 "view_show_map", &show_map_widget,
1214 "room_join_favorites", &window->room_join_favorites,
1215 "presence_toolbar", &window->presence_toolbar,
1216 "roster_scrolledwindow", &sw,
1220 empathy_builder_connect (gui, window,
1221 "main_window", "destroy", main_window_destroy_cb,
1222 "main_window", "configure_event", main_window_configure_event_cb,
1223 "chat_quit", "activate", main_window_chat_quit_cb,
1224 "chat_new_message", "activate", main_window_chat_new_message_cb,
1225 "view_history", "activate", main_window_view_history_cb,
1226 "room_join_new", "activate", main_window_room_join_new_cb,
1227 "room_join_favorites", "activate", main_window_room_join_favorites_cb,
1228 "room_manage_favorites", "activate", main_window_room_manage_favorites_cb,
1229 "chat_add_contact", "activate", main_window_chat_add_contact_cb,
1230 "view_show_ft_manager", "activate", main_window_view_show_ft_manager,
1231 "view_show_offline", "toggled", main_window_view_show_offline_cb,
1232 "view_sort_by_name", "changed", main_window_view_sort_contacts_cb,
1233 "view_normal_size_with_avatars", "changed", main_window_view_contacts_list_size_cb,
1234 "view_show_map", "activate", main_window_view_show_map_cb,
1235 "edit", "activate", main_window_edit_cb,
1236 "edit_accounts", "activate", main_window_edit_accounts_cb,
1237 "edit_personal_information", "activate", main_window_edit_personal_information_cb,
1238 "edit_preferences", "activate", main_window_edit_preferences_cb,
1239 "help_about", "activate", main_window_help_about_cb,
1240 "help_debug", "activate", main_window_help_debug_cb,
1241 "help_contents", "activate", main_window_help_contents_cb,
1244 /* Set up connection related widgets. */
1245 main_window_connection_items_setup (window, gui);
1247 g_object_ref (window->ui_manager);
1248 g_object_unref (gui);
1250 #if !HAVE_LIBCHAMPLAIN
1251 gtk_action_set_visible (show_map_widget, FALSE);
1254 window->account_manager = tp_account_manager_dup ();
1256 tp_account_manager_prepare_async (window->account_manager, NULL,
1257 account_manager_prepared_cb, window);
1259 window->errors = g_hash_table_new_full (g_direct_hash,
1265 main_window_favorite_chatroom_menu_setup (window);
1267 window->edit_context = gtk_ui_manager_get_widget (window->ui_manager,
1268 "/menubar/edit/edit_context");
1269 window->edit_context_separator = gtk_ui_manager_get_widget (window->ui_manager,
1270 "/menubar/edit/edit_context_separator");
1271 gtk_widget_hide (window->edit_context);
1272 gtk_widget_hide (window->edit_context_separator);
1274 /* Set up contact list. */
1275 empathy_status_presets_get_all ();
1277 /* Set up presence chooser */
1278 window->presence_chooser = empathy_presence_chooser_new ();
1279 gtk_widget_show (window->presence_chooser);
1280 item = gtk_tool_item_new ();
1281 gtk_widget_show (GTK_WIDGET (item));
1282 gtk_container_add (GTK_CONTAINER (item), window->presence_chooser);
1283 gtk_tool_item_set_is_important (item, TRUE);
1284 gtk_tool_item_set_expand (item, TRUE);
1285 gtk_toolbar_insert (GTK_TOOLBAR (window->presence_toolbar), item, -1);
1287 /* Set up the throbber */
1288 ebox = gtk_event_box_new ();
1289 gtk_event_box_set_visible_window (GTK_EVENT_BOX (ebox), FALSE);
1290 gtk_widget_set_tooltip_text (ebox, _("Show and edit accounts"));
1291 g_signal_connect (ebox,
1292 "button-press-event",
1293 G_CALLBACK (main_window_throbber_button_press_event_cb),
1295 gtk_widget_show (ebox);
1297 window->throbber = ephy_spinner_new ();
1298 ephy_spinner_set_size (EPHY_SPINNER (window->throbber), GTK_ICON_SIZE_LARGE_TOOLBAR);
1299 gtk_container_add (GTK_CONTAINER (ebox), window->throbber);
1300 gtk_widget_show (window->throbber);
1302 item = gtk_tool_item_new ();
1303 gtk_container_add (GTK_CONTAINER (item), ebox);
1304 gtk_toolbar_insert (GTK_TOOLBAR (window->presence_toolbar), item, -1);
1305 gtk_widget_show (GTK_WIDGET (item));
1307 list_iface = EMPATHY_CONTACT_LIST (empathy_contact_manager_dup_singleton ());
1308 monitor = empathy_contact_list_get_monitor (list_iface);
1309 window->list_store = empathy_contact_list_store_new (list_iface);
1310 window->list_view = empathy_contact_list_view_new (window->list_store,
1311 EMPATHY_CONTACT_LIST_FEATURE_ALL,
1312 EMPATHY_CONTACT_FEATURE_ALL);
1313 g_signal_connect (monitor, "contact-presence-changed",
1314 G_CALLBACK (main_window_contact_presence_changed_cb), window);
1315 g_object_unref (list_iface);
1317 gtk_widget_show (GTK_WIDGET (window->list_view));
1318 gtk_container_add (GTK_CONTAINER (sw),
1319 GTK_WIDGET (window->list_view));
1320 g_signal_connect (window->list_view, "row-activated",
1321 G_CALLBACK (main_window_row_activated_cb),
1324 /* Load user-defined accelerators. */
1325 main_window_accels_load ();
1327 /* Set window size. */
1328 empathy_geometry_load (GEOMETRY_NAME, &x, &y, &w, &h);
1330 if (w >= 1 && h >= 1) {
1331 /* Use the defaults from the ui file if we
1332 * don't have good w, h geometry.
1334 DEBUG ("Configuring window default size w:%d, h:%d", w, h);
1335 gtk_window_set_default_size (GTK_WINDOW (window->window), w, h);
1338 if (x >= 0 && y >= 0) {
1339 /* Let the window manager position it if we
1340 * don't have good x, y coordinates.
1342 DEBUG ("Configuring window default position x:%d, y:%d", x, y);
1343 gtk_window_move (GTK_WINDOW (window->window), x, y);
1346 /* Enable event handling */
1347 window->event_manager = empathy_event_manager_dup_singleton ();
1348 g_signal_connect (window->event_manager, "event-added",
1349 G_CALLBACK (main_window_event_added_cb),
1351 g_signal_connect (window->event_manager, "event-removed",
1352 G_CALLBACK (main_window_event_removed_cb),
1355 g_signal_connect (window->account_manager, "account-validity-changed",
1356 G_CALLBACK (main_window_account_validity_changed_cb),
1358 g_signal_connect (window->account_manager, "account-removed",
1359 G_CALLBACK (main_window_account_removed_cb),
1362 l = empathy_event_manager_get_events (window->event_manager);
1364 main_window_event_added_cb (window->event_manager,
1369 conf = empathy_conf_get ();
1371 /* Show offline ? */
1372 empathy_conf_get_bool (conf,
1373 EMPATHY_PREFS_CONTACTS_SHOW_OFFLINE,
1375 empathy_conf_notify_add (conf,
1376 EMPATHY_PREFS_CONTACTS_SHOW_OFFLINE,
1377 main_window_notify_show_offline_cb,
1378 show_offline_widget);
1380 gtk_toggle_action_set_active (show_offline_widget, show_offline);
1382 /* Sort by name / by status ? */
1383 empathy_conf_notify_add (conf,
1384 EMPATHY_PREFS_CONTACTS_SORT_CRITERIUM,
1385 (EmpathyConfNotifyFunc) main_window_notify_sort_contact_cb,
1388 main_window_notify_sort_contact_cb (conf,
1389 EMPATHY_PREFS_CONTACTS_SORT_CRITERIUM,
1392 /* Contacts list size */
1393 empathy_conf_notify_add (conf,
1394 EMPATHY_PREFS_UI_COMPACT_CONTACT_LIST,
1395 (EmpathyConfNotifyFunc) main_window_notify_contact_list_size_cb,
1397 empathy_conf_notify_add (conf,
1398 EMPATHY_PREFS_UI_SHOW_AVATARS,
1399 (EmpathyConfNotifyFunc) main_window_notify_contact_list_size_cb,
1402 main_window_notify_contact_list_size_cb (conf,
1403 EMPATHY_PREFS_UI_SHOW_AVATARS,
1406 return window->window;