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