changeset 19334:fda2d2d99850

Add support to parse and add XHTML in a textview. This we can use for logs, in the conversation window etc. places.
author Sadrul Habib Chowdhury <imadil@gmail.com>
date Thu, 23 Aug 2007 05:55:57 +0000
parents 5e39506a0e3b
children 9dc79e0a8d6c
files finch/libgnt/gntutils.c finch/libgnt/gntutils.h finch/libgnt/test/tv.c
diffstat 3 files changed, 90 insertions(+), 1 deletions(-) [+]
line wrap: on
line diff
--- a/finch/libgnt/gntutils.c	Sat Aug 18 05:57:52 2007 +0000
+++ b/finch/libgnt/gntutils.c	Thu Aug 23 05:55:57 2007 +0000
@@ -376,6 +376,82 @@
 #endif
 }
 
+#ifndef NO_LIBXML
+static void
+util_parse_html_to_tv(xmlNode *node, GntTextView *tv, GntTextFormatFlags flag)
+{
+	const char *name;
+	char *content;
+	xmlNode *ch;
+	gboolean processed = FALSE;
+	char *url = NULL;
+
+	if (node == NULL || node->name == NULL || node->type != XML_ELEMENT_NODE)
+		return;
+
+	name = (char*)node->name;
+	if (g_ascii_strcasecmp(name, "b") == 0 ||
+		g_ascii_strcasecmp(name, "strong") == 0 ||
+		g_ascii_strcasecmp(name, "i") == 0 ||
+		g_ascii_strcasecmp(name, "blockquote") == 0) {
+		flag |= GNT_TEXT_FLAG_BOLD;
+	} else if (g_ascii_strcasecmp(name, "u") == 0) {
+		flag |= GNT_TEXT_FLAG_UNDERLINE;
+	} else if (g_ascii_strcasecmp(name, "br") == 0) {
+		gnt_text_view_append_text_with_flags(tv, "\n", flag);
+	} else if (g_ascii_strcasecmp(name, "a") == 0) {
+		flag |= GNT_TEXT_FLAG_UNDERLINE;
+		url = (char *)xmlGetProp(node, (xmlChar*)"href");
+	} else {
+		/* XXX: Process other possible tags */
+	}
+
+	for (ch = node->children; ch; ch = ch->next) {
+		if (ch->type == XML_ELEMENT_NODE) {
+			processed = TRUE;
+		}
+		util_parse_html_to_tv(ch, tv, flag);
+	}
+
+	if (!processed) {
+		content = (char*)xmlNodeGetContent(node);
+		gnt_text_view_append_text_with_flags(tv, content, flag);
+		xmlFree(content);
+	}
+
+	if (url) {
+		char *href = g_strdup_printf(" (%s)", url);
+		gnt_text_view_append_text_with_flags(tv, href, flag);
+		g_free(href);
+		xmlFree(url);
+	}
+}
+#endif
+
+gboolean gnt_util_parse_xhtml_to_textview(const char *string, GntTextView *tv)
+{
+#ifdef NO_LIBXML
+	return FALSE;
+#else
+	xmlParserCtxtPtr ctxt;
+	xmlDocPtr doc;
+	xmlNodePtr node;
+	GntTextFormatFlags flag = GNT_TEXT_FLAG_NORMAL;
+	gboolean ret = FALSE;
+
+	ctxt = xmlNewParserCtxt();
+	doc = xmlCtxtReadDoc(ctxt, (xmlChar*)string, NULL, NULL, XML_PARSE_NOBLANKS | XML_PARSE_RECOVER);
+	if (doc) {
+		node = xmlDocGetRootElement(doc);
+		util_parse_html_to_tv(node, tv, flag);
+		xmlFreeDoc(doc);
+		ret = TRUE;
+	}
+	xmlCleanupParser();
+	return ret;
+#endif
+}
+
 /* Setup trigger widget */
 typedef struct {
 	char *text;
@@ -408,4 +484,3 @@
 	g_signal_connect(G_OBJECT(wid), "key_pressed", G_CALLBACK(key_pressed), tb);
 	g_signal_connect_swapped(G_OBJECT(button), "destroy", G_CALLBACK(free_trigger_button), tb);
 }
-
--- a/finch/libgnt/gntutils.h	Sat Aug 18 05:57:52 2007 +0000
+++ b/finch/libgnt/gntutils.h	Thu Aug 23 05:55:57 2007 +0000
@@ -27,6 +27,7 @@
 #include <glib.h>
 
 #include "gnt.h"
+#include "gnttextview.h"
 #include "gntwidget.h"
 
 typedef gpointer (*GDupFunc)(gconstpointer data);
@@ -132,6 +133,16 @@
 void gnt_util_parse_widgets(const char *string, int num, ...);
 
 /**
+ * Parse an XHTML string and add it in a GntTextView with
+ * appropriate text flags.
+ *
+ * @param string   The XHTML string
+ * @param tv       The GntTextView
+ * @return  @c TRUE if the string was added to the textview properly, @c FALSE otherwise.
+ */
+gboolean gnt_util_parse_xhtml_to_textview(const char *string, GntTextView *tv);
+
+/**
  * Make some keypress activate a button when some key is pressed with 'wid' in focus.
  *
  * @param widget  The widget
--- a/finch/libgnt/test/tv.c	Sat Aug 18 05:57:52 2007 +0000
+++ b/finch/libgnt/test/tv.c	Thu Aug 23 05:55:57 2007 +0000
@@ -5,6 +5,7 @@
 #include "gntbox.h"
 #include "gntentry.h"
 #include "gnttextview.h"
+#include "gntutils.h"
 
 static gboolean
 key_pressed(GntWidget *w, const char *key, GntWidget *view)
@@ -117,6 +118,8 @@
 	gnt_text_view_append_text_with_flags(GNT_TEXT_VIEW(view), "plugins: ", GNT_TEXT_FLAG_BOLD);
 	gnt_text_view_append_text_with_flags(GNT_TEXT_VIEW(view), "this is the 4th line\n", GNT_TEXT_FLAG_NORMAL);
 
+	gnt_util_parse_xhtml_to_textview("<p><b>Ohoy hoy!!</b><br/><p>I think this is going to</p> <u> WORK!!! </u><a href='www.google.com'>check this out!!</a></p>", GNT_TEXT_VIEW(view));
+
 #ifdef STANDALONE
 	gnt_main();