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