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