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