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