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;
73 GtkWidget *chat_connect;
74 GtkWidget *chat_disconnect;
75 GtkWidget *chat_search;
79 GtkWidget *room_join_favorites;
80 GtkWidget *edit_context;
81 GtkWidget *edit_context_separator;
86 /* Widgets that are enabled when we're connected/disconnected */
87 GList *widgets_connected;
88 GList *widgets_disconnected;
91 GtkWidget *presence_toolbar;
92 GtkWidget *presence_chooser;
95 guint size_timeout_id;
98 static void main_window_destroy_cb (GtkWidget *widget,
99 EmpathyMainWindow *window);
100 static void main_window_favorite_chatroom_menu_setup (void);
101 static void main_window_chat_quit_cb (GtkWidget *widget,
102 EmpathyMainWindow *window);
103 static void main_window_chat_connect_cb (GtkWidget *widget,
104 EmpathyMainWindow *window);
105 static void main_window_chat_disconnect_cb (GtkWidget *widget,
106 EmpathyMainWindow *window);
107 static void main_window_chat_search_cb (GtkWidget *widget,
108 EmpathyMainWindow *window);
109 static void main_window_chat_new_message_cb (GtkWidget *widget,
110 EmpathyMainWindow *window);
111 static void main_window_chat_history_cb (GtkWidget *widget,
112 EmpathyMainWindow *window);
113 static void main_window_room_join_new_cb (GtkWidget *widget,
114 EmpathyMainWindow *window);
115 static void main_window_room_join_favorites_cb (GtkWidget *widget,
116 EmpathyMainWindow *window);
117 static void main_window_room_manage_favorites_cb (GtkWidget *widget,
118 EmpathyMainWindow *window);
119 static void main_window_chat_add_contact_cb (GtkWidget *widget,
120 EmpathyMainWindow *window);
121 static void main_window_chat_show_offline_cb (GtkCheckMenuItem *item,
122 EmpathyMainWindow *window);
123 static gboolean main_window_edit_button_press_event_cb (GtkWidget *widget,
124 GdkEventButton *event,
125 EmpathyMainWindow *window);
126 static void main_window_edit_accounts_cb (GtkWidget *widget,
127 EmpathyMainWindow *window);
128 static void main_window_edit_personal_information_cb (GtkWidget *widget,
129 EmpathyMainWindow *window);
130 static void main_window_edit_preferences_cb (GtkWidget *widget,
131 EmpathyMainWindow *window);
132 static void main_window_help_about_cb (GtkWidget *widget,
133 EmpathyMainWindow *window);
134 static void main_window_help_contents_cb (GtkWidget *widget,
135 EmpathyMainWindow *window);
136 static gboolean main_window_throbber_button_press_event_cb (GtkWidget *throbber_ebox,
137 GdkEventButton *event,
139 static void main_window_accels_load (void);
140 static void main_window_accels_save (void);
141 static void main_window_connection_items_setup (EmpathyMainWindow *window,
143 //static void main_window_connection_items_update (void);
144 static void main_window_presence_changed_cb (DBusGProxy *proxy,
146 EmpathyMainWindow *window);
147 static void main_window_presence_chooser_changed_cb (GtkWidget *chooser,
150 EmpathyMainWindow *window);
151 static gboolean main_window_configure_event_timeout_cb (EmpathyMainWindow *window);
152 static gboolean main_window_configure_event_cb (GtkWidget *widget,
153 GdkEventConfigure *event,
154 EmpathyMainWindow *window);
155 static void main_window_notify_show_offline_cb (GossipConf *conf,
157 gpointer check_menu_item);
158 static void main_window_notify_show_avatars_cb (GossipConf *conf,
160 EmpathyMainWindow *window);
161 static void main_window_notify_compact_contact_list_cb (GossipConf *conf,
163 EmpathyMainWindow *window);
166 empathy_main_window_show (void)
168 static EmpathyMainWindow *window = NULL;
172 GtkWidget *show_offline_widget;
177 gboolean show_offline;
178 gboolean show_avatars;
179 gboolean compact_contact_list;
183 gtk_window_present (GTK_WINDOW (window->window));
184 return window->window;
187 window = g_new0 (EmpathyMainWindow, 1);
189 /* Set up interface */
190 glade = gossip_glade_get_file ("empathy-main-window.glade",
193 "main_window", &window->window,
194 "main_vbox", &window->main_vbox,
195 "chat_connect", &window->chat_connect,
196 "chat_disconnect", &window->chat_disconnect,
197 "chat_search", &window->chat_search,
198 "chat_show_offline", &show_offline_widget,
199 "room", &window->room,
200 "room_sep", &window->room_sep,
201 "room_join_favorites", &window->room_join_favorites,
202 "edit_context", &window->edit_context,
203 "edit_context_separator", &window->edit_context_separator,
204 "presence_toolbar", &window->presence_toolbar,
205 "roster_scrolledwindow", &sw,
208 gossip_glade_connect (glade,
210 "main_window", "destroy", main_window_destroy_cb,
211 "main_window", "configure_event", main_window_configure_event_cb,
212 "chat_quit", "activate", main_window_chat_quit_cb,
213 "chat_connect", "activate", main_window_chat_connect_cb,
214 "chat_disconnect", "activate", main_window_chat_disconnect_cb,
215 "chat_search", "activate", main_window_chat_search_cb,
216 "chat_new_message", "activate", main_window_chat_new_message_cb,
217 "chat_history", "activate", main_window_chat_history_cb,
218 "room_join_new", "activate", main_window_room_join_new_cb,
219 "room_join_favorites", "activate", main_window_room_join_favorites_cb,
220 "room_manage_favorites", "activate", main_window_room_manage_favorites_cb,
221 "chat_add_contact", "activate", main_window_chat_add_contact_cb,
222 "chat_show_offline", "toggled", main_window_chat_show_offline_cb,
223 "edit", "button-press-event", main_window_edit_button_press_event_cb,
224 "edit_accounts", "activate", main_window_edit_accounts_cb,
225 "edit_personal_information", "activate", main_window_edit_personal_information_cb,
226 "edit_preferences", "activate", main_window_edit_preferences_cb,
227 "help_about", "activate", main_window_help_about_cb,
228 "help_contents", "activate", main_window_help_contents_cb,
231 /* Set up connection related widgets. */
232 main_window_connection_items_setup (window, glade);
233 g_object_unref (glade);
235 window->tooltips = g_object_ref_sink (gtk_tooltips_new ());
238 main_window_favorite_chatroom_menu_setup ();
240 gtk_widget_hide (window->edit_context);
241 gtk_widget_hide (window->edit_context_separator);
243 /* Set up presence chooser
244 * FIXME: Update status message not yet supported by MC
246 window->mc = mission_control_new (tp_get_bus ());
247 window->presence_chooser = gossip_presence_chooser_new ();
248 gtk_widget_show (window->presence_chooser);
249 gossip_presence_chooser_set_flash_interval (GOSSIP_PRESENCE_CHOOSER (window->presence_chooser),
251 state = mission_control_get_presence (window->mc, NULL);
252 gossip_presence_chooser_set_state (GOSSIP_PRESENCE_CHOOSER (window->presence_chooser),
254 dbus_g_proxy_connect_signal (DBUS_G_PROXY (window->mc),
255 "PresenceStatusRequested",
256 G_CALLBACK (main_window_presence_changed_cb),
258 g_signal_connect (window->presence_chooser,
260 G_CALLBACK (main_window_presence_chooser_changed_cb),
264 item = gtk_tool_item_new ();
265 gtk_widget_show (GTK_WIDGET (item));
266 gtk_container_add (GTK_CONTAINER (item), window->presence_chooser);
267 gtk_tool_item_set_is_important (item, TRUE);
268 gtk_tool_item_set_expand (item, TRUE);
269 gtk_toolbar_insert (GTK_TOOLBAR (window->presence_toolbar), item, -1);
271 window->widgets_connected = g_list_prepend (window->widgets_connected,
272 window->presence_chooser);
274 /* Set up the throbber */
275 ebox = gtk_event_box_new ();
276 gtk_event_box_set_visible_window (GTK_EVENT_BOX (ebox), FALSE);
278 window->throbber = ephy_spinner_new ();
279 ephy_spinner_set_size (EPHY_SPINNER (window->throbber), GTK_ICON_SIZE_LARGE_TOOLBAR);
280 gtk_container_add (GTK_CONTAINER (ebox), window->throbber);
282 item = gtk_tool_item_new ();
283 gtk_container_add (GTK_CONTAINER (item), ebox);
284 gtk_widget_show_all (GTK_WIDGET (item));
286 gtk_toolbar_insert (GTK_TOOLBAR (window->presence_toolbar), item, -1);
288 str = _("Show and edit accounts");
289 gtk_tooltips_set_tip (GTK_TOOLTIPS (window->tooltips),
292 g_signal_connect (ebox,
293 "button-press-event",
294 G_CALLBACK (main_window_throbber_button_press_event_cb),
297 /* Set up contact list. */
298 gossip_status_presets_get_all ();
299 window->contact_list = gossip_contact_list_new ();
300 gtk_widget_show (GTK_WIDGET (window->contact_list));
301 gtk_container_add (GTK_CONTAINER (sw),
302 GTK_WIDGET (window->contact_list));
304 /* Load user-defined accelerators. */
305 main_window_accels_load ();
307 /* Set window size. */
308 gossip_geometry_load (GEOMETRY_NAME, &x, &y, &w, &h);
310 if (w >= 1 && h >= 1) {
311 /* Use the defaults from the glade file if we
312 * don't have good w, h geometry.
314 gossip_debug (DEBUG_DOMAIN, "Configuring window default size w:%d, h:%d", w, h);
315 gtk_window_set_default_size (GTK_WINDOW (window->window), w, h);
318 if (x >= 0 && y >= 0) {
319 /* Let the window manager position it if we
320 * don't have good x, y coordinates.
322 gossip_debug (DEBUG_DOMAIN, "Configuring window default position x:%d, y:%d", x, y);
323 gtk_window_move (GTK_WINDOW (window->window), x, y);
326 /* Get preferences */
327 conf = gossip_conf_get ();
328 gossip_conf_get_bool (conf,
329 GOSSIP_PREFS_CONTACTS_SHOW_OFFLINE,
331 gossip_conf_notify_add (conf,
332 GOSSIP_PREFS_CONTACTS_SHOW_OFFLINE,
333 main_window_notify_show_offline_cb,
334 show_offline_widget);
336 gtk_check_menu_item_set_active (GTK_CHECK_MENU_ITEM (show_offline_widget),
339 gossip_conf_get_bool (conf,
340 GOSSIP_PREFS_UI_SHOW_AVATARS,
342 gossip_conf_notify_add (conf,
343 GOSSIP_PREFS_UI_SHOW_AVATARS,
344 (GossipConfNotifyFunc) main_window_notify_show_avatars_cb,
346 gossip_conf_get_bool (conf,
347 GOSSIP_PREFS_UI_COMPACT_CONTACT_LIST,
348 &compact_contact_list);
349 gossip_conf_notify_add (conf,
350 GOSSIP_PREFS_UI_COMPACT_CONTACT_LIST,
351 (GossipConfNotifyFunc) main_window_notify_compact_contact_list_cb,
354 g_object_set (window->contact_list,
355 "show-avatars", show_avatars,
356 "is-compact", compact_contact_list,
359 gtk_widget_show (window->window);
361 return window->window;
365 main_window_destroy_cb (GtkWidget *widget,
366 EmpathyMainWindow *window)
368 /* Save user-defined accelerators. */
369 main_window_accels_save ();
371 if (window->size_timeout_id) {
372 g_source_remove (window->size_timeout_id);
375 g_list_free (window->widgets_connected);
376 g_list_free (window->widgets_disconnected);
378 g_object_unref (window->tooltips);
379 g_object_unref (window->mc);
385 main_window_favorite_chatroom_menu_setup (void)
390 main_window_chat_quit_cb (GtkWidget *widget,
391 EmpathyMainWindow *window)
393 gtk_widget_destroy (window->window);
397 main_window_chat_connect_cb (GtkWidget *widget,
398 EmpathyMainWindow *window)
403 main_window_chat_disconnect_cb (GtkWidget *widget,
404 EmpathyMainWindow *window)
409 main_window_chat_search_cb (GtkWidget *widget,
410 EmpathyMainWindow *window)
415 main_window_chat_new_message_cb (GtkWidget *widget,
416 EmpathyMainWindow *window)
418 //gossip_new_message_dialog_show (GTK_WINDOW (window->window));
422 main_window_chat_history_cb (GtkWidget *widget,
423 EmpathyMainWindow *window)
425 //gossip_log_window_show (NULL, NULL);
429 main_window_room_join_new_cb (GtkWidget *widget,
430 EmpathyMainWindow *window)
432 //gossip_new_chatroom_dialog_show (GTK_WINDOW (window->window));
436 main_window_room_join_favorites_cb (GtkWidget *widget,
437 EmpathyMainWindow *window)
439 //gossip_session_chatroom_join_favorites (window->session);
443 main_window_room_manage_favorites_cb (GtkWidget *widget,
444 EmpathyMainWindow *window)
446 //gossip_chatrooms_window_show (NULL, FALSE);
450 main_window_chat_add_contact_cb (GtkWidget *widget,
451 EmpathyMainWindow *window)
453 //gossip_add_contact_dialog_show (GTK_WINDOW (window->window), NULL);
457 main_window_chat_show_offline_cb (GtkCheckMenuItem *item,
458 EmpathyMainWindow *window)
462 current = gtk_check_menu_item_get_active (item);
464 gossip_conf_set_bool (gossip_conf_get (),
465 GOSSIP_PREFS_CONTACTS_SHOW_OFFLINE,
468 /* Turn off sound just while we alter the contact list. */
469 // FIXME: gossip_sound_set_enabled (FALSE);
470 g_object_set (window->contact_list, "show_offline", current, NULL);
471 //gossip_sound_set_enabled (TRUE);
475 main_window_edit_button_press_event_cb (GtkWidget *widget,
476 GdkEventButton *event,
477 EmpathyMainWindow *window)
479 GossipContact *contact;
482 if (!event->button == 1) {
486 group = gossip_contact_list_get_selected_group (window->contact_list);
492 item = GTK_MENU_ITEM (window->edit_context);
493 label = gtk_bin_get_child (GTK_BIN (item));
494 gtk_label_set_text (GTK_LABEL (label), _("Group"));
496 gtk_widget_show (window->edit_context);
497 gtk_widget_show (window->edit_context_separator);
499 submenu = gossip_contact_list_get_group_menu (window->contact_list);
500 gtk_menu_item_set_submenu (item, submenu);
507 contact = gossip_contact_list_get_selected (window->contact_list);
513 item = GTK_MENU_ITEM (window->edit_context);
514 label = gtk_bin_get_child (GTK_BIN (item));
515 gtk_label_set_text (GTK_LABEL (label), _("Contact"));
517 gtk_widget_show (window->edit_context);
518 gtk_widget_show (window->edit_context_separator);
520 submenu = gossip_contact_list_get_contact_menu (window->contact_list,
522 gtk_menu_item_set_submenu (item, submenu);
524 g_object_unref (contact);
529 gtk_widget_hide (window->edit_context);
530 gtk_widget_hide (window->edit_context_separator);
536 main_window_edit_accounts_cb (GtkWidget *widget,
537 EmpathyMainWindow *window)
539 gossip_accounts_dialog_show ();
543 main_window_edit_personal_information_cb (GtkWidget *widget,
544 EmpathyMainWindow *window)
546 //gossip_vcard_dialog_show (GTK_WINDOW (window->window));
550 main_window_edit_preferences_cb (GtkWidget *widget,
551 EmpathyMainWindow *window)
553 gossip_preferences_show ();
557 main_window_help_about_cb (GtkWidget *widget,
558 EmpathyMainWindow *window)
560 //gossip_about_dialog_new (GTK_WINDOW (window->window));
564 main_window_help_contents_cb (GtkWidget *widget,
565 EmpathyMainWindow *window)
567 //gossip_help_show ();
571 main_window_throbber_button_press_event_cb (GtkWidget *throbber_ebox,
572 GdkEventButton *event,
575 if (event->type != GDK_BUTTON_PRESS ||
576 event->button != 1) {
580 gossip_accounts_dialog_show ();
586 main_window_session_protocol_connecting_cb (GossipSession *session,
587 GossipAccount *account,
588 GossipProtocol *protocol,
594 priv = GET_PRIV (app);
596 name = gossip_account_get_name (account);
597 gossip_debug (DEBUG_DOMAIN, "Connecting account:'%s'", name);
599 ephy_spinner_start (EPHY_SPINNER (window->throbber));
603 main_window_session_protocol_connected_cb (GossipSession *session,
604 GossipAccount *account,
605 GossipProtocol *protocol,
612 priv = GET_PRIV (app);
614 name = gossip_account_get_name (account);
615 gossip_debug (DEBUG_DOMAIN, "Connected account:'%s'", name);
617 gossip_session_count_accounts (window->session,
622 if (connecting < 1) {
623 ephy_spinner_stop (EPHY_SPINNER (window->throbber));
626 g_hash_table_remove (window->errors, account);
627 g_hash_table_remove (window->reconnects, account);
629 app_connection_items_update ();
630 app_favorite_chatroom_menu_update ();
632 /* Use saved presence */
633 gossip_app_set_presence (gossip_status_presets_get_default_state (),
634 gossip_status_presets_get_default_status());
636 app_presence_updated ();
640 main_window_session_protocol_disconnected_cb (GossipSession *session,
641 GossipAccount *account,
642 GossipProtocol *protocol,
648 gboolean should_reconnect;
651 priv = GET_PRIV (app);
653 name = gossip_account_get_name (account);
654 gossip_debug (DEBUG_DOMAIN, "Disconnected account:'%s'", name);
656 gossip_session_count_accounts (window->session,
661 if (connecting < 1) {
662 ephy_spinner_stop (EPHY_SPINNER (window->throbber));
665 app_connection_items_update ();
666 app_favorite_chatroom_menu_update ();
667 app_presence_updated ();
669 should_reconnect = reason != GOSSIP_PROTOCOL_DISCONNECT_ASKED;
672 should_reconnect &= !g_hash_table_lookup (window->reconnects, account);
674 if (should_reconnect) {
677 /* Unexpected disconnection, try to reconnect */
678 id = g_timeout_add (RETRY_CONNECT_TIMEOUT * 1000,
679 (GSourceFunc) app_reconnect_cb,
681 g_hash_table_insert (window->reconnects,
682 g_object_ref (account),
692 main_window_accels_load (void)
696 filename = g_build_filename (g_get_home_dir (), ".gnome2", PACKAGE_NAME, ACCELS_FILENAME, NULL);
697 if (g_file_test (filename, G_FILE_TEST_EXISTS)) {
698 gossip_debug (DEBUG_DOMAIN, "Loading from:'%s'", filename);
699 gtk_accel_map_load (filename);
706 main_window_accels_save (void)
709 gchar *file_with_path;
711 dir = g_build_filename (g_get_home_dir (), ".gnome2", PACKAGE_NAME, NULL);
712 g_mkdir_with_parents (dir, S_IRUSR | S_IWUSR | S_IXUSR);
713 file_with_path = g_build_filename (dir, ACCELS_FILENAME, NULL);
716 gossip_debug (DEBUG_DOMAIN, "Saving to:'%s'", file_with_path);
717 gtk_accel_map_save (file_with_path);
719 g_free (file_with_path);
723 main_window_connection_items_setup (EmpathyMainWindow *window,
729 const gchar *widgets_connected[] = {
734 "edit_personal_information"
736 const gchar *widgets_disconnected[] = {
740 for (i = 0, list = NULL; i < G_N_ELEMENTS (widgets_connected); i++) {
741 w = glade_xml_get_widget (glade, widgets_connected[i]);
742 list = g_list_prepend (list, w);
745 window->widgets_connected = list;
747 for (i = 0, list = NULL; i < G_N_ELEMENTS (widgets_disconnected); i++) {
748 w = glade_xml_get_widget (glade, widgets_disconnected[i]);
749 list = g_list_prepend (list, w);
752 window->widgets_disconnected = list;
758 main_window_connection_items_update (void)
762 guint disconnected = 0;
764 /* Get account count for:
765 * - connected and disabled,
766 * - connected and enabled
767 * - disabled and enabled
769 gossip_session_count_accounts (window->session,
774 for (l = window->widgets_connected; l; l = l->next) {
775 gtk_widget_set_sensitive (l->data, (connected > 0));
778 for (l = window->widgets_disconnected; l; l = l->next) {
779 gtk_widget_set_sensitive (l->data, (disconnected > 0));
785 main_window_presence_changed_cb (DBusGProxy *proxy,
787 EmpathyMainWindow *window)
789 gossip_debug (DEBUG_DOMAIN, "presence changed to %d", state);
790 gossip_presence_chooser_set_state (GOSSIP_PRESENCE_CHOOSER (window->presence_chooser),
795 main_window_presence_chooser_changed_cb (GtkWidget *chooser,
798 EmpathyMainWindow *window)
800 gossip_status_presets_set_default (state, status);
801 mission_control_set_presence (window->mc, state, status, NULL, NULL);
805 main_window_configure_event_timeout_cb (EmpathyMainWindow *window)
809 gtk_window_get_size (GTK_WINDOW (window->window), &w, &h);
810 gtk_window_get_position (GTK_WINDOW (window->window), &x, &y);
812 gossip_geometry_save (GEOMETRY_NAME, x, y, w, h);
814 window->size_timeout_id = 0;
820 main_window_configure_event_cb (GtkWidget *widget,
821 GdkEventConfigure *event,
822 EmpathyMainWindow *window)
824 if (window->size_timeout_id) {
825 g_source_remove (window->size_timeout_id);
828 window->size_timeout_id = g_timeout_add (500,
829 (GSourceFunc) main_window_configure_event_timeout_cb,
836 main_window_notify_show_offline_cb (GossipConf *conf,
838 gpointer check_menu_item)
840 gboolean show_offline;
842 if (gossip_conf_get_bool (conf, key, &show_offline)) {
843 gtk_check_menu_item_set_active (GTK_CHECK_MENU_ITEM (check_menu_item),
849 main_window_notify_show_avatars_cb (GossipConf *conf,
851 EmpathyMainWindow *window)
853 gboolean show_avatars;
855 if (gossip_conf_get_bool (conf, key, &show_avatars)) {
856 gossip_contact_list_set_show_avatars (window->contact_list,
862 main_window_notify_compact_contact_list_cb (GossipConf *conf,
864 EmpathyMainWindow *window)
866 gboolean compact_contact_list;
868 if (gossip_conf_get_bool (conf, key, &compact_contact_list)) {
869 gossip_contact_list_set_is_compact (window->contact_list,
870 compact_contact_list);