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