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