pidgin: 72d651be: Use the same fix for Purple::Util::fetch...

sadrul at pidgin.im sadrul at pidgin.im
Tue Apr 22 22:35:39 EDT 2008


-----------------------------------------------------------------
Revision: 72d651bedf881d43552b232aa17cbb16208b0df0
Ancestor: 706f541e3c5a550392f0424aa63704da0341ae0c
Author: sadrul at pidgin.im
Date: 2008-04-23T02:29:39
Branch: im.pidgin.pidgin
URL: http://d.pidgin.im/viewmtn/revision/info/72d651bedf881d43552b232aa17cbb16208b0df0

Modified files:
        libpurple/plugins/perl/common/Request.xs
        libpurple/plugins/perl/common/Util.xs
        libpurple/plugins/perl/perl-common.c
        libpurple/plugins/perl/perl-common.h

ChangeLog: 

Use the same fix for Purple::Util::fetch_url (from 4b6378d5e) for
Purple::Request:: functions as well. This allows the callbacks to be
specified both as coderefs or as strings (name of the callback function).

-------------- next part --------------
============================================================
--- libpurple/plugins/perl/common/Request.xs	07e51e9fd6b1a279f9e1e8a80095ad298572e032
+++ libpurple/plugins/perl/common/Request.xs	6cd7820431a575963ebf547820fd0e5b7b59ba72
@@ -15,10 +15,20 @@ typedef struct {
 
 
 typedef struct {
-	char *cancel_cb;
-	char *ok_cb;
+	SV *ok_fun;
+	SV *cancel_fun;
 } PurplePerlRequestData;
 
+static void
+purple_perl_request_data_free(PurplePerlRequestData *ppr)
+{
+	if (ppr->ok_fun)
+		SvREFCNT_dec(ppr->ok_fun);
+	if (ppr->cancel_fun)
+		SvREFCNT_dec(ppr->cancel_fun);
+	g_free(ppr);
+}
+
 /********************************************************/
 /*                                                      */
 /* Callback function that calls a perl subroutine       */
@@ -39,23 +49,19 @@ purple_perl_request_ok_cb(void * data, P
 
 	XPUSHs(purple_perl_bless_object(fields, "Purple::Request::Fields"));
 	PUTBACK;
-
-	call_pv(gpr->ok_cb, G_EVAL | G_SCALAR);
+	call_sv(gpr->ok_fun, G_EVAL | G_SCALAR);
 	SPAGAIN;
 
 	PUTBACK;
 	FREETMPS;
 	LEAVE;
 
-	g_free(gpr->ok_cb);
-	g_free(gpr->cancel_cb);
-	g_free(gpr);
+	purple_perl_request_data_free(gpr);
 }
 
 static void
 purple_perl_request_cancel_cb(void * data, PurpleRequestFields *fields)
 {
-
 	PurplePerlRequestData *gpr = (PurplePerlRequestData *)data;
 
 	dSP;
@@ -65,16 +71,14 @@ purple_perl_request_cancel_cb(void * dat
 
 	XPUSHs(purple_perl_bless_object(fields, "Purple::Request::Fields"));
 	PUTBACK;
-	call_pv(gpr->cancel_cb, G_EVAL | G_SCALAR);
+	call_sv(gpr->cancel_fun, G_EVAL | G_SCALAR);
 	SPAGAIN;
 
 	PUTBACK;
 	FREETMPS;
 	LEAVE;
 
-	g_free(gpr->ok_cb);
-	g_free(gpr->cancel_cb);
-	g_free(gpr);
+	purple_perl_request_data_free(gpr);
 }
 
 MODULE = Purple::Request  PACKAGE = Purple::Request  PREFIX = purple_request_
@@ -131,14 +135,13 @@ CODE:
 	SV * cancel_cb
 CODE:
 	PurplePerlRequestData *gpr;
-	STRLEN len;
 	char *basename;
 
 	basename = g_path_get_basename(handle->path);
 	purple_perl_normalize_script_name(basename);
 	gpr = g_new(PurplePerlRequestData, 1);
-	gpr->ok_cb = g_strdup_printf("Purple::Script::%s::%s", basename, SvPV(ok_cb, len));
-	gpr->cancel_cb = g_strdup_printf("Purple::Script::%s::%s", basename, SvPV(cancel_cb, len));
+	gpr->ok_fun = purple_perl_sv_from_fun(handle, ok_cb);
+	gpr->cancel_fun = purple_perl_sv_from_fun(handle, cancel_cb);
 	g_free(basename);
 
 	RETVAL = purple_request_input(handle, title, primary, secondary, default_value, multiline, masked, hint, ok_text, G_CALLBACK(purple_perl_request_ok_cb), cancel_text, G_CALLBACK(purple_perl_request_cancel_cb), NULL, NULL, NULL, gpr);
@@ -155,14 +158,13 @@ CODE:
 	SV * cancel_cb
 CODE:
 	PurplePerlRequestData *gpr;
-	STRLEN len;
 	char *basename;
 
 	basename = g_path_get_basename(handle->path);
 	purple_perl_normalize_script_name(basename);
 	gpr = g_new(PurplePerlRequestData, 1);
-	gpr->ok_cb = g_strdup_printf("Purple::Script::%s::%s", basename, SvPV(ok_cb, len));
-	gpr->cancel_cb = g_strdup_printf("Purple::Script::%s::%s", basename, SvPV(cancel_cb, len));
+	gpr->ok_fun = purple_perl_sv_from_fun(handle, ok_cb);
+	gpr->cancel_fun = purple_perl_sv_from_fun(handle, cancel_cb);
 	g_free(basename);
 
 	RETVAL = purple_request_file(handle, title, filename, savedialog, G_CALLBACK(purple_perl_request_ok_cb), G_CALLBACK(purple_perl_request_cancel_cb), NULL, NULL, NULL, gpr);
@@ -182,14 +184,13 @@ CODE:
 	SV * cancel_cb
 CODE:
 	PurplePerlRequestData *gpr;
-	STRLEN len;
 	char *basename;
 
 	basename = g_path_get_basename(handle->path);
 	purple_perl_normalize_script_name(basename);
 	gpr = g_new(PurplePerlRequestData, 1);
-	gpr->ok_cb = g_strdup_printf("Purple::Script::%s::%s", basename, SvPV(ok_cb, len));
-	gpr->cancel_cb = g_strdup_printf("Purple::Script::%s::%s", basename, SvPV(cancel_cb, len));
+	gpr->ok_fun = purple_perl_sv_from_fun(handle, ok_cb);
+	gpr->cancel_fun = purple_perl_sv_from_fun(handle, cancel_cb);
 	g_free(basename);
 
 	RETVAL = purple_request_fields(handle, title, primary, secondary, fields, ok_text, G_CALLBACK(purple_perl_request_ok_cb), cancel_text, G_CALLBACK(purple_perl_request_cancel_cb), NULL, NULL, NULL, gpr);
============================================================
--- libpurple/plugins/perl/common/Util.xs	19edeb0d7ba209988b79b448ac676c349d54a03e
+++ libpurple/plugins/perl/common/Util.xs	cce0d176dcee17ef5f4a52db256c668070e7db98
@@ -257,29 +257,13 @@ CODE:
 	gboolean http11
 	SV * cb
 CODE:
-	SV *sv = NULL;
+	SV *sv = purple_perl_sv_from_fun(plugin, cb);
 
-
-	if (SvTYPE(cb) == SVt_RV) {
-		SV *cbsv = SvRV(cb);
-
-		if (SvTYPE(cbsv) == SVt_PVCV) {
-			sv = newSVsv(cb);
-		} else {
-			purple_debug_warning("perl", "Callback not a valid coderef in purple_util_fetch_url.\n");
-		}
-	} else if (SvTYPE(cb) == SVt_PV) {
-		PurplePerlScript *gps;
-
-		gps = (PurplePerlScript *)PURPLE_PLUGIN_LOADER_INFO(plugin);
-		sv = newSVpvf("%s::%s", gps->package, SvPV_nolen(cb));
-	} else {
-		purple_debug_warning("perl", "Callback not a valid type, only strings and coderefs allowed in purple_util_fetch_url.\n");
-	}
-
 	if (sv != NULL) {
 		purple_util_fetch_url(url, full, user_agent, http11,
 		                      purple_perl_util_url_cb, sv);
+	} else {
+		purple_debug_warning("perl", "Callback not a valid type, only strings and coderefs allowed in purple_util_fetch_url.\n");
 	}
 
 void
============================================================
--- libpurple/plugins/perl/perl-common.c	3eff18f5858e3fde91476a65d5cc32693129dbba
+++ libpurple/plugins/perl/perl-common.c	6b970966474ea7eb29f3e7fa1a9f8f935176fff7
@@ -616,3 +616,26 @@ purple_perl_sv_from_vargs(const PurpleVa
 
 	return NULL;
 }
+
+SV *purple_perl_sv_from_fun(PurplePlugin *plugin, SV *callback)
+{
+	SV *sv = NULL;
+
+	if (SvTYPE(callback) == SVt_RV) {
+		SV *cbsv = SvRV(callback);
+
+		if (SvTYPE(cbsv) == SVt_PVCV) {
+			sv = newSVsv(callback);
+		}
+	} else if (SvTYPE(callback) == SVt_PV) {
+		PurplePerlScript *gps;
+
+		gps = (PurplePerlScript *)PURPLE_PLUGIN_LOADER_INFO(plugin);
+		sv = newSVpvf("%s::%s", gps->package, SvPV_nolen(callback));
+	} else {
+		purple_debug_warning("perl", "Callback not a valid type, only strings and coderefs allowed.\n");
+	}
+
+	return sv;
+}
+
============================================================
--- libpurple/plugins/perl/perl-common.h	98734a7bc056d39b7195307a4195066a8c23d895
+++ libpurple/plugins/perl/perl-common.h	fe675694322560f37e1f861cacaae9ba7572882f
@@ -67,5 +67,5 @@ SV *purple_perl_sv_from_vargs(const Purp
 void *purple_perl_data_from_sv(PurpleValue *value, SV *sv);
 SV *purple_perl_sv_from_vargs(const PurpleValue *value, va_list *args,
                             void ***copy_arg);
-
+SV *purple_perl_sv_from_fun(PurplePlugin *plugin, SV *callback);
 #endif /* _PURPLE_PERL_COMMON_H_ */


More information about the Commits mailing list