]> git.0d.be Git - empathy.git/blob - src/empathy-main-window.c
disable the call observer for now
[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         /*
879         g_object_unref (priv->call_observer);
880         */
881         g_object_unref (priv->event_manager);
882         g_object_unref (priv->ui_manager);
883         g_object_unref (priv->chatroom_manager);
884
885         g_object_unref (priv->gsettings_ui);
886         g_object_unref (priv->gsettings_contacts);
887
888         G_OBJECT_CLASS (empathy_main_window_parent_class)->finalize (window);
889 }
890
891 static gboolean
892 main_window_key_press_event_cb  (GtkWidget   *window,
893                                  GdkEventKey *event,
894                                  gpointer     user_data)
895 {
896         EmpathyChatManager *chat_manager;
897
898         if (event->keyval == GDK_KEY_T
899             && event->state & GDK_SHIFT_MASK
900             && event->state & GDK_CONTROL_MASK) {
901                 chat_manager = empathy_chat_manager_dup_singleton ();
902                 empathy_chat_manager_undo_closed_chat (chat_manager);
903                 g_object_unref (chat_manager);
904         }
905         return FALSE;
906 }
907
908 static void
909 main_window_chat_quit_cb (GtkAction         *action,
910                           EmpathyMainWindow *window)
911 {
912         gtk_main_quit ();
913 }
914
915 static void
916 main_window_view_history_cb (GtkAction         *action,
917                              EmpathyMainWindow *window)
918 {
919         empathy_log_window_show (NULL, NULL, FALSE, GTK_WINDOW (window));
920 }
921
922 static void
923 main_window_chat_new_message_cb (GtkAction         *action,
924                                  EmpathyMainWindow *window)
925 {
926         empathy_new_message_dialog_show (GTK_WINDOW (window));
927 }
928
929 static void
930 main_window_chat_new_call_cb (GtkAction         *action,
931                               EmpathyMainWindow *window)
932 {
933         empathy_new_call_dialog_show (GTK_WINDOW (window));
934 }
935
936 static void
937 main_window_chat_add_contact_cb (GtkAction         *action,
938                                  EmpathyMainWindow *window)
939 {
940         empathy_new_individual_dialog_show (GTK_WINDOW (window));
941 }
942
943 static void
944 main_window_chat_search_contacts_cb (GtkAction         *action,
945                                      EmpathyMainWindow *window)
946 {
947         GtkWidget *dialog = empathy_contact_search_dialog_new (
948                         GTK_WINDOW (window));
949         gtk_widget_show (dialog);
950 }
951
952 static void
953 main_window_view_show_ft_manager (GtkAction         *action,
954                                   EmpathyMainWindow *window)
955 {
956         empathy_ft_manager_show ();
957 }
958
959 static void
960 main_window_view_show_offline_cb (GtkToggleAction   *action,
961                                   EmpathyMainWindow *window)
962 {
963         EmpathyMainWindowPriv *priv = GET_PRIV (window);
964         gboolean current;
965
966         current = gtk_toggle_action_get_active (action);
967         g_settings_set_boolean (priv->gsettings_ui,
968                                 EMPATHY_PREFS_UI_SHOW_OFFLINE,
969                                 current);
970
971         empathy_individual_view_set_show_offline (priv->individual_view,
972                         current);
973 }
974
975 static void
976 main_window_notify_sort_contact_cb (GSettings         *gsettings,
977                                     const gchar       *key,
978                                     EmpathyMainWindow *window)
979 {
980         EmpathyMainWindowPriv *priv = GET_PRIV (window);
981         gchar *str;
982
983         str = g_settings_get_string (gsettings, key);
984
985         if (str != NULL) {
986                 GType       type;
987                 GEnumClass *enum_class;
988                 GEnumValue *enum_value;
989
990                 type = empathy_individual_store_sort_get_type ();
991                 enum_class = G_ENUM_CLASS (g_type_class_peek (type));
992                 enum_value = g_enum_get_value_by_nick (enum_class, str);
993                 if (enum_value) {
994                         /* By changing the value of the GtkRadioAction,
995                            it emits a signal that calls main_window_view_sort_contacts_cb
996                            which updates the contacts list */
997                         gtk_radio_action_set_current_value (priv->sort_by_name,
998                                                             enum_value->value);
999                 } else {
1000                         g_warning ("Wrong value for sort_criterium configuration : %s", str);
1001                 }
1002                 g_free (str);
1003         }
1004 }
1005
1006 static void
1007 main_window_view_sort_contacts_cb (GtkRadioAction    *action,
1008                                    GtkRadioAction    *current,
1009                                    EmpathyMainWindow *window)
1010 {
1011         EmpathyMainWindowPriv *priv = GET_PRIV (window);
1012         EmpathyContactListStoreSort value;
1013         GSList      *group;
1014         GType        type;
1015         GEnumClass  *enum_class;
1016         GEnumValue  *enum_value;
1017
1018         value = gtk_radio_action_get_current_value (action);
1019         group = gtk_radio_action_get_group (action);
1020
1021         /* Get string from index */
1022         type = empathy_individual_store_sort_get_type ();
1023         enum_class = G_ENUM_CLASS (g_type_class_peek (type));
1024         enum_value = g_enum_get_value (enum_class, g_slist_index (group, current));
1025
1026         if (!enum_value) {
1027                 g_warning ("No GEnumValue for EmpathyContactListSort with GtkRadioAction index:%d",
1028                            g_slist_index (group, action));
1029         } else {
1030                 g_settings_set_string (priv->gsettings_contacts,
1031                                        EMPATHY_PREFS_CONTACTS_SORT_CRITERIUM,
1032                                        enum_value->value_nick);
1033         }
1034         empathy_individual_store_set_sort_criterium (priv->individual_store,
1035                         value);
1036 }
1037
1038 static void
1039 main_window_view_show_protocols_cb (GtkToggleAction   *action,
1040                                     EmpathyMainWindow *window)
1041 {
1042         EmpathyMainWindowPriv *priv = GET_PRIV (window);
1043         gboolean value;
1044
1045         value = gtk_toggle_action_get_active (action);
1046
1047         g_settings_set_boolean (priv->gsettings_ui,
1048                                 EMPATHY_PREFS_UI_SHOW_PROTOCOLS,
1049                                 value);
1050         empathy_individual_store_set_show_protocols (priv->individual_store,
1051                                                      value);
1052 }
1053
1054 /* Matches GtkRadioAction values set in empathy-main-window.ui */
1055 #define CONTACT_LIST_NORMAL_SIZE_WITH_AVATARS           0
1056 #define CONTACT_LIST_NORMAL_SIZE                        1
1057 #define CONTACT_LIST_COMPACT_SIZE                       2
1058
1059 static void
1060 main_window_view_contacts_list_size_cb (GtkRadioAction    *action,
1061                                         GtkRadioAction    *current,
1062                                         EmpathyMainWindow *window)
1063 {
1064         EmpathyMainWindowPriv *priv = GET_PRIV (window);
1065         GSettings *gsettings_ui;
1066         gint value;
1067
1068         value = gtk_radio_action_get_current_value (action);
1069         /* create a new GSettings, so we can delay the setting until both
1070          * values are set */
1071         gsettings_ui = g_settings_new (EMPATHY_PREFS_UI_SCHEMA);
1072
1073         DEBUG ("radio button toggled, value = %i", value);
1074
1075         g_settings_delay (gsettings_ui);
1076         g_settings_set_boolean (gsettings_ui,
1077                                 EMPATHY_PREFS_UI_SHOW_AVATARS,
1078                                 value == CONTACT_LIST_NORMAL_SIZE_WITH_AVATARS);
1079
1080         g_settings_set_boolean (gsettings_ui,
1081                                 EMPATHY_PREFS_UI_COMPACT_CONTACT_LIST,
1082                                 value == CONTACT_LIST_COMPACT_SIZE);
1083         g_settings_apply (gsettings_ui);
1084
1085         /* FIXME: these enums probably have the wrong namespace */
1086         empathy_individual_store_set_show_avatars (priv->individual_store,
1087                         value == CONTACT_LIST_NORMAL_SIZE_WITH_AVATARS);
1088         empathy_individual_store_set_is_compact (priv->individual_store,
1089                         value == CONTACT_LIST_COMPACT_SIZE);
1090
1091         g_object_unref (gsettings_ui);
1092 }
1093
1094 static void main_window_notify_show_protocols_cb (GSettings         *gsettings,
1095                                                   const gchar       *key,
1096                                                   EmpathyMainWindow *window)
1097 {
1098         EmpathyMainWindowPriv *priv = GET_PRIV (window);
1099
1100         gtk_toggle_action_set_active (priv->show_protocols,
1101                         g_settings_get_boolean (gsettings,
1102                                 EMPATHY_PREFS_UI_SHOW_PROTOCOLS));
1103 }
1104
1105
1106 static void
1107 main_window_notify_contact_list_size_cb (GSettings         *gsettings,
1108                                          const gchar       *key,
1109                                          EmpathyMainWindow *window)
1110 {
1111         EmpathyMainWindowPriv *priv = GET_PRIV (window);
1112         gint value;
1113
1114         if (g_settings_get_boolean (gsettings,
1115                         EMPATHY_PREFS_UI_COMPACT_CONTACT_LIST)) {
1116                 value = CONTACT_LIST_COMPACT_SIZE;
1117         } else if (g_settings_get_boolean (gsettings,
1118                         EMPATHY_PREFS_UI_SHOW_AVATARS)) {
1119                 value = CONTACT_LIST_NORMAL_SIZE_WITH_AVATARS;
1120         } else {
1121                 value = CONTACT_LIST_NORMAL_SIZE;
1122         }
1123
1124         DEBUG ("setting changed, value = %i", value);
1125
1126         /* By changing the value of the GtkRadioAction,
1127            it emits a signal that calls main_window_view_contacts_list_size_cb
1128            which updates the contacts list */
1129         gtk_radio_action_set_current_value (priv->normal_with_avatars, value);
1130 }
1131
1132 static void
1133 main_window_edit_search_contacts_cb (GtkCheckMenuItem  *item,
1134                                      EmpathyMainWindow *window)
1135 {
1136         EmpathyMainWindowPriv *priv = GET_PRIV (window);
1137
1138         empathy_individual_view_start_search (priv->individual_view);
1139 }
1140
1141 static void
1142 main_window_view_show_map_cb (GtkCheckMenuItem  *item,
1143                               EmpathyMainWindow *window)
1144 {
1145 #ifdef HAVE_LIBCHAMPLAIN
1146         empathy_map_view_show ();
1147 #endif
1148 }
1149
1150 static void
1151 join_chatroom (EmpathyChatroom *chatroom,
1152                gint64 timestamp)
1153 {
1154         TpAccount      *account;
1155         const gchar    *room;
1156
1157         account = empathy_chatroom_get_account (chatroom);
1158         room = empathy_chatroom_get_room (chatroom);
1159
1160         DEBUG ("Requesting channel for '%s'", room);
1161         empathy_dispatcher_join_muc (account, room, timestamp);
1162 }
1163
1164 typedef struct
1165 {
1166         TpAccount *account;
1167         EmpathyChatroom *chatroom;
1168         gint64 timestamp;
1169         glong sig_id;
1170         guint timeout;
1171 } join_fav_account_sig_ctx;
1172
1173 static join_fav_account_sig_ctx *
1174 join_fav_account_sig_ctx_new (TpAccount *account,
1175                              EmpathyChatroom *chatroom,
1176                               gint64 timestamp)
1177 {
1178         join_fav_account_sig_ctx *ctx = g_slice_new0 (
1179                 join_fav_account_sig_ctx);
1180
1181         ctx->account = g_object_ref (account);
1182         ctx->chatroom = g_object_ref (chatroom);
1183         ctx->timestamp = timestamp;
1184         return ctx;
1185 }
1186
1187 static void
1188 join_fav_account_sig_ctx_free (join_fav_account_sig_ctx *ctx)
1189 {
1190         g_object_unref (ctx->account);
1191         g_object_unref (ctx->chatroom);
1192         g_slice_free (join_fav_account_sig_ctx, ctx);
1193 }
1194
1195 static void
1196 account_status_changed_cb (TpAccount  *account,
1197                            TpConnectionStatus old_status,
1198                            TpConnectionStatus new_status,
1199                            guint reason,
1200                            gchar *dbus_error_name,
1201                            GHashTable *details,
1202                            gpointer user_data)
1203 {
1204         join_fav_account_sig_ctx *ctx = user_data;
1205
1206         switch (new_status) {
1207                 case TP_CONNECTION_STATUS_DISCONNECTED:
1208                         /* Don't wait any longer */
1209                         goto finally;
1210                         break;
1211
1212                 case TP_CONNECTION_STATUS_CONNECTING:
1213                         /* Wait a bit */
1214                         return;
1215
1216                 case TP_CONNECTION_STATUS_CONNECTED:
1217                         /* We can join the room */
1218                         break;
1219
1220                 default:
1221                         g_assert_not_reached ();
1222         }
1223
1224         join_chatroom (ctx->chatroom, ctx->timestamp);
1225
1226 finally:
1227         g_source_remove (ctx->timeout);
1228         g_signal_handler_disconnect (account, ctx->sig_id);
1229 }
1230
1231 #define JOIN_FAVORITE_TIMEOUT 5
1232
1233 static gboolean
1234 join_favorite_timeout_cb (gpointer data)
1235 {
1236         join_fav_account_sig_ctx *ctx = data;
1237
1238         /* stop waiting for joining the favorite room */
1239         g_signal_handler_disconnect (ctx->account, ctx->sig_id);
1240         return FALSE;
1241 }
1242
1243 static void
1244 main_window_favorite_chatroom_join (EmpathyChatroom *chatroom)
1245 {
1246         TpAccount      *account;
1247
1248         account = empathy_chatroom_get_account (chatroom);
1249         if (tp_account_get_connection_status (account, NULL) !=
1250                                              TP_CONNECTION_STATUS_CONNECTED) {
1251                 join_fav_account_sig_ctx *ctx;
1252
1253                 ctx = join_fav_account_sig_ctx_new (account, chatroom,
1254                         gtk_get_current_event_time ());
1255
1256                 ctx->sig_id = g_signal_connect_data (account, "status-changed",
1257                         G_CALLBACK (account_status_changed_cb), ctx,
1258                         (GClosureNotify) join_fav_account_sig_ctx_free, 0);
1259
1260                 ctx->timeout = g_timeout_add_seconds (JOIN_FAVORITE_TIMEOUT,
1261                         join_favorite_timeout_cb, ctx);
1262                 return;
1263         }
1264
1265         join_chatroom (chatroom, gtk_get_current_event_time ());
1266 }
1267
1268 static void
1269 main_window_favorite_chatroom_menu_activate_cb (GtkMenuItem     *menu_item,
1270                                                 EmpathyChatroom *chatroom)
1271 {
1272         main_window_favorite_chatroom_join (chatroom);
1273 }
1274
1275 static void
1276 main_window_favorite_chatroom_menu_add (EmpathyMainWindow *window,
1277                                         EmpathyChatroom   *chatroom)
1278 {
1279         EmpathyMainWindowPriv *priv = GET_PRIV (window);
1280         GtkWidget   *menu_item;
1281         const gchar *name;
1282
1283         if (g_object_get_data (G_OBJECT (chatroom), "menu_item")) {
1284                 return;
1285         }
1286
1287         name = empathy_chatroom_get_name (chatroom);
1288         menu_item = gtk_menu_item_new_with_label (name);
1289         g_object_set_data (G_OBJECT (menu_item), "is_favorite",
1290                         GUINT_TO_POINTER (TRUE));
1291
1292         g_object_set_data (G_OBJECT (chatroom), "menu_item", menu_item);
1293         g_signal_connect (menu_item, "activate",
1294                           G_CALLBACK (main_window_favorite_chatroom_menu_activate_cb),
1295                           chatroom);
1296
1297         gtk_menu_shell_insert (GTK_MENU_SHELL (priv->room_menu),
1298                                menu_item, 4);
1299
1300         gtk_widget_show (menu_item);
1301 }
1302
1303 static void
1304 main_window_favorite_chatroom_menu_added_cb (EmpathyChatroomManager *manager,
1305                                              EmpathyChatroom        *chatroom,
1306                                              EmpathyMainWindow      *window)
1307 {
1308         EmpathyMainWindowPriv *priv = GET_PRIV (window);
1309
1310         main_window_favorite_chatroom_menu_add (window, chatroom);
1311         gtk_widget_show (priv->room_separator);
1312         gtk_action_set_sensitive (priv->room_join_favorites, TRUE);
1313 }
1314
1315 static void
1316 main_window_favorite_chatroom_menu_removed_cb (EmpathyChatroomManager *manager,
1317                                                EmpathyChatroom        *chatroom,
1318                                                EmpathyMainWindow      *window)
1319 {
1320         EmpathyMainWindowPriv *priv = GET_PRIV (window);
1321         GtkWidget *menu_item;
1322         GList *chatrooms;
1323
1324         menu_item = g_object_get_data (G_OBJECT (chatroom), "menu_item");
1325         g_object_set_data (G_OBJECT (chatroom), "menu_item", NULL);
1326         gtk_widget_destroy (menu_item);
1327
1328         chatrooms = empathy_chatroom_manager_get_chatrooms (priv->chatroom_manager, NULL);
1329         if (chatrooms) {
1330                 gtk_widget_show (priv->room_separator);
1331         } else {
1332                 gtk_widget_hide (priv->room_separator);
1333         }
1334
1335         gtk_action_set_sensitive (priv->room_join_favorites, chatrooms != NULL);
1336         g_list_free (chatrooms);
1337 }
1338
1339 static void
1340 main_window_favorite_chatroom_menu_setup (EmpathyMainWindow *window)
1341 {
1342         EmpathyMainWindowPriv *priv = GET_PRIV (window);
1343         GList *chatrooms, *l;
1344         GtkWidget *room;
1345
1346         priv->chatroom_manager = empathy_chatroom_manager_dup_singleton (NULL);
1347         chatrooms = empathy_chatroom_manager_get_chatrooms (
1348                 priv->chatroom_manager, NULL);
1349         room = gtk_ui_manager_get_widget (priv->ui_manager,
1350                 "/menubar/room");
1351         priv->room_menu = gtk_menu_item_get_submenu (GTK_MENU_ITEM (room));
1352         priv->room_separator = gtk_ui_manager_get_widget (priv->ui_manager,
1353                 "/menubar/room/room_separator");
1354
1355         for (l = chatrooms; l; l = l->next) {
1356                 main_window_favorite_chatroom_menu_add (window, l->data);
1357         }
1358
1359         if (!chatrooms) {
1360                 gtk_widget_hide (priv->room_separator);
1361         }
1362
1363         gtk_action_set_sensitive (priv->room_join_favorites, chatrooms != NULL);
1364
1365         g_signal_connect (priv->chatroom_manager, "chatroom-added",
1366                           G_CALLBACK (main_window_favorite_chatroom_menu_added_cb),
1367                           window);
1368         g_signal_connect (priv->chatroom_manager, "chatroom-removed",
1369                           G_CALLBACK (main_window_favorite_chatroom_menu_removed_cb),
1370                           window);
1371
1372         g_list_free (chatrooms);
1373 }
1374
1375 static void
1376 main_window_room_join_new_cb (GtkAction         *action,
1377                               EmpathyMainWindow *window)
1378 {
1379         empathy_new_chatroom_dialog_show (GTK_WINDOW (window));
1380 }
1381
1382 static void
1383 main_window_room_join_favorites_cb (GtkAction         *action,
1384                                     EmpathyMainWindow *window)
1385 {
1386         EmpathyMainWindowPriv *priv = GET_PRIV (window);
1387         GList *chatrooms, *l;
1388
1389         chatrooms = empathy_chatroom_manager_get_chatrooms (priv->chatroom_manager, NULL);
1390         for (l = chatrooms; l; l = l->next) {
1391                 main_window_favorite_chatroom_join (l->data);
1392         }
1393         g_list_free (chatrooms);
1394 }
1395
1396 static void
1397 main_window_room_manage_favorites_cb (GtkAction         *action,
1398                                       EmpathyMainWindow *window)
1399 {
1400         empathy_chatrooms_window_show (GTK_WINDOW (window));
1401 }
1402
1403 static void
1404 main_window_edit_cb (GtkAction         *action,
1405                      EmpathyMainWindow *window)
1406 {
1407         EmpathyMainWindowPriv *priv = GET_PRIV (window);
1408         GtkWidget *submenu;
1409
1410         /* FIXME: It should use the UIManager to merge the contact/group submenu */
1411         submenu = empathy_individual_view_get_individual_menu (
1412                         priv->individual_view);
1413         if (submenu) {
1414                 GtkMenuItem *item;
1415                 GtkWidget   *label;
1416
1417                 item = GTK_MENU_ITEM (priv->edit_context);
1418                 label = gtk_bin_get_child (GTK_BIN (item));
1419                 gtk_label_set_text (GTK_LABEL (label), _("Contact"));
1420
1421                 gtk_widget_show (priv->edit_context);
1422                 gtk_widget_show (priv->edit_context_separator);
1423
1424                 gtk_menu_item_set_submenu (item, submenu);
1425
1426                 return;
1427         }
1428
1429         submenu = empathy_individual_view_get_group_menu (
1430                         priv->individual_view);
1431         if (submenu) {
1432                 GtkMenuItem *item;
1433                 GtkWidget   *label;
1434
1435                 item = GTK_MENU_ITEM (priv->edit_context);
1436                 label = gtk_bin_get_child (GTK_BIN (item));
1437                 gtk_label_set_text (GTK_LABEL (label), _("Group"));
1438
1439                 gtk_widget_show (priv->edit_context);
1440                 gtk_widget_show (priv->edit_context_separator);
1441
1442                 gtk_menu_item_set_submenu (item, submenu);
1443
1444                 return;
1445         }
1446
1447         gtk_widget_hide (priv->edit_context);
1448         gtk_widget_hide (priv->edit_context_separator);
1449
1450         return;
1451 }
1452
1453 static void
1454 main_window_edit_accounts_cb (GtkAction         *action,
1455                               EmpathyMainWindow *window)
1456 {
1457         empathy_accounts_dialog_show_application (gdk_screen_get_default (),
1458                         NULL, FALSE, FALSE);
1459 }
1460
1461 static void
1462 main_window_edit_personal_information_cb (GtkAction         *action,
1463                                           EmpathyMainWindow *window)
1464 {
1465         empathy_contact_personal_dialog_show (GTK_WINDOW (window));
1466 }
1467
1468 static void
1469 main_window_edit_preferences_cb (GtkAction         *action,
1470                                  EmpathyMainWindow *window)
1471 {
1472         EmpathyMainWindowPriv *priv = GET_PRIV (window);
1473
1474         if (priv->preferences == NULL) {
1475                 priv->preferences = empathy_preferences_new (GTK_WINDOW (window));
1476                 g_object_add_weak_pointer (G_OBJECT (priv->preferences),
1477                                            (gpointer) &priv->preferences);
1478
1479                 gtk_widget_show (priv->preferences);
1480         } else {
1481                 gtk_window_present (GTK_WINDOW (priv->preferences));
1482         }
1483 }
1484
1485 static void
1486 main_window_help_about_cb (GtkAction         *action,
1487                            EmpathyMainWindow *window)
1488 {
1489         empathy_about_dialog_new (GTK_WINDOW (window));
1490 }
1491
1492 static void
1493 main_window_help_debug_cb (GtkAction         *action,
1494                            EmpathyMainWindow *window)
1495 {
1496         GdkDisplay *display;
1497         GError *error = NULL;
1498         gchar *path;
1499         GAppInfo *app_info;
1500         GdkAppLaunchContext *context = NULL;
1501
1502         /* Try to run from source directory if possible */
1503         path = g_build_filename (g_getenv ("EMPATHY_SRCDIR"), "src",
1504                         "empathy-debugger", NULL);
1505
1506         if (!g_file_test (path, G_FILE_TEST_EXISTS)) {
1507                 g_free (path);
1508                 path = g_build_filename (BIN_DIR, "empathy-debugger", NULL);
1509         }
1510
1511         app_info = g_app_info_create_from_commandline (path, NULL, 0, &error);
1512         if (app_info == NULL) {
1513                 DEBUG ("Failed to create app info: %s", error->message);
1514                 g_error_free (error);
1515                 goto out;
1516         }
1517
1518         display = gdk_display_get_default ();
1519         context = gdk_display_get_app_launch_context (display);
1520
1521         if (!g_app_info_launch (app_info, NULL, (GAppLaunchContext *) context,
1522                 &error)) {
1523                 g_warning ("Failed to open debug window: %s", error->message);
1524                 g_error_free (error);
1525                 goto out;
1526         }
1527
1528 out:
1529         tp_clear_object (&app_info);
1530         tp_clear_object (&context);
1531         g_free (path);
1532 }
1533
1534 static void
1535 main_window_help_contents_cb (GtkAction         *action,
1536                               EmpathyMainWindow *window)
1537 {
1538         empathy_url_show (GTK_WIDGET (window), "ghelp:empathy");
1539 }
1540
1541 static gboolean
1542 main_window_throbber_button_press_event_cb (GtkWidget         *throbber,
1543                                             GdkEventButton    *event,
1544                                             EmpathyMainWindow *window)
1545 {
1546         if (event->type != GDK_BUTTON_PRESS ||
1547             event->button != 1) {
1548                 return FALSE;
1549         }
1550
1551         empathy_accounts_dialog_show_application (
1552                         gtk_widget_get_screen (GTK_WIDGET (throbber)),
1553                         NULL, FALSE, FALSE);
1554
1555         return FALSE;
1556 }
1557
1558 static void
1559 main_window_account_removed_cb (TpAccountManager  *manager,
1560                                 TpAccount         *account,
1561                                 EmpathyMainWindow *window)
1562 {
1563         EmpathyMainWindowPriv *priv = GET_PRIV (window);
1564         GList *a;
1565
1566         a = tp_account_manager_get_valid_accounts (manager);
1567
1568         gtk_action_set_sensitive (priv->view_history,
1569                 g_list_length (a) > 0);
1570
1571         g_list_free (a);
1572
1573         /* remove errors if any */
1574         main_window_remove_error (window, account);
1575 }
1576
1577 static void
1578 main_window_account_validity_changed_cb (TpAccountManager  *manager,
1579                                          TpAccount         *account,
1580                                          gboolean           valid,
1581                                          EmpathyMainWindow *window)
1582 {
1583         EmpathyMainWindowPriv *priv = GET_PRIV (window);
1584
1585         if (valid) {
1586                 gulong handler_id;
1587                 handler_id = GPOINTER_TO_UINT (g_hash_table_lookup (
1588                         priv->status_changed_handlers, account));
1589
1590                 /* connect signal only if it was not connected yet */
1591                 if (handler_id == 0) {
1592                         handler_id = g_signal_connect (account,
1593                                 "status-changed",
1594                                 G_CALLBACK (main_window_connection_changed_cb),
1595                                 window);
1596                         g_hash_table_insert (priv->status_changed_handlers,
1597                                 account, GUINT_TO_POINTER (handler_id));
1598                 }
1599         }
1600
1601         main_window_account_removed_cb (manager, account, window);
1602 }
1603
1604 static void
1605 main_window_notify_show_offline_cb (GSettings   *gsettings,
1606                                     const gchar *key,
1607                                     gpointer     toggle_action)
1608 {
1609         gtk_toggle_action_set_active (toggle_action,
1610                         g_settings_get_boolean (gsettings, key));
1611 }
1612
1613 static void
1614 main_window_connection_items_setup (EmpathyMainWindow *window,
1615                                     GtkBuilder        *gui)
1616 {
1617         EmpathyMainWindowPriv *priv = GET_PRIV (window);
1618         GList         *list;
1619         GObject       *action;
1620         guint          i;
1621         const gchar *actions_connected[] = {
1622                 "room_join_new",
1623                 "room_join_favorites",
1624                 "chat_new_message",
1625                 "chat_new_call",
1626                 "chat_add_contact",
1627                 "edit_personal_information"
1628         };
1629
1630         for (i = 0, list = NULL; i < G_N_ELEMENTS (actions_connected); i++) {
1631                 action = gtk_builder_get_object (gui, actions_connected[i]);
1632                 list = g_list_prepend (list, action);
1633         }
1634
1635         priv->actions_connected = list;
1636 }
1637
1638 static void
1639 account_manager_prepared_cb (GObject      *source_object,
1640                              GAsyncResult *result,
1641                              gpointer      user_data)
1642 {
1643         GList *accounts, *j;
1644         TpAccountManager *manager = TP_ACCOUNT_MANAGER (source_object);
1645         EmpathyMainWindow *window = user_data;
1646         EmpathyMainWindowPriv *priv = GET_PRIV (window);
1647         GError *error = NULL;
1648
1649         if (!tp_account_manager_prepare_finish (manager, result, &error)) {
1650                 DEBUG ("Failed to prepare account manager: %s", error->message);
1651                 g_error_free (error);
1652                 return;
1653         }
1654
1655         accounts = tp_account_manager_get_valid_accounts (priv->account_manager);
1656         for (j = accounts; j != NULL; j = j->next) {
1657                 TpAccount *account = TP_ACCOUNT (j->data);
1658                 gulong handler_id;
1659
1660                 handler_id = g_signal_connect (account, "status-changed",
1661                                   G_CALLBACK (main_window_connection_changed_cb),
1662                                   window);
1663                 g_hash_table_insert (priv->status_changed_handlers,
1664                                      account, GUINT_TO_POINTER (handler_id));
1665         }
1666
1667         g_signal_connect (manager, "account-validity-changed",
1668                           G_CALLBACK (main_window_account_validity_changed_cb),
1669                           window);
1670
1671         main_window_update_status (window);
1672
1673         /* Disable the "Previous Conversations" menu entry if there is no account */
1674         gtk_action_set_sensitive (priv->view_history,
1675                 g_list_length (accounts) > 0);
1676
1677         g_list_free (accounts);
1678 }
1679
1680 static void
1681 main_window_members_changed_cb (EmpathyContactList *list,
1682                                 EmpathyContact     *contact,
1683                                 EmpathyContact     *actor,
1684                                 guint               reason,
1685                                 gchar              *message,
1686                                 gboolean            is_member,
1687                                 EmpathyMainWindow  *window)
1688 {
1689         EmpathyMainWindowPriv *priv = GET_PRIV (window);
1690
1691         if (!is_member)
1692                 return;
1693
1694         if (!empathy_migrate_butterfly_logs (contact)) {
1695                 g_signal_handler_disconnect (list,
1696                         priv->butterfly_log_migration_members_changed_id);
1697                 priv->butterfly_log_migration_members_changed_id = 0;
1698         }
1699 }
1700
1701 static GObject *
1702 empathy_main_window_constructor (GType type,
1703                                  guint n_construct_params,
1704                                  GObjectConstructParam *construct_params)
1705 {
1706         static GObject *window = NULL;
1707
1708         if (window != NULL)
1709                 return g_object_ref (window);
1710
1711         window = G_OBJECT_CLASS (empathy_main_window_parent_class)->constructor (
1712                 type, n_construct_params, construct_params);
1713
1714         g_object_add_weak_pointer (window, (gpointer) &window);
1715
1716         return window;
1717 }
1718
1719 static void
1720 empathy_main_window_class_init (EmpathyMainWindowClass *klass)
1721 {
1722         GObjectClass *object_class = G_OBJECT_CLASS (klass);
1723
1724         object_class->finalize = empathy_main_window_finalize;
1725         object_class->constructor = empathy_main_window_constructor;
1726
1727         g_type_class_add_private (object_class, sizeof (EmpathyMainWindowPriv));
1728 }
1729
1730 static void
1731 empathy_main_window_init (EmpathyMainWindow *window)
1732 {
1733         EmpathyMainWindowPriv    *priv;
1734         EmpathyIndividualManager *individual_manager;
1735         GtkBuilder               *gui;
1736         GtkWidget                *sw;
1737         GtkToggleAction          *show_offline_widget;
1738         GtkAction                *show_map_widget;
1739         GtkToolItem              *item;
1740         gboolean                  show_offline;
1741         gchar                    *filename;
1742         GSList                   *l;
1743         GtkTreeModel             *model;
1744
1745         priv = window->priv = G_TYPE_INSTANCE_GET_PRIVATE (window,
1746                         EMPATHY_TYPE_MAIN_WINDOW, EmpathyMainWindowPriv);
1747
1748         priv->gsettings_ui = g_settings_new (EMPATHY_PREFS_UI_SCHEMA);
1749         priv->gsettings_contacts = g_settings_new (EMPATHY_PREFS_CONTACTS_SCHEMA);
1750
1751         priv->sound_mgr = empathy_sound_manager_dup_singleton ();
1752
1753         gtk_window_set_title (GTK_WINDOW (window), _("Contact List"));
1754         gtk_window_set_role (GTK_WINDOW (window), "contact_list");
1755         gtk_window_set_default_size (GTK_WINDOW (window), 225, 325);
1756
1757         /* Set up interface */
1758         filename = empathy_file_lookup ("empathy-main-window.ui", "src");
1759         gui = empathy_builder_get_file (filename,
1760                                        "main_vbox", &priv->main_vbox,
1761                                        "errors_vbox", &priv->errors_vbox,
1762                                        "auth_vbox", &priv->auth_vbox,
1763                                        "ui_manager", &priv->ui_manager,
1764                                        "view_show_offline", &show_offline_widget,
1765                                        "view_show_protocols", &priv->show_protocols,
1766                                        "view_sort_by_name", &priv->sort_by_name,
1767                                        "view_sort_by_status", &priv->sort_by_status,
1768                                        "view_normal_size_with_avatars", &priv->normal_with_avatars,
1769                                        "view_normal_size", &priv->normal_size,
1770                                        "view_compact_size", &priv->compact_size,
1771                                        "view_history", &priv->view_history,
1772                                        "view_show_map", &show_map_widget,
1773                                        "room_join_favorites", &priv->room_join_favorites,
1774                                        "presence_toolbar", &priv->presence_toolbar,
1775                                        "notebook", &priv->notebook,
1776                                        "no_entry_label", &priv->no_entry_label,
1777                                        "roster_scrolledwindow", &sw,
1778                                        NULL);
1779         g_free (filename);
1780
1781         gtk_container_add (GTK_CONTAINER (window), priv->main_vbox);
1782         gtk_widget_show (priv->main_vbox);
1783
1784         g_signal_connect (window, "key-press-event",
1785                           G_CALLBACK (main_window_key_press_event_cb), NULL);
1786
1787         empathy_builder_connect (gui, window,
1788                               "chat_quit", "activate", main_window_chat_quit_cb,
1789                               "chat_new_message", "activate", main_window_chat_new_message_cb,
1790                               "chat_new_call", "activate", main_window_chat_new_call_cb,
1791                               "view_history", "activate", main_window_view_history_cb,
1792                               "room_join_new", "activate", main_window_room_join_new_cb,
1793                               "room_join_favorites", "activate", main_window_room_join_favorites_cb,
1794                               "room_manage_favorites", "activate", main_window_room_manage_favorites_cb,
1795                               "chat_add_contact", "activate", main_window_chat_add_contact_cb,
1796                               "chat_search_contacts", "activate", main_window_chat_search_contacts_cb,
1797                               "view_show_ft_manager", "activate", main_window_view_show_ft_manager,
1798                               "view_show_offline", "toggled", main_window_view_show_offline_cb,
1799                               "view_show_protocols", "toggled", main_window_view_show_protocols_cb,
1800                               "view_sort_by_name", "changed", main_window_view_sort_contacts_cb,
1801                               "view_normal_size_with_avatars", "changed", main_window_view_contacts_list_size_cb,
1802                               "view_show_map", "activate", main_window_view_show_map_cb,
1803                               "edit", "activate", main_window_edit_cb,
1804                               "edit_accounts", "activate", main_window_edit_accounts_cb,
1805                               "edit_personal_information", "activate", main_window_edit_personal_information_cb,
1806                               "edit_preferences", "activate", main_window_edit_preferences_cb,
1807                               "edit_search_contacts", "activate", main_window_edit_search_contacts_cb,
1808                               "help_about", "activate", main_window_help_about_cb,
1809                               "help_debug", "activate", main_window_help_debug_cb,
1810                               "help_contents", "activate", main_window_help_contents_cb,
1811                               NULL);
1812
1813         /* Set up connection related widgets. */
1814         main_window_connection_items_setup (window, gui);
1815
1816         g_object_ref (priv->ui_manager);
1817         g_object_unref (gui);
1818
1819 #ifndef HAVE_LIBCHAMPLAIN
1820         gtk_action_set_visible (show_map_widget, FALSE);
1821 #endif
1822
1823         priv->account_manager = tp_account_manager_dup ();
1824
1825         tp_account_manager_prepare_async (priv->account_manager, NULL,
1826                                           account_manager_prepared_cb, window);
1827
1828         priv->errors = g_hash_table_new_full (g_direct_hash,
1829                                               g_direct_equal,
1830                                               g_object_unref,
1831                                               NULL);
1832
1833         priv->auths = g_hash_table_new (NULL, NULL);
1834
1835         priv->status_changed_handlers = g_hash_table_new_full (g_direct_hash,
1836                                                                g_direct_equal,
1837                                                                NULL,
1838                                                                NULL);
1839
1840         /* Set up menu */
1841         main_window_favorite_chatroom_menu_setup (window);
1842
1843         priv->edit_context = gtk_ui_manager_get_widget (priv->ui_manager,
1844                 "/menubar/edit/edit_context");
1845         priv->edit_context_separator = gtk_ui_manager_get_widget (
1846                 priv->ui_manager,
1847                 "/menubar/edit/edit_context_separator");
1848         gtk_widget_hide (priv->edit_context);
1849         gtk_widget_hide (priv->edit_context_separator);
1850
1851         /* Set up contact list. */
1852         empathy_status_presets_get_all ();
1853
1854         /* Set up presence chooser */
1855         priv->presence_chooser = empathy_presence_chooser_new ();
1856         gtk_widget_show (priv->presence_chooser);
1857         item = gtk_tool_item_new ();
1858         gtk_widget_show (GTK_WIDGET (item));
1859         gtk_widget_set_size_request (priv->presence_chooser, 10, -1);
1860         gtk_container_add (GTK_CONTAINER (item), priv->presence_chooser);
1861         gtk_tool_item_set_is_important (item, TRUE);
1862         gtk_tool_item_set_expand (item, TRUE);
1863         gtk_toolbar_insert (GTK_TOOLBAR (priv->presence_toolbar), item, -1);
1864
1865         /* Set up the throbber */
1866         priv->throbber = gtk_spinner_new ();
1867         gtk_widget_set_size_request (priv->throbber, 16, -1);
1868         gtk_widget_set_tooltip_text (priv->throbber, _("Show and edit accounts"));
1869         gtk_widget_set_events (priv->throbber, GDK_BUTTON_PRESS_MASK);
1870         g_signal_connect (priv->throbber, "button-press-event",
1871                 G_CALLBACK (main_window_throbber_button_press_event_cb),
1872                 window);
1873         gtk_widget_show (priv->throbber);
1874
1875         item = gtk_tool_item_new ();
1876         gtk_container_set_border_width (GTK_CONTAINER (item), 6);
1877         gtk_toolbar_insert (GTK_TOOLBAR (priv->presence_toolbar), item, -1);
1878         gtk_container_add (GTK_CONTAINER (item), priv->throbber);
1879         priv->throbber_tool_item = GTK_WIDGET (item);
1880
1881         /* XXX: this class is designed to live for the duration of the program,
1882          * so it's got a race condition between its signal handlers and its
1883          * finalization. The class is planned to be removed, so we won't fix
1884          * this before then. */
1885         priv->contact_manager = EMPATHY_CONTACT_LIST (
1886                         empathy_contact_manager_dup_singleton ());
1887         individual_manager = empathy_individual_manager_dup_singleton ();
1888         priv->individual_store = empathy_individual_store_new (
1889                         individual_manager);
1890         g_object_unref (individual_manager);
1891
1892         /* For the moment, we disallow Persona drops onto the main contact list (e.g. from things such as
1893          * the EmpathyPersonaView in the linking dialogue). No code is hooked up to do anything on a Persona
1894          * drop, so allowing them would achieve nothing except confusion. */
1895         priv->individual_view = empathy_individual_view_new (
1896                         priv->individual_store,
1897                         EMPATHY_INDIVIDUAL_VIEW_FEATURE_ALL ^ EMPATHY_INDIVIDUAL_VIEW_FEATURE_PERSONA_DROP,
1898                         EMPATHY_INDIVIDUAL_FEATURE_ALL);
1899
1900         priv->butterfly_log_migration_members_changed_id = g_signal_connect (
1901                         priv->contact_manager, "members-changed",
1902                         G_CALLBACK (main_window_members_changed_cb), window);
1903
1904         gtk_widget_show (GTK_WIDGET (priv->individual_view));
1905         gtk_container_add (GTK_CONTAINER (sw),
1906                            GTK_WIDGET (priv->individual_view));
1907         g_signal_connect (priv->individual_view, "row-activated",
1908                           G_CALLBACK (main_window_row_activated_cb),
1909                           window);
1910
1911         /* Set up search bar */
1912         priv->search_bar = empathy_live_search_new (
1913                 GTK_WIDGET (priv->individual_view));
1914         empathy_individual_view_set_live_search (priv->individual_view,
1915                 EMPATHY_LIVE_SEARCH (priv->search_bar));
1916         gtk_box_pack_start (GTK_BOX (priv->main_vbox), priv->search_bar,
1917                 FALSE, TRUE, 0);
1918         g_signal_connect_swapped (window, "map",
1919                 G_CALLBACK (gtk_widget_grab_focus), priv->individual_view);
1920
1921         /* Connect to proper signals to check if contact list is empty or not */
1922         model = gtk_tree_view_get_model (GTK_TREE_VIEW (priv->individual_view));
1923         priv->empty = TRUE;
1924         g_signal_connect (model, "row-inserted",
1925                           G_CALLBACK (main_window_row_inserted_cb),
1926                           window);
1927         g_signal_connect (model, "row-deleted",
1928                           G_CALLBACK (main_window_row_deleted_cb),
1929                           window);
1930
1931         /* Load user-defined accelerators. */
1932         main_window_accels_load ();
1933
1934         /* Set window size. */
1935         empathy_geometry_bind (GTK_WINDOW (window), GEOMETRY_NAME);
1936
1937         /* Enable event handling */
1938         /*
1939         * FIXME: https://bugzilla.gnome.org/show_bug.cgi?id=644127
1940         priv->call_observer = empathy_call_observer_dup_singleton ();
1941         */
1942         priv->event_manager = empathy_event_manager_dup_singleton ();
1943
1944         g_signal_connect (priv->event_manager, "event-added",
1945                           G_CALLBACK (main_window_event_added_cb), window);
1946         g_signal_connect (priv->event_manager, "event-removed",
1947                           G_CALLBACK (main_window_event_removed_cb), window);
1948         g_signal_connect (priv->account_manager, "account-validity-changed",
1949                           G_CALLBACK (main_window_account_validity_changed_cb),
1950                           window);
1951         g_signal_connect (priv->account_manager, "account-removed",
1952                           G_CALLBACK (main_window_account_removed_cb),
1953                           window);
1954         g_signal_connect (priv->account_manager, "account-disabled",
1955                           G_CALLBACK (main_window_account_disabled_cb),
1956                           window);
1957
1958         l = empathy_event_manager_get_events (priv->event_manager);
1959         while (l) {
1960                 main_window_event_added_cb (priv->event_manager, l->data,
1961                                 window);
1962                 l = l->next;
1963         }
1964
1965         /* Show offline ? */
1966         show_offline = g_settings_get_boolean (priv->gsettings_ui,
1967                                                EMPATHY_PREFS_UI_SHOW_OFFLINE);
1968         g_signal_connect (priv->gsettings_ui,
1969                           "changed::" EMPATHY_PREFS_UI_SHOW_OFFLINE,
1970                           G_CALLBACK (main_window_notify_show_offline_cb),
1971                           show_offline_widget);
1972
1973         gtk_toggle_action_set_active (show_offline_widget, show_offline);
1974
1975         /* Show protocol ? */
1976         g_signal_connect (priv->gsettings_ui,
1977                           "changed::" EMPATHY_PREFS_UI_SHOW_PROTOCOLS,
1978                           G_CALLBACK (main_window_notify_show_protocols_cb),
1979                           window);
1980
1981         main_window_notify_show_protocols_cb (priv->gsettings_ui,
1982                                               EMPATHY_PREFS_UI_SHOW_PROTOCOLS,
1983                                               window);
1984
1985         /* Sort by name / by status ? */
1986         g_signal_connect (priv->gsettings_contacts,
1987                           "changed::" EMPATHY_PREFS_CONTACTS_SORT_CRITERIUM,
1988                           G_CALLBACK (main_window_notify_sort_contact_cb),
1989                           window);
1990
1991         main_window_notify_sort_contact_cb (priv->gsettings_contacts,
1992                                             EMPATHY_PREFS_CONTACTS_SORT_CRITERIUM,
1993                                             window);
1994
1995         /* Contacts list size */
1996         g_signal_connect (priv->gsettings_ui,
1997                           "changed::" EMPATHY_PREFS_UI_COMPACT_CONTACT_LIST,
1998                           G_CALLBACK (main_window_notify_contact_list_size_cb),
1999                           window);
2000         g_signal_connect (priv->gsettings_ui,
2001                           "changed::" EMPATHY_PREFS_UI_SHOW_AVATARS,
2002                           G_CALLBACK (main_window_notify_contact_list_size_cb),
2003                           window);
2004
2005         main_window_notify_contact_list_size_cb (priv->gsettings_ui,
2006                                                  EMPATHY_PREFS_UI_SHOW_AVATARS,
2007                                                  window);
2008 }
2009
2010 GtkWidget *
2011 empathy_main_window_dup (void)
2012 {
2013         return g_object_new (EMPATHY_TYPE_MAIN_WINDOW, NULL);
2014 }