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