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., 59 Temple Place - Suite 330,
19 * Boston, MA 02111-1307, 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-contact-factory.h>
39 #include <libempathy/empathy-status-presets.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-presence-chooser.h>
45 #include <libempathy-gtk/empathy-ui-utils.h>
46 #include <libempathy-gtk/empathy-geometry.h>
47 #include <libempathy-gtk/empathy-conf.h>
48 #include <libempathy-gtk/empathy-log-window.h>
49 #include <libempathy-gtk/empathy-new-message-dialog.h>
50 #include <libempathy-gtk/empathy-gtk-enum-types.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-new-chatroom-dialog.h"
60 #include "empathy-chatrooms-window.h"
61 #include "empathy-event-manager.h"
63 #define DEBUG_FLAG EMPATHY_DEBUG_OTHER
64 #include <libempathy/empathy-debug.h>
66 /* Flashing delay for icons (milliseconds). */
67 #define FLASH_TIMEOUT 500
69 /* Minimum width of roster window if something goes wrong. */
72 /* Accels (menu shortcuts) can be configured and saved */
73 #define ACCELS_FILENAME "accels.txt"
75 /* Name in the geometry file */
76 #define GEOMETRY_NAME "main-window"
79 EmpathyContactListView *list_view;
80 EmpathyContactListStore *list_store;
82 EmpathyAccountManager *account_manager;
83 EmpathyChatroomManager *chatroom_manager;
84 EmpathyEventManager *event_manager;
85 guint flash_timeout_id;
91 GtkWidget *presence_toolbar;
92 GtkWidget *presence_chooser;
93 GtkWidget *errors_vbox;
95 GtkUIManager *ui_manager;
96 GtkAction *chat_history;
97 GtkAction *room_join_favorites;
99 GtkWidget *room_separator;
100 GtkWidget *edit_context;
101 GtkWidget *edit_context_separator;
103 guint size_timeout_id;
106 /* Actions that are enabled when there are connected accounts */
107 GList *actions_connected;
110 static EmpathyMainWindow *window = NULL;
113 main_window_flash_stop (EmpathyMainWindow *window)
115 if (window->flash_timeout_id == 0) {
119 DEBUG ("Stop flashing");
120 g_source_remove (window->flash_timeout_id);
121 window->flash_timeout_id = 0;
122 window->flash_on = FALSE;
131 main_window_flash_foreach (GtkTreeModel *model,
136 FlashForeachData *data = (FlashForeachData*) user_data;
137 EmpathyContact *contact;
138 const gchar *icon_name;
139 GtkTreePath *parent_path = NULL;
140 GtkTreeIter parent_iter;
142 /* To be used with gtk_tree_model_foreach, update the status icon
143 * of the contact to show the event icon (on=TRUE) or the presence
145 gtk_tree_model_get (model, iter,
146 EMPATHY_CONTACT_LIST_STORE_COL_CONTACT, &contact,
149 if (contact != data->event->contact) {
151 g_object_unref (contact);
157 icon_name = data->event->icon_name;
159 icon_name = empathy_icon_name_for_contact (contact);
162 gtk_tree_store_set (GTK_TREE_STORE (model), iter,
163 EMPATHY_CONTACT_LIST_STORE_COL_ICON_STATUS, icon_name,
166 /* To make sure the parent is shown correctly, we emit
167 * the row-changed signal on the parent so it prompts
168 * it to be refreshed by the filter func.
170 if (gtk_tree_model_iter_parent (model, &parent_iter, iter)) {
171 parent_path = gtk_tree_model_get_path (model, &parent_iter);
174 gtk_tree_model_row_changed (model, parent_path, &parent_iter);
175 gtk_tree_path_free (parent_path);
178 g_object_unref (contact);
184 main_window_flash_cb (EmpathyMainWindow *window)
188 gboolean found_event = FALSE;
189 FlashForeachData data;
191 window->flash_on = !window->flash_on;
192 data.on = window->flash_on;
193 model = GTK_TREE_MODEL (window->list_store);
195 events = empathy_event_manager_get_events (window->event_manager);
196 for (l = events; l; l = l->next) {
197 data.event = l->data;
198 if (!data.event->contact) {
203 gtk_tree_model_foreach (model,
204 main_window_flash_foreach,
209 main_window_flash_stop (window);
216 main_window_flash_start (EmpathyMainWindow *window)
218 if (window->flash_timeout_id != 0) {
222 DEBUG ("Start flashing");
223 window->flash_timeout_id = g_timeout_add (FLASH_TIMEOUT,
224 (GSourceFunc) main_window_flash_cb,
226 main_window_flash_cb (window);
230 main_window_event_added_cb (EmpathyEventManager *manager,
232 EmpathyMainWindow *window)
234 if (event->contact) {
235 main_window_flash_start (window);
240 main_window_event_removed_cb (EmpathyEventManager *manager,
242 EmpathyMainWindow *window)
244 FlashForeachData data;
246 if (!event->contact) {
252 gtk_tree_model_foreach (GTK_TREE_MODEL (window->list_store),
253 main_window_flash_foreach,
258 main_window_row_activated_cb (EmpathyContactListView *view,
260 GtkTreeViewColumn *col,
261 EmpathyMainWindow *window)
263 EmpathyContact *contact;
268 model = GTK_TREE_MODEL (window->list_store);
269 gtk_tree_model_get_iter (model, &iter, path);
270 gtk_tree_model_get (model, &iter,
271 EMPATHY_CONTACT_LIST_STORE_COL_CONTACT, &contact,
278 /* If the contact has an event activate it, otherwise the
279 * default handler of row-activated will be called. */
280 events = empathy_event_manager_get_events (window->event_manager);
281 for (l = events; l; l = l->next) {
282 EmpathyEvent *event = l->data;
284 if (event->contact == contact) {
285 DEBUG ("Activate event");
286 empathy_event_activate (event);
288 /* We don't want the default handler of this signal
289 * (e.g. open a chat) */
290 g_signal_stop_emission_by_name (view, "row-activated");
295 g_object_unref (contact);
299 main_window_error_edit_clicked_cb (GtkButton *button,
300 EmpathyMainWindow *window)
303 GtkWidget *error_widget;
305 account = g_object_get_data (G_OBJECT (button), "account");
306 empathy_accounts_dialog_show (GTK_WINDOW (window->window), account);
308 error_widget = g_hash_table_lookup (window->errors, account);
309 gtk_widget_destroy (error_widget);
310 g_hash_table_remove (window->errors, account);
314 main_window_error_clear_clicked_cb (GtkButton *button,
315 EmpathyMainWindow *window)
318 GtkWidget *error_widget;
320 account = g_object_get_data (G_OBJECT (button), "account");
321 error_widget = g_hash_table_lookup (window->errors, account);
322 gtk_widget_destroy (error_widget);
323 g_hash_table_remove (window->errors, account);
327 main_window_error_display (EmpathyMainWindow *window,
329 const gchar *message)
334 GtkWidget *button_edit;
335 GtkWidget *alignment;
340 GtkWidget *button_close;
343 child = g_hash_table_lookup (window->errors, account);
345 label = g_object_get_data (G_OBJECT (child), "label");
347 /* Just set the latest error and return */
348 str = g_markup_printf_escaped ("<b>%s</b>\n%s",
349 mc_account_get_display_name (account),
351 gtk_label_set_markup (GTK_LABEL (label), str);
357 child = gtk_vbox_new (FALSE, 0);
358 gtk_box_pack_start (GTK_BOX (window->errors_vbox), child, FALSE, TRUE, 0);
359 gtk_container_set_border_width (GTK_CONTAINER (child), 6);
360 gtk_widget_show (child);
362 table = gtk_table_new (2, 4, FALSE);
363 gtk_widget_show (table);
364 gtk_box_pack_start (GTK_BOX (child), table, TRUE, TRUE, 0);
365 gtk_table_set_row_spacings (GTK_TABLE (table), 12);
366 gtk_table_set_col_spacings (GTK_TABLE (table), 6);
368 image = gtk_image_new_from_stock (GTK_STOCK_DISCONNECT, GTK_ICON_SIZE_MENU);
369 gtk_widget_show (image);
370 gtk_table_attach (GTK_TABLE (table), image, 0, 1, 0, 2,
371 (GtkAttachOptions) (GTK_FILL),
372 (GtkAttachOptions) (GTK_FILL), 0, 0);
373 gtk_misc_set_alignment (GTK_MISC (image), 0.5, 0);
375 button_edit = gtk_button_new ();
376 gtk_widget_show (button_edit);
377 gtk_table_attach (GTK_TABLE (table), button_edit, 1, 2, 1, 2,
378 (GtkAttachOptions) (GTK_FILL),
379 (GtkAttachOptions) (0), 0, 0);
381 alignment = gtk_alignment_new (0.5, 0.5, 0, 0);
382 gtk_widget_show (alignment);
383 gtk_container_add (GTK_CONTAINER (button_edit), alignment);
385 hbox = gtk_hbox_new (FALSE, 2);
386 gtk_widget_show (hbox);
387 gtk_container_add (GTK_CONTAINER (alignment), hbox);
389 image = gtk_image_new_from_stock (GTK_STOCK_EDIT, GTK_ICON_SIZE_BUTTON);
390 gtk_widget_show (image);
391 gtk_box_pack_start (GTK_BOX (hbox), image, FALSE, FALSE, 0);
393 label = gtk_label_new_with_mnemonic (_("_Edit account"));
394 gtk_widget_show (label);
395 gtk_box_pack_start (GTK_BOX (hbox), label, FALSE, FALSE, 0);
397 fixed = gtk_fixed_new ();
398 gtk_widget_show (fixed);
399 gtk_table_attach (GTK_TABLE (table), fixed, 2, 3, 1, 2,
400 (GtkAttachOptions) (GTK_EXPAND | GTK_FILL),
401 (GtkAttachOptions) (GTK_FILL), 0, 0);
403 vbox = gtk_vbox_new (FALSE, 6);
404 gtk_widget_show (vbox);
405 gtk_table_attach (GTK_TABLE (table), vbox, 3, 4, 0, 2,
406 (GtkAttachOptions) (GTK_FILL),
407 (GtkAttachOptions) (GTK_FILL), 0, 0);
409 button_close = gtk_button_new ();
410 gtk_widget_show (button_close);
411 gtk_box_pack_start (GTK_BOX (vbox), button_close, FALSE, FALSE, 0);
412 gtk_button_set_relief (GTK_BUTTON (button_close), GTK_RELIEF_NONE);
415 image = gtk_image_new_from_stock ("gtk-close", GTK_ICON_SIZE_MENU);
416 gtk_widget_show (image);
417 gtk_container_add (GTK_CONTAINER (button_close), image);
419 label = gtk_label_new ("");
420 gtk_widget_show (label);
421 gtk_table_attach (GTK_TABLE (table), label, 1, 3, 0, 1,
422 (GtkAttachOptions) (GTK_EXPAND | GTK_SHRINK | GTK_FILL),
423 (GtkAttachOptions) (GTK_EXPAND | GTK_SHRINK | GTK_FILL), 0, 0);
424 gtk_widget_set_size_request (label, 175, -1);
425 gtk_label_set_use_markup (GTK_LABEL (label), TRUE);
426 gtk_label_set_line_wrap (GTK_LABEL (label), TRUE);
427 gtk_misc_set_alignment (GTK_MISC (label), 0, 0);
429 str = g_markup_printf_escaped ("<b>%s</b>\n%s",
430 mc_account_get_display_name (account),
432 gtk_label_set_markup (GTK_LABEL (label), str);
435 g_object_set_data (G_OBJECT (child), "label", label);
436 g_object_set_data_full (G_OBJECT (button_edit),
437 "account", g_object_ref (account),
439 g_object_set_data_full (G_OBJECT (button_close),
440 "account", g_object_ref (account),
443 g_signal_connect (button_edit, "clicked",
444 G_CALLBACK (main_window_error_edit_clicked_cb),
447 g_signal_connect (button_close, "clicked",
448 G_CALLBACK (main_window_error_clear_clicked_cb),
451 gtk_widget_show (window->errors_vbox);
453 g_hash_table_insert (window->errors, g_object_ref (account), child);
457 main_window_update_status (EmpathyMainWindow *window, EmpathyAccountManager *manager)
463 /* Count number of connected/connecting/disconnected accounts */
464 connected = empathy_account_manager_get_connected_accounts (manager);
465 connecting = empathy_account_manager_get_connecting_accounts (manager);
467 /* Update the spinner state */
468 if (connecting > 0) {
469 ephy_spinner_start (EPHY_SPINNER (window->throbber));
471 ephy_spinner_stop (EPHY_SPINNER (window->throbber));
474 /* Update widgets sensibility */
475 for (l = window->actions_connected; l; l = l->next) {
476 gtk_action_set_sensitive (l->data, (connected > 0));
481 main_window_connection_changed_cb (EmpathyAccountManager *manager,
483 TpConnectionStatusReason reason,
484 TpConnectionStatus current,
485 TpConnectionStatus previous,
486 EmpathyMainWindow *window)
488 main_window_update_status (window, manager);
490 if (current == TP_CONNECTION_STATUS_DISCONNECTED &&
491 reason != TP_CONNECTION_STATUS_REASON_REQUESTED) {
492 const gchar *message;
495 case TP_CONNECTION_STATUS_REASON_NONE_SPECIFIED:
496 message = _("No error specified");
498 case TP_CONNECTION_STATUS_REASON_NETWORK_ERROR:
499 message = _("Network error");
501 case TP_CONNECTION_STATUS_REASON_AUTHENTICATION_FAILED:
502 message = _("Authentication failed");
504 case TP_CONNECTION_STATUS_REASON_ENCRYPTION_ERROR:
505 message = _("Encryption error");
507 case TP_CONNECTION_STATUS_REASON_NAME_IN_USE:
508 message = _("Name in use");
510 case TP_CONNECTION_STATUS_REASON_CERT_NOT_PROVIDED:
511 message = _("Certificate not provided");
513 case TP_CONNECTION_STATUS_REASON_CERT_UNTRUSTED:
514 message = _("Certificate untrusted");
516 case TP_CONNECTION_STATUS_REASON_CERT_EXPIRED:
517 message = _("Certificate expired");
519 case TP_CONNECTION_STATUS_REASON_CERT_NOT_ACTIVATED:
520 message = _("Certificate not activated");
522 case TP_CONNECTION_STATUS_REASON_CERT_HOSTNAME_MISMATCH:
523 message = _("Certificate hostname mismatch");
525 case TP_CONNECTION_STATUS_REASON_CERT_FINGERPRINT_MISMATCH:
526 message = _("Certificate fingerprint mismatch");
528 case TP_CONNECTION_STATUS_REASON_CERT_SELF_SIGNED:
529 message = _("Certificate self-signed");
531 case TP_CONNECTION_STATUS_REASON_CERT_OTHER_ERROR:
532 message = _("Certificate error");
535 message = _("Unknown error");
539 main_window_error_display (window, account, message);
542 if (current == TP_CONNECTION_STATUS_DISCONNECTED) {
543 empathy_sound_play (GTK_WIDGET (window->window),
544 EMPATHY_SOUND_ACCOUNT_DISCONNECTED);
547 if (current == TP_CONNECTION_STATUS_CONNECTED) {
548 GtkWidget *error_widget;
550 empathy_sound_play (GTK_WIDGET (window->window),
551 EMPATHY_SOUND_ACCOUNT_CONNECTED);
553 /* Account connected without error, remove error message if any */
554 error_widget = g_hash_table_lookup (window->errors, account);
556 gtk_widget_destroy (error_widget);
557 g_hash_table_remove (window->errors, account);
563 main_window_contact_presence_changed_cb (EmpathyContactMonitor *monitor,
564 EmpathyContact *contact,
567 EmpathyMainWindow *window)
570 gboolean should_play;
572 account = empathy_contact_get_account (contact);
573 should_play = !empathy_account_manager_is_account_just_connected (window->account_manager, account);
579 if (previous < MC_PRESENCE_AVAILABLE && current > MC_PRESENCE_OFFLINE) {
580 /* someone is logging in */
581 empathy_sound_play (GTK_WIDGET (window->window),
582 EMPATHY_SOUND_CONTACT_CONNECTED);
586 if (previous > MC_PRESENCE_OFFLINE && current < MC_PRESENCE_AVAILABLE) {
587 /* someone is logging off */
588 empathy_sound_play (GTK_WIDGET (window->window),
589 EMPATHY_SOUND_CONTACT_DISCONNECTED);
594 main_window_accels_load (void)
598 filename = g_build_filename (g_get_home_dir (), ".gnome2", PACKAGE_NAME, ACCELS_FILENAME, NULL);
599 if (g_file_test (filename, G_FILE_TEST_EXISTS)) {
600 DEBUG ("Loading from:'%s'", filename);
601 gtk_accel_map_load (filename);
608 main_window_accels_save (void)
611 gchar *file_with_path;
613 dir = g_build_filename (g_get_home_dir (), ".gnome2", PACKAGE_NAME, NULL);
614 g_mkdir_with_parents (dir, S_IRUSR | S_IWUSR | S_IXUSR);
615 file_with_path = g_build_filename (dir, ACCELS_FILENAME, NULL);
618 DEBUG ("Saving to:'%s'", file_with_path);
619 gtk_accel_map_save (file_with_path);
621 g_free (file_with_path);
625 main_window_destroy_cb (GtkWidget *widget,
626 EmpathyMainWindow *window)
628 /* Save user-defined accelerators. */
629 main_window_accels_save ();
631 g_signal_handlers_disconnect_by_func (window->account_manager,
632 main_window_connection_changed_cb,
635 if (window->size_timeout_id) {
636 g_source_remove (window->size_timeout_id);
639 g_list_free (window->actions_connected);
641 g_object_unref (window->mc);
642 g_object_unref (window->account_manager);
643 g_object_unref (window->list_store);
644 g_hash_table_destroy (window->errors);
646 g_signal_handlers_disconnect_by_func (window->event_manager,
647 main_window_event_added_cb,
649 g_signal_handlers_disconnect_by_func (window->event_manager,
650 main_window_event_removed_cb,
652 g_object_unref (window->event_manager);
653 g_object_unref (window->ui_manager);
659 main_window_chat_quit_cb (GtkAction *action,
660 EmpathyMainWindow *window)
666 main_window_chat_history_cb (GtkAction *action,
667 EmpathyMainWindow *window)
669 empathy_log_window_show (NULL, NULL, FALSE, GTK_WINDOW (window->window));
673 main_window_chat_new_message_cb (GtkAction *action,
674 EmpathyMainWindow *window)
676 empathy_new_message_dialog_show (GTK_WINDOW (window->window));
680 main_window_chat_add_contact_cb (GtkAction *action,
681 EmpathyMainWindow *window)
683 empathy_new_contact_dialog_show (GTK_WINDOW (window->window));
687 main_window_chat_show_offline_cb (GtkToggleAction *action,
688 EmpathyMainWindow *window)
692 current = gtk_toggle_action_get_active (action);
693 empathy_conf_set_bool (empathy_conf_get (),
694 EMPATHY_PREFS_CONTACTS_SHOW_OFFLINE,
697 /* Turn off sound just while we alter the contact list. */
698 // FIXME: empathy_sound_set_enabled (FALSE);
699 empathy_contact_list_store_set_show_offline (window->list_store, current);
700 //empathy_sound_set_enabled (TRUE);
704 main_window_favorite_chatroom_join (EmpathyChatroom *chatroom)
710 mc = empathy_mission_control_dup_singleton ();
711 account = empathy_chatroom_get_account (chatroom);
712 room = empathy_chatroom_get_room (chatroom);
714 if (mission_control_get_connection_status (mc, account, NULL) !=
715 TP_CONNECTION_STATUS_CONNECTED) {
719 DEBUG ("Requesting channel for '%s'", room);
720 empathy_dispatcher_join_muc (account, room, NULL, NULL);
726 main_window_favorite_chatroom_menu_activate_cb (GtkMenuItem *menu_item,
727 EmpathyChatroom *chatroom)
729 main_window_favorite_chatroom_join (chatroom);
733 main_window_favorite_chatroom_menu_add (EmpathyMainWindow *window,
734 EmpathyChatroom *chatroom)
736 GtkWidget *menu_item;
739 if (g_object_get_data (G_OBJECT (chatroom), "menu_item")) {
743 name = empathy_chatroom_get_name (chatroom);
744 menu_item = gtk_menu_item_new_with_label (name);
746 g_object_set_data (G_OBJECT (chatroom), "menu_item", menu_item);
747 g_signal_connect (menu_item, "activate",
748 G_CALLBACK (main_window_favorite_chatroom_menu_activate_cb),
751 gtk_menu_shell_insert (GTK_MENU_SHELL (window->room_menu),
754 gtk_widget_show (menu_item);
758 main_window_favorite_chatroom_menu_added_cb (EmpathyChatroomManager *manager,
759 EmpathyChatroom *chatroom,
760 EmpathyMainWindow *window)
762 main_window_favorite_chatroom_menu_add (window, chatroom);
763 gtk_widget_show (window->room_separator);
764 gtk_action_set_sensitive (window->room_join_favorites, TRUE);
768 main_window_favorite_chatroom_menu_removed_cb (EmpathyChatroomManager *manager,
769 EmpathyChatroom *chatroom,
770 EmpathyMainWindow *window)
772 GtkWidget *menu_item;
775 menu_item = g_object_get_data (G_OBJECT (chatroom), "menu_item");
776 g_object_set_data (G_OBJECT (chatroom), "menu_item", NULL);
777 gtk_widget_destroy (menu_item);
779 chatrooms = empathy_chatroom_manager_get_chatrooms (window->chatroom_manager, NULL);
781 gtk_widget_show (window->room_separator);
783 gtk_widget_hide (window->room_separator);
786 gtk_action_set_sensitive (window->room_join_favorites, chatrooms != NULL);
787 g_list_free (chatrooms);
791 main_window_favorite_chatroom_menu_setup (EmpathyMainWindow *window)
793 GList *chatrooms, *l;
796 window->chatroom_manager = empathy_chatroom_manager_dup_singleton (NULL);
797 chatrooms = empathy_chatroom_manager_get_chatrooms (window->chatroom_manager, NULL);
798 room = gtk_ui_manager_get_widget (window->ui_manager,
800 window->room_menu = gtk_menu_item_get_submenu (GTK_MENU_ITEM (room));
801 window->room_separator = gtk_ui_manager_get_widget (window->ui_manager,
802 "/menubar/room/room_separator");
804 for (l = chatrooms; l; l = l->next) {
805 main_window_favorite_chatroom_menu_add (window, l->data);
809 gtk_widget_hide (window->room_separator);
812 gtk_action_set_sensitive (window->room_join_favorites, chatrooms != NULL);
814 g_signal_connect (window->chatroom_manager, "chatroom-added",
815 G_CALLBACK (main_window_favorite_chatroom_menu_added_cb),
817 g_signal_connect (window->chatroom_manager, "chatroom-removed",
818 G_CALLBACK (main_window_favorite_chatroom_menu_removed_cb),
821 g_list_free (chatrooms);
825 main_window_room_join_new_cb (GtkAction *action,
826 EmpathyMainWindow *window)
828 empathy_new_chatroom_dialog_show (GTK_WINDOW (window->window));
832 main_window_room_join_favorites_cb (GtkAction *action,
833 EmpathyMainWindow *window)
835 GList *chatrooms, *l;
837 chatrooms = empathy_chatroom_manager_get_chatrooms (window->chatroom_manager, NULL);
838 for (l = chatrooms; l; l = l->next) {
839 main_window_favorite_chatroom_join (l->data);
841 g_list_free (chatrooms);
845 main_window_room_manage_favorites_cb (GtkAction *action,
846 EmpathyMainWindow *window)
848 empathy_chatrooms_window_show (GTK_WINDOW (window->window));
852 main_window_edit_cb (GtkAction *action,
853 EmpathyMainWindow *window)
857 /* FIXME: It should use the UIManager to merge the contact/group submenu */
858 submenu = empathy_contact_list_view_get_contact_menu (window->list_view);
863 item = GTK_MENU_ITEM (window->edit_context);
864 label = gtk_bin_get_child (GTK_BIN (item));
865 gtk_label_set_text (GTK_LABEL (label), _("Contact"));
867 gtk_widget_show (window->edit_context);
868 gtk_widget_show (window->edit_context_separator);
870 gtk_menu_item_set_submenu (item, submenu);
875 submenu = empathy_contact_list_view_get_group_menu (window->list_view);
880 item = GTK_MENU_ITEM (window->edit_context);
881 label = gtk_bin_get_child (GTK_BIN (item));
882 gtk_label_set_text (GTK_LABEL (label), _("Group"));
884 gtk_widget_show (window->edit_context);
885 gtk_widget_show (window->edit_context_separator);
887 gtk_menu_item_set_submenu (item, submenu);
892 gtk_widget_hide (window->edit_context);
893 gtk_widget_hide (window->edit_context_separator);
899 main_window_edit_accounts_cb (GtkAction *action,
900 EmpathyMainWindow *window)
902 empathy_accounts_dialog_show (GTK_WINDOW (window->window), NULL);
906 main_window_edit_personal_information_cb (GtkAction *action,
907 EmpathyMainWindow *window)
911 accounts = mission_control_get_online_connections (window->mc, NULL);
913 EmpathyContactFactory *factory;
914 EmpathyContact *contact;
917 account = accounts->data;
918 factory = empathy_contact_factory_dup_singleton ();
919 contact = empathy_contact_factory_get_user (factory, account);
920 empathy_contact_run_until_ready (contact,
921 EMPATHY_CONTACT_READY_HANDLE |
922 EMPATHY_CONTACT_READY_ID,
925 empathy_contact_information_dialog_show (contact,
926 GTK_WINDOW (window->window),
929 g_slist_foreach (accounts, (GFunc) g_object_unref, NULL);
930 g_slist_free (accounts);
931 g_object_unref (factory);
932 g_object_unref (contact);
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_contents_cb (GtkAction *action,
952 EmpathyMainWindow *window)
954 empathy_url_show (window->window, "ghelp:empathy");
958 main_window_throbber_button_press_event_cb (GtkWidget *throbber_ebox,
959 GdkEventButton *event,
960 EmpathyMainWindow *window)
962 if (event->type != GDK_BUTTON_PRESS ||
963 event->button != 1) {
967 empathy_accounts_dialog_show (GTK_WINDOW (window->window), NULL);
973 main_window_configure_event_timeout_cb (EmpathyMainWindow *window)
977 gtk_window_get_size (GTK_WINDOW (window->window), &w, &h);
978 gtk_window_get_position (GTK_WINDOW (window->window), &x, &y);
980 empathy_geometry_save (GEOMETRY_NAME, x, y, w, h);
982 window->size_timeout_id = 0;
988 main_window_configure_event_cb (GtkWidget *widget,
989 GdkEventConfigure *event,
990 EmpathyMainWindow *window)
992 if (window->size_timeout_id) {
993 g_source_remove (window->size_timeout_id);
996 window->size_timeout_id = g_timeout_add_seconds (1,
997 (GSourceFunc) main_window_configure_event_timeout_cb,
1004 main_window_account_created_or_deleted_cb (EmpathyAccountManager *manager,
1006 EmpathyMainWindow *window)
1008 gtk_action_set_sensitive (window->chat_history,
1009 empathy_account_manager_get_count (manager) > 0);
1013 main_window_notify_show_offline_cb (EmpathyConf *conf,
1015 gpointer toggle_action)
1017 gboolean show_offline;
1019 if (empathy_conf_get_bool (conf, key, &show_offline)) {
1020 gtk_toggle_action_set_active (toggle_action, show_offline);
1025 main_window_notify_show_avatars_cb (EmpathyConf *conf,
1027 EmpathyMainWindow *window)
1029 gboolean show_avatars;
1031 if (empathy_conf_get_bool (conf, key, &show_avatars)) {
1032 empathy_contact_list_store_set_show_avatars (window->list_store,
1038 main_window_notify_compact_contact_list_cb (EmpathyConf *conf,
1040 EmpathyMainWindow *window)
1042 gboolean compact_contact_list;
1044 if (empathy_conf_get_bool (conf, key, &compact_contact_list)) {
1045 empathy_contact_list_store_set_is_compact (window->list_store,
1046 compact_contact_list);
1051 main_window_notify_sort_criterium_cb (EmpathyConf *conf,
1053 EmpathyMainWindow *window)
1057 if (empathy_conf_get_string (conf, key, &str) && str) {
1059 GEnumClass *enum_class;
1060 GEnumValue *enum_value;
1062 type = empathy_contact_list_store_sort_get_type ();
1063 enum_class = G_ENUM_CLASS (g_type_class_peek (type));
1064 enum_value = g_enum_get_value_by_nick (enum_class, str);
1068 empathy_contact_list_store_set_sort_criterium (window->list_store,
1075 main_window_connection_items_setup (EmpathyMainWindow *window,
1081 const gchar *actions_connected[] = {
1085 "edit_personal_information"
1088 for (i = 0, list = NULL; i < G_N_ELEMENTS (actions_connected); i++) {
1089 action = gtk_builder_get_object (gui, actions_connected[i]);
1090 list = g_list_prepend (list, action);
1093 window->actions_connected = list;
1097 empathy_main_window_get (void)
1099 return window != NULL ? window->window : NULL;
1103 empathy_main_window_show (void)
1105 EmpathyContactList *list_iface;
1106 EmpathyContactMonitor *monitor;
1110 GtkToggleAction *show_offline_widget;
1113 gboolean show_offline;
1114 gboolean show_avatars;
1115 gboolean compact_contact_list;
1121 empathy_window_present (GTK_WINDOW (window->window), TRUE);
1122 return window->window;
1125 window = g_new0 (EmpathyMainWindow, 1);
1127 /* Set up interface */
1128 filename = empathy_file_lookup ("empathy-main-window.ui", "src");
1129 gui = empathy_builder_get_file (filename,
1130 "main_window", &window->window,
1131 "main_vbox", &window->main_vbox,
1132 "errors_vbox", &window->errors_vbox,
1133 "ui_manager", &window->ui_manager,
1134 "chat_show_offline", &show_offline_widget,
1135 "chat_history", &window->chat_history,
1136 "room_join_favorites", &window->room_join_favorites,
1137 "presence_toolbar", &window->presence_toolbar,
1138 "roster_scrolledwindow", &sw,
1142 empathy_builder_connect (gui, window,
1143 "main_window", "destroy", main_window_destroy_cb,
1144 "main_window", "configure_event", main_window_configure_event_cb,
1145 "chat_quit", "activate", main_window_chat_quit_cb,
1146 "chat_new_message", "activate", main_window_chat_new_message_cb,
1147 "chat_history", "activate", main_window_chat_history_cb,
1148 "room_join_new", "activate", main_window_room_join_new_cb,
1149 "room_join_favorites", "activate", main_window_room_join_favorites_cb,
1150 "room_manage_favorites", "activate", main_window_room_manage_favorites_cb,
1151 "chat_add_contact", "activate", main_window_chat_add_contact_cb,
1152 "chat_show_offline", "toggled", main_window_chat_show_offline_cb,
1153 "edit", "activate", main_window_edit_cb,
1154 "edit_accounts", "activate", main_window_edit_accounts_cb,
1155 "edit_personal_information", "activate", main_window_edit_personal_information_cb,
1156 "edit_preferences", "activate", main_window_edit_preferences_cb,
1157 "help_about", "activate", main_window_help_about_cb,
1158 "help_contents", "activate", main_window_help_contents_cb,
1161 /* Set up connection related widgets. */
1162 main_window_connection_items_setup (window, gui);
1164 g_object_ref (window->ui_manager);
1165 g_object_unref (gui);
1167 window->mc = empathy_mission_control_dup_singleton ();
1168 window->account_manager = empathy_account_manager_dup_singleton ();
1170 g_signal_connect (window->account_manager,
1171 "account-connection-changed",
1172 G_CALLBACK (main_window_connection_changed_cb), window);
1174 window->errors = g_hash_table_new_full (empathy_account_hash,
1175 empathy_account_equal,
1180 main_window_favorite_chatroom_menu_setup (window);
1182 window->edit_context = gtk_ui_manager_get_widget (window->ui_manager,
1183 "/menubar/edit/edit_context");
1184 window->edit_context_separator = gtk_ui_manager_get_widget (window->ui_manager,
1185 "/menubar/edit/edit_context_separator");
1186 gtk_widget_hide (window->edit_context);
1187 gtk_widget_hide (window->edit_context_separator);
1189 /* Set up presence chooser */
1190 window->presence_chooser = empathy_presence_chooser_new ();
1191 gtk_widget_show (window->presence_chooser);
1192 item = gtk_tool_item_new ();
1193 gtk_widget_show (GTK_WIDGET (item));
1194 gtk_container_add (GTK_CONTAINER (item), window->presence_chooser);
1195 gtk_tool_item_set_is_important (item, TRUE);
1196 gtk_tool_item_set_expand (item, TRUE);
1197 gtk_toolbar_insert (GTK_TOOLBAR (window->presence_toolbar), item, -1);
1199 /* Set up the throbber */
1200 ebox = gtk_event_box_new ();
1201 gtk_event_box_set_visible_window (GTK_EVENT_BOX (ebox), FALSE);
1202 gtk_widget_set_tooltip_text (ebox, _("Show and edit accounts"));
1203 g_signal_connect (ebox,
1204 "button-press-event",
1205 G_CALLBACK (main_window_throbber_button_press_event_cb),
1207 gtk_widget_show (ebox);
1209 window->throbber = ephy_spinner_new ();
1210 ephy_spinner_set_size (EPHY_SPINNER (window->throbber), GTK_ICON_SIZE_LARGE_TOOLBAR);
1211 gtk_container_add (GTK_CONTAINER (ebox), window->throbber);
1212 gtk_widget_show (window->throbber);
1214 item = gtk_tool_item_new ();
1215 gtk_container_add (GTK_CONTAINER (item), ebox);
1216 gtk_toolbar_insert (GTK_TOOLBAR (window->presence_toolbar), item, -1);
1217 gtk_widget_show (GTK_WIDGET (item));
1219 /* Set up contact list. */
1220 empathy_status_presets_get_all ();
1222 list_iface = EMPATHY_CONTACT_LIST (empathy_contact_manager_dup_singleton ());
1223 monitor = empathy_contact_list_get_monitor (list_iface);
1224 window->list_store = empathy_contact_list_store_new (list_iface);
1225 window->list_view = empathy_contact_list_view_new (window->list_store,
1226 EMPATHY_CONTACT_LIST_FEATURE_ALL,
1227 EMPATHY_CONTACT_FEATURE_ALL);
1228 g_signal_connect (monitor, "contact-presence-changed",
1229 G_CALLBACK (main_window_contact_presence_changed_cb), window);
1230 g_object_unref (list_iface);
1232 gtk_widget_show (GTK_WIDGET (window->list_view));
1233 gtk_container_add (GTK_CONTAINER (sw),
1234 GTK_WIDGET (window->list_view));
1235 g_signal_connect (window->list_view, "row-activated",
1236 G_CALLBACK (main_window_row_activated_cb),
1239 /* Load user-defined accelerators. */
1240 main_window_accels_load ();
1242 /* Set window size. */
1243 empathy_geometry_load (GEOMETRY_NAME, &x, &y, &w, &h);
1245 if (w >= 1 && h >= 1) {
1246 /* Use the defaults from the ui file if we
1247 * don't have good w, h geometry.
1249 DEBUG ("Configuring window default size w:%d, h:%d", w, h);
1250 gtk_window_set_default_size (GTK_WINDOW (window->window), w, h);
1253 if (x >= 0 && y >= 0) {
1254 /* Let the window manager position it if we
1255 * don't have good x, y coordinates.
1257 DEBUG ("Configuring window default position x:%d, y:%d", x, y);
1258 gtk_window_move (GTK_WINDOW (window->window), x, y);
1261 /* Enable event handling */
1262 window->event_manager = empathy_event_manager_dup_singleton ();
1263 g_signal_connect (window->event_manager, "event-added",
1264 G_CALLBACK (main_window_event_added_cb),
1266 g_signal_connect (window->event_manager, "event-removed",
1267 G_CALLBACK (main_window_event_removed_cb),
1270 g_signal_connect (window->account_manager, "account-created",
1271 G_CALLBACK (main_window_account_created_or_deleted_cb),
1273 g_signal_connect (window->account_manager, "account-deleted",
1274 G_CALLBACK (main_window_account_created_or_deleted_cb),
1276 main_window_account_created_or_deleted_cb (window->account_manager, NULL, window);
1278 l = empathy_event_manager_get_events (window->event_manager);
1280 main_window_event_added_cb (window->event_manager,
1285 conf = empathy_conf_get ();
1287 /* Show offline ? */
1288 empathy_conf_get_bool (conf,
1289 EMPATHY_PREFS_CONTACTS_SHOW_OFFLINE,
1291 empathy_conf_notify_add (conf,
1292 EMPATHY_PREFS_CONTACTS_SHOW_OFFLINE,
1293 main_window_notify_show_offline_cb,
1294 show_offline_widget);
1296 gtk_toggle_action_set_active (show_offline_widget, show_offline);
1298 /* Show avatars ? */
1299 empathy_conf_get_bool (conf,
1300 EMPATHY_PREFS_UI_SHOW_AVATARS,
1302 empathy_conf_notify_add (conf,
1303 EMPATHY_PREFS_UI_SHOW_AVATARS,
1304 (EmpathyConfNotifyFunc) main_window_notify_show_avatars_cb,
1306 empathy_contact_list_store_set_show_avatars (window->list_store, show_avatars);
1309 empathy_conf_get_bool (conf,
1310 EMPATHY_PREFS_UI_COMPACT_CONTACT_LIST,
1311 &compact_contact_list);
1312 empathy_conf_notify_add (conf,
1313 EMPATHY_PREFS_UI_COMPACT_CONTACT_LIST,
1314 (EmpathyConfNotifyFunc) main_window_notify_compact_contact_list_cb,
1316 empathy_contact_list_store_set_is_compact (window->list_store, compact_contact_list);
1318 /* Sort criterium */
1319 empathy_conf_notify_add (conf,
1320 EMPATHY_PREFS_CONTACTS_SORT_CRITERIUM,
1321 (EmpathyConfNotifyFunc) main_window_notify_sort_criterium_cb,
1323 main_window_notify_sort_criterium_cb (conf,
1324 EMPATHY_PREFS_CONTACTS_SORT_CRITERIUM,
1327 main_window_update_status (window, window->account_manager);
1329 return window->window;