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