+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;
+ }
+
+ if (res == REMOVE_DIALOG_RESPONSE_DELETE ||
+ res == REMOVE_DIALOG_RESPONSE_DELETE_AND_BLOCK)
+ {
+ gboolean abusive;
+
+ if (res == REMOVE_DIALOG_RESPONSE_DELETE_AND_BLOCK)
+ {
+ if (!empathy_block_individual_dialog_show (NULL, individual,
+ avatar, &abusive))
+ goto finally;
+
+ empathy_individual_manager_set_blocked (manager, individual,
+ TRUE, abusive);
+ }
+
+ empathy_individual_manager_remove (manager, individual, "");
+ }
+
+ finally:
+ g_free (text);
+ g_object_unref (manager);
+ g_object_unref (self);
+}
+
+static void
+remove_activate_cb (GtkMenuItem *menuitem,
+ EmpathyIndividualMenu *self)
+{
+ EmpathyIndividualMenuPriv *priv = GET_PRIV (self);
+
+ empathy_pixbuf_avatar_from_individual_scaled_async (priv->individual,
+ 48, 48, NULL, remove_got_avatar, g_object_ref (self));
+}
+
+static GtkWidget *
+empathy_individiual_remove_menu_item_new (EmpathyIndividualMenu *self)
+{
+ GeeSet *personas;
+ GeeIterator *iter;
+ gboolean can_remove = FALSE;
+ GtkWidget *item, *image;
+ EmpathyIndividualMenuPriv *priv = GET_PRIV (self);
+
+ /* If any of the Individual's personas can be removed, add an option to
+ * remove. This will act as a best-effort option. If any Personas cannot be
+ * removed from the server, then this option will just be inactive upon
+ * subsequent menu openings */
+ personas = folks_individual_get_personas (priv->individual);
+ iter = gee_iterable_iterator (GEE_ITERABLE (personas));
+ while (!can_remove && gee_iterator_next (iter))
+ {
+ FolksPersona *persona = gee_iterator_get (iter);
+ FolksPersonaStore *store = folks_persona_get_store (persona);
+ FolksMaybeBool maybe_can_remove =
+ folks_persona_store_get_can_remove_personas (store);
+
+ if (maybe_can_remove == FOLKS_MAYBE_BOOL_TRUE)
+ can_remove = TRUE;
+
+ g_clear_object (&persona);
+ }
+ g_clear_object (&iter);
+
+ if (!can_remove)
+ return NULL;
+
+ item = gtk_image_menu_item_new_with_mnemonic (_("_Remove"));
+ image = gtk_image_new_from_icon_name (GTK_STOCK_REMOVE,
+ GTK_ICON_SIZE_MENU);
+ gtk_image_menu_item_set_image (GTK_IMAGE_MENU_ITEM (item), image);
+
+ g_signal_connect (item, "activate",
+ G_CALLBACK (remove_activate_cb), self);
+
+ return item;
+}
+