]> git.0d.be Git - empathy.git/blob - src/empathy-main-window.c
Merge branch 'glassrose-contact-blocking-rebase'
[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-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_dispatcher_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;
1281
1282         if (g_object_get_data (G_OBJECT (chatroom), "menu_item")) {
1283                 return;
1284         }
1285
1286         name = empathy_chatroom_get_name (chatroom);
1287         menu_item = gtk_menu_item_new_with_label (name);
1288         g_object_set_data (G_OBJECT (menu_item), "is_favorite",
1289                         GUINT_TO_POINTER (TRUE));
1290
1291         g_object_set_data (G_OBJECT (chatroom), "menu_item", menu_item);
1292         g_signal_connect (menu_item, "activate",
1293                           G_CALLBACK (main_window_favorite_chatroom_menu_activate_cb),
1294                           chatroom);
1295
1296         gtk_menu_shell_insert (GTK_MENU_SHELL (priv->room_menu),
1297                                menu_item, 4);
1298
1299         gtk_widget_show (menu_item);
1300 }
1301
1302 static void
1303 main_window_favorite_chatroom_menu_added_cb (EmpathyChatroomManager *manager,
1304                                              EmpathyChatroom        *chatroom,
1305                                              EmpathyMainWindow      *window)
1306 {
1307         EmpathyMainWindowPriv *priv = GET_PRIV (window);
1308
1309         main_window_favorite_chatroom_menu_add (window, chatroom);
1310         gtk_widget_show (priv->room_separator);
1311         gtk_action_set_sensitive (priv->room_join_favorites, TRUE);
1312 }
1313
1314 static void
1315 main_window_favorite_chatroom_menu_removed_cb (EmpathyChatroomManager *manager,
1316                                                EmpathyChatroom        *chatroom,
1317                                                EmpathyMainWindow      *window)
1318 {
1319         EmpathyMainWindowPriv *priv = GET_PRIV (window);
1320         GtkWidget *menu_item;
1321         GList *chatrooms;
1322
1323         menu_item = g_object_get_data (G_OBJECT (chatroom), "menu_item");
1324         g_object_set_data (G_OBJECT (chatroom), "menu_item", NULL);
1325         gtk_widget_destroy (menu_item);
1326
1327         chatrooms = empathy_chatroom_manager_get_chatrooms (priv->chatroom_manager, NULL);
1328         if (chatrooms) {
1329                 gtk_widget_show (priv->room_separator);
1330         } else {
1331                 gtk_widget_hide (priv->room_separator);
1332         }
1333
1334         gtk_action_set_sensitive (priv->room_join_favorites, chatrooms != NULL);
1335         g_list_free (chatrooms);
1336 }
1337
1338 static void
1339 main_window_favorite_chatroom_menu_setup (EmpathyMainWindow *window)
1340 {
1341         EmpathyMainWindowPriv *priv = GET_PRIV (window);
1342         GList *chatrooms, *l;
1343         GtkWidget *room;
1344
1345         priv->chatroom_manager = empathy_chatroom_manager_dup_singleton (NULL);
1346         chatrooms = empathy_chatroom_manager_get_chatrooms (
1347                 priv->chatroom_manager, NULL);
1348         room = gtk_ui_manager_get_widget (priv->ui_manager,
1349                 "/menubar/room");
1350         priv->room_menu = gtk_menu_item_get_submenu (GTK_MENU_ITEM (room));
1351         priv->room_separator = gtk_ui_manager_get_widget (priv->ui_manager,
1352                 "/menubar/room/room_separator");
1353
1354         for (l = chatrooms; l; l = l->next) {
1355                 main_window_favorite_chatroom_menu_add (window, l->data);
1356         }
1357
1358         if (!chatrooms) {
1359                 gtk_widget_hide (priv->room_separator);
1360         }
1361
1362         gtk_action_set_sensitive (priv->room_join_favorites, chatrooms != NULL);
1363
1364         g_signal_connect (priv->chatroom_manager, "chatroom-added",
1365                           G_CALLBACK (main_window_favorite_chatroom_menu_added_cb),
1366                           window);
1367         g_signal_connect (priv->chatroom_manager, "chatroom-removed",
1368                           G_CALLBACK (main_window_favorite_chatroom_menu_removed_cb),
1369                           window);
1370
1371         g_list_free (chatrooms);
1372 }
1373
1374 static void
1375 main_window_room_join_new_cb (GtkAction         *action,
1376                               EmpathyMainWindow *window)
1377 {
1378         empathy_new_chatroom_dialog_show (GTK_WINDOW (window));
1379 }
1380
1381 static void
1382 main_window_room_join_favorites_cb (GtkAction         *action,
1383                                     EmpathyMainWindow *window)
1384 {
1385         EmpathyMainWindowPriv *priv = GET_PRIV (window);
1386         GList *chatrooms, *l;
1387
1388         chatrooms = empathy_chatroom_manager_get_chatrooms (priv->chatroom_manager, NULL);
1389         for (l = chatrooms; l; l = l->next) {
1390                 main_window_favorite_chatroom_join (l->data);
1391         }
1392         g_list_free (chatrooms);
1393 }
1394
1395 static void
1396 main_window_room_manage_favorites_cb (GtkAction         *action,
1397                                       EmpathyMainWindow *window)
1398 {
1399         empathy_chatrooms_window_show (GTK_WINDOW (window));
1400 }
1401
1402 static void
1403 main_window_edit_cb (GtkAction         *action,
1404                      EmpathyMainWindow *window)
1405 {
1406         EmpathyMainWindowPriv *priv = GET_PRIV (window);
1407         GtkWidget *submenu;
1408
1409         /* FIXME: It should use the UIManager to merge the contact/group submenu */
1410         submenu = empathy_individual_view_get_individual_menu (
1411                         priv->individual_view);
1412         if (submenu) {
1413                 GtkMenuItem *item;
1414                 GtkWidget   *label;
1415
1416                 item = GTK_MENU_ITEM (priv->edit_context);
1417                 label = gtk_bin_get_child (GTK_BIN (item));
1418                 gtk_label_set_text (GTK_LABEL (label), _("Contact"));
1419
1420                 gtk_widget_show (priv->edit_context);
1421                 gtk_widget_show (priv->edit_context_separator);
1422
1423                 gtk_menu_item_set_submenu (item, submenu);
1424
1425                 return;
1426         }
1427
1428         submenu = empathy_individual_view_get_group_menu (
1429                         priv->individual_view);
1430         if (submenu) {
1431                 GtkMenuItem *item;
1432                 GtkWidget   *label;
1433
1434                 item = GTK_MENU_ITEM (priv->edit_context);
1435                 label = gtk_bin_get_child (GTK_BIN (item));
1436                 gtk_label_set_text (GTK_LABEL (label), _("Group"));
1437
1438                 gtk_widget_show (priv->edit_context);
1439                 gtk_widget_show (priv->edit_context_separator);
1440
1441                 gtk_menu_item_set_submenu (item, submenu);
1442
1443                 return;
1444         }
1445
1446         gtk_widget_hide (priv->edit_context);
1447         gtk_widget_hide (priv->edit_context_separator);
1448
1449         return;
1450 }
1451
1452 static void
1453 main_window_edit_accounts_cb (GtkAction         *action,
1454                               EmpathyMainWindow *window)
1455 {
1456         empathy_accounts_dialog_show_application (gdk_screen_get_default (),
1457                         NULL, FALSE, FALSE);
1458 }
1459
1460 static void
1461 main_window_edit_personal_information_cb (GtkAction         *action,
1462                                           EmpathyMainWindow *window)
1463 {
1464         empathy_contact_personal_dialog_show (GTK_WINDOW (window));
1465 }
1466
1467 static void
1468 main_window_edit_blocked_contacts_cb (GtkAction         *action,
1469                                       EmpathyMainWindow *window)
1470 {
1471         GtkWidget *dialog;
1472
1473         dialog = empathy_contact_blocking_dialog_new (GTK_WINDOW (window));
1474         gtk_widget_show (dialog);
1475         g_signal_connect (dialog, "response",
1476                         G_CALLBACK (gtk_widget_destroy), NULL);
1477 }
1478
1479 static void
1480 main_window_edit_preferences_cb (GtkAction         *action,
1481                                  EmpathyMainWindow *window)
1482 {
1483         EmpathyMainWindowPriv *priv = GET_PRIV (window);
1484
1485         if (priv->preferences == NULL) {
1486                 priv->preferences = empathy_preferences_new (GTK_WINDOW (window));
1487                 g_object_add_weak_pointer (G_OBJECT (priv->preferences),
1488                                            (gpointer) &priv->preferences);
1489
1490                 gtk_widget_show (priv->preferences);
1491         } else {
1492                 gtk_window_present (GTK_WINDOW (priv->preferences));
1493         }
1494 }
1495
1496 static void
1497 main_window_help_about_cb (GtkAction         *action,
1498                            EmpathyMainWindow *window)
1499 {
1500         empathy_about_dialog_new (GTK_WINDOW (window));
1501 }
1502
1503 static void
1504 main_window_help_debug_cb (GtkAction         *action,
1505                            EmpathyMainWindow *window)
1506 {
1507         GdkDisplay *display;
1508         GError *error = NULL;
1509         gchar *path;
1510         GAppInfo *app_info;
1511         GdkAppLaunchContext *context = NULL;
1512
1513         /* Try to run from source directory if possible */
1514         path = g_build_filename (g_getenv ("EMPATHY_SRCDIR"), "src",
1515                         "empathy-debugger", NULL);
1516
1517         if (!g_file_test (path, G_FILE_TEST_EXISTS)) {
1518                 g_free (path);
1519                 path = g_build_filename (BIN_DIR, "empathy-debugger", NULL);
1520         }
1521
1522         app_info = g_app_info_create_from_commandline (path, NULL, 0, &error);
1523         if (app_info == NULL) {
1524                 DEBUG ("Failed to create app info: %s", error->message);
1525                 g_error_free (error);
1526                 goto out;
1527         }
1528
1529         display = gdk_display_get_default ();
1530         context = gdk_display_get_app_launch_context (display);
1531
1532         if (!g_app_info_launch (app_info, NULL, (GAppLaunchContext *) context,
1533                 &error)) {
1534                 g_warning ("Failed to open debug window: %s", error->message);
1535                 g_error_free (error);
1536                 goto out;
1537         }
1538
1539 out:
1540         tp_clear_object (&app_info);
1541         tp_clear_object (&context);
1542         g_free (path);
1543 }
1544
1545 static void
1546 main_window_help_contents_cb (GtkAction         *action,
1547                               EmpathyMainWindow *window)
1548 {
1549         empathy_url_show (GTK_WIDGET (window), "ghelp:empathy");
1550 }
1551
1552 static gboolean
1553 main_window_throbber_button_press_event_cb (GtkWidget         *throbber,
1554                                             GdkEventButton    *event,
1555                                             EmpathyMainWindow *window)
1556 {
1557         if (event->type != GDK_BUTTON_PRESS ||
1558             event->button != 1) {
1559                 return FALSE;
1560         }
1561
1562         empathy_accounts_dialog_show_application (
1563                         gtk_widget_get_screen (GTK_WIDGET (throbber)),
1564                         NULL, FALSE, FALSE);
1565
1566         return FALSE;
1567 }
1568
1569 static void
1570 main_window_account_removed_cb (TpAccountManager  *manager,
1571                                 TpAccount         *account,
1572                                 EmpathyMainWindow *window)
1573 {
1574         EmpathyMainWindowPriv *priv = GET_PRIV (window);
1575         GList *a;
1576
1577         a = tp_account_manager_get_valid_accounts (manager);
1578
1579         gtk_action_set_sensitive (priv->view_history,
1580                 g_list_length (a) > 0);
1581
1582         g_list_free (a);
1583
1584         /* remove errors if any */
1585         main_window_remove_error (window, account);
1586 }
1587
1588 static void
1589 main_window_account_validity_changed_cb (TpAccountManager  *manager,
1590                                          TpAccount         *account,
1591                                          gboolean           valid,
1592                                          EmpathyMainWindow *window)
1593 {
1594         EmpathyMainWindowPriv *priv = GET_PRIV (window);
1595
1596         if (valid) {
1597                 gulong handler_id;
1598                 handler_id = GPOINTER_TO_UINT (g_hash_table_lookup (
1599                         priv->status_changed_handlers, account));
1600
1601                 /* connect signal only if it was not connected yet */
1602                 if (handler_id == 0) {
1603                         handler_id = g_signal_connect (account,
1604                                 "status-changed",
1605                                 G_CALLBACK (main_window_connection_changed_cb),
1606                                 window);
1607                         g_hash_table_insert (priv->status_changed_handlers,
1608                                 account, GUINT_TO_POINTER (handler_id));
1609                 }
1610         }
1611
1612         main_window_account_removed_cb (manager, account, window);
1613 }
1614
1615 static void
1616 main_window_notify_show_offline_cb (GSettings   *gsettings,
1617                                     const gchar *key,
1618                                     gpointer     toggle_action)
1619 {
1620         gtk_toggle_action_set_active (toggle_action,
1621                         g_settings_get_boolean (gsettings, key));
1622 }
1623
1624 static void
1625 main_window_connection_items_setup (EmpathyMainWindow *window,
1626                                     GtkBuilder        *gui)
1627 {
1628         EmpathyMainWindowPriv *priv = GET_PRIV (window);
1629         GList         *list;
1630         GObject       *action;
1631         guint          i;
1632         const gchar *actions_connected[] = {
1633                 "room_join_new",
1634                 "room_join_favorites",
1635                 "chat_new_message",
1636                 "chat_new_call",
1637                 "chat_add_contact",
1638                 "edit_personal_information",
1639                 "edit_blocked_contacts"
1640         };
1641
1642         for (i = 0, list = NULL; i < G_N_ELEMENTS (actions_connected); i++) {
1643                 action = gtk_builder_get_object (gui, actions_connected[i]);
1644                 list = g_list_prepend (list, action);
1645         }
1646
1647         priv->actions_connected = list;
1648 }
1649
1650 static void
1651 account_manager_prepared_cb (GObject      *source_object,
1652                              GAsyncResult *result,
1653                              gpointer      user_data)
1654 {
1655         GList *accounts, *j;
1656         TpAccountManager *manager = TP_ACCOUNT_MANAGER (source_object);
1657         EmpathyMainWindow *window = user_data;
1658         EmpathyMainWindowPriv *priv = GET_PRIV (window);
1659         GError *error = NULL;
1660
1661         if (!tp_account_manager_prepare_finish (manager, result, &error)) {
1662                 DEBUG ("Failed to prepare account manager: %s", error->message);
1663                 g_error_free (error);
1664                 return;
1665         }
1666
1667         accounts = tp_account_manager_get_valid_accounts (priv->account_manager);
1668         for (j = accounts; j != NULL; j = j->next) {
1669                 TpAccount *account = TP_ACCOUNT (j->data);
1670                 gulong handler_id;
1671
1672                 handler_id = g_signal_connect (account, "status-changed",
1673                                   G_CALLBACK (main_window_connection_changed_cb),
1674                                   window);
1675                 g_hash_table_insert (priv->status_changed_handlers,
1676                                      account, GUINT_TO_POINTER (handler_id));
1677         }
1678
1679         g_signal_connect (manager, "account-validity-changed",
1680                           G_CALLBACK (main_window_account_validity_changed_cb),
1681                           window);
1682
1683         main_window_update_status (window);
1684
1685         /* Disable the "Previous Conversations" menu entry if there is no account */
1686         gtk_action_set_sensitive (priv->view_history,
1687                 g_list_length (accounts) > 0);
1688
1689         g_list_free (accounts);
1690 }
1691
1692 static void
1693 main_window_members_changed_cb (EmpathyContactList *list,
1694                                 EmpathyContact     *contact,
1695                                 EmpathyContact     *actor,
1696                                 guint               reason,
1697                                 gchar              *message,
1698                                 gboolean            is_member,
1699                                 EmpathyMainWindow  *window)
1700 {
1701         EmpathyMainWindowPriv *priv = GET_PRIV (window);
1702
1703         if (!is_member)
1704                 return;
1705
1706         if (!empathy_migrate_butterfly_logs (contact)) {
1707                 g_signal_handler_disconnect (list,
1708                         priv->butterfly_log_migration_members_changed_id);
1709                 priv->butterfly_log_migration_members_changed_id = 0;
1710         }
1711 }
1712
1713 static GObject *
1714 empathy_main_window_constructor (GType type,
1715                                  guint n_construct_params,
1716                                  GObjectConstructParam *construct_params)
1717 {
1718         static GObject *window = NULL;
1719
1720         if (window != NULL)
1721                 return g_object_ref (window);
1722
1723         window = G_OBJECT_CLASS (empathy_main_window_parent_class)->constructor (
1724                 type, n_construct_params, construct_params);
1725
1726         g_object_add_weak_pointer (window, (gpointer) &window);
1727
1728         return window;
1729 }
1730
1731 static void
1732 empathy_main_window_class_init (EmpathyMainWindowClass *klass)
1733 {
1734         GObjectClass *object_class = G_OBJECT_CLASS (klass);
1735
1736         object_class->finalize = empathy_main_window_finalize;
1737         object_class->constructor = empathy_main_window_constructor;
1738
1739         g_type_class_add_private (object_class, sizeof (EmpathyMainWindowPriv));
1740 }
1741
1742 static void
1743 empathy_main_window_init (EmpathyMainWindow *window)
1744 {
1745         EmpathyMainWindowPriv    *priv;
1746         EmpathyIndividualManager *individual_manager;
1747         GtkBuilder               *gui;
1748         GtkWidget                *sw;
1749         GtkToggleAction          *show_offline_widget;
1750         GtkAction                *show_map_widget;
1751         GtkToolItem              *item;
1752         gboolean                  show_offline;
1753         gchar                    *filename;
1754         GSList                   *l;
1755         GtkTreeModel             *model;
1756
1757         priv = window->priv = G_TYPE_INSTANCE_GET_PRIVATE (window,
1758                         EMPATHY_TYPE_MAIN_WINDOW, EmpathyMainWindowPriv);
1759
1760         priv->gsettings_ui = g_settings_new (EMPATHY_PREFS_UI_SCHEMA);
1761         priv->gsettings_contacts = g_settings_new (EMPATHY_PREFS_CONTACTS_SCHEMA);
1762
1763         priv->sound_mgr = empathy_sound_manager_dup_singleton ();
1764
1765         gtk_window_set_title (GTK_WINDOW (window), _("Contact List"));
1766         gtk_window_set_role (GTK_WINDOW (window), "contact_list");
1767         gtk_window_set_default_size (GTK_WINDOW (window), 225, 325);
1768
1769         /* Set up interface */
1770         filename = empathy_file_lookup ("empathy-main-window.ui", "src");
1771         gui = empathy_builder_get_file (filename,
1772                                        "main_vbox", &priv->main_vbox,
1773                                        "errors_vbox", &priv->errors_vbox,
1774                                        "auth_vbox", &priv->auth_vbox,
1775                                        "ui_manager", &priv->ui_manager,
1776                                        "view_show_offline", &show_offline_widget,
1777                                        "view_show_protocols", &priv->show_protocols,
1778                                        "view_sort_by_name", &priv->sort_by_name,
1779                                        "view_sort_by_status", &priv->sort_by_status,
1780                                        "view_normal_size_with_avatars", &priv->normal_with_avatars,
1781                                        "view_normal_size", &priv->normal_size,
1782                                        "view_compact_size", &priv->compact_size,
1783                                        "view_history", &priv->view_history,
1784                                        "view_show_map", &show_map_widget,
1785                                        "room_join_favorites", &priv->room_join_favorites,
1786                                        "presence_toolbar", &priv->presence_toolbar,
1787                                        "notebook", &priv->notebook,
1788                                        "no_entry_label", &priv->no_entry_label,
1789                                        "roster_scrolledwindow", &sw,
1790                                        NULL);
1791         g_free (filename);
1792
1793         gtk_container_add (GTK_CONTAINER (window), priv->main_vbox);
1794         gtk_widget_show (priv->main_vbox);
1795
1796         g_signal_connect (window, "key-press-event",
1797                           G_CALLBACK (main_window_key_press_event_cb), NULL);
1798
1799         empathy_builder_connect (gui, window,
1800                               "chat_quit", "activate", main_window_chat_quit_cb,
1801                               "chat_new_message", "activate", main_window_chat_new_message_cb,
1802                               "chat_new_call", "activate", main_window_chat_new_call_cb,
1803                               "view_history", "activate", main_window_view_history_cb,
1804                               "room_join_new", "activate", main_window_room_join_new_cb,
1805                               "room_join_favorites", "activate", main_window_room_join_favorites_cb,
1806                               "room_manage_favorites", "activate", main_window_room_manage_favorites_cb,
1807                               "chat_add_contact", "activate", main_window_chat_add_contact_cb,
1808                               "chat_search_contacts", "activate", main_window_chat_search_contacts_cb,
1809                               "view_show_ft_manager", "activate", main_window_view_show_ft_manager,
1810                               "view_show_offline", "toggled", main_window_view_show_offline_cb,
1811                               "view_show_protocols", "toggled", main_window_view_show_protocols_cb,
1812                               "view_sort_by_name", "changed", main_window_view_sort_contacts_cb,
1813                               "view_normal_size_with_avatars", "changed", main_window_view_contacts_list_size_cb,
1814                               "view_show_map", "activate", main_window_view_show_map_cb,
1815                               "edit", "activate", main_window_edit_cb,
1816                               "edit_accounts", "activate", main_window_edit_accounts_cb,
1817                               "edit_personal_information", "activate", main_window_edit_personal_information_cb,
1818                               "edit_blocked_contacts", "activate", main_window_edit_blocked_contacts_cb,
1819                               "edit_preferences", "activate", main_window_edit_preferences_cb,
1820                               "edit_search_contacts", "activate", main_window_edit_search_contacts_cb,
1821                               "help_about", "activate", main_window_help_about_cb,
1822                               "help_debug", "activate", main_window_help_debug_cb,
1823                               "help_contents", "activate", main_window_help_contents_cb,
1824                               NULL);
1825
1826         /* Set up connection related widgets. */
1827         main_window_connection_items_setup (window, gui);
1828
1829         g_object_ref (priv->ui_manager);
1830         g_object_unref (gui);
1831
1832 #ifndef HAVE_LIBCHAMPLAIN
1833         gtk_action_set_visible (show_map_widget, FALSE);
1834 #endif
1835
1836         priv->account_manager = tp_account_manager_dup ();
1837
1838         tp_account_manager_prepare_async (priv->account_manager, NULL,
1839                                           account_manager_prepared_cb, window);
1840
1841         priv->errors = g_hash_table_new_full (g_direct_hash,
1842                                               g_direct_equal,
1843                                               g_object_unref,
1844                                               NULL);
1845
1846         priv->auths = g_hash_table_new (NULL, NULL);
1847
1848         priv->status_changed_handlers = g_hash_table_new_full (g_direct_hash,
1849                                                                g_direct_equal,
1850                                                                NULL,
1851                                                                NULL);
1852
1853         /* Set up menu */
1854         main_window_favorite_chatroom_menu_setup (window);
1855
1856         priv->edit_context = gtk_ui_manager_get_widget (priv->ui_manager,
1857                 "/menubar/edit/edit_context");
1858         priv->edit_context_separator = gtk_ui_manager_get_widget (
1859                 priv->ui_manager,
1860                 "/menubar/edit/edit_context_separator");
1861         gtk_widget_hide (priv->edit_context);
1862         gtk_widget_hide (priv->edit_context_separator);
1863
1864         /* Set up contact list. */
1865         empathy_status_presets_get_all ();
1866
1867         /* Set up presence chooser */
1868         priv->presence_chooser = empathy_presence_chooser_new ();
1869         gtk_widget_show (priv->presence_chooser);
1870         item = gtk_tool_item_new ();
1871         gtk_widget_show (GTK_WIDGET (item));
1872         gtk_widget_set_size_request (priv->presence_chooser, 10, -1);
1873         gtk_container_add (GTK_CONTAINER (item), priv->presence_chooser);
1874         gtk_tool_item_set_is_important (item, TRUE);
1875         gtk_tool_item_set_expand (item, TRUE);
1876         gtk_toolbar_insert (GTK_TOOLBAR (priv->presence_toolbar), item, -1);
1877
1878         /* Set up the throbber */
1879         priv->throbber = gtk_spinner_new ();
1880         gtk_widget_set_size_request (priv->throbber, 16, -1);
1881         gtk_widget_set_tooltip_text (priv->throbber, _("Show and edit accounts"));
1882         gtk_widget_set_events (priv->throbber, GDK_BUTTON_PRESS_MASK);
1883         g_signal_connect (priv->throbber, "button-press-event",
1884                 G_CALLBACK (main_window_throbber_button_press_event_cb),
1885                 window);
1886         gtk_widget_show (priv->throbber);
1887
1888         item = gtk_tool_item_new ();
1889         gtk_container_set_border_width (GTK_CONTAINER (item), 6);
1890         gtk_toolbar_insert (GTK_TOOLBAR (priv->presence_toolbar), item, -1);
1891         gtk_container_add (GTK_CONTAINER (item), priv->throbber);
1892         priv->throbber_tool_item = GTK_WIDGET (item);
1893
1894         /* XXX: this class is designed to live for the duration of the program,
1895          * so it's got a race condition between its signal handlers and its
1896          * finalization. The class is planned to be removed, so we won't fix
1897          * this before then. */
1898         priv->contact_manager = EMPATHY_CONTACT_LIST (
1899                         empathy_contact_manager_dup_singleton ());
1900         individual_manager = empathy_individual_manager_dup_singleton ();
1901         priv->individual_store = empathy_individual_store_new (
1902                         individual_manager);
1903         g_object_unref (individual_manager);
1904
1905         /* For the moment, we disallow Persona drops onto the main contact list (e.g. from things such as
1906          * the EmpathyPersonaView in the linking dialogue). No code is hooked up to do anything on a Persona
1907          * drop, so allowing them would achieve nothing except confusion. */
1908         priv->individual_view = empathy_individual_view_new (
1909                         priv->individual_store,
1910                         EMPATHY_INDIVIDUAL_VIEW_FEATURE_ALL ^ EMPATHY_INDIVIDUAL_VIEW_FEATURE_PERSONA_DROP,
1911                         EMPATHY_INDIVIDUAL_FEATURE_ALL);
1912
1913         priv->butterfly_log_migration_members_changed_id = g_signal_connect (
1914                         priv->contact_manager, "members-changed",
1915                         G_CALLBACK (main_window_members_changed_cb), window);
1916
1917         gtk_widget_show (GTK_WIDGET (priv->individual_view));
1918         gtk_container_add (GTK_CONTAINER (sw),
1919                            GTK_WIDGET (priv->individual_view));
1920         g_signal_connect (priv->individual_view, "row-activated",
1921                           G_CALLBACK (main_window_row_activated_cb),
1922                           window);
1923
1924         /* Set up search bar */
1925         priv->search_bar = empathy_live_search_new (
1926                 GTK_WIDGET (priv->individual_view));
1927         empathy_individual_view_set_live_search (priv->individual_view,
1928                 EMPATHY_LIVE_SEARCH (priv->search_bar));
1929         gtk_box_pack_start (GTK_BOX (priv->main_vbox), priv->search_bar,
1930                 FALSE, TRUE, 0);
1931         g_signal_connect_swapped (window, "map",
1932                 G_CALLBACK (gtk_widget_grab_focus), priv->individual_view);
1933
1934         /* Connect to proper signals to check if contact list is empty or not */
1935         model = gtk_tree_view_get_model (GTK_TREE_VIEW (priv->individual_view));
1936         priv->empty = TRUE;
1937         g_signal_connect (model, "row-inserted",
1938                           G_CALLBACK (main_window_row_inserted_cb),
1939                           window);
1940         g_signal_connect (model, "row-deleted",
1941                           G_CALLBACK (main_window_row_deleted_cb),
1942                           window);
1943
1944         /* Load user-defined accelerators. */
1945         main_window_accels_load ();
1946
1947         /* Set window size. */
1948         empathy_geometry_bind (GTK_WINDOW (window), GEOMETRY_NAME);
1949
1950         /* Enable event handling */
1951         priv->call_observer = empathy_call_observer_dup_singleton ();
1952         priv->event_manager = empathy_event_manager_dup_singleton ();
1953
1954         g_signal_connect (priv->event_manager, "event-added",
1955                           G_CALLBACK (main_window_event_added_cb), window);
1956         g_signal_connect (priv->event_manager, "event-removed",
1957                           G_CALLBACK (main_window_event_removed_cb), window);
1958         g_signal_connect (priv->account_manager, "account-validity-changed",
1959                           G_CALLBACK (main_window_account_validity_changed_cb),
1960                           window);
1961         g_signal_connect (priv->account_manager, "account-removed",
1962                           G_CALLBACK (main_window_account_removed_cb),
1963                           window);
1964         g_signal_connect (priv->account_manager, "account-disabled",
1965                           G_CALLBACK (main_window_account_disabled_cb),
1966                           window);
1967
1968         l = empathy_event_manager_get_events (priv->event_manager);
1969         while (l) {
1970                 main_window_event_added_cb (priv->event_manager, l->data,
1971                                 window);
1972                 l = l->next;
1973         }
1974
1975         /* Show offline ? */
1976         show_offline = g_settings_get_boolean (priv->gsettings_ui,
1977                                                EMPATHY_PREFS_UI_SHOW_OFFLINE);
1978         g_signal_connect (priv->gsettings_ui,
1979                           "changed::" EMPATHY_PREFS_UI_SHOW_OFFLINE,
1980                           G_CALLBACK (main_window_notify_show_offline_cb),
1981                           show_offline_widget);
1982
1983         gtk_toggle_action_set_active (show_offline_widget, show_offline);
1984
1985         /* Show protocol ? */
1986         g_signal_connect (priv->gsettings_ui,
1987                           "changed::" EMPATHY_PREFS_UI_SHOW_PROTOCOLS,
1988                           G_CALLBACK (main_window_notify_show_protocols_cb),
1989                           window);
1990
1991         main_window_notify_show_protocols_cb (priv->gsettings_ui,
1992                                               EMPATHY_PREFS_UI_SHOW_PROTOCOLS,
1993                                               window);
1994
1995         /* Sort by name / by status ? */
1996         g_signal_connect (priv->gsettings_contacts,
1997                           "changed::" EMPATHY_PREFS_CONTACTS_SORT_CRITERIUM,
1998                           G_CALLBACK (main_window_notify_sort_contact_cb),
1999                           window);
2000
2001         main_window_notify_sort_contact_cb (priv->gsettings_contacts,
2002                                             EMPATHY_PREFS_CONTACTS_SORT_CRITERIUM,
2003                                             window);
2004
2005         /* Contacts list size */
2006         g_signal_connect (priv->gsettings_ui,
2007                           "changed::" EMPATHY_PREFS_UI_COMPACT_CONTACT_LIST,
2008                           G_CALLBACK (main_window_notify_contact_list_size_cb),
2009                           window);
2010         g_signal_connect (priv->gsettings_ui,
2011                           "changed::" EMPATHY_PREFS_UI_SHOW_AVATARS,
2012                           G_CALLBACK (main_window_notify_contact_list_size_cb),
2013                           window);
2014
2015         main_window_notify_contact_list_size_cb (priv->gsettings_ui,
2016                                                  EMPATHY_PREFS_UI_SHOW_AVATARS,
2017                                                  window);
2018 }
2019
2020 GtkWidget *
2021 empathy_main_window_dup (void)
2022 {
2023         return g_object_new (EMPATHY_TYPE_MAIN_WINDOW, NULL);
2024 }