]> git.0d.be Git - empathy.git/blob - src/empathy-main-window.c
Merge branch 'people-nearby-fake-group-613558'
[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-2010 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 #include "empathy-migrate-butterfly-logs.h"
67
68 #define DEBUG_FLAG EMPATHY_DEBUG_OTHER
69 #include <libempathy/empathy-debug.h>
70
71 /* Flashing delay for icons (milliseconds). */
72 #define FLASH_TIMEOUT 500
73
74 /* Minimum width of roster window if something goes wrong. */
75 #define MIN_WIDTH 50
76
77 /* Accels (menu shortcuts) can be configured and saved */
78 #define ACCELS_FILENAME "accels.txt"
79
80 /* Name in the geometry file */
81 #define GEOMETRY_NAME "main-window"
82
83 typedef struct {
84         EmpathyContactListView  *list_view;
85         EmpathyContactListStore *list_store;
86         TpAccountManager        *account_manager;
87         EmpathyChatroomManager  *chatroom_manager;
88         EmpathyEventManager     *event_manager;
89         guint                    flash_timeout_id;
90         gboolean                 flash_on;
91
92         GtkWidget              *window;
93         GtkWidget              *main_vbox;
94         GtkWidget              *throbber;
95         GtkWidget              *presence_toolbar;
96         GtkWidget              *presence_chooser;
97         GtkWidget              *errors_vbox;
98
99         GtkToggleAction        *show_protocols;
100         GtkRadioAction         *sort_by_name;
101         GtkRadioAction         *sort_by_status;
102         GtkRadioAction         *normal_with_avatars;
103         GtkRadioAction         *normal_size;
104         GtkRadioAction         *compact_size;
105
106         GtkUIManager           *ui_manager;
107         GtkAction              *view_history;
108         GtkAction              *room_join_favorites;
109         GtkWidget              *room_menu;
110         GtkWidget              *room_separator;
111         GtkWidget              *edit_context;
112         GtkWidget              *edit_context_separator;
113
114         guint                   size_timeout_id;
115         GHashTable             *errors;
116
117         /* stores a mapping from TpAccount to Handler ID to prevent
118          * to listen more than once to the status-changed signal */
119         GHashTable             *status_changed_handlers;
120
121         /* Actions that are enabled when there are connected accounts */
122         GList                  *actions_connected;
123
124         /* The idle event source to migrate butterfly's logs */
125         guint butterfly_log_migration_contact_added_id;
126 } EmpathyMainWindow;
127
128 static EmpathyMainWindow *main_window = NULL;
129
130 static void
131 main_window_flash_stop (EmpathyMainWindow *window)
132 {
133         if (window->flash_timeout_id == 0) {
134                 return;
135         }
136
137         DEBUG ("Stop flashing");
138         g_source_remove (window->flash_timeout_id);
139         window->flash_timeout_id = 0;
140         window->flash_on = FALSE;
141 }
142
143 typedef struct {
144         EmpathyEvent       *event;
145         gboolean            on;
146         EmpathyMainWindow  *window;
147 } FlashForeachData;
148
149 static gboolean
150 main_window_flash_foreach (GtkTreeModel *model,
151                            GtkTreePath  *path,
152                            GtkTreeIter  *iter,
153                            gpointer      user_data)
154 {
155         FlashForeachData *data = (FlashForeachData *) user_data;
156         EmpathyContact   *contact;
157         const gchar      *icon_name;
158         GtkTreePath      *parent_path = NULL;
159         GtkTreeIter       parent_iter;
160         GdkPixbuf        *pixbuf = NULL;
161
162         /* To be used with gtk_tree_model_foreach, update the status icon
163          * of the contact to show the event icon (on=TRUE) or the presence
164          * (on=FALSE) */
165         gtk_tree_model_get (model, iter,
166                             EMPATHY_CONTACT_LIST_STORE_COL_CONTACT, &contact,
167                             -1);
168
169         if (contact != data->event->contact) {
170                 if (contact) {
171                         g_object_unref (contact);
172                 }
173                 return FALSE;
174         }
175
176         if (data->on) {
177                 icon_name = data->event->icon_name;
178                 pixbuf = empathy_pixbuf_from_icon_name (icon_name, GTK_ICON_SIZE_MENU);
179         } else {
180                 pixbuf = contact_list_store_get_contact_status_icon (
181                                                 data->window->list_store,
182                                                 contact);
183         }
184
185         gtk_tree_store_set (GTK_TREE_STORE (model), iter,
186                             EMPATHY_CONTACT_LIST_STORE_COL_ICON_STATUS, pixbuf,
187                             -1);
188
189         /* To make sure the parent is shown correctly, we emit
190          * the row-changed signal on the parent so it prompts
191          * it to be refreshed by the filter func.
192          */
193         if (gtk_tree_model_iter_parent (model, &parent_iter, iter)) {
194                 parent_path = gtk_tree_model_get_path (model, &parent_iter);
195         }
196         if (parent_path) {
197                 gtk_tree_model_row_changed (model, parent_path, &parent_iter);
198                 gtk_tree_path_free (parent_path);
199         }
200
201         g_object_unref (contact);
202
203         return FALSE;
204 }
205
206 static gboolean
207 main_window_flash_cb (EmpathyMainWindow *window)
208 {
209         GtkTreeModel     *model;
210         GSList           *events, *l;
211         gboolean          found_event = FALSE;
212         FlashForeachData  data;
213
214         window->flash_on = !window->flash_on;
215         data.on = window->flash_on;
216         model = GTK_TREE_MODEL (window->list_store);
217
218         events = empathy_event_manager_get_events (window->event_manager);
219         for (l = events; l; l = l->next) {
220                 data.event = l->data;
221                 data.window = window;
222                 if (!data.event->contact || !data.event->must_ack) {
223                         continue;
224                 }
225
226                 found_event = TRUE;
227                 gtk_tree_model_foreach (model,
228                                         main_window_flash_foreach,
229                                         &data);
230         }
231
232         if (!found_event) {
233                 main_window_flash_stop (window);
234         }
235
236         return TRUE;
237 }
238
239 static void
240 main_window_flash_start (EmpathyMainWindow *window)
241 {
242         if (window->flash_timeout_id != 0) {
243                 return;
244         }
245
246         DEBUG ("Start flashing");
247         window->flash_timeout_id = g_timeout_add (FLASH_TIMEOUT,
248                                                   (GSourceFunc) main_window_flash_cb,
249                                                   window);
250         main_window_flash_cb (window);
251 }
252
253 static void
254 main_window_event_added_cb (EmpathyEventManager *manager,
255                             EmpathyEvent        *event,
256                             EmpathyMainWindow   *window)
257 {
258         if (event->contact) {
259                 main_window_flash_start (window);
260         }
261 }
262
263 static void
264 main_window_event_removed_cb (EmpathyEventManager *manager,
265                               EmpathyEvent        *event,
266                               EmpathyMainWindow   *window)
267 {
268         FlashForeachData data;
269
270         if (!event->contact) {
271                 return;
272         }
273
274         data.on = FALSE;
275         data.event = event;
276         data.window = window;
277         gtk_tree_model_foreach (GTK_TREE_MODEL (window->list_store),
278                                 main_window_flash_foreach,
279                                 &data);
280 }
281
282 static void
283 main_window_row_activated_cb (EmpathyContactListView *view,
284                               GtkTreePath            *path,
285                               GtkTreeViewColumn      *col,
286                               EmpathyMainWindow      *window)
287 {
288         EmpathyContact *contact;
289         GtkTreeModel   *model;
290         GtkTreeIter     iter;
291         GSList         *events, *l;
292
293         model = GTK_TREE_MODEL (window->list_store);
294         gtk_tree_model_get_iter (model, &iter, path);
295         gtk_tree_model_get (model, &iter,
296                             EMPATHY_CONTACT_LIST_STORE_COL_CONTACT, &contact,
297                             -1);
298
299         if (!contact) {
300                 return;
301         }
302
303         /* If the contact has an event activate it, otherwise the
304          * default handler of row-activated will be called. */
305         events = empathy_event_manager_get_events (window->event_manager);
306         for (l = events; l; l = l->next) {
307                 EmpathyEvent *event = l->data;
308
309                 if (event->contact == contact) {
310                         DEBUG ("Activate event");
311                         empathy_event_activate (event);
312
313                         /* We don't want the default handler of this signal
314                          * (e.g. open a chat) */
315                         g_signal_stop_emission_by_name (view, "row-activated");
316                         break;
317                 }
318         }
319
320         g_object_unref (contact);
321 }
322
323 static void
324 main_window_remove_error (EmpathyMainWindow *window,
325                           TpAccount *account)
326 {
327         GtkWidget *error_widget;
328
329         error_widget = g_hash_table_lookup (window->errors, account);
330         if (error_widget != NULL) {
331                 gtk_widget_destroy (error_widget);
332                 g_hash_table_remove (window->errors, account);
333         }
334 }
335
336 static void
337 main_window_account_disabled_cb (TpAccountManager *manager,
338                                  TpAccount *account,
339                                  EmpathyMainWindow *window)
340 {
341         main_window_remove_error (window, account);
342 }
343
344 static void
345 main_window_error_retry_clicked_cb (GtkButton *button,
346                                     EmpathyMainWindow *window)
347 {
348         TpAccount *account;
349
350         account = g_object_get_data (G_OBJECT (button), "account");
351         tp_account_reconnect_async (account, NULL, NULL);
352
353         main_window_remove_error (window, account);
354 }
355
356 static void
357 main_window_error_edit_clicked_cb (GtkButton *button,
358                                    EmpathyMainWindow *window)
359 {
360         TpAccount *account;
361
362         account = g_object_get_data (G_OBJECT (button), "account");
363
364         empathy_accounts_dialog_show_application (
365                         gtk_widget_get_screen (GTK_WIDGET (button)),
366                         account, FALSE, FALSE);
367
368         main_window_remove_error (window, account);
369 }
370
371 static void
372 main_window_error_close_clicked_cb (GtkButton *button,
373                                     EmpathyMainWindow *window)
374 {
375         TpAccount *account;
376
377         account = g_object_get_data (G_OBJECT (button), "account");
378         main_window_remove_error (window, account);
379 }
380
381 static void
382 main_window_error_display (EmpathyMainWindow *window,
383                            TpAccount         *account,
384                            const gchar       *message)
385 {
386         GtkWidget *info_bar;
387         GtkWidget *content_area;
388         GtkWidget *label;
389         GtkWidget *image;
390         GtkWidget *retry_button;
391         GtkWidget *edit_button;
392         GtkWidget *close_button;
393         GtkWidget *action_area;
394         GtkWidget *action_table;
395         gchar     *str;
396         const gchar     *icon_name;
397
398         str = g_markup_printf_escaped ("<b>%s</b>\n%s",
399                                                tp_account_get_display_name (account),
400                                                message);
401
402         info_bar = g_hash_table_lookup (window->errors, account);
403         if (info_bar) {
404                 label = g_object_get_data (G_OBJECT (info_bar), "label");
405
406                 /* Just set the latest error and return */
407                 gtk_label_set_markup (GTK_LABEL (label), str);
408                 g_free (str);
409
410                 return;
411         }
412
413         info_bar = gtk_info_bar_new ();
414         gtk_info_bar_set_message_type (GTK_INFO_BAR (info_bar), GTK_MESSAGE_ERROR);
415
416         gtk_widget_set_no_show_all (info_bar, TRUE);
417         gtk_box_pack_start (GTK_BOX (window->errors_vbox), info_bar, FALSE, TRUE, 0);
418         gtk_widget_show (info_bar);
419
420         icon_name = tp_account_get_icon_name (account);
421         image = gtk_image_new_from_icon_name (icon_name, GTK_ICON_SIZE_SMALL_TOOLBAR);
422         gtk_widget_show (image);
423
424         label = gtk_label_new (str);
425         gtk_label_set_use_markup (GTK_LABEL (label), TRUE);
426         gtk_label_set_ellipsize (GTK_LABEL (label), PANGO_ELLIPSIZE_END);
427         gtk_misc_set_alignment (GTK_MISC (label), 0, 0.5);
428         gtk_widget_show (label);
429         g_free (str);
430
431         content_area = gtk_info_bar_get_content_area (GTK_INFO_BAR (info_bar));
432         gtk_box_pack_start (GTK_BOX (content_area), image, FALSE, FALSE, 0);
433         gtk_box_pack_start (GTK_BOX (content_area), label, TRUE, TRUE, 0);
434
435         image = gtk_image_new_from_stock (GTK_STOCK_REFRESH, GTK_ICON_SIZE_BUTTON);
436         retry_button = gtk_button_new ();
437         gtk_button_set_image (GTK_BUTTON (retry_button), image);
438         gtk_widget_set_tooltip_text (retry_button, _("Reconnect"));
439         gtk_widget_show (retry_button);
440
441         image = gtk_image_new_from_stock (GTK_STOCK_EDIT, GTK_ICON_SIZE_BUTTON);
442         edit_button = gtk_button_new ();
443         gtk_button_set_image (GTK_BUTTON (edit_button), image);
444         gtk_widget_set_tooltip_text (edit_button, _("Edit Account"));
445         gtk_widget_show (edit_button);
446
447         image = gtk_image_new_from_stock (GTK_STOCK_CLOSE, GTK_ICON_SIZE_BUTTON);
448         close_button = gtk_button_new ();
449         gtk_button_set_image (GTK_BUTTON (close_button), image);
450         gtk_widget_set_tooltip_text (close_button, _("Close"));
451         gtk_widget_show (close_button);
452
453         action_table = gtk_table_new (1, 3, FALSE);
454         gtk_table_set_col_spacings (GTK_TABLE (action_table), 2);
455         gtk_widget_show (action_table);
456
457         action_area = gtk_info_bar_get_action_area (GTK_INFO_BAR (info_bar));
458         gtk_box_pack_start (GTK_BOX (action_area), action_table, FALSE, FALSE, 0);
459
460         gtk_table_attach (GTK_TABLE (action_table), retry_button, 0, 1, 0, 1,
461                                                                                 (GtkAttachOptions) (GTK_SHRINK),
462                                                                                 (GtkAttachOptions) (GTK_SHRINK), 0, 0);
463         gtk_table_attach (GTK_TABLE (action_table), edit_button, 1, 2, 0, 1,
464                                                                                 (GtkAttachOptions) (GTK_SHRINK),
465                                                                                 (GtkAttachOptions) (GTK_SHRINK), 0, 0);
466         gtk_table_attach (GTK_TABLE (action_table), close_button, 2, 3, 0, 1,
467                                                                                 (GtkAttachOptions) (GTK_SHRINK),
468                                                                                 (GtkAttachOptions) (GTK_SHRINK), 0, 0);
469
470         g_object_set_data (G_OBJECT (info_bar), "label", label);
471         g_object_set_data_full (G_OBJECT (info_bar),
472                                 "account", g_object_ref (account),
473                                 g_object_unref);
474         g_object_set_data_full (G_OBJECT (edit_button),
475                                 "account", g_object_ref (account),
476                                 g_object_unref);
477         g_object_set_data_full (G_OBJECT (close_button),
478                                 "account", g_object_ref (account),
479                                 g_object_unref);
480         g_object_set_data_full (G_OBJECT (retry_button),
481                                 "account", g_object_ref (account),
482                                 g_object_unref);
483
484         g_signal_connect (edit_button, "clicked",
485                           G_CALLBACK (main_window_error_edit_clicked_cb),
486                           window);
487         g_signal_connect (close_button, "clicked",
488                           G_CALLBACK (main_window_error_close_clicked_cb),
489                           window);
490         g_signal_connect (retry_button, "clicked",
491                           G_CALLBACK (main_window_error_retry_clicked_cb),
492                           window);
493
494         gtk_widget_show (window->errors_vbox);
495
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         g_object_unref (window->chatroom_manager);
661
662         g_free (window);
663 }
664
665 static void
666 main_window_chat_quit_cb (GtkAction         *action,
667                           EmpathyMainWindow *window)
668 {
669         gtk_main_quit ();
670 }
671
672 static void
673 main_window_view_history_cb (GtkAction         *action,
674                              EmpathyMainWindow *window)
675 {
676         empathy_log_window_show (NULL, NULL, FALSE, GTK_WINDOW (window->window));
677 }
678
679 static void
680 main_window_chat_new_message_cb (GtkAction         *action,
681                                  EmpathyMainWindow *window)
682 {
683         empathy_new_message_dialog_show (GTK_WINDOW (window->window));
684 }
685
686 static void
687 main_window_chat_new_call_cb (GtkAction         *action,
688                                  EmpathyMainWindow *window)
689 {
690         empathy_new_call_dialog_show (GTK_WINDOW (window->window));
691 }
692
693 static void
694 main_window_chat_add_contact_cb (GtkAction         *action,
695                                  EmpathyMainWindow *window)
696 {
697         empathy_new_contact_dialog_show (GTK_WINDOW (window->window));
698 }
699
700 static void
701 main_window_view_show_ft_manager (GtkAction         *action,
702                                   EmpathyMainWindow *window)
703 {
704         empathy_ft_manager_show ();
705 }
706
707 static void
708 main_window_view_show_offline_cb (GtkToggleAction   *action,
709                                   EmpathyMainWindow *window)
710 {
711         gboolean current;
712
713         current = gtk_toggle_action_get_active (action);
714         empathy_conf_set_bool (empathy_conf_get (),
715                               EMPATHY_PREFS_CONTACTS_SHOW_OFFLINE,
716                               current);
717
718         /* Turn off sound just while we alter the contact list. */
719         // FIXME: empathy_sound_set_enabled (FALSE);
720         empathy_contact_list_store_set_show_offline (window->list_store, current);
721         //empathy_sound_set_enabled (TRUE);
722 }
723
724 static void
725 main_window_notify_sort_contact_cb (EmpathyConf       *conf,
726                                     const gchar       *key,
727                                     EmpathyMainWindow *window)
728 {
729         gchar *str = NULL;
730
731         if (empathy_conf_get_string (conf, key, &str) && str) {
732                 GType       type;
733                 GEnumClass *enum_class;
734                 GEnumValue *enum_value;
735
736                 type = empathy_contact_list_store_sort_get_type ();
737                 enum_class = G_ENUM_CLASS (g_type_class_peek (type));
738                 enum_value = g_enum_get_value_by_nick (enum_class, str);
739                 if (enum_value) {
740                         /* By changing the value of the GtkRadioAction,
741                            it emits a signal that calls main_window_view_sort_contacts_cb
742                            which updates the contacts list */
743                         gtk_radio_action_set_current_value (window->sort_by_name,
744                                                             enum_value->value);
745                 } else {
746                         g_warning ("Wrong value for sort_criterium configuration : %s", str);
747                 }
748                 g_free (str);
749         }
750 }
751
752 static void
753 main_window_view_sort_contacts_cb (GtkRadioAction    *action,
754                                    GtkRadioAction    *current,
755                                    EmpathyMainWindow *window)
756 {
757         EmpathyContactListStoreSort value;
758         GSList      *group;
759         GType        type;
760         GEnumClass  *enum_class;
761         GEnumValue  *enum_value;
762
763         value = gtk_radio_action_get_current_value (action);
764         group = gtk_radio_action_get_group (action);
765
766         /* Get string from index */
767         type = empathy_contact_list_store_sort_get_type ();
768         enum_class = G_ENUM_CLASS (g_type_class_peek (type));
769         enum_value = g_enum_get_value (enum_class, g_slist_index (group, current));
770
771         if (!enum_value) {
772                 g_warning ("No GEnumValue for EmpathyContactListSort with GtkRadioAction index:%d",
773                            g_slist_index (group, action));
774         } else {
775                 empathy_conf_set_string (empathy_conf_get (),
776                                          EMPATHY_PREFS_CONTACTS_SORT_CRITERIUM,
777                                          enum_value->value_nick);
778         }
779         empathy_contact_list_store_set_sort_criterium (window->list_store, value);
780 }
781
782 static void
783 main_window_view_show_protocols_cb (GtkToggleAction *action,
784                                         EmpathyMainWindow *window)
785 {
786         gboolean value;
787
788         value = gtk_toggle_action_get_active (action);
789
790         empathy_conf_set_bool (empathy_conf_get (),
791                                          EMPATHY_PREFS_UI_SHOW_PROTOCOLS,
792                                          value == TRUE);
793         empathy_contact_list_store_set_show_protocols (window->list_store,
794                                          value == TRUE);
795 }
796
797 /* Matches GtkRadioAction values set in empathy-main-window.ui */
798 #define CONTACT_LIST_NORMAL_SIZE_WITH_AVATARS           0
799 #define CONTACT_LIST_NORMAL_SIZE                        1
800 #define CONTACT_LIST_COMPACT_SIZE                       2
801
802 static void
803 main_window_view_contacts_list_size_cb (GtkRadioAction    *action,
804                                         GtkRadioAction    *current,
805                                         EmpathyMainWindow *window)
806 {
807         gint     value;
808
809         value = gtk_radio_action_get_current_value (action);
810
811         empathy_conf_set_bool (empathy_conf_get (),
812                                EMPATHY_PREFS_UI_SHOW_AVATARS,
813                                value == CONTACT_LIST_NORMAL_SIZE_WITH_AVATARS);
814         empathy_conf_set_bool (empathy_conf_get (),
815                                EMPATHY_PREFS_UI_COMPACT_CONTACT_LIST,
816                                value == CONTACT_LIST_COMPACT_SIZE);
817
818         empathy_contact_list_store_set_show_avatars (window->list_store,
819                                                      value == CONTACT_LIST_NORMAL_SIZE_WITH_AVATARS);
820         empathy_contact_list_store_set_is_compact (window->list_store,
821                                                    value == CONTACT_LIST_COMPACT_SIZE);
822 }
823
824 static void main_window_notify_show_protocols_cb (EmpathyConf       *conf,
825                                         const gchar       *key,
826                                         EmpathyMainWindow *window)
827 {
828         gboolean show_protocols;
829
830         if (empathy_conf_get_bool (conf,
831                                    EMPATHY_PREFS_UI_SHOW_PROTOCOLS,
832                                    &show_protocols)) {
833                 gtk_toggle_action_set_active (window->show_protocols,
834                                               show_protocols);
835         }
836 }
837
838
839 static void
840 main_window_notify_contact_list_size_cb (EmpathyConf       *conf,
841                                          const gchar       *key,
842                                          EmpathyMainWindow *window)
843 {
844         gboolean show_avatars;
845         gboolean compact_contact_list;
846         gint value = CONTACT_LIST_NORMAL_SIZE_WITH_AVATARS;
847
848         if (empathy_conf_get_bool (conf,
849                                    EMPATHY_PREFS_UI_SHOW_AVATARS,
850                                    &show_avatars)
851             && empathy_conf_get_bool (conf,
852                                       EMPATHY_PREFS_UI_COMPACT_CONTACT_LIST,
853                                       &compact_contact_list)) {
854                 if (compact_contact_list) {
855                         value = CONTACT_LIST_COMPACT_SIZE;
856                 } else if (show_avatars) {
857                         value = CONTACT_LIST_NORMAL_SIZE_WITH_AVATARS;
858                 } else {
859                         value = CONTACT_LIST_NORMAL_SIZE;
860                 }
861         }
862         /* By changing the value of the GtkRadioAction,
863            it emits a signal that calls main_window_view_contacts_list_size_cb
864            which updates the contacts list */
865         gtk_radio_action_set_current_value (window->normal_with_avatars, value);
866 }
867
868 static void
869 main_window_view_show_map_cb (GtkCheckMenuItem  *item,
870                               EmpathyMainWindow *window)
871 {
872 #if HAVE_LIBCHAMPLAIN
873         empathy_map_view_show ();
874 #endif
875 }
876
877 static void
878 main_window_favorite_chatroom_join (EmpathyChatroom *chatroom)
879 {
880         TpAccount      *account;
881         TpConnection   *connection;
882         const gchar    *room;
883
884         account = empathy_chatroom_get_account (chatroom);
885         connection = tp_account_get_connection (account);
886         room = empathy_chatroom_get_room (chatroom);
887
888         if (connection != NULL) {
889                 DEBUG ("Requesting channel for '%s'", room);
890                 empathy_dispatcher_join_muc (connection, room, NULL, NULL);
891         }
892 }
893
894 static void
895 main_window_favorite_chatroom_menu_activate_cb (GtkMenuItem    *menu_item,
896                                                 EmpathyChatroom *chatroom)
897 {
898         main_window_favorite_chatroom_join (chatroom);
899 }
900
901 static void
902 main_window_favorite_chatroom_menu_add (EmpathyMainWindow *window,
903                                         EmpathyChatroom    *chatroom)
904 {
905         GtkWidget   *menu_item;
906         const gchar *name;
907
908         if (g_object_get_data (G_OBJECT (chatroom), "menu_item")) {
909                 return;
910         }
911
912         name = empathy_chatroom_get_name (chatroom);
913         menu_item = gtk_menu_item_new_with_label (name);
914
915         g_object_set_data (G_OBJECT (chatroom), "menu_item", menu_item);
916         g_signal_connect (menu_item, "activate",
917                           G_CALLBACK (main_window_favorite_chatroom_menu_activate_cb),
918                           chatroom);
919
920         gtk_menu_shell_insert (GTK_MENU_SHELL (window->room_menu),
921                                menu_item, 4);
922
923         gtk_widget_show (menu_item);
924 }
925
926 static void
927 main_window_favorite_chatroom_menu_added_cb (EmpathyChatroomManager *manager,
928                                              EmpathyChatroom        *chatroom,
929                                              EmpathyMainWindow     *window)
930 {
931         main_window_favorite_chatroom_menu_add (window, chatroom);
932         gtk_widget_show (window->room_separator);
933         gtk_action_set_sensitive (window->room_join_favorites, TRUE);
934 }
935
936 static void
937 main_window_favorite_chatroom_menu_removed_cb (EmpathyChatroomManager *manager,
938                                                EmpathyChatroom        *chatroom,
939                                                EmpathyMainWindow     *window)
940 {
941         GtkWidget *menu_item;
942         GList *chatrooms;
943
944         menu_item = g_object_get_data (G_OBJECT (chatroom), "menu_item");
945         g_object_set_data (G_OBJECT (chatroom), "menu_item", NULL);
946         gtk_widget_destroy (menu_item);
947
948         chatrooms = empathy_chatroom_manager_get_chatrooms (window->chatroom_manager, NULL);
949         if (chatrooms) {
950                 gtk_widget_show (window->room_separator);
951         } else {
952                 gtk_widget_hide (window->room_separator);
953         }
954
955         gtk_action_set_sensitive (window->room_join_favorites, chatrooms != NULL);
956         g_list_free (chatrooms);
957 }
958
959 static void
960 main_window_favorite_chatroom_menu_setup (EmpathyMainWindow *window)
961 {
962         GList *chatrooms, *l;
963         GtkWidget *room;
964
965         window->chatroom_manager = empathy_chatroom_manager_dup_singleton (NULL);
966         chatrooms = empathy_chatroom_manager_get_chatrooms (window->chatroom_manager, NULL);
967         room = gtk_ui_manager_get_widget (window->ui_manager,
968                 "/menubar/room");
969         window->room_menu = gtk_menu_item_get_submenu (GTK_MENU_ITEM (room));
970         window->room_separator = gtk_ui_manager_get_widget (window->ui_manager,
971                 "/menubar/room/room_separator");
972
973         for (l = chatrooms; l; l = l->next) {
974                 main_window_favorite_chatroom_menu_add (window, l->data);
975         }
976
977         if (!chatrooms) {
978                 gtk_widget_hide (window->room_separator);
979         }
980
981         gtk_action_set_sensitive (window->room_join_favorites, chatrooms != NULL);
982
983         g_signal_connect (window->chatroom_manager, "chatroom-added",
984                           G_CALLBACK (main_window_favorite_chatroom_menu_added_cb),
985                           window);
986         g_signal_connect (window->chatroom_manager, "chatroom-removed",
987                           G_CALLBACK (main_window_favorite_chatroom_menu_removed_cb),
988                           window);
989
990         g_list_free (chatrooms);
991 }
992
993 static void
994 main_window_room_join_new_cb (GtkAction         *action,
995                               EmpathyMainWindow *window)
996 {
997         empathy_new_chatroom_dialog_show (GTK_WINDOW (window->window));
998 }
999
1000 static void
1001 main_window_room_join_favorites_cb (GtkAction         *action,
1002                                     EmpathyMainWindow *window)
1003 {
1004         GList *chatrooms, *l;
1005
1006         chatrooms = empathy_chatroom_manager_get_chatrooms (window->chatroom_manager, NULL);
1007         for (l = chatrooms; l; l = l->next) {
1008                 main_window_favorite_chatroom_join (l->data);
1009         }
1010         g_list_free (chatrooms);
1011 }
1012
1013 static void
1014 main_window_room_manage_favorites_cb (GtkAction         *action,
1015                                       EmpathyMainWindow *window)
1016 {
1017         empathy_chatrooms_window_show (GTK_WINDOW (window->window));
1018 }
1019
1020 static void
1021 main_window_edit_cb (GtkAction *action,
1022                      EmpathyMainWindow *window)
1023 {
1024         GtkWidget *submenu;
1025
1026         /* FIXME: It should use the UIManager to merge the contact/group submenu */
1027         submenu = empathy_contact_list_view_get_contact_menu (window->list_view);
1028         if (submenu) {
1029                 GtkMenuItem *item;
1030                 GtkWidget   *label;
1031
1032                 item = GTK_MENU_ITEM (window->edit_context);
1033                 label = gtk_bin_get_child (GTK_BIN (item));
1034                 gtk_label_set_text (GTK_LABEL (label), _("Contact"));
1035
1036                 gtk_widget_show (window->edit_context);
1037                 gtk_widget_show (window->edit_context_separator);
1038
1039                 gtk_menu_item_set_submenu (item, submenu);
1040
1041                 return;
1042         }
1043
1044         submenu = empathy_contact_list_view_get_group_menu (window->list_view);
1045         if (submenu) {
1046                 GtkMenuItem *item;
1047                 GtkWidget   *label;
1048
1049                 item = GTK_MENU_ITEM (window->edit_context);
1050                 label = gtk_bin_get_child (GTK_BIN (item));
1051                 gtk_label_set_text (GTK_LABEL (label), _("Group"));
1052
1053                 gtk_widget_show (window->edit_context);
1054                 gtk_widget_show (window->edit_context_separator);
1055
1056                 gtk_menu_item_set_submenu (item, submenu);
1057
1058                 return;
1059         }
1060
1061         gtk_widget_hide (window->edit_context);
1062         gtk_widget_hide (window->edit_context_separator);
1063
1064         return;
1065 }
1066
1067 static void
1068 main_window_edit_accounts_cb (GtkAction         *action,
1069                               EmpathyMainWindow *window)
1070 {
1071         empathy_accounts_dialog_show_application (gdk_screen_get_default (),
1072                         NULL, FALSE, FALSE);
1073 }
1074
1075 static void
1076 main_window_edit_personal_information_cb (GtkAction         *action,
1077                                           EmpathyMainWindow *window)
1078 {
1079         empathy_contact_personal_dialog_show (GTK_WINDOW (window->window));
1080 }
1081
1082 static void
1083 main_window_edit_preferences_cb (GtkAction         *action,
1084                                  EmpathyMainWindow *window)
1085 {
1086         empathy_preferences_show (GTK_WINDOW (window->window));
1087 }
1088
1089 static void
1090 main_window_help_about_cb (GtkAction         *action,
1091                            EmpathyMainWindow *window)
1092 {
1093         empathy_about_dialog_new (GTK_WINDOW (window->window));
1094 }
1095
1096 static void
1097 main_window_help_debug_cb (GtkAction         *action,
1098                            EmpathyMainWindow *window)
1099 {
1100         GdkScreen *screen = gdk_screen_get_default ();
1101         GError *error = NULL;
1102         gchar *argv[2] = { NULL, };
1103         gint i = 0;
1104         gchar *path;
1105
1106         g_return_if_fail (GDK_IS_SCREEN (screen));
1107
1108         /* Try to run from source directory if possible */
1109         path = g_build_filename (g_getenv ("EMPATHY_SRCDIR"), "src",
1110                         "empathy-debugger", NULL);
1111
1112         if (!g_file_test (path, G_FILE_TEST_EXISTS)) {
1113                 g_free (path);
1114                 path = g_build_filename (BIN_DIR, "empathy-debugger", NULL);
1115         }
1116
1117         argv[i++] = path;
1118
1119         gdk_spawn_on_screen (screen, NULL, argv, NULL,
1120                         G_SPAWN_SEARCH_PATH,
1121                         NULL, NULL, NULL, &error);
1122
1123         if (error) {
1124                 g_warning ("Failed to open debug window: %s", error->message);
1125                 g_error_free (error);
1126         }
1127
1128         g_free (path);
1129 }
1130
1131 static void
1132 main_window_help_contents_cb (GtkAction         *action,
1133                               EmpathyMainWindow *window)
1134 {
1135         empathy_url_show (window->window, "ghelp:empathy");
1136 }
1137
1138 static gboolean
1139 main_window_throbber_button_press_event_cb (GtkWidget         *throbber_ebox,
1140                                             GdkEventButton    *event,
1141                                             EmpathyMainWindow *window)
1142 {
1143         if (event->type != GDK_BUTTON_PRESS ||
1144             event->button != 1) {
1145                 return FALSE;
1146         }
1147
1148         empathy_accounts_dialog_show_application (
1149                         gtk_widget_get_screen (GTK_WIDGET (throbber_ebox)),
1150                         NULL, FALSE, FALSE);
1151
1152         return FALSE;
1153 }
1154
1155 static void
1156 main_window_account_removed_cb (TpAccountManager  *manager,
1157                                 TpAccount         *account,
1158                                 EmpathyMainWindow *window)
1159 {
1160         GList *a;
1161
1162         a = tp_account_manager_get_valid_accounts (manager);
1163
1164         gtk_action_set_sensitive (window->view_history,
1165                 g_list_length (a) > 0);
1166
1167         g_list_free (a);
1168
1169         /* remove errors if any */
1170         main_window_remove_error (window, account);
1171 }
1172
1173 static void
1174 main_window_account_validity_changed_cb (TpAccountManager *manager,
1175                                          TpAccount *account,
1176                                          gboolean valid,
1177                                          EmpathyMainWindow *window)
1178 {
1179         if (valid) {
1180                 gulong handler_id;
1181                 handler_id = GPOINTER_TO_UINT (g_hash_table_lookup (
1182                         window->status_changed_handlers, account));
1183
1184                 /* connect signal only if it was not connected yet */
1185                 if (handler_id == 0) {
1186                         handler_id = g_signal_connect (account,
1187                                 "status-changed",
1188                                 G_CALLBACK (main_window_connection_changed_cb),
1189                                 window);
1190                         g_hash_table_insert (window->status_changed_handlers,
1191                                 account, GUINT_TO_POINTER (handler_id));
1192                 }
1193         }
1194
1195         main_window_account_removed_cb (manager, account, window);
1196 }
1197
1198 static void
1199 main_window_notify_show_offline_cb (EmpathyConf *conf,
1200                                     const gchar *key,
1201                                     gpointer     toggle_action)
1202 {
1203         gboolean show_offline;
1204
1205         if (empathy_conf_get_bool (conf, key, &show_offline)) {
1206                 gtk_toggle_action_set_active (toggle_action, show_offline);
1207         }
1208 }
1209
1210 static void
1211 main_window_connection_items_setup (EmpathyMainWindow *window,
1212                                     GtkBuilder        *gui)
1213 {
1214         GList         *list;
1215         GObject       *action;
1216         guint          i;
1217         const gchar *actions_connected[] = {
1218                 "room",
1219                 "chat_new_message",
1220                 "chat_new_call",
1221                 "chat_add_contact",
1222                 "edit_personal_information"
1223         };
1224
1225         for (i = 0, list = NULL; i < G_N_ELEMENTS (actions_connected); i++) {
1226                 action = gtk_builder_get_object (gui, actions_connected[i]);
1227                 list = g_list_prepend (list, action);
1228         }
1229
1230         window->actions_connected = list;
1231 }
1232
1233 GtkWidget *
1234 empathy_main_window_get (void)
1235 {
1236   return main_window != NULL ? main_window->window : NULL;
1237 }
1238
1239 static void
1240 account_manager_prepared_cb (GObject *source_object,
1241                              GAsyncResult *result,
1242                              gpointer user_data)
1243 {
1244         GList *accounts, *j;
1245         TpAccountManager *manager = TP_ACCOUNT_MANAGER (source_object);
1246         EmpathyMainWindow *window = user_data;
1247         GError *error = NULL;
1248
1249         if (!tp_account_manager_prepare_finish (manager, result, &error)) {
1250                 DEBUG ("Failed to prepare account manager: %s", error->message);
1251                 g_error_free (error);
1252                 return;
1253         }
1254
1255         accounts = tp_account_manager_get_valid_accounts (window->account_manager);
1256         for (j = accounts; j != NULL; j = j->next) {
1257                 TpAccount *account = TP_ACCOUNT (j->data);
1258                 gulong handler_id;
1259
1260                 handler_id = g_signal_connect (account, "status-changed",
1261                                   G_CALLBACK (main_window_connection_changed_cb),
1262                                   window);
1263                 g_hash_table_insert (window->status_changed_handlers,
1264                                      account, GUINT_TO_POINTER (handler_id));
1265         }
1266
1267         g_signal_connect (manager, "account-validity-changed",
1268                           G_CALLBACK (main_window_account_validity_changed_cb),
1269                           window);
1270
1271         main_window_update_status (window);
1272
1273         /* Disable the "Previous Conversations" menu entry if there is no account */
1274         gtk_action_set_sensitive (window->view_history,
1275                 g_list_length (accounts) > 0);
1276
1277         g_list_free (accounts);
1278 }
1279
1280 static void
1281 main_window_contact_added_cb (EmpathyContactMonitor     *monitor,
1282                               EmpathyContact            *contact,
1283                               EmpathyMainWindow         *window)
1284 {
1285         if (!empathy_migrate_butterfly_logs (contact)) {
1286                 g_signal_handler_disconnect (monitor,
1287                                              window->butterfly_log_migration_contact_added_id);
1288                 window->butterfly_log_migration_contact_added_id = 0;
1289         }
1290 }
1291
1292 GtkWidget *
1293 empathy_main_window_show (void)
1294 {
1295         EmpathyMainWindow        *window;
1296         EmpathyContactList       *list_iface;
1297         EmpathyContactMonitor    *monitor;
1298         GtkBuilder               *gui;
1299         EmpathyConf              *conf;
1300         GtkWidget                *sw;
1301         GtkToggleAction          *show_offline_widget;
1302         GtkWidget                *ebox;
1303         GtkAction                *show_map_widget;
1304         GtkToolItem              *item;
1305         gboolean                  show_offline;
1306         gchar                    *filename;
1307         GSList                   *l;
1308
1309         if (main_window) {
1310                 empathy_window_present (GTK_WINDOW (main_window->window));
1311                 return main_window->window;
1312         }
1313
1314         main_window = g_new0 (EmpathyMainWindow, 1);
1315         window = main_window;
1316
1317         /* Set up interface */
1318         filename = empathy_file_lookup ("empathy-main-window.ui", "src");
1319         gui = empathy_builder_get_file (filename,
1320                                        "main_window", &window->window,
1321                                        "main_vbox", &window->main_vbox,
1322                                        "errors_vbox", &window->errors_vbox,
1323                                        "ui_manager", &window->ui_manager,
1324                                        "view_show_offline", &show_offline_widget,
1325                                        "view_show_protocols", &window->show_protocols,
1326                                        "view_sort_by_name", &window->sort_by_name,
1327                                        "view_sort_by_status", &window->sort_by_status,
1328                                        "view_normal_size_with_avatars", &window->normal_with_avatars,
1329                                        "view_normal_size", &window->normal_size,
1330                                        "view_compact_size", &window->compact_size,
1331                                        "view_history", &window->view_history,
1332                                        "view_show_map", &show_map_widget,
1333                                        "room_join_favorites", &window->room_join_favorites,
1334                                        "presence_toolbar", &window->presence_toolbar,
1335                                        "roster_scrolledwindow", &sw,
1336                                        NULL);
1337         g_free (filename);
1338
1339         empathy_builder_connect (gui, window,
1340                               "main_window", "destroy", main_window_destroy_cb,
1341                               "chat_quit", "activate", main_window_chat_quit_cb,
1342                               "chat_new_message", "activate", main_window_chat_new_message_cb,
1343                               "chat_new_call", "activate", main_window_chat_new_call_cb,
1344                               "view_history", "activate", main_window_view_history_cb,
1345                               "room_join_new", "activate", main_window_room_join_new_cb,
1346                               "room_join_favorites", "activate", main_window_room_join_favorites_cb,
1347                               "room_manage_favorites", "activate", main_window_room_manage_favorites_cb,
1348                               "chat_add_contact", "activate", main_window_chat_add_contact_cb,
1349                               "view_show_ft_manager", "activate", main_window_view_show_ft_manager,
1350                               "view_show_offline", "toggled", main_window_view_show_offline_cb,
1351                               "view_show_protocols", "toggled", main_window_view_show_protocols_cb,
1352                               "view_sort_by_name", "changed", main_window_view_sort_contacts_cb,
1353                               "view_normal_size_with_avatars", "changed", main_window_view_contacts_list_size_cb,
1354                               "view_show_map", "activate", main_window_view_show_map_cb,
1355                               "edit", "activate", main_window_edit_cb,
1356                               "edit_accounts", "activate", main_window_edit_accounts_cb,
1357                               "edit_personal_information", "activate", main_window_edit_personal_information_cb,
1358                               "edit_preferences", "activate", main_window_edit_preferences_cb,
1359                               "help_about", "activate", main_window_help_about_cb,
1360                               "help_debug", "activate", main_window_help_debug_cb,
1361                               "help_contents", "activate", main_window_help_contents_cb,
1362                               NULL);
1363
1364         /* Set up connection related widgets. */
1365         main_window_connection_items_setup (window, gui);
1366
1367         g_object_ref (window->ui_manager);
1368         g_object_unref (gui);
1369
1370 #if !HAVE_LIBCHAMPLAIN
1371         gtk_action_set_visible (show_map_widget, FALSE);
1372 #endif
1373
1374         window->account_manager = tp_account_manager_dup ();
1375
1376         tp_account_manager_prepare_async (window->account_manager, NULL,
1377                                           account_manager_prepared_cb, window);
1378
1379         window->errors = g_hash_table_new_full (g_direct_hash,
1380                                                 g_direct_equal,
1381                                                 g_object_unref,
1382                                                 NULL);
1383
1384         window->status_changed_handlers = g_hash_table_new_full (g_direct_hash,
1385                                                                  g_direct_equal,
1386                                                                  NULL,
1387                                                                  NULL);
1388
1389         /* Set up menu */
1390         main_window_favorite_chatroom_menu_setup (window);
1391
1392         window->edit_context = gtk_ui_manager_get_widget (window->ui_manager,
1393                 "/menubar/edit/edit_context");
1394         window->edit_context_separator = gtk_ui_manager_get_widget (window->ui_manager,
1395                 "/menubar/edit/edit_context_separator");
1396         gtk_widget_hide (window->edit_context);
1397         gtk_widget_hide (window->edit_context_separator);
1398
1399         /* Set up contact list. */
1400         empathy_status_presets_get_all ();
1401
1402         /* Set up presence chooser */
1403         window->presence_chooser = empathy_presence_chooser_new ();
1404         gtk_widget_show (window->presence_chooser);
1405         item = gtk_tool_item_new ();
1406         gtk_widget_show (GTK_WIDGET (item));
1407         gtk_container_add (GTK_CONTAINER (item), window->presence_chooser);
1408         gtk_tool_item_set_is_important (item, TRUE);
1409         gtk_tool_item_set_expand (item, TRUE);
1410         gtk_toolbar_insert (GTK_TOOLBAR (window->presence_toolbar), item, -1);
1411
1412         /* Set up the throbber */
1413         ebox = gtk_event_box_new ();
1414         gtk_event_box_set_visible_window (GTK_EVENT_BOX (ebox), FALSE);
1415         gtk_widget_set_tooltip_text (ebox, _("Show and edit accounts"));
1416         g_signal_connect (ebox,
1417                           "button-press-event",
1418                           G_CALLBACK (main_window_throbber_button_press_event_cb),
1419                           window);
1420         gtk_widget_show (ebox);
1421
1422         window->throbber = ephy_spinner_new ();
1423         ephy_spinner_set_size (EPHY_SPINNER (window->throbber), GTK_ICON_SIZE_LARGE_TOOLBAR);
1424         gtk_container_add (GTK_CONTAINER (ebox), window->throbber);
1425         gtk_widget_show (window->throbber);
1426
1427         item = gtk_tool_item_new ();
1428         gtk_container_add (GTK_CONTAINER (item), ebox);
1429         gtk_toolbar_insert (GTK_TOOLBAR (window->presence_toolbar), item, -1);
1430         gtk_widget_show (GTK_WIDGET (item));
1431
1432         list_iface = EMPATHY_CONTACT_LIST (empathy_contact_manager_dup_singleton ());
1433         monitor = empathy_contact_list_get_monitor (list_iface);
1434         window->list_store = empathy_contact_list_store_new (list_iface);
1435         window->list_view = empathy_contact_list_view_new (window->list_store,
1436                                                            EMPATHY_CONTACT_LIST_FEATURE_ALL,
1437                                                            EMPATHY_CONTACT_FEATURE_ALL);
1438         g_signal_connect (monitor, "contact-presence-changed",
1439                           G_CALLBACK (main_window_contact_presence_changed_cb), window);
1440         window->butterfly_log_migration_contact_added_id =  g_signal_connect (monitor, "contact-added",
1441                           G_CALLBACK (main_window_contact_added_cb), window);
1442         g_object_unref (list_iface);
1443
1444         gtk_widget_show (GTK_WIDGET (window->list_view));
1445         gtk_container_add (GTK_CONTAINER (sw),
1446                            GTK_WIDGET (window->list_view));
1447         g_signal_connect (window->list_view, "row-activated",
1448                           G_CALLBACK (main_window_row_activated_cb),
1449                           window);
1450
1451         /* Load user-defined accelerators. */
1452         main_window_accels_load ();
1453
1454         /* Set window size. */
1455         empathy_geometry_bind (GTK_WINDOW (window->window), GEOMETRY_NAME);
1456
1457         /* Enable event handling */
1458         window->event_manager = empathy_event_manager_dup_singleton ();
1459         g_signal_connect (window->event_manager, "event-added",
1460                           G_CALLBACK (main_window_event_added_cb),
1461                           window);
1462         g_signal_connect (window->event_manager, "event-removed",
1463                           G_CALLBACK (main_window_event_removed_cb),
1464                           window);
1465
1466         g_signal_connect (window->account_manager, "account-validity-changed",
1467                           G_CALLBACK (main_window_account_validity_changed_cb),
1468                           window);
1469         g_signal_connect (window->account_manager, "account-removed",
1470                           G_CALLBACK (main_window_account_removed_cb),
1471                           window);
1472         g_signal_connect (window->account_manager, "account-disabled",
1473                           G_CALLBACK (main_window_account_disabled_cb),
1474                           window);
1475
1476         l = empathy_event_manager_get_events (window->event_manager);
1477         while (l) {
1478                 main_window_event_added_cb (window->event_manager,
1479                                             l->data, window);
1480                 l = l->next;
1481         }
1482
1483         conf = empathy_conf_get ();
1484
1485         /* Show offline ? */
1486         empathy_conf_get_bool (conf,
1487                               EMPATHY_PREFS_CONTACTS_SHOW_OFFLINE,
1488                               &show_offline);
1489         empathy_conf_notify_add (conf,
1490                                 EMPATHY_PREFS_CONTACTS_SHOW_OFFLINE,
1491                                 main_window_notify_show_offline_cb,
1492                                 show_offline_widget);
1493
1494         gtk_toggle_action_set_active (show_offline_widget, show_offline);
1495
1496         /* Show protocol ? */
1497         empathy_conf_notify_add (conf,
1498                                  EMPATHY_PREFS_UI_SHOW_PROTOCOLS,
1499                                  (EmpathyConfNotifyFunc) main_window_notify_show_protocols_cb,
1500                                  window);
1501
1502         main_window_notify_show_protocols_cb (conf,
1503                                             EMPATHY_PREFS_UI_SHOW_PROTOCOLS,
1504                                             window);
1505
1506         /* Sort by name / by status ? */
1507         empathy_conf_notify_add (conf,
1508                                  EMPATHY_PREFS_CONTACTS_SORT_CRITERIUM,
1509                                  (EmpathyConfNotifyFunc) main_window_notify_sort_contact_cb,
1510                                  window);
1511
1512         main_window_notify_sort_contact_cb (conf,
1513                                             EMPATHY_PREFS_CONTACTS_SORT_CRITERIUM,
1514                                             window);
1515
1516         /* Contacts list size */
1517         empathy_conf_notify_add (conf,
1518                                  EMPATHY_PREFS_UI_COMPACT_CONTACT_LIST,
1519                                  (EmpathyConfNotifyFunc) main_window_notify_contact_list_size_cb,
1520                                  window);
1521         empathy_conf_notify_add (conf,
1522                                  EMPATHY_PREFS_UI_SHOW_AVATARS,
1523                                  (EmpathyConfNotifyFunc) main_window_notify_contact_list_size_cb,
1524                                  window);
1525
1526         main_window_notify_contact_list_size_cb (conf,
1527                                                  EMPATHY_PREFS_UI_SHOW_AVATARS,
1528                                                  window);
1529
1530         return window->window;
1531 }
1532