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