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