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