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 <glade/glade.h>
29 #include <glib/gi18n.h>
31 #include <libempathy/empathy-contact.h>
32 #include <libempathy/empathy-utils.h>
33 #include <libempathy/empathy-account-manager.h>
34 #include <libempathy/empathy-dispatcher.h>
35 #include <libempathy/empathy-chatroom-manager.h>
36 #include <libempathy/empathy-chatroom.h>
37 #include <libempathy/empathy-contact-list.h>
38 #include <libempathy/empathy-contact-manager.h>
39 #include <libempathy/empathy-contact-factory.h>
40 #include <libempathy/empathy-status-presets.h>
42 #include <libempathy-gtk/empathy-contact-dialogs.h>
43 #include <libempathy-gtk/empathy-contact-list-store.h>
44 #include <libempathy-gtk/empathy-contact-list-view.h>
45 #include <libempathy-gtk/empathy-presence-chooser.h>
46 #include <libempathy-gtk/empathy-ui-utils.h>
47 #include <libempathy-gtk/empathy-geometry.h>
48 #include <libempathy-gtk/empathy-conf.h>
49 #include <libempathy-gtk/empathy-log-window.h>
50 #include <libempathy-gtk/empathy-new-message-dialog.h>
51 #include <libempathy-gtk/empathy-gtk-enum-types.h>
53 #include <libmissioncontrol/mission-control.h>
55 #include "empathy-accounts-dialog.h"
56 #include "empathy-main-window.h"
57 #include "ephy-spinner.h"
58 #include "empathy-preferences.h"
59 #include "empathy-about-dialog.h"
60 #include "empathy-new-chatroom-dialog.h"
61 #include "empathy-chatrooms-window.h"
62 #include "empathy-event-manager.h"
64 #define DEBUG_FLAG EMPATHY_DEBUG_OTHER
65 #include <libempathy/empathy-debug.h>
67 /* Flashing delay for icons (milliseconds). */
68 #define FLASH_TIMEOUT 500
70 /* Minimum width of roster window if something goes wrong. */
73 /* Accels (menu shortcuts) can be configured and saved */
74 #define ACCELS_FILENAME "accels.txt"
76 /* Name in the geometry file */
77 #define GEOMETRY_NAME "main-window"
80 EmpathyContactListView *list_view;
81 EmpathyContactListStore *list_store;
83 EmpathyAccountManager *account_manager;
84 EmpathyChatroomManager *chatroom_manager;
85 EmpathyEventManager *event_manager;
86 guint flash_timeout_id;
92 GtkWidget *presence_toolbar;
93 GtkWidget *presence_chooser;
94 GtkWidget *errors_vbox;
99 GtkWidget *room_join_favorites;
100 GtkWidget *edit_context;
101 GtkWidget *edit_context_separator;
102 GtkWidget *chat_history_menu_item;
104 guint size_timeout_id;
107 /* Widgets that are enabled when there are connected accounts */
108 GList *widgets_connected;
111 static EmpathyMainWindow *window = NULL;
113 static void main_window_destroy_cb (GtkWidget *widget,
114 EmpathyMainWindow *window);
115 static void main_window_favorite_chatroom_menu_setup (EmpathyMainWindow *window);
116 static void main_window_favorite_chatroom_menu_added_cb (EmpathyChatroomManager *manager,
117 EmpathyChatroom *chatroom,
118 EmpathyMainWindow *window);
119 static void main_window_favorite_chatroom_menu_removed_cb (EmpathyChatroomManager *manager,
120 EmpathyChatroom *chatroom,
121 EmpathyMainWindow *window);
122 static void main_window_favorite_chatroom_menu_activate_cb (GtkMenuItem *menu_item,
123 EmpathyChatroom *chatroom);
124 static void main_window_favorite_chatroom_menu_update (EmpathyMainWindow *window);
125 static void main_window_favorite_chatroom_menu_add (EmpathyMainWindow *window,
126 EmpathyChatroom *chatroom);
127 static void main_window_favorite_chatroom_join (EmpathyChatroom *chatroom);
128 static void main_window_chat_quit_cb (GtkWidget *widget,
129 EmpathyMainWindow *window);
130 static void main_window_chat_new_message_cb (GtkWidget *widget,
131 EmpathyMainWindow *window);
132 static void main_window_chat_history_cb (GtkWidget *widget,
133 EmpathyMainWindow *window);
134 static void main_window_room_join_new_cb (GtkWidget *widget,
135 EmpathyMainWindow *window);
136 static void main_window_room_join_favorites_cb (GtkWidget *widget,
137 EmpathyMainWindow *window);
138 static void main_window_room_manage_favorites_cb (GtkWidget *widget,
139 EmpathyMainWindow *window);
140 static void main_window_chat_add_contact_cb (GtkWidget *widget,
141 EmpathyMainWindow *window);
142 static void main_window_chat_show_offline_cb (GtkCheckMenuItem *item,
143 EmpathyMainWindow *window);
144 static gboolean main_window_edit_button_press_event_cb (GtkWidget *widget,
145 GdkEventButton *event,
146 EmpathyMainWindow *window);
147 static void main_window_edit_accounts_cb (GtkWidget *widget,
148 EmpathyMainWindow *window);
149 static void main_window_edit_personal_information_cb (GtkWidget *widget,
150 EmpathyMainWindow *window);
151 static void main_window_edit_preferences_cb (GtkWidget *widget,
152 EmpathyMainWindow *window);
153 static void main_window_help_about_cb (GtkWidget *widget,
154 EmpathyMainWindow *window);
155 static void main_window_help_contents_cb (GtkWidget *widget,
156 EmpathyMainWindow *window);
157 static gboolean main_window_throbber_button_press_event_cb (GtkWidget *throbber_ebox,
158 GdkEventButton *event,
159 EmpathyMainWindow *window);
160 static void main_window_update_status (EmpathyMainWindow *window,
161 EmpathyAccountManager *manager);
162 static void main_window_error_display (EmpathyMainWindow *window,
164 const gchar *message);
165 static void main_window_accels_load (void);
166 static void main_window_accels_save (void);
167 static void main_window_connection_items_setup (EmpathyMainWindow *window,
169 static gboolean main_window_configure_event_timeout_cb (EmpathyMainWindow *window);
170 static gboolean main_window_configure_event_cb (GtkWidget *widget,
171 GdkEventConfigure *event,
172 EmpathyMainWindow *window);
173 static void main_window_notify_show_offline_cb (EmpathyConf *conf,
175 gpointer check_menu_item);
176 static void main_window_notify_show_avatars_cb (EmpathyConf *conf,
178 EmpathyMainWindow *window);
179 static void main_window_notify_compact_contact_list_cb (EmpathyConf *conf,
181 EmpathyMainWindow *window);
182 static void main_window_notify_sort_criterium_cb (EmpathyConf *conf,
184 EmpathyMainWindow *window);
185 static void main_window_account_created_or_deleted_cb (EmpathyAccountManager *manager,
187 EmpathyMainWindow *window);
190 main_window_flash_stop (EmpathyMainWindow *window)
192 if (window->flash_timeout_id == 0) {
196 DEBUG ("Stop flashing");
197 g_source_remove (window->flash_timeout_id);
198 window->flash_timeout_id = 0;
199 window->flash_on = FALSE;
208 main_window_flash_foreach (GtkTreeModel *model,
213 FlashForeachData *data = (FlashForeachData*) user_data;
214 EmpathyContact *contact;
215 const gchar *icon_name;
216 GtkTreePath *parent_path = NULL;
217 GtkTreeIter parent_iter;
219 /* To be used with gtk_tree_model_foreach, update the status icon
220 * of the contact to show the event icon (on=TRUE) or the presence
222 gtk_tree_model_get (model, iter,
223 EMPATHY_CONTACT_LIST_STORE_COL_CONTACT, &contact,
226 if (contact != data->event->contact) {
228 g_object_unref (contact);
234 icon_name = data->event->icon_name;
236 icon_name = empathy_icon_name_for_contact (contact);
239 gtk_tree_store_set (GTK_TREE_STORE (model), iter,
240 EMPATHY_CONTACT_LIST_STORE_COL_ICON_STATUS, icon_name,
243 /* To make sure the parent is shown correctly, we emit
244 * the row-changed signal on the parent so it prompts
245 * it to be refreshed by the filter func.
247 if (gtk_tree_model_iter_parent (model, &parent_iter, iter)) {
248 parent_path = gtk_tree_model_get_path (model, &parent_iter);
251 gtk_tree_model_row_changed (model, parent_path, &parent_iter);
252 gtk_tree_path_free (parent_path);
255 g_object_unref (contact);
261 main_window_flash_cb (EmpathyMainWindow *window)
265 gboolean found_event = FALSE;
266 FlashForeachData data;
268 window->flash_on = !window->flash_on;
269 data.on = window->flash_on;
270 model = GTK_TREE_MODEL (window->list_store);
272 events = empathy_event_manager_get_events (window->event_manager);
273 for (l = events; l; l = l->next) {
274 data.event = l->data;
275 if (!data.event->contact) {
280 gtk_tree_model_foreach (model,
281 main_window_flash_foreach,
286 main_window_flash_stop (window);
293 main_window_flash_start (EmpathyMainWindow *window)
295 empathy_sound_play (GTK_WIDGET (window->window),
296 EMPATHY_SOUND_CONVERSATION_NEW);
298 if (window->flash_timeout_id != 0) {
302 DEBUG ("Start flashing");
303 window->flash_timeout_id = g_timeout_add (FLASH_TIMEOUT,
304 (GSourceFunc) main_window_flash_cb,
306 main_window_flash_cb (window);
310 main_window_event_added_cb (EmpathyEventManager *manager,
312 EmpathyMainWindow *window)
314 if (event->contact) {
315 main_window_flash_start (window);
320 main_window_event_removed_cb (EmpathyEventManager *manager,
322 EmpathyMainWindow *window)
324 FlashForeachData data;
326 if (!event->contact) {
332 gtk_tree_model_foreach (GTK_TREE_MODEL (window->list_store),
333 main_window_flash_foreach,
338 main_window_row_activated_cb (EmpathyContactListView *view,
340 GtkTreeViewColumn *col,
341 EmpathyMainWindow *window)
343 EmpathyContact *contact;
348 model = GTK_TREE_MODEL (window->list_store);
349 gtk_tree_model_get_iter (model, &iter, path);
350 gtk_tree_model_get (model, &iter,
351 EMPATHY_CONTACT_LIST_STORE_COL_CONTACT, &contact,
358 /* If the contact has an event activate it, otherwise the
359 * default handler of row-activated will be called. */
360 events = empathy_event_manager_get_events (window->event_manager);
361 for (l = events; l; l = l->next) {
362 EmpathyEvent *event = l->data;
364 if (event->contact == contact) {
365 DEBUG ("Activate event");
366 empathy_event_activate (event);
368 /* We don't want the default handler of this signal
369 * (e.g. open a chat) */
370 g_signal_stop_emission_by_name (view, "row-activated");
375 g_object_unref (contact);
379 main_window_connection_changed_cb (EmpathyAccountManager *manager,
381 TpConnectionStatusReason reason,
382 TpConnectionStatus current,
383 TpConnectionStatus previous,
384 EmpathyMainWindow *window)
386 main_window_update_status (window, manager);
388 if (current == TP_CONNECTION_STATUS_DISCONNECTED &&
389 reason != TP_CONNECTION_STATUS_REASON_REQUESTED) {
390 const gchar *message;
393 case TP_CONNECTION_STATUS_REASON_NONE_SPECIFIED:
394 message = _("No error specified");
396 case TP_CONNECTION_STATUS_REASON_NETWORK_ERROR:
397 message = _("Network error");
399 case TP_CONNECTION_STATUS_REASON_AUTHENTICATION_FAILED:
400 message = _("Authentication failed");
402 case TP_CONNECTION_STATUS_REASON_ENCRYPTION_ERROR:
403 message = _("Encryption error");
405 case TP_CONNECTION_STATUS_REASON_NAME_IN_USE:
406 message = _("Name in use");
408 case TP_CONNECTION_STATUS_REASON_CERT_NOT_PROVIDED:
409 message = _("Certificate not provided");
411 case TP_CONNECTION_STATUS_REASON_CERT_UNTRUSTED:
412 message = _("Certificate untrusted");
414 case TP_CONNECTION_STATUS_REASON_CERT_EXPIRED:
415 message = _("Certificate expired");
417 case TP_CONNECTION_STATUS_REASON_CERT_NOT_ACTIVATED:
418 message = _("Certificate not activated");
420 case TP_CONNECTION_STATUS_REASON_CERT_HOSTNAME_MISMATCH:
421 message = _("Certificate hostname mismatch");
423 case TP_CONNECTION_STATUS_REASON_CERT_FINGERPRINT_MISMATCH:
424 message = _("Certificate fingerprint mismatch");
426 case TP_CONNECTION_STATUS_REASON_CERT_SELF_SIGNED:
427 message = _("Certificate self-signed");
429 case TP_CONNECTION_STATUS_REASON_CERT_OTHER_ERROR:
430 message = _("Certificate error");
433 message = _("Unknown error");
437 main_window_error_display (window, account, message);
440 if (current == TP_CONNECTION_STATUS_DISCONNECTED) {
441 empathy_sound_play (GTK_WIDGET (window->window),
442 EMPATHY_SOUND_ACCOUNT_DISCONNECTED);
445 if (current == TP_CONNECTION_STATUS_CONNECTED) {
446 GtkWidget *error_widget;
448 empathy_sound_play (GTK_WIDGET (window->window),
449 EMPATHY_SOUND_ACCOUNT_CONNECTED);
451 /* Account connected without error, remove error message if any */
452 error_widget = g_hash_table_lookup (window->errors, account);
454 gtk_widget_destroy (error_widget);
455 g_hash_table_remove (window->errors, account);
461 main_window_contact_presence_changed_cb (EmpathyContactMonitor *monitor,
462 EmpathyContact *contact,
465 EmpathyMainWindow *window)
468 gboolean should_play;
470 account = empathy_contact_get_account (contact);
471 should_play = !empathy_account_manager_is_account_just_connected (window->account_manager, account);
477 if (previous < MC_PRESENCE_AVAILABLE && current > MC_PRESENCE_OFFLINE) {
478 /* someone is logging in */
479 empathy_sound_play (GTK_WIDGET (window->window),
480 EMPATHY_SOUND_CONTACT_CONNECTED);
484 if (previous > MC_PRESENCE_OFFLINE && current < MC_PRESENCE_AVAILABLE) {
485 /* someone is logging off */
486 empathy_sound_play (GTK_WIDGET (window->window),
487 EMPATHY_SOUND_CONTACT_DISCONNECTED);
492 empathy_main_window_get (void)
494 return window != NULL ? window->window : NULL;
498 empathy_main_window_show (void)
500 EmpathyContactList *list_iface;
501 EmpathyContactMonitor *monitor;
505 GtkWidget *show_offline_widget;
508 gboolean show_offline;
509 gboolean show_avatars;
510 gboolean compact_contact_list;
516 empathy_window_present (GTK_WINDOW (window->window), TRUE);
517 return window->window;
520 window = g_new0 (EmpathyMainWindow, 1);
522 /* Set up interface */
523 filename = empathy_file_lookup ("empathy-main-window.glade", "src");
524 glade = empathy_glade_get_file (filename,
527 "main_window", &window->window,
528 "main_vbox", &window->main_vbox,
529 "errors_vbox", &window->errors_vbox,
530 "chat_show_offline", &show_offline_widget,
531 "room", &window->room,
532 "room_sep", &window->room_sep,
533 "room_join_favorites", &window->room_join_favorites,
534 "edit_context", &window->edit_context,
535 "edit_context_separator", &window->edit_context_separator,
536 "presence_toolbar", &window->presence_toolbar,
537 "roster_scrolledwindow", &sw,
538 "chat_history", &window->chat_history_menu_item,
542 empathy_glade_connect (glade,
544 "main_window", "destroy", main_window_destroy_cb,
545 "main_window", "configure_event", main_window_configure_event_cb,
546 "chat_quit", "activate", main_window_chat_quit_cb,
547 "chat_new_message", "activate", main_window_chat_new_message_cb,
548 "chat_history", "activate", main_window_chat_history_cb,
549 "room_join_new", "activate", main_window_room_join_new_cb,
550 "room_join_favorites", "activate", main_window_room_join_favorites_cb,
551 "room_manage_favorites", "activate", main_window_room_manage_favorites_cb,
552 "chat_add_contact", "activate", main_window_chat_add_contact_cb,
553 "chat_show_offline", "toggled", main_window_chat_show_offline_cb,
554 "edit", "button-press-event", main_window_edit_button_press_event_cb,
555 "edit_accounts", "activate", main_window_edit_accounts_cb,
556 "edit_personal_information", "activate", main_window_edit_personal_information_cb,
557 "edit_preferences", "activate", main_window_edit_preferences_cb,
558 "help_about", "activate", main_window_help_about_cb,
559 "help_contents", "activate", main_window_help_contents_cb,
562 /* Set up connection related widgets. */
563 main_window_connection_items_setup (window, glade);
564 g_object_unref (glade);
566 window->mc = empathy_mission_control_dup_singleton ();
567 window->account_manager = empathy_account_manager_dup_singleton ();
569 g_signal_connect (window->account_manager,
570 "account-connection-changed",
571 G_CALLBACK (main_window_connection_changed_cb), window);
573 window->errors = g_hash_table_new_full (empathy_account_hash,
574 empathy_account_equal,
579 main_window_favorite_chatroom_menu_setup (window);
581 gtk_widget_hide (window->edit_context);
582 gtk_widget_hide (window->edit_context_separator);
584 /* Set up presence chooser */
585 window->presence_chooser = empathy_presence_chooser_new ();
586 gtk_widget_show (window->presence_chooser);
587 item = gtk_tool_item_new ();
588 gtk_widget_show (GTK_WIDGET (item));
589 gtk_container_add (GTK_CONTAINER (item), window->presence_chooser);
590 gtk_tool_item_set_is_important (item, TRUE);
591 gtk_tool_item_set_expand (item, TRUE);
592 gtk_toolbar_insert (GTK_TOOLBAR (window->presence_toolbar), item, -1);
594 /* Set up the throbber */
595 ebox = gtk_event_box_new ();
596 gtk_event_box_set_visible_window (GTK_EVENT_BOX (ebox), FALSE);
597 gtk_widget_set_tooltip_text (ebox, _("Show and edit accounts"));
598 g_signal_connect (ebox,
599 "button-press-event",
600 G_CALLBACK (main_window_throbber_button_press_event_cb),
602 gtk_widget_show (ebox);
604 window->throbber = ephy_spinner_new ();
605 ephy_spinner_set_size (EPHY_SPINNER (window->throbber), GTK_ICON_SIZE_LARGE_TOOLBAR);
606 gtk_container_add (GTK_CONTAINER (ebox), window->throbber);
607 gtk_widget_show (window->throbber);
609 item = gtk_tool_item_new ();
610 gtk_container_add (GTK_CONTAINER (item), ebox);
611 gtk_toolbar_insert (GTK_TOOLBAR (window->presence_toolbar), item, -1);
612 gtk_widget_show (GTK_WIDGET (item));
614 /* Set up contact list. */
615 empathy_status_presets_get_all ();
617 list_iface = EMPATHY_CONTACT_LIST (empathy_contact_manager_dup_singleton ());
618 monitor = empathy_contact_list_get_monitor (list_iface);
619 window->list_store = empathy_contact_list_store_new (list_iface);
620 window->list_view = empathy_contact_list_view_new (window->list_store,
621 EMPATHY_CONTACT_LIST_FEATURE_ALL,
622 EMPATHY_CONTACT_FEATURE_ALL);
623 g_signal_connect (monitor, "contact-presence-changed",
624 G_CALLBACK (main_window_contact_presence_changed_cb), window);
625 g_object_unref (list_iface);
627 gtk_widget_show (GTK_WIDGET (window->list_view));
628 gtk_container_add (GTK_CONTAINER (sw),
629 GTK_WIDGET (window->list_view));
630 g_signal_connect (window->list_view, "row-activated",
631 G_CALLBACK (main_window_row_activated_cb),
634 /* Load user-defined accelerators. */
635 main_window_accels_load ();
637 /* Set window size. */
638 empathy_geometry_load (GEOMETRY_NAME, &x, &y, &w, &h);
640 if (w >= 1 && h >= 1) {
641 /* Use the defaults from the glade file if we
642 * don't have good w, h geometry.
644 DEBUG ("Configuring window default size w:%d, h:%d", w, h);
645 gtk_window_set_default_size (GTK_WINDOW (window->window), w, h);
648 if (x >= 0 && y >= 0) {
649 /* Let the window manager position it if we
650 * don't have good x, y coordinates.
652 DEBUG ("Configuring window default position x:%d, y:%d", x, y);
653 gtk_window_move (GTK_WINDOW (window->window), x, y);
656 /* Enable event handling */
657 window->event_manager = empathy_event_manager_dup_singleton ();
658 g_signal_connect (window->event_manager, "event-added",
659 G_CALLBACK (main_window_event_added_cb),
661 g_signal_connect (window->event_manager, "event-removed",
662 G_CALLBACK (main_window_event_removed_cb),
665 g_signal_connect (window->account_manager, "account-created",
666 G_CALLBACK (main_window_account_created_or_deleted_cb),
668 g_signal_connect (window->account_manager, "account-deleted",
669 G_CALLBACK (main_window_account_created_or_deleted_cb),
671 main_window_account_created_or_deleted_cb (window->account_manager, NULL, window);
673 l = empathy_event_manager_get_events (window->event_manager);
675 main_window_event_added_cb (window->event_manager,
680 conf = empathy_conf_get ();
683 empathy_conf_get_bool (conf,
684 EMPATHY_PREFS_CONTACTS_SHOW_OFFLINE,
686 empathy_conf_notify_add (conf,
687 EMPATHY_PREFS_CONTACTS_SHOW_OFFLINE,
688 main_window_notify_show_offline_cb,
689 show_offline_widget);
691 gtk_check_menu_item_set_active (GTK_CHECK_MENU_ITEM (show_offline_widget),
695 empathy_conf_get_bool (conf,
696 EMPATHY_PREFS_UI_SHOW_AVATARS,
698 empathy_conf_notify_add (conf,
699 EMPATHY_PREFS_UI_SHOW_AVATARS,
700 (EmpathyConfNotifyFunc) main_window_notify_show_avatars_cb,
702 empathy_contact_list_store_set_show_avatars (window->list_store, show_avatars);
705 empathy_conf_get_bool (conf,
706 EMPATHY_PREFS_UI_COMPACT_CONTACT_LIST,
707 &compact_contact_list);
708 empathy_conf_notify_add (conf,
709 EMPATHY_PREFS_UI_COMPACT_CONTACT_LIST,
710 (EmpathyConfNotifyFunc) main_window_notify_compact_contact_list_cb,
712 empathy_contact_list_store_set_is_compact (window->list_store, compact_contact_list);
715 empathy_conf_notify_add (conf,
716 EMPATHY_PREFS_CONTACTS_SORT_CRITERIUM,
717 (EmpathyConfNotifyFunc) main_window_notify_sort_criterium_cb,
719 main_window_notify_sort_criterium_cb (conf,
720 EMPATHY_PREFS_CONTACTS_SORT_CRITERIUM,
723 main_window_update_status (window, window->account_manager);
725 return window->window;
729 main_window_destroy_cb (GtkWidget *widget,
730 EmpathyMainWindow *window)
732 /* Save user-defined accelerators. */
733 main_window_accels_save ();
735 g_signal_handlers_disconnect_by_func (window->account_manager,
736 main_window_connection_changed_cb,
739 if (window->size_timeout_id) {
740 g_source_remove (window->size_timeout_id);
743 g_list_free (window->widgets_connected);
745 g_object_unref (window->mc);
746 g_object_unref (window->account_manager);
747 g_object_unref (window->list_store);
748 g_hash_table_destroy (window->errors);
750 g_signal_handlers_disconnect_by_func (window->event_manager,
751 main_window_event_added_cb,
753 g_signal_handlers_disconnect_by_func (window->event_manager,
754 main_window_event_removed_cb,
756 g_object_unref (window->event_manager);
762 main_window_favorite_chatroom_menu_setup (EmpathyMainWindow *window)
764 GList *chatrooms, *l;
766 window->chatroom_manager = empathy_chatroom_manager_dup_singleton (NULL);
767 chatrooms = empathy_chatroom_manager_get_chatrooms (window->chatroom_manager, NULL);
768 window->room_menu = gtk_menu_item_get_submenu (GTK_MENU_ITEM (window->room));
770 for (l = chatrooms; l; l = l->next) {
771 main_window_favorite_chatroom_menu_add (window, l->data);
775 gtk_widget_hide (window->room_sep);
778 gtk_widget_set_sensitive (window->room_join_favorites, chatrooms != NULL);
780 g_signal_connect (window->chatroom_manager, "chatroom-added",
781 G_CALLBACK (main_window_favorite_chatroom_menu_added_cb),
783 g_signal_connect (window->chatroom_manager, "chatroom-removed",
784 G_CALLBACK (main_window_favorite_chatroom_menu_removed_cb),
787 g_list_free (chatrooms);
791 main_window_favorite_chatroom_menu_added_cb (EmpathyChatroomManager *manager,
792 EmpathyChatroom *chatroom,
793 EmpathyMainWindow *window)
795 main_window_favorite_chatroom_menu_add (window, chatroom);
796 gtk_widget_show (window->room_sep);
797 gtk_widget_set_sensitive (window->room_join_favorites, TRUE);
801 main_window_favorite_chatroom_menu_removed_cb (EmpathyChatroomManager *manager,
802 EmpathyChatroom *chatroom,
803 EmpathyMainWindow *window)
805 GtkWidget *menu_item;
807 menu_item = g_object_get_data (G_OBJECT (chatroom), "menu_item");
809 g_object_set_data (G_OBJECT (chatroom), "menu_item", NULL);
810 gtk_widget_destroy (menu_item);
812 main_window_favorite_chatroom_menu_update (window);
816 main_window_favorite_chatroom_menu_activate_cb (GtkMenuItem *menu_item,
817 EmpathyChatroom *chatroom)
819 main_window_favorite_chatroom_join (chatroom);
823 main_window_favorite_chatroom_menu_update (EmpathyMainWindow *window)
827 chatrooms = empathy_chatroom_manager_get_chatrooms (window->chatroom_manager, NULL);
830 gtk_widget_show (window->room_sep);
832 gtk_widget_hide (window->room_sep);
835 gtk_widget_set_sensitive (window->room_join_favorites, chatrooms != NULL);
836 g_list_free (chatrooms);
840 main_window_favorite_chatroom_menu_add (EmpathyMainWindow *window,
841 EmpathyChatroom *chatroom)
843 GtkWidget *menu_item;
846 if (g_object_get_data (G_OBJECT (chatroom), "menu_item")) {
850 name = empathy_chatroom_get_name (chatroom);
851 menu_item = gtk_menu_item_new_with_label (name);
853 g_object_set_data (G_OBJECT (chatroom), "menu_item", menu_item);
854 g_signal_connect (menu_item, "activate",
855 G_CALLBACK (main_window_favorite_chatroom_menu_activate_cb),
858 gtk_menu_shell_insert (GTK_MENU_SHELL (window->room_menu),
861 gtk_widget_show (menu_item);
865 main_window_favorite_chatroom_join (EmpathyChatroom *chatroom)
871 mc = empathy_mission_control_dup_singleton ();
872 account = empathy_chatroom_get_account (chatroom);
873 room = empathy_chatroom_get_room (chatroom);
875 if (mission_control_get_connection_status (mc, account, NULL) !=
876 TP_CONNECTION_STATUS_CONNECTED) {
880 DEBUG ("Requesting channel for '%s'", room);
881 empathy_dispatcher_join_muc (account, room, NULL, NULL);
887 main_window_chat_quit_cb (GtkWidget *widget,
888 EmpathyMainWindow *window)
894 main_window_chat_new_message_cb (GtkWidget *widget,
895 EmpathyMainWindow *window)
897 empathy_new_message_dialog_show (GTK_WINDOW (window->window));
901 main_window_chat_history_cb (GtkWidget *widget,
902 EmpathyMainWindow *window)
904 empathy_log_window_show (NULL, NULL, FALSE, GTK_WINDOW (window->window));
908 main_window_room_join_new_cb (GtkWidget *widget,
909 EmpathyMainWindow *window)
911 empathy_new_chatroom_dialog_show (GTK_WINDOW (window->window));
915 main_window_room_join_favorites_cb (GtkWidget *widget,
916 EmpathyMainWindow *window)
918 GList *chatrooms, *l;
920 chatrooms = empathy_chatroom_manager_get_chatrooms (window->chatroom_manager, NULL);
921 for (l = chatrooms; l; l = l->next) {
922 main_window_favorite_chatroom_join (l->data);
924 g_list_free (chatrooms);
928 main_window_room_manage_favorites_cb (GtkWidget *widget,
929 EmpathyMainWindow *window)
931 empathy_chatrooms_window_show (GTK_WINDOW (window->window));
935 main_window_chat_add_contact_cb (GtkWidget *widget,
936 EmpathyMainWindow *window)
938 empathy_new_contact_dialog_show (GTK_WINDOW (window->window));
942 main_window_chat_show_offline_cb (GtkCheckMenuItem *item,
943 EmpathyMainWindow *window)
947 current = gtk_check_menu_item_get_active (item);
949 empathy_conf_set_bool (empathy_conf_get (),
950 EMPATHY_PREFS_CONTACTS_SHOW_OFFLINE,
953 /* Turn off sound just while we alter the contact list. */
954 // FIXME: empathy_sound_set_enabled (FALSE);
955 empathy_contact_list_store_set_show_offline (window->list_store, current);
956 //empathy_sound_set_enabled (TRUE);
960 main_window_edit_button_press_event_cb (GtkWidget *widget,
961 GdkEventButton *event,
962 EmpathyMainWindow *window)
966 if (!event->button == 1) {
970 submenu = empathy_contact_list_view_get_contact_menu (window->list_view);
975 item = GTK_MENU_ITEM (window->edit_context);
976 label = gtk_bin_get_child (GTK_BIN (item));
977 gtk_label_set_text (GTK_LABEL (label), _("Contact"));
979 gtk_widget_show (window->edit_context);
980 gtk_widget_show (window->edit_context_separator);
982 gtk_menu_item_set_submenu (item, submenu);
987 submenu = empathy_contact_list_view_get_group_menu (window->list_view);
992 item = GTK_MENU_ITEM (window->edit_context);
993 label = gtk_bin_get_child (GTK_BIN (item));
994 gtk_label_set_text (GTK_LABEL (label), _("Group"));
996 gtk_widget_show (window->edit_context);
997 gtk_widget_show (window->edit_context_separator);
999 gtk_menu_item_set_submenu (item, submenu);
1004 gtk_widget_hide (window->edit_context);
1005 gtk_widget_hide (window->edit_context_separator);
1011 main_window_edit_accounts_cb (GtkWidget *widget,
1012 EmpathyMainWindow *window)
1014 empathy_accounts_dialog_show (GTK_WINDOW (window->window), NULL);
1018 main_window_edit_personal_information_cb (GtkWidget *widget,
1019 EmpathyMainWindow *window)
1023 accounts = mission_control_get_online_connections (window->mc, NULL);
1025 EmpathyContactFactory *factory;
1026 EmpathyContact *contact;
1029 account = accounts->data;
1030 factory = empathy_contact_factory_dup_singleton ();
1031 contact = empathy_contact_factory_get_user (factory, account);
1032 empathy_contact_run_until_ready (contact,
1033 EMPATHY_CONTACT_READY_HANDLE |
1034 EMPATHY_CONTACT_READY_ID,
1037 empathy_contact_information_dialog_show (contact,
1038 GTK_WINDOW (window->window),
1041 g_slist_foreach (accounts, (GFunc) g_object_unref, NULL);
1042 g_slist_free (accounts);
1043 g_object_unref (factory);
1044 g_object_unref (contact);
1049 main_window_edit_preferences_cb (GtkWidget *widget,
1050 EmpathyMainWindow *window)
1052 empathy_preferences_show (GTK_WINDOW (window->window));
1056 main_window_help_about_cb (GtkWidget *widget,
1057 EmpathyMainWindow *window)
1059 empathy_about_dialog_new (GTK_WINDOW (window->window));
1063 main_window_help_contents_cb (GtkWidget *widget,
1064 EmpathyMainWindow *window)
1066 empathy_url_show (widget, "ghelp:empathy");
1070 main_window_throbber_button_press_event_cb (GtkWidget *throbber_ebox,
1071 GdkEventButton *event,
1072 EmpathyMainWindow *window)
1074 if (event->type != GDK_BUTTON_PRESS ||
1075 event->button != 1) {
1079 empathy_accounts_dialog_show (GTK_WINDOW (window->window), NULL);
1085 main_window_error_edit_clicked_cb (GtkButton *button,
1086 EmpathyMainWindow *window)
1089 GtkWidget *error_widget;
1091 account = g_object_get_data (G_OBJECT (button), "account");
1092 empathy_accounts_dialog_show (GTK_WINDOW (window->window), account);
1094 error_widget = g_hash_table_lookup (window->errors, account);
1095 gtk_widget_destroy (error_widget);
1096 g_hash_table_remove (window->errors, account);
1100 main_window_error_clear_clicked_cb (GtkButton *button,
1101 EmpathyMainWindow *window)
1104 GtkWidget *error_widget;
1106 account = g_object_get_data (G_OBJECT (button), "account");
1107 error_widget = g_hash_table_lookup (window->errors, account);
1108 gtk_widget_destroy (error_widget);
1109 g_hash_table_remove (window->errors, account);
1113 main_window_error_display (EmpathyMainWindow *window,
1115 const gchar *message)
1120 GtkWidget *button_edit;
1121 GtkWidget *alignment;
1126 GtkWidget *button_close;
1129 child = g_hash_table_lookup (window->errors, account);
1131 label = g_object_get_data (G_OBJECT (child), "label");
1133 /* Just set the latest error and return */
1134 str = g_markup_printf_escaped ("<b>%s</b>\n%s",
1135 mc_account_get_display_name (account),
1137 gtk_label_set_markup (GTK_LABEL (label), str);
1143 child = gtk_vbox_new (FALSE, 0);
1144 gtk_box_pack_start (GTK_BOX (window->errors_vbox), child, FALSE, TRUE, 0);
1145 gtk_container_set_border_width (GTK_CONTAINER (child), 6);
1146 gtk_widget_show (child);
1148 table = gtk_table_new (2, 4, FALSE);
1149 gtk_widget_show (table);
1150 gtk_box_pack_start (GTK_BOX (child), table, TRUE, TRUE, 0);
1151 gtk_table_set_row_spacings (GTK_TABLE (table), 12);
1152 gtk_table_set_col_spacings (GTK_TABLE (table), 6);
1154 image = gtk_image_new_from_stock (GTK_STOCK_DISCONNECT, GTK_ICON_SIZE_MENU);
1155 gtk_widget_show (image);
1156 gtk_table_attach (GTK_TABLE (table), image, 0, 1, 0, 2,
1157 (GtkAttachOptions) (GTK_FILL),
1158 (GtkAttachOptions) (GTK_FILL), 0, 0);
1159 gtk_misc_set_alignment (GTK_MISC (image), 0.5, 0);
1161 button_edit = gtk_button_new ();
1162 gtk_widget_show (button_edit);
1163 gtk_table_attach (GTK_TABLE (table), button_edit, 1, 2, 1, 2,
1164 (GtkAttachOptions) (GTK_FILL),
1165 (GtkAttachOptions) (0), 0, 0);
1167 alignment = gtk_alignment_new (0.5, 0.5, 0, 0);
1168 gtk_widget_show (alignment);
1169 gtk_container_add (GTK_CONTAINER (button_edit), alignment);
1171 hbox = gtk_hbox_new (FALSE, 2);
1172 gtk_widget_show (hbox);
1173 gtk_container_add (GTK_CONTAINER (alignment), hbox);
1175 image = gtk_image_new_from_stock (GTK_STOCK_EDIT, GTK_ICON_SIZE_BUTTON);
1176 gtk_widget_show (image);
1177 gtk_box_pack_start (GTK_BOX (hbox), image, FALSE, FALSE, 0);
1179 label = gtk_label_new_with_mnemonic (_("_Edit account"));
1180 gtk_widget_show (label);
1181 gtk_box_pack_start (GTK_BOX (hbox), label, FALSE, FALSE, 0);
1183 fixed = gtk_fixed_new ();
1184 gtk_widget_show (fixed);
1185 gtk_table_attach (GTK_TABLE (table), fixed, 2, 3, 1, 2,
1186 (GtkAttachOptions) (GTK_EXPAND | GTK_FILL),
1187 (GtkAttachOptions) (GTK_FILL), 0, 0);
1189 vbox = gtk_vbox_new (FALSE, 6);
1190 gtk_widget_show (vbox);
1191 gtk_table_attach (GTK_TABLE (table), vbox, 3, 4, 0, 2,
1192 (GtkAttachOptions) (GTK_FILL),
1193 (GtkAttachOptions) (GTK_FILL), 0, 0);
1195 button_close = gtk_button_new ();
1196 gtk_widget_show (button_close);
1197 gtk_box_pack_start (GTK_BOX (vbox), button_close, FALSE, FALSE, 0);
1198 gtk_button_set_relief (GTK_BUTTON (button_close), GTK_RELIEF_NONE);
1201 image = gtk_image_new_from_stock ("gtk-close", GTK_ICON_SIZE_MENU);
1202 gtk_widget_show (image);
1203 gtk_container_add (GTK_CONTAINER (button_close), image);
1205 label = gtk_label_new ("");
1206 gtk_widget_show (label);
1207 gtk_table_attach (GTK_TABLE (table), label, 1, 3, 0, 1,
1208 (GtkAttachOptions) (GTK_EXPAND | GTK_SHRINK | GTK_FILL),
1209 (GtkAttachOptions) (GTK_EXPAND | GTK_SHRINK | GTK_FILL), 0, 0);
1210 gtk_widget_set_size_request (label, 175, -1);
1211 gtk_label_set_use_markup (GTK_LABEL (label), TRUE);
1212 gtk_label_set_line_wrap (GTK_LABEL (label), TRUE);
1213 gtk_misc_set_alignment (GTK_MISC (label), 0, 0);
1215 str = g_markup_printf_escaped ("<b>%s</b>\n%s",
1216 mc_account_get_display_name (account),
1218 gtk_label_set_markup (GTK_LABEL (label), str);
1221 g_object_set_data (G_OBJECT (child), "label", label);
1222 g_object_set_data_full (G_OBJECT (button_edit),
1223 "account", g_object_ref (account),
1225 g_object_set_data_full (G_OBJECT (button_close),
1226 "account", g_object_ref (account),
1229 g_signal_connect (button_edit, "clicked",
1230 G_CALLBACK (main_window_error_edit_clicked_cb),
1233 g_signal_connect (button_close, "clicked",
1234 G_CALLBACK (main_window_error_clear_clicked_cb),
1237 gtk_widget_show (window->errors_vbox);
1239 g_hash_table_insert (window->errors, g_object_ref (account), child);
1243 main_window_update_status (EmpathyMainWindow *window, EmpathyAccountManager *manager)
1249 /* Count number of connected/connecting/disconnected accounts */
1250 connected = empathy_account_manager_get_connected_accounts (manager);
1251 connecting = empathy_account_manager_get_connecting_accounts (manager);
1253 /* Update the spinner state */
1254 if (connecting > 0) {
1255 ephy_spinner_start (EPHY_SPINNER (window->throbber));
1257 ephy_spinner_stop (EPHY_SPINNER (window->throbber));
1260 /* Update widgets sensibility */
1261 for (l = window->widgets_connected; l; l = l->next) {
1262 gtk_widget_set_sensitive (l->data, (connected > 0));
1270 main_window_accels_load (void)
1274 filename = g_build_filename (g_get_home_dir (), ".gnome2", PACKAGE_NAME, ACCELS_FILENAME, NULL);
1275 if (g_file_test (filename, G_FILE_TEST_EXISTS)) {
1276 DEBUG ("Loading from:'%s'", filename);
1277 gtk_accel_map_load (filename);
1284 main_window_accels_save (void)
1287 gchar *file_with_path;
1289 dir = g_build_filename (g_get_home_dir (), ".gnome2", PACKAGE_NAME, NULL);
1290 g_mkdir_with_parents (dir, S_IRUSR | S_IWUSR | S_IXUSR);
1291 file_with_path = g_build_filename (dir, ACCELS_FILENAME, NULL);
1294 DEBUG ("Saving to:'%s'", file_with_path);
1295 gtk_accel_map_save (file_with_path);
1297 g_free (file_with_path);
1301 main_window_connection_items_setup (EmpathyMainWindow *window,
1307 const gchar *widgets_connected[] = {
1311 "edit_personal_information"
1314 for (i = 0, list = NULL; i < G_N_ELEMENTS (widgets_connected); i++) {
1315 w = glade_xml_get_widget (glade, widgets_connected[i]);
1316 list = g_list_prepend (list, w);
1319 window->widgets_connected = list;
1323 main_window_configure_event_timeout_cb (EmpathyMainWindow *window)
1327 gtk_window_get_size (GTK_WINDOW (window->window), &w, &h);
1328 gtk_window_get_position (GTK_WINDOW (window->window), &x, &y);
1330 empathy_geometry_save (GEOMETRY_NAME, x, y, w, h);
1332 window->size_timeout_id = 0;
1338 main_window_configure_event_cb (GtkWidget *widget,
1339 GdkEventConfigure *event,
1340 EmpathyMainWindow *window)
1342 if (window->size_timeout_id) {
1343 g_source_remove (window->size_timeout_id);
1346 window->size_timeout_id = g_timeout_add_seconds (1,
1347 (GSourceFunc) main_window_configure_event_timeout_cb,
1354 main_window_notify_show_offline_cb (EmpathyConf *conf,
1356 gpointer check_menu_item)
1358 gboolean show_offline;
1360 if (empathy_conf_get_bool (conf, key, &show_offline)) {
1361 gtk_check_menu_item_set_active (GTK_CHECK_MENU_ITEM (check_menu_item),
1367 main_window_notify_show_avatars_cb (EmpathyConf *conf,
1369 EmpathyMainWindow *window)
1371 gboolean show_avatars;
1373 if (empathy_conf_get_bool (conf, key, &show_avatars)) {
1374 empathy_contact_list_store_set_show_avatars (window->list_store,
1380 main_window_notify_compact_contact_list_cb (EmpathyConf *conf,
1382 EmpathyMainWindow *window)
1384 gboolean compact_contact_list;
1386 if (empathy_conf_get_bool (conf, key, &compact_contact_list)) {
1387 empathy_contact_list_store_set_is_compact (window->list_store,
1388 compact_contact_list);
1393 main_window_notify_sort_criterium_cb (EmpathyConf *conf,
1395 EmpathyMainWindow *window)
1399 if (empathy_conf_get_string (conf, key, &str) && str) {
1401 GEnumClass *enum_class;
1402 GEnumValue *enum_value;
1404 type = empathy_contact_list_store_sort_get_type ();
1405 enum_class = G_ENUM_CLASS (g_type_class_peek (type));
1406 enum_value = g_enum_get_value_by_nick (enum_class, str);
1410 empathy_contact_list_store_set_sort_criterium (window->list_store,
1417 main_window_account_created_or_deleted_cb (EmpathyAccountManager *manager,
1419 EmpathyMainWindow *window)
1421 gtk_widget_set_sensitive (GTK_WIDGET (window->chat_history_menu_item),
1422 empathy_account_manager_get_count (manager) > 0);