]> git.0d.be Git - empathy.git/blob - src/empathy-main-window.c
Updated Polish translation
[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)), NULL, NULL,
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_widget_show (label);
428         g_free (str);
429
430         content_area = gtk_info_bar_get_content_area (GTK_INFO_BAR (info_bar));
431         gtk_box_pack_start (GTK_BOX (content_area), image, FALSE, FALSE, 0);
432         gtk_box_pack_start (GTK_BOX (content_area), label, TRUE, TRUE, 0);
433
434         image = gtk_image_new_from_stock (GTK_STOCK_REFRESH, GTK_ICON_SIZE_BUTTON);
435         retry_button = gtk_button_new ();
436         gtk_button_set_image (GTK_BUTTON (retry_button), image);
437         gtk_widget_set_tooltip_text (retry_button, _("Reconnect"));
438         gtk_widget_show (retry_button);
439
440         image = gtk_image_new_from_stock (GTK_STOCK_EDIT, GTK_ICON_SIZE_BUTTON);
441         edit_button = gtk_button_new ();
442         gtk_button_set_image (GTK_BUTTON (edit_button), image);
443         gtk_widget_set_tooltip_text (edit_button, _("Edit Account"));
444         gtk_widget_show (edit_button);
445
446         image = gtk_image_new_from_stock (GTK_STOCK_CLOSE, GTK_ICON_SIZE_BUTTON);
447         close_button = gtk_button_new ();
448         gtk_button_set_image (GTK_BUTTON (close_button), image);
449         gtk_widget_set_tooltip_text (close_button, _("Close"));
450         gtk_widget_show (close_button);
451
452         action_table = gtk_table_new (1, 3, FALSE);
453         gtk_table_set_col_spacings (GTK_TABLE (action_table), 2);
454         gtk_widget_show (action_table);
455
456         action_area = gtk_info_bar_get_action_area (GTK_INFO_BAR (info_bar));
457         gtk_box_pack_start (GTK_BOX (action_area), action_table, FALSE, FALSE, 0);
458
459         gtk_table_attach (GTK_TABLE (action_table), retry_button, 0, 1, 0, 1,
460                                                                                 (GtkAttachOptions) (GTK_SHRINK),
461                                                                                 (GtkAttachOptions) (GTK_SHRINK), 0, 0);
462         gtk_table_attach (GTK_TABLE (action_table), edit_button, 1, 2, 0, 1,
463                                                                                 (GtkAttachOptions) (GTK_SHRINK),
464                                                                                 (GtkAttachOptions) (GTK_SHRINK), 0, 0);
465         gtk_table_attach (GTK_TABLE (action_table), close_button, 2, 3, 0, 1,
466                                                                                 (GtkAttachOptions) (GTK_SHRINK),
467                                                                                 (GtkAttachOptions) (GTK_SHRINK), 0, 0);
468
469         g_object_set_data (G_OBJECT (info_bar), "label", label);
470         g_object_set_data_full (G_OBJECT (info_bar),
471                                 "account", g_object_ref (account),
472                                 g_object_unref);
473         g_object_set_data_full (G_OBJECT (edit_button),
474                                 "account", g_object_ref (account),
475                                 g_object_unref);
476         g_object_set_data_full (G_OBJECT (close_button),
477                                 "account", g_object_ref (account),
478                                 g_object_unref);
479         g_object_set_data_full (G_OBJECT (retry_button),
480                                 "account", g_object_ref (account),
481                                 g_object_unref);
482
483         g_signal_connect (edit_button, "clicked",
484                           G_CALLBACK (main_window_error_edit_clicked_cb),
485                           window);
486         g_signal_connect (close_button, "clicked",
487                           G_CALLBACK (main_window_error_close_clicked_cb),
488                           window);
489         g_signal_connect (retry_button, "clicked",
490                           G_CALLBACK (main_window_error_retry_clicked_cb),
491                           window);
492
493         gtk_widget_show (window->errors_vbox);
494
495         g_hash_table_insert (window->errors, g_object_ref (account), info_bar);
496 }
497
498 static void
499 main_window_update_status (EmpathyMainWindow *window)
500 {
501         gboolean connected, connecting;
502         GList *l;
503
504         connected = empathy_account_manager_get_accounts_connected (&connecting);
505
506         /* Update the spinner state */
507         if (connecting) {
508                 ephy_spinner_start (EPHY_SPINNER (window->throbber));
509         } else {
510                 ephy_spinner_stop (EPHY_SPINNER (window->throbber));
511         }
512
513         /* Update widgets sensibility */
514         for (l = window->actions_connected; l; l = l->next) {
515                 gtk_action_set_sensitive (l->data, connected);
516         }
517 }
518
519 static void
520 main_window_connection_changed_cb (TpAccount  *account,
521                                    guint       old_status,
522                                    guint       current,
523                                    guint       reason,
524                                    gchar      *dbus_error_name,
525                                    GHashTable *details,
526                                    EmpathyMainWindow *window)
527 {
528         main_window_update_status (window);
529
530         if (current == TP_CONNECTION_STATUS_DISCONNECTED &&
531             reason != TP_CONNECTION_STATUS_REASON_REQUESTED) {
532                 const gchar *message;
533
534                 message = empathy_status_reason_get_default_message (reason);
535
536                 main_window_error_display (window, account, message);
537         }
538
539         if (current == TP_CONNECTION_STATUS_DISCONNECTED) {
540                 empathy_sound_play (GTK_WIDGET (window->window),
541                                     EMPATHY_SOUND_ACCOUNT_DISCONNECTED);
542         }
543
544         if (current == TP_CONNECTION_STATUS_CONNECTED) {
545                 empathy_sound_play (GTK_WIDGET (window->window),
546                                     EMPATHY_SOUND_ACCOUNT_CONNECTED);
547
548                 /* Account connected without error, remove error message if any */
549                 main_window_remove_error (window, account);
550         }
551 }
552
553 static void
554 main_window_contact_presence_changed_cb (EmpathyContactMonitor *monitor,
555                                          EmpathyContact *contact,
556                                          TpConnectionPresenceType current,
557                                          TpConnectionPresenceType previous,
558                                          EmpathyMainWindow *window)
559 {
560   TpAccount *account;
561   gboolean should_play = FALSE;
562   EmpathyIdle *idle;
563
564   account = empathy_contact_get_account (contact);
565   idle = empathy_idle_dup_singleton ();
566
567   should_play = !empathy_idle_account_is_just_connected (idle, account);
568
569   if (!should_play)
570     goto out;
571
572   if (tp_connection_presence_type_cmp_availability (previous,
573      TP_CONNECTION_PRESENCE_TYPE_OFFLINE) > 0)
574     {
575       /* contact was online */
576       if (tp_connection_presence_type_cmp_availability (current,
577           TP_CONNECTION_PRESENCE_TYPE_OFFLINE) <= 0)
578         /* someone is logging off */
579         empathy_sound_play (GTK_WIDGET (window->window),
580           EMPATHY_SOUND_CONTACT_DISCONNECTED);
581     }
582   else
583     {
584       /* contact was offline */
585       if (tp_connection_presence_type_cmp_availability (current,
586           TP_CONNECTION_PRESENCE_TYPE_OFFLINE) > 0)
587         /* someone is logging in */
588         empathy_sound_play (GTK_WIDGET (window->window),
589           EMPATHY_SOUND_CONTACT_CONNECTED);
590     }
591
592 out:
593   g_object_unref (idle);
594 }
595
596 static void
597 main_window_accels_load (void)
598 {
599         gchar *filename;
600
601         filename = g_build_filename (g_get_user_config_dir (), PACKAGE_NAME, ACCELS_FILENAME, NULL);
602         if (g_file_test (filename, G_FILE_TEST_EXISTS)) {
603                 DEBUG ("Loading from:'%s'", filename);
604                 gtk_accel_map_load (filename);
605         }
606
607         g_free (filename);
608 }
609
610 static void
611 main_window_accels_save (void)
612 {
613         gchar *dir;
614         gchar *file_with_path;
615
616         dir = g_build_filename (g_get_user_config_dir (), PACKAGE_NAME, NULL);
617         g_mkdir_with_parents (dir, S_IRUSR | S_IWUSR | S_IXUSR);
618         file_with_path = g_build_filename (dir, ACCELS_FILENAME, NULL);
619         g_free (dir);
620
621         DEBUG ("Saving to:'%s'", file_with_path);
622         gtk_accel_map_save (file_with_path);
623
624         g_free (file_with_path);
625 }
626
627 static void
628 main_window_destroy_cb (GtkWidget         *widget,
629                         EmpathyMainWindow *window)
630 {
631         GHashTableIter iter;
632         gpointer key, value;
633
634         /* Save user-defined accelerators. */
635         main_window_accels_save ();
636
637         g_list_free (window->actions_connected);
638
639         g_object_unref (window->account_manager);
640         g_object_unref (window->list_store);
641         g_hash_table_destroy (window->errors);
642
643         /* disconnect all handlers of status-changed signal */
644         g_hash_table_iter_init (&iter, window->status_changed_handlers);
645         while (g_hash_table_iter_next (&iter, &key, &value))
646                 g_signal_handler_disconnect (TP_ACCOUNT (key),
647                                              GPOINTER_TO_UINT (value));
648
649         g_hash_table_destroy (window->status_changed_handlers);
650
651         g_signal_handlers_disconnect_by_func (window->event_manager,
652                                               main_window_event_added_cb,
653                                               window);
654         g_signal_handlers_disconnect_by_func (window->event_manager,
655                                               main_window_event_removed_cb,
656                                               window);
657         g_object_unref (window->event_manager);
658         g_object_unref (window->ui_manager);
659         g_object_unref (window->chatroom_manager);
660
661         g_free (window);
662 }
663
664 static void
665 main_window_chat_quit_cb (GtkAction         *action,
666                           EmpathyMainWindow *window)
667 {
668         gtk_main_quit ();
669 }
670
671 static void
672 main_window_view_history_cb (GtkAction         *action,
673                              EmpathyMainWindow *window)
674 {
675         empathy_log_window_show (NULL, NULL, FALSE, GTK_WINDOW (window->window));
676 }
677
678 static void
679 main_window_chat_new_message_cb (GtkAction         *action,
680                                  EmpathyMainWindow *window)
681 {
682         empathy_new_message_dialog_show (GTK_WINDOW (window->window));
683 }
684
685 static void
686 main_window_chat_new_call_cb (GtkAction         *action,
687                                  EmpathyMainWindow *window)
688 {
689         empathy_new_call_dialog_show (GTK_WINDOW (window->window));
690 }
691
692 static void
693 main_window_chat_add_contact_cb (GtkAction         *action,
694                                  EmpathyMainWindow *window)
695 {
696         empathy_new_contact_dialog_show (GTK_WINDOW (window->window));
697 }
698
699 static void
700 main_window_view_show_ft_manager (GtkAction         *action,
701                                   EmpathyMainWindow *window)
702 {
703         empathy_ft_manager_show ();
704 }
705
706 static void
707 main_window_view_show_offline_cb (GtkToggleAction   *action,
708                                   EmpathyMainWindow *window)
709 {
710         gboolean current;
711
712         current = gtk_toggle_action_get_active (action);
713         empathy_conf_set_bool (empathy_conf_get (),
714                               EMPATHY_PREFS_CONTACTS_SHOW_OFFLINE,
715                               current);
716
717         /* Turn off sound just while we alter the contact list. */
718         // FIXME: empathy_sound_set_enabled (FALSE);
719         empathy_contact_list_store_set_show_offline (window->list_store, current);
720         //empathy_sound_set_enabled (TRUE);
721 }
722
723 static void
724 main_window_notify_sort_contact_cb (EmpathyConf       *conf,
725                                     const gchar       *key,
726                                     EmpathyMainWindow *window)
727 {
728         gchar *str = NULL;
729
730         if (empathy_conf_get_string (conf, key, &str) && str) {
731                 GType       type;
732                 GEnumClass *enum_class;
733                 GEnumValue *enum_value;
734
735                 type = empathy_contact_list_store_sort_get_type ();
736                 enum_class = G_ENUM_CLASS (g_type_class_peek (type));
737                 enum_value = g_enum_get_value_by_nick (enum_class, str);
738                 if (enum_value) {
739                         /* By changing the value of the GtkRadioAction,
740                            it emits a signal that calls main_window_view_sort_contacts_cb
741                            which updates the contacts list */
742                         gtk_radio_action_set_current_value (window->sort_by_name,
743                                                             enum_value->value);
744                 } else {
745                         g_warning ("Wrong value for sort_criterium configuration : %s", str);
746                 }
747                 g_free (str);
748         }
749 }
750
751 static void
752 main_window_view_sort_contacts_cb (GtkRadioAction    *action,
753                                    GtkRadioAction    *current,
754                                    EmpathyMainWindow *window)
755 {
756         EmpathyContactListStoreSort value;
757         GSList      *group;
758         GType        type;
759         GEnumClass  *enum_class;
760         GEnumValue  *enum_value;
761
762         value = gtk_radio_action_get_current_value (action);
763         group = gtk_radio_action_get_group (action);
764
765         /* Get string from index */
766         type = empathy_contact_list_store_sort_get_type ();
767         enum_class = G_ENUM_CLASS (g_type_class_peek (type));
768         enum_value = g_enum_get_value (enum_class, g_slist_index (group, current));
769
770         if (!enum_value) {
771                 g_warning ("No GEnumValue for EmpathyContactListSort with GtkRadioAction index:%d",
772                            g_slist_index (group, action));
773         } else {
774                 empathy_conf_set_string (empathy_conf_get (),
775                                          EMPATHY_PREFS_CONTACTS_SORT_CRITERIUM,
776                                          enum_value->value_nick);
777         }
778         empathy_contact_list_store_set_sort_criterium (window->list_store, value);
779 }
780
781 static void
782 main_window_view_show_protocols_cb (GtkToggleAction *action,
783                                         EmpathyMainWindow *window)
784 {
785         gboolean value;
786
787         value = gtk_toggle_action_get_active (action);
788
789         empathy_conf_set_bool (empathy_conf_get (),
790                                          EMPATHY_PREFS_UI_SHOW_PROTOCOLS,
791                                          value == TRUE);
792         empathy_contact_list_store_set_show_protocols (window->list_store,
793                                          value == TRUE);
794 }
795
796 /* Matches GtkRadioAction values set in empathy-main-window.ui */
797 #define CONTACT_LIST_NORMAL_SIZE_WITH_AVATARS           0
798 #define CONTACT_LIST_NORMAL_SIZE                        1
799 #define CONTACT_LIST_COMPACT_SIZE                       2
800
801 static void
802 main_window_view_contacts_list_size_cb (GtkRadioAction    *action,
803                                         GtkRadioAction    *current,
804                                         EmpathyMainWindow *window)
805 {
806         gint     value;
807
808         value = gtk_radio_action_get_current_value (action);
809
810         empathy_conf_set_bool (empathy_conf_get (),
811                                EMPATHY_PREFS_UI_SHOW_AVATARS,
812                                value == CONTACT_LIST_NORMAL_SIZE_WITH_AVATARS);
813         empathy_conf_set_bool (empathy_conf_get (),
814                                EMPATHY_PREFS_UI_COMPACT_CONTACT_LIST,
815                                value == CONTACT_LIST_COMPACT_SIZE);
816
817         empathy_contact_list_store_set_show_avatars (window->list_store,
818                                                      value == CONTACT_LIST_NORMAL_SIZE_WITH_AVATARS);
819         empathy_contact_list_store_set_is_compact (window->list_store,
820                                                    value == CONTACT_LIST_COMPACT_SIZE);
821 }
822
823 static void main_window_notify_show_protocols_cb (EmpathyConf       *conf,
824                                         const gchar       *key,
825                                         EmpathyMainWindow *window)
826 {
827         gboolean show_protocols;
828
829         if (empathy_conf_get_bool (conf,
830                                    EMPATHY_PREFS_UI_SHOW_PROTOCOLS,
831                                    &show_protocols)) {
832                 gtk_toggle_action_set_active (window->show_protocols,
833                                               show_protocols);
834         }
835 }
836
837
838 static void
839 main_window_notify_contact_list_size_cb (EmpathyConf       *conf,
840                                          const gchar       *key,
841                                          EmpathyMainWindow *window)
842 {
843         gboolean show_avatars;
844         gboolean compact_contact_list;
845         gint value = CONTACT_LIST_NORMAL_SIZE_WITH_AVATARS;
846
847         if (empathy_conf_get_bool (conf,
848                                    EMPATHY_PREFS_UI_SHOW_AVATARS,
849                                    &show_avatars)
850             && empathy_conf_get_bool (conf,
851                                       EMPATHY_PREFS_UI_COMPACT_CONTACT_LIST,
852                                       &compact_contact_list)) {
853                 if (compact_contact_list) {
854                         value = CONTACT_LIST_COMPACT_SIZE;
855                 } else if (show_avatars) {
856                         value = CONTACT_LIST_NORMAL_SIZE_WITH_AVATARS;
857                 } else {
858                         value = CONTACT_LIST_NORMAL_SIZE;
859                 }
860         }
861         /* By changing the value of the GtkRadioAction,
862            it emits a signal that calls main_window_view_contacts_list_size_cb
863            which updates the contacts list */
864         gtk_radio_action_set_current_value (window->normal_with_avatars, value);
865 }
866
867 static void
868 main_window_view_show_map_cb (GtkCheckMenuItem  *item,
869                               EmpathyMainWindow *window)
870 {
871 #if HAVE_LIBCHAMPLAIN
872         empathy_map_view_show ();
873 #endif
874 }
875
876 static void
877 main_window_favorite_chatroom_join (EmpathyChatroom *chatroom)
878 {
879         TpAccount      *account;
880         TpConnection   *connection;
881         const gchar    *room;
882
883         account = empathy_chatroom_get_account (chatroom);
884         connection = tp_account_get_connection (account);
885         room = empathy_chatroom_get_room (chatroom);
886
887         if (connection != NULL) {
888                 DEBUG ("Requesting channel for '%s'", room);
889                 empathy_dispatcher_join_muc (connection, room, NULL, NULL);
890         }
891 }
892
893 static void
894 main_window_favorite_chatroom_menu_activate_cb (GtkMenuItem    *menu_item,
895                                                 EmpathyChatroom *chatroom)
896 {
897         main_window_favorite_chatroom_join (chatroom);
898 }
899
900 static void
901 main_window_favorite_chatroom_menu_add (EmpathyMainWindow *window,
902                                         EmpathyChatroom    *chatroom)
903 {
904         GtkWidget   *menu_item;
905         const gchar *name;
906
907         if (g_object_get_data (G_OBJECT (chatroom), "menu_item")) {
908                 return;
909         }
910
911         name = empathy_chatroom_get_name (chatroom);
912         menu_item = gtk_menu_item_new_with_label (name);
913
914         g_object_set_data (G_OBJECT (chatroom), "menu_item", menu_item);
915         g_signal_connect (menu_item, "activate",
916                           G_CALLBACK (main_window_favorite_chatroom_menu_activate_cb),
917                           chatroom);
918
919         gtk_menu_shell_insert (GTK_MENU_SHELL (window->room_menu),
920                                menu_item, 4);
921
922         gtk_widget_show (menu_item);
923 }
924
925 static void
926 main_window_favorite_chatroom_menu_added_cb (EmpathyChatroomManager *manager,
927                                              EmpathyChatroom        *chatroom,
928                                              EmpathyMainWindow     *window)
929 {
930         main_window_favorite_chatroom_menu_add (window, chatroom);
931         gtk_widget_show (window->room_separator);
932         gtk_action_set_sensitive (window->room_join_favorites, TRUE);
933 }
934
935 static void
936 main_window_favorite_chatroom_menu_removed_cb (EmpathyChatroomManager *manager,
937                                                EmpathyChatroom        *chatroom,
938                                                EmpathyMainWindow     *window)
939 {
940         GtkWidget *menu_item;
941         GList *chatrooms;
942
943         menu_item = g_object_get_data (G_OBJECT (chatroom), "menu_item");
944         g_object_set_data (G_OBJECT (chatroom), "menu_item", NULL);
945         gtk_widget_destroy (menu_item);
946
947         chatrooms = empathy_chatroom_manager_get_chatrooms (window->chatroom_manager, NULL);
948         if (chatrooms) {
949                 gtk_widget_show (window->room_separator);
950         } else {
951                 gtk_widget_hide (window->room_separator);
952         }
953
954         gtk_action_set_sensitive (window->room_join_favorites, chatrooms != NULL);
955         g_list_free (chatrooms);
956 }
957
958 static void
959 main_window_favorite_chatroom_menu_setup (EmpathyMainWindow *window)
960 {
961         GList *chatrooms, *l;
962         GtkWidget *room;
963
964         window->chatroom_manager = empathy_chatroom_manager_dup_singleton (NULL);
965         chatrooms = empathy_chatroom_manager_get_chatrooms (window->chatroom_manager, NULL);
966         room = gtk_ui_manager_get_widget (window->ui_manager,
967                 "/menubar/room");
968         window->room_menu = gtk_menu_item_get_submenu (GTK_MENU_ITEM (room));
969         window->room_separator = gtk_ui_manager_get_widget (window->ui_manager,
970                 "/menubar/room/room_separator");
971
972         for (l = chatrooms; l; l = l->next) {
973                 main_window_favorite_chatroom_menu_add (window, l->data);
974         }
975
976         if (!chatrooms) {
977                 gtk_widget_hide (window->room_separator);
978         }
979
980         gtk_action_set_sensitive (window->room_join_favorites, chatrooms != NULL);
981
982         g_signal_connect (window->chatroom_manager, "chatroom-added",
983                           G_CALLBACK (main_window_favorite_chatroom_menu_added_cb),
984                           window);
985         g_signal_connect (window->chatroom_manager, "chatroom-removed",
986                           G_CALLBACK (main_window_favorite_chatroom_menu_removed_cb),
987                           window);
988
989         g_list_free (chatrooms);
990 }
991
992 static void
993 main_window_room_join_new_cb (GtkAction         *action,
994                               EmpathyMainWindow *window)
995 {
996         empathy_new_chatroom_dialog_show (GTK_WINDOW (window->window));
997 }
998
999 static void
1000 main_window_room_join_favorites_cb (GtkAction         *action,
1001                                     EmpathyMainWindow *window)
1002 {
1003         GList *chatrooms, *l;
1004
1005         chatrooms = empathy_chatroom_manager_get_chatrooms (window->chatroom_manager, NULL);
1006         for (l = chatrooms; l; l = l->next) {
1007                 main_window_favorite_chatroom_join (l->data);
1008         }
1009         g_list_free (chatrooms);
1010 }
1011
1012 static void
1013 main_window_room_manage_favorites_cb (GtkAction         *action,
1014                                       EmpathyMainWindow *window)
1015 {
1016         empathy_chatrooms_window_show (GTK_WINDOW (window->window));
1017 }
1018
1019 static void
1020 main_window_edit_cb (GtkAction *action,
1021                      EmpathyMainWindow *window)
1022 {
1023         GtkWidget *submenu;
1024
1025         /* FIXME: It should use the UIManager to merge the contact/group submenu */
1026         submenu = empathy_contact_list_view_get_contact_menu (window->list_view);
1027         if (submenu) {
1028                 GtkMenuItem *item;
1029                 GtkWidget   *label;
1030
1031                 item = GTK_MENU_ITEM (window->edit_context);
1032                 label = gtk_bin_get_child (GTK_BIN (item));
1033                 gtk_label_set_text (GTK_LABEL (label), _("Contact"));
1034
1035                 gtk_widget_show (window->edit_context);
1036                 gtk_widget_show (window->edit_context_separator);
1037
1038                 gtk_menu_item_set_submenu (item, submenu);
1039
1040                 return;
1041         }
1042
1043         submenu = empathy_contact_list_view_get_group_menu (window->list_view);
1044         if (submenu) {
1045                 GtkMenuItem *item;
1046                 GtkWidget   *label;
1047
1048                 item = GTK_MENU_ITEM (window->edit_context);
1049                 label = gtk_bin_get_child (GTK_BIN (item));
1050                 gtk_label_set_text (GTK_LABEL (label), _("Group"));
1051
1052                 gtk_widget_show (window->edit_context);
1053                 gtk_widget_show (window->edit_context_separator);
1054
1055                 gtk_menu_item_set_submenu (item, submenu);
1056
1057                 return;
1058         }
1059
1060         gtk_widget_hide (window->edit_context);
1061         gtk_widget_hide (window->edit_context_separator);
1062
1063         return;
1064 }
1065
1066 static void
1067 main_window_edit_accounts_cb (GtkAction         *action,
1068                               EmpathyMainWindow *window)
1069 {
1070         empathy_accounts_dialog_show_application (gdk_screen_get_default (),
1071                         NULL, NULL, NULL, FALSE, FALSE);
1072 }
1073
1074 static void
1075 main_window_edit_personal_information_cb (GtkAction         *action,
1076                                           EmpathyMainWindow *window)
1077 {
1078         empathy_contact_personal_dialog_show (GTK_WINDOW (window->window));
1079 }
1080
1081 static void
1082 main_window_edit_preferences_cb (GtkAction         *action,
1083                                  EmpathyMainWindow *window)
1084 {
1085         empathy_preferences_show (GTK_WINDOW (window->window));
1086 }
1087
1088 static void
1089 main_window_help_about_cb (GtkAction         *action,
1090                            EmpathyMainWindow *window)
1091 {
1092         empathy_about_dialog_new (GTK_WINDOW (window->window));
1093 }
1094
1095 static void
1096 main_window_help_debug_cb (GtkAction         *action,
1097                            EmpathyMainWindow *window)
1098 {
1099         GdkScreen *screen = gdk_screen_get_default ();
1100         GError *error = NULL;
1101         gchar *argv[2] = { NULL, };
1102         gint i = 0;
1103         gchar *path;
1104
1105         g_return_if_fail (GDK_IS_SCREEN (screen));
1106
1107         /* Try to run from source directory if possible */
1108         path = g_build_filename (g_getenv ("EMPATHY_SRCDIR"), "src",
1109                         "empathy-debugger", NULL);
1110
1111         if (!g_file_test (path, G_FILE_TEST_EXISTS)) {
1112                 g_free (path);
1113                 path = g_build_filename (BIN_DIR, "empathy-debugger", NULL);
1114         }
1115
1116         argv[i++] = path;
1117
1118         gdk_spawn_on_screen (screen, NULL, argv, NULL,
1119                         G_SPAWN_SEARCH_PATH | G_SPAWN_DO_NOT_REAP_CHILD,
1120                         NULL, NULL, NULL, &error);
1121
1122         if (error) {
1123                 g_warning ("Failed to open debug window: %s", error->message);
1124                 g_error_free (error);
1125         }
1126
1127         g_free (path);
1128 }
1129
1130 static void
1131 main_window_help_contents_cb (GtkAction         *action,
1132                               EmpathyMainWindow *window)
1133 {
1134         empathy_url_show (window->window, "ghelp:empathy");
1135 }
1136
1137 static gboolean
1138 main_window_throbber_button_press_event_cb (GtkWidget         *throbber_ebox,
1139                                             GdkEventButton    *event,
1140                                             EmpathyMainWindow *window)
1141 {
1142         if (event->type != GDK_BUTTON_PRESS ||
1143             event->button != 1) {
1144                 return FALSE;
1145         }
1146
1147         empathy_accounts_dialog_show_application (
1148                         gtk_widget_get_screen (GTK_WIDGET (throbber_ebox)),
1149                         NULL, NULL, NULL, FALSE, FALSE);
1150
1151         return FALSE;
1152 }
1153
1154 static void
1155 main_window_account_removed_cb (TpAccountManager  *manager,
1156                                 TpAccount         *account,
1157                                 EmpathyMainWindow *window)
1158 {
1159         GList *a;
1160
1161         a = tp_account_manager_get_valid_accounts (manager);
1162
1163         gtk_action_set_sensitive (window->view_history,
1164                 g_list_length (a) > 0);
1165
1166         g_list_free (a);
1167
1168         /* remove errors if any */
1169         main_window_remove_error (window, account);
1170 }
1171
1172 static void
1173 main_window_account_validity_changed_cb (TpAccountManager *manager,
1174                                          TpAccount *account,
1175                                          gboolean valid,
1176                                          EmpathyMainWindow *window)
1177 {
1178         if (valid) {
1179                 gulong handler_id;
1180                 handler_id = GPOINTER_TO_UINT (g_hash_table_lookup (
1181                         window->status_changed_handlers, account));
1182
1183                 /* connect signal only if it was not connected yet */
1184                 if (handler_id == 0) {
1185                         handler_id = g_signal_connect (account,
1186                                 "status-changed",
1187                                 G_CALLBACK (main_window_connection_changed_cb),
1188                                 window);
1189                         g_hash_table_insert (window->status_changed_handlers,
1190                                 account, GUINT_TO_POINTER (handler_id));
1191                 }
1192         }
1193
1194         main_window_account_removed_cb (manager, account, window);
1195 }
1196
1197 static void
1198 main_window_notify_show_offline_cb (EmpathyConf *conf,
1199                                     const gchar *key,
1200                                     gpointer     toggle_action)
1201 {
1202         gboolean show_offline;
1203
1204         if (empathy_conf_get_bool (conf, key, &show_offline)) {
1205                 gtk_toggle_action_set_active (toggle_action, show_offline);
1206         }
1207 }
1208
1209 static void
1210 main_window_connection_items_setup (EmpathyMainWindow *window,
1211                                     GtkBuilder        *gui)
1212 {
1213         GList         *list;
1214         GObject       *action;
1215         guint          i;
1216         const gchar *actions_connected[] = {
1217                 "room",
1218                 "chat_new_message",
1219                 "chat_new_call",
1220                 "chat_add_contact",
1221                 "edit_personal_information"
1222         };
1223
1224         for (i = 0, list = NULL; i < G_N_ELEMENTS (actions_connected); i++) {
1225                 action = gtk_builder_get_object (gui, actions_connected[i]);
1226                 list = g_list_prepend (list, action);
1227         }
1228
1229         window->actions_connected = list;
1230 }
1231
1232 GtkWidget *
1233 empathy_main_window_get (void)
1234 {
1235   return main_window != NULL ? main_window->window : NULL;
1236 }
1237
1238 static void
1239 account_manager_prepared_cb (GObject *source_object,
1240                              GAsyncResult *result,
1241                              gpointer user_data)
1242 {
1243         GList *accounts, *j;
1244         TpAccountManager *manager = TP_ACCOUNT_MANAGER (source_object);
1245         EmpathyMainWindow *window = user_data;
1246         GError *error = NULL;
1247
1248         if (!tp_account_manager_prepare_finish (manager, result, &error)) {
1249                 DEBUG ("Failed to prepare account manager: %s", error->message);
1250                 g_error_free (error);
1251                 return;
1252         }
1253
1254         accounts = tp_account_manager_get_valid_accounts (window->account_manager);
1255         for (j = accounts; j != NULL; j = j->next) {
1256                 TpAccount *account = TP_ACCOUNT (j->data);
1257                 gulong handler_id;
1258
1259                 handler_id = g_signal_connect (account, "status-changed",
1260                                   G_CALLBACK (main_window_connection_changed_cb),
1261                                   window);
1262                 g_hash_table_insert (window->status_changed_handlers,
1263                                      account, GUINT_TO_POINTER (handler_id));
1264         }
1265
1266         g_signal_connect (manager, "account-validity-changed",
1267                           G_CALLBACK (main_window_account_validity_changed_cb),
1268                           window);
1269
1270         main_window_update_status (window);
1271
1272         /* Disable the "Previous Conversations" menu entry if there is no account */
1273         gtk_action_set_sensitive (window->view_history,
1274                 g_list_length (accounts) > 0);
1275
1276         g_list_free (accounts);
1277 }
1278
1279 static void
1280 main_window_contact_added_cb (EmpathyContactMonitor     *monitor,
1281                               EmpathyContact            *contact,
1282                               EmpathyMainWindow         *window)
1283 {
1284         if (!empathy_migrate_butterfly_logs (contact)) {
1285                 g_signal_handler_disconnect (monitor,
1286                                              window->butterfly_log_migration_contact_added_id);
1287                 window->butterfly_log_migration_contact_added_id = 0;
1288         }
1289 }
1290
1291 GtkWidget *
1292 empathy_main_window_show (void)
1293 {
1294         EmpathyMainWindow        *window;
1295         EmpathyContactList       *list_iface;
1296         EmpathyContactMonitor    *monitor;
1297         GtkBuilder               *gui;
1298         EmpathyConf              *conf;
1299         GtkWidget                *sw;
1300         GtkToggleAction          *show_offline_widget;
1301         GtkWidget                *ebox;
1302         GtkAction                *show_map_widget;
1303         GtkToolItem              *item;
1304         gboolean                  show_offline;
1305         gchar                    *filename;
1306         GSList                   *l;
1307
1308         if (main_window) {
1309                 empathy_window_present (GTK_WINDOW (main_window->window));
1310                 return main_window->window;
1311         }
1312
1313         main_window = g_new0 (EmpathyMainWindow, 1);
1314         window = main_window;
1315
1316         /* Set up interface */
1317         filename = empathy_file_lookup ("empathy-main-window.ui", "src");
1318         gui = empathy_builder_get_file (filename,
1319                                        "main_window", &window->window,
1320                                        "main_vbox", &window->main_vbox,
1321                                        "errors_vbox", &window->errors_vbox,
1322                                        "ui_manager", &window->ui_manager,
1323                                        "view_show_offline", &show_offline_widget,
1324                                        "view_show_protocols", &window->show_protocols,
1325                                        "view_sort_by_name", &window->sort_by_name,
1326                                        "view_sort_by_status", &window->sort_by_status,
1327                                        "view_normal_size_with_avatars", &window->normal_with_avatars,
1328                                        "view_normal_size", &window->normal_size,
1329                                        "view_compact_size", &window->compact_size,
1330                                        "view_history", &window->view_history,
1331                                        "view_show_map", &show_map_widget,
1332                                        "room_join_favorites", &window->room_join_favorites,
1333                                        "presence_toolbar", &window->presence_toolbar,
1334                                        "roster_scrolledwindow", &sw,
1335                                        NULL);
1336         g_free (filename);
1337
1338         empathy_builder_connect (gui, window,
1339                               "main_window", "destroy", main_window_destroy_cb,
1340                               "chat_quit", "activate", main_window_chat_quit_cb,
1341                               "chat_new_message", "activate", main_window_chat_new_message_cb,
1342                               "chat_new_call", "activate", main_window_chat_new_call_cb,
1343                               "view_history", "activate", main_window_view_history_cb,
1344                               "room_join_new", "activate", main_window_room_join_new_cb,
1345                               "room_join_favorites", "activate", main_window_room_join_favorites_cb,
1346                               "room_manage_favorites", "activate", main_window_room_manage_favorites_cb,
1347                               "chat_add_contact", "activate", main_window_chat_add_contact_cb,
1348                               "view_show_ft_manager", "activate", main_window_view_show_ft_manager,
1349                               "view_show_offline", "toggled", main_window_view_show_offline_cb,
1350                               "view_show_protocols", "toggled", main_window_view_show_protocols_cb,
1351                               "view_sort_by_name", "changed", main_window_view_sort_contacts_cb,
1352                               "view_normal_size_with_avatars", "changed", main_window_view_contacts_list_size_cb,
1353                               "view_show_map", "activate", main_window_view_show_map_cb,
1354                               "edit", "activate", main_window_edit_cb,
1355                               "edit_accounts", "activate", main_window_edit_accounts_cb,
1356                               "edit_personal_information", "activate", main_window_edit_personal_information_cb,
1357                               "edit_preferences", "activate", main_window_edit_preferences_cb,
1358                               "help_about", "activate", main_window_help_about_cb,
1359                               "help_debug", "activate", main_window_help_debug_cb,
1360                               "help_contents", "activate", main_window_help_contents_cb,
1361                               NULL);
1362
1363         /* Set up connection related widgets. */
1364         main_window_connection_items_setup (window, gui);
1365
1366         g_object_ref (window->ui_manager);
1367         g_object_unref (gui);
1368
1369 #if !HAVE_LIBCHAMPLAIN
1370         gtk_action_set_visible (show_map_widget, FALSE);
1371 #endif
1372
1373         window->account_manager = tp_account_manager_dup ();
1374
1375         tp_account_manager_prepare_async (window->account_manager, NULL,
1376                                           account_manager_prepared_cb, window);
1377
1378         window->errors = g_hash_table_new_full (g_direct_hash,
1379                                                 g_direct_equal,
1380                                                 g_object_unref,
1381                                                 NULL);
1382
1383         window->status_changed_handlers = g_hash_table_new_full (g_direct_hash,
1384                                                                  g_direct_equal,
1385                                                                  NULL,
1386                                                                  NULL);
1387
1388         /* Set up menu */
1389         main_window_favorite_chatroom_menu_setup (window);
1390
1391         window->edit_context = gtk_ui_manager_get_widget (window->ui_manager,
1392                 "/menubar/edit/edit_context");
1393         window->edit_context_separator = gtk_ui_manager_get_widget (window->ui_manager,
1394                 "/menubar/edit/edit_context_separator");
1395         gtk_widget_hide (window->edit_context);
1396         gtk_widget_hide (window->edit_context_separator);
1397
1398         /* Set up contact list. */
1399         empathy_status_presets_get_all ();
1400
1401         /* Set up presence chooser */
1402         window->presence_chooser = empathy_presence_chooser_new ();
1403         gtk_widget_show (window->presence_chooser);
1404         item = gtk_tool_item_new ();
1405         gtk_widget_show (GTK_WIDGET (item));
1406         gtk_container_add (GTK_CONTAINER (item), window->presence_chooser);
1407         gtk_tool_item_set_is_important (item, TRUE);
1408         gtk_tool_item_set_expand (item, TRUE);
1409         gtk_toolbar_insert (GTK_TOOLBAR (window->presence_toolbar), item, -1);
1410
1411         /* Set up the throbber */
1412         ebox = gtk_event_box_new ();
1413         gtk_event_box_set_visible_window (GTK_EVENT_BOX (ebox), FALSE);
1414         gtk_widget_set_tooltip_text (ebox, _("Show and edit accounts"));
1415         g_signal_connect (ebox,
1416                           "button-press-event",
1417                           G_CALLBACK (main_window_throbber_button_press_event_cb),
1418                           window);
1419         gtk_widget_show (ebox);
1420
1421         window->throbber = ephy_spinner_new ();
1422         ephy_spinner_set_size (EPHY_SPINNER (window->throbber), GTK_ICON_SIZE_LARGE_TOOLBAR);
1423         gtk_container_add (GTK_CONTAINER (ebox), window->throbber);
1424         gtk_widget_show (window->throbber);
1425
1426         item = gtk_tool_item_new ();
1427         gtk_container_add (GTK_CONTAINER (item), ebox);
1428         gtk_toolbar_insert (GTK_TOOLBAR (window->presence_toolbar), item, -1);
1429         gtk_widget_show (GTK_WIDGET (item));
1430
1431         list_iface = EMPATHY_CONTACT_LIST (empathy_contact_manager_dup_singleton ());
1432         monitor = empathy_contact_list_get_monitor (list_iface);
1433         window->list_store = empathy_contact_list_store_new (list_iface);
1434         window->list_view = empathy_contact_list_view_new (window->list_store,
1435                                                            EMPATHY_CONTACT_LIST_FEATURE_ALL,
1436                                                            EMPATHY_CONTACT_FEATURE_ALL);
1437         g_signal_connect (monitor, "contact-presence-changed",
1438                           G_CALLBACK (main_window_contact_presence_changed_cb), window);
1439         window->butterfly_log_migration_contact_added_id =  g_signal_connect (monitor, "contact-added",
1440                           G_CALLBACK (main_window_contact_added_cb), window);
1441         g_object_unref (list_iface);
1442
1443         gtk_widget_show (GTK_WIDGET (window->list_view));
1444         gtk_container_add (GTK_CONTAINER (sw),
1445                            GTK_WIDGET (window->list_view));
1446         g_signal_connect (window->list_view, "row-activated",
1447                           G_CALLBACK (main_window_row_activated_cb),
1448                           window);
1449
1450         /* Load user-defined accelerators. */
1451         main_window_accels_load ();
1452
1453         /* Set window size. */
1454         empathy_geometry_bind (GTK_WINDOW (window->window), GEOMETRY_NAME);
1455
1456         /* Enable event handling */
1457         window->event_manager = empathy_event_manager_dup_singleton ();
1458         g_signal_connect (window->event_manager, "event-added",
1459                           G_CALLBACK (main_window_event_added_cb),
1460                           window);
1461         g_signal_connect (window->event_manager, "event-removed",
1462                           G_CALLBACK (main_window_event_removed_cb),
1463                           window);
1464
1465         g_signal_connect (window->account_manager, "account-validity-changed",
1466                           G_CALLBACK (main_window_account_validity_changed_cb),
1467                           window);
1468         g_signal_connect (window->account_manager, "account-removed",
1469                           G_CALLBACK (main_window_account_removed_cb),
1470                           window);
1471         g_signal_connect (window->account_manager, "account-disabled",
1472                           G_CALLBACK (main_window_account_disabled_cb),
1473                           window);
1474
1475         l = empathy_event_manager_get_events (window->event_manager);
1476         while (l) {
1477                 main_window_event_added_cb (window->event_manager,
1478                                             l->data, window);
1479                 l = l->next;
1480         }
1481
1482         conf = empathy_conf_get ();
1483
1484         /* Show offline ? */
1485         empathy_conf_get_bool (conf,
1486                               EMPATHY_PREFS_CONTACTS_SHOW_OFFLINE,
1487                               &show_offline);
1488         empathy_conf_notify_add (conf,
1489                                 EMPATHY_PREFS_CONTACTS_SHOW_OFFLINE,
1490                                 main_window_notify_show_offline_cb,
1491                                 show_offline_widget);
1492
1493         gtk_toggle_action_set_active (show_offline_widget, show_offline);
1494
1495         /* Show protocol ? */
1496         empathy_conf_notify_add (conf,
1497                                  EMPATHY_PREFS_UI_SHOW_PROTOCOLS,
1498                                  (EmpathyConfNotifyFunc) main_window_notify_show_protocols_cb,
1499                                  window);
1500
1501         main_window_notify_show_protocols_cb (conf,
1502                                             EMPATHY_PREFS_UI_SHOW_PROTOCOLS,
1503                                             window);
1504
1505         /* Sort by name / by status ? */
1506         empathy_conf_notify_add (conf,
1507                                  EMPATHY_PREFS_CONTACTS_SORT_CRITERIUM,
1508                                  (EmpathyConfNotifyFunc) main_window_notify_sort_contact_cb,
1509                                  window);
1510
1511         main_window_notify_sort_contact_cb (conf,
1512                                             EMPATHY_PREFS_CONTACTS_SORT_CRITERIUM,
1513                                             window);
1514
1515         /* Contacts list size */
1516         empathy_conf_notify_add (conf,
1517                                  EMPATHY_PREFS_UI_COMPACT_CONTACT_LIST,
1518                                  (EmpathyConfNotifyFunc) main_window_notify_contact_list_size_cb,
1519                                  window);
1520         empathy_conf_notify_add (conf,
1521                                  EMPATHY_PREFS_UI_SHOW_AVATARS,
1522                                  (EmpathyConfNotifyFunc) main_window_notify_contact_list_size_cb,
1523                                  window);
1524
1525         main_window_notify_contact_list_size_cb (conf,
1526                                                  EMPATHY_PREFS_UI_SHOW_AVATARS,
1527                                                  window);
1528
1529         return window->window;
1530 }
1531