]> git.0d.be Git - empathy.git/blob - src/empathy-roster-window.c
7a45df6a48db3dc8d68475c99a83b35dfcb7440b
[empathy.git] / src / empathy-roster-window.c
1 /*
2  * Copyright (C) 2002-2007 Imendio AB
3  * Copyright (C) 2007-2010 Collabora Ltd.
4  *
5  * This program is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU General Public License as
7  * published by the Free Software Foundation; either version 2 of the
8  * License, or (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public
16  * License along with this program; if not, write to the
17  * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
18  * Boston, MA  02110-1301  USA
19  *
20  * Authors: Xavier Claessens <xclaesse@gmail.com>
21  *          Danielle Madeley <danielle.madeley@collabora.co.uk>
22  */
23
24 #include <config.h>
25
26 #include <sys/stat.h>
27 #include <gtk/gtk.h>
28 #include <gdk/gdkkeysyms.h>
29 #include <glib/gi18n.h>
30
31 #include <telepathy-glib/account-manager.h>
32 #include <telepathy-glib/util.h>
33 #include <folks/folks.h>
34
35 #include <libempathy/empathy-contact.h>
36 #include <libempathy/empathy-utils.h>
37 #include <libempathy/empathy-request-util.h>
38 #include <libempathy/empathy-chatroom-manager.h>
39 #include <libempathy/empathy-chatroom.h>
40 #include <libempathy/empathy-gsettings.h>
41 #include <libempathy/empathy-individual-manager.h>
42 #include <libempathy/empathy-gsettings.h>
43 #include <libempathy/empathy-status-presets.h>
44 #include <libempathy/empathy-tp-contact-factory.h>
45
46 #include <libempathy-gtk/empathy-contact-dialogs.h>
47 #include <libempathy-gtk/empathy-live-search.h>
48 #include <libempathy-gtk/empathy-contact-blocking-dialog.h>
49 #include <libempathy-gtk/empathy-contact-search-dialog.h>
50 #include <libempathy-gtk/empathy-geometry.h>
51 #include <libempathy-gtk/empathy-gtk-enum-types.h>
52 #include <libempathy-gtk/empathy-individual-dialogs.h>
53 #include <libempathy-gtk/empathy-individual-store.h>
54 #include <libempathy-gtk/empathy-individual-store-manager.h>
55 #include <libempathy-gtk/empathy-individual-view.h>
56 #include <libempathy-gtk/empathy-new-message-dialog.h>
57 #include <libempathy-gtk/empathy-new-call-dialog.h>
58 #include <libempathy-gtk/empathy-log-window.h>
59 #include <libempathy-gtk/empathy-presence-chooser.h>
60 #include <libempathy-gtk/empathy-sound-manager.h>
61 #include <libempathy-gtk/empathy-ui-utils.h>
62
63 #include "empathy-accounts-dialog.h"
64 #include "empathy-call-observer.h"
65 #include "empathy-chat-manager.h"
66 #include "empathy-roster-window.h"
67 #include "empathy-preferences.h"
68 #include "empathy-about-dialog.h"
69 #include "empathy-debug-window.h"
70 #include "empathy-new-chatroom-dialog.h"
71 #include "empathy-map-view.h"
72 #include "empathy-chatrooms-window.h"
73 #include "empathy-event-manager.h"
74 #include "empathy-ft-manager.h"
75
76 #define DEBUG_FLAG EMPATHY_DEBUG_OTHER
77 #include <libempathy/empathy-debug.h>
78
79 /* Flashing delay for icons (milliseconds). */
80 #define FLASH_TIMEOUT 500
81
82 /* Minimum width of roster window if something goes wrong. */
83 #define MIN_WIDTH 50
84
85 /* Accels (menu shortcuts) can be configured and saved */
86 #define ACCELS_FILENAME "accels.txt"
87
88 /* Name in the geometry file */
89 #define GEOMETRY_NAME "roster-window"
90
91 enum
92 {
93   PAGE_CONTACT_LIST = 0,
94   PAGE_MESSAGE
95 };
96
97 enum
98 {
99   PROP_0,
100   PROP_SHELL_RUNNING
101 };
102
103 G_DEFINE_TYPE (EmpathyRosterWindow, empathy_roster_window, GTK_TYPE_APPLICATION_WINDOW)
104
105 struct _EmpathyRosterWindowPriv {
106   EmpathyIndividualStore *individual_store;
107   EmpathyIndividualView *individual_view;
108   TpAccountManager *account_manager;
109   EmpathyChatroomManager *chatroom_manager;
110   EmpathyEventManager *event_manager;
111   EmpathySoundManager *sound_mgr;
112   EmpathyCallObserver *call_observer;
113   EmpathyIndividualManager *individual_manager;
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   GtkWidget *button_account_settings;
133   GtkWidget *spinner_loading;
134
135   GtkToggleAction *show_protocols;
136   GtkRadioAction *sort_by_name;
137   GtkRadioAction *sort_by_status;
138   GtkRadioAction *normal_with_avatars;
139   GtkRadioAction *normal_size;
140   GtkRadioAction *compact_size;
141
142   GtkUIManager *ui_manager;
143   GMenu *rooms_section;
144   GMenu *balance_section;
145   GAction *view_credit_action;
146   GtkWidget *edit_context;
147   GtkWidget *edit_context_separator;
148
149   GtkActionGroup *balance_action_group;
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   gboolean shell_running;
168
169   /* GAction => GMenuItem */
170   GHashTable *topup_menu_items;
171 };
172
173 static void
174 roster_window_flash_stop (EmpathyRosterWindow *self)
175 {
176   if (self->priv->flash_timeout_id == 0)
177     return;
178
179   DEBUG ("Stop flashing");
180   g_source_remove (self->priv->flash_timeout_id);
181   self->priv->flash_timeout_id = 0;
182   self->priv->flash_on = FALSE;
183 }
184
185 typedef struct
186 {
187   EmpathyEvent *event;
188   gboolean on;
189   EmpathyRosterWindow *self;
190 } FlashForeachData;
191
192 static gboolean
193 roster_window_flash_foreach (GtkTreeModel *model,
194     GtkTreePath *path,
195     GtkTreeIter *iter,
196     gpointer user_data)
197 {
198   FlashForeachData *data = (FlashForeachData *) user_data;
199   FolksIndividual *individual;
200   EmpathyContact *contact;
201   const gchar *icon_name;
202   GtkTreePath *parent_path = NULL;
203   GtkTreeIter parent_iter;
204   GdkPixbuf *pixbuf = NULL;
205
206   gtk_tree_model_get (model, iter,
207           EMPATHY_INDIVIDUAL_STORE_COL_INDIVIDUAL, &individual,
208           -1);
209
210   if (individual == NULL)
211     return FALSE;
212
213   contact = empathy_contact_dup_from_folks_individual (individual);
214   if (contact != data->event->contact)
215     goto out;
216
217   if (data->on)
218     {
219       icon_name = data->event->icon_name;
220       pixbuf = empathy_pixbuf_from_icon_name (icon_name, GTK_ICON_SIZE_MENU);
221     }
222   else
223     {
224       pixbuf = empathy_individual_store_get_individual_status_icon (
225               data->self->priv->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     {
241       parent_path = gtk_tree_model_get_path (model, &parent_iter);
242     }
243
244   if (parent_path != NULL)
245     {
246       gtk_tree_model_row_changed (model, parent_path, &parent_iter);
247       gtk_tree_path_free (parent_path);
248     }
249
250 out:
251   g_object_unref (individual);
252   tp_clear_object (&contact);
253   tp_clear_object (&pixbuf);
254
255   return FALSE;
256 }
257
258 static gboolean
259 roster_window_flash_cb (EmpathyRosterWindow *self)
260 {
261   GtkTreeModel *model;
262   GSList *events, *l;
263   gboolean found_event = FALSE;
264   FlashForeachData  data;
265
266   self->priv->flash_on = !self->priv->flash_on;
267   data.on = self->priv->flash_on;
268   model = GTK_TREE_MODEL (self->priv->individual_store);
269
270   events = empathy_event_manager_get_events (self->priv->event_manager);
271   for (l = events; l; l = l->next)
272     {
273       data.event = l->data;
274       data.self = self;
275       if (!data.event->contact || !data.event->must_ack)
276         continue;
277
278       found_event = TRUE;
279       gtk_tree_model_foreach (model,
280           roster_window_flash_foreach,
281           &data);
282     }
283
284   if (!found_event)
285     roster_window_flash_stop (self);
286
287   return TRUE;
288 }
289
290 static void
291 roster_window_flash_start (EmpathyRosterWindow *self)
292 {
293   if (self->priv->flash_timeout_id != 0)
294     return;
295
296   DEBUG ("Start flashing");
297   self->priv->flash_timeout_id = g_timeout_add (FLASH_TIMEOUT,
298       (GSourceFunc) roster_window_flash_cb, self);
299
300   roster_window_flash_cb (self);
301 }
302
303 static void
304 roster_window_remove_auth (EmpathyRosterWindow *self,
305     EmpathyEvent *event)
306 {
307   GtkWidget *error_widget;
308
309   error_widget = g_hash_table_lookup (self->priv->auths, event);
310   if (error_widget != NULL)
311     {
312       gtk_widget_destroy (error_widget);
313       g_hash_table_remove (self->priv->auths, event);
314     }
315 }
316
317 static void
318 roster_window_auth_add_clicked_cb (GtkButton *button,
319     EmpathyRosterWindow *self)
320 {
321   EmpathyEvent *event;
322
323   event = g_object_get_data (G_OBJECT (button), "event");
324
325   empathy_event_approve (event);
326
327   roster_window_remove_auth (self, event);
328 }
329
330 static void
331 roster_window_auth_close_clicked_cb (GtkButton *button,
332     EmpathyRosterWindow *self)
333 {
334   EmpathyEvent *event;
335
336   event = g_object_get_data (G_OBJECT (button), "event");
337
338   empathy_event_decline (event);
339   roster_window_remove_auth (self, event);
340 }
341
342 static void
343 roster_window_auth_display (EmpathyRosterWindow *self,
344     EmpathyEvent *event)
345 {
346   TpAccount *account = event->account;
347   GtkWidget *info_bar;
348   GtkWidget *content_area;
349   GtkWidget *image;
350   GtkWidget *label;
351   GtkWidget *add_button;
352   GtkWidget *close_button;
353   GtkWidget *action_area;
354   GtkWidget *action_grid;
355   const gchar *icon_name;
356   gchar *str;
357
358   if (g_hash_table_lookup (self->priv->auths, event) != NULL)
359     return;
360
361   info_bar = gtk_info_bar_new ();
362   gtk_info_bar_set_message_type (GTK_INFO_BAR (info_bar), GTK_MESSAGE_QUESTION);
363
364   gtk_widget_set_no_show_all (info_bar, TRUE);
365   gtk_box_pack_start (GTK_BOX (self->priv->auth_vbox), info_bar, FALSE, TRUE, 0);
366   gtk_widget_show (info_bar);
367
368   icon_name = tp_account_get_icon_name (account);
369   image = gtk_image_new_from_icon_name (icon_name, GTK_ICON_SIZE_SMALL_TOOLBAR);
370   gtk_widget_show (image);
371
372   str = g_markup_printf_escaped ("<b>%s</b>\n%s",
373       tp_account_get_display_name (account),
374       _("Password required"));
375
376   label = gtk_label_new (str);
377   gtk_label_set_use_markup (GTK_LABEL (label), TRUE);
378   gtk_label_set_ellipsize (GTK_LABEL (label), PANGO_ELLIPSIZE_END);
379   gtk_misc_set_alignment (GTK_MISC (label), 0, 0.5);
380   gtk_widget_show (label);
381
382   g_free (str);
383
384   content_area = gtk_info_bar_get_content_area (GTK_INFO_BAR (info_bar));
385   gtk_box_pack_start (GTK_BOX (content_area), image, FALSE, FALSE, 0);
386   gtk_box_pack_start (GTK_BOX (content_area), label, TRUE, TRUE, 0);
387
388   image = gtk_image_new_from_stock (GTK_STOCK_ADD, GTK_ICON_SIZE_BUTTON);
389   add_button = gtk_button_new ();
390   gtk_button_set_image (GTK_BUTTON (add_button), image);
391   gtk_widget_set_tooltip_text (add_button, _("Provide Password"));
392   gtk_widget_show (add_button);
393
394   image = gtk_image_new_from_stock (GTK_STOCK_CLOSE, GTK_ICON_SIZE_BUTTON);
395   close_button = gtk_button_new ();
396   gtk_button_set_image (GTK_BUTTON (close_button), image);
397   gtk_widget_set_tooltip_text (close_button, _("Disconnect"));
398   gtk_widget_show (close_button);
399
400   action_grid = gtk_grid_new ();
401   gtk_grid_set_column_spacing (GTK_GRID (action_grid), 6);
402   gtk_widget_show (action_grid);
403
404   action_area = gtk_info_bar_get_action_area (GTK_INFO_BAR (info_bar));
405   gtk_box_pack_start (GTK_BOX (action_area), action_grid, FALSE, FALSE, 0);
406
407   gtk_grid_attach (GTK_GRID (action_grid), add_button, 0, 0, 1, 1);
408   gtk_grid_attach (GTK_GRID (action_grid), close_button, 1, 0, 1, 1);
409
410   g_object_set_data_full (G_OBJECT (info_bar),
411       "event", event, NULL);
412   g_object_set_data_full (G_OBJECT (add_button),
413       "event", event, NULL);
414   g_object_set_data_full (G_OBJECT (close_button),
415       "event", event, NULL);
416
417   g_signal_connect (add_button, "clicked",
418       G_CALLBACK (roster_window_auth_add_clicked_cb), self);
419   g_signal_connect (close_button, "clicked",
420       G_CALLBACK (roster_window_auth_close_clicked_cb), self);
421
422   gtk_widget_show (self->priv->auth_vbox);
423
424   g_hash_table_insert (self->priv->auths, event, info_bar);
425 }
426
427 static void
428 modify_event_count (GtkTreeModel *model,
429     GtkTreeIter *iter,
430     EmpathyEvent *event,
431     gboolean increase)
432 {
433   FolksIndividual *individual;
434   EmpathyContact *contact;
435   guint count;
436
437   gtk_tree_model_get (model, iter,
438       EMPATHY_INDIVIDUAL_STORE_COL_INDIVIDUAL, &individual,
439       EMPATHY_INDIVIDUAL_STORE_COL_EVENT_COUNT, &count,
440       -1);
441
442   if (individual == NULL)
443     return;
444
445   increase ? count++ : count--;
446
447   contact = empathy_contact_dup_from_folks_individual (individual);
448   if (contact == event->contact)
449     gtk_tree_store_set (GTK_TREE_STORE (model), iter,
450         EMPATHY_INDIVIDUAL_STORE_COL_EVENT_COUNT, count, -1);
451
452   tp_clear_object (&contact);
453   g_object_unref (individual);
454 }
455
456 static gboolean
457 increase_event_count_foreach (GtkTreeModel *model,
458     GtkTreePath *path,
459     GtkTreeIter *iter,
460     gpointer user_data)
461 {
462   EmpathyEvent *event = user_data;
463
464   modify_event_count (model, iter, event, TRUE);
465
466   return FALSE;
467 }
468
469 static void
470 increase_event_count (EmpathyRosterWindow *self,
471     EmpathyEvent *event)
472 {
473   GtkTreeModel *model;
474
475   model = GTK_TREE_MODEL (self->priv->individual_store);
476
477   gtk_tree_model_foreach (model, increase_event_count_foreach, event);
478 }
479
480 static gboolean
481 decrease_event_count_foreach (GtkTreeModel *model,
482     GtkTreePath *path,
483     GtkTreeIter *iter,
484     gpointer user_data)
485 {
486   EmpathyEvent *event = user_data;
487
488   modify_event_count (model, iter, event, FALSE);
489
490   return FALSE;
491 }
492
493 static void
494 decrease_event_count (EmpathyRosterWindow *self,
495     EmpathyEvent *event)
496 {
497   GtkTreeModel *model;
498
499   model = GTK_TREE_MODEL (self->priv->individual_store);
500
501   gtk_tree_model_foreach (model, decrease_event_count_foreach, event);
502 }
503
504 static void
505 roster_window_event_added_cb (EmpathyEventManager *manager,
506     EmpathyEvent *event,
507     EmpathyRosterWindow *self)
508 {
509   if (event->contact)
510     {
511       increase_event_count (self, event);
512
513       roster_window_flash_start (self);
514     }
515   else if (event->type == EMPATHY_EVENT_TYPE_AUTH)
516     {
517       roster_window_auth_display (self, event);
518     }
519 }
520
521 static void
522 roster_window_event_removed_cb (EmpathyEventManager *manager,
523     EmpathyEvent *event,
524     EmpathyRosterWindow *self)
525 {
526   FlashForeachData data;
527
528   if (event->type == EMPATHY_EVENT_TYPE_AUTH)
529     {
530       roster_window_remove_auth (self, event);
531       return;
532     }
533
534   if (!event->contact)
535     return;
536
537   decrease_event_count (self, event);
538
539   data.on = FALSE;
540   data.event = event;
541   data.self = self;
542
543   gtk_tree_model_foreach (GTK_TREE_MODEL (self->priv->individual_store),
544       roster_window_flash_foreach,
545       &data);
546 }
547
548 static gboolean
549 roster_window_load_events_idle_cb (gpointer user_data)
550 {
551   EmpathyRosterWindow *self = user_data;
552   GSList *l;
553
554   l = empathy_event_manager_get_events (self->priv->event_manager);
555   while (l != NULL)
556     {
557       roster_window_event_added_cb (self->priv->event_manager, l->data,
558           self);
559       l = l->next;
560     }
561
562   return FALSE;
563 }
564
565 static void
566 roster_window_row_activated_cb (EmpathyIndividualView *view,
567     GtkTreePath *path,
568     GtkTreeViewColumn *col,
569     EmpathyRosterWindow *self)
570 {
571   EmpathyContact *contact = NULL;
572   FolksIndividual *individual;
573   GtkTreeModel *model;
574   GtkTreeIter iter;
575   GSList *events, *l;
576
577   model = gtk_tree_view_get_model (GTK_TREE_VIEW (self->priv->individual_view));
578   gtk_tree_model_get_iter (model, &iter, path);
579
580   gtk_tree_model_get (model, &iter, EMPATHY_INDIVIDUAL_STORE_COL_INDIVIDUAL,
581       &individual, -1);
582
583   if (individual != NULL)
584     contact = empathy_contact_dup_from_folks_individual (individual);
585
586   if (contact == NULL)
587     goto OUT;
588
589   /* If the contact has an event activate it, otherwise the
590    * default handler of row-activated will be called. */
591   events = empathy_event_manager_get_events (self->priv->event_manager);
592   for (l = events; l; l = l->next)
593     {
594       EmpathyEvent *event = l->data;
595
596       if (event->contact == contact)
597         {
598           DEBUG ("Activate event");
599           empathy_event_activate (event);
600
601           /* We don't want the default handler of this signal
602            * (e.g. open a chat) */
603           g_signal_stop_emission_by_name (view, "row-activated");
604           break;
605         }
606     }
607
608   g_object_unref (contact);
609 OUT:
610   tp_clear_object (&individual);
611 }
612
613 static void
614 button_account_settings_clicked_cb (GtkButton *button,
615     EmpathyRosterWindow *self)
616 {
617   empathy_accounts_dialog_show_application (gdk_screen_get_default (),
618       NULL, FALSE, FALSE);
619 }
620
621 static void
622 display_page_message (EmpathyRosterWindow *self,
623     const gchar *msg,
624     gboolean display_accounts_button,
625     gboolean display_spinner)
626 {
627   if (msg != NULL)
628     {
629       gchar *tmp;
630
631       tmp = g_strdup_printf ("<b><span size='xx-large'>%s</span></b>", msg);
632
633       gtk_label_set_markup (GTK_LABEL (self->priv->no_entry_label), tmp);
634       g_free (tmp);
635
636       gtk_label_set_line_wrap (GTK_LABEL (self->priv->no_entry_label), TRUE);
637       gtk_widget_show (self->priv->no_entry_label);
638     }
639   else
640     {
641       gtk_widget_hide (self->priv->no_entry_label);
642     }
643
644   gtk_widget_set_visible (self->priv->button_account_settings,
645       display_accounts_button);
646   gtk_widget_set_visible (self->priv->spinner_loading,
647       display_spinner);
648
649   gtk_notebook_set_current_page (GTK_NOTEBOOK (self->priv->notebook),
650       PAGE_MESSAGE);
651 }
652
653 static void
654 display_page_no_account (EmpathyRosterWindow *self)
655 {
656   display_page_message (self,
657       _("You need to setup an account to see contacts here."), TRUE, FALSE);
658 }
659
660 static void
661 roster_window_row_deleted_cb (GtkTreeModel *model,
662     GtkTreePath *path,
663     EmpathyRosterWindow *self)
664 {
665   GtkTreeIter help_iter;
666
667   if (!gtk_tree_model_get_iter_first (model, &help_iter))
668     {
669       self->priv->empty = TRUE;
670
671       if (empathy_individual_view_is_searching (self->priv->individual_view))
672         {
673           display_page_message (self, _("No match found"), FALSE, FALSE);
674         }
675     }
676 }
677
678 static void
679 display_page_contact_list (EmpathyRosterWindow *self)
680 {
681   if (!empathy_individual_manager_get_contacts_loaded (
682         self->priv->individual_manager))
683     /* We'll display the contact list once we're done loading */
684     return;
685
686   gtk_notebook_set_current_page (GTK_NOTEBOOK (self->priv->notebook),
687       PAGE_CONTACT_LIST);
688 }
689
690 static void
691 roster_window_row_inserted_cb (GtkTreeModel      *model,
692     GtkTreePath *path,
693     GtkTreeIter *iter,
694     EmpathyRosterWindow *self)
695 {
696   if (self->priv->empty)
697     {
698       self->priv->empty = FALSE;
699
700       display_page_contact_list (self);
701       gtk_widget_grab_focus (GTK_WIDGET (self->priv->individual_view));
702
703       /* The store is being filled, it will be done after an idle cb.
704        * So we can then get events. If we do that too soon, event's
705        * contact is not yet in the store and it won't get marked as
706        * having events. */
707       g_idle_add (roster_window_load_events_idle_cb, self);
708     }
709 }
710
711 static void
712 roster_window_remove_error (EmpathyRosterWindow *self,
713     TpAccount *account)
714 {
715   GtkWidget *error_widget;
716
717   error_widget = g_hash_table_lookup (self->priv->errors, account);
718   if (error_widget != NULL)
719     {
720       gtk_widget_destroy (error_widget);
721       g_hash_table_remove (self->priv->errors, account);
722     }
723 }
724
725 static void
726 roster_window_account_disabled_cb (TpAccountManager  *manager,
727     TpAccount *account,
728     EmpathyRosterWindow *self)
729 {
730   roster_window_remove_error (self, account);
731 }
732
733 static void
734 roster_window_error_retry_clicked_cb (GtkButton *button,
735     EmpathyRosterWindow *self)
736 {
737   TpAccount *account;
738
739   account = g_object_get_data (G_OBJECT (button), "account");
740   tp_account_reconnect_async (account, NULL, NULL);
741
742   roster_window_remove_error (self, account);
743 }
744
745 static void
746 roster_window_error_edit_clicked_cb (GtkButton *button,
747     EmpathyRosterWindow *self)
748 {
749   TpAccount *account;
750
751   account = g_object_get_data (G_OBJECT (button), "account");
752
753   empathy_accounts_dialog_show_application (
754       gtk_widget_get_screen (GTK_WIDGET (button)),
755       account, FALSE, FALSE);
756
757   roster_window_remove_error (self, account);
758 }
759
760 static void
761 roster_window_error_close_clicked_cb (GtkButton *button,
762     EmpathyRosterWindow *self)
763 {
764   TpAccount *account;
765
766   account = g_object_get_data (G_OBJECT (button), "account");
767   roster_window_remove_error (self, account);
768 }
769
770 static void
771 roster_window_error_upgrade_sw_clicked_cb (GtkButton *button,
772     EmpathyRosterWindow *self)
773 {
774   TpAccount *account;
775   GtkWidget *dialog;
776
777   account = g_object_get_data (G_OBJECT (button), "account");
778   roster_window_remove_error (self, account);
779
780   dialog = gtk_message_dialog_new (GTK_WINDOW (self),
781       GTK_DIALOG_MODAL, GTK_MESSAGE_ERROR,
782       GTK_BUTTONS_OK,
783       _("Sorry, %s accounts can’t be used until your %s software is updated."),
784       tp_account_get_protocol (account),
785       tp_account_get_protocol (account));
786
787   g_signal_connect_swapped (dialog, "response",
788       G_CALLBACK (gtk_widget_destroy),
789       dialog);
790
791   gtk_widget_show (dialog);
792 }
793
794 static void
795 roster_window_upgrade_software_error (EmpathyRosterWindow *self,
796     TpAccount *account)
797 {
798   GtkWidget *info_bar;
799   GtkWidget *content_area;
800   GtkWidget *label;
801   GtkWidget *image;
802   GtkWidget *upgrade_button;
803   GtkWidget *close_button;
804   GtkWidget *action_area;
805   GtkWidget *action_grid;
806   gchar *str;
807   const gchar *icon_name;
808   const gchar *error_message;
809   gboolean user_requested;
810
811   error_message =
812     empathy_account_get_error_message (account, &user_requested);
813
814   if (user_requested)
815     return;
816
817   str = g_markup_printf_escaped ("<b>%s</b>\n%s",
818       tp_account_get_display_name (account),
819       error_message);
820
821   /* If there are other errors, remove them */
822   roster_window_remove_error (self, account);
823
824   info_bar = gtk_info_bar_new ();
825   gtk_info_bar_set_message_type (GTK_INFO_BAR (info_bar), GTK_MESSAGE_ERROR);
826
827   gtk_widget_set_no_show_all (info_bar, TRUE);
828   gtk_box_pack_start (GTK_BOX (self->priv->errors_vbox), info_bar, FALSE, TRUE, 0);
829   gtk_widget_show (info_bar);
830
831   icon_name = tp_account_get_icon_name (account);
832   image = gtk_image_new_from_icon_name (icon_name, GTK_ICON_SIZE_SMALL_TOOLBAR);
833   gtk_widget_show (image);
834
835   label = gtk_label_new (str);
836   gtk_label_set_use_markup (GTK_LABEL (label), TRUE);
837   gtk_label_set_ellipsize (GTK_LABEL (label), PANGO_ELLIPSIZE_END);
838   gtk_misc_set_alignment (GTK_MISC (label), 0, 0.5);
839   gtk_widget_show (label);
840   g_free (str);
841
842   content_area = gtk_info_bar_get_content_area (GTK_INFO_BAR (info_bar));
843   gtk_box_pack_start (GTK_BOX (content_area), image, FALSE, FALSE, 0);
844   gtk_box_pack_start (GTK_BOX (content_area), label, TRUE, TRUE, 0);
845
846   image = gtk_image_new_from_stock (GTK_STOCK_REFRESH, GTK_ICON_SIZE_BUTTON);
847   upgrade_button = gtk_button_new ();
848   gtk_button_set_image (GTK_BUTTON (upgrade_button), image);
849   gtk_widget_set_tooltip_text (upgrade_button, _("Update software..."));
850   gtk_widget_show (upgrade_button);
851
852   image = gtk_image_new_from_stock (GTK_STOCK_CLOSE, GTK_ICON_SIZE_BUTTON);
853   close_button = gtk_button_new ();
854   gtk_button_set_image (GTK_BUTTON (close_button), image);
855   gtk_widget_set_tooltip_text (close_button, _("Close"));
856   gtk_widget_show (close_button);
857
858   action_grid = gtk_grid_new ();
859   gtk_grid_set_column_spacing (GTK_GRID (action_grid), 2);
860   gtk_widget_show (action_grid);
861
862   action_area = gtk_info_bar_get_action_area (GTK_INFO_BAR (info_bar));
863   gtk_box_pack_start (GTK_BOX (action_area), action_grid, FALSE, FALSE, 0);
864
865   gtk_grid_attach (GTK_GRID (action_grid), upgrade_button, 0, 0, 1, 1);
866   gtk_grid_attach (GTK_GRID (action_grid), close_button, 1, 0, 1, 1);
867
868   g_object_set_data (G_OBJECT (info_bar), "label", label);
869   g_object_set_data_full (G_OBJECT (info_bar),
870       "account", g_object_ref (account),
871       g_object_unref);
872   g_object_set_data_full (G_OBJECT (upgrade_button),
873       "account", g_object_ref (account),
874       g_object_unref);
875   g_object_set_data_full (G_OBJECT (close_button),
876       "account", g_object_ref (account),
877       g_object_unref);
878
879   g_signal_connect (upgrade_button, "clicked",
880       G_CALLBACK (roster_window_error_upgrade_sw_clicked_cb), self);
881   g_signal_connect (close_button, "clicked",
882       G_CALLBACK (roster_window_error_close_clicked_cb), self);
883
884   gtk_widget_set_tooltip_text (self->priv->errors_vbox, error_message);
885   gtk_widget_show (self->priv->errors_vbox);
886
887   g_hash_table_insert (self->priv->errors, g_object_ref (account), info_bar);
888 }
889
890 static void
891 roster_window_error_display (EmpathyRosterWindow *self,
892     TpAccount *account)
893 {
894   GtkWidget *info_bar;
895   GtkWidget *content_area;
896   GtkWidget *label;
897   GtkWidget *image;
898   GtkWidget *retry_button;
899   GtkWidget *edit_button;
900   GtkWidget *close_button;
901   GtkWidget *action_area;
902   GtkWidget *action_grid;
903   gchar *str;
904   const gchar *icon_name;
905   const gchar *error_message;
906   gboolean user_requested;
907
908   if (!tp_strdiff (TP_ERROR_STR_SOFTWARE_UPGRADE_REQUIRED,
909        tp_account_get_detailed_error (account, NULL)))
910     {
911       roster_window_upgrade_software_error (self, account);
912       return;
913     }
914
915   error_message = empathy_account_get_error_message (account, &user_requested);
916
917   if (user_requested)
918     return;
919
920   str = g_markup_printf_escaped ("<b>%s</b>\n%s",
921       tp_account_get_display_name (account), error_message);
922
923   info_bar = g_hash_table_lookup (self->priv->errors, account);
924   if (info_bar)
925     {
926       label = g_object_get_data (G_OBJECT (info_bar), "label");
927
928       /* Just set the latest error and return */
929       gtk_label_set_markup (GTK_LABEL (label), str);
930       g_free (str);
931
932       return;
933     }
934
935   info_bar = gtk_info_bar_new ();
936   gtk_info_bar_set_message_type (GTK_INFO_BAR (info_bar), GTK_MESSAGE_ERROR);
937
938   gtk_widget_set_no_show_all (info_bar, TRUE);
939   gtk_box_pack_start (GTK_BOX (self->priv->errors_vbox), info_bar, FALSE, TRUE, 0);
940   gtk_widget_show (info_bar);
941
942   icon_name = tp_account_get_icon_name (account);
943   image = gtk_image_new_from_icon_name (icon_name, GTK_ICON_SIZE_SMALL_TOOLBAR);
944   gtk_widget_show (image);
945
946   label = gtk_label_new (str);
947   gtk_label_set_use_markup (GTK_LABEL (label), TRUE);
948   gtk_label_set_ellipsize (GTK_LABEL (label), PANGO_ELLIPSIZE_END);
949   gtk_misc_set_alignment (GTK_MISC (label), 0, 0.5);
950   gtk_widget_show (label);
951   g_free (str);
952
953   content_area = gtk_info_bar_get_content_area (GTK_INFO_BAR (info_bar));
954   gtk_box_pack_start (GTK_BOX (content_area), image, FALSE, FALSE, 0);
955   gtk_box_pack_start (GTK_BOX (content_area), label, TRUE, TRUE, 0);
956
957   image = gtk_image_new_from_stock (GTK_STOCK_REFRESH, GTK_ICON_SIZE_BUTTON);
958   retry_button = gtk_button_new ();
959   gtk_button_set_image (GTK_BUTTON (retry_button), image);
960   gtk_widget_set_tooltip_text (retry_button, _("Reconnect"));
961   gtk_widget_show (retry_button);
962
963   image = gtk_image_new_from_stock (GTK_STOCK_EDIT, GTK_ICON_SIZE_BUTTON);
964   edit_button = gtk_button_new ();
965   gtk_button_set_image (GTK_BUTTON (edit_button), image);
966   gtk_widget_set_tooltip_text (edit_button, _("Edit Account"));
967   gtk_widget_show (edit_button);
968
969   image = gtk_image_new_from_stock (GTK_STOCK_CLOSE, GTK_ICON_SIZE_BUTTON);
970   close_button = gtk_button_new ();
971   gtk_button_set_image (GTK_BUTTON (close_button), image);
972   gtk_widget_set_tooltip_text (close_button, _("Close"));
973   gtk_widget_show (close_button);
974
975   action_grid = gtk_grid_new ();
976   gtk_grid_set_column_spacing (GTK_GRID (action_grid), 2);
977   gtk_widget_show (action_grid);
978
979   action_area = gtk_info_bar_get_action_area (GTK_INFO_BAR (info_bar));
980   gtk_box_pack_start (GTK_BOX (action_area), action_grid, FALSE, FALSE, 0);
981
982   gtk_grid_attach (GTK_GRID (action_grid), retry_button, 0, 0, 1, 1);
983   gtk_grid_attach (GTK_GRID (action_grid), edit_button, 1, 0, 1, 1);
984   gtk_grid_attach (GTK_GRID (action_grid), close_button, 2, 0, 1, 1);
985
986   g_object_set_data (G_OBJECT (info_bar), "label", label);
987   g_object_set_data_full (G_OBJECT (info_bar),
988       "account", g_object_ref (account),
989       g_object_unref);
990   g_object_set_data_full (G_OBJECT (edit_button),
991       "account", g_object_ref (account),
992       g_object_unref);
993   g_object_set_data_full (G_OBJECT (close_button),
994       "account", g_object_ref (account),
995       g_object_unref);
996   g_object_set_data_full (G_OBJECT (retry_button),
997       "account", g_object_ref (account),
998       g_object_unref);
999
1000   g_signal_connect (edit_button, "clicked",
1001       G_CALLBACK (roster_window_error_edit_clicked_cb), self);
1002   g_signal_connect (close_button, "clicked",
1003       G_CALLBACK (roster_window_error_close_clicked_cb), self);
1004   g_signal_connect (retry_button, "clicked",
1005       G_CALLBACK (roster_window_error_retry_clicked_cb), self);
1006
1007   gtk_widget_set_tooltip_text (self->priv->errors_vbox, error_message);
1008   gtk_widget_show (self->priv->errors_vbox);
1009
1010   g_hash_table_insert (self->priv->errors, g_object_ref (account), info_bar);
1011 }
1012
1013 static void
1014 roster_window_update_status (EmpathyRosterWindow *self)
1015 {
1016   gboolean connected, connecting;
1017   GList *l;
1018
1019   connected = empathy_account_manager_get_accounts_connected (&connecting);
1020
1021   /* Update the spinner state */
1022   if (connecting)
1023     {
1024       gtk_spinner_start (GTK_SPINNER (self->priv->throbber));
1025       gtk_widget_show (self->priv->throbber_tool_item);
1026     }
1027   else
1028     {
1029       gtk_spinner_stop (GTK_SPINNER (self->priv->throbber));
1030       gtk_widget_hide (self->priv->throbber_tool_item);
1031     }
1032
1033   /* Update widgets sensibility */
1034   for (l = self->priv->actions_connected; l; l = l->next)
1035     g_simple_action_set_enabled (l->data, connected);
1036 }
1037
1038 static char *
1039 account_to_topup_action_name (TpAccount *account,
1040     gboolean prefix)
1041 {
1042   char *r;
1043   gchar *result;
1044
1045   /* action names can't have '/' in them, replace it with '.' */
1046   r = g_strdup (tp_account_get_path_suffix (account));
1047   r = g_strdelimit (r, "/", '.');
1048
1049   result = g_strconcat (prefix ? "win." : "", "topup-", r, NULL);
1050   g_free (r);
1051
1052   return result;
1053 }
1054
1055 static void
1056 roster_window_balance_activate_cb (GtkAction *action,
1057     EmpathyRosterWindow *self)
1058 {
1059   const char *uri;
1060
1061   uri = g_object_get_data (G_OBJECT (action), "manage-credit-uri");
1062
1063   if (!tp_str_empty (uri))
1064     {
1065       DEBUG ("Top-up credit URI: %s", uri);
1066       empathy_url_show (GTK_WIDGET (self), uri);
1067     }
1068   else
1069     {
1070       DEBUG ("unknown protocol for top-up");
1071     }
1072 }
1073
1074 static void
1075 roster_window_balance_update_balance (EmpathyRosterWindow *self,
1076     TpAccount *account)
1077 {
1078   TpConnection *conn;
1079   GAction *action;
1080   GMenuItem *item;
1081   GtkWidget *label;
1082   int amount = 0;
1083   guint scale = G_MAXINT32;
1084   const gchar *currency = "";
1085   char *money;
1086   gchar *name;
1087
1088   g_print ("111111111\n");
1089   conn = tp_account_get_connection (account);
1090   if (conn == NULL)
1091     return;
1092   g_print ("2222222222\n");
1093
1094   name = account_to_topup_action_name (account, FALSE);
1095
1096   action = g_action_map_lookup_action (G_ACTION_MAP (self), name);
1097   g_free (name);
1098
1099   if (action == NULL)
1100     return;
1101   g_print ("333333333\n");
1102
1103   if (!tp_connection_get_balance (conn, &amount, &scale, &currency))
1104     return;
1105   g_print ("44444444444\n");
1106
1107   if (amount == 0 &&
1108       scale == G_MAXINT32 &&
1109       tp_str_empty (currency))
1110     {
1111       /* unknown balance */
1112       money = g_strdup ("--");
1113     }
1114   else
1115     {
1116       char *tmp = empathy_format_currency (amount, scale, currency);
1117
1118       money = g_strdup_printf ("%s %s", currency, tmp);
1119       g_free (tmp);
1120     }
1121
1122   item = g_hash_table_lookup (self->priv->topup_menu_items, action);
1123   g_print ("5555555 %p\n", item);
1124   if (item != NULL)
1125     {
1126       gchar *str;
1127
1128       /* Translators: this string will be something like:
1129        * Top up My Account ($1.23)..." */
1130       str = g_strdup_printf (_("Top up %s (%s)..."),
1131         tp_account_get_display_name (account), money);
1132
1133       g_print ("SET %s\n", str);
1134       g_menu_item_set_label (item, str);
1135       g_free (str);
1136     }
1137
1138   /* update the money label in the roster */
1139   label = g_object_get_data (G_OBJECT (action), "money-label");
1140
1141   gtk_label_set_text (GTK_LABEL (label), money);
1142   g_free (money);
1143 }
1144
1145 static void
1146 roster_window_balance_changed_cb (TpConnection *conn,
1147     guint balance,
1148     guint scale,
1149     const gchar *currency,
1150     EmpathyRosterWindow *self)
1151 {
1152   TpAccount *account;
1153
1154   account = tp_connection_get_account (conn);
1155   if (account == NULL)
1156     return;
1157
1158   roster_window_balance_update_balance (self, account);
1159 }
1160
1161 static GAction *
1162 roster_window_setup_balance_create_action (EmpathyRosterWindow *self,
1163     TpAccount *account)
1164 {
1165   GAction *action;
1166   gchar *name;
1167   GMenuItem *item;
1168
1169   /* create the action */
1170   name = account_to_topup_action_name (account, FALSE);
1171
1172   action = (GAction *) g_simple_action_new (name, NULL);
1173
1174   g_action_map_add_action (G_ACTION_MAP (self), action);
1175   g_free (name);
1176
1177   /* Add to the menu */
1178   name = account_to_topup_action_name (account, TRUE);
1179
1180   item = g_menu_item_new (_("Top up account credit"), name);
1181   g_print ("CREATE %p\n", item);
1182
1183   g_menu_append_item (self->priv->balance_section, item);
1184   g_menu_item_set_label (item, "mushroom");
1185
1186   /* Pass ownership of action to the hash table */
1187   g_hash_table_insert (self->priv->topup_menu_items,
1188       action, g_object_ref (item));
1189
1190   if (0) {
1191   g_object_bind_property (account, "icon-name", action, "icon-name",
1192       G_BINDING_SYNC_CREATE);
1193
1194   g_signal_connect (action, "activate",
1195       G_CALLBACK (roster_window_balance_activate_cb), self);
1196   }
1197
1198   g_free (name);
1199
1200   return action;
1201 }
1202
1203 static GtkWidget *
1204 roster_window_setup_balance_create_widget (EmpathyRosterWindow *self,
1205     GAction *action,
1206     TpAccount *account)
1207 {
1208 #if 0
1209   GtkWidget *hbox, *image, *label, *button;
1210
1211   hbox = gtk_box_new (GTK_ORIENTATION_HORIZONTAL, 6);
1212
1213   /* protocol icon */
1214   image = gtk_image_new ();
1215   gtk_box_pack_start (GTK_BOX (hbox), image, FALSE, TRUE, 0);
1216   g_object_bind_property (action, "icon-name", image, "icon-name",
1217       G_BINDING_SYNC_CREATE);
1218
1219   /* account name label */
1220   label = gtk_label_new ("");
1221   gtk_misc_set_alignment (GTK_MISC (label), 0.0, 0.5);
1222   gtk_box_pack_start (GTK_BOX (hbox), label, TRUE, TRUE, 0);
1223   g_object_bind_property (account, "display-name", label, "label",
1224       G_BINDING_SYNC_CREATE);
1225
1226   /* balance label */
1227   label = gtk_label_new ("");
1228   gtk_misc_set_alignment (GTK_MISC (label), 0.0, 0.5);
1229   gtk_box_pack_start (GTK_BOX (hbox), label, FALSE, TRUE, 0);
1230   g_object_set_data (G_OBJECT (action), "money-label", label);
1231
1232   /* top up button */
1233   button = gtk_button_new_with_label (_("Top Up..."));
1234   gtk_box_pack_start (GTK_BOX (hbox), button, FALSE, TRUE, 0);
1235   g_signal_connect_swapped (button, "clicked",
1236       G_CALLBACK (gtk_action_activate), action);
1237
1238   gtk_box_pack_start (GTK_BOX (self->priv->balance_vbox), hbox, FALSE, TRUE, 0);
1239   gtk_widget_show_all (hbox);
1240
1241   /* tie the lifetime of the widget to the lifetime of the action */
1242   g_object_weak_ref (G_OBJECT (action),
1243       (GWeakNotify) gtk_widget_destroy, hbox);
1244
1245   return hbox;
1246 #endif
1247   return NULL;
1248 }
1249
1250 #define ACTION_SHOW_CREDIT_BALANCE "show-credit-balance"
1251
1252 static void
1253 add_view_credit_balance_item (EmpathyRosterWindow *self)
1254 {
1255   GMenuItem *item;
1256
1257   if (self->priv->view_credit_action != NULL)
1258     return;
1259
1260   self->priv->view_credit_action = (GAction *) g_simple_action_new (
1261       ACTION_SHOW_CREDIT_BALANCE, NULL);
1262
1263   /* TODO: connect sig */
1264   /* bind view_balance_show_in_roster */
1265   /*
1266   g_settings_bind (self->priv->gsettings_ui, "show-balance-in-roster",
1267       self->priv->view_balance_show_in_roster, "active",
1268       G_SETTINGS_BIND_DEFAULT);
1269   g_object_bind_property (self->priv->view_balance_show_in_roster, "active",
1270       self->priv->balance_vbox, "visible",
1271       G_BINDING_SYNC_CREATE);
1272       */
1273
1274   g_action_map_add_action (G_ACTION_MAP (self), self->priv->view_credit_action);
1275
1276   item = g_menu_item_new (_("Credit Balance"),
1277       "win." ACTION_SHOW_CREDIT_BALANCE);
1278
1279   g_menu_append_item (self->priv->balance_section, item);
1280 }
1281
1282 static void
1283 roster_window_setup_balance (EmpathyRosterWindow *self,
1284     TpAccount *account)
1285 {
1286   TpConnection *conn = tp_account_get_connection (account);
1287   GAction *action;
1288   const gchar *uri;
1289
1290   if (conn == NULL)
1291     return;
1292
1293   if (!tp_proxy_is_prepared (conn, TP_CONNECTION_FEATURE_BALANCE))
1294     return;
1295
1296   DEBUG ("Setting up balance for acct: %s",
1297       tp_account_get_display_name (account));
1298
1299   add_view_credit_balance_item (self);
1300
1301   /* create the action */
1302   action = roster_window_setup_balance_create_action (self, account);
1303
1304   if (action == NULL)
1305     return;
1306
1307   /* create the display widget */
1308   roster_window_setup_balance_create_widget (self, action, account);
1309
1310   /* check the current balance and monitor for any changes */
1311   uri = tp_connection_get_balance_uri (conn);
1312
1313   g_object_set_data_full (G_OBJECT (action), "manage-credit-uri",
1314       g_strdup (uri), g_free);
1315   gtk_action_set_sensitive (GTK_ACTION (action), !tp_str_empty (uri));
1316
1317   roster_window_balance_update_balance (self, account);
1318
1319   g_signal_connect (conn, "balance-changed",
1320       G_CALLBACK (roster_window_balance_changed_cb), self);
1321 }
1322
1323 static void
1324 remove_view_credit_balance_item (EmpathyRosterWindow *self)
1325 {
1326   gint i;
1327
1328   if (self->priv->view_credit_action == NULL)
1329     return;
1330
1331   for (i = 0; i < g_menu_model_get_n_items (
1332         G_MENU_MODEL (self->priv->balance_section)); i++)
1333     {
1334       const gchar *action;
1335
1336       if (g_menu_model_get_item_attribute (
1337             G_MENU_MODEL (self->priv->balance_section), i,
1338             G_MENU_ATTRIBUTE_ACTION, "s", &action))
1339         {
1340           if (!tp_strdiff (action, "win." ACTION_SHOW_CREDIT_BALANCE))
1341             {
1342               g_menu_remove (self->priv->balance_section, i);
1343               break;
1344             }
1345         }
1346     }
1347
1348   g_action_map_remove_action (G_ACTION_MAP (self),
1349       g_action_get_name (self->priv->view_credit_action));
1350
1351   g_clear_object (&self->priv->view_credit_action);
1352 }
1353
1354 static void
1355 roster_window_remove_balance_action (EmpathyRosterWindow *self,
1356     TpAccount *account)
1357 {
1358   /* Remove the "Credit Balance" entry if we just removed the last account
1359    * supporting balance and so that's the last entry in the section. */
1360   if (g_menu_model_get_n_items (G_MENU_MODEL (self->priv->balance_section)) <= 1)
1361     remove_view_credit_balance_item (self);
1362 #if 0
1363   GtkAction *action;
1364   char *name;
1365   GList *a;
1366
1367   name = roster_window_account_to_action_name (account);
1368
1369   action = gtk_action_group_get_action (
1370       self->priv->balance_action_group, name);
1371
1372   if (action != NULL)
1373     {
1374       guint merge_id;
1375
1376       DEBUG ("Removing action");
1377
1378       merge_id = GPOINTER_TO_UINT (g_object_get_data (G_OBJECT (action),
1379             "merge-id"));
1380
1381       gtk_ui_manager_remove_ui (self->priv->ui_manager,
1382           merge_id);
1383       gtk_action_group_remove_action (
1384           self->priv->balance_action_group, action);
1385     }
1386
1387   g_free (name);
1388
1389   a = gtk_action_group_list_actions (
1390       self->priv->balance_action_group);
1391
1392   gtk_action_set_visible (self->priv->view_balance_show_in_roster,
1393       g_list_length (a) > 0);
1394
1395   g_list_free (a);
1396 #endif
1397 }
1398
1399 static void
1400 roster_window_connection_changed_cb (TpAccount  *account,
1401     guint old_status,
1402     guint current,
1403     guint reason,
1404     gchar *dbus_error_name,
1405     GHashTable *details,
1406     EmpathyRosterWindow *self)
1407 {
1408   roster_window_update_status (self);
1409
1410   if (current == TP_CONNECTION_STATUS_DISCONNECTED &&
1411       reason != TP_CONNECTION_STATUS_REASON_REQUESTED)
1412     {
1413       roster_window_error_display (self, account);
1414     }
1415
1416   if (current == TP_CONNECTION_STATUS_DISCONNECTED)
1417     {
1418       empathy_sound_manager_play (self->priv->sound_mgr, GTK_WIDGET (self),
1419               EMPATHY_SOUND_ACCOUNT_DISCONNECTED);
1420     }
1421
1422   if (current == TP_CONNECTION_STATUS_CONNECTED)
1423     {
1424       empathy_sound_manager_play (self->priv->sound_mgr, GTK_WIDGET (self),
1425               EMPATHY_SOUND_ACCOUNT_CONNECTED);
1426
1427       /* Account connected without error, remove error message if any */
1428       roster_window_remove_error (self, account);
1429     }
1430 }
1431
1432 static void
1433 roster_window_accels_load (void)
1434 {
1435   gchar *filename;
1436
1437   filename = g_build_filename (g_get_user_config_dir (),
1438       PACKAGE_NAME, ACCELS_FILENAME, NULL);
1439   if (g_file_test (filename, G_FILE_TEST_EXISTS))
1440     {
1441       DEBUG ("Loading from:'%s'", filename);
1442       gtk_accel_map_load (filename);
1443     }
1444
1445   g_free (filename);
1446 }
1447
1448 static void
1449 roster_window_accels_save (void)
1450 {
1451   gchar *dir;
1452   gchar *file_with_path;
1453
1454   dir = g_build_filename (g_get_user_config_dir (), PACKAGE_NAME, NULL);
1455   g_mkdir_with_parents (dir, S_IRUSR | S_IWUSR | S_IXUSR);
1456   file_with_path = g_build_filename (dir, ACCELS_FILENAME, NULL);
1457   g_free (dir);
1458
1459   DEBUG ("Saving to:'%s'", file_with_path);
1460   gtk_accel_map_save (file_with_path);
1461
1462   g_free (file_with_path);
1463 }
1464
1465 static void
1466 empathy_roster_window_finalize (GObject *window)
1467 {
1468   EmpathyRosterWindow *self = EMPATHY_ROSTER_WINDOW (window);
1469   GHashTableIter iter;
1470   gpointer key, value;
1471
1472   /* Save user-defined accelerators. */
1473   roster_window_accels_save ();
1474
1475   g_list_free (self->priv->actions_connected);
1476
1477   g_object_unref (self->priv->account_manager);
1478   g_object_unref (self->priv->individual_store);
1479   g_object_unref (self->priv->sound_mgr);
1480   g_hash_table_unref (self->priv->errors);
1481   g_hash_table_unref (self->priv->auths);
1482
1483   /* disconnect all handlers of status-changed signal */
1484   g_hash_table_iter_init (&iter, self->priv->status_changed_handlers);
1485   while (g_hash_table_iter_next (&iter, &key, &value))
1486     g_signal_handler_disconnect (TP_ACCOUNT (key), GPOINTER_TO_UINT (value));
1487
1488   g_hash_table_unref (self->priv->status_changed_handlers);
1489
1490   g_signal_handlers_disconnect_by_func (self->priv->event_manager,
1491       roster_window_event_added_cb, self);
1492   g_signal_handlers_disconnect_by_func (self->priv->event_manager,
1493       roster_window_event_removed_cb, self);
1494
1495   g_object_unref (self->priv->call_observer);
1496   g_object_unref (self->priv->event_manager);
1497   g_object_unref (self->priv->ui_manager);
1498   g_object_unref (self->priv->chatroom_manager);
1499
1500   g_object_unref (self->priv->gsettings_ui);
1501   g_object_unref (self->priv->gsettings_contacts);
1502   g_object_unref (self->priv->individual_manager);
1503
1504   g_object_unref (self->priv->rooms_section);
1505   g_object_unref (self->priv->balance_section);
1506   g_clear_object (&self->priv->view_credit_action);
1507   g_hash_table_unref (self->priv->topup_menu_items);
1508
1509   G_OBJECT_CLASS (empathy_roster_window_parent_class)->finalize (window);
1510 }
1511
1512 static gboolean
1513 roster_window_key_press_event_cb  (GtkWidget *window,
1514     GdkEventKey *event,
1515     gpointer user_data)
1516 {
1517   if (event->keyval == GDK_KEY_T
1518       && event->state & GDK_SHIFT_MASK
1519       && event->state & GDK_CONTROL_MASK)
1520     empathy_chat_manager_call_undo_closed_chat ();
1521
1522   return FALSE;
1523 }
1524
1525 static void
1526 roster_window_chat_quit_cb (GSimpleAction *action,
1527     GVariant *parameter,
1528     gpointer user_data)
1529 {
1530   EmpathyRosterWindow *self = user_data;
1531
1532   gtk_widget_destroy (GTK_WIDGET (self));
1533 }
1534
1535 static void
1536 roster_window_view_history_cb (GSimpleAction *action,
1537     GVariant *parameter,
1538     gpointer user_data)
1539 {
1540   EmpathyRosterWindow *self = user_data;
1541
1542   empathy_log_window_show (NULL, NULL, FALSE, GTK_WINDOW (self));
1543 }
1544
1545 static void
1546 roster_window_chat_new_message_cb (GSimpleAction *action,
1547     GVariant *parameter,
1548     gpointer user_data)
1549 {
1550   EmpathyRosterWindow *self = user_data;
1551
1552   empathy_new_message_dialog_show (GTK_WINDOW (self));
1553 }
1554
1555 static void
1556 roster_window_chat_new_call_cb (GSimpleAction *action,
1557     GVariant *parameter,
1558     gpointer user_data)
1559 {
1560   EmpathyRosterWindow *self = user_data;
1561
1562   empathy_new_call_dialog_show (GTK_WINDOW (self));
1563 }
1564
1565 static void
1566 roster_window_chat_add_contact_cb (GSimpleAction *action,
1567     GVariant *parameter,
1568     gpointer user_data)
1569 {
1570   EmpathyRosterWindow *self = user_data;
1571
1572   empathy_new_individual_dialog_show (GTK_WINDOW (self));
1573 }
1574
1575 static void
1576 roster_window_chat_search_contacts_cb (GSimpleAction *action,
1577     GVariant *parameter,
1578     gpointer user_data)
1579 {
1580   EmpathyRosterWindow *self = user_data;
1581   GtkWidget *dialog;
1582
1583   dialog = empathy_contact_search_dialog_new (
1584       GTK_WINDOW (self));
1585
1586   gtk_widget_show (dialog);
1587 }
1588
1589 static void
1590 roster_window_view_show_ft_manager (GSimpleAction *action,
1591     GVariant *parameter,
1592     gpointer user_data)
1593 {
1594   empathy_ft_manager_show ();
1595 }
1596
1597 static void
1598 activate_toggle (GSimpleAction *action,
1599     GVariant *parameter,
1600     gpointer user_data)
1601 {
1602   GVariant *state;
1603
1604   state = g_action_get_state (G_ACTION (action));
1605   g_action_change_state (G_ACTION (action),
1606       g_variant_new_boolean (!g_variant_get_boolean (state)));
1607   g_variant_unref (state);
1608 }
1609
1610 static void
1611 roster_window_view_show_offline_cb (GSimpleAction *action,
1612     GVariant *state,
1613     gpointer user_data)
1614 {
1615   EmpathyRosterWindow *self = user_data;
1616   gboolean current;
1617
1618   current = g_variant_get_boolean (state);
1619
1620   g_settings_set_boolean (self->priv->gsettings_ui,
1621       EMPATHY_PREFS_UI_SHOW_OFFLINE,
1622       current);
1623
1624   empathy_individual_view_set_show_offline (self->priv->individual_view,
1625       current);
1626
1627   g_simple_action_set_state (action, state);
1628 }
1629
1630 #if 0
1631 static void
1632 roster_window_notify_sort_contact_cb (GSettings         *gsettings,
1633     const gchar *key,
1634     EmpathyRosterWindow *self)
1635 {
1636   gchar *str;
1637
1638   str = g_settings_get_string (gsettings, key);
1639
1640   if (str != NULL)
1641     {
1642       GType       type;
1643       GEnumClass *enum_class;
1644       GEnumValue *enum_value;
1645
1646       type = empathy_individual_store_sort_get_type ();
1647       enum_class = G_ENUM_CLASS (g_type_class_peek (type));
1648       enum_value = g_enum_get_value_by_nick (enum_class, str);
1649       if (enum_value)
1650         {
1651           /* By changing the value of the GtkRadioAction,
1652              it emits a signal that calls roster_window_view_sort_contacts_cb
1653              which updates the contacts list */
1654           gtk_radio_action_set_current_value (self->priv->sort_by_name,
1655                       enum_value->value);
1656         }
1657       else
1658         {
1659           g_warning ("Wrong value for sort_criterium configuration : %s", str);
1660         }
1661     g_free (str);
1662   }
1663 }
1664 #endif
1665
1666 #if 0
1667 static void
1668 roster_window_view_sort_contacts_cb (GtkRadioAction *action,
1669     GtkRadioAction *current,
1670     EmpathyRosterWindow *self)
1671 {
1672   EmpathyIndividualStoreSort value;
1673   GSList *group;
1674   GType type;
1675   GEnumClass *enum_class;
1676   GEnumValue *enum_value;
1677
1678   value = gtk_radio_action_get_current_value (action);
1679   group = gtk_radio_action_get_group (action);
1680
1681   /* Get string from index */
1682   type = empathy_individual_store_sort_get_type ();
1683   enum_class = G_ENUM_CLASS (g_type_class_peek (type));
1684   enum_value = g_enum_get_value (enum_class, g_slist_index (group, current));
1685
1686   if (!enum_value)
1687     g_warning ("No GEnumValue for EmpathyContactListSort with GtkRadioAction index:%d",
1688         g_slist_index (group, action));
1689   else
1690     g_settings_set_string (self->priv->gsettings_contacts,
1691         EMPATHY_PREFS_CONTACTS_SORT_CRITERIUM,
1692         enum_value->value_nick);
1693
1694   empathy_individual_store_set_sort_criterium (self->priv->individual_store,
1695       value);
1696 }
1697
1698 static void
1699 roster_window_view_show_protocols_cb (GtkToggleAction *action,
1700     EmpathyRosterWindow *self)
1701 {
1702   gboolean value;
1703
1704   value = gtk_toggle_action_get_active (action);
1705
1706   g_settings_set_boolean (self->priv->gsettings_ui,
1707       EMPATHY_PREFS_UI_SHOW_PROTOCOLS,
1708       value);
1709
1710   empathy_individual_store_set_show_protocols (self->priv->individual_store,
1711       value);
1712 }
1713 #endif
1714
1715 /* Matches GtkRadioAction values set in empathy-roster-window.ui */
1716 #define CONTACT_LIST_NORMAL_SIZE_WITH_AVATARS   0
1717 #define CONTACT_LIST_NORMAL_SIZE      1
1718 #define CONTACT_LIST_COMPACT_SIZE     2
1719
1720 #if 0
1721 static void
1722 roster_window_view_contacts_list_size_cb (GtkRadioAction *action,
1723     GtkRadioAction *current,
1724     EmpathyRosterWindow *self)
1725 {
1726   GSettings *gsettings_ui;
1727   gint value;
1728
1729   value = gtk_radio_action_get_current_value (action);
1730   /* create a new GSettings, so we can delay the setting until both
1731    * values are set */
1732   gsettings_ui = g_settings_new (EMPATHY_PREFS_UI_SCHEMA);
1733
1734   DEBUG ("radio button toggled, value = %i", value);
1735
1736   g_settings_delay (gsettings_ui);
1737   g_settings_set_boolean (gsettings_ui, EMPATHY_PREFS_UI_SHOW_AVATARS,
1738       value == CONTACT_LIST_NORMAL_SIZE_WITH_AVATARS);
1739
1740   g_settings_set_boolean (gsettings_ui, EMPATHY_PREFS_UI_COMPACT_CONTACT_LIST,
1741       value == CONTACT_LIST_COMPACT_SIZE);
1742   g_settings_apply (gsettings_ui);
1743
1744   /* FIXME: these enums probably have the wrong namespace */
1745   empathy_individual_store_set_show_avatars (self->priv->individual_store,
1746       value == CONTACT_LIST_NORMAL_SIZE_WITH_AVATARS);
1747   empathy_individual_store_set_is_compact (self->priv->individual_store,
1748       value == CONTACT_LIST_COMPACT_SIZE);
1749
1750   g_object_unref (gsettings_ui);
1751 }
1752
1753 static void roster_window_notify_show_protocols_cb (GSettings *gsettings,
1754     const gchar *key,
1755     EmpathyRosterWindow *self)
1756 {
1757   gtk_toggle_action_set_active (self->priv->show_protocols,
1758       g_settings_get_boolean (gsettings, EMPATHY_PREFS_UI_SHOW_PROTOCOLS));
1759 }
1760
1761
1762 static void
1763 roster_window_notify_contact_list_size_cb (GSettings *gsettings,
1764     const gchar *key,
1765     EmpathyRosterWindow *self)
1766 {
1767   gint value;
1768
1769   if (g_settings_get_boolean (gsettings,
1770       EMPATHY_PREFS_UI_COMPACT_CONTACT_LIST))
1771     value = CONTACT_LIST_COMPACT_SIZE;
1772   else if (g_settings_get_boolean (gsettings,
1773       EMPATHY_PREFS_UI_SHOW_AVATARS))
1774     value = CONTACT_LIST_NORMAL_SIZE_WITH_AVATARS;
1775   else
1776     value = CONTACT_LIST_NORMAL_SIZE;
1777
1778   DEBUG ("setting changed, value = %i", value);
1779
1780   /* By changing the value of the GtkRadioAction,
1781      it emits a signal that calls roster_window_view_contacts_list_size_cb
1782      which updates the contacts list */
1783   gtk_radio_action_set_current_value (self->priv->normal_with_avatars, value);
1784 }
1785 #endif
1786
1787 static void
1788 roster_window_edit_search_contacts_cb (GSimpleAction *action,
1789     GVariant *parameter,
1790     gpointer user_data)
1791 {
1792   EmpathyRosterWindow *self = user_data;
1793
1794   empathy_individual_view_start_search (self->priv->individual_view);
1795 }
1796
1797 static void
1798 roster_window_view_show_map_cb (GSimpleAction *action,
1799     GVariant *parameter,
1800     gpointer user_data)
1801 {
1802 #ifdef HAVE_LIBCHAMPLAIN
1803   empathy_map_view_show ();
1804 #endif
1805 }
1806
1807 static void
1808 join_chatroom (EmpathyChatroom *chatroom,
1809     gint64 timestamp)
1810 {
1811   TpAccount *account;
1812   const gchar *room;
1813
1814   account = empathy_chatroom_get_account (chatroom);
1815   room = empathy_chatroom_get_room (chatroom);
1816
1817   DEBUG ("Requesting channel for '%s'", room);
1818   empathy_join_muc (account, room, timestamp);
1819 }
1820
1821 typedef struct
1822 {
1823   TpAccount *account;
1824   EmpathyChatroom *chatroom;
1825   gint64 timestamp;
1826   glong sig_id;
1827   guint timeout;
1828 } join_fav_account_sig_ctx;
1829
1830 static join_fav_account_sig_ctx *
1831 join_fav_account_sig_ctx_new (TpAccount *account,
1832     EmpathyChatroom *chatroom,
1833     gint64 timestamp)
1834 {
1835   join_fav_account_sig_ctx *ctx = g_slice_new0 (
1836       join_fav_account_sig_ctx);
1837
1838   ctx->account = g_object_ref (account);
1839   ctx->chatroom = g_object_ref (chatroom);
1840   ctx->timestamp = timestamp;
1841   return ctx;
1842 }
1843
1844 static void
1845 join_fav_account_sig_ctx_free (join_fav_account_sig_ctx *ctx)
1846 {
1847   g_object_unref (ctx->account);
1848   g_object_unref (ctx->chatroom);
1849   g_slice_free (join_fav_account_sig_ctx, ctx);
1850 }
1851
1852 static void
1853 account_status_changed_cb (TpAccount  *account,
1854     TpConnectionStatus old_status,
1855     TpConnectionStatus new_status,
1856     guint reason,
1857     gchar *dbus_error_name,
1858     GHashTable *details,
1859     gpointer user_data)
1860 {
1861   join_fav_account_sig_ctx *ctx = user_data;
1862
1863   switch (new_status)
1864     {
1865       case TP_CONNECTION_STATUS_DISCONNECTED:
1866         /* Don't wait any longer */
1867         goto finally;
1868         break;
1869
1870       case TP_CONNECTION_STATUS_CONNECTING:
1871         /* Wait a bit */
1872         return;
1873
1874       case TP_CONNECTION_STATUS_CONNECTED:
1875         /* We can join the room */
1876         break;
1877
1878       default:
1879         g_assert_not_reached ();
1880     }
1881
1882   join_chatroom (ctx->chatroom, ctx->timestamp);
1883
1884 finally:
1885   g_source_remove (ctx->timeout);
1886   g_signal_handler_disconnect (account, ctx->sig_id);
1887 }
1888
1889 #define JOIN_FAVORITE_TIMEOUT 5
1890
1891 static gboolean
1892 join_favorite_timeout_cb (gpointer data)
1893 {
1894   join_fav_account_sig_ctx *ctx = data;
1895
1896   /* stop waiting for joining the favorite room */
1897   g_signal_handler_disconnect (ctx->account, ctx->sig_id);
1898   return FALSE;
1899 }
1900
1901 static void
1902 roster_window_favorite_chatroom_join (EmpathyChatroom *chatroom)
1903 {
1904   TpAccount *account;
1905
1906   account = empathy_chatroom_get_account (chatroom);
1907   if (tp_account_get_connection_status (account, NULL) !=
1908                TP_CONNECTION_STATUS_CONNECTED)
1909     {
1910       join_fav_account_sig_ctx *ctx;
1911
1912       ctx = join_fav_account_sig_ctx_new (account, chatroom,
1913         empathy_get_current_action_time ());
1914
1915       ctx->sig_id = g_signal_connect_data (account, "status-changed",
1916         G_CALLBACK (account_status_changed_cb), ctx,
1917         (GClosureNotify) join_fav_account_sig_ctx_free, 0);
1918
1919       ctx->timeout = g_timeout_add_seconds (JOIN_FAVORITE_TIMEOUT,
1920         join_favorite_timeout_cb, ctx);
1921       return;
1922     }
1923
1924   join_chatroom (chatroom, empathy_get_current_action_time ());
1925 }
1926
1927 static void
1928 roster_window_favorite_chatroom_menu_activate_cb (GAction *action,
1929     GVariant *parameter,
1930     EmpathyChatroom *chatroom)
1931 {
1932   roster_window_favorite_chatroom_join (chatroom);
1933 }
1934
1935 static gchar *
1936 dup_join_action_name (EmpathyChatroom *chatroom,
1937     gboolean prefix)
1938 {
1939   return g_strconcat (prefix ? "win." : "", "join-",
1940       empathy_chatroom_get_name (chatroom), NULL);
1941 }
1942
1943 static void
1944 roster_window_favorite_chatroom_menu_add (EmpathyRosterWindow *self,
1945     EmpathyChatroom *chatroom)
1946 {
1947   GMenuItem *item;
1948   const gchar *name, *account_name;
1949   gchar *label, *action_name;
1950   GAction *action;
1951
1952   name = empathy_chatroom_get_name (chatroom);
1953   account_name = tp_account_get_display_name (
1954       empathy_chatroom_get_account (chatroom));
1955
1956   label = g_strdup_printf ("%s (%s)", name, account_name);
1957   action_name = dup_join_action_name (chatroom, FALSE);
1958
1959   action = (GAction *) g_simple_action_new (action_name, NULL);
1960   g_free (action_name);
1961
1962   g_signal_connect (action, "activate",
1963       G_CALLBACK (roster_window_favorite_chatroom_menu_activate_cb),
1964       chatroom);
1965
1966   g_action_map_add_action (G_ACTION_MAP (self), action);
1967
1968   action_name = dup_join_action_name (chatroom, TRUE);
1969
1970   item = g_menu_item_new (label, action_name);
1971   g_menu_item_set_attribute (item, "room-name", "s", name);
1972   g_menu_append_item (self->priv->rooms_section, item);
1973
1974   g_free (label);
1975   g_free (action_name);
1976   g_object_unref (action);
1977 }
1978
1979 static void
1980 roster_window_favorite_chatroom_menu_added_cb (EmpathyChatroomManager *manager,
1981     EmpathyChatroom *chatroom,
1982     EmpathyRosterWindow *self)
1983 {
1984   roster_window_favorite_chatroom_menu_add (self, chatroom);
1985 }
1986
1987 static void
1988 roster_window_favorite_chatroom_menu_removed_cb (
1989     EmpathyChatroomManager *manager,
1990     EmpathyChatroom *chatroom,
1991     EmpathyRosterWindow *self)
1992 {
1993   GList *chatrooms;
1994   gchar *act;
1995   gint i;
1996
1997   act = dup_join_action_name (chatroom, TRUE);
1998
1999   g_action_map_remove_action (G_ACTION_MAP (self), act);
2000
2001   for (i = 0; i < g_menu_model_get_n_items (
2002         G_MENU_MODEL (self->priv->rooms_section)); i++)
2003     {
2004       const gchar *name;
2005
2006       if (g_menu_model_get_item_attribute (
2007             G_MENU_MODEL (self->priv->rooms_section), i, "room-name",
2008             "s", &name)
2009           && !tp_strdiff (name, empathy_chatroom_get_name (chatroom)))
2010         {
2011           g_menu_remove (self->priv->rooms_section, i);
2012           break;
2013         }
2014     }
2015
2016   chatrooms = empathy_chatroom_manager_get_chatrooms (
2017       self->priv->chatroom_manager, NULL);
2018
2019   g_list_free (chatrooms);
2020 }
2021
2022 static void
2023 roster_window_favorite_chatroom_menu_setup (EmpathyRosterWindow *self)
2024 {
2025   GList *chatrooms, *l;
2026
2027   self->priv->chatroom_manager = empathy_chatroom_manager_dup_singleton (NULL);
2028
2029   chatrooms = empathy_chatroom_manager_get_chatrooms (
2030     self->priv->chatroom_manager, NULL);
2031
2032   for (l = chatrooms; l; l = l->next)
2033     roster_window_favorite_chatroom_menu_add (self, l->data);
2034
2035   g_signal_connect (self->priv->chatroom_manager, "chatroom-added",
2036       G_CALLBACK (roster_window_favorite_chatroom_menu_added_cb),
2037       self);
2038
2039   g_signal_connect (self->priv->chatroom_manager, "chatroom-removed",
2040       G_CALLBACK (roster_window_favorite_chatroom_menu_removed_cb),
2041       self);
2042
2043   g_list_free (chatrooms);
2044 }
2045
2046 static void
2047 roster_window_room_join_new_cb (GSimpleAction *action,
2048     GVariant *parameter,
2049     gpointer user_data)
2050 {
2051   EmpathyRosterWindow *self = user_data;
2052
2053   empathy_new_chatroom_dialog_show (GTK_WINDOW (self));
2054 }
2055
2056 static void
2057 roster_window_room_join_favorites_cb (GSimpleAction *action,
2058     GVariant *parameter,
2059     gpointer user_data)
2060 {
2061   EmpathyRosterWindow *self = user_data;
2062   GList *chatrooms, *l;
2063
2064   chatrooms = empathy_chatroom_manager_get_chatrooms (self->priv->chatroom_manager,
2065       NULL);
2066
2067   for (l = chatrooms; l; l = l->next)
2068     roster_window_favorite_chatroom_join (l->data);
2069
2070   g_list_free (chatrooms);
2071 }
2072
2073 static void
2074 roster_window_room_manage_favorites_cb (GSimpleAction *action,
2075     GVariant *parameter,
2076     gpointer user_data)
2077 {
2078   EmpathyRosterWindow *self = user_data;
2079
2080   empathy_chatrooms_window_show (GTK_WINDOW (self));
2081 }
2082
2083 #if 0
2084 static void
2085 roster_window_edit_cb (GtkAction *action,
2086     EmpathyRosterWindow *self)
2087 {
2088   GtkWidget *submenu;
2089
2090   /* FIXME: It should use the UIManager to merge the contact/group submenu */
2091   submenu = empathy_individual_view_get_individual_menu (
2092       self->priv->individual_view);
2093   if (submenu)
2094     {
2095       GtkMenuItem *item;
2096       GtkWidget   *label;
2097
2098       item = GTK_MENU_ITEM (self->priv->edit_context);
2099       label = gtk_bin_get_child (GTK_BIN (item));
2100       gtk_label_set_text (GTK_LABEL (label), _("Contact"));
2101
2102       gtk_widget_show (self->priv->edit_context);
2103       gtk_widget_show (self->priv->edit_context_separator);
2104
2105       gtk_menu_item_set_submenu (item, submenu);
2106
2107       return;
2108     }
2109
2110   submenu = empathy_individual_view_get_group_menu (self->priv->individual_view);
2111   if (submenu)
2112     {
2113       GtkMenuItem *item;
2114       GtkWidget   *label;
2115
2116       item = GTK_MENU_ITEM (self->priv->edit_context);
2117       label = gtk_bin_get_child (GTK_BIN (item));
2118       gtk_label_set_text (GTK_LABEL (label), _("Group"));
2119
2120       gtk_widget_show (self->priv->edit_context);
2121       gtk_widget_show (self->priv->edit_context_separator);
2122
2123       gtk_menu_item_set_submenu (item, submenu);
2124
2125       return;
2126     }
2127
2128   gtk_widget_hide (self->priv->edit_context);
2129   gtk_widget_hide (self->priv->edit_context_separator);
2130
2131   return;
2132 }
2133 #endif
2134
2135 static void
2136 roster_window_edit_accounts_cb (GSimpleAction *action,
2137     GVariant *parameter,
2138     gpointer user_data)
2139 {
2140   empathy_accounts_dialog_show_application (gdk_screen_get_default (),
2141       NULL, FALSE, FALSE);
2142 }
2143
2144 static void
2145 roster_window_edit_blocked_contacts_cb (GSimpleAction *action,
2146     GVariant *parameter,
2147     gpointer user_data)
2148 {
2149   EmpathyRosterWindow *self = user_data;
2150   GtkWidget *dialog;
2151
2152   dialog = empathy_contact_blocking_dialog_new (GTK_WINDOW (self));
2153   gtk_widget_show (dialog);
2154
2155   g_signal_connect (dialog, "response",
2156       G_CALLBACK (gtk_widget_destroy), NULL);
2157 }
2158
2159 void
2160 empathy_roster_window_show_preferences (EmpathyRosterWindow *self,
2161     const gchar *tab)
2162 {
2163   if (self->priv->preferences == NULL)
2164     {
2165       self->priv->preferences = empathy_preferences_new (GTK_WINDOW (self),
2166                                                    self->priv->shell_running);
2167       g_object_add_weak_pointer (G_OBJECT (self->priv->preferences),
2168                (gpointer) &self->priv->preferences);
2169
2170       gtk_widget_show (self->priv->preferences);
2171     }
2172   else
2173     {
2174       gtk_window_present (GTK_WINDOW (self->priv->preferences));
2175     }
2176
2177   if (tab != NULL)
2178     empathy_preferences_show_tab (
2179       EMPATHY_PREFERENCES (self->priv->preferences), tab);
2180 }
2181
2182 static void
2183 roster_window_edit_preferences_cb (GSimpleAction *action,
2184     GVariant *parameter,
2185     gpointer user_data)
2186 {
2187   EmpathyRosterWindow *self = user_data;
2188
2189   empathy_roster_window_show_preferences (self, NULL);
2190 }
2191
2192 static void
2193 roster_window_help_about_cb (GSimpleAction *action,
2194     GVariant *parameter,
2195     gpointer user_data)
2196 {
2197   EmpathyRosterWindow *self = user_data;
2198
2199   empathy_about_dialog_new (GTK_WINDOW (self));
2200 }
2201
2202 static void
2203 roster_window_help_debug_cb (GSimpleAction *action,
2204     GVariant *parameter,
2205     gpointer user_data)
2206 {
2207   empathy_launch_program (BIN_DIR, "empathy-debugger", NULL);
2208 }
2209
2210 static void
2211 roster_window_help_contents_cb (GSimpleAction *action,
2212     GVariant *parameter,
2213     gpointer user_data)
2214 {
2215   EmpathyRosterWindow *self = user_data;
2216
2217   empathy_url_show (GTK_WIDGET (self), "help:empathy");
2218 }
2219
2220 static gboolean
2221 roster_window_throbber_button_press_event_cb (GtkWidget *throbber,
2222     GdkEventButton *event,
2223     EmpathyRosterWindow *self)
2224 {
2225   if (event->type != GDK_BUTTON_PRESS ||
2226       event->button != 1)
2227     return FALSE;
2228
2229   empathy_accounts_dialog_show_application (
2230       gtk_widget_get_screen (GTK_WIDGET (throbber)),
2231       NULL, FALSE, FALSE);
2232
2233   return FALSE;
2234 }
2235
2236 static void
2237 roster_window_account_removed_cb (TpAccountManager  *manager,
2238     TpAccount *account,
2239     EmpathyRosterWindow *self)
2240 {
2241   /* remove errors if any */
2242   roster_window_remove_error (self, account);
2243
2244   /* remove the balance action if required */
2245   roster_window_remove_balance_action (self, account);
2246 }
2247
2248 static void
2249 account_connection_notify_cb (TpAccount *account,
2250     GParamSpec *spec,
2251     EmpathyRosterWindow *self)
2252 {
2253   TpConnection *conn;
2254
2255   conn = tp_account_get_connection (account);
2256
2257   if (conn != NULL)
2258     {
2259       roster_window_setup_balance (self, account);
2260     }
2261   else
2262     {
2263       /* remove balance action if required */
2264       roster_window_remove_balance_action (self, account);
2265     }
2266 }
2267
2268 static void
2269 add_account (EmpathyRosterWindow *self,
2270     TpAccount *account)
2271 {
2272   gulong handler_id;
2273
2274   handler_id = GPOINTER_TO_UINT (g_hash_table_lookup (
2275     self->priv->status_changed_handlers, account));
2276
2277   /* connect signal only if it was not connected yet */
2278   if (handler_id != 0)
2279     return;
2280
2281   handler_id = g_signal_connect (account, "status-changed",
2282     G_CALLBACK (roster_window_connection_changed_cb), self);
2283
2284   g_hash_table_insert (self->priv->status_changed_handlers,
2285     account, GUINT_TO_POINTER (handler_id));
2286
2287   /* roster_window_setup_balance() relies on the TpConnection to be ready on
2288    * the TpAccount so we connect this signal as well. */
2289   tp_g_signal_connect_object (account, "notify::connection",
2290       G_CALLBACK (account_connection_notify_cb), self, 0);
2291
2292   roster_window_setup_balance (self, account);
2293 }
2294
2295 /* @account: if not %NULL, the only account which can be enabled */
2296 static void
2297 display_page_account_not_enabled (EmpathyRosterWindow *self,
2298     TpAccount *account)
2299 {
2300   if (account == NULL)
2301     {
2302       display_page_message (self,
2303           _("You need to enable one of your accounts to see contacts here."),
2304           TRUE, FALSE);
2305     }
2306   else
2307     {
2308       gchar *tmp;
2309
2310       /* translators: argument is an account name */
2311       tmp = g_strdup_printf (_("You need to enable %s to see contacts here."),
2312           tp_account_get_display_name (account));
2313
2314       display_page_message (self, tmp, TRUE, FALSE);
2315       g_free (tmp);
2316     }
2317 }
2318 static gboolean
2319 has_enabled_account (GList *accounts)
2320 {
2321   GList *l;
2322
2323   for (l = accounts; l != NULL; l = g_list_next (l))
2324     {
2325       TpAccount *account = l->data;
2326
2327       if (tp_account_is_enabled (account))
2328         return TRUE;
2329     }
2330
2331   return FALSE;
2332 }
2333
2334 static void
2335 set_notebook_page (EmpathyRosterWindow *self)
2336 {
2337   GList *accounts;
2338   guint len;
2339
2340   accounts = tp_account_manager_get_valid_accounts (
2341       self->priv->account_manager);
2342
2343   len = g_list_length (accounts);
2344
2345   if (len == 0)
2346     {
2347       /* No account */
2348       display_page_no_account (self);
2349       goto out;
2350     }
2351
2352   if (!has_enabled_account (accounts))
2353     {
2354       TpAccount *account = NULL;
2355
2356       /* Pass the account if there is only one which can be enabled */
2357       if (len == 1)
2358         account = accounts->data;
2359
2360       display_page_account_not_enabled (self, account);
2361       goto out;
2362     }
2363
2364   display_page_contact_list (self);
2365
2366 out:
2367   g_list_free (accounts);
2368 }
2369
2370 static void
2371 roster_window_account_validity_changed_cb (TpAccountManager  *manager,
2372     TpAccount *account,
2373     gboolean valid,
2374     EmpathyRosterWindow *self)
2375 {
2376   if (valid)
2377     add_account (self, account);
2378   else
2379     roster_window_account_removed_cb (manager, account, self);
2380
2381   set_notebook_page (self);
2382 }
2383
2384 #if 0
2385 static void
2386 roster_window_notify_show_offline_cb (GSettings *gsettings,
2387     const gchar *key,
2388     gpointer toggle_action)
2389 {
2390   gtk_toggle_action_set_active (toggle_action,
2391       g_settings_get_boolean (gsettings, key));
2392 }
2393 #endif
2394
2395 static void
2396 roster_window_connection_items_setup (EmpathyRosterWindow *self,
2397     GtkBuilder *gui)
2398 {
2399   guint i;
2400   const gchar *actions_connected[] = {
2401       "room_join_new",
2402       "room_join_favorites",
2403       "chat_new_message",
2404       "chat_new_call",
2405       "chat_search_contacts",
2406       "chat_add_contact",
2407       "edit_blocked_contacts",
2408       "edit_search_contacts"
2409   };
2410
2411   for (i = 0; i < G_N_ELEMENTS (actions_connected); i++)
2412     {
2413       GAction *action;
2414
2415       action = g_action_map_lookup_action (G_ACTION_MAP (self),
2416           actions_connected[i]);
2417
2418       self->priv->actions_connected = g_list_prepend (
2419           self->priv->actions_connected, action);
2420     }
2421 }
2422
2423 static void
2424 account_enabled_cb (TpAccountManager *manager,
2425     TpAccount *account,
2426     EmpathyRosterWindow *self)
2427 {
2428   set_notebook_page (self);
2429 }
2430
2431 static void
2432 account_disabled_cb (TpAccountManager *manager,
2433     TpAccount *account,
2434     EmpathyRosterWindow *self)
2435 {
2436   set_notebook_page (self);
2437 }
2438
2439 static void
2440 account_manager_prepared_cb (GObject *source_object,
2441     GAsyncResult *result,
2442     gpointer user_data)
2443 {
2444   GList *accounts, *j;
2445   TpAccountManager *manager = TP_ACCOUNT_MANAGER (source_object);
2446   EmpathyRosterWindow *self = user_data;
2447   GError *error = NULL;
2448
2449   if (!tp_proxy_prepare_finish (manager, result, &error))
2450     {
2451       DEBUG ("Failed to prepare account manager: %s", error->message);
2452       g_error_free (error);
2453       return;
2454     }
2455
2456   accounts = tp_account_manager_get_valid_accounts (self->priv->account_manager);
2457   for (j = accounts; j != NULL; j = j->next)
2458     {
2459       TpAccount *account = TP_ACCOUNT (j->data);
2460
2461       add_account (self, account);
2462     }
2463
2464   g_signal_connect (manager, "account-validity-changed",
2465       G_CALLBACK (roster_window_account_validity_changed_cb), self);
2466   tp_g_signal_connect_object (manager, "account-disabled",
2467       G_CALLBACK (account_disabled_cb), self, 0);
2468   tp_g_signal_connect_object (manager, "account-enabled",
2469       G_CALLBACK (account_enabled_cb), self, 0);
2470
2471   roster_window_update_status (self);
2472
2473   set_notebook_page (self);
2474
2475   g_list_free (accounts);
2476 }
2477
2478 void
2479 empathy_roster_window_set_shell_running (EmpathyRosterWindow *self,
2480     gboolean shell_running)
2481 {
2482   if (self->priv->shell_running == shell_running)
2483     return;
2484
2485   self->priv->shell_running = shell_running;
2486   g_object_notify (G_OBJECT (self), "shell-running");
2487 }
2488
2489 static GObject *
2490 empathy_roster_window_constructor (GType type,
2491     guint n_construct_params,
2492     GObjectConstructParam *construct_params)
2493 {
2494   static GObject *window = NULL;
2495
2496   if (window != NULL)
2497     return g_object_ref (window);
2498
2499   window = G_OBJECT_CLASS (empathy_roster_window_parent_class)->constructor (
2500     type, n_construct_params, construct_params);
2501
2502   g_object_add_weak_pointer (window, (gpointer) &window);
2503
2504   return window;
2505 }
2506
2507 static GActionEntry menubar_entries[] = {
2508   { "chat_new_message", roster_window_chat_new_message_cb, NULL, NULL, NULL },
2509   { "chat_new_call", roster_window_chat_new_call_cb, NULL, NULL, NULL },
2510   { "chat_add_contact", roster_window_chat_add_contact_cb, NULL, NULL, NULL },
2511   { "chat_search_contacts", roster_window_chat_search_contacts_cb, NULL, NULL, NULL },
2512   { "chat_quit", roster_window_chat_quit_cb, NULL, NULL, NULL },
2513
2514   { "edit_accounts", roster_window_edit_accounts_cb, NULL, NULL, NULL },
2515   { "edit_search_contacts", roster_window_edit_search_contacts_cb, NULL, NULL, NULL },
2516   { "edit_blocked_contacts", roster_window_edit_blocked_contacts_cb, NULL, NULL, NULL },
2517   { "edit_preferences", roster_window_edit_preferences_cb, NULL, NULL, NULL },
2518
2519   { "view_show_offline", activate_toggle, NULL, "false", roster_window_view_show_offline_cb },
2520   /* TODO */
2521   { "view_history", roster_window_view_history_cb, NULL, NULL, NULL },
2522   { "view_show_ft_manager", roster_window_view_show_ft_manager, NULL, NULL, NULL },
2523   { "view_show_map", roster_window_view_show_map_cb, NULL, NULL, NULL },
2524
2525   { "room_join_new", roster_window_room_join_new_cb, NULL, NULL, NULL },
2526   { "room_join_favorites", roster_window_room_join_favorites_cb, NULL, NULL, NULL },
2527   { "room_manage_favorites", roster_window_room_manage_favorites_cb, NULL, NULL, NULL },
2528
2529   { "help_contents", roster_window_help_contents_cb, NULL, NULL, NULL },
2530   { "help_debug", roster_window_help_debug_cb, NULL, NULL, NULL },
2531   { "help_about", roster_window_help_about_cb, NULL, NULL, NULL },
2532 };
2533
2534 static void
2535 empathy_roster_window_constructed (GObject *object)
2536 {
2537   EmpathyRosterWindow *self = EMPATHY_ROSTER_WINDOW (object);
2538   gchar *filename;
2539   GtkBuilder *builder;
2540   GtkApplication *app;
2541   GMenu *menubar;
2542   /* TODO: <cassidy> desrt, yeah. So I should basically move all my menu code
2543    * from gtkappwin to gtkapp, just deal with it in the app itself and stop
2544    * caring about the appwin ? */
2545
2546   G_OBJECT_CLASS (empathy_roster_window_parent_class)->constructed (object);
2547
2548   app = gtk_window_get_application (GTK_WINDOW (self));
2549   g_return_if_fail (app != NULL);
2550
2551   g_action_map_add_action_entries (G_ACTION_MAP (self),
2552       menubar_entries, G_N_ELEMENTS (menubar_entries), self);
2553
2554   filename = empathy_file_lookup ("empathy-roster-window-menubar.ui", "src");
2555   builder = empathy_builder_get_file (filename,
2556       /*
2557       "ui_manager", &self->priv->ui_manager,
2558       "view_show_offline", &show_offline_widget,
2559       "view_show_protocols", &self->priv->show_protocols,
2560       "view_sort_by_name", &self->priv->sort_by_name,
2561       "view_sort_by_status", &self->priv->sort_by_status,
2562       "view_normal_size_with_avatars", &self->priv->normal_with_avatars,
2563       "view_normal_size", &self->priv->normal_size,
2564       "view_compact_size", &self->priv->compact_size,
2565       "view_history", &self->priv->view_history,
2566       "menubar", &menubar,
2567       */
2568       NULL);
2569   g_free (filename);
2570
2571   menubar = G_MENU (gtk_builder_get_object (builder, "menubar"));
2572
2573   gtk_application_set_menubar (app, G_MENU_MODEL(menubar));
2574
2575   /* Disable map if built without champlain */
2576 #ifndef HAVE_LIBCHAMPLAIN
2577     {
2578       GAction *view_show_map;
2579
2580       view_show_map = g_action_map_lookup_action (G_ACTION_MAP (self),
2581           "view_show_map");
2582       g_simple_action_set_enabled (G_SIMPLE_ACTION (view_show_map), FALSE);
2583     }
2584 #endif
2585
2586   /* Set up connection related actions. */
2587   roster_window_connection_items_setup (self, NULL);
2588
2589   /* Add rooms to the menu */
2590   self->priv->rooms_section = G_MENU (gtk_builder_get_object (builder,
2591         "rooms"));
2592   g_object_ref (self->priv->rooms_section);
2593
2594   self->priv->balance_section = G_MENU (gtk_builder_get_object (builder,
2595         "balance"));
2596   g_object_ref (self->priv->balance_section);
2597
2598   roster_window_favorite_chatroom_menu_setup (self);
2599
2600   g_object_unref (builder);
2601 }
2602
2603 static void
2604 empathy_roster_window_set_property (GObject *object,
2605     guint property_id,
2606     const GValue *value,
2607     GParamSpec *pspec)
2608 {
2609   EmpathyRosterWindow *self = EMPATHY_ROSTER_WINDOW (object);
2610
2611   switch (property_id)
2612     {
2613       case PROP_SHELL_RUNNING:
2614         self->priv->shell_running = g_value_get_boolean (value);
2615         break;
2616       default:
2617         G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
2618         break;
2619     }
2620 }
2621
2622 static void
2623 empathy_roster_window_get_property (GObject    *object,
2624     guint property_id,
2625     GValue *value,
2626     GParamSpec *pspec)
2627 {
2628   EmpathyRosterWindow *self = EMPATHY_ROSTER_WINDOW (object);
2629
2630   switch (property_id)
2631     {
2632       case PROP_SHELL_RUNNING:
2633         g_value_set_boolean (value, self->priv->shell_running);
2634         break;
2635       default:
2636         G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
2637         break;
2638     }
2639 }
2640
2641 static void
2642 empathy_roster_window_class_init (EmpathyRosterWindowClass *klass)
2643 {
2644   GObjectClass *object_class = G_OBJECT_CLASS (klass);
2645   GParamSpec *pspec;
2646
2647   object_class->finalize = empathy_roster_window_finalize;
2648   object_class->constructor = empathy_roster_window_constructor;
2649   object_class->constructed = empathy_roster_window_constructed;
2650
2651   object_class->set_property = empathy_roster_window_set_property;
2652   object_class->get_property = empathy_roster_window_get_property;
2653
2654   pspec = g_param_spec_boolean ("shell-running",
2655       "Shell running",
2656       "Whether the Shell is running or not",
2657       FALSE,
2658       G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS);
2659   g_object_class_install_property (object_class, PROP_SHELL_RUNNING, pspec);
2660
2661   g_type_class_add_private (object_class, sizeof (EmpathyRosterWindowPriv));
2662 }
2663
2664 static void
2665 show_contacts_loading (EmpathyRosterWindow *self)
2666 {
2667   display_page_message (self, NULL, FALSE, TRUE);
2668
2669   gtk_spinner_start (GTK_SPINNER (self->priv->spinner_loading));
2670 }
2671
2672 static void
2673 hide_contacts_loading (EmpathyRosterWindow *self)
2674 {
2675   gtk_spinner_stop (GTK_SPINNER (self->priv->spinner_loading));
2676
2677   set_notebook_page (self);
2678 }
2679
2680 static void
2681 contacts_loaded_cb (EmpathyIndividualManager *manager,
2682     EmpathyRosterWindow *self)
2683 {
2684   hide_contacts_loading (self);
2685 }
2686
2687 static void
2688 empathy_roster_window_init (EmpathyRosterWindow *self)
2689 {
2690   GtkBuilder *gui;
2691   GtkWidget *sw;
2692   //GtkToggleAction *show_offline_widget;
2693   GtkToolItem *item;
2694   //gboolean show_offline;
2695   gchar *filename;
2696   GtkTreeModel *model;
2697   GtkWidget *search_vbox;
2698
2699   self->priv = G_TYPE_INSTANCE_GET_PRIVATE (self,
2700       EMPATHY_TYPE_ROSTER_WINDOW, EmpathyRosterWindowPriv);
2701
2702   self->priv->gsettings_ui = g_settings_new (EMPATHY_PREFS_UI_SCHEMA);
2703   self->priv->gsettings_contacts = g_settings_new (EMPATHY_PREFS_CONTACTS_SCHEMA);
2704
2705   self->priv->sound_mgr = empathy_sound_manager_dup_singleton ();
2706
2707   gtk_window_set_title (GTK_WINDOW (self), _("Contact List"));
2708   gtk_window_set_role (GTK_WINDOW (self), "contact_list");
2709   gtk_window_set_default_size (GTK_WINDOW (self), 225, 325);
2710
2711   /* don't finalize the widget on delete-event, just hide it */
2712   g_signal_connect (self, "delete-event",
2713     G_CALLBACK (gtk_widget_hide_on_delete), NULL);
2714
2715   /* Set up interface */
2716   filename = empathy_file_lookup ("empathy-roster-window.ui", "src");
2717   gui = empathy_builder_get_file (filename,
2718       "main_vbox", &self->priv->main_vbox,
2719       "balance_vbox", &self->priv->balance_vbox,
2720       "errors_vbox", &self->priv->errors_vbox,
2721       "auth_vbox", &self->priv->auth_vbox,
2722       "search_vbox", &search_vbox,
2723       "presence_toolbar", &self->priv->presence_toolbar,
2724       "notebook", &self->priv->notebook,
2725       "no_entry_label", &self->priv->no_entry_label,
2726       "roster_scrolledwindow", &sw,
2727       "button_account_settings", &self->priv->button_account_settings,
2728       "spinner_loading", &self->priv->spinner_loading,
2729       NULL);
2730   g_free (filename);
2731
2732   gtk_container_add (GTK_CONTAINER (self), self->priv->main_vbox);
2733   gtk_widget_show (self->priv->main_vbox);
2734
2735   g_signal_connect (self, "key-press-event",
2736       G_CALLBACK (roster_window_key_press_event_cb), NULL);
2737
2738   //g_object_ref (self->priv->ui_manager);
2739   g_object_unref (gui);
2740
2741   self->priv->account_manager = tp_account_manager_dup ();
2742
2743   tp_proxy_prepare_async (self->priv->account_manager, NULL,
2744       account_manager_prepared_cb, self);
2745
2746   self->priv->errors = g_hash_table_new_full (g_direct_hash, g_direct_equal,
2747       g_object_unref, NULL);
2748
2749   self->priv->auths = g_hash_table_new (NULL, NULL);
2750
2751   self->priv->status_changed_handlers = g_hash_table_new_full (g_direct_hash,
2752       g_direct_equal, NULL, NULL);
2753
2754   self->priv->topup_menu_items = g_hash_table_new_full (NULL, NULL,
2755       g_object_unref, g_object_unref);
2756
2757   /*
2758   self->priv->edit_context = gtk_ui_manager_get_widget (self->priv->ui_manager,
2759       "/menubar/edit/edit_context");
2760   self->priv->edit_context_separator = gtk_ui_manager_get_widget (
2761       self->priv->ui_manager,
2762       "/menubar/edit/edit_context_separator");
2763   gtk_widget_hide (self->priv->edit_context);
2764   gtk_widget_hide (self->priv->edit_context_separator);
2765       */
2766
2767   /* Set up contact list. */
2768   empathy_status_presets_get_all ();
2769
2770   /* Set up presence chooser */
2771   self->priv->presence_chooser = empathy_presence_chooser_new ();
2772   gtk_widget_show (self->priv->presence_chooser);
2773   item = gtk_tool_item_new ();
2774   gtk_widget_show (GTK_WIDGET (item));
2775   gtk_widget_set_size_request (self->priv->presence_chooser, 10, -1);
2776   gtk_container_add (GTK_CONTAINER (item), self->priv->presence_chooser);
2777   gtk_tool_item_set_is_important (item, TRUE);
2778   gtk_tool_item_set_expand (item, TRUE);
2779   gtk_toolbar_insert (GTK_TOOLBAR (self->priv->presence_toolbar), item, -1);
2780
2781   /* Set up the throbber */
2782   self->priv->throbber = gtk_spinner_new ();
2783   gtk_widget_set_size_request (self->priv->throbber, 16, -1);
2784   gtk_widget_set_events (self->priv->throbber, GDK_BUTTON_PRESS_MASK);
2785   g_signal_connect (self->priv->throbber, "button-press-event",
2786     G_CALLBACK (roster_window_throbber_button_press_event_cb),
2787     self);
2788   gtk_widget_show (self->priv->throbber);
2789
2790   item = gtk_tool_item_new ();
2791   gtk_container_set_border_width (GTK_CONTAINER (item), 6);
2792   gtk_toolbar_insert (GTK_TOOLBAR (self->priv->presence_toolbar), item, -1);
2793   gtk_container_add (GTK_CONTAINER (item), self->priv->throbber);
2794   self->priv->throbber_tool_item = GTK_WIDGET (item);
2795
2796   /* XXX: this class is designed to live for the duration of the program,
2797    * so it's got a race condition between its signal handlers and its
2798    * finalization. The class is planned to be removed, so we won't fix
2799    * this before then. */
2800   self->priv->individual_manager = empathy_individual_manager_dup_singleton ();
2801
2802   if (!empathy_individual_manager_get_contacts_loaded (
2803         self->priv->individual_manager))
2804     {
2805       show_contacts_loading (self);
2806
2807       tp_g_signal_connect_object (self->priv->individual_manager,
2808           "contacts-loaded", G_CALLBACK (contacts_loaded_cb), self, 0);
2809     }
2810
2811   self->priv->individual_store = EMPATHY_INDIVIDUAL_STORE (
2812       empathy_individual_store_manager_new (self->priv->individual_manager));
2813
2814   /* For the moment, we disallow Persona drops onto the roster contact list
2815    * (e.g. from things such as the EmpathyPersonaView in the linking dialogue).
2816    * No code is hooked up to do anything on a Persona drop, so allowing them
2817    * would achieve nothing except confusion. */
2818   self->priv->individual_view = empathy_individual_view_new (
2819       self->priv->individual_store,
2820       /* EmpathyIndividualViewFeatureFlags */
2821       EMPATHY_INDIVIDUAL_VIEW_FEATURE_GROUPS_SAVE |
2822       EMPATHY_INDIVIDUAL_VIEW_FEATURE_GROUPS_RENAME |
2823       EMPATHY_INDIVIDUAL_VIEW_FEATURE_GROUPS_REMOVE |
2824       EMPATHY_INDIVIDUAL_VIEW_FEATURE_GROUPS_CHANGE |
2825       EMPATHY_INDIVIDUAL_VIEW_FEATURE_INDIVIDUAL_REMOVE |
2826       EMPATHY_INDIVIDUAL_VIEW_FEATURE_INDIVIDUAL_DROP |
2827       EMPATHY_INDIVIDUAL_VIEW_FEATURE_INDIVIDUAL_DRAG |
2828       EMPATHY_INDIVIDUAL_VIEW_FEATURE_INDIVIDUAL_TOOLTIP |
2829       EMPATHY_INDIVIDUAL_VIEW_FEATURE_INDIVIDUAL_CALL |
2830       EMPATHY_INDIVIDUAL_VIEW_FEATURE_FILE_DROP,
2831       /* EmpathyIndividualFeatureFlags  */
2832       EMPATHY_INDIVIDUAL_FEATURE_CHAT |
2833       EMPATHY_INDIVIDUAL_FEATURE_CALL |
2834       EMPATHY_INDIVIDUAL_FEATURE_EDIT |
2835       EMPATHY_INDIVIDUAL_FEATURE_INFO |
2836       EMPATHY_INDIVIDUAL_FEATURE_LOG |
2837       EMPATHY_INDIVIDUAL_FEATURE_SMS |
2838       EMPATHY_INDIVIDUAL_FEATURE_CALL_PHONE);
2839
2840   gtk_widget_show (GTK_WIDGET (self->priv->individual_view));
2841   gtk_container_add (GTK_CONTAINER (sw),
2842       GTK_WIDGET (self->priv->individual_view));
2843   g_signal_connect (self->priv->individual_view, "row-activated",
2844      G_CALLBACK (roster_window_row_activated_cb), self);
2845
2846   /* Set up search bar */
2847   self->priv->search_bar = empathy_live_search_new (
2848       GTK_WIDGET (self->priv->individual_view));
2849   empathy_individual_view_set_live_search (self->priv->individual_view,
2850       EMPATHY_LIVE_SEARCH (self->priv->search_bar));
2851   gtk_box_pack_start (GTK_BOX (search_vbox), self->priv->search_bar,
2852       FALSE, TRUE, 0);
2853
2854   g_signal_connect_swapped (self, "map",
2855       G_CALLBACK (gtk_widget_grab_focus), self->priv->individual_view);
2856
2857   /* Connect to proper signals to check if contact list is empty or not */
2858   model = gtk_tree_view_get_model (GTK_TREE_VIEW (self->priv->individual_view));
2859   self->priv->empty = TRUE;
2860   g_signal_connect (model, "row-inserted",
2861       G_CALLBACK (roster_window_row_inserted_cb), self);
2862   g_signal_connect (model, "row-deleted",
2863       G_CALLBACK (roster_window_row_deleted_cb), self);
2864
2865   /* Load user-defined accelerators. */
2866   roster_window_accels_load ();
2867
2868   gtk_window_set_default_size (GTK_WINDOW (self), -1, 600);
2869   /* Set window size. */
2870   empathy_geometry_bind (GTK_WINDOW (self), GEOMETRY_NAME);
2871
2872   /* Enable event handling */
2873   self->priv->call_observer = empathy_call_observer_dup_singleton ();
2874   self->priv->event_manager = empathy_event_manager_dup_singleton ();
2875
2876   g_signal_connect (self->priv->event_manager, "event-added",
2877       G_CALLBACK (roster_window_event_added_cb), self);
2878   g_signal_connect (self->priv->event_manager, "event-removed",
2879       G_CALLBACK (roster_window_event_removed_cb), self);
2880   g_signal_connect (self->priv->account_manager, "account-validity-changed",
2881       G_CALLBACK (roster_window_account_validity_changed_cb), self);
2882   g_signal_connect (self->priv->account_manager, "account-removed",
2883       G_CALLBACK (roster_window_account_removed_cb), self);
2884   g_signal_connect (self->priv->account_manager, "account-disabled",
2885       G_CALLBACK (roster_window_account_disabled_cb), self);
2886
2887   /* Show offline ? */
2888   /*
2889   show_offline = g_settings_get_boolean (self->priv->gsettings_ui,
2890       EMPATHY_PREFS_UI_SHOW_OFFLINE);
2891
2892   g_signal_connect (self->priv->gsettings_ui,
2893       "changed::" EMPATHY_PREFS_UI_SHOW_OFFLINE,
2894       G_CALLBACK (roster_window_notify_show_offline_cb), show_offline_widget);
2895
2896   gtk_toggle_action_set_active (show_offline_widget, show_offline);
2897       */
2898
2899   /* Show protocol ? */
2900   /*
2901   g_signal_connect (self->priv->gsettings_ui,
2902       "changed::" EMPATHY_PREFS_UI_SHOW_PROTOCOLS,
2903       G_CALLBACK (roster_window_notify_show_protocols_cb), self);
2904
2905   roster_window_notify_show_protocols_cb (self->priv->gsettings_ui,
2906       EMPATHY_PREFS_UI_SHOW_PROTOCOLS, self);
2907       */
2908
2909   /* Sort by name / by status ? */
2910   /*
2911   g_signal_connect (self->priv->gsettings_contacts,
2912       "changed::" EMPATHY_PREFS_CONTACTS_SORT_CRITERIUM,
2913       G_CALLBACK (roster_window_notify_sort_contact_cb), self);
2914
2915   roster_window_notify_sort_contact_cb (self->priv->gsettings_contacts,
2916       EMPATHY_PREFS_CONTACTS_SORT_CRITERIUM, self);
2917       */
2918
2919   /* Contacts list size */
2920   /*
2921   g_signal_connect (self->priv->gsettings_ui,
2922       "changed::" EMPATHY_PREFS_UI_COMPACT_CONTACT_LIST,
2923       G_CALLBACK (roster_window_notify_contact_list_size_cb), self);
2924
2925   g_signal_connect (self->priv->gsettings_ui,
2926       "changed::" EMPATHY_PREFS_UI_SHOW_AVATARS,
2927       G_CALLBACK (roster_window_notify_contact_list_size_cb),
2928       self);
2929       */
2930
2931   /*
2932   roster_window_notify_contact_list_size_cb (self->priv->gsettings_ui,
2933       EMPATHY_PREFS_UI_SHOW_AVATARS, self);
2934       */
2935
2936   g_signal_connect (self->priv->button_account_settings, "clicked",
2937       G_CALLBACK (button_account_settings_clicked_cb), self);
2938 }
2939
2940 GtkWidget *
2941 empathy_roster_window_new (GtkApplication *app)
2942 {
2943   return g_object_new (EMPATHY_TYPE_ROSTER_WINDOW,
2944       "application", app,
2945       NULL);
2946 }