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 <telepathy-glib/account-manager.h>
32 #include <libempathy/empathy-contact.h>
33 #include <libempathy/empathy-idle.h>
34 #include <libempathy/empathy-utils.h>
35 #include <libempathy/empathy-dispatcher.h>
36 #include <libempathy/empathy-chatroom-manager.h>
37 #include <libempathy/empathy-chatroom.h>
38 #include <libempathy/empathy-contact-list.h>
39 #include <libempathy/empathy-contact-manager.h>
40 #include <libempathy/empathy-status-presets.h>
42 #include <libempathy-gtk/empathy-conf.h>
43 #include <libempathy-gtk/empathy-contact-dialogs.h>
44 #include <libempathy-gtk/empathy-contact-list-store.h>
45 #include <libempathy-gtk/empathy-contact-list-view.h>
46 #include <libempathy-gtk/empathy-geometry.h>
47 #include <libempathy-gtk/empathy-gtk-enum-types.h>
48 #include <libempathy-gtk/empathy-new-message-dialog.h>
49 #include <libempathy-gtk/empathy-log-window.h>
50 #include <libempathy-gtk/empathy-presence-chooser.h>
51 #include <libempathy-gtk/empathy-sound.h>
52 #include <libempathy-gtk/empathy-ui-utils.h>
54 #include "empathy-accounts-dialog.h"
55 #include "empathy-main-window.h"
56 #include "ephy-spinner.h"
57 #include "empathy-preferences.h"
58 #include "empathy-about-dialog.h"
59 #include "empathy-debug-window.h"
60 #include "empathy-new-chatroom-dialog.h"
61 #include "empathy-map-view.h"
62 #include "empathy-chatrooms-window.h"
63 #include "empathy-event-manager.h"
64 #include "empathy-ft-manager.h"
66 #define DEBUG_FLAG EMPATHY_DEBUG_OTHER
67 #include <libempathy/empathy-debug.h>
69 /* Flashing delay for icons (milliseconds). */
70 #define FLASH_TIMEOUT 500
72 /* Minimum width of roster window if something goes wrong. */
75 /* Accels (menu shortcuts) can be configured and saved */
76 #define ACCELS_FILENAME "accels.txt"
78 /* Name in the geometry file */
79 #define GEOMETRY_NAME "main-window"
82 EmpathyContactListView *list_view;
83 EmpathyContactListStore *list_store;
84 TpAccountManager *account_manager;
85 EmpathyChatroomManager *chatroom_manager;
86 EmpathyEventManager *event_manager;
87 guint flash_timeout_id;
93 GtkWidget *presence_toolbar;
94 GtkWidget *presence_chooser;
95 GtkWidget *errors_vbox;
97 GtkRadioAction *sort_by_name;
98 GtkRadioAction *sort_by_status;
99 GtkRadioAction *normal_with_avatars;
100 GtkRadioAction *normal_size;
101 GtkRadioAction *compact_size;
103 GtkUIManager *ui_manager;
104 GtkAction *view_history;
105 GtkAction *room_join_favorites;
106 GtkWidget *room_menu;
107 GtkWidget *room_separator;
108 GtkWidget *edit_context;
109 GtkWidget *edit_context_separator;
111 guint size_timeout_id;
114 /* Actions that are enabled when there are connected accounts */
115 GList *actions_connected;
118 static EmpathyMainWindow *main_window = NULL;
120 static gboolean main_window_configure_event_timeout_cb (
121 EmpathyMainWindow *window);
124 main_window_flash_stop (EmpathyMainWindow *window)
126 if (window->flash_timeout_id == 0) {
130 DEBUG ("Stop flashing");
131 g_source_remove (window->flash_timeout_id);
132 window->flash_timeout_id = 0;
133 window->flash_on = FALSE;
142 main_window_flash_foreach (GtkTreeModel *model,
147 FlashForeachData *data = (FlashForeachData *) user_data;
148 EmpathyContact *contact;
149 const gchar *icon_name;
150 GtkTreePath *parent_path = NULL;
151 GtkTreeIter parent_iter;
153 /* To be used with gtk_tree_model_foreach, update the status icon
154 * of the contact to show the event icon (on=TRUE) or the presence
156 gtk_tree_model_get (model, iter,
157 EMPATHY_CONTACT_LIST_STORE_COL_CONTACT, &contact,
160 if (contact != data->event->contact) {
162 g_object_unref (contact);
168 icon_name = data->event->icon_name;
170 icon_name = empathy_icon_name_for_contact (contact);
173 gtk_tree_store_set (GTK_TREE_STORE (model), iter,
174 EMPATHY_CONTACT_LIST_STORE_COL_ICON_STATUS, icon_name,
177 /* To make sure the parent is shown correctly, we emit
178 * the row-changed signal on the parent so it prompts
179 * it to be refreshed by the filter func.
181 if (gtk_tree_model_iter_parent (model, &parent_iter, iter)) {
182 parent_path = gtk_tree_model_get_path (model, &parent_iter);
185 gtk_tree_model_row_changed (model, parent_path, &parent_iter);
186 gtk_tree_path_free (parent_path);
189 g_object_unref (contact);
195 main_window_flash_cb (EmpathyMainWindow *window)
199 gboolean found_event = FALSE;
200 FlashForeachData data;
202 window->flash_on = !window->flash_on;
203 data.on = window->flash_on;
204 model = GTK_TREE_MODEL (window->list_store);
206 events = empathy_event_manager_get_events (window->event_manager);
207 for (l = events; l; l = l->next) {
208 data.event = l->data;
209 if (!data.event->contact || !data.event->must_ack) {
214 gtk_tree_model_foreach (model,
215 main_window_flash_foreach,
220 main_window_flash_stop (window);
227 main_window_flash_start (EmpathyMainWindow *window)
229 if (window->flash_timeout_id != 0) {
233 DEBUG ("Start flashing");
234 window->flash_timeout_id = g_timeout_add (FLASH_TIMEOUT,
235 (GSourceFunc) main_window_flash_cb,
237 main_window_flash_cb (window);
241 main_window_event_added_cb (EmpathyEventManager *manager,
243 EmpathyMainWindow *window)
245 if (event->contact) {
246 main_window_flash_start (window);
251 main_window_event_removed_cb (EmpathyEventManager *manager,
253 EmpathyMainWindow *window)
255 FlashForeachData data;
257 if (!event->contact) {
263 gtk_tree_model_foreach (GTK_TREE_MODEL (window->list_store),
264 main_window_flash_foreach,
269 main_window_row_activated_cb (EmpathyContactListView *view,
271 GtkTreeViewColumn *col,
272 EmpathyMainWindow *window)
274 EmpathyContact *contact;
279 model = GTK_TREE_MODEL (window->list_store);
280 gtk_tree_model_get_iter (model, &iter, path);
281 gtk_tree_model_get (model, &iter,
282 EMPATHY_CONTACT_LIST_STORE_COL_CONTACT, &contact,
289 /* If the contact has an event activate it, otherwise the
290 * default handler of row-activated will be called. */
291 events = empathy_event_manager_get_events (window->event_manager);
292 for (l = events; l; l = l->next) {
293 EmpathyEvent *event = l->data;
295 if (event->contact == contact) {
296 DEBUG ("Activate event");
297 empathy_event_activate (event);
299 /* We don't want the default handler of this signal
300 * (e.g. open a chat) */
301 g_signal_stop_emission_by_name (view, "row-activated");
306 g_object_unref (contact);
310 main_window_error_edit_clicked_cb (GtkButton *button,
311 EmpathyMainWindow *window)
314 GtkWidget *error_widget;
316 account = g_object_get_data (G_OBJECT (button), "account");
317 empathy_accounts_dialog_show (GTK_WINDOW (window->window), account);
319 error_widget = g_hash_table_lookup (window->errors, account);
320 gtk_widget_destroy (error_widget);
321 g_hash_table_remove (window->errors, account);
325 main_window_error_clear_clicked_cb (GtkButton *button,
326 EmpathyMainWindow *window)
329 GtkWidget *error_widget;
331 account = g_object_get_data (G_OBJECT (button), "account");
332 error_widget = g_hash_table_lookup (window->errors, account);
333 gtk_widget_destroy (error_widget);
334 g_hash_table_remove (window->errors, account);
338 main_window_error_display (EmpathyMainWindow *window,
340 const gchar *message)
345 GtkWidget *button_edit;
346 GtkWidget *alignment;
351 GtkWidget *button_close;
354 child = g_hash_table_lookup (window->errors, account);
356 label = g_object_get_data (G_OBJECT (child), "label");
358 /* Just set the latest error and return */
359 str = g_markup_printf_escaped ("<b>%s</b>\n%s",
360 tp_account_get_display_name (account),
362 gtk_label_set_markup (GTK_LABEL (label), str);
368 child = gtk_vbox_new (FALSE, 0);
369 gtk_box_pack_start (GTK_BOX (window->errors_vbox), child, FALSE, TRUE, 0);
370 gtk_container_set_border_width (GTK_CONTAINER (child), 6);
371 gtk_widget_show (child);
373 table = gtk_table_new (2, 4, FALSE);
374 gtk_widget_show (table);
375 gtk_box_pack_start (GTK_BOX (child), table, TRUE, TRUE, 0);
376 gtk_table_set_row_spacings (GTK_TABLE (table), 12);
377 gtk_table_set_col_spacings (GTK_TABLE (table), 6);
379 image = gtk_image_new_from_stock (GTK_STOCK_DISCONNECT, GTK_ICON_SIZE_MENU);
380 gtk_widget_show (image);
381 gtk_table_attach (GTK_TABLE (table), image, 0, 1, 0, 2,
382 (GtkAttachOptions) (GTK_FILL),
383 (GtkAttachOptions) (GTK_FILL), 0, 0);
384 gtk_misc_set_alignment (GTK_MISC (image), 0.5, 0);
386 button_edit = gtk_button_new ();
387 gtk_widget_show (button_edit);
388 gtk_table_attach (GTK_TABLE (table), button_edit, 1, 2, 1, 2,
389 (GtkAttachOptions) (GTK_FILL),
390 (GtkAttachOptions) (0), 0, 0);
392 alignment = gtk_alignment_new (0.5, 0.5, 0, 0);
393 gtk_widget_show (alignment);
394 gtk_container_add (GTK_CONTAINER (button_edit), alignment);
396 hbox = gtk_hbox_new (FALSE, 2);
397 gtk_widget_show (hbox);
398 gtk_container_add (GTK_CONTAINER (alignment), hbox);
400 image = gtk_image_new_from_stock (GTK_STOCK_EDIT, GTK_ICON_SIZE_BUTTON);
401 gtk_widget_show (image);
402 gtk_box_pack_start (GTK_BOX (hbox), image, FALSE, FALSE, 0);
404 label = gtk_label_new_with_mnemonic (_("_Edit account"));
405 gtk_widget_show (label);
406 gtk_box_pack_start (GTK_BOX (hbox), label, FALSE, FALSE, 0);
408 fixed = gtk_fixed_new ();
409 gtk_widget_show (fixed);
410 gtk_table_attach (GTK_TABLE (table), fixed, 2, 3, 1, 2,
411 (GtkAttachOptions) (GTK_EXPAND | GTK_FILL),
412 (GtkAttachOptions) (GTK_FILL), 0, 0);
414 vbox = gtk_vbox_new (FALSE, 6);
415 gtk_widget_show (vbox);
416 gtk_table_attach (GTK_TABLE (table), vbox, 3, 4, 0, 2,
417 (GtkAttachOptions) (GTK_FILL),
418 (GtkAttachOptions) (GTK_FILL), 0, 0);
420 button_close = gtk_button_new ();
421 gtk_widget_show (button_close);
422 gtk_box_pack_start (GTK_BOX (vbox), button_close, FALSE, FALSE, 0);
423 gtk_button_set_relief (GTK_BUTTON (button_close), GTK_RELIEF_NONE);
426 image = gtk_image_new_from_stock ("gtk-close", GTK_ICON_SIZE_MENU);
427 gtk_widget_show (image);
428 gtk_container_add (GTK_CONTAINER (button_close), image);
430 label = gtk_label_new ("");
431 gtk_widget_show (label);
432 gtk_table_attach (GTK_TABLE (table), label, 1, 3, 0, 1,
433 (GtkAttachOptions) (GTK_EXPAND | GTK_SHRINK | GTK_FILL),
434 (GtkAttachOptions) (GTK_EXPAND | GTK_SHRINK | GTK_FILL), 0, 0);
435 gtk_widget_set_size_request (label, 175, -1);
436 gtk_label_set_use_markup (GTK_LABEL (label), TRUE);
437 gtk_label_set_line_wrap (GTK_LABEL (label), TRUE);
438 gtk_misc_set_alignment (GTK_MISC (label), 0, 0);
440 str = g_markup_printf_escaped ("<b>%s</b>\n%s",
441 tp_account_get_display_name (account),
443 gtk_label_set_markup (GTK_LABEL (label), str);
446 g_object_set_data (G_OBJECT (child), "label", label);
447 g_object_set_data_full (G_OBJECT (button_edit),
448 "account", g_object_ref (account),
450 g_object_set_data_full (G_OBJECT (button_close),
451 "account", g_object_ref (account),
454 g_signal_connect (button_edit, "clicked",
455 G_CALLBACK (main_window_error_edit_clicked_cb),
458 g_signal_connect (button_close, "clicked",
459 G_CALLBACK (main_window_error_clear_clicked_cb),
462 gtk_widget_show (window->errors_vbox);
464 g_hash_table_insert (window->errors, g_object_ref (account), child);
468 main_window_update_status (EmpathyMainWindow *window)
470 gboolean connected, connecting;
473 connected = empathy_account_manager_get_accounts_connected (&connecting);
475 /* Update the spinner state */
477 ephy_spinner_start (EPHY_SPINNER (window->throbber));
479 ephy_spinner_stop (EPHY_SPINNER (window->throbber));
482 /* Update widgets sensibility */
483 for (l = window->actions_connected; l; l = l->next) {
484 gtk_action_set_sensitive (l->data, connected);
489 main_window_connection_changed_cb (TpAccount *account,
493 gchar *dbus_error_name,
495 EmpathyMainWindow *window)
497 main_window_update_status (window);
499 if (current == TP_CONNECTION_STATUS_DISCONNECTED &&
500 reason != TP_CONNECTION_STATUS_REASON_REQUESTED) {
501 const gchar *message;
504 case TP_CONNECTION_STATUS_REASON_NONE_SPECIFIED:
505 message = _("No error specified");
507 case TP_CONNECTION_STATUS_REASON_NETWORK_ERROR:
508 message = _("Network error");
510 case TP_CONNECTION_STATUS_REASON_AUTHENTICATION_FAILED:
511 message = _("Authentication failed");
513 case TP_CONNECTION_STATUS_REASON_ENCRYPTION_ERROR:
514 message = _("Encryption error");
516 case TP_CONNECTION_STATUS_REASON_NAME_IN_USE:
517 message = _("Name in use");
519 case TP_CONNECTION_STATUS_REASON_CERT_NOT_PROVIDED:
520 message = _("Certificate not provided");
522 case TP_CONNECTION_STATUS_REASON_CERT_UNTRUSTED:
523 message = _("Certificate untrusted");
525 case TP_CONNECTION_STATUS_REASON_CERT_EXPIRED:
526 message = _("Certificate expired");
528 case TP_CONNECTION_STATUS_REASON_CERT_NOT_ACTIVATED:
529 message = _("Certificate not activated");
531 case TP_CONNECTION_STATUS_REASON_CERT_HOSTNAME_MISMATCH:
532 message = _("Certificate hostname mismatch");
534 case TP_CONNECTION_STATUS_REASON_CERT_FINGERPRINT_MISMATCH:
535 message = _("Certificate fingerprint mismatch");
537 case TP_CONNECTION_STATUS_REASON_CERT_SELF_SIGNED:
538 message = _("Certificate self-signed");
540 case TP_CONNECTION_STATUS_REASON_CERT_OTHER_ERROR:
541 message = _("Certificate error");
544 message = _("Unknown error");
548 main_window_error_display (window, account, message);
551 if (current == TP_CONNECTION_STATUS_DISCONNECTED) {
552 empathy_sound_play (GTK_WIDGET (window->window),
553 EMPATHY_SOUND_ACCOUNT_DISCONNECTED);
556 if (current == TP_CONNECTION_STATUS_CONNECTED) {
557 GtkWidget *error_widget;
559 empathy_sound_play (GTK_WIDGET (window->window),
560 EMPATHY_SOUND_ACCOUNT_CONNECTED);
562 /* Account connected without error, remove error message if any */
563 error_widget = g_hash_table_lookup (window->errors, account);
565 gtk_widget_destroy (error_widget);
566 g_hash_table_remove (window->errors, account);
572 main_window_contact_presence_changed_cb (EmpathyContactMonitor *monitor,
573 EmpathyContact *contact,
574 TpConnectionPresenceType current,
575 TpConnectionPresenceType previous,
576 EmpathyMainWindow *window)
579 gboolean should_play = FALSE;
582 account = empathy_contact_get_account (contact);
583 idle = empathy_idle_dup_singleton ();
585 should_play = !empathy_idle_account_is_just_connected (idle, account);
590 if (tp_connection_presence_type_cmp_availability (previous,
591 TP_CONNECTION_PRESENCE_TYPE_OFFLINE) > 0)
593 /* contact was online */
594 if (tp_connection_presence_type_cmp_availability (current,
595 TP_CONNECTION_PRESENCE_TYPE_OFFLINE) <= 0)
596 /* someone is logging off */
597 empathy_sound_play (GTK_WIDGET (window->window),
598 EMPATHY_SOUND_CONTACT_DISCONNECTED);
602 /* contact was offline */
603 if (tp_connection_presence_type_cmp_availability (current,
604 TP_CONNECTION_PRESENCE_TYPE_OFFLINE) > 0)
605 /* someone is logging in */
606 empathy_sound_play (GTK_WIDGET (window->window),
607 EMPATHY_SOUND_CONTACT_CONNECTED);
611 g_object_unref (idle);
615 main_window_accels_load (void)
619 filename = g_build_filename (g_get_user_config_dir (), PACKAGE_NAME, ACCELS_FILENAME, NULL);
620 if (g_file_test (filename, G_FILE_TEST_EXISTS)) {
621 DEBUG ("Loading from:'%s'", filename);
622 gtk_accel_map_load (filename);
629 main_window_accels_save (void)
632 gchar *file_with_path;
634 dir = g_build_filename (g_get_user_config_dir (), PACKAGE_NAME, NULL);
635 g_mkdir_with_parents (dir, S_IRUSR | S_IWUSR | S_IXUSR);
636 file_with_path = g_build_filename (dir, ACCELS_FILENAME, NULL);
639 DEBUG ("Saving to:'%s'", file_with_path);
640 gtk_accel_map_save (file_with_path);
642 g_free (file_with_path);
646 main_window_destroy_cb (GtkWidget *widget,
647 EmpathyMainWindow *window)
649 /* Save user-defined accelerators. */
650 main_window_accels_save ();
652 if (window->size_timeout_id) {
653 g_source_remove (window->size_timeout_id);
654 main_window_configure_event_timeout_cb (window);
657 g_list_free (window->actions_connected);
659 g_object_unref (window->account_manager);
660 g_object_unref (window->list_store);
661 g_hash_table_destroy (window->errors);
663 g_signal_handlers_disconnect_by_func (window->event_manager,
664 main_window_event_added_cb,
666 g_signal_handlers_disconnect_by_func (window->event_manager,
667 main_window_event_removed_cb,
669 g_object_unref (window->event_manager);
670 g_object_unref (window->ui_manager);
676 main_window_chat_quit_cb (GtkAction *action,
677 EmpathyMainWindow *window)
683 main_window_view_history_cb (GtkAction *action,
684 EmpathyMainWindow *window)
686 empathy_log_window_show (NULL, NULL, FALSE, GTK_WINDOW (window->window));
690 main_window_chat_new_message_cb (GtkAction *action,
691 EmpathyMainWindow *window)
693 empathy_new_message_dialog_show (GTK_WINDOW (window->window));
697 main_window_chat_add_contact_cb (GtkAction *action,
698 EmpathyMainWindow *window)
700 empathy_new_contact_dialog_show (GTK_WINDOW (window->window));
704 main_window_view_show_ft_manager (GtkAction *action,
705 EmpathyMainWindow *window)
707 empathy_ft_manager_show ();
711 main_window_view_show_offline_cb (GtkToggleAction *action,
712 EmpathyMainWindow *window)
716 current = gtk_toggle_action_get_active (action);
717 empathy_conf_set_bool (empathy_conf_get (),
718 EMPATHY_PREFS_CONTACTS_SHOW_OFFLINE,
721 /* Turn off sound just while we alter the contact list. */
722 // FIXME: empathy_sound_set_enabled (FALSE);
723 empathy_contact_list_store_set_show_offline (window->list_store, current);
724 //empathy_sound_set_enabled (TRUE);
728 main_window_notify_sort_contact_cb (EmpathyConf *conf,
730 EmpathyMainWindow *window)
734 if (empathy_conf_get_string (conf, key, &str) && str) {
736 GEnumClass *enum_class;
737 GEnumValue *enum_value;
739 type = empathy_contact_list_store_sort_get_type ();
740 enum_class = G_ENUM_CLASS (g_type_class_peek (type));
741 enum_value = g_enum_get_value_by_nick (enum_class, str);
743 /* By changing the value of the GtkRadioAction,
744 it emits a signal that calls main_window_view_sort_contacts_cb
745 which updates the contacts list */
746 gtk_radio_action_set_current_value (window->sort_by_name,
749 g_warning ("Wrong value for sort_criterium configuration : %s", str);
756 main_window_view_sort_contacts_cb (GtkRadioAction *action,
757 GtkRadioAction *current,
758 EmpathyMainWindow *window)
760 EmpathyContactListStoreSort value;
763 GEnumClass *enum_class;
764 GEnumValue *enum_value;
766 value = gtk_radio_action_get_current_value (action);
767 group = gtk_radio_action_get_group (action);
769 /* Get string from index */
770 type = empathy_contact_list_store_sort_get_type ();
771 enum_class = G_ENUM_CLASS (g_type_class_peek (type));
772 enum_value = g_enum_get_value (enum_class, g_slist_index (group, current));
775 g_warning ("No GEnumValue for EmpathyContactListSort with GtkRadioAction index:%d",
776 g_slist_index (group, action));
778 empathy_conf_set_string (empathy_conf_get (),
779 EMPATHY_PREFS_CONTACTS_SORT_CRITERIUM,
780 enum_value->value_nick);
782 empathy_contact_list_store_set_sort_criterium (window->list_store, value);
785 /* Matches GtkRadioAction values set in empathy-main-window.ui */
786 #define CONTACT_LIST_NORMAL_SIZE_WITH_AVATARS 0
787 #define CONTACT_LIST_NORMAL_SIZE 1
788 #define CONTACT_LIST_COMPACT_SIZE 2
791 main_window_view_contacts_list_size_cb (GtkRadioAction *action,
792 GtkRadioAction *current,
793 EmpathyMainWindow *window)
797 value = gtk_radio_action_get_current_value (action);
799 empathy_conf_set_bool (empathy_conf_get (),
800 EMPATHY_PREFS_UI_SHOW_AVATARS,
801 value == CONTACT_LIST_NORMAL_SIZE_WITH_AVATARS);
802 empathy_conf_set_bool (empathy_conf_get (),
803 EMPATHY_PREFS_UI_COMPACT_CONTACT_LIST,
804 value == CONTACT_LIST_COMPACT_SIZE);
806 empathy_contact_list_store_set_show_avatars (window->list_store,
807 value == CONTACT_LIST_NORMAL_SIZE_WITH_AVATARS);
808 empathy_contact_list_store_set_is_compact (window->list_store,
809 value == CONTACT_LIST_COMPACT_SIZE);
813 main_window_notify_contact_list_size_cb (EmpathyConf *conf,
815 EmpathyMainWindow *window)
817 gboolean show_avatars;
818 gboolean compact_contact_list;
819 gint value = CONTACT_LIST_NORMAL_SIZE_WITH_AVATARS;
821 if (empathy_conf_get_bool (conf,
822 EMPATHY_PREFS_UI_SHOW_AVATARS,
824 && empathy_conf_get_bool (conf,
825 EMPATHY_PREFS_UI_COMPACT_CONTACT_LIST,
826 &compact_contact_list)) {
827 if (compact_contact_list) {
828 value = CONTACT_LIST_COMPACT_SIZE;
829 } else if (show_avatars) {
830 value = CONTACT_LIST_NORMAL_SIZE_WITH_AVATARS;
832 value = CONTACT_LIST_NORMAL_SIZE;
835 /* By changing the value of the GtkRadioAction,
836 it emits a signal that calls main_window_view_contacts_list_size_cb
837 which updates the contacts list */
838 gtk_radio_action_set_current_value (window->normal_with_avatars, value);
842 main_window_view_show_map_cb (GtkCheckMenuItem *item,
843 EmpathyMainWindow *window)
845 #if HAVE_LIBCHAMPLAIN
846 empathy_map_view_show ();
851 main_window_favorite_chatroom_join (EmpathyChatroom *chatroom)
854 TpConnection *connection;
857 account = empathy_chatroom_get_account (chatroom);
858 connection = tp_account_get_connection (account);
859 room = empathy_chatroom_get_room (chatroom);
861 if (connection != NULL) {
862 DEBUG ("Requesting channel for '%s'", room);
863 empathy_dispatcher_join_muc (connection, room, NULL, NULL);
868 main_window_favorite_chatroom_menu_activate_cb (GtkMenuItem *menu_item,
869 EmpathyChatroom *chatroom)
871 main_window_favorite_chatroom_join (chatroom);
875 main_window_favorite_chatroom_menu_add (EmpathyMainWindow *window,
876 EmpathyChatroom *chatroom)
878 GtkWidget *menu_item;
881 if (g_object_get_data (G_OBJECT (chatroom), "menu_item")) {
885 name = empathy_chatroom_get_name (chatroom);
886 menu_item = gtk_menu_item_new_with_label (name);
888 g_object_set_data (G_OBJECT (chatroom), "menu_item", menu_item);
889 g_signal_connect (menu_item, "activate",
890 G_CALLBACK (main_window_favorite_chatroom_menu_activate_cb),
893 gtk_menu_shell_insert (GTK_MENU_SHELL (window->room_menu),
896 gtk_widget_show (menu_item);
900 main_window_favorite_chatroom_menu_added_cb (EmpathyChatroomManager *manager,
901 EmpathyChatroom *chatroom,
902 EmpathyMainWindow *window)
904 main_window_favorite_chatroom_menu_add (window, chatroom);
905 gtk_widget_show (window->room_separator);
906 gtk_action_set_sensitive (window->room_join_favorites, TRUE);
910 main_window_favorite_chatroom_menu_removed_cb (EmpathyChatroomManager *manager,
911 EmpathyChatroom *chatroom,
912 EmpathyMainWindow *window)
914 GtkWidget *menu_item;
917 menu_item = g_object_get_data (G_OBJECT (chatroom), "menu_item");
918 g_object_set_data (G_OBJECT (chatroom), "menu_item", NULL);
919 gtk_widget_destroy (menu_item);
921 chatrooms = empathy_chatroom_manager_get_chatrooms (window->chatroom_manager, NULL);
923 gtk_widget_show (window->room_separator);
925 gtk_widget_hide (window->room_separator);
928 gtk_action_set_sensitive (window->room_join_favorites, chatrooms != NULL);
929 g_list_free (chatrooms);
933 main_window_favorite_chatroom_menu_setup (EmpathyMainWindow *window)
935 GList *chatrooms, *l;
938 window->chatroom_manager = empathy_chatroom_manager_dup_singleton (NULL);
939 chatrooms = empathy_chatroom_manager_get_chatrooms (window->chatroom_manager, NULL);
940 room = gtk_ui_manager_get_widget (window->ui_manager,
942 window->room_menu = gtk_menu_item_get_submenu (GTK_MENU_ITEM (room));
943 window->room_separator = gtk_ui_manager_get_widget (window->ui_manager,
944 "/menubar/room/room_separator");
946 for (l = chatrooms; l; l = l->next) {
947 main_window_favorite_chatroom_menu_add (window, l->data);
951 gtk_widget_hide (window->room_separator);
954 gtk_action_set_sensitive (window->room_join_favorites, chatrooms != NULL);
956 g_signal_connect (window->chatroom_manager, "chatroom-added",
957 G_CALLBACK (main_window_favorite_chatroom_menu_added_cb),
959 g_signal_connect (window->chatroom_manager, "chatroom-removed",
960 G_CALLBACK (main_window_favorite_chatroom_menu_removed_cb),
963 g_list_free (chatrooms);
967 main_window_room_join_new_cb (GtkAction *action,
968 EmpathyMainWindow *window)
970 empathy_new_chatroom_dialog_show (GTK_WINDOW (window->window));
974 main_window_room_join_favorites_cb (GtkAction *action,
975 EmpathyMainWindow *window)
977 GList *chatrooms, *l;
979 chatrooms = empathy_chatroom_manager_get_chatrooms (window->chatroom_manager, NULL);
980 for (l = chatrooms; l; l = l->next) {
981 main_window_favorite_chatroom_join (l->data);
983 g_list_free (chatrooms);
987 main_window_room_manage_favorites_cb (GtkAction *action,
988 EmpathyMainWindow *window)
990 empathy_chatrooms_window_show (GTK_WINDOW (window->window));
994 main_window_edit_cb (GtkAction *action,
995 EmpathyMainWindow *window)
999 /* FIXME: It should use the UIManager to merge the contact/group submenu */
1000 submenu = empathy_contact_list_view_get_contact_menu (window->list_view);
1005 item = GTK_MENU_ITEM (window->edit_context);
1006 label = gtk_bin_get_child (GTK_BIN (item));
1007 gtk_label_set_text (GTK_LABEL (label), _("Contact"));
1009 gtk_widget_show (window->edit_context);
1010 gtk_widget_show (window->edit_context_separator);
1012 gtk_menu_item_set_submenu (item, submenu);
1017 submenu = empathy_contact_list_view_get_group_menu (window->list_view);
1022 item = GTK_MENU_ITEM (window->edit_context);
1023 label = gtk_bin_get_child (GTK_BIN (item));
1024 gtk_label_set_text (GTK_LABEL (label), _("Group"));
1026 gtk_widget_show (window->edit_context);
1027 gtk_widget_show (window->edit_context_separator);
1029 gtk_menu_item_set_submenu (item, submenu);
1034 gtk_widget_hide (window->edit_context);
1035 gtk_widget_hide (window->edit_context_separator);
1041 main_window_edit_accounts_cb (GtkAction *action,
1042 EmpathyMainWindow *window)
1044 empathy_accounts_dialog_show (GTK_WINDOW (window->window), NULL);
1048 main_window_edit_personal_information_cb (GtkAction *action,
1049 EmpathyMainWindow *window)
1051 empathy_contact_personal_dialog_show (GTK_WINDOW (window->window));
1055 main_window_edit_preferences_cb (GtkAction *action,
1056 EmpathyMainWindow *window)
1058 empathy_preferences_show (GTK_WINDOW (window->window));
1062 main_window_help_about_cb (GtkAction *action,
1063 EmpathyMainWindow *window)
1065 empathy_about_dialog_new (GTK_WINDOW (window->window));
1069 main_window_help_debug_cb (GtkAction *action,
1070 EmpathyMainWindow *window)
1072 empathy_debug_window_new (NULL);
1076 main_window_help_contents_cb (GtkAction *action,
1077 EmpathyMainWindow *window)
1079 empathy_url_show (window->window, "ghelp:empathy");
1083 main_window_throbber_button_press_event_cb (GtkWidget *throbber_ebox,
1084 GdkEventButton *event,
1085 EmpathyMainWindow *window)
1087 if (event->type != GDK_BUTTON_PRESS ||
1088 event->button != 1) {
1092 empathy_accounts_dialog_show (GTK_WINDOW (window->window), NULL);
1098 main_window_configure_event_timeout_cb (EmpathyMainWindow *window)
1102 gtk_window_get_size (GTK_WINDOW (window->window), &w, &h);
1103 gtk_window_get_position (GTK_WINDOW (window->window), &x, &y);
1105 empathy_geometry_save (GEOMETRY_NAME, x, y, w, h);
1107 window->size_timeout_id = 0;
1113 main_window_configure_event_cb (GtkWidget *widget,
1114 GdkEventConfigure *event,
1115 EmpathyMainWindow *window)
1117 if (window->size_timeout_id) {
1118 g_source_remove (window->size_timeout_id);
1121 window->size_timeout_id = g_timeout_add_seconds (1,
1122 (GSourceFunc) main_window_configure_event_timeout_cb,
1129 main_window_account_removed_cb (TpAccountManager *manager,
1131 EmpathyMainWindow *window)
1135 a = tp_account_manager_get_valid_accounts (manager);
1137 gtk_action_set_sensitive (window->view_history,
1138 g_list_length (a) > 0);
1144 main_window_account_validity_changed_cb (TpAccountManager *manager,
1147 EmpathyMainWindow *window)
1149 main_window_account_removed_cb (manager, account, window);
1153 main_window_notify_show_offline_cb (EmpathyConf *conf,
1155 gpointer toggle_action)
1157 gboolean show_offline;
1159 if (empathy_conf_get_bool (conf, key, &show_offline)) {
1160 gtk_toggle_action_set_active (toggle_action, show_offline);
1165 main_window_connection_items_setup (EmpathyMainWindow *window,
1171 const gchar *actions_connected[] = {
1175 "edit_personal_information"
1178 for (i = 0, list = NULL; i < G_N_ELEMENTS (actions_connected); i++) {
1179 action = gtk_builder_get_object (gui, actions_connected[i]);
1180 list = g_list_prepend (list, action);
1183 window->actions_connected = list;
1187 empathy_main_window_get (void)
1189 return main_window != NULL ? main_window->window : NULL;
1193 account_manager_prepared_cb (GObject *source_object,
1194 GAsyncResult *result,
1197 GList *accounts, *j;
1198 TpAccountManager *manager = TP_ACCOUNT_MANAGER (source_object);
1199 EmpathyMainWindow *window = user_data;
1200 GError *error = NULL;
1202 if (!tp_account_manager_prepare_finish (manager, result, &error)) {
1203 DEBUG ("Failed to prepare account manager: %s", error->message);
1204 g_error_free (error);
1208 accounts = tp_account_manager_get_valid_accounts (window->account_manager);
1209 for (j = accounts; j != NULL; j = j->next) {
1210 TpAccount *account = TP_ACCOUNT (j->data);
1212 g_signal_connect (account, "status-changed",
1213 G_CALLBACK (main_window_connection_changed_cb),
1217 main_window_update_status (window);
1219 /* Disable the "Previous Conversations" menu entry if there is no account */
1220 gtk_action_set_sensitive (window->view_history,
1221 g_list_length (accounts) > 0);
1223 g_list_free (accounts);
1227 empathy_main_window_show (void)
1229 EmpathyMainWindow *window;
1230 EmpathyContactList *list_iface;
1231 EmpathyContactMonitor *monitor;
1235 GtkToggleAction *show_offline_widget;
1237 GtkAction *show_map_widget;
1239 gboolean show_offline;
1245 empathy_window_present (GTK_WINDOW (main_window->window), TRUE);
1246 return main_window->window;
1249 main_window = g_new0 (EmpathyMainWindow, 1);
1250 window = main_window;
1252 /* Set up interface */
1253 filename = empathy_file_lookup ("empathy-main-window.ui", "src");
1254 gui = empathy_builder_get_file (filename,
1255 "main_window", &window->window,
1256 "main_vbox", &window->main_vbox,
1257 "errors_vbox", &window->errors_vbox,
1258 "ui_manager", &window->ui_manager,
1259 "view_show_offline", &show_offline_widget,
1260 "view_sort_by_name", &window->sort_by_name,
1261 "view_sort_by_status", &window->sort_by_status,
1262 "view_normal_size_with_avatars", &window->normal_with_avatars,
1263 "view_normal_size", &window->normal_size,
1264 "view_compact_size", &window->compact_size,
1265 "view_history", &window->view_history,
1266 "view_show_map", &show_map_widget,
1267 "room_join_favorites", &window->room_join_favorites,
1268 "presence_toolbar", &window->presence_toolbar,
1269 "roster_scrolledwindow", &sw,
1273 empathy_builder_connect (gui, window,
1274 "main_window", "destroy", main_window_destroy_cb,
1275 "main_window", "configure_event", main_window_configure_event_cb,
1276 "chat_quit", "activate", main_window_chat_quit_cb,
1277 "chat_new_message", "activate", main_window_chat_new_message_cb,
1278 "view_history", "activate", main_window_view_history_cb,
1279 "room_join_new", "activate", main_window_room_join_new_cb,
1280 "room_join_favorites", "activate", main_window_room_join_favorites_cb,
1281 "room_manage_favorites", "activate", main_window_room_manage_favorites_cb,
1282 "chat_add_contact", "activate", main_window_chat_add_contact_cb,
1283 "view_show_ft_manager", "activate", main_window_view_show_ft_manager,
1284 "view_show_offline", "toggled", main_window_view_show_offline_cb,
1285 "view_sort_by_name", "changed", main_window_view_sort_contacts_cb,
1286 "view_normal_size_with_avatars", "changed", main_window_view_contacts_list_size_cb,
1287 "view_show_map", "activate", main_window_view_show_map_cb,
1288 "edit", "activate", main_window_edit_cb,
1289 "edit_accounts", "activate", main_window_edit_accounts_cb,
1290 "edit_personal_information", "activate", main_window_edit_personal_information_cb,
1291 "edit_preferences", "activate", main_window_edit_preferences_cb,
1292 "help_about", "activate", main_window_help_about_cb,
1293 "help_debug", "activate", main_window_help_debug_cb,
1294 "help_contents", "activate", main_window_help_contents_cb,
1297 /* Set up connection related widgets. */
1298 main_window_connection_items_setup (window, gui);
1300 g_object_ref (window->ui_manager);
1301 g_object_unref (gui);
1303 #if !HAVE_LIBCHAMPLAIN
1304 gtk_action_set_visible (show_map_widget, FALSE);
1307 window->account_manager = tp_account_manager_dup ();
1309 tp_account_manager_prepare_async (window->account_manager, NULL,
1310 account_manager_prepared_cb, window);
1312 window->errors = g_hash_table_new_full (g_direct_hash,
1318 main_window_favorite_chatroom_menu_setup (window);
1320 window->edit_context = gtk_ui_manager_get_widget (window->ui_manager,
1321 "/menubar/edit/edit_context");
1322 window->edit_context_separator = gtk_ui_manager_get_widget (window->ui_manager,
1323 "/menubar/edit/edit_context_separator");
1324 gtk_widget_hide (window->edit_context);
1325 gtk_widget_hide (window->edit_context_separator);
1327 /* Set up contact list. */
1328 empathy_status_presets_get_all ();
1330 /* Set up presence chooser */
1331 window->presence_chooser = empathy_presence_chooser_new ();
1332 gtk_widget_show (window->presence_chooser);
1333 item = gtk_tool_item_new ();
1334 gtk_widget_show (GTK_WIDGET (item));
1335 gtk_container_add (GTK_CONTAINER (item), window->presence_chooser);
1336 gtk_tool_item_set_is_important (item, TRUE);
1337 gtk_tool_item_set_expand (item, TRUE);
1338 gtk_toolbar_insert (GTK_TOOLBAR (window->presence_toolbar), item, -1);
1340 /* Set up the throbber */
1341 ebox = gtk_event_box_new ();
1342 gtk_event_box_set_visible_window (GTK_EVENT_BOX (ebox), FALSE);
1343 gtk_widget_set_tooltip_text (ebox, _("Show and edit accounts"));
1344 g_signal_connect (ebox,
1345 "button-press-event",
1346 G_CALLBACK (main_window_throbber_button_press_event_cb),
1348 gtk_widget_show (ebox);
1350 window->throbber = ephy_spinner_new ();
1351 ephy_spinner_set_size (EPHY_SPINNER (window->throbber), GTK_ICON_SIZE_LARGE_TOOLBAR);
1352 gtk_container_add (GTK_CONTAINER (ebox), window->throbber);
1353 gtk_widget_show (window->throbber);
1355 item = gtk_tool_item_new ();
1356 gtk_container_add (GTK_CONTAINER (item), ebox);
1357 gtk_toolbar_insert (GTK_TOOLBAR (window->presence_toolbar), item, -1);
1358 gtk_widget_show (GTK_WIDGET (item));
1360 list_iface = EMPATHY_CONTACT_LIST (empathy_contact_manager_dup_singleton ());
1361 monitor = empathy_contact_list_get_monitor (list_iface);
1362 window->list_store = empathy_contact_list_store_new (list_iface);
1363 window->list_view = empathy_contact_list_view_new (window->list_store,
1364 EMPATHY_CONTACT_LIST_FEATURE_ALL,
1365 EMPATHY_CONTACT_FEATURE_ALL);
1366 g_signal_connect (monitor, "contact-presence-changed",
1367 G_CALLBACK (main_window_contact_presence_changed_cb), window);
1368 g_object_unref (list_iface);
1370 gtk_widget_show (GTK_WIDGET (window->list_view));
1371 gtk_container_add (GTK_CONTAINER (sw),
1372 GTK_WIDGET (window->list_view));
1373 g_signal_connect (window->list_view, "row-activated",
1374 G_CALLBACK (main_window_row_activated_cb),
1377 /* Load user-defined accelerators. */
1378 main_window_accels_load ();
1380 /* Set window size. */
1381 empathy_geometry_load (GEOMETRY_NAME, &x, &y, &w, &h);
1383 if (w >= 1 && h >= 1) {
1384 /* Use the defaults from the ui file if we
1385 * don't have good w, h geometry.
1387 DEBUG ("Configuring window default size w:%d, h:%d", w, h);
1388 gtk_window_set_default_size (GTK_WINDOW (window->window), w, h);
1391 if (x >= 0 && y >= 0) {
1392 /* Let the window manager position it if we
1393 * don't have good x, y coordinates.
1395 DEBUG ("Configuring window default position x:%d, y:%d", x, y);
1396 gtk_window_move (GTK_WINDOW (window->window), x, y);
1399 /* Enable event handling */
1400 window->event_manager = empathy_event_manager_dup_singleton ();
1401 g_signal_connect (window->event_manager, "event-added",
1402 G_CALLBACK (main_window_event_added_cb),
1404 g_signal_connect (window->event_manager, "event-removed",
1405 G_CALLBACK (main_window_event_removed_cb),
1408 g_signal_connect (window->account_manager, "account-validity-changed",
1409 G_CALLBACK (main_window_account_validity_changed_cb),
1411 g_signal_connect (window->account_manager, "account-removed",
1412 G_CALLBACK (main_window_account_removed_cb),
1415 l = empathy_event_manager_get_events (window->event_manager);
1417 main_window_event_added_cb (window->event_manager,
1422 conf = empathy_conf_get ();
1424 /* Show offline ? */
1425 empathy_conf_get_bool (conf,
1426 EMPATHY_PREFS_CONTACTS_SHOW_OFFLINE,
1428 empathy_conf_notify_add (conf,
1429 EMPATHY_PREFS_CONTACTS_SHOW_OFFLINE,
1430 main_window_notify_show_offline_cb,
1431 show_offline_widget);
1433 gtk_toggle_action_set_active (show_offline_widget, show_offline);
1435 /* Sort by name / by status ? */
1436 empathy_conf_notify_add (conf,
1437 EMPATHY_PREFS_CONTACTS_SORT_CRITERIUM,
1438 (EmpathyConfNotifyFunc) main_window_notify_sort_contact_cb,
1441 main_window_notify_sort_contact_cb (conf,
1442 EMPATHY_PREFS_CONTACTS_SORT_CRITERIUM,
1445 /* Contacts list size */
1446 empathy_conf_notify_add (conf,
1447 EMPATHY_PREFS_UI_COMPACT_CONTACT_LIST,
1448 (EmpathyConfNotifyFunc) main_window_notify_contact_list_size_cb,
1450 empathy_conf_notify_add (conf,
1451 EMPATHY_PREFS_UI_SHOW_AVATARS,
1452 (EmpathyConfNotifyFunc) main_window_notify_contact_list_size_cb,
1455 main_window_notify_contact_list_size_cb (conf,
1456 EMPATHY_PREFS_UI_SHOW_AVATARS,
1459 return window->window;