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