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