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