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 <libmissioncontrol/mission-control.h>
54 #include "empathy-accounts-dialog.h"
55 #include "empathy-main-window.h"
56 #include "ephy-spinner.h"
57 #include "empathy-preferences.h"
58 #include "empathy-about-dialog.h"
59 #include "empathy-debug-dialog.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;
85 EmpathyAccountManager *account_manager;
86 EmpathyChatroomManager *chatroom_manager;
87 EmpathyEventManager *event_manager;
88 guint flash_timeout_id;
94 GtkWidget *presence_toolbar;
95 GtkWidget *presence_chooser;
96 GtkWidget *errors_vbox;
98 GtkRadioAction *sort_by_name;
99 GtkRadioAction *sort_by_status;
100 GtkRadioAction *normal_with_avatars;
101 GtkRadioAction *normal_size;
102 GtkRadioAction *compact_size;
104 GtkUIManager *ui_manager;
105 GtkAction *view_history;
106 GtkAction *room_join_favorites;
107 GtkWidget *room_menu;
108 GtkWidget *room_separator;
109 GtkWidget *edit_context;
110 GtkWidget *edit_context_separator;
112 guint size_timeout_id;
115 /* Actions that are enabled when there are connected accounts */
116 GList *actions_connected;
119 static EmpathyMainWindow *window = NULL;
122 main_window_flash_stop (EmpathyMainWindow *window)
124 if (window->flash_timeout_id == 0) {
128 DEBUG ("Stop flashing");
129 g_source_remove (window->flash_timeout_id);
130 window->flash_timeout_id = 0;
131 window->flash_on = FALSE;
140 main_window_flash_foreach (GtkTreeModel *model,
145 FlashForeachData *data = (FlashForeachData *) user_data;
146 EmpathyContact *contact;
147 const gchar *icon_name;
148 GtkTreePath *parent_path = NULL;
149 GtkTreeIter parent_iter;
151 /* To be used with gtk_tree_model_foreach, update the status icon
152 * of the contact to show the event icon (on=TRUE) or the presence
154 gtk_tree_model_get (model, iter,
155 EMPATHY_CONTACT_LIST_STORE_COL_CONTACT, &contact,
158 if (contact != data->event->contact) {
160 g_object_unref (contact);
166 icon_name = data->event->icon_name;
168 icon_name = empathy_icon_name_for_contact (contact);
171 gtk_tree_store_set (GTK_TREE_STORE (model), iter,
172 EMPATHY_CONTACT_LIST_STORE_COL_ICON_STATUS, icon_name,
175 /* To make sure the parent is shown correctly, we emit
176 * the row-changed signal on the parent so it prompts
177 * it to be refreshed by the filter func.
179 if (gtk_tree_model_iter_parent (model, &parent_iter, iter)) {
180 parent_path = gtk_tree_model_get_path (model, &parent_iter);
183 gtk_tree_model_row_changed (model, parent_path, &parent_iter);
184 gtk_tree_path_free (parent_path);
187 g_object_unref (contact);
193 main_window_flash_cb (EmpathyMainWindow *window)
197 gboolean found_event = FALSE;
198 FlashForeachData data;
200 window->flash_on = !window->flash_on;
201 data.on = window->flash_on;
202 model = GTK_TREE_MODEL (window->list_store);
204 events = empathy_event_manager_get_events (window->event_manager);
205 for (l = events; l; l = l->next) {
206 data.event = l->data;
207 if (!data.event->contact || !data.event->must_ack) {
212 gtk_tree_model_foreach (model,
213 main_window_flash_foreach,
218 main_window_flash_stop (window);
225 main_window_flash_start (EmpathyMainWindow *window)
227 if (window->flash_timeout_id != 0) {
231 DEBUG ("Start flashing");
232 window->flash_timeout_id = g_timeout_add (FLASH_TIMEOUT,
233 (GSourceFunc) main_window_flash_cb,
235 main_window_flash_cb (window);
239 main_window_event_added_cb (EmpathyEventManager *manager,
241 EmpathyMainWindow *window)
243 if (event->contact) {
244 main_window_flash_start (window);
249 main_window_event_removed_cb (EmpathyEventManager *manager,
251 EmpathyMainWindow *window)
253 FlashForeachData data;
255 if (!event->contact) {
261 gtk_tree_model_foreach (GTK_TREE_MODEL (window->list_store),
262 main_window_flash_foreach,
267 main_window_row_activated_cb (EmpathyContactListView *view,
269 GtkTreeViewColumn *col,
270 EmpathyMainWindow *window)
272 EmpathyContact *contact;
277 model = GTK_TREE_MODEL (window->list_store);
278 gtk_tree_model_get_iter (model, &iter, path);
279 gtk_tree_model_get (model, &iter,
280 EMPATHY_CONTACT_LIST_STORE_COL_CONTACT, &contact,
287 /* If the contact has an event activate it, otherwise the
288 * default handler of row-activated will be called. */
289 events = empathy_event_manager_get_events (window->event_manager);
290 for (l = events; l; l = l->next) {
291 EmpathyEvent *event = l->data;
293 if (event->contact == contact) {
294 DEBUG ("Activate event");
295 empathy_event_activate (event);
297 /* We don't want the default handler of this signal
298 * (e.g. open a chat) */
299 g_signal_stop_emission_by_name (view, "row-activated");
304 g_object_unref (contact);
308 main_window_error_edit_clicked_cb (GtkButton *button,
309 EmpathyMainWindow *window)
311 EmpathyAccount *account;
312 GtkWidget *error_widget;
314 account = g_object_get_data (G_OBJECT (button), "account");
315 empathy_accounts_dialog_show (GTK_WINDOW (window->window), account);
317 error_widget = g_hash_table_lookup (window->errors, account);
318 gtk_widget_destroy (error_widget);
319 g_hash_table_remove (window->errors, account);
323 main_window_error_clear_clicked_cb (GtkButton *button,
324 EmpathyMainWindow *window)
326 EmpathyAccount *account;
327 GtkWidget *error_widget;
329 account = g_object_get_data (G_OBJECT (button), "account");
330 error_widget = g_hash_table_lookup (window->errors, account);
331 gtk_widget_destroy (error_widget);
332 g_hash_table_remove (window->errors, account);
336 main_window_error_display (EmpathyMainWindow *window,
337 EmpathyAccount *account,
338 const gchar *message)
343 GtkWidget *button_edit;
344 GtkWidget *alignment;
349 GtkWidget *button_close;
352 child = g_hash_table_lookup (window->errors, account);
354 label = g_object_get_data (G_OBJECT (child), "label");
356 /* Just set the latest error and return */
357 str = g_markup_printf_escaped ("<b>%s</b>\n%s",
358 empathy_account_get_display_name (account),
360 gtk_label_set_markup (GTK_LABEL (label), str);
366 child = gtk_vbox_new (FALSE, 0);
367 gtk_box_pack_start (GTK_BOX (window->errors_vbox), child, FALSE, TRUE, 0);
368 gtk_container_set_border_width (GTK_CONTAINER (child), 6);
369 gtk_widget_show (child);
371 table = gtk_table_new (2, 4, FALSE);
372 gtk_widget_show (table);
373 gtk_box_pack_start (GTK_BOX (child), table, TRUE, TRUE, 0);
374 gtk_table_set_row_spacings (GTK_TABLE (table), 12);
375 gtk_table_set_col_spacings (GTK_TABLE (table), 6);
377 image = gtk_image_new_from_stock (GTK_STOCK_DISCONNECT, GTK_ICON_SIZE_MENU);
378 gtk_widget_show (image);
379 gtk_table_attach (GTK_TABLE (table), image, 0, 1, 0, 2,
380 (GtkAttachOptions) (GTK_FILL),
381 (GtkAttachOptions) (GTK_FILL), 0, 0);
382 gtk_misc_set_alignment (GTK_MISC (image), 0.5, 0);
384 button_edit = gtk_button_new ();
385 gtk_widget_show (button_edit);
386 gtk_table_attach (GTK_TABLE (table), button_edit, 1, 2, 1, 2,
387 (GtkAttachOptions) (GTK_FILL),
388 (GtkAttachOptions) (0), 0, 0);
390 alignment = gtk_alignment_new (0.5, 0.5, 0, 0);
391 gtk_widget_show (alignment);
392 gtk_container_add (GTK_CONTAINER (button_edit), alignment);
394 hbox = gtk_hbox_new (FALSE, 2);
395 gtk_widget_show (hbox);
396 gtk_container_add (GTK_CONTAINER (alignment), hbox);
398 image = gtk_image_new_from_stock (GTK_STOCK_EDIT, GTK_ICON_SIZE_BUTTON);
399 gtk_widget_show (image);
400 gtk_box_pack_start (GTK_BOX (hbox), image, FALSE, FALSE, 0);
402 label = gtk_label_new_with_mnemonic (_("_Edit account"));
403 gtk_widget_show (label);
404 gtk_box_pack_start (GTK_BOX (hbox), label, FALSE, FALSE, 0);
406 fixed = gtk_fixed_new ();
407 gtk_widget_show (fixed);
408 gtk_table_attach (GTK_TABLE (table), fixed, 2, 3, 1, 2,
409 (GtkAttachOptions) (GTK_EXPAND | GTK_FILL),
410 (GtkAttachOptions) (GTK_FILL), 0, 0);
412 vbox = gtk_vbox_new (FALSE, 6);
413 gtk_widget_show (vbox);
414 gtk_table_attach (GTK_TABLE (table), vbox, 3, 4, 0, 2,
415 (GtkAttachOptions) (GTK_FILL),
416 (GtkAttachOptions) (GTK_FILL), 0, 0);
418 button_close = gtk_button_new ();
419 gtk_widget_show (button_close);
420 gtk_box_pack_start (GTK_BOX (vbox), button_close, FALSE, FALSE, 0);
421 gtk_button_set_relief (GTK_BUTTON (button_close), GTK_RELIEF_NONE);
424 image = gtk_image_new_from_stock ("gtk-close", GTK_ICON_SIZE_MENU);
425 gtk_widget_show (image);
426 gtk_container_add (GTK_CONTAINER (button_close), image);
428 label = gtk_label_new ("");
429 gtk_widget_show (label);
430 gtk_table_attach (GTK_TABLE (table), label, 1, 3, 0, 1,
431 (GtkAttachOptions) (GTK_EXPAND | GTK_SHRINK | GTK_FILL),
432 (GtkAttachOptions) (GTK_EXPAND | GTK_SHRINK | GTK_FILL), 0, 0);
433 gtk_widget_set_size_request (label, 175, -1);
434 gtk_label_set_use_markup (GTK_LABEL (label), TRUE);
435 gtk_label_set_line_wrap (GTK_LABEL (label), TRUE);
436 gtk_misc_set_alignment (GTK_MISC (label), 0, 0);
438 str = g_markup_printf_escaped ("<b>%s</b>\n%s",
439 empathy_account_get_display_name (account),
441 gtk_label_set_markup (GTK_LABEL (label), str);
444 g_object_set_data (G_OBJECT (child), "label", label);
445 g_object_set_data_full (G_OBJECT (button_edit),
446 "account", g_object_ref (account),
448 g_object_set_data_full (G_OBJECT (button_close),
449 "account", g_object_ref (account),
452 g_signal_connect (button_edit, "clicked",
453 G_CALLBACK (main_window_error_edit_clicked_cb),
456 g_signal_connect (button_close, "clicked",
457 G_CALLBACK (main_window_error_clear_clicked_cb),
460 gtk_widget_show (window->errors_vbox);
462 g_hash_table_insert (window->errors, g_object_ref (account), child);
466 main_window_update_status (EmpathyMainWindow *window, EmpathyAccountManager *manager)
472 /* Count number of connected/connecting/disconnected accounts */
473 connected = empathy_account_manager_get_connected_accounts (manager);
474 connecting = empathy_account_manager_get_connecting_accounts (manager);
476 /* Update the spinner state */
477 if (connecting > 0) {
478 ephy_spinner_start (EPHY_SPINNER (window->throbber));
480 ephy_spinner_stop (EPHY_SPINNER (window->throbber));
483 /* Update widgets sensibility */
484 for (l = window->actions_connected; l; l = l->next) {
485 gtk_action_set_sensitive (l->data, (connected > 0));
490 main_window_connection_changed_cb (EmpathyAccountManager *manager,
491 EmpathyAccount *account,
492 TpConnectionStatusReason reason,
493 TpConnectionStatus current,
494 TpConnectionStatus previous,
495 EmpathyMainWindow *window)
497 main_window_update_status (window, manager);
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)
578 EmpathyAccount *account;
579 gboolean should_play;
581 account = empathy_contact_get_account (contact);
582 should_play = !empathy_account_is_just_connected (account);
588 if (tp_connection_presence_type_cmp_availability (previous,
589 TP_CONNECTION_PRESENCE_TYPE_OFFLINE) > 0)
591 /* contact was online */
592 if (tp_connection_presence_type_cmp_availability (current,
593 TP_CONNECTION_PRESENCE_TYPE_OFFLINE) <= 0)
594 /* someone is logging off */
595 empathy_sound_play (GTK_WIDGET (window->window),
596 EMPATHY_SOUND_CONTACT_DISCONNECTED);
600 /* contact was offline */
601 if (tp_connection_presence_type_cmp_availability (current,
602 TP_CONNECTION_PRESENCE_TYPE_OFFLINE) > 0)
603 /* someone is logging in */
604 empathy_sound_play (GTK_WIDGET (window->window),
605 EMPATHY_SOUND_CONTACT_CONNECTED);
610 main_window_accels_load (void)
614 filename = g_build_filename (g_get_home_dir (), ".gnome2", PACKAGE_NAME, ACCELS_FILENAME, NULL);
615 if (g_file_test (filename, G_FILE_TEST_EXISTS)) {
616 DEBUG ("Loading from:'%s'", filename);
617 gtk_accel_map_load (filename);
624 main_window_accels_save (void)
627 gchar *file_with_path;
629 dir = g_build_filename (g_get_home_dir (), ".gnome2", PACKAGE_NAME, NULL);
630 g_mkdir_with_parents (dir, S_IRUSR | S_IWUSR | S_IXUSR);
631 file_with_path = g_build_filename (dir, ACCELS_FILENAME, NULL);
634 DEBUG ("Saving to:'%s'", file_with_path);
635 gtk_accel_map_save (file_with_path);
637 g_free (file_with_path);
641 main_window_destroy_cb (GtkWidget *widget,
642 EmpathyMainWindow *window)
644 /* Save user-defined accelerators. */
645 main_window_accels_save ();
647 g_signal_handlers_disconnect_by_func (window->account_manager,
648 main_window_connection_changed_cb,
651 if (window->size_timeout_id) {
652 g_source_remove (window->size_timeout_id);
655 g_list_free (window->actions_connected);
657 g_object_unref (window->mc);
658 g_object_unref (window->account_manager);
659 g_object_unref (window->list_store);
660 g_hash_table_destroy (window->errors);
662 g_signal_handlers_disconnect_by_func (window->event_manager,
663 main_window_event_added_cb,
665 g_signal_handlers_disconnect_by_func (window->event_manager,
666 main_window_event_removed_cb,
668 g_object_unref (window->event_manager);
669 g_object_unref (window->ui_manager);
675 main_window_chat_quit_cb (GtkAction *action,
676 EmpathyMainWindow *window)
682 main_window_view_history_cb (GtkAction *action,
683 EmpathyMainWindow *window)
685 empathy_log_window_show (NULL, NULL, FALSE, GTK_WINDOW (window->window));
689 main_window_chat_new_message_cb (GtkAction *action,
690 EmpathyMainWindow *window)
692 empathy_new_message_dialog_show (GTK_WINDOW (window->window));
696 main_window_chat_add_contact_cb (GtkAction *action,
697 EmpathyMainWindow *window)
699 empathy_new_contact_dialog_show (GTK_WINDOW (window->window));
703 main_window_view_show_ft_manager (GtkAction *action,
704 EmpathyMainWindow *window)
706 empathy_ft_manager_show ();
710 main_window_view_show_offline_cb (GtkToggleAction *action,
711 EmpathyMainWindow *window)
715 current = gtk_toggle_action_get_active (action);
716 empathy_conf_set_bool (empathy_conf_get (),
717 EMPATHY_PREFS_CONTACTS_SHOW_OFFLINE,
720 /* Turn off sound just while we alter the contact list. */
721 // FIXME: empathy_sound_set_enabled (FALSE);
722 empathy_contact_list_store_set_show_offline (window->list_store, current);
723 //empathy_sound_set_enabled (TRUE);
727 main_window_notify_sort_contact_cb (EmpathyConf *conf,
729 EmpathyMainWindow *window)
733 if (empathy_conf_get_string (conf, key, &str) && str) {
735 GEnumClass *enum_class;
736 GEnumValue *enum_value;
738 type = empathy_contact_list_store_sort_get_type ();
739 enum_class = G_ENUM_CLASS (g_type_class_peek (type));
740 enum_value = g_enum_get_value_by_nick (enum_class, str);
742 /* By changing the value of the GtkRadioAction,
743 it emits a signal that calls main_window_view_sort_contacts_cb
744 which updates the contacts list */
745 gtk_radio_action_set_current_value (window->sort_by_name,
748 g_warning ("Wrong value for sort_criterium configuration : %s", str);
755 main_window_view_sort_contacts_cb (GtkRadioAction *action,
756 GtkRadioAction *current,
757 EmpathyMainWindow *window)
759 EmpathyContactListStoreSort value;
762 GEnumClass *enum_class;
763 GEnumValue *enum_value;
765 value = gtk_radio_action_get_current_value (action);
766 group = gtk_radio_action_get_group (action);
768 /* Get string from index */
769 type = empathy_contact_list_store_sort_get_type ();
770 enum_class = G_ENUM_CLASS (g_type_class_peek (type));
771 enum_value = g_enum_get_value (enum_class, g_slist_index (group, current));
774 g_warning ("No GEnumValue for EmpathyContactListSort with GtkRadioAction index:%d",
775 g_slist_index (group, action));
777 const gchar *value_str;
778 value_str = enum_value->value_nick;
780 empathy_conf_set_string (empathy_conf_get (),
781 EMPATHY_PREFS_CONTACTS_SORT_CRITERIUM,
784 empathy_contact_list_store_set_sort_criterium (window->list_store, value);
787 /* Matches GtkRadioAction values set in empathy-main-window.ui */
788 #define CONTACT_LIST_NORMAL_SIZE_WITH_AVATARS 0
789 #define CONTACT_LIST_NORMAL_SIZE 1
790 #define CONTACT_LIST_COMPACT_SIZE 2
793 main_window_view_contacts_list_size_cb (GtkRadioAction *action,
794 GtkRadioAction *current,
795 EmpathyMainWindow *window)
799 value = gtk_radio_action_get_current_value (action);
801 empathy_conf_set_bool (empathy_conf_get (),
802 EMPATHY_PREFS_UI_SHOW_AVATARS,
803 value == CONTACT_LIST_NORMAL_SIZE_WITH_AVATARS);
804 empathy_conf_set_bool (empathy_conf_get (),
805 EMPATHY_PREFS_UI_COMPACT_CONTACT_LIST,
806 value == CONTACT_LIST_COMPACT_SIZE);
808 empathy_contact_list_store_set_show_avatars (window->list_store,
809 value == CONTACT_LIST_NORMAL_SIZE_WITH_AVATARS);
810 empathy_contact_list_store_set_is_compact (window->list_store,
811 value == CONTACT_LIST_COMPACT_SIZE);
815 main_window_notify_contact_list_size_cb (EmpathyConf *conf,
817 EmpathyMainWindow *window)
819 gboolean show_avatars;
820 gboolean compact_contact_list;
821 gint value = CONTACT_LIST_NORMAL_SIZE_WITH_AVATARS;
823 if (empathy_conf_get_bool (conf,
824 EMPATHY_PREFS_UI_SHOW_AVATARS,
826 && empathy_conf_get_bool (conf,
827 EMPATHY_PREFS_UI_COMPACT_CONTACT_LIST,
828 &compact_contact_list)) {
829 if (compact_contact_list) {
830 value = CONTACT_LIST_COMPACT_SIZE;
831 } else if (show_avatars) {
832 value = CONTACT_LIST_NORMAL_SIZE_WITH_AVATARS;
834 value = CONTACT_LIST_NORMAL_SIZE;
837 gtk_radio_action_set_current_value (window->normal_with_avatars, value);
841 main_window_view_show_map_cb (GtkCheckMenuItem *item,
842 EmpathyMainWindow *window)
844 #if HAVE_LIBCHAMPLAIN
845 empathy_map_view_show ();
850 main_window_favorite_chatroom_join (EmpathyChatroom *chatroom)
852 EmpathyAccount *account;
853 TpConnection *connection;
856 account = empathy_chatroom_get_account (chatroom);
857 connection = empathy_account_get_connection (account);
858 room = empathy_chatroom_get_room (chatroom);
860 if (connection != NULL) {
861 DEBUG ("Requesting channel for '%s'", room);
862 empathy_dispatcher_join_muc (connection, room, NULL, NULL);
867 main_window_favorite_chatroom_menu_activate_cb (GtkMenuItem *menu_item,
868 EmpathyChatroom *chatroom)
870 main_window_favorite_chatroom_join (chatroom);
874 main_window_favorite_chatroom_menu_add (EmpathyMainWindow *window,
875 EmpathyChatroom *chatroom)
877 GtkWidget *menu_item;
880 if (g_object_get_data (G_OBJECT (chatroom), "menu_item")) {
884 name = empathy_chatroom_get_name (chatroom);
885 menu_item = gtk_menu_item_new_with_label (name);
887 g_object_set_data (G_OBJECT (chatroom), "menu_item", menu_item);
888 g_signal_connect (menu_item, "activate",
889 G_CALLBACK (main_window_favorite_chatroom_menu_activate_cb),
892 gtk_menu_shell_insert (GTK_MENU_SHELL (window->room_menu),
895 gtk_widget_show (menu_item);
899 main_window_favorite_chatroom_menu_added_cb (EmpathyChatroomManager *manager,
900 EmpathyChatroom *chatroom,
901 EmpathyMainWindow *window)
903 main_window_favorite_chatroom_menu_add (window, chatroom);
904 gtk_widget_show (window->room_separator);
905 gtk_action_set_sensitive (window->room_join_favorites, TRUE);
909 main_window_favorite_chatroom_menu_removed_cb (EmpathyChatroomManager *manager,
910 EmpathyChatroom *chatroom,
911 EmpathyMainWindow *window)
913 GtkWidget *menu_item;
916 menu_item = g_object_get_data (G_OBJECT (chatroom), "menu_item");
917 g_object_set_data (G_OBJECT (chatroom), "menu_item", NULL);
918 gtk_widget_destroy (menu_item);
920 chatrooms = empathy_chatroom_manager_get_chatrooms (window->chatroom_manager, NULL);
922 gtk_widget_show (window->room_separator);
924 gtk_widget_hide (window->room_separator);
927 gtk_action_set_sensitive (window->room_join_favorites, chatrooms != NULL);
928 g_list_free (chatrooms);
932 main_window_favorite_chatroom_menu_setup (EmpathyMainWindow *window)
934 GList *chatrooms, *l;
937 window->chatroom_manager = empathy_chatroom_manager_dup_singleton (NULL);
938 chatrooms = empathy_chatroom_manager_get_chatrooms (window->chatroom_manager, NULL);
939 room = gtk_ui_manager_get_widget (window->ui_manager,
941 window->room_menu = gtk_menu_item_get_submenu (GTK_MENU_ITEM (room));
942 window->room_separator = gtk_ui_manager_get_widget (window->ui_manager,
943 "/menubar/room/room_separator");
945 for (l = chatrooms; l; l = l->next) {
946 main_window_favorite_chatroom_menu_add (window, l->data);
950 gtk_widget_hide (window->room_separator);
953 gtk_action_set_sensitive (window->room_join_favorites, chatrooms != NULL);
955 g_signal_connect (window->chatroom_manager, "chatroom-added",
956 G_CALLBACK (main_window_favorite_chatroom_menu_added_cb),
958 g_signal_connect (window->chatroom_manager, "chatroom-removed",
959 G_CALLBACK (main_window_favorite_chatroom_menu_removed_cb),
962 g_list_free (chatrooms);
966 main_window_room_join_new_cb (GtkAction *action,
967 EmpathyMainWindow *window)
969 empathy_new_chatroom_dialog_show (GTK_WINDOW (window->window));
973 main_window_room_join_favorites_cb (GtkAction *action,
974 EmpathyMainWindow *window)
976 GList *chatrooms, *l;
978 chatrooms = empathy_chatroom_manager_get_chatrooms (window->chatroom_manager, NULL);
979 for (l = chatrooms; l; l = l->next) {
980 main_window_favorite_chatroom_join (l->data);
982 g_list_free (chatrooms);
986 main_window_room_manage_favorites_cb (GtkAction *action,
987 EmpathyMainWindow *window)
989 empathy_chatrooms_window_show (GTK_WINDOW (window->window));
993 main_window_edit_cb (GtkAction *action,
994 EmpathyMainWindow *window)
998 /* FIXME: It should use the UIManager to merge the contact/group submenu */
999 submenu = empathy_contact_list_view_get_contact_menu (window->list_view);
1004 item = GTK_MENU_ITEM (window->edit_context);
1005 label = gtk_bin_get_child (GTK_BIN (item));
1006 gtk_label_set_text (GTK_LABEL (label), _("Contact"));
1008 gtk_widget_show (window->edit_context);
1009 gtk_widget_show (window->edit_context_separator);
1011 gtk_menu_item_set_submenu (item, submenu);
1016 submenu = empathy_contact_list_view_get_group_menu (window->list_view);
1021 item = GTK_MENU_ITEM (window->edit_context);
1022 label = gtk_bin_get_child (GTK_BIN (item));
1023 gtk_label_set_text (GTK_LABEL (label), _("Group"));
1025 gtk_widget_show (window->edit_context);
1026 gtk_widget_show (window->edit_context_separator);
1028 gtk_menu_item_set_submenu (item, submenu);
1033 gtk_widget_hide (window->edit_context);
1034 gtk_widget_hide (window->edit_context_separator);
1040 main_window_edit_accounts_cb (GtkAction *action,
1041 EmpathyMainWindow *window)
1043 empathy_accounts_dialog_show (GTK_WINDOW (window->window), NULL);
1047 main_window_edit_personal_information_cb (GtkAction *action,
1048 EmpathyMainWindow *window)
1050 empathy_contact_personal_dialog_show (GTK_WINDOW (window->window));
1054 main_window_edit_preferences_cb (GtkAction *action,
1055 EmpathyMainWindow *window)
1057 empathy_preferences_show (GTK_WINDOW (window->window));
1061 main_window_help_about_cb (GtkAction *action,
1062 EmpathyMainWindow *window)
1064 empathy_about_dialog_new (GTK_WINDOW (window->window));
1068 main_window_help_debug_cb (GtkAction *action,
1069 EmpathyMainWindow *window)
1071 empathy_debug_dialog_new (GTK_WINDOW (window->window));
1075 main_window_help_contents_cb (GtkAction *action,
1076 EmpathyMainWindow *window)
1078 empathy_url_show (window->window, "ghelp:empathy");
1082 main_window_throbber_button_press_event_cb (GtkWidget *throbber_ebox,
1083 GdkEventButton *event,
1084 EmpathyMainWindow *window)
1086 if (event->type != GDK_BUTTON_PRESS ||
1087 event->button != 1) {
1091 empathy_accounts_dialog_show (GTK_WINDOW (window->window), NULL);
1097 main_window_configure_event_timeout_cb (EmpathyMainWindow *window)
1101 gtk_window_get_size (GTK_WINDOW (window->window), &w, &h);
1102 gtk_window_get_position (GTK_WINDOW (window->window), &x, &y);
1104 empathy_geometry_save (GEOMETRY_NAME, x, y, w, h);
1106 window->size_timeout_id = 0;
1112 main_window_configure_event_cb (GtkWidget *widget,
1113 GdkEventConfigure *event,
1114 EmpathyMainWindow *window)
1116 if (window->size_timeout_id) {
1117 g_source_remove (window->size_timeout_id);
1120 window->size_timeout_id = g_timeout_add_seconds (1,
1121 (GSourceFunc) main_window_configure_event_timeout_cb,
1128 main_window_account_created_or_deleted_cb (EmpathyAccountManager *manager,
1129 EmpathyAccount *account,
1130 EmpathyMainWindow *window)
1132 gtk_action_set_sensitive (window->view_history,
1133 empathy_account_manager_get_count (manager) > 0);
1137 main_window_notify_show_offline_cb (EmpathyConf *conf,
1139 gpointer toggle_action)
1141 gboolean show_offline;
1143 if (empathy_conf_get_bool (conf, key, &show_offline)) {
1144 gtk_toggle_action_set_active (toggle_action, show_offline);
1149 main_window_connection_items_setup (EmpathyMainWindow *window,
1155 const gchar *actions_connected[] = {
1159 "edit_personal_information"
1162 for (i = 0, list = NULL; i < G_N_ELEMENTS (actions_connected); i++) {
1163 action = gtk_builder_get_object (gui, actions_connected[i]);
1164 list = g_list_prepend (list, action);
1167 window->actions_connected = list;
1171 empathy_main_window_get (void)
1173 return window != NULL ? window->window : NULL;
1177 empathy_main_window_show (void)
1179 EmpathyContactList *list_iface;
1180 EmpathyContactMonitor *monitor;
1184 GtkToggleAction *show_offline_widget;
1186 GtkAction *show_map_widget;
1188 gboolean show_offline;
1194 empathy_window_present (GTK_WINDOW (window->window), TRUE);
1195 return window->window;
1198 window = g_new0 (EmpathyMainWindow, 1);
1200 /* Set up interface */
1201 filename = empathy_file_lookup ("empathy-main-window.ui", "src");
1202 gui = empathy_builder_get_file (filename,
1203 "main_window", &window->window,
1204 "main_vbox", &window->main_vbox,
1205 "errors_vbox", &window->errors_vbox,
1206 "ui_manager", &window->ui_manager,
1207 "view_show_offline", &show_offline_widget,
1208 "view_sort_by_name", &window->sort_by_name,
1209 "view_sort_by_status", &window->sort_by_status,
1210 "view_normal_size_with_avatars", &window->normal_with_avatars,
1211 "view_normal_size", &window->normal_size,
1212 "view_compact_size", &window->compact_size,
1213 "view_history", &window->view_history,
1214 "view_show_map", &show_map_widget,
1215 "room_join_favorites", &window->room_join_favorites,
1216 "presence_toolbar", &window->presence_toolbar,
1217 "roster_scrolledwindow", &sw,
1221 empathy_builder_connect (gui, window,
1222 "main_window", "destroy", main_window_destroy_cb,
1223 "main_window", "configure_event", main_window_configure_event_cb,
1224 "chat_quit", "activate", main_window_chat_quit_cb,
1225 "chat_new_message", "activate", main_window_chat_new_message_cb,
1226 "view_history", "activate", main_window_view_history_cb,
1227 "room_join_new", "activate", main_window_room_join_new_cb,
1228 "room_join_favorites", "activate", main_window_room_join_favorites_cb,
1229 "room_manage_favorites", "activate", main_window_room_manage_favorites_cb,
1230 "chat_add_contact", "activate", main_window_chat_add_contact_cb,
1231 "view_show_ft_manager", "activate", main_window_view_show_ft_manager,
1232 "view_show_offline", "toggled", main_window_view_show_offline_cb,
1233 "view_sort_by_name", "changed", main_window_view_sort_contacts_cb,
1234 "view_normal_size_with_avatars", "changed", main_window_view_contacts_list_size_cb,
1235 "view_show_map", "activate", main_window_view_show_map_cb,
1236 "edit", "activate", main_window_edit_cb,
1237 "edit_accounts", "activate", main_window_edit_accounts_cb,
1238 "edit_personal_information", "activate", main_window_edit_personal_information_cb,
1239 "edit_preferences", "activate", main_window_edit_preferences_cb,
1240 "help_about", "activate", main_window_help_about_cb,
1241 "help_debug", "activate", main_window_help_debug_cb,
1242 "help_contents", "activate", main_window_help_contents_cb,
1245 /* Set up connection related widgets. */
1246 main_window_connection_items_setup (window, gui);
1248 g_object_ref (window->ui_manager);
1249 g_object_unref (gui);
1251 #if !HAVE_LIBCHAMPLAIN
1252 gtk_action_set_visible (show_map_widget, FALSE);
1255 window->mc = empathy_mission_control_dup_singleton ();
1256 window->account_manager = empathy_account_manager_dup_singleton ();
1258 g_signal_connect (window->account_manager,
1259 "account-connection-changed",
1260 G_CALLBACK (main_window_connection_changed_cb), window);
1262 window->errors = g_hash_table_new_full (empathy_account_hash,
1263 empathy_account_equal,
1268 main_window_favorite_chatroom_menu_setup (window);
1270 window->edit_context = gtk_ui_manager_get_widget (window->ui_manager,
1271 "/menubar/edit/edit_context");
1272 window->edit_context_separator = gtk_ui_manager_get_widget (window->ui_manager,
1273 "/menubar/edit/edit_context_separator");
1274 gtk_widget_hide (window->edit_context);
1275 gtk_widget_hide (window->edit_context_separator);
1277 /* Set up contact list. */
1278 empathy_status_presets_get_all ();
1280 /* Set up presence chooser */
1281 window->presence_chooser = empathy_presence_chooser_new ();
1282 gtk_widget_show (window->presence_chooser);
1283 item = gtk_tool_item_new ();
1284 gtk_widget_show (GTK_WIDGET (item));
1285 gtk_container_add (GTK_CONTAINER (item), window->presence_chooser);
1286 gtk_tool_item_set_is_important (item, TRUE);
1287 gtk_tool_item_set_expand (item, TRUE);
1288 gtk_toolbar_insert (GTK_TOOLBAR (window->presence_toolbar), item, -1);
1290 /* Set up the throbber */
1291 ebox = gtk_event_box_new ();
1292 gtk_event_box_set_visible_window (GTK_EVENT_BOX (ebox), FALSE);
1293 gtk_widget_set_tooltip_text (ebox, _("Show and edit accounts"));
1294 g_signal_connect (ebox,
1295 "button-press-event",
1296 G_CALLBACK (main_window_throbber_button_press_event_cb),
1298 gtk_widget_show (ebox);
1300 window->throbber = ephy_spinner_new ();
1301 ephy_spinner_set_size (EPHY_SPINNER (window->throbber), GTK_ICON_SIZE_LARGE_TOOLBAR);
1302 gtk_container_add (GTK_CONTAINER (ebox), window->throbber);
1303 gtk_widget_show (window->throbber);
1305 item = gtk_tool_item_new ();
1306 gtk_container_add (GTK_CONTAINER (item), ebox);
1307 gtk_toolbar_insert (GTK_TOOLBAR (window->presence_toolbar), item, -1);
1308 gtk_widget_show (GTK_WIDGET (item));
1310 list_iface = EMPATHY_CONTACT_LIST (empathy_contact_manager_dup_singleton ());
1311 monitor = empathy_contact_list_get_monitor (list_iface);
1312 window->list_store = empathy_contact_list_store_new (list_iface);
1313 window->list_view = empathy_contact_list_view_new (window->list_store,
1314 EMPATHY_CONTACT_LIST_FEATURE_ALL,
1315 EMPATHY_CONTACT_FEATURE_ALL);
1316 g_signal_connect (monitor, "contact-presence-changed",
1317 G_CALLBACK (main_window_contact_presence_changed_cb), window);
1318 g_object_unref (list_iface);
1320 gtk_widget_show (GTK_WIDGET (window->list_view));
1321 gtk_container_add (GTK_CONTAINER (sw),
1322 GTK_WIDGET (window->list_view));
1323 g_signal_connect (window->list_view, "row-activated",
1324 G_CALLBACK (main_window_row_activated_cb),
1327 /* Load user-defined accelerators. */
1328 main_window_accels_load ();
1330 /* Set window size. */
1331 empathy_geometry_load (GEOMETRY_NAME, &x, &y, &w, &h);
1333 if (w >= 1 && h >= 1) {
1334 /* Use the defaults from the ui file if we
1335 * don't have good w, h geometry.
1337 DEBUG ("Configuring window default size w:%d, h:%d", w, h);
1338 gtk_window_set_default_size (GTK_WINDOW (window->window), w, h);
1341 if (x >= 0 && y >= 0) {
1342 /* Let the window manager position it if we
1343 * don't have good x, y coordinates.
1345 DEBUG ("Configuring window default position x:%d, y:%d", x, y);
1346 gtk_window_move (GTK_WINDOW (window->window), x, y);
1349 /* Enable event handling */
1350 window->event_manager = empathy_event_manager_dup_singleton ();
1351 g_signal_connect (window->event_manager, "event-added",
1352 G_CALLBACK (main_window_event_added_cb),
1354 g_signal_connect (window->event_manager, "event-removed",
1355 G_CALLBACK (main_window_event_removed_cb),
1358 g_signal_connect (window->account_manager, "account-created",
1359 G_CALLBACK (main_window_account_created_or_deleted_cb),
1361 g_signal_connect (window->account_manager, "account-deleted",
1362 G_CALLBACK (main_window_account_created_or_deleted_cb),
1364 main_window_account_created_or_deleted_cb (window->account_manager, NULL, window);
1366 l = empathy_event_manager_get_events (window->event_manager);
1368 main_window_event_added_cb (window->event_manager,
1373 conf = empathy_conf_get ();
1375 /* Show offline ? */
1376 empathy_conf_get_bool (conf,
1377 EMPATHY_PREFS_CONTACTS_SHOW_OFFLINE,
1379 empathy_conf_notify_add (conf,
1380 EMPATHY_PREFS_CONTACTS_SHOW_OFFLINE,
1381 main_window_notify_show_offline_cb,
1382 show_offline_widget);
1384 gtk_toggle_action_set_active (show_offline_widget, show_offline);
1386 /* Sort by name / by status ? */
1387 empathy_conf_notify_add (conf,
1388 EMPATHY_PREFS_CONTACTS_SORT_CRITERIUM,
1389 (EmpathyConfNotifyFunc) main_window_notify_sort_contact_cb,
1392 main_window_notify_sort_contact_cb (conf,
1393 EMPATHY_PREFS_CONTACTS_SORT_CRITERIUM,
1396 /* Contacts list size */
1397 empathy_conf_notify_add (conf,
1398 EMPATHY_PREFS_UI_COMPACT_CONTACT_LIST,
1399 (EmpathyConfNotifyFunc) main_window_notify_contact_list_size_cb,
1401 empathy_conf_notify_add (conf,
1402 EMPATHY_PREFS_UI_SHOW_AVATARS,
1403 (EmpathyConfNotifyFunc) main_window_notify_contact_list_size_cb,
1406 main_window_notify_contact_list_size_cb (conf,
1407 EMPATHY_PREFS_UI_SHOW_AVATARS,
1410 main_window_update_status (window, window->account_manager);
1412 return window->window;