]> git.0d.be Git - empathy.git/blob - libempathy-gtk/empathy-individual-menu.c
6759cc51ab5a783fec4d6cb5def68de12a029f6e
[empathy.git] / libempathy-gtk / empathy-individual-menu.c
1 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
2 /*
3  * Copyright (C) 2008-2010 Collabora Ltd.
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2.1 of the License, or (at your option) any later version.
9  *
10  * This library 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  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this library; if not, write to the Free Software
17  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
18  *
19  * Authors: Xavier Claessens <xclaesse@gmail.com>
20  *          Travis Reitter <travis.reitter@collabora.co.uk>
21  */
22
23 #include "config.h"
24 #include "empathy-individual-menu.h"
25
26 #include <glib/gi18n-lib.h>
27 #include <tp-account-widgets/tpaw-camera-monitor.h>
28 #include <telepathy-glib/telepathy-glib-dbus.h>
29
30 #include "empathy-account-selector-dialog.h"
31 #include "empathy-call-utils.h"
32 #include "empathy-chatroom-manager.h"
33 #include "empathy-gtk-enum-types.h"
34 #include "empathy-images.h"
35 #include "empathy-individual-dialogs.h"
36 #include "empathy-individual-dialogs.h"
37 #include "empathy-individual-edit-dialog.h"
38 #include "empathy-individual-information-dialog.h"
39 #include "empathy-individual-manager.h"
40 #include "empathy-individual-store-channel.h"
41 #include "empathy-log-window.h"
42 #include "empathy-request-util.h"
43 #include "empathy-share-my-desktop.h"
44 #include "empathy-ui-utils.h"
45 #include "empathy-utils.h"
46
47 #define DEBUG_FLAG EMPATHY_DEBUG_CONTACT
48 #include "empathy-debug.h"
49
50 #define GET_PRIV(obj) EMPATHY_GET_PRIV (obj, EmpathyIndividualMenu)
51
52 typedef struct {
53   gchar *active_group; /* may be NULL */
54   FolksIndividual *individual; /* owned */
55   EmpathyIndividualFeatureFlags features;
56   EmpathyIndividualStore *store; /* may be NULL */
57 } EmpathyIndividualMenuPriv;
58
59 enum {
60   PROP_ACTIVE_GROUP = 1,
61   PROP_INDIVIDUAL,
62   PROP_FEATURES,
63   PROP_STORE,
64 };
65
66 enum {
67   MENU_ITEM_ACTIVATED,
68   LAST_SIGNAL
69 };
70
71 static guint signals [LAST_SIGNAL] = { 0 };
72
73 G_DEFINE_TYPE (EmpathyIndividualMenu, empathy_individual_menu, GTK_TYPE_MENU);
74
75 static GtkWidget * chat_menu_item_new_individual (EmpathyIndividualMenu *self,
76     FolksIndividual *individual);
77 static GtkWidget * sms_menu_item_new_individual (EmpathyIndividualMenu *self,
78     FolksIndividual *individual);
79 static GtkWidget * log_menu_item_new_individual  (FolksIndividual *individual);
80 static GtkWidget * info_menu_item_new_individual (FolksIndividual *individual);
81 static GtkWidget * edit_menu_item_new_individual (FolksIndividual *individual);
82 static GtkWidget * invite_menu_item_new (FolksIndividual *individual,
83     EmpathyContact *contact);
84 static GtkWidget * file_transfer_menu_item_new_individual (EmpathyIndividualMenu *self,
85     FolksIndividual *individual);
86 static GtkWidget * share_my_desktop_menu_item_new_individual (EmpathyIndividualMenu *self,
87     FolksIndividual *individual);
88 static GtkWidget * favourite_menu_item_new_individual (FolksIndividual *individual);
89 static GtkWidget * add_menu_item_new_individual (EmpathyIndividualMenu *self,
90     FolksIndividual *individual);
91 static GtkWidget * block_menu_item_new_individual (FolksIndividual *individual);
92 static GtkWidget * remove_menu_item_new_individual (EmpathyIndividualMenu *self);
93
94 static void
95 individual_menu_add_personas (EmpathyIndividualMenu *self,
96     GtkMenuShell *menu,
97     FolksIndividual *individual,
98     EmpathyIndividualFeatureFlags features)
99 {
100   GtkWidget *item;
101   GeeSet *personas;
102   GeeIterator *iter;
103   guint persona_count = 0;
104
105   g_return_if_fail (GTK_IS_MENU (menu));
106   g_return_if_fail (FOLKS_IS_INDIVIDUAL (individual));
107   g_return_if_fail (empathy_folks_individual_contains_contact (individual));
108
109   personas = folks_individual_get_personas (individual);
110   /* we'll re-use this iterator throughout */
111   iter = gee_iterable_iterator (GEE_ITERABLE (personas));
112
113   /* Make sure we've got enough valid entries for these menu items to add
114    * functionality */
115   while (gee_iterator_next (iter))
116     {
117       FolksPersona *persona = gee_iterator_get (iter);
118       if (empathy_folks_persona_is_interesting (persona))
119         persona_count++;
120
121       g_clear_object (&persona);
122     }
123
124   g_clear_object (&iter);
125
126   /* return early if these entries would add nothing beyond the "quick" items */
127   if (persona_count <= 1)
128     goto out;
129
130   /* add a separator before the list of personas */
131   item = gtk_separator_menu_item_new ();
132   gtk_menu_shell_append (GTK_MENU_SHELL (menu), item);
133   gtk_widget_show (item);
134
135   iter = gee_iterable_iterator (GEE_ITERABLE (personas));
136   while (gee_iterator_next (iter))
137     {
138       GtkWidget *image;
139       GtkWidget *contact_item;
140       GtkWidget *contact_submenu;
141       TpContact *tp_contact;
142       EmpathyContact *contact;
143       TpfPersona *persona = gee_iterator_get (iter);
144       gchar *label;
145       FolksPersonaStore *store;
146       const gchar *account;
147       GtkWidget *action;
148       /* Individual containing only persona */
149       FolksIndividual *single_individual;
150
151       if (!empathy_folks_persona_is_interesting (FOLKS_PERSONA (persona)))
152         goto while_finish;
153
154       tp_contact = tpf_persona_get_contact (persona);
155       if (tp_contact == NULL)
156         goto while_finish;
157
158       contact = empathy_contact_dup_from_tp_contact (tp_contact);
159       single_individual = empathy_ensure_individual_from_tp_contact (
160           tp_contact);
161
162       /* Pretty hacky. Creating single_individual had a side effect to change
163        * persona.individual from individual to single_individual which is not
164        * what we want so we set it back. See bgo#684971 for details. */
165       g_object_set (persona, "individual", individual, NULL);
166
167       store = folks_persona_get_store (FOLKS_PERSONA (persona));
168       account = folks_persona_store_get_display_name (store);
169
170       /* Translators: this is used in the context menu for a contact. The first
171        * parameter is a contact ID (e.g. foo@jabber.org) and the second is one
172        * of the user's account IDs (e.g. me@hotmail.com). */
173       label = g_strdup_printf (_("%s (%s)"),
174           folks_persona_get_display_id (FOLKS_PERSONA (persona)), account);
175
176       contact_item = gtk_image_menu_item_new_with_label (label);
177       gtk_image_menu_item_set_always_show_image (GTK_IMAGE_MENU_ITEM (contact_item),
178                                                  TRUE);
179       contact_submenu = gtk_menu_new ();
180       gtk_menu_item_set_submenu (GTK_MENU_ITEM (contact_item), contact_submenu);
181       image = gtk_image_new_from_icon_name (
182           empathy_icon_name_for_contact (contact), GTK_ICON_SIZE_MENU);
183       gtk_image_menu_item_set_image (GTK_IMAGE_MENU_ITEM (contact_item), image);
184       gtk_widget_show (image);
185
186       /* Chat */
187       if (features & EMPATHY_INDIVIDUAL_FEATURE_CHAT)
188         {
189           action = chat_menu_item_new_individual (self, single_individual);
190           gtk_menu_shell_append (GTK_MENU_SHELL (contact_submenu), action);
191           gtk_widget_show (action);
192         }
193
194       /* SMS */
195       if (features & EMPATHY_INDIVIDUAL_FEATURE_SMS)
196         {
197           action = sms_menu_item_new_individual (self, single_individual);
198           gtk_menu_shell_append (GTK_MENU_SHELL (contact_submenu), action);
199           gtk_widget_show (action);
200         }
201
202       if (features & EMPATHY_INDIVIDUAL_FEATURE_CALL)
203         {
204           /* Audio Call */
205           action = empathy_individual_audio_call_menu_item_new_individual (
206               self, single_individual);
207           gtk_menu_shell_append (GTK_MENU_SHELL (contact_submenu), action);
208           gtk_widget_show (action);
209
210           /* Video Call */
211           action = empathy_individual_video_call_menu_item_new_individual (
212               self, single_individual);
213           gtk_menu_shell_append (GTK_MENU_SHELL (contact_submenu), action);
214           gtk_widget_show (action);
215         }
216
217       /* Log */
218       if (features & EMPATHY_INDIVIDUAL_FEATURE_LOG)
219         {
220           action = log_menu_item_new_individual (single_individual);
221           gtk_menu_shell_append (GTK_MENU_SHELL (contact_submenu), action);
222           gtk_widget_show (action);
223         }
224
225       /* Invite */
226       action = invite_menu_item_new (NULL, contact);
227       gtk_menu_shell_append (GTK_MENU_SHELL (contact_submenu), action);
228       gtk_widget_show (action);
229
230       /* File transfer */
231       if (features & EMPATHY_INDIVIDUAL_FEATURE_FILE_TRANSFER)
232         {
233           action = file_transfer_menu_item_new_individual (self, single_individual);
234           gtk_menu_shell_append (GTK_MENU_SHELL (contact_submenu), action);
235           gtk_widget_show (action);
236         }
237
238       /* Share my desktop */
239       action = share_my_desktop_menu_item_new_individual (self, single_individual);
240       gtk_menu_shell_append (GTK_MENU_SHELL (contact_submenu), action);
241       gtk_widget_show (action);
242
243       /* Block */
244       if (features & EMPATHY_INDIVIDUAL_FEATURE_BLOCK &&
245           (item = block_menu_item_new_individual (single_individual))
246           != NULL) {
247         GtkWidget *sep;
248
249         sep = gtk_separator_menu_item_new ();
250         gtk_menu_shell_append (GTK_MENU_SHELL (contact_submenu), sep);
251         gtk_widget_show (sep);
252
253         gtk_menu_shell_append (GTK_MENU_SHELL (contact_submenu), item);
254         gtk_widget_show (item);
255       }
256
257       gtk_menu_shell_append (GTK_MENU_SHELL (menu), contact_item);
258       gtk_widget_show (contact_item);
259
260       g_free (label);
261       g_object_unref (contact);
262       g_object_unref (single_individual);
263
264 while_finish:
265       g_clear_object (&persona);
266     }
267
268 out:
269   g_clear_object (&iter);
270 }
271
272 static void
273 empathy_individual_menu_init (EmpathyIndividualMenu *self)
274 {
275   EmpathyIndividualMenuPriv *priv = G_TYPE_INSTANCE_GET_PRIVATE (self,
276       EMPATHY_TYPE_INDIVIDUAL_MENU, EmpathyIndividualMenuPriv);
277
278   self->priv = priv;
279 }
280
281 static GList *
282 find_phone_accounts (void)
283 {
284   TpAccountManager *am;
285   GList *accounts, *l;
286   GList *found = NULL;
287
288   am = tp_account_manager_dup ();
289   g_return_val_if_fail (am != NULL, NULL);
290
291   accounts = tp_account_manager_dup_valid_accounts (am);
292   for (l = accounts; l != NULL; l = g_list_next (l))
293     {
294       TpAccount *account = l->data;
295
296       if (tp_account_get_connection_status (account, NULL) !=
297           TP_CONNECTION_STATUS_CONNECTED)
298         continue;
299
300       if (!tp_account_associated_with_uri_scheme (account, "tel"))
301         continue;
302
303       found = g_list_prepend (found, g_object_ref (account));
304     }
305
306   g_list_free_full (accounts, g_object_unref);
307   g_object_unref (am);
308
309   return found;
310 }
311
312 static gboolean
313 has_phone_account (void)
314 {
315   GList *accounts;
316   gboolean result;
317
318   accounts = find_phone_accounts ();
319   result = (accounts != NULL);
320
321   g_list_free_full (accounts, (GDestroyNotify) g_object_unref);
322
323   return result;
324 }
325
326 static void
327 call_phone_number (FolksPhoneFieldDetails *details,
328     TpAccount *account)
329 {
330   gchar *number;
331
332   number = folks_phone_field_details_get_normalised (details);
333   DEBUG ("Try to call %s", number);
334
335   empathy_call_new_with_streams (number,
336       account, FALSE, empathy_get_current_action_time ());
337
338   g_free (number);
339 }
340
341 static void
342 display_call_phone_dialog (FolksPhoneFieldDetails *details,
343     GList *accounts)
344 {
345   GtkWidget *dialog;
346   gint response;
347
348   dialog = empathy_account_selector_dialog_new (accounts);
349
350   gtk_window_set_title (GTK_WINDOW (dialog),
351       _("Select account to use to place the call"));
352
353   gtk_dialog_add_buttons (GTK_DIALOG (dialog),
354       GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
355       _("Call"), GTK_RESPONSE_OK,
356       NULL);
357
358   response = gtk_dialog_run (GTK_DIALOG (dialog));
359
360   if (response == GTK_RESPONSE_OK)
361     {
362       TpAccount *account;
363
364       account = empathy_account_selector_dialog_dup_selected (
365            EMPATHY_ACCOUNT_SELECTOR_DIALOG (dialog));
366
367       if (account != NULL)
368         {
369           call_phone_number (details, account);
370
371           g_object_unref (account);
372         }
373     }
374
375   gtk_widget_destroy (dialog);
376 }
377
378 static void
379 call_phone_number_cb (GtkMenuItem *item,
380       FolksPhoneFieldDetails *details)
381 {
382   GList *accounts;
383
384   accounts = find_phone_accounts ();
385   if (accounts == NULL)
386     {
387       DEBUG ("No phone aware account connected; can't call");
388     }
389   else if (g_list_length (accounts) == 1)
390     {
391       call_phone_number (details, accounts->data);
392     }
393   else
394     {
395       /* Ask which account to use */
396       display_call_phone_dialog (details, accounts);
397     }
398
399   g_list_free_full (accounts, (GDestroyNotify) g_object_unref);
400 }
401
402 static const gchar *
403 find_phone_type (FolksPhoneFieldDetails *details)
404 {
405   GeeCollection *types;
406   GeeIterator *iter;
407   const gchar *retval = NULL;
408
409   types = folks_abstract_field_details_get_parameter_values (
410       FOLKS_ABSTRACT_FIELD_DETAILS (details), "type");
411
412   if (types == NULL)
413     return NULL;
414
415   iter = gee_iterable_iterator (GEE_ITERABLE (types));
416   while (gee_iterator_next (iter))
417     {
418       gchar *type = gee_iterator_get (iter);
419
420       if (!tp_strdiff (type, "CELL"))
421         retval = _("Mobile");
422       else if (!tp_strdiff (type, "WORK"))
423         retval = _("Work");
424       else if (!tp_strdiff (type, "HOME"))
425         retval = _("HOME");
426
427       g_free (type);
428
429       if (retval != NULL)
430         break;
431     }
432
433   g_object_unref (iter);
434
435   return retval;
436 }
437
438 static void
439 add_phone_numbers (EmpathyIndividualMenu *self)
440 {
441   EmpathyIndividualMenuPriv *priv = GET_PRIV (self);
442   GeeSet *all_numbers;
443   GeeIterator *iter;
444   gboolean sensitive;
445
446   all_numbers = folks_phone_details_get_phone_numbers (
447       FOLKS_PHONE_DETAILS (priv->individual));
448
449   sensitive = has_phone_account ();
450
451   iter = gee_iterable_iterator (GEE_ITERABLE (all_numbers));
452   while (gee_iterator_next (iter))
453     {
454       FolksPhoneFieldDetails *details = gee_iterator_get (iter);
455       GtkWidget *item, *image;
456       gchar *tmp, *number;
457       const gchar *type;
458
459       type = find_phone_type (details);
460       number = folks_phone_field_details_get_normalised (details);
461
462       if (type != NULL)
463         {
464           /* translators: first argument is a phone number like +32123456 and
465            * the second one is something like 'home' or 'work'. */
466           tmp = g_strdup_printf (_("Call %s (%s)"), number, type);
467         }
468       else
469         {
470           /* translators: argument is a phone number like +32123456 */
471           tmp = g_strdup_printf (_("Call %s"), number);
472         }
473
474       g_free (number);
475
476       item = gtk_image_menu_item_new_with_mnemonic (tmp);
477       g_free (tmp);
478
479       g_signal_connect_data (item, "activate",
480           G_CALLBACK (call_phone_number_cb), g_object_ref (details),
481           (GClosureNotify) g_object_unref, 0);
482
483       gtk_widget_set_sensitive (item, sensitive);
484
485       image = gtk_image_new_from_icon_name (EMPATHY_IMAGE_CALL,
486           GTK_ICON_SIZE_MENU);
487       gtk_image_menu_item_set_image (GTK_IMAGE_MENU_ITEM (item), image);
488       gtk_widget_show (image);
489
490       gtk_menu_shell_append (GTK_MENU_SHELL (self), item);
491       gtk_widget_show (item);
492
493       g_object_unref (details);
494     }
495
496   g_object_unref (iter);
497 }
498
499 /* return a list of TpContact supporting the blocking iface */
500 static GList *
501 get_contacts_supporting_blocking (FolksIndividual *individual)
502 {
503   GeeSet *personas;
504   GeeIterator *iter;
505   GList *result = NULL;
506
507   personas = folks_individual_get_personas (individual);
508
509   iter = gee_iterable_iterator (GEE_ITERABLE (personas));
510   while (gee_iterator_next (iter))
511     {
512       TpfPersona *persona = gee_iterator_get (iter);
513       TpContact *contact;
514       TpConnection *conn;
515
516       if (!TPF_IS_PERSONA (persona))
517         goto while_next;
518
519       contact = tpf_persona_get_contact (persona);
520       if (contact == NULL)
521         goto while_next;
522
523       conn = tp_contact_get_connection (contact);
524
525       if (tp_proxy_has_interface_by_id (conn,
526         TP_IFACE_QUARK_CONNECTION_INTERFACE_CONTACT_BLOCKING))
527         result = g_list_prepend (result, contact);
528
529 while_next:
530       g_clear_object (&persona);
531     }
532
533   g_clear_object (&iter);
534
535   return result;
536 }
537
538 typedef struct
539 {
540   gboolean blocked;
541   GtkWidget *parent;
542 } GotAvatarCtx;
543
544 static GotAvatarCtx *
545 got_avatar_ctx_new (gboolean blocked,
546     GtkWidget *parent)
547 {
548   GotAvatarCtx *ctx = g_slice_new0 (GotAvatarCtx);
549
550   ctx->blocked = blocked;
551   ctx->parent = parent != NULL ? g_object_ref (parent) : NULL;
552   return ctx;
553 }
554
555 static void
556 got_avatar_ctx_free (GotAvatarCtx *ctx)
557 {
558   g_clear_object (&ctx->parent);
559   g_slice_free (GotAvatarCtx, ctx);
560 }
561
562 static void
563 got_avatar (GObject *source_object,
564     GAsyncResult *result,
565     gpointer user_data)
566 {
567   FolksIndividual *individual = FOLKS_INDIVIDUAL (source_object);
568   GotAvatarCtx *ctx = user_data;
569   GdkPixbuf *avatar;
570   GError *error = NULL;
571   gboolean abusive = FALSE;
572   EmpathyIndividualManager *manager;
573
574   avatar = empathy_pixbuf_avatar_from_individual_scaled_finish (individual,
575       result, &error);
576
577   if (error != NULL)
578     {
579       DEBUG ("Could not get avatar: %s", error->message);
580       g_error_free (error);
581     }
582
583   if (ctx->blocked) {
584     /* confirm the user really wishes to block the contact */
585     if (!empathy_block_individual_dialog_show (GTK_WINDOW (ctx->parent),
586           individual, avatar, &abusive))
587       goto out;
588   }
589
590   manager = empathy_individual_manager_dup_singleton ();
591
592   empathy_individual_manager_set_blocked (manager, individual,
593       ctx->blocked, abusive);
594
595   g_object_unref (manager);
596
597 out:
598   g_clear_object (&avatar);
599   got_avatar_ctx_free (ctx);
600 }
601
602 static void
603 empathy_individual_block_menu_item_toggled (GtkCheckMenuItem *item,
604     FolksIndividual *individual)
605 {
606   GotAvatarCtx *ctx;
607   gboolean blocked;
608   GtkWidget *parent;
609
610   /* @item may be destroyed while the async call is running to get the things
611    * we need from it right now. */
612   blocked = gtk_check_menu_item_get_active (item);
613
614   parent = g_object_get_data (
615     G_OBJECT (gtk_widget_get_parent (GTK_WIDGET (item))),
616     "window");
617
618   ctx = got_avatar_ctx_new (blocked, parent);
619
620   empathy_pixbuf_avatar_from_individual_scaled_async (individual,
621       48, 48, NULL, got_avatar, ctx);
622 }
623
624 static void
625 update_block_menu_item (GtkWidget *item,
626     FolksIndividual *individual)
627 {
628   GList *contacts, *l;
629   gboolean is_blocked = TRUE;
630
631   contacts = get_contacts_supporting_blocking (individual);
632
633   if (contacts == NULL)
634     is_blocked = FALSE;
635
636   /* Check the menu item if all his personas are blocked */
637   for (l = contacts; l != NULL; l = g_list_next (l))
638     {
639       TpContact *contact = l->data;
640
641       if (!tp_contact_is_blocked (contact))
642         {
643           is_blocked = FALSE;
644           break;
645         }
646     }
647
648   g_signal_handlers_block_by_func (item,
649       empathy_individual_block_menu_item_toggled, individual);
650
651   gtk_check_menu_item_set_active (GTK_CHECK_MENU_ITEM (item), is_blocked);
652
653   g_signal_handlers_unblock_by_func (item,
654       empathy_individual_block_menu_item_toggled, individual);
655
656   g_list_free (contacts);
657 }
658
659 static void
660 contact_blocked_changed_cb (TpContact *contact,
661     GParamSpec *spec,
662     GtkWidget *item)
663 {
664   FolksIndividual *individual;
665
666   individual = g_object_get_data (G_OBJECT (item), "individual");
667
668   update_block_menu_item (item, individual);
669 }
670
671 static GtkWidget *
672 block_menu_item_new_individual (FolksIndividual *individual)
673 {
674   GtkWidget *item;
675   GList *contacts, *l;
676
677   contacts = get_contacts_supporting_blocking (individual);
678
679   /* Can't block, no persona supports blocking */
680   if (contacts == NULL)
681     return NULL;
682
683   item = gtk_check_menu_item_new_with_mnemonic (_("_Block Contact"));
684
685   g_object_set_data_full (G_OBJECT (item), "individual",
686       g_object_ref (individual), g_object_unref);
687
688   for (l = contacts; l != NULL; l = g_list_next (l))
689     {
690       TpContact *contact = l->data;
691
692       tp_g_signal_connect_object (contact, "notify::is-blocked",
693           G_CALLBACK (contact_blocked_changed_cb), item, 0);
694     }
695
696   g_signal_connect (item, "toggled",
697       G_CALLBACK (empathy_individual_block_menu_item_toggled), individual);
698
699   update_block_menu_item (item, individual);
700
701   g_list_free (contacts);
702
703   return item;
704 }
705
706 enum
707 {
708   REMOVE_DIALOG_RESPONSE_CANCEL = 0,
709   REMOVE_DIALOG_RESPONSE_DELETE,
710   REMOVE_DIALOG_RESPONSE_DELETE_AND_BLOCK,
711   REMOVE_DIALOG_RESPONSE_REMOVE_FROM_GROUP
712 };
713
714 static int
715 remove_dialog_show (const gchar *message,
716     const gchar *secondary_text,
717     gboolean show_remove_from_group,
718     gboolean block_button,
719     GdkPixbuf *avatar,
720     const gchar *active_group)
721 {
722   GtkWidget *dialog;
723   gboolean res;
724
725   dialog = gtk_message_dialog_new (NULL, GTK_DIALOG_MODAL,
726       GTK_MESSAGE_QUESTION, GTK_BUTTONS_NONE, "%s", message);
727
728   if (avatar != NULL)
729     {
730       GtkWidget *image = gtk_image_new_from_pixbuf (avatar);
731       gtk_message_dialog_set_image (GTK_MESSAGE_DIALOG (dialog), image);
732       gtk_widget_show (image);
733     }
734
735   if (show_remove_from_group)
736     {
737       GtkWidget *button;
738       gchar *button_text = g_strdup_printf (_("Remove from _Group \'%s\'"),
739           active_group);
740
741       /* gtk_dialog_add_button() doesn't allow us to pass a string with a
742        * mnemonic so we have to create the button manually. */
743       button = gtk_button_new_with_mnemonic (button_text);
744       g_free (button_text);
745
746       gtk_dialog_add_action_widget (GTK_DIALOG (dialog), button,
747           REMOVE_DIALOG_RESPONSE_REMOVE_FROM_GROUP);
748
749       gtk_widget_show (button);
750     }
751
752   if (block_button)
753     {
754       GtkWidget *button;
755
756       /* gtk_dialog_add_button() doesn't allow us to pass a string with a
757        * mnemonic so we have to create the button manually. */
758       button = gtk_button_new_with_mnemonic (
759           _("Delete and _Block"));
760
761       gtk_dialog_add_action_widget (GTK_DIALOG (dialog), button,
762           REMOVE_DIALOG_RESPONSE_DELETE_AND_BLOCK);
763
764       gtk_widget_show (button);
765     }
766
767   gtk_dialog_add_buttons (GTK_DIALOG (dialog),
768       GTK_STOCK_CANCEL, REMOVE_DIALOG_RESPONSE_CANCEL,
769       GTK_STOCK_DELETE, REMOVE_DIALOG_RESPONSE_DELETE, NULL);
770   gtk_message_dialog_format_secondary_text (GTK_MESSAGE_DIALOG (dialog),
771       "%s", secondary_text);
772
773   gtk_widget_show (dialog);
774
775   res = gtk_dialog_run (GTK_DIALOG (dialog));
776   gtk_widget_destroy (dialog);
777
778   return res;
779 }
780
781 static void
782 individual_removed_from_group_cb (GObject *source_object,
783     GAsyncResult *res,
784     gpointer user_data)
785 {
786   GError *error = NULL;
787   FolksIndividual *individual = FOLKS_INDIVIDUAL (source_object);
788
789   folks_group_details_change_group_finish (
790       FOLKS_GROUP_DETAILS (individual), res, &error);
791   if (error != NULL)
792     {
793       DEBUG ("Individual could not be removed from group: %s",
794           error->message);
795       g_error_free (error);
796     }
797 }
798
799 static void
800 remove_got_avatar (GObject *source_object,
801     GAsyncResult *result,
802     gpointer user_data)
803 {
804   FolksIndividual *individual = FOLKS_INDIVIDUAL (source_object);
805   EmpathyIndividualMenu *self = EMPATHY_INDIVIDUAL_MENU (user_data);
806   EmpathyIndividualMenuPriv *priv = GET_PRIV (self);
807   GdkPixbuf *avatar;
808   EmpathyIndividualManager *manager;
809   gchar *text;
810   GeeSet *personas;
811   guint persona_count = 0;
812   gboolean can_block;
813   GError *error = NULL;
814   gint res;
815   gboolean show_remove_from_group;
816   GeeSet *groups;
817
818   avatar = empathy_pixbuf_avatar_from_individual_scaled_finish (individual,
819       result, &error);
820
821   if (error != NULL)
822     {
823       DEBUG ("Could not get avatar: %s", error->message);
824       g_error_free (error);
825     }
826
827   /* We couldn't retrieve the avatar, but that isn't a fatal error,
828    * so we still display the remove dialog. */
829
830   groups = folks_group_details_get_groups (FOLKS_GROUP_DETAILS (individual));
831   show_remove_from_group =
832       gee_collection_get_size (GEE_COLLECTION (groups)) > 1;
833
834   personas = folks_individual_get_personas (individual);
835
836   persona_count = gee_collection_get_size (GEE_COLLECTION (personas));
837
838   /* If we have more than one TpfPersona, display a different message
839    * ensuring the user knows that *all* of the meta-contacts' personas will
840    * be removed. */
841
842   if (persona_count < 2)
843     {
844       /* Not a meta-contact */
845       text =
846           g_strdup_printf (
847               _("Do you really want to remove the contact '%s'?"),
848               folks_alias_details_get_alias (
849                   FOLKS_ALIAS_DETAILS (individual)));
850     }
851   else
852     {
853       /* Meta-contact */
854       text =
855           g_strdup_printf (
856               _("Do you really want to remove the linked contact '%s'? "
857                 "Note that this will remove all the contacts which make up "
858                 "this linked contact."),
859               folks_alias_details_get_alias (
860                   FOLKS_ALIAS_DETAILS (individual)));
861     }
862
863
864   manager = empathy_individual_manager_dup_singleton ();
865   can_block = empathy_individual_manager_supports_blocking (manager,
866       individual);
867   res = remove_dialog_show (_("Removing contact"), text,
868       show_remove_from_group, can_block, avatar, priv->active_group);
869
870   if (res == REMOVE_DIALOG_RESPONSE_REMOVE_FROM_GROUP)
871     {
872       folks_group_details_change_group (FOLKS_GROUP_DETAILS (individual),
873           priv->active_group, false, individual_removed_from_group_cb, NULL);
874       goto finally;
875     }
876
877   if (res == REMOVE_DIALOG_RESPONSE_DELETE ||
878       res == REMOVE_DIALOG_RESPONSE_DELETE_AND_BLOCK)
879     {
880       gboolean abusive;
881
882       if (res == REMOVE_DIALOG_RESPONSE_DELETE_AND_BLOCK)
883         {
884           if (!empathy_block_individual_dialog_show (NULL, individual,
885                 avatar, &abusive))
886             goto finally;
887
888           empathy_individual_manager_set_blocked (manager, individual,
889               TRUE, abusive);
890         }
891
892       empathy_individual_manager_remove (manager, individual, "");
893     }
894
895  finally:
896   g_free (text);
897   g_object_unref (manager);
898   g_object_unref (self);
899 }
900
901 static void
902 remove_activate_cb (GtkMenuItem *menuitem,
903     EmpathyIndividualMenu *self)
904 {
905   EmpathyIndividualMenuPriv *priv = GET_PRIV (self);
906
907   empathy_pixbuf_avatar_from_individual_scaled_async (priv->individual,
908       48, 48, NULL, remove_got_avatar, g_object_ref (self));
909 }
910
911 static GtkWidget *
912 remove_menu_item_new_individual (EmpathyIndividualMenu *self)
913 {
914   GeeSet *personas;
915   GeeIterator *iter;
916   gboolean can_remove = FALSE;
917   GtkWidget *item, *image;
918   EmpathyIndividualMenuPriv *priv = GET_PRIV (self);
919
920   /* If any of the Individual's personas can be removed, add an option to
921    * remove. This will act as a best-effort option. If any Personas cannot be
922    * removed from the server, then this option will just be inactive upon
923    * subsequent menu openings */
924   personas = folks_individual_get_personas (priv->individual);
925   iter = gee_iterable_iterator (GEE_ITERABLE (personas));
926   while (!can_remove && gee_iterator_next (iter))
927     {
928       FolksPersona *persona = gee_iterator_get (iter);
929       FolksPersonaStore *store = folks_persona_get_store (persona);
930       FolksMaybeBool maybe_can_remove =
931           folks_persona_store_get_can_remove_personas (store);
932
933       if (maybe_can_remove == FOLKS_MAYBE_BOOL_TRUE)
934         can_remove = TRUE;
935
936       g_clear_object (&persona);
937     }
938   g_clear_object (&iter);
939
940   if (!can_remove)
941     return NULL;
942
943   item = gtk_image_menu_item_new_with_mnemonic (_("_Remove"));
944   image = gtk_image_new_from_icon_name (GTK_STOCK_REMOVE,
945       GTK_ICON_SIZE_MENU);
946   gtk_image_menu_item_set_image (GTK_IMAGE_MENU_ITEM (item), image);
947
948   g_signal_connect (item, "activate",
949       G_CALLBACK (remove_activate_cb), self);
950
951   return item;
952 }
953
954 static void
955 constructed (GObject *object)
956 {
957   EmpathyIndividualMenu *self = EMPATHY_INDIVIDUAL_MENU (object);
958   EmpathyIndividualMenuPriv *priv = GET_PRIV (object);
959   GtkMenuShell *shell;
960   GtkWidget *item;
961   FolksIndividual *individual;
962   EmpathyIndividualFeatureFlags features;
963
964   /* Build the menu */
965   shell = GTK_MENU_SHELL (object);
966   individual = priv->individual;
967   features = priv->features;
968
969   /* Add contact */
970   if (features & EMPATHY_INDIVIDUAL_FEATURE_ADD_CONTACT)
971     {
972       item = add_menu_item_new_individual (self, individual);
973       if (item != NULL)
974         {
975           gtk_menu_shell_append (GTK_MENU_SHELL (shell), item);
976           gtk_widget_show (item);
977         }
978     }
979
980   /* Chat */
981   if (features & EMPATHY_INDIVIDUAL_FEATURE_CHAT)
982     {
983       item = chat_menu_item_new_individual (self, individual);
984       if (item != NULL)
985         {
986           gtk_menu_shell_append (shell, item);
987           gtk_widget_show (item);
988         }
989     }
990
991   /* SMS */
992   if (features & EMPATHY_INDIVIDUAL_FEATURE_SMS)
993     {
994       item = sms_menu_item_new_individual (self, individual);
995       if (item != NULL)
996         {
997           gtk_menu_shell_append (shell, item);
998           gtk_widget_show (item);
999         }
1000     }
1001
1002   if (features & EMPATHY_INDIVIDUAL_FEATURE_CALL)
1003     {
1004       /* Audio Call */
1005       item = empathy_individual_audio_call_menu_item_new_individual (self,
1006           individual);
1007       gtk_menu_shell_append (shell, item);
1008       gtk_widget_show (item);
1009
1010       /* Video Call */
1011       item = empathy_individual_video_call_menu_item_new_individual (self,
1012           individual);
1013       gtk_menu_shell_append (shell, item);
1014       gtk_widget_show (item);
1015     }
1016
1017   if (features & EMPATHY_INDIVIDUAL_FEATURE_CALL_PHONE)
1018     add_phone_numbers (self);
1019
1020   /* Invite */
1021   item = invite_menu_item_new (individual, NULL);
1022   gtk_menu_shell_append (shell, item);
1023   gtk_widget_show (item);
1024
1025   /* File transfer */
1026   if (features & EMPATHY_INDIVIDUAL_FEATURE_FILE_TRANSFER)
1027     {
1028       item = file_transfer_menu_item_new_individual (self, individual);
1029       gtk_menu_shell_append (shell, item);
1030       gtk_widget_show (item);
1031     }
1032
1033   /* Share my desktop */
1034   /* FIXME we should add the "Share my desktop" menu item if Vino is
1035   a registered handler in MC5 */
1036   item = share_my_desktop_menu_item_new_individual (self, individual);
1037   gtk_menu_shell_append (shell, item);
1038   gtk_widget_show (item);
1039
1040   /* Menu items to target specific contacts */
1041   individual_menu_add_personas (self, GTK_MENU_SHELL (object),
1042       individual, features);
1043
1044   /* Separator */
1045   if (features & (EMPATHY_INDIVIDUAL_FEATURE_EDIT |
1046       EMPATHY_INDIVIDUAL_FEATURE_INFO |
1047       EMPATHY_INDIVIDUAL_FEATURE_FAVOURITE))
1048     {
1049       item = gtk_separator_menu_item_new ();
1050       gtk_menu_shell_append (shell, item);
1051       gtk_widget_show (item);
1052     }
1053
1054   /* Edit */
1055   if (features & EMPATHY_INDIVIDUAL_FEATURE_EDIT)
1056     {
1057       item = edit_menu_item_new_individual (individual);
1058       gtk_menu_shell_append (shell, item);
1059       gtk_widget_show (item);
1060     }
1061
1062   /* Log */
1063   if (features & EMPATHY_INDIVIDUAL_FEATURE_LOG)
1064     {
1065       item = log_menu_item_new_individual (individual);
1066       gtk_menu_shell_append (shell, item);
1067       gtk_widget_show (item);
1068     }
1069
1070   /* Info */
1071   if (features & EMPATHY_INDIVIDUAL_FEATURE_INFO)
1072     {
1073       item = info_menu_item_new_individual (individual);
1074       gtk_menu_shell_append (shell, item);
1075       gtk_widget_show (item);
1076     }
1077
1078   /* Favorite checkbox */
1079   if (features & EMPATHY_INDIVIDUAL_FEATURE_FAVOURITE)
1080     {
1081       item = favourite_menu_item_new_individual (individual);
1082       gtk_menu_shell_append (shell, item);
1083       gtk_widget_show (item);
1084     }
1085
1086   /* Separator & Block */
1087   if (features & EMPATHY_INDIVIDUAL_FEATURE_BLOCK &&
1088       (item = block_menu_item_new_individual (individual)) != NULL) {
1089     GtkWidget *sep;
1090
1091     sep = gtk_separator_menu_item_new ();
1092     gtk_menu_shell_append (shell, sep);
1093     gtk_widget_show (sep);
1094
1095     gtk_menu_shell_append (shell, item);
1096     gtk_widget_show (item);
1097   }
1098
1099   /* Separator & Remove */
1100   if (features & EMPATHY_INDIVIDUAL_FEATURE_REMOVE &&
1101       (item = remove_menu_item_new_individual (self)) != NULL) {
1102     GtkWidget *sep;
1103
1104     sep = gtk_separator_menu_item_new ();
1105     gtk_menu_shell_append (shell, sep);
1106     gtk_widget_show (sep);
1107
1108     gtk_menu_shell_append (shell, item);
1109     gtk_widget_show (item);
1110   }
1111 }
1112
1113 static void
1114 get_property (GObject *object,
1115     guint param_id,
1116     GValue *value,
1117     GParamSpec *pspec)
1118 {
1119   EmpathyIndividualMenuPriv *priv;
1120
1121   priv = GET_PRIV (object);
1122
1123   switch (param_id)
1124     {
1125       case PROP_ACTIVE_GROUP:
1126         g_value_set_string (value, priv->active_group);
1127         break;
1128       case PROP_INDIVIDUAL:
1129         g_value_set_object (value, priv->individual);
1130         break;
1131       case PROP_FEATURES:
1132         g_value_set_flags (value, priv->features);
1133         break;
1134       case PROP_STORE:
1135         g_value_set_object (value, priv->store);
1136         break;
1137       default:
1138         G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
1139         break;
1140     }
1141 }
1142
1143 static void
1144 set_property (GObject *object,
1145     guint param_id,
1146     const GValue *value,
1147     GParamSpec *pspec)
1148 {
1149   EmpathyIndividualMenuPriv *priv;
1150
1151   priv = GET_PRIV (object);
1152
1153   switch (param_id)
1154     {
1155       case PROP_ACTIVE_GROUP:
1156         g_assert (priv->active_group == NULL); /* construct only */
1157         priv->active_group = g_value_dup_string (value);
1158         break;
1159       case PROP_INDIVIDUAL:
1160         priv->individual = g_value_dup_object (value);
1161         break;
1162       case PROP_FEATURES:
1163         priv->features = g_value_get_flags (value);
1164         break;
1165       case PROP_STORE:
1166         priv->store = g_value_dup_object (value); /* read only */
1167         break;
1168       default:
1169         G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
1170         break;
1171     }
1172 }
1173
1174 static void
1175 dispose (GObject *object)
1176 {
1177   EmpathyIndividualMenuPriv *priv = GET_PRIV (object);
1178
1179   tp_clear_object (&priv->individual);
1180   tp_clear_object (&priv->store);
1181
1182   G_OBJECT_CLASS (empathy_individual_menu_parent_class)->dispose (object);
1183 }
1184
1185 static void
1186 finalize (GObject *object)
1187 {
1188   EmpathyIndividualMenuPriv *priv = GET_PRIV (object);
1189
1190   g_free (priv->active_group);
1191
1192   G_OBJECT_CLASS (empathy_individual_menu_parent_class)->finalize (object);
1193 }
1194
1195 static void
1196 empathy_individual_menu_class_init (EmpathyIndividualMenuClass *klass)
1197 {
1198   GObjectClass *object_class = G_OBJECT_CLASS (klass);
1199
1200   object_class->constructed = constructed;
1201   object_class->get_property = get_property;
1202   object_class->set_property = set_property;
1203   object_class->dispose = dispose;
1204   object_class->finalize = finalize;
1205
1206   /**
1207    * gchar *:active-group:
1208    *
1209    * The group the selected roster-contact widget belongs, or NULL.
1210    */
1211   g_object_class_install_property (object_class, PROP_ACTIVE_GROUP,
1212       g_param_spec_string ("active-group",
1213           "Active group",
1214           "The group the selected roster-contact widget belongs, or NULL",
1215           NULL,
1216           G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY | G_PARAM_STATIC_STRINGS));
1217
1218   /**
1219    * EmpathyIndividualMenu:individual:
1220    *
1221    * The #FolksIndividual the menu is for.
1222    */
1223   g_object_class_install_property (object_class, PROP_INDIVIDUAL,
1224       g_param_spec_object ("individual",
1225           "Individual",
1226           "The #FolksIndividual the menu is for.",
1227           FOLKS_TYPE_INDIVIDUAL,
1228           G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY | G_PARAM_STATIC_STRINGS));
1229
1230   /**
1231    * EmpathyIndividualMenu:features:
1232    *
1233    * A set of feature flags controlling which entries are shown.
1234    */
1235   g_object_class_install_property (object_class, PROP_FEATURES,
1236       g_param_spec_flags ("features",
1237           "Features",
1238           "A set of feature flags controlling which entries are shown.",
1239           EMPATHY_TYPE_INDIVIDUAL_FEATURE_FLAGS,
1240           EMPATHY_INDIVIDUAL_FEATURE_NONE,
1241           G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY | G_PARAM_STATIC_STRINGS));
1242
1243   g_object_class_install_property (object_class, PROP_STORE,
1244       g_param_spec_object ("store",
1245           "Store",
1246           "The EmpathyIndividualStore to use to get contact owner",
1247           EMPATHY_TYPE_INDIVIDUAL_STORE,
1248           G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY | G_PARAM_STATIC_STRINGS));
1249
1250   signals[MENU_ITEM_ACTIVATED] =
1251       g_signal_new ("menu-item-activated",
1252           G_TYPE_FROM_CLASS (klass),
1253           G_SIGNAL_RUN_LAST,
1254           0,
1255           NULL, NULL,
1256           g_cclosure_marshal_generic,
1257           G_TYPE_NONE,
1258           0);
1259
1260   g_type_class_add_private (object_class, sizeof (EmpathyIndividualMenuPriv));
1261 }
1262
1263 GtkWidget *
1264 empathy_individual_menu_new (FolksIndividual *individual,
1265     const gchar *active_group,
1266     EmpathyIndividualFeatureFlags features,
1267     EmpathyIndividualStore *store)
1268 {
1269   g_return_val_if_fail (FOLKS_IS_INDIVIDUAL (individual), NULL);
1270   g_return_val_if_fail (store == NULL ||
1271       EMPATHY_IS_INDIVIDUAL_STORE (store), NULL);
1272   g_return_val_if_fail (features != EMPATHY_INDIVIDUAL_FEATURE_NONE, NULL);
1273
1274   return g_object_new (EMPATHY_TYPE_INDIVIDUAL_MENU,
1275       "active-group", active_group,
1276       "individual", individual,
1277       "features", features,
1278       "store", store,
1279       NULL);
1280 }
1281
1282 /* Like menu_item_set_first_contact(), but always operates upon the given
1283  * contact. If the contact is non-NULL, it is assumed that the menu entry should
1284  * be sensitive. */
1285 static gboolean
1286 menu_item_set_contact (GtkWidget *item,
1287     EmpathyContact *contact,
1288     GCallback activate_callback,
1289     EmpathyActionType action_type)
1290 {
1291   gboolean can_do_action = FALSE;
1292
1293   if (contact != NULL)
1294     can_do_action = empathy_contact_can_do_action (contact, action_type);
1295   gtk_widget_set_sensitive (item, can_do_action);
1296
1297   if (can_do_action == TRUE)
1298     {
1299       /* We want to make sure that the EmpathyContact stays alive while the
1300        * signal is connected. */
1301       g_signal_connect_data (item, "activate", G_CALLBACK (activate_callback),
1302           g_object_ref (contact), (GClosureNotify) g_object_unref, 0);
1303     }
1304
1305   return can_do_action;
1306 }
1307
1308 /**
1309  * Set the given menu @item to call @activate_callback using the TpContact
1310  * (associated with @individual) with the highest availability who is also valid
1311  * whenever @item is activated.
1312  *
1313  * @action_type is the type of action performed by the menu entry; this is used
1314  * so that only contacts which can perform that action (e.g. are capable of
1315  * receiving video calls) are selected, as appropriate.
1316  */
1317 static GtkWidget *
1318 menu_item_set_first_contact (GtkWidget *item,
1319     FolksIndividual *individual,
1320     GCallback activate_callback,
1321     EmpathyActionType action_type)
1322 {
1323   EmpathyContact *best_contact;
1324
1325   best_contact = empathy_contact_dup_best_for_action (individual, action_type);
1326   menu_item_set_contact (item, best_contact, G_CALLBACK (activate_callback),
1327       action_type);
1328   tp_clear_object (&best_contact);
1329
1330   return item;
1331 }
1332
1333 static void
1334 emit_menu_item_activated (GtkMenuItem *item)
1335 {
1336   EmpathyIndividualMenu *self;
1337
1338   self = EMPATHY_INDIVIDUAL_MENU (g_object_get_data (G_OBJECT (item),
1339       "individual-menu"));
1340   g_signal_emit (self, signals [MENU_ITEM_ACTIVATED], 0);
1341 }
1342
1343 static void
1344 empathy_individual_chat_menu_item_activated (GtkMenuItem *item,
1345   EmpathyContact *contact)
1346 {
1347   g_return_if_fail (EMPATHY_IS_CONTACT (contact));
1348
1349   empathy_chat_with_contact (contact, empathy_get_current_action_time ());
1350
1351   emit_menu_item_activated (item);
1352 }
1353
1354 static GtkWidget *
1355 chat_menu_item_new (EmpathyIndividualMenu *self)
1356 {
1357   GtkWidget *item;
1358   GtkWidget *image;
1359
1360   item = gtk_image_menu_item_new_with_mnemonic (_("_Chat"));
1361   image = gtk_image_new_from_icon_name (EMPATHY_IMAGE_MESSAGE,
1362       GTK_ICON_SIZE_MENU);
1363   gtk_image_menu_item_set_image (GTK_IMAGE_MENU_ITEM (item), image);
1364   gtk_widget_show (image);
1365
1366   g_object_set_data (G_OBJECT (item), "individual-menu", self);
1367
1368   return item;
1369 }
1370
1371 static GtkWidget *
1372 chat_menu_item_new_individual (EmpathyIndividualMenu *self,
1373     FolksIndividual *individual)
1374 {
1375   GtkWidget *item;
1376
1377   g_return_val_if_fail (FOLKS_IS_INDIVIDUAL (individual) &&
1378       empathy_folks_individual_contains_contact (individual), NULL);
1379
1380   item = chat_menu_item_new (self);
1381
1382   menu_item_set_first_contact (item, individual,
1383       G_CALLBACK (empathy_individual_chat_menu_item_activated),
1384       EMPATHY_ACTION_CHAT);
1385
1386   return item;
1387 }
1388
1389 static void
1390 empathy_individual_sms_menu_item_activated (GtkMenuItem *item,
1391   EmpathyContact *contact)
1392 {
1393   g_return_if_fail (EMPATHY_IS_CONTACT (contact));
1394
1395   empathy_sms_contact_id (
1396       empathy_contact_get_account (contact),
1397       empathy_contact_get_id (contact),
1398       empathy_get_current_action_time (),
1399       NULL, NULL);
1400
1401   emit_menu_item_activated (item);
1402 }
1403
1404 static GtkWidget *
1405 sms_menu_item_new (EmpathyIndividualMenu *self)
1406 {
1407   GtkWidget *item;
1408   GtkWidget *image;
1409
1410   item = gtk_image_menu_item_new_with_mnemonic (_("_SMS"));
1411   image = gtk_image_new_from_icon_name (EMPATHY_IMAGE_SMS,
1412       GTK_ICON_SIZE_MENU);
1413   gtk_image_menu_item_set_image (GTK_IMAGE_MENU_ITEM (item), image);
1414   gtk_widget_show (image);
1415
1416   g_object_set_data (G_OBJECT (item), "individual-menu", self);
1417
1418   return item;
1419 }
1420
1421 static GtkWidget *
1422 sms_menu_item_new_individual (EmpathyIndividualMenu *self,
1423     FolksIndividual *individual)
1424 {
1425   GtkWidget *item;
1426
1427   g_return_val_if_fail (FOLKS_IS_INDIVIDUAL (individual) &&
1428       empathy_folks_individual_contains_contact (individual), NULL);
1429
1430   item = sms_menu_item_new (self);
1431
1432   menu_item_set_first_contact (item, individual,
1433       G_CALLBACK (empathy_individual_sms_menu_item_activated),
1434       EMPATHY_ACTION_SMS);
1435
1436   return item;
1437 }
1438
1439 static void
1440 empathy_individual_audio_call_menu_item_activated (GtkMenuItem *item,
1441   EmpathyContact *contact)
1442 {
1443   g_return_if_fail (EMPATHY_IS_CONTACT (contact));
1444
1445   empathy_call_new_with_streams (empathy_contact_get_id (contact),
1446       empathy_contact_get_account (contact),
1447       FALSE, empathy_get_current_action_time ());
1448
1449   emit_menu_item_activated (item);
1450 }
1451
1452 static GtkWidget *
1453 audio_call_menu_item_new (EmpathyIndividualMenu *self)
1454 {
1455   GtkWidget *item;
1456   GtkWidget *image;
1457
1458   item = gtk_image_menu_item_new_with_mnemonic (C_("menu item", "_Audio Call"));
1459   image = gtk_image_new_from_icon_name (EMPATHY_IMAGE_VOIP, GTK_ICON_SIZE_MENU);
1460   gtk_image_menu_item_set_image (GTK_IMAGE_MENU_ITEM (item), image);
1461   gtk_widget_show (image);
1462
1463   g_object_set_data (G_OBJECT (item), "individual-menu", self);
1464
1465   return item;
1466 }
1467
1468 GtkWidget *
1469 empathy_individual_audio_call_menu_item_new_individual (
1470     EmpathyIndividualMenu *self,
1471     FolksIndividual *individual)
1472 {
1473   GtkWidget *item;
1474
1475   g_return_val_if_fail (FOLKS_IS_INDIVIDUAL (individual), NULL);
1476
1477   item = audio_call_menu_item_new (self);
1478
1479   menu_item_set_first_contact (item, individual,
1480       G_CALLBACK (empathy_individual_audio_call_menu_item_activated),
1481       EMPATHY_ACTION_AUDIO_CALL);
1482
1483   return item;
1484 }
1485
1486 static void
1487 empathy_individual_video_call_menu_item_activated (GtkMenuItem *item,
1488   EmpathyContact *contact)
1489 {
1490   g_return_if_fail (EMPATHY_IS_CONTACT (contact));
1491
1492   empathy_call_new_with_streams (empathy_contact_get_id (contact),
1493       empathy_contact_get_account (contact),
1494       TRUE, empathy_get_current_action_time ());
1495
1496   emit_menu_item_activated (item);
1497 }
1498
1499 static GtkWidget *
1500 video_call_menu_item_new (EmpathyIndividualMenu *self)
1501 {
1502   GtkWidget *item;
1503   GtkWidget *image;
1504
1505   item = gtk_image_menu_item_new_with_mnemonic (C_("menu item", "_Video Call"));
1506   image = gtk_image_new_from_icon_name (EMPATHY_IMAGE_VIDEO_CALL,
1507       GTK_ICON_SIZE_MENU);
1508   gtk_image_menu_item_set_image (GTK_IMAGE_MENU_ITEM (item), image);
1509   gtk_widget_show (image);
1510
1511   g_object_set_data (G_OBJECT (item), "individual-menu", self);
1512
1513   return item;
1514 }
1515
1516 static void
1517 check_camera_available (GtkWidget *item)
1518 {
1519   TpawCameraMonitor *monitor;
1520
1521   /* Only follow available cameras if the contact can do Video calls */
1522   if (gtk_widget_get_sensitive (item))
1523     {
1524       monitor = tpaw_camera_monitor_dup_singleton ();
1525       g_object_set_data_full (G_OBJECT (item),
1526           "monitor", monitor, g_object_unref);
1527       g_object_bind_property (monitor, "available", item, "sensitive",
1528           G_BINDING_SYNC_CREATE);
1529     }
1530 }
1531
1532 GtkWidget *
1533 empathy_individual_video_call_menu_item_new_individual (
1534     EmpathyIndividualMenu *self,
1535     FolksIndividual *individual)
1536 {
1537   GtkWidget *item;
1538
1539   g_return_val_if_fail (FOLKS_IS_INDIVIDUAL (individual), NULL);
1540
1541   item = video_call_menu_item_new (self);
1542
1543   menu_item_set_first_contact (item, individual,
1544       G_CALLBACK (empathy_individual_video_call_menu_item_activated),
1545       EMPATHY_ACTION_VIDEO_CALL);
1546
1547   check_camera_available (item);
1548
1549   return item;
1550 }
1551
1552
1553 static void
1554 empathy_individual_log_menu_item_activated (GtkMenuItem *item,
1555   EmpathyContact *contact)
1556 {
1557   g_return_if_fail (EMPATHY_IS_CONTACT (contact));
1558
1559   empathy_log_window_show (empathy_contact_get_account (contact),
1560       empathy_contact_get_id (contact), FALSE, NULL);
1561 }
1562
1563 static GtkWidget *
1564 log_menu_item_new (void)
1565 {
1566   GtkWidget *item;
1567   GtkWidget *image;
1568
1569   item = gtk_image_menu_item_new_with_mnemonic (_("_Previous Conversations"));
1570   image = gtk_image_new_from_icon_name (EMPATHY_IMAGE_LOG, GTK_ICON_SIZE_MENU);
1571   gtk_image_menu_item_set_image (GTK_IMAGE_MENU_ITEM (item), image);
1572   gtk_widget_show (image);
1573
1574   return item;
1575 }
1576
1577 static GtkWidget *
1578 log_menu_item_new_individual (FolksIndividual *individual)
1579 {
1580   GtkWidget *item;
1581
1582   g_return_val_if_fail (FOLKS_IS_INDIVIDUAL (individual), NULL);
1583
1584   item = log_menu_item_new ();
1585
1586   menu_item_set_first_contact (item, individual,
1587       G_CALLBACK (empathy_individual_log_menu_item_activated),
1588       EMPATHY_ACTION_VIEW_LOGS);
1589
1590   return item;
1591 }
1592
1593 static void
1594 empathy_individual_file_transfer_menu_item_activated (GtkMenuItem *item,
1595     EmpathyContact *contact)
1596 {
1597   g_return_if_fail (EMPATHY_IS_CONTACT (contact));
1598
1599   empathy_send_file_with_file_chooser (contact);
1600
1601   emit_menu_item_activated (item);
1602 }
1603
1604 static GtkWidget *
1605 file_transfer_menu_item_new (EmpathyIndividualMenu *self)
1606 {
1607   GtkWidget *item;
1608   GtkWidget *image;
1609
1610   item = gtk_image_menu_item_new_with_mnemonic (_("Send File"));
1611   image = gtk_image_new_from_icon_name (EMPATHY_IMAGE_DOCUMENT_SEND,
1612       GTK_ICON_SIZE_MENU);
1613   gtk_image_menu_item_set_image (GTK_IMAGE_MENU_ITEM (item), image);
1614   gtk_widget_show (image);
1615
1616   g_object_set_data (G_OBJECT (item), "individual-menu", self);
1617
1618   return item;
1619 }
1620
1621 static GtkWidget *
1622 file_transfer_menu_item_new_individual (EmpathyIndividualMenu *self,
1623     FolksIndividual *individual)
1624 {
1625   GtkWidget *item;
1626
1627   g_return_val_if_fail (FOLKS_IS_INDIVIDUAL (individual), NULL);
1628
1629   item = file_transfer_menu_item_new (self);
1630
1631   menu_item_set_first_contact (item, individual,
1632       G_CALLBACK (empathy_individual_file_transfer_menu_item_activated),
1633       EMPATHY_ACTION_SEND_FILE);
1634
1635   return item;
1636 }
1637
1638 static void
1639 empathy_individual_share_my_desktop_menu_item_activated (GtkMenuItem *item,
1640     EmpathyContact *contact)
1641 {
1642   g_return_if_fail (EMPATHY_IS_CONTACT (contact));
1643
1644   empathy_share_my_desktop_share_with_contact (contact);
1645
1646   emit_menu_item_activated (item);
1647 }
1648
1649 static GtkWidget *
1650 share_my_desktop_menu_item_new (EmpathyIndividualMenu *self)
1651 {
1652   GtkWidget *item;
1653   GtkWidget *image;
1654
1655   item = gtk_image_menu_item_new_with_mnemonic (_("Share My Desktop"));
1656   image = gtk_image_new_from_icon_name (GTK_STOCK_NETWORK, GTK_ICON_SIZE_MENU);
1657   gtk_image_menu_item_set_image (GTK_IMAGE_MENU_ITEM (item), image);
1658   gtk_widget_show (image);
1659
1660   g_object_set_data (G_OBJECT (item), "individual-menu", self);
1661
1662   return item;
1663 }
1664
1665 static GtkWidget *
1666 share_my_desktop_menu_item_new_individual (EmpathyIndividualMenu *self,
1667     FolksIndividual *individual)
1668 {
1669   GtkWidget *item;
1670
1671   g_return_val_if_fail (FOLKS_IS_INDIVIDUAL (individual), NULL);
1672
1673   item = share_my_desktop_menu_item_new (self);
1674
1675   menu_item_set_first_contact (item, individual,
1676       G_CALLBACK (empathy_individual_share_my_desktop_menu_item_activated),
1677       EMPATHY_ACTION_SHARE_MY_DESKTOP);
1678
1679   return item;
1680 }
1681
1682 static void
1683 favourite_menu_item_toggled_cb (GtkCheckMenuItem *item,
1684   FolksIndividual *individual)
1685 {
1686   folks_favourite_details_set_is_favourite (
1687       FOLKS_FAVOURITE_DETAILS (individual),
1688       gtk_check_menu_item_get_active (item));
1689 }
1690
1691 static GtkWidget *
1692 favourite_menu_item_new_individual (FolksIndividual *individual)
1693 {
1694   GtkWidget *item;
1695
1696   item = gtk_check_menu_item_new_with_label (_("Favorite"));
1697
1698   gtk_check_menu_item_set_active (GTK_CHECK_MENU_ITEM (item),
1699       folks_favourite_details_get_is_favourite (
1700           FOLKS_FAVOURITE_DETAILS (individual)));
1701
1702   g_signal_connect (item, "toggled",
1703       G_CALLBACK (favourite_menu_item_toggled_cb), individual);
1704
1705   return item;
1706 }
1707
1708 static void
1709 individual_info_menu_item_activate_cb (GtkMenuItem *item,
1710     FolksIndividual *individual)
1711 {
1712   empathy_display_individual_info (individual);
1713 }
1714
1715 static GtkWidget *
1716 info_menu_item_new_individual (FolksIndividual *individual)
1717 {
1718   GtkWidget *item;
1719   GtkWidget *image;
1720
1721   g_return_val_if_fail (FOLKS_IS_INDIVIDUAL (individual), NULL);
1722   g_return_val_if_fail (empathy_folks_individual_contains_contact (individual),
1723       NULL);
1724
1725   item = gtk_image_menu_item_new_with_mnemonic (_("Infor_mation"));
1726   image = gtk_image_new_from_icon_name (EMPATHY_IMAGE_CONTACT_INFORMATION,
1727                 GTK_ICON_SIZE_MENU);
1728   gtk_image_menu_item_set_image (GTK_IMAGE_MENU_ITEM (item), image);
1729   gtk_widget_show (image);
1730
1731   g_signal_connect (item, "activate",
1732           G_CALLBACK (individual_info_menu_item_activate_cb),
1733           individual);
1734
1735   return item;
1736 }
1737
1738 static void
1739 individual_edit_menu_item_activate_cb (FolksIndividual *individual)
1740 {
1741   empathy_individual_edit_dialog_show (individual, NULL);
1742 }
1743
1744 static GtkWidget *
1745 edit_menu_item_new_individual (FolksIndividual *individual)
1746 {
1747   EmpathyIndividualManager *manager;
1748   GtkWidget *item;
1749   GtkWidget *image;
1750   gboolean enable = FALSE;
1751   EmpathyContact *contact;
1752
1753   g_return_val_if_fail (FOLKS_IS_INDIVIDUAL (individual), NULL);
1754
1755   contact = empathy_contact_dup_from_folks_individual (individual);
1756
1757   g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), NULL);
1758
1759   if (empathy_individual_manager_initialized ())
1760     {
1761       TpConnection *connection;
1762
1763       manager = empathy_individual_manager_dup_singleton ();
1764       connection = empathy_contact_get_connection (contact);
1765
1766       enable = (empathy_connection_can_alias_personas (connection,
1767                                                        individual) &&
1768                 empathy_connection_can_group_personas (connection, individual));
1769
1770       g_object_unref (manager);
1771     }
1772
1773   item = gtk_image_menu_item_new_with_mnemonic (
1774       C_("Edit individual (contextual menu)", "_Edit"));
1775   image = gtk_image_new_from_icon_name (GTK_STOCK_EDIT, GTK_ICON_SIZE_MENU);
1776   gtk_image_menu_item_set_image (GTK_IMAGE_MENU_ITEM (item), image);
1777   gtk_widget_show (image);
1778
1779   gtk_widget_set_sensitive (item, enable);
1780
1781   g_signal_connect_swapped (item, "activate",
1782       G_CALLBACK (individual_edit_menu_item_activate_cb), individual);
1783
1784   g_object_unref (contact);
1785
1786   return item;
1787 }
1788
1789 typedef struct
1790 {
1791   FolksIndividual *individual;
1792   EmpathyContact *contact;
1793   EmpathyChatroom *chatroom;
1794 } RoomSubMenuData;
1795
1796 static RoomSubMenuData *
1797 room_sub_menu_data_new (FolksIndividual *individual,
1798     EmpathyContact *contact,
1799     EmpathyChatroom *chatroom)
1800 {
1801   RoomSubMenuData *data;
1802
1803   data = g_slice_new0 (RoomSubMenuData);
1804   if (individual != NULL)
1805     data->individual = g_object_ref (individual);
1806   if (contact != NULL)
1807     data->contact = g_object_ref (contact);
1808   data->chatroom = g_object_ref (chatroom);
1809
1810   return data;
1811 }
1812
1813 static void
1814 room_sub_menu_data_free (RoomSubMenuData *data)
1815 {
1816   tp_clear_object (&data->individual);
1817   tp_clear_object (&data->contact);
1818   g_object_unref (data->chatroom);
1819   g_slice_free (RoomSubMenuData, data);
1820 }
1821
1822 static void
1823 room_sub_menu_activate_cb (GtkWidget *item,
1824          RoomSubMenuData *data)
1825 {
1826   EmpathyTpChat *chat;
1827   EmpathyChatroomManager *mgr;
1828   EmpathyContact *contact = NULL;
1829
1830   chat = empathy_chatroom_get_tp_chat (data->chatroom);
1831   if (chat == NULL)
1832     {
1833       /* channel was invalidated. Ignoring */
1834       return;
1835     }
1836
1837   mgr = empathy_chatroom_manager_dup_singleton (NULL);
1838
1839   if (data->contact != NULL)
1840     contact = g_object_ref (data->contact);
1841   else
1842     {
1843       GeeSet *personas;
1844       GeeIterator *iter;
1845
1846       /* find the first of this Individual's contacts who can join this room */
1847       personas = folks_individual_get_personas (data->individual);
1848
1849       iter = gee_iterable_iterator (GEE_ITERABLE (personas));
1850       while (gee_iterator_next (iter) && (contact == NULL))
1851         {
1852           TpfPersona *persona = gee_iterator_get (iter);
1853           TpContact *tp_contact;
1854           GList *rooms;
1855
1856           if (empathy_folks_persona_is_interesting (FOLKS_PERSONA (persona)))
1857             {
1858               tp_contact = tpf_persona_get_contact (persona);
1859               if (tp_contact != NULL)
1860                 {
1861                   contact = empathy_contact_dup_from_tp_contact (tp_contact);
1862
1863                   rooms = empathy_chatroom_manager_get_chatrooms (mgr,
1864                       empathy_contact_get_account (contact));
1865
1866                   if (g_list_find (rooms, data->chatroom) == NULL)
1867                     g_clear_object (&contact);
1868
1869                   /* if contact != NULL here, we've found our match */
1870
1871                   g_list_free (rooms);
1872                 }
1873             }
1874           g_clear_object (&persona);
1875         }
1876       g_clear_object (&iter);
1877     }
1878
1879   g_object_unref (mgr);
1880
1881   if (contact == NULL)
1882     {
1883       /* contact disappeared. Ignoring */
1884       goto out;
1885     }
1886
1887   g_return_if_fail (EMPATHY_IS_CONTACT (contact));
1888
1889   /* send invitation */
1890   empathy_tp_chat_add (chat, contact, _("Inviting you to this room"));
1891
1892 out:
1893   g_object_unref (contact);
1894 }
1895
1896 static GtkWidget *
1897 create_room_sub_menu (FolksIndividual *individual,
1898                       EmpathyContact *contact,
1899                       EmpathyChatroom *chatroom)
1900 {
1901   GtkWidget *item;
1902   RoomSubMenuData *data;
1903
1904   item = gtk_menu_item_new_with_label (empathy_chatroom_get_name (chatroom));
1905   data = room_sub_menu_data_new (individual, contact, chatroom);
1906   g_signal_connect_data (item, "activate",
1907       G_CALLBACK (room_sub_menu_activate_cb), data,
1908       (GClosureNotify) room_sub_menu_data_free, 0);
1909
1910   return item;
1911 }
1912
1913 static GtkWidget *
1914 invite_menu_item_new (FolksIndividual *individual,
1915     EmpathyContact *contact)
1916 {
1917   GtkWidget *item;
1918   GtkWidget *image;
1919   GtkWidget *room_item;
1920   EmpathyChatroomManager *mgr;
1921   GList *rooms = NULL;
1922   GList *names = NULL;
1923   GList *l;
1924   GtkWidget *submenu = NULL;
1925   /* map of chat room names to their objects; just a utility to remove
1926    * duplicates and to make construction of the alphabetized list easier */
1927   GHashTable *name_room_map;
1928
1929   g_return_val_if_fail (FOLKS_IS_INDIVIDUAL (individual) ||
1930       EMPATHY_IS_CONTACT (contact),
1931       NULL);
1932
1933   name_room_map = g_hash_table_new_full (g_str_hash, g_str_equal, NULL,
1934       g_object_unref);
1935
1936   item = gtk_image_menu_item_new_with_mnemonic (_("_Invite to Chat Room"));
1937   image = gtk_image_new_from_icon_name (EMPATHY_IMAGE_GROUP_MESSAGE,
1938       GTK_ICON_SIZE_MENU);
1939   gtk_image_menu_item_set_image (GTK_IMAGE_MENU_ITEM (item), image);
1940
1941   mgr = empathy_chatroom_manager_dup_singleton (NULL);
1942
1943   if (contact != NULL)
1944     {
1945       rooms = empathy_chatroom_manager_get_chatrooms (mgr,
1946           empathy_contact_get_account (contact));
1947     }
1948   else
1949     {
1950       GeeSet *personas;
1951       GeeIterator *iter;
1952
1953       /* find the first of this Individual's contacts who can join this room */
1954       personas = folks_individual_get_personas (individual);
1955       iter = gee_iterable_iterator (GEE_ITERABLE (personas));
1956       while (gee_iterator_next (iter))
1957         {
1958           TpfPersona *persona = gee_iterator_get (iter);
1959           GList *rooms_cur;
1960           TpContact *tp_contact;
1961           EmpathyContact *contact_cur;
1962
1963           if (empathy_folks_persona_is_interesting (FOLKS_PERSONA (persona)))
1964             {
1965               tp_contact = tpf_persona_get_contact (persona);
1966               if (tp_contact != NULL)
1967                 {
1968                   contact_cur = empathy_contact_dup_from_tp_contact (
1969                       tp_contact);
1970
1971                   rooms_cur = empathy_chatroom_manager_get_chatrooms (mgr,
1972                       empathy_contact_get_account (contact_cur));
1973                   rooms = g_list_concat (rooms, rooms_cur);
1974
1975                   g_object_unref (contact_cur);
1976                 }
1977             }
1978           g_clear_object (&persona);
1979         }
1980       g_clear_object (&iter);
1981     }
1982
1983   /* alphabetize the rooms */
1984   for (l = rooms; l != NULL; l = g_list_next (l))
1985     {
1986       EmpathyChatroom *chatroom = l->data;
1987       gboolean existed;
1988
1989       if (empathy_chatroom_get_tp_chat (chatroom) != NULL)
1990         {
1991           const gchar *name;
1992
1993           name = empathy_chatroom_get_name (chatroom);
1994           existed = (g_hash_table_lookup (name_room_map, name) != NULL);
1995           g_hash_table_insert (name_room_map, (gpointer) name,
1996               g_object_ref (chatroom));
1997
1998           /* this will take care of duplicates in rooms */
1999           if (!existed)
2000             {
2001               names = g_list_insert_sorted (names, (gpointer) name,
2002                   (GCompareFunc) g_strcmp0);
2003             }
2004         }
2005     }
2006
2007   for (l = names; l != NULL; l = g_list_next (l))
2008     {
2009       const gchar *name = l->data;
2010       EmpathyChatroom *chatroom;
2011
2012       if (G_UNLIKELY (submenu == NULL))
2013         submenu = gtk_menu_new ();
2014
2015       chatroom = g_hash_table_lookup (name_room_map, name);
2016       room_item = create_room_sub_menu (individual, contact, chatroom);
2017       gtk_menu_shell_append ((GtkMenuShell *) submenu, room_item);
2018       gtk_widget_show (room_item);
2019     }
2020
2021   if (submenu)
2022     gtk_menu_item_set_submenu (GTK_MENU_ITEM (item), submenu);
2023   else
2024     gtk_widget_set_sensitive (item, FALSE);
2025
2026   gtk_widget_show (image);
2027
2028   g_hash_table_unref (name_room_map);
2029   g_object_unref (mgr);
2030   g_list_free (names);
2031   g_list_free (rooms);
2032
2033   return item;
2034 }
2035
2036 static void
2037 add_menu_item_activated (GtkMenuItem *item,
2038     TpContact *tp_contact)
2039 {
2040   GtkWidget *toplevel;
2041   FolksIndividual *individual;
2042
2043   toplevel = gtk_widget_get_toplevel (GTK_WIDGET (item));
2044   if (!gtk_widget_is_toplevel (toplevel) || !GTK_IS_WINDOW (toplevel))
2045     toplevel = NULL;
2046
2047   individual = empathy_ensure_individual_from_tp_contact (tp_contact);
2048
2049   empathy_new_individual_dialog_show_with_individual (GTK_WINDOW (toplevel),
2050       individual);
2051
2052   g_object_unref (individual);
2053 }
2054
2055 static GtkWidget *
2056 add_menu_item_new_individual (EmpathyIndividualMenu *self,
2057     FolksIndividual *individual)
2058 {
2059   EmpathyIndividualMenuPriv *priv = GET_PRIV (self);
2060   GtkWidget *item, *image;
2061   GeeSet *personas;
2062   GeeIterator *iter;
2063   TpContact *to_add = NULL;
2064
2065   /* find the first of this Individual's personas which are not in our contact
2066    * list. */
2067   personas = folks_individual_get_personas (individual);
2068   iter = gee_iterable_iterator (GEE_ITERABLE (personas));
2069   while (gee_iterator_next (iter))
2070     {
2071       TpfPersona *persona = gee_iterator_get (iter);
2072       TpContact *contact;
2073       TpConnection *conn;
2074
2075       if (!TPF_IS_PERSONA (persona))
2076         goto next;
2077
2078       contact = tpf_persona_get_contact (persona);
2079       if (contact == NULL)
2080         goto next;
2081
2082       /* be sure to use a not channel specific contact.
2083        * TODO: Ideally tp-glib should do this for us (fdo #42702)*/
2084       if (EMPATHY_IS_INDIVIDUAL_STORE_CHANNEL (priv->store))
2085         {
2086           TpChannel *channel;
2087           TpChannelGroupFlags flags;
2088
2089           channel = empathy_individual_store_channel_get_channel (
2090               EMPATHY_INDIVIDUAL_STORE_CHANNEL (priv->store));
2091
2092           flags = tp_channel_group_get_flags (channel);
2093           if ((flags & TP_CHANNEL_GROUP_FLAG_CHANNEL_SPECIFIC_HANDLES) != 0)
2094             {
2095               /* Channel uses channel specific handles (thanks XMPP...) */
2096               contact = tp_channel_group_get_contact_owner (channel, contact);
2097
2098               /* If we don't know the owner, we can't add the contact */
2099               if (contact == NULL)
2100                 goto next;
2101             }
2102         }
2103
2104       conn = tp_contact_get_connection (contact);
2105       if (conn == NULL)
2106         goto next;
2107
2108       /* No point to try adding a contact if the CM doesn't support it */
2109       if (!tp_connection_get_can_change_contact_list (conn))
2110         goto next;
2111
2112       /* Can't add ourself */
2113       if (tp_connection_get_self_contact (conn) == contact)
2114         goto next;
2115
2116       if (tp_contact_get_subscribe_state (contact) == TP_SUBSCRIPTION_STATE_YES)
2117         goto next;
2118
2119       g_object_unref (persona);
2120       to_add = contact;
2121       break;
2122
2123 next:
2124       g_object_unref (persona);
2125     }
2126
2127   g_object_unref (iter);
2128
2129   if (to_add == NULL)
2130     return NULL;
2131
2132   item = gtk_image_menu_item_new_with_mnemonic (_("_Add Contact…"));
2133   image = gtk_image_new_from_icon_name (GTK_STOCK_ADD, GTK_ICON_SIZE_MENU);
2134   gtk_image_menu_item_set_image (GTK_IMAGE_MENU_ITEM (item), image);
2135
2136   g_signal_connect_data (item, "activate",
2137       G_CALLBACK (add_menu_item_activated),
2138       g_object_ref (to_add), (GClosureNotify) g_object_unref, 0);
2139
2140   return item;
2141 }