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 <libempathy/empathy-contact.h>
31 #include <libempathy/empathy-utils.h>
32 #include <libempathy/empathy-account-manager.h>
33 #include <libempathy/empathy-dispatcher.h>
34 #include <libempathy/empathy-chatroom-manager.h>
35 #include <libempathy/empathy-chatroom.h>
36 #include <libempathy/empathy-contact-list.h>
37 #include <libempathy/empathy-contact-manager.h>
38 #include <libempathy/empathy-status-presets.h>
40 #include <libempathy-gtk/empathy-contact-dialogs.h>
41 #include <libempathy-gtk/empathy-contact-list-store.h>
42 #include <libempathy-gtk/empathy-contact-list-view.h>
43 #include <libempathy-gtk/empathy-presence-chooser.h>
44 #include <libempathy-gtk/empathy-ui-utils.h>
45 #include <libempathy-gtk/empathy-geometry.h>
46 #include <libempathy-gtk/empathy-conf.h>
47 #include <libempathy-gtk/empathy-log-window.h>
48 #include <libempathy-gtk/empathy-new-message-dialog.h>
49 #include <libempathy-gtk/empathy-gtk-enum-types.h>
51 #include <libmissioncontrol/mission-control.h>
53 #include "empathy-accounts-dialog.h"
54 #include "empathy-main-window.h"
55 #include "ephy-spinner.h"
56 #include "empathy-preferences.h"
57 #include "empathy-about-dialog.h"
58 #include "empathy-new-chatroom-dialog.h"
59 #include "empathy-map-view.h"
60 #include "empathy-chatrooms-window.h"
61 #include "empathy-event-manager.h"
62 #include "empathy-ft-manager.h"
64 #define DEBUG_FLAG EMPATHY_DEBUG_OTHER
65 #include <libempathy/empathy-debug.h>
67 /* Flashing delay for icons (milliseconds). */
68 #define FLASH_TIMEOUT 500
70 /* Minimum width of roster window if something goes wrong. */
73 /* Accels (menu shortcuts) can be configured and saved */
74 #define ACCELS_FILENAME "accels.txt"
76 /* Name in the geometry file */
77 #define GEOMETRY_NAME "main-window"
80 EmpathyContactListView *list_view;
81 EmpathyContactListStore *list_store;
83 EmpathyAccountManager *account_manager;
84 EmpathyChatroomManager *chatroom_manager;
85 EmpathyEventManager *event_manager;
86 guint flash_timeout_id;
92 GtkWidget *presence_toolbar;
93 GtkWidget *presence_chooser;
94 GtkWidget *errors_vbox;
96 GtkUIManager *ui_manager;
97 GtkAction *view_history;
98 GtkAction *room_join_favorites;
100 GtkWidget *room_separator;
101 GtkWidget *edit_context;
102 GtkWidget *edit_context_separator;
104 guint size_timeout_id;
107 /* Actions that are enabled when there are connected accounts */
108 GList *actions_connected;
111 static EmpathyMainWindow *window = NULL;
114 main_window_flash_stop (EmpathyMainWindow *window)
116 if (window->flash_timeout_id == 0) {
120 DEBUG ("Stop flashing");
121 g_source_remove (window->flash_timeout_id);
122 window->flash_timeout_id = 0;
123 window->flash_on = FALSE;
132 main_window_flash_foreach (GtkTreeModel *model,
137 FlashForeachData *data = (FlashForeachData *) user_data;
138 EmpathyContact *contact;
139 const gchar *icon_name;
140 GtkTreePath *parent_path = NULL;
141 GtkTreeIter parent_iter;
143 /* To be used with gtk_tree_model_foreach, update the status icon
144 * of the contact to show the event icon (on=TRUE) or the presence
146 gtk_tree_model_get (model, iter,
147 EMPATHY_CONTACT_LIST_STORE_COL_CONTACT, &contact,
150 if (contact != data->event->contact) {
152 g_object_unref (contact);
158 icon_name = data->event->icon_name;
160 icon_name = empathy_icon_name_for_contact (contact);
163 gtk_tree_store_set (GTK_TREE_STORE (model), iter,
164 EMPATHY_CONTACT_LIST_STORE_COL_ICON_STATUS, icon_name,
167 /* To make sure the parent is shown correctly, we emit
168 * the row-changed signal on the parent so it prompts
169 * it to be refreshed by the filter func.
171 if (gtk_tree_model_iter_parent (model, &parent_iter, iter)) {
172 parent_path = gtk_tree_model_get_path (model, &parent_iter);
175 gtk_tree_model_row_changed (model, parent_path, &parent_iter);
176 gtk_tree_path_free (parent_path);
179 g_object_unref (contact);
185 main_window_flash_cb (EmpathyMainWindow *window)
189 gboolean found_event = FALSE;
190 FlashForeachData data;
192 window->flash_on = !window->flash_on;
193 data.on = window->flash_on;
194 model = GTK_TREE_MODEL (window->list_store);
196 events = empathy_event_manager_get_events (window->event_manager);
197 for (l = events; l; l = l->next) {
198 data.event = l->data;
199 if (!data.event->contact) {
204 gtk_tree_model_foreach (model,
205 main_window_flash_foreach,
210 main_window_flash_stop (window);
217 main_window_flash_start (EmpathyMainWindow *window)
219 if (window->flash_timeout_id != 0) {
223 DEBUG ("Start flashing");
224 window->flash_timeout_id = g_timeout_add (FLASH_TIMEOUT,
225 (GSourceFunc) main_window_flash_cb,
227 main_window_flash_cb (window);
231 main_window_event_added_cb (EmpathyEventManager *manager,
233 EmpathyMainWindow *window)
235 if (event->contact) {
236 main_window_flash_start (window);
241 main_window_event_removed_cb (EmpathyEventManager *manager,
243 EmpathyMainWindow *window)
245 FlashForeachData data;
247 if (!event->contact) {
253 gtk_tree_model_foreach (GTK_TREE_MODEL (window->list_store),
254 main_window_flash_foreach,
259 main_window_row_activated_cb (EmpathyContactListView *view,
261 GtkTreeViewColumn *col,
262 EmpathyMainWindow *window)
264 EmpathyContact *contact;
269 model = GTK_TREE_MODEL (window->list_store);
270 gtk_tree_model_get_iter (model, &iter, path);
271 gtk_tree_model_get (model, &iter,
272 EMPATHY_CONTACT_LIST_STORE_COL_CONTACT, &contact,
279 /* If the contact has an event activate it, otherwise the
280 * default handler of row-activated will be called. */
281 events = empathy_event_manager_get_events (window->event_manager);
282 for (l = events; l; l = l->next) {
283 EmpathyEvent *event = l->data;
285 if (event->contact == contact) {
286 DEBUG ("Activate event");
287 empathy_event_activate (event);
289 /* We don't want the default handler of this signal
290 * (e.g. open a chat) */
291 g_signal_stop_emission_by_name (view, "row-activated");
296 g_object_unref (contact);
300 main_window_error_edit_clicked_cb (GtkButton *button,
301 EmpathyMainWindow *window)
304 GtkWidget *error_widget;
306 account = g_object_get_data (G_OBJECT (button), "account");
307 empathy_accounts_dialog_show (GTK_WINDOW (window->window), account);
309 error_widget = g_hash_table_lookup (window->errors, account);
310 gtk_widget_destroy (error_widget);
311 g_hash_table_remove (window->errors, account);
315 main_window_error_clear_clicked_cb (GtkButton *button,
316 EmpathyMainWindow *window)
319 GtkWidget *error_widget;
321 account = g_object_get_data (G_OBJECT (button), "account");
322 error_widget = g_hash_table_lookup (window->errors, account);
323 gtk_widget_destroy (error_widget);
324 g_hash_table_remove (window->errors, account);
328 main_window_error_display (EmpathyMainWindow *window,
330 const gchar *message)
335 GtkWidget *button_edit;
336 GtkWidget *alignment;
341 GtkWidget *button_close;
344 child = g_hash_table_lookup (window->errors, account);
346 label = g_object_get_data (G_OBJECT (child), "label");
348 /* Just set the latest error and return */
349 str = g_markup_printf_escaped ("<b>%s</b>\n%s",
350 mc_account_get_display_name (account),
352 gtk_label_set_markup (GTK_LABEL (label), str);
358 child = gtk_vbox_new (FALSE, 0);
359 gtk_box_pack_start (GTK_BOX (window->errors_vbox), child, FALSE, TRUE, 0);
360 gtk_container_set_border_width (GTK_CONTAINER (child), 6);
361 gtk_widget_show (child);
363 table = gtk_table_new (2, 4, FALSE);
364 gtk_widget_show (table);
365 gtk_box_pack_start (GTK_BOX (child), table, TRUE, TRUE, 0);
366 gtk_table_set_row_spacings (GTK_TABLE (table), 12);
367 gtk_table_set_col_spacings (GTK_TABLE (table), 6);
369 image = gtk_image_new_from_stock (GTK_STOCK_DISCONNECT, GTK_ICON_SIZE_MENU);
370 gtk_widget_show (image);
371 gtk_table_attach (GTK_TABLE (table), image, 0, 1, 0, 2,
372 (GtkAttachOptions) (GTK_FILL),
373 (GtkAttachOptions) (GTK_FILL), 0, 0);
374 gtk_misc_set_alignment (GTK_MISC (image), 0.5, 0);
376 button_edit = gtk_button_new ();
377 gtk_widget_show (button_edit);
378 gtk_table_attach (GTK_TABLE (table), button_edit, 1, 2, 1, 2,
379 (GtkAttachOptions) (GTK_FILL),
380 (GtkAttachOptions) (0), 0, 0);
382 alignment = gtk_alignment_new (0.5, 0.5, 0, 0);
383 gtk_widget_show (alignment);
384 gtk_container_add (GTK_CONTAINER (button_edit), alignment);
386 hbox = gtk_hbox_new (FALSE, 2);
387 gtk_widget_show (hbox);
388 gtk_container_add (GTK_CONTAINER (alignment), hbox);
390 image = gtk_image_new_from_stock (GTK_STOCK_EDIT, GTK_ICON_SIZE_BUTTON);
391 gtk_widget_show (image);
392 gtk_box_pack_start (GTK_BOX (hbox), image, FALSE, FALSE, 0);
394 label = gtk_label_new_with_mnemonic (_("_Edit account"));
395 gtk_widget_show (label);
396 gtk_box_pack_start (GTK_BOX (hbox), label, FALSE, FALSE, 0);
398 fixed = gtk_fixed_new ();
399 gtk_widget_show (fixed);
400 gtk_table_attach (GTK_TABLE (table), fixed, 2, 3, 1, 2,
401 (GtkAttachOptions) (GTK_EXPAND | GTK_FILL),
402 (GtkAttachOptions) (GTK_FILL), 0, 0);
404 vbox = gtk_vbox_new (FALSE, 6);
405 gtk_widget_show (vbox);
406 gtk_table_attach (GTK_TABLE (table), vbox, 3, 4, 0, 2,
407 (GtkAttachOptions) (GTK_FILL),
408 (GtkAttachOptions) (GTK_FILL), 0, 0);
410 button_close = gtk_button_new ();
411 gtk_widget_show (button_close);
412 gtk_box_pack_start (GTK_BOX (vbox), button_close, FALSE, FALSE, 0);
413 gtk_button_set_relief (GTK_BUTTON (button_close), GTK_RELIEF_NONE);
416 image = gtk_image_new_from_stock ("gtk-close", GTK_ICON_SIZE_MENU);
417 gtk_widget_show (image);
418 gtk_container_add (GTK_CONTAINER (button_close), image);
420 label = gtk_label_new ("");
421 gtk_widget_show (label);
422 gtk_table_attach (GTK_TABLE (table), label, 1, 3, 0, 1,
423 (GtkAttachOptions) (GTK_EXPAND | GTK_SHRINK | GTK_FILL),
424 (GtkAttachOptions) (GTK_EXPAND | GTK_SHRINK | GTK_FILL), 0, 0);
425 gtk_widget_set_size_request (label, 175, -1);
426 gtk_label_set_use_markup (GTK_LABEL (label), TRUE);
427 gtk_label_set_line_wrap (GTK_LABEL (label), TRUE);
428 gtk_misc_set_alignment (GTK_MISC (label), 0, 0);
430 str = g_markup_printf_escaped ("<b>%s</b>\n%s",
431 mc_account_get_display_name (account),
433 gtk_label_set_markup (GTK_LABEL (label), str);
436 g_object_set_data (G_OBJECT (child), "label", label);
437 g_object_set_data_full (G_OBJECT (button_edit),
438 "account", g_object_ref (account),
440 g_object_set_data_full (G_OBJECT (button_close),
441 "account", g_object_ref (account),
444 g_signal_connect (button_edit, "clicked",
445 G_CALLBACK (main_window_error_edit_clicked_cb),
448 g_signal_connect (button_close, "clicked",
449 G_CALLBACK (main_window_error_clear_clicked_cb),
452 gtk_widget_show (window->errors_vbox);
454 g_hash_table_insert (window->errors, g_object_ref (account), child);
458 main_window_update_status (EmpathyMainWindow *window, EmpathyAccountManager *manager)
464 /* Count number of connected/connecting/disconnected accounts */
465 connected = empathy_account_manager_get_connected_accounts (manager);
466 connecting = empathy_account_manager_get_connecting_accounts (manager);
468 /* Update the spinner state */
469 if (connecting > 0) {
470 ephy_spinner_start (EPHY_SPINNER (window->throbber));
472 ephy_spinner_stop (EPHY_SPINNER (window->throbber));
475 /* Update widgets sensibility */
476 for (l = window->actions_connected; l; l = l->next) {
477 gtk_action_set_sensitive (l->data, (connected > 0));
482 main_window_connection_changed_cb (EmpathyAccountManager *manager,
484 TpConnectionStatusReason reason,
485 TpConnectionStatus current,
486 TpConnectionStatus previous,
487 EmpathyMainWindow *window)
489 main_window_update_status (window, manager);
491 if (current == TP_CONNECTION_STATUS_DISCONNECTED &&
492 reason != TP_CONNECTION_STATUS_REASON_REQUESTED) {
493 const gchar *message;
496 case TP_CONNECTION_STATUS_REASON_NONE_SPECIFIED:
497 message = _("No error specified");
499 case TP_CONNECTION_STATUS_REASON_NETWORK_ERROR:
500 message = _("Network error");
502 case TP_CONNECTION_STATUS_REASON_AUTHENTICATION_FAILED:
503 message = _("Authentication failed");
505 case TP_CONNECTION_STATUS_REASON_ENCRYPTION_ERROR:
506 message = _("Encryption error");
508 case TP_CONNECTION_STATUS_REASON_NAME_IN_USE:
509 message = _("Name in use");
511 case TP_CONNECTION_STATUS_REASON_CERT_NOT_PROVIDED:
512 message = _("Certificate not provided");
514 case TP_CONNECTION_STATUS_REASON_CERT_UNTRUSTED:
515 message = _("Certificate untrusted");
517 case TP_CONNECTION_STATUS_REASON_CERT_EXPIRED:
518 message = _("Certificate expired");
520 case TP_CONNECTION_STATUS_REASON_CERT_NOT_ACTIVATED:
521 message = _("Certificate not activated");
523 case TP_CONNECTION_STATUS_REASON_CERT_HOSTNAME_MISMATCH:
524 message = _("Certificate hostname mismatch");
526 case TP_CONNECTION_STATUS_REASON_CERT_FINGERPRINT_MISMATCH:
527 message = _("Certificate fingerprint mismatch");
529 case TP_CONNECTION_STATUS_REASON_CERT_SELF_SIGNED:
530 message = _("Certificate self-signed");
532 case TP_CONNECTION_STATUS_REASON_CERT_OTHER_ERROR:
533 message = _("Certificate error");
536 message = _("Unknown error");
540 main_window_error_display (window, account, message);
543 if (current == TP_CONNECTION_STATUS_DISCONNECTED) {
544 empathy_sound_play (GTK_WIDGET (window->window),
545 EMPATHY_SOUND_ACCOUNT_DISCONNECTED);
548 if (current == TP_CONNECTION_STATUS_CONNECTED) {
549 GtkWidget *error_widget;
551 empathy_sound_play (GTK_WIDGET (window->window),
552 EMPATHY_SOUND_ACCOUNT_CONNECTED);
554 /* Account connected without error, remove error message if any */
555 error_widget = g_hash_table_lookup (window->errors, account);
557 gtk_widget_destroy (error_widget);
558 g_hash_table_remove (window->errors, account);
564 main_window_contact_presence_changed_cb (EmpathyContactMonitor *monitor,
565 EmpathyContact *contact,
566 TpConnectionPresenceType current,
567 TpConnectionPresenceType previous,
568 EmpathyMainWindow *window)
571 gboolean should_play;
573 account = empathy_contact_get_account (contact);
574 should_play = !empathy_account_manager_is_account_just_connected (window->account_manager, account);
580 if (tp_connection_presence_type_cmp_availability (previous,
581 TP_CONNECTION_PRESENCE_TYPE_OFFLINE) > 0)
583 /* contact was online */
584 if (tp_connection_presence_type_cmp_availability (current,
585 TP_CONNECTION_PRESENCE_TYPE_OFFLINE) <= 0)
586 /* someone is logging off */
587 empathy_sound_play (GTK_WIDGET (window->window),
588 EMPATHY_SOUND_CONTACT_DISCONNECTED);
592 /* contact was offline */
593 if (tp_connection_presence_type_cmp_availability (current,
594 TP_CONNECTION_PRESENCE_TYPE_OFFLINE) > 0)
595 /* someone is logging in */
596 empathy_sound_play (GTK_WIDGET (window->window),
597 EMPATHY_SOUND_CONTACT_CONNECTED);
602 main_window_accels_load (void)
606 filename = g_build_filename (g_get_home_dir (), ".gnome2", PACKAGE_NAME, ACCELS_FILENAME, NULL);
607 if (g_file_test (filename, G_FILE_TEST_EXISTS)) {
608 DEBUG ("Loading from:'%s'", filename);
609 gtk_accel_map_load (filename);
616 main_window_accels_save (void)
619 gchar *file_with_path;
621 dir = g_build_filename (g_get_home_dir (), ".gnome2", PACKAGE_NAME, NULL);
622 g_mkdir_with_parents (dir, S_IRUSR | S_IWUSR | S_IXUSR);
623 file_with_path = g_build_filename (dir, ACCELS_FILENAME, NULL);
626 DEBUG ("Saving to:'%s'", file_with_path);
627 gtk_accel_map_save (file_with_path);
629 g_free (file_with_path);
633 main_window_destroy_cb (GtkWidget *widget,
634 EmpathyMainWindow *window)
636 /* Save user-defined accelerators. */
637 main_window_accels_save ();
639 g_signal_handlers_disconnect_by_func (window->account_manager,
640 main_window_connection_changed_cb,
643 if (window->size_timeout_id) {
644 g_source_remove (window->size_timeout_id);
647 g_list_free (window->actions_connected);
649 g_object_unref (window->mc);
650 g_object_unref (window->account_manager);
651 g_object_unref (window->list_store);
652 g_hash_table_destroy (window->errors);
654 g_signal_handlers_disconnect_by_func (window->event_manager,
655 main_window_event_added_cb,
657 g_signal_handlers_disconnect_by_func (window->event_manager,
658 main_window_event_removed_cb,
660 g_object_unref (window->event_manager);
661 g_object_unref (window->ui_manager);
667 main_window_chat_quit_cb (GtkAction *action,
668 EmpathyMainWindow *window)
674 main_window_view_history_cb (GtkAction *action,
675 EmpathyMainWindow *window)
677 empathy_log_window_show (NULL, NULL, FALSE, GTK_WINDOW (window->window));
681 main_window_chat_new_message_cb (GtkAction *action,
682 EmpathyMainWindow *window)
684 empathy_new_message_dialog_show (GTK_WINDOW (window->window));
688 main_window_chat_add_contact_cb (GtkAction *action,
689 EmpathyMainWindow *window)
691 empathy_new_contact_dialog_show (GTK_WINDOW (window->window));
695 main_window_view_show_ft_manager (GtkAction *action,
696 EmpathyMainWindow *window)
698 EmpathyFTManager *manager;
701 manager = empathy_ft_manager_dup_singleton ();
702 dialog = empathy_ft_manager_get_dialog (manager);
704 gtk_window_present (GTK_WINDOW (dialog));
705 g_object_unref (manager);
709 main_window_view_show_offline_cb (GtkToggleAction *action,
710 EmpathyMainWindow *window)
714 current = gtk_toggle_action_get_active (action);
715 empathy_conf_set_bool (empathy_conf_get (),
716 EMPATHY_PREFS_CONTACTS_SHOW_OFFLINE,
719 /* Turn off sound just while we alter the contact list. */
720 // FIXME: empathy_sound_set_enabled (FALSE);
721 empathy_contact_list_store_set_show_offline (window->list_store, current);
722 //empathy_sound_set_enabled (TRUE);
726 main_window_view_show_map_cb (GtkCheckMenuItem *item,
727 EmpathyMainWindow *window)
729 #if HAVE_LIBCHAMPLAIN
730 empathy_map_view_show ();
735 main_window_favorite_chatroom_join (EmpathyChatroom *chatroom)
737 EmpathyAccountManager *manager;
739 TpConnection *connection;
742 manager = empathy_account_manager_dup_singleton ();
743 account = empathy_chatroom_get_account (chatroom);
744 connection = empathy_account_manager_get_connection (manager, account);
745 room = empathy_chatroom_get_room (chatroom);
746 g_object_unref (manager);
748 if (connection != NULL) {
749 DEBUG ("Requesting channel for '%s'", room);
750 empathy_dispatcher_join_muc (connection, room, NULL, NULL);
755 main_window_favorite_chatroom_menu_activate_cb (GtkMenuItem *menu_item,
756 EmpathyChatroom *chatroom)
758 main_window_favorite_chatroom_join (chatroom);
762 main_window_favorite_chatroom_menu_add (EmpathyMainWindow *window,
763 EmpathyChatroom *chatroom)
765 GtkWidget *menu_item;
768 if (g_object_get_data (G_OBJECT (chatroom), "menu_item")) {
772 name = empathy_chatroom_get_name (chatroom);
773 menu_item = gtk_menu_item_new_with_label (name);
775 g_object_set_data (G_OBJECT (chatroom), "menu_item", menu_item);
776 g_signal_connect (menu_item, "activate",
777 G_CALLBACK (main_window_favorite_chatroom_menu_activate_cb),
780 gtk_menu_shell_insert (GTK_MENU_SHELL (window->room_menu),
783 gtk_widget_show (menu_item);
787 main_window_favorite_chatroom_menu_added_cb (EmpathyChatroomManager *manager,
788 EmpathyChatroom *chatroom,
789 EmpathyMainWindow *window)
791 main_window_favorite_chatroom_menu_add (window, chatroom);
792 gtk_widget_show (window->room_separator);
793 gtk_action_set_sensitive (window->room_join_favorites, TRUE);
797 main_window_favorite_chatroom_menu_removed_cb (EmpathyChatroomManager *manager,
798 EmpathyChatroom *chatroom,
799 EmpathyMainWindow *window)
801 GtkWidget *menu_item;
804 menu_item = g_object_get_data (G_OBJECT (chatroom), "menu_item");
805 g_object_set_data (G_OBJECT (chatroom), "menu_item", NULL);
806 gtk_widget_destroy (menu_item);
808 chatrooms = empathy_chatroom_manager_get_chatrooms (window->chatroom_manager, NULL);
810 gtk_widget_show (window->room_separator);
812 gtk_widget_hide (window->room_separator);
815 gtk_action_set_sensitive (window->room_join_favorites, chatrooms != NULL);
816 g_list_free (chatrooms);
820 main_window_favorite_chatroom_menu_setup (EmpathyMainWindow *window)
822 GList *chatrooms, *l;
825 window->chatroom_manager = empathy_chatroom_manager_dup_singleton (NULL);
826 chatrooms = empathy_chatroom_manager_get_chatrooms (window->chatroom_manager, NULL);
827 room = gtk_ui_manager_get_widget (window->ui_manager,
829 window->room_menu = gtk_menu_item_get_submenu (GTK_MENU_ITEM (room));
830 window->room_separator = gtk_ui_manager_get_widget (window->ui_manager,
831 "/menubar/room/room_separator");
833 for (l = chatrooms; l; l = l->next) {
834 main_window_favorite_chatroom_menu_add (window, l->data);
838 gtk_widget_hide (window->room_separator);
841 gtk_action_set_sensitive (window->room_join_favorites, chatrooms != NULL);
843 g_signal_connect (window->chatroom_manager, "chatroom-added",
844 G_CALLBACK (main_window_favorite_chatroom_menu_added_cb),
846 g_signal_connect (window->chatroom_manager, "chatroom-removed",
847 G_CALLBACK (main_window_favorite_chatroom_menu_removed_cb),
850 g_list_free (chatrooms);
854 main_window_room_join_new_cb (GtkAction *action,
855 EmpathyMainWindow *window)
857 empathy_new_chatroom_dialog_show (GTK_WINDOW (window->window));
861 main_window_room_join_favorites_cb (GtkAction *action,
862 EmpathyMainWindow *window)
864 GList *chatrooms, *l;
866 chatrooms = empathy_chatroom_manager_get_chatrooms (window->chatroom_manager, NULL);
867 for (l = chatrooms; l; l = l->next) {
868 main_window_favorite_chatroom_join (l->data);
870 g_list_free (chatrooms);
874 main_window_room_manage_favorites_cb (GtkAction *action,
875 EmpathyMainWindow *window)
877 empathy_chatrooms_window_show (GTK_WINDOW (window->window));
881 main_window_edit_cb (GtkAction *action,
882 EmpathyMainWindow *window)
886 /* FIXME: It should use the UIManager to merge the contact/group submenu */
887 submenu = empathy_contact_list_view_get_contact_menu (window->list_view);
892 item = GTK_MENU_ITEM (window->edit_context);
893 label = gtk_bin_get_child (GTK_BIN (item));
894 gtk_label_set_text (GTK_LABEL (label), _("Contact"));
896 gtk_widget_show (window->edit_context);
897 gtk_widget_show (window->edit_context_separator);
899 gtk_menu_item_set_submenu (item, submenu);
904 submenu = empathy_contact_list_view_get_group_menu (window->list_view);
909 item = GTK_MENU_ITEM (window->edit_context);
910 label = gtk_bin_get_child (GTK_BIN (item));
911 gtk_label_set_text (GTK_LABEL (label), _("Group"));
913 gtk_widget_show (window->edit_context);
914 gtk_widget_show (window->edit_context_separator);
916 gtk_menu_item_set_submenu (item, submenu);
921 gtk_widget_hide (window->edit_context);
922 gtk_widget_hide (window->edit_context_separator);
928 main_window_edit_accounts_cb (GtkAction *action,
929 EmpathyMainWindow *window)
931 empathy_accounts_dialog_show (GTK_WINDOW (window->window), NULL);
935 main_window_edit_personal_information_cb (GtkAction *action,
936 EmpathyMainWindow *window)
938 empathy_contact_personal_dialog_show (GTK_WINDOW (window->window));
942 main_window_edit_preferences_cb (GtkAction *action,
943 EmpathyMainWindow *window)
945 empathy_preferences_show (GTK_WINDOW (window->window));
949 main_window_help_about_cb (GtkAction *action,
950 EmpathyMainWindow *window)
952 empathy_about_dialog_new (GTK_WINDOW (window->window));
956 main_window_help_contents_cb (GtkAction *action,
957 EmpathyMainWindow *window)
959 empathy_url_show (window->window, "ghelp:empathy");
963 main_window_throbber_button_press_event_cb (GtkWidget *throbber_ebox,
964 GdkEventButton *event,
965 EmpathyMainWindow *window)
967 if (event->type != GDK_BUTTON_PRESS ||
968 event->button != 1) {
972 empathy_accounts_dialog_show (GTK_WINDOW (window->window), NULL);
978 main_window_configure_event_timeout_cb (EmpathyMainWindow *window)
982 gtk_window_get_size (GTK_WINDOW (window->window), &w, &h);
983 gtk_window_get_position (GTK_WINDOW (window->window), &x, &y);
985 empathy_geometry_save (GEOMETRY_NAME, x, y, w, h);
987 window->size_timeout_id = 0;
993 main_window_configure_event_cb (GtkWidget *widget,
994 GdkEventConfigure *event,
995 EmpathyMainWindow *window)
997 if (window->size_timeout_id) {
998 g_source_remove (window->size_timeout_id);
1001 window->size_timeout_id = g_timeout_add_seconds (1,
1002 (GSourceFunc) main_window_configure_event_timeout_cb,
1009 main_window_account_created_or_deleted_cb (EmpathyAccountManager *manager,
1011 EmpathyMainWindow *window)
1013 gtk_action_set_sensitive (window->view_history,
1014 empathy_account_manager_get_count (manager) > 0);
1018 main_window_notify_show_offline_cb (EmpathyConf *conf,
1020 gpointer toggle_action)
1022 gboolean show_offline;
1024 if (empathy_conf_get_bool (conf, key, &show_offline)) {
1025 gtk_toggle_action_set_active (toggle_action, show_offline);
1030 main_window_notify_show_avatars_cb (EmpathyConf *conf,
1032 EmpathyMainWindow *window)
1034 gboolean show_avatars;
1036 if (empathy_conf_get_bool (conf, key, &show_avatars)) {
1037 empathy_contact_list_store_set_show_avatars (window->list_store,
1043 main_window_notify_compact_contact_list_cb (EmpathyConf *conf,
1045 EmpathyMainWindow *window)
1047 gboolean compact_contact_list;
1049 if (empathy_conf_get_bool (conf, key, &compact_contact_list)) {
1050 empathy_contact_list_store_set_is_compact (window->list_store,
1051 compact_contact_list);
1056 main_window_notify_sort_criterium_cb (EmpathyConf *conf,
1058 EmpathyMainWindow *window)
1062 if (empathy_conf_get_string (conf, key, &str) && str) {
1064 GEnumClass *enum_class;
1065 GEnumValue *enum_value;
1067 type = empathy_contact_list_store_sort_get_type ();
1068 enum_class = G_ENUM_CLASS (g_type_class_peek (type));
1069 enum_value = g_enum_get_value_by_nick (enum_class, str);
1073 empathy_contact_list_store_set_sort_criterium (window->list_store,
1080 main_window_connection_items_setup (EmpathyMainWindow *window,
1086 const gchar *actions_connected[] = {
1090 "edit_personal_information"
1093 for (i = 0, list = NULL; i < G_N_ELEMENTS (actions_connected); i++) {
1094 action = gtk_builder_get_object (gui, actions_connected[i]);
1095 list = g_list_prepend (list, action);
1098 window->actions_connected = list;
1102 empathy_main_window_get (void)
1104 return window != NULL ? window->window : NULL;
1108 empathy_main_window_show (void)
1110 EmpathyContactList *list_iface;
1111 EmpathyContactMonitor *monitor;
1115 GtkToggleAction *show_offline_widget;
1117 GtkAction *show_map_widget;
1119 gboolean show_offline;
1120 gboolean show_avatars;
1121 gboolean compact_contact_list;
1127 empathy_window_present (GTK_WINDOW (window->window), TRUE);
1128 return window->window;
1131 window = g_new0 (EmpathyMainWindow, 1);
1133 /* Set up interface */
1134 filename = empathy_file_lookup ("empathy-main-window.ui", "src");
1135 gui = empathy_builder_get_file (filename,
1136 "main_window", &window->window,
1137 "main_vbox", &window->main_vbox,
1138 "errors_vbox", &window->errors_vbox,
1139 "ui_manager", &window->ui_manager,
1140 "view_show_offline", &show_offline_widget,
1141 "view_history", &window->view_history,
1142 "view_show_map", &show_map_widget,
1143 "room_join_favorites", &window->room_join_favorites,
1144 "presence_toolbar", &window->presence_toolbar,
1145 "roster_scrolledwindow", &sw,
1149 empathy_builder_connect (gui, window,
1150 "main_window", "destroy", main_window_destroy_cb,
1151 "main_window", "configure_event", main_window_configure_event_cb,
1152 "chat_quit", "activate", main_window_chat_quit_cb,
1153 "chat_new_message", "activate", main_window_chat_new_message_cb,
1154 "view_history", "activate", main_window_view_history_cb,
1155 "room_join_new", "activate", main_window_room_join_new_cb,
1156 "room_join_favorites", "activate", main_window_room_join_favorites_cb,
1157 "room_manage_favorites", "activate", main_window_room_manage_favorites_cb,
1158 "chat_add_contact", "activate", main_window_chat_add_contact_cb,
1159 "view_show_ft_manager", "activate", main_window_view_show_ft_manager,
1160 "view_show_offline", "toggled", main_window_view_show_offline_cb,
1161 "view_show_map", "activate", main_window_view_show_map_cb,
1162 "edit", "activate", main_window_edit_cb,
1163 "edit_accounts", "activate", main_window_edit_accounts_cb,
1164 "edit_personal_information", "activate", main_window_edit_personal_information_cb,
1165 "edit_preferences", "activate", main_window_edit_preferences_cb,
1166 "help_about", "activate", main_window_help_about_cb,
1167 "help_contents", "activate", main_window_help_contents_cb,
1170 /* Set up connection related widgets. */
1171 main_window_connection_items_setup (window, gui);
1173 g_object_ref (window->ui_manager);
1174 g_object_unref (gui);
1176 #if !HAVE_LIBCHAMPLAIN
1177 gtk_action_set_visible (show_map_widget, FALSE);
1180 window->mc = empathy_mission_control_dup_singleton ();
1181 window->account_manager = empathy_account_manager_dup_singleton ();
1183 g_signal_connect (window->account_manager,
1184 "account-connection-changed",
1185 G_CALLBACK (main_window_connection_changed_cb), window);
1187 window->errors = g_hash_table_new_full (empathy_account_hash,
1188 empathy_account_equal,
1193 main_window_favorite_chatroom_menu_setup (window);
1195 window->edit_context = gtk_ui_manager_get_widget (window->ui_manager,
1196 "/menubar/edit/edit_context");
1197 window->edit_context_separator = gtk_ui_manager_get_widget (window->ui_manager,
1198 "/menubar/edit/edit_context_separator");
1199 gtk_widget_hide (window->edit_context);
1200 gtk_widget_hide (window->edit_context_separator);
1202 /* Set up contact list. */
1203 empathy_status_presets_get_all ();
1205 /* Set up presence chooser */
1206 window->presence_chooser = empathy_presence_chooser_new ();
1207 gtk_widget_show (window->presence_chooser);
1208 item = gtk_tool_item_new ();
1209 gtk_widget_show (GTK_WIDGET (item));
1210 gtk_container_add (GTK_CONTAINER (item), window->presence_chooser);
1211 gtk_tool_item_set_is_important (item, TRUE);
1212 gtk_tool_item_set_expand (item, TRUE);
1213 gtk_toolbar_insert (GTK_TOOLBAR (window->presence_toolbar), item, -1);
1215 /* Set up the throbber */
1216 ebox = gtk_event_box_new ();
1217 gtk_event_box_set_visible_window (GTK_EVENT_BOX (ebox), FALSE);
1218 gtk_widget_set_tooltip_text (ebox, _("Show and edit accounts"));
1219 g_signal_connect (ebox,
1220 "button-press-event",
1221 G_CALLBACK (main_window_throbber_button_press_event_cb),
1223 gtk_widget_show (ebox);
1225 window->throbber = ephy_spinner_new ();
1226 ephy_spinner_set_size (EPHY_SPINNER (window->throbber), GTK_ICON_SIZE_LARGE_TOOLBAR);
1227 gtk_container_add (GTK_CONTAINER (ebox), window->throbber);
1228 gtk_widget_show (window->throbber);
1230 item = gtk_tool_item_new ();
1231 gtk_container_add (GTK_CONTAINER (item), ebox);
1232 gtk_toolbar_insert (GTK_TOOLBAR (window->presence_toolbar), item, -1);
1233 gtk_widget_show (GTK_WIDGET (item));
1235 list_iface = EMPATHY_CONTACT_LIST (empathy_contact_manager_dup_singleton ());
1236 monitor = empathy_contact_list_get_monitor (list_iface);
1237 window->list_store = empathy_contact_list_store_new (list_iface);
1238 window->list_view = empathy_contact_list_view_new (window->list_store,
1239 EMPATHY_CONTACT_LIST_FEATURE_ALL,
1240 EMPATHY_CONTACT_FEATURE_ALL);
1241 g_signal_connect (monitor, "contact-presence-changed",
1242 G_CALLBACK (main_window_contact_presence_changed_cb), window);
1243 g_object_unref (list_iface);
1245 gtk_widget_show (GTK_WIDGET (window->list_view));
1246 gtk_container_add (GTK_CONTAINER (sw),
1247 GTK_WIDGET (window->list_view));
1248 g_signal_connect (window->list_view, "row-activated",
1249 G_CALLBACK (main_window_row_activated_cb),
1252 /* Load user-defined accelerators. */
1253 main_window_accels_load ();
1255 /* Set window size. */
1256 empathy_geometry_load (GEOMETRY_NAME, &x, &y, &w, &h);
1258 if (w >= 1 && h >= 1) {
1259 /* Use the defaults from the ui file if we
1260 * don't have good w, h geometry.
1262 DEBUG ("Configuring window default size w:%d, h:%d", w, h);
1263 gtk_window_set_default_size (GTK_WINDOW (window->window), w, h);
1266 if (x >= 0 && y >= 0) {
1267 /* Let the window manager position it if we
1268 * don't have good x, y coordinates.
1270 DEBUG ("Configuring window default position x:%d, y:%d", x, y);
1271 gtk_window_move (GTK_WINDOW (window->window), x, y);
1274 /* Enable event handling */
1275 window->event_manager = empathy_event_manager_dup_singleton ();
1276 g_signal_connect (window->event_manager, "event-added",
1277 G_CALLBACK (main_window_event_added_cb),
1279 g_signal_connect (window->event_manager, "event-removed",
1280 G_CALLBACK (main_window_event_removed_cb),
1283 g_signal_connect (window->account_manager, "account-created",
1284 G_CALLBACK (main_window_account_created_or_deleted_cb),
1286 g_signal_connect (window->account_manager, "account-deleted",
1287 G_CALLBACK (main_window_account_created_or_deleted_cb),
1289 main_window_account_created_or_deleted_cb (window->account_manager, NULL, window);
1291 l = empathy_event_manager_get_events (window->event_manager);
1293 main_window_event_added_cb (window->event_manager,
1298 conf = empathy_conf_get ();
1300 /* Show offline ? */
1301 empathy_conf_get_bool (conf,
1302 EMPATHY_PREFS_CONTACTS_SHOW_OFFLINE,
1304 empathy_conf_notify_add (conf,
1305 EMPATHY_PREFS_CONTACTS_SHOW_OFFLINE,
1306 main_window_notify_show_offline_cb,
1307 show_offline_widget);
1309 gtk_toggle_action_set_active (show_offline_widget, show_offline);
1311 /* Show avatars ? */
1312 empathy_conf_get_bool (conf,
1313 EMPATHY_PREFS_UI_SHOW_AVATARS,
1315 empathy_conf_notify_add (conf,
1316 EMPATHY_PREFS_UI_SHOW_AVATARS,
1317 (EmpathyConfNotifyFunc) main_window_notify_show_avatars_cb,
1319 empathy_contact_list_store_set_show_avatars (window->list_store, show_avatars);
1322 empathy_conf_get_bool (conf,
1323 EMPATHY_PREFS_UI_COMPACT_CONTACT_LIST,
1324 &compact_contact_list);
1325 empathy_conf_notify_add (conf,
1326 EMPATHY_PREFS_UI_COMPACT_CONTACT_LIST,
1327 (EmpathyConfNotifyFunc) main_window_notify_compact_contact_list_cb,
1329 empathy_contact_list_store_set_is_compact (window->list_store, compact_contact_list);
1331 /* Sort criterium */
1332 empathy_conf_notify_add (conf,
1333 EMPATHY_PREFS_CONTACTS_SORT_CRITERIUM,
1334 (EmpathyConfNotifyFunc) main_window_notify_sort_criterium_cb,
1336 main_window_notify_sort_criterium_cb (conf,
1337 EMPATHY_PREFS_CONTACTS_SORT_CRITERIUM,
1340 main_window_update_status (window, window->account_manager);
1342 return window->window;