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