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