]> git.0d.be Git - empathy.git/blob - src/empathy-main-window.c
Added notification for confs Show avatar, Compact Size and Sort criterium
[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_size;
101         GtkRadioAction         *normal_without_icons;
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_home_dir (), ".gnome2", 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_home_dir (), ".gnome2", 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_view_sort_contacts_cb (GtkRadioAction    *action,
728                                    GtkRadioAction    *current,
729                                    EmpathyMainWindow *window)
730 {
731         EmpathyContactListStoreSort value;
732         const gchar *valueStr = NULL;
733
734         value = gtk_radio_action_get_current_value (action);
735
736         GSList      *group;
737         GType        type;
738         GEnumClass  *enum_class;
739         GEnumValue  *enum_value;
740
741         group = gtk_radio_action_get_group (action);
742
743         /* Get string from index */
744         type = empathy_contact_list_store_sort_get_type ();
745         enum_class = G_ENUM_CLASS (g_type_class_peek (type));
746         enum_value = g_enum_get_value (enum_class, g_slist_index (group, current));
747
748         if (!enum_value) {
749                 g_warning ("No GEnumValue for EmpathyContactListSort with GtkRadioButton index:%d",
750                            g_slist_index (group, action));
751         } else {
752                 valueStr = enum_value->value_nick;
753
754                 empathy_conf_set_string (empathy_conf_get (),
755                                          EMPATHY_PREFS_CONTACTS_SORT_CRITERIUM,
756                                          valueStr);
757         }
758         empathy_contact_list_store_set_sort_criterium (window->list_store, value);
759 }
760
761 /* Matches GtkRadioAction values set in empathy-main-window.ui */
762 #define CONTACT_LIST_NORMAL_SIZE                0
763 #define CONTACT_LIST_NORMAL_WITHOUT_ICONS       1
764 #define CONTACT_LIST_COMPACT_SIZE               2
765
766 static void
767 main_window_view_contacts_list_size_cb (GtkRadioAction    *action,
768                                         GtkRadioAction    *current,
769                                         EmpathyMainWindow *window)
770 {
771         gint     value;
772
773         value = gtk_radio_action_get_current_value (action);
774
775         empathy_conf_set_bool (empathy_conf_get (),
776                                EMPATHY_PREFS_UI_SHOW_AVATARS,
777                                value == CONTACT_LIST_NORMAL_SIZE);
778         empathy_conf_set_bool (empathy_conf_get (),
779                                EMPATHY_PREFS_UI_COMPACT_CONTACT_LIST,
780                                value == CONTACT_LIST_COMPACT_SIZE);
781
782         empathy_contact_list_store_set_show_avatars (window->list_store,
783                                                      value == CONTACT_LIST_NORMAL_SIZE);
784         empathy_contact_list_store_set_is_compact (window->list_store,
785                                                    value == CONTACT_LIST_COMPACT_SIZE);
786 }
787
788 static void
789 main_window_view_show_map_cb (GtkCheckMenuItem  *item,
790                               EmpathyMainWindow *window)
791 {
792 #if HAVE_LIBCHAMPLAIN
793         empathy_map_view_show ();
794 #endif
795 }
796
797 static void
798 main_window_favorite_chatroom_join (EmpathyChatroom *chatroom)
799 {
800         EmpathyAccount *account;
801         TpConnection   *connection;
802         const gchar    *room;
803
804         account = empathy_chatroom_get_account (chatroom);
805         connection = empathy_account_get_connection (account);
806         room = empathy_chatroom_get_room (chatroom);
807
808         if (connection != NULL) {
809                 DEBUG ("Requesting channel for '%s'", room);
810                 empathy_dispatcher_join_muc (connection, room, NULL, NULL);
811         }
812 }
813
814 static void
815 main_window_favorite_chatroom_menu_activate_cb (GtkMenuItem    *menu_item,
816                                                 EmpathyChatroom *chatroom)
817 {
818         main_window_favorite_chatroom_join (chatroom);
819 }
820
821 static void
822 main_window_favorite_chatroom_menu_add (EmpathyMainWindow *window,
823                                         EmpathyChatroom    *chatroom)
824 {
825         GtkWidget   *menu_item;
826         const gchar *name;
827
828         if (g_object_get_data (G_OBJECT (chatroom), "menu_item")) {
829                 return;
830         }
831
832         name = empathy_chatroom_get_name (chatroom);
833         menu_item = gtk_menu_item_new_with_label (name);
834
835         g_object_set_data (G_OBJECT (chatroom), "menu_item", menu_item);
836         g_signal_connect (menu_item, "activate",
837                           G_CALLBACK (main_window_favorite_chatroom_menu_activate_cb),
838                           chatroom);
839
840         gtk_menu_shell_insert (GTK_MENU_SHELL (window->room_menu),
841                                menu_item, 4);
842
843         gtk_widget_show (menu_item);
844 }
845
846 static void
847 main_window_favorite_chatroom_menu_added_cb (EmpathyChatroomManager *manager,
848                                              EmpathyChatroom        *chatroom,
849                                              EmpathyMainWindow     *window)
850 {
851         main_window_favorite_chatroom_menu_add (window, chatroom);
852         gtk_widget_show (window->room_separator);
853         gtk_action_set_sensitive (window->room_join_favorites, TRUE);
854 }
855
856 static void
857 main_window_favorite_chatroom_menu_removed_cb (EmpathyChatroomManager *manager,
858                                                EmpathyChatroom        *chatroom,
859                                                EmpathyMainWindow     *window)
860 {
861         GtkWidget *menu_item;
862         GList *chatrooms;
863
864         menu_item = g_object_get_data (G_OBJECT (chatroom), "menu_item");
865         g_object_set_data (G_OBJECT (chatroom), "menu_item", NULL);
866         gtk_widget_destroy (menu_item);
867
868         chatrooms = empathy_chatroom_manager_get_chatrooms (window->chatroom_manager, NULL);
869         if (chatrooms) {
870                 gtk_widget_show (window->room_separator);
871         } else {
872                 gtk_widget_hide (window->room_separator);
873         }
874
875         gtk_action_set_sensitive (window->room_join_favorites, chatrooms != NULL);
876         g_list_free (chatrooms);
877 }
878
879 static void
880 main_window_favorite_chatroom_menu_setup (EmpathyMainWindow *window)
881 {
882         GList *chatrooms, *l;
883         GtkWidget *room;
884
885         window->chatroom_manager = empathy_chatroom_manager_dup_singleton (NULL);
886         chatrooms = empathy_chatroom_manager_get_chatrooms (window->chatroom_manager, NULL);
887         room = gtk_ui_manager_get_widget (window->ui_manager,
888                 "/menubar/room");
889         window->room_menu = gtk_menu_item_get_submenu (GTK_MENU_ITEM (room));
890         window->room_separator = gtk_ui_manager_get_widget (window->ui_manager,
891                 "/menubar/room/room_separator");
892
893         for (l = chatrooms; l; l = l->next) {
894                 main_window_favorite_chatroom_menu_add (window, l->data);
895         }
896
897         if (!chatrooms) {
898                 gtk_widget_hide (window->room_separator);
899         }
900
901         gtk_action_set_sensitive (window->room_join_favorites, chatrooms != NULL);
902
903         g_signal_connect (window->chatroom_manager, "chatroom-added",
904                           G_CALLBACK (main_window_favorite_chatroom_menu_added_cb),
905                           window);
906         g_signal_connect (window->chatroom_manager, "chatroom-removed",
907                           G_CALLBACK (main_window_favorite_chatroom_menu_removed_cb),
908                           window);
909
910         g_list_free (chatrooms);
911 }
912
913 static void
914 main_window_room_join_new_cb (GtkAction         *action,
915                               EmpathyMainWindow *window)
916 {
917         empathy_new_chatroom_dialog_show (GTK_WINDOW (window->window));
918 }
919
920 static void
921 main_window_room_join_favorites_cb (GtkAction         *action,
922                                     EmpathyMainWindow *window)
923 {
924         GList *chatrooms, *l;
925
926         chatrooms = empathy_chatroom_manager_get_chatrooms (window->chatroom_manager, NULL);
927         for (l = chatrooms; l; l = l->next) {
928                 main_window_favorite_chatroom_join (l->data);
929         }
930         g_list_free (chatrooms);
931 }
932
933 static void
934 main_window_room_manage_favorites_cb (GtkAction         *action,
935                                       EmpathyMainWindow *window)
936 {
937         empathy_chatrooms_window_show (GTK_WINDOW (window->window));
938 }
939
940 static void
941 main_window_edit_cb (GtkAction *action,
942                      EmpathyMainWindow *window)
943 {
944         GtkWidget *submenu;
945
946         /* FIXME: It should use the UIManager to merge the contact/group submenu */
947         submenu = empathy_contact_list_view_get_contact_menu (window->list_view);
948         if (submenu) {
949                 GtkMenuItem *item;
950                 GtkWidget   *label;
951
952                 item = GTK_MENU_ITEM (window->edit_context);
953                 label = gtk_bin_get_child (GTK_BIN (item));
954                 gtk_label_set_text (GTK_LABEL (label), _("Contact"));
955
956                 gtk_widget_show (window->edit_context);
957                 gtk_widget_show (window->edit_context_separator);
958
959                 gtk_menu_item_set_submenu (item, submenu);
960
961                 return;
962         }
963
964         submenu = empathy_contact_list_view_get_group_menu (window->list_view);
965         if (submenu) {
966                 GtkMenuItem *item;
967                 GtkWidget   *label;
968
969                 item = GTK_MENU_ITEM (window->edit_context);
970                 label = gtk_bin_get_child (GTK_BIN (item));
971                 gtk_label_set_text (GTK_LABEL (label), _("Group"));
972
973                 gtk_widget_show (window->edit_context);
974                 gtk_widget_show (window->edit_context_separator);
975
976                 gtk_menu_item_set_submenu (item, submenu);
977
978                 return;
979         }
980
981         gtk_widget_hide (window->edit_context);
982         gtk_widget_hide (window->edit_context_separator);
983
984         return;
985 }
986
987 static void
988 main_window_edit_accounts_cb (GtkAction         *action,
989                               EmpathyMainWindow *window)
990 {
991         empathy_accounts_dialog_show (GTK_WINDOW (window->window), NULL);
992 }
993
994 static void
995 main_window_edit_personal_information_cb (GtkAction         *action,
996                                           EmpathyMainWindow *window)
997 {
998         empathy_contact_personal_dialog_show (GTK_WINDOW (window->window));
999 }
1000
1001 static void
1002 main_window_edit_preferences_cb (GtkAction         *action,
1003                                  EmpathyMainWindow *window)
1004 {
1005         empathy_preferences_show (GTK_WINDOW (window->window));
1006 }
1007
1008 static void
1009 main_window_help_about_cb (GtkAction         *action,
1010                            EmpathyMainWindow *window)
1011 {
1012         empathy_about_dialog_new (GTK_WINDOW (window->window));
1013 }
1014
1015 static void
1016 main_window_help_debug_cb (GtkAction         *action,
1017                            EmpathyMainWindow *window)
1018 {
1019         empathy_debug_dialog_new (GTK_WINDOW (window->window));
1020 }
1021
1022 static void
1023 main_window_help_contents_cb (GtkAction         *action,
1024                               EmpathyMainWindow *window)
1025 {
1026         empathy_url_show (window->window, "ghelp:empathy");
1027 }
1028
1029 static gboolean
1030 main_window_throbber_button_press_event_cb (GtkWidget         *throbber_ebox,
1031                                             GdkEventButton    *event,
1032                                             EmpathyMainWindow *window)
1033 {
1034         if (event->type != GDK_BUTTON_PRESS ||
1035             event->button != 1) {
1036                 return FALSE;
1037         }
1038
1039         empathy_accounts_dialog_show (GTK_WINDOW (window->window), NULL);
1040
1041         return FALSE;
1042 }
1043
1044 static gboolean
1045 main_window_configure_event_timeout_cb (EmpathyMainWindow *window)
1046 {
1047         gint x, y, w, h;
1048
1049         gtk_window_get_size (GTK_WINDOW (window->window), &w, &h);
1050         gtk_window_get_position (GTK_WINDOW (window->window), &x, &y);
1051
1052         empathy_geometry_save (GEOMETRY_NAME, x, y, w, h);
1053
1054         window->size_timeout_id = 0;
1055
1056         return FALSE;
1057 }
1058
1059 static gboolean
1060 main_window_configure_event_cb (GtkWidget         *widget,
1061                                 GdkEventConfigure *event,
1062                                 EmpathyMainWindow *window)
1063 {
1064         if (window->size_timeout_id) {
1065                 g_source_remove (window->size_timeout_id);
1066         }
1067
1068         window->size_timeout_id = g_timeout_add_seconds (1,
1069                                                          (GSourceFunc) main_window_configure_event_timeout_cb,
1070                                                          window);
1071
1072         return FALSE;
1073 }
1074
1075 static void
1076 main_window_account_created_or_deleted_cb (EmpathyAccountManager  *manager,
1077                                            EmpathyAccount         *account,
1078                                            EmpathyMainWindow      *window)
1079 {
1080         gtk_action_set_sensitive (window->view_history,
1081                 empathy_account_manager_get_count (manager) > 0);
1082 }
1083
1084 static void
1085 main_window_notify_show_offline_cb (EmpathyConf *conf,
1086                                     const gchar *key,
1087                                     gpointer     toggle_action)
1088 {
1089         gboolean show_offline;
1090
1091         if (empathy_conf_get_bool (conf, key, &show_offline)) {
1092                 gtk_toggle_action_set_active (toggle_action, show_offline);
1093         }
1094 }
1095
1096 static void
1097 main_window_notify_contact_list_size_cb (EmpathyConf       *conf,
1098                                          const gchar       *key,
1099                                          EmpathyMainWindow *window)
1100 {
1101         gboolean show_avatars;
1102         gboolean compact_contact_list;
1103         gint value = CONTACT_LIST_NORMAL_SIZE;
1104
1105         if (empathy_conf_get_bool (conf,
1106                                    EMPATHY_PREFS_UI_SHOW_AVATARS,
1107                                    &show_avatars)
1108             && empathy_conf_get_bool (conf,
1109                                       EMPATHY_PREFS_UI_COMPACT_CONTACT_LIST,
1110                                       &compact_contact_list)) {
1111                 if (compact_contact_list) {
1112                         value = CONTACT_LIST_COMPACT_SIZE;
1113                 } else if (show_avatars) {
1114                         value = CONTACT_LIST_NORMAL_SIZE;
1115                 } else {
1116                         value = CONTACT_LIST_NORMAL_WITHOUT_ICONS;
1117                 }
1118         }
1119         gtk_radio_action_set_current_value (window->normal_size, value);
1120 }
1121
1122
1123 /* Matches GtkRadioAction values set in empathy-main-window.ui */
1124 #define CONTACT_LIST_SORT_BY_STATUS             0
1125 #define CONTACT_LIST_SORT_BY_NAME               1
1126
1127 static void
1128 main_window_notify_sort_contact_cb (EmpathyConf       *conf,
1129                                     const gchar       *key,
1130                                     EmpathyMainWindow *window)
1131 {
1132         gchar *str = NULL;
1133
1134         if (empathy_conf_get_string (conf, key, &str) && str) {
1135                 GType       type;
1136                 GEnumClass *enum_class;
1137                 GEnumValue *enum_value;
1138
1139                 type = empathy_contact_list_store_sort_get_type ();
1140                 enum_class = G_ENUM_CLASS (g_type_class_peek (type));
1141                 enum_value = g_enum_get_value_by_nick (enum_class, str);
1142
1143                 if (enum_value) {
1144                         if (enum_value->value == EMPATHY_CONTACT_LIST_STORE_SORT_STATE) {
1145                                 gtk_radio_action_set_current_value (window->sort_by_name,
1146                                                                     CONTACT_LIST_SORT_BY_STATUS);
1147                         } else {
1148                                 gtk_radio_action_set_current_value (window->sort_by_name,
1149                                                                     CONTACT_LIST_SORT_BY_NAME);
1150                         }
1151                 } else {
1152                         g_warning ("Wrong value for sort_criterium configuration : %s", str);
1153                 }
1154                 g_free (str);
1155         }
1156 }
1157
1158 static void
1159 main_window_connection_items_setup (EmpathyMainWindow *window,
1160                                     GtkBuilder        *gui)
1161 {
1162         GList         *list;
1163         GObject       *action;
1164         gint           i;
1165         const gchar *actions_connected[] = {
1166                 "room",
1167                 "chat_new_message",
1168                 "chat_add_contact",
1169                 "edit_personal_information"
1170         };
1171
1172         for (i = 0, list = NULL; i < G_N_ELEMENTS (actions_connected); i++) {
1173                 action = gtk_builder_get_object (gui, actions_connected[i]);
1174                 list = g_list_prepend (list, action);
1175         }
1176
1177         window->actions_connected = list;
1178 }
1179
1180 GtkWidget *
1181 empathy_main_window_get (void)
1182 {
1183   return window != NULL ? window->window : NULL;
1184 }
1185
1186 GtkWidget *
1187 empathy_main_window_show (void)
1188 {
1189         EmpathyContactList       *list_iface;
1190         EmpathyContactMonitor    *monitor;
1191         GtkBuilder               *gui;
1192         EmpathyConf              *conf;
1193         GtkWidget                *sw;
1194         GtkToggleAction          *show_offline_widget;
1195         GtkWidget                *ebox;
1196         GtkAction                *show_map_widget;
1197         GtkToolItem              *item;
1198         gboolean                  show_offline;
1199         gint                      x, y, w, h;
1200         gchar                    *filename;
1201         GSList                   *l;
1202
1203         if (window) {
1204                 empathy_window_present (GTK_WINDOW (window->window), TRUE);
1205                 return window->window;
1206         }
1207
1208         window = g_new0 (EmpathyMainWindow, 1);
1209
1210         /* Set up interface */
1211         filename = empathy_file_lookup ("empathy-main-window.ui", "src");
1212         gui = empathy_builder_get_file (filename,
1213                                        "main_window", &window->window,
1214                                        "main_vbox", &window->main_vbox,
1215                                        "errors_vbox", &window->errors_vbox,
1216                                        "ui_manager", &window->ui_manager,
1217                                        "view_show_offline", &show_offline_widget,
1218                                        "view_sort_by_name", &window->sort_by_name,
1219                                        "view_sort_by_status", &window->sort_by_status,
1220                                        "view_normal_size", &window->normal_size,
1221                                        "view_normal_without_icons", &window->normal_without_icons,
1222                                        "view_compact_size", &window->compact_size,
1223                                        "view_history", &window->view_history,
1224                                        "view_show_map", &show_map_widget,
1225                                        "room_join_favorites", &window->room_join_favorites,
1226                                        "presence_toolbar", &window->presence_toolbar,
1227                                        "roster_scrolledwindow", &sw,
1228                                        NULL);
1229         g_free (filename);
1230
1231         empathy_builder_connect (gui, window,
1232                               "main_window", "destroy", main_window_destroy_cb,
1233                               "main_window", "configure_event", main_window_configure_event_cb,
1234                               "chat_quit", "activate", main_window_chat_quit_cb,
1235                               "chat_new_message", "activate", main_window_chat_new_message_cb,
1236                               "view_history", "activate", main_window_view_history_cb,
1237                               "room_join_new", "activate", main_window_room_join_new_cb,
1238                               "room_join_favorites", "activate", main_window_room_join_favorites_cb,
1239                               "room_manage_favorites", "activate", main_window_room_manage_favorites_cb,
1240                               "chat_add_contact", "activate", main_window_chat_add_contact_cb,
1241                               "view_show_ft_manager", "activate", main_window_view_show_ft_manager,
1242                               "view_show_offline", "toggled", main_window_view_show_offline_cb,
1243                               "view_sort_by_name", "changed", main_window_view_sort_contacts_cb,
1244                               "view_normal_size", "changed", main_window_view_contacts_list_size_cb,
1245                               "view_show_map", "activate", main_window_view_show_map_cb,
1246                               "edit", "activate", main_window_edit_cb,
1247                               "edit_accounts", "activate", main_window_edit_accounts_cb,
1248                               "edit_personal_information", "activate", main_window_edit_personal_information_cb,
1249                               "edit_preferences", "activate", main_window_edit_preferences_cb,
1250                               "help_about", "activate", main_window_help_about_cb,
1251                               "help_debug", "activate", main_window_help_debug_cb,
1252                               "help_contents", "activate", main_window_help_contents_cb,
1253                               NULL);
1254
1255         /* Set up connection related widgets. */
1256         main_window_connection_items_setup (window, gui);
1257
1258         g_object_ref (window->ui_manager);
1259         g_object_unref (gui);
1260
1261 #if !HAVE_LIBCHAMPLAIN
1262         gtk_action_set_visible (show_map_widget, FALSE);
1263 #endif
1264
1265         window->mc = empathy_mission_control_dup_singleton ();
1266         window->account_manager = empathy_account_manager_dup_singleton ();
1267
1268         g_signal_connect (window->account_manager,
1269                           "account-connection-changed",
1270                           G_CALLBACK (main_window_connection_changed_cb), window);
1271
1272         window->errors = g_hash_table_new_full (empathy_account_hash,
1273                                                 empathy_account_equal,
1274                                                 g_object_unref,
1275                                                 NULL);
1276
1277         /* Set up menu */
1278         main_window_favorite_chatroom_menu_setup (window);
1279
1280         window->edit_context = gtk_ui_manager_get_widget (window->ui_manager,
1281                 "/menubar/edit/edit_context");
1282         window->edit_context_separator = gtk_ui_manager_get_widget (window->ui_manager,
1283                 "/menubar/edit/edit_context_separator");
1284         gtk_widget_hide (window->edit_context);
1285         gtk_widget_hide (window->edit_context_separator);
1286
1287         /* Set up contact list. */
1288         empathy_status_presets_get_all ();
1289
1290         /* Set up presence chooser */
1291         window->presence_chooser = empathy_presence_chooser_new ();
1292         gtk_widget_show (window->presence_chooser);
1293         item = gtk_tool_item_new ();
1294         gtk_widget_show (GTK_WIDGET (item));
1295         gtk_container_add (GTK_CONTAINER (item), window->presence_chooser);
1296         gtk_tool_item_set_is_important (item, TRUE);
1297         gtk_tool_item_set_expand (item, TRUE);
1298         gtk_toolbar_insert (GTK_TOOLBAR (window->presence_toolbar), item, -1);
1299
1300         /* Set up the throbber */
1301         ebox = gtk_event_box_new ();
1302         gtk_event_box_set_visible_window (GTK_EVENT_BOX (ebox), FALSE);
1303         gtk_widget_set_tooltip_text (ebox, _("Show and edit accounts"));
1304         g_signal_connect (ebox,
1305                           "button-press-event",
1306                           G_CALLBACK (main_window_throbber_button_press_event_cb),
1307                           window);
1308         gtk_widget_show (ebox);
1309
1310         window->throbber = ephy_spinner_new ();
1311         ephy_spinner_set_size (EPHY_SPINNER (window->throbber), GTK_ICON_SIZE_LARGE_TOOLBAR);
1312         gtk_container_add (GTK_CONTAINER (ebox), window->throbber);
1313         gtk_widget_show (window->throbber);
1314
1315         item = gtk_tool_item_new ();
1316         gtk_container_add (GTK_CONTAINER (item), ebox);
1317         gtk_toolbar_insert (GTK_TOOLBAR (window->presence_toolbar), item, -1);
1318         gtk_widget_show (GTK_WIDGET (item));
1319
1320         list_iface = EMPATHY_CONTACT_LIST (empathy_contact_manager_dup_singleton ());
1321         monitor = empathy_contact_list_get_monitor (list_iface);
1322         window->list_store = empathy_contact_list_store_new (list_iface);
1323         window->list_view = empathy_contact_list_view_new (window->list_store,
1324                                                            EMPATHY_CONTACT_LIST_FEATURE_ALL,
1325                                                            EMPATHY_CONTACT_FEATURE_ALL);
1326         g_signal_connect (monitor, "contact-presence-changed",
1327                           G_CALLBACK (main_window_contact_presence_changed_cb), window);
1328         g_object_unref (list_iface);
1329
1330         gtk_widget_show (GTK_WIDGET (window->list_view));
1331         gtk_container_add (GTK_CONTAINER (sw),
1332                            GTK_WIDGET (window->list_view));
1333         g_signal_connect (window->list_view, "row-activated",
1334                           G_CALLBACK (main_window_row_activated_cb),
1335                           window);
1336
1337         /* Load user-defined accelerators. */
1338         main_window_accels_load ();
1339
1340         /* Set window size. */
1341         empathy_geometry_load (GEOMETRY_NAME, &x, &y, &w, &h);
1342
1343         if (w >= 1 && h >= 1) {
1344                 /* Use the defaults from the ui file if we
1345                  * don't have good w, h geometry.
1346                  */
1347                 DEBUG ("Configuring window default size w:%d, h:%d", w, h);
1348                 gtk_window_set_default_size (GTK_WINDOW (window->window), w, h);
1349         }
1350
1351         if (x >= 0 && y >= 0) {
1352                 /* Let the window manager position it if we
1353                  * don't have good x, y coordinates.
1354                  */
1355                 DEBUG ("Configuring window default position x:%d, y:%d", x, y);
1356                 gtk_window_move (GTK_WINDOW (window->window), x, y);
1357         }
1358
1359         /* Enable event handling */
1360         window->event_manager = empathy_event_manager_dup_singleton ();
1361         g_signal_connect (window->event_manager, "event-added",
1362                           G_CALLBACK (main_window_event_added_cb),
1363                           window);
1364         g_signal_connect (window->event_manager, "event-removed",
1365                           G_CALLBACK (main_window_event_removed_cb),
1366                           window);
1367
1368         g_signal_connect (window->account_manager, "account-created",
1369                           G_CALLBACK (main_window_account_created_or_deleted_cb),
1370                           window);
1371         g_signal_connect (window->account_manager, "account-deleted",
1372                           G_CALLBACK (main_window_account_created_or_deleted_cb),
1373                           window);
1374         main_window_account_created_or_deleted_cb (window->account_manager, NULL, window);
1375
1376         l = empathy_event_manager_get_events (window->event_manager);
1377         while (l) {
1378                 main_window_event_added_cb (window->event_manager,
1379                                             l->data, window);
1380                 l = l->next;
1381         }
1382
1383         conf = empathy_conf_get ();
1384
1385         /* Show offline ? */
1386         empathy_conf_get_bool (conf,
1387                               EMPATHY_PREFS_CONTACTS_SHOW_OFFLINE,
1388                               &show_offline);
1389         empathy_conf_notify_add (conf,
1390                                 EMPATHY_PREFS_CONTACTS_SHOW_OFFLINE,
1391                                 main_window_notify_show_offline_cb,
1392                                 show_offline_widget);
1393
1394         gtk_toggle_action_set_active (show_offline_widget, show_offline);
1395
1396         /* Sort by name / by status ? */
1397         empathy_conf_notify_add (conf,
1398                                  EMPATHY_PREFS_CONTACTS_SORT_CRITERIUM,
1399                                  (EmpathyConfNotifyFunc) main_window_notify_sort_contact_cb,
1400                                  window);
1401
1402         main_window_notify_sort_contact_cb (conf,
1403                                             EMPATHY_PREFS_CONTACTS_SORT_CRITERIUM,
1404                                             window);
1405
1406         /* Contacts list size */
1407         empathy_conf_notify_add (conf,
1408                                  EMPATHY_PREFS_UI_COMPACT_CONTACT_LIST,
1409                                  (EmpathyConfNotifyFunc) main_window_notify_contact_list_size_cb,
1410                                  window);
1411         empathy_conf_notify_add (conf,
1412                                  EMPATHY_PREFS_UI_SHOW_AVATARS,
1413                                  (EmpathyConfNotifyFunc) main_window_notify_contact_list_size_cb,
1414                                  window);
1415
1416         main_window_notify_contact_list_size_cb (conf,
1417                                                  EMPATHY_PREFS_UI_SHOW_AVATARS,
1418                                                  window);
1419
1420         main_window_update_status (window, window->account_manager);
1421
1422         return window->window;
1423 }
1424