]> git.0d.be Git - empathy.git/blob - libempathy-gtk/empathy-main-window.c
No need to RequestHandle, MissionControl has API to request the channel
[empathy.git] / libempathy-gtk / empathy-main-window.c
1 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
2 /*
3  * Copyright (C) 2007 Collabora Ltd.
4  *
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.
9  *
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.
14  *
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.
19  * 
20  * Authors: Xavier Claessens <xclaesse@gmail.com>
21  */
22
23 #include <config.h>
24
25 #include <sys/stat.h>
26 #include <gtk/gtk.h>
27 #include <glade/glade.h>
28 #include <glib/gi18n.h>
29
30 #include <libempathy/gossip-conf.h>
31 #include <libempathy/gossip-contact.h>
32 #include <libempathy/gossip-debug.h>
33 #include <libempathy/gossip-utils.h>
34 #include <libempathy/gossip-chatroom-manager.h>
35 #include <libempathy/gossip-chatroom.h>
36 #include <libempathy/empathy-contact-list.h>
37 #include <libempathy/empathy-contact-manager.h>
38
39 #include "empathy-main-window.h"
40 #include "ephy-spinner.h"
41 #include "gossip-contact-list-store.h"
42 #include "gossip-contact-list-view.h"
43 #include "gossip-presence-chooser.h"
44 #include "gossip-ui-utils.h"
45 #include "gossip-status-presets.h"
46 #include "gossip-geometry.h"
47 #include "gossip-preferences.h"
48 #include "gossip-accounts-dialog.h"
49 #include "gossip-about-dialog.h"
50 #include "gossip-new-chatroom-dialog.h"
51 #include "gossip-chatrooms-window.h"
52
53 #define DEBUG_DOMAIN "MainWindow"
54
55 /* Minimum width of roster window if something goes wrong. */
56 #define MIN_WIDTH 50
57
58 /* Accels (menu shortcuts) can be configured and saved */
59 #define ACCELS_FILENAME "accels.txt"
60
61 /* Name in the geometry file */
62 #define GEOMETRY_NAME "main-window"
63
64 typedef struct {
65         GossipContactListView  *list_view;
66         GossipContactListStore *list_store;
67         MissionControl         *mc;
68         GossipChatroomManager  *chatroom_manager;
69
70         /* Main widgets */
71         GtkWidget              *window;
72         GtkWidget              *main_vbox;
73
74         /* Tooltips for all widgets */
75         GtkTooltips            *tooltips;
76
77         /* Menu widgets */
78         GtkWidget              *room;
79         GtkWidget              *room_menu;
80         GtkWidget              *room_sep;
81         GtkWidget              *room_join_favorites;
82         GtkWidget              *edit_context;
83         GtkWidget              *edit_context_separator;
84
85         /* Throbber */
86         GtkWidget              *throbber;
87
88         /* Widgets that are enabled when there is... */
89         GList                  *widgets_connected;      /* ... connected accounts */
90         GList                  *widgets_disconnected;   /* ... disconnected accounts */
91
92         /* Status popup */
93         GtkWidget              *presence_toolbar;
94         GtkWidget              *presence_chooser;
95
96         /* Misc */
97         guint                   size_timeout_id;
98 } EmpathyMainWindow;
99
100 static void     main_window_destroy_cb                         (GtkWidget                       *widget,
101                                                                 EmpathyMainWindow               *window);
102 static void     main_window_favorite_chatroom_menu_setup       (EmpathyMainWindow               *window);
103 static void     main_window_favorite_chatroom_menu_added_cb    (GossipChatroomManager           *manager,
104                                                                 GossipChatroom                  *chatroom,
105                                                                 EmpathyMainWindow               *window);
106 static void     main_window_favorite_chatroom_menu_removed_cb  (GossipChatroomManager           *manager,
107                                                                 GossipChatroom                  *chatroom,
108                                                                 EmpathyMainWindow               *window);
109 static void     main_window_favorite_chatroom_menu_activate_cb (GtkMenuItem                     *menu_item,
110                                                                 GossipChatroom                  *chatroom);
111 static void     main_window_favorite_chatroom_menu_update      (EmpathyMainWindow               *window);
112 static void     main_window_favorite_chatroom_menu_add         (EmpathyMainWindow               *window,
113                                                                 GossipChatroom                  *chatroom);
114 static void     main_window_favorite_chatroom_join             (GossipChatroom                  *chatroom);
115 static void     main_window_chat_quit_cb                       (GtkWidget                       *widget,
116                                                                 EmpathyMainWindow               *window);
117 static void     main_window_chat_new_message_cb                (GtkWidget                       *widget,
118                                                                 EmpathyMainWindow               *window);
119 static void     main_window_chat_history_cb                    (GtkWidget                       *widget,
120                                                                 EmpathyMainWindow               *window);
121 static void     main_window_room_join_new_cb                   (GtkWidget                       *widget,
122                                                                 EmpathyMainWindow               *window);
123 static void     main_window_room_join_favorites_cb             (GtkWidget                       *widget,
124                                                                 EmpathyMainWindow               *window);
125 static void     main_window_room_manage_favorites_cb           (GtkWidget                       *widget,
126                                                                 EmpathyMainWindow               *window);
127 static void     main_window_chat_add_contact_cb                (GtkWidget                       *widget,
128                                                                 EmpathyMainWindow               *window);
129 static void     main_window_chat_show_offline_cb               (GtkCheckMenuItem                *item,
130                                                                 EmpathyMainWindow               *window);
131 static gboolean main_window_edit_button_press_event_cb         (GtkWidget                       *widget,
132                                                                 GdkEventButton                  *event,
133                                                                 EmpathyMainWindow               *window);
134 static void     main_window_edit_accounts_cb                   (GtkWidget                       *widget,
135                                                                 EmpathyMainWindow               *window);
136 static void     main_window_edit_personal_information_cb       (GtkWidget                       *widget,
137                                                                 EmpathyMainWindow               *window);
138 static void     main_window_edit_preferences_cb                (GtkWidget                       *widget,
139                                                                 EmpathyMainWindow               *window);
140 static void     main_window_help_about_cb                      (GtkWidget                       *widget,
141                                                                 EmpathyMainWindow               *window);
142 static void     main_window_help_contents_cb                   (GtkWidget                       *widget,
143                                                                 EmpathyMainWindow               *window);
144 static gboolean main_window_throbber_button_press_event_cb     (GtkWidget                       *throbber_ebox,
145                                                                 GdkEventButton                  *event,
146                                                                 EmpathyMainWindow               *window);
147 static void     main_window_status_changed_cb                  (MissionControl                  *mc,
148                                                                 TelepathyConnectionStatus        status,
149                                                                 McPresence                       presence,
150                                                                 TelepathyConnectionStatusReason  reason,
151                                                                 const gchar                     *unique_name,
152                                                                 EmpathyMainWindow               *window);
153 static void     main_window_update_status                      (EmpathyMainWindow               *window);
154 static void     main_window_accels_load                        (void);
155 static void     main_window_accels_save                        (void);
156 static void     main_window_connection_items_setup             (EmpathyMainWindow               *window,
157                                                                 GladeXML                        *glade);
158 static gboolean main_window_configure_event_timeout_cb         (EmpathyMainWindow               *window);
159 static gboolean main_window_configure_event_cb                 (GtkWidget                       *widget,
160                                                                 GdkEventConfigure               *event,
161                                                                 EmpathyMainWindow               *window);
162 static void     main_window_notify_show_offline_cb             (GossipConf                      *conf,
163                                                                 const gchar                     *key,
164                                                                 gpointer                         check_menu_item);
165 static void     main_window_notify_show_avatars_cb             (GossipConf                      *conf,
166                                                                 const gchar                     *key,
167                                                                 EmpathyMainWindow               *window);
168 static void     main_window_notify_compact_contact_list_cb     (GossipConf                      *conf,
169                                                                 const gchar                     *key,
170                                                                 EmpathyMainWindow               *window);
171 static void     main_window_notify_sort_criterium_cb           (GossipConf                      *conf,
172                                                                 const gchar                     *key,
173                                                                 EmpathyMainWindow               *window);
174
175 GtkWidget *
176 empathy_main_window_show (void)
177 {
178         static EmpathyMainWindow *window = NULL;
179         EmpathyContactList       *list_iface;
180         GladeXML                 *glade;
181         GossipConf               *conf;
182         GtkWidget                *sw;
183         GtkWidget                *show_offline_widget;
184         GtkWidget                *ebox;
185         GtkToolItem              *item;
186         gchar                    *str;
187         gboolean                  show_offline;
188         gboolean                  show_avatars;
189         gboolean                  compact_contact_list;
190         gint                      x, y, w, h;
191
192         if (window) {
193                 gtk_window_present (GTK_WINDOW (window->window));
194                 return window->window;
195         }
196
197         window = g_new0 (EmpathyMainWindow, 1);
198
199         /* Set up interface */
200         glade = gossip_glade_get_file ("empathy-main-window.glade",
201                                        "main_window",
202                                        NULL,
203                                        "main_window", &window->window,
204                                        "main_vbox", &window->main_vbox,
205                                        "chat_show_offline", &show_offline_widget,
206                                        "room", &window->room,
207                                        "room_sep", &window->room_sep,
208                                        "room_join_favorites", &window->room_join_favorites,
209                                        "edit_context", &window->edit_context,
210                                        "edit_context_separator", &window->edit_context_separator,
211                                        "presence_toolbar", &window->presence_toolbar,
212                                        "roster_scrolledwindow", &sw,
213                                        NULL);
214
215         gossip_glade_connect (glade,
216                               window,
217                               "main_window", "destroy", main_window_destroy_cb,
218                               "main_window", "configure_event", main_window_configure_event_cb,
219                               "chat_quit", "activate", main_window_chat_quit_cb,
220                               "chat_new_message", "activate", main_window_chat_new_message_cb,
221                               "chat_history", "activate", main_window_chat_history_cb,
222                               "room_join_new", "activate", main_window_room_join_new_cb,
223                               "room_join_favorites", "activate", main_window_room_join_favorites_cb,
224                               "room_manage_favorites", "activate", main_window_room_manage_favorites_cb,
225                               "chat_add_contact", "activate", main_window_chat_add_contact_cb,
226                               "chat_show_offline", "toggled", main_window_chat_show_offline_cb,
227                               "edit", "button-press-event", main_window_edit_button_press_event_cb,
228                               "edit_accounts", "activate", main_window_edit_accounts_cb,
229                               "edit_personal_information", "activate", main_window_edit_personal_information_cb,
230                               "edit_preferences", "activate", main_window_edit_preferences_cb,
231                               "help_about", "activate", main_window_help_about_cb,
232                               "help_contents", "activate", main_window_help_contents_cb,
233                               NULL);
234
235         /* Set up connection related widgets. */
236         main_window_connection_items_setup (window, glade);
237         g_object_unref (glade);
238
239         window->tooltips = g_object_ref_sink (gtk_tooltips_new ());
240         window->mc = gossip_mission_control_new ();
241         dbus_g_proxy_connect_signal (DBUS_G_PROXY (window->mc), "AccountStatusChanged",
242                                      G_CALLBACK (main_window_status_changed_cb),
243                                      window, NULL);
244
245         /* Set up menu */
246         main_window_favorite_chatroom_menu_setup (window);
247
248         gtk_widget_hide (window->edit_context);
249         gtk_widget_hide (window->edit_context_separator);
250
251         /* Set up presence chooser */
252         window->presence_chooser = gossip_presence_chooser_new ();
253         gtk_widget_show (window->presence_chooser);
254         item = gtk_tool_item_new ();
255         gtk_widget_show (GTK_WIDGET (item));
256         gtk_container_add (GTK_CONTAINER (item), window->presence_chooser);
257         gtk_tool_item_set_is_important (item, TRUE);
258         gtk_tool_item_set_expand (item, TRUE);
259         gtk_toolbar_insert (GTK_TOOLBAR (window->presence_toolbar), item, -1);
260
261         /* Set up the throbber */
262         ebox = gtk_event_box_new ();
263         gtk_event_box_set_visible_window (GTK_EVENT_BOX (ebox), FALSE);
264
265         window->throbber = ephy_spinner_new ();
266         ephy_spinner_set_size (EPHY_SPINNER (window->throbber), GTK_ICON_SIZE_LARGE_TOOLBAR);
267         gtk_container_add (GTK_CONTAINER (ebox), window->throbber);
268
269         item = gtk_tool_item_new ();
270         gtk_container_add (GTK_CONTAINER (item), ebox);
271         gtk_widget_show_all (GTK_WIDGET (item));
272
273         gtk_toolbar_insert (GTK_TOOLBAR (window->presence_toolbar), item, -1);
274
275         str = _("Show and edit accounts");
276         gtk_tooltips_set_tip (GTK_TOOLTIPS (window->tooltips),
277                               ebox, str, str);
278
279         g_signal_connect (ebox,
280                           "button-press-event",
281                           G_CALLBACK (main_window_throbber_button_press_event_cb),
282                           window);
283
284         /* Set up contact list. */
285         gossip_status_presets_get_all ();
286
287         list_iface = EMPATHY_CONTACT_LIST (empathy_contact_manager_new ());
288         empathy_contact_list_setup (list_iface);
289         window->list_store = gossip_contact_list_store_new (list_iface);
290         window->list_view = gossip_contact_list_view_new (window->list_store);
291         g_object_unref (list_iface);
292
293         gtk_widget_show (GTK_WIDGET (window->list_view));
294         gtk_container_add (GTK_CONTAINER (sw),
295                            GTK_WIDGET (window->list_view));
296
297         /* Load user-defined accelerators. */
298         main_window_accels_load ();
299
300         /* Set window size. */
301         gossip_geometry_load (GEOMETRY_NAME, &x, &y, &w, &h);
302
303         if (w >= 1 && h >= 1) {
304                 /* Use the defaults from the glade file if we
305                  * don't have good w, h geometry.
306                  */
307                 gossip_debug (DEBUG_DOMAIN, "Configuring window default size w:%d, h:%d", w, h);
308                 gtk_window_set_default_size (GTK_WINDOW (window->window), w, h);
309         }
310
311         if (x >= 0 && y >= 0) {
312                 /* Let the window manager position it if we
313                  * don't have good x, y coordinates.
314                  */
315                 gossip_debug (DEBUG_DOMAIN, "Configuring window default position x:%d, y:%d", x, y);
316                 gtk_window_move (GTK_WINDOW (window->window), x, y);
317         }
318
319         conf = gossip_conf_get ();
320         
321         /* Show offline ? */
322         gossip_conf_get_bool (conf,
323                               GOSSIP_PREFS_CONTACTS_SHOW_OFFLINE,
324                               &show_offline);
325         gossip_conf_notify_add (conf,
326                                 GOSSIP_PREFS_CONTACTS_SHOW_OFFLINE,
327                                 main_window_notify_show_offline_cb,
328                                 show_offline_widget);
329
330         gtk_check_menu_item_set_active (GTK_CHECK_MENU_ITEM (show_offline_widget),
331                                         show_offline);
332
333         /* Show avatars ? */
334         gossip_conf_get_bool (conf,
335                               GOSSIP_PREFS_UI_SHOW_AVATARS,
336                               &show_avatars);
337         gossip_conf_notify_add (conf,
338                                 GOSSIP_PREFS_UI_SHOW_AVATARS,
339                                 (GossipConfNotifyFunc) main_window_notify_show_avatars_cb,
340                                 window);
341         gossip_contact_list_store_set_show_avatars (window->list_store, show_avatars);
342
343         /* Is compact ? */
344         gossip_conf_get_bool (conf,
345                               GOSSIP_PREFS_UI_COMPACT_CONTACT_LIST,
346                               &compact_contact_list);
347         gossip_conf_notify_add (conf,
348                                 GOSSIP_PREFS_UI_COMPACT_CONTACT_LIST,
349                                 (GossipConfNotifyFunc) main_window_notify_compact_contact_list_cb,
350                                 window);
351         gossip_contact_list_store_set_is_compact (window->list_store, compact_contact_list);
352
353         /* Sort criterium */
354         gossip_conf_notify_add (conf,
355                                 GOSSIP_PREFS_CONTACTS_SORT_CRITERIUM,
356                                 (GossipConfNotifyFunc) main_window_notify_sort_criterium_cb,
357                                 window);
358         main_window_notify_sort_criterium_cb (conf,
359                                               GOSSIP_PREFS_CONTACTS_SORT_CRITERIUM,
360                                               window);
361
362         main_window_update_status (window);
363
364         return window->window;
365 }
366
367 static void
368 main_window_destroy_cb (GtkWidget         *widget,
369                         EmpathyMainWindow *window)
370 {
371         /* Save user-defined accelerators. */
372         main_window_accels_save ();
373
374         dbus_g_proxy_disconnect_signal (DBUS_G_PROXY (window->mc), "AccountStatusChanged",
375                                         G_CALLBACK (main_window_status_changed_cb),
376                                         window);
377
378         if (window->size_timeout_id) {
379                 g_source_remove (window->size_timeout_id);
380         }
381
382         g_list_free (window->widgets_connected);
383         g_list_free (window->widgets_disconnected);
384
385         g_object_unref (window->tooltips);
386         g_object_unref (window->mc);
387         g_object_unref (window->list_store);
388
389         g_free (window);
390 }
391
392 static void
393 main_window_favorite_chatroom_menu_setup (EmpathyMainWindow *window)
394 {
395         GList *chatrooms, *l;
396
397         window->chatroom_manager = gossip_chatroom_manager_new ();
398         chatrooms = gossip_chatroom_manager_get_chatrooms (window->chatroom_manager, NULL);
399         window->room_menu = gtk_menu_item_get_submenu (GTK_MENU_ITEM (window->room));
400
401         for (l = chatrooms; l; l = l->next) {
402                 main_window_favorite_chatroom_menu_add (window, l->data);
403         }
404
405         if (!chatrooms) {
406                 gtk_widget_hide (window->room_sep);
407         }
408
409         gtk_widget_set_sensitive (window->room_join_favorites, chatrooms != NULL);
410
411         g_signal_connect (window->chatroom_manager, "chatroom-added",
412                           G_CALLBACK (main_window_favorite_chatroom_menu_added_cb),
413                           window);
414         g_signal_connect (window->chatroom_manager, "chatroom-removed",
415                           G_CALLBACK (main_window_favorite_chatroom_menu_removed_cb),
416                           window);
417
418         g_list_free (chatrooms);
419 }
420
421 static void
422 main_window_favorite_chatroom_menu_added_cb (GossipChatroomManager *manager,
423                                              GossipChatroom        *chatroom,
424                                              EmpathyMainWindow     *window)
425 {
426         main_window_favorite_chatroom_menu_add (window, chatroom);
427         gtk_widget_show (window->room_sep);
428         gtk_widget_set_sensitive (window->room_join_favorites, TRUE);
429 }
430
431 static void
432 main_window_favorite_chatroom_menu_removed_cb (GossipChatroomManager *manager,
433                                                GossipChatroom        *chatroom,
434                                                EmpathyMainWindow     *window)
435 {
436         GtkWidget *menu_item;
437
438         menu_item = g_object_get_data (G_OBJECT (chatroom), "menu_item");
439
440         g_object_set_data (G_OBJECT (chatroom), "menu_item", NULL);
441         gtk_widget_destroy (menu_item);
442
443         main_window_favorite_chatroom_menu_update (window);
444 }
445
446 static void
447 main_window_favorite_chatroom_menu_activate_cb (GtkMenuItem    *menu_item,
448                                                 GossipChatroom *chatroom)
449 {
450         main_window_favorite_chatroom_join (chatroom);
451 }
452
453 static void
454 main_window_favorite_chatroom_menu_update (EmpathyMainWindow *window)
455 {
456         GList *chatrooms;
457
458         chatrooms = gossip_chatroom_manager_get_chatrooms (window->chatroom_manager, NULL);
459
460         if (chatrooms) {
461                 gtk_widget_show (window->room_sep);
462         } else {
463                 gtk_widget_hide (window->room_sep);
464         }
465
466         gtk_widget_set_sensitive (window->room_join_favorites, chatrooms != NULL);
467         g_list_free (chatrooms);
468 }
469
470 static void
471 main_window_favorite_chatroom_menu_add (EmpathyMainWindow *window,
472                                         GossipChatroom    *chatroom)
473 {
474         GtkWidget   *menu_item;
475         const gchar *name;
476
477         if (g_object_get_data (G_OBJECT (chatroom), "menu_item")) {
478                 return;
479         }
480
481         name = gossip_chatroom_get_name (chatroom);
482         menu_item = gtk_menu_item_new_with_label (name);
483
484         g_object_set_data (G_OBJECT (chatroom), "menu_item", menu_item);
485         g_signal_connect (menu_item, "activate",
486                           G_CALLBACK (main_window_favorite_chatroom_menu_activate_cb),
487                           chatroom);
488
489         gtk_menu_shell_insert (GTK_MENU_SHELL (window->room_menu),
490                                menu_item, 3);
491
492         gtk_widget_show (menu_item);
493 }
494
495 static void
496 main_window_favorite_chatroom_join (GossipChatroom *chatroom)
497 {
498         MissionControl *mc;
499         McAccount      *account;
500         const gchar    *room;
501
502         mc = gossip_mission_control_new ();
503         account = gossip_chatroom_get_account (chatroom);
504         room = gossip_chatroom_get_room (chatroom);
505
506         gossip_debug (DEBUG_DOMAIN, "Requesting channel for '%s'", room);
507
508         mission_control_request_channel_with_string_handle (mc,
509                                                             account,
510                                                             TP_IFACE_CHANNEL_TYPE_TEXT,
511                                                             room,
512                                                             TP_HANDLE_TYPE_ROOM,
513                                                             NULL, NULL);        
514         g_object_unref (mc);
515 }
516
517 static void
518 main_window_chat_quit_cb (GtkWidget         *widget,
519                           EmpathyMainWindow *window)
520 {
521         gtk_main_quit ();
522 }
523
524 static void
525 main_window_chat_new_message_cb (GtkWidget         *widget,
526                                  EmpathyMainWindow *window)
527 {
528         //gossip_new_message_dialog_show (GTK_WINDOW (window->window));
529 }
530
531 static void
532 main_window_chat_history_cb (GtkWidget         *widget,
533                              EmpathyMainWindow *window)
534 {
535         //gossip_log_window_show (NULL, NULL);
536 }
537
538 static void
539 main_window_room_join_new_cb (GtkWidget         *widget,
540                               EmpathyMainWindow *window)
541 {
542         gossip_new_chatroom_dialog_show (GTK_WINDOW (window->window));
543 }
544
545 static void
546 main_window_room_join_favorites_cb (GtkWidget         *widget,
547                                     EmpathyMainWindow *window)
548 {
549         GList *chatrooms, *l;
550
551         chatrooms = gossip_chatroom_manager_get_chatrooms (window->chatroom_manager, NULL);
552         for (l = chatrooms; l; l = l->next) {
553                 main_window_favorite_chatroom_join (l->data);
554         }
555         g_list_free (chatrooms);
556 }
557
558 static void
559 main_window_room_manage_favorites_cb (GtkWidget         *widget,
560                                       EmpathyMainWindow *window)
561 {
562         gossip_chatrooms_window_show (GTK_WINDOW (window->window));
563 }
564
565 static void
566 main_window_chat_add_contact_cb (GtkWidget         *widget,
567                                  EmpathyMainWindow *window)
568 {
569         //gossip_add_contact_dialog_show (GTK_WINDOW (window->window), NULL);
570 }
571
572 static void
573 main_window_chat_show_offline_cb (GtkCheckMenuItem  *item,
574                                   EmpathyMainWindow *window)
575 {
576         gboolean current;
577
578         current = gtk_check_menu_item_get_active (item);
579
580         gossip_conf_set_bool (gossip_conf_get (),
581                               GOSSIP_PREFS_CONTACTS_SHOW_OFFLINE,
582                               current);
583
584         /* Turn off sound just while we alter the contact list. */
585         // FIXME: gossip_sound_set_enabled (FALSE);
586         gossip_contact_list_store_set_show_offline (window->list_store, current);
587         //gossip_sound_set_enabled (TRUE);
588 }
589
590 static gboolean
591 main_window_edit_button_press_event_cb (GtkWidget         *widget,
592                                         GdkEventButton    *event,
593                                         EmpathyMainWindow *window)
594 {
595         GossipContact *contact;
596         gchar         *group;
597
598         if (!event->button == 1) {
599                 return FALSE;
600         }
601
602         group = gossip_contact_list_view_get_selected_group (window->list_view);
603         if (group) {
604                 GtkMenuItem *item;
605                 GtkWidget   *label;
606                 GtkWidget   *submenu;
607
608                 item = GTK_MENU_ITEM (window->edit_context);
609                 label = gtk_bin_get_child (GTK_BIN (item));
610                 gtk_label_set_text (GTK_LABEL (label), _("Group"));
611
612                 gtk_widget_show (window->edit_context);
613                 gtk_widget_show (window->edit_context_separator);
614
615                 submenu = gossip_contact_list_view_get_group_menu (window->list_view);
616                 gtk_menu_item_set_submenu (item, submenu);
617
618                 g_free (group);
619
620                 return FALSE;
621         }
622
623         contact = gossip_contact_list_view_get_selected (window->list_view);
624         if (contact) {
625                 GtkMenuItem *item;
626                 GtkWidget   *label;
627                 GtkWidget   *submenu;
628
629                 item = GTK_MENU_ITEM (window->edit_context);
630                 label = gtk_bin_get_child (GTK_BIN (item));
631                 gtk_label_set_text (GTK_LABEL (label), _("Contact"));
632
633                 gtk_widget_show (window->edit_context);
634                 gtk_widget_show (window->edit_context_separator);
635
636                 submenu = gossip_contact_list_view_get_contact_menu (window->list_view,
637                                                                      contact);
638                 gtk_menu_item_set_submenu (item, submenu);
639
640                 g_object_unref (contact);
641
642                 return FALSE;
643         }
644
645         gtk_widget_hide (window->edit_context);
646         gtk_widget_hide (window->edit_context_separator);
647
648         return FALSE;
649 }
650
651 static void
652 main_window_edit_accounts_cb (GtkWidget         *widget,
653                               EmpathyMainWindow *window)
654 {
655         gossip_accounts_dialog_show (GTK_WINDOW (window->window));
656 }
657
658 static void
659 main_window_edit_personal_information_cb (GtkWidget         *widget,
660                                           EmpathyMainWindow *window)
661 {
662         //gossip_vcard_dialog_show (GTK_WINDOW (window->window));
663 }
664
665 static void
666 main_window_edit_preferences_cb (GtkWidget         *widget,
667                                  EmpathyMainWindow *window)
668 {
669         gossip_preferences_show (GTK_WINDOW (window->window));
670 }
671
672 static void
673 main_window_help_about_cb (GtkWidget         *widget,
674                            EmpathyMainWindow *window)
675 {
676         gossip_about_dialog_new (GTK_WINDOW (window->window));
677 }
678
679 static void
680 main_window_help_contents_cb (GtkWidget         *widget,
681                               EmpathyMainWindow *window)
682 {
683         //gossip_help_show ();
684 }
685
686 static gboolean
687 main_window_throbber_button_press_event_cb (GtkWidget         *throbber_ebox,
688                                             GdkEventButton    *event,
689                                             EmpathyMainWindow *window)
690 {
691         if (event->type != GDK_BUTTON_PRESS ||
692             event->button != 1) {
693                 return FALSE;
694         }
695
696         gossip_accounts_dialog_show (GTK_WINDOW (window->window));
697
698         return FALSE;
699 }
700
701 static void
702 main_window_status_changed_cb (MissionControl                  *mc,
703                                TelepathyConnectionStatus        status,
704                                McPresence                       presence,
705                                TelepathyConnectionStatusReason  reason,
706                                const gchar                     *unique_name,
707                                EmpathyMainWindow               *window)
708 {
709         main_window_update_status (window);
710 }
711
712 static void
713 main_window_update_status (EmpathyMainWindow *window)
714 {
715         GList *accounts, *l;
716         guint  connected = 0;
717         guint  connecting = 0;
718         guint  disconnected = 0;
719
720         /* Count number of connected/connecting/disconnected accounts */
721         accounts = mc_accounts_list (); 
722         for (l = accounts; l; l = l->next) {
723                 McAccount *account;
724                 guint      status;
725
726                 account = l->data;
727
728                 status = mission_control_get_connection_status (window->mc,
729                                                                 account,
730                                                                 NULL);
731
732                 if (status == 0) {
733                         connected++;
734                 } else if (status == 1) {
735                         connecting++;
736                 } else if (status == 2) {
737                         disconnected++;
738                 }
739
740                 g_object_unref (account);
741         }
742         g_list_free (accounts);
743
744         /* Update the spinner state */
745         if (connecting > 0) {
746                 ephy_spinner_start (EPHY_SPINNER (window->throbber));
747         } else {
748                 ephy_spinner_stop (EPHY_SPINNER (window->throbber));
749         }
750
751         /* Update widgets sensibility */
752         for (l = window->widgets_connected; l; l = l->next) {
753                 gtk_widget_set_sensitive (l->data, (connected > 0));
754         }
755
756         for (l = window->widgets_disconnected; l; l = l->next) {
757                 gtk_widget_set_sensitive (l->data, (disconnected > 0));
758         }
759 }
760
761 /*
762  * Accels
763  */
764 static void
765 main_window_accels_load (void)
766 {
767         gchar *filename;
768
769         filename = g_build_filename (g_get_home_dir (), ".gnome2", PACKAGE_NAME, ACCELS_FILENAME, NULL);
770         if (g_file_test (filename, G_FILE_TEST_EXISTS)) {
771                 gossip_debug (DEBUG_DOMAIN, "Loading from:'%s'", filename);
772                 gtk_accel_map_load (filename);
773         }
774
775         g_free (filename);
776 }
777
778 static void
779 main_window_accels_save (void)
780 {
781         gchar *dir;
782         gchar *file_with_path;
783
784         dir = g_build_filename (g_get_home_dir (), ".gnome2", PACKAGE_NAME, NULL);
785         g_mkdir_with_parents (dir, S_IRUSR | S_IWUSR | S_IXUSR);
786         file_with_path = g_build_filename (dir, ACCELS_FILENAME, NULL);
787         g_free (dir);
788
789         gossip_debug (DEBUG_DOMAIN, "Saving to:'%s'", file_with_path);
790         gtk_accel_map_save (file_with_path);
791
792         g_free (file_with_path);
793 }
794
795 static void
796 main_window_connection_items_setup (EmpathyMainWindow *window,
797                                     GladeXML          *glade)
798 {
799         GList         *list;
800         GtkWidget     *w;
801         gint           i;
802         const gchar *widgets_connected[] = {
803                 "room",
804                 "chat_new_message",
805                 "chat_add_contact",
806                 "edit_personal_information"
807         };
808         const gchar *widgets_disconnected[] = {
809         };
810
811         for (i = 0, list = NULL; i < G_N_ELEMENTS (widgets_connected); i++) {
812                 w = glade_xml_get_widget (glade, widgets_connected[i]);
813                 list = g_list_prepend (list, w);
814         }
815
816         window->widgets_connected = list;
817
818         for (i = 0, list = NULL; i < G_N_ELEMENTS (widgets_disconnected); i++) {
819                 w = glade_xml_get_widget (glade, widgets_disconnected[i]);
820                 list = g_list_prepend (list, w);
821         }
822
823         window->widgets_disconnected = list;
824 }
825
826 static gboolean
827 main_window_configure_event_timeout_cb (EmpathyMainWindow *window)
828 {
829         gint x, y, w, h;
830
831         gtk_window_get_size (GTK_WINDOW (window->window), &w, &h);
832         gtk_window_get_position (GTK_WINDOW (window->window), &x, &y);
833
834         gossip_geometry_save (GEOMETRY_NAME, x, y, w, h);
835
836         window->size_timeout_id = 0;
837
838         return FALSE;
839 }
840
841 static gboolean
842 main_window_configure_event_cb (GtkWidget         *widget,
843                                 GdkEventConfigure *event,
844                                 EmpathyMainWindow *window)
845 {
846         if (window->size_timeout_id) {
847                 g_source_remove (window->size_timeout_id);
848         }
849
850         window->size_timeout_id = g_timeout_add (500,
851                                                (GSourceFunc) main_window_configure_event_timeout_cb,
852                                                window);
853
854         return FALSE;
855 }
856
857 static void
858 main_window_notify_show_offline_cb (GossipConf  *conf,
859                                     const gchar *key,
860                                     gpointer     check_menu_item)
861 {
862         gboolean show_offline;
863
864         if (gossip_conf_get_bool (conf, key, &show_offline)) {
865                 gtk_check_menu_item_set_active (GTK_CHECK_MENU_ITEM (check_menu_item),
866                                                 show_offline);
867         }
868 }
869
870 static void
871 main_window_notify_show_avatars_cb (GossipConf        *conf,
872                                     const gchar       *key,
873                                     EmpathyMainWindow *window)
874 {
875         gboolean show_avatars;
876
877         if (gossip_conf_get_bool (conf, key, &show_avatars)) {
878                 gossip_contact_list_store_set_show_avatars (window->list_store,
879                                                             show_avatars);
880         }
881 }
882
883 static void
884 main_window_notify_compact_contact_list_cb (GossipConf        *conf,
885                                             const gchar       *key,
886                                             EmpathyMainWindow *window)
887 {
888         gboolean compact_contact_list;
889
890         if (gossip_conf_get_bool (conf, key, &compact_contact_list)) {
891                 gossip_contact_list_store_set_is_compact (window->list_store,
892                                                           compact_contact_list);
893         }
894 }
895
896 static void
897 main_window_notify_sort_criterium_cb (GossipConf        *conf,
898                                       const gchar       *key,
899                                       EmpathyMainWindow *window)
900 {
901         gchar *str = NULL;
902
903         if (gossip_conf_get_string (conf, key, &str)) {
904                 GType       type;
905                 GEnumClass *enum_class;
906                 GEnumValue *enum_value;
907
908                 type = gossip_contact_list_store_sort_get_type ();
909                 enum_class = G_ENUM_CLASS (g_type_class_peek (type));
910                 enum_value = g_enum_get_value_by_nick (enum_class, str);
911                 g_free (str);
912
913                 if (enum_value) {
914                         gossip_contact_list_store_set_sort_criterium (window->list_store, 
915                                                                       enum_value->value);
916                 }
917         }
918 }
919