/pidgin/main: 3bba206f27f6: HTTP: migrate purple_util_fetch_url ...

Tomasz Wasilczyk tomkiewicz at cpw.pidgin.im
Sun Jun 23 06:40:33 EDT 2013


Changeset: 3bba206f27f601ad873a7d90e2e33a36dc75788c
Author:	 Tomasz Wasilczyk <tomkiewicz at cpw.pidgin.im>
Date:	 2013-06-23 12:40 +0200
Branch:	 default
URL: https://hg.pidgin.im/pidgin/main/rev/3bba206f27f6

Description:

HTTP: migrate purple_util_fetch_url to new API for Pidgin UI

diffstat:

 libpurple/http.c      |   3 +-
 pidgin/gtkprefs.c     |  55 ++++++++++++++++++++++++++++++-------------------
 pidgin/gtksmiley.c    |  56 +++++++++++++++++++-------------------------------
 pidgin/gtkstatusbox.c |  51 ----------------------------------------------
 4 files changed, 57 insertions(+), 108 deletions(-)

diffs (truncated from 322 to 300 lines):

diff --git a/libpurple/http.c b/libpurple/http.c
--- a/libpurple/http.c
+++ b/libpurple/http.c
@@ -38,6 +38,7 @@
 
 #define PURPLE_HTTP_REQUEST_DEFAULT_MAX_REDIRECTS 20
 #define PURPLE_HTTP_REQUEST_DEFAULT_TIMEOUT 30
+#define PURPLE_HTTP_REQUEST_DEFAULT_MAX_LENGTH 1048576
 
 typedef struct _PurpleHttpSocket PurpleHttpSocket;
 
@@ -1714,7 +1715,7 @@ PurpleHttpRequest * purple_http_request_
 	request->timeout = PURPLE_HTTP_REQUEST_DEFAULT_TIMEOUT;
 	request->max_redirects = PURPLE_HTTP_REQUEST_DEFAULT_MAX_REDIRECTS;
 	request->http11 = TRUE;
-	request->max_length = -1;
+	request->max_length = PURPLE_HTTP_REQUEST_DEFAULT_MAX_LENGTH;
 
 	return request;
 }
diff --git a/pidgin/gtkprefs.c b/pidgin/gtkprefs.c
--- a/pidgin/gtkprefs.c
+++ b/pidgin/gtkprefs.c
@@ -26,9 +26,9 @@
  */
 #include "internal.h"
 #include "pidgin.h"
-#include "obsolete.h"
 
 #include "debug.h"
+#include "http.h"
 #include "nat-pmp.h"
 #include "notify.h"
 #include "prefs.h"
@@ -87,6 +87,9 @@
 
 #define PREFS_OPTIMAL_ICON_SIZE 32
 
+/* 25MB */
+#define PREFS_MAX_DOWNLOADED_THEME_SIZE 26214400
+
 struct theme_info {
 	gchar *type;
 	gchar *extension;
@@ -110,6 +113,7 @@ static GtkWidget *prefs_conv_themes_comb
 static GtkWidget *prefs_conv_variants_combo_box;
 static GtkWidget *prefs_status_themes_combo_box;
 static GtkWidget *prefs_smiley_themes_combo_box;
+static PurpleHttpConnection *prefs_conv_themes_running_request = NULL;
 
 /* Keyrings page */
 static GtkWidget *keyring_page_instance = NULL;
@@ -535,6 +539,10 @@ static void keyring_page_cleanup(void);
 static void
 delete_prefs(GtkWidget *asdf, void *gdsa)
 {
+	/* Cancel HTTP requests */
+	purple_http_conn_cancel(prefs_conv_themes_running_request);
+	prefs_conv_themes_running_request = NULL;
+
 	/* Close any "select sound" request dialogs */
 	purple_request_close_with_handle(prefs);
 
@@ -1060,18 +1068,26 @@ theme_install_theme(char *path, struct t
 }
 
 static void
-theme_got_url(PurpleUtilFetchUrlData *url_data, gpointer user_data,
-		const gchar *themedata, size_t len, const gchar *error_message)
+theme_got_url(PurpleHttpConnection *http_conn, PurpleHttpResponse *response,
+	gpointer _info)
 {
+	struct theme_info *info = _info;
+	const gchar *themedata;
+	size_t len;
 	FILE *f;
 	gchar *path;
 	size_t wc;
 
-	if ((error_message != NULL) || (len == 0)) {
-		free_theme_info(user_data);
+	g_assert(http_conn == prefs_conv_themes_running_request);
+	prefs_conv_themes_running_request = NULL;
+
+	if (!purple_http_response_is_successfull(response)) {
+		free_theme_info(info);
 		return;
 	}
 
+	themedata = purple_http_response_get_data(response, &len);
+
 	f = purple_mkstemp(&path, TRUE);
 	wc = fwrite(themedata, len, 1, f);
 	if (wc != 1) {
@@ -1079,12 +1095,12 @@ theme_got_url(PurpleUtilFetchUrlData *ur
 		fclose(f);
 		g_unlink(path);
 		g_free(path);
-		free_theme_info(user_data);
+		free_theme_info(info);
 		return;
 	}
 	fclose(f);
 
-	theme_install_theme(path, user_data);
+	theme_install_theme(path, info);
 
 	g_unlink(path);
 	g_free(path);
@@ -1121,22 +1137,19 @@ theme_dnd_recv(GtkWidget *widget, GdkDra
 			}
 			theme_install_theme(tmp, info);
 			g_free(tmp);
-		} else if (!g_ascii_strncasecmp(name, "http://", 7)) {
+		} else if (!g_ascii_strncasecmp(name, "http://", 7) ||
+			!g_ascii_strncasecmp(name, "https://", 8)) {
 			/* Oo, a web drag and drop. This is where things
 			 * will start to get interesting */
-			purple_util_fetch_url(name, TRUE, NULL, FALSE, -1, theme_got_url, info);
-		} else if (!g_ascii_strncasecmp(name, "https://", 8)) {
-			/* purple_util_fetch_url() doesn't support HTTPS, but we want users
-			 * to be able to drag and drop links from the SF trackers, so
-			 * we'll try it as an HTTP URL. */
-			char *tmp = g_strdup(name + 1);
-			tmp[0] = 'h';
-			tmp[1] = 't';
-			tmp[2] = 't';
-			tmp[3] = 'p';
-
-			purple_util_fetch_url(tmp, TRUE, NULL, FALSE, -1, theme_got_url, info);
-			g_free(tmp);
+			PurpleHttpRequest *hr;
+			purple_http_conn_cancel(prefs_conv_themes_running_request);
+
+			hr = purple_http_request_new(name);
+			purple_http_request_set_max_len(hr,
+				PREFS_MAX_DOWNLOADED_THEME_SIZE);
+			prefs_conv_themes_running_request = purple_http_request(
+				NULL, hr, theme_got_url, info);
+			purple_http_request_unref(hr);
 		} else
 			free_theme_info(info);
 
diff --git a/pidgin/gtksmiley.c b/pidgin/gtksmiley.c
--- a/pidgin/gtksmiley.c
+++ b/pidgin/gtksmiley.c
@@ -27,9 +27,9 @@
 
 #include "internal.h"
 #include "pidgin.h"
-#include "obsolete.h"
 
 #include "debug.h"
+#include "http.h"
 #include "notify.h"
 #include "smiley.h"
 
@@ -61,6 +61,7 @@ typedef struct
 
 	GtkWidget *treeview;
 	GtkListStore *model;
+	PurpleHttpConnection *running_request;
 } SmileyManager;
 
 enum
@@ -723,34 +724,23 @@ edit_selected_cb(GtkTreeModel *model, Gt
 }
 
 static void
-smiley_got_url(PurpleUtilFetchUrlData *url_data, gpointer user_data,
-		const gchar *smileydata, size_t len, const gchar *error_message)
+smiley_got_url(PurpleHttpConnection *http_conn, PurpleHttpResponse *response,
+	gpointer _dialog)
 {
-	SmileyManager *dialog = user_data;
-	FILE *f;
-	gchar *path;
-	size_t wc;
+	SmileyManager *dialog = _dialog;
 	PidginSmiley *ps;
 	GdkPixbuf *image;
+	const gchar *smileydata;
+	size_t len;
 
-	if ((error_message != NULL) || (len == 0)) {
+	g_assert(http_conn == smiley_manager->running_request);
+	smiley_manager->running_request = NULL;
+
+	if (!purple_http_response_is_successfull(response))
 		return;
-	}
 
-	f = purple_mkstemp(&path, TRUE);
-	wc = fwrite(smileydata, len, 1, f);
-	if (wc != 1) {
-		purple_debug_warning("smiley_got_url", "Unable to write smiley data.\n");
-		fclose(f);
-		g_unlink(path);
-		g_free(path);
-		return;
-	}
-	fclose(f);
-
-	image = pidgin_pixbuf_new_from_file(path);
-	g_unlink(path);
-	g_free(path);
+	smileydata = purple_http_response_get_data(response, &len);
+	image = pidgin_pixbuf_from_data((const guchar *)smileydata, len);
 	if (!image)
 		return;
 
@@ -788,20 +778,15 @@ smiley_dnd_recv(GtkWidget *widget, GdkDr
 			if (gtk_image_get_pixbuf(GTK_IMAGE(ps->smiley_image)) == NULL)
 				gtk_dialog_response(GTK_DIALOG(ps->parent), GTK_RESPONSE_CANCEL);
 			g_free(tmp);
-		} else if (!g_ascii_strncasecmp(name, "http://", 7)) {
+		} else if (!g_ascii_strncasecmp(name, "http://", 7) ||
+			!g_ascii_strncasecmp(name, "https://", 8))
+		{
 			/* Oo, a web drag and drop. This is where things
 			 * will start to get interesting */
-			purple_util_fetch_url(name, TRUE, NULL, FALSE, -1, smiley_got_url, dialog);
-		} else if (!g_ascii_strncasecmp(name, "https://", 8)) {
-			/* purple_util_fetch_url() doesn't support HTTPS */
-			char *tmp = g_strdup(name + 1);
-			tmp[0] = 'h';
-			tmp[1] = 't';
-			tmp[2] = 't';
-			tmp[3] = 'p';
-
-			purple_util_fetch_url(tmp, TRUE, NULL, FALSE, -1, smiley_got_url, dialog);
-			g_free(tmp);
+			purple_http_conn_cancel(smiley_manager->
+				running_request);
+			smiley_manager->running_request = purple_http_get(NULL,
+				name, smiley_got_url, dialog);
 		}
 
 		gtk_drag_finish(dc, TRUE, FALSE, t);
@@ -872,6 +857,7 @@ static void smiley_manager_select_cb(Gtk
 		case GTK_RESPONSE_DELETE_EVENT:
 		case GTK_RESPONSE_CLOSE:
 			gtk_widget_destroy(dialog->window);
+			purple_http_conn_cancel(smiley_manager->running_request);
 			g_free(smiley_manager);
 			smiley_manager = NULL;
 			break;
diff --git a/pidgin/gtkstatusbox.c b/pidgin/gtkstatusbox.c
--- a/pidgin/gtkstatusbox.c
+++ b/pidgin/gtkstatusbox.c
@@ -43,7 +43,6 @@
 #include <gdk/gdkkeysyms.h>
 
 #include "internal.h"
-#include "obsolete.h"
 
 #include "account.h"
 #include "buddyicon.h"
@@ -372,54 +371,6 @@ icon_box_dnd_cb(GtkWidget *widget, GdkDr
 	gtk_drag_finish(dc, FALSE, FALSE, t);
 }
 
-static void
-statusbox_got_url(PurpleUtilFetchUrlData *url_data, gpointer user_data,
-                  const gchar *themedata, size_t len, const gchar *error_message)
-{
-	FILE *f;
-	gchar *path;
-	size_t wc;
-
-	if ((error_message != NULL) || (len == 0))
-		return;
-
-	f = purple_mkstemp(&path, TRUE);
-	wc = fwrite(themedata, len, 1, f);
-	if (wc != 1) {
-		purple_debug_warning("theme_got_url", "Unable to write theme data.\n");
-		fclose(f);
-		g_unlink(path);
-		g_free(path);
-		return;
-	}
-	fclose(f);
-
-	icon_choose_cb(path, user_data);
-
-	g_unlink(path);
-	g_free(path);
-}
-
-
-static gboolean
-statusbox_uri_handler(const char *proto, const char *cmd, GHashTable *params, void *data)
-{
-	const char *src;
-
-	if (g_ascii_strcasecmp(proto, "aim"))
-		return FALSE;
-
-	if (g_ascii_strcasecmp(cmd, "buddyicon"))



More information about the Commits mailing list