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