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>
32 #include <libempathy/gossip-conf.h>
33 #include <libempathy/gossip-contact.h>
34 #include <libempathy/gossip-debug.h>
35 #include <libempathy/gossip-utils.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 /* Name in the geometry file */
56 #define GEOMETRY_NAME "main-window"
59 GossipContactList *contact_list;
65 /* Tooltips for all widgets */
66 GtkTooltips *tooltips;
72 GtkWidget *room_join_favorites;
73 GtkWidget *edit_context;
74 GtkWidget *edit_context_separator;
79 /* Widgets that are enabled when we're connected/disconnected */
80 GList *widgets_connected;
81 GList *widgets_disconnected;
84 GtkWidget *presence_toolbar;
85 GtkWidget *presence_chooser;
88 guint size_timeout_id;
91 static void main_window_destroy_cb (GtkWidget *widget,
92 EmpathyMainWindow *window);
93 static void main_window_favorite_chatroom_menu_setup (void);
94 static void main_window_chat_quit_cb (GtkWidget *widget,
95 EmpathyMainWindow *window);
96 static void main_window_chat_new_message_cb (GtkWidget *widget,
97 EmpathyMainWindow *window);
98 static void main_window_chat_history_cb (GtkWidget *widget,
99 EmpathyMainWindow *window);
100 static void main_window_room_join_new_cb (GtkWidget *widget,
101 EmpathyMainWindow *window);
102 static void main_window_room_join_favorites_cb (GtkWidget *widget,
103 EmpathyMainWindow *window);
104 static void main_window_room_manage_favorites_cb (GtkWidget *widget,
105 EmpathyMainWindow *window);
106 static void main_window_chat_add_contact_cb (GtkWidget *widget,
107 EmpathyMainWindow *window);
108 static void main_window_chat_show_offline_cb (GtkCheckMenuItem *item,
109 EmpathyMainWindow *window);
110 static gboolean main_window_edit_button_press_event_cb (GtkWidget *widget,
111 GdkEventButton *event,
112 EmpathyMainWindow *window);
113 static void main_window_edit_accounts_cb (GtkWidget *widget,
114 EmpathyMainWindow *window);
115 static void main_window_edit_personal_information_cb (GtkWidget *widget,
116 EmpathyMainWindow *window);
117 static void main_window_edit_preferences_cb (GtkWidget *widget,
118 EmpathyMainWindow *window);
119 static void main_window_help_about_cb (GtkWidget *widget,
120 EmpathyMainWindow *window);
121 static void main_window_help_contents_cb (GtkWidget *widget,
122 EmpathyMainWindow *window);
123 static gboolean main_window_throbber_button_press_event_cb (GtkWidget *throbber_ebox,
124 GdkEventButton *event,
126 static void main_window_accels_load (void);
127 static void main_window_accels_save (void);
128 static void main_window_connection_items_setup (EmpathyMainWindow *window,
130 static gboolean main_window_configure_event_timeout_cb (EmpathyMainWindow *window);
131 static gboolean main_window_configure_event_cb (GtkWidget *widget,
132 GdkEventConfigure *event,
133 EmpathyMainWindow *window);
134 static void main_window_notify_show_offline_cb (GossipConf *conf,
136 gpointer check_menu_item);
137 static void main_window_notify_show_avatars_cb (GossipConf *conf,
139 EmpathyMainWindow *window);
140 static void main_window_notify_compact_contact_list_cb (GossipConf *conf,
142 EmpathyMainWindow *window);
143 static void main_window_notify_sort_criterium_cb (GossipConf *conf,
145 EmpathyMainWindow *window);
148 empathy_main_window_show (void)
150 static EmpathyMainWindow *window = NULL;
154 GtkWidget *show_offline_widget;
158 gboolean show_offline;
159 gboolean show_avatars;
160 gboolean compact_contact_list;
164 gtk_window_present (GTK_WINDOW (window->window));
165 return window->window;
168 window = g_new0 (EmpathyMainWindow, 1);
170 /* Set up interface */
171 glade = gossip_glade_get_file ("empathy-main-window.glade",
174 "main_window", &window->window,
175 "main_vbox", &window->main_vbox,
176 "chat_show_offline", &show_offline_widget,
177 "room", &window->room,
178 "room_sep", &window->room_sep,
179 "room_join_favorites", &window->room_join_favorites,
180 "edit_context", &window->edit_context,
181 "edit_context_separator", &window->edit_context_separator,
182 "presence_toolbar", &window->presence_toolbar,
183 "roster_scrolledwindow", &sw,
186 gossip_glade_connect (glade,
188 "main_window", "destroy", main_window_destroy_cb,
189 "main_window", "configure_event", main_window_configure_event_cb,
190 "chat_quit", "activate", main_window_chat_quit_cb,
191 "chat_new_message", "activate", main_window_chat_new_message_cb,
192 "chat_history", "activate", main_window_chat_history_cb,
193 "room_join_new", "activate", main_window_room_join_new_cb,
194 "room_join_favorites", "activate", main_window_room_join_favorites_cb,
195 "room_manage_favorites", "activate", main_window_room_manage_favorites_cb,
196 "chat_add_contact", "activate", main_window_chat_add_contact_cb,
197 "chat_show_offline", "toggled", main_window_chat_show_offline_cb,
198 "edit", "button-press-event", main_window_edit_button_press_event_cb,
199 "edit_accounts", "activate", main_window_edit_accounts_cb,
200 "edit_personal_information", "activate", main_window_edit_personal_information_cb,
201 "edit_preferences", "activate", main_window_edit_preferences_cb,
202 "help_about", "activate", main_window_help_about_cb,
203 "help_contents", "activate", main_window_help_contents_cb,
206 /* Set up connection related widgets. */
207 main_window_connection_items_setup (window, glade);
208 g_object_unref (glade);
210 window->tooltips = g_object_ref_sink (gtk_tooltips_new ());
213 main_window_favorite_chatroom_menu_setup ();
215 gtk_widget_hide (window->edit_context);
216 gtk_widget_hide (window->edit_context_separator);
218 /* Set up presence chooser */
219 window->presence_chooser = gossip_presence_chooser_new ();
220 gtk_widget_show (window->presence_chooser);
221 item = gtk_tool_item_new ();
222 gtk_widget_show (GTK_WIDGET (item));
223 gtk_container_add (GTK_CONTAINER (item), window->presence_chooser);
224 gtk_tool_item_set_is_important (item, TRUE);
225 gtk_tool_item_set_expand (item, TRUE);
226 gtk_toolbar_insert (GTK_TOOLBAR (window->presence_toolbar), item, -1);
228 window->widgets_connected = g_list_prepend (window->widgets_connected,
229 window->presence_chooser);
231 /* Set up the throbber */
232 ebox = gtk_event_box_new ();
233 gtk_event_box_set_visible_window (GTK_EVENT_BOX (ebox), FALSE);
235 window->throbber = ephy_spinner_new ();
236 ephy_spinner_set_size (EPHY_SPINNER (window->throbber), GTK_ICON_SIZE_LARGE_TOOLBAR);
237 gtk_container_add (GTK_CONTAINER (ebox), window->throbber);
239 item = gtk_tool_item_new ();
240 gtk_container_add (GTK_CONTAINER (item), ebox);
241 gtk_widget_show_all (GTK_WIDGET (item));
243 gtk_toolbar_insert (GTK_TOOLBAR (window->presence_toolbar), item, -1);
245 str = _("Show and edit accounts");
246 gtk_tooltips_set_tip (GTK_TOOLTIPS (window->tooltips),
249 g_signal_connect (ebox,
250 "button-press-event",
251 G_CALLBACK (main_window_throbber_button_press_event_cb),
254 /* Set up contact list. */
255 gossip_status_presets_get_all ();
256 window->contact_list = gossip_contact_list_new ();
257 gtk_widget_show (GTK_WIDGET (window->contact_list));
258 gtk_container_add (GTK_CONTAINER (sw),
259 GTK_WIDGET (window->contact_list));
261 /* Load user-defined accelerators. */
262 main_window_accels_load ();
264 /* Set window size. */
265 gossip_geometry_load (GEOMETRY_NAME, &x, &y, &w, &h);
267 if (w >= 1 && h >= 1) {
268 /* Use the defaults from the glade file if we
269 * don't have good w, h geometry.
271 gossip_debug (DEBUG_DOMAIN, "Configuring window default size w:%d, h:%d", w, h);
272 gtk_window_set_default_size (GTK_WINDOW (window->window), w, h);
275 if (x >= 0 && y >= 0) {
276 /* Let the window manager position it if we
277 * don't have good x, y coordinates.
279 gossip_debug (DEBUG_DOMAIN, "Configuring window default position x:%d, y:%d", x, y);
280 gtk_window_move (GTK_WINDOW (window->window), x, y);
283 conf = gossip_conf_get ();
286 gossip_conf_get_bool (conf,
287 GOSSIP_PREFS_CONTACTS_SHOW_OFFLINE,
289 gossip_conf_notify_add (conf,
290 GOSSIP_PREFS_CONTACTS_SHOW_OFFLINE,
291 main_window_notify_show_offline_cb,
292 show_offline_widget);
294 gtk_check_menu_item_set_active (GTK_CHECK_MENU_ITEM (show_offline_widget),
298 gossip_conf_get_bool (conf,
299 GOSSIP_PREFS_UI_SHOW_AVATARS,
301 gossip_conf_notify_add (conf,
302 GOSSIP_PREFS_UI_SHOW_AVATARS,
303 (GossipConfNotifyFunc) main_window_notify_show_avatars_cb,
305 gossip_contact_list_set_show_avatars (window->contact_list, show_avatars);
308 gossip_conf_get_bool (conf,
309 GOSSIP_PREFS_UI_COMPACT_CONTACT_LIST,
310 &compact_contact_list);
311 gossip_conf_notify_add (conf,
312 GOSSIP_PREFS_UI_COMPACT_CONTACT_LIST,
313 (GossipConfNotifyFunc) main_window_notify_compact_contact_list_cb,
315 gossip_contact_list_set_is_compact (window->contact_list, compact_contact_list);
318 gossip_conf_notify_add (conf,
319 GOSSIP_PREFS_CONTACTS_SORT_CRITERIUM,
320 (GossipConfNotifyFunc) main_window_notify_sort_criterium_cb,
322 main_window_notify_sort_criterium_cb (conf,
323 GOSSIP_PREFS_CONTACTS_SORT_CRITERIUM,
326 gtk_widget_show (window->window);
328 return window->window;
332 main_window_destroy_cb (GtkWidget *widget,
333 EmpathyMainWindow *window)
335 /* Save user-defined accelerators. */
336 main_window_accels_save ();
338 if (window->size_timeout_id) {
339 g_source_remove (window->size_timeout_id);
342 g_list_free (window->widgets_connected);
343 g_list_free (window->widgets_disconnected);
345 g_object_unref (window->tooltips);
351 main_window_favorite_chatroom_menu_setup (void)
356 main_window_chat_quit_cb (GtkWidget *widget,
357 EmpathyMainWindow *window)
363 main_window_chat_new_message_cb (GtkWidget *widget,
364 EmpathyMainWindow *window)
366 //gossip_new_message_dialog_show (GTK_WINDOW (window->window));
370 main_window_chat_history_cb (GtkWidget *widget,
371 EmpathyMainWindow *window)
373 //gossip_log_window_show (NULL, NULL);
377 main_window_room_join_new_cb (GtkWidget *widget,
378 EmpathyMainWindow *window)
380 //gossip_new_chatroom_dialog_show (GTK_WINDOW (window->window));
384 main_window_room_join_favorites_cb (GtkWidget *widget,
385 EmpathyMainWindow *window)
387 //gossip_session_chatroom_join_favorites (window->session);
391 main_window_room_manage_favorites_cb (GtkWidget *widget,
392 EmpathyMainWindow *window)
394 //gossip_chatrooms_window_show (NULL, FALSE);
398 main_window_chat_add_contact_cb (GtkWidget *widget,
399 EmpathyMainWindow *window)
401 //gossip_add_contact_dialog_show (GTK_WINDOW (window->window), NULL);
405 main_window_chat_show_offline_cb (GtkCheckMenuItem *item,
406 EmpathyMainWindow *window)
410 current = gtk_check_menu_item_get_active (item);
412 gossip_conf_set_bool (gossip_conf_get (),
413 GOSSIP_PREFS_CONTACTS_SHOW_OFFLINE,
416 /* Turn off sound just while we alter the contact list. */
417 // FIXME: gossip_sound_set_enabled (FALSE);
418 g_object_set (window->contact_list, "show_offline", current, NULL);
419 //gossip_sound_set_enabled (TRUE);
423 main_window_edit_button_press_event_cb (GtkWidget *widget,
424 GdkEventButton *event,
425 EmpathyMainWindow *window)
427 GossipContact *contact;
430 if (!event->button == 1) {
434 group = gossip_contact_list_get_selected_group (window->contact_list);
440 item = GTK_MENU_ITEM (window->edit_context);
441 label = gtk_bin_get_child (GTK_BIN (item));
442 gtk_label_set_text (GTK_LABEL (label), _("Group"));
444 gtk_widget_show (window->edit_context);
445 gtk_widget_show (window->edit_context_separator);
447 submenu = gossip_contact_list_get_group_menu (window->contact_list);
448 gtk_menu_item_set_submenu (item, submenu);
455 contact = gossip_contact_list_get_selected (window->contact_list);
461 item = GTK_MENU_ITEM (window->edit_context);
462 label = gtk_bin_get_child (GTK_BIN (item));
463 gtk_label_set_text (GTK_LABEL (label), _("Contact"));
465 gtk_widget_show (window->edit_context);
466 gtk_widget_show (window->edit_context_separator);
468 submenu = gossip_contact_list_get_contact_menu (window->contact_list,
470 gtk_menu_item_set_submenu (item, submenu);
472 g_object_unref (contact);
477 gtk_widget_hide (window->edit_context);
478 gtk_widget_hide (window->edit_context_separator);
484 main_window_edit_accounts_cb (GtkWidget *widget,
485 EmpathyMainWindow *window)
487 gossip_accounts_dialog_show ();
491 main_window_edit_personal_information_cb (GtkWidget *widget,
492 EmpathyMainWindow *window)
494 //gossip_vcard_dialog_show (GTK_WINDOW (window->window));
498 main_window_edit_preferences_cb (GtkWidget *widget,
499 EmpathyMainWindow *window)
501 gossip_preferences_show ();
505 main_window_help_about_cb (GtkWidget *widget,
506 EmpathyMainWindow *window)
508 //gossip_about_dialog_new (GTK_WINDOW (window->window));
512 main_window_help_contents_cb (GtkWidget *widget,
513 EmpathyMainWindow *window)
515 //gossip_help_show ();
519 main_window_throbber_button_press_event_cb (GtkWidget *throbber_ebox,
520 GdkEventButton *event,
523 if (event->type != GDK_BUTTON_PRESS ||
524 event->button != 1) {
528 gossip_accounts_dialog_show ();
535 main_window_session_protocol_connecting_cb (GossipSession *session,
536 GossipAccount *account,
537 GossipProtocol *protocol,
543 priv = GET_PRIV (app);
545 name = gossip_account_get_name (account);
546 gossip_debug (DEBUG_DOMAIN, "Connecting account:'%s'", name);
548 ephy_spinner_start (EPHY_SPINNER (window->throbber));
552 main_window_session_protocol_connected_cb (GossipSession *session,
553 GossipAccount *account,
554 GossipProtocol *protocol,
561 priv = GET_PRIV (app);
563 name = gossip_account_get_name (account);
564 gossip_debug (DEBUG_DOMAIN, "Connected account:'%s'", name);
566 gossip_session_count_accounts (window->session,
571 if (connecting < 1) {
572 ephy_spinner_stop (EPHY_SPINNER (window->throbber));
575 g_hash_table_remove (window->errors, account);
576 g_hash_table_remove (window->reconnects, account);
578 app_connection_items_update ();
579 app_favorite_chatroom_menu_update ();
581 /* Use saved presence */
582 gossip_app_set_presence (gossip_status_presets_get_default_state (),
583 gossip_status_presets_get_default_status());
585 app_presence_updated ();
589 main_window_session_protocol_disconnected_cb (GossipSession *session,
590 GossipAccount *account,
591 GossipProtocol *protocol,
597 gboolean should_reconnect;
600 priv = GET_PRIV (app);
602 name = gossip_account_get_name (account);
603 gossip_debug (DEBUG_DOMAIN, "Disconnected account:'%s'", name);
605 gossip_session_count_accounts (window->session,
610 if (connecting < 1) {
611 ephy_spinner_stop (EPHY_SPINNER (window->throbber));
614 app_connection_items_update ();
615 app_favorite_chatroom_menu_update ();
616 app_presence_updated ();
618 should_reconnect = reason != GOSSIP_PROTOCOL_DISCONNECT_ASKED;
621 should_reconnect &= !g_hash_table_lookup (window->reconnects, account);
623 if (should_reconnect) {
626 /* Unexpected disconnection, try to reconnect */
627 id = g_timeout_add (RETRY_CONNECT_TIMEOUT * 1000,
628 (GSourceFunc) app_reconnect_cb,
630 g_hash_table_insert (window->reconnects,
631 g_object_ref (account),
641 main_window_accels_load (void)
645 filename = g_build_filename (g_get_home_dir (), ".gnome2", PACKAGE_NAME, ACCELS_FILENAME, NULL);
646 if (g_file_test (filename, G_FILE_TEST_EXISTS)) {
647 gossip_debug (DEBUG_DOMAIN, "Loading from:'%s'", filename);
648 gtk_accel_map_load (filename);
655 main_window_accels_save (void)
658 gchar *file_with_path;
660 dir = g_build_filename (g_get_home_dir (), ".gnome2", PACKAGE_NAME, NULL);
661 g_mkdir_with_parents (dir, S_IRUSR | S_IWUSR | S_IXUSR);
662 file_with_path = g_build_filename (dir, ACCELS_FILENAME, NULL);
665 gossip_debug (DEBUG_DOMAIN, "Saving to:'%s'", file_with_path);
666 gtk_accel_map_save (file_with_path);
668 g_free (file_with_path);
672 main_window_connection_items_setup (EmpathyMainWindow *window,
678 const gchar *widgets_connected[] = {
683 "edit_personal_information"
685 const gchar *widgets_disconnected[] = {
689 for (i = 0, list = NULL; i < G_N_ELEMENTS (widgets_connected); i++) {
690 w = glade_xml_get_widget (glade, widgets_connected[i]);
691 list = g_list_prepend (list, w);
694 window->widgets_connected = list;
696 for (i = 0, list = NULL; i < G_N_ELEMENTS (widgets_disconnected); i++) {
697 w = glade_xml_get_widget (glade, widgets_disconnected[i]);
698 list = g_list_prepend (list, w);
701 window->widgets_disconnected = list;
705 main_window_configure_event_timeout_cb (EmpathyMainWindow *window)
709 gtk_window_get_size (GTK_WINDOW (window->window), &w, &h);
710 gtk_window_get_position (GTK_WINDOW (window->window), &x, &y);
712 gossip_geometry_save (GEOMETRY_NAME, x, y, w, h);
714 window->size_timeout_id = 0;
720 main_window_configure_event_cb (GtkWidget *widget,
721 GdkEventConfigure *event,
722 EmpathyMainWindow *window)
724 if (window->size_timeout_id) {
725 g_source_remove (window->size_timeout_id);
728 window->size_timeout_id = g_timeout_add (500,
729 (GSourceFunc) main_window_configure_event_timeout_cb,
736 main_window_notify_show_offline_cb (GossipConf *conf,
738 gpointer check_menu_item)
740 gboolean show_offline;
742 if (gossip_conf_get_bool (conf, key, &show_offline)) {
743 gtk_check_menu_item_set_active (GTK_CHECK_MENU_ITEM (check_menu_item),
749 main_window_notify_show_avatars_cb (GossipConf *conf,
751 EmpathyMainWindow *window)
753 gboolean show_avatars;
755 if (gossip_conf_get_bool (conf, key, &show_avatars)) {
756 gossip_contact_list_set_show_avatars (window->contact_list,
762 main_window_notify_compact_contact_list_cb (GossipConf *conf,
764 EmpathyMainWindow *window)
766 gboolean compact_contact_list;
768 if (gossip_conf_get_bool (conf, key, &compact_contact_list)) {
769 gossip_contact_list_set_is_compact (window->contact_list,
770 compact_contact_list);
775 main_window_notify_sort_criterium_cb (GossipConf *conf,
777 EmpathyMainWindow *window)
781 if (gossip_conf_get_string (conf, key, &str)) {
783 GEnumClass *enum_class;
784 GEnumValue *enum_value;
786 type = gossip_contact_list_sort_get_type ();
787 enum_class = G_ENUM_CLASS (g_type_class_peek (type));
788 enum_value = g_enum_get_value_by_nick (enum_class, str);
791 gossip_contact_list_set_sort_criterium (window->contact_list,