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-status-presets.h>
40 #include <libempathy-gtk/empathy-contact-dialogs.h>
41 #include <libempathy-gtk/empathy-contact-list-store.h>
42 #include <libempathy-gtk/empathy-contact-list-view.h>
43 #include <libempathy-gtk/empathy-presence-chooser.h>
44 #include <libempathy-gtk/empathy-ui-utils.h>
45 #include <libempathy-gtk/empathy-geometry.h>
46 #include <libempathy-gtk/empathy-conf.h>
47 #include <libempathy-gtk/empathy-log-window.h>
48 #include <libempathy-gtk/empathy-new-message-dialog.h>
49 #include <libempathy-gtk/empathy-gtk-enum-types.h>
51 #include <libmissioncontrol/mission-control.h>
53 #include "empathy-accounts-dialog.h"
54 #include "empathy-main-window.h"
55 #include "ephy-spinner.h"
56 #include "empathy-preferences.h"
57 #include "empathy-about-dialog.h"
58 #include "empathy-new-chatroom-dialog.h"
59 #include "empathy-chatrooms-window.h"
60 #include "empathy-event-manager.h"
62 #define DEBUG_FLAG EMPATHY_DEBUG_OTHER
63 #include <libempathy/empathy-debug.h>
65 /* Flashing delay for icons (milliseconds). */
66 #define FLASH_TIMEOUT 500
68 /* Minimum width of roster window if something goes wrong. */
71 /* Accels (menu shortcuts) can be configured and saved */
72 #define ACCELS_FILENAME "accels.txt"
74 /* Name in the geometry file */
75 #define GEOMETRY_NAME "main-window"
78 EmpathyContactListView *list_view;
79 EmpathyContactListStore *list_store;
81 EmpathyAccountManager *account_manager;
82 EmpathyChatroomManager *chatroom_manager;
83 EmpathyEventManager *event_manager;
84 guint flash_timeout_id;
90 GtkWidget *presence_toolbar;
91 GtkWidget *presence_chooser;
92 GtkWidget *errors_vbox;
94 GtkUIManager *ui_manager;
95 GtkAction *chat_history;
96 GtkAction *room_join_favorites;
98 GtkWidget *room_separator;
99 GtkWidget *edit_context;
100 GtkWidget *edit_context_separator;
102 guint size_timeout_id;
105 /* Actions that are enabled when there are connected accounts */
106 GList *actions_connected;
109 static EmpathyMainWindow *window = NULL;
112 main_window_flash_stop (EmpathyMainWindow *window)
114 if (window->flash_timeout_id == 0) {
118 DEBUG ("Stop flashing");
119 g_source_remove (window->flash_timeout_id);
120 window->flash_timeout_id = 0;
121 window->flash_on = FALSE;
130 main_window_flash_foreach (GtkTreeModel *model,
135 FlashForeachData *data = (FlashForeachData*) user_data;
136 EmpathyContact *contact;
137 const gchar *icon_name;
138 GtkTreePath *parent_path = NULL;
139 GtkTreeIter parent_iter;
141 /* To be used with gtk_tree_model_foreach, update the status icon
142 * of the contact to show the event icon (on=TRUE) or the presence
144 gtk_tree_model_get (model, iter,
145 EMPATHY_CONTACT_LIST_STORE_COL_CONTACT, &contact,
148 if (contact != data->event->contact) {
150 g_object_unref (contact);
156 icon_name = data->event->icon_name;
158 icon_name = empathy_icon_name_for_contact (contact);
161 gtk_tree_store_set (GTK_TREE_STORE (model), iter,
162 EMPATHY_CONTACT_LIST_STORE_COL_ICON_STATUS, icon_name,
165 /* To make sure the parent is shown correctly, we emit
166 * the row-changed signal on the parent so it prompts
167 * it to be refreshed by the filter func.
169 if (gtk_tree_model_iter_parent (model, &parent_iter, iter)) {
170 parent_path = gtk_tree_model_get_path (model, &parent_iter);
173 gtk_tree_model_row_changed (model, parent_path, &parent_iter);
174 gtk_tree_path_free (parent_path);
177 g_object_unref (contact);
183 main_window_flash_cb (EmpathyMainWindow *window)
187 gboolean found_event = FALSE;
188 FlashForeachData data;
190 window->flash_on = !window->flash_on;
191 data.on = window->flash_on;
192 model = GTK_TREE_MODEL (window->list_store);
194 events = empathy_event_manager_get_events (window->event_manager);
195 for (l = events; l; l = l->next) {
196 data.event = l->data;
197 if (!data.event->contact) {
202 gtk_tree_model_foreach (model,
203 main_window_flash_foreach,
208 main_window_flash_stop (window);
215 main_window_flash_start (EmpathyMainWindow *window)
217 if (window->flash_timeout_id != 0) {
221 DEBUG ("Start flashing");
222 window->flash_timeout_id = g_timeout_add (FLASH_TIMEOUT,
223 (GSourceFunc) main_window_flash_cb,
225 main_window_flash_cb (window);
229 main_window_event_added_cb (EmpathyEventManager *manager,
231 EmpathyMainWindow *window)
233 if (event->contact) {
234 main_window_flash_start (window);
239 main_window_event_removed_cb (EmpathyEventManager *manager,
241 EmpathyMainWindow *window)
243 FlashForeachData data;
245 if (!event->contact) {
251 gtk_tree_model_foreach (GTK_TREE_MODEL (window->list_store),
252 main_window_flash_foreach,
257 main_window_row_activated_cb (EmpathyContactListView *view,
259 GtkTreeViewColumn *col,
260 EmpathyMainWindow *window)
262 EmpathyContact *contact;
267 model = GTK_TREE_MODEL (window->list_store);
268 gtk_tree_model_get_iter (model, &iter, path);
269 gtk_tree_model_get (model, &iter,
270 EMPATHY_CONTACT_LIST_STORE_COL_CONTACT, &contact,
277 /* If the contact has an event activate it, otherwise the
278 * default handler of row-activated will be called. */
279 events = empathy_event_manager_get_events (window->event_manager);
280 for (l = events; l; l = l->next) {
281 EmpathyEvent *event = l->data;
283 if (event->contact == contact) {
284 DEBUG ("Activate event");
285 empathy_event_activate (event);
287 /* We don't want the default handler of this signal
288 * (e.g. open a chat) */
289 g_signal_stop_emission_by_name (view, "row-activated");
294 g_object_unref (contact);
298 main_window_error_edit_clicked_cb (GtkButton *button,
299 EmpathyMainWindow *window)
302 GtkWidget *error_widget;
304 account = g_object_get_data (G_OBJECT (button), "account");
305 empathy_accounts_dialog_show (GTK_WINDOW (window->window), account);
307 error_widget = g_hash_table_lookup (window->errors, account);
308 gtk_widget_destroy (error_widget);
309 g_hash_table_remove (window->errors, account);
313 main_window_error_clear_clicked_cb (GtkButton *button,
314 EmpathyMainWindow *window)
317 GtkWidget *error_widget;
319 account = g_object_get_data (G_OBJECT (button), "account");
320 error_widget = g_hash_table_lookup (window->errors, account);
321 gtk_widget_destroy (error_widget);
322 g_hash_table_remove (window->errors, account);
326 main_window_error_display (EmpathyMainWindow *window,
328 const gchar *message)
333 GtkWidget *button_edit;
334 GtkWidget *alignment;
339 GtkWidget *button_close;
342 child = g_hash_table_lookup (window->errors, account);
344 label = g_object_get_data (G_OBJECT (child), "label");
346 /* Just set the latest error and return */
347 str = g_markup_printf_escaped ("<b>%s</b>\n%s",
348 mc_account_get_display_name (account),
350 gtk_label_set_markup (GTK_LABEL (label), str);
356 child = gtk_vbox_new (FALSE, 0);
357 gtk_box_pack_start (GTK_BOX (window->errors_vbox), child, FALSE, TRUE, 0);
358 gtk_container_set_border_width (GTK_CONTAINER (child), 6);
359 gtk_widget_show (child);
361 table = gtk_table_new (2, 4, FALSE);
362 gtk_widget_show (table);
363 gtk_box_pack_start (GTK_BOX (child), table, TRUE, TRUE, 0);
364 gtk_table_set_row_spacings (GTK_TABLE (table), 12);
365 gtk_table_set_col_spacings (GTK_TABLE (table), 6);
367 image = gtk_image_new_from_stock (GTK_STOCK_DISCONNECT, GTK_ICON_SIZE_MENU);
368 gtk_widget_show (image);
369 gtk_table_attach (GTK_TABLE (table), image, 0, 1, 0, 2,
370 (GtkAttachOptions) (GTK_FILL),
371 (GtkAttachOptions) (GTK_FILL), 0, 0);
372 gtk_misc_set_alignment (GTK_MISC (image), 0.5, 0);
374 button_edit = gtk_button_new ();
375 gtk_widget_show (button_edit);
376 gtk_table_attach (GTK_TABLE (table), button_edit, 1, 2, 1, 2,
377 (GtkAttachOptions) (GTK_FILL),
378 (GtkAttachOptions) (0), 0, 0);
380 alignment = gtk_alignment_new (0.5, 0.5, 0, 0);
381 gtk_widget_show (alignment);
382 gtk_container_add (GTK_CONTAINER (button_edit), alignment);
384 hbox = gtk_hbox_new (FALSE, 2);
385 gtk_widget_show (hbox);
386 gtk_container_add (GTK_CONTAINER (alignment), hbox);
388 image = gtk_image_new_from_stock (GTK_STOCK_EDIT, GTK_ICON_SIZE_BUTTON);
389 gtk_widget_show (image);
390 gtk_box_pack_start (GTK_BOX (hbox), image, FALSE, FALSE, 0);
392 label = gtk_label_new_with_mnemonic (_("_Edit account"));
393 gtk_widget_show (label);
394 gtk_box_pack_start (GTK_BOX (hbox), label, FALSE, FALSE, 0);
396 fixed = gtk_fixed_new ();
397 gtk_widget_show (fixed);
398 gtk_table_attach (GTK_TABLE (table), fixed, 2, 3, 1, 2,
399 (GtkAttachOptions) (GTK_EXPAND | GTK_FILL),
400 (GtkAttachOptions) (GTK_FILL), 0, 0);
402 vbox = gtk_vbox_new (FALSE, 6);
403 gtk_widget_show (vbox);
404 gtk_table_attach (GTK_TABLE (table), vbox, 3, 4, 0, 2,
405 (GtkAttachOptions) (GTK_FILL),
406 (GtkAttachOptions) (GTK_FILL), 0, 0);
408 button_close = gtk_button_new ();
409 gtk_widget_show (button_close);
410 gtk_box_pack_start (GTK_BOX (vbox), button_close, FALSE, FALSE, 0);
411 gtk_button_set_relief (GTK_BUTTON (button_close), GTK_RELIEF_NONE);
414 image = gtk_image_new_from_stock ("gtk-close", GTK_ICON_SIZE_MENU);
415 gtk_widget_show (image);
416 gtk_container_add (GTK_CONTAINER (button_close), image);
418 label = gtk_label_new ("");
419 gtk_widget_show (label);
420 gtk_table_attach (GTK_TABLE (table), label, 1, 3, 0, 1,
421 (GtkAttachOptions) (GTK_EXPAND | GTK_SHRINK | GTK_FILL),
422 (GtkAttachOptions) (GTK_EXPAND | GTK_SHRINK | GTK_FILL), 0, 0);
423 gtk_widget_set_size_request (label, 175, -1);
424 gtk_label_set_use_markup (GTK_LABEL (label), TRUE);
425 gtk_label_set_line_wrap (GTK_LABEL (label), TRUE);
426 gtk_misc_set_alignment (GTK_MISC (label), 0, 0);
428 str = g_markup_printf_escaped ("<b>%s</b>\n%s",
429 mc_account_get_display_name (account),
431 gtk_label_set_markup (GTK_LABEL (label), str);
434 g_object_set_data (G_OBJECT (child), "label", label);
435 g_object_set_data_full (G_OBJECT (button_edit),
436 "account", g_object_ref (account),
438 g_object_set_data_full (G_OBJECT (button_close),
439 "account", g_object_ref (account),
442 g_signal_connect (button_edit, "clicked",
443 G_CALLBACK (main_window_error_edit_clicked_cb),
446 g_signal_connect (button_close, "clicked",
447 G_CALLBACK (main_window_error_clear_clicked_cb),
450 gtk_widget_show (window->errors_vbox);
452 g_hash_table_insert (window->errors, g_object_ref (account), child);
456 main_window_update_status (EmpathyMainWindow *window, EmpathyAccountManager *manager)
462 /* Count number of connected/connecting/disconnected accounts */
463 connected = empathy_account_manager_get_connected_accounts (manager);
464 connecting = empathy_account_manager_get_connecting_accounts (manager);
466 /* Update the spinner state */
467 if (connecting > 0) {
468 ephy_spinner_start (EPHY_SPINNER (window->throbber));
470 ephy_spinner_stop (EPHY_SPINNER (window->throbber));
473 /* Update widgets sensibility */
474 for (l = window->actions_connected; l; l = l->next) {
475 gtk_action_set_sensitive (l->data, (connected > 0));
480 main_window_connection_changed_cb (EmpathyAccountManager *manager,
482 TpConnectionStatusReason reason,
483 TpConnectionStatus current,
484 TpConnectionStatus previous,
485 EmpathyMainWindow *window)
487 main_window_update_status (window, manager);
489 if (current == TP_CONNECTION_STATUS_DISCONNECTED &&
490 reason != TP_CONNECTION_STATUS_REASON_REQUESTED) {
491 const gchar *message;
494 case TP_CONNECTION_STATUS_REASON_NONE_SPECIFIED:
495 message = _("No error specified");
497 case TP_CONNECTION_STATUS_REASON_NETWORK_ERROR:
498 message = _("Network error");
500 case TP_CONNECTION_STATUS_REASON_AUTHENTICATION_FAILED:
501 message = _("Authentication failed");
503 case TP_CONNECTION_STATUS_REASON_ENCRYPTION_ERROR:
504 message = _("Encryption error");
506 case TP_CONNECTION_STATUS_REASON_NAME_IN_USE:
507 message = _("Name in use");
509 case TP_CONNECTION_STATUS_REASON_CERT_NOT_PROVIDED:
510 message = _("Certificate not provided");
512 case TP_CONNECTION_STATUS_REASON_CERT_UNTRUSTED:
513 message = _("Certificate untrusted");
515 case TP_CONNECTION_STATUS_REASON_CERT_EXPIRED:
516 message = _("Certificate expired");
518 case TP_CONNECTION_STATUS_REASON_CERT_NOT_ACTIVATED:
519 message = _("Certificate not activated");
521 case TP_CONNECTION_STATUS_REASON_CERT_HOSTNAME_MISMATCH:
522 message = _("Certificate hostname mismatch");
524 case TP_CONNECTION_STATUS_REASON_CERT_FINGERPRINT_MISMATCH:
525 message = _("Certificate fingerprint mismatch");
527 case TP_CONNECTION_STATUS_REASON_CERT_SELF_SIGNED:
528 message = _("Certificate self-signed");
530 case TP_CONNECTION_STATUS_REASON_CERT_OTHER_ERROR:
531 message = _("Certificate error");
534 message = _("Unknown error");
538 main_window_error_display (window, account, message);
541 if (current == TP_CONNECTION_STATUS_DISCONNECTED) {
542 empathy_sound_play (GTK_WIDGET (window->window),
543 EMPATHY_SOUND_ACCOUNT_DISCONNECTED);
546 if (current == TP_CONNECTION_STATUS_CONNECTED) {
547 GtkWidget *error_widget;
549 empathy_sound_play (GTK_WIDGET (window->window),
550 EMPATHY_SOUND_ACCOUNT_CONNECTED);
552 /* Account connected without error, remove error message if any */
553 error_widget = g_hash_table_lookup (window->errors, account);
555 gtk_widget_destroy (error_widget);
556 g_hash_table_remove (window->errors, account);
562 main_window_contact_presence_changed_cb (EmpathyContactMonitor *monitor,
563 EmpathyContact *contact,
566 EmpathyMainWindow *window)
569 gboolean should_play;
571 account = empathy_contact_get_account (contact);
572 should_play = !empathy_account_manager_is_account_just_connected (window->account_manager, account);
578 if (previous < MC_PRESENCE_AVAILABLE && current > MC_PRESENCE_OFFLINE) {
579 /* someone is logging in */
580 empathy_sound_play (GTK_WIDGET (window->window),
581 EMPATHY_SOUND_CONTACT_CONNECTED);
585 if (previous > MC_PRESENCE_OFFLINE && current < MC_PRESENCE_AVAILABLE) {
586 /* someone is logging off */
587 empathy_sound_play (GTK_WIDGET (window->window),
588 EMPATHY_SOUND_CONTACT_DISCONNECTED);
593 main_window_accels_load (void)
597 filename = g_build_filename (g_get_home_dir (), ".gnome2", PACKAGE_NAME, ACCELS_FILENAME, NULL);
598 if (g_file_test (filename, G_FILE_TEST_EXISTS)) {
599 DEBUG ("Loading from:'%s'", filename);
600 gtk_accel_map_load (filename);
607 main_window_accels_save (void)
610 gchar *file_with_path;
612 dir = g_build_filename (g_get_home_dir (), ".gnome2", PACKAGE_NAME, NULL);
613 g_mkdir_with_parents (dir, S_IRUSR | S_IWUSR | S_IXUSR);
614 file_with_path = g_build_filename (dir, ACCELS_FILENAME, NULL);
617 DEBUG ("Saving to:'%s'", file_with_path);
618 gtk_accel_map_save (file_with_path);
620 g_free (file_with_path);
624 main_window_destroy_cb (GtkWidget *widget,
625 EmpathyMainWindow *window)
627 /* Save user-defined accelerators. */
628 main_window_accels_save ();
630 g_signal_handlers_disconnect_by_func (window->account_manager,
631 main_window_connection_changed_cb,
634 if (window->size_timeout_id) {
635 g_source_remove (window->size_timeout_id);
638 g_list_free (window->actions_connected);
640 g_object_unref (window->mc);
641 g_object_unref (window->account_manager);
642 g_object_unref (window->list_store);
643 g_hash_table_destroy (window->errors);
645 g_signal_handlers_disconnect_by_func (window->event_manager,
646 main_window_event_added_cb,
648 g_signal_handlers_disconnect_by_func (window->event_manager,
649 main_window_event_removed_cb,
651 g_object_unref (window->event_manager);
652 g_object_unref (window->ui_manager);
658 main_window_chat_quit_cb (GtkAction *action,
659 EmpathyMainWindow *window)
665 main_window_chat_history_cb (GtkAction *action,
666 EmpathyMainWindow *window)
668 empathy_log_window_show (NULL, NULL, FALSE, GTK_WINDOW (window->window));
672 main_window_chat_new_message_cb (GtkAction *action,
673 EmpathyMainWindow *window)
675 empathy_new_message_dialog_show (GTK_WINDOW (window->window));
679 main_window_chat_add_contact_cb (GtkAction *action,
680 EmpathyMainWindow *window)
682 empathy_new_contact_dialog_show (GTK_WINDOW (window->window));
686 main_window_chat_show_offline_cb (GtkToggleAction *action,
687 EmpathyMainWindow *window)
691 current = gtk_toggle_action_get_active (action);
692 empathy_conf_set_bool (empathy_conf_get (),
693 EMPATHY_PREFS_CONTACTS_SHOW_OFFLINE,
696 /* Turn off sound just while we alter the contact list. */
697 // FIXME: empathy_sound_set_enabled (FALSE);
698 empathy_contact_list_store_set_show_offline (window->list_store, current);
699 //empathy_sound_set_enabled (TRUE);
703 main_window_favorite_chatroom_join (EmpathyChatroom *chatroom)
705 EmpathyAccountManager *manager;
707 TpConnection *connection;
710 manager = empathy_account_manager_dup_singleton ();
711 account = empathy_chatroom_get_account (chatroom);
712 connection = empathy_account_manager_get_connection (manager, account);
713 room = empathy_chatroom_get_room (chatroom);
714 g_object_unref (manager);
716 if (connection != NULL) {
717 DEBUG ("Requesting channel for '%s'", room);
718 empathy_dispatcher_join_muc (connection, room, NULL, NULL);
723 main_window_favorite_chatroom_menu_activate_cb (GtkMenuItem *menu_item,
724 EmpathyChatroom *chatroom)
726 main_window_favorite_chatroom_join (chatroom);
730 main_window_favorite_chatroom_menu_add (EmpathyMainWindow *window,
731 EmpathyChatroom *chatroom)
733 GtkWidget *menu_item;
736 if (g_object_get_data (G_OBJECT (chatroom), "menu_item")) {
740 name = empathy_chatroom_get_name (chatroom);
741 menu_item = gtk_menu_item_new_with_label (name);
743 g_object_set_data (G_OBJECT (chatroom), "menu_item", menu_item);
744 g_signal_connect (menu_item, "activate",
745 G_CALLBACK (main_window_favorite_chatroom_menu_activate_cb),
748 gtk_menu_shell_insert (GTK_MENU_SHELL (window->room_menu),
751 gtk_widget_show (menu_item);
755 main_window_favorite_chatroom_menu_added_cb (EmpathyChatroomManager *manager,
756 EmpathyChatroom *chatroom,
757 EmpathyMainWindow *window)
759 main_window_favorite_chatroom_menu_add (window, chatroom);
760 gtk_widget_show (window->room_separator);
761 gtk_action_set_sensitive (window->room_join_favorites, TRUE);
765 main_window_favorite_chatroom_menu_removed_cb (EmpathyChatroomManager *manager,
766 EmpathyChatroom *chatroom,
767 EmpathyMainWindow *window)
769 GtkWidget *menu_item;
772 menu_item = g_object_get_data (G_OBJECT (chatroom), "menu_item");
773 g_object_set_data (G_OBJECT (chatroom), "menu_item", NULL);
774 gtk_widget_destroy (menu_item);
776 chatrooms = empathy_chatroom_manager_get_chatrooms (window->chatroom_manager, NULL);
778 gtk_widget_show (window->room_separator);
780 gtk_widget_hide (window->room_separator);
783 gtk_action_set_sensitive (window->room_join_favorites, chatrooms != NULL);
784 g_list_free (chatrooms);
788 main_window_favorite_chatroom_menu_setup (EmpathyMainWindow *window)
790 GList *chatrooms, *l;
793 window->chatroom_manager = empathy_chatroom_manager_dup_singleton (NULL);
794 chatrooms = empathy_chatroom_manager_get_chatrooms (window->chatroom_manager, NULL);
795 room = gtk_ui_manager_get_widget (window->ui_manager,
797 window->room_menu = gtk_menu_item_get_submenu (GTK_MENU_ITEM (room));
798 window->room_separator = gtk_ui_manager_get_widget (window->ui_manager,
799 "/menubar/room/room_separator");
801 for (l = chatrooms; l; l = l->next) {
802 main_window_favorite_chatroom_menu_add (window, l->data);
806 gtk_widget_hide (window->room_separator);
809 gtk_action_set_sensitive (window->room_join_favorites, chatrooms != NULL);
811 g_signal_connect (window->chatroom_manager, "chatroom-added",
812 G_CALLBACK (main_window_favorite_chatroom_menu_added_cb),
814 g_signal_connect (window->chatroom_manager, "chatroom-removed",
815 G_CALLBACK (main_window_favorite_chatroom_menu_removed_cb),
818 g_list_free (chatrooms);
822 main_window_room_join_new_cb (GtkAction *action,
823 EmpathyMainWindow *window)
825 empathy_new_chatroom_dialog_show (GTK_WINDOW (window->window));
829 main_window_room_join_favorites_cb (GtkAction *action,
830 EmpathyMainWindow *window)
832 GList *chatrooms, *l;
834 chatrooms = empathy_chatroom_manager_get_chatrooms (window->chatroom_manager, NULL);
835 for (l = chatrooms; l; l = l->next) {
836 main_window_favorite_chatroom_join (l->data);
838 g_list_free (chatrooms);
842 main_window_room_manage_favorites_cb (GtkAction *action,
843 EmpathyMainWindow *window)
845 empathy_chatrooms_window_show (GTK_WINDOW (window->window));
849 main_window_edit_cb (GtkAction *action,
850 EmpathyMainWindow *window)
854 /* FIXME: It should use the UIManager to merge the contact/group submenu */
855 submenu = empathy_contact_list_view_get_contact_menu (window->list_view);
860 item = GTK_MENU_ITEM (window->edit_context);
861 label = gtk_bin_get_child (GTK_BIN (item));
862 gtk_label_set_text (GTK_LABEL (label), _("Contact"));
864 gtk_widget_show (window->edit_context);
865 gtk_widget_show (window->edit_context_separator);
867 gtk_menu_item_set_submenu (item, submenu);
872 submenu = empathy_contact_list_view_get_group_menu (window->list_view);
877 item = GTK_MENU_ITEM (window->edit_context);
878 label = gtk_bin_get_child (GTK_BIN (item));
879 gtk_label_set_text (GTK_LABEL (label), _("Group"));
881 gtk_widget_show (window->edit_context);
882 gtk_widget_show (window->edit_context_separator);
884 gtk_menu_item_set_submenu (item, submenu);
889 gtk_widget_hide (window->edit_context);
890 gtk_widget_hide (window->edit_context_separator);
896 main_window_edit_accounts_cb (GtkAction *action,
897 EmpathyMainWindow *window)
899 empathy_accounts_dialog_show (GTK_WINDOW (window->window), NULL);
903 main_window_edit_personal_information_cb (GtkAction *action,
904 EmpathyMainWindow *window)
906 empathy_contact_personal_dialog_show (GTK_WINDOW (window->window));
910 main_window_edit_preferences_cb (GtkAction *action,
911 EmpathyMainWindow *window)
913 empathy_preferences_show (GTK_WINDOW (window->window));
917 main_window_help_about_cb (GtkAction *action,
918 EmpathyMainWindow *window)
920 empathy_about_dialog_new (GTK_WINDOW (window->window));
924 main_window_help_contents_cb (GtkAction *action,
925 EmpathyMainWindow *window)
927 empathy_url_show (window->window, "ghelp:empathy");
931 main_window_throbber_button_press_event_cb (GtkWidget *throbber_ebox,
932 GdkEventButton *event,
933 EmpathyMainWindow *window)
935 if (event->type != GDK_BUTTON_PRESS ||
936 event->button != 1) {
940 empathy_accounts_dialog_show (GTK_WINDOW (window->window), NULL);
946 main_window_configure_event_timeout_cb (EmpathyMainWindow *window)
950 gtk_window_get_size (GTK_WINDOW (window->window), &w, &h);
951 gtk_window_get_position (GTK_WINDOW (window->window), &x, &y);
953 empathy_geometry_save (GEOMETRY_NAME, x, y, w, h);
955 window->size_timeout_id = 0;
961 main_window_configure_event_cb (GtkWidget *widget,
962 GdkEventConfigure *event,
963 EmpathyMainWindow *window)
965 if (window->size_timeout_id) {
966 g_source_remove (window->size_timeout_id);
969 window->size_timeout_id = g_timeout_add_seconds (1,
970 (GSourceFunc) main_window_configure_event_timeout_cb,
977 main_window_account_created_or_deleted_cb (EmpathyAccountManager *manager,
979 EmpathyMainWindow *window)
981 gtk_action_set_sensitive (window->chat_history,
982 empathy_account_manager_get_count (manager) > 0);
986 main_window_notify_show_offline_cb (EmpathyConf *conf,
988 gpointer toggle_action)
990 gboolean show_offline;
992 if (empathy_conf_get_bool (conf, key, &show_offline)) {
993 gtk_toggle_action_set_active (toggle_action, show_offline);
998 main_window_notify_show_avatars_cb (EmpathyConf *conf,
1000 EmpathyMainWindow *window)
1002 gboolean show_avatars;
1004 if (empathy_conf_get_bool (conf, key, &show_avatars)) {
1005 empathy_contact_list_store_set_show_avatars (window->list_store,
1011 main_window_notify_compact_contact_list_cb (EmpathyConf *conf,
1013 EmpathyMainWindow *window)
1015 gboolean compact_contact_list;
1017 if (empathy_conf_get_bool (conf, key, &compact_contact_list)) {
1018 empathy_contact_list_store_set_is_compact (window->list_store,
1019 compact_contact_list);
1024 main_window_notify_sort_criterium_cb (EmpathyConf *conf,
1026 EmpathyMainWindow *window)
1030 if (empathy_conf_get_string (conf, key, &str) && str) {
1032 GEnumClass *enum_class;
1033 GEnumValue *enum_value;
1035 type = empathy_contact_list_store_sort_get_type ();
1036 enum_class = G_ENUM_CLASS (g_type_class_peek (type));
1037 enum_value = g_enum_get_value_by_nick (enum_class, str);
1041 empathy_contact_list_store_set_sort_criterium (window->list_store,
1048 main_window_connection_items_setup (EmpathyMainWindow *window,
1054 const gchar *actions_connected[] = {
1058 "edit_personal_information"
1061 for (i = 0, list = NULL; i < G_N_ELEMENTS (actions_connected); i++) {
1062 action = gtk_builder_get_object (gui, actions_connected[i]);
1063 list = g_list_prepend (list, action);
1066 window->actions_connected = list;
1070 empathy_main_window_get (void)
1072 return window != NULL ? window->window : NULL;
1076 empathy_main_window_show (void)
1078 EmpathyContactList *list_iface;
1079 EmpathyContactMonitor *monitor;
1083 GtkToggleAction *show_offline_widget;
1086 gboolean show_offline;
1087 gboolean show_avatars;
1088 gboolean compact_contact_list;
1094 empathy_window_present (GTK_WINDOW (window->window), TRUE);
1095 return window->window;
1098 window = g_new0 (EmpathyMainWindow, 1);
1100 /* Set up interface */
1101 filename = empathy_file_lookup ("empathy-main-window.ui", "src");
1102 gui = empathy_builder_get_file (filename,
1103 "main_window", &window->window,
1104 "main_vbox", &window->main_vbox,
1105 "errors_vbox", &window->errors_vbox,
1106 "ui_manager", &window->ui_manager,
1107 "chat_show_offline", &show_offline_widget,
1108 "chat_history", &window->chat_history,
1109 "room_join_favorites", &window->room_join_favorites,
1110 "presence_toolbar", &window->presence_toolbar,
1111 "roster_scrolledwindow", &sw,
1115 empathy_builder_connect (gui, window,
1116 "main_window", "destroy", main_window_destroy_cb,
1117 "main_window", "configure_event", main_window_configure_event_cb,
1118 "chat_quit", "activate", main_window_chat_quit_cb,
1119 "chat_new_message", "activate", main_window_chat_new_message_cb,
1120 "chat_history", "activate", main_window_chat_history_cb,
1121 "room_join_new", "activate", main_window_room_join_new_cb,
1122 "room_join_favorites", "activate", main_window_room_join_favorites_cb,
1123 "room_manage_favorites", "activate", main_window_room_manage_favorites_cb,
1124 "chat_add_contact", "activate", main_window_chat_add_contact_cb,
1125 "chat_show_offline", "toggled", main_window_chat_show_offline_cb,
1126 "edit", "activate", main_window_edit_cb,
1127 "edit_accounts", "activate", main_window_edit_accounts_cb,
1128 "edit_personal_information", "activate", main_window_edit_personal_information_cb,
1129 "edit_preferences", "activate", main_window_edit_preferences_cb,
1130 "help_about", "activate", main_window_help_about_cb,
1131 "help_contents", "activate", main_window_help_contents_cb,
1134 /* Set up connection related widgets. */
1135 main_window_connection_items_setup (window, gui);
1137 g_object_ref (window->ui_manager);
1138 g_object_unref (gui);
1140 window->mc = empathy_mission_control_dup_singleton ();
1141 window->account_manager = empathy_account_manager_dup_singleton ();
1143 g_signal_connect (window->account_manager,
1144 "account-connection-changed",
1145 G_CALLBACK (main_window_connection_changed_cb), window);
1147 window->errors = g_hash_table_new_full (empathy_account_hash,
1148 empathy_account_equal,
1153 main_window_favorite_chatroom_menu_setup (window);
1155 window->edit_context = gtk_ui_manager_get_widget (window->ui_manager,
1156 "/menubar/edit/edit_context");
1157 window->edit_context_separator = gtk_ui_manager_get_widget (window->ui_manager,
1158 "/menubar/edit/edit_context_separator");
1159 gtk_widget_hide (window->edit_context);
1160 gtk_widget_hide (window->edit_context_separator);
1162 /* Set up contact list. */
1163 empathy_status_presets_get_all ();
1165 /* Set up presence chooser */
1166 window->presence_chooser = empathy_presence_chooser_new ();
1167 gtk_widget_show (window->presence_chooser);
1168 item = gtk_tool_item_new ();
1169 gtk_widget_show (GTK_WIDGET (item));
1170 gtk_container_add (GTK_CONTAINER (item), window->presence_chooser);
1171 gtk_tool_item_set_is_important (item, TRUE);
1172 gtk_tool_item_set_expand (item, TRUE);
1173 gtk_toolbar_insert (GTK_TOOLBAR (window->presence_toolbar), item, -1);
1175 /* Set up the throbber */
1176 ebox = gtk_event_box_new ();
1177 gtk_event_box_set_visible_window (GTK_EVENT_BOX (ebox), FALSE);
1178 gtk_widget_set_tooltip_text (ebox, _("Show and edit accounts"));
1179 g_signal_connect (ebox,
1180 "button-press-event",
1181 G_CALLBACK (main_window_throbber_button_press_event_cb),
1183 gtk_widget_show (ebox);
1185 window->throbber = ephy_spinner_new ();
1186 ephy_spinner_set_size (EPHY_SPINNER (window->throbber), GTK_ICON_SIZE_LARGE_TOOLBAR);
1187 gtk_container_add (GTK_CONTAINER (ebox), window->throbber);
1188 gtk_widget_show (window->throbber);
1190 item = gtk_tool_item_new ();
1191 gtk_container_add (GTK_CONTAINER (item), ebox);
1192 gtk_toolbar_insert (GTK_TOOLBAR (window->presence_toolbar), item, -1);
1193 gtk_widget_show (GTK_WIDGET (item));
1195 list_iface = EMPATHY_CONTACT_LIST (empathy_contact_manager_dup_singleton ());
1196 monitor = empathy_contact_list_get_monitor (list_iface);
1197 window->list_store = empathy_contact_list_store_new (list_iface);
1198 window->list_view = empathy_contact_list_view_new (window->list_store,
1199 EMPATHY_CONTACT_LIST_FEATURE_ALL,
1200 EMPATHY_CONTACT_FEATURE_ALL);
1201 g_signal_connect (monitor, "contact-presence-changed",
1202 G_CALLBACK (main_window_contact_presence_changed_cb), window);
1203 g_object_unref (list_iface);
1205 gtk_widget_show (GTK_WIDGET (window->list_view));
1206 gtk_container_add (GTK_CONTAINER (sw),
1207 GTK_WIDGET (window->list_view));
1208 g_signal_connect (window->list_view, "row-activated",
1209 G_CALLBACK (main_window_row_activated_cb),
1212 /* Load user-defined accelerators. */
1213 main_window_accels_load ();
1215 /* Set window size. */
1216 empathy_geometry_load (GEOMETRY_NAME, &x, &y, &w, &h);
1218 if (w >= 1 && h >= 1) {
1219 /* Use the defaults from the ui file if we
1220 * don't have good w, h geometry.
1222 DEBUG ("Configuring window default size w:%d, h:%d", w, h);
1223 gtk_window_set_default_size (GTK_WINDOW (window->window), w, h);
1226 if (x >= 0 && y >= 0) {
1227 /* Let the window manager position it if we
1228 * don't have good x, y coordinates.
1230 DEBUG ("Configuring window default position x:%d, y:%d", x, y);
1231 gtk_window_move (GTK_WINDOW (window->window), x, y);
1234 /* Enable event handling */
1235 window->event_manager = empathy_event_manager_dup_singleton ();
1236 g_signal_connect (window->event_manager, "event-added",
1237 G_CALLBACK (main_window_event_added_cb),
1239 g_signal_connect (window->event_manager, "event-removed",
1240 G_CALLBACK (main_window_event_removed_cb),
1243 g_signal_connect (window->account_manager, "account-created",
1244 G_CALLBACK (main_window_account_created_or_deleted_cb),
1246 g_signal_connect (window->account_manager, "account-deleted",
1247 G_CALLBACK (main_window_account_created_or_deleted_cb),
1249 main_window_account_created_or_deleted_cb (window->account_manager, NULL, window);
1251 l = empathy_event_manager_get_events (window->event_manager);
1253 main_window_event_added_cb (window->event_manager,
1258 conf = empathy_conf_get ();
1260 /* Show offline ? */
1261 empathy_conf_get_bool (conf,
1262 EMPATHY_PREFS_CONTACTS_SHOW_OFFLINE,
1264 empathy_conf_notify_add (conf,
1265 EMPATHY_PREFS_CONTACTS_SHOW_OFFLINE,
1266 main_window_notify_show_offline_cb,
1267 show_offline_widget);
1269 gtk_toggle_action_set_active (show_offline_widget, show_offline);
1271 /* Show avatars ? */
1272 empathy_conf_get_bool (conf,
1273 EMPATHY_PREFS_UI_SHOW_AVATARS,
1275 empathy_conf_notify_add (conf,
1276 EMPATHY_PREFS_UI_SHOW_AVATARS,
1277 (EmpathyConfNotifyFunc) main_window_notify_show_avatars_cb,
1279 empathy_contact_list_store_set_show_avatars (window->list_store, show_avatars);
1282 empathy_conf_get_bool (conf,
1283 EMPATHY_PREFS_UI_COMPACT_CONTACT_LIST,
1284 &compact_contact_list);
1285 empathy_conf_notify_add (conf,
1286 EMPATHY_PREFS_UI_COMPACT_CONTACT_LIST,
1287 (EmpathyConfNotifyFunc) main_window_notify_compact_contact_list_cb,
1289 empathy_contact_list_store_set_is_compact (window->list_store, compact_contact_list);
1291 /* Sort criterium */
1292 empathy_conf_notify_add (conf,
1293 EMPATHY_PREFS_CONTACTS_SORT_CRITERIUM,
1294 (EmpathyConfNotifyFunc) main_window_notify_sort_criterium_cb,
1296 main_window_notify_sort_criterium_cb (conf,
1297 EMPATHY_PREFS_CONTACTS_SORT_CRITERIUM,
1300 main_window_update_status (window, window->account_manager);
1302 return window->window;