]> git.0d.be Git - empathy.git/blob - src/empathy-main-window.c
Don't emit sound when changing state and properly play login sound.
[empathy.git] / src / empathy-main-window.c
1 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
2 /*
3  * Copyright (C) 2002-2007 Imendio AB
4  * Copyright (C) 2007-2008 Collabora Ltd.
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License as
8  * published by the Free Software Foundation; either version 2 of the
9  * License, or (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public
17  * License along with this program; if not, write to the
18  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
19  * Boston, MA 02111-1307, USA.
20  * 
21  * Authors: Xavier Claessens <xclaesse@gmail.com>
22  */
23
24 #include <config.h>
25
26 #include <sys/stat.h>
27 #include <gtk/gtk.h>
28 #include <glade/glade.h>
29 #include <glib/gi18n.h>
30 #include <canberra-gtk.h>
31
32 #include <libempathy/empathy-contact.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 #include <libempathy/empathy-idle.h>
40 #include <libempathy/empathy-status-presets.h>
41
42 #include <libempathy-gtk/empathy-contact-dialogs.h>
43 #include <libempathy-gtk/empathy-contact-list-store.h>
44 #include <libempathy-gtk/empathy-contact-list-view.h>
45 #include <libempathy-gtk/empathy-presence-chooser.h>
46 #include <libempathy-gtk/empathy-ui-utils.h>
47 #include <libempathy-gtk/empathy-geometry.h>
48 #include <libempathy-gtk/empathy-conf.h>
49 #include <libempathy-gtk/empathy-log-window.h>
50 #include <libempathy-gtk/empathy-new-message-dialog.h>
51 #include <libempathy-gtk/empathy-gtk-enum-types.h>
52
53 #include "empathy-accounts-dialog.h"
54 #include "empathy-main-window.h"
55 #include "ephy-spinner.h"
56 #include "empathy-preferences.h"
57 #include "empathy-about-dialog.h"
58 #include "empathy-new-chatroom-dialog.h"
59 #include "empathy-chatrooms-window.h"
60 #include "empathy-event-manager.h"
61
62 #define DEBUG_FLAG EMPATHY_DEBUG_OTHER
63 #include <libempathy/empathy-debug.h>
64
65 /* Flashing delay for icons (milliseconds). */
66 #define FLASH_TIMEOUT 500
67
68 /* Minimum width of roster window if something goes wrong. */
69 #define MIN_WIDTH 50
70
71 /* Accels (menu shortcuts) can be configured and saved */
72 #define ACCELS_FILENAME "accels.txt"
73
74 /* Name in the geometry file */
75 #define GEOMETRY_NAME "main-window"
76
77 typedef struct {
78         EmpathyContactListView  *list_view;
79         EmpathyContactListStore *list_store;
80         MissionControl          *mc;
81         EmpathyChatroomManager  *chatroom_manager;
82         EmpathyEventManager     *event_manager;
83         guint                    flash_timeout_id;
84         gboolean                 flash_on;
85         gpointer                 token;
86
87         GtkWidget              *window;
88         GtkWidget              *main_vbox;
89         GtkWidget              *throbber;
90         GtkWidget              *presence_toolbar;
91         GtkWidget              *presence_chooser;
92         GtkWidget              *errors_vbox;
93
94         GtkWidget              *room;
95         GtkWidget              *room_menu;
96         GtkWidget              *room_sep;
97         GtkWidget              *room_join_favorites;
98         GtkWidget              *edit_context;
99         GtkWidget              *edit_context_separator;
100
101         guint                   size_timeout_id;
102         GHashTable             *errors;
103
104         /* Widgets that are enabled when there is... */
105         GList                  *widgets_connected;      /* ... connected accounts */
106         GList                  *widgets_disconnected;   /* ... disconnected accounts */
107 } EmpathyMainWindow;
108
109 static void     main_window_destroy_cb                         (GtkWidget                *widget,
110                                                                 EmpathyMainWindow        *window);
111 static void     main_window_favorite_chatroom_menu_setup       (EmpathyMainWindow        *window);
112 static void     main_window_favorite_chatroom_menu_added_cb    (EmpathyChatroomManager   *manager,
113                                                                 EmpathyChatroom          *chatroom,
114                                                                 EmpathyMainWindow        *window);
115 static void     main_window_favorite_chatroom_menu_removed_cb  (EmpathyChatroomManager   *manager,
116                                                                 EmpathyChatroom          *chatroom,
117                                                                 EmpathyMainWindow        *window);
118 static void     main_window_favorite_chatroom_menu_activate_cb (GtkMenuItem              *menu_item,
119                                                                 EmpathyChatroom          *chatroom);
120 static void     main_window_favorite_chatroom_menu_update      (EmpathyMainWindow        *window);
121 static void     main_window_favorite_chatroom_menu_add         (EmpathyMainWindow        *window,
122                                                                 EmpathyChatroom          *chatroom);
123 static void     main_window_favorite_chatroom_join             (EmpathyChatroom          *chatroom);
124 static void     main_window_chat_quit_cb                       (GtkWidget                *widget,
125                                                                 EmpathyMainWindow        *window);
126 static void     main_window_chat_new_message_cb                (GtkWidget                *widget,
127                                                                 EmpathyMainWindow        *window);
128 static void     main_window_chat_history_cb                    (GtkWidget                *widget,
129                                                                 EmpathyMainWindow        *window);
130 static void     main_window_room_join_new_cb                   (GtkWidget                *widget,
131                                                                 EmpathyMainWindow        *window);
132 static void     main_window_room_join_favorites_cb             (GtkWidget                *widget,
133                                                                 EmpathyMainWindow        *window);
134 static void     main_window_room_manage_favorites_cb           (GtkWidget                *widget,
135                                                                 EmpathyMainWindow        *window);
136 static void     main_window_chat_add_contact_cb                (GtkWidget                *widget,
137                                                                 EmpathyMainWindow        *window);
138 static void     main_window_chat_show_offline_cb               (GtkCheckMenuItem         *item,
139                                                                 EmpathyMainWindow        *window);
140 static gboolean main_window_edit_button_press_event_cb         (GtkWidget                *widget,
141                                                                 GdkEventButton           *event,
142                                                                 EmpathyMainWindow        *window);
143 static void     main_window_edit_accounts_cb                   (GtkWidget                *widget,
144                                                                 EmpathyMainWindow        *window);
145 static void     main_window_edit_personal_information_cb       (GtkWidget                *widget,
146                                                                 EmpathyMainWindow        *window);
147 static void     main_window_edit_preferences_cb                (GtkWidget                *widget,
148                                                                 EmpathyMainWindow        *window);
149 static void     main_window_help_about_cb                      (GtkWidget                *widget,
150                                                                 EmpathyMainWindow        *window);
151 static void     main_window_help_contents_cb                   (GtkWidget                *widget,
152                                                                 EmpathyMainWindow        *window);
153 static gboolean main_window_throbber_button_press_event_cb     (GtkWidget                *throbber_ebox,
154                                                                 GdkEventButton           *event,
155                                                                 EmpathyMainWindow        *window);
156 static void     main_window_status_changed_cb                  (MissionControl           *mc,
157                                                                 TpConnectionStatus        status,
158                                                                 McPresence                presence,
159                                                                 TpConnectionStatusReason  reason,
160                                                                 const gchar              *unique_name,
161                                                                 EmpathyMainWindow        *window);
162 static void     main_window_update_status                      (EmpathyMainWindow        *window);
163 static void     main_window_accels_load                        (void);
164 static void     main_window_accels_save                        (void);
165 static void     main_window_connection_items_setup             (EmpathyMainWindow        *window,
166                                                                 GladeXML                 *glade);
167 static gboolean main_window_configure_event_timeout_cb         (EmpathyMainWindow        *window);
168 static gboolean main_window_configure_event_cb                 (GtkWidget                *widget,
169                                                                 GdkEventConfigure        *event,
170                                                                 EmpathyMainWindow        *window);
171 static void     main_window_notify_show_offline_cb             (EmpathyConf              *conf,
172                                                                 const gchar              *key,
173                                                                 gpointer                  check_menu_item);
174 static void     main_window_notify_show_avatars_cb             (EmpathyConf              *conf,
175                                                                 const gchar              *key,
176                                                                 EmpathyMainWindow        *window);
177 static void     main_window_notify_compact_contact_list_cb     (EmpathyConf              *conf,
178                                                                 const gchar              *key,
179                                                                 EmpathyMainWindow        *window);
180 static void     main_window_notify_sort_criterium_cb           (EmpathyConf              *conf,
181                                                                 const gchar              *key,
182                                                                 EmpathyMainWindow        *window);
183
184 static void
185 main_window_flash_stop (EmpathyMainWindow *window)
186 {
187         if (window->flash_timeout_id == 0) {
188                 return;
189         }
190
191         DEBUG ("Stop flashing");
192         g_source_remove (window->flash_timeout_id);
193         window->flash_timeout_id = 0;
194         window->flash_on = FALSE;
195 }
196
197 typedef struct {
198         EmpathyEvent *event;
199         gboolean      on;
200 } FlashForeachData;
201
202 static gboolean
203 main_window_flash_foreach (GtkTreeModel *model,
204                            GtkTreePath  *path,
205                            GtkTreeIter  *iter,
206                            gpointer      user_data)
207 {
208         FlashForeachData *data = (FlashForeachData*) user_data;
209         EmpathyContact   *contact;
210         const gchar      *icon_name;
211         GtkTreePath      *parent_path = NULL;
212         GtkTreeIter       parent_iter;
213
214         /* To be used with gtk_tree_model_foreach, update the status icon
215          * of the contact to show the event icon (on=TRUE) or the presence
216          * (on=FALSE) */
217         gtk_tree_model_get (model, iter,
218                             EMPATHY_CONTACT_LIST_STORE_COL_CONTACT, &contact,
219                             -1);
220
221         if (contact != data->event->contact) {
222                 if (contact) {
223                         g_object_unref (contact);
224                 }
225                 return FALSE;
226         }
227
228         if (data->on) {
229                 icon_name = data->event->icon_name;
230         } else {
231                 icon_name = empathy_icon_name_for_contact (contact);
232         }
233
234         gtk_tree_store_set (GTK_TREE_STORE (model), iter,
235                             EMPATHY_CONTACT_LIST_STORE_COL_ICON_STATUS, icon_name,
236                             -1);
237
238         /* To make sure the parent is shown correctly, we emit
239          * the row-changed signal on the parent so it prompts
240          * it to be refreshed by the filter func. 
241          */
242         if (gtk_tree_model_iter_parent (model, &parent_iter, iter)) {
243                 parent_path = gtk_tree_model_get_path (model, &parent_iter);
244         }
245         if (parent_path) {
246                 gtk_tree_model_row_changed (model, parent_path, &parent_iter);
247                 gtk_tree_path_free (parent_path);
248         }
249
250         g_object_unref (contact);
251
252         return FALSE;
253 }
254
255 static gboolean
256 main_window_flash_cb (EmpathyMainWindow *window)
257 {
258         GtkTreeModel     *model;
259         GSList           *events, *l;
260         gboolean          found_event = FALSE;
261         FlashForeachData  data;
262
263         window->flash_on = !window->flash_on;
264         data.on = window->flash_on;
265         model = GTK_TREE_MODEL (window->list_store);
266
267         events = empathy_event_manager_get_events (window->event_manager);
268         for (l = events; l; l = l->next) {
269                 data.event = l->data;
270                 if (!data.event->contact) {
271                         continue;
272                 }
273
274                 found_event = TRUE;
275                 gtk_tree_model_foreach (model,
276                                         main_window_flash_foreach,
277                                         &data);
278         }
279
280         if (!found_event) {
281                 main_window_flash_stop (window);
282         }
283
284         return TRUE;
285 }
286
287 static void
288 main_window_flash_start (EmpathyMainWindow *window)
289 {
290         if (empathy_sound_pref_is_enabled (EMPATHY_PREFS_SOUNDS_NEW_CONVERSATION)) {
291                 ca_gtk_play_for_widget (GTK_WIDGET (window->window), 0,
292                                         CA_PROP_EVENT_ID, "message-new-instant",
293                                         CA_PROP_EVENT_DESCRIPTION, _("Incoming chat request"),
294                                         NULL);
295         }
296
297         if (window->flash_timeout_id != 0) {
298                 return;
299         }
300
301         DEBUG ("Start flashing");
302         window->flash_timeout_id = g_timeout_add (FLASH_TIMEOUT,
303                                                   (GSourceFunc) main_window_flash_cb,
304                                                   window);
305         main_window_flash_cb (window);
306 }
307
308 static void
309 main_window_event_added_cb (EmpathyEventManager *manager,
310                             EmpathyEvent        *event,
311                             EmpathyMainWindow   *window)
312 {
313         if (event->contact) {
314                 main_window_flash_start (window);
315         }
316 }
317
318 static void
319 main_window_event_removed_cb (EmpathyEventManager *manager,
320                               EmpathyEvent        *event,
321                               EmpathyMainWindow   *window)
322 {
323         FlashForeachData data;
324
325         if (!event->contact) {
326                 return;
327         }
328
329         data.on = FALSE;
330         data.event = event;
331         gtk_tree_model_foreach (GTK_TREE_MODEL (window->list_store),
332                                 main_window_flash_foreach,
333                                 &data);
334 }
335
336 static void
337 main_window_row_activated_cb (EmpathyContactListView *view,
338                               GtkTreePath            *path,
339                               GtkTreeViewColumn      *col,
340                               EmpathyMainWindow      *window)
341 {
342         EmpathyContact *contact;
343         GtkTreeModel   *model;
344         GtkTreeIter     iter;
345         GSList         *events, *l;
346
347         model = GTK_TREE_MODEL (window->list_store);
348         gtk_tree_model_get_iter (model, &iter, path);
349         gtk_tree_model_get (model, &iter,
350                             EMPATHY_CONTACT_LIST_STORE_COL_CONTACT, &contact,
351                             -1);
352
353         if (!contact) {
354                 return;
355         }
356
357         /* If the contact has an event activate it, otherwise the
358          * default handler of row-activated will be called. */
359         events = empathy_event_manager_get_events (window->event_manager);
360         for (l = events; l; l = l->next) {
361                 EmpathyEvent *event = l->data;
362
363                 if (event->contact == contact) {
364                         DEBUG ("Activate event");
365                         empathy_event_activate (event);
366
367                         /* We don't want the default handler of this signal
368                          * (e.g. open a chat) */
369                         g_signal_stop_emission_by_name (view, "row-activated");
370                         break;
371                 }
372         }
373
374         g_object_unref (contact);
375 }
376
377 GtkWidget *
378 empathy_main_window_show (void)
379 {
380         static EmpathyMainWindow *window = NULL;
381         EmpathyContactList       *list_iface;
382         GladeXML                 *glade;
383         EmpathyConf               *conf;
384         GtkWidget                *sw;
385         GtkWidget                *show_offline_widget;
386         GtkWidget                *ebox;
387         GtkToolItem              *item;
388         gboolean                  show_offline;
389         gboolean                  show_avatars;
390         gboolean                  compact_contact_list;
391         gint                      x, y, w, h;
392         gchar                    *filename;
393         GSList                   *l;
394
395         if (window) {
396                 empathy_window_present (GTK_WINDOW (window->window), TRUE);
397                 return window->window;
398         }
399
400         window = g_new0 (EmpathyMainWindow, 1);
401
402         /* Set up interface */
403         filename = empathy_file_lookup ("empathy-main-window.glade", "src");
404         glade = empathy_glade_get_file (filename,
405                                        "main_window",
406                                        NULL,
407                                        "main_window", &window->window,
408                                        "main_vbox", &window->main_vbox,
409                                        "errors_vbox", &window->errors_vbox,
410                                        "chat_show_offline", &show_offline_widget,
411                                        "room", &window->room,
412                                        "room_sep", &window->room_sep,
413                                        "room_join_favorites", &window->room_join_favorites,
414                                        "edit_context", &window->edit_context,
415                                        "edit_context_separator", &window->edit_context_separator,
416                                        "presence_toolbar", &window->presence_toolbar,
417                                        "roster_scrolledwindow", &sw,
418                                        NULL);
419         g_free (filename);
420
421         empathy_glade_connect (glade,
422                               window,
423                               "main_window", "destroy", main_window_destroy_cb,
424                               "main_window", "configure_event", main_window_configure_event_cb,
425                               "chat_quit", "activate", main_window_chat_quit_cb,
426                               "chat_new_message", "activate", main_window_chat_new_message_cb,
427                               "chat_history", "activate", main_window_chat_history_cb,
428                               "room_join_new", "activate", main_window_room_join_new_cb,
429                               "room_join_favorites", "activate", main_window_room_join_favorites_cb,
430                               "room_manage_favorites", "activate", main_window_room_manage_favorites_cb,
431                               "chat_add_contact", "activate", main_window_chat_add_contact_cb,
432                               "chat_show_offline", "toggled", main_window_chat_show_offline_cb,
433                               "edit", "button-press-event", main_window_edit_button_press_event_cb,
434                               "edit_accounts", "activate", main_window_edit_accounts_cb,
435                               "edit_personal_information", "activate", main_window_edit_personal_information_cb,
436                               "edit_preferences", "activate", main_window_edit_preferences_cb,
437                               "help_about", "activate", main_window_help_about_cb,
438                               "help_contents", "activate", main_window_help_contents_cb,
439                               NULL);
440
441         /* Set up connection related widgets. */
442         main_window_connection_items_setup (window, glade);
443         g_object_unref (glade);
444
445         window->mc = empathy_mission_control_new ();
446         window->token = empathy_connect_to_account_status_changed (window->mc,
447                                                    G_CALLBACK (main_window_status_changed_cb),
448                                                    window, NULL);
449
450         window->errors = g_hash_table_new_full (empathy_account_hash,
451                                                 empathy_account_equal,
452                                                 g_object_unref,
453                                                 NULL);
454
455         /* Set up menu */
456         main_window_favorite_chatroom_menu_setup (window);
457
458         gtk_widget_hide (window->edit_context);
459         gtk_widget_hide (window->edit_context_separator);
460
461         /* Set up presence chooser */
462         window->presence_chooser = empathy_presence_chooser_new ();
463         gtk_widget_show (window->presence_chooser);
464         item = gtk_tool_item_new ();
465         gtk_widget_show (GTK_WIDGET (item));
466         gtk_container_add (GTK_CONTAINER (item), window->presence_chooser);
467         gtk_tool_item_set_is_important (item, TRUE);
468         gtk_tool_item_set_expand (item, TRUE);
469         gtk_toolbar_insert (GTK_TOOLBAR (window->presence_toolbar), item, -1);
470
471         /* Set up the throbber */
472         ebox = gtk_event_box_new ();
473         gtk_event_box_set_visible_window (GTK_EVENT_BOX (ebox), FALSE);
474         gtk_widget_set_tooltip_text (ebox, _("Show and edit accounts"));
475         g_signal_connect (ebox,
476                           "button-press-event",
477                           G_CALLBACK (main_window_throbber_button_press_event_cb),
478                           window);
479         gtk_widget_show (ebox);
480
481         window->throbber = ephy_spinner_new ();
482         ephy_spinner_set_size (EPHY_SPINNER (window->throbber), GTK_ICON_SIZE_LARGE_TOOLBAR);
483         gtk_container_add (GTK_CONTAINER (ebox), window->throbber);
484         gtk_widget_show (window->throbber);
485
486         item = gtk_tool_item_new ();
487         gtk_container_add (GTK_CONTAINER (item), ebox);
488         gtk_toolbar_insert (GTK_TOOLBAR (window->presence_toolbar), item, -1);
489         gtk_widget_show (GTK_WIDGET (item));
490
491         /* Set up contact list. */
492         empathy_status_presets_get_all ();
493
494         list_iface = EMPATHY_CONTACT_LIST (empathy_contact_manager_new ());
495         window->list_store = empathy_contact_list_store_new (list_iface);
496         window->list_view = empathy_contact_list_view_new (window->list_store,
497                                                            EMPATHY_CONTACT_LIST_FEATURE_ALL,
498                                                            EMPATHY_CONTACT_FEATURE_ALL);
499         g_object_unref (list_iface);
500
501         gtk_widget_show (GTK_WIDGET (window->list_view));
502         gtk_container_add (GTK_CONTAINER (sw),
503                            GTK_WIDGET (window->list_view));
504         g_signal_connect (window->list_view, "row-activated",
505                           G_CALLBACK (main_window_row_activated_cb),
506                           window);
507
508         /* Load user-defined accelerators. */
509         main_window_accels_load ();
510
511         /* Set window size. */
512         empathy_geometry_load (GEOMETRY_NAME, &x, &y, &w, &h);
513
514         if (w >= 1 && h >= 1) {
515                 /* Use the defaults from the glade file if we
516                  * don't have good w, h geometry.
517                  */
518                 DEBUG ("Configuring window default size w:%d, h:%d", w, h);
519                 gtk_window_set_default_size (GTK_WINDOW (window->window), w, h);
520         }
521
522         if (x >= 0 && y >= 0) {
523                 /* Let the window manager position it if we
524                  * don't have good x, y coordinates.
525                  */
526                 DEBUG ("Configuring window default position x:%d, y:%d", x, y);
527                 gtk_window_move (GTK_WINDOW (window->window), x, y);
528         }
529
530         /* Enable event handling */
531         window->event_manager = empathy_event_manager_new ();
532         g_signal_connect (window->event_manager, "event-added",
533                           G_CALLBACK (main_window_event_added_cb),
534                           window);
535         g_signal_connect (window->event_manager, "event-removed",
536                           G_CALLBACK (main_window_event_removed_cb),
537                           window);
538
539         l = empathy_event_manager_get_events (window->event_manager);
540         while (l) {
541                 main_window_event_added_cb (window->event_manager,
542                                             l->data, window);
543                 l = l->next;
544         }
545
546         conf = empathy_conf_get ();
547
548         /* Show offline ? */
549         empathy_conf_get_bool (conf,
550                               EMPATHY_PREFS_CONTACTS_SHOW_OFFLINE,
551                               &show_offline);
552         empathy_conf_notify_add (conf,
553                                 EMPATHY_PREFS_CONTACTS_SHOW_OFFLINE,
554                                 main_window_notify_show_offline_cb,
555                                 show_offline_widget);
556
557         gtk_check_menu_item_set_active (GTK_CHECK_MENU_ITEM (show_offline_widget),
558                                         show_offline);
559
560         /* Show avatars ? */
561         empathy_conf_get_bool (conf,
562                               EMPATHY_PREFS_UI_SHOW_AVATARS,
563                               &show_avatars);
564         empathy_conf_notify_add (conf,
565                                 EMPATHY_PREFS_UI_SHOW_AVATARS,
566                                 (EmpathyConfNotifyFunc) main_window_notify_show_avatars_cb,
567                                 window);
568         empathy_contact_list_store_set_show_avatars (window->list_store, show_avatars);
569
570         /* Is compact ? */
571         empathy_conf_get_bool (conf,
572                               EMPATHY_PREFS_UI_COMPACT_CONTACT_LIST,
573                               &compact_contact_list);
574         empathy_conf_notify_add (conf,
575                                 EMPATHY_PREFS_UI_COMPACT_CONTACT_LIST,
576                                 (EmpathyConfNotifyFunc) main_window_notify_compact_contact_list_cb,
577                                 window);
578         empathy_contact_list_store_set_is_compact (window->list_store, compact_contact_list);
579
580         /* Sort criterium */
581         empathy_conf_notify_add (conf,
582                                 EMPATHY_PREFS_CONTACTS_SORT_CRITERIUM,
583                                 (EmpathyConfNotifyFunc) main_window_notify_sort_criterium_cb,
584                                 window);
585         main_window_notify_sort_criterium_cb (conf,
586                                               EMPATHY_PREFS_CONTACTS_SORT_CRITERIUM,
587                                               window);
588
589         main_window_update_status (window);
590
591         return window->window;
592 }
593
594 static void
595 main_window_destroy_cb (GtkWidget         *widget,
596                         EmpathyMainWindow *window)
597 {
598         /* Save user-defined accelerators. */
599         main_window_accels_save ();
600
601         empathy_disconnect_account_status_changed (window->token);
602
603         if (window->size_timeout_id) {
604                 g_source_remove (window->size_timeout_id);
605         }
606
607         g_list_free (window->widgets_connected);
608         g_list_free (window->widgets_disconnected);
609
610         g_object_unref (window->mc);
611         g_object_unref (window->list_store);
612         g_hash_table_destroy (window->errors);
613
614         g_signal_handlers_disconnect_by_func (window->event_manager,
615                                               main_window_event_added_cb,
616                                               window);
617         g_signal_handlers_disconnect_by_func (window->event_manager,
618                                               main_window_event_removed_cb,
619                                               window);
620         g_object_unref (window->event_manager);
621
622         g_free (window);
623 }
624
625 static void
626 main_window_favorite_chatroom_menu_setup (EmpathyMainWindow *window)
627 {
628         GList *chatrooms, *l;
629
630         window->chatroom_manager = empathy_chatroom_manager_new (NULL);
631         chatrooms = empathy_chatroom_manager_get_chatrooms (window->chatroom_manager, NULL);
632         window->room_menu = gtk_menu_item_get_submenu (GTK_MENU_ITEM (window->room));
633
634         for (l = chatrooms; l; l = l->next) {
635                 main_window_favorite_chatroom_menu_add (window, l->data);
636         }
637
638         if (!chatrooms) {
639                 gtk_widget_hide (window->room_sep);
640         }
641
642         gtk_widget_set_sensitive (window->room_join_favorites, chatrooms != NULL);
643
644         g_signal_connect (window->chatroom_manager, "chatroom-added",
645                           G_CALLBACK (main_window_favorite_chatroom_menu_added_cb),
646                           window);
647         g_signal_connect (window->chatroom_manager, "chatroom-removed",
648                           G_CALLBACK (main_window_favorite_chatroom_menu_removed_cb),
649                           window);
650
651         g_list_free (chatrooms);
652 }
653
654 static void
655 main_window_favorite_chatroom_menu_added_cb (EmpathyChatroomManager *manager,
656                                              EmpathyChatroom        *chatroom,
657                                              EmpathyMainWindow     *window)
658 {
659         main_window_favorite_chatroom_menu_add (window, chatroom);
660         gtk_widget_show (window->room_sep);
661         gtk_widget_set_sensitive (window->room_join_favorites, TRUE);
662 }
663
664 static void
665 main_window_favorite_chatroom_menu_removed_cb (EmpathyChatroomManager *manager,
666                                                EmpathyChatroom        *chatroom,
667                                                EmpathyMainWindow     *window)
668 {
669         GtkWidget *menu_item;
670
671         menu_item = g_object_get_data (G_OBJECT (chatroom), "menu_item");
672
673         g_object_set_data (G_OBJECT (chatroom), "menu_item", NULL);
674         gtk_widget_destroy (menu_item);
675
676         main_window_favorite_chatroom_menu_update (window);
677 }
678
679 static void
680 main_window_favorite_chatroom_menu_activate_cb (GtkMenuItem    *menu_item,
681                                                 EmpathyChatroom *chatroom)
682 {
683         main_window_favorite_chatroom_join (chatroom);
684 }
685
686 static void
687 main_window_favorite_chatroom_menu_update (EmpathyMainWindow *window)
688 {
689         GList *chatrooms;
690
691         chatrooms = empathy_chatroom_manager_get_chatrooms (window->chatroom_manager, NULL);
692
693         if (chatrooms) {
694                 gtk_widget_show (window->room_sep);
695         } else {
696                 gtk_widget_hide (window->room_sep);
697         }
698
699         gtk_widget_set_sensitive (window->room_join_favorites, chatrooms != NULL);
700         g_list_free (chatrooms);
701 }
702
703 static void
704 main_window_favorite_chatroom_menu_add (EmpathyMainWindow *window,
705                                         EmpathyChatroom    *chatroom)
706 {
707         GtkWidget   *menu_item;
708         const gchar *name;
709
710         if (g_object_get_data (G_OBJECT (chatroom), "menu_item")) {
711                 return;
712         }
713
714         name = empathy_chatroom_get_name (chatroom);
715         menu_item = gtk_menu_item_new_with_label (name);
716
717         g_object_set_data (G_OBJECT (chatroom), "menu_item", menu_item);
718         g_signal_connect (menu_item, "activate",
719                           G_CALLBACK (main_window_favorite_chatroom_menu_activate_cb),
720                           chatroom);
721
722         gtk_menu_shell_insert (GTK_MENU_SHELL (window->room_menu),
723                                menu_item, 3);
724
725         gtk_widget_show (menu_item);
726 }
727
728 static void
729 main_window_favorite_chatroom_join (EmpathyChatroom *chatroom)
730 {
731         MissionControl *mc;
732         McAccount      *account;
733         TpConnection   *connection;
734         const gchar    *room;
735
736         mc = empathy_mission_control_new ();
737         account = empathy_chatroom_get_account (chatroom);
738         room = empathy_chatroom_get_room (chatroom);
739
740         DEBUG ("Requesting channel for '%s'", room);
741
742         connection = mission_control_get_tpconnection (mc, account, NULL);
743         if (connection) {
744                 tp_connection_run_until_ready (connection, TRUE, NULL, NULL);   
745                 empathy_connection_request_channel (connection, -1,
746                                                     TP_IFACE_CHANNEL_TYPE_TEXT,
747                                                     TP_HANDLE_TYPE_ROOM,
748                                                     room, TRUE,
749                                                     NULL, NULL, NULL, NULL);
750                 g_object_unref (connection);
751         }
752         g_object_unref (mc);
753 }
754
755 static void
756 main_window_chat_quit_cb (GtkWidget         *widget,
757                           EmpathyMainWindow *window)
758 {
759         gtk_main_quit ();
760 }
761
762 static void
763 main_window_chat_new_message_cb (GtkWidget         *widget,
764                                  EmpathyMainWindow *window)
765 {
766         empathy_new_message_dialog_show (GTK_WINDOW (window->window));
767 }
768
769 static void
770 main_window_chat_history_cb (GtkWidget         *widget,
771                              EmpathyMainWindow *window)
772 {
773         empathy_log_window_show (NULL, NULL, FALSE, GTK_WINDOW (window->window));
774 }
775
776 static void
777 main_window_room_join_new_cb (GtkWidget         *widget,
778                               EmpathyMainWindow *window)
779 {
780         empathy_new_chatroom_dialog_show (GTK_WINDOW (window->window));
781 }
782
783 static void
784 main_window_room_join_favorites_cb (GtkWidget         *widget,
785                                     EmpathyMainWindow *window)
786 {
787         GList *chatrooms, *l;
788
789         chatrooms = empathy_chatroom_manager_get_chatrooms (window->chatroom_manager, NULL);
790         for (l = chatrooms; l; l = l->next) {
791                 main_window_favorite_chatroom_join (l->data);
792         }
793         g_list_free (chatrooms);
794 }
795
796 static void
797 main_window_room_manage_favorites_cb (GtkWidget         *widget,
798                                       EmpathyMainWindow *window)
799 {
800         empathy_chatrooms_window_show (GTK_WINDOW (window->window));
801 }
802
803 static void
804 main_window_chat_add_contact_cb (GtkWidget         *widget,
805                                  EmpathyMainWindow *window)
806 {
807         empathy_new_contact_dialog_show (GTK_WINDOW (window->window));
808 }
809
810 static void
811 main_window_chat_show_offline_cb (GtkCheckMenuItem  *item,
812                                   EmpathyMainWindow *window)
813 {
814         gboolean current;
815
816         current = gtk_check_menu_item_get_active (item);
817
818         empathy_conf_set_bool (empathy_conf_get (),
819                               EMPATHY_PREFS_CONTACTS_SHOW_OFFLINE,
820                               current);
821
822         /* Turn off sound just while we alter the contact list. */
823         // FIXME: empathy_sound_set_enabled (FALSE);
824         empathy_contact_list_store_set_show_offline (window->list_store, current);
825         //empathy_sound_set_enabled (TRUE);
826 }
827
828 static gboolean
829 main_window_edit_button_press_event_cb (GtkWidget         *widget,
830                                         GdkEventButton    *event,
831                                         EmpathyMainWindow *window)
832 {
833         GtkWidget *submenu;
834
835         if (!event->button == 1) {
836                 return FALSE;
837         }
838
839         submenu = empathy_contact_list_view_get_contact_menu (window->list_view);
840         if (submenu) {
841                 GtkMenuItem *item;
842                 GtkWidget   *label;
843
844                 item = GTK_MENU_ITEM (window->edit_context);
845                 label = gtk_bin_get_child (GTK_BIN (item));
846                 gtk_label_set_text (GTK_LABEL (label), _("Contact"));
847
848                 gtk_widget_show (window->edit_context);
849                 gtk_widget_show (window->edit_context_separator);
850
851                 gtk_menu_item_set_submenu (item, submenu);
852
853                 return FALSE;
854         }
855
856         submenu = empathy_contact_list_view_get_group_menu (window->list_view);
857         if (submenu) {
858                 GtkMenuItem *item;
859                 GtkWidget   *label;
860
861                 item = GTK_MENU_ITEM (window->edit_context);
862                 label = gtk_bin_get_child (GTK_BIN (item));
863                 gtk_label_set_text (GTK_LABEL (label), _("Group"));
864
865                 gtk_widget_show (window->edit_context);
866                 gtk_widget_show (window->edit_context_separator);
867
868                 gtk_menu_item_set_submenu (item, submenu);
869
870                 return FALSE;
871         }
872
873         gtk_widget_hide (window->edit_context);
874         gtk_widget_hide (window->edit_context_separator);
875
876         return FALSE;
877 }
878
879 static void
880 main_window_edit_accounts_cb (GtkWidget         *widget,
881                               EmpathyMainWindow *window)
882 {
883         empathy_accounts_dialog_show (GTK_WINDOW (window->window), NULL);
884 }
885
886 static void
887 main_window_edit_personal_information_cb (GtkWidget         *widget,
888                                           EmpathyMainWindow *window)
889 {
890         GSList *accounts;
891
892         accounts = mission_control_get_online_connections (window->mc, NULL);
893         if (accounts) {
894                 EmpathyContactFactory *factory;
895                 EmpathyContact        *contact;
896                 McAccount             *account;
897
898                 account = accounts->data;
899                 factory = empathy_contact_factory_new ();
900                 contact = empathy_contact_factory_get_user (factory, account);
901                 empathy_contact_run_until_ready (contact,
902                                                  EMPATHY_CONTACT_READY_HANDLE |
903                                                  EMPATHY_CONTACT_READY_ID,
904                                                  NULL);
905
906                 empathy_contact_information_dialog_show (contact,
907                                                          GTK_WINDOW (window->window),
908                                                          TRUE, TRUE);
909
910                 g_slist_foreach (accounts, (GFunc) g_object_unref, NULL);
911                 g_slist_free (accounts);
912                 g_object_unref (factory);
913                 g_object_unref (contact);
914         }
915 }
916
917 static void
918 main_window_edit_preferences_cb (GtkWidget         *widget,
919                                  EmpathyMainWindow *window)
920 {
921         empathy_preferences_show (GTK_WINDOW (window->window));
922 }
923
924 static void
925 main_window_help_about_cb (GtkWidget         *widget,
926                            EmpathyMainWindow *window)
927 {
928         empathy_about_dialog_new (GTK_WINDOW (window->window));
929 }
930
931 static void
932 main_window_help_contents_cb (GtkWidget         *widget,
933                               EmpathyMainWindow *window)
934 {
935         empathy_url_show (widget, "ghelp:empathy");
936 }
937
938 static gboolean
939 main_window_throbber_button_press_event_cb (GtkWidget         *throbber_ebox,
940                                             GdkEventButton    *event,
941                                             EmpathyMainWindow *window)
942 {
943         if (event->type != GDK_BUTTON_PRESS ||
944             event->button != 1) {
945                 return FALSE;
946         }
947
948         empathy_accounts_dialog_show (GTK_WINDOW (window->window), NULL);
949
950         return FALSE;
951 }
952
953 static void
954 main_window_error_edit_clicked_cb (GtkButton         *button,
955                                    EmpathyMainWindow *window)
956 {
957         McAccount *account;
958         GtkWidget *error_widget;
959
960         account = g_object_get_data (G_OBJECT (button), "account");
961         empathy_accounts_dialog_show (GTK_WINDOW (window->window), account);
962
963         error_widget = g_hash_table_lookup (window->errors, account);
964         gtk_widget_destroy (error_widget);
965         g_hash_table_remove (window->errors, account);
966 }
967
968 static void
969 main_window_error_clear_clicked_cb (GtkButton         *button,
970                                     EmpathyMainWindow *window)
971 {
972         McAccount *account;
973         GtkWidget *error_widget;
974
975         account = g_object_get_data (G_OBJECT (button), "account");
976         error_widget = g_hash_table_lookup (window->errors, account);
977         gtk_widget_destroy (error_widget);
978         g_hash_table_remove (window->errors, account);
979 }
980
981 static void
982 main_window_error_display (EmpathyMainWindow *window,
983                            McAccount         *account,
984                            const gchar       *message)
985 {
986         GtkWidget *child;
987         GtkWidget *table;
988         GtkWidget *image;
989         GtkWidget *button_edit;
990         GtkWidget *alignment;
991         GtkWidget *hbox;
992         GtkWidget *label;
993         GtkWidget *fixed;
994         GtkWidget *vbox;
995         GtkWidget *button_close;
996         gchar     *str;
997
998         child = g_hash_table_lookup (window->errors, account);
999         if (child) {
1000                 label = g_object_get_data (G_OBJECT (child), "label");
1001
1002                 /* Just set the latest error and return */
1003                 str = g_markup_printf_escaped ("<b>%s</b>\n%s",
1004                                                mc_account_get_display_name (account),
1005                                                message);
1006                 gtk_label_set_markup (GTK_LABEL (label), str);
1007                 g_free (str);
1008
1009                 return;
1010         }
1011
1012         child = gtk_vbox_new (FALSE, 0);
1013         gtk_box_pack_start (GTK_BOX (window->errors_vbox), child, FALSE, TRUE, 0);
1014         gtk_container_set_border_width (GTK_CONTAINER (child), 6);
1015         gtk_widget_show (child);
1016
1017         table = gtk_table_new (2, 4, FALSE);
1018         gtk_widget_show (table);
1019         gtk_box_pack_start (GTK_BOX (child), table, TRUE, TRUE, 0);
1020         gtk_table_set_row_spacings (GTK_TABLE (table), 12);
1021         gtk_table_set_col_spacings (GTK_TABLE (table), 6);
1022
1023         image = gtk_image_new_from_stock (GTK_STOCK_DISCONNECT, GTK_ICON_SIZE_MENU);
1024         gtk_widget_show (image);
1025         gtk_table_attach (GTK_TABLE (table), image, 0, 1, 0, 2,
1026                           (GtkAttachOptions) (GTK_FILL),
1027                           (GtkAttachOptions) (GTK_FILL), 0, 0);
1028         gtk_misc_set_alignment (GTK_MISC (image), 0.5, 0);
1029
1030         button_edit = gtk_button_new ();
1031         gtk_widget_show (button_edit);
1032         gtk_table_attach (GTK_TABLE (table), button_edit, 1, 2, 1, 2,
1033                           (GtkAttachOptions) (GTK_FILL),
1034                           (GtkAttachOptions) (0), 0, 0);
1035
1036         alignment = gtk_alignment_new (0.5, 0.5, 0, 0);
1037         gtk_widget_show (alignment);
1038         gtk_container_add (GTK_CONTAINER (button_edit), alignment);
1039
1040         hbox = gtk_hbox_new (FALSE, 2);
1041         gtk_widget_show (hbox);
1042         gtk_container_add (GTK_CONTAINER (alignment), hbox);
1043
1044         image = gtk_image_new_from_stock (GTK_STOCK_EDIT, GTK_ICON_SIZE_BUTTON);
1045         gtk_widget_show (image);
1046         gtk_box_pack_start (GTK_BOX (hbox), image, FALSE, FALSE, 0);
1047
1048         label = gtk_label_new_with_mnemonic (_("_Edit account"));
1049         gtk_widget_show (label);
1050         gtk_box_pack_start (GTK_BOX (hbox), label, FALSE, FALSE, 0);
1051
1052         fixed = gtk_fixed_new ();
1053         gtk_widget_show (fixed);
1054         gtk_table_attach (GTK_TABLE (table), fixed, 2, 3, 1, 2,
1055                           (GtkAttachOptions) (GTK_EXPAND | GTK_FILL),
1056                           (GtkAttachOptions) (GTK_FILL), 0, 0);
1057
1058         vbox = gtk_vbox_new (FALSE, 6);
1059         gtk_widget_show (vbox);
1060         gtk_table_attach (GTK_TABLE (table), vbox, 3, 4, 0, 2,
1061                           (GtkAttachOptions) (GTK_FILL),
1062                           (GtkAttachOptions) (GTK_FILL), 0, 0);
1063
1064         button_close = gtk_button_new ();
1065         gtk_widget_show (button_close);
1066         gtk_box_pack_start (GTK_BOX (vbox), button_close, FALSE, FALSE, 0);
1067         gtk_button_set_relief (GTK_BUTTON (button_close), GTK_RELIEF_NONE);
1068
1069
1070         image = gtk_image_new_from_stock ("gtk-close", GTK_ICON_SIZE_MENU);
1071         gtk_widget_show (image);
1072         gtk_container_add (GTK_CONTAINER (button_close), image);
1073
1074         label = gtk_label_new ("");
1075         gtk_widget_show (label);
1076         gtk_table_attach (GTK_TABLE (table), label, 1, 3, 0, 1,
1077                           (GtkAttachOptions) (GTK_EXPAND | GTK_SHRINK | GTK_FILL),
1078                           (GtkAttachOptions) (GTK_EXPAND | GTK_SHRINK | GTK_FILL), 0, 0);
1079         gtk_widget_set_size_request (label, 175, -1);
1080         gtk_label_set_use_markup (GTK_LABEL (label), TRUE);
1081         gtk_label_set_line_wrap (GTK_LABEL (label), TRUE);
1082         gtk_misc_set_alignment (GTK_MISC (label), 0, 0);
1083
1084         str = g_markup_printf_escaped ("<b>%s</b>\n%s",
1085                                        mc_account_get_display_name (account),
1086                                        message);
1087         gtk_label_set_markup (GTK_LABEL (label), str);
1088         g_free (str);
1089
1090         g_object_set_data (G_OBJECT (child), "label", label);
1091         g_object_set_data_full (G_OBJECT (button_edit),
1092                                 "account", g_object_ref (account),
1093                                 g_object_unref);
1094         g_object_set_data_full (G_OBJECT (button_close),
1095                                 "account", g_object_ref (account),
1096                                 g_object_unref);
1097
1098         g_signal_connect (button_edit, "clicked",
1099                           G_CALLBACK (main_window_error_edit_clicked_cb),
1100                           window);
1101
1102         g_signal_connect (button_close, "clicked",
1103                           G_CALLBACK (main_window_error_clear_clicked_cb),
1104                           window);
1105
1106         gtk_widget_show (window->errors_vbox);
1107
1108         g_hash_table_insert (window->errors, g_object_ref (account), child);
1109 }
1110
1111 static void
1112 main_window_status_changed_cb (MissionControl           *mc,
1113                                TpConnectionStatus        status,
1114                                McPresence                presence,
1115                                TpConnectionStatusReason  reason,
1116                                const gchar              *unique_name,
1117                                EmpathyMainWindow        *window)
1118 {
1119         McAccount *account;
1120
1121         main_window_update_status (window);
1122
1123         account = mc_account_lookup (unique_name);
1124
1125         if (status == TP_CONNECTION_STATUS_DISCONNECTED &&
1126             reason != TP_CONNECTION_STATUS_REASON_REQUESTED) {
1127                 const gchar *message;
1128
1129                 switch (reason) {
1130                 case TP_CONNECTION_STATUS_REASON_NONE_SPECIFIED:
1131                         message = _("No error specified");
1132                         break;
1133                 case TP_CONNECTION_STATUS_REASON_NETWORK_ERROR:
1134                         message = _("Network error");
1135                         break;
1136                 case TP_CONNECTION_STATUS_REASON_AUTHENTICATION_FAILED:
1137                         message = _("Authentication failed");
1138                         break;
1139                 case TP_CONNECTION_STATUS_REASON_ENCRYPTION_ERROR:
1140                         message = _("Encryption error");
1141                         break;
1142                 case TP_CONNECTION_STATUS_REASON_NAME_IN_USE:
1143                         message = _("Name in use");
1144                         break;
1145                 case TP_CONNECTION_STATUS_REASON_CERT_NOT_PROVIDED:
1146                         message = _("Certificate not provided");
1147                         break;
1148                 case TP_CONNECTION_STATUS_REASON_CERT_UNTRUSTED:
1149                         message = _("Certificate untrusted");
1150                         break;
1151                 case TP_CONNECTION_STATUS_REASON_CERT_EXPIRED:
1152                         message = _("Certificate expired");
1153                         break;
1154                 case TP_CONNECTION_STATUS_REASON_CERT_NOT_ACTIVATED:
1155                         message = _("Certificate not activated");
1156                         break;
1157                 case TP_CONNECTION_STATUS_REASON_CERT_HOSTNAME_MISMATCH:
1158                         message = _("Certificate hostname mismatch");
1159                         break;
1160                 case TP_CONNECTION_STATUS_REASON_CERT_FINGERPRINT_MISMATCH:
1161                         message = _("Certificate fingerprint mismatch");
1162                         break;
1163                 case TP_CONNECTION_STATUS_REASON_CERT_SELF_SIGNED:
1164                         message = _("Certificate self-signed");
1165                         break;
1166                 case TP_CONNECTION_STATUS_REASON_CERT_OTHER_ERROR:
1167                         message = _("Certificate error");
1168                         break;
1169                 default:
1170                         message = _("Unknown error");
1171                         break;
1172                 }
1173
1174                 main_window_error_display (window, account, message);
1175         }
1176
1177         if (status == TP_CONNECTION_STATUS_DISCONNECTED &&
1178             empathy_sound_pref_is_enabled (EMPATHY_PREFS_SOUNDS_SERVICE_LOGOUT)) {
1179                 ca_gtk_play_for_widget (GTK_WIDGET (window->window), 0,
1180                                         CA_PROP_EVENT_ID, "service-logout",
1181                                         CA_PROP_EVENT_DESCRIPTION, _("Disconnected from server"),
1182                                         NULL);
1183         }
1184
1185         if (status == TP_CONNECTION_STATUS_CONNECTED) {
1186                 GtkWidget *error_widget;
1187                 EmpathyIdle *idle;
1188
1189                 idle = empathy_idle_new ();
1190
1191                 /* emit the sound only on first connect, i.e. when the saved
1192                  * idle state is MC_PRESENCE_UNSET.
1193                  */
1194
1195                 if (empathy_idle_get_state (idle) == MC_PRESENCE_UNSET &&
1196                     empathy_sound_pref_is_enabled (EMPATHY_PREFS_SOUNDS_SERVICE_LOGIN)) {
1197                         ca_gtk_play_for_widget (GTK_WIDGET (window->window), 0,
1198                                                 CA_PROP_EVENT_ID, "service-login",
1199                                                 CA_PROP_EVENT_DESCRIPTION, _("Connected to server"),
1200                                                 NULL);
1201                 }
1202
1203                 g_object_unref (idle);
1204
1205                 /* Account connected without error, remove error message if any */
1206                 error_widget = g_hash_table_lookup (window->errors, account);
1207                 if (error_widget) {
1208                         gtk_widget_destroy (error_widget);
1209                         g_hash_table_remove (window->errors, account);
1210                 }
1211         }
1212
1213         g_object_unref (account);
1214 }
1215
1216 static void
1217 main_window_update_status (EmpathyMainWindow *window)
1218 {
1219         GList *accounts, *l;
1220         guint  connected = 0;
1221         guint  connecting = 0;
1222         guint  disconnected = 0;
1223
1224         /* Count number of connected/connecting/disconnected accounts */
1225         accounts = mc_accounts_list (); 
1226         for (l = accounts; l; l = l->next) {
1227                 McAccount *account;
1228                 guint      status;
1229
1230                 account = l->data;
1231
1232                 status = mission_control_get_connection_status (window->mc,
1233                                                                 account,
1234                                                                 NULL);
1235
1236                 if (status == 0) {
1237                         connected++;
1238                 } else if (status == 1) {
1239                         connecting++;
1240                 } else if (status == 2) {
1241                         disconnected++;
1242                 }
1243
1244                 g_object_unref (account);
1245         }
1246         g_list_free (accounts);
1247
1248         /* Update the spinner state */
1249         if (connecting > 0) {
1250                 ephy_spinner_start (EPHY_SPINNER (window->throbber));
1251         } else {
1252                 ephy_spinner_stop (EPHY_SPINNER (window->throbber));
1253         }
1254
1255         /* Update widgets sensibility */
1256         for (l = window->widgets_connected; l; l = l->next) {
1257                 gtk_widget_set_sensitive (l->data, (connected > 0));
1258         }
1259
1260         for (l = window->widgets_disconnected; l; l = l->next) {
1261                 gtk_widget_set_sensitive (l->data, (disconnected > 0));
1262         }
1263 }
1264
1265 /*
1266  * Accels
1267  */
1268 static void
1269 main_window_accels_load (void)
1270 {
1271         gchar *filename;
1272
1273         filename = g_build_filename (g_get_home_dir (), ".gnome2", PACKAGE_NAME, ACCELS_FILENAME, NULL);
1274         if (g_file_test (filename, G_FILE_TEST_EXISTS)) {
1275                 DEBUG ("Loading from:'%s'", filename);
1276                 gtk_accel_map_load (filename);
1277         }
1278
1279         g_free (filename);
1280 }
1281
1282 static void
1283 main_window_accels_save (void)
1284 {
1285         gchar *dir;
1286         gchar *file_with_path;
1287
1288         dir = g_build_filename (g_get_home_dir (), ".gnome2", PACKAGE_NAME, NULL);
1289         g_mkdir_with_parents (dir, S_IRUSR | S_IWUSR | S_IXUSR);
1290         file_with_path = g_build_filename (dir, ACCELS_FILENAME, NULL);
1291         g_free (dir);
1292
1293         DEBUG ("Saving to:'%s'", file_with_path);
1294         gtk_accel_map_save (file_with_path);
1295
1296         g_free (file_with_path);
1297 }
1298
1299 static void
1300 main_window_connection_items_setup (EmpathyMainWindow *window,
1301                                     GladeXML          *glade)
1302 {
1303         GList         *list;
1304         GtkWidget     *w;
1305         gint           i;
1306         const gchar *widgets_connected[] = {
1307                 "room",
1308                 "chat_new_message",
1309                 "chat_add_contact",
1310                 "edit_personal_information"
1311         };
1312         const gchar *widgets_disconnected[] = {
1313         };
1314
1315         for (i = 0, list = NULL; i < G_N_ELEMENTS (widgets_connected); i++) {
1316                 w = glade_xml_get_widget (glade, widgets_connected[i]);
1317                 list = g_list_prepend (list, w);
1318         }
1319
1320         window->widgets_connected = list;
1321
1322         for (i = 0, list = NULL; i < G_N_ELEMENTS (widgets_disconnected); i++) {
1323                 w = glade_xml_get_widget (glade, widgets_disconnected[i]);
1324                 list = g_list_prepend (list, w);
1325         }
1326
1327         window->widgets_disconnected = list;
1328 }
1329
1330 static gboolean
1331 main_window_configure_event_timeout_cb (EmpathyMainWindow *window)
1332 {
1333         gint x, y, w, h;
1334
1335         gtk_window_get_size (GTK_WINDOW (window->window), &w, &h);
1336         gtk_window_get_position (GTK_WINDOW (window->window), &x, &y);
1337
1338         empathy_geometry_save (GEOMETRY_NAME, x, y, w, h);
1339
1340         window->size_timeout_id = 0;
1341
1342         return FALSE;
1343 }
1344
1345 static gboolean
1346 main_window_configure_event_cb (GtkWidget         *widget,
1347                                 GdkEventConfigure *event,
1348                                 EmpathyMainWindow *window)
1349 {
1350         if (window->size_timeout_id) {
1351                 g_source_remove (window->size_timeout_id);
1352         }
1353
1354         window->size_timeout_id = g_timeout_add_seconds (1,
1355                                                          (GSourceFunc) main_window_configure_event_timeout_cb,
1356                                                          window);
1357
1358         return FALSE;
1359 }
1360
1361 static void
1362 main_window_notify_show_offline_cb (EmpathyConf  *conf,
1363                                     const gchar *key,
1364                                     gpointer     check_menu_item)
1365 {
1366         gboolean show_offline;
1367
1368         if (empathy_conf_get_bool (conf, key, &show_offline)) {
1369                 gtk_check_menu_item_set_active (GTK_CHECK_MENU_ITEM (check_menu_item),
1370                                                 show_offline);
1371         }
1372 }
1373
1374 static void
1375 main_window_notify_show_avatars_cb (EmpathyConf        *conf,
1376                                     const gchar       *key,
1377                                     EmpathyMainWindow *window)
1378 {
1379         gboolean show_avatars;
1380
1381         if (empathy_conf_get_bool (conf, key, &show_avatars)) {
1382                 empathy_contact_list_store_set_show_avatars (window->list_store,
1383                                                             show_avatars);
1384         }
1385 }
1386
1387 static void
1388 main_window_notify_compact_contact_list_cb (EmpathyConf        *conf,
1389                                             const gchar       *key,
1390                                             EmpathyMainWindow *window)
1391 {
1392         gboolean compact_contact_list;
1393
1394         if (empathy_conf_get_bool (conf, key, &compact_contact_list)) {
1395                 empathy_contact_list_store_set_is_compact (window->list_store,
1396                                                           compact_contact_list);
1397         }
1398 }
1399
1400 static void
1401 main_window_notify_sort_criterium_cb (EmpathyConf       *conf,
1402                                       const gchar       *key,
1403                                       EmpathyMainWindow *window)
1404 {
1405         gchar *str = NULL;
1406
1407         if (empathy_conf_get_string (conf, key, &str) && str) {
1408                 GType       type;
1409                 GEnumClass *enum_class;
1410                 GEnumValue *enum_value;
1411
1412                 type = empathy_contact_list_store_sort_get_type ();
1413                 enum_class = G_ENUM_CLASS (g_type_class_peek (type));
1414                 enum_value = g_enum_get_value_by_nick (enum_class, str);
1415                 g_free (str);
1416
1417                 if (enum_value) {
1418                         empathy_contact_list_store_set_sort_criterium (window->list_store, 
1419                                                                        enum_value->value);
1420                 }
1421         }
1422 }
1423