Add an account action to the Demo protocol plugin to create authorization requests

Fri, 20 Sep 2024 20:27:48 -0500

author
Gary Kramlich <grim@reaperworld.com>
date
Fri, 20 Sep 2024 20:27:48 -0500
changeset 42959
f1b48841d33e
parent 42958
5086e3c19207
child 42960
f80f044d734c

Add an account action to the Demo protocol plugin to create authorization requests

I also had to bind the Purple.Notification:subtitle to the
Purple.AuthorizationRequest:message.

Testing Done:
Made sure the notification displayed as expected and wrapped properly.

Bugs closed: PIDGIN-17948

Reviewed at https://reviews.imfreedom.org/r/3506/

libpurple/purplenotificationauthorizationrequest.c file | annotate | diff | comparison | revisions
protocols/demo/purpledemoprotocolactions.c file | annotate | diff | comparison | revisions
protocols/demo/resources/buddy_icons/Chuck.png file | annotate | diff | comparison | revisions
protocols/demo/resources/buddy_icons/Eve.png file | annotate | diff | comparison | revisions
protocols/demo/resources/buddy_icons/Mallory.png file | annotate | diff | comparison | revisions
protocols/demo/resources/buddy_icons/Michael.png file | annotate | diff | comparison | revisions
protocols/demo/resources/contacts.json file | annotate | diff | comparison | revisions
protocols/demo/resources/purpledemo.gresource.xml file | annotate | diff | comparison | revisions
--- a/libpurple/purplenotificationauthorizationrequest.c	Thu Sep 19 23:25:05 2024 -0500
+++ b/libpurple/purplenotificationauthorizationrequest.c	Fri Sep 20 20:27:48 2024 -0500
@@ -97,6 +97,10 @@
 			g_signal_connect_object(contact, "notify::name-for-display",
 			                        G_CALLBACK(purple_notification_authorization_request_notify_cb),
 			                        notification, G_CONNECT_DEFAULT);
+
+			g_object_bind_property(request, "message",
+			                       notification, "subtitle",
+			                       G_BINDING_BIDIRECTIONAL | G_BINDING_SYNC_CREATE);
 		}
 
 		purple_notification_set_account(PURPLE_NOTIFICATION(notification),
--- a/protocols/demo/purpledemoprotocolactions.c	Thu Sep 19 23:25:05 2024 -0500
+++ b/protocols/demo/purpledemoprotocolactions.c	Fri Sep 20 20:27:48 2024 -0500
@@ -517,6 +517,55 @@
 	g_clear_object(&account);
 }
 
+static const char *auth_request_ids[] = {"chuck", "eve", "mallory", "michael"};
+
+static void
+purple_demo_protocol_authorization_request(G_GNUC_UNUSED GSimpleAction *action,
+                                           GVariant *parameter,
+                                           G_GNUC_UNUSED gpointer data)
+{
+	PurpleAccount *account = NULL;
+	PurpleAccountManager *account_manager = NULL;
+	PurpleAuthorizationRequest *request = NULL;
+	PurpleContact *contact = NULL;
+	PurpleContactManager *contact_manager = NULL;
+	PurpleNotification *notification = NULL;
+	PurpleNotificationManager *notification_manager = NULL;
+	PurpleTags *tags = NULL;
+	const char *account_id = NULL;
+	const char *message = NULL;
+	static guint counter = 0;
+
+	account_id = g_variant_get_string(parameter, NULL);
+	account_manager = purple_account_manager_get_default();
+	account = purple_account_manager_find_by_id(account_manager, account_id);
+
+	contact_manager = purple_contact_manager_get_default();
+	contact = purple_contact_manager_find_with_id(contact_manager, account,
+	                                              auth_request_ids[counter]);
+
+	tags = purple_contact_info_get_tags(PURPLE_CONTACT_INFO(contact));
+	message = purple_tags_get(tags, "auth-request-msg");
+
+	request = purple_authorization_request_new(contact);
+	if(!purple_strempty(message)) {
+		purple_authorization_request_set_message(request, message);
+	}
+
+	notification = purple_notification_authorization_request_new(NULL,
+	                                                             request);
+	notification_manager = purple_notification_manager_get_default();
+	purple_notification_manager_add(notification_manager, notification);
+
+	counter++;
+	if(counter >= G_N_ELEMENTS(auth_request_ids)) {
+		counter = 0;
+	}
+
+	g_clear_object(&notification);
+	g_clear_object(&account);
+}
+
 /******************************************************************************
  * Helpers
  *****************************************************************************/
@@ -541,6 +590,10 @@
 			.activate = purple_demo_protocol_remote_add,
 			.parameter_type = "s",
 		}, {
+			.name = "authorization-request",
+			.activate = purple_demo_protocol_authorization_request,
+			.parameter_type = "s",
+		}, {
 			.name = "request-fields",
 			.activate = purple_demo_protocol_request_fields_activate,
 			.parameter_type = "s",
@@ -590,6 +643,13 @@
 	g_menu_append_item(menu, item);
 	g_object_unref(item);
 
+	item = g_menu_item_new(_("Trigger a contact authorization request"),
+	                       "prpl-demo.authorization-request");
+	g_menu_item_set_attribute(item, PURPLE_MENU_ATTRIBUTE_DYNAMIC_TARGET, "s",
+	                          "account");
+	g_menu_append_item(menu, item);
+	g_object_unref(item);
+
 	item = g_menu_item_new(_("Request Fields"), "prpl-demo.request-fields");
 	g_menu_item_set_attribute(item, PURPLE_MENU_ATTRIBUTE_DYNAMIC_TARGET, "s",
 	                          "account");
Binary file protocols/demo/resources/buddy_icons/Chuck.png has changed
Binary file protocols/demo/resources/buddy_icons/Eve.png has changed
Binary file protocols/demo/resources/buddy_icons/Mallory.png has changed
Binary file protocols/demo/resources/buddy_icons/Michael.png has changed
--- a/protocols/demo/resources/contacts.json	Thu Sep 19 23:25:05 2024 -0500
+++ b/protocols/demo/resources/contacts.json	Fri Sep 20 20:27:48 2024 -0500
@@ -136,4 +136,34 @@
 		"primitive": "do-not-disturb"
 	},
 	"username": "Erin"
+}, {
+	"id": "chuck",
+	"presence": {
+		"primitive": "available"
+	},
+	"username": "Chuck"
+}, {
+	"id": "eve",
+	"presence": {
+		"primitive": "invisible"
+	},
+	"tags": [
+		"auth-request-msg:Nice avatar!"
+	],
+	"username": "Eve"
+}, {
+	"id": "mallory",
+	"presence": {
+		"primitive": "offline"
+	},
+	"username": "Mallory"
+}, {
+	"id": "michael",
+	"presence": {
+		"primitive": "streaming"
+	},
+	"tags": [
+		"auth-request-msg:Hey come check out my stream!"
+	],
+	"username": "Michael"
 }]
--- a/protocols/demo/resources/purpledemo.gresource.xml	Thu Sep 19 23:25:05 2024 -0500
+++ b/protocols/demo/resources/purpledemo.gresource.xml	Fri Sep 20 20:27:48 2024 -0500
@@ -10,13 +10,17 @@
     <file>buddy_icons/Alice.png</file>
     <file>buddy_icons/Bob.png</file>
     <file>buddy_icons/Carlos.png</file>
+    <file>buddy_icons/Chuck.png</file>
     <file>buddy_icons/Echo.png</file>
     <file>buddy_icons/Eion.png</file>
     <file>buddy_icons/Elliott.png</file>
     <file>buddy_icons/Erin.png</file>
+    <file>buddy_icons/Eve.png</file>
     <file>buddy_icons/Gary.png</file>
     <file>buddy_icons/John.png</file>
+    <file>buddy_icons/Mallory.png</file>
     <file>buddy_icons/Markus.png</file>
+    <file>buddy_icons/Michael.png</file>
     <file>buddy_icons/Richard.png</file>
   </gresource>
 </gresources>

mercurial