1 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
3 * Copyright (C) 2007 Collabora Ltd.
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License as
7 * published by the Free Software Foundation; either version 2 of the
8 * License, or (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * General Public License for more details.
15 * You should have received a copy of the GNU General Public
16 * License along with this program; if not, write to the
17 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 * Boston, MA 02111-1307, USA.
20 * Authors: Xavier Claessens <xclaesse@gmail.com>
27 #include <glade/glade.h>
28 #include <glib/gi18n.h>
30 #include <libtelepathy/tp-helpers.h>
31 #include <libmissioncontrol/mission-control.h>
33 #include <libempathy/gossip-conf.h>
34 #include <libempathy/gossip-contact.h>
35 #include <libempathy/gossip-debug.h>
37 #include "empathy-main-window.h"
38 #include "ephy-spinner.h"
39 #include "gossip-contact-list.h"
40 #include "gossip-presence-chooser.h"
41 #include "gossip-ui-utils.h"
42 #include "gossip-status-presets.h"
43 #include "gossip-geometry.h"
44 #include "gossip-preferences.h"
45 #include "gossip-accounts-dialog.h"
47 #define DEBUG_DOMAIN "MainWindow"
49 /* Minimum width of roster window if something goes wrong. */
52 /* Accels (menu shortcuts) can be configured and saved */
53 #define ACCELS_FILENAME "accels.txt"
55 /* Flashing delay for icons (milliseconds). */
56 #define FLASH_TIMEOUT 500
58 /* Name in the geometry file */
59 #define GEOMETRY_NAME "main-window"
62 GossipContactList *contact_list;
69 /* Tooltips for all widgets */
70 GtkTooltips *tooltips;
76 GtkWidget *room_join_favorites;
77 GtkWidget *edit_context;
78 GtkWidget *edit_context_separator;
83 /* Widgets that are enabled when we're connected/disconnected */
84 GList *widgets_connected;
85 GList *widgets_disconnected;
88 GtkWidget *presence_toolbar;
89 GtkWidget *presence_chooser;
92 guint size_timeout_id;
95 static void main_window_destroy_cb (GtkWidget *widget,
96 EmpathyMainWindow *window);
97 static void main_window_favorite_chatroom_menu_setup (void);
98 static void main_window_chat_quit_cb (GtkWidget *widget,
99 EmpathyMainWindow *window);
100 static void main_window_chat_new_message_cb (GtkWidget *widget,
101 EmpathyMainWindow *window);
102 static void main_window_chat_history_cb (GtkWidget *widget,
103 EmpathyMainWindow *window);
104 static void main_window_room_join_new_cb (GtkWidget *widget,
105 EmpathyMainWindow *window);
106 static void main_window_room_join_favorites_cb (GtkWidget *widget,
107 EmpathyMainWindow *window);
108 static void main_window_room_manage_favorites_cb (GtkWidget *widget,
109 EmpathyMainWindow *window);
110 static void main_window_chat_add_contact_cb (GtkWidget *widget,
111 EmpathyMainWindow *window);
112 static void main_window_chat_show_offline_cb (GtkCheckMenuItem *item,
113 EmpathyMainWindow *window);
114 static gboolean main_window_edit_button_press_event_cb (GtkWidget *widget,
115 GdkEventButton *event,
116 EmpathyMainWindow *window);
117 static void main_window_edit_accounts_cb (GtkWidget *widget,
118 EmpathyMainWindow *window);
119 static void main_window_edit_personal_information_cb (GtkWidget *widget,
120 EmpathyMainWindow *window);
121 static void main_window_edit_preferences_cb (GtkWidget *widget,
122 EmpathyMainWindow *window);
123 static void main_window_help_about_cb (GtkWidget *widget,
124 EmpathyMainWindow *window);
125 static void main_window_help_contents_cb (GtkWidget *widget,
126 EmpathyMainWindow *window);
127 static gboolean main_window_throbber_button_press_event_cb (GtkWidget *throbber_ebox,
128 GdkEventButton *event,
130 static void main_window_accels_load (void);
131 static void main_window_accels_save (void);
132 static void main_window_connection_items_setup (EmpathyMainWindow *window,
134 //static void main_window_connection_items_update (void);
135 static void main_window_presence_changed_cb (DBusGProxy *proxy,
137 EmpathyMainWindow *window);
138 static void main_window_presence_chooser_changed_cb (GtkWidget *chooser,
141 EmpathyMainWindow *window);
142 static gboolean main_window_configure_event_timeout_cb (EmpathyMainWindow *window);
143 static gboolean main_window_configure_event_cb (GtkWidget *widget,
144 GdkEventConfigure *event,
145 EmpathyMainWindow *window);
146 static void main_window_notify_show_offline_cb (GossipConf *conf,
148 gpointer check_menu_item);
149 static void main_window_notify_show_avatars_cb (GossipConf *conf,
151 EmpathyMainWindow *window);
152 static void main_window_notify_compact_contact_list_cb (GossipConf *conf,
154 EmpathyMainWindow *window);
155 static void main_window_notify_sort_criterium_cb (GossipConf *conf,
157 EmpathyMainWindow *window);
160 empathy_main_window_show (void)
162 static EmpathyMainWindow *window = NULL;
166 GtkWidget *show_offline_widget;
171 gboolean show_offline;
172 gboolean show_avatars;
173 gboolean compact_contact_list;
177 gtk_window_present (GTK_WINDOW (window->window));
178 return window->window;
181 window = g_new0 (EmpathyMainWindow, 1);
183 /* Set up interface */
184 glade = gossip_glade_get_file ("empathy-main-window.glade",
187 "main_window", &window->window,
188 "main_vbox", &window->main_vbox,
189 "chat_show_offline", &show_offline_widget,
190 "room", &window->room,
191 "room_sep", &window->room_sep,
192 "room_join_favorites", &window->room_join_favorites,
193 "edit_context", &window->edit_context,
194 "edit_context_separator", &window->edit_context_separator,
195 "presence_toolbar", &window->presence_toolbar,
196 "roster_scrolledwindow", &sw,
199 gossip_glade_connect (glade,
201 "main_window", "destroy", main_window_destroy_cb,
202 "main_window", "configure_event", main_window_configure_event_cb,
203 "chat_quit", "activate", main_window_chat_quit_cb,
204 "chat_new_message", "activate", main_window_chat_new_message_cb,
205 "chat_history", "activate", main_window_chat_history_cb,
206 "room_join_new", "activate", main_window_room_join_new_cb,
207 "room_join_favorites", "activate", main_window_room_join_favorites_cb,
208 "room_manage_favorites", "activate", main_window_room_manage_favorites_cb,
209 "chat_add_contact", "activate", main_window_chat_add_contact_cb,
210 "chat_show_offline", "toggled", main_window_chat_show_offline_cb,
211 "edit", "button-press-event", main_window_edit_button_press_event_cb,
212 "edit_accounts", "activate", main_window_edit_accounts_cb,
213 "edit_personal_information", "activate", main_window_edit_personal_information_cb,
214 "edit_preferences", "activate", main_window_edit_preferences_cb,
215 "help_about", "activate", main_window_help_about_cb,
216 "help_contents", "activate", main_window_help_contents_cb,
219 /* Set up connection related widgets. */
220 main_window_connection_items_setup (window, glade);
221 g_object_unref (glade);
223 window->tooltips = g_object_ref_sink (gtk_tooltips_new ());
226 main_window_favorite_chatroom_menu_setup ();
228 gtk_widget_hide (window->edit_context);
229 gtk_widget_hide (window->edit_context_separator);
231 /* Set up presence chooser
232 * FIXME: Update status message not yet supported by MC
234 window->mc = mission_control_new (tp_get_bus ());
235 window->presence_chooser = gossip_presence_chooser_new ();
236 gtk_widget_show (window->presence_chooser);
237 gossip_presence_chooser_set_flash_interval (GOSSIP_PRESENCE_CHOOSER (window->presence_chooser),
239 state = mission_control_get_presence (window->mc, NULL);
240 gossip_presence_chooser_set_state (GOSSIP_PRESENCE_CHOOSER (window->presence_chooser),
242 dbus_g_proxy_connect_signal (DBUS_G_PROXY (window->mc),
243 "PresenceStatusRequested",
244 G_CALLBACK (main_window_presence_changed_cb),
246 g_signal_connect (window->presence_chooser,
248 G_CALLBACK (main_window_presence_chooser_changed_cb),
252 item = gtk_tool_item_new ();
253 gtk_widget_show (GTK_WIDGET (item));
254 gtk_container_add (GTK_CONTAINER (item), window->presence_chooser);
255 gtk_tool_item_set_is_important (item, TRUE);
256 gtk_tool_item_set_expand (item, TRUE);
257 gtk_toolbar_insert (GTK_TOOLBAR (window->presence_toolbar), item, -1);
259 window->widgets_connected = g_list_prepend (window->widgets_connected,
260 window->presence_chooser);
262 /* Set up the throbber */
263 ebox = gtk_event_box_new ();
264 gtk_event_box_set_visible_window (GTK_EVENT_BOX (ebox), FALSE);
266 window->throbber = ephy_spinner_new ();
267 ephy_spinner_set_size (EPHY_SPINNER (window->throbber), GTK_ICON_SIZE_LARGE_TOOLBAR);
268 gtk_container_add (GTK_CONTAINER (ebox), window->throbber);
270 item = gtk_tool_item_new ();
271 gtk_container_add (GTK_CONTAINER (item), ebox);
272 gtk_widget_show_all (GTK_WIDGET (item));
274 gtk_toolbar_insert (GTK_TOOLBAR (window->presence_toolbar), item, -1);
276 str = _("Show and edit accounts");
277 gtk_tooltips_set_tip (GTK_TOOLTIPS (window->tooltips),
280 g_signal_connect (ebox,
281 "button-press-event",
282 G_CALLBACK (main_window_throbber_button_press_event_cb),
285 /* Set up contact list. */
286 gossip_status_presets_get_all ();
287 window->contact_list = gossip_contact_list_new ();
288 gtk_widget_show (GTK_WIDGET (window->contact_list));
289 gtk_container_add (GTK_CONTAINER (sw),
290 GTK_WIDGET (window->contact_list));
292 /* Load user-defined accelerators. */
293 main_window_accels_load ();
295 /* Set window size. */
296 gossip_geometry_load (GEOMETRY_NAME, &x, &y, &w, &h);
298 if (w >= 1 && h >= 1) {
299 /* Use the defaults from the glade file if we
300 * don't have good w, h geometry.
302 gossip_debug (DEBUG_DOMAIN, "Configuring window default size w:%d, h:%d", w, h);
303 gtk_window_set_default_size (GTK_WINDOW (window->window), w, h);
306 if (x >= 0 && y >= 0) {
307 /* Let the window manager position it if we
308 * don't have good x, y coordinates.
310 gossip_debug (DEBUG_DOMAIN, "Configuring window default position x:%d, y:%d", x, y);
311 gtk_window_move (GTK_WINDOW (window->window), x, y);
314 conf = gossip_conf_get ();
317 gossip_conf_get_bool (conf,
318 GOSSIP_PREFS_CONTACTS_SHOW_OFFLINE,
320 gossip_conf_notify_add (conf,
321 GOSSIP_PREFS_CONTACTS_SHOW_OFFLINE,
322 main_window_notify_show_offline_cb,
323 show_offline_widget);
325 gtk_check_menu_item_set_active (GTK_CHECK_MENU_ITEM (show_offline_widget),
329 gossip_conf_get_bool (conf,
330 GOSSIP_PREFS_UI_SHOW_AVATARS,
332 gossip_conf_notify_add (conf,
333 GOSSIP_PREFS_UI_SHOW_AVATARS,
334 (GossipConfNotifyFunc) main_window_notify_show_avatars_cb,
336 gossip_contact_list_set_show_avatars (window->contact_list, show_avatars);
339 gossip_conf_get_bool (conf,
340 GOSSIP_PREFS_UI_COMPACT_CONTACT_LIST,
341 &compact_contact_list);
342 gossip_conf_notify_add (conf,
343 GOSSIP_PREFS_UI_COMPACT_CONTACT_LIST,
344 (GossipConfNotifyFunc) main_window_notify_compact_contact_list_cb,
346 gossip_contact_list_set_is_compact (window->contact_list, compact_contact_list);
349 gossip_conf_notify_add (conf,
350 GOSSIP_PREFS_CONTACTS_SORT_CRITERIUM,
351 (GossipConfNotifyFunc) main_window_notify_sort_criterium_cb,
353 main_window_notify_sort_criterium_cb (conf,
354 GOSSIP_PREFS_CONTACTS_SORT_CRITERIUM,
357 gtk_widget_show (window->window);
359 return window->window;
363 main_window_destroy_cb (GtkWidget *widget,
364 EmpathyMainWindow *window)
366 /* Save user-defined accelerators. */
367 main_window_accels_save ();
369 if (window->size_timeout_id) {
370 g_source_remove (window->size_timeout_id);
373 g_list_free (window->widgets_connected);
374 g_list_free (window->widgets_disconnected);
376 g_object_unref (window->tooltips);
377 g_object_unref (window->mc);
383 main_window_favorite_chatroom_menu_setup (void)
388 main_window_chat_quit_cb (GtkWidget *widget,
389 EmpathyMainWindow *window)
391 gtk_widget_destroy (window->window);
395 main_window_chat_new_message_cb (GtkWidget *widget,
396 EmpathyMainWindow *window)
398 //gossip_new_message_dialog_show (GTK_WINDOW (window->window));
402 main_window_chat_history_cb (GtkWidget *widget,
403 EmpathyMainWindow *window)
405 //gossip_log_window_show (NULL, NULL);
409 main_window_room_join_new_cb (GtkWidget *widget,
410 EmpathyMainWindow *window)
412 //gossip_new_chatroom_dialog_show (GTK_WINDOW (window->window));
416 main_window_room_join_favorites_cb (GtkWidget *widget,
417 EmpathyMainWindow *window)
419 //gossip_session_chatroom_join_favorites (window->session);
423 main_window_room_manage_favorites_cb (GtkWidget *widget,
424 EmpathyMainWindow *window)
426 //gossip_chatrooms_window_show (NULL, FALSE);
430 main_window_chat_add_contact_cb (GtkWidget *widget,
431 EmpathyMainWindow *window)
433 //gossip_add_contact_dialog_show (GTK_WINDOW (window->window), NULL);
437 main_window_chat_show_offline_cb (GtkCheckMenuItem *item,
438 EmpathyMainWindow *window)
442 current = gtk_check_menu_item_get_active (item);
444 gossip_conf_set_bool (gossip_conf_get (),
445 GOSSIP_PREFS_CONTACTS_SHOW_OFFLINE,
448 /* Turn off sound just while we alter the contact list. */
449 // FIXME: gossip_sound_set_enabled (FALSE);
450 g_object_set (window->contact_list, "show_offline", current, NULL);
451 //gossip_sound_set_enabled (TRUE);
455 main_window_edit_button_press_event_cb (GtkWidget *widget,
456 GdkEventButton *event,
457 EmpathyMainWindow *window)
459 GossipContact *contact;
462 if (!event->button == 1) {
466 group = gossip_contact_list_get_selected_group (window->contact_list);
472 item = GTK_MENU_ITEM (window->edit_context);
473 label = gtk_bin_get_child (GTK_BIN (item));
474 gtk_label_set_text (GTK_LABEL (label), _("Group"));
476 gtk_widget_show (window->edit_context);
477 gtk_widget_show (window->edit_context_separator);
479 submenu = gossip_contact_list_get_group_menu (window->contact_list);
480 gtk_menu_item_set_submenu (item, submenu);
487 contact = gossip_contact_list_get_selected (window->contact_list);
493 item = GTK_MENU_ITEM (window->edit_context);
494 label = gtk_bin_get_child (GTK_BIN (item));
495 gtk_label_set_text (GTK_LABEL (label), _("Contact"));
497 gtk_widget_show (window->edit_context);
498 gtk_widget_show (window->edit_context_separator);
500 submenu = gossip_contact_list_get_contact_menu (window->contact_list,
502 gtk_menu_item_set_submenu (item, submenu);
504 g_object_unref (contact);
509 gtk_widget_hide (window->edit_context);
510 gtk_widget_hide (window->edit_context_separator);
516 main_window_edit_accounts_cb (GtkWidget *widget,
517 EmpathyMainWindow *window)
519 gossip_accounts_dialog_show ();
523 main_window_edit_personal_information_cb (GtkWidget *widget,
524 EmpathyMainWindow *window)
526 //gossip_vcard_dialog_show (GTK_WINDOW (window->window));
530 main_window_edit_preferences_cb (GtkWidget *widget,
531 EmpathyMainWindow *window)
533 gossip_preferences_show ();
537 main_window_help_about_cb (GtkWidget *widget,
538 EmpathyMainWindow *window)
540 //gossip_about_dialog_new (GTK_WINDOW (window->window));
544 main_window_help_contents_cb (GtkWidget *widget,
545 EmpathyMainWindow *window)
547 //gossip_help_show ();
551 main_window_throbber_button_press_event_cb (GtkWidget *throbber_ebox,
552 GdkEventButton *event,
555 if (event->type != GDK_BUTTON_PRESS ||
556 event->button != 1) {
560 gossip_accounts_dialog_show ();
566 main_window_session_protocol_connecting_cb (GossipSession *session,
567 GossipAccount *account,
568 GossipProtocol *protocol,
574 priv = GET_PRIV (app);
576 name = gossip_account_get_name (account);
577 gossip_debug (DEBUG_DOMAIN, "Connecting account:'%s'", name);
579 ephy_spinner_start (EPHY_SPINNER (window->throbber));
583 main_window_session_protocol_connected_cb (GossipSession *session,
584 GossipAccount *account,
585 GossipProtocol *protocol,
592 priv = GET_PRIV (app);
594 name = gossip_account_get_name (account);
595 gossip_debug (DEBUG_DOMAIN, "Connected account:'%s'", name);
597 gossip_session_count_accounts (window->session,
602 if (connecting < 1) {
603 ephy_spinner_stop (EPHY_SPINNER (window->throbber));
606 g_hash_table_remove (window->errors, account);
607 g_hash_table_remove (window->reconnects, account);
609 app_connection_items_update ();
610 app_favorite_chatroom_menu_update ();
612 /* Use saved presence */
613 gossip_app_set_presence (gossip_status_presets_get_default_state (),
614 gossip_status_presets_get_default_status());
616 app_presence_updated ();
620 main_window_session_protocol_disconnected_cb (GossipSession *session,
621 GossipAccount *account,
622 GossipProtocol *protocol,
628 gboolean should_reconnect;
631 priv = GET_PRIV (app);
633 name = gossip_account_get_name (account);
634 gossip_debug (DEBUG_DOMAIN, "Disconnected account:'%s'", name);
636 gossip_session_count_accounts (window->session,
641 if (connecting < 1) {
642 ephy_spinner_stop (EPHY_SPINNER (window->throbber));
645 app_connection_items_update ();
646 app_favorite_chatroom_menu_update ();
647 app_presence_updated ();
649 should_reconnect = reason != GOSSIP_PROTOCOL_DISCONNECT_ASKED;
652 should_reconnect &= !g_hash_table_lookup (window->reconnects, account);
654 if (should_reconnect) {
657 /* Unexpected disconnection, try to reconnect */
658 id = g_timeout_add (RETRY_CONNECT_TIMEOUT * 1000,
659 (GSourceFunc) app_reconnect_cb,
661 g_hash_table_insert (window->reconnects,
662 g_object_ref (account),
672 main_window_accels_load (void)
676 filename = g_build_filename (g_get_home_dir (), ".gnome2", PACKAGE_NAME, ACCELS_FILENAME, NULL);
677 if (g_file_test (filename, G_FILE_TEST_EXISTS)) {
678 gossip_debug (DEBUG_DOMAIN, "Loading from:'%s'", filename);
679 gtk_accel_map_load (filename);
686 main_window_accels_save (void)
689 gchar *file_with_path;
691 dir = g_build_filename (g_get_home_dir (), ".gnome2", PACKAGE_NAME, NULL);
692 g_mkdir_with_parents (dir, S_IRUSR | S_IWUSR | S_IXUSR);
693 file_with_path = g_build_filename (dir, ACCELS_FILENAME, NULL);
696 gossip_debug (DEBUG_DOMAIN, "Saving to:'%s'", file_with_path);
697 gtk_accel_map_save (file_with_path);
699 g_free (file_with_path);
703 main_window_connection_items_setup (EmpathyMainWindow *window,
709 const gchar *widgets_connected[] = {
714 "edit_personal_information"
716 const gchar *widgets_disconnected[] = {
720 for (i = 0, list = NULL; i < G_N_ELEMENTS (widgets_connected); i++) {
721 w = glade_xml_get_widget (glade, widgets_connected[i]);
722 list = g_list_prepend (list, w);
725 window->widgets_connected = list;
727 for (i = 0, list = NULL; i < G_N_ELEMENTS (widgets_disconnected); i++) {
728 w = glade_xml_get_widget (glade, widgets_disconnected[i]);
729 list = g_list_prepend (list, w);
732 window->widgets_disconnected = list;
738 main_window_connection_items_update (void)
742 guint disconnected = 0;
744 /* Get account count for:
745 * - connected and disabled,
746 * - connected and enabled
747 * - disabled and enabled
749 gossip_session_count_accounts (window->session,
754 for (l = window->widgets_connected; l; l = l->next) {
755 gtk_widget_set_sensitive (l->data, (connected > 0));
758 for (l = window->widgets_disconnected; l; l = l->next) {
759 gtk_widget_set_sensitive (l->data, (disconnected > 0));
765 main_window_presence_changed_cb (DBusGProxy *proxy,
767 EmpathyMainWindow *window)
769 gossip_debug (DEBUG_DOMAIN, "presence changed to %d", state);
770 gossip_presence_chooser_set_state (GOSSIP_PRESENCE_CHOOSER (window->presence_chooser),
775 main_window_presence_chooser_changed_cb (GtkWidget *chooser,
778 EmpathyMainWindow *window)
780 gossip_status_presets_set_default (state, status);
781 mission_control_set_presence (window->mc, state, status, NULL, NULL);
785 main_window_configure_event_timeout_cb (EmpathyMainWindow *window)
789 gtk_window_get_size (GTK_WINDOW (window->window), &w, &h);
790 gtk_window_get_position (GTK_WINDOW (window->window), &x, &y);
792 gossip_geometry_save (GEOMETRY_NAME, x, y, w, h);
794 window->size_timeout_id = 0;
800 main_window_configure_event_cb (GtkWidget *widget,
801 GdkEventConfigure *event,
802 EmpathyMainWindow *window)
804 if (window->size_timeout_id) {
805 g_source_remove (window->size_timeout_id);
808 window->size_timeout_id = g_timeout_add (500,
809 (GSourceFunc) main_window_configure_event_timeout_cb,
816 main_window_notify_show_offline_cb (GossipConf *conf,
818 gpointer check_menu_item)
820 gboolean show_offline;
822 if (gossip_conf_get_bool (conf, key, &show_offline)) {
823 gtk_check_menu_item_set_active (GTK_CHECK_MENU_ITEM (check_menu_item),
829 main_window_notify_show_avatars_cb (GossipConf *conf,
831 EmpathyMainWindow *window)
833 gboolean show_avatars;
835 if (gossip_conf_get_bool (conf, key, &show_avatars)) {
836 gossip_contact_list_set_show_avatars (window->contact_list,
842 main_window_notify_compact_contact_list_cb (GossipConf *conf,
844 EmpathyMainWindow *window)
846 gboolean compact_contact_list;
848 if (gossip_conf_get_bool (conf, key, &compact_contact_list)) {
849 gossip_contact_list_set_is_compact (window->contact_list,
850 compact_contact_list);
855 main_window_notify_sort_criterium_cb (GossipConf *conf,
857 EmpathyMainWindow *window)
861 if (gossip_conf_get_string (conf, key, &str)) {
863 GEnumClass *enum_class;
864 GEnumValue *enum_value;
866 type = gossip_contact_list_sort_get_type ();
867 enum_class = G_ENUM_CLASS (g_type_class_peek (type));
868 enum_value = g_enum_get_value_by_nick (enum_class, str);
871 gossip_contact_list_set_sort_criterium (window->contact_list,