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