1 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
3 * Copyright (C) 2002-2007 Imendio AB
4 * Copyright (C) 2007-2008 Collabora Ltd.
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License as
8 * published by the Free Software Foundation; either version 2 of the
9 * License, or (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * General Public License for more details.
16 * You should have received a copy of the GNU General Public
17 * License along with this program; if not, write to the
18 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
19 * Boston, MA 02110-1301 USA
21 * Authors: Xavier Claessens <xclaesse@gmail.com>
28 #include <glib/gi18n.h>
30 #include <libempathy/empathy-contact.h>
31 #include <libempathy/empathy-utils.h>
32 #include <libempathy/empathy-account-manager.h>
33 #include <libempathy/empathy-dispatcher.h>
34 #include <libempathy/empathy-chatroom-manager.h>
35 #include <libempathy/empathy-chatroom.h>
36 #include <libempathy/empathy-contact-list.h>
37 #include <libempathy/empathy-contact-manager.h>
38 #include <libempathy/empathy-status-presets.h>
40 #include <libempathy-gtk/empathy-conf.h>
41 #include <libempathy-gtk/empathy-contact-dialogs.h>
42 #include <libempathy-gtk/empathy-contact-list-store.h>
43 #include <libempathy-gtk/empathy-contact-list-view.h>
44 #include <libempathy-gtk/empathy-geometry.h>
45 #include <libempathy-gtk/empathy-gtk-enum-types.h>
46 #include <libempathy-gtk/empathy-new-message-dialog.h>
47 #include <libempathy-gtk/empathy-log-window.h>
48 #include <libempathy-gtk/empathy-presence-chooser.h>
49 #include <libempathy-gtk/empathy-sound.h>
50 #include <libempathy-gtk/empathy-ui-utils.h>
52 #include "empathy-accounts-dialog.h"
53 #include "empathy-main-window.h"
54 #include "ephy-spinner.h"
55 #include "empathy-preferences.h"
56 #include "empathy-about-dialog.h"
57 #include "empathy-debug-window.h"
58 #include "empathy-new-chatroom-dialog.h"
59 #include "empathy-map-view.h"
60 #include "empathy-chatrooms-window.h"
61 #include "empathy-event-manager.h"
62 #include "empathy-ft-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;
82 EmpathyAccountManager *account_manager;
83 EmpathyChatroomManager *chatroom_manager;
84 EmpathyEventManager *event_manager;
85 guint flash_timeout_id;
91 GtkWidget *presence_toolbar;
92 GtkWidget *presence_chooser;
93 GtkWidget *errors_vbox;
95 GtkRadioAction *sort_by_name;
96 GtkRadioAction *sort_by_status;
97 GtkRadioAction *normal_with_avatars;
98 GtkRadioAction *normal_size;
99 GtkRadioAction *compact_size;
101 GtkUIManager *ui_manager;
102 GtkAction *view_history;
103 GtkAction *room_join_favorites;
104 GtkWidget *room_menu;
105 GtkWidget *room_separator;
106 GtkWidget *edit_context;
107 GtkWidget *edit_context_separator;
109 guint size_timeout_id;
112 /* Actions that are enabled when there are connected accounts */
113 GList *actions_connected;
116 static EmpathyMainWindow *window = NULL;
119 main_window_flash_stop (EmpathyMainWindow *window)
121 if (window->flash_timeout_id == 0) {
125 DEBUG ("Stop flashing");
126 g_source_remove (window->flash_timeout_id);
127 window->flash_timeout_id = 0;
128 window->flash_on = FALSE;
137 main_window_flash_foreach (GtkTreeModel *model,
142 FlashForeachData *data = (FlashForeachData *) user_data;
143 EmpathyContact *contact;
144 const gchar *icon_name;
145 GtkTreePath *parent_path = NULL;
146 GtkTreeIter parent_iter;
148 /* To be used with gtk_tree_model_foreach, update the status icon
149 * of the contact to show the event icon (on=TRUE) or the presence
151 gtk_tree_model_get (model, iter,
152 EMPATHY_CONTACT_LIST_STORE_COL_CONTACT, &contact,
155 if (contact != data->event->contact) {
157 g_object_unref (contact);
163 icon_name = data->event->icon_name;
165 icon_name = empathy_icon_name_for_contact (contact);
168 gtk_tree_store_set (GTK_TREE_STORE (model), iter,
169 EMPATHY_CONTACT_LIST_STORE_COL_ICON_STATUS, icon_name,
172 /* To make sure the parent is shown correctly, we emit
173 * the row-changed signal on the parent so it prompts
174 * it to be refreshed by the filter func.
176 if (gtk_tree_model_iter_parent (model, &parent_iter, iter)) {
177 parent_path = gtk_tree_model_get_path (model, &parent_iter);
180 gtk_tree_model_row_changed (model, parent_path, &parent_iter);
181 gtk_tree_path_free (parent_path);
184 g_object_unref (contact);
190 main_window_flash_cb (EmpathyMainWindow *window)
194 gboolean found_event = FALSE;
195 FlashForeachData data;
197 window->flash_on = !window->flash_on;
198 data.on = window->flash_on;
199 model = GTK_TREE_MODEL (window->list_store);
201 events = empathy_event_manager_get_events (window->event_manager);
202 for (l = events; l; l = l->next) {
203 data.event = l->data;
204 if (!data.event->contact || !data.event->must_ack) {
209 gtk_tree_model_foreach (model,
210 main_window_flash_foreach,
215 main_window_flash_stop (window);
222 main_window_flash_start (EmpathyMainWindow *window)
224 if (window->flash_timeout_id != 0) {
228 DEBUG ("Start flashing");
229 window->flash_timeout_id = g_timeout_add (FLASH_TIMEOUT,
230 (GSourceFunc) main_window_flash_cb,
232 main_window_flash_cb (window);
236 main_window_event_added_cb (EmpathyEventManager *manager,
238 EmpathyMainWindow *window)
240 if (event->contact) {
241 main_window_flash_start (window);
246 main_window_event_removed_cb (EmpathyEventManager *manager,
248 EmpathyMainWindow *window)
250 FlashForeachData data;
252 if (!event->contact) {
258 gtk_tree_model_foreach (GTK_TREE_MODEL (window->list_store),
259 main_window_flash_foreach,
264 main_window_row_activated_cb (EmpathyContactListView *view,
266 GtkTreeViewColumn *col,
267 EmpathyMainWindow *window)
269 EmpathyContact *contact;
274 model = GTK_TREE_MODEL (window->list_store);
275 gtk_tree_model_get_iter (model, &iter, path);
276 gtk_tree_model_get (model, &iter,
277 EMPATHY_CONTACT_LIST_STORE_COL_CONTACT, &contact,
284 /* If the contact has an event activate it, otherwise the
285 * default handler of row-activated will be called. */
286 events = empathy_event_manager_get_events (window->event_manager);
287 for (l = events; l; l = l->next) {
288 EmpathyEvent *event = l->data;
290 if (event->contact == contact) {
291 DEBUG ("Activate event");
292 empathy_event_activate (event);
294 /* We don't want the default handler of this signal
295 * (e.g. open a chat) */
296 g_signal_stop_emission_by_name (view, "row-activated");
301 g_object_unref (contact);
305 main_window_error_edit_clicked_cb (GtkButton *button,
306 EmpathyMainWindow *window)
308 EmpathyAccount *account;
309 GtkWidget *error_widget;
311 account = g_object_get_data (G_OBJECT (button), "account");
312 empathy_accounts_dialog_show (GTK_WINDOW (window->window), account);
314 error_widget = g_hash_table_lookup (window->errors, account);
315 gtk_widget_destroy (error_widget);
316 g_hash_table_remove (window->errors, account);
320 main_window_error_clear_clicked_cb (GtkButton *button,
321 EmpathyMainWindow *window)
323 EmpathyAccount *account;
324 GtkWidget *error_widget;
326 account = g_object_get_data (G_OBJECT (button), "account");
327 error_widget = g_hash_table_lookup (window->errors, account);
328 gtk_widget_destroy (error_widget);
329 g_hash_table_remove (window->errors, account);
333 main_window_error_display (EmpathyMainWindow *window,
334 EmpathyAccount *account,
335 const gchar *message)
340 GtkWidget *button_edit;
341 GtkWidget *alignment;
346 GtkWidget *button_close;
349 child = g_hash_table_lookup (window->errors, account);
351 label = g_object_get_data (G_OBJECT (child), "label");
353 /* Just set the latest error and return */
354 str = g_markup_printf_escaped ("<b>%s</b>\n%s",
355 empathy_account_get_display_name (account),
357 gtk_label_set_markup (GTK_LABEL (label), str);
363 child = gtk_vbox_new (FALSE, 0);
364 gtk_box_pack_start (GTK_BOX (window->errors_vbox), child, FALSE, TRUE, 0);
365 gtk_container_set_border_width (GTK_CONTAINER (child), 6);
366 gtk_widget_show (child);
368 table = gtk_table_new (2, 4, FALSE);
369 gtk_widget_show (table);
370 gtk_box_pack_start (GTK_BOX (child), table, TRUE, TRUE, 0);
371 gtk_table_set_row_spacings (GTK_TABLE (table), 12);
372 gtk_table_set_col_spacings (GTK_TABLE (table), 6);
374 image = gtk_image_new_from_stock (GTK_STOCK_DISCONNECT, GTK_ICON_SIZE_MENU);
375 gtk_widget_show (image);
376 gtk_table_attach (GTK_TABLE (table), image, 0, 1, 0, 2,
377 (GtkAttachOptions) (GTK_FILL),
378 (GtkAttachOptions) (GTK_FILL), 0, 0);
379 gtk_misc_set_alignment (GTK_MISC (image), 0.5, 0);
381 button_edit = gtk_button_new ();
382 gtk_widget_show (button_edit);
383 gtk_table_attach (GTK_TABLE (table), button_edit, 1, 2, 1, 2,
384 (GtkAttachOptions) (GTK_FILL),
385 (GtkAttachOptions) (0), 0, 0);
387 alignment = gtk_alignment_new (0.5, 0.5, 0, 0);
388 gtk_widget_show (alignment);
389 gtk_container_add (GTK_CONTAINER (button_edit), alignment);
391 hbox = gtk_hbox_new (FALSE, 2);
392 gtk_widget_show (hbox);
393 gtk_container_add (GTK_CONTAINER (alignment), hbox);
395 image = gtk_image_new_from_stock (GTK_STOCK_EDIT, GTK_ICON_SIZE_BUTTON);
396 gtk_widget_show (image);
397 gtk_box_pack_start (GTK_BOX (hbox), image, FALSE, FALSE, 0);
399 label = gtk_label_new_with_mnemonic (_("_Edit account"));
400 gtk_widget_show (label);
401 gtk_box_pack_start (GTK_BOX (hbox), label, FALSE, FALSE, 0);
403 fixed = gtk_fixed_new ();
404 gtk_widget_show (fixed);
405 gtk_table_attach (GTK_TABLE (table), fixed, 2, 3, 1, 2,
406 (GtkAttachOptions) (GTK_EXPAND | GTK_FILL),
407 (GtkAttachOptions) (GTK_FILL), 0, 0);
409 vbox = gtk_vbox_new (FALSE, 6);
410 gtk_widget_show (vbox);
411 gtk_table_attach (GTK_TABLE (table), vbox, 3, 4, 0, 2,
412 (GtkAttachOptions) (GTK_FILL),
413 (GtkAttachOptions) (GTK_FILL), 0, 0);
415 button_close = gtk_button_new ();
416 gtk_widget_show (button_close);
417 gtk_box_pack_start (GTK_BOX (vbox), button_close, FALSE, FALSE, 0);
418 gtk_button_set_relief (GTK_BUTTON (button_close), GTK_RELIEF_NONE);
421 image = gtk_image_new_from_stock ("gtk-close", GTK_ICON_SIZE_MENU);
422 gtk_widget_show (image);
423 gtk_container_add (GTK_CONTAINER (button_close), image);
425 label = gtk_label_new ("");
426 gtk_widget_show (label);
427 gtk_table_attach (GTK_TABLE (table), label, 1, 3, 0, 1,
428 (GtkAttachOptions) (GTK_EXPAND | GTK_SHRINK | GTK_FILL),
429 (GtkAttachOptions) (GTK_EXPAND | GTK_SHRINK | GTK_FILL), 0, 0);
430 gtk_widget_set_size_request (label, 175, -1);
431 gtk_label_set_use_markup (GTK_LABEL (label), TRUE);
432 gtk_label_set_line_wrap (GTK_LABEL (label), TRUE);
433 gtk_misc_set_alignment (GTK_MISC (label), 0, 0);
435 str = g_markup_printf_escaped ("<b>%s</b>\n%s",
436 empathy_account_get_display_name (account),
438 gtk_label_set_markup (GTK_LABEL (label), str);
441 g_object_set_data (G_OBJECT (child), "label", label);
442 g_object_set_data_full (G_OBJECT (button_edit),
443 "account", g_object_ref (account),
445 g_object_set_data_full (G_OBJECT (button_close),
446 "account", g_object_ref (account),
449 g_signal_connect (button_edit, "clicked",
450 G_CALLBACK (main_window_error_edit_clicked_cb),
453 g_signal_connect (button_close, "clicked",
454 G_CALLBACK (main_window_error_clear_clicked_cb),
457 gtk_widget_show (window->errors_vbox);
459 g_hash_table_insert (window->errors, g_object_ref (account), child);
463 main_window_update_status (EmpathyMainWindow *window, EmpathyAccountManager *manager)
469 /* Count number of connected/connecting/disconnected accounts */
470 connected = empathy_account_manager_get_connected_accounts (manager);
471 connecting = empathy_account_manager_get_connecting_accounts (manager);
473 /* Update the spinner state */
474 if (connecting > 0) {
475 ephy_spinner_start (EPHY_SPINNER (window->throbber));
477 ephy_spinner_stop (EPHY_SPINNER (window->throbber));
480 /* Update widgets sensibility */
481 for (l = window->actions_connected; l; l = l->next) {
482 gtk_action_set_sensitive (l->data, (connected > 0));
487 main_window_connection_changed_cb (EmpathyAccountManager *manager,
488 EmpathyAccount *account,
489 TpConnectionStatusReason reason,
490 TpConnectionStatus current,
491 TpConnectionStatus previous,
492 EmpathyMainWindow *window)
494 main_window_update_status (window, manager);
496 if (current == TP_CONNECTION_STATUS_DISCONNECTED &&
497 reason != TP_CONNECTION_STATUS_REASON_REQUESTED) {
498 const gchar *message;
501 case TP_CONNECTION_STATUS_REASON_NONE_SPECIFIED:
502 message = _("No error specified");
504 case TP_CONNECTION_STATUS_REASON_NETWORK_ERROR:
505 message = _("Network error");
507 case TP_CONNECTION_STATUS_REASON_AUTHENTICATION_FAILED:
508 message = _("Authentication failed");
510 case TP_CONNECTION_STATUS_REASON_ENCRYPTION_ERROR:
511 message = _("Encryption error");
513 case TP_CONNECTION_STATUS_REASON_NAME_IN_USE:
514 message = _("Name in use");
516 case TP_CONNECTION_STATUS_REASON_CERT_NOT_PROVIDED:
517 message = _("Certificate not provided");
519 case TP_CONNECTION_STATUS_REASON_CERT_UNTRUSTED:
520 message = _("Certificate untrusted");
522 case TP_CONNECTION_STATUS_REASON_CERT_EXPIRED:
523 message = _("Certificate expired");
525 case TP_CONNECTION_STATUS_REASON_CERT_NOT_ACTIVATED:
526 message = _("Certificate not activated");
528 case TP_CONNECTION_STATUS_REASON_CERT_HOSTNAME_MISMATCH:
529 message = _("Certificate hostname mismatch");
531 case TP_CONNECTION_STATUS_REASON_CERT_FINGERPRINT_MISMATCH:
532 message = _("Certificate fingerprint mismatch");
534 case TP_CONNECTION_STATUS_REASON_CERT_SELF_SIGNED:
535 message = _("Certificate self-signed");
537 case TP_CONNECTION_STATUS_REASON_CERT_OTHER_ERROR:
538 message = _("Certificate error");
541 message = _("Unknown error");
545 main_window_error_display (window, account, message);
548 if (current == TP_CONNECTION_STATUS_DISCONNECTED) {
549 empathy_sound_play (GTK_WIDGET (window->window),
550 EMPATHY_SOUND_ACCOUNT_DISCONNECTED);
553 if (current == TP_CONNECTION_STATUS_CONNECTED) {
554 GtkWidget *error_widget;
556 empathy_sound_play (GTK_WIDGET (window->window),
557 EMPATHY_SOUND_ACCOUNT_CONNECTED);
559 /* Account connected without error, remove error message if any */
560 error_widget = g_hash_table_lookup (window->errors, account);
562 gtk_widget_destroy (error_widget);
563 g_hash_table_remove (window->errors, account);
569 main_window_contact_presence_changed_cb (EmpathyContactMonitor *monitor,
570 EmpathyContact *contact,
571 TpConnectionPresenceType current,
572 TpConnectionPresenceType previous,
573 EmpathyMainWindow *window)
575 EmpathyAccount *account;
576 gboolean should_play;
578 account = empathy_contact_get_account (contact);
579 should_play = !empathy_account_is_just_connected (account);
585 if (tp_connection_presence_type_cmp_availability (previous,
586 TP_CONNECTION_PRESENCE_TYPE_OFFLINE) > 0)
588 /* contact was online */
589 if (tp_connection_presence_type_cmp_availability (current,
590 TP_CONNECTION_PRESENCE_TYPE_OFFLINE) <= 0)
591 /* someone is logging off */
592 empathy_sound_play (GTK_WIDGET (window->window),
593 EMPATHY_SOUND_CONTACT_DISCONNECTED);
597 /* contact was offline */
598 if (tp_connection_presence_type_cmp_availability (current,
599 TP_CONNECTION_PRESENCE_TYPE_OFFLINE) > 0)
600 /* someone is logging in */
601 empathy_sound_play (GTK_WIDGET (window->window),
602 EMPATHY_SOUND_CONTACT_CONNECTED);
607 main_window_accels_load (void)
611 filename = g_build_filename (g_get_user_config_dir (), PACKAGE_NAME, ACCELS_FILENAME, NULL);
612 if (g_file_test (filename, G_FILE_TEST_EXISTS)) {
613 DEBUG ("Loading from:'%s'", filename);
614 gtk_accel_map_load (filename);
621 main_window_accels_save (void)
624 gchar *file_with_path;
626 dir = g_build_filename (g_get_user_config_dir (), PACKAGE_NAME, NULL);
627 g_mkdir_with_parents (dir, S_IRUSR | S_IWUSR | S_IXUSR);
628 file_with_path = g_build_filename (dir, ACCELS_FILENAME, NULL);
631 DEBUG ("Saving to:'%s'", file_with_path);
632 gtk_accel_map_save (file_with_path);
634 g_free (file_with_path);
638 main_window_destroy_cb (GtkWidget *widget,
639 EmpathyMainWindow *window)
641 /* Save user-defined accelerators. */
642 main_window_accels_save ();
644 g_signal_handlers_disconnect_by_func (window->account_manager,
645 main_window_connection_changed_cb,
648 if (window->size_timeout_id) {
649 g_source_remove (window->size_timeout_id);
652 g_list_free (window->actions_connected);
654 g_object_unref (window->account_manager);
655 g_object_unref (window->list_store);
656 g_hash_table_destroy (window->errors);
658 g_signal_handlers_disconnect_by_func (window->event_manager,
659 main_window_event_added_cb,
661 g_signal_handlers_disconnect_by_func (window->event_manager,
662 main_window_event_removed_cb,
664 g_object_unref (window->event_manager);
665 g_object_unref (window->ui_manager);
671 main_window_chat_quit_cb (GtkAction *action,
672 EmpathyMainWindow *window)
678 main_window_view_history_cb (GtkAction *action,
679 EmpathyMainWindow *window)
681 empathy_log_window_show (NULL, NULL, FALSE, GTK_WINDOW (window->window));
685 main_window_chat_new_message_cb (GtkAction *action,
686 EmpathyMainWindow *window)
688 empathy_new_message_dialog_show (GTK_WINDOW (window->window));
692 main_window_chat_add_contact_cb (GtkAction *action,
693 EmpathyMainWindow *window)
695 empathy_new_contact_dialog_show (GTK_WINDOW (window->window));
699 main_window_view_show_ft_manager (GtkAction *action,
700 EmpathyMainWindow *window)
702 empathy_ft_manager_show ();
706 main_window_view_show_offline_cb (GtkToggleAction *action,
707 EmpathyMainWindow *window)
711 current = gtk_toggle_action_get_active (action);
712 empathy_conf_set_bool (empathy_conf_get (),
713 EMPATHY_PREFS_CONTACTS_SHOW_OFFLINE,
716 /* Turn off sound just while we alter the contact list. */
717 // FIXME: empathy_sound_set_enabled (FALSE);
718 empathy_contact_list_store_set_show_offline (window->list_store, current);
719 //empathy_sound_set_enabled (TRUE);
723 main_window_notify_sort_contact_cb (EmpathyConf *conf,
725 EmpathyMainWindow *window)
729 if (empathy_conf_get_string (conf, key, &str) && str) {
731 GEnumClass *enum_class;
732 GEnumValue *enum_value;
734 type = empathy_contact_list_store_sort_get_type ();
735 enum_class = G_ENUM_CLASS (g_type_class_peek (type));
736 enum_value = g_enum_get_value_by_nick (enum_class, str);
738 /* By changing the value of the GtkRadioAction,
739 it emits a signal that calls main_window_view_sort_contacts_cb
740 which updates the contacts list */
741 gtk_radio_action_set_current_value (window->sort_by_name,
744 g_warning ("Wrong value for sort_criterium configuration : %s", str);
751 main_window_view_sort_contacts_cb (GtkRadioAction *action,
752 GtkRadioAction *current,
753 EmpathyMainWindow *window)
755 EmpathyContactListStoreSort value;
758 GEnumClass *enum_class;
759 GEnumValue *enum_value;
761 value = gtk_radio_action_get_current_value (action);
762 group = gtk_radio_action_get_group (action);
764 /* Get string from index */
765 type = empathy_contact_list_store_sort_get_type ();
766 enum_class = G_ENUM_CLASS (g_type_class_peek (type));
767 enum_value = g_enum_get_value (enum_class, g_slist_index (group, current));
770 g_warning ("No GEnumValue for EmpathyContactListSort with GtkRadioAction index:%d",
771 g_slist_index (group, action));
773 empathy_conf_set_string (empathy_conf_get (),
774 EMPATHY_PREFS_CONTACTS_SORT_CRITERIUM,
775 enum_value->value_nick);
777 empathy_contact_list_store_set_sort_criterium (window->list_store, value);
780 /* Matches GtkRadioAction values set in empathy-main-window.ui */
781 #define CONTACT_LIST_NORMAL_SIZE_WITH_AVATARS 0
782 #define CONTACT_LIST_NORMAL_SIZE 1
783 #define CONTACT_LIST_COMPACT_SIZE 2
786 main_window_view_contacts_list_size_cb (GtkRadioAction *action,
787 GtkRadioAction *current,
788 EmpathyMainWindow *window)
792 value = gtk_radio_action_get_current_value (action);
794 empathy_conf_set_bool (empathy_conf_get (),
795 EMPATHY_PREFS_UI_SHOW_AVATARS,
796 value == CONTACT_LIST_NORMAL_SIZE_WITH_AVATARS);
797 empathy_conf_set_bool (empathy_conf_get (),
798 EMPATHY_PREFS_UI_COMPACT_CONTACT_LIST,
799 value == CONTACT_LIST_COMPACT_SIZE);
801 empathy_contact_list_store_set_show_avatars (window->list_store,
802 value == CONTACT_LIST_NORMAL_SIZE_WITH_AVATARS);
803 empathy_contact_list_store_set_is_compact (window->list_store,
804 value == CONTACT_LIST_COMPACT_SIZE);
808 main_window_notify_contact_list_size_cb (EmpathyConf *conf,
810 EmpathyMainWindow *window)
812 gboolean show_avatars;
813 gboolean compact_contact_list;
814 gint value = CONTACT_LIST_NORMAL_SIZE_WITH_AVATARS;
816 if (empathy_conf_get_bool (conf,
817 EMPATHY_PREFS_UI_SHOW_AVATARS,
819 && empathy_conf_get_bool (conf,
820 EMPATHY_PREFS_UI_COMPACT_CONTACT_LIST,
821 &compact_contact_list)) {
822 if (compact_contact_list) {
823 value = CONTACT_LIST_COMPACT_SIZE;
824 } else if (show_avatars) {
825 value = CONTACT_LIST_NORMAL_SIZE_WITH_AVATARS;
827 value = CONTACT_LIST_NORMAL_SIZE;
830 /* By changing the value of the GtkRadioAction,
831 it emits a signal that calls main_window_view_contacts_list_size_cb
832 which updates the contacts list */
833 gtk_radio_action_set_current_value (window->normal_with_avatars, value);
837 main_window_view_show_map_cb (GtkCheckMenuItem *item,
838 EmpathyMainWindow *window)
840 #if HAVE_LIBCHAMPLAIN
841 empathy_map_view_show ();
846 main_window_favorite_chatroom_join (EmpathyChatroom *chatroom)
848 EmpathyAccount *account;
849 TpConnection *connection;
852 account = empathy_chatroom_get_account (chatroom);
853 connection = empathy_account_get_connection (account);
854 room = empathy_chatroom_get_room (chatroom);
856 if (connection != NULL) {
857 DEBUG ("Requesting channel for '%s'", room);
858 empathy_dispatcher_join_muc (connection, room, NULL, NULL);
863 main_window_favorite_chatroom_menu_activate_cb (GtkMenuItem *menu_item,
864 EmpathyChatroom *chatroom)
866 main_window_favorite_chatroom_join (chatroom);
870 main_window_favorite_chatroom_menu_add (EmpathyMainWindow *window,
871 EmpathyChatroom *chatroom)
873 GtkWidget *menu_item;
876 if (g_object_get_data (G_OBJECT (chatroom), "menu_item")) {
880 name = empathy_chatroom_get_name (chatroom);
881 menu_item = gtk_menu_item_new_with_label (name);
883 g_object_set_data (G_OBJECT (chatroom), "menu_item", menu_item);
884 g_signal_connect (menu_item, "activate",
885 G_CALLBACK (main_window_favorite_chatroom_menu_activate_cb),
888 gtk_menu_shell_insert (GTK_MENU_SHELL (window->room_menu),
891 gtk_widget_show (menu_item);
895 main_window_favorite_chatroom_menu_added_cb (EmpathyChatroomManager *manager,
896 EmpathyChatroom *chatroom,
897 EmpathyMainWindow *window)
899 main_window_favorite_chatroom_menu_add (window, chatroom);
900 gtk_widget_show (window->room_separator);
901 gtk_action_set_sensitive (window->room_join_favorites, TRUE);
905 main_window_favorite_chatroom_menu_removed_cb (EmpathyChatroomManager *manager,
906 EmpathyChatroom *chatroom,
907 EmpathyMainWindow *window)
909 GtkWidget *menu_item;
912 menu_item = g_object_get_data (G_OBJECT (chatroom), "menu_item");
913 g_object_set_data (G_OBJECT (chatroom), "menu_item", NULL);
914 gtk_widget_destroy (menu_item);
916 chatrooms = empathy_chatroom_manager_get_chatrooms (window->chatroom_manager, NULL);
918 gtk_widget_show (window->room_separator);
920 gtk_widget_hide (window->room_separator);
923 gtk_action_set_sensitive (window->room_join_favorites, chatrooms != NULL);
924 g_list_free (chatrooms);
928 main_window_favorite_chatroom_menu_setup (EmpathyMainWindow *window)
930 GList *chatrooms, *l;
933 window->chatroom_manager = empathy_chatroom_manager_dup_singleton (NULL);
934 chatrooms = empathy_chatroom_manager_get_chatrooms (window->chatroom_manager, NULL);
935 room = gtk_ui_manager_get_widget (window->ui_manager,
937 window->room_menu = gtk_menu_item_get_submenu (GTK_MENU_ITEM (room));
938 window->room_separator = gtk_ui_manager_get_widget (window->ui_manager,
939 "/menubar/room/room_separator");
941 for (l = chatrooms; l; l = l->next) {
942 main_window_favorite_chatroom_menu_add (window, l->data);
946 gtk_widget_hide (window->room_separator);
949 gtk_action_set_sensitive (window->room_join_favorites, chatrooms != NULL);
951 g_signal_connect (window->chatroom_manager, "chatroom-added",
952 G_CALLBACK (main_window_favorite_chatroom_menu_added_cb),
954 g_signal_connect (window->chatroom_manager, "chatroom-removed",
955 G_CALLBACK (main_window_favorite_chatroom_menu_removed_cb),
958 g_list_free (chatrooms);
962 main_window_room_join_new_cb (GtkAction *action,
963 EmpathyMainWindow *window)
965 empathy_new_chatroom_dialog_show (GTK_WINDOW (window->window));
969 main_window_room_join_favorites_cb (GtkAction *action,
970 EmpathyMainWindow *window)
972 GList *chatrooms, *l;
974 chatrooms = empathy_chatroom_manager_get_chatrooms (window->chatroom_manager, NULL);
975 for (l = chatrooms; l; l = l->next) {
976 main_window_favorite_chatroom_join (l->data);
978 g_list_free (chatrooms);
982 main_window_room_manage_favorites_cb (GtkAction *action,
983 EmpathyMainWindow *window)
985 empathy_chatrooms_window_show (GTK_WINDOW (window->window));
989 main_window_edit_cb (GtkAction *action,
990 EmpathyMainWindow *window)
994 /* FIXME: It should use the UIManager to merge the contact/group submenu */
995 submenu = empathy_contact_list_view_get_contact_menu (window->list_view);
1000 item = GTK_MENU_ITEM (window->edit_context);
1001 label = gtk_bin_get_child (GTK_BIN (item));
1002 gtk_label_set_text (GTK_LABEL (label), _("Contact"));
1004 gtk_widget_show (window->edit_context);
1005 gtk_widget_show (window->edit_context_separator);
1007 gtk_menu_item_set_submenu (item, submenu);
1012 submenu = empathy_contact_list_view_get_group_menu (window->list_view);
1017 item = GTK_MENU_ITEM (window->edit_context);
1018 label = gtk_bin_get_child (GTK_BIN (item));
1019 gtk_label_set_text (GTK_LABEL (label), _("Group"));
1021 gtk_widget_show (window->edit_context);
1022 gtk_widget_show (window->edit_context_separator);
1024 gtk_menu_item_set_submenu (item, submenu);
1029 gtk_widget_hide (window->edit_context);
1030 gtk_widget_hide (window->edit_context_separator);
1036 main_window_edit_accounts_cb (GtkAction *action,
1037 EmpathyMainWindow *window)
1039 empathy_accounts_dialog_show (GTK_WINDOW (window->window), NULL);
1043 main_window_edit_personal_information_cb (GtkAction *action,
1044 EmpathyMainWindow *window)
1046 empathy_contact_personal_dialog_show (GTK_WINDOW (window->window));
1050 main_window_edit_preferences_cb (GtkAction *action,
1051 EmpathyMainWindow *window)
1053 empathy_preferences_show (GTK_WINDOW (window->window));
1057 main_window_help_about_cb (GtkAction *action,
1058 EmpathyMainWindow *window)
1060 empathy_about_dialog_new (GTK_WINDOW (window->window));
1064 main_window_help_debug_cb (GtkAction *action,
1065 EmpathyMainWindow *window)
1067 empathy_debug_window_new (GTK_WINDOW (window->window));
1071 main_window_help_contents_cb (GtkAction *action,
1072 EmpathyMainWindow *window)
1074 empathy_url_show (window->window, "ghelp:empathy");
1078 main_window_throbber_button_press_event_cb (GtkWidget *throbber_ebox,
1079 GdkEventButton *event,
1080 EmpathyMainWindow *window)
1082 if (event->type != GDK_BUTTON_PRESS ||
1083 event->button != 1) {
1087 empathy_accounts_dialog_show (GTK_WINDOW (window->window), NULL);
1093 main_window_configure_event_timeout_cb (EmpathyMainWindow *window)
1097 gtk_window_get_size (GTK_WINDOW (window->window), &w, &h);
1098 gtk_window_get_position (GTK_WINDOW (window->window), &x, &y);
1100 empathy_geometry_save (GEOMETRY_NAME, x, y, w, h);
1102 window->size_timeout_id = 0;
1108 main_window_configure_event_cb (GtkWidget *widget,
1109 GdkEventConfigure *event,
1110 EmpathyMainWindow *window)
1112 if (window->size_timeout_id) {
1113 g_source_remove (window->size_timeout_id);
1116 window->size_timeout_id = g_timeout_add_seconds (1,
1117 (GSourceFunc) main_window_configure_event_timeout_cb,
1124 main_window_account_created_or_deleted_cb (EmpathyAccountManager *manager,
1125 EmpathyAccount *account,
1126 EmpathyMainWindow *window)
1128 gtk_action_set_sensitive (window->view_history,
1129 empathy_account_manager_get_count (manager) > 0);
1133 main_window_notify_show_offline_cb (EmpathyConf *conf,
1135 gpointer toggle_action)
1137 gboolean show_offline;
1139 if (empathy_conf_get_bool (conf, key, &show_offline)) {
1140 gtk_toggle_action_set_active (toggle_action, show_offline);
1145 main_window_connection_items_setup (EmpathyMainWindow *window,
1151 const gchar *actions_connected[] = {
1155 "edit_personal_information"
1158 for (i = 0, list = NULL; i < G_N_ELEMENTS (actions_connected); i++) {
1159 action = gtk_builder_get_object (gui, actions_connected[i]);
1160 list = g_list_prepend (list, action);
1163 window->actions_connected = list;
1167 empathy_main_window_get (void)
1169 return window != NULL ? window->window : NULL;
1173 empathy_main_window_show (void)
1175 EmpathyContactList *list_iface;
1176 EmpathyContactMonitor *monitor;
1180 GtkToggleAction *show_offline_widget;
1182 GtkAction *show_map_widget;
1184 gboolean show_offline;
1190 empathy_window_present (GTK_WINDOW (window->window), TRUE);
1191 return window->window;
1194 window = g_new0 (EmpathyMainWindow, 1);
1196 /* Set up interface */
1197 filename = empathy_file_lookup ("empathy-main-window.ui", "src");
1198 gui = empathy_builder_get_file (filename,
1199 "main_window", &window->window,
1200 "main_vbox", &window->main_vbox,
1201 "errors_vbox", &window->errors_vbox,
1202 "ui_manager", &window->ui_manager,
1203 "view_show_offline", &show_offline_widget,
1204 "view_sort_by_name", &window->sort_by_name,
1205 "view_sort_by_status", &window->sort_by_status,
1206 "view_normal_size_with_avatars", &window->normal_with_avatars,
1207 "view_normal_size", &window->normal_size,
1208 "view_compact_size", &window->compact_size,
1209 "view_history", &window->view_history,
1210 "view_show_map", &show_map_widget,
1211 "room_join_favorites", &window->room_join_favorites,
1212 "presence_toolbar", &window->presence_toolbar,
1213 "roster_scrolledwindow", &sw,
1217 empathy_builder_connect (gui, window,
1218 "main_window", "destroy", main_window_destroy_cb,
1219 "main_window", "configure_event", main_window_configure_event_cb,
1220 "chat_quit", "activate", main_window_chat_quit_cb,
1221 "chat_new_message", "activate", main_window_chat_new_message_cb,
1222 "view_history", "activate", main_window_view_history_cb,
1223 "room_join_new", "activate", main_window_room_join_new_cb,
1224 "room_join_favorites", "activate", main_window_room_join_favorites_cb,
1225 "room_manage_favorites", "activate", main_window_room_manage_favorites_cb,
1226 "chat_add_contact", "activate", main_window_chat_add_contact_cb,
1227 "view_show_ft_manager", "activate", main_window_view_show_ft_manager,
1228 "view_show_offline", "toggled", main_window_view_show_offline_cb,
1229 "view_sort_by_name", "changed", main_window_view_sort_contacts_cb,
1230 "view_normal_size_with_avatars", "changed", main_window_view_contacts_list_size_cb,
1231 "view_show_map", "activate", main_window_view_show_map_cb,
1232 "edit", "activate", main_window_edit_cb,
1233 "edit_accounts", "activate", main_window_edit_accounts_cb,
1234 "edit_personal_information", "activate", main_window_edit_personal_information_cb,
1235 "edit_preferences", "activate", main_window_edit_preferences_cb,
1236 "help_about", "activate", main_window_help_about_cb,
1237 "help_debug", "activate", main_window_help_debug_cb,
1238 "help_contents", "activate", main_window_help_contents_cb,
1241 /* Set up connection related widgets. */
1242 main_window_connection_items_setup (window, gui);
1244 g_object_ref (window->ui_manager);
1245 g_object_unref (gui);
1247 #if !HAVE_LIBCHAMPLAIN
1248 gtk_action_set_visible (show_map_widget, FALSE);
1251 window->account_manager = empathy_account_manager_dup_singleton ();
1253 g_signal_connect (window->account_manager,
1254 "account-connection-changed",
1255 G_CALLBACK (main_window_connection_changed_cb), window);
1257 window->errors = g_hash_table_new_full (g_direct_hash,
1263 main_window_favorite_chatroom_menu_setup (window);
1265 window->edit_context = gtk_ui_manager_get_widget (window->ui_manager,
1266 "/menubar/edit/edit_context");
1267 window->edit_context_separator = gtk_ui_manager_get_widget (window->ui_manager,
1268 "/menubar/edit/edit_context_separator");
1269 gtk_widget_hide (window->edit_context);
1270 gtk_widget_hide (window->edit_context_separator);
1272 /* Set up contact list. */
1273 empathy_status_presets_get_all ();
1275 /* Set up presence chooser */
1276 window->presence_chooser = empathy_presence_chooser_new ();
1277 gtk_widget_show (window->presence_chooser);
1278 item = gtk_tool_item_new ();
1279 gtk_widget_show (GTK_WIDGET (item));
1280 gtk_container_add (GTK_CONTAINER (item), window->presence_chooser);
1281 gtk_tool_item_set_is_important (item, TRUE);
1282 gtk_tool_item_set_expand (item, TRUE);
1283 gtk_toolbar_insert (GTK_TOOLBAR (window->presence_toolbar), item, -1);
1285 /* Set up the throbber */
1286 ebox = gtk_event_box_new ();
1287 gtk_event_box_set_visible_window (GTK_EVENT_BOX (ebox), FALSE);
1288 gtk_widget_set_tooltip_text (ebox, _("Show and edit accounts"));
1289 g_signal_connect (ebox,
1290 "button-press-event",
1291 G_CALLBACK (main_window_throbber_button_press_event_cb),
1293 gtk_widget_show (ebox);
1295 window->throbber = ephy_spinner_new ();
1296 ephy_spinner_set_size (EPHY_SPINNER (window->throbber), GTK_ICON_SIZE_LARGE_TOOLBAR);
1297 gtk_container_add (GTK_CONTAINER (ebox), window->throbber);
1298 gtk_widget_show (window->throbber);
1300 item = gtk_tool_item_new ();
1301 gtk_container_add (GTK_CONTAINER (item), ebox);
1302 gtk_toolbar_insert (GTK_TOOLBAR (window->presence_toolbar), item, -1);
1303 gtk_widget_show (GTK_WIDGET (item));
1305 list_iface = EMPATHY_CONTACT_LIST (empathy_contact_manager_dup_singleton ());
1306 monitor = empathy_contact_list_get_monitor (list_iface);
1307 window->list_store = empathy_contact_list_store_new (list_iface);
1308 window->list_view = empathy_contact_list_view_new (window->list_store,
1309 EMPATHY_CONTACT_LIST_FEATURE_ALL,
1310 EMPATHY_CONTACT_FEATURE_ALL);
1311 g_signal_connect (monitor, "contact-presence-changed",
1312 G_CALLBACK (main_window_contact_presence_changed_cb), window);
1313 g_object_unref (list_iface);
1315 gtk_widget_show (GTK_WIDGET (window->list_view));
1316 gtk_container_add (GTK_CONTAINER (sw),
1317 GTK_WIDGET (window->list_view));
1318 g_signal_connect (window->list_view, "row-activated",
1319 G_CALLBACK (main_window_row_activated_cb),
1322 /* Load user-defined accelerators. */
1323 main_window_accels_load ();
1325 /* Set window size. */
1326 empathy_geometry_load (GEOMETRY_NAME, &x, &y, &w, &h);
1328 if (w >= 1 && h >= 1) {
1329 /* Use the defaults from the ui file if we
1330 * don't have good w, h geometry.
1332 DEBUG ("Configuring window default size w:%d, h:%d", w, h);
1333 gtk_window_set_default_size (GTK_WINDOW (window->window), w, h);
1336 if (x >= 0 && y >= 0) {
1337 /* Let the window manager position it if we
1338 * don't have good x, y coordinates.
1340 DEBUG ("Configuring window default position x:%d, y:%d", x, y);
1341 gtk_window_move (GTK_WINDOW (window->window), x, y);
1344 /* Enable event handling */
1345 window->event_manager = empathy_event_manager_dup_singleton ();
1346 g_signal_connect (window->event_manager, "event-added",
1347 G_CALLBACK (main_window_event_added_cb),
1349 g_signal_connect (window->event_manager, "event-removed",
1350 G_CALLBACK (main_window_event_removed_cb),
1353 g_signal_connect (window->account_manager, "account-created",
1354 G_CALLBACK (main_window_account_created_or_deleted_cb),
1356 g_signal_connect (window->account_manager, "account-deleted",
1357 G_CALLBACK (main_window_account_created_or_deleted_cb),
1359 main_window_account_created_or_deleted_cb (window->account_manager, NULL, window);
1361 l = empathy_event_manager_get_events (window->event_manager);
1363 main_window_event_added_cb (window->event_manager,
1368 conf = empathy_conf_get ();
1370 /* Show offline ? */
1371 empathy_conf_get_bool (conf,
1372 EMPATHY_PREFS_CONTACTS_SHOW_OFFLINE,
1374 empathy_conf_notify_add (conf,
1375 EMPATHY_PREFS_CONTACTS_SHOW_OFFLINE,
1376 main_window_notify_show_offline_cb,
1377 show_offline_widget);
1379 gtk_toggle_action_set_active (show_offline_widget, show_offline);
1381 /* Sort by name / by status ? */
1382 empathy_conf_notify_add (conf,
1383 EMPATHY_PREFS_CONTACTS_SORT_CRITERIUM,
1384 (EmpathyConfNotifyFunc) main_window_notify_sort_contact_cb,
1387 main_window_notify_sort_contact_cb (conf,
1388 EMPATHY_PREFS_CONTACTS_SORT_CRITERIUM,
1391 /* Contacts list size */
1392 empathy_conf_notify_add (conf,
1393 EMPATHY_PREFS_UI_COMPACT_CONTACT_LIST,
1394 (EmpathyConfNotifyFunc) main_window_notify_contact_list_size_cb,
1396 empathy_conf_notify_add (conf,
1397 EMPATHY_PREFS_UI_SHOW_AVATARS,
1398 (EmpathyConfNotifyFunc) main_window_notify_contact_list_size_cb,
1401 main_window_notify_contact_list_size_cb (conf,
1402 EMPATHY_PREFS_UI_SHOW_AVATARS,
1405 main_window_update_status (window, window->account_manager);
1407 gtk_widget_show (window->window);
1409 return window->window;