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 <libmissioncontrol/mission-control.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-dialog.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"
82 EmpathyContactListView *list_view;
83 EmpathyContactListStore *list_store;
85 EmpathyAccountManager *account_manager;
86 EmpathyChatroomManager *chatroom_manager;
87 EmpathyEventManager *event_manager;
88 guint flash_timeout_id;
94 GtkWidget *presence_toolbar;
95 GtkWidget *presence_chooser;
96 GtkWidget *errors_vbox;
98 GtkUIManager *ui_manager;
99 GtkAction *view_history;
100 GtkAction *room_join_favorites;
101 GtkWidget *room_menu;
102 GtkWidget *room_separator;
103 GtkWidget *edit_context;
104 GtkWidget *edit_context_separator;
106 guint size_timeout_id;
109 /* Actions that are enabled when there are connected accounts */
110 GList *actions_connected;
113 static EmpathyMainWindow *window = NULL;
116 main_window_flash_stop (EmpathyMainWindow *window)
118 if (window->flash_timeout_id == 0) {
122 DEBUG ("Stop flashing");
123 g_source_remove (window->flash_timeout_id);
124 window->flash_timeout_id = 0;
125 window->flash_on = FALSE;
134 main_window_flash_foreach (GtkTreeModel *model,
139 FlashForeachData *data = (FlashForeachData *) user_data;
140 EmpathyContact *contact;
141 const gchar *icon_name;
142 GtkTreePath *parent_path = NULL;
143 GtkTreeIter parent_iter;
145 /* To be used with gtk_tree_model_foreach, update the status icon
146 * of the contact to show the event icon (on=TRUE) or the presence
148 gtk_tree_model_get (model, iter,
149 EMPATHY_CONTACT_LIST_STORE_COL_CONTACT, &contact,
152 if (contact != data->event->contact) {
154 g_object_unref (contact);
160 icon_name = data->event->icon_name;
162 icon_name = empathy_icon_name_for_contact (contact);
165 gtk_tree_store_set (GTK_TREE_STORE (model), iter,
166 EMPATHY_CONTACT_LIST_STORE_COL_ICON_STATUS, icon_name,
169 /* To make sure the parent is shown correctly, we emit
170 * the row-changed signal on the parent so it prompts
171 * it to be refreshed by the filter func.
173 if (gtk_tree_model_iter_parent (model, &parent_iter, iter)) {
174 parent_path = gtk_tree_model_get_path (model, &parent_iter);
177 gtk_tree_model_row_changed (model, parent_path, &parent_iter);
178 gtk_tree_path_free (parent_path);
181 g_object_unref (contact);
187 main_window_flash_cb (EmpathyMainWindow *window)
191 gboolean found_event = FALSE;
192 FlashForeachData data;
194 window->flash_on = !window->flash_on;
195 data.on = window->flash_on;
196 model = GTK_TREE_MODEL (window->list_store);
198 events = empathy_event_manager_get_events (window->event_manager);
199 for (l = events; l; l = l->next) {
200 data.event = l->data;
201 if (!data.event->contact || !data.event->must_ack) {
206 gtk_tree_model_foreach (model,
207 main_window_flash_foreach,
212 main_window_flash_stop (window);
219 main_window_flash_start (EmpathyMainWindow *window)
221 if (window->flash_timeout_id != 0) {
225 DEBUG ("Start flashing");
226 window->flash_timeout_id = g_timeout_add (FLASH_TIMEOUT,
227 (GSourceFunc) main_window_flash_cb,
229 main_window_flash_cb (window);
233 main_window_event_added_cb (EmpathyEventManager *manager,
235 EmpathyMainWindow *window)
237 if (event->contact) {
238 main_window_flash_start (window);
243 main_window_event_removed_cb (EmpathyEventManager *manager,
245 EmpathyMainWindow *window)
247 FlashForeachData data;
249 if (!event->contact) {
255 gtk_tree_model_foreach (GTK_TREE_MODEL (window->list_store),
256 main_window_flash_foreach,
261 main_window_row_activated_cb (EmpathyContactListView *view,
263 GtkTreeViewColumn *col,
264 EmpathyMainWindow *window)
266 EmpathyContact *contact;
271 model = GTK_TREE_MODEL (window->list_store);
272 gtk_tree_model_get_iter (model, &iter, path);
273 gtk_tree_model_get (model, &iter,
274 EMPATHY_CONTACT_LIST_STORE_COL_CONTACT, &contact,
281 /* If the contact has an event activate it, otherwise the
282 * default handler of row-activated will be called. */
283 events = empathy_event_manager_get_events (window->event_manager);
284 for (l = events; l; l = l->next) {
285 EmpathyEvent *event = l->data;
287 if (event->contact == contact) {
288 DEBUG ("Activate event");
289 empathy_event_activate (event);
291 /* We don't want the default handler of this signal
292 * (e.g. open a chat) */
293 g_signal_stop_emission_by_name (view, "row-activated");
298 g_object_unref (contact);
302 main_window_error_edit_clicked_cb (GtkButton *button,
303 EmpathyMainWindow *window)
306 GtkWidget *error_widget;
308 account = g_object_get_data (G_OBJECT (button), "account");
309 empathy_accounts_dialog_show (GTK_WINDOW (window->window), account);
311 error_widget = g_hash_table_lookup (window->errors, account);
312 gtk_widget_destroy (error_widget);
313 g_hash_table_remove (window->errors, account);
317 main_window_error_clear_clicked_cb (GtkButton *button,
318 EmpathyMainWindow *window)
321 GtkWidget *error_widget;
323 account = g_object_get_data (G_OBJECT (button), "account");
324 error_widget = g_hash_table_lookup (window->errors, account);
325 gtk_widget_destroy (error_widget);
326 g_hash_table_remove (window->errors, account);
330 main_window_error_display (EmpathyMainWindow *window,
332 const gchar *message)
337 GtkWidget *button_edit;
338 GtkWidget *alignment;
343 GtkWidget *button_close;
346 child = g_hash_table_lookup (window->errors, account);
348 label = g_object_get_data (G_OBJECT (child), "label");
350 /* Just set the latest error and return */
351 str = g_markup_printf_escaped ("<b>%s</b>\n%s",
352 mc_account_get_display_name (account),
354 gtk_label_set_markup (GTK_LABEL (label), str);
360 child = gtk_vbox_new (FALSE, 0);
361 gtk_box_pack_start (GTK_BOX (window->errors_vbox), child, FALSE, TRUE, 0);
362 gtk_container_set_border_width (GTK_CONTAINER (child), 6);
363 gtk_widget_show (child);
365 table = gtk_table_new (2, 4, FALSE);
366 gtk_widget_show (table);
367 gtk_box_pack_start (GTK_BOX (child), table, TRUE, TRUE, 0);
368 gtk_table_set_row_spacings (GTK_TABLE (table), 12);
369 gtk_table_set_col_spacings (GTK_TABLE (table), 6);
371 image = gtk_image_new_from_stock (GTK_STOCK_DISCONNECT, GTK_ICON_SIZE_MENU);
372 gtk_widget_show (image);
373 gtk_table_attach (GTK_TABLE (table), image, 0, 1, 0, 2,
374 (GtkAttachOptions) (GTK_FILL),
375 (GtkAttachOptions) (GTK_FILL), 0, 0);
376 gtk_misc_set_alignment (GTK_MISC (image), 0.5, 0);
378 button_edit = gtk_button_new ();
379 gtk_widget_show (button_edit);
380 gtk_table_attach (GTK_TABLE (table), button_edit, 1, 2, 1, 2,
381 (GtkAttachOptions) (GTK_FILL),
382 (GtkAttachOptions) (0), 0, 0);
384 alignment = gtk_alignment_new (0.5, 0.5, 0, 0);
385 gtk_widget_show (alignment);
386 gtk_container_add (GTK_CONTAINER (button_edit), alignment);
388 hbox = gtk_hbox_new (FALSE, 2);
389 gtk_widget_show (hbox);
390 gtk_container_add (GTK_CONTAINER (alignment), hbox);
392 image = gtk_image_new_from_stock (GTK_STOCK_EDIT, GTK_ICON_SIZE_BUTTON);
393 gtk_widget_show (image);
394 gtk_box_pack_start (GTK_BOX (hbox), image, FALSE, FALSE, 0);
396 label = gtk_label_new_with_mnemonic (_("_Edit account"));
397 gtk_widget_show (label);
398 gtk_box_pack_start (GTK_BOX (hbox), label, FALSE, FALSE, 0);
400 fixed = gtk_fixed_new ();
401 gtk_widget_show (fixed);
402 gtk_table_attach (GTK_TABLE (table), fixed, 2, 3, 1, 2,
403 (GtkAttachOptions) (GTK_EXPAND | GTK_FILL),
404 (GtkAttachOptions) (GTK_FILL), 0, 0);
406 vbox = gtk_vbox_new (FALSE, 6);
407 gtk_widget_show (vbox);
408 gtk_table_attach (GTK_TABLE (table), vbox, 3, 4, 0, 2,
409 (GtkAttachOptions) (GTK_FILL),
410 (GtkAttachOptions) (GTK_FILL), 0, 0);
412 button_close = gtk_button_new ();
413 gtk_widget_show (button_close);
414 gtk_box_pack_start (GTK_BOX (vbox), button_close, FALSE, FALSE, 0);
415 gtk_button_set_relief (GTK_BUTTON (button_close), GTK_RELIEF_NONE);
418 image = gtk_image_new_from_stock ("gtk-close", GTK_ICON_SIZE_MENU);
419 gtk_widget_show (image);
420 gtk_container_add (GTK_CONTAINER (button_close), image);
422 label = gtk_label_new ("");
423 gtk_widget_show (label);
424 gtk_table_attach (GTK_TABLE (table), label, 1, 3, 0, 1,
425 (GtkAttachOptions) (GTK_EXPAND | GTK_SHRINK | GTK_FILL),
426 (GtkAttachOptions) (GTK_EXPAND | GTK_SHRINK | GTK_FILL), 0, 0);
427 gtk_widget_set_size_request (label, 175, -1);
428 gtk_label_set_use_markup (GTK_LABEL (label), TRUE);
429 gtk_label_set_line_wrap (GTK_LABEL (label), TRUE);
430 gtk_misc_set_alignment (GTK_MISC (label), 0, 0);
432 str = g_markup_printf_escaped ("<b>%s</b>\n%s",
433 mc_account_get_display_name (account),
435 gtk_label_set_markup (GTK_LABEL (label), str);
438 g_object_set_data (G_OBJECT (child), "label", label);
439 g_object_set_data_full (G_OBJECT (button_edit),
440 "account", g_object_ref (account),
442 g_object_set_data_full (G_OBJECT (button_close),
443 "account", g_object_ref (account),
446 g_signal_connect (button_edit, "clicked",
447 G_CALLBACK (main_window_error_edit_clicked_cb),
450 g_signal_connect (button_close, "clicked",
451 G_CALLBACK (main_window_error_clear_clicked_cb),
454 gtk_widget_show (window->errors_vbox);
456 g_hash_table_insert (window->errors, g_object_ref (account), child);
460 main_window_update_status (EmpathyMainWindow *window, EmpathyAccountManager *manager)
466 /* Count number of connected/connecting/disconnected accounts */
467 connected = empathy_account_manager_get_connected_accounts (manager);
468 connecting = empathy_account_manager_get_connecting_accounts (manager);
470 /* Update the spinner state */
471 if (connecting > 0) {
472 ephy_spinner_start (EPHY_SPINNER (window->throbber));
474 ephy_spinner_stop (EPHY_SPINNER (window->throbber));
477 /* Update widgets sensibility */
478 for (l = window->actions_connected; l; l = l->next) {
479 gtk_action_set_sensitive (l->data, (connected > 0));
484 main_window_connection_changed_cb (EmpathyAccountManager *manager,
486 TpConnectionStatusReason reason,
487 TpConnectionStatus current,
488 TpConnectionStatus previous,
489 EmpathyMainWindow *window)
491 main_window_update_status (window, manager);
493 if (current == TP_CONNECTION_STATUS_DISCONNECTED &&
494 reason != TP_CONNECTION_STATUS_REASON_REQUESTED) {
495 const gchar *message;
498 case TP_CONNECTION_STATUS_REASON_NONE_SPECIFIED:
499 message = _("No error specified");
501 case TP_CONNECTION_STATUS_REASON_NETWORK_ERROR:
502 message = _("Network error");
504 case TP_CONNECTION_STATUS_REASON_AUTHENTICATION_FAILED:
505 message = _("Authentication failed");
507 case TP_CONNECTION_STATUS_REASON_ENCRYPTION_ERROR:
508 message = _("Encryption error");
510 case TP_CONNECTION_STATUS_REASON_NAME_IN_USE:
511 message = _("Name in use");
513 case TP_CONNECTION_STATUS_REASON_CERT_NOT_PROVIDED:
514 message = _("Certificate not provided");
516 case TP_CONNECTION_STATUS_REASON_CERT_UNTRUSTED:
517 message = _("Certificate untrusted");
519 case TP_CONNECTION_STATUS_REASON_CERT_EXPIRED:
520 message = _("Certificate expired");
522 case TP_CONNECTION_STATUS_REASON_CERT_NOT_ACTIVATED:
523 message = _("Certificate not activated");
525 case TP_CONNECTION_STATUS_REASON_CERT_HOSTNAME_MISMATCH:
526 message = _("Certificate hostname mismatch");
528 case TP_CONNECTION_STATUS_REASON_CERT_FINGERPRINT_MISMATCH:
529 message = _("Certificate fingerprint mismatch");
531 case TP_CONNECTION_STATUS_REASON_CERT_SELF_SIGNED:
532 message = _("Certificate self-signed");
534 case TP_CONNECTION_STATUS_REASON_CERT_OTHER_ERROR:
535 message = _("Certificate error");
538 message = _("Unknown error");
542 main_window_error_display (window, account, message);
545 if (current == TP_CONNECTION_STATUS_DISCONNECTED) {
546 empathy_sound_play (GTK_WIDGET (window->window),
547 EMPATHY_SOUND_ACCOUNT_DISCONNECTED);
550 if (current == TP_CONNECTION_STATUS_CONNECTED) {
551 GtkWidget *error_widget;
553 empathy_sound_play (GTK_WIDGET (window->window),
554 EMPATHY_SOUND_ACCOUNT_CONNECTED);
556 /* Account connected without error, remove error message if any */
557 error_widget = g_hash_table_lookup (window->errors, account);
559 gtk_widget_destroy (error_widget);
560 g_hash_table_remove (window->errors, account);
566 main_window_contact_presence_changed_cb (EmpathyContactMonitor *monitor,
567 EmpathyContact *contact,
568 TpConnectionPresenceType current,
569 TpConnectionPresenceType previous,
570 EmpathyMainWindow *window)
573 gboolean should_play;
575 account = empathy_contact_get_account (contact);
576 should_play = !empathy_account_manager_is_account_just_connected (window->account_manager, account);
582 if (tp_connection_presence_type_cmp_availability (previous,
583 TP_CONNECTION_PRESENCE_TYPE_OFFLINE) > 0)
585 /* contact was online */
586 if (tp_connection_presence_type_cmp_availability (current,
587 TP_CONNECTION_PRESENCE_TYPE_OFFLINE) <= 0)
588 /* someone is logging off */
589 empathy_sound_play (GTK_WIDGET (window->window),
590 EMPATHY_SOUND_CONTACT_DISCONNECTED);
594 /* contact was offline */
595 if (tp_connection_presence_type_cmp_availability (current,
596 TP_CONNECTION_PRESENCE_TYPE_OFFLINE) > 0)
597 /* someone is logging in */
598 empathy_sound_play (GTK_WIDGET (window->window),
599 EMPATHY_SOUND_CONTACT_CONNECTED);
604 main_window_accels_load (void)
608 filename = g_build_filename (g_get_home_dir (), ".gnome2", PACKAGE_NAME, ACCELS_FILENAME, NULL);
609 if (g_file_test (filename, G_FILE_TEST_EXISTS)) {
610 DEBUG ("Loading from:'%s'", filename);
611 gtk_accel_map_load (filename);
618 main_window_accels_save (void)
621 gchar *file_with_path;
623 dir = g_build_filename (g_get_home_dir (), ".gnome2", PACKAGE_NAME, NULL);
624 g_mkdir_with_parents (dir, S_IRUSR | S_IWUSR | S_IXUSR);
625 file_with_path = g_build_filename (dir, ACCELS_FILENAME, NULL);
628 DEBUG ("Saving to:'%s'", file_with_path);
629 gtk_accel_map_save (file_with_path);
631 g_free (file_with_path);
635 main_window_destroy_cb (GtkWidget *widget,
636 EmpathyMainWindow *window)
638 /* Save user-defined accelerators. */
639 main_window_accels_save ();
641 g_signal_handlers_disconnect_by_func (window->account_manager,
642 main_window_connection_changed_cb,
645 if (window->size_timeout_id) {
646 g_source_remove (window->size_timeout_id);
649 g_list_free (window->actions_connected);
651 g_object_unref (window->mc);
652 g_object_unref (window->account_manager);
653 g_object_unref (window->list_store);
654 g_hash_table_destroy (window->errors);
656 g_signal_handlers_disconnect_by_func (window->event_manager,
657 main_window_event_added_cb,
659 g_signal_handlers_disconnect_by_func (window->event_manager,
660 main_window_event_removed_cb,
662 g_object_unref (window->event_manager);
663 g_object_unref (window->ui_manager);
669 main_window_chat_quit_cb (GtkAction *action,
670 EmpathyMainWindow *window)
676 main_window_view_history_cb (GtkAction *action,
677 EmpathyMainWindow *window)
679 empathy_log_window_show (NULL, NULL, FALSE, GTK_WINDOW (window->window));
683 main_window_chat_new_message_cb (GtkAction *action,
684 EmpathyMainWindow *window)
686 empathy_new_message_dialog_show (GTK_WINDOW (window->window));
690 main_window_chat_add_contact_cb (GtkAction *action,
691 EmpathyMainWindow *window)
693 empathy_new_contact_dialog_show (GTK_WINDOW (window->window));
697 main_window_view_show_ft_manager (GtkAction *action,
698 EmpathyMainWindow *window)
700 empathy_ft_manager_show ();
704 main_window_view_show_offline_cb (GtkToggleAction *action,
705 EmpathyMainWindow *window)
709 current = gtk_toggle_action_get_active (action);
710 empathy_conf_set_bool (empathy_conf_get (),
711 EMPATHY_PREFS_CONTACTS_SHOW_OFFLINE,
714 /* Turn off sound just while we alter the contact list. */
715 // FIXME: empathy_sound_set_enabled (FALSE);
716 empathy_contact_list_store_set_show_offline (window->list_store, current);
717 //empathy_sound_set_enabled (TRUE);
721 main_window_view_show_map_cb (GtkCheckMenuItem *item,
722 EmpathyMainWindow *window)
724 #if HAVE_LIBCHAMPLAIN
725 empathy_map_view_show ();
730 main_window_favorite_chatroom_join (EmpathyChatroom *chatroom)
732 EmpathyAccountManager *manager;
734 TpConnection *connection;
737 manager = empathy_account_manager_dup_singleton ();
738 account = empathy_chatroom_get_account (chatroom);
739 connection = empathy_account_manager_get_connection (manager, account);
740 room = empathy_chatroom_get_room (chatroom);
741 g_object_unref (manager);
743 if (connection != NULL) {
744 DEBUG ("Requesting channel for '%s'", room);
745 empathy_dispatcher_join_muc (connection, room, NULL, NULL);
750 main_window_favorite_chatroom_menu_activate_cb (GtkMenuItem *menu_item,
751 EmpathyChatroom *chatroom)
753 main_window_favorite_chatroom_join (chatroom);
757 main_window_favorite_chatroom_menu_add (EmpathyMainWindow *window,
758 EmpathyChatroom *chatroom)
760 GtkWidget *menu_item;
763 if (g_object_get_data (G_OBJECT (chatroom), "menu_item")) {
767 name = empathy_chatroom_get_name (chatroom);
768 menu_item = gtk_menu_item_new_with_label (name);
770 g_object_set_data (G_OBJECT (chatroom), "menu_item", menu_item);
771 g_signal_connect (menu_item, "activate",
772 G_CALLBACK (main_window_favorite_chatroom_menu_activate_cb),
775 gtk_menu_shell_insert (GTK_MENU_SHELL (window->room_menu),
778 gtk_widget_show (menu_item);
782 main_window_favorite_chatroom_menu_added_cb (EmpathyChatroomManager *manager,
783 EmpathyChatroom *chatroom,
784 EmpathyMainWindow *window)
786 main_window_favorite_chatroom_menu_add (window, chatroom);
787 gtk_widget_show (window->room_separator);
788 gtk_action_set_sensitive (window->room_join_favorites, TRUE);
792 main_window_favorite_chatroom_menu_removed_cb (EmpathyChatroomManager *manager,
793 EmpathyChatroom *chatroom,
794 EmpathyMainWindow *window)
796 GtkWidget *menu_item;
799 menu_item = g_object_get_data (G_OBJECT (chatroom), "menu_item");
800 g_object_set_data (G_OBJECT (chatroom), "menu_item", NULL);
801 gtk_widget_destroy (menu_item);
803 chatrooms = empathy_chatroom_manager_get_chatrooms (window->chatroom_manager, NULL);
805 gtk_widget_show (window->room_separator);
807 gtk_widget_hide (window->room_separator);
810 gtk_action_set_sensitive (window->room_join_favorites, chatrooms != NULL);
811 g_list_free (chatrooms);
815 main_window_favorite_chatroom_menu_setup (EmpathyMainWindow *window)
817 GList *chatrooms, *l;
820 window->chatroom_manager = empathy_chatroom_manager_dup_singleton (NULL);
821 chatrooms = empathy_chatroom_manager_get_chatrooms (window->chatroom_manager, NULL);
822 room = gtk_ui_manager_get_widget (window->ui_manager,
824 window->room_menu = gtk_menu_item_get_submenu (GTK_MENU_ITEM (room));
825 window->room_separator = gtk_ui_manager_get_widget (window->ui_manager,
826 "/menubar/room/room_separator");
828 for (l = chatrooms; l; l = l->next) {
829 main_window_favorite_chatroom_menu_add (window, l->data);
833 gtk_widget_hide (window->room_separator);
836 gtk_action_set_sensitive (window->room_join_favorites, chatrooms != NULL);
838 g_signal_connect (window->chatroom_manager, "chatroom-added",
839 G_CALLBACK (main_window_favorite_chatroom_menu_added_cb),
841 g_signal_connect (window->chatroom_manager, "chatroom-removed",
842 G_CALLBACK (main_window_favorite_chatroom_menu_removed_cb),
845 g_list_free (chatrooms);
849 main_window_room_join_new_cb (GtkAction *action,
850 EmpathyMainWindow *window)
852 empathy_new_chatroom_dialog_show (GTK_WINDOW (window->window));
856 main_window_room_join_favorites_cb (GtkAction *action,
857 EmpathyMainWindow *window)
859 GList *chatrooms, *l;
861 chatrooms = empathy_chatroom_manager_get_chatrooms (window->chatroom_manager, NULL);
862 for (l = chatrooms; l; l = l->next) {
863 main_window_favorite_chatroom_join (l->data);
865 g_list_free (chatrooms);
869 main_window_room_manage_favorites_cb (GtkAction *action,
870 EmpathyMainWindow *window)
872 empathy_chatrooms_window_show (GTK_WINDOW (window->window));
876 main_window_edit_cb (GtkAction *action,
877 EmpathyMainWindow *window)
881 /* FIXME: It should use the UIManager to merge the contact/group submenu */
882 submenu = empathy_contact_list_view_get_contact_menu (window->list_view);
887 item = GTK_MENU_ITEM (window->edit_context);
888 label = gtk_bin_get_child (GTK_BIN (item));
889 gtk_label_set_text (GTK_LABEL (label), _("Contact"));
891 gtk_widget_show (window->edit_context);
892 gtk_widget_show (window->edit_context_separator);
894 gtk_menu_item_set_submenu (item, submenu);
899 submenu = empathy_contact_list_view_get_group_menu (window->list_view);
904 item = GTK_MENU_ITEM (window->edit_context);
905 label = gtk_bin_get_child (GTK_BIN (item));
906 gtk_label_set_text (GTK_LABEL (label), _("Group"));
908 gtk_widget_show (window->edit_context);
909 gtk_widget_show (window->edit_context_separator);
911 gtk_menu_item_set_submenu (item, submenu);
916 gtk_widget_hide (window->edit_context);
917 gtk_widget_hide (window->edit_context_separator);
923 main_window_edit_accounts_cb (GtkAction *action,
924 EmpathyMainWindow *window)
926 empathy_accounts_dialog_show (GTK_WINDOW (window->window), NULL);
930 main_window_edit_personal_information_cb (GtkAction *action,
931 EmpathyMainWindow *window)
933 empathy_contact_personal_dialog_show (GTK_WINDOW (window->window));
937 main_window_edit_preferences_cb (GtkAction *action,
938 EmpathyMainWindow *window)
940 empathy_preferences_show (GTK_WINDOW (window->window));
944 main_window_help_about_cb (GtkAction *action,
945 EmpathyMainWindow *window)
947 empathy_about_dialog_new (GTK_WINDOW (window->window));
951 main_window_help_debug_cb (GtkAction *action,
952 EmpathyMainWindow *window)
954 empathy_debug_dialog_new (GTK_WINDOW (window->window));
958 main_window_help_contents_cb (GtkAction *action,
959 EmpathyMainWindow *window)
961 empathy_url_show (window->window, "ghelp:empathy");
965 main_window_throbber_button_press_event_cb (GtkWidget *throbber_ebox,
966 GdkEventButton *event,
967 EmpathyMainWindow *window)
969 if (event->type != GDK_BUTTON_PRESS ||
970 event->button != 1) {
974 empathy_accounts_dialog_show (GTK_WINDOW (window->window), NULL);
980 main_window_configure_event_timeout_cb (EmpathyMainWindow *window)
984 gtk_window_get_size (GTK_WINDOW (window->window), &w, &h);
985 gtk_window_get_position (GTK_WINDOW (window->window), &x, &y);
987 empathy_geometry_save (GEOMETRY_NAME, x, y, w, h);
989 window->size_timeout_id = 0;
995 main_window_configure_event_cb (GtkWidget *widget,
996 GdkEventConfigure *event,
997 EmpathyMainWindow *window)
999 if (window->size_timeout_id) {
1000 g_source_remove (window->size_timeout_id);
1003 window->size_timeout_id = g_timeout_add_seconds (1,
1004 (GSourceFunc) main_window_configure_event_timeout_cb,
1011 main_window_account_created_or_deleted_cb (EmpathyAccountManager *manager,
1013 EmpathyMainWindow *window)
1015 gtk_action_set_sensitive (window->view_history,
1016 empathy_account_manager_get_count (manager) > 0);
1020 main_window_notify_show_offline_cb (EmpathyConf *conf,
1022 gpointer toggle_action)
1024 gboolean show_offline;
1026 if (empathy_conf_get_bool (conf, key, &show_offline)) {
1027 gtk_toggle_action_set_active (toggle_action, show_offline);
1032 main_window_notify_show_avatars_cb (EmpathyConf *conf,
1034 EmpathyMainWindow *window)
1036 gboolean show_avatars;
1038 if (empathy_conf_get_bool (conf, key, &show_avatars)) {
1039 empathy_contact_list_store_set_show_avatars (window->list_store,
1045 main_window_notify_compact_contact_list_cb (EmpathyConf *conf,
1047 EmpathyMainWindow *window)
1049 gboolean compact_contact_list;
1051 if (empathy_conf_get_bool (conf, key, &compact_contact_list)) {
1052 empathy_contact_list_store_set_is_compact (window->list_store,
1053 compact_contact_list);
1058 main_window_notify_sort_criterium_cb (EmpathyConf *conf,
1060 EmpathyMainWindow *window)
1064 if (empathy_conf_get_string (conf, key, &str) && str) {
1066 GEnumClass *enum_class;
1067 GEnumValue *enum_value;
1069 type = empathy_contact_list_store_sort_get_type ();
1070 enum_class = G_ENUM_CLASS (g_type_class_peek (type));
1071 enum_value = g_enum_get_value_by_nick (enum_class, str);
1075 empathy_contact_list_store_set_sort_criterium (window->list_store,
1082 main_window_connection_items_setup (EmpathyMainWindow *window,
1088 const gchar *actions_connected[] = {
1092 "edit_personal_information"
1095 for (i = 0, list = NULL; i < G_N_ELEMENTS (actions_connected); i++) {
1096 action = gtk_builder_get_object (gui, actions_connected[i]);
1097 list = g_list_prepend (list, action);
1100 window->actions_connected = list;
1104 empathy_main_window_get (void)
1106 return window != NULL ? window->window : NULL;
1110 empathy_main_window_show (void)
1112 EmpathyContactList *list_iface;
1113 EmpathyContactMonitor *monitor;
1117 GtkToggleAction *show_offline_widget;
1119 GtkAction *show_map_widget;
1121 gboolean show_offline;
1122 gboolean show_avatars;
1123 gboolean compact_contact_list;
1129 empathy_window_present (GTK_WINDOW (window->window), TRUE);
1130 return window->window;
1133 window = g_new0 (EmpathyMainWindow, 1);
1135 /* Set up interface */
1136 filename = empathy_file_lookup ("empathy-main-window.ui", "src");
1137 gui = empathy_builder_get_file (filename,
1138 "main_window", &window->window,
1139 "main_vbox", &window->main_vbox,
1140 "errors_vbox", &window->errors_vbox,
1141 "ui_manager", &window->ui_manager,
1142 "view_show_offline", &show_offline_widget,
1143 "view_history", &window->view_history,
1144 "view_show_map", &show_map_widget,
1145 "room_join_favorites", &window->room_join_favorites,
1146 "presence_toolbar", &window->presence_toolbar,
1147 "roster_scrolledwindow", &sw,
1151 empathy_builder_connect (gui, window,
1152 "main_window", "destroy", main_window_destroy_cb,
1153 "main_window", "configure_event", main_window_configure_event_cb,
1154 "chat_quit", "activate", main_window_chat_quit_cb,
1155 "chat_new_message", "activate", main_window_chat_new_message_cb,
1156 "view_history", "activate", main_window_view_history_cb,
1157 "room_join_new", "activate", main_window_room_join_new_cb,
1158 "room_join_favorites", "activate", main_window_room_join_favorites_cb,
1159 "room_manage_favorites", "activate", main_window_room_manage_favorites_cb,
1160 "chat_add_contact", "activate", main_window_chat_add_contact_cb,
1161 "view_show_ft_manager", "activate", main_window_view_show_ft_manager,
1162 "view_show_offline", "toggled", main_window_view_show_offline_cb,
1163 "view_show_map", "activate", main_window_view_show_map_cb,
1164 "edit", "activate", main_window_edit_cb,
1165 "edit_accounts", "activate", main_window_edit_accounts_cb,
1166 "edit_personal_information", "activate", main_window_edit_personal_information_cb,
1167 "edit_preferences", "activate", main_window_edit_preferences_cb,
1168 "help_about", "activate", main_window_help_about_cb,
1169 "help_debug", "activate", main_window_help_debug_cb,
1170 "help_contents", "activate", main_window_help_contents_cb,
1173 /* Set up connection related widgets. */
1174 main_window_connection_items_setup (window, gui);
1176 g_object_ref (window->ui_manager);
1177 g_object_unref (gui);
1179 #if !HAVE_LIBCHAMPLAIN
1180 gtk_action_set_visible (show_map_widget, FALSE);
1183 window->mc = empathy_mission_control_dup_singleton ();
1184 window->account_manager = empathy_account_manager_dup_singleton ();
1186 g_signal_connect (window->account_manager,
1187 "account-connection-changed",
1188 G_CALLBACK (main_window_connection_changed_cb), window);
1190 window->errors = g_hash_table_new_full (empathy_account_hash,
1191 empathy_account_equal,
1196 main_window_favorite_chatroom_menu_setup (window);
1198 window->edit_context = gtk_ui_manager_get_widget (window->ui_manager,
1199 "/menubar/edit/edit_context");
1200 window->edit_context_separator = gtk_ui_manager_get_widget (window->ui_manager,
1201 "/menubar/edit/edit_context_separator");
1202 gtk_widget_hide (window->edit_context);
1203 gtk_widget_hide (window->edit_context_separator);
1205 /* Set up contact list. */
1206 empathy_status_presets_get_all ();
1208 /* Set up presence chooser */
1209 window->presence_chooser = empathy_presence_chooser_new ();
1210 gtk_widget_show (window->presence_chooser);
1211 item = gtk_tool_item_new ();
1212 gtk_widget_show (GTK_WIDGET (item));
1213 gtk_container_add (GTK_CONTAINER (item), window->presence_chooser);
1214 gtk_tool_item_set_is_important (item, TRUE);
1215 gtk_tool_item_set_expand (item, TRUE);
1216 gtk_toolbar_insert (GTK_TOOLBAR (window->presence_toolbar), item, -1);
1218 /* Set up the throbber */
1219 ebox = gtk_event_box_new ();
1220 gtk_event_box_set_visible_window (GTK_EVENT_BOX (ebox), FALSE);
1221 gtk_widget_set_tooltip_text (ebox, _("Show and edit accounts"));
1222 g_signal_connect (ebox,
1223 "button-press-event",
1224 G_CALLBACK (main_window_throbber_button_press_event_cb),
1226 gtk_widget_show (ebox);
1228 window->throbber = ephy_spinner_new ();
1229 ephy_spinner_set_size (EPHY_SPINNER (window->throbber), GTK_ICON_SIZE_LARGE_TOOLBAR);
1230 gtk_container_add (GTK_CONTAINER (ebox), window->throbber);
1231 gtk_widget_show (window->throbber);
1233 item = gtk_tool_item_new ();
1234 gtk_container_add (GTK_CONTAINER (item), ebox);
1235 gtk_toolbar_insert (GTK_TOOLBAR (window->presence_toolbar), item, -1);
1236 gtk_widget_show (GTK_WIDGET (item));
1238 list_iface = EMPATHY_CONTACT_LIST (empathy_contact_manager_dup_singleton ());
1239 monitor = empathy_contact_list_get_monitor (list_iface);
1240 window->list_store = empathy_contact_list_store_new (list_iface);
1241 window->list_view = empathy_contact_list_view_new (window->list_store,
1242 EMPATHY_CONTACT_LIST_FEATURE_ALL,
1243 EMPATHY_CONTACT_FEATURE_ALL);
1244 g_signal_connect (monitor, "contact-presence-changed",
1245 G_CALLBACK (main_window_contact_presence_changed_cb), window);
1246 g_object_unref (list_iface);
1248 gtk_widget_show (GTK_WIDGET (window->list_view));
1249 gtk_container_add (GTK_CONTAINER (sw),
1250 GTK_WIDGET (window->list_view));
1251 g_signal_connect (window->list_view, "row-activated",
1252 G_CALLBACK (main_window_row_activated_cb),
1255 /* Load user-defined accelerators. */
1256 main_window_accels_load ();
1258 /* Set window size. */
1259 empathy_geometry_load (GEOMETRY_NAME, &x, &y, &w, &h);
1261 if (w >= 1 && h >= 1) {
1262 /* Use the defaults from the ui file if we
1263 * don't have good w, h geometry.
1265 DEBUG ("Configuring window default size w:%d, h:%d", w, h);
1266 gtk_window_set_default_size (GTK_WINDOW (window->window), w, h);
1269 if (x >= 0 && y >= 0) {
1270 /* Let the window manager position it if we
1271 * don't have good x, y coordinates.
1273 DEBUG ("Configuring window default position x:%d, y:%d", x, y);
1274 gtk_window_move (GTK_WINDOW (window->window), x, y);
1277 /* Enable event handling */
1278 window->event_manager = empathy_event_manager_dup_singleton ();
1279 g_signal_connect (window->event_manager, "event-added",
1280 G_CALLBACK (main_window_event_added_cb),
1282 g_signal_connect (window->event_manager, "event-removed",
1283 G_CALLBACK (main_window_event_removed_cb),
1286 g_signal_connect (window->account_manager, "account-created",
1287 G_CALLBACK (main_window_account_created_or_deleted_cb),
1289 g_signal_connect (window->account_manager, "account-deleted",
1290 G_CALLBACK (main_window_account_created_or_deleted_cb),
1292 main_window_account_created_or_deleted_cb (window->account_manager, NULL, window);
1294 l = empathy_event_manager_get_events (window->event_manager);
1296 main_window_event_added_cb (window->event_manager,
1301 conf = empathy_conf_get ();
1303 /* Show offline ? */
1304 empathy_conf_get_bool (conf,
1305 EMPATHY_PREFS_CONTACTS_SHOW_OFFLINE,
1307 empathy_conf_notify_add (conf,
1308 EMPATHY_PREFS_CONTACTS_SHOW_OFFLINE,
1309 main_window_notify_show_offline_cb,
1310 show_offline_widget);
1312 gtk_toggle_action_set_active (show_offline_widget, show_offline);
1314 /* Show avatars ? */
1315 empathy_conf_get_bool (conf,
1316 EMPATHY_PREFS_UI_SHOW_AVATARS,
1318 empathy_conf_notify_add (conf,
1319 EMPATHY_PREFS_UI_SHOW_AVATARS,
1320 (EmpathyConfNotifyFunc) main_window_notify_show_avatars_cb,
1322 empathy_contact_list_store_set_show_avatars (window->list_store, show_avatars);
1325 empathy_conf_get_bool (conf,
1326 EMPATHY_PREFS_UI_COMPACT_CONTACT_LIST,
1327 &compact_contact_list);
1328 empathy_conf_notify_add (conf,
1329 EMPATHY_PREFS_UI_COMPACT_CONTACT_LIST,
1330 (EmpathyConfNotifyFunc) main_window_notify_compact_contact_list_cb,
1332 empathy_contact_list_store_set_is_compact (window->list_store, compact_contact_list);
1334 /* Sort criterium */
1335 empathy_conf_notify_add (conf,
1336 EMPATHY_PREFS_CONTACTS_SORT_CRITERIUM,
1337 (EmpathyConfNotifyFunc) main_window_notify_sort_criterium_cb,
1339 main_window_notify_sort_criterium_cb (conf,
1340 EMPATHY_PREFS_CONTACTS_SORT_CRITERIUM,
1343 main_window_update_status (window, window->account_manager);
1345 return window->window;