# HG changeset patch
# Node ID 79abea5e4b64
# Parent  392bb692c505
diff --git a/libpurple/protocols/demo/meson.build b/libpurple/protocols/demo/meson.build
--- a/libpurple/protocols/demo/meson.build
+++ b/libpurple/protocols/demo/meson.build
@@ -11,6 +11,8 @@
 	'purpledemoprotocolactions.h',
 	'purpledemoprotocolclient.c',
 	'purpledemoprotocolclient.h',
+	'purpledemoprotocolcontacts.c',
+	'purpledemoprotocolcontacts.h',
 	'purpledemoprotocolim.c',
 	'purpledemoprotocolim.h',
 	'purpledemoprotocolmedia.c',
diff --git a/libpurple/protocols/demo/purpledemocontacts.c b/libpurple/protocols/demo/purpledemocontacts.c
--- a/libpurple/protocols/demo/purpledemocontacts.c
+++ b/libpurple/protocols/demo/purpledemocontacts.c
@@ -248,6 +248,15 @@
 		}
 	}
 
+	/* Load the profile if it exists. */
+	if(json_object_has_member(contact_object, "profile")) {
+		value = json_object_get_string_member(contact_object, "profile");
+		if(!purple_strempty(value)) {
+			g_object_set_data_full(G_OBJECT(info), "demo-profile",
+			                       g_strdup(value), g_free);
+		}
+	}
+
 	/* Load the tags. */
 	if(json_object_has_member(contact_object, "tags")) {
 		PurpleTags *tags = purple_contact_info_get_tags(info);
diff --git a/libpurple/protocols/demo/purpledemoprotocol.c b/libpurple/protocols/demo/purpledemoprotocol.c
--- a/libpurple/protocols/demo/purpledemoprotocol.c
+++ b/libpurple/protocols/demo/purpledemoprotocol.c
@@ -23,6 +23,7 @@
 #include "purpledemoconnection.h"
 #include "purpledemoprotocolactions.h"
 #include "purpledemoprotocolclient.h"
+#include "purpledemoprotocolcontacts.h"
 #include "purpledemoprotocolim.h"
 #include "purpledemoprotocolmedia.h"
 
@@ -99,6 +100,8 @@
 	                              purple_demo_protocol_actions_init)
 	G_IMPLEMENT_INTERFACE_DYNAMIC(PURPLE_TYPE_PROTOCOL_CLIENT,
 	                              purple_demo_protocol_client_init)
+	G_IMPLEMENT_INTERFACE_DYNAMIC(PURPLE_TYPE_PROTOCOL_CONTACTS,
+	                              purple_demo_protocol_contacts_init)
 	G_IMPLEMENT_INTERFACE_DYNAMIC(PURPLE_TYPE_PROTOCOL_IM,
 	                              purple_demo_protocol_im_init)
 	G_IMPLEMENT_INTERFACE_DYNAMIC(PURPLE_TYPE_PROTOCOL_MEDIA,
diff --git a/libpurple/protocols/demo/purpledemoprotocolcontacts.h b/libpurple/protocols/demo/purpledemoprotocolcontacts.h
new file mode 100644
--- /dev/null
+++ b/libpurple/protocols/demo/purpledemoprotocolcontacts.h
@@ -0,0 +1,28 @@
+/*
+ * Purple - Internet Messaging Library
+ * Copyright (C) Pidgin Developers <devel@pidgin.im>
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; if not, see <https://www.gnu.org/liceng_task_return_pointerses/>.
+ */
+
+#ifndef PURPLE_DEMO_PROTOCOL_CONTACTS_H
+#define PURPLE_DEMO_PROTOCOL_CONTACTS_H
+
+#include <glib.h>
+
+#include <purple.h>
+
+G_GNUC_INTERNAL void purple_demo_protocol_contacts_init(PurpleProtocolContactsInterface *iface);
+
+#endif /* PURPLE_DEMO_PROTOCOL_CONTACTS_H */
diff --git a/libpurple/protocols/demo/purpledemoprotocolcontacts.c b/libpurple/protocols/demo/purpledemoprotocolcontacts.c
new file mode 100644
--- /dev/null
+++ b/libpurple/protocols/demo/purpledemoprotocolcontacts.c
@@ -0,0 +1,57 @@
+/*
+ * Purple - Internet Messaging Library
+ * Copyright (C) Pidgin Developers <devel@pidgin.im>
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; if not, see <https://www.gnu.org/licenses/>.
+ */
+
+#include <glib/gi18n-lib.h>
+
+#include "purpledemoprotocolcontacts.h"
+
+/******************************************************************************
+ * PurpleProtocolContacts Implementation
+ *****************************************************************************/
+static char *
+purple_demo_protocol_contacts_get_profile_finish(G_GNUC_UNUSED PurpleProtocolContacts *contacts,
+                                                 GAsyncResult *result,
+                                                 GError **error)
+{
+	g_return_val_if_fail(G_IS_TASK(result), NULL);
+
+	return g_task_propagate_pointer(G_TASK(result), error);
+}
+
+static void
+purple_demo_protocol_contacts_get_profile_async(PurpleProtocolContacts *contacts,
+                                                PurpleContactInfo *info,
+                                                GCancellable *cancellable,
+                                                GAsyncReadyCallback callback,
+                                                gpointer data)
+{
+	GTask *task = NULL;
+	const char *profile = NULL;
+
+	task = g_task_new(contacts, cancellable, callback, data);
+
+	profile = g_object_get_data(G_OBJECT(info), "demo-profile");
+	g_task_return_pointer(task, g_strdup(profile), g_free);
+	g_clear_object(&task);
+}
+
+void
+purple_demo_protocol_contacts_init(PurpleProtocolContactsInterface *iface) {
+	iface->get_profile_async = purple_demo_protocol_contacts_get_profile_async;
+	iface->get_profile_finish = purple_demo_protocol_contacts_get_profile_finish;
+}
diff --git a/libpurple/protocols/demo/resources/contacts.json b/libpurple/protocols/demo/resources/contacts.json
--- a/libpurple/protocols/demo/resources/contacts.json
+++ b/libpurple/protocols/demo/resources/contacts.json
@@ -12,7 +12,8 @@
 	"username": "Gary",
 	"tags": [
 		"group:Family"
-	]
+	],
+	"profile": "Pidgin 3 will be ready when it is ready! 🗿"
 }, {
 	"id": "john",
 	"alias": "Rekkanoryo",
@@ -25,7 +26,8 @@
 	"tags": [
 		"group:Friends"
 	],
-	"username": "John"
+	"username": "John",
+	"profile": "Idiot Windows sysadmin who used to develop for Pidgin and libpurple in the bright days before Windows 8"
 }, {
 	"id": "elliott",
 	"person": {
diff --git a/pidgin/meson.build b/pidgin/meson.build
--- a/pidgin/meson.build
+++ b/pidgin/meson.build
@@ -34,6 +34,7 @@
 	'pidginavatar.c',
 	'pidgincolor.c',
 	'pidgincommands.c',
+	'pidgincontactinfomenu.c',
 	'pidgincontactlist.c',
 	'pidgincontactlistwindow.c',
 	'pidgindebug.c',
@@ -100,6 +101,7 @@
 	'pidginattachment.h',
 	'pidginavatar.h',
 	'pidgincolor.h',
+	'pidgincontactinfomenu.h',
 	'pidgincontactlist.h',
 	'pidgincontactlistwindow.h',
 	'pidgincore.h',
diff --git a/pidgin/pidgincontactinfomenu.h b/pidgin/pidgincontactinfomenu.h
new file mode 100644
--- /dev/null
+++ b/pidgin/pidgincontactinfomenu.h
@@ -0,0 +1,55 @@
+/*
+ * Pidgin - Internet Messenger
+ * Copyright (C) Pidgin Developers <devel@pidgin.im>
+ *
+ * Pidgin is the legal property of its developers, whose names are too numerous
+ * to list here.  Please refer to the COPYRIGHT file distributed with this
+ * source distribution.
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, see <https://www.gnu.org/licenses/>.
+ */
+
+#if !defined(PIDGIN_GLOBAL_HEADER_INSIDE) && !defined(PIDGIN_COMPILATION)
+# error "only <pidgin.h> may be included directly"
+#endif
+
+#ifndef PIDGIN_CONTACT_INFO_MENU_H
+#define PIDGIN_CONTACT_INFO_MENU_H
+
+#include <glib.h>
+
+#include <gtk/gtk.h>
+
+#include <purple.h>
+
+G_BEGIN_DECLS
+
+/**
+ * pidgin_contact_info_menu_popup:
+ * @info: The [class@Purple.ContactInfo] that the menu should use.
+ * @account: The [class@Purple.Account] that @info is for.
+ * @parent: The parent [class@Gtk.Widget] that the menu should use.
+ * @x: The x coordinate to point to.
+ * @y: The y coordinate to point to.
+ *
+ * Creates and display a [class@Gtk.PopoverMenu] for @info and displays it at
+ * the @x and @y coordinates instead of @parent.
+ *
+ * Since: 3.0.0
+ */
+void pidgin_contact_info_menu_popup(PurpleContactInfo *info, PurpleAccount *account, GtkWidget *parent, gdouble x, gdouble y);
+
+G_END_DECLS
+
+#endif /* PIDGIN_CONTACT_INFO_MENU_H */
diff --git a/pidgin/pidgincontactinfomenu.c b/pidgin/pidgincontactinfomenu.c
new file mode 100644
--- /dev/null
+++ b/pidgin/pidgincontactinfomenu.c
@@ -0,0 +1,288 @@
+/*
+ * Pidgin - Internet Messenger
+ * Copyright (C) Pidgin Developers <devel@pidgin.im>
+ *
+ * Pidgin is the legal property of its developers, whose names are too numerous
+ * to list here.  Please refer to the COPYRIGHT file distributed with this
+ * source distribution.
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, see <https://www.gnu.org/licenses/>.
+ */
+
+#include <glib/gi18n-lib.h>
+
+#include "pidgincontactinfomenu.h"
+
+/******************************************************************************
+ * Helpers
+ *****************************************************************************/
+static gboolean
+pidgin_contact_info_menu_parse_parameter(GVariant *parameter,
+                                         PurpleAccount **account,
+                                         PurpleContact **contact)
+{
+	PurpleAccount *local_account = NULL;
+	PurpleAccountManager *account_manager = NULL;
+	PurpleContact *local_contact = NULL;
+	PurpleContactManager *contact_manager = NULL;
+	char *account_id = NULL;
+	char *contact_id = NULL;
+
+	g_variant_get(parameter, "(ss)", &account_id, &contact_id);
+
+	/* Find the account. */
+	account_manager = purple_account_manager_get_default();
+	local_account = purple_account_manager_find_by_id(account_manager,
+	                                                  account_id);
+	if(!PURPLE_IS_ACCOUNT(local_account)) {
+		return FALSE;
+	}
+
+	if(account != NULL) {
+		*account = local_account;
+	}
+
+	/* Find the contact. */
+	contact_manager = purple_contact_manager_get_default();
+	local_contact = purple_contact_manager_find_with_id(contact_manager,
+	                                                    local_account,
+	                                                    contact_id);
+	if(!PURPLE_IS_CONTACT(local_contact)) {
+		return FALSE;
+	}
+
+	if(contact != NULL) {
+		*contact = local_contact;
+	}
+
+	g_clear_pointer(&account_id, g_free);
+	g_clear_pointer(&contact_id, g_free);
+
+	return TRUE;
+}
+
+static GMenuModel *
+pidgin_contact_info_menu_get_starting_items(PurpleContactInfo *info,
+                                            PurpleAccount *account)
+{
+	GMenu *menu = NULL;
+	GMenuItem *item = NULL;
+	const char *account_id = NULL;
+	const char *contact_id = NULL;
+
+	account_id = purple_contact_info_get_id(PURPLE_CONTACT_INFO(account));
+	contact_id = purple_contact_info_get_id(info);
+
+	menu = g_menu_new();
+
+	item = g_menu_item_new(_("View Profile"), NULL);
+	g_menu_item_set_action_and_target(item, "contact.profile", "(ss)",
+	                                  account_id, contact_id);
+	g_menu_append_item(menu, item);
+	g_clear_object(&item);
+
+	item = g_menu_item_new(_("Message"), NULL);
+	g_menu_item_set_action_and_target(item, "contact.message", "(ss)",
+	                                  account_id, contact_id);
+	g_menu_append_item(menu, item);
+	g_clear_object(&item);
+
+	g_menu_append(menu, _("Send File..."), "contact.send-file");
+
+	return G_MENU_MODEL(menu);
+}
+
+static void
+pidgin_contact_info_menu_disable_items(GActionMap *map,
+                                       PurpleAccount *account)
+{
+	PurpleProtocol *protocol = NULL;
+	GAction *action = NULL;
+	gboolean enabled = TRUE;
+
+	protocol = purple_account_get_protocol(account);
+	if(!PURPLE_IS_PROTOCOL_CONTACTS(protocol)) {
+		enabled = FALSE;
+	} else {
+		enabled = PURPLE_PROTOCOL_IMPLEMENTS(protocol, CONTACTS,
+		                                     get_profile_async);
+	}
+
+	action = g_action_map_lookup_action(map, "profile");
+	g_simple_action_set_enabled(G_SIMPLE_ACTION(action), enabled);
+}
+
+/******************************************************************************
+ * Contact Actions
+ *****************************************************************************/
+static void
+pidgin_contact_info_menu_get_info_activate_cb(GObject *contacts,
+                                              GAsyncResult *result,
+                                              gpointer data)
+{
+	PurpleContact *contact = data;
+	GError *error = NULL;
+	char *profile = NULL;
+
+	profile = purple_protocol_contacts_get_profile_finish(PURPLE_PROTOCOL_CONTACTS(contacts),
+	                                                      result, &error);
+
+	if(error != NULL) {
+		g_warning("failed to get profile for '%s': %s",
+		          purple_contact_info_get_id(PURPLE_CONTACT_INFO(contact)),
+		          error->message);
+
+		g_clear_error(&error);
+
+		return;
+	}
+
+	g_message("profile data for '%s': %s",
+	          purple_contact_info_get_id(PURPLE_CONTACT_INFO(contact)),
+	          profile);
+}
+
+static void
+pidgin_contact_info_menu_get_info_activate(G_GNUC_UNUSED GSimpleAction *action,
+                                           GVariant *parameter,
+                                           G_GNUC_UNUSED gpointer data)
+{
+	PurpleAccount *account = NULL;
+	PurpleContact *contact = NULL;
+	PurpleProtocol *protocol = NULL;
+
+	g_return_if_fail(g_variant_is_of_type(parameter, (GVariantType*)"(ss)"));
+
+	if(!pidgin_contact_info_menu_parse_parameter(parameter, &account, &contact)) {
+		/* TODO: when purple_account_manager_find_by_id returns a ref we need
+		 * to unref it.
+		 */
+		g_clear_object(&contact);
+
+		g_warning("failed to parse parameter");
+
+		return;
+	}
+
+	protocol = purple_account_get_protocol(account);
+	if(PURPLE_IS_PROTOCOL_CONTACTS(protocol)) {
+		if(PURPLE_PROTOCOL_IMPLEMENTS(protocol, CONTACTS, get_profile_async)) {
+			purple_protocol_contacts_get_profile_async(PURPLE_PROTOCOL_CONTACTS(protocol),
+			                                           PURPLE_CONTACT_INFO(contact),
+			                                           NULL,
+			                                           pidgin_contact_info_menu_get_info_activate_cb,
+			                                           contact);
+		}
+	}
+}
+
+static void
+pidgin_contact_info_menu_message_activate(G_GNUC_UNUSED GSimpleAction *action,
+                                          GVariant *parameter,
+                                          G_GNUC_UNUSED gpointer data)
+{
+	PurpleAccount *account = NULL;
+	PurpleContact *contact = NULL;
+	PurpleConversation *conversation = NULL;
+	PurpleConversationManager *manager = NULL;
+	const char *contact_id = NULL;
+
+	g_return_if_fail(g_variant_is_of_type(parameter, (GVariantType*)"(ss)"));
+
+	if(!pidgin_contact_info_menu_parse_parameter(parameter, &account, &contact)) {
+		/* TODO: when purple_account_manager_find_by_id returns a ref we need
+		 * to unref it.
+		 */
+		g_clear_object(&contact);
+
+		g_warning("failed to parse parameter");
+
+		return;
+	}
+
+	contact_id = purple_contact_info_get_username(PURPLE_CONTACT_INFO(contact));
+
+	manager = purple_conversation_manager_get_default();
+	conversation = purple_conversation_manager_find_im(manager, account,
+	                                                   contact_id);
+	if(!PURPLE_IS_CONVERSATION(conversation)) {
+		conversation = purple_im_conversation_new(account, contact_id);
+		purple_conversation_manager_register(manager, conversation);
+	}
+
+	g_clear_object(&contact);
+}
+
+static GActionGroup *
+pidgin_contact_info_menu_get_contact_actions(void) {
+	GSimpleActionGroup *action_group = NULL;
+	GActionEntry entries[] = {
+		{
+			.name = "profile",
+			.activate = pidgin_contact_info_menu_get_info_activate,
+			.parameter_type = "(ss)",
+
+		}, {
+			.name = "message",
+			.activate = pidgin_contact_info_menu_message_activate,
+			.parameter_type = "(ss)",
+		},
+	};
+
+	action_group = g_simple_action_group_new();
+	g_action_map_add_action_entries(G_ACTION_MAP(action_group), entries,
+	                                G_N_ELEMENTS(entries), NULL);
+
+	return G_ACTION_GROUP(action_group);
+}
+
+/******************************************************************************
+ * Public API
+ *****************************************************************************/
+void
+pidgin_contact_info_menu_popup(PurpleContactInfo *info, PurpleAccount *account,
+                               GtkWidget *parent, gdouble x, gdouble y)
+{
+	GtkWidget *popover = NULL;
+	GActionGroup *action_group = NULL;
+	GMenu *menu = NULL;
+	GMenuModel *section = NULL;
+
+	g_return_if_fail(PURPLE_IS_CONTACT_INFO(info));
+	g_return_if_fail(PURPLE_IS_ACCOUNT(account));
+	g_return_if_fail(GTK_IS_WIDGET(parent));
+
+	/* Add actions to the parent. */
+	action_group = pidgin_contact_info_menu_get_contact_actions();
+	gtk_widget_insert_action_group(parent, "contact", action_group);
+
+	/* Disable any actions that aren't currently supported. */
+	pidgin_contact_info_menu_disable_items(G_ACTION_MAP(action_group),
+	                                       account);
+
+	/* Create the menu. */
+	menu = g_menu_new();
+
+	section = pidgin_contact_info_menu_get_starting_items(info, account);
+	g_menu_append_section(menu, NULL, section);
+
+	/* Create the popover and pop it up! */
+	popover = gtk_popover_menu_new_from_model(G_MENU_MODEL(menu));
+	gtk_widget_set_parent(popover, parent);
+	gtk_popover_set_pointing_to(GTK_POPOVER(popover),
+	                            &(const GdkRectangle){x, y, 1, 1});
+	gtk_popover_popup(GTK_POPOVER(popover));
+
+	g_clear_object(&menu);
+}
diff --git a/pidgin/pidgincontactlist.c b/pidgin/pidgincontactlist.c
--- a/pidgin/pidgincontactlist.c
+++ b/pidgin/pidgincontactlist.c
@@ -24,6 +24,7 @@
 
 #include <purple.h>
 
+#include "pidgin/pidgincontactinfomenu.h"
 #include "pidgin/pidgincontactlist.h"
 
 struct _PidginContactList {
@@ -281,6 +282,35 @@
 	return g_string_free(str, FALSE);
 }
 
+static void
+pidgin_contact_list_context_cb(GtkGestureSingle *self,
+                               G_GNUC_UNUSED gint n_press,
+                               gdouble x,
+                               gdouble y,
+                               gpointer data)
+{
+	PurpleAccount *account = NULL;
+	PurpleContactInfo *info = NULL;
+	PurplePerson *person = NULL;
+	GtkWidget *parent = NULL;
+	GtkListItem *item = data;
+
+	parent = gtk_event_controller_get_widget(GTK_EVENT_CONTROLLER(self));
+
+	/* Maybe eventually we'll make this show all contacts, but for now we'll
+	 * just show the priority contact's menu.
+	 */
+	person = gtk_list_item_get_item(item);
+	info = purple_person_get_priority_contact_info(person);
+
+	/* We know the info is a PurpleContact because that's what the contact list
+	 * is made of so this cast is fine.
+	 */
+	account = purple_contact_get_account(PURPLE_CONTACT(info));
+
+	pidgin_contact_info_menu_popup(info, account, parent, x, y);
+}
+
 /******************************************************************************
  * GObject Implementation
  *****************************************************************************/
@@ -345,6 +375,8 @@
 	                                        pidgin_contact_list_message_visible_cb);
 	gtk_widget_class_bind_template_callback(widget_class,
 	                                        pidgin_contact_list_get_primitive_as_string);
+	gtk_widget_class_bind_template_callback(widget_class,
+	                                        pidgin_contact_list_context_cb);
 }
 
 /******************************************************************************
diff --git a/pidgin/resources/ContactList/widget.ui b/pidgin/resources/ContactList/widget.ui
--- a/pidgin/resources/ContactList/widget.ui
+++ b/pidgin/resources/ContactList/widget.ui
@@ -53,6 +53,12 @@
         <property name="orientation">horizontal</property>
         <property name="spacing">6</property>
         <child>
+          <object class="GtkGestureClick">
+            <property name="button">3</property>
+            <signal name="released" handler="pidgin_contact_list_context_cb" object="GtkListItem" swapped="no"/>
+          </object>
+        </child>
+        <child>
           <!-- switch to a PidginAvatar when that can take a PurpleAvatar -->
           <object class="GtkImage" id="avatar">
             <property name="icon-size">large</property>
diff --git a/po/POTFILES.in b/po/POTFILES.in
--- a/po/POTFILES.in
+++ b/po/POTFILES.in
@@ -256,6 +256,7 @@
 pidgin/pidgin.c
 pidgin/pidgincolor.c
 pidgin/pidgincommands.c
+pidgin/pidgincontactinfomenu.c
 pidgin/pidgincontactlistwindow.c
 pidgin/pidgindebug.c
 pidgin/pidgindisplaywindow.c
