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