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