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