]> git.0d.be Git - empathy.git/blob - src/empathy-main-window.c
Merge branch 'remember-any-exception'
[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-2010 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  *          Danielle Madeley <danielle.madeley@collabora.co.uk>
23  */
24
25 #include <config.h>
26
27 #include <sys/stat.h>
28 #include <gtk/gtk.h>
29 #include <gdk/gdkkeysyms.h>
30 #include <glib/gi18n.h>
31
32 #include <telepathy-glib/account-manager.h>
33 #include <telepathy-glib/util.h>
34 #include <folks/folks.h>
35
36 #include <libempathy/empathy-contact.h>
37 #include <libempathy/empathy-utils.h>
38 #include <libempathy/empathy-dispatcher.h>
39 #include <libempathy/empathy-chatroom-manager.h>
40 #include <libempathy/empathy-chatroom.h>
41 #include <libempathy/empathy-contact-list.h>
42 #include <libempathy/empathy-contact-manager.h>
43 #include <libempathy/empathy-gsettings.h>
44 #include <libempathy/empathy-individual-manager.h>
45 #include <libempathy/empathy-gsettings.h>
46 #include <libempathy/empathy-status-presets.h>
47 #include <libempathy/empathy-tp-contact-factory.h>
48
49 #include <libempathy-gtk/empathy-contact-dialogs.h>
50 #include <libempathy-gtk/empathy-contact-list-store.h>
51 #include <libempathy-gtk/empathy-contact-list-view.h>
52 #include <libempathy-gtk/empathy-live-search.h>
53 #include <libempathy-gtk/empathy-contact-search-dialog.h>
54 #include <libempathy-gtk/empathy-geometry.h>
55 #include <libempathy-gtk/empathy-gtk-enum-types.h>
56 #include <libempathy-gtk/empathy-individual-dialogs.h>
57 #include <libempathy-gtk/empathy-individual-store.h>
58 #include <libempathy-gtk/empathy-individual-view.h>
59 #include <libempathy-gtk/empathy-new-message-dialog.h>
60 #include <libempathy-gtk/empathy-new-call-dialog.h>
61 #include <libempathy-gtk/empathy-log-window.h>
62 #include <libempathy-gtk/empathy-presence-chooser.h>
63 #include <libempathy-gtk/empathy-sound-manager.h>
64 #include <libempathy-gtk/empathy-ui-utils.h>
65
66 #include "empathy-accounts-dialog.h"
67 #include "empathy-call-observer.h"
68 #include "empathy-chat-manager.h"
69 #include "empathy-main-window.h"
70 #include "empathy-preferences.h"
71 #include "empathy-about-dialog.h"
72 #include "empathy-debug-window.h"
73 #include "empathy-new-chatroom-dialog.h"
74 #include "empathy-map-view.h"
75 #include "empathy-chatrooms-window.h"
76 #include "empathy-event-manager.h"
77 #include "empathy-ft-manager.h"
78 #include "empathy-migrate-butterfly-logs.h"
79
80 #define DEBUG_FLAG EMPATHY_DEBUG_OTHER
81 #include <libempathy/empathy-debug.h>
82
83 /* Flashing delay for icons (milliseconds). */
84 #define FLASH_TIMEOUT 500
85
86 /* Minimum width of roster window if something goes wrong. */
87 #define MIN_WIDTH 50
88
89 /* Accels (menu shortcuts) can be configured and saved */
90 #define ACCELS_FILENAME "accels.txt"
91
92 /* Name in the geometry file */
93 #define GEOMETRY_NAME "main-window"
94
95 enum {
96         PAGE_CONTACT_LIST = 0,
97         PAGE_NO_MATCH
98 };
99
100 G_DEFINE_TYPE (EmpathyMainWindow, empathy_main_window, GTK_TYPE_WINDOW);
101
102 #define GET_PRIV(self) ((EmpathyMainWindowPriv *)((EmpathyMainWindow *) self)->priv)
103
104 struct _EmpathyMainWindowPriv {
105         EmpathyContactList      *contact_manager;
106         EmpathyIndividualStore  *individual_store;
107         EmpathyIndividualView   *individual_view;
108         TpAccountManager        *account_manager;
109         EmpathyChatroomManager  *chatroom_manager;
110         EmpathyEventManager     *event_manager;
111         EmpathySoundManager     *sound_mgr;
112         EmpathyCallObserver     *call_observer;
113         guint                    flash_timeout_id;
114         gboolean                 flash_on;
115         gboolean                 empty;
116
117         GSettings              *gsettings_ui;
118         GSettings              *gsettings_contacts;
119
120         GtkWidget              *preferences;
121         GtkWidget              *main_vbox;
122         GtkWidget              *throbber;
123         GtkWidget              *throbber_tool_item;
124         GtkWidget              *presence_toolbar;
125         GtkWidget              *presence_chooser;
126         GtkWidget              *errors_vbox;
127         GtkWidget              *auth_vbox;
128         GtkWidget              *search_bar;
129         GtkWidget              *notebook;
130         GtkWidget              *no_entry_label;
131
132         GtkToggleAction        *show_protocols;
133         GtkRadioAction         *sort_by_name;
134         GtkRadioAction         *sort_by_status;
135         GtkRadioAction         *normal_with_avatars;
136         GtkRadioAction         *normal_size;
137         GtkRadioAction         *compact_size;
138
139         GtkUIManager           *ui_manager;
140         GtkAction              *view_history;
141         GtkAction              *room_join_favorites;
142         GtkWidget              *room_menu;
143         GtkWidget              *room_separator;
144         GtkWidget              *edit_context;
145         GtkWidget              *edit_context_separator;
146
147         guint                   size_timeout_id;
148
149         /* reffed TpAccount* => visible GtkInfoBar* */
150         GHashTable             *errors;
151
152         /* EmpathyEvent* => visible GtkInfoBar* */
153         GHashTable             *auths;
154
155         /* stores a mapping from TpAccount to Handler ID to prevent
156          * to listen more than once to the status-changed signal */
157         GHashTable             *status_changed_handlers;
158
159         /* Actions that are enabled when there are connected accounts */
160         GList                  *actions_connected;
161
162         /* The idle event source to migrate butterfly's logs */
163         guint butterfly_log_migration_members_changed_id;
164 };
165
166 static void
167 main_window_flash_stop (EmpathyMainWindow *window)
168 {
169         EmpathyMainWindowPriv *priv = GET_PRIV (window);
170
171         if (priv->flash_timeout_id == 0) {
172                 return;
173         }
174
175         DEBUG ("Stop flashing");
176         g_source_remove (priv->flash_timeout_id);
177         priv->flash_timeout_id = 0;
178         priv->flash_on = FALSE;
179 }
180
181 typedef struct {
182         EmpathyEvent       *event;
183         gboolean            on;
184         EmpathyMainWindow  *window;
185 } FlashForeachData;
186
187 static gboolean
188 main_window_flash_foreach (GtkTreeModel *model,
189                            GtkTreePath  *path,
190                            GtkTreeIter  *iter,
191                            gpointer      user_data)
192 {
193         FlashForeachData *data = (FlashForeachData *) user_data;
194         FolksIndividual *individual;
195         EmpathyContact   *contact;
196         const gchar      *icon_name;
197         GtkTreePath      *parent_path = NULL;
198         GtkTreeIter       parent_iter;
199         GdkPixbuf        *pixbuf = NULL;
200
201         gtk_tree_model_get (model, iter,
202                             EMPATHY_INDIVIDUAL_STORE_COL_INDIVIDUAL,
203                                 &individual,
204                             -1);
205
206         if (individual == NULL)
207                 return FALSE;
208
209         contact = empathy_contact_dup_from_folks_individual (individual);
210         if (contact != data->event->contact) {
211                 tp_clear_object (&contact);
212                 return FALSE;
213         }
214
215         if (data->on) {
216                 icon_name = data->event->icon_name;
217                 pixbuf = empathy_pixbuf_from_icon_name (icon_name, GTK_ICON_SIZE_MENU);
218         } else {
219                 pixbuf = empathy_individual_store_get_individual_status_icon (
220                                                 GET_PRIV (data->window)->individual_store,
221                                                 individual);
222         }
223
224         gtk_tree_store_set (GTK_TREE_STORE (model), iter,
225                             EMPATHY_INDIVIDUAL_STORE_COL_ICON_STATUS, pixbuf,
226                             -1);
227
228         /* To make sure the parent is shown correctly, we emit
229          * the row-changed signal on the parent so it prompts
230          * it to be refreshed by the filter func.
231          */
232         if (gtk_tree_model_iter_parent (model, &parent_iter, iter)) {
233                 parent_path = gtk_tree_model_get_path (model, &parent_iter);
234         }
235         if (parent_path) {
236                 gtk_tree_model_row_changed (model, parent_path, &parent_iter);
237                 gtk_tree_path_free (parent_path);
238         }
239
240         g_object_unref (individual);
241         tp_clear_object (&contact);
242
243         return FALSE;
244 }
245
246 static gboolean
247 main_window_flash_cb (EmpathyMainWindow *window)
248 {
249         EmpathyMainWindowPriv *priv = GET_PRIV (window);
250         GtkTreeModel     *model;
251         GSList           *events, *l;
252         gboolean          found_event = FALSE;
253         FlashForeachData  data;
254
255         priv->flash_on = !priv->flash_on;
256         data.on = priv->flash_on;
257         model = GTK_TREE_MODEL (priv->individual_store);
258
259         events = empathy_event_manager_get_events (priv->event_manager);
260         for (l = events; l; l = l->next) {
261                 data.event = l->data;
262                 data.window = window;
263                 if (!data.event->contact || !data.event->must_ack) {
264                         continue;
265                 }
266
267                 found_event = TRUE;
268                 gtk_tree_model_foreach (model,
269                                         main_window_flash_foreach,
270                                         &data);
271         }
272
273         if (!found_event) {
274                 main_window_flash_stop (window);
275         }
276
277         return TRUE;
278 }
279
280 static void
281 main_window_flash_start (EmpathyMainWindow *window)
282 {
283         EmpathyMainWindowPriv *priv = GET_PRIV (window);
284
285         if (priv->flash_timeout_id != 0) {
286                 return;
287         }
288
289         DEBUG ("Start flashing");
290         priv->flash_timeout_id = g_timeout_add (FLASH_TIMEOUT,
291                                                 (GSourceFunc) main_window_flash_cb,
292                                                 window);
293         main_window_flash_cb (window);
294 }
295
296 static void
297 main_window_remove_auth (EmpathyMainWindow *window,
298                          EmpathyEvent      *event)
299 {
300         EmpathyMainWindowPriv *priv = GET_PRIV (window);
301         GtkWidget *error_widget;
302
303         error_widget = g_hash_table_lookup (priv->auths, event);
304         if (error_widget != NULL) {
305                 gtk_widget_destroy (error_widget);
306                 g_hash_table_remove (priv->auths, event);
307         }
308 }
309
310 static void
311 main_window_auth_add_clicked_cb (GtkButton         *button,
312                                  EmpathyMainWindow *window)
313 {
314         EmpathyEvent *event;
315
316         event = g_object_get_data (G_OBJECT (button), "event");
317
318         empathy_event_approve (event);
319
320         main_window_remove_auth (window, event);
321 }
322
323 static void
324 main_window_auth_close_clicked_cb (GtkButton         *button,
325                                    EmpathyMainWindow *window)
326 {
327         EmpathyEvent *event;
328
329         event = g_object_get_data (G_OBJECT (button), "event");
330
331         empathy_event_decline (event);
332         main_window_remove_auth (window, event);
333 }
334
335 static void
336 main_window_auth_display (EmpathyMainWindow *window,
337                           EmpathyEvent      *event)
338 {
339         EmpathyMainWindowPriv *priv = GET_PRIV (window);
340         TpAccount *account = event->account;
341         GtkWidget *info_bar;
342         GtkWidget *content_area;
343         GtkWidget *image;
344         GtkWidget *label;
345         GtkWidget *add_button;
346         GtkWidget *close_button;
347         GtkWidget *action_area;
348         GtkWidget *action_table;
349         const gchar *icon_name;
350         gchar *str;
351
352         if (g_hash_table_lookup (priv->auths, event) != NULL) {
353                 return;
354         }
355
356         info_bar = gtk_info_bar_new ();
357         gtk_info_bar_set_message_type (GTK_INFO_BAR (info_bar), GTK_MESSAGE_QUESTION);
358
359         gtk_widget_set_no_show_all (info_bar, TRUE);
360         gtk_box_pack_start (GTK_BOX (priv->auth_vbox), info_bar, FALSE, TRUE, 0);
361         gtk_widget_show (info_bar);
362
363         icon_name = tp_account_get_icon_name (account);
364         image = gtk_image_new_from_icon_name (icon_name, GTK_ICON_SIZE_SMALL_TOOLBAR);
365         gtk_widget_show (image);
366
367         str = g_markup_printf_escaped ("<b>%s</b>\n%s",
368                                        tp_account_get_display_name (account),
369                                        _("Password required"));
370
371         label = gtk_label_new (str);
372         gtk_label_set_use_markup (GTK_LABEL (label), TRUE);
373         gtk_label_set_ellipsize (GTK_LABEL (label), PANGO_ELLIPSIZE_END);
374         gtk_misc_set_alignment (GTK_MISC (label), 0, 0.5);
375         gtk_widget_show (label);
376
377         g_free (str);
378
379         content_area = gtk_info_bar_get_content_area (GTK_INFO_BAR (info_bar));
380         gtk_box_pack_start (GTK_BOX (content_area), image, FALSE, FALSE, 0);
381         gtk_box_pack_start (GTK_BOX (content_area), label, TRUE, TRUE, 0);
382
383         image = gtk_image_new_from_stock (GTK_STOCK_ADD, GTK_ICON_SIZE_BUTTON);
384         add_button = gtk_button_new ();
385         gtk_button_set_image (GTK_BUTTON (add_button), image);
386         gtk_widget_set_tooltip_text (add_button, _("Provide Password"));
387         gtk_widget_show (add_button);
388
389         image = gtk_image_new_from_stock (GTK_STOCK_CLOSE, GTK_ICON_SIZE_BUTTON);
390         close_button = gtk_button_new ();
391         gtk_button_set_image (GTK_BUTTON (close_button), image);
392         gtk_widget_set_tooltip_text (close_button, _("Disconnect"));
393         gtk_widget_show (close_button);
394
395         action_table = gtk_table_new (1, 2, FALSE);
396         gtk_table_set_col_spacings (GTK_TABLE (action_table), 6);
397         gtk_widget_show (action_table);
398
399         action_area = gtk_info_bar_get_action_area (GTK_INFO_BAR (info_bar));
400         gtk_box_pack_start (GTK_BOX (action_area), action_table, FALSE, FALSE, 0);
401
402         gtk_table_attach (GTK_TABLE (action_table), add_button, 0, 1, 0, 1,
403                           (GtkAttachOptions) (GTK_SHRINK),
404                           (GtkAttachOptions) (GTK_SHRINK), 0, 0);
405         gtk_table_attach (GTK_TABLE (action_table), close_button, 1, 2, 0, 1,
406                           (GtkAttachOptions) (GTK_SHRINK),
407                           (GtkAttachOptions) (GTK_SHRINK), 0, 0);
408
409         g_object_set_data_full (G_OBJECT (info_bar),
410                                 "event", event, NULL);
411         g_object_set_data_full (G_OBJECT (add_button),
412                                 "event", event, NULL);
413         g_object_set_data_full (G_OBJECT (close_button),
414                                 "event", event, NULL);
415
416         g_signal_connect (add_button, "clicked",
417                           G_CALLBACK (main_window_auth_add_clicked_cb),
418                           window);
419         g_signal_connect (close_button, "clicked",
420                           G_CALLBACK (main_window_auth_close_clicked_cb),
421                           window);
422
423         gtk_widget_show (priv->auth_vbox);
424
425         g_hash_table_insert (priv->auths, event, info_bar);
426 }
427
428 static void
429 main_window_event_added_cb (EmpathyEventManager *manager,
430                             EmpathyEvent        *event,
431                             EmpathyMainWindow   *window)
432 {
433         if (event->contact) {
434                 main_window_flash_start (window);
435         } else if (event->type == EMPATHY_EVENT_TYPE_AUTH) {
436                 main_window_auth_display (window, event);
437         }
438 }
439
440 static void
441 main_window_event_removed_cb (EmpathyEventManager *manager,
442                               EmpathyEvent        *event,
443                               EmpathyMainWindow   *window)
444 {
445         EmpathyMainWindowPriv *priv = GET_PRIV (window);
446         FlashForeachData data;
447
448         if (event->type == EMPATHY_EVENT_TYPE_AUTH) {
449                 main_window_remove_auth (window, event);
450                 return;
451         }
452
453         if (!event->contact) {
454                 return;
455         }
456
457         data.on = FALSE;
458         data.event = event;
459         data.window = window;
460         gtk_tree_model_foreach (GTK_TREE_MODEL (priv->individual_store),
461                                 main_window_flash_foreach,
462                                 &data);
463 }
464
465 static void
466 main_window_row_activated_cb (EmpathyContactListView *view,
467                               GtkTreePath            *path,
468                               GtkTreeViewColumn      *col,
469                               EmpathyMainWindow      *window)
470 {
471         EmpathyMainWindowPriv *priv = GET_PRIV (window);
472         EmpathyContact *contact = NULL;
473         FolksIndividual *individual;
474         GtkTreeModel   *model;
475         GtkTreeIter     iter;
476         GSList         *events, *l;
477
478         model = gtk_tree_view_get_model (GTK_TREE_VIEW (priv->individual_view));
479         gtk_tree_model_get_iter (model, &iter, path);
480
481         gtk_tree_model_get (model, &iter,
482                             EMPATHY_INDIVIDUAL_STORE_COL_INDIVIDUAL,
483                                 &individual,
484                             -1);
485
486         if (individual != NULL) {
487                 contact = empathy_contact_dup_from_folks_individual (individual);
488         }
489
490         if (!contact) {
491                 goto OUT;
492         }
493
494         /* If the contact has an event activate it, otherwise the
495          * default handler of row-activated will be called. */
496         events = empathy_event_manager_get_events (priv->event_manager);
497         for (l = events; l; l = l->next) {
498                 EmpathyEvent *event = l->data;
499
500                 if (event->contact == contact) {
501                         DEBUG ("Activate event");
502                         empathy_event_activate (event);
503
504                         /* We don't want the default handler of this signal
505                          * (e.g. open a chat) */
506                         g_signal_stop_emission_by_name (view, "row-activated");
507                         break;
508                 }
509         }
510
511         g_object_unref (contact);
512 OUT:
513         tp_clear_object (&individual);
514 }
515
516 static void
517 main_window_row_deleted_cb (GtkTreeModel      *model,
518                             GtkTreePath       *path,
519                             EmpathyMainWindow *window)
520 {
521         EmpathyMainWindowPriv *priv = GET_PRIV (window);
522         GtkTreeIter help_iter;
523
524         if (!gtk_tree_model_get_iter_first (model, &help_iter)) {
525                 priv->empty = TRUE;
526
527                 if (empathy_individual_view_is_searching (
528                                 priv->individual_view)) {
529                         gchar *tmp;
530
531                         tmp = g_strdup_printf ("<b><span size='xx-large'>%s</span></b>",
532                                 _("No match found"));
533
534                         gtk_label_set_markup (GTK_LABEL (priv->no_entry_label), tmp);
535                         g_free (tmp);
536
537                         gtk_label_set_line_wrap (GTK_LABEL (priv->no_entry_label),
538                                 TRUE);
539
540                         gtk_notebook_set_current_page (
541                                         GTK_NOTEBOOK (priv->notebook), PAGE_NO_MATCH);
542                 }
543         }
544 }
545
546 static void
547 main_window_row_inserted_cb (GtkTreeModel      *model,
548                              GtkTreePath       *path,
549                              GtkTreeIter       *iter,
550                              EmpathyMainWindow *window)
551 {
552         EmpathyMainWindowPriv *priv = GET_PRIV (window);
553
554         if (priv->empty) {
555                 priv->empty = FALSE;
556                 gtk_notebook_set_current_page (GTK_NOTEBOOK (priv->notebook),
557                                 PAGE_CONTACT_LIST);
558                 gtk_widget_grab_focus (GTK_WIDGET (priv->individual_view));
559         }
560 }
561
562 static void
563 main_window_remove_error (EmpathyMainWindow *window,
564                           TpAccount         *account)
565 {
566         EmpathyMainWindowPriv *priv = GET_PRIV (window);
567         GtkWidget *error_widget;
568
569         error_widget = g_hash_table_lookup (priv->errors, account);
570         if (error_widget != NULL) {
571                 gtk_widget_destroy (error_widget);
572                 g_hash_table_remove (priv->errors, account);
573         }
574 }
575
576 static void
577 main_window_account_disabled_cb (TpAccountManager  *manager,
578                                  TpAccount         *account,
579                                  EmpathyMainWindow *window)
580 {
581         main_window_remove_error (window, account);
582 }
583
584 static void
585 main_window_error_retry_clicked_cb (GtkButton         *button,
586                                     EmpathyMainWindow *window)
587 {
588         TpAccount *account;
589
590         account = g_object_get_data (G_OBJECT (button), "account");
591         tp_account_reconnect_async (account, NULL, NULL);
592
593         main_window_remove_error (window, account);
594 }
595
596 static void
597 main_window_error_edit_clicked_cb (GtkButton         *button,
598                                    EmpathyMainWindow *window)
599 {
600         TpAccount *account;
601
602         account = g_object_get_data (G_OBJECT (button), "account");
603
604         empathy_accounts_dialog_show_application (
605                         gtk_widget_get_screen (GTK_WIDGET (button)),
606                         account, FALSE, FALSE);
607
608         main_window_remove_error (window, account);
609 }
610
611 static void
612 main_window_error_close_clicked_cb (GtkButton         *button,
613                                     EmpathyMainWindow *window)
614 {
615         TpAccount *account;
616
617         account = g_object_get_data (G_OBJECT (button), "account");
618         main_window_remove_error (window, account);
619 }
620
621 static void
622 main_window_error_display (EmpathyMainWindow *window,
623                            TpAccount         *account)
624 {
625         EmpathyMainWindowPriv *priv = GET_PRIV (window);
626         GtkWidget *info_bar;
627         GtkWidget *content_area;
628         GtkWidget *label;
629         GtkWidget *image;
630         GtkWidget *retry_button;
631         GtkWidget *edit_button;
632         GtkWidget *close_button;
633         GtkWidget *action_area;
634         GtkWidget *action_table;
635         gchar     *str;
636         const gchar     *icon_name;
637         const gchar *error_message;
638         gboolean user_requested;
639
640         error_message =
641                 empathy_account_get_error_message (account, &user_requested);
642
643         if (user_requested) {
644                 return;
645         }
646
647         str = g_markup_printf_escaped ("<b>%s</b>\n%s",
648                                                tp_account_get_display_name (account),
649                                                error_message);
650
651         info_bar = g_hash_table_lookup (priv->errors, account);
652         if (info_bar) {
653                 label = g_object_get_data (G_OBJECT (info_bar), "label");
654
655                 /* Just set the latest error and return */
656                 gtk_label_set_markup (GTK_LABEL (label), str);
657                 g_free (str);
658
659                 return;
660         }
661
662         info_bar = gtk_info_bar_new ();
663         gtk_info_bar_set_message_type (GTK_INFO_BAR (info_bar), GTK_MESSAGE_ERROR);
664
665         gtk_widget_set_no_show_all (info_bar, TRUE);
666         gtk_box_pack_start (GTK_BOX (priv->errors_vbox), info_bar, FALSE, TRUE, 0);
667         gtk_widget_show (info_bar);
668
669         icon_name = tp_account_get_icon_name (account);
670         image = gtk_image_new_from_icon_name (icon_name, GTK_ICON_SIZE_SMALL_TOOLBAR);
671         gtk_widget_show (image);
672
673         label = gtk_label_new (str);
674         gtk_label_set_use_markup (GTK_LABEL (label), TRUE);
675         gtk_label_set_ellipsize (GTK_LABEL (label), PANGO_ELLIPSIZE_END);
676         gtk_misc_set_alignment (GTK_MISC (label), 0, 0.5);
677         gtk_widget_show (label);
678         g_free (str);
679
680         content_area = gtk_info_bar_get_content_area (GTK_INFO_BAR (info_bar));
681         gtk_box_pack_start (GTK_BOX (content_area), image, FALSE, FALSE, 0);
682         gtk_box_pack_start (GTK_BOX (content_area), label, TRUE, TRUE, 0);
683
684         image = gtk_image_new_from_stock (GTK_STOCK_REFRESH, GTK_ICON_SIZE_BUTTON);
685         retry_button = gtk_button_new ();
686         gtk_button_set_image (GTK_BUTTON (retry_button), image);
687         gtk_widget_set_tooltip_text (retry_button, _("Reconnect"));
688         gtk_widget_show (retry_button);
689
690         image = gtk_image_new_from_stock (GTK_STOCK_EDIT, GTK_ICON_SIZE_BUTTON);
691         edit_button = gtk_button_new ();
692         gtk_button_set_image (GTK_BUTTON (edit_button), image);
693         gtk_widget_set_tooltip_text (edit_button, _("Edit Account"));
694         gtk_widget_show (edit_button);
695
696         image = gtk_image_new_from_stock (GTK_STOCK_CLOSE, GTK_ICON_SIZE_BUTTON);
697         close_button = gtk_button_new ();
698         gtk_button_set_image (GTK_BUTTON (close_button), image);
699         gtk_widget_set_tooltip_text (close_button, _("Close"));
700         gtk_widget_show (close_button);
701
702         action_table = gtk_table_new (1, 3, FALSE);
703         gtk_table_set_col_spacings (GTK_TABLE (action_table), 2);
704         gtk_widget_show (action_table);
705
706         action_area = gtk_info_bar_get_action_area (GTK_INFO_BAR (info_bar));
707         gtk_box_pack_start (GTK_BOX (action_area), action_table, FALSE, FALSE, 0);
708
709         gtk_table_attach (GTK_TABLE (action_table), retry_button, 0, 1, 0, 1,
710                                                                                 (GtkAttachOptions) (GTK_SHRINK),
711                                                                                 (GtkAttachOptions) (GTK_SHRINK), 0, 0);
712         gtk_table_attach (GTK_TABLE (action_table), edit_button, 1, 2, 0, 1,
713                                                                                 (GtkAttachOptions) (GTK_SHRINK),
714                                                                                 (GtkAttachOptions) (GTK_SHRINK), 0, 0);
715         gtk_table_attach (GTK_TABLE (action_table), close_button, 2, 3, 0, 1,
716                                                                                 (GtkAttachOptions) (GTK_SHRINK),
717                                                                                 (GtkAttachOptions) (GTK_SHRINK), 0, 0);
718
719         g_object_set_data (G_OBJECT (info_bar), "label", label);
720         g_object_set_data_full (G_OBJECT (info_bar),
721                                 "account", g_object_ref (account),
722                                 g_object_unref);
723         g_object_set_data_full (G_OBJECT (edit_button),
724                                 "account", g_object_ref (account),
725                                 g_object_unref);
726         g_object_set_data_full (G_OBJECT (close_button),
727                                 "account", g_object_ref (account),
728                                 g_object_unref);
729         g_object_set_data_full (G_OBJECT (retry_button),
730                                 "account", g_object_ref (account),
731                                 g_object_unref);
732
733         g_signal_connect (edit_button, "clicked",
734                           G_CALLBACK (main_window_error_edit_clicked_cb),
735                           window);
736         g_signal_connect (close_button, "clicked",
737                           G_CALLBACK (main_window_error_close_clicked_cb),
738                           window);
739         g_signal_connect (retry_button, "clicked",
740                           G_CALLBACK (main_window_error_retry_clicked_cb),
741                           window);
742
743         gtk_widget_set_tooltip_text (priv->errors_vbox, error_message);
744         gtk_widget_show (priv->errors_vbox);
745
746         g_hash_table_insert (priv->errors, g_object_ref (account), info_bar);
747 }
748
749 static void
750 main_window_update_status (EmpathyMainWindow *window)
751 {
752         EmpathyMainWindowPriv *priv = GET_PRIV (window);
753         gboolean connected, connecting;
754         GList *l, *children;
755
756         connected = empathy_account_manager_get_accounts_connected (&connecting);
757
758         /* Update the spinner state */
759         if (connecting) {
760                 gtk_spinner_start (GTK_SPINNER (priv->throbber));
761                 gtk_widget_show (priv->throbber_tool_item);
762         } else {
763                 gtk_spinner_stop (GTK_SPINNER (priv->throbber));
764                 gtk_widget_hide (priv->throbber_tool_item);
765         }
766
767         /* Update widgets sensibility */
768         for (l = priv->actions_connected; l; l = l->next) {
769                 gtk_action_set_sensitive (l->data, connected);
770         }
771
772         /* Update favourite rooms sensitivity */
773         children = gtk_container_get_children (GTK_CONTAINER (priv->room_menu));
774         for (l = children; l != NULL; l = l->next) {
775                 if (g_object_get_data (G_OBJECT (l->data), "is_favorite") != NULL) {
776                         gtk_widget_set_sensitive (GTK_WIDGET (l->data), connected);
777                 }
778         }
779         g_list_free (children);
780 }
781
782 static void
783 main_window_connection_changed_cb (TpAccount  *account,
784                                    guint       old_status,
785                                    guint       current,
786                                    guint       reason,
787                                    gchar      *dbus_error_name,
788                                    GHashTable *details,
789                                    EmpathyMainWindow *window)
790 {
791         EmpathyMainWindowPriv *priv = GET_PRIV (window);
792
793         main_window_update_status (window);
794
795         if (current == TP_CONNECTION_STATUS_DISCONNECTED &&
796             reason != TP_CONNECTION_STATUS_REASON_REQUESTED) {
797                 main_window_error_display (window, account);
798         }
799
800         if (current == TP_CONNECTION_STATUS_DISCONNECTED) {
801                 empathy_sound_manager_play (priv->sound_mgr, GTK_WIDGET (window),
802                                     EMPATHY_SOUND_ACCOUNT_DISCONNECTED);
803         }
804
805         if (current == TP_CONNECTION_STATUS_CONNECTED) {
806                 empathy_sound_manager_play (priv->sound_mgr, GTK_WIDGET (window),
807                                     EMPATHY_SOUND_ACCOUNT_CONNECTED);
808
809                 /* Account connected without error, remove error message if any */
810                 main_window_remove_error (window, account);
811         }
812 }
813
814 static void
815 main_window_accels_load (void)
816 {
817         gchar *filename;
818
819         filename = g_build_filename (g_get_user_config_dir (), PACKAGE_NAME, ACCELS_FILENAME, NULL);
820         if (g_file_test (filename, G_FILE_TEST_EXISTS)) {
821                 DEBUG ("Loading from:'%s'", filename);
822                 gtk_accel_map_load (filename);
823         }
824
825         g_free (filename);
826 }
827
828 static void
829 main_window_accels_save (void)
830 {
831         gchar *dir;
832         gchar *file_with_path;
833
834         dir = g_build_filename (g_get_user_config_dir (), PACKAGE_NAME, NULL);
835         g_mkdir_with_parents (dir, S_IRUSR | S_IWUSR | S_IXUSR);
836         file_with_path = g_build_filename (dir, ACCELS_FILENAME, NULL);
837         g_free (dir);
838
839         DEBUG ("Saving to:'%s'", file_with_path);
840         gtk_accel_map_save (file_with_path);
841
842         g_free (file_with_path);
843 }
844
845 static void
846 empathy_main_window_finalize (GObject *window)
847 {
848         EmpathyMainWindowPriv *priv = GET_PRIV (window);
849         GHashTableIter iter;
850         gpointer key, value;
851
852         /* Save user-defined accelerators. */
853         main_window_accels_save ();
854
855         g_list_free (priv->actions_connected);
856
857         g_object_unref (priv->account_manager);
858         g_object_unref (priv->individual_store);
859         g_object_unref (priv->contact_manager);
860         g_object_unref (priv->sound_mgr);
861         g_hash_table_destroy (priv->errors);
862         g_hash_table_destroy (priv->auths);
863
864         /* disconnect all handlers of status-changed signal */
865         g_hash_table_iter_init (&iter, priv->status_changed_handlers);
866         while (g_hash_table_iter_next (&iter, &key, &value))
867                 g_signal_handler_disconnect (TP_ACCOUNT (key),
868                                              GPOINTER_TO_UINT (value));
869
870         g_hash_table_destroy (priv->status_changed_handlers);
871
872         g_signal_handlers_disconnect_by_func (priv->event_manager,
873                                               main_window_event_added_cb,
874                                               window);
875         g_signal_handlers_disconnect_by_func (priv->event_manager,
876                                               main_window_event_removed_cb,
877                                               window);
878         g_object_unref (priv->call_observer);
879         g_object_unref (priv->event_manager);
880         g_object_unref (priv->ui_manager);
881         g_object_unref (priv->chatroom_manager);
882
883         g_object_unref (priv->gsettings_ui);
884         g_object_unref (priv->gsettings_contacts);
885
886         G_OBJECT_CLASS (empathy_main_window_parent_class)->finalize (window);
887 }
888
889 static gboolean
890 main_window_key_press_event_cb  (GtkWidget   *window,
891                                  GdkEventKey *event,
892                                  gpointer     user_data)
893 {
894         EmpathyChatManager *chat_manager;
895
896         if (event->keyval == GDK_KEY_T
897             && event->state & GDK_SHIFT_MASK
898             && event->state & GDK_CONTROL_MASK) {
899                 chat_manager = empathy_chat_manager_dup_singleton ();
900                 empathy_chat_manager_undo_closed_chat (chat_manager);
901                 g_object_unref (chat_manager);
902         }
903         return FALSE;
904 }
905
906 static void
907 main_window_chat_quit_cb (GtkAction         *action,
908                           EmpathyMainWindow *window)
909 {
910         gtk_main_quit ();
911 }
912
913 static void
914 main_window_view_history_cb (GtkAction         *action,
915                              EmpathyMainWindow *window)
916 {
917         empathy_log_window_show (NULL, NULL, FALSE, GTK_WINDOW (window));
918 }
919
920 static void
921 main_window_chat_new_message_cb (GtkAction         *action,
922                                  EmpathyMainWindow *window)
923 {
924         empathy_new_message_dialog_show (GTK_WINDOW (window));
925 }
926
927 static void
928 main_window_chat_new_call_cb (GtkAction         *action,
929                               EmpathyMainWindow *window)
930 {
931         empathy_new_call_dialog_show (GTK_WINDOW (window));
932 }
933
934 static void
935 main_window_chat_add_contact_cb (GtkAction         *action,
936                                  EmpathyMainWindow *window)
937 {
938         empathy_new_individual_dialog_show (GTK_WINDOW (window));
939 }
940
941 static void
942 main_window_chat_search_contacts_cb (GtkAction         *action,
943                                      EmpathyMainWindow *window)
944 {
945         GtkWidget *dialog = empathy_contact_search_dialog_new (
946                         GTK_WINDOW (window));
947         gtk_widget_show (dialog);
948 }
949
950 static void
951 main_window_view_show_ft_manager (GtkAction         *action,
952                                   EmpathyMainWindow *window)
953 {
954         empathy_ft_manager_show ();
955 }
956
957 static void
958 main_window_view_show_offline_cb (GtkToggleAction   *action,
959                                   EmpathyMainWindow *window)
960 {
961         EmpathyMainWindowPriv *priv = GET_PRIV (window);
962         gboolean current;
963
964         current = gtk_toggle_action_get_active (action);
965         g_settings_set_boolean (priv->gsettings_ui,
966                                 EMPATHY_PREFS_UI_SHOW_OFFLINE,
967                                 current);
968
969         empathy_individual_view_set_show_offline (priv->individual_view,
970                         current);
971 }
972
973 static void
974 main_window_notify_sort_contact_cb (GSettings         *gsettings,
975                                     const gchar       *key,
976                                     EmpathyMainWindow *window)
977 {
978         EmpathyMainWindowPriv *priv = GET_PRIV (window);
979         gchar *str;
980
981         str = g_settings_get_string (gsettings, key);
982
983         if (str != NULL) {
984                 GType       type;
985                 GEnumClass *enum_class;
986                 GEnumValue *enum_value;
987
988                 type = empathy_individual_store_sort_get_type ();
989                 enum_class = G_ENUM_CLASS (g_type_class_peek (type));
990                 enum_value = g_enum_get_value_by_nick (enum_class, str);
991                 if (enum_value) {
992                         /* By changing the value of the GtkRadioAction,
993                            it emits a signal that calls main_window_view_sort_contacts_cb
994                            which updates the contacts list */
995                         gtk_radio_action_set_current_value (priv->sort_by_name,
996                                                             enum_value->value);
997                 } else {
998                         g_warning ("Wrong value for sort_criterium configuration : %s", str);
999                 }
1000                 g_free (str);
1001         }
1002 }
1003
1004 static void
1005 main_window_view_sort_contacts_cb (GtkRadioAction    *action,
1006                                    GtkRadioAction    *current,
1007                                    EmpathyMainWindow *window)
1008 {
1009         EmpathyMainWindowPriv *priv = GET_PRIV (window);
1010         EmpathyContactListStoreSort value;
1011         GSList      *group;
1012         GType        type;
1013         GEnumClass  *enum_class;
1014         GEnumValue  *enum_value;
1015
1016         value = gtk_radio_action_get_current_value (action);
1017         group = gtk_radio_action_get_group (action);
1018
1019         /* Get string from index */
1020         type = empathy_individual_store_sort_get_type ();
1021         enum_class = G_ENUM_CLASS (g_type_class_peek (type));
1022         enum_value = g_enum_get_value (enum_class, g_slist_index (group, current));
1023
1024         if (!enum_value) {
1025                 g_warning ("No GEnumValue for EmpathyContactListSort with GtkRadioAction index:%d",
1026                            g_slist_index (group, action));
1027         } else {
1028                 g_settings_set_string (priv->gsettings_contacts,
1029                                        EMPATHY_PREFS_CONTACTS_SORT_CRITERIUM,
1030                                        enum_value->value_nick);
1031         }
1032         empathy_individual_store_set_sort_criterium (priv->individual_store,
1033                         value);
1034 }
1035
1036 static void
1037 main_window_view_show_protocols_cb (GtkToggleAction   *action,
1038                                     EmpathyMainWindow *window)
1039 {
1040         EmpathyMainWindowPriv *priv = GET_PRIV (window);
1041         gboolean value;
1042
1043         value = gtk_toggle_action_get_active (action);
1044
1045         g_settings_set_boolean (priv->gsettings_ui,
1046                                 EMPATHY_PREFS_UI_SHOW_PROTOCOLS,
1047                                 value);
1048         empathy_individual_store_set_show_protocols (priv->individual_store,
1049                                                      value);
1050 }
1051
1052 /* Matches GtkRadioAction values set in empathy-main-window.ui */
1053 #define CONTACT_LIST_NORMAL_SIZE_WITH_AVATARS           0
1054 #define CONTACT_LIST_NORMAL_SIZE                        1
1055 #define CONTACT_LIST_COMPACT_SIZE                       2
1056
1057 static void
1058 main_window_view_contacts_list_size_cb (GtkRadioAction    *action,
1059                                         GtkRadioAction    *current,
1060                                         EmpathyMainWindow *window)
1061 {
1062         EmpathyMainWindowPriv *priv = GET_PRIV (window);
1063         GSettings *gsettings_ui;
1064         gint value;
1065
1066         value = gtk_radio_action_get_current_value (action);
1067         /* create a new GSettings, so we can delay the setting until both
1068          * values are set */
1069         gsettings_ui = g_settings_new (EMPATHY_PREFS_UI_SCHEMA);
1070
1071         DEBUG ("radio button toggled, value = %i", value);
1072
1073         g_settings_delay (gsettings_ui);
1074         g_settings_set_boolean (gsettings_ui,
1075                                 EMPATHY_PREFS_UI_SHOW_AVATARS,
1076                                 value == CONTACT_LIST_NORMAL_SIZE_WITH_AVATARS);
1077
1078         g_settings_set_boolean (gsettings_ui,
1079                                 EMPATHY_PREFS_UI_COMPACT_CONTACT_LIST,
1080                                 value == CONTACT_LIST_COMPACT_SIZE);
1081         g_settings_apply (gsettings_ui);
1082
1083         /* FIXME: these enums probably have the wrong namespace */
1084         empathy_individual_store_set_show_avatars (priv->individual_store,
1085                         value == CONTACT_LIST_NORMAL_SIZE_WITH_AVATARS);
1086         empathy_individual_store_set_is_compact (priv->individual_store,
1087                         value == CONTACT_LIST_COMPACT_SIZE);
1088
1089         g_object_unref (gsettings_ui);
1090 }
1091
1092 static void main_window_notify_show_protocols_cb (GSettings         *gsettings,
1093                                                   const gchar       *key,
1094                                                   EmpathyMainWindow *window)
1095 {
1096         EmpathyMainWindowPriv *priv = GET_PRIV (window);
1097
1098         gtk_toggle_action_set_active (priv->show_protocols,
1099                         g_settings_get_boolean (gsettings,
1100                                 EMPATHY_PREFS_UI_SHOW_PROTOCOLS));
1101 }
1102
1103
1104 static void
1105 main_window_notify_contact_list_size_cb (GSettings         *gsettings,
1106                                          const gchar       *key,
1107                                          EmpathyMainWindow *window)
1108 {
1109         EmpathyMainWindowPriv *priv = GET_PRIV (window);
1110         gint value;
1111
1112         if (g_settings_get_boolean (gsettings,
1113                         EMPATHY_PREFS_UI_COMPACT_CONTACT_LIST)) {
1114                 value = CONTACT_LIST_COMPACT_SIZE;
1115         } else if (g_settings_get_boolean (gsettings,
1116                         EMPATHY_PREFS_UI_SHOW_AVATARS)) {
1117                 value = CONTACT_LIST_NORMAL_SIZE_WITH_AVATARS;
1118         } else {
1119                 value = CONTACT_LIST_NORMAL_SIZE;
1120         }
1121
1122         DEBUG ("setting changed, value = %i", value);
1123
1124         /* By changing the value of the GtkRadioAction,
1125            it emits a signal that calls main_window_view_contacts_list_size_cb
1126            which updates the contacts list */
1127         gtk_radio_action_set_current_value (priv->normal_with_avatars, value);
1128 }
1129
1130 static void
1131 main_window_edit_search_contacts_cb (GtkCheckMenuItem  *item,
1132                                      EmpathyMainWindow *window)
1133 {
1134         EmpathyMainWindowPriv *priv = GET_PRIV (window);
1135
1136         empathy_individual_view_start_search (priv->individual_view);
1137 }
1138
1139 static void
1140 main_window_view_show_map_cb (GtkCheckMenuItem  *item,
1141                               EmpathyMainWindow *window)
1142 {
1143 #ifdef HAVE_LIBCHAMPLAIN
1144         empathy_map_view_show ();
1145 #endif
1146 }
1147
1148 static void
1149 join_chatroom (EmpathyChatroom *chatroom,
1150                gint64 timestamp)
1151 {
1152         TpAccount      *account;
1153         const gchar    *room;
1154
1155         account = empathy_chatroom_get_account (chatroom);
1156         room = empathy_chatroom_get_room (chatroom);
1157
1158         DEBUG ("Requesting channel for '%s'", room);
1159         empathy_dispatcher_join_muc (account, room, timestamp);
1160 }
1161
1162 typedef struct
1163 {
1164         TpAccount *account;
1165         EmpathyChatroom *chatroom;
1166         gint64 timestamp;
1167         glong sig_id;
1168         guint timeout;
1169 } join_fav_account_sig_ctx;
1170
1171 static join_fav_account_sig_ctx *
1172 join_fav_account_sig_ctx_new (TpAccount *account,
1173                              EmpathyChatroom *chatroom,
1174                               gint64 timestamp)
1175 {
1176         join_fav_account_sig_ctx *ctx = g_slice_new0 (
1177                 join_fav_account_sig_ctx);
1178
1179         ctx->account = g_object_ref (account);
1180         ctx->chatroom = g_object_ref (chatroom);
1181         ctx->timestamp = timestamp;
1182         return ctx;
1183 }
1184
1185 static void
1186 join_fav_account_sig_ctx_free (join_fav_account_sig_ctx *ctx)
1187 {
1188         g_object_unref (ctx->account);
1189         g_object_unref (ctx->chatroom);
1190         g_slice_free (join_fav_account_sig_ctx, ctx);
1191 }
1192
1193 static void
1194 account_status_changed_cb (TpAccount  *account,
1195                            TpConnectionStatus old_status,
1196                            TpConnectionStatus new_status,
1197                            guint reason,
1198                            gchar *dbus_error_name,
1199                            GHashTable *details,
1200                            gpointer user_data)
1201 {
1202         join_fav_account_sig_ctx *ctx = user_data;
1203
1204         switch (new_status) {
1205                 case TP_CONNECTION_STATUS_DISCONNECTED:
1206                         /* Don't wait any longer */
1207                         goto finally;
1208                         break;
1209
1210                 case TP_CONNECTION_STATUS_CONNECTING:
1211                         /* Wait a bit */
1212                         return;
1213
1214                 case TP_CONNECTION_STATUS_CONNECTED:
1215                         /* We can join the room */
1216                         break;
1217
1218                 default:
1219                         g_assert_not_reached ();
1220         }
1221
1222         join_chatroom (ctx->chatroom, ctx->timestamp);
1223
1224 finally:
1225         g_source_remove (ctx->timeout);
1226         g_signal_handler_disconnect (account, ctx->sig_id);
1227 }
1228
1229 #define JOIN_FAVORITE_TIMEOUT 5
1230
1231 static gboolean
1232 join_favorite_timeout_cb (gpointer data)
1233 {
1234         join_fav_account_sig_ctx *ctx = data;
1235
1236         /* stop waiting for joining the favorite room */
1237         g_signal_handler_disconnect (ctx->account, ctx->sig_id);
1238         return FALSE;
1239 }
1240
1241 static void
1242 main_window_favorite_chatroom_join (EmpathyChatroom *chatroom)
1243 {
1244         TpAccount      *account;
1245
1246         account = empathy_chatroom_get_account (chatroom);
1247         if (tp_account_get_connection_status (account, NULL) !=
1248                                              TP_CONNECTION_STATUS_CONNECTED) {
1249                 join_fav_account_sig_ctx *ctx;
1250
1251                 ctx = join_fav_account_sig_ctx_new (account, chatroom,
1252                         gtk_get_current_event_time ());
1253
1254                 ctx->sig_id = g_signal_connect_data (account, "status-changed",
1255                         G_CALLBACK (account_status_changed_cb), ctx,
1256                         (GClosureNotify) join_fav_account_sig_ctx_free, 0);
1257
1258                 ctx->timeout = g_timeout_add_seconds (JOIN_FAVORITE_TIMEOUT,
1259                         join_favorite_timeout_cb, ctx);
1260                 return;
1261         }
1262
1263         join_chatroom (chatroom, gtk_get_current_event_time ());
1264 }
1265
1266 static void
1267 main_window_favorite_chatroom_menu_activate_cb (GtkMenuItem     *menu_item,
1268                                                 EmpathyChatroom *chatroom)
1269 {
1270         main_window_favorite_chatroom_join (chatroom);
1271 }
1272
1273 static void
1274 main_window_favorite_chatroom_menu_add (EmpathyMainWindow *window,
1275                                         EmpathyChatroom   *chatroom)
1276 {
1277         EmpathyMainWindowPriv *priv = GET_PRIV (window);
1278         GtkWidget   *menu_item;
1279         const gchar *name;
1280
1281         if (g_object_get_data (G_OBJECT (chatroom), "menu_item")) {
1282                 return;
1283         }
1284
1285         name = empathy_chatroom_get_name (chatroom);
1286         menu_item = gtk_menu_item_new_with_label (name);
1287         g_object_set_data (G_OBJECT (menu_item), "is_favorite",
1288                         GUINT_TO_POINTER (TRUE));
1289
1290         g_object_set_data (G_OBJECT (chatroom), "menu_item", menu_item);
1291         g_signal_connect (menu_item, "activate",
1292                           G_CALLBACK (main_window_favorite_chatroom_menu_activate_cb),
1293                           chatroom);
1294
1295         gtk_menu_shell_insert (GTK_MENU_SHELL (priv->room_menu),
1296                                menu_item, 4);
1297
1298         gtk_widget_show (menu_item);
1299 }
1300
1301 static void
1302 main_window_favorite_chatroom_menu_added_cb (EmpathyChatroomManager *manager,
1303                                              EmpathyChatroom        *chatroom,
1304                                              EmpathyMainWindow      *window)
1305 {
1306         EmpathyMainWindowPriv *priv = GET_PRIV (window);
1307
1308         main_window_favorite_chatroom_menu_add (window, chatroom);
1309         gtk_widget_show (priv->room_separator);
1310         gtk_action_set_sensitive (priv->room_join_favorites, TRUE);
1311 }
1312
1313 static void
1314 main_window_favorite_chatroom_menu_removed_cb (EmpathyChatroomManager *manager,
1315                                                EmpathyChatroom        *chatroom,
1316                                                EmpathyMainWindow      *window)
1317 {
1318         EmpathyMainWindowPriv *priv = GET_PRIV (window);
1319         GtkWidget *menu_item;
1320         GList *chatrooms;
1321
1322         menu_item = g_object_get_data (G_OBJECT (chatroom), "menu_item");
1323         g_object_set_data (G_OBJECT (chatroom), "menu_item", NULL);
1324         gtk_widget_destroy (menu_item);
1325
1326         chatrooms = empathy_chatroom_manager_get_chatrooms (priv->chatroom_manager, NULL);
1327         if (chatrooms) {
1328                 gtk_widget_show (priv->room_separator);
1329         } else {
1330                 gtk_widget_hide (priv->room_separator);
1331         }
1332
1333         gtk_action_set_sensitive (priv->room_join_favorites, chatrooms != NULL);
1334         g_list_free (chatrooms);
1335 }
1336
1337 static void
1338 main_window_favorite_chatroom_menu_setup (EmpathyMainWindow *window)
1339 {
1340         EmpathyMainWindowPriv *priv = GET_PRIV (window);
1341         GList *chatrooms, *l;
1342         GtkWidget *room;
1343
1344         priv->chatroom_manager = empathy_chatroom_manager_dup_singleton (NULL);
1345         chatrooms = empathy_chatroom_manager_get_chatrooms (
1346                 priv->chatroom_manager, NULL);
1347         room = gtk_ui_manager_get_widget (priv->ui_manager,
1348                 "/menubar/room");
1349         priv->room_menu = gtk_menu_item_get_submenu (GTK_MENU_ITEM (room));
1350         priv->room_separator = gtk_ui_manager_get_widget (priv->ui_manager,
1351                 "/menubar/room/room_separator");
1352
1353         for (l = chatrooms; l; l = l->next) {
1354                 main_window_favorite_chatroom_menu_add (window, l->data);
1355         }
1356
1357         if (!chatrooms) {
1358                 gtk_widget_hide (priv->room_separator);
1359         }
1360
1361         gtk_action_set_sensitive (priv->room_join_favorites, chatrooms != NULL);
1362
1363         g_signal_connect (priv->chatroom_manager, "chatroom-added",
1364                           G_CALLBACK (main_window_favorite_chatroom_menu_added_cb),
1365                           window);
1366         g_signal_connect (priv->chatroom_manager, "chatroom-removed",
1367                           G_CALLBACK (main_window_favorite_chatroom_menu_removed_cb),
1368                           window);
1369
1370         g_list_free (chatrooms);
1371 }
1372
1373 static void
1374 main_window_room_join_new_cb (GtkAction         *action,
1375                               EmpathyMainWindow *window)
1376 {
1377         empathy_new_chatroom_dialog_show (GTK_WINDOW (window));
1378 }
1379
1380 static void
1381 main_window_room_join_favorites_cb (GtkAction         *action,
1382                                     EmpathyMainWindow *window)
1383 {
1384         EmpathyMainWindowPriv *priv = GET_PRIV (window);
1385         GList *chatrooms, *l;
1386
1387         chatrooms = empathy_chatroom_manager_get_chatrooms (priv->chatroom_manager, NULL);
1388         for (l = chatrooms; l; l = l->next) {
1389                 main_window_favorite_chatroom_join (l->data);
1390         }
1391         g_list_free (chatrooms);
1392 }
1393
1394 static void
1395 main_window_room_manage_favorites_cb (GtkAction         *action,
1396                                       EmpathyMainWindow *window)
1397 {
1398         empathy_chatrooms_window_show (GTK_WINDOW (window));
1399 }
1400
1401 static void
1402 main_window_edit_cb (GtkAction         *action,
1403                      EmpathyMainWindow *window)
1404 {
1405         EmpathyMainWindowPriv *priv = GET_PRIV (window);
1406         GtkWidget *submenu;
1407
1408         /* FIXME: It should use the UIManager to merge the contact/group submenu */
1409         submenu = empathy_individual_view_get_individual_menu (
1410                         priv->individual_view);
1411         if (submenu) {
1412                 GtkMenuItem *item;
1413                 GtkWidget   *label;
1414
1415                 item = GTK_MENU_ITEM (priv->edit_context);
1416                 label = gtk_bin_get_child (GTK_BIN (item));
1417                 gtk_label_set_text (GTK_LABEL (label), _("Contact"));
1418
1419                 gtk_widget_show (priv->edit_context);
1420                 gtk_widget_show (priv->edit_context_separator);
1421
1422                 gtk_menu_item_set_submenu (item, submenu);
1423
1424                 return;
1425         }
1426
1427         submenu = empathy_individual_view_get_group_menu (
1428                         priv->individual_view);
1429         if (submenu) {
1430                 GtkMenuItem *item;
1431                 GtkWidget   *label;
1432
1433                 item = GTK_MENU_ITEM (priv->edit_context);
1434                 label = gtk_bin_get_child (GTK_BIN (item));
1435                 gtk_label_set_text (GTK_LABEL (label), _("Group"));
1436
1437                 gtk_widget_show (priv->edit_context);
1438                 gtk_widget_show (priv->edit_context_separator);
1439
1440                 gtk_menu_item_set_submenu (item, submenu);
1441
1442                 return;
1443         }
1444
1445         gtk_widget_hide (priv->edit_context);
1446         gtk_widget_hide (priv->edit_context_separator);
1447
1448         return;
1449 }
1450
1451 static void
1452 main_window_edit_accounts_cb (GtkAction         *action,
1453                               EmpathyMainWindow *window)
1454 {
1455         empathy_accounts_dialog_show_application (gdk_screen_get_default (),
1456                         NULL, FALSE, FALSE);
1457 }
1458
1459 static void
1460 main_window_edit_personal_information_cb (GtkAction         *action,
1461                                           EmpathyMainWindow *window)
1462 {
1463         empathy_contact_personal_dialog_show (GTK_WINDOW (window));
1464 }
1465
1466 static void
1467 main_window_edit_preferences_cb (GtkAction         *action,
1468                                  EmpathyMainWindow *window)
1469 {
1470         EmpathyMainWindowPriv *priv = GET_PRIV (window);
1471
1472         if (priv->preferences == NULL) {
1473                 priv->preferences = empathy_preferences_new (GTK_WINDOW (window));
1474                 g_object_add_weak_pointer (G_OBJECT (priv->preferences),
1475                                            (gpointer) &priv->preferences);
1476
1477                 gtk_widget_show (priv->preferences);
1478         } else {
1479                 gtk_window_present (GTK_WINDOW (priv->preferences));
1480         }
1481 }
1482
1483 static void
1484 main_window_help_about_cb (GtkAction         *action,
1485                            EmpathyMainWindow *window)
1486 {
1487         empathy_about_dialog_new (GTK_WINDOW (window));
1488 }
1489
1490 static void
1491 main_window_help_debug_cb (GtkAction         *action,
1492                            EmpathyMainWindow *window)
1493 {
1494         GdkDisplay *display;
1495         GError *error = NULL;
1496         gchar *path;
1497         GAppInfo *app_info;
1498         GdkAppLaunchContext *context = NULL;
1499
1500         /* Try to run from source directory if possible */
1501         path = g_build_filename (g_getenv ("EMPATHY_SRCDIR"), "src",
1502                         "empathy-debugger", NULL);
1503
1504         if (!g_file_test (path, G_FILE_TEST_EXISTS)) {
1505                 g_free (path);
1506                 path = g_build_filename (BIN_DIR, "empathy-debugger", NULL);
1507         }
1508
1509         app_info = g_app_info_create_from_commandline (path, NULL, 0, &error);
1510         if (app_info == NULL) {
1511                 DEBUG ("Failed to create app info: %s", error->message);
1512                 g_error_free (error);
1513                 goto out;
1514         }
1515
1516         display = gdk_display_get_default ();
1517         context = gdk_display_get_app_launch_context (display);
1518
1519         if (!g_app_info_launch (app_info, NULL, (GAppLaunchContext *) context,
1520                 &error)) {
1521                 g_warning ("Failed to open debug window: %s", error->message);
1522                 g_error_free (error);
1523                 goto out;
1524         }
1525
1526 out:
1527         tp_clear_object (&app_info);
1528         tp_clear_object (&context);
1529         g_free (path);
1530 }
1531
1532 static void
1533 main_window_help_contents_cb (GtkAction         *action,
1534                               EmpathyMainWindow *window)
1535 {
1536         empathy_url_show (GTK_WIDGET (window), "ghelp:empathy");
1537 }
1538
1539 static gboolean
1540 main_window_throbber_button_press_event_cb (GtkWidget         *throbber,
1541                                             GdkEventButton    *event,
1542                                             EmpathyMainWindow *window)
1543 {
1544         if (event->type != GDK_BUTTON_PRESS ||
1545             event->button != 1) {
1546                 return FALSE;
1547         }
1548
1549         empathy_accounts_dialog_show_application (
1550                         gtk_widget_get_screen (GTK_WIDGET (throbber)),
1551                         NULL, FALSE, FALSE);
1552
1553         return FALSE;
1554 }
1555
1556 static void
1557 main_window_account_removed_cb (TpAccountManager  *manager,
1558                                 TpAccount         *account,
1559                                 EmpathyMainWindow *window)
1560 {
1561         EmpathyMainWindowPriv *priv = GET_PRIV (window);
1562         GList *a;
1563
1564         a = tp_account_manager_get_valid_accounts (manager);
1565
1566         gtk_action_set_sensitive (priv->view_history,
1567                 g_list_length (a) > 0);
1568
1569         g_list_free (a);
1570
1571         /* remove errors if any */
1572         main_window_remove_error (window, account);
1573 }
1574
1575 static void
1576 main_window_account_validity_changed_cb (TpAccountManager  *manager,
1577                                          TpAccount         *account,
1578                                          gboolean           valid,
1579                                          EmpathyMainWindow *window)
1580 {
1581         EmpathyMainWindowPriv *priv = GET_PRIV (window);
1582
1583         if (valid) {
1584                 gulong handler_id;
1585                 handler_id = GPOINTER_TO_UINT (g_hash_table_lookup (
1586                         priv->status_changed_handlers, account));
1587
1588                 /* connect signal only if it was not connected yet */
1589                 if (handler_id == 0) {
1590                         handler_id = g_signal_connect (account,
1591                                 "status-changed",
1592                                 G_CALLBACK (main_window_connection_changed_cb),
1593                                 window);
1594                         g_hash_table_insert (priv->status_changed_handlers,
1595                                 account, GUINT_TO_POINTER (handler_id));
1596                 }
1597         }
1598
1599         main_window_account_removed_cb (manager, account, window);
1600 }
1601
1602 static void
1603 main_window_notify_show_offline_cb (GSettings   *gsettings,
1604                                     const gchar *key,
1605                                     gpointer     toggle_action)
1606 {
1607         gtk_toggle_action_set_active (toggle_action,
1608                         g_settings_get_boolean (gsettings, key));
1609 }
1610
1611 static void
1612 main_window_connection_items_setup (EmpathyMainWindow *window,
1613                                     GtkBuilder        *gui)
1614 {
1615         EmpathyMainWindowPriv *priv = GET_PRIV (window);
1616         GList         *list;
1617         GObject       *action;
1618         guint          i;
1619         const gchar *actions_connected[] = {
1620                 "room_join_new",
1621                 "room_join_favorites",
1622                 "chat_new_message",
1623                 "chat_new_call",
1624                 "chat_add_contact",
1625                 "edit_personal_information"
1626         };
1627
1628         for (i = 0, list = NULL; i < G_N_ELEMENTS (actions_connected); i++) {
1629                 action = gtk_builder_get_object (gui, actions_connected[i]);
1630                 list = g_list_prepend (list, action);
1631         }
1632
1633         priv->actions_connected = list;
1634 }
1635
1636 static void
1637 account_manager_prepared_cb (GObject      *source_object,
1638                              GAsyncResult *result,
1639                              gpointer      user_data)
1640 {
1641         GList *accounts, *j;
1642         TpAccountManager *manager = TP_ACCOUNT_MANAGER (source_object);
1643         EmpathyMainWindow *window = user_data;
1644         EmpathyMainWindowPriv *priv = GET_PRIV (window);
1645         GError *error = NULL;
1646
1647         if (!tp_account_manager_prepare_finish (manager, result, &error)) {
1648                 DEBUG ("Failed to prepare account manager: %s", error->message);
1649                 g_error_free (error);
1650                 return;
1651         }
1652
1653         accounts = tp_account_manager_get_valid_accounts (priv->account_manager);
1654         for (j = accounts; j != NULL; j = j->next) {
1655                 TpAccount *account = TP_ACCOUNT (j->data);
1656                 gulong handler_id;
1657
1658                 handler_id = g_signal_connect (account, "status-changed",
1659                                   G_CALLBACK (main_window_connection_changed_cb),
1660                                   window);
1661                 g_hash_table_insert (priv->status_changed_handlers,
1662                                      account, GUINT_TO_POINTER (handler_id));
1663         }
1664
1665         g_signal_connect (manager, "account-validity-changed",
1666                           G_CALLBACK (main_window_account_validity_changed_cb),
1667                           window);
1668
1669         main_window_update_status (window);
1670
1671         /* Disable the "Previous Conversations" menu entry if there is no account */
1672         gtk_action_set_sensitive (priv->view_history,
1673                 g_list_length (accounts) > 0);
1674
1675         g_list_free (accounts);
1676 }
1677
1678 static void
1679 main_window_members_changed_cb (EmpathyContactList *list,
1680                                 EmpathyContact     *contact,
1681                                 EmpathyContact     *actor,
1682                                 guint               reason,
1683                                 gchar              *message,
1684                                 gboolean            is_member,
1685                                 EmpathyMainWindow  *window)
1686 {
1687         EmpathyMainWindowPriv *priv = GET_PRIV (window);
1688
1689         if (!is_member)
1690                 return;
1691
1692         if (!empathy_migrate_butterfly_logs (contact)) {
1693                 g_signal_handler_disconnect (list,
1694                         priv->butterfly_log_migration_members_changed_id);
1695                 priv->butterfly_log_migration_members_changed_id = 0;
1696         }
1697 }
1698
1699 static GObject *
1700 empathy_main_window_constructor (GType type,
1701                                  guint n_construct_params,
1702                                  GObjectConstructParam *construct_params)
1703 {
1704         static GObject *window = NULL;
1705
1706         if (window != NULL)
1707                 return g_object_ref (window);
1708
1709         window = G_OBJECT_CLASS (empathy_main_window_parent_class)->constructor (
1710                 type, n_construct_params, construct_params);
1711
1712         g_object_add_weak_pointer (window, (gpointer) &window);
1713
1714         return window;
1715 }
1716
1717 static void
1718 empathy_main_window_class_init (EmpathyMainWindowClass *klass)
1719 {
1720         GObjectClass *object_class = G_OBJECT_CLASS (klass);
1721
1722         object_class->finalize = empathy_main_window_finalize;
1723         object_class->constructor = empathy_main_window_constructor;
1724
1725         g_type_class_add_private (object_class, sizeof (EmpathyMainWindowPriv));
1726 }
1727
1728 static void
1729 empathy_main_window_init (EmpathyMainWindow *window)
1730 {
1731         EmpathyMainWindowPriv    *priv;
1732         EmpathyIndividualManager *individual_manager;
1733         GtkBuilder               *gui;
1734         GtkWidget                *sw;
1735         GtkToggleAction          *show_offline_widget;
1736         GtkAction                *show_map_widget;
1737         GtkToolItem              *item;
1738         gboolean                  show_offline;
1739         gchar                    *filename;
1740         GSList                   *l;
1741         GtkTreeModel             *model;
1742
1743         priv = window->priv = G_TYPE_INSTANCE_GET_PRIVATE (window,
1744                         EMPATHY_TYPE_MAIN_WINDOW, EmpathyMainWindowPriv);
1745
1746         priv->gsettings_ui = g_settings_new (EMPATHY_PREFS_UI_SCHEMA);
1747         priv->gsettings_contacts = g_settings_new (EMPATHY_PREFS_CONTACTS_SCHEMA);
1748
1749         priv->sound_mgr = empathy_sound_manager_dup_singleton ();
1750
1751         gtk_window_set_title (GTK_WINDOW (window), _("Contact List"));
1752         gtk_window_set_role (GTK_WINDOW (window), "contact_list");
1753         gtk_window_set_default_size (GTK_WINDOW (window), 225, 325);
1754
1755         /* Set up interface */
1756         filename = empathy_file_lookup ("empathy-main-window.ui", "src");
1757         gui = empathy_builder_get_file (filename,
1758                                        "main_vbox", &priv->main_vbox,
1759                                        "errors_vbox", &priv->errors_vbox,
1760                                        "auth_vbox", &priv->auth_vbox,
1761                                        "ui_manager", &priv->ui_manager,
1762                                        "view_show_offline", &show_offline_widget,
1763                                        "view_show_protocols", &priv->show_protocols,
1764                                        "view_sort_by_name", &priv->sort_by_name,
1765                                        "view_sort_by_status", &priv->sort_by_status,
1766                                        "view_normal_size_with_avatars", &priv->normal_with_avatars,
1767                                        "view_normal_size", &priv->normal_size,
1768                                        "view_compact_size", &priv->compact_size,
1769                                        "view_history", &priv->view_history,
1770                                        "view_show_map", &show_map_widget,
1771                                        "room_join_favorites", &priv->room_join_favorites,
1772                                        "presence_toolbar", &priv->presence_toolbar,
1773                                        "notebook", &priv->notebook,
1774                                        "no_entry_label", &priv->no_entry_label,
1775                                        "roster_scrolledwindow", &sw,
1776                                        NULL);
1777         g_free (filename);
1778
1779         gtk_container_add (GTK_CONTAINER (window), priv->main_vbox);
1780         gtk_widget_show (priv->main_vbox);
1781
1782         g_signal_connect (window, "key-press-event",
1783                           G_CALLBACK (main_window_key_press_event_cb), NULL);
1784
1785         empathy_builder_connect (gui, window,
1786                               "chat_quit", "activate", main_window_chat_quit_cb,
1787                               "chat_new_message", "activate", main_window_chat_new_message_cb,
1788                               "chat_new_call", "activate", main_window_chat_new_call_cb,
1789                               "view_history", "activate", main_window_view_history_cb,
1790                               "room_join_new", "activate", main_window_room_join_new_cb,
1791                               "room_join_favorites", "activate", main_window_room_join_favorites_cb,
1792                               "room_manage_favorites", "activate", main_window_room_manage_favorites_cb,
1793                               "chat_add_contact", "activate", main_window_chat_add_contact_cb,
1794                               "chat_search_contacts", "activate", main_window_chat_search_contacts_cb,
1795                               "view_show_ft_manager", "activate", main_window_view_show_ft_manager,
1796                               "view_show_offline", "toggled", main_window_view_show_offline_cb,
1797                               "view_show_protocols", "toggled", main_window_view_show_protocols_cb,
1798                               "view_sort_by_name", "changed", main_window_view_sort_contacts_cb,
1799                               "view_normal_size_with_avatars", "changed", main_window_view_contacts_list_size_cb,
1800                               "view_show_map", "activate", main_window_view_show_map_cb,
1801                               "edit", "activate", main_window_edit_cb,
1802                               "edit_accounts", "activate", main_window_edit_accounts_cb,
1803                               "edit_personal_information", "activate", main_window_edit_personal_information_cb,
1804                               "edit_preferences", "activate", main_window_edit_preferences_cb,
1805                               "edit_search_contacts", "activate", main_window_edit_search_contacts_cb,
1806                               "help_about", "activate", main_window_help_about_cb,
1807                               "help_debug", "activate", main_window_help_debug_cb,
1808                               "help_contents", "activate", main_window_help_contents_cb,
1809                               NULL);
1810
1811         /* Set up connection related widgets. */
1812         main_window_connection_items_setup (window, gui);
1813
1814         g_object_ref (priv->ui_manager);
1815         g_object_unref (gui);
1816
1817 #ifndef HAVE_LIBCHAMPLAIN
1818         gtk_action_set_visible (show_map_widget, FALSE);
1819 #endif
1820
1821         priv->account_manager = tp_account_manager_dup ();
1822
1823         tp_account_manager_prepare_async (priv->account_manager, NULL,
1824                                           account_manager_prepared_cb, window);
1825
1826         priv->errors = g_hash_table_new_full (g_direct_hash,
1827                                               g_direct_equal,
1828                                               g_object_unref,
1829                                               NULL);
1830
1831         priv->auths = g_hash_table_new (NULL, NULL);
1832
1833         priv->status_changed_handlers = g_hash_table_new_full (g_direct_hash,
1834                                                                g_direct_equal,
1835                                                                NULL,
1836                                                                NULL);
1837
1838         /* Set up menu */
1839         main_window_favorite_chatroom_menu_setup (window);
1840
1841         priv->edit_context = gtk_ui_manager_get_widget (priv->ui_manager,
1842                 "/menubar/edit/edit_context");
1843         priv->edit_context_separator = gtk_ui_manager_get_widget (
1844                 priv->ui_manager,
1845                 "/menubar/edit/edit_context_separator");
1846         gtk_widget_hide (priv->edit_context);
1847         gtk_widget_hide (priv->edit_context_separator);
1848
1849         /* Set up contact list. */
1850         empathy_status_presets_get_all ();
1851
1852         /* Set up presence chooser */
1853         priv->presence_chooser = empathy_presence_chooser_new ();
1854         gtk_widget_show (priv->presence_chooser);
1855         item = gtk_tool_item_new ();
1856         gtk_widget_show (GTK_WIDGET (item));
1857         gtk_widget_set_size_request (priv->presence_chooser, 10, -1);
1858         gtk_container_add (GTK_CONTAINER (item), priv->presence_chooser);
1859         gtk_tool_item_set_is_important (item, TRUE);
1860         gtk_tool_item_set_expand (item, TRUE);
1861         gtk_toolbar_insert (GTK_TOOLBAR (priv->presence_toolbar), item, -1);
1862
1863         /* Set up the throbber */
1864         priv->throbber = gtk_spinner_new ();
1865         gtk_widget_set_size_request (priv->throbber, 16, -1);
1866         gtk_widget_set_tooltip_text (priv->throbber, _("Show and edit accounts"));
1867         gtk_widget_set_events (priv->throbber, GDK_BUTTON_PRESS_MASK);
1868         g_signal_connect (priv->throbber, "button-press-event",
1869                 G_CALLBACK (main_window_throbber_button_press_event_cb),
1870                 window);
1871         gtk_widget_show (priv->throbber);
1872
1873         item = gtk_tool_item_new ();
1874         gtk_container_set_border_width (GTK_CONTAINER (item), 6);
1875         gtk_toolbar_insert (GTK_TOOLBAR (priv->presence_toolbar), item, -1);
1876         gtk_container_add (GTK_CONTAINER (item), priv->throbber);
1877         priv->throbber_tool_item = GTK_WIDGET (item);
1878
1879         /* XXX: this class is designed to live for the duration of the program,
1880          * so it's got a race condition between its signal handlers and its
1881          * finalization. The class is planned to be removed, so we won't fix
1882          * this before then. */
1883         priv->contact_manager = EMPATHY_CONTACT_LIST (
1884                         empathy_contact_manager_dup_singleton ());
1885         individual_manager = empathy_individual_manager_dup_singleton ();
1886         priv->individual_store = empathy_individual_store_new (
1887                         individual_manager);
1888         g_object_unref (individual_manager);
1889
1890         /* For the moment, we disallow Persona drops onto the main contact list (e.g. from things such as
1891          * the EmpathyPersonaView in the linking dialogue). No code is hooked up to do anything on a Persona
1892          * drop, so allowing them would achieve nothing except confusion. */
1893         priv->individual_view = empathy_individual_view_new (
1894                         priv->individual_store,
1895                         EMPATHY_INDIVIDUAL_VIEW_FEATURE_ALL ^ EMPATHY_INDIVIDUAL_VIEW_FEATURE_PERSONA_DROP,
1896                         EMPATHY_INDIVIDUAL_FEATURE_ALL);
1897
1898         priv->butterfly_log_migration_members_changed_id = g_signal_connect (
1899                         priv->contact_manager, "members-changed",
1900                         G_CALLBACK (main_window_members_changed_cb), window);
1901
1902         gtk_widget_show (GTK_WIDGET (priv->individual_view));
1903         gtk_container_add (GTK_CONTAINER (sw),
1904                            GTK_WIDGET (priv->individual_view));
1905         g_signal_connect (priv->individual_view, "row-activated",
1906                           G_CALLBACK (main_window_row_activated_cb),
1907                           window);
1908
1909         /* Set up search bar */
1910         priv->search_bar = empathy_live_search_new (
1911                 GTK_WIDGET (priv->individual_view));
1912         empathy_individual_view_set_live_search (priv->individual_view,
1913                 EMPATHY_LIVE_SEARCH (priv->search_bar));
1914         gtk_box_pack_start (GTK_BOX (priv->main_vbox), priv->search_bar,
1915                 FALSE, TRUE, 0);
1916         g_signal_connect_swapped (window, "map",
1917                 G_CALLBACK (gtk_widget_grab_focus), priv->individual_view);
1918
1919         /* Connect to proper signals to check if contact list is empty or not */
1920         model = gtk_tree_view_get_model (GTK_TREE_VIEW (priv->individual_view));
1921         priv->empty = TRUE;
1922         g_signal_connect (model, "row-inserted",
1923                           G_CALLBACK (main_window_row_inserted_cb),
1924                           window);
1925         g_signal_connect (model, "row-deleted",
1926                           G_CALLBACK (main_window_row_deleted_cb),
1927                           window);
1928
1929         /* Load user-defined accelerators. */
1930         main_window_accels_load ();
1931
1932         /* Set window size. */
1933         empathy_geometry_bind (GTK_WINDOW (window), GEOMETRY_NAME);
1934
1935         /* Enable event handling */
1936         priv->call_observer = empathy_call_observer_dup_singleton ();
1937         priv->event_manager = empathy_event_manager_dup_singleton ();
1938
1939         g_signal_connect (priv->event_manager, "event-added",
1940                           G_CALLBACK (main_window_event_added_cb), window);
1941         g_signal_connect (priv->event_manager, "event-removed",
1942                           G_CALLBACK (main_window_event_removed_cb), window);
1943         g_signal_connect (priv->account_manager, "account-validity-changed",
1944                           G_CALLBACK (main_window_account_validity_changed_cb),
1945                           window);
1946         g_signal_connect (priv->account_manager, "account-removed",
1947                           G_CALLBACK (main_window_account_removed_cb),
1948                           window);
1949         g_signal_connect (priv->account_manager, "account-disabled",
1950                           G_CALLBACK (main_window_account_disabled_cb),
1951                           window);
1952
1953         l = empathy_event_manager_get_events (priv->event_manager);
1954         while (l) {
1955                 main_window_event_added_cb (priv->event_manager, l->data,
1956                                 window);
1957                 l = l->next;
1958         }
1959
1960         /* Show offline ? */
1961         show_offline = g_settings_get_boolean (priv->gsettings_ui,
1962                                                EMPATHY_PREFS_UI_SHOW_OFFLINE);
1963         g_signal_connect (priv->gsettings_ui,
1964                           "changed::" EMPATHY_PREFS_UI_SHOW_OFFLINE,
1965                           G_CALLBACK (main_window_notify_show_offline_cb),
1966                           show_offline_widget);
1967
1968         gtk_toggle_action_set_active (show_offline_widget, show_offline);
1969
1970         /* Show protocol ? */
1971         g_signal_connect (priv->gsettings_ui,
1972                           "changed::" EMPATHY_PREFS_UI_SHOW_PROTOCOLS,
1973                           G_CALLBACK (main_window_notify_show_protocols_cb),
1974                           window);
1975
1976         main_window_notify_show_protocols_cb (priv->gsettings_ui,
1977                                               EMPATHY_PREFS_UI_SHOW_PROTOCOLS,
1978                                               window);
1979
1980         /* Sort by name / by status ? */
1981         g_signal_connect (priv->gsettings_contacts,
1982                           "changed::" EMPATHY_PREFS_CONTACTS_SORT_CRITERIUM,
1983                           G_CALLBACK (main_window_notify_sort_contact_cb),
1984                           window);
1985
1986         main_window_notify_sort_contact_cb (priv->gsettings_contacts,
1987                                             EMPATHY_PREFS_CONTACTS_SORT_CRITERIUM,
1988                                             window);
1989
1990         /* Contacts list size */
1991         g_signal_connect (priv->gsettings_ui,
1992                           "changed::" EMPATHY_PREFS_UI_COMPACT_CONTACT_LIST,
1993                           G_CALLBACK (main_window_notify_contact_list_size_cb),
1994                           window);
1995         g_signal_connect (priv->gsettings_ui,
1996                           "changed::" EMPATHY_PREFS_UI_SHOW_AVATARS,
1997                           G_CALLBACK (main_window_notify_contact_list_size_cb),
1998                           window);
1999
2000         main_window_notify_contact_list_size_cb (priv->gsettings_ui,
2001                                                  EMPATHY_PREFS_UI_SHOW_AVATARS,
2002                                                  window);
2003 }
2004
2005 GtkWidget *
2006 empathy_main_window_dup (void)
2007 {
2008         return g_object_new (EMPATHY_TYPE_MAIN_WINDOW, NULL);
2009 }