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;
98 GtkWidget *room_join_favorites;
99 GtkWidget *edit_context;
100 GtkWidget *edit_context_separator;
101 GtkWidget *chat_history_menu_item;
103 guint size_timeout_id;
106 /* Widgets that are enabled when there are connected accounts */
107 GList *widgets_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->widgets_connected; l; l = l->next) {
476 gtk_widget_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->widgets_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);
658 main_window_favorite_chatroom_join (EmpathyChatroom *chatroom)
664 mc = empathy_mission_control_dup_singleton ();
665 account = empathy_chatroom_get_account (chatroom);
666 room = empathy_chatroom_get_room (chatroom);
668 if (mission_control_get_connection_status (mc, account, NULL) !=
669 TP_CONNECTION_STATUS_CONNECTED) {
673 DEBUG ("Requesting channel for '%s'", room);
674 empathy_dispatcher_join_muc (account, room, NULL, NULL);
680 main_window_favorite_chatroom_menu_activate_cb (GtkMenuItem *menu_item,
681 EmpathyChatroom *chatroom)
683 main_window_favorite_chatroom_join (chatroom);
687 main_window_favorite_chatroom_menu_add (EmpathyMainWindow *window,
688 EmpathyChatroom *chatroom)
690 GtkWidget *menu_item;
693 if (g_object_get_data (G_OBJECT (chatroom), "menu_item")) {
697 name = empathy_chatroom_get_name (chatroom);
698 menu_item = gtk_menu_item_new_with_label (name);
700 g_object_set_data (G_OBJECT (chatroom), "menu_item", menu_item);
701 g_signal_connect (menu_item, "activate",
702 G_CALLBACK (main_window_favorite_chatroom_menu_activate_cb),
705 gtk_menu_shell_insert (GTK_MENU_SHELL (window->room_menu),
708 gtk_widget_show (menu_item);
712 main_window_favorite_chatroom_menu_added_cb (EmpathyChatroomManager *manager,
713 EmpathyChatroom *chatroom,
714 EmpathyMainWindow *window)
716 main_window_favorite_chatroom_menu_add (window, chatroom);
717 gtk_widget_show (window->room_sep);
718 gtk_widget_set_sensitive (window->room_join_favorites, TRUE);
722 main_window_favorite_chatroom_menu_removed_cb (EmpathyChatroomManager *manager,
723 EmpathyChatroom *chatroom,
724 EmpathyMainWindow *window)
726 GtkWidget *menu_item;
729 menu_item = g_object_get_data (G_OBJECT (chatroom), "menu_item");
730 g_object_set_data (G_OBJECT (chatroom), "menu_item", NULL);
731 gtk_widget_destroy (menu_item);
733 chatrooms = empathy_chatroom_manager_get_chatrooms (window->chatroom_manager, NULL);
735 gtk_widget_show (window->room_sep);
737 gtk_widget_hide (window->room_sep);
740 gtk_widget_set_sensitive (window->room_join_favorites, chatrooms != NULL);
741 g_list_free (chatrooms);
745 main_window_favorite_chatroom_menu_setup (EmpathyMainWindow *window)
747 GList *chatrooms, *l;
749 window->chatroom_manager = empathy_chatroom_manager_dup_singleton (NULL);
750 chatrooms = empathy_chatroom_manager_get_chatrooms (window->chatroom_manager, NULL);
751 window->room_menu = gtk_menu_item_get_submenu (GTK_MENU_ITEM (window->room));
753 for (l = chatrooms; l; l = l->next) {
754 main_window_favorite_chatroom_menu_add (window, l->data);
758 gtk_widget_hide (window->room_sep);
761 gtk_widget_set_sensitive (window->room_join_favorites, chatrooms != NULL);
763 g_signal_connect (window->chatroom_manager, "chatroom-added",
764 G_CALLBACK (main_window_favorite_chatroom_menu_added_cb),
766 g_signal_connect (window->chatroom_manager, "chatroom-removed",
767 G_CALLBACK (main_window_favorite_chatroom_menu_removed_cb),
770 g_list_free (chatrooms);
774 main_window_chat_quit_cb (GtkWidget *widget,
775 EmpathyMainWindow *window)
781 main_window_chat_new_message_cb (GtkWidget *widget,
782 EmpathyMainWindow *window)
784 empathy_new_message_dialog_show (GTK_WINDOW (window->window));
788 main_window_chat_history_cb (GtkWidget *widget,
789 EmpathyMainWindow *window)
791 empathy_log_window_show (NULL, NULL, FALSE, GTK_WINDOW (window->window));
795 main_window_room_join_new_cb (GtkWidget *widget,
796 EmpathyMainWindow *window)
798 empathy_new_chatroom_dialog_show (GTK_WINDOW (window->window));
802 main_window_room_join_favorites_cb (GtkWidget *widget,
803 EmpathyMainWindow *window)
805 GList *chatrooms, *l;
807 chatrooms = empathy_chatroom_manager_get_chatrooms (window->chatroom_manager, NULL);
808 for (l = chatrooms; l; l = l->next) {
809 main_window_favorite_chatroom_join (l->data);
811 g_list_free (chatrooms);
815 main_window_room_manage_favorites_cb (GtkWidget *widget,
816 EmpathyMainWindow *window)
818 empathy_chatrooms_window_show (GTK_WINDOW (window->window));
822 main_window_chat_add_contact_cb (GtkWidget *widget,
823 EmpathyMainWindow *window)
825 empathy_new_contact_dialog_show (GTK_WINDOW (window->window));
829 main_window_chat_show_offline_cb (GtkCheckMenuItem *item,
830 EmpathyMainWindow *window)
834 current = gtk_check_menu_item_get_active (item);
836 empathy_conf_set_bool (empathy_conf_get (),
837 EMPATHY_PREFS_CONTACTS_SHOW_OFFLINE,
840 /* Turn off sound just while we alter the contact list. */
841 // FIXME: empathy_sound_set_enabled (FALSE);
842 empathy_contact_list_store_set_show_offline (window->list_store, current);
843 //empathy_sound_set_enabled (TRUE);
847 main_window_edit_button_press_event_cb (GtkWidget *widget,
848 GdkEventButton *event,
849 EmpathyMainWindow *window)
853 if (!event->button == 1) {
857 submenu = empathy_contact_list_view_get_contact_menu (window->list_view);
862 item = GTK_MENU_ITEM (window->edit_context);
863 label = gtk_bin_get_child (GTK_BIN (item));
864 gtk_label_set_text (GTK_LABEL (label), _("Contact"));
866 gtk_widget_show (window->edit_context);
867 gtk_widget_show (window->edit_context_separator);
869 gtk_menu_item_set_submenu (item, submenu);
874 submenu = empathy_contact_list_view_get_group_menu (window->list_view);
879 item = GTK_MENU_ITEM (window->edit_context);
880 label = gtk_bin_get_child (GTK_BIN (item));
881 gtk_label_set_text (GTK_LABEL (label), _("Group"));
883 gtk_widget_show (window->edit_context);
884 gtk_widget_show (window->edit_context_separator);
886 gtk_menu_item_set_submenu (item, submenu);
891 gtk_widget_hide (window->edit_context);
892 gtk_widget_hide (window->edit_context_separator);
898 main_window_edit_accounts_cb (GtkWidget *widget,
899 EmpathyMainWindow *window)
901 empathy_accounts_dialog_show (GTK_WINDOW (window->window), NULL);
905 main_window_edit_personal_information_cb (GtkWidget *widget,
906 EmpathyMainWindow *window)
910 accounts = mission_control_get_online_connections (window->mc, NULL);
912 EmpathyContactFactory *factory;
913 EmpathyContact *contact;
916 account = accounts->data;
917 factory = empathy_contact_factory_dup_singleton ();
918 contact = empathy_contact_factory_get_user (factory, account);
919 empathy_contact_run_until_ready (contact,
920 EMPATHY_CONTACT_READY_HANDLE |
921 EMPATHY_CONTACT_READY_ID,
924 empathy_contact_information_dialog_show (contact,
925 GTK_WINDOW (window->window),
928 g_slist_foreach (accounts, (GFunc) g_object_unref, NULL);
929 g_slist_free (accounts);
930 g_object_unref (factory);
931 g_object_unref (contact);
936 main_window_edit_preferences_cb (GtkWidget *widget,
937 EmpathyMainWindow *window)
939 empathy_preferences_show (GTK_WINDOW (window->window));
943 main_window_help_about_cb (GtkWidget *widget,
944 EmpathyMainWindow *window)
946 empathy_about_dialog_new (GTK_WINDOW (window->window));
950 main_window_help_contents_cb (GtkWidget *widget,
951 EmpathyMainWindow *window)
953 empathy_url_show (widget, "ghelp:empathy");
957 main_window_throbber_button_press_event_cb (GtkWidget *throbber_ebox,
958 GdkEventButton *event,
959 EmpathyMainWindow *window)
961 if (event->type != GDK_BUTTON_PRESS ||
962 event->button != 1) {
966 empathy_accounts_dialog_show (GTK_WINDOW (window->window), NULL);
972 main_window_configure_event_timeout_cb (EmpathyMainWindow *window)
976 gtk_window_get_size (GTK_WINDOW (window->window), &w, &h);
977 gtk_window_get_position (GTK_WINDOW (window->window), &x, &y);
979 empathy_geometry_save (GEOMETRY_NAME, x, y, w, h);
981 window->size_timeout_id = 0;
987 main_window_configure_event_cb (GtkWidget *widget,
988 GdkEventConfigure *event,
989 EmpathyMainWindow *window)
991 if (window->size_timeout_id) {
992 g_source_remove (window->size_timeout_id);
995 window->size_timeout_id = g_timeout_add_seconds (1,
996 (GSourceFunc) main_window_configure_event_timeout_cb,
1003 main_window_account_created_or_deleted_cb (EmpathyAccountManager *manager,
1005 EmpathyMainWindow *window)
1007 gtk_widget_set_sensitive (GTK_WIDGET (window->chat_history_menu_item),
1008 empathy_account_manager_get_count (manager) > 0);
1012 main_window_notify_show_offline_cb (EmpathyConf *conf,
1014 gpointer check_menu_item)
1016 gboolean show_offline;
1018 if (empathy_conf_get_bool (conf, key, &show_offline)) {
1019 gtk_check_menu_item_set_active (GTK_CHECK_MENU_ITEM (check_menu_item),
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 *widgets_connected[] = {
1085 "edit_personal_information"
1088 for (i = 0, list = NULL; i < G_N_ELEMENTS (widgets_connected); i++) {
1089 w = gtk_builder_get_object (gui, widgets_connected[i]);
1090 list = g_list_prepend (list, w);
1093 window->widgets_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 GtkWidget *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 "chat_show_offline", &show_offline_widget,
1134 "room", &window->room,
1135 "room_sep", &window->room_sep,
1136 "room_join_favorites", &window->room_join_favorites,
1137 "edit_context", &window->edit_context,
1138 "edit_context_separator", &window->edit_context_separator,
1139 "presence_toolbar", &window->presence_toolbar,
1140 "roster_scrolledwindow", &sw,
1141 "chat_history", &window->chat_history_menu_item,
1145 empathy_builder_connect (gui, window,
1146 "main_window", "destroy", main_window_destroy_cb,
1147 "main_window", "configure_event", main_window_configure_event_cb,
1148 "chat_quit", "activate", main_window_chat_quit_cb,
1149 "chat_new_message", "activate", main_window_chat_new_message_cb,
1150 "chat_history", "activate", main_window_chat_history_cb,
1151 "room_join_new", "activate", main_window_room_join_new_cb,
1152 "room_join_favorites", "activate", main_window_room_join_favorites_cb,
1153 "room_manage_favorites", "activate", main_window_room_manage_favorites_cb,
1154 "chat_add_contact", "activate", main_window_chat_add_contact_cb,
1155 "chat_show_offline", "toggled", main_window_chat_show_offline_cb,
1156 "edit", "button-press-event", main_window_edit_button_press_event_cb,
1157 "edit_accounts", "activate", main_window_edit_accounts_cb,
1158 "edit_personal_information", "activate", main_window_edit_personal_information_cb,
1159 "edit_preferences", "activate", main_window_edit_preferences_cb,
1160 "help_about", "activate", main_window_help_about_cb,
1161 "help_contents", "activate", main_window_help_contents_cb,
1164 /* Set up connection related widgets. */
1165 main_window_connection_items_setup (window, gui);
1166 g_object_unref (gui);
1168 window->mc = empathy_mission_control_dup_singleton ();
1169 window->account_manager = empathy_account_manager_dup_singleton ();
1171 g_signal_connect (window->account_manager,
1172 "account-connection-changed",
1173 G_CALLBACK (main_window_connection_changed_cb), window);
1175 window->errors = g_hash_table_new_full (empathy_account_hash,
1176 empathy_account_equal,
1181 main_window_favorite_chatroom_menu_setup (window);
1183 gtk_widget_hide (window->edit_context);
1184 gtk_widget_hide (window->edit_context_separator);
1186 /* Set up presence chooser */
1187 window->presence_chooser = empathy_presence_chooser_new ();
1188 gtk_widget_show (window->presence_chooser);
1189 item = gtk_tool_item_new ();
1190 gtk_widget_show (GTK_WIDGET (item));
1191 gtk_container_add (GTK_CONTAINER (item), window->presence_chooser);
1192 gtk_tool_item_set_is_important (item, TRUE);
1193 gtk_tool_item_set_expand (item, TRUE);
1194 gtk_toolbar_insert (GTK_TOOLBAR (window->presence_toolbar), item, -1);
1196 /* Set up the throbber */
1197 ebox = gtk_event_box_new ();
1198 gtk_event_box_set_visible_window (GTK_EVENT_BOX (ebox), FALSE);
1199 gtk_widget_set_tooltip_text (ebox, _("Show and edit accounts"));
1200 g_signal_connect (ebox,
1201 "button-press-event",
1202 G_CALLBACK (main_window_throbber_button_press_event_cb),
1204 gtk_widget_show (ebox);
1206 window->throbber = ephy_spinner_new ();
1207 ephy_spinner_set_size (EPHY_SPINNER (window->throbber), GTK_ICON_SIZE_LARGE_TOOLBAR);
1208 gtk_container_add (GTK_CONTAINER (ebox), window->throbber);
1209 gtk_widget_show (window->throbber);
1211 item = gtk_tool_item_new ();
1212 gtk_container_add (GTK_CONTAINER (item), ebox);
1213 gtk_toolbar_insert (GTK_TOOLBAR (window->presence_toolbar), item, -1);
1214 gtk_widget_show (GTK_WIDGET (item));
1216 /* Set up contact list. */
1217 empathy_status_presets_get_all ();
1219 list_iface = EMPATHY_CONTACT_LIST (empathy_contact_manager_dup_singleton ());
1220 monitor = empathy_contact_list_get_monitor (list_iface);
1221 window->list_store = empathy_contact_list_store_new (list_iface);
1222 window->list_view = empathy_contact_list_view_new (window->list_store,
1223 EMPATHY_CONTACT_LIST_FEATURE_ALL,
1224 EMPATHY_CONTACT_FEATURE_ALL);
1225 g_signal_connect (monitor, "contact-presence-changed",
1226 G_CALLBACK (main_window_contact_presence_changed_cb), window);
1227 g_object_unref (list_iface);
1229 gtk_widget_show (GTK_WIDGET (window->list_view));
1230 gtk_container_add (GTK_CONTAINER (sw),
1231 GTK_WIDGET (window->list_view));
1232 g_signal_connect (window->list_view, "row-activated",
1233 G_CALLBACK (main_window_row_activated_cb),
1236 /* Load user-defined accelerators. */
1237 main_window_accels_load ();
1239 /* Set window size. */
1240 empathy_geometry_load (GEOMETRY_NAME, &x, &y, &w, &h);
1242 if (w >= 1 && h >= 1) {
1243 /* Use the defaults from the ui file if we
1244 * don't have good w, h geometry.
1246 DEBUG ("Configuring window default size w:%d, h:%d", w, h);
1247 gtk_window_set_default_size (GTK_WINDOW (window->window), w, h);
1250 if (x >= 0 && y >= 0) {
1251 /* Let the window manager position it if we
1252 * don't have good x, y coordinates.
1254 DEBUG ("Configuring window default position x:%d, y:%d", x, y);
1255 gtk_window_move (GTK_WINDOW (window->window), x, y);
1258 /* Enable event handling */
1259 window->event_manager = empathy_event_manager_dup_singleton ();
1260 g_signal_connect (window->event_manager, "event-added",
1261 G_CALLBACK (main_window_event_added_cb),
1263 g_signal_connect (window->event_manager, "event-removed",
1264 G_CALLBACK (main_window_event_removed_cb),
1267 g_signal_connect (window->account_manager, "account-created",
1268 G_CALLBACK (main_window_account_created_or_deleted_cb),
1270 g_signal_connect (window->account_manager, "account-deleted",
1271 G_CALLBACK (main_window_account_created_or_deleted_cb),
1273 main_window_account_created_or_deleted_cb (window->account_manager, NULL, window);
1275 l = empathy_event_manager_get_events (window->event_manager);
1277 main_window_event_added_cb (window->event_manager,
1282 conf = empathy_conf_get ();
1284 /* Show offline ? */
1285 empathy_conf_get_bool (conf,
1286 EMPATHY_PREFS_CONTACTS_SHOW_OFFLINE,
1288 empathy_conf_notify_add (conf,
1289 EMPATHY_PREFS_CONTACTS_SHOW_OFFLINE,
1290 main_window_notify_show_offline_cb,
1291 show_offline_widget);
1293 gtk_check_menu_item_set_active (GTK_CHECK_MENU_ITEM (show_offline_widget),
1296 /* Show avatars ? */
1297 empathy_conf_get_bool (conf,
1298 EMPATHY_PREFS_UI_SHOW_AVATARS,
1300 empathy_conf_notify_add (conf,
1301 EMPATHY_PREFS_UI_SHOW_AVATARS,
1302 (EmpathyConfNotifyFunc) main_window_notify_show_avatars_cb,
1304 empathy_contact_list_store_set_show_avatars (window->list_store, show_avatars);
1307 empathy_conf_get_bool (conf,
1308 EMPATHY_PREFS_UI_COMPACT_CONTACT_LIST,
1309 &compact_contact_list);
1310 empathy_conf_notify_add (conf,
1311 EMPATHY_PREFS_UI_COMPACT_CONTACT_LIST,
1312 (EmpathyConfNotifyFunc) main_window_notify_compact_contact_list_cb,
1314 empathy_contact_list_store_set_is_compact (window->list_store, compact_contact_list);
1316 /* Sort criterium */
1317 empathy_conf_notify_add (conf,
1318 EMPATHY_PREFS_CONTACTS_SORT_CRITERIUM,
1319 (EmpathyConfNotifyFunc) main_window_notify_sort_criterium_cb,
1321 main_window_notify_sort_criterium_cb (conf,
1322 EMPATHY_PREFS_CONTACTS_SORT_CRITERIUM,
1325 main_window_update_status (window, window->account_manager);
1327 return window->window;