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