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