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 <libempathy/gossip-conf.h>
31 #include <libempathy/gossip-contact.h>
32 #include <libempathy/gossip-debug.h>
34 #include "empathy-main-window.h"
35 #include "ephy-spinner.h"
36 #include "gossip-contact-list.h"
37 #include "gossip-presence-chooser.h"
38 #include "gossip-ui-utils.h"
39 #include "gossip-status-presets.h"
40 #include "gossip-geometry.h"
41 #include "gossip-preferences.h"
42 #include "gossip-accounts-dialog.h"
44 #define DEBUG_DOMAIN "EmpathyMainWindow"
46 /* Minimum width of roster window if something goes wrong. */
49 /* Accels (menu shortcuts) can be configured and saved */
50 #define ACCELS_FILENAME "accels.txt"
52 /* Flashing delay for icons (milliseconds). */
53 #define FLASH_TIMEOUT 500
55 /* Name in the geometry file */
56 #define GEOMETRY_NAME "main-window"
59 GossipContactList *contact_list;
65 /* Tooltips for all widgets */
66 GtkTooltips *tooltips;
69 GtkWidget *chat_connect;
70 GtkWidget *chat_disconnect;
71 GtkWidget *chat_search;
75 GtkWidget *room_join_favorites;
76 GtkWidget *edit_context;
77 GtkWidget *edit_context_separator;
82 /* Widgets that are enabled when we're connected/disconnected */
83 GList *widgets_connected;
84 GList *widgets_disconnected;
87 GtkWidget *presence_toolbar;
88 GtkWidget *presence_chooser;
91 guint size_timeout_id;
94 static void main_window_destroy_cb (GtkWidget *widget,
95 EmpathyMainWindow *window);
96 static void main_window_favorite_chatroom_menu_setup (void);
97 static void main_window_chat_quit_cb (GtkWidget *widget,
98 EmpathyMainWindow *window);
99 static void main_window_chat_connect_cb (GtkWidget *widget,
100 EmpathyMainWindow *window);
101 static void main_window_chat_disconnect_cb (GtkWidget *widget,
102 EmpathyMainWindow *window);
103 static void main_window_chat_search_cb (GtkWidget *widget,
104 EmpathyMainWindow *window);
105 static void main_window_chat_new_message_cb (GtkWidget *widget,
106 EmpathyMainWindow *window);
107 static void main_window_chat_history_cb (GtkWidget *widget,
108 EmpathyMainWindow *window);
109 static void main_window_room_join_new_cb (GtkWidget *widget,
110 EmpathyMainWindow *window);
111 static void main_window_room_join_favorites_cb (GtkWidget *widget,
112 EmpathyMainWindow *window);
113 static void main_window_room_manage_favorites_cb (GtkWidget *widget,
114 EmpathyMainWindow *window);
115 static void main_window_chat_add_contact_cb (GtkWidget *widget,
116 EmpathyMainWindow *window);
117 static void main_window_chat_show_offline_cb (GtkCheckMenuItem *item,
118 EmpathyMainWindow *window);
119 static gboolean main_window_edit_button_press_event_cb (GtkWidget *widget,
120 GdkEventButton *event,
121 EmpathyMainWindow *window);
122 static void main_window_edit_accounts_cb (GtkWidget *widget,
123 EmpathyMainWindow *window);
124 static void main_window_edit_personal_information_cb (GtkWidget *widget,
125 EmpathyMainWindow *window);
126 static void main_window_edit_preferences_cb (GtkWidget *widget,
127 EmpathyMainWindow *window);
128 static void main_window_help_about_cb (GtkWidget *widget,
129 EmpathyMainWindow *window);
130 static void main_window_help_contents_cb (GtkWidget *widget,
131 EmpathyMainWindow *window);
132 static gboolean main_window_throbber_button_press_event_cb (GtkWidget *throbber_ebox,
133 GdkEventButton *event,
135 static void main_window_accels_load (void);
136 static void main_window_accels_save (void);
137 static void main_window_connection_items_setup (EmpathyMainWindow *window,
139 //static void main_window_connection_items_update (void);
140 static void main_window_presence_chooser_changed_cb (GtkWidget *chooser,
141 GossipPresenceState state,
143 EmpathyMainWindow *window);
144 static gboolean main_window_configure_event_timeout_cb (EmpathyMainWindow *window);
145 static gboolean main_window_configure_event_cb (GtkWidget *widget,
146 GdkEventConfigure *event,
147 EmpathyMainWindow *window);
148 static void main_window_notify_show_offline_cb (GossipConf *conf,
150 gpointer check_menu_item);
151 static void main_window_notify_show_avatars_cb (GossipConf *conf,
153 EmpathyMainWindow *window);
154 static void main_window_notify_compact_contact_list_cb (GossipConf *conf,
156 EmpathyMainWindow *window);
159 empathy_main_window_show (void)
161 static EmpathyMainWindow *window = NULL;
165 GtkWidget *show_offline_widget;
169 gboolean show_offline;
170 gboolean show_avatars;
171 gboolean compact_contact_list;
175 gtk_window_present (GTK_WINDOW (window->window));
176 return window->window;
179 window = g_new0 (EmpathyMainWindow, 1);
181 /* Set up interface */
182 glade = gossip_glade_get_file ("empathy-main-window.glade",
185 "main_window", &window->window,
186 "main_vbox", &window->main_vbox,
187 "chat_connect", &window->chat_connect,
188 "chat_disconnect", &window->chat_disconnect,
189 "chat_search", &window->chat_search,
190 "chat_show_offline", &show_offline_widget,
191 "room", &window->room,
192 "room_sep", &window->room_sep,
193 "room_join_favorites", &window->room_join_favorites,
194 "edit_context", &window->edit_context,
195 "edit_context_separator", &window->edit_context_separator,
196 "presence_toolbar", &window->presence_toolbar,
197 "roster_scrolledwindow", &sw,
200 gossip_glade_connect (glade,
202 "main_window", "destroy", main_window_destroy_cb,
203 "main_window", "configure_event", main_window_configure_event_cb,
204 "chat_quit", "activate", main_window_chat_quit_cb,
205 "chat_connect", "activate", main_window_chat_connect_cb,
206 "chat_disconnect", "activate", main_window_chat_disconnect_cb,
207 "chat_search", "activate", main_window_chat_search_cb,
208 "chat_new_message", "activate", main_window_chat_new_message_cb,
209 "chat_history", "activate", main_window_chat_history_cb,
210 "room_join_new", "activate", main_window_room_join_new_cb,
211 "room_join_favorites", "activate", main_window_room_join_favorites_cb,
212 "room_manage_favorites", "activate", main_window_room_manage_favorites_cb,
213 "chat_add_contact", "activate", main_window_chat_add_contact_cb,
214 "chat_show_offline", "toggled", main_window_chat_show_offline_cb,
215 "edit", "button-press-event", main_window_edit_button_press_event_cb,
216 "edit_accounts", "activate", main_window_edit_accounts_cb,
217 "edit_personal_information", "activate", main_window_edit_personal_information_cb,
218 "edit_preferences", "activate", main_window_edit_preferences_cb,
219 "help_about", "activate", main_window_help_about_cb,
220 "help_contents", "activate", main_window_help_contents_cb,
223 /* Set up connection related widgets. */
224 main_window_connection_items_setup (window, glade);
225 g_object_unref (glade);
227 window->tooltips = g_object_ref_sink (gtk_tooltips_new ());
230 main_window_favorite_chatroom_menu_setup ();
232 gtk_widget_hide (window->edit_context);
233 gtk_widget_hide (window->edit_context_separator);
235 /* Set up presence chooser */
236 window->presence_chooser = gossip_presence_chooser_new ();
237 gtk_widget_show (window->presence_chooser);
238 gossip_presence_chooser_set_flash_interval (GOSSIP_PRESENCE_CHOOSER (window->presence_chooser),
241 item = gtk_tool_item_new ();
242 gtk_widget_show (GTK_WIDGET (item));
243 gtk_container_add (GTK_CONTAINER (item), window->presence_chooser);
244 gtk_tool_item_set_is_important (item, TRUE);
245 gtk_tool_item_set_expand (item, TRUE);
246 gtk_toolbar_insert (GTK_TOOLBAR (window->presence_toolbar), item, -1);
248 g_signal_connect (window->presence_chooser,
250 G_CALLBACK (main_window_presence_chooser_changed_cb),
253 window->widgets_connected = g_list_prepend (window->widgets_connected,
254 window->presence_chooser);
256 /* Set up the throbber */
257 ebox = gtk_event_box_new ();
258 gtk_event_box_set_visible_window (GTK_EVENT_BOX (ebox), FALSE);
260 window->throbber = ephy_spinner_new ();
261 ephy_spinner_set_size (EPHY_SPINNER (window->throbber), GTK_ICON_SIZE_LARGE_TOOLBAR);
262 gtk_container_add (GTK_CONTAINER (ebox), window->throbber);
264 item = gtk_tool_item_new ();
265 gtk_container_add (GTK_CONTAINER (item), ebox);
266 gtk_widget_show_all (GTK_WIDGET (item));
268 gtk_toolbar_insert (GTK_TOOLBAR (window->presence_toolbar), item, -1);
270 str = _("Show and edit accounts");
271 gtk_tooltips_set_tip (GTK_TOOLTIPS (window->tooltips),
274 g_signal_connect (ebox,
275 "button-press-event",
276 G_CALLBACK (main_window_throbber_button_press_event_cb),
279 /* Set up contact list. */
280 gossip_status_presets_get_all ();
281 window->contact_list = gossip_contact_list_new ();
282 gtk_widget_show (GTK_WIDGET (window->contact_list));
283 gtk_container_add (GTK_CONTAINER (sw),
284 GTK_WIDGET (window->contact_list));
286 /* Load user-defined accelerators. */
287 main_window_accels_load ();
289 /* Set window size. */
290 gossip_geometry_load (GEOMETRY_NAME, &x, &y, &w, &h);
292 if (w >= 1 && h >= 1) {
293 /* Use the defaults from the glade file if we
294 * don't have good w, h geometry.
296 gossip_debug (DEBUG_DOMAIN, "Configuring window default size w:%d, h:%d", w, h);
297 gtk_window_set_default_size (GTK_WINDOW (window->window), w, h);
300 if (x >= 0 && y >= 0) {
301 /* Let the window manager position it if we
302 * don't have good x, y coordinates.
304 gossip_debug (DEBUG_DOMAIN, "Configuring window default position x:%d, y:%d", x, y);
305 gtk_window_move (GTK_WINDOW (window->window), x, y);
308 /* Get preferences */
309 conf = gossip_conf_get ();
310 gossip_conf_get_bool (conf,
311 GOSSIP_PREFS_CONTACTS_SHOW_OFFLINE,
313 gossip_conf_notify_add (conf,
314 GOSSIP_PREFS_CONTACTS_SHOW_OFFLINE,
315 main_window_notify_show_offline_cb,
316 show_offline_widget);
318 gtk_check_menu_item_set_active (GTK_CHECK_MENU_ITEM (show_offline_widget),
321 gossip_conf_get_bool (conf,
322 GOSSIP_PREFS_UI_SHOW_AVATARS,
324 gossip_conf_notify_add (conf,
325 GOSSIP_PREFS_UI_SHOW_AVATARS,
326 (GossipConfNotifyFunc) main_window_notify_show_avatars_cb,
328 gossip_conf_get_bool (conf,
329 GOSSIP_PREFS_UI_COMPACT_CONTACT_LIST,
330 &compact_contact_list);
331 gossip_conf_notify_add (conf,
332 GOSSIP_PREFS_UI_COMPACT_CONTACT_LIST,
333 (GossipConfNotifyFunc) main_window_notify_compact_contact_list_cb,
336 g_object_set (window->contact_list,
337 "show-avatars", show_avatars,
338 "is-compact", compact_contact_list,
341 gtk_widget_show (window->window);
343 return window->window;
347 main_window_destroy_cb (GtkWidget *widget,
348 EmpathyMainWindow *window)
350 /* Save user-defined accelerators. */
351 main_window_accels_save ();
353 if (window->size_timeout_id) {
354 g_source_remove (window->size_timeout_id);
357 g_list_free (window->widgets_connected);
358 g_list_free (window->widgets_disconnected);
360 g_object_unref (window->tooltips);
366 main_window_favorite_chatroom_menu_setup (void)
371 main_window_chat_quit_cb (GtkWidget *widget,
372 EmpathyMainWindow *window)
374 gtk_widget_destroy (window->window);
378 main_window_chat_connect_cb (GtkWidget *widget,
379 EmpathyMainWindow *window)
384 main_window_chat_disconnect_cb (GtkWidget *widget,
385 EmpathyMainWindow *window)
390 main_window_chat_search_cb (GtkWidget *widget,
391 EmpathyMainWindow *window)
396 main_window_chat_new_message_cb (GtkWidget *widget,
397 EmpathyMainWindow *window)
399 //gossip_new_message_dialog_show (GTK_WINDOW (window->window));
403 main_window_chat_history_cb (GtkWidget *widget,
404 EmpathyMainWindow *window)
406 //gossip_log_window_show (NULL, NULL);
410 main_window_room_join_new_cb (GtkWidget *widget,
411 EmpathyMainWindow *window)
413 //gossip_new_chatroom_dialog_show (GTK_WINDOW (window->window));
417 main_window_room_join_favorites_cb (GtkWidget *widget,
418 EmpathyMainWindow *window)
420 //gossip_session_chatroom_join_favorites (window->session);
424 main_window_room_manage_favorites_cb (GtkWidget *widget,
425 EmpathyMainWindow *window)
427 //gossip_chatrooms_window_show (NULL, FALSE);
431 main_window_chat_add_contact_cb (GtkWidget *widget,
432 EmpathyMainWindow *window)
434 //gossip_add_contact_dialog_show (GTK_WINDOW (window->window), NULL);
438 main_window_chat_show_offline_cb (GtkCheckMenuItem *item,
439 EmpathyMainWindow *window)
443 current = gtk_check_menu_item_get_active (item);
445 gossip_conf_set_bool (gossip_conf_get (),
446 GOSSIP_PREFS_CONTACTS_SHOW_OFFLINE,
449 /* Turn off sound just while we alter the contact list. */
450 // FIXME: gossip_sound_set_enabled (FALSE);
451 g_object_set (window->contact_list, "show_offline", current, NULL);
452 //gossip_sound_set_enabled (TRUE);
456 main_window_edit_button_press_event_cb (GtkWidget *widget,
457 GdkEventButton *event,
458 EmpathyMainWindow *window)
460 GossipContact *contact;
463 if (!event->button == 1) {
467 group = gossip_contact_list_get_selected_group (window->contact_list);
473 item = GTK_MENU_ITEM (window->edit_context);
474 label = gtk_bin_get_child (GTK_BIN (item));
475 gtk_label_set_text (GTK_LABEL (label), _("Group"));
477 gtk_widget_show (window->edit_context);
478 gtk_widget_show (window->edit_context_separator);
480 submenu = gossip_contact_list_get_group_menu (window->contact_list);
481 gtk_menu_item_set_submenu (item, submenu);
488 contact = gossip_contact_list_get_selected (window->contact_list);
494 item = GTK_MENU_ITEM (window->edit_context);
495 label = gtk_bin_get_child (GTK_BIN (item));
496 gtk_label_set_text (GTK_LABEL (label), _("Contact"));
498 gtk_widget_show (window->edit_context);
499 gtk_widget_show (window->edit_context_separator);
501 submenu = gossip_contact_list_get_contact_menu (window->contact_list,
503 gtk_menu_item_set_submenu (item, submenu);
505 g_object_unref (contact);
510 gtk_widget_hide (window->edit_context);
511 gtk_widget_hide (window->edit_context_separator);
517 main_window_edit_accounts_cb (GtkWidget *widget,
518 EmpathyMainWindow *window)
520 gossip_accounts_dialog_show ();
524 main_window_edit_personal_information_cb (GtkWidget *widget,
525 EmpathyMainWindow *window)
527 //gossip_vcard_dialog_show (GTK_WINDOW (window->window));
531 main_window_edit_preferences_cb (GtkWidget *widget,
532 EmpathyMainWindow *window)
534 //gossip_preferences_show ();
538 main_window_help_about_cb (GtkWidget *widget,
539 EmpathyMainWindow *window)
541 //gossip_about_dialog_new (GTK_WINDOW (window->window));
545 main_window_help_contents_cb (GtkWidget *widget,
546 EmpathyMainWindow *window)
548 //gossip_help_show ();
552 main_window_throbber_button_press_event_cb (GtkWidget *throbber_ebox,
553 GdkEventButton *event,
556 if (event->type != GDK_BUTTON_PRESS ||
557 event->button != 1) {
561 gossip_accounts_dialog_show ();
567 main_window_session_protocol_connecting_cb (GossipSession *session,
568 GossipAccount *account,
569 GossipProtocol *protocol,
575 priv = GET_PRIV (app);
577 name = gossip_account_get_name (account);
578 gossip_debug (DEBUG_DOMAIN, "Connecting account:'%s'", name);
580 ephy_spinner_start (EPHY_SPINNER (window->throbber));
584 main_window_session_protocol_connected_cb (GossipSession *session,
585 GossipAccount *account,
586 GossipProtocol *protocol,
593 priv = GET_PRIV (app);
595 name = gossip_account_get_name (account);
596 gossip_debug (DEBUG_DOMAIN, "Connected account:'%s'", name);
598 gossip_session_count_accounts (window->session,
603 if (connecting < 1) {
604 ephy_spinner_stop (EPHY_SPINNER (window->throbber));
607 g_hash_table_remove (window->errors, account);
608 g_hash_table_remove (window->reconnects, account);
610 app_connection_items_update ();
611 app_favorite_chatroom_menu_update ();
613 /* Use saved presence */
614 gossip_app_set_presence (gossip_status_presets_get_default_state (),
615 gossip_status_presets_get_default_status());
617 app_presence_updated ();
621 main_window_session_protocol_disconnected_cb (GossipSession *session,
622 GossipAccount *account,
623 GossipProtocol *protocol,
629 gboolean should_reconnect;
632 priv = GET_PRIV (app);
634 name = gossip_account_get_name (account);
635 gossip_debug (DEBUG_DOMAIN, "Disconnected account:'%s'", name);
637 gossip_session_count_accounts (window->session,
642 if (connecting < 1) {
643 ephy_spinner_stop (EPHY_SPINNER (window->throbber));
646 app_connection_items_update ();
647 app_favorite_chatroom_menu_update ();
648 app_presence_updated ();
650 should_reconnect = reason != GOSSIP_PROTOCOL_DISCONNECT_ASKED;
653 should_reconnect &= !g_hash_table_lookup (window->reconnects, account);
655 if (should_reconnect) {
658 /* Unexpected disconnection, try to reconnect */
659 id = g_timeout_add (RETRY_CONNECT_TIMEOUT * 1000,
660 (GSourceFunc) app_reconnect_cb,
662 g_hash_table_insert (window->reconnects,
663 g_object_ref (account),
673 main_window_accels_load (void)
677 filename = g_build_filename (g_get_home_dir (), ".gnome2", PACKAGE_NAME, ACCELS_FILENAME, NULL);
678 if (g_file_test (filename, G_FILE_TEST_EXISTS)) {
679 gossip_debug (DEBUG_DOMAIN, "Loading from:'%s'", filename);
680 gtk_accel_map_load (filename);
687 main_window_accels_save (void)
690 gchar *file_with_path;
692 dir = g_build_filename (g_get_home_dir (), ".gnome2", PACKAGE_NAME, NULL);
693 g_mkdir_with_parents (dir, S_IRUSR | S_IWUSR | S_IXUSR);
694 file_with_path = g_build_filename (dir, ACCELS_FILENAME, NULL);
697 gossip_debug (DEBUG_DOMAIN, "Saving to:'%s'", file_with_path);
698 gtk_accel_map_save (file_with_path);
700 g_free (file_with_path);
704 main_window_connection_items_setup (EmpathyMainWindow *window,
710 const gchar *widgets_connected[] = {
715 "edit_personal_information"
717 const gchar *widgets_disconnected[] = {
721 for (i = 0, list = NULL; i < G_N_ELEMENTS (widgets_connected); i++) {
722 w = glade_xml_get_widget (glade, widgets_connected[i]);
723 list = g_list_prepend (list, w);
726 window->widgets_connected = list;
728 for (i = 0, list = NULL; i < G_N_ELEMENTS (widgets_disconnected); i++) {
729 w = glade_xml_get_widget (glade, widgets_disconnected[i]);
730 list = g_list_prepend (list, w);
733 window->widgets_disconnected = list;
739 main_window_connection_items_update (void)
743 guint disconnected = 0;
745 /* Get account count for:
746 * - connected and disabled,
747 * - connected and enabled
748 * - disabled and enabled
750 gossip_session_count_accounts (window->session,
755 for (l = window->widgets_connected; l; l = l->next) {
756 gtk_widget_set_sensitive (l->data, (connected > 0));
759 for (l = window->widgets_disconnected; l; l = l->next) {
760 gtk_widget_set_sensitive (l->data, (disconnected > 0));
766 main_window_presence_chooser_changed_cb (GtkWidget *chooser,
767 GossipPresenceState state,
769 EmpathyMainWindow *window)
771 gossip_status_presets_set_default (state, status);
775 main_window_configure_event_timeout_cb (EmpathyMainWindow *window)
779 gtk_window_get_size (GTK_WINDOW (window->window), &w, &h);
780 gtk_window_get_position (GTK_WINDOW (window->window), &x, &y);
782 gossip_geometry_save (GEOMETRY_NAME, x, y, w, h);
784 window->size_timeout_id = 0;
790 main_window_configure_event_cb (GtkWidget *widget,
791 GdkEventConfigure *event,
792 EmpathyMainWindow *window)
794 if (window->size_timeout_id) {
795 g_source_remove (window->size_timeout_id);
798 window->size_timeout_id = g_timeout_add (500,
799 (GSourceFunc) main_window_configure_event_timeout_cb,
806 main_window_notify_show_offline_cb (GossipConf *conf,
808 gpointer check_menu_item)
810 gboolean show_offline;
812 if (gossip_conf_get_bool (conf, key, &show_offline)) {
813 gtk_check_menu_item_set_active (GTK_CHECK_MENU_ITEM (check_menu_item),
819 main_window_notify_show_avatars_cb (GossipConf *conf,
821 EmpathyMainWindow *window)
823 gboolean show_avatars;
825 if (gossip_conf_get_bool (conf, key, &show_avatars)) {
826 gossip_contact_list_set_show_avatars (window->contact_list,
832 main_window_notify_compact_contact_list_cb (GossipConf *conf,
834 EmpathyMainWindow *window)
836 gboolean compact_contact_list;
838 if (gossip_conf_get_bool (conf, key, &compact_contact_list)) {
839 gossip_contact_list_set_is_compact (window->contact_list,
840 compact_contact_list);