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