]> git.0d.be Git - empathy.git/blob - libempathy-gtk/empathy-individual-menu.c
b891854dadb66aaaf4f756aa49b592a211fcdbae
[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 individual_info_menu_item_activate_cb (FolksIndividual *individual)
1296 {
1297   GDesktopAppInfo *desktop_info;
1298   gchar *cmd;
1299   GAppInfo *app_info;
1300   GError *error = NULL;
1301   GdkAppLaunchContext *context = NULL;
1302   GdkDisplay *display;
1303
1304   /* Start gnome-contacts */
1305   display = gdk_display_get_default ();
1306   context = gdk_display_get_app_launch_context (display);
1307
1308   desktop_info = g_desktop_app_info_new ("gnome-contacts.desktop");
1309   if (desktop_info == NULL)
1310     {
1311       GtkWidget *dialog;
1312
1313       DEBUG ("gnome-contacts not installed");
1314
1315       dialog = gtk_message_dialog_new (NULL, GTK_DIALOG_MODAL,
1316           GTK_MESSAGE_ERROR, GTK_BUTTONS_CLOSE,
1317           _("gnome-contacts not installed"));
1318
1319       gtk_message_dialog_format_secondary_text (GTK_MESSAGE_DIALOG (dialog),
1320           _("Please install gnome-contacts to access contacts details."));
1321
1322       g_signal_connect_swapped (dialog, "response",
1323           G_CALLBACK (gtk_widget_destroy),
1324           dialog);
1325
1326       gtk_widget_show (dialog);
1327       return;
1328     }
1329
1330   /* glib doesn't have API to start a desktop file with args... (#637875) */
1331   cmd = g_strdup_printf ("%s -i %s", g_app_info_get_commandline (
1332         (GAppInfo *) desktop_info), folks_individual_get_id (individual));
1333
1334   app_info = g_app_info_create_from_commandline (cmd, NULL, 0, &error);
1335   if (app_info == NULL)
1336     {
1337       DEBUG ("Failed to create app_info: %s", error->message);
1338       g_error_free (error);
1339       return;
1340     }
1341
1342   if (!g_app_info_launch (app_info, NULL, (GAppLaunchContext *) context,
1343         &error))
1344     {
1345       g_critical ("Failed to start gnome-contacts: %s", error->message);
1346       g_error_free (error);
1347     }
1348
1349   g_object_unref (desktop_info);
1350   g_object_unref (app_info);
1351 }
1352
1353 static GtkWidget *
1354 empathy_individual_info_menu_item_new (FolksIndividual *individual)
1355 {
1356   GtkWidget *item;
1357   GtkWidget *image;
1358
1359   g_return_val_if_fail (FOLKS_IS_INDIVIDUAL (individual), NULL);
1360   g_return_val_if_fail (empathy_folks_individual_contains_contact (individual),
1361       NULL);
1362
1363   item = gtk_image_menu_item_new_with_mnemonic (_("Infor_mation"));
1364   image = gtk_image_new_from_icon_name (EMPATHY_IMAGE_CONTACT_INFORMATION,
1365                 GTK_ICON_SIZE_MENU);
1366   gtk_image_menu_item_set_image (GTK_IMAGE_MENU_ITEM (item), image);
1367   gtk_widget_show (image);
1368
1369   g_signal_connect_swapped (item, "activate",
1370           G_CALLBACK (individual_info_menu_item_activate_cb),
1371           individual);
1372
1373   return item;
1374 }
1375
1376 static void
1377 individual_edit_menu_item_activate_cb (FolksIndividual *individual)
1378 {
1379   empathy_individual_edit_dialog_show (individual, NULL);
1380 }
1381
1382 static GtkWidget *
1383 empathy_individual_edit_menu_item_new (FolksIndividual *individual)
1384 {
1385   EmpathyIndividualManager *manager;
1386   GtkWidget *item;
1387   GtkWidget *image;
1388   gboolean enable = FALSE;
1389   EmpathyContact *contact;
1390
1391   g_return_val_if_fail (FOLKS_IS_INDIVIDUAL (individual), NULL);
1392
1393   contact = empathy_contact_dup_from_folks_individual (individual);
1394
1395   g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), NULL);
1396
1397   if (empathy_individual_manager_initialized ())
1398     {
1399       TpConnection *connection;
1400
1401       manager = empathy_individual_manager_dup_singleton ();
1402       connection = empathy_contact_get_connection (contact);
1403
1404       enable = (empathy_connection_can_alias_personas (connection,
1405                                                        individual) &&
1406                 empathy_connection_can_group_personas (connection, individual));
1407
1408       g_object_unref (manager);
1409     }
1410
1411   item = gtk_image_menu_item_new_with_mnemonic (
1412       C_("Edit individual (contextual menu)", "_Edit"));
1413   image = gtk_image_new_from_icon_name (GTK_STOCK_EDIT, GTK_ICON_SIZE_MENU);
1414   gtk_image_menu_item_set_image (GTK_IMAGE_MENU_ITEM (item), image);
1415   gtk_widget_show (image);
1416
1417   gtk_widget_set_sensitive (item, enable);
1418
1419   g_signal_connect_swapped (item, "activate",
1420       G_CALLBACK (individual_edit_menu_item_activate_cb), individual);
1421
1422   g_object_unref (contact);
1423
1424   return item;
1425 }
1426
1427 static GtkWidget *
1428 empathy_individual_link_menu_item_new (FolksIndividual *individual)
1429 {
1430   GtkWidget *item;
1431   /*GtkWidget *image;*/
1432
1433   g_return_val_if_fail (FOLKS_IS_INDIVIDUAL (individual), NULL);
1434
1435   item = gtk_image_menu_item_new_with_mnemonic (
1436       /* Translators: this is a verb meaning "to connect two contacts together
1437        * to form a meta-contact". */
1438       C_("Link individual (contextual menu)", "_Link Contacts…"));
1439   /* TODO */
1440   /*image = gtk_image_new_from_icon_name (GTK_STOCK_EDIT, GTK_ICON_SIZE_MENU);
1441   gtk_image_menu_item_set_image (GTK_IMAGE_MENU_ITEM (item), image);
1442   gtk_widget_show (image);*/
1443
1444   /* Only allow trusted Individuals to be linked */
1445   gtk_widget_set_sensitive (item,
1446       folks_individual_get_trust_level (individual) ==
1447           FOLKS_TRUST_LEVEL_PERSONAS);
1448
1449   return item;
1450 }
1451
1452 typedef struct
1453 {
1454   FolksIndividual *individual;
1455   EmpathyContact *contact;
1456   EmpathyChatroom *chatroom;
1457 } RoomSubMenuData;
1458
1459 static RoomSubMenuData *
1460 room_sub_menu_data_new (FolksIndividual *individual,
1461     EmpathyContact *contact,
1462     EmpathyChatroom *chatroom)
1463 {
1464   RoomSubMenuData *data;
1465
1466   data = g_slice_new0 (RoomSubMenuData);
1467   if (individual != NULL)
1468     data->individual = g_object_ref (individual);
1469   if (contact != NULL)
1470     data->contact = g_object_ref (contact);
1471   data->chatroom = g_object_ref (chatroom);
1472
1473   return data;
1474 }
1475
1476 static void
1477 room_sub_menu_data_free (RoomSubMenuData *data)
1478 {
1479   tp_clear_object (&data->individual);
1480   tp_clear_object (&data->contact);
1481   g_object_unref (data->chatroom);
1482   g_slice_free (RoomSubMenuData, data);
1483 }
1484
1485 static void
1486 room_sub_menu_activate_cb (GtkWidget *item,
1487          RoomSubMenuData *data)
1488 {
1489   EmpathyTpChat *chat;
1490   EmpathyChatroomManager *mgr;
1491   EmpathyContact *contact = NULL;
1492
1493   chat = empathy_chatroom_get_tp_chat (data->chatroom);
1494   if (chat == NULL)
1495     {
1496       /* channel was invalidated. Ignoring */
1497       return;
1498     }
1499
1500   mgr = empathy_chatroom_manager_dup_singleton (NULL);
1501
1502   if (data->contact != NULL)
1503     contact = g_object_ref (data->contact);
1504   else
1505     {
1506       GeeSet *personas;
1507       GeeIterator *iter;
1508
1509       /* find the first of this Individual's contacts who can join this room */
1510       personas = folks_individual_get_personas (data->individual);
1511
1512       iter = gee_iterable_iterator (GEE_ITERABLE (personas));
1513       while (gee_iterator_next (iter) && (contact == NULL))
1514         {
1515           TpfPersona *persona = gee_iterator_get (iter);
1516           TpContact *tp_contact;
1517           GList *rooms;
1518
1519           if (empathy_folks_persona_is_interesting (FOLKS_PERSONA (persona)))
1520             {
1521               tp_contact = tpf_persona_get_contact (persona);
1522               if (tp_contact != NULL)
1523                 {
1524                   contact = empathy_contact_dup_from_tp_contact (tp_contact);
1525
1526                   rooms = empathy_chatroom_manager_get_chatrooms (mgr,
1527                       empathy_contact_get_account (contact));
1528
1529                   if (g_list_find (rooms, data->chatroom) == NULL)
1530                     g_clear_object (&contact);
1531
1532                   /* if contact != NULL here, we've found our match */
1533
1534                   g_list_free (rooms);
1535                 }
1536             }
1537           g_clear_object (&persona);
1538         }
1539       g_clear_object (&iter);
1540     }
1541
1542   g_object_unref (mgr);
1543
1544   if (contact == NULL)
1545     {
1546       /* contact disappeared. Ignoring */
1547       goto out;
1548     }
1549
1550   g_return_if_fail (EMPATHY_IS_CONTACT (contact));
1551
1552   /* send invitation */
1553   empathy_contact_list_add (EMPATHY_CONTACT_LIST (chat),
1554       contact, _("Inviting you to this room"));
1555
1556 out:
1557   g_object_unref (contact);
1558 }
1559
1560 static GtkWidget *
1561 create_room_sub_menu (FolksIndividual *individual,
1562                       EmpathyContact *contact,
1563                       EmpathyChatroom *chatroom)
1564 {
1565   GtkWidget *item;
1566   RoomSubMenuData *data;
1567
1568   item = gtk_menu_item_new_with_label (empathy_chatroom_get_name (chatroom));
1569   data = room_sub_menu_data_new (individual, contact, chatroom);
1570   g_signal_connect_data (item, "activate",
1571       G_CALLBACK (room_sub_menu_activate_cb), data,
1572       (GClosureNotify) room_sub_menu_data_free, 0);
1573
1574   return item;
1575 }
1576
1577 static GtkWidget *
1578 empathy_individual_invite_menu_item_new (FolksIndividual *individual,
1579     EmpathyContact *contact)
1580 {
1581   GtkWidget *item;
1582   GtkWidget *image;
1583   GtkWidget *room_item;
1584   EmpathyChatroomManager *mgr;
1585   GList *rooms = NULL;
1586   GList *names = NULL;
1587   GList *l;
1588   GtkWidget *submenu = NULL;
1589   /* map of chat room names to their objects; just a utility to remove
1590    * duplicates and to make construction of the alphabetized list easier */
1591   GHashTable *name_room_map;
1592
1593   g_return_val_if_fail (FOLKS_IS_INDIVIDUAL (individual) ||
1594       EMPATHY_IS_CONTACT (contact),
1595       NULL);
1596
1597   name_room_map = g_hash_table_new_full (g_str_hash, g_str_equal, NULL,
1598       g_object_unref);
1599
1600   item = gtk_image_menu_item_new_with_mnemonic (_("_Invite to Chat Room"));
1601   image = gtk_image_new_from_icon_name (EMPATHY_IMAGE_GROUP_MESSAGE,
1602       GTK_ICON_SIZE_MENU);
1603   gtk_image_menu_item_set_image (GTK_IMAGE_MENU_ITEM (item), image);
1604
1605   mgr = empathy_chatroom_manager_dup_singleton (NULL);
1606
1607   if (contact != NULL)
1608     {
1609       rooms = empathy_chatroom_manager_get_chatrooms (mgr,
1610           empathy_contact_get_account (contact));
1611     }
1612   else
1613     {
1614       GeeSet *personas;
1615       GeeIterator *iter;
1616
1617       /* find the first of this Individual's contacts who can join this room */
1618       personas = folks_individual_get_personas (individual);
1619       iter = gee_iterable_iterator (GEE_ITERABLE (personas));
1620       while (gee_iterator_next (iter))
1621         {
1622           TpfPersona *persona = gee_iterator_get (iter);
1623           GList *rooms_cur;
1624           TpContact *tp_contact;
1625           EmpathyContact *contact_cur;
1626
1627           if (empathy_folks_persona_is_interesting (FOLKS_PERSONA (persona)))
1628             {
1629               tp_contact = tpf_persona_get_contact (persona);
1630               if (tp_contact != NULL)
1631                 {
1632                   contact_cur = empathy_contact_dup_from_tp_contact (
1633                       tp_contact);
1634
1635                   rooms_cur = empathy_chatroom_manager_get_chatrooms (mgr,
1636                       empathy_contact_get_account (contact_cur));
1637                   rooms = g_list_concat (rooms, rooms_cur);
1638
1639                   g_object_unref (contact_cur);
1640                 }
1641             }
1642           g_clear_object (&persona);
1643         }
1644       g_clear_object (&iter);
1645     }
1646
1647   /* alphabetize the rooms */
1648   for (l = rooms; l != NULL; l = g_list_next (l))
1649     {
1650       EmpathyChatroom *chatroom = l->data;
1651       gboolean existed;
1652
1653       if (empathy_chatroom_get_tp_chat (chatroom) != NULL)
1654         {
1655           const gchar *name;
1656
1657           name = empathy_chatroom_get_name (chatroom);
1658           existed = (g_hash_table_lookup (name_room_map, name) != NULL);
1659           g_hash_table_insert (name_room_map, (gpointer) name,
1660               g_object_ref (chatroom));
1661
1662           /* this will take care of duplicates in rooms */
1663           if (!existed)
1664             {
1665               names = g_list_insert_sorted (names, (gpointer) name,
1666                   (GCompareFunc) g_strcmp0);
1667             }
1668         }
1669     }
1670
1671   for (l = names; l != NULL; l = g_list_next (l))
1672     {
1673       const gchar *name = l->data;
1674       EmpathyChatroom *chatroom;
1675
1676       if (G_UNLIKELY (submenu == NULL))
1677         submenu = gtk_menu_new ();
1678
1679       chatroom = g_hash_table_lookup (name_room_map, name);
1680       room_item = create_room_sub_menu (individual, contact, chatroom);
1681       gtk_menu_shell_append ((GtkMenuShell *) submenu, room_item);
1682       gtk_widget_show (room_item);
1683     }
1684
1685   if (submenu)
1686     gtk_menu_item_set_submenu (GTK_MENU_ITEM (item), submenu);
1687   else
1688     gtk_widget_set_sensitive (item, FALSE);
1689
1690   gtk_widget_show (image);
1691
1692   g_hash_table_unref (name_room_map);
1693   g_object_unref (mgr);
1694   g_list_free (names);
1695   g_list_free (rooms);
1696
1697   return item;
1698 }
1699
1700 static void
1701 add_menu_item_activated (GtkMenuItem *item,
1702     TpContact *tp_contact)
1703 {
1704   GtkWidget *toplevel;
1705   EmpathyContact *contact;
1706
1707   toplevel = gtk_widget_get_toplevel (GTK_WIDGET (item));
1708   if (!gtk_widget_is_toplevel (toplevel) || !GTK_IS_WINDOW (toplevel))
1709     toplevel = NULL;
1710
1711   contact = empathy_contact_dup_from_tp_contact (tp_contact);
1712
1713   empathy_new_contact_dialog_show_with_contact (GTK_WINDOW (toplevel),
1714       contact);
1715
1716   g_object_unref (contact);
1717 }
1718
1719 static GtkWidget *
1720 empathy_individual_add_menu_item_new (EmpathyIndividualMenu *self,
1721     FolksIndividual *individual)
1722 {
1723   EmpathyIndividualMenuPriv *priv = GET_PRIV (self);
1724   GtkWidget *item, *image;
1725   GeeSet *personas;
1726   GeeIterator *iter;
1727   TpContact *to_add = NULL;
1728
1729   /* find the first of this Individual's personas which are not in our contact
1730    * list. */
1731   personas = folks_individual_get_personas (individual);
1732   iter = gee_iterable_iterator (GEE_ITERABLE (personas));
1733   while (gee_iterator_next (iter))
1734     {
1735       TpfPersona *persona = gee_iterator_get (iter);
1736       TpContact *contact;
1737       TpConnection *conn;
1738
1739       if (!TPF_IS_PERSONA (persona))
1740         goto next;
1741
1742       contact = tpf_persona_get_contact (persona);
1743       if (contact == NULL)
1744         goto next;
1745
1746       /* be sure to use a not channel specific contact.
1747        * TODO: Ideally tp-glib should do this for us (fdo #42702)*/
1748       if (EMPATHY_IS_INDIVIDUAL_STORE_CHANNEL (priv->store))
1749         {
1750           TpChannel *channel;
1751           TpChannelGroupFlags flags;
1752
1753           channel = empathy_individual_store_channel_get_channel (
1754               EMPATHY_INDIVIDUAL_STORE_CHANNEL (priv->store));
1755
1756           flags = tp_channel_group_get_flags (channel);
1757           if ((flags & TP_CHANNEL_GROUP_FLAG_CHANNEL_SPECIFIC_HANDLES) != 0)
1758             {
1759               /* Channel uses channel specific handles (thanks XMPP...) */
1760               contact = tp_channel_group_get_contact_owner (channel, contact);
1761
1762               /* If we don't know the owner, we can't add the contact */
1763               if (contact == NULL)
1764                 goto next;
1765             }
1766         }
1767
1768       conn = tp_contact_get_connection (contact);
1769       if (conn == NULL)
1770         goto next;
1771
1772       /* No point to try adding a contact if the CM doesn't support it */
1773       if (!tp_connection_get_can_change_contact_list (conn))
1774         goto next;
1775
1776       /* Can't add ourself */
1777       if (tp_connection_get_self_contact (conn) == contact)
1778         goto next;
1779
1780       if (tp_contact_get_subscribe_state (contact) == TP_SUBSCRIPTION_STATE_YES)
1781         goto next;
1782
1783       g_object_unref (persona);
1784       to_add = contact;
1785       break;
1786
1787 next:
1788       g_object_unref (persona);
1789     }
1790
1791   g_object_unref (iter);
1792
1793   if (to_add == NULL)
1794     return NULL;
1795
1796   item = gtk_image_menu_item_new_with_mnemonic (_("_Add Contact…"));
1797   image = gtk_image_new_from_icon_name (GTK_STOCK_ADD, GTK_ICON_SIZE_MENU);
1798   gtk_image_menu_item_set_image (GTK_IMAGE_MENU_ITEM (item), image);
1799
1800   g_signal_connect_data (item, "activate",
1801       G_CALLBACK (add_menu_item_activated),
1802       g_object_ref (to_add), (GClosureNotify) g_object_unref, 0);
1803
1804   return item;
1805 }