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);
157 empathy_main_window_show (void)
159 static EmpathyMainWindow *window = NULL;
163 GtkWidget *show_offline_widget;
168 gboolean show_offline;
169 gboolean show_avatars;
170 gboolean compact_contact_list;
174 gtk_window_present (GTK_WINDOW (window->window));
175 return window->window;
178 window = g_new0 (EmpathyMainWindow, 1);
180 /* Set up interface */
181 glade = gossip_glade_get_file ("empathy-main-window.glade",
184 "main_window", &window->window,
185 "main_vbox", &window->main_vbox,
186 "chat_show_offline", &show_offline_widget,
187 "room", &window->room,
188 "room_sep", &window->room_sep,
189 "room_join_favorites", &window->room_join_favorites,
190 "edit_context", &window->edit_context,
191 "edit_context_separator", &window->edit_context_separator,
192 "presence_toolbar", &window->presence_toolbar,
193 "roster_scrolledwindow", &sw,
196 gossip_glade_connect (glade,
198 "main_window", "destroy", main_window_destroy_cb,
199 "main_window", "configure_event", main_window_configure_event_cb,
200 "chat_quit", "activate", main_window_chat_quit_cb,
201 "chat_new_message", "activate", main_window_chat_new_message_cb,
202 "chat_history", "activate", main_window_chat_history_cb,
203 "room_join_new", "activate", main_window_room_join_new_cb,
204 "room_join_favorites", "activate", main_window_room_join_favorites_cb,
205 "room_manage_favorites", "activate", main_window_room_manage_favorites_cb,
206 "chat_add_contact", "activate", main_window_chat_add_contact_cb,
207 "chat_show_offline", "toggled", main_window_chat_show_offline_cb,
208 "edit", "button-press-event", main_window_edit_button_press_event_cb,
209 "edit_accounts", "activate", main_window_edit_accounts_cb,
210 "edit_personal_information", "activate", main_window_edit_personal_information_cb,
211 "edit_preferences", "activate", main_window_edit_preferences_cb,
212 "help_about", "activate", main_window_help_about_cb,
213 "help_contents", "activate", main_window_help_contents_cb,
216 /* Set up connection related widgets. */
217 main_window_connection_items_setup (window, glade);
218 g_object_unref (glade);
220 window->tooltips = g_object_ref_sink (gtk_tooltips_new ());
223 main_window_favorite_chatroom_menu_setup ();
225 gtk_widget_hide (window->edit_context);
226 gtk_widget_hide (window->edit_context_separator);
228 /* Set up presence chooser
229 * FIXME: Update status message not yet supported by MC
231 window->mc = mission_control_new (tp_get_bus ());
232 window->presence_chooser = gossip_presence_chooser_new ();
233 gtk_widget_show (window->presence_chooser);
234 gossip_presence_chooser_set_flash_interval (GOSSIP_PRESENCE_CHOOSER (window->presence_chooser),
236 state = mission_control_get_presence (window->mc, NULL);
237 gossip_presence_chooser_set_state (GOSSIP_PRESENCE_CHOOSER (window->presence_chooser),
239 dbus_g_proxy_connect_signal (DBUS_G_PROXY (window->mc),
240 "PresenceStatusRequested",
241 G_CALLBACK (main_window_presence_changed_cb),
243 g_signal_connect (window->presence_chooser,
245 G_CALLBACK (main_window_presence_chooser_changed_cb),
249 item = gtk_tool_item_new ();
250 gtk_widget_show (GTK_WIDGET (item));
251 gtk_container_add (GTK_CONTAINER (item), window->presence_chooser);
252 gtk_tool_item_set_is_important (item, TRUE);
253 gtk_tool_item_set_expand (item, TRUE);
254 gtk_toolbar_insert (GTK_TOOLBAR (window->presence_toolbar), item, -1);
256 window->widgets_connected = g_list_prepend (window->widgets_connected,
257 window->presence_chooser);
259 /* Set up the throbber */
260 ebox = gtk_event_box_new ();
261 gtk_event_box_set_visible_window (GTK_EVENT_BOX (ebox), FALSE);
263 window->throbber = ephy_spinner_new ();
264 ephy_spinner_set_size (EPHY_SPINNER (window->throbber), GTK_ICON_SIZE_LARGE_TOOLBAR);
265 gtk_container_add (GTK_CONTAINER (ebox), window->throbber);
267 item = gtk_tool_item_new ();
268 gtk_container_add (GTK_CONTAINER (item), ebox);
269 gtk_widget_show_all (GTK_WIDGET (item));
271 gtk_toolbar_insert (GTK_TOOLBAR (window->presence_toolbar), item, -1);
273 str = _("Show and edit accounts");
274 gtk_tooltips_set_tip (GTK_TOOLTIPS (window->tooltips),
277 g_signal_connect (ebox,
278 "button-press-event",
279 G_CALLBACK (main_window_throbber_button_press_event_cb),
282 /* Set up contact list. */
283 gossip_status_presets_get_all ();
284 window->contact_list = gossip_contact_list_new ();
285 gtk_widget_show (GTK_WIDGET (window->contact_list));
286 gtk_container_add (GTK_CONTAINER (sw),
287 GTK_WIDGET (window->contact_list));
289 /* Load user-defined accelerators. */
290 main_window_accels_load ();
292 /* Set window size. */
293 gossip_geometry_load (GEOMETRY_NAME, &x, &y, &w, &h);
295 if (w >= 1 && h >= 1) {
296 /* Use the defaults from the glade file if we
297 * don't have good w, h geometry.
299 gossip_debug (DEBUG_DOMAIN, "Configuring window default size w:%d, h:%d", w, h);
300 gtk_window_set_default_size (GTK_WINDOW (window->window), w, h);
303 if (x >= 0 && y >= 0) {
304 /* Let the window manager position it if we
305 * don't have good x, y coordinates.
307 gossip_debug (DEBUG_DOMAIN, "Configuring window default position x:%d, y:%d", x, y);
308 gtk_window_move (GTK_WINDOW (window->window), x, y);
311 /* Get preferences */
312 conf = gossip_conf_get ();
313 gossip_conf_get_bool (conf,
314 GOSSIP_PREFS_CONTACTS_SHOW_OFFLINE,
316 gossip_conf_notify_add (conf,
317 GOSSIP_PREFS_CONTACTS_SHOW_OFFLINE,
318 main_window_notify_show_offline_cb,
319 show_offline_widget);
321 gtk_check_menu_item_set_active (GTK_CHECK_MENU_ITEM (show_offline_widget),
324 gossip_conf_get_bool (conf,
325 GOSSIP_PREFS_UI_SHOW_AVATARS,
327 gossip_conf_notify_add (conf,
328 GOSSIP_PREFS_UI_SHOW_AVATARS,
329 (GossipConfNotifyFunc) main_window_notify_show_avatars_cb,
331 gossip_conf_get_bool (conf,
332 GOSSIP_PREFS_UI_COMPACT_CONTACT_LIST,
333 &compact_contact_list);
334 gossip_conf_notify_add (conf,
335 GOSSIP_PREFS_UI_COMPACT_CONTACT_LIST,
336 (GossipConfNotifyFunc) main_window_notify_compact_contact_list_cb,
339 g_object_set (window->contact_list,
340 "show-avatars", show_avatars,
341 "is-compact", compact_contact_list,
344 gtk_widget_show (window->window);
346 return window->window;
350 main_window_destroy_cb (GtkWidget *widget,
351 EmpathyMainWindow *window)
353 /* Save user-defined accelerators. */
354 main_window_accels_save ();
356 if (window->size_timeout_id) {
357 g_source_remove (window->size_timeout_id);
360 g_list_free (window->widgets_connected);
361 g_list_free (window->widgets_disconnected);
363 g_object_unref (window->tooltips);
364 g_object_unref (window->mc);
370 main_window_favorite_chatroom_menu_setup (void)
375 main_window_chat_quit_cb (GtkWidget *widget,
376 EmpathyMainWindow *window)
378 gtk_widget_destroy (window->window);
382 main_window_chat_new_message_cb (GtkWidget *widget,
383 EmpathyMainWindow *window)
385 //gossip_new_message_dialog_show (GTK_WINDOW (window->window));
389 main_window_chat_history_cb (GtkWidget *widget,
390 EmpathyMainWindow *window)
392 //gossip_log_window_show (NULL, NULL);
396 main_window_room_join_new_cb (GtkWidget *widget,
397 EmpathyMainWindow *window)
399 //gossip_new_chatroom_dialog_show (GTK_WINDOW (window->window));
403 main_window_room_join_favorites_cb (GtkWidget *widget,
404 EmpathyMainWindow *window)
406 //gossip_session_chatroom_join_favorites (window->session);
410 main_window_room_manage_favorites_cb (GtkWidget *widget,
411 EmpathyMainWindow *window)
413 //gossip_chatrooms_window_show (NULL, FALSE);
417 main_window_chat_add_contact_cb (GtkWidget *widget,
418 EmpathyMainWindow *window)
420 //gossip_add_contact_dialog_show (GTK_WINDOW (window->window), NULL);
424 main_window_chat_show_offline_cb (GtkCheckMenuItem *item,
425 EmpathyMainWindow *window)
429 current = gtk_check_menu_item_get_active (item);
431 gossip_conf_set_bool (gossip_conf_get (),
432 GOSSIP_PREFS_CONTACTS_SHOW_OFFLINE,
435 /* Turn off sound just while we alter the contact list. */
436 // FIXME: gossip_sound_set_enabled (FALSE);
437 g_object_set (window->contact_list, "show_offline", current, NULL);
438 //gossip_sound_set_enabled (TRUE);
442 main_window_edit_button_press_event_cb (GtkWidget *widget,
443 GdkEventButton *event,
444 EmpathyMainWindow *window)
446 GossipContact *contact;
449 if (!event->button == 1) {
453 group = gossip_contact_list_get_selected_group (window->contact_list);
459 item = GTK_MENU_ITEM (window->edit_context);
460 label = gtk_bin_get_child (GTK_BIN (item));
461 gtk_label_set_text (GTK_LABEL (label), _("Group"));
463 gtk_widget_show (window->edit_context);
464 gtk_widget_show (window->edit_context_separator);
466 submenu = gossip_contact_list_get_group_menu (window->contact_list);
467 gtk_menu_item_set_submenu (item, submenu);
474 contact = gossip_contact_list_get_selected (window->contact_list);
480 item = GTK_MENU_ITEM (window->edit_context);
481 label = gtk_bin_get_child (GTK_BIN (item));
482 gtk_label_set_text (GTK_LABEL (label), _("Contact"));
484 gtk_widget_show (window->edit_context);
485 gtk_widget_show (window->edit_context_separator);
487 submenu = gossip_contact_list_get_contact_menu (window->contact_list,
489 gtk_menu_item_set_submenu (item, submenu);
491 g_object_unref (contact);
496 gtk_widget_hide (window->edit_context);
497 gtk_widget_hide (window->edit_context_separator);
503 main_window_edit_accounts_cb (GtkWidget *widget,
504 EmpathyMainWindow *window)
506 gossip_accounts_dialog_show ();
510 main_window_edit_personal_information_cb (GtkWidget *widget,
511 EmpathyMainWindow *window)
513 //gossip_vcard_dialog_show (GTK_WINDOW (window->window));
517 main_window_edit_preferences_cb (GtkWidget *widget,
518 EmpathyMainWindow *window)
520 gossip_preferences_show ();
524 main_window_help_about_cb (GtkWidget *widget,
525 EmpathyMainWindow *window)
527 //gossip_about_dialog_new (GTK_WINDOW (window->window));
531 main_window_help_contents_cb (GtkWidget *widget,
532 EmpathyMainWindow *window)
534 //gossip_help_show ();
538 main_window_throbber_button_press_event_cb (GtkWidget *throbber_ebox,
539 GdkEventButton *event,
542 if (event->type != GDK_BUTTON_PRESS ||
543 event->button != 1) {
547 gossip_accounts_dialog_show ();
553 main_window_session_protocol_connecting_cb (GossipSession *session,
554 GossipAccount *account,
555 GossipProtocol *protocol,
561 priv = GET_PRIV (app);
563 name = gossip_account_get_name (account);
564 gossip_debug (DEBUG_DOMAIN, "Connecting account:'%s'", name);
566 ephy_spinner_start (EPHY_SPINNER (window->throbber));
570 main_window_session_protocol_connected_cb (GossipSession *session,
571 GossipAccount *account,
572 GossipProtocol *protocol,
579 priv = GET_PRIV (app);
581 name = gossip_account_get_name (account);
582 gossip_debug (DEBUG_DOMAIN, "Connected account:'%s'", name);
584 gossip_session_count_accounts (window->session,
589 if (connecting < 1) {
590 ephy_spinner_stop (EPHY_SPINNER (window->throbber));
593 g_hash_table_remove (window->errors, account);
594 g_hash_table_remove (window->reconnects, account);
596 app_connection_items_update ();
597 app_favorite_chatroom_menu_update ();
599 /* Use saved presence */
600 gossip_app_set_presence (gossip_status_presets_get_default_state (),
601 gossip_status_presets_get_default_status());
603 app_presence_updated ();
607 main_window_session_protocol_disconnected_cb (GossipSession *session,
608 GossipAccount *account,
609 GossipProtocol *protocol,
615 gboolean should_reconnect;
618 priv = GET_PRIV (app);
620 name = gossip_account_get_name (account);
621 gossip_debug (DEBUG_DOMAIN, "Disconnected account:'%s'", name);
623 gossip_session_count_accounts (window->session,
628 if (connecting < 1) {
629 ephy_spinner_stop (EPHY_SPINNER (window->throbber));
632 app_connection_items_update ();
633 app_favorite_chatroom_menu_update ();
634 app_presence_updated ();
636 should_reconnect = reason != GOSSIP_PROTOCOL_DISCONNECT_ASKED;
639 should_reconnect &= !g_hash_table_lookup (window->reconnects, account);
641 if (should_reconnect) {
644 /* Unexpected disconnection, try to reconnect */
645 id = g_timeout_add (RETRY_CONNECT_TIMEOUT * 1000,
646 (GSourceFunc) app_reconnect_cb,
648 g_hash_table_insert (window->reconnects,
649 g_object_ref (account),
659 main_window_accels_load (void)
663 filename = g_build_filename (g_get_home_dir (), ".gnome2", PACKAGE_NAME, ACCELS_FILENAME, NULL);
664 if (g_file_test (filename, G_FILE_TEST_EXISTS)) {
665 gossip_debug (DEBUG_DOMAIN, "Loading from:'%s'", filename);
666 gtk_accel_map_load (filename);
673 main_window_accels_save (void)
676 gchar *file_with_path;
678 dir = g_build_filename (g_get_home_dir (), ".gnome2", PACKAGE_NAME, NULL);
679 g_mkdir_with_parents (dir, S_IRUSR | S_IWUSR | S_IXUSR);
680 file_with_path = g_build_filename (dir, ACCELS_FILENAME, NULL);
683 gossip_debug (DEBUG_DOMAIN, "Saving to:'%s'", file_with_path);
684 gtk_accel_map_save (file_with_path);
686 g_free (file_with_path);
690 main_window_connection_items_setup (EmpathyMainWindow *window,
696 const gchar *widgets_connected[] = {
701 "edit_personal_information"
703 const gchar *widgets_disconnected[] = {
707 for (i = 0, list = NULL; i < G_N_ELEMENTS (widgets_connected); i++) {
708 w = glade_xml_get_widget (glade, widgets_connected[i]);
709 list = g_list_prepend (list, w);
712 window->widgets_connected = list;
714 for (i = 0, list = NULL; i < G_N_ELEMENTS (widgets_disconnected); i++) {
715 w = glade_xml_get_widget (glade, widgets_disconnected[i]);
716 list = g_list_prepend (list, w);
719 window->widgets_disconnected = list;
725 main_window_connection_items_update (void)
729 guint disconnected = 0;
731 /* Get account count for:
732 * - connected and disabled,
733 * - connected and enabled
734 * - disabled and enabled
736 gossip_session_count_accounts (window->session,
741 for (l = window->widgets_connected; l; l = l->next) {
742 gtk_widget_set_sensitive (l->data, (connected > 0));
745 for (l = window->widgets_disconnected; l; l = l->next) {
746 gtk_widget_set_sensitive (l->data, (disconnected > 0));
752 main_window_presence_changed_cb (DBusGProxy *proxy,
754 EmpathyMainWindow *window)
756 gossip_debug (DEBUG_DOMAIN, "presence changed to %d", state);
757 gossip_presence_chooser_set_state (GOSSIP_PRESENCE_CHOOSER (window->presence_chooser),
762 main_window_presence_chooser_changed_cb (GtkWidget *chooser,
765 EmpathyMainWindow *window)
767 gossip_status_presets_set_default (state, status);
768 mission_control_set_presence (window->mc, state, status, NULL, NULL);
772 main_window_configure_event_timeout_cb (EmpathyMainWindow *window)
776 gtk_window_get_size (GTK_WINDOW (window->window), &w, &h);
777 gtk_window_get_position (GTK_WINDOW (window->window), &x, &y);
779 gossip_geometry_save (GEOMETRY_NAME, x, y, w, h);
781 window->size_timeout_id = 0;
787 main_window_configure_event_cb (GtkWidget *widget,
788 GdkEventConfigure *event,
789 EmpathyMainWindow *window)
791 if (window->size_timeout_id) {
792 g_source_remove (window->size_timeout_id);
795 window->size_timeout_id = g_timeout_add (500,
796 (GSourceFunc) main_window_configure_event_timeout_cb,
803 main_window_notify_show_offline_cb (GossipConf *conf,
805 gpointer check_menu_item)
807 gboolean show_offline;
809 if (gossip_conf_get_bool (conf, key, &show_offline)) {
810 gtk_check_menu_item_set_active (GTK_CHECK_MENU_ITEM (check_menu_item),
816 main_window_notify_show_avatars_cb (GossipConf *conf,
818 EmpathyMainWindow *window)
820 gboolean show_avatars;
822 if (gossip_conf_get_bool (conf, key, &show_avatars)) {
823 gossip_contact_list_set_show_avatars (window->contact_list,
829 main_window_notify_compact_contact_list_cb (GossipConf *conf,
831 EmpathyMainWindow *window)
833 gboolean compact_contact_list;
835 if (gossip_conf_get_bool (conf, key, &compact_contact_list)) {
836 gossip_contact_list_set_is_compact (window->contact_list,
837 compact_contact_list);