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