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-conf.h>
41 #include <libempathy-gtk/empathy-contact-dialogs.h>
42 #include <libempathy-gtk/empathy-contact-list-store.h>
43 #include <libempathy-gtk/empathy-contact-list-view.h>
44 #include <libempathy-gtk/empathy-geometry.h>
45 #include <libempathy-gtk/empathy-gtk-enum-types.h>
46 #include <libempathy-gtk/empathy-new-message-dialog.h>
47 #include <libempathy-gtk/empathy-log-window.h>
48 #include <libempathy-gtk/empathy-presence-chooser.h>
49 #include <libempathy-gtk/empathy-sound.h>
50 #include <libempathy-gtk/empathy-ui-utils.h>
52 #include "empathy-accounts-dialog.h"
53 #include "empathy-main-window.h"
54 #include "ephy-spinner.h"
55 #include "empathy-preferences.h"
56 #include "empathy-about-dialog.h"
57 #include "empathy-debug-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 || !data.event->must_ack) {
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)
303 EmpathyAccount *account;
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)
318 EmpathyAccount *account;
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,
329 EmpathyAccount *account,
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 empathy_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 empathy_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,
483 EmpathyAccount *account,
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)
570 EmpathyAccount *account;
571 gboolean should_play;
573 account = empathy_contact_get_account (contact);
574 should_play = !empathy_account_is_just_connected (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 empathy_ft_manager_show ();
702 main_window_view_show_offline_cb (GtkToggleAction *action,
703 EmpathyMainWindow *window)
707 current = gtk_toggle_action_get_active (action);
708 empathy_conf_set_bool (empathy_conf_get (),
709 EMPATHY_PREFS_CONTACTS_SHOW_OFFLINE,
712 /* Turn off sound just while we alter the contact list. */
713 // FIXME: empathy_sound_set_enabled (FALSE);
714 empathy_contact_list_store_set_show_offline (window->list_store, current);
715 //empathy_sound_set_enabled (TRUE);
719 main_window_view_show_map_cb (GtkCheckMenuItem *item,
720 EmpathyMainWindow *window)
722 #if HAVE_LIBCHAMPLAIN
723 empathy_map_view_show ();
728 main_window_favorite_chatroom_join (EmpathyChatroom *chatroom)
730 EmpathyAccount *account;
731 TpConnection *connection;
734 account = empathy_chatroom_get_account (chatroom);
735 connection = empathy_account_get_connection (account);
736 room = empathy_chatroom_get_room (chatroom);
738 if (connection != NULL) {
739 DEBUG ("Requesting channel for '%s'", room);
740 empathy_dispatcher_join_muc (connection, room, NULL, NULL);
745 main_window_favorite_chatroom_menu_activate_cb (GtkMenuItem *menu_item,
746 EmpathyChatroom *chatroom)
748 main_window_favorite_chatroom_join (chatroom);
752 main_window_favorite_chatroom_menu_add (EmpathyMainWindow *window,
753 EmpathyChatroom *chatroom)
755 GtkWidget *menu_item;
758 if (g_object_get_data (G_OBJECT (chatroom), "menu_item")) {
762 name = empathy_chatroom_get_name (chatroom);
763 menu_item = gtk_menu_item_new_with_label (name);
765 g_object_set_data (G_OBJECT (chatroom), "menu_item", menu_item);
766 g_signal_connect (menu_item, "activate",
767 G_CALLBACK (main_window_favorite_chatroom_menu_activate_cb),
770 gtk_menu_shell_insert (GTK_MENU_SHELL (window->room_menu),
773 gtk_widget_show (menu_item);
777 main_window_favorite_chatroom_menu_added_cb (EmpathyChatroomManager *manager,
778 EmpathyChatroom *chatroom,
779 EmpathyMainWindow *window)
781 main_window_favorite_chatroom_menu_add (window, chatroom);
782 gtk_widget_show (window->room_separator);
783 gtk_action_set_sensitive (window->room_join_favorites, TRUE);
787 main_window_favorite_chatroom_menu_removed_cb (EmpathyChatroomManager *manager,
788 EmpathyChatroom *chatroom,
789 EmpathyMainWindow *window)
791 GtkWidget *menu_item;
794 menu_item = g_object_get_data (G_OBJECT (chatroom), "menu_item");
795 g_object_set_data (G_OBJECT (chatroom), "menu_item", NULL);
796 gtk_widget_destroy (menu_item);
798 chatrooms = empathy_chatroom_manager_get_chatrooms (window->chatroom_manager, NULL);
800 gtk_widget_show (window->room_separator);
802 gtk_widget_hide (window->room_separator);
805 gtk_action_set_sensitive (window->room_join_favorites, chatrooms != NULL);
806 g_list_free (chatrooms);
810 main_window_favorite_chatroom_menu_setup (EmpathyMainWindow *window)
812 GList *chatrooms, *l;
815 window->chatroom_manager = empathy_chatroom_manager_dup_singleton (NULL);
816 chatrooms = empathy_chatroom_manager_get_chatrooms (window->chatroom_manager, NULL);
817 room = gtk_ui_manager_get_widget (window->ui_manager,
819 window->room_menu = gtk_menu_item_get_submenu (GTK_MENU_ITEM (room));
820 window->room_separator = gtk_ui_manager_get_widget (window->ui_manager,
821 "/menubar/room/room_separator");
823 for (l = chatrooms; l; l = l->next) {
824 main_window_favorite_chatroom_menu_add (window, l->data);
828 gtk_widget_hide (window->room_separator);
831 gtk_action_set_sensitive (window->room_join_favorites, chatrooms != NULL);
833 g_signal_connect (window->chatroom_manager, "chatroom-added",
834 G_CALLBACK (main_window_favorite_chatroom_menu_added_cb),
836 g_signal_connect (window->chatroom_manager, "chatroom-removed",
837 G_CALLBACK (main_window_favorite_chatroom_menu_removed_cb),
840 g_list_free (chatrooms);
844 main_window_room_join_new_cb (GtkAction *action,
845 EmpathyMainWindow *window)
847 empathy_new_chatroom_dialog_show (GTK_WINDOW (window->window));
851 main_window_room_join_favorites_cb (GtkAction *action,
852 EmpathyMainWindow *window)
854 GList *chatrooms, *l;
856 chatrooms = empathy_chatroom_manager_get_chatrooms (window->chatroom_manager, NULL);
857 for (l = chatrooms; l; l = l->next) {
858 main_window_favorite_chatroom_join (l->data);
860 g_list_free (chatrooms);
864 main_window_room_manage_favorites_cb (GtkAction *action,
865 EmpathyMainWindow *window)
867 empathy_chatrooms_window_show (GTK_WINDOW (window->window));
871 main_window_edit_cb (GtkAction *action,
872 EmpathyMainWindow *window)
876 /* FIXME: It should use the UIManager to merge the contact/group submenu */
877 submenu = empathy_contact_list_view_get_contact_menu (window->list_view);
882 item = GTK_MENU_ITEM (window->edit_context);
883 label = gtk_bin_get_child (GTK_BIN (item));
884 gtk_label_set_text (GTK_LABEL (label), _("Contact"));
886 gtk_widget_show (window->edit_context);
887 gtk_widget_show (window->edit_context_separator);
889 gtk_menu_item_set_submenu (item, submenu);
894 submenu = empathy_contact_list_view_get_group_menu (window->list_view);
899 item = GTK_MENU_ITEM (window->edit_context);
900 label = gtk_bin_get_child (GTK_BIN (item));
901 gtk_label_set_text (GTK_LABEL (label), _("Group"));
903 gtk_widget_show (window->edit_context);
904 gtk_widget_show (window->edit_context_separator);
906 gtk_menu_item_set_submenu (item, submenu);
911 gtk_widget_hide (window->edit_context);
912 gtk_widget_hide (window->edit_context_separator);
918 main_window_edit_accounts_cb (GtkAction *action,
919 EmpathyMainWindow *window)
921 empathy_accounts_dialog_show (GTK_WINDOW (window->window), NULL);
925 main_window_edit_personal_information_cb (GtkAction *action,
926 EmpathyMainWindow *window)
928 empathy_contact_personal_dialog_show (GTK_WINDOW (window->window));
932 main_window_edit_preferences_cb (GtkAction *action,
933 EmpathyMainWindow *window)
935 empathy_preferences_show (GTK_WINDOW (window->window));
939 main_window_help_about_cb (GtkAction *action,
940 EmpathyMainWindow *window)
942 empathy_about_dialog_new (GTK_WINDOW (window->window));
946 main_window_help_debug_cb (GtkAction *action,
947 EmpathyMainWindow *window)
949 empathy_debug_dialog_new (GTK_WINDOW (window->window));
953 main_window_help_contents_cb (GtkAction *action,
954 EmpathyMainWindow *window)
956 empathy_url_show (window->window, "ghelp:empathy");
960 main_window_throbber_button_press_event_cb (GtkWidget *throbber_ebox,
961 GdkEventButton *event,
962 EmpathyMainWindow *window)
964 if (event->type != GDK_BUTTON_PRESS ||
965 event->button != 1) {
969 empathy_accounts_dialog_show (GTK_WINDOW (window->window), NULL);
975 main_window_configure_event_timeout_cb (EmpathyMainWindow *window)
979 gtk_window_get_size (GTK_WINDOW (window->window), &w, &h);
980 gtk_window_get_position (GTK_WINDOW (window->window), &x, &y);
982 empathy_geometry_save (GEOMETRY_NAME, x, y, w, h);
984 window->size_timeout_id = 0;
990 main_window_configure_event_cb (GtkWidget *widget,
991 GdkEventConfigure *event,
992 EmpathyMainWindow *window)
994 if (window->size_timeout_id) {
995 g_source_remove (window->size_timeout_id);
998 window->size_timeout_id = g_timeout_add_seconds (1,
999 (GSourceFunc) main_window_configure_event_timeout_cb,
1006 main_window_account_created_or_deleted_cb (EmpathyAccountManager *manager,
1007 EmpathyAccount *account,
1008 EmpathyMainWindow *window)
1010 gtk_action_set_sensitive (window->view_history,
1011 empathy_account_manager_get_count (manager) > 0);
1015 main_window_notify_show_offline_cb (EmpathyConf *conf,
1017 gpointer toggle_action)
1019 gboolean show_offline;
1021 if (empathy_conf_get_bool (conf, key, &show_offline)) {
1022 gtk_toggle_action_set_active (toggle_action, show_offline);
1027 main_window_notify_show_avatars_cb (EmpathyConf *conf,
1029 EmpathyMainWindow *window)
1031 gboolean show_avatars;
1033 if (empathy_conf_get_bool (conf, key, &show_avatars)) {
1034 empathy_contact_list_store_set_show_avatars (window->list_store,
1040 main_window_notify_compact_contact_list_cb (EmpathyConf *conf,
1042 EmpathyMainWindow *window)
1044 gboolean compact_contact_list;
1046 if (empathy_conf_get_bool (conf, key, &compact_contact_list)) {
1047 empathy_contact_list_store_set_is_compact (window->list_store,
1048 compact_contact_list);
1053 main_window_notify_sort_criterium_cb (EmpathyConf *conf,
1055 EmpathyMainWindow *window)
1059 if (empathy_conf_get_string (conf, key, &str) && str) {
1061 GEnumClass *enum_class;
1062 GEnumValue *enum_value;
1064 type = empathy_contact_list_store_sort_get_type ();
1065 enum_class = G_ENUM_CLASS (g_type_class_peek (type));
1066 enum_value = g_enum_get_value_by_nick (enum_class, str);
1070 empathy_contact_list_store_set_sort_criterium (window->list_store,
1077 main_window_connection_items_setup (EmpathyMainWindow *window,
1083 const gchar *actions_connected[] = {
1087 "edit_personal_information"
1090 for (i = 0, list = NULL; i < G_N_ELEMENTS (actions_connected); i++) {
1091 action = gtk_builder_get_object (gui, actions_connected[i]);
1092 list = g_list_prepend (list, action);
1095 window->actions_connected = list;
1099 empathy_main_window_get (void)
1101 return window != NULL ? window->window : NULL;
1105 empathy_main_window_show (void)
1107 EmpathyContactList *list_iface;
1108 EmpathyContactMonitor *monitor;
1112 GtkToggleAction *show_offline_widget;
1114 GtkAction *show_map_widget;
1116 gboolean show_offline;
1117 gboolean show_avatars;
1118 gboolean compact_contact_list;
1124 empathy_window_present (GTK_WINDOW (window->window), TRUE);
1125 return window->window;
1128 window = g_new0 (EmpathyMainWindow, 1);
1130 /* Set up interface */
1131 filename = empathy_file_lookup ("empathy-main-window.ui", "src");
1132 gui = empathy_builder_get_file (filename,
1133 "main_window", &window->window,
1134 "main_vbox", &window->main_vbox,
1135 "errors_vbox", &window->errors_vbox,
1136 "ui_manager", &window->ui_manager,
1137 "view_show_offline", &show_offline_widget,
1138 "view_history", &window->view_history,
1139 "view_show_map", &show_map_widget,
1140 "room_join_favorites", &window->room_join_favorites,
1141 "presence_toolbar", &window->presence_toolbar,
1142 "roster_scrolledwindow", &sw,
1146 empathy_builder_connect (gui, window,
1147 "main_window", "destroy", main_window_destroy_cb,
1148 "main_window", "configure_event", main_window_configure_event_cb,
1149 "chat_quit", "activate", main_window_chat_quit_cb,
1150 "chat_new_message", "activate", main_window_chat_new_message_cb,
1151 "view_history", "activate", main_window_view_history_cb,
1152 "room_join_new", "activate", main_window_room_join_new_cb,
1153 "room_join_favorites", "activate", main_window_room_join_favorites_cb,
1154 "room_manage_favorites", "activate", main_window_room_manage_favorites_cb,
1155 "chat_add_contact", "activate", main_window_chat_add_contact_cb,
1156 "view_show_ft_manager", "activate", main_window_view_show_ft_manager,
1157 "view_show_offline", "toggled", main_window_view_show_offline_cb,
1158 "view_show_map", "activate", main_window_view_show_map_cb,
1159 "edit", "activate", main_window_edit_cb,
1160 "edit_accounts", "activate", main_window_edit_accounts_cb,
1161 "edit_personal_information", "activate", main_window_edit_personal_information_cb,
1162 "edit_preferences", "activate", main_window_edit_preferences_cb,
1163 "help_about", "activate", main_window_help_about_cb,
1164 "help_debug", "activate", main_window_help_debug_cb,
1165 "help_contents", "activate", main_window_help_contents_cb,
1168 /* Set up connection related widgets. */
1169 main_window_connection_items_setup (window, gui);
1171 g_object_ref (window->ui_manager);
1172 g_object_unref (gui);
1174 #if !HAVE_LIBCHAMPLAIN
1175 gtk_action_set_visible (show_map_widget, FALSE);
1178 window->mc = empathy_mission_control_dup_singleton ();
1179 window->account_manager = empathy_account_manager_dup_singleton ();
1181 g_signal_connect (window->account_manager,
1182 "account-connection-changed",
1183 G_CALLBACK (main_window_connection_changed_cb), window);
1185 window->errors = g_hash_table_new_full (empathy_account_hash,
1186 empathy_account_equal,
1191 main_window_favorite_chatroom_menu_setup (window);
1193 window->edit_context = gtk_ui_manager_get_widget (window->ui_manager,
1194 "/menubar/edit/edit_context");
1195 window->edit_context_separator = gtk_ui_manager_get_widget (window->ui_manager,
1196 "/menubar/edit/edit_context_separator");
1197 gtk_widget_hide (window->edit_context);
1198 gtk_widget_hide (window->edit_context_separator);
1200 /* Set up contact list. */
1201 empathy_status_presets_get_all ();
1203 /* Set up presence chooser */
1204 window->presence_chooser = empathy_presence_chooser_new ();
1205 gtk_widget_show (window->presence_chooser);
1206 item = gtk_tool_item_new ();
1207 gtk_widget_show (GTK_WIDGET (item));
1208 gtk_container_add (GTK_CONTAINER (item), window->presence_chooser);
1209 gtk_tool_item_set_is_important (item, TRUE);
1210 gtk_tool_item_set_expand (item, TRUE);
1211 gtk_toolbar_insert (GTK_TOOLBAR (window->presence_toolbar), item, -1);
1213 /* Set up the throbber */
1214 ebox = gtk_event_box_new ();
1215 gtk_event_box_set_visible_window (GTK_EVENT_BOX (ebox), FALSE);
1216 gtk_widget_set_tooltip_text (ebox, _("Show and edit accounts"));
1217 g_signal_connect (ebox,
1218 "button-press-event",
1219 G_CALLBACK (main_window_throbber_button_press_event_cb),
1221 gtk_widget_show (ebox);
1223 window->throbber = ephy_spinner_new ();
1224 ephy_spinner_set_size (EPHY_SPINNER (window->throbber), GTK_ICON_SIZE_LARGE_TOOLBAR);
1225 gtk_container_add (GTK_CONTAINER (ebox), window->throbber);
1226 gtk_widget_show (window->throbber);
1228 item = gtk_tool_item_new ();
1229 gtk_container_add (GTK_CONTAINER (item), ebox);
1230 gtk_toolbar_insert (GTK_TOOLBAR (window->presence_toolbar), item, -1);
1231 gtk_widget_show (GTK_WIDGET (item));
1233 list_iface = EMPATHY_CONTACT_LIST (empathy_contact_manager_dup_singleton ());
1234 monitor = empathy_contact_list_get_monitor (list_iface);
1235 window->list_store = empathy_contact_list_store_new (list_iface);
1236 window->list_view = empathy_contact_list_view_new (window->list_store,
1237 EMPATHY_CONTACT_LIST_FEATURE_ALL,
1238 EMPATHY_CONTACT_FEATURE_ALL);
1239 g_signal_connect (monitor, "contact-presence-changed",
1240 G_CALLBACK (main_window_contact_presence_changed_cb), window);
1241 g_object_unref (list_iface);
1243 gtk_widget_show (GTK_WIDGET (window->list_view));
1244 gtk_container_add (GTK_CONTAINER (sw),
1245 GTK_WIDGET (window->list_view));
1246 g_signal_connect (window->list_view, "row-activated",
1247 G_CALLBACK (main_window_row_activated_cb),
1250 /* Load user-defined accelerators. */
1251 main_window_accels_load ();
1253 /* Set window size. */
1254 empathy_geometry_load (GEOMETRY_NAME, &x, &y, &w, &h);
1256 if (w >= 1 && h >= 1) {
1257 /* Use the defaults from the ui file if we
1258 * don't have good w, h geometry.
1260 DEBUG ("Configuring window default size w:%d, h:%d", w, h);
1261 gtk_window_set_default_size (GTK_WINDOW (window->window), w, h);
1264 if (x >= 0 && y >= 0) {
1265 /* Let the window manager position it if we
1266 * don't have good x, y coordinates.
1268 DEBUG ("Configuring window default position x:%d, y:%d", x, y);
1269 gtk_window_move (GTK_WINDOW (window->window), x, y);
1272 /* Enable event handling */
1273 window->event_manager = empathy_event_manager_dup_singleton ();
1274 g_signal_connect (window->event_manager, "event-added",
1275 G_CALLBACK (main_window_event_added_cb),
1277 g_signal_connect (window->event_manager, "event-removed",
1278 G_CALLBACK (main_window_event_removed_cb),
1281 g_signal_connect (window->account_manager, "account-created",
1282 G_CALLBACK (main_window_account_created_or_deleted_cb),
1284 g_signal_connect (window->account_manager, "account-deleted",
1285 G_CALLBACK (main_window_account_created_or_deleted_cb),
1287 main_window_account_created_or_deleted_cb (window->account_manager, NULL, window);
1289 l = empathy_event_manager_get_events (window->event_manager);
1291 main_window_event_added_cb (window->event_manager,
1296 conf = empathy_conf_get ();
1298 /* Show offline ? */
1299 empathy_conf_get_bool (conf,
1300 EMPATHY_PREFS_CONTACTS_SHOW_OFFLINE,
1302 empathy_conf_notify_add (conf,
1303 EMPATHY_PREFS_CONTACTS_SHOW_OFFLINE,
1304 main_window_notify_show_offline_cb,
1305 show_offline_widget);
1307 gtk_toggle_action_set_active (show_offline_widget, show_offline);
1309 /* Show avatars ? */
1310 empathy_conf_get_bool (conf,
1311 EMPATHY_PREFS_UI_SHOW_AVATARS,
1313 empathy_conf_notify_add (conf,
1314 EMPATHY_PREFS_UI_SHOW_AVATARS,
1315 (EmpathyConfNotifyFunc) main_window_notify_show_avatars_cb,
1317 empathy_contact_list_store_set_show_avatars (window->list_store, show_avatars);
1320 empathy_conf_get_bool (conf,
1321 EMPATHY_PREFS_UI_COMPACT_CONTACT_LIST,
1322 &compact_contact_list);
1323 empathy_conf_notify_add (conf,
1324 EMPATHY_PREFS_UI_COMPACT_CONTACT_LIST,
1325 (EmpathyConfNotifyFunc) main_window_notify_compact_contact_list_cb,
1327 empathy_contact_list_store_set_is_compact (window->list_store, compact_contact_list);
1329 /* Sort criterium */
1330 empathy_conf_notify_add (conf,
1331 EMPATHY_PREFS_CONTACTS_SORT_CRITERIUM,
1332 (EmpathyConfNotifyFunc) main_window_notify_sort_criterium_cb,
1334 main_window_notify_sort_criterium_cb (conf,
1335 EMPATHY_PREFS_CONTACTS_SORT_CRITERIUM,
1338 main_window_update_status (window, window->account_manager);
1340 return window->window;