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