Convert Pidgin plugins to libsoup.

Thu, 10 Oct 2019 23:25:02 -0400

author
Elliott Sales de Andrade <qulogic@pidgin.im>
date
Thu, 10 Oct 2019 23:25:02 -0400
changeset 39992
bffad41e933f
parent 39991
aef6dc04cf17
child 39993
0d18609d0d01

Convert Pidgin plugins to libsoup.

pidgin/plugins/imgupload.c file | annotate | diff | comparison | revisions
pidgin/plugins/meson.build file | annotate | diff | comparison | revisions
pidgin/plugins/relnot.c file | annotate | diff | comparison | revisions
--- a/pidgin/plugins/imgupload.c	Thu Oct 10 22:50:09 2019 -0400
+++ b/pidgin/plugins/imgupload.c	Thu Oct 10 23:25:02 2019 -0400
@@ -33,11 +33,13 @@
 #include "gtkwebviewtoolbar.h"
 
 #include <json-glib/json-glib.h>
+#include <libsoup/soup.h>
 
 #define IMGUP_IMGUR_CLIENT_ID "b6d33c6bb80e1b6"
 #define IMGUP_PREF_PREFIX "/plugins/gtk/imgupload/"
 
 static PurplePlugin *plugin_handle = NULL;
+static SoupSession *session = NULL;
 
 static void
 imgup_upload_done(PidginWebView *webview, const gchar *url, const gchar *title);
@@ -61,24 +63,22 @@
  ******************************************************************************/
 
 static void
-imgup_imgur_uploaded(PurpleHttpConnection *hc, PurpleHttpResponse *resp,
-	gpointer _webview)
+imgup_imgur_uploaded(G_GNUC_UNUSED SoupSession *session, SoupMessage *msg,
+                     gpointer _webview)
 {
 	JsonParser *parser;
 	JsonObject *result;
-	const gchar *data;
-	gsize data_len;
 	PidginWebView *webview = PIDGIN_WEBVIEW(_webview);
 	const gchar *url, *title;
 
-	if (!purple_http_response_is_successful(resp)) {
+	if (!SOUP_STATUS_IS_SUCCESSFUL(msg->status_code)) {
 		imgup_upload_failed(webview);
 		return;
 	}
 
-	data = purple_http_response_get_data(resp, &data_len);
 	parser = json_parser_new();
-	if (!json_parser_load_from_data(parser, data, data_len, NULL)) {
+	if (!json_parser_load_from_data(parser, msg->response_body->data,
+	                                msg->response_body->length, NULL)) {
 		purple_debug_warning("imgupload", "Invalid json got from imgur");
 
 		imgup_upload_failed(webview);
@@ -99,25 +99,23 @@
 	result = json_object_get_object_member(result, "data");
 	url = json_object_get_string_member(result, "link");
 
-	title = g_object_get_data(G_OBJECT(webview), "imgupload-imgur-name");
+	title = g_object_get_data(G_OBJECT(msg), "imgupload-imgur-name");
 
 	imgup_upload_done(webview, url, title);
 
 	g_object_unref(parser);
-	g_object_set_data(G_OBJECT(webview), "imgupload-imgur-name", NULL);
+	g_object_set_data(G_OBJECT(msg), "imgupload-imgur-name", NULL);
 }
 
 static PurpleHttpConnection *
 imgup_imgur_upload(PidginWebView *webview, PurpleImage *image)
 {
-	PurpleHttpRequest *req;
-	PurpleHttpConnection *hc;
+	SoupMessage *msg;
 	gchar *req_data, *img_data, *img_data_e;
 
-	req = purple_http_request_new("https://api.imgur.com/3/image");
-	purple_http_request_set_method(req, "POST");
-	purple_http_request_header_set(req, "Authorization",
-		"Client-ID " IMGUP_IMGUR_CLIENT_ID);
+	msg = soup_message_new("POST", "https://api.imgur.com/3/image");
+	soup_message_headers_replace(msg, "Authorization",
+	                             "Client-ID " IMGUP_IMGUR_CLIENT_ID);
 
 	/* TODO: make it a plain, multipart/form-data request */
 	img_data = g_base64_encode(purple_image_get_data(image),
@@ -127,19 +125,16 @@
 	req_data = g_strdup_printf("type=base64&image=%s", img_data_e);
 	g_free(img_data_e);
 
-	purple_http_request_header_set(req, "Content-Type",
-		"application/x-www-form-urlencoded");
-	purple_http_request_set_contents(req, req_data, -1);
-	g_free(req_data);
+	soup_message_set_request(msg, "application/x-www-form-urlencoded",
+	                         SOUP_MESSAGE_TAKE, req_data, -1);
 
-	/* TODO: set it to hc, not webview (after gobjectifying it) */
-	g_object_set_data_full(G_OBJECT(webview), "imgupload-imgur-name",
-		g_strdup(purple_image_get_friendly_filename(image)), g_free);
+	g_object_set_data_full(G_OBJECT(msg), "imgupload-imgur-name",
+	                       g_strdup(purple_image_get_friendly_filename(image)),
+	                       g_free);
 
-	hc = purple_http_request(NULL, req, imgup_imgur_uploaded, webview);
-	purple_http_request_unref(req);
+	soup_session_queue_message(session, msg, imgup_imgur_uploaded, webview);
 
-	return hc;
+	return msg;
 }
 
 /******************************************************************************
@@ -151,7 +146,7 @@
 {
 	gpointer plswait;
 
-	g_object_steal_data(G_OBJECT(webview), "imgupload-hc");
+	g_object_steal_data(G_OBJECT(webview), "imgupload-msg");
 	plswait = g_object_get_data(G_OBJECT(webview), "imgupload-plswait");
 	g_object_set_data(G_OBJECT(webview), "imgupload-plswait", NULL);
 
@@ -194,17 +189,24 @@
 }
 
 static void
+imgup_upload_cancel_message(SoupMessage *msg)
+{
+	soup_session_cancel_message(session, msg, SOUP_STATUS_CANCELLED);
+}
+
+static void
 imgup_upload_cancel(gpointer _webview)
 {
-	PurpleHttpConnection *hc;
+	SoupMessage *msg;
 	PidginWebView *webview = PIDGIN_WEBVIEW(_webview);
 
 	g_object_set_data(G_OBJECT(webview), "imgupload-plswait", NULL);
 	g_object_set_data(G_OBJECT(webview), "imgupload-cancelled",
 		GINT_TO_POINTER(TRUE));
-	hc = g_object_get_data(G_OBJECT(webview), "imgupload-hc");
-	if (hc)
-		purple_http_conn_cancel(hc);
+	msg = g_object_steal_data(G_OBJECT(webview), "imgupload-msg");
+	if (msg) {
+		soup_session_cancel_message(session, msg, SOUP_STATUS_CANCELLED);
+	}
 }
 
 static gboolean
@@ -212,15 +214,15 @@
 {
 	PidginConversation *gtkconv = _gtkconv;
 	PurpleConversation *conv = gtkconv->active_conv;
-	PurpleHttpConnection *hc;
+	SoupMessage *msg;
 	gpointer plswait;
 
 	if (!imgup_conn_is_hooked(purple_conversation_get_connection(conv)))
 		return FALSE;
 
-	hc = imgup_imgur_upload(webview, image);
-	g_object_set_data_full(G_OBJECT(webview), "imgupload-hc",
-		hc, (GDestroyNotify)purple_http_conn_cancel);
+	msg = imgup_imgur_upload(webview, image);
+	g_object_set_data_full(G_OBJECT(webview), "imgupload-msg", msg,
+	                       (GDestroyNotify)imgup_upload_cancel_message);
 
 	plswait = purple_request_wait(plugin_handle, _("Uploading image"),
 		_("Please wait for image URL being retrieved..."),
@@ -411,6 +413,7 @@
 	purple_prefs_add_bool(IMGUP_PREF_PREFIX "use_url_desc", TRUE);
 
 	plugin_handle = plugin;
+	session = soup_session_new();
 
 	it = purple_connections_get_all();
 	for (; it; it = g_list_next(it)) {
@@ -444,6 +447,8 @@
 {
 	GList *it;
 
+	soup_session_abort(session);
+
 	it = purple_conversations_get_all();
 	for (; it; it = g_list_next(it)) {
 		PurpleConversation *conv = it->data;
@@ -458,6 +463,7 @@
 		imgup_conn_uninit(gc);
 	}
 
+	g_clear_object(&session);
 	plugin_handle = NULL;
 
 	return TRUE;
--- a/pidgin/plugins/meson.build	Thu Oct 10 22:50:09 2019 -0400
+++ b/pidgin/plugins/meson.build	Thu Oct 10 23:25:02 2019 -0400
@@ -51,7 +51,7 @@
 	    install : true, install_dir : PIDGIN_PLUGINDIR)
 
 	imgupload = library('imgupload', 'imgupload.c',
-	    dependencies : [json, libpurple_dep, libpidgin_dep, glib],
+	    dependencies : [json, libpurple_dep, libpidgin_dep, libsoup, glib],
 	    name_prefix : '',
 	    build_by_default: false,
 	    install : false, install_dir : PIDGIN_PLUGINDIR)
@@ -63,7 +63,7 @@
 	    install : false, install_dir : PIDGIN_PLUGINDIR)
 
 	relnot = library('relnot', 'relnot.c',
-	    dependencies : [libpurple_dep, libpidgin_dep, glib],
+	    dependencies : [libpurple_dep, libpidgin_dep, libsoup, glib],
 	    name_prefix : '',
 	    install : true, install_dir : PIDGIN_PLUGINDIR)
 
--- a/pidgin/plugins/relnot.c	Thu Oct 10 22:50:09 2019 -0400
+++ b/pidgin/plugins/relnot.c	Thu Oct 10 23:25:02 2019 -0400
@@ -25,6 +25,7 @@
 
 #include "internal.h"
 
+#include <libsoup/soup.h>
 #include <string.h>
 
 #include "connection.h"
@@ -42,6 +43,8 @@
 
 #include "pidgin.h"
 
+static SoupSession *session = NULL;
+
 /* 1 day */
 #define MIN_CHECK_INTERVAL 60 * 60 * 24
 
@@ -58,8 +61,9 @@
 	purple_notify_uri(NULL, PURPLE_WEBSITE);
 }
 
-static void version_fetch_cb(PurpleHttpConnection *hc,
-	PurpleHttpResponse *response, gpointer user_data)
+static void
+version_fetch_cb(G_GNUC_UNUSED SoupSession *session, SoupMessage *msg,
+                 gpointer user_data)
 {
 	gchar *cur_ver;
 	const char *changelog;
@@ -68,10 +72,11 @@
 	GString *message;
 	int i = 0;
 
-	if(!purple_http_response_is_successful(response))
+	if (!SOUP_STATUS_IS_SUCCESSFUL(msg->status_code)) {
 		return;
+	}
 
-	changelog = purple_http_response_get_data(response, NULL);
+	changelog = msg->response_body->data;
 
 	while(changelog[i] && changelog[i] != '\n') i++;
 
@@ -107,25 +112,23 @@
 {
 	int last_check = purple_prefs_get_int("/plugins/gtk/relnot/last_check");
 	if(!last_check || time(NULL) - last_check > MIN_CHECK_INTERVAL) {
-		gchar *url;
-		const char *host = "pidgin.im";
+		SoupMessage *msg;
+
+		purple_debug_info("relnot", "Checking for new version.");
 
-		url = g_strdup_printf("https://%s/version.php?version=%s&build=%s",
-				host,
-				purple_core_get_version(),
+		msg = soup_form_request_new("GET", "https://pidgin.im/version.php",
+		                            "version", purple_core_get_version(),
+		                            "build",
 #ifdef _WIN32
-				"purple-win32"
+		                            "purple-win32",
 #else
-				"purple"
+		                            "purple",
 #endif
-		);
+		                            NULL);
 
-		purple_http_get(NULL, version_fetch_cb, NULL, url);
-
-		g_free(url);
+		soup_session_queue_message(session, msg, version_fetch_cb, NULL);
 
 		purple_prefs_set_int("/plugins/gtk/relnot/last_check", time(NULL));
-
 	}
 }
 
@@ -169,6 +172,8 @@
 	purple_signal_connect(purple_connections_get_handle(), "signed-on",
 						plugin, PURPLE_CALLBACK(signed_on_cb), NULL);
 
+	session = soup_session_new();
+
 	/* we don't check if we're offline */
 	if(purple_connections_get_all())
 		do_check();
@@ -179,6 +184,8 @@
 static gboolean
 plugin_unload(PurplePlugin *plugin, GError **error)
 {
+	soup_session_abort(session);
+	g_clear_object(&session);
 	return TRUE;
 }
 

mercurial