[Xfce4-commits] [apps/xfce4-screenshooter] 09/14: Remove dependency on curl/json-glib and port to libsoup (doesnt work yet..)

noreply at xfce.org noreply at xfce.org
Mon Dec 15 22:35:03 CET 2014


This is an automated email from the git hooks/post-receive script.

landry pushed a commit to branch master
in repository apps/xfce4-screenshooter.

commit 06105c6cf21dda029197003aa3b36ad279d275f6
Author: Landry Breuil <landry at xfce.org>
Date:   Mon Dec 15 21:37:45 2014 +0100

    Remove dependency on curl/json-glib and port to libsoup (doesnt work yet..)
---
 Makefile.am               |   12 ---
 configure.ac.in           |    2 -
 lib/screenshooter-imgur.c |  183 ++++++++++++++-------------------------------
 3 files changed, 58 insertions(+), 139 deletions(-)

diff --git a/Makefile.am b/Makefile.am
index 5eb3d62..b448616 100644
--- a/Makefile.am
+++ b/Makefile.am
@@ -44,8 +44,6 @@ lib_libscreenshooter_la_CFLAGS = \
 	@LIBXFCE4UTIL_CFLAGS@ \
 	@LIBXFCE4UI_CFLAGS@ \
 	@SOUP_CFLAGS@ \
-	@CURL_CFLAGS@ \
-	@JSON_GLIB_CFLAGS@ \
 	@XFIXES_CFLAGS@ \
   -DPACKAGE_LOCALE_DIR=\"$(localedir)\"
 
@@ -57,8 +55,6 @@ lib_libscreenshooter_la_LIBADD = \
 	@LIBXFCE4UI_LIBS@ \
   @GLIB_LIBS@ \
 	@SOUP_LIBS@ \
-	@CURL_LIBS@ \
-	@JSON_GLIB_LIBS@ \
 	@LIBXEXT_LIBS@ \
 	@LIBX11_LIBS@ \
 	@XFIXES_LIBS@
@@ -96,8 +92,6 @@ src_xfce4_screenshooter_CFLAGS = \
 	@LIBXFCE4UI_CFLAGS@ \
 	@GTHREAD_CFLAGS@ \
 	@SOUP_CFLAGS@ \
-	@CURL_CFLAGS@ \
-	@JSON_GLIB_CFLAGS@ \
 	-DPACKAGE_LOCALE_DIR=\"$(localedir)\"
 
 src_xfce4_screenshooter_LDFLAGS = \
@@ -106,8 +100,6 @@ src_xfce4_screenshooter_LDFLAGS = \
 	@GLIB_LIBS@ \
 	@GTHREAD_LIBS@ \
 	@SOUP_LIBS@ \
-	@CURL_LIBS@ \
-	@JSON_GLIB_LIBS@ \
 	@LIBXFCE4UTIL_LIBS@ \
 	@LIBXFCE4UI_LIBS@
 
@@ -141,8 +133,6 @@ panel_plugin_libscreenshooterplugin_la_CFLAGS =	\
 	@LIBXFCE4PANEL_CFLAGS@ \
 	@LIBXFCE4UI_CFLAGS@ \
 	@GTHREAD_CFLAGS@ \
-	@CURL_CFLAGS@ \
-	@JSON_GLIB_CFLAGS@ \
 	@SOUP_CFLAGS@
 
 panel_plugin_libscreenshooterplugin_la_LDFLAGS = \
@@ -157,8 +147,6 @@ panel_plugin_libscreenshooterplugin_la_LIBADD = \
 	@LIBXFCE4PANEL_LIBS@ \
 	@GTHREAD_LIBS@ \
 	@SOUP_LIBS@ \
-	@CURL_LIBS@ \
-	@JSON_GLIB_LIBS@ \
 	@LIBXFCE4UI_LIBS@ \
 	lib/libscreenshooter.la
 
diff --git a/configure.ac.in b/configure.ac.in
index 9173e8b..14aa3ad 100644
--- a/configure.ac.in
+++ b/configure.ac.in
@@ -56,8 +56,6 @@ XDT_CHECK_PACKAGE([SOUP], [libsoup-2.4], [2.26.0])
 XDT_CHECK_PACKAGE([EXO], [exo-1], [0.5.0])
 XDT_CHECK_PACKAGE([LIBXEXT], [xext], [1.0.0])
 XDT_CHECK_OPTIONAL_PACKAGE([XFIXES], [xfixes], [4.0.0], [xfixes], [XFIXES extension support])
-XDT_CHECK_OPTIONAL_PACKAGE([CURL], [libcurl], [7.19], [curl], [curl for imgur.com support])
-XDT_CHECK_OPTIONAL_PACKAGE([JSON_GLIB], [json-glib-1.0], [0.7.6], [json-glib], [json-glib for imgur.com support])
 XDT_CHECK_LIBX11()
 
 dnl ******************************
diff --git a/lib/screenshooter-imgur.c b/lib/screenshooter-imgur.c
index f3a88ca..adf4eca 100644
--- a/lib/screenshooter-imgur.c
+++ b/lib/screenshooter-imgur.c
@@ -21,9 +21,7 @@
 #include "screenshooter-imgur.h"
 #include <string.h>
 #include <stdlib.h>
-#include <curl/curl.h>
-#include <json-glib/json-glib.h>
-#include <json-glib/json-gobject.h>
+#include <libsoup/soup.h>
 
 typedef enum
 {
@@ -51,83 +49,22 @@ static void              cb_update_info            (ExoJob            *job,
                                                     gchar             *message,
                                                     GtkWidget         *label);
 
-
-
-/* Private */
-
-
-struct MemoryStruct {
-  char *memory;
-  size_t size;
-};
- 
- 
-static size_t WriteMemoryCallback(void *ptr, size_t size, size_t nmemb, void *data)
-{
-	size_t realsize = size * nmemb;
-	struct MemoryStruct *mem = (struct MemoryStruct *)data;
-
-	mem->memory = realloc(mem->memory, mem->size + realsize + 1);
-	if (mem->memory == NULL) {
-		printf("not enough memory (realloc returned NULL)\n");
-		exit(0);
-	}
-
-	memcpy(&(mem->memory[mem->size]), ptr, realsize);
-	mem->size += realsize;
-	mem->memory[mem->size] = 0;
-
-	return realsize;
-}
-
-static char *get_image_hash(char *json)
-{
-	JsonParser *parser;
-	JsonNode *root;
-	GError *error;
-	gchar *ret = NULL;
-
-	parser = json_parser_new ();
-
-	error = NULL;
-	json_parser_load_from_data(parser, json, strlen(json), &error);
-
-	if (error)
-	{
-		g_error_free (error);
-		g_object_unref (parser);
-		return NULL;
-	}
-
-	root = json_parser_get_root (parser);
-	if (JSON_NODE_TYPE(root) == JSON_NODE_OBJECT) {
-		JsonNode *upload = json_object_get_member(json_node_get_object(root), "upload");
-		if (JSON_NODE_TYPE(upload) == JSON_NODE_OBJECT) {
-			JsonNode *image = json_object_get_member(json_node_get_object(upload), "image");
-			if (JSON_NODE_TYPE(image) == JSON_NODE_OBJECT) {
-				JsonNode *hash = json_object_get_member(json_node_get_object(image), "hash");
-				if (JSON_NODE_TYPE(hash) == JSON_NODE_VALUE) {
-					ret = json_node_dup_string(hash);
-				}
-			}
-		}
-	}
-
-	g_object_unref (parser);
-
-	return ret;
-}
-
 static gboolean
 imgur_upload_job (ScreenshooterJob *job, GValueArray *param_values, GError **error)
 {
   const gchar *image_path;
   gchar *online_file_name = NULL;
-  CURL *curl;
-  CURLcode res;
-
-
-  const gchar *upload_url = "http://api.imgur.com/2/upload.json";
+  gchar* proxy_uri;
+  SoupURI *soup_proxy_uri;
+  SoupLogger *log;
+  guint status;
+  SoupSession *session;
+  SoupMessage *msg;
+  SoupBuffer *buf;
+  GMappedFile *mapping;
+  SoupMultipart *mp;
+
+  const gchar *upload_url = "http://api.imgur.com/2/upload.xml";
 
   GError *tmp_error = NULL;
 
@@ -145,71 +82,67 @@ imgur_upload_job (ScreenshooterJob *job, GValueArray *param_values, GError **err
 
   image_path = g_value_get_string (g_value_array_get_nth (param_values, 0));
 
-	curl = curl_easy_init();
-	if(curl) {
-		struct MemoryStruct chunk;
-		struct curl_httppost *formpost=NULL;
-		struct curl_httppost *lastptr=NULL;
+  session = soup_session_sync_new ();
+  log = soup_logger_new (SOUP_LOGGER_LOG_HEADERS, -1);
+  soup_session_add_feature (session, (SoupSessionFeature *)log);
 
-		chunk.memory = malloc(1);  /* will be grown as needed by the realloc above */ 
-		chunk.size = 0;    /* no data at this point */ 
+  /* Set the proxy URI if any */
+  proxy_uri = g_getenv ("http_proxy");
 
-		curl_formadd(&formpost,
-			&lastptr,
-			CURLFORM_COPYNAME, "image",
-			CURLFORM_FILE, image_path,
-			CURLFORM_END);
-
-		curl_formadd(&formpost,
-			&lastptr,
-			CURLFORM_COPYNAME, "title",
-			CURLFORM_COPYCONTENTS, "Screenshot",
-			CURLFORM_END);
-
-		curl_formadd(&formpost,
-			&lastptr,
-			CURLFORM_COPYNAME, "name",
-			CURLFORM_COPYCONTENTS, "Screenshot",
-			CURLFORM_END);
+  if (proxy_uri != NULL)
+    {
+      soup_proxy_uri = soup_uri_new (proxy_uri);
+      g_object_set (session, "proxy-uri", soup_proxy_uri, NULL);
+      soup_uri_free (soup_proxy_uri);
+    }
 
-		curl_formadd(&formpost,
-			&lastptr,
-			CURLFORM_COPYNAME, "key",
-			CURLFORM_COPYCONTENTS, "a094536e9503bf5e289b65a8116a8d1c",
-			CURLFORM_END);
+  mapping = g_mapped_file_new(image_path, FALSE, NULL);
+  if (!mapping) {
+    g_object_unref (session);
+    g_object_unref (msg);
 
+    return FALSE;
+  }
 
-		curl_easy_setopt(curl, CURLOPT_URL, upload_url);
+  mp = soup_multipart_new(SOUP_FORM_MIME_TYPE_MULTIPART);
+  buf = soup_buffer_new_with_owner (g_mapped_file_get_contents (mapping),
+                                    g_mapped_file_get_length (mapping),
+                                    mapping, (GDestroyNotify)g_mapped_file_unref);
 
-		curl_easy_setopt(curl, CURLOPT_HTTPPOST, formpost);
-		curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, WriteMemoryCallback);
-		curl_easy_setopt(curl, CURLOPT_WRITEDATA, (void *)&chunk);
+  soup_multipart_append_form_file (mp, "image", NULL, NULL, buf);
+  soup_multipart_append_form_string (mp, "name", "Screenshot");
+  soup_multipart_append_form_string (mp, "title", "Screenshot");
+  soup_multipart_append_form_string (mp, "key", "a094536e9503bf5e289b65a8116a8d1c");
+  msg = soup_form_request_new_from_multipart (upload_url, mp);
 
+  // for v3 soup_message_headers_append (msg->request_headers, "Referer", referring_url);
+  status = soup_session_send_message (session, msg);
 
-		res = curl_easy_perform(curl);
+  if (!SOUP_STATUS_IS_SUCCESSFUL (msg->status_code))
+    {
+      TRACE ("Error during the POST exchange: %d %s\n",
+             msg->status_code, msg->reason_phrase);
 
-		curl_formfree(formpost);
-		curl_easy_cleanup(curl);
+      tmp_error = g_error_new (SOUP_HTTP_ERROR,
+                         msg->status_code,
+                         _("An error occurred when transferring the data"
+                           " to imgur."));
+      g_propagate_error (error, tmp_error);
+      g_object_unref (session);
+      g_object_unref (msg);
 
-		online_file_name = get_image_hash(chunk.memory);
+      return FALSE;
+    }
 
-		if(chunk.memory)
-			free(chunk.memory);
+  TRACE("response was %s\n", msg->response_body->data);
+  sscanf (msg->response_body->data, "<hash>%s</hash>", online_file_name);
 
-		curl_global_cleanup();
-	} else {
-		fprintf(stderr, "Unable to init curl\n");
-	}
+  soup_buffer_free (buf);
+  g_object_unref (session);
+  g_object_unref (msg);
 
   screenshooter_job_image_uploaded (job, online_file_name);
 
-  if (tmp_error)
-    {
-      g_propagate_error (error, tmp_error);
-
-      return FALSE;
-    }
-
   return TRUE;
 }
 

-- 
To stop receiving notification emails like this one, please contact
the administrator of this repository.


More information about the Xfce4-commits mailing list