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