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