e584cdd3db3a851cb65866e3f26355533de45712
[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 #include "empathy-roster-window.h"
26
27 #include <sys/stat.h>
28 #include <glib/gi18n.h>
29 #include <tp-account-widgets/tpaw-builder.h>
30
31 #include "empathy-about-dialog.h"
32 #include "empathy-accounts-dialog.h"
33 #include "empathy-call-observer.h"
34 #include "empathy-chat-manager.h"
35 #include "empathy-chatroom-manager.h"
36 #include "empathy-chatrooms-window.h"
37 #include "empathy-client-factory.h"
38 #include "empathy-contact-blocking-dialog.h"
39 #include "empathy-contact-search-dialog.h"
40 #include "empathy-event-manager.h"
41 #include "empathy-ft-manager.h"
42 #include "empathy-geometry.h"
43 #include "empathy-gsettings.h"
44 #include "empathy-gsettings.h"
45 #include "empathy-gtk-enum-types.h"
46 #include "empathy-individual-dialogs.h"
47 #include "empathy-log-window.h"
48 #include "empathy-new-call-dialog.h"
49 #include "empathy-new-chatroom-dialog.h"
50 #include "empathy-new-message-dialog.h"
51 #include "empathy-preferences.h"
52 #include "empathy-presence-chooser.h"
53 #include "empathy-presence-manager.h"
54 #include "empathy-request-util.h"
55 #include "empathy-roster-model-manager.h"
56 #include "empathy-roster-view.h"
57 #include "empathy-status-presets.h"
58 #include "empathy-ui-utils.h"
59 #include "empathy-utils.h"
60
61 #define DEBUG_FLAG EMPATHY_DEBUG_OTHER
62 #include "empathy-debug.h"
63
64 /* Flashing delay for icons (milliseconds). */
65 #define FLASH_TIMEOUT 500
66
67 /* Minimum width of roster window if something goes wrong. */
68 #define MIN_WIDTH 50
69
70 /* Accels (menu shortcuts) can be configured and saved */
71 #define ACCELS_FILENAME "accels.txt"
72
73 /* Name in the geometry file */
74 #define GEOMETRY_NAME "roster-window"
75
76 enum
77 {
78   PAGE_CONTACT_LIST = 0,
79   PAGE_MESSAGE
80 };
81
82 enum
83 {
84   PROP_0,
85   PROP_SHELL_RUNNING
86 };
87
88 G_DEFINE_TYPE (EmpathyRosterWindow, empathy_roster_window, GTK_TYPE_APPLICATION_WINDOW)
89
90 struct _EmpathyRosterWindowPriv {
91   EmpathyRosterView *view;
92   TpAccountManager *account_manager;
93   EmpathyChatroomManager *chatroom_manager;
94   EmpathyEventManager *event_manager;
95   EmpathySoundManager *sound_mgr;
96   EmpathyCallObserver *call_observer;
97   EmpathyIndividualManager *individual_manager;
98   guint flash_timeout_id;
99   gboolean flash_on;
100
101   GSettings *gsettings_ui;
102
103   GtkWidget *preferences;
104   GtkWidget *main_vbox;
105   GtkWidget *throbber;
106   GtkWidget *presence_toolbar;
107   GtkWidget *presence_chooser;
108   GtkWidget *errors_vbox;
109   GtkWidget *auth_vbox;
110   GtkWidget *search_bar;
111   GtkWidget *notebook;
112   GtkWidget *no_entry_label;
113   GtkWidget *button_account_settings;
114   GtkWidget *button_online;
115   GtkWidget *button_show_offline;
116   GtkWidget *button_add_contact;
117   GtkWidget *spinner_loading;
118   GtkWidget *tooltip_widget;
119
120   GMenu *menumodel;
121   GMenu *rooms_section;
122
123   GtkWidget *balance_vbox;
124
125   guint size_timeout_id;
126
127   /* reffed TpAccount* => visible GtkInfoBar* */
128   GHashTable *errors;
129
130   /* EmpathyEvent* => visible GtkInfoBar* */
131   GHashTable *auths;
132
133   /* stores a mapping from TpAccount to Handler ID to prevent
134    * to listen more than once to the status-changed signal */
135   GHashTable *status_changed_handlers;
136
137   /* Actions that are enabled when there are connected accounts */
138   GList *actions_connected;
139
140   gboolean shell_running;
141 };
142
143 static void
144 roster_window_remove_auth (EmpathyRosterWindow *self,
145     EmpathyEvent *event)
146 {
147   GtkWidget *error_widget;
148
149   error_widget = g_hash_table_lookup (self->priv->auths, event);
150   if (error_widget != NULL)
151     {
152       gtk_widget_destroy (error_widget);
153       g_hash_table_remove (self->priv->auths, event);
154     }
155 }
156
157 static void
158 roster_window_auth_add_clicked_cb (GtkButton *button,
159     EmpathyRosterWindow *self)
160 {
161   EmpathyEvent *event;
162
163   event = g_object_get_data (G_OBJECT (button), "event");
164
165   empathy_event_approve (event);
166
167   roster_window_remove_auth (self, event);
168 }
169
170 static void
171 roster_window_auth_close_clicked_cb (GtkButton *button,
172     EmpathyRosterWindow *self)
173 {
174   EmpathyEvent *event;
175
176   event = g_object_get_data (G_OBJECT (button), "event");
177
178   empathy_event_decline (event);
179   roster_window_remove_auth (self, event);
180 }
181
182 static void
183 roster_window_auth_display (EmpathyRosterWindow *self,
184     EmpathyEvent *event)
185 {
186   TpAccount *account = event->account;
187   GtkWidget *info_bar;
188   GtkWidget *content_area;
189   GtkWidget *image;
190   GtkWidget *label;
191   GtkWidget *add_button;
192   GtkWidget *close_button;
193   GtkWidget *action_area;
194   GtkWidget *action_grid;
195   const gchar *icon_name;
196   gchar *str;
197
198   if (g_hash_table_lookup (self->priv->auths, event) != NULL)
199     return;
200
201   info_bar = gtk_info_bar_new ();
202   gtk_info_bar_set_message_type (GTK_INFO_BAR (info_bar), GTK_MESSAGE_QUESTION);
203
204   gtk_widget_set_no_show_all (info_bar, TRUE);
205   gtk_box_pack_start (GTK_BOX (self->priv->auth_vbox), info_bar, FALSE, TRUE, 0);
206   gtk_widget_show (info_bar);
207
208   icon_name = tp_account_get_icon_name (account);
209   image = gtk_image_new_from_icon_name (icon_name, GTK_ICON_SIZE_SMALL_TOOLBAR);
210   gtk_widget_show (image);
211
212   str = g_markup_printf_escaped ("<b>%s</b>\n%s",
213       tp_account_get_display_name (account),
214       _("Password required"));
215
216   label = gtk_label_new (str);
217   gtk_label_set_use_markup (GTK_LABEL (label), TRUE);
218   gtk_label_set_ellipsize (GTK_LABEL (label), PANGO_ELLIPSIZE_END);
219   gtk_misc_set_alignment (GTK_MISC (label), 0, 0.5);
220   gtk_widget_show (label);
221
222   g_free (str);
223
224   content_area = gtk_info_bar_get_content_area (GTK_INFO_BAR (info_bar));
225   gtk_box_pack_start (GTK_BOX (content_area), image, FALSE, FALSE, 0);
226   gtk_box_pack_start (GTK_BOX (content_area), label, TRUE, TRUE, 0);
227
228   image = gtk_image_new_from_stock (GTK_STOCK_ADD, GTK_ICON_SIZE_BUTTON);
229   add_button = gtk_button_new ();
230   gtk_button_set_image (GTK_BUTTON (add_button), image);
231   gtk_widget_set_tooltip_text (add_button, _("Provide Password"));
232   gtk_widget_show (add_button);
233
234   image = gtk_image_new_from_stock (GTK_STOCK_CLOSE, GTK_ICON_SIZE_BUTTON);
235   close_button = gtk_button_new ();
236   gtk_button_set_image (GTK_BUTTON (close_button), image);
237   gtk_widget_set_tooltip_text (close_button, _("Disconnect"));
238   gtk_widget_show (close_button);
239
240   action_grid = gtk_grid_new ();
241   gtk_grid_set_column_spacing (GTK_GRID (action_grid), 6);
242   gtk_widget_show (action_grid);
243
244   action_area = gtk_info_bar_get_action_area (GTK_INFO_BAR (info_bar));
245   gtk_box_pack_start (GTK_BOX (action_area), action_grid, FALSE, FALSE, 0);
246
247   gtk_grid_attach (GTK_GRID (action_grid), add_button, 0, 0, 1, 1);
248   gtk_grid_attach (GTK_GRID (action_grid), close_button, 1, 0, 1, 1);
249
250   g_object_set_data_full (G_OBJECT (info_bar),
251       "event", event, NULL);
252   g_object_set_data_full (G_OBJECT (add_button),
253       "event", event, NULL);
254   g_object_set_data_full (G_OBJECT (close_button),
255       "event", event, NULL);
256
257   g_signal_connect (add_button, "clicked",
258       G_CALLBACK (roster_window_auth_add_clicked_cb), self);
259   g_signal_connect (close_button, "clicked",
260       G_CALLBACK (roster_window_auth_close_clicked_cb), self);
261
262   gtk_widget_show (self->priv->auth_vbox);
263
264   g_hash_table_insert (self->priv->auths, event, info_bar);
265 }
266
267 static FolksIndividual *
268 ensure_individual_for_event (EmpathyEvent *event)
269 {
270   TpContact *contact;
271
272   contact = empathy_contact_get_tp_contact (event->contact);
273   if (contact == NULL)
274     return NULL;
275
276   return empathy_ensure_individual_from_tp_contact (contact);
277 }
278
279 static void
280 roster_window_event_added_cb (EmpathyEventManager *manager,
281     EmpathyEvent *event,
282     EmpathyRosterWindow *self)
283 {
284   if (event->contact)
285     {
286       FolksIndividual *individual;
287
288       individual = ensure_individual_for_event (event);
289       if (individual == NULL)
290         return;
291
292       event->roster_view_id = empathy_roster_view_add_event (self->priv->view,
293           individual, event->icon_name, event);
294
295       g_object_unref (individual);
296     }
297   else if (event->type == EMPATHY_EVENT_TYPE_AUTH)
298     {
299       roster_window_auth_display (self, event);
300     }
301 }
302
303 static void
304 roster_window_event_removed_cb (EmpathyEventManager *manager,
305     EmpathyEvent *event,
306     EmpathyRosterWindow *self)
307 {
308   if (event->type == EMPATHY_EVENT_TYPE_AUTH)
309     {
310       roster_window_remove_auth (self, event);
311       return;
312     }
313
314   empathy_roster_view_remove_event (self->priv->view, event->roster_view_id);
315 }
316
317 static gboolean
318 roster_window_load_events_idle_cb (gpointer user_data)
319 {
320   EmpathyRosterWindow *self = user_data;
321   GSList *l;
322
323   l = empathy_event_manager_get_events (self->priv->event_manager);
324   while (l != NULL)
325     {
326       roster_window_event_added_cb (self->priv->event_manager, l->data,
327           self);
328       l = l->next;
329     }
330
331   return FALSE;
332 }
333
334 static void
335 hide_search_bar (EmpathyRosterWindow *roster_window)
336 {
337   if (TPAW_IS_LIVE_SEARCH (roster_window->priv->search_bar) &&
338       gtk_widget_is_visible (roster_window->priv->search_bar))
339     gtk_widget_hide (roster_window->priv->search_bar);
340 }
341
342 static void
343 individual_activated_cb (EmpathyRosterView *self,
344     FolksIndividual *individual,
345     gpointer user_data)
346 {
347   EmpathyContact *contact;
348
349   contact = empathy_contact_dup_best_for_action (individual,
350       EMPATHY_ACTION_CHAT);
351
352   if (contact == NULL)
353     return;
354
355   DEBUG ("Starting a chat");
356
357   empathy_chat_with_contact (contact, gtk_get_current_event_time ());
358
359   g_object_unref (contact);
360
361   /* Hide the search-bar upon hitting "Enter" on an individual */
362   hide_search_bar (EMPATHY_ROSTER_WINDOW (user_data));
363 }
364
365 static void
366 event_activated_cb (EmpathyRosterView *self,
367     FolksIndividual *individual,
368     EmpathyEvent *event,
369     gpointer user_data)
370 {
371   empathy_event_activate (event);
372
373   /* Hide the search-bar upon an event activation */
374   hide_search_bar (EMPATHY_ROSTER_WINDOW (user_data));
375 }
376
377 static void
378 button_account_settings_clicked_cb (GtkButton *button,
379     EmpathyRosterWindow *self)
380 {
381   empathy_accounts_dialog_show_application (gdk_screen_get_default (),
382       NULL, FALSE, FALSE);
383 }
384
385 static void
386 button_online_clicked_cb (GtkButton *button,
387     EmpathyRosterWindow *self)
388 {
389   EmpathyPresenceManager *mgr;
390
391   mgr = empathy_presence_manager_dup_singleton ();
392
393   empathy_presence_manager_set_state (mgr,
394       TP_CONNECTION_PRESENCE_TYPE_AVAILABLE);
395
396   g_object_unref (mgr);
397 }
398
399 static void
400 button_show_offline_clicked_cb (GtkButton *button,
401     EmpathyRosterWindow *self)
402 {
403   g_settings_set_boolean (self->priv->gsettings_ui,
404       EMPATHY_PREFS_UI_SHOW_OFFLINE, TRUE);
405 }
406
407 static void
408 button_add_contact_clicked_cb (GtkButton *button,
409     EmpathyRosterWindow *self)
410 {
411   empathy_new_individual_dialog_show (GTK_WINDOW (self));
412 }
413
414 typedef enum
415 {
416   PAGE_MESSAGE_FLAG_NONE = 0,
417   PAGE_MESSAGE_FLAG_ACCOUNTS = 1 << 0,
418   PAGE_MESSAGE_FLAG_SPINNER = 1 << 2,
419   PAGE_MESSAGE_FLAG_ONLINE = 1 << 3,
420   PAGE_MESSAGE_FLAG_SHOW_OFFLINE = 1 << 4,
421   PAGE_MESSAGE_FLAG_ADD_CONTACT = 1 << 5,
422 } PageMessageFlags;
423
424 static gboolean
425 can_add_contact (EmpathyRosterWindow *self)
426 {
427   GList *accounts, *l;
428   gboolean result = FALSE;
429
430   accounts = tp_account_manager_dup_valid_accounts (
431       self->priv->account_manager);
432   for (l = accounts; l != NULL && !result; l = g_list_next (l))
433     {
434       TpAccount *account = TP_ACCOUNT (l->data);
435       TpConnection *conn;
436
437       conn = tp_account_get_connection (account);
438       if (conn == NULL)
439         continue;
440
441       if (tp_connection_get_can_change_contact_list (conn))
442         result = TRUE;
443     }
444
445   g_list_free_full (accounts, g_object_unref);
446   return result;
447 }
448
449 static void
450 display_page_message (EmpathyRosterWindow *self,
451     const gchar *msg,
452     PageMessageFlags flags)
453 {
454   if (msg != NULL)
455     {
456       gchar *tmp;
457
458       tmp = g_strdup_printf ("<b><span size='xx-large'>%s</span></b>", msg);
459
460       gtk_label_set_markup (GTK_LABEL (self->priv->no_entry_label), tmp);
461       g_free (tmp);
462
463       gtk_label_set_line_wrap (GTK_LABEL (self->priv->no_entry_label), TRUE);
464       gtk_widget_show (self->priv->no_entry_label);
465     }
466   else
467     {
468       gtk_widget_hide (self->priv->no_entry_label);
469     }
470
471   gtk_widget_set_visible (self->priv->button_account_settings,
472       (flags & PAGE_MESSAGE_FLAG_ACCOUNTS) != 0);
473   gtk_widget_set_visible (self->priv->spinner_loading,
474       (flags & PAGE_MESSAGE_FLAG_SPINNER) != 0);
475   gtk_widget_set_visible (self->priv->button_online,
476       (flags & PAGE_MESSAGE_FLAG_ONLINE) != 0);
477   gtk_widget_set_visible (self->priv->button_show_offline,
478       (flags & PAGE_MESSAGE_FLAG_SHOW_OFFLINE) != 0);
479   gtk_widget_set_visible (self->priv->button_add_contact,
480       (flags & PAGE_MESSAGE_FLAG_ADD_CONTACT) != 0);
481
482   if ((flags & PAGE_MESSAGE_FLAG_ADD_CONTACT) != 0)
483     gtk_widget_set_sensitive (self->priv->button_add_contact,
484         can_add_contact (self));
485
486   gtk_notebook_set_current_page (GTK_NOTEBOOK (self->priv->notebook),
487       PAGE_MESSAGE);
488 }
489
490 static void
491 display_page_no_account (EmpathyRosterWindow *self)
492 {
493   display_page_message (self,
494       _("You need to set up an account to see contacts here."),
495       PAGE_MESSAGE_FLAG_ACCOUNTS);
496 }
497
498 static void
499 display_page_contact_list (EmpathyRosterWindow *self)
500 {
501   if (!empathy_individual_manager_get_contacts_loaded (
502         self->priv->individual_manager))
503     /* We'll display the contact list once we're done loading */
504     return;
505
506   gtk_notebook_set_current_page (GTK_NOTEBOOK (self->priv->notebook),
507       PAGE_CONTACT_LIST);
508 }
509
510 static void
511 roster_window_remove_error (EmpathyRosterWindow *self,
512     TpAccount *account)
513 {
514   GtkWidget *error_widget;
515
516   error_widget = g_hash_table_lookup (self->priv->errors, account);
517   if (error_widget != NULL)
518     {
519       gtk_widget_destroy (error_widget);
520       g_hash_table_remove (self->priv->errors, account);
521     }
522 }
523
524 static void
525 roster_window_account_disabled_cb (TpAccountManager  *manager,
526     TpAccount *account,
527     EmpathyRosterWindow *self)
528 {
529   roster_window_remove_error (self, account);
530 }
531
532 typedef enum
533 {
534   ERROR_RESPONSE_RETRY,
535   ERROR_RESPONSE_EDIT,
536   ERROR_RESPONSE_CLOSE,
537   ERROR_RESPONSE_UPGRADE,
538 } ErrorResponse;
539
540 static void
541 roster_window_error_response_cb (GtkInfoBar *infobar,
542     gint response_id,
543     EmpathyRosterWindow *self)
544 {
545   TpAccount *account;
546
547   account = g_object_get_data (G_OBJECT (infobar), "account");
548
549   switch (response_id)
550     {
551       case ERROR_RESPONSE_RETRY:
552         tp_account_reconnect_async (account, NULL, NULL);
553         break;
554
555       case ERROR_RESPONSE_EDIT:
556         empathy_accounts_dialog_show_application (
557             gtk_widget_get_screen (GTK_WIDGET (infobar)),
558             account, FALSE, FALSE);
559         break;
560
561       case ERROR_RESPONSE_CLOSE:
562         break;
563
564       case ERROR_RESPONSE_UPGRADE:
565         {
566           GtkWidget *dialog;
567
568           dialog = gtk_message_dialog_new (GTK_WINDOW (self),
569               GTK_DIALOG_MODAL, GTK_MESSAGE_ERROR, GTK_BUTTONS_OK,
570               _("Sorry, %s accounts can’t be used until your %s software is updated."),
571               tp_account_get_protocol_name (account),
572               tp_account_get_protocol_name (account));
573
574           g_signal_connect_swapped (dialog, "response",
575               G_CALLBACK (gtk_widget_destroy),
576               dialog);
577
578           gtk_widget_show (dialog);
579         }
580     }
581
582   roster_window_remove_error (self, account);
583 }
584
585 static GtkWidget *
586 roster_window_error_create_info_bar (EmpathyRosterWindow *self,
587     TpAccount *account,
588     GtkMessageType message_type,
589     const gchar *message_markup)
590 {
591   GtkWidget *info_bar;
592   GtkWidget *content_area;
593   GtkWidget *action_area;
594   GtkWidget *label;
595   GtkWidget *image;
596   const gchar *icon_name;
597
598   /* If there are other errors, remove them */
599   roster_window_remove_error (self, account);
600
601   info_bar = gtk_info_bar_new ();
602   gtk_info_bar_set_message_type (GTK_INFO_BAR (info_bar), message_type);
603
604   gtk_widget_set_no_show_all (info_bar, TRUE);
605   gtk_box_pack_start (GTK_BOX (self->priv->errors_vbox), info_bar, FALSE, TRUE, 0);
606   gtk_widget_show (info_bar);
607
608   icon_name = tp_account_get_icon_name (account);
609   image = gtk_image_new_from_icon_name (icon_name, GTK_ICON_SIZE_SMALL_TOOLBAR);
610   gtk_widget_show (image);
611
612   label = gtk_label_new (message_markup);
613   gtk_label_set_use_markup (GTK_LABEL (label), TRUE);
614   gtk_label_set_line_wrap (GTK_LABEL (label), TRUE);
615   gtk_label_set_ellipsize (GTK_LABEL (label), PANGO_ELLIPSIZE_END);
616   gtk_misc_set_alignment (GTK_MISC (label), 0, 0.5);
617   gtk_widget_show (label);
618
619   content_area = gtk_info_bar_get_content_area (GTK_INFO_BAR (info_bar));
620   gtk_box_pack_start (GTK_BOX (content_area), image, FALSE, FALSE, 0);
621   gtk_box_pack_start (GTK_BOX (content_area), label, TRUE, TRUE, 0);
622
623   action_area = gtk_info_bar_get_action_area (GTK_INFO_BAR (info_bar));
624   gtk_orientable_set_orientation (GTK_ORIENTABLE (action_area),
625       GTK_ORIENTATION_HORIZONTAL);
626   gtk_style_context_add_class (gtk_widget_get_style_context (action_area),
627       "empathy-roster-window-error-button");
628
629   g_object_set_data_full (G_OBJECT (info_bar),
630       "account", g_object_ref (account),
631       g_object_unref);
632
633   g_signal_connect (info_bar, "response",
634       G_CALLBACK (roster_window_error_response_cb), self);
635
636   gtk_widget_show (self->priv->errors_vbox);
637
638   g_hash_table_insert (self->priv->errors, g_object_ref (account), info_bar);
639
640   return info_bar;
641 }
642
643 static void
644 roster_window_error_add_stock_button (GtkInfoBar *info_bar,
645     const gchar *stock_id,
646     const gchar *tooltip,
647     ErrorResponse response_id)
648 {
649   GtkWidget *image;
650   GtkWidget *button;
651
652   image = gtk_image_new_from_stock (stock_id, GTK_ICON_SIZE_BUTTON);
653   button = gtk_button_new ();
654   gtk_button_set_image (GTK_BUTTON (button), image);
655   gtk_widget_set_tooltip_text (button, tooltip);
656   gtk_widget_show (button);
657
658   gtk_info_bar_add_action_widget (info_bar, button, response_id);
659 }
660
661 #ifdef HAVE_UOA
662 static const gchar *
663 uoa_account_display_string (TpAccount *account)
664 {
665   const gchar *service;
666
667   service = tp_account_get_service (account);
668
669   /* Use well known service name, if available */
670   if (!tp_strdiff (service, "windows-live"))
671     return _("Windows Live");
672   else if (!tp_strdiff (service, "google-talk"))
673     return _("Google Talk");
674   else if (!tp_strdiff (service, "facebook"))
675     return _("Facebook");
676
677   return tp_account_get_display_name (account);
678 }
679
680 static void
681 roster_window_uoa_auth_error (EmpathyRosterWindow *self,
682     TpAccount *account)
683 {
684   GtkWidget *info_bar;
685   GtkWidget *image;
686   GtkWidget *button;
687   gchar *str;
688
689   /* translators: %s is an account name like 'Facebook' or 'Google Talk' */
690   str = g_strdup_printf (_("%s account requires authorisation"),
691       uoa_account_display_string (account));
692
693   info_bar = roster_window_error_create_info_bar (self, account,
694       GTK_MESSAGE_OTHER, str);
695   g_free (str);
696
697   image = gtk_image_new_from_icon_name ("credentials-preferences",
698       GTK_ICON_SIZE_BUTTON);
699   button = gtk_button_new ();
700   gtk_button_set_image (GTK_BUTTON (button), image);
701   gtk_widget_set_tooltip_text (button, _("Online Accounts"));
702   gtk_widget_show (button);
703
704   gtk_info_bar_add_action_widget (GTK_INFO_BAR (info_bar), button,
705       ERROR_RESPONSE_EDIT);
706 }
707 #endif
708
709 static void
710 roster_window_error_display (EmpathyRosterWindow *self,
711     TpAccount *account)
712 {
713   const gchar *error_message;
714   gboolean user_requested;
715   GtkWidget *info_bar;
716   gchar *str;
717
718   error_message =
719     empathy_account_get_error_message (account, &user_requested);
720
721   if (user_requested)
722     return;
723
724 #ifdef HAVE_UOA
725   if (!tp_strdiff (TP_ERROR_STR_AUTHENTICATION_FAILED,
726        tp_account_get_detailed_error (account, NULL)) &&
727       !tp_strdiff (tp_account_get_storage_provider (account),
728        EMPATHY_UOA_PROVIDER))
729     {
730       roster_window_uoa_auth_error (self, account);
731       return;
732     }
733 #endif
734
735   str = g_markup_printf_escaped ("<b>%s</b>\n%s",
736       tp_account_get_display_name (account), error_message);
737
738   info_bar = roster_window_error_create_info_bar (self, account,
739       GTK_MESSAGE_ERROR, str);
740   g_free (str);
741
742   gtk_widget_set_tooltip_text (self->priv->errors_vbox, error_message);
743
744   if (!tp_strdiff (TP_ERROR_STR_SOFTWARE_UPGRADE_REQUIRED,
745        tp_account_get_detailed_error (account, NULL)))
746     {
747       roster_window_error_add_stock_button (GTK_INFO_BAR (info_bar),
748           GTK_STOCK_REFRESH, _("Update software…"),
749           ERROR_RESPONSE_RETRY);
750     }
751   else
752     {
753       roster_window_error_add_stock_button (GTK_INFO_BAR (info_bar),
754           GTK_STOCK_REFRESH, _("Reconnect"),
755           ERROR_RESPONSE_RETRY);
756
757       roster_window_error_add_stock_button (GTK_INFO_BAR (info_bar),
758           GTK_STOCK_EDIT, _("Edit Account"),
759           ERROR_RESPONSE_EDIT);
760     }
761
762   roster_window_error_add_stock_button (GTK_INFO_BAR (info_bar),
763       GTK_STOCK_CLOSE, _("Close"),
764       ERROR_RESPONSE_CLOSE);
765 }
766
767 static void
768 roster_window_update_status (EmpathyRosterWindow *self)
769 {
770   gboolean connected, connecting;
771   GList *l;
772   GAction *action;
773
774   connected = empathy_account_manager_get_accounts_connected (&connecting);
775
776   /* Update the spinner state */
777   if (connecting)
778     {
779       gtk_spinner_start (GTK_SPINNER (self->priv->throbber));
780       gtk_widget_show (self->priv->throbber);
781     }
782   else
783     {
784       gtk_spinner_stop (GTK_SPINNER (self->priv->throbber));
785       gtk_widget_hide (self->priv->throbber);
786     }
787
788   /* Update widgets sensibility */
789   for (l = self->priv->actions_connected; l; l = l->next)
790     g_simple_action_set_enabled (l->data, connected);
791
792   action = g_action_map_lookup_action (G_ACTION_MAP (self), "chat_add_contact");
793   if (!can_add_contact (self))
794     g_simple_action_set_enabled (G_SIMPLE_ACTION (action), FALSE);
795 }
796
797 static void
798 roster_window_balance_update_balance (EmpathyRosterWindow *self,
799     TpAccount *account)
800 {
801   TpConnection *conn;
802   GtkWidget *label;
803   int amount = 0;
804   guint scale = G_MAXINT32;
805   const gchar *currency = "";
806   char *money;
807
808   conn = tp_account_get_connection (account);
809   if (conn == NULL)
810     return;
811
812   if (!tp_connection_get_balance (conn, &amount, &scale, &currency))
813     return;
814
815   if (amount == 0 &&
816       scale == G_MAXINT32 &&
817       tp_str_empty (currency))
818     {
819       /* unknown balance */
820       money = g_strdup ("--");
821     }
822   else
823     {
824       char *tmp = empathy_format_currency (amount, scale, currency);
825
826       money = g_strdup_printf ("%s %s", currency, tmp);
827       g_free (tmp);
828     }
829
830   /* update the money label in the roster */
831   label = g_object_get_data (G_OBJECT (account), "balance-money-label");
832
833   gtk_label_set_text (GTK_LABEL (label), money);
834   g_free (money);
835 }
836
837 static void
838 roster_window_balance_changed_cb (TpConnection *conn,
839     guint balance,
840     guint scale,
841     const gchar *currency,
842     EmpathyRosterWindow *self)
843 {
844   TpAccount *account;
845
846   account = tp_connection_get_account (conn);
847   if (account == NULL)
848     return;
849
850   roster_window_balance_update_balance (self, account);
851 }
852
853 static void
854 roster_window_setup_balance (EmpathyRosterWindow *self,
855     TpAccount *account)
856 {
857   TpConnection *conn = tp_account_get_connection (account);
858   GtkWidget *hbox, *image, *label;
859   const gchar *uri;
860
861   if (conn == NULL)
862     return;
863
864   if (!tp_proxy_is_prepared (conn, TP_CONNECTION_FEATURE_BALANCE))
865     return;
866
867   DEBUG ("Setting up balance for acct: %s",
868       tp_account_get_display_name (account));
869
870   /* create the display widget */
871   hbox = gtk_box_new (GTK_ORIENTATION_HORIZONTAL, 3);
872   gtk_container_set_border_width (GTK_CONTAINER (hbox), 3);
873
874   /* protocol icon */
875   image = gtk_image_new ();
876   gtk_box_pack_start (GTK_BOX (hbox), image, FALSE, TRUE, 0);
877   g_object_bind_property (account, "icon-name", image, "icon-name",
878       G_BINDING_SYNC_CREATE);
879
880   /* account name label */
881   label = gtk_label_new ("");
882   gtk_misc_set_alignment (GTK_MISC (label), 0.0, 0.5);
883   gtk_label_set_ellipsize (GTK_LABEL (label), PANGO_ELLIPSIZE_END);
884   gtk_box_pack_start (GTK_BOX (hbox), label, TRUE, TRUE, 0);
885   g_object_bind_property (account, "display-name", label, "label",
886       G_BINDING_SYNC_CREATE);
887
888   /* balance label */
889   label = gtk_label_new ("");
890   gtk_misc_set_alignment (GTK_MISC (label), 0.0, 0.5);
891   gtk_box_pack_start (GTK_BOX (hbox), label, FALSE, TRUE, 0);
892
893   /* top up button */
894   uri = tp_connection_get_balance_uri (conn);
895
896   if (!tp_str_empty (uri))
897     {
898       GtkWidget *button;
899
900       button = gtk_button_new ();
901       gtk_container_add (GTK_CONTAINER (button),
902           gtk_image_new_from_icon_name ("emblem-symbolic-link",
903             GTK_ICON_SIZE_SMALL_TOOLBAR));
904       gtk_button_set_relief (GTK_BUTTON (button), GTK_RELIEF_NONE);
905       gtk_widget_set_tooltip_text (button, _("Top up account"));
906       gtk_box_pack_start (GTK_BOX (hbox), button, FALSE, TRUE, 0);
907
908       g_signal_connect_data (button, "clicked",
909           G_CALLBACK (empathy_url_show),
910           g_strdup (uri), (GClosureNotify) g_free,
911           0);
912     }
913
914   gtk_box_pack_start (GTK_BOX (self->priv->balance_vbox), hbox, FALSE, TRUE, 0);
915   gtk_widget_show_all (hbox);
916
917   g_object_set_data (G_OBJECT (account), "balance-money-label", label);
918   g_object_set_data (G_OBJECT (account), "balance-money-hbox", hbox);
919
920   roster_window_balance_update_balance (self, account);
921
922   g_signal_connect (conn, "balance-changed",
923       G_CALLBACK (roster_window_balance_changed_cb), self);
924 }
925
926 static void
927 roster_window_remove_balance_action (EmpathyRosterWindow *self,
928     TpAccount *account)
929 {
930   GtkWidget *hbox =
931     g_object_get_data (G_OBJECT (account), "balance-money-hbox");
932
933   if (hbox == NULL)
934     return;
935
936   g_return_if_fail (GTK_IS_BOX (hbox));
937
938   gtk_widget_destroy (hbox);
939 }
940
941 static void set_notebook_page (EmpathyRosterWindow *self);
942
943 static void
944 roster_window_connection_changed_cb (TpAccount  *account,
945     guint old_status,
946     guint current,
947     guint reason,
948     gchar *dbus_error_name,
949     GHashTable *details,
950     EmpathyRosterWindow *self)
951 {
952   roster_window_update_status (self);
953   set_notebook_page (self);
954
955   if (current == TP_CONNECTION_STATUS_DISCONNECTED &&
956       reason != TP_CONNECTION_STATUS_REASON_REQUESTED)
957     {
958       roster_window_error_display (self, account);
959     }
960
961   if (current == TP_CONNECTION_STATUS_DISCONNECTED)
962     {
963       empathy_sound_manager_play (self->priv->sound_mgr, GTK_WIDGET (self),
964               EMPATHY_SOUND_ACCOUNT_DISCONNECTED);
965     }
966
967   if (current == TP_CONNECTION_STATUS_CONNECTED)
968     {
969       empathy_sound_manager_play (self->priv->sound_mgr, GTK_WIDGET (self),
970               EMPATHY_SOUND_ACCOUNT_CONNECTED);
971
972       /* Account connected without error, remove error message if any */
973       roster_window_remove_error (self, account);
974     }
975 }
976
977 static void
978 roster_window_accels_load (void)
979 {
980   gchar *filename;
981
982   filename = g_build_filename (g_get_user_config_dir (),
983       PACKAGE_NAME, ACCELS_FILENAME, NULL);
984   if (g_file_test (filename, G_FILE_TEST_EXISTS))
985     {
986       DEBUG ("Loading from:'%s'", filename);
987       gtk_accel_map_load (filename);
988     }
989
990   g_free (filename);
991 }
992
993 static void
994 roster_window_accels_save (void)
995 {
996   gchar *dir;
997   gchar *file_with_path;
998
999   dir = g_build_filename (g_get_user_config_dir (), PACKAGE_NAME, NULL);
1000   g_mkdir_with_parents (dir, S_IRUSR | S_IWUSR | S_IXUSR);
1001   file_with_path = g_build_filename (dir, ACCELS_FILENAME, NULL);
1002   g_free (dir);
1003
1004   DEBUG ("Saving to:'%s'", file_with_path);
1005   gtk_accel_map_save (file_with_path);
1006
1007   g_free (file_with_path);
1008 }
1009
1010 static void
1011 empathy_roster_window_finalize (GObject *window)
1012 {
1013   EmpathyRosterWindow *self = EMPATHY_ROSTER_WINDOW (window);
1014   GHashTableIter iter;
1015   gpointer key, value;
1016
1017   /* Save user-defined accelerators. */
1018   roster_window_accels_save ();
1019
1020   g_list_free (self->priv->actions_connected);
1021
1022   g_object_unref (self->priv->account_manager);
1023   g_object_unref (self->priv->sound_mgr);
1024   g_hash_table_unref (self->priv->errors);
1025   g_hash_table_unref (self->priv->auths);
1026
1027   /* disconnect all handlers of status-changed signal */
1028   g_hash_table_iter_init (&iter, self->priv->status_changed_handlers);
1029   while (g_hash_table_iter_next (&iter, &key, &value))
1030     g_signal_handler_disconnect (TP_ACCOUNT (key), GPOINTER_TO_UINT (value));
1031
1032   g_hash_table_unref (self->priv->status_changed_handlers);
1033
1034   g_object_unref (self->priv->call_observer);
1035   g_object_unref (self->priv->event_manager);
1036   g_object_unref (self->priv->chatroom_manager);
1037
1038   g_object_unref (self->priv->gsettings_ui);
1039   g_object_unref (self->priv->individual_manager);
1040
1041   g_object_unref (self->priv->menumodel);
1042   g_object_unref (self->priv->rooms_section);
1043
1044   g_clear_object (&self->priv->tooltip_widget);
1045
1046   G_OBJECT_CLASS (empathy_roster_window_parent_class)->finalize (window);
1047 }
1048
1049 static gboolean
1050 roster_window_key_press_event_cb  (GtkWidget *window,
1051     GdkEventKey *event,
1052     EmpathyRosterWindow *self)
1053 {
1054   if (event->keyval == GDK_KEY_T
1055       && event->state & GDK_SHIFT_MASK
1056       && event->state & GDK_CONTROL_MASK)
1057     empathy_chat_manager_call_undo_closed_chat ();
1058
1059   if (event->keyval == GDK_KEY_f
1060       && event->state & GDK_CONTROL_MASK)
1061     gtk_widget_show (self->priv->search_bar);
1062
1063   return FALSE;
1064 }
1065
1066 static void
1067 unprepare_cb (GObject *source,
1068     GAsyncResult *result,
1069     gpointer user_data)
1070 {
1071   GtkWidget *self = user_data;
1072
1073   gtk_widget_destroy (self);
1074 }
1075
1076 static void
1077 roster_window_chat_quit_cb (GSimpleAction *action,
1078     GVariant *parameter,
1079     gpointer user_data)
1080 {
1081   EmpathyRosterWindow *self = user_data;
1082
1083   /* Destroying the window will make us leave the main loop and so exit the
1084    * process. Before doing so we want to unprepare the individual manager.
1085    * Just hide the window now and actually destroy it once Folks is done.
1086    */
1087   gtk_widget_hide (GTK_WIDGET (self));
1088
1089   empathy_individual_manager_unprepare_async (self->priv->individual_manager,
1090       unprepare_cb, self);
1091 }
1092
1093 static void
1094 roster_window_view_history_cb (GSimpleAction *action,
1095     GVariant *parameter,
1096     gpointer user_data)
1097 {
1098   EmpathyRosterWindow *self = user_data;
1099
1100   empathy_log_window_show (NULL, NULL, FALSE, GTK_WINDOW (self));
1101 }
1102
1103 static void
1104 roster_window_chat_new_message_cb (GSimpleAction *action,
1105     GVariant *parameter,
1106     gpointer user_data)
1107 {
1108   EmpathyRosterWindow *self = user_data;
1109
1110   empathy_new_message_dialog_show (GTK_WINDOW (self));
1111 }
1112
1113 static void
1114 roster_window_chat_new_call_cb (GSimpleAction *action,
1115     GVariant *parameter,
1116     gpointer user_data)
1117 {
1118   EmpathyRosterWindow *self = user_data;
1119
1120   empathy_new_call_dialog_show (GTK_WINDOW (self));
1121 }
1122
1123 static void
1124 roster_window_chat_add_contact_cb (GSimpleAction *action,
1125     GVariant *parameter,
1126     gpointer user_data)
1127 {
1128   EmpathyRosterWindow *self = user_data;
1129
1130   empathy_new_individual_dialog_show (GTK_WINDOW (self));
1131 }
1132
1133 static void
1134 roster_window_chat_search_contacts_cb (GSimpleAction *action,
1135     GVariant *parameter,
1136     gpointer user_data)
1137 {
1138   EmpathyRosterWindow *self = user_data;
1139   GtkWidget *dialog;
1140
1141   dialog = empathy_contact_search_dialog_new (
1142       GTK_WINDOW (self));
1143
1144   gtk_widget_show (dialog);
1145 }
1146
1147 static void
1148 roster_window_view_show_ft_manager (GSimpleAction *action,
1149     GVariant *parameter,
1150     gpointer user_data)
1151 {
1152   empathy_ft_manager_show ();
1153 }
1154
1155 static void
1156 join_chatroom (EmpathyChatroom *chatroom,
1157     gint64 timestamp)
1158 {
1159   TpAccount *account;
1160   const gchar *room;
1161
1162   account = empathy_chatroom_get_account (chatroom);
1163   room = empathy_chatroom_get_room (chatroom);
1164
1165   DEBUG ("Requesting channel for '%s'", room);
1166   empathy_join_muc (account, room, timestamp);
1167 }
1168
1169 typedef struct
1170 {
1171   TpAccount *account;
1172   EmpathyChatroom *chatroom;
1173   gint64 timestamp;
1174   glong sig_id;
1175   guint timeout;
1176 } join_fav_account_sig_ctx;
1177
1178 static join_fav_account_sig_ctx *
1179 join_fav_account_sig_ctx_new (TpAccount *account,
1180     EmpathyChatroom *chatroom,
1181     gint64 timestamp)
1182 {
1183   join_fav_account_sig_ctx *ctx = g_slice_new0 (
1184       join_fav_account_sig_ctx);
1185
1186   ctx->account = g_object_ref (account);
1187   ctx->chatroom = g_object_ref (chatroom);
1188   ctx->timestamp = timestamp;
1189   return ctx;
1190 }
1191
1192 static void
1193 join_fav_account_sig_ctx_free (join_fav_account_sig_ctx *ctx)
1194 {
1195   g_object_unref (ctx->account);
1196   g_object_unref (ctx->chatroom);
1197   g_slice_free (join_fav_account_sig_ctx, ctx);
1198 }
1199
1200 static void
1201 account_status_changed_cb (TpAccount  *account,
1202     TpConnectionStatus old_status,
1203     TpConnectionStatus new_status,
1204     guint reason,
1205     gchar *dbus_error_name,
1206     GHashTable *details,
1207     gpointer user_data)
1208 {
1209   join_fav_account_sig_ctx *ctx = user_data;
1210
1211   switch (new_status)
1212     {
1213       case TP_CONNECTION_STATUS_DISCONNECTED:
1214         /* Don't wait any longer */
1215         goto finally;
1216         break;
1217
1218       case TP_CONNECTION_STATUS_CONNECTING:
1219         /* Wait a bit */
1220         return;
1221
1222       case TP_CONNECTION_STATUS_CONNECTED:
1223         /* We can join the room */
1224         break;
1225
1226       default:
1227         g_assert_not_reached ();
1228     }
1229
1230   join_chatroom (ctx->chatroom, ctx->timestamp);
1231
1232 finally:
1233   g_source_remove (ctx->timeout);
1234   g_signal_handler_disconnect (account, ctx->sig_id);
1235 }
1236
1237 #define JOIN_FAVORITE_TIMEOUT 5
1238
1239 static gboolean
1240 join_favorite_timeout_cb (gpointer data)
1241 {
1242   join_fav_account_sig_ctx *ctx = data;
1243
1244   /* stop waiting for joining the favorite room */
1245   g_signal_handler_disconnect (ctx->account, ctx->sig_id);
1246   return FALSE;
1247 }
1248
1249 static void
1250 roster_window_favorite_chatroom_join (EmpathyChatroom *chatroom)
1251 {
1252   TpAccount *account;
1253
1254   account = empathy_chatroom_get_account (chatroom);
1255   if (tp_account_get_connection_status (account, NULL) !=
1256                TP_CONNECTION_STATUS_CONNECTED)
1257     {
1258       join_fav_account_sig_ctx *ctx;
1259
1260       ctx = join_fav_account_sig_ctx_new (account, chatroom,
1261         empathy_get_current_action_time ());
1262
1263       ctx->sig_id = g_signal_connect_data (account, "status-changed",
1264         G_CALLBACK (account_status_changed_cb), ctx,
1265         (GClosureNotify) join_fav_account_sig_ctx_free, 0);
1266
1267       ctx->timeout = g_timeout_add_seconds (JOIN_FAVORITE_TIMEOUT,
1268         join_favorite_timeout_cb, ctx);
1269       return;
1270     }
1271
1272   join_chatroom (chatroom, empathy_get_current_action_time ());
1273 }
1274
1275 static void
1276 roster_window_join_chatroom_menu_activate_cb (GSimpleAction *action,
1277     GVariant *parameter,
1278     gpointer user_data)
1279 {
1280   EmpathyRosterWindow *self = user_data;
1281   const gchar *room, *path;
1282   EmpathyClientFactory *factory;
1283   TpAccount *account;
1284   GError *error = NULL;
1285   EmpathyChatroom *chatroom;
1286
1287   g_variant_get (parameter, "(&s&s)", &room, &path);
1288
1289   factory = empathy_client_factory_dup ();
1290
1291   account = tp_simple_client_factory_ensure_account (
1292       TP_SIMPLE_CLIENT_FACTORY (factory), path, NULL, &error);
1293   if (account == NULL)
1294     {
1295       DEBUG ("Failed to get account '%s': %s", path, error->message);
1296       g_error_free (error);
1297       goto out;
1298     }
1299
1300   chatroom = empathy_chatroom_manager_find (self->priv->chatroom_manager,
1301       account, room);
1302   if (chatroom == NULL)
1303     {
1304       DEBUG ("Failed to get chatroom '%s' on '%s'",
1305           room, path);
1306       goto out;
1307     }
1308
1309   roster_window_favorite_chatroom_join (chatroom);
1310
1311 out:
1312   g_object_unref (factory);
1313 }
1314
1315 static void
1316 roster_window_favorite_chatroom_menu_add (EmpathyRosterWindow *self,
1317     EmpathyChatroom *chatroom)
1318 {
1319   GMenuItem *item;
1320   const gchar *name, *account_name, *account_path;
1321   TpAccount *account;
1322   gchar *label;
1323
1324   account = empathy_chatroom_get_account (chatroom);
1325
1326   name = empathy_chatroom_get_name (chatroom);
1327   account_name = tp_account_get_display_name (account);
1328   account_path = tp_proxy_get_object_path (account);
1329
1330   label = g_strdup_printf ("%s (%s)", name, account_name);
1331
1332   item = g_menu_item_new (label, NULL);
1333   g_menu_item_set_action_and_target (item, "win.join", "(ss)",
1334       name, account_path);
1335   g_menu_item_set_attribute (item, "room-name", "s", name);
1336   g_menu_item_set_attribute (item, "account-path", "s", account_path);
1337   g_menu_append_item (self->priv->rooms_section, item);
1338
1339   g_free (label);
1340 }
1341
1342 static void
1343 roster_window_favorite_chatroom_menu_added_cb (EmpathyChatroomManager *manager,
1344     EmpathyChatroom *chatroom,
1345     EmpathyRosterWindow *self)
1346 {
1347   roster_window_favorite_chatroom_menu_add (self, chatroom);
1348 }
1349
1350 static void
1351 roster_window_favorite_chatroom_menu_removed_cb (
1352     EmpathyChatroomManager *manager,
1353     EmpathyChatroom *chatroom,
1354     EmpathyRosterWindow *self)
1355 {
1356   GList *chatrooms;
1357   guint i, n;
1358   TpAccount *account;
1359   const gchar *account_path;
1360
1361   account = empathy_chatroom_get_account (chatroom);
1362   account_path = tp_proxy_get_object_path (account);
1363
1364   n = g_menu_model_get_n_items (G_MENU_MODEL (self->priv->rooms_section));
1365
1366   for (i = 0; i < n; i++)
1367     {
1368       gchar *tmp;
1369
1370       if (!g_menu_model_get_item_attribute (
1371             G_MENU_MODEL (self->priv->rooms_section), i,
1372             "room-name", "s", &tmp))
1373         continue;
1374
1375       if (tp_strdiff (tmp, empathy_chatroom_get_name (chatroom)))
1376         {
1377           g_free (tmp);
1378           continue;
1379         }
1380
1381       g_free (tmp);
1382
1383       if (!g_menu_model_get_item_attribute (
1384             G_MENU_MODEL (self->priv->rooms_section), i,
1385             "account-path", "s", &tmp))
1386         continue;
1387
1388       if (tp_strdiff (tmp, account_path))
1389         {
1390           g_free (tmp);
1391           continue;
1392         }
1393
1394       g_menu_remove (self->priv->rooms_section, i);
1395       g_free (tmp);
1396       break;
1397     }
1398
1399   chatrooms = empathy_chatroom_manager_get_chatrooms (
1400       self->priv->chatroom_manager, NULL);
1401
1402   g_list_free (chatrooms);
1403 }
1404
1405 static void
1406 roster_window_favorite_chatroom_menu_setup (EmpathyRosterWindow *self)
1407 {
1408   GList *chatrooms, *l;
1409
1410   self->priv->chatroom_manager = empathy_chatroom_manager_dup_singleton (NULL);
1411
1412   chatrooms = empathy_chatroom_manager_get_chatrooms (
1413     self->priv->chatroom_manager, NULL);
1414
1415   for (l = chatrooms; l; l = l->next)
1416     roster_window_favorite_chatroom_menu_add (self, l->data);
1417
1418   g_signal_connect (self->priv->chatroom_manager, "chatroom-added",
1419       G_CALLBACK (roster_window_favorite_chatroom_menu_added_cb),
1420       self);
1421
1422   g_signal_connect (self->priv->chatroom_manager, "chatroom-removed",
1423       G_CALLBACK (roster_window_favorite_chatroom_menu_removed_cb),
1424       self);
1425
1426   g_list_free (chatrooms);
1427 }
1428
1429 static void
1430 roster_window_room_join_new_cb (GSimpleAction *action,
1431     GVariant *parameter,
1432     gpointer user_data)
1433 {
1434   EmpathyRosterWindow *self = user_data;
1435
1436   empathy_new_chatroom_dialog_show (GTK_WINDOW (self));
1437 }
1438
1439 static void
1440 roster_window_room_join_favorites_cb (GSimpleAction *action,
1441     GVariant *parameter,
1442     gpointer user_data)
1443 {
1444   EmpathyRosterWindow *self = user_data;
1445   GList *chatrooms, *l;
1446
1447   chatrooms = empathy_chatroom_manager_get_chatrooms (self->priv->chatroom_manager,
1448       NULL);
1449
1450   for (l = chatrooms; l; l = l->next)
1451     roster_window_favorite_chatroom_join (l->data);
1452
1453   g_list_free (chatrooms);
1454 }
1455
1456 static void
1457 roster_window_room_manage_favorites_cb (GSimpleAction *action,
1458     GVariant *parameter,
1459     gpointer user_data)
1460 {
1461   EmpathyRosterWindow *self = user_data;
1462
1463   empathy_chatrooms_window_show (GTK_WINDOW (self));
1464 }
1465
1466 static void
1467 roster_window_edit_accounts_cb (GSimpleAction *action,
1468     GVariant *parameter,
1469     gpointer user_data)
1470 {
1471   empathy_accounts_dialog_show_application (gdk_screen_get_default (),
1472       NULL, FALSE, FALSE);
1473 }
1474
1475 static void
1476 roster_window_edit_blocked_contacts_cb (GSimpleAction *action,
1477     GVariant *parameter,
1478     gpointer user_data)
1479 {
1480   EmpathyRosterWindow *self = user_data;
1481   GtkWidget *dialog;
1482
1483   dialog = empathy_contact_blocking_dialog_new (GTK_WINDOW (self));
1484   gtk_widget_show (dialog);
1485
1486   g_signal_connect (dialog, "response",
1487       G_CALLBACK (gtk_widget_destroy), NULL);
1488 }
1489
1490 void
1491 empathy_roster_window_show_preferences (EmpathyRosterWindow *self,
1492     const gchar *tab)
1493 {
1494   if (self->priv->preferences == NULL)
1495     {
1496       self->priv->preferences = empathy_preferences_new (GTK_WINDOW (self),
1497                                                    self->priv->shell_running);
1498       g_object_add_weak_pointer (G_OBJECT (self->priv->preferences),
1499                (gpointer) &self->priv->preferences);
1500
1501       gtk_widget_show (self->priv->preferences);
1502     }
1503   else
1504     {
1505       gtk_window_present (GTK_WINDOW (self->priv->preferences));
1506     }
1507
1508   if (tab != NULL)
1509     empathy_preferences_show_tab (
1510       EMPATHY_PREFERENCES (self->priv->preferences), tab);
1511 }
1512
1513 static void
1514 roster_window_edit_preferences_cb (GSimpleAction *action,
1515     GVariant *parameter,
1516     gpointer user_data)
1517 {
1518   EmpathyRosterWindow *self = user_data;
1519
1520   empathy_roster_window_show_preferences (self, NULL);
1521 }
1522
1523 static void
1524 roster_window_help_about_cb (GSimpleAction *action,
1525     GVariant *parameter,
1526     gpointer user_data)
1527 {
1528   EmpathyRosterWindow *self = user_data;
1529
1530   empathy_about_dialog_new (GTK_WINDOW (self));
1531 }
1532
1533 static void
1534 roster_window_help_contents_cb (GSimpleAction *action,
1535     GVariant *parameter,
1536     gpointer user_data)
1537 {
1538   EmpathyRosterWindow *self = user_data;
1539
1540   empathy_url_show (GTK_WIDGET (self), "help:empathy");
1541 }
1542
1543 static gboolean
1544 roster_window_throbber_button_press_event_cb (GtkWidget *throbber,
1545     GdkEventButton *event,
1546     EmpathyRosterWindow *self)
1547 {
1548   if (event->type != GDK_BUTTON_PRESS ||
1549       event->button != 1)
1550     return FALSE;
1551
1552   empathy_accounts_dialog_show_application (
1553       gtk_widget_get_screen (GTK_WIDGET (throbber)),
1554       NULL, FALSE, FALSE);
1555
1556   return FALSE;
1557 }
1558
1559 static void
1560 roster_window_account_removed_cb (TpAccountManager  *manager,
1561     TpAccount *account,
1562     EmpathyRosterWindow *self)
1563 {
1564   /* remove errors if any */
1565   roster_window_remove_error (self, account);
1566
1567   /* remove the balance action if required */
1568   roster_window_remove_balance_action (self, account);
1569 }
1570
1571 static void
1572 account_connection_notify_cb (TpAccount *account,
1573     GParamSpec *spec,
1574     EmpathyRosterWindow *self)
1575 {
1576   TpConnection *conn;
1577
1578   conn = tp_account_get_connection (account);
1579
1580   if (conn != NULL)
1581     {
1582       roster_window_setup_balance (self, account);
1583     }
1584   else
1585     {
1586       /* remove balance action if required */
1587       roster_window_remove_balance_action (self, account);
1588     }
1589 }
1590
1591 static void
1592 add_account (EmpathyRosterWindow *self,
1593     TpAccount *account)
1594 {
1595   gulong handler_id;
1596
1597   handler_id = GPOINTER_TO_UINT (g_hash_table_lookup (
1598     self->priv->status_changed_handlers, account));
1599
1600   /* connect signal only if it was not connected yet */
1601   if (handler_id != 0)
1602     return;
1603
1604   handler_id = g_signal_connect (account, "status-changed",
1605     G_CALLBACK (roster_window_connection_changed_cb), self);
1606
1607   g_hash_table_insert (self->priv->status_changed_handlers,
1608     account, GUINT_TO_POINTER (handler_id));
1609
1610   /* roster_window_setup_balance() relies on the TpConnection to be ready on
1611    * the TpAccount so we connect this signal as well. */
1612   tp_g_signal_connect_object (account, "notify::connection",
1613       G_CALLBACK (account_connection_notify_cb), self, 0);
1614
1615   roster_window_setup_balance (self, account);
1616 }
1617
1618 /* @account: if not %NULL, the only account which can be enabled */
1619 static void
1620 display_page_account_not_enabled (EmpathyRosterWindow *self,
1621     TpAccount *account)
1622 {
1623   if (account == NULL)
1624     {
1625       display_page_message (self,
1626           _("You need to enable one of your accounts to see contacts here."),
1627           PAGE_MESSAGE_FLAG_ACCOUNTS);
1628     }
1629   else
1630     {
1631       gchar *tmp;
1632
1633       /* translators: argument is an account name */
1634       tmp = g_strdup_printf (_("You need to enable %s to see contacts here."),
1635           tp_account_get_display_name (account));
1636
1637       display_page_message (self, tmp, PAGE_MESSAGE_FLAG_ACCOUNTS);
1638       g_free (tmp);
1639     }
1640 }
1641 static gboolean
1642 has_enabled_account (GList *accounts)
1643 {
1644   GList *l;
1645
1646   for (l = accounts; l != NULL; l = g_list_next (l))
1647     {
1648       TpAccount *account = l->data;
1649
1650       if (tp_account_is_enabled (account))
1651         return TRUE;
1652     }
1653
1654   return FALSE;
1655 }
1656
1657 static void
1658 set_notebook_page (EmpathyRosterWindow *self)
1659 {
1660   GList *accounts;
1661   guint len;
1662   TpConnectionPresenceType presence;
1663   gboolean connected, connecting;
1664
1665   connected = empathy_account_manager_get_accounts_connected (&connecting);
1666
1667   /* Display the loading page if either:
1668    * - We are fetching contacts from Folks (startup)
1669    * - There is no account connected but at least one is connecting
1670    */
1671   if (!empathy_individual_manager_get_contacts_loaded (
1672         self->priv->individual_manager) ||
1673       (!connected && connecting))
1674     {
1675       display_page_message (self, NULL, PAGE_MESSAGE_FLAG_SPINNER);
1676       gtk_spinner_start (GTK_SPINNER (self->priv->spinner_loading));
1677       return;
1678     }
1679
1680   gtk_spinner_stop (GTK_SPINNER (self->priv->spinner_loading));
1681
1682   accounts = tp_account_manager_dup_valid_accounts (
1683       self->priv->account_manager);
1684
1685   len = g_list_length (accounts);
1686
1687   if (len == 0)
1688     {
1689       /* No account */
1690       display_page_no_account (self);
1691       goto out;
1692     }
1693
1694   if (!has_enabled_account (accounts))
1695     {
1696       TpAccount *account = NULL;
1697
1698       /* Pass the account if there is only one which can be enabled */
1699       if (len == 1)
1700         account = accounts->data;
1701
1702       display_page_account_not_enabled (self, account);
1703       goto out;
1704     }
1705
1706   presence = tp_account_manager_get_most_available_presence (
1707       self->priv->account_manager, NULL, NULL);
1708
1709   if (presence == TP_CONNECTION_PRESENCE_TYPE_OFFLINE)
1710     {
1711       display_page_message (self,
1712           _("Change your presence to see contacts here"),
1713           PAGE_MESSAGE_FLAG_ONLINE);
1714       goto out;
1715     }
1716
1717   if (empathy_roster_view_is_empty (self->priv->view))
1718     {
1719       if (empathy_roster_view_is_searching (self->priv->view))
1720         {
1721           display_page_message (self, _("No match found"),
1722               PAGE_MESSAGE_FLAG_NONE);
1723         }
1724       else
1725         {
1726           if (g_settings_get_boolean (self->priv->gsettings_ui,
1727                 EMPATHY_PREFS_UI_SHOW_OFFLINE))
1728             display_page_message (self, _("You haven't added any contacts yet"),
1729                 PAGE_MESSAGE_FLAG_ADD_CONTACT);
1730           else
1731             display_page_message (self, _("No online contacts"),
1732                 PAGE_MESSAGE_FLAG_SHOW_OFFLINE);
1733         }
1734       goto out;
1735     }
1736
1737   display_page_contact_list (self);
1738
1739 out:
1740   g_list_free_full (accounts, g_object_unref);
1741 }
1742
1743 static void
1744 roster_window_account_validity_changed_cb (TpAccountManager  *manager,
1745     TpAccount *account,
1746     gboolean valid,
1747     EmpathyRosterWindow *self)
1748 {
1749   if (valid)
1750     add_account (self, account);
1751   else
1752     roster_window_account_removed_cb (manager, account, self);
1753
1754   set_notebook_page (self);
1755 }
1756
1757 static void
1758 roster_window_connection_items_setup (EmpathyRosterWindow *self)
1759 {
1760   guint i;
1761   const gchar *actions_connected[] = {
1762       "room_join_new",
1763       "room_join_favorites",
1764       "chat_new_message",
1765       "chat_new_call",
1766       "chat_search_contacts",
1767       "chat_add_contact",
1768       "edit_blocked_contacts",
1769   };
1770
1771   for (i = 0; i < G_N_ELEMENTS (actions_connected); i++)
1772     {
1773       GAction *action;
1774
1775       action = g_action_map_lookup_action (G_ACTION_MAP (self),
1776           actions_connected[i]);
1777
1778       self->priv->actions_connected = g_list_prepend (
1779           self->priv->actions_connected, action);
1780     }
1781 }
1782
1783 static void
1784 account_enabled_cb (TpAccountManager *manager,
1785     TpAccount *account,
1786     EmpathyRosterWindow *self)
1787 {
1788   set_notebook_page (self);
1789 }
1790
1791 static void
1792 account_disabled_cb (TpAccountManager *manager,
1793     TpAccount *account,
1794     EmpathyRosterWindow *self)
1795 {
1796   set_notebook_page (self);
1797 }
1798
1799 static void
1800 account_removed_cb (TpAccountManager *manager,
1801     TpAccount *account,
1802     EmpathyRosterWindow *self)
1803 {
1804   set_notebook_page (self);
1805 }
1806
1807 static void
1808 account_manager_prepared_cb (GObject *source_object,
1809     GAsyncResult *result,
1810     gpointer user_data)
1811 {
1812   GList *accounts, *j;
1813   TpAccountManager *manager = TP_ACCOUNT_MANAGER (source_object);
1814   EmpathyRosterWindow *self = user_data;
1815   GError *error = NULL;
1816
1817   if (!tp_proxy_prepare_finish (manager, result, &error))
1818     {
1819       DEBUG ("Failed to prepare account manager: %s", error->message);
1820       g_error_free (error);
1821       return;
1822     }
1823
1824   accounts = tp_account_manager_dup_valid_accounts (
1825       self->priv->account_manager);
1826   for (j = accounts; j != NULL; j = j->next)
1827     {
1828       TpAccount *account = TP_ACCOUNT (j->data);
1829
1830       add_account (self, account);
1831     }
1832
1833   g_signal_connect (manager, "account-validity-changed",
1834       G_CALLBACK (roster_window_account_validity_changed_cb), self);
1835   tp_g_signal_connect_object (manager, "account-removed",
1836       G_CALLBACK (account_removed_cb), self, 0);
1837   tp_g_signal_connect_object (manager, "account-disabled",
1838       G_CALLBACK (account_disabled_cb), self, 0);
1839   tp_g_signal_connect_object (manager, "account-enabled",
1840       G_CALLBACK (account_enabled_cb), self, 0);
1841
1842   roster_window_update_status (self);
1843
1844   set_notebook_page (self);
1845
1846   g_list_free_full (accounts, g_object_unref);
1847 }
1848
1849 void
1850 empathy_roster_window_set_shell_running (EmpathyRosterWindow *self,
1851     gboolean shell_running)
1852 {
1853   if (self->priv->shell_running == shell_running)
1854     return;
1855
1856   self->priv->shell_running = shell_running;
1857   g_object_notify (G_OBJECT (self), "shell-running");
1858 }
1859
1860 static GObject *
1861 empathy_roster_window_constructor (GType type,
1862     guint n_construct_params,
1863     GObjectConstructParam *construct_params)
1864 {
1865   static GObject *window = NULL;
1866
1867   if (window != NULL)
1868     return g_object_ref (window);
1869
1870   window = G_OBJECT_CLASS (empathy_roster_window_parent_class)->constructor (
1871     type, n_construct_params, construct_params);
1872
1873   g_object_add_weak_pointer (window, (gpointer) &window);
1874
1875   return window;
1876 }
1877
1878 static GActionEntry menubar_entries[] = {
1879   { "chat_new_message", roster_window_chat_new_message_cb, NULL, NULL, NULL },
1880   { "chat_new_call", roster_window_chat_new_call_cb, NULL, NULL, NULL },
1881   { "chat_add_contact", roster_window_chat_add_contact_cb, NULL, NULL, NULL },
1882   { "chat_search_contacts", roster_window_chat_search_contacts_cb, NULL, NULL, NULL },
1883   { "chat_quit", roster_window_chat_quit_cb, NULL, NULL, NULL },
1884
1885   { "edit_accounts", roster_window_edit_accounts_cb, NULL, NULL, NULL },
1886   { "edit_blocked_contacts", roster_window_edit_blocked_contacts_cb, NULL, NULL, NULL },
1887   { "edit_preferences", roster_window_edit_preferences_cb, NULL, NULL, NULL },
1888
1889   { "view_history", roster_window_view_history_cb, NULL, NULL, NULL },
1890   { "view_show_ft_manager", roster_window_view_show_ft_manager, NULL, NULL, NULL },
1891
1892   { "room_join_new", roster_window_room_join_new_cb, NULL, NULL, NULL },
1893   { "room_join_favorites", roster_window_room_join_favorites_cb, NULL, NULL, NULL },
1894   { "join", roster_window_join_chatroom_menu_activate_cb, "(ss)", NULL, NULL },
1895   { "room_manage_favorites", roster_window_room_manage_favorites_cb, NULL, NULL, NULL },
1896
1897   { "help_contents", roster_window_help_contents_cb, NULL, NULL, NULL },
1898   { "help_about", roster_window_help_about_cb, NULL, NULL, NULL },
1899 };
1900
1901 static void
1902 empathy_roster_window_set_property (GObject *object,
1903     guint property_id,
1904     const GValue *value,
1905     GParamSpec *pspec)
1906 {
1907   EmpathyRosterWindow *self = EMPATHY_ROSTER_WINDOW (object);
1908
1909   switch (property_id)
1910     {
1911       case PROP_SHELL_RUNNING:
1912         self->priv->shell_running = g_value_get_boolean (value);
1913         break;
1914       default:
1915         G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
1916         break;
1917     }
1918 }
1919
1920 static void
1921 empathy_roster_window_get_property (GObject    *object,
1922     guint property_id,
1923     GValue *value,
1924     GParamSpec *pspec)
1925 {
1926   EmpathyRosterWindow *self = EMPATHY_ROSTER_WINDOW (object);
1927
1928   switch (property_id)
1929     {
1930       case PROP_SHELL_RUNNING:
1931         g_value_set_boolean (value, self->priv->shell_running);
1932         break;
1933       default:
1934         G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
1935         break;
1936     }
1937 }
1938
1939 static void
1940 empathy_roster_window_constructed (GObject *self)
1941 {
1942   G_OBJECT_CLASS (empathy_roster_window_parent_class)->constructed (self);
1943 }
1944
1945 static void
1946 empathy_roster_window_class_init (EmpathyRosterWindowClass *klass)
1947 {
1948   GObjectClass *object_class = G_OBJECT_CLASS (klass);
1949   GParamSpec *pspec;
1950
1951   object_class->finalize = empathy_roster_window_finalize;
1952   object_class->constructor = empathy_roster_window_constructor;
1953   object_class->constructed = empathy_roster_window_constructed;
1954
1955   object_class->set_property = empathy_roster_window_set_property;
1956   object_class->get_property = empathy_roster_window_get_property;
1957
1958   pspec = g_param_spec_boolean ("shell-running",
1959       "Shell running",
1960       "Whether the Shell is running or not",
1961       FALSE,
1962       G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS);
1963   g_object_class_install_property (object_class, PROP_SHELL_RUNNING, pspec);
1964
1965   g_type_class_add_private (object_class, sizeof (EmpathyRosterWindowPriv));
1966 }
1967
1968 static void
1969 contacts_loaded_cb (EmpathyIndividualManager *manager,
1970     EmpathyRosterWindow *self)
1971 {
1972   set_notebook_page (self);
1973 }
1974
1975 static void
1976 roster_window_setup_actions (EmpathyRosterWindow *self)
1977 {
1978   GAction *action;
1979
1980 #define ADD_GSETTINGS_ACTION(schema, key) \
1981   action = g_settings_create_action (self->priv->gsettings_##schema, \
1982       EMPATHY_PREFS_##key); \
1983   g_action_map_add_action (G_ACTION_MAP (self), action); \
1984   g_object_unref (action);
1985
1986   ADD_GSETTINGS_ACTION (ui, UI_SHOW_OFFLINE);
1987
1988 #undef ADD_GSETTINGS_ACTION
1989 }
1990
1991 static void
1992 menu_deactivate_cb (GtkMenuShell *menushell,
1993     gpointer user_data)
1994 {
1995   /* FIXME: we shouldn't have to disconnect the signal (bgo #641327) */
1996   g_signal_handlers_disconnect_by_func (menushell,
1997       menu_deactivate_cb, user_data);
1998
1999   gtk_menu_detach (GTK_MENU (menushell));
2000 }
2001
2002 static void
2003 menu_item_activated_cb (GtkMenuShell *menushell,
2004     gpointer user_data)
2005 {
2006     EmpathyRosterWindow *roster_window = EMPATHY_ROSTER_WINDOW (user_data);
2007
2008     hide_search_bar (roster_window);
2009 }
2010
2011 static void
2012 popup_individual_menu_cb (EmpathyRosterView *view,
2013     const gchar *active_group,
2014     FolksIndividual *individual,
2015     guint button,
2016     guint time,
2017     gpointer user_data)
2018 {
2019   GtkWidget *menu;
2020   EmpathyIndividualFeatureFlags features = EMPATHY_INDIVIDUAL_FEATURE_CHAT |
2021     EMPATHY_INDIVIDUAL_FEATURE_CALL |
2022     EMPATHY_INDIVIDUAL_FEATURE_EDIT |
2023     EMPATHY_INDIVIDUAL_FEATURE_INFO |
2024     EMPATHY_INDIVIDUAL_FEATURE_LOG |
2025     EMPATHY_INDIVIDUAL_FEATURE_SMS |
2026     EMPATHY_INDIVIDUAL_FEATURE_CALL_PHONE |
2027     EMPATHY_INDIVIDUAL_FEATURE_REMOVE |
2028     EMPATHY_INDIVIDUAL_FEATURE_FILE_TRANSFER;
2029
2030   menu = empathy_individual_menu_new (individual, active_group,
2031       features, NULL);
2032
2033   /* menu is initially unowned but gtk_menu_attach_to_widget() takes its
2034    * floating ref. We can either wait for the view to release its ref
2035    * when it is destroyed (when leaving Empathy) or explicitly
2036    * detach the menu when it's not displayed any more.
2037    * We go for the latter as we don't want to keep useless menus in memory
2038    * during the whole lifetime of Empathy. */
2039   g_signal_connect (menu, "deactivate", G_CALLBACK (menu_deactivate_cb),
2040       NULL);
2041   g_signal_connect (menu, "menu-item-activated",
2042       G_CALLBACK (menu_item_activated_cb), user_data);
2043
2044   gtk_menu_attach_to_widget (GTK_MENU (menu), GTK_WIDGET (view), NULL);
2045   gtk_menu_popup (GTK_MENU (menu), NULL, NULL, NULL, NULL, button, time);
2046 }
2047
2048 static void
2049 view_empty_cb (EmpathyRosterView *view,
2050     GParamSpec *spec,
2051     EmpathyRosterWindow *self)
2052 {
2053   set_notebook_page (self);
2054
2055   if (!empathy_roster_view_is_empty (view))
2056     {
2057       gtk_widget_grab_focus (GTK_WIDGET (self->priv->view));
2058
2059       /* The store is being filled, it will be done after an idle cb.
2060        * So we can then get events. If we do that too soon, event's
2061        * contact is not yet in the store and it won't get marked as
2062        * having events. */
2063       g_idle_add (roster_window_load_events_idle_cb, self);
2064     }
2065 }
2066
2067 static void
2068 tooltip_destroy_cb (GtkWidget *widget,
2069     EmpathyRosterWindow *self)
2070 {
2071   g_clear_object (&self->priv->tooltip_widget);
2072 }
2073
2074 static gboolean
2075 individual_tooltip_cb (EmpathyRosterView *view,
2076     FolksIndividual *individual,
2077     gboolean keyboard_mode,
2078     GtkTooltip *tooltip,
2079     EmpathyRosterWindow *self)
2080 {
2081   if (self->priv->tooltip_widget == NULL)
2082     {
2083       self->priv->tooltip_widget = empathy_individual_widget_new (individual,
2084           EMPATHY_INDIVIDUAL_WIDGET_FOR_TOOLTIP |
2085           EMPATHY_INDIVIDUAL_WIDGET_SHOW_LOCATION |
2086           EMPATHY_INDIVIDUAL_WIDGET_SHOW_CLIENT_TYPES);
2087
2088       gtk_container_set_border_width (
2089           GTK_CONTAINER (self->priv->tooltip_widget), 8);
2090
2091       g_object_ref (self->priv->tooltip_widget);
2092
2093       tp_g_signal_connect_object (self->priv->tooltip_widget, "destroy",
2094           G_CALLBACK (tooltip_destroy_cb), self, 0);
2095
2096       gtk_widget_show (self->priv->tooltip_widget);
2097     }
2098   else
2099     {
2100       empathy_individual_widget_set_individual (
2101         EMPATHY_INDIVIDUAL_WIDGET (self->priv->tooltip_widget), individual);
2102     }
2103
2104   gtk_tooltip_set_custom (tooltip, self->priv->tooltip_widget);
2105
2106   return TRUE;
2107 }
2108
2109 typedef enum
2110 {
2111   DND_DRAG_TYPE_INVALID = -1,
2112   DND_DRAG_TYPE_URI_LIST,
2113 } DndDragType;
2114
2115 #define DRAG_TYPE(T,I) \
2116   { (gchar *) T, 0, I }
2117
2118 static const GtkTargetEntry drag_types_dest[] = {
2119   DRAG_TYPE ("text/path-list", DND_DRAG_TYPE_URI_LIST),
2120   DRAG_TYPE ("text/uri-list", DND_DRAG_TYPE_URI_LIST),
2121 };
2122
2123 static GdkAtom drag_atoms_dest[G_N_ELEMENTS (drag_types_dest)];
2124
2125 static DndDragType
2126 get_drag_type (GtkWidget *widget,
2127     GdkDragContext *context)
2128 {
2129   GdkAtom target;
2130   guint i;
2131
2132   target = gtk_drag_dest_find_target (widget, context, NULL);
2133
2134   for (i = 0; i < G_N_ELEMENTS (drag_atoms_dest); i++)
2135     {
2136       if (target == drag_atoms_dest[i])
2137           return drag_types_dest[i].info;
2138     }
2139
2140   return DND_DRAG_TYPE_INVALID;
2141 }
2142
2143 static gboolean
2144 individual_supports_ft (FolksIndividual *individual)
2145 {
2146   EmpathyContact *contact;
2147   EmpathyCapabilities caps;
2148   gboolean result;
2149
2150   contact = empathy_contact_dup_from_folks_individual (individual);
2151   if (contact == NULL)
2152     return FALSE;
2153
2154   caps = empathy_contact_get_capabilities (contact);
2155   result = (caps & EMPATHY_CAPABILITIES_FT);
2156
2157   g_object_unref (contact);
2158   return result;
2159 }
2160
2161 static gboolean
2162 view_drag_motion_cb (GtkWidget *widget,
2163     GdkDragContext *context,
2164     gint x,
2165     gint y,
2166     guint time_,
2167     EmpathyRosterWindow *self)
2168 {
2169   DndDragType type;
2170
2171   type = get_drag_type (widget, context);
2172
2173   if (type == DND_DRAG_TYPE_URI_LIST)
2174     {
2175       /* Check if contact supports FT */
2176       FolksIndividual *individual;
2177       GtkListBoxRow *row;
2178
2179       individual = empathy_roster_view_get_individual_at_y (self->priv->view,
2180           y, &row);
2181       if (individual == NULL)
2182         goto no_hl;
2183
2184       if (!individual_supports_ft (individual))
2185         goto no_hl;
2186
2187       gtk_list_box_drag_highlight_row (GTK_LIST_BOX (widget), row);
2188       return FALSE;
2189     }
2190
2191 no_hl:
2192   gtk_list_box_drag_unhighlight_row (GTK_LIST_BOX (widget));
2193   return FALSE;
2194 }
2195
2196 static gboolean
2197 view_drag_drop_cb (GtkWidget *widget,
2198     GdkDragContext *context,
2199     gint x,
2200     gint y,
2201     guint time_,
2202     EmpathyRosterWindow *self)
2203 {
2204   DndDragType type;
2205   FolksIndividual *individual;
2206
2207   type = get_drag_type (widget, context);
2208   if (type == DND_DRAG_TYPE_INVALID)
2209     return FALSE;
2210
2211   individual = empathy_roster_view_get_individual_at_y (self->priv->view, y,
2212       NULL);
2213   if (individual == NULL)
2214     return FALSE;
2215
2216   if (!individual_supports_ft (individual))
2217     return FALSE;
2218
2219   gtk_drag_get_data (widget, context,
2220       gtk_drag_dest_find_target (widget, context, NULL), time_);
2221
2222   return TRUE;
2223 }
2224
2225 static void
2226 view_drag_data_received_cb (GtkWidget *widget,
2227     GdkDragContext *context,
2228     gint x,
2229     gint y,
2230     GtkSelectionData *selection,
2231     guint info,
2232     guint time_,
2233     EmpathyRosterWindow *self)
2234 {
2235   gboolean success = FALSE;
2236
2237   if (selection == NULL)
2238     goto out;
2239
2240   if (info == DND_DRAG_TYPE_URI_LIST)
2241     {
2242       const gchar *path;
2243       FolksIndividual *individual;
2244       EmpathyContact *contact;
2245
2246       individual = empathy_roster_view_get_individual_at_y (self->priv->view,
2247           y, NULL);
2248       g_return_if_fail (individual != NULL);
2249
2250       path = (const gchar *) gtk_selection_data_get_data (selection);
2251
2252       contact = empathy_contact_dup_from_folks_individual (individual);
2253       empathy_send_file_from_uri_list (contact, path);
2254
2255       g_object_unref (contact);
2256
2257       success = TRUE;
2258     }
2259
2260 out:
2261   gtk_drag_finish (context, success, FALSE, time_);
2262 }
2263
2264 static void
2265 roster_window_most_available_presence_changed_cb (TpAccountManager *manager,
2266     TpConnectionPresenceType presence,
2267     const gchar *status,
2268     const gchar *message,
2269     EmpathyRosterWindow *self)
2270 {
2271   set_notebook_page (self);
2272 }
2273
2274 static void
2275 show_offline_changed_cb (GSettings *settings,
2276     const gchar *key,
2277     EmpathyRosterWindow *self)
2278 {
2279   set_notebook_page (self);
2280 }
2281
2282 static void
2283 empathy_roster_window_init (EmpathyRosterWindow *self)
2284 {
2285   GtkBuilder *gui;
2286   GtkWidget *sw;
2287   gchar *filename;
2288   GtkWidget *search_vbox;
2289   GtkWidget *header_bar;
2290   GtkWidget *new_conversation_button;
2291   GtkWidget *image;
2292   guint i;
2293   EmpathyRosterModel *model;
2294
2295   self->priv = G_TYPE_INSTANCE_GET_PRIVATE (self,
2296       EMPATHY_TYPE_ROSTER_WINDOW, EmpathyRosterWindowPriv);
2297
2298   empathy_set_css_provider (GTK_WIDGET (self));
2299
2300   self->priv->gsettings_ui = g_settings_new (EMPATHY_PREFS_UI_SCHEMA);
2301
2302   self->priv->sound_mgr = empathy_sound_manager_dup_singleton ();
2303
2304   gtk_window_set_title (GTK_WINDOW (self), _("Contact List"));
2305   gtk_window_set_role (GTK_WINDOW (self), "contact_list");
2306   gtk_window_set_default_size (GTK_WINDOW (self), 225, 325);
2307
2308   /* don't finalize the widget on delete-event, just hide it */
2309   g_signal_connect (self, "delete-event",
2310     G_CALLBACK (gtk_widget_hide_on_delete), NULL);
2311
2312   /* Set up interface */
2313   filename = empathy_file_lookup ("empathy-roster-window.ui", "src");
2314   gui = tpaw_builder_get_file (filename,
2315       "main_vbox", &self->priv->main_vbox,
2316       "balance_vbox", &self->priv->balance_vbox,
2317       "errors_vbox", &self->priv->errors_vbox,
2318       "auth_vbox", &self->priv->auth_vbox,
2319       "search_vbox", &search_vbox,
2320       "presence_toolbar", &self->priv->presence_toolbar,
2321       "notebook", &self->priv->notebook,
2322       "no_entry_label", &self->priv->no_entry_label,
2323       "roster_scrolledwindow", &sw,
2324       "button_account_settings", &self->priv->button_account_settings,
2325       "button_online", &self->priv->button_online,
2326       "button_show_offline", &self->priv->button_show_offline,
2327       "button_add_contact", &self->priv->button_add_contact,
2328       "spinner_loading", &self->priv->spinner_loading,
2329       NULL);
2330   g_free (filename);
2331
2332   header_bar = gtk_header_bar_new ();
2333   gtk_header_bar_set_title (GTK_HEADER_BAR(header_bar), _("Conversations"));
2334   gtk_header_bar_set_show_close_button (GTK_HEADER_BAR(header_bar), TRUE);
2335
2336   image = gtk_image_new_from_icon_name ("list-add-symbolic", GTK_ICON_SIZE_BUTTON);
2337   new_conversation_button = gtk_button_new ();
2338   g_signal_connect (new_conversation_button, "clicked",
2339       G_CALLBACK (roster_window_chat_new_message_cb), self);
2340   gtk_button_set_image (GTK_BUTTON (new_conversation_button), image);
2341   gtk_widget_set_tooltip_text (new_conversation_button, _("New Conversation"));
2342   gtk_window_set_titlebar (GTK_WINDOW (self), header_bar);
2343   gtk_container_add (GTK_CONTAINER (header_bar), new_conversation_button);
2344   gtk_widget_show_all (header_bar);
2345
2346   gtk_container_add (GTK_CONTAINER (self), self->priv->main_vbox);
2347   gtk_widget_show (self->priv->main_vbox);
2348
2349   g_signal_connect (self, "key-press-event",
2350       G_CALLBACK (roster_window_key_press_event_cb), self);
2351
2352   g_object_unref (gui);
2353
2354   self->priv->account_manager = tp_account_manager_dup ();
2355
2356   tp_proxy_prepare_async (self->priv->account_manager, NULL,
2357       account_manager_prepared_cb, self);
2358
2359   self->priv->errors = g_hash_table_new_full (g_direct_hash, g_direct_equal,
2360       g_object_unref, NULL);
2361
2362   self->priv->auths = g_hash_table_new (NULL, NULL);
2363
2364   self->priv->status_changed_handlers = g_hash_table_new_full (g_direct_hash,
2365       g_direct_equal, NULL, NULL);
2366
2367   /* set up menus */
2368   g_action_map_add_action_entries (G_ACTION_MAP (self),
2369       menubar_entries, G_N_ELEMENTS (menubar_entries), self);
2370   roster_window_setup_actions (self);
2371
2372   filename = empathy_file_lookup ("empathy-roster-window-menubar.ui", "src");
2373   gui = tpaw_builder_get_file (filename,
2374       "appmenu", &self->priv->menumodel,
2375       "rooms", &self->priv->rooms_section,
2376       NULL);
2377   g_free (filename);
2378
2379   g_object_ref (self->priv->menumodel);
2380   g_object_ref (self->priv->rooms_section);
2381
2382   /* Set up connection related actions. */
2383   roster_window_connection_items_setup (self);
2384   roster_window_favorite_chatroom_menu_setup (self);
2385
2386   g_object_unref (gui);
2387
2388   /* Set up contact list. */
2389   empathy_status_presets_get_all ();
2390
2391   /* Set up presence chooser */
2392   self->priv->presence_chooser = empathy_presence_chooser_new ();
2393   gtk_widget_show (self->priv->presence_chooser);
2394   gtk_box_pack_start (GTK_BOX (self->priv->presence_toolbar),
2395       self->priv->presence_chooser,
2396       TRUE, TRUE, 0);
2397
2398   /* Set up the throbber */
2399   self->priv->throbber = gtk_spinner_new ();
2400   gtk_widget_set_size_request (self->priv->throbber, 16, -1);
2401   gtk_widget_set_events (self->priv->throbber, GDK_BUTTON_PRESS_MASK);
2402   g_signal_connect (self->priv->throbber, "button-press-event",
2403     G_CALLBACK (roster_window_throbber_button_press_event_cb),
2404     self);
2405   gtk_box_pack_start (GTK_BOX (self->priv->presence_toolbar),
2406       self->priv->throbber,
2407       FALSE, TRUE, 0);
2408
2409   self->priv->individual_manager = empathy_individual_manager_dup_singleton ();
2410
2411   model = EMPATHY_ROSTER_MODEL (empathy_roster_model_manager_new (self->priv->individual_manager));
2412
2413   tp_g_signal_connect_object (self->priv->individual_manager,
2414       "contacts-loaded", G_CALLBACK (contacts_loaded_cb), self, 0);
2415
2416   self->priv->view = EMPATHY_ROSTER_VIEW (
2417       empathy_roster_view_new (model));
2418
2419   g_object_unref (model);
2420
2421   gtk_widget_show (GTK_WIDGET (self->priv->view));
2422
2423   gtk_container_add (GTK_CONTAINER (sw), GTK_WIDGET (self->priv->view));
2424
2425   g_signal_connect (self->priv->view, "individual-activated",
2426       G_CALLBACK (individual_activated_cb), self);
2427   g_signal_connect (self->priv->view, "event-activated",
2428       G_CALLBACK (event_activated_cb), self);
2429   g_signal_connect (self->priv->view, "popup-individual-menu",
2430       G_CALLBACK (popup_individual_menu_cb), self);
2431   g_signal_connect (self->priv->view, "notify::empty",
2432       G_CALLBACK (view_empty_cb), self);
2433   g_signal_connect (self->priv->view, "individual-tooltip",
2434       G_CALLBACK (individual_tooltip_cb), self);
2435
2436   /* DnD - destination */
2437   gtk_drag_dest_set (GTK_WIDGET (self->priv->view), GTK_DEST_DEFAULT_MOTION,
2438       drag_types_dest, G_N_ELEMENTS (drag_types_dest), GDK_ACTION_COPY);
2439
2440   for (i = 0; i < G_N_ELEMENTS (drag_types_dest); ++i)
2441     drag_atoms_dest[i] = gdk_atom_intern (drag_types_dest[i].target, FALSE);
2442
2443   g_signal_connect (self->priv->view, "drag-motion",
2444       G_CALLBACK (view_drag_motion_cb), self);
2445   g_signal_connect (self->priv->view, "drag-drop",
2446       G_CALLBACK (view_drag_drop_cb), self);
2447   g_signal_connect (self->priv->view, "drag-data-received",
2448       G_CALLBACK (view_drag_data_received_cb), self);
2449
2450   gtk_widget_set_has_tooltip (GTK_WIDGET (self->priv->view), TRUE);
2451
2452   /* Set up search bar */
2453   self->priv->search_bar = tpaw_live_search_new (
2454       GTK_WIDGET (self->priv->view));
2455   empathy_roster_view_set_live_search (self->priv->view,
2456       TPAW_LIVE_SEARCH (self->priv->search_bar));
2457   gtk_box_pack_start (GTK_BOX (search_vbox), self->priv->search_bar,
2458       FALSE, TRUE, 0);
2459
2460   g_signal_connect_swapped (self, "map",
2461       G_CALLBACK (gtk_widget_grab_focus), self->priv->view);
2462
2463   /* Load user-defined accelerators. */
2464   roster_window_accels_load ();
2465
2466   gtk_window_set_default_size (GTK_WINDOW (self), -1, 600);
2467   /* Set window size. */
2468   empathy_geometry_bind (GTK_WINDOW (self), GEOMETRY_NAME);
2469
2470   /* Enable event handling */
2471   self->priv->call_observer = empathy_call_observer_dup_singleton ();
2472   self->priv->event_manager = empathy_event_manager_dup_singleton ();
2473
2474   tp_g_signal_connect_object (self->priv->event_manager, "event-added",
2475       G_CALLBACK (roster_window_event_added_cb), self, 0);
2476   tp_g_signal_connect_object (self->priv->event_manager, "event-removed",
2477       G_CALLBACK (roster_window_event_removed_cb), self, 0);
2478
2479   g_signal_connect (self->priv->account_manager, "account-validity-changed",
2480       G_CALLBACK (roster_window_account_validity_changed_cb), self);
2481   g_signal_connect (self->priv->account_manager, "account-removed",
2482       G_CALLBACK (roster_window_account_removed_cb), self);
2483   g_signal_connect (self->priv->account_manager, "account-disabled",
2484       G_CALLBACK (roster_window_account_disabled_cb), self);
2485   g_signal_connect (self->priv->account_manager,
2486       "most-available-presence-changed",
2487       G_CALLBACK (roster_window_most_available_presence_changed_cb), self);
2488
2489   g_settings_bind (self->priv->gsettings_ui, EMPATHY_PREFS_UI_SHOW_OFFLINE,
2490       self->priv->view, "show-offline",
2491       G_SETTINGS_BIND_GET);
2492   tp_g_signal_connect_object (self->priv->gsettings_ui,
2493       "changed::" EMPATHY_PREFS_UI_SHOW_OFFLINE,
2494       G_CALLBACK (show_offline_changed_cb), self, 0);
2495   g_settings_bind (self->priv->gsettings_ui, EMPATHY_PREFS_UI_SHOW_GROUPS,
2496       self->priv->view, "show-groups",
2497       G_SETTINGS_BIND_GET);
2498   g_settings_bind (self->priv->gsettings_ui, "show-balance-in-roster",
2499       self->priv->balance_vbox, "visible",
2500       G_SETTINGS_BIND_GET);
2501
2502   g_signal_connect (self->priv->button_account_settings, "clicked",
2503       G_CALLBACK (button_account_settings_clicked_cb), self);
2504   g_signal_connect (self->priv->button_online, "clicked",
2505       G_CALLBACK (button_online_clicked_cb), self);
2506   g_signal_connect (self->priv->button_show_offline, "clicked",
2507       G_CALLBACK (button_show_offline_clicked_cb), self);
2508   g_signal_connect (self->priv->button_add_contact, "clicked",
2509       G_CALLBACK (button_add_contact_clicked_cb), self);
2510 }
2511
2512 GtkWidget *
2513 empathy_roster_window_new (GtkApplication *app)
2514 {
2515   return g_object_new (EMPATHY_TYPE_ROSTER_WINDOW,
2516       "application", app,
2517       NULL);
2518 }
2519
2520 GMenuModel *
2521 empathy_roster_window_get_menu_model (EmpathyRosterWindow *self)
2522 {
2523   g_return_val_if_fail (EMPATHY_IS_ROSTER_WINDOW (self), NULL);
2524
2525   return G_MENU_MODEL (self->priv->menumodel);
2526 }