+ /* File transfer */
+ if (features & EMPATHY_INDIVIDUAL_FEATURE_FILE_TRANSFER)
+ {
+ action = empathy_individual_file_transfer_menu_item_new (
+ self, single_individual);
+ gtk_menu_shell_append (GTK_MENU_SHELL (contact_submenu), action);
+ gtk_widget_show (action);
+ }
+
+ /* Share my desktop */
+ action = empathy_individual_share_my_desktop_menu_item_new (
+ self, single_individual);
+ gtk_menu_shell_append (GTK_MENU_SHELL (contact_submenu), action);
+ gtk_widget_show (action);
+
+ /* Block */
+ if (features & EMPATHY_INDIVIDUAL_FEATURE_BLOCK &&
+ (item = empathy_individiual_block_menu_item_new (single_individual))
+ != NULL) {
+ GtkWidget *sep;
+
+ sep = gtk_separator_menu_item_new ();
+ gtk_menu_shell_append (GTK_MENU_SHELL (contact_submenu), sep);
+ gtk_widget_show (sep);
+
+ gtk_menu_shell_append (GTK_MENU_SHELL (contact_submenu), item);
+ gtk_widget_show (item);
+ }
+
+ gtk_menu_shell_append (GTK_MENU_SHELL (menu), contact_item);
+ gtk_widget_show (contact_item);
+
+ g_free (label);
+ g_object_unref (contact);
+ g_object_unref (single_individual);
+
+while_finish:
+ g_clear_object (&persona);
+ }
+
+out:
+ g_clear_object (&iter);
+}
+
+static void
+empathy_individual_menu_init (EmpathyIndividualMenu *self)
+{
+ EmpathyIndividualMenuPriv *priv = G_TYPE_INSTANCE_GET_PRIVATE (self,
+ EMPATHY_TYPE_INDIVIDUAL_MENU, EmpathyIndividualMenuPriv);
+
+ self->priv = priv;
+}
+
+static GList *
+find_phone_accounts (void)
+{
+ TpAccountManager *am;
+ GList *accounts, *l;
+ GList *found = NULL;
+
+ am = tp_account_manager_dup ();
+ g_return_val_if_fail (am != NULL, NULL);
+
+ accounts = tp_account_manager_dup_valid_accounts (am);
+ for (l = accounts; l != NULL; l = g_list_next (l))
+ {
+ TpAccount *account = l->data;
+
+ if (tp_account_get_connection_status (account, NULL) !=
+ TP_CONNECTION_STATUS_CONNECTED)
+ continue;
+
+ if (!tp_account_associated_with_uri_scheme (account, "tel"))
+ continue;
+
+ found = g_list_prepend (found, g_object_ref (account));
+ }
+
+ g_list_free_full (accounts, g_object_unref);
+ g_object_unref (am);
+
+ return found;
+}
+
+static gboolean
+has_phone_account (void)
+{
+ GList *accounts;
+ gboolean result;
+
+ accounts = find_phone_accounts ();
+ result = (accounts != NULL);
+
+ g_list_free_full (accounts, (GDestroyNotify) g_object_unref);
+
+ return result;
+}
+
+static void
+call_phone_number (FolksPhoneFieldDetails *details,
+ TpAccount *account)
+{
+ DEBUG ("Try to call %s", folks_phone_field_details_get_normalised (details));
+
+ empathy_call_new_with_streams (
+ folks_phone_field_details_get_normalised (details),
+ account, TRUE, FALSE, empathy_get_current_action_time ());
+}
+
+static void
+display_call_phone_dialog (FolksPhoneFieldDetails *details,
+ GList *accounts)
+{
+ GtkWidget *dialog;
+ gint response;
+
+ dialog = empathy_account_selector_dialog_new (accounts);
+
+ gtk_window_set_title (GTK_WINDOW (dialog),
+ _("Select account to use to place the call"));
+
+ gtk_dialog_add_buttons (GTK_DIALOG (dialog),
+ GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
+ _("Call"), GTK_RESPONSE_OK,
+ NULL);
+
+ response = gtk_dialog_run (GTK_DIALOG (dialog));
+
+ if (response == GTK_RESPONSE_OK)
+ {
+ TpAccount *account;
+
+ account = empathy_account_selector_dialog_dup_selected (
+ EMPATHY_ACCOUNT_SELECTOR_DIALOG (dialog));
+
+ if (account != NULL)
+ {
+ call_phone_number (details, account);
+
+ g_object_unref (account);
+ }
+ }
+
+ gtk_widget_destroy (dialog);
+}
+
+static void
+call_phone_number_cb (GtkMenuItem *item,
+ FolksPhoneFieldDetails *details)
+{
+ GList *accounts;
+
+ accounts = find_phone_accounts ();
+ if (accounts == NULL)
+ {
+ DEBUG ("No phone aware account connected; can't call");
+ }
+ else if (g_list_length (accounts) == 1)
+ {
+ call_phone_number (details, accounts->data);
+ }
+ else
+ {
+ /* Ask which account to use */
+ display_call_phone_dialog (details, accounts);
+ }
+
+ g_list_free_full (accounts, (GDestroyNotify) g_object_unref);
+}
+
+static const gchar *
+find_phone_type (FolksPhoneFieldDetails *details)
+{
+ GeeCollection *types;
+ GeeIterator *iter;
+
+ types = folks_abstract_field_details_get_parameter_values (
+ FOLKS_ABSTRACT_FIELD_DETAILS (details), "type");
+
+ if (types == NULL)
+ return NULL;
+
+ iter = gee_iterable_iterator (GEE_ITERABLE (types));
+ while (gee_iterator_next (iter))
+ {
+ const gchar *type = gee_iterator_get (iter);
+
+ if (!tp_strdiff (type, "CELL"))
+ return _("Mobile");
+ else if (!tp_strdiff (type, "WORK"))
+ return _("Work");
+ else if (!tp_strdiff (type, "HOME"))
+ return _("HOME");
+ }
+
+ return NULL;
+}
+
+static void
+add_phone_numbers (EmpathyIndividualMenu *self)
+{
+ EmpathyIndividualMenuPriv *priv = GET_PRIV (self);
+ GeeSet *all_numbers;
+ GeeIterator *iter;
+ gboolean sensitive;
+
+ all_numbers = folks_phone_details_get_phone_numbers (
+ FOLKS_PHONE_DETAILS (priv->individual));
+
+ sensitive = has_phone_account ();
+
+ iter = gee_iterable_iterator (GEE_ITERABLE (all_numbers));
+ while (gee_iterator_next (iter))
+ {
+ FolksPhoneFieldDetails *details = gee_iterator_get (iter);
+ GtkWidget *item, *image;
+ gchar *tmp;
+ const gchar *type;
+
+ type = find_phone_type (details);
+
+ if (type != NULL)
+ {
+ /* translators: first argument is a phone number like +32123456 and
+ * the second one is something like 'home' or 'work'. */
+ tmp = g_strdup_printf (_("Call %s (%s)"),
+ folks_phone_field_details_get_normalised (details),
+ type);
+ }
+ else
+ {
+ /* translators: argument is a phone number like +32123456 */
+ tmp = g_strdup_printf (_("Call %s"),
+ folks_phone_field_details_get_normalised (details));
+ }
+
+ item = gtk_image_menu_item_new_with_mnemonic (tmp);
+ g_free (tmp);
+
+ g_signal_connect_data (item, "activate",
+ G_CALLBACK (call_phone_number_cb), g_object_ref (details),
+ (GClosureNotify) g_object_unref, 0);
+
+ gtk_widget_set_sensitive (item, sensitive);
+
+ image = gtk_image_new_from_icon_name (EMPATHY_IMAGE_CALL,
+ GTK_ICON_SIZE_MENU);
+ gtk_image_menu_item_set_image (GTK_IMAGE_MENU_ITEM (item), image);
+ gtk_widget_show (image);
+
+ gtk_menu_shell_append (GTK_MENU_SHELL (self), item);
+ gtk_widget_show (item);
+ }
+
+ g_object_unref (iter);
+}
+
+/* return a list of TpContact supporting the blocking iface */
+static GList *
+get_contacts_supporting_blocking (FolksIndividual *individual)
+{
+ GeeSet *personas;
+ GeeIterator *iter;
+ GList *result = NULL;
+
+ personas = folks_individual_get_personas (individual);
+
+ iter = gee_iterable_iterator (GEE_ITERABLE (personas));
+ while (gee_iterator_next (iter))
+ {
+ TpfPersona *persona = gee_iterator_get (iter);
+ TpContact *contact;
+ TpConnection *conn;
+
+ if (!TPF_IS_PERSONA (persona))
+ continue;
+
+ contact = tpf_persona_get_contact (persona);
+ if (contact == NULL)
+ continue;
+
+ conn = tp_contact_get_connection (contact);
+
+ if (tp_proxy_has_interface_by_id (conn,
+ TP_IFACE_QUARK_CONNECTION_INTERFACE_CONTACT_BLOCKING))
+ result = g_list_prepend (result, contact);
+ }
+
+ g_clear_object (&iter);
+
+ return result;
+}
+
+typedef struct
+{
+ gboolean blocked;
+ GtkWidget *parent;
+} GotAvatarCtx;
+
+static GotAvatarCtx *
+got_avatar_ctx_new (gboolean blocked,
+ GtkWidget *parent)
+{
+ GotAvatarCtx *ctx = g_slice_new0 (GotAvatarCtx);
+
+ ctx->blocked = blocked;
+ ctx->parent = parent != NULL ? g_object_ref (parent) : NULL;
+ return ctx;
+}
+
+static void
+got_avatar_ctx_free (GotAvatarCtx *ctx)
+{
+ g_clear_object (&ctx->parent);
+ g_slice_free (GotAvatarCtx, ctx);
+}
+
+static void
+got_avatar (GObject *source_object,
+ GAsyncResult *result,
+ gpointer user_data)
+{
+ FolksIndividual *individual = FOLKS_INDIVIDUAL (source_object);
+ GotAvatarCtx *ctx = user_data;
+ GdkPixbuf *avatar;
+ GError *error = NULL;
+ gboolean abusive = FALSE;
+ EmpathyIndividualManager *manager;
+
+ avatar = empathy_pixbuf_avatar_from_individual_scaled_finish (individual,
+ result, &error);
+
+ if (error != NULL)
+ {
+ DEBUG ("Could not get avatar: %s", error->message);
+ g_error_free (error);
+ }
+
+ if (ctx->blocked) {
+ /* confirm the user really wishes to block the contact */
+ if (!empathy_block_individual_dialog_show (GTK_WINDOW (ctx->parent),
+ individual, avatar, &abusive))
+ goto out;
+ }
+
+ manager = empathy_individual_manager_dup_singleton ();
+
+ empathy_individual_manager_set_blocked (manager, individual,
+ ctx->blocked, abusive);
+
+ g_object_unref (manager);
+
+out:
+ g_clear_object (&avatar);
+ got_avatar_ctx_free (ctx);
+}
+
+static void
+empathy_individual_block_menu_item_toggled (GtkCheckMenuItem *item,
+ FolksIndividual *individual)
+{
+ GotAvatarCtx *ctx;
+ gboolean blocked;
+ GtkWidget *parent;
+
+ /* @item may be destroyed while the async call is running to get the things
+ * we need from it right now. */
+ blocked = gtk_check_menu_item_get_active (item);
+
+ parent = g_object_get_data (
+ G_OBJECT (gtk_widget_get_parent (GTK_WIDGET (item))),
+ "window");
+
+ ctx = got_avatar_ctx_new (blocked, parent);
+
+ empathy_pixbuf_avatar_from_individual_scaled_async (individual,
+ 48, 48, NULL, got_avatar, ctx);
+}
+
+static void
+update_block_menu_item (GtkWidget *item,
+ FolksIndividual *individual)
+{
+ GList *contacts, *l;
+ gboolean is_blocked = TRUE;
+
+ contacts = get_contacts_supporting_blocking (individual);
+
+ if (contacts == NULL)
+ is_blocked = FALSE;
+
+ /* Check the menu item if all his personas are blocked */
+ for (l = contacts; l != NULL; l = g_list_next (l))
+ {
+ TpContact *contact = l->data;
+
+ if (!tp_contact_is_blocked (contact))
+ {
+ is_blocked = FALSE;
+ break;
+ }
+ }
+
+ g_signal_handlers_block_by_func (item,
+ empathy_individual_block_menu_item_toggled, individual);
+
+ gtk_check_menu_item_set_active (GTK_CHECK_MENU_ITEM (item), is_blocked);
+
+ g_signal_handlers_unblock_by_func (item,
+ empathy_individual_block_menu_item_toggled, individual);
+
+ g_list_free (contacts);
+}
+
+static void
+contact_blocked_changed_cb (TpContact *contact,
+ GParamSpec *spec,
+ GtkWidget *item)
+{
+ FolksIndividual *individual;
+
+ individual = g_object_get_data (G_OBJECT (item), "individual");
+
+ update_block_menu_item (item, individual);
+}
+
+static GtkWidget *
+empathy_individiual_block_menu_item_new (FolksIndividual *individual)
+{
+ GtkWidget *item;
+ GList *contacts, *l;
+
+ contacts = get_contacts_supporting_blocking (individual);
+
+ /* Can't block, no persona supports blocking */
+ if (contacts == NULL)
+ return NULL;
+
+ item = gtk_check_menu_item_new_with_mnemonic (_("_Block Contact"));
+
+ g_object_set_data_full (G_OBJECT (item), "individual",
+ g_object_ref (individual), g_object_unref);
+
+ for (l = contacts; l != NULL; l = g_list_next (l))
+ {
+ TpContact *contact = l->data;
+
+ tp_g_signal_connect_object (contact, "notify::is-blocked",
+ G_CALLBACK (contact_blocked_changed_cb), item, 0);
+ }
+
+ g_signal_connect (item, "toggled",
+ G_CALLBACK (empathy_individual_block_menu_item_toggled), individual);
+
+ update_block_menu_item (item, individual);
+
+ g_list_free (contacts);
+
+ return item;
+}
+
+enum
+{
+ REMOVE_DIALOG_RESPONSE_CANCEL = 0,
+ REMOVE_DIALOG_RESPONSE_DELETE,
+ REMOVE_DIALOG_RESPONSE_DELETE_AND_BLOCK,
+ REMOVE_DIALOG_RESPONSE_REMOVE_FROM_GROUP
+};
+
+static int
+remove_dialog_show (const gchar *message,
+ const gchar *secondary_text,
+ gboolean show_remove_from_group,
+ gboolean block_button,
+ GdkPixbuf *avatar,
+ const gchar *active_group)
+{
+ GtkWidget *dialog;
+ gboolean res;
+
+ dialog = gtk_message_dialog_new (NULL, GTK_DIALOG_MODAL,
+ GTK_MESSAGE_QUESTION, GTK_BUTTONS_NONE, "%s", message);
+
+ if (avatar != NULL)
+ {
+ GtkWidget *image = gtk_image_new_from_pixbuf (avatar);
+ gtk_message_dialog_set_image (GTK_MESSAGE_DIALOG (dialog), image);
+ gtk_widget_show (image);
+ }
+
+ if (show_remove_from_group)
+ {
+ GtkWidget *button;
+ gchar *button_text = g_strdup_printf (_("Remove from _Group \'%s\'"),
+ active_group);
+
+ /* gtk_dialog_add_button() doesn't allow us to pass a string with a
+ * mnemonic so we have to create the button manually. */
+ button = gtk_button_new_with_mnemonic (button_text);
+ g_free (button_text);
+
+ gtk_dialog_add_action_widget (GTK_DIALOG (dialog), button,
+ REMOVE_DIALOG_RESPONSE_REMOVE_FROM_GROUP);
+
+ gtk_widget_show (button);
+ }
+
+ if (block_button)
+ {
+ GtkWidget *button;
+
+ /* gtk_dialog_add_button() doesn't allow us to pass a string with a
+ * mnemonic so we have to create the button manually. */
+ button = gtk_button_new_with_mnemonic (
+ _("Delete and _Block"));
+
+ gtk_dialog_add_action_widget (GTK_DIALOG (dialog), button,
+ REMOVE_DIALOG_RESPONSE_DELETE_AND_BLOCK);
+
+ gtk_widget_show (button);
+ }
+
+ gtk_dialog_add_buttons (GTK_DIALOG (dialog),
+ GTK_STOCK_CANCEL, REMOVE_DIALOG_RESPONSE_CANCEL,
+ GTK_STOCK_DELETE, REMOVE_DIALOG_RESPONSE_DELETE, NULL);
+ gtk_message_dialog_format_secondary_text (GTK_MESSAGE_DIALOG (dialog),
+ "%s", secondary_text);
+
+ gtk_widget_show (dialog);
+
+ res = gtk_dialog_run (GTK_DIALOG (dialog));
+ gtk_widget_destroy (dialog);
+
+ return res;
+}
+
+static void
+individual_removed_from_group_cb (GObject *source_object,
+ GAsyncResult *res,
+ gpointer user_data)
+{
+ GError *error = NULL;
+ FolksIndividual *individual = FOLKS_INDIVIDUAL (source_object);
+
+ folks_group_details_change_group_finish (
+ FOLKS_GROUP_DETAILS (individual), res, &error);
+ if (error != NULL)
+ {
+ DEBUG ("Individual could not be removed from group: %s",
+ error->message);
+ g_error_free (error);
+ }
+}
+
+static void
+remove_got_avatar (GObject *source_object,
+ GAsyncResult *result,
+ gpointer user_data)
+{
+ FolksIndividual *individual = FOLKS_INDIVIDUAL (source_object);
+ EmpathyIndividualMenu *self = EMPATHY_INDIVIDUAL_MENU (user_data);
+ EmpathyIndividualMenuPriv *priv = GET_PRIV (self);
+ GdkPixbuf *avatar;
+ EmpathyIndividualManager *manager;
+ gchar *text;
+ GeeSet *personas;
+ guint persona_count = 0;
+ gboolean can_block;
+ GError *error = NULL;
+ gint res;
+ gboolean show_remove_from_group;
+ GeeSet *groups;
+
+ avatar = empathy_pixbuf_avatar_from_individual_scaled_finish (individual,
+ result, &error);
+
+ if (error != NULL)
+ {
+ DEBUG ("Could not get avatar: %s", error->message);
+ g_error_free (error);
+ }
+
+ /* We couldn't retrieve the avatar, but that isn't a fatal error,
+ * so we still display the remove dialog. */
+
+ groups = folks_group_details_get_groups (FOLKS_GROUP_DETAILS (individual));
+ show_remove_from_group =
+ gee_collection_get_size (GEE_COLLECTION (groups)) > 1;
+
+ personas = folks_individual_get_personas (individual);
+
+ persona_count = gee_collection_get_size (GEE_COLLECTION (personas));
+
+ /* If we have more than one TpfPersona, display a different message
+ * ensuring the user knows that *all* of the meta-contacts' personas will
+ * be removed. */
+
+ if (persona_count < 2)
+ {
+ /* Not a meta-contact */
+ text =
+ g_strdup_printf (
+ _("Do you really want to remove the contact '%s'?"),
+ folks_alias_details_get_alias (
+ FOLKS_ALIAS_DETAILS (individual)));
+ }
+ else
+ {
+ /* Meta-contact */
+ text =
+ g_strdup_printf (
+ _("Do you really want to remove the linked contact '%s'? "
+ "Note that this will remove all the contacts which make up "
+ "this linked contact."),
+ folks_alias_details_get_alias (
+ FOLKS_ALIAS_DETAILS (individual)));
+ }
+
+
+ manager = empathy_individual_manager_dup_singleton ();
+ can_block = empathy_individual_manager_supports_blocking (manager,
+ individual);
+ res = remove_dialog_show (_("Removing contact"), text,
+ show_remove_from_group, can_block, avatar, priv->active_group);
+
+ if (res == REMOVE_DIALOG_RESPONSE_REMOVE_FROM_GROUP)
+ {
+ folks_group_details_change_group (FOLKS_GROUP_DETAILS (individual),
+ priv->active_group, false, individual_removed_from_group_cb, NULL);
+ goto finally;
+ }