/pidgin/main: 2496c53cd401: Request API: HTML feature

Tomasz Wasilczyk twasilczyk at pidgin.im
Wed Sep 11 06:41:10 EDT 2013


Changeset: 2496c53cd401f6ac7de635ab0e2438a7be058dce
Author:	 Tomasz Wasilczyk <twasilczyk at pidgin.im>
Date:	 2013-09-11 12:41 +0200
Branch:	 default
URL: https://hg.pidgin.im/pidgin/main/rev/2496c53cd401

Description:

Request API: HTML feature

diffstat:

 finch/gntrequest.c  |   1 +
 libpurple/request.c |  89 +++++++++++++++++++++++++++++++++++++++++++++++++---
 libpurple/request.h |  29 +++++++++++++++++
 pidgin/gtkrequest.c |  39 ++++++++++++++++++----
 4 files changed, 144 insertions(+), 14 deletions(-)

diffs (truncated from 356 to 300 lines):

diff --git a/finch/gntrequest.c b/finch/gntrequest.c
--- a/finch/gntrequest.c
+++ b/finch/gntrequest.c
@@ -804,6 +804,7 @@ finch_request_folder(const char *title, 
 
 static PurpleRequestUiOps uiops =
 {
+	0,
 	finch_request_input,
 	finch_request_choice,
 	finch_request_action,
diff --git a/libpurple/request.c b/libpurple/request.c
--- a/libpurple/request.c
+++ b/libpurple/request.c
@@ -164,6 +164,7 @@ struct _PurpleRequestCommonParameters
 	PurpleConversation *conv;
 	gconstpointer icon_data;
 	gsize icon_size;
+	gboolean html;
 };
 
 PurpleRequestCommonParameters *
@@ -293,6 +294,24 @@ purple_request_cpar_get_custom_icon(Purp
 	return cpar->icon_data;
 }
 
+void
+purple_request_cpar_set_html(PurpleRequestCommonParameters *cpar,
+	gboolean enabled)
+{
+	g_return_if_fail(cpar != NULL);
+
+	cpar->html = enabled;
+}
+
+gboolean
+purple_request_cpar_is_html(PurpleRequestCommonParameters *cpar)
+{
+	if (cpar == NULL)
+		return FALSE;
+
+	return cpar->html;
+}
+
 PurpleRequestFields *
 purple_request_fields_new(void)
 {
@@ -1708,6 +1727,37 @@ purple_request_field_alphanumeric_valida
 
 /* -- */
 
+static gchar *
+purple_request_strip_html_custom(const gchar *html)
+{
+	gchar *tmp, *ret;
+
+	tmp = purple_strreplace(html, "\n", "<br>");
+	ret = purple_markup_strip_html(tmp);
+	g_free(tmp);
+
+	return ret;
+}
+
+static gchar **
+purple_request_strip_html(PurpleRequestCommonParameters *cpar,
+	const char **primary, const char **secondary)
+{
+	PurpleRequestUiOps *ops = purple_request_get_ui_ops();
+	gchar **ret;
+
+	if (!purple_request_cpar_is_html(cpar))
+		return NULL;
+	if (ops->features & PURPLE_REQUEST_FEATURE_HTML)
+		return NULL;
+
+	ret = g_new0(gchar*, 3);
+	*primary = ret[0] = purple_request_strip_html_custom(*primary);
+	*secondary = ret[1] = purple_request_strip_html_custom(*secondary);
+
+	return ret;
+}
+
 void *
 purple_request_input(void *handle, const char *title, const char *primary,
 				   const char *secondary, const char *default_value,
@@ -1719,8 +1769,10 @@ purple_request_input(void *handle, const
 {
 	PurpleRequestUiOps *ops;
 
-	if (G_UNLIKELY(ok_text != NULL || ok_cb != NULL)) {
+	if (G_UNLIKELY(ok_text == NULL || ok_cb == NULL)) {
 		purple_request_cpar_unref(cpar);
+		g_warn_if_fail(ok_text != NULL);
+		g_warn_if_fail(ok_cb != NULL);
 		g_return_val_if_reached(NULL);
 	}
 
@@ -1728,6 +1780,9 @@ purple_request_input(void *handle, const
 
 	if (ops != NULL && ops->request_input != NULL) {
 		PurpleRequestInfo *info;
+		gchar **tmp;
+
+		tmp = purple_request_strip_html(cpar, &primary, &secondary);
 
 		info            = g_new0(PurpleRequestInfo, 1);
 		info->type      = PURPLE_REQUEST_INPUT;
@@ -1738,6 +1793,7 @@ purple_request_input(void *handle, const
 
 		handles = g_list_append(handles, info);
 
+		g_strfreev(tmp);
 		purple_request_cpar_unref(cpar);
 		return info->ui_handle;
 	}
@@ -1757,8 +1813,10 @@ purple_request_choice(void *handle, cons
 	void *ui_handle;
 	va_list args;
 
-	if (G_UNLIKELY(ok_text != NULL || ok_cb != NULL)) {
+	if (G_UNLIKELY(ok_text == NULL || ok_cb == NULL)) {
 		purple_request_cpar_unref(cpar);
+		g_warn_if_fail(ok_text != NULL);
+		g_warn_if_fail(ok_cb != NULL);
 		g_return_val_if_reached(NULL);
 	}
 
@@ -1783,10 +1841,13 @@ purple_request_choice_varg(void *handle,
 {
 	PurpleRequestUiOps *ops;
 
-	if (G_UNLIKELY(ok_text != NULL || ok_cb != NULL ||
-		cancel_text != NULL))
+	if (G_UNLIKELY(ok_text == NULL || ok_cb == NULL ||
+		cancel_text == NULL))
 	{
 		purple_request_cpar_unref(cpar);
+		g_warn_if_fail(ok_text != NULL);
+		g_warn_if_fail(ok_cb != NULL);
+		g_warn_if_fail(cancel_text != NULL);
 		g_return_val_if_reached(NULL);
 	}
 
@@ -1794,6 +1855,9 @@ purple_request_choice_varg(void *handle,
 
 	if (ops != NULL && ops->request_choice != NULL) {
 		PurpleRequestInfo *info;
+		gchar **tmp;
+
+		tmp = purple_request_strip_html(cpar, &primary, &secondary);
 
 		info            = g_new0(PurpleRequestInfo, 1);
 		info->type      = PURPLE_REQUEST_CHOICE;
@@ -1804,6 +1868,7 @@ purple_request_choice_varg(void *handle,
 
 		handles = g_list_append(handles, info);
 
+		g_strfreev(tmp);
 		purple_request_cpar_unref(cpar);
 		return info->ui_handle;
 	}
@@ -1841,6 +1906,9 @@ purple_request_action_varg(void *handle,
 
 	if (ops != NULL && ops->request_action != NULL) {
 		PurpleRequestInfo *info;
+		gchar **tmp;
+
+		tmp = purple_request_strip_html(cpar, &primary, &secondary);
 
 		info            = g_new0(PurpleRequestInfo, 1);
 		info->type      = PURPLE_REQUEST_ACTION;
@@ -1850,6 +1918,7 @@ purple_request_action_varg(void *handle,
 
 		handles = g_list_append(handles, info);
 
+		g_strfreev(tmp);
 		purple_request_cpar_unref(cpar);
 		return info->ui_handle;
 	}
@@ -1866,10 +1935,14 @@ purple_request_fields(void *handle, cons
 {
 	PurpleRequestUiOps *ops;
 
-	if (G_UNLIKELY(fields != NULL || ok_text != NULL || ok_cb != NULL ||
-		cancel_text != NULL))
+	if (G_UNLIKELY(fields == NULL || ok_text == NULL || ok_cb == NULL ||
+		cancel_text == NULL))
 	{
 		purple_request_cpar_unref(cpar);
+		g_warn_if_fail(fields != NULL);
+		g_warn_if_fail(ok_text != NULL);
+		g_warn_if_fail(ok_cb != NULL);
+		g_warn_if_fail(cancel_text != NULL);
 		g_return_val_if_reached(NULL);
 	}
 
@@ -1877,6 +1950,9 @@ purple_request_fields(void *handle, cons
 
 	if (ops != NULL && ops->request_fields != NULL) {
 		PurpleRequestInfo *info;
+		gchar **tmp;
+
+		tmp = purple_request_strip_html(cpar, &primary, &secondary);
 
 		info            = g_new0(PurpleRequestInfo, 1);
 		info->type      = PURPLE_REQUEST_FIELDS;
@@ -1887,6 +1963,7 @@ purple_request_fields(void *handle, cons
 
 		handles = g_list_append(handles, info);
 
+		g_strfreev(tmp);
 		purple_request_cpar_unref(cpar);
 		return info->ui_handle;
 	}
diff --git a/libpurple/request.h b/libpurple/request.h
--- a/libpurple/request.h
+++ b/libpurple/request.h
@@ -88,11 +88,18 @@ typedef enum
 
 } PurpleRequestFieldType;
 
+typedef enum
+{
+	PURPLE_REQUEST_FEATURE_HTML = 0x00000001
+} PurpleRequestFeature;
+
 /**
  * Request UI operations.
  */
 typedef struct
 {
+	PurpleRequestFeature features;
+
 	/** @see purple_request_input(). */
 	void *(*request_input)(const char *title, const char *primary,
 		const char *secondary, const char *default_value,
@@ -278,6 +285,28 @@ gconstpointer
 purple_request_cpar_get_custom_icon(PurpleRequestCommonParameters *cpar,
 	gsize *icon_size);
 
+/**
+ * Switches the request text to be HTML or not.
+ *
+ * @param cpar    The parameters set.
+ * @param enabled 1, if the text passed with the request contains HTML,
+ *                0 otherwise. Don't use any other values, as they may be
+ *                redefined in the future.
+ */
+void
+purple_request_cpar_set_html(PurpleRequestCommonParameters *cpar,
+	gboolean enabled);
+
+/**
+ * Checks, if the text passed to the request is HTML.
+ *
+ * @param cpar The parameters set (may be @c NULL).
+ *
+ * @return @c TRUE, if the text is HTML, @c FALSE otherwise.
+ */
+gboolean
+purple_request_cpar_is_html(PurpleRequestCommonParameters *cpar);
+
 /*@}*/
 
 /**************************************************************************/
diff --git a/pidgin/gtkrequest.c b/pidgin/gtkrequest.c
--- a/pidgin/gtkrequest.c
+++ b/pidgin/gtkrequest.c
@@ -309,6 +309,28 @@ text_to_stock(const char *text)
 	return text;
 }
 
+static gchar *
+pidgin_request_escape(PurpleRequestCommonParameters *cpar, const gchar *text)
+{
+	if (text == NULL)
+		return NULL;
+
+	if (purple_request_cpar_is_html(cpar)) {
+		gboolean valid;
+
+		valid = pango_parse_markup(text, -1, 0, NULL, NULL, NULL, NULL);
+
+		if (valid)
+			return g_strdup(text);
+		else {
+			purple_debug_error("pidgin", "Passed label text is not "
+				"a valid markup. Falling back to plain text.");
+		}
+	}
+
+	return g_markup_escape_text(text, -1);
+}
+
 static void *
 pidgin_request_input(const char *title, const char *primary,
 					   const char *secondary, const char *default_value,
@@ -378,8 +400,8 @@ pidgin_request_input(const char *title, 
 	pidgin_widget_decorate_account(hbox, purple_request_cpar_get_account(cpar));
 



More information about the Commits mailing list