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