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