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