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