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