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