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