[empathy] Make possible to define different replace function for parsers



commit ce6f2983d99033c0144a622295ae72ec567e2582
Author: Xavier Claessens <xclaesse gmail com>
Date:   Tue Nov 24 14:29:45 2009 +0100

    Make possible to define different replace function for parsers

 libempathy-gtk/empathy-theme-adium.c |   96 ++++++++++++++++------------------
 libempathy-gtk/empathy-ui-utils.c    |   79 ++++++++++++++++++++--------
 libempathy-gtk/empathy-ui-utils.h    |   46 ++++++++++++----
 3 files changed, 135 insertions(+), 86 deletions(-)
---
diff --git a/libempathy-gtk/empathy-theme-adium.c b/libempathy-gtk/empathy-theme-adium.c
index 78a218a..e0aff10 100644
--- a/libempathy-gtk/empathy-theme-adium.c
+++ b/libempathy-gtk/empathy-theme-adium.c
@@ -192,10 +192,11 @@ theme_adium_open_address_cb (GtkMenuItem *menuitem,
 }
 
 static void
-theme_adium_parser_newline (GString *string,
-			    const gchar *text,
-			    gssize len,
-			    gpointer user_data)
+theme_adium_match_newline (GString *string,
+			   const gchar *text,
+			   gssize len,
+			   EmpathyStringReplace replace_func,
+			   EmpathyStringParser *sub_parsers)
 {
 	gint i;
 	gint prev = 0;
@@ -208,69 +209,61 @@ theme_adium_parser_newline (GString *string,
 	for (i = 0; i < len && text[i] != '\0'; i++) {
 		if (text[i] == '\n') {
 			empathy_string_parser_substr (string, text + prev,
-						      i - prev, user_data);
+						      i - prev, sub_parsers);
 			g_string_append (string, "<br/>");
 			prev = i + 1;
 		}
 	}
-	empathy_string_parser_substr (string, text + prev, i - prev, user_data);
+	empathy_string_parser_substr (string, text + prev, i - prev, sub_parsers);
 }
 
-static gboolean use_smileys = FALSE;
-
 static void
-theme_adium_parser_smiley (GString *string,
-			   const gchar *text,
-			   gssize len,
-			   gpointer user_data)
+theme_adium_replace_link (GString *string,
+			  const gchar *text,
+			  gssize len,
+			  gpointer user_data)
 {
-	guint last = 0;
+	gchar *real_url;
 
-	if (use_smileys) {
-		EmpathySmileyManager *smiley_manager;
-		GSList *hits, *l;
-
-		smiley_manager = empathy_smiley_manager_dup_singleton ();
-		hits = empathy_smiley_manager_parse_len (smiley_manager, text, len);
+	/* Append the link inside <a href=""></a> tag */
+	real_url = empathy_make_absolute_url_len (text, len);
 
-		for (l = hits; l; l = l->next) {
-			EmpathySmileyHit *hit = l->data;
+	g_string_append_printf (string, "<a href=\"%s\">", real_url);
+	g_string_append_len (string, text, len);
+	g_string_append (string, "</a>");
 
-			if (hit->start > last) {
-				/* Append the text between last smiley (or the
-				 * start of the message) and this smiley */
-				empathy_string_parser_substr (string, text + last,
-							      hit->start - last,
-							      user_data);
-			}
+	g_free (real_url);
+}
 
-			/* Replace smileys by a <img/> tag */
-			g_string_append (string, "<abbr title=\"");
-			g_string_append_len (string, text + hit->start,
-					     hit->end - hit->start);
-			g_string_append_printf (string, "\"><img src=\"%s\" alt=\"",
-						hit->path);
-			g_string_append_len (string, text + hit->start,
-					     hit->end - hit->start);
-			g_string_append (string, "\"/></abbr>");
+static gboolean use_smileys = FALSE;
 
-			last = hit->end;
+static void
+theme_adium_replace_smiley (GString *string,
+			    const gchar *text,
+			    gssize len,
+			    gpointer user_data)
+{
+	EmpathySmileyHit *hit = user_data;
 
-			empathy_smiley_hit_free (hit);
-		}
-		g_slist_free (hits);
-		g_object_unref (smiley_manager);
+	if (use_smileys) {
+		/* Replace smileys by a <img/> tag */
+		g_string_append (string, "<abbr title=\"");
+		g_string_append_len (string, text, len);
+		g_string_append_printf (string, "\"><img src=\"%s\" alt=\"",
+					hit->path);
+		g_string_append_len (string, text, len);
+		g_string_append (string, "\"/></abbr>");
+	} else {
+		g_string_append_len (string, text, len);
 	}
-
-	empathy_string_parser_substr (string, text + last, len - last, user_data);
 }
 
 static EmpathyStringParser string_parsers[] = {
-	empathy_string_parser_link,
-	theme_adium_parser_smiley,
-	theme_adium_parser_newline,
-	empathy_string_parser_escape,
-	NULL,
+	{empathy_string_match_link, theme_adium_replace_link},
+	{empathy_string_match_smiley, theme_adium_replace_smiley},
+	{theme_adium_match_newline, NULL},
+	{empathy_string_match_escape, NULL},
+	{NULL, NULL}
 };
 
 static gchar *
@@ -278,6 +271,7 @@ theme_adium_parse_body (const gchar *text)
 {
 	GString *string;
 
+	/* Get use_smileys value now to avoid getting it for each match */
 	empathy_conf_get_bool (empathy_conf_get (),
 			       EMPATHY_PREFS_CHAT_SHOW_SMILEYS,
 			       &use_smileys);
diff --git a/libempathy-gtk/empathy-ui-utils.c b/libempathy-gtk/empathy-ui-utils.c
index 1a27f0d..137dd3b 100644
--- a/libempathy-gtk/empathy-ui-utils.c
+++ b/libempathy-gtk/empathy-ui-utils.c
@@ -40,6 +40,7 @@
 
 #include "empathy-ui-utils.h"
 #include "empathy-images.h"
+#include "empathy-smiley-manager.h"
 #include "empathy-conf.h"
 
 #define DEBUG_FLAG EMPATHY_DEBUG_OTHER
@@ -1576,33 +1577,32 @@ empathy_string_parser_substr (GString *string,
 			      gssize len,
 			      EmpathyStringParser *parsers)
 {
-	if (parsers != NULL && parsers[0] != NULL) {
-		parsers[0] (string, text, len, parsers + 1);
+	if (parsers != NULL && parsers[0].match_func != NULL) {
+		parsers[0].match_func (string, text, len,
+			parsers[0].replace_func, parsers + 1);
 	} else {
 		g_string_append_len (string, text, len);
 	}
 }
 
 void
-empathy_string_parser_link (GString *string,
-			    const gchar *text,
-			    gssize len,
-			    gpointer user_data)
+empathy_string_match_link (GString *string,
+			   const gchar *text,
+			   gssize len,
+			   EmpathyStringReplace replace_func,
+			   EmpathyStringParser *sub_parsers)
 {
 	GRegex     *uri_regex;
 	GMatchInfo *match_info;
 	gboolean    match;
 	gint        last = 0;
 
-	/* Add <a href></a> arround links */
 	uri_regex = empathy_uri_regex_dup_singleton ();
 	match = g_regex_match_full (uri_regex, text, len, 0, 0, &match_info, NULL);
 	if (match) {
 		gint s = 0, e = 0;
 
 		do {
-			gchar *real_url;
-
 			g_match_info_fetch_pos (match_info, 0, &s, &e);
 
 			if (s > last) {
@@ -1610,33 +1610,66 @@ empathy_string_parser_link (GString *string,
 				 * start of the message) and this link */
 				empathy_string_parser_substr (string, text + last,
 							      s - last,
-							      user_data);
+							      sub_parsers);
 			}
 
-			/* Append the link inside <a href=""></a> tag */
-			real_url = empathy_make_absolute_url_len (text + s, e - s);
-
-			g_string_append_printf (string, "<a href=\"%s\">",
-				real_url);
-			g_string_append_len (string, text + s, e - s);
-			g_string_append (string, "</a>");
+			replace_func (string, text + s, e - s, NULL);
 
-			g_free (real_url);
 			last = e;
 		} while (g_match_info_next (match_info, NULL));
 	}
 
-	empathy_string_parser_substr (string, text + last, len - last, user_data);
+	empathy_string_parser_substr (string, text + last, len - last, sub_parsers);
 
 	g_match_info_free (match_info);
 	g_regex_unref (uri_regex);
 }
 
 void
-empathy_string_parser_escape (GString *string,
-			      const gchar *text,
-			      gssize len,
-			      gpointer user_data)
+empathy_string_match_smiley (GString *string,
+			     const gchar *text,
+			     gssize len,
+			     EmpathyStringReplace replace_func,
+			     EmpathyStringParser *sub_parsers)
+{
+	guint last = 0;
+	EmpathySmileyManager *smiley_manager;
+	GSList *hits, *l;
+
+	smiley_manager = empathy_smiley_manager_dup_singleton ();
+	hits = empathy_smiley_manager_parse_len (smiley_manager, text, len);
+
+	for (l = hits; l; l = l->next) {
+		EmpathySmileyHit *hit = l->data;
+
+		if (hit->start > last) {
+			/* Append the text between last smiley (or the
+			 * start of the message) and this smiley */
+			empathy_string_parser_substr (string, text + last,
+						      hit->start - last,
+						      sub_parsers);
+		}
+
+		replace_func (string,
+			      text + hit->start, hit->end - hit->start,
+			      hit);
+
+		last = hit->end;
+
+		empathy_smiley_hit_free (hit);
+	}
+	g_slist_free (hits);
+	g_object_unref (smiley_manager);
+
+	empathy_string_parser_substr (string, text + last, len - last, sub_parsers);
+}
+
+void
+empathy_string_match_escape (GString *string,
+			     const gchar *text,
+			     gssize len,
+			     EmpathyStringReplace replace_func,
+			     EmpathyStringParser *sub_parsers)
 {
 	gchar *escaped;
 
diff --git a/libempathy-gtk/empathy-ui-utils.h b/libempathy-gtk/empathy-ui-utils.h
index 80332c9..f125b06 100644
--- a/libempathy-gtk/empathy-ui-utils.h
+++ b/libempathy-gtk/empathy-ui-utils.h
@@ -118,10 +118,23 @@ gchar *     empathy_make_absolute_url_len               (const gchar *url,
 							 guint len);
 
 /* String parser */
-typedef void (*EmpathyStringParser) (GString *string,
-				     const gchar *text,
-				     gssize len,
-				     gpointer user_data);
+
+typedef struct _EmpathyStringParser EmpathyStringParser;
+
+typedef void (*EmpathyStringReplace) (GString *string,
+				      const gchar *text,
+				      gssize len,
+				      gpointer user_data);
+typedef void (*EmpathyStringMatch) (GString *string,
+				    const gchar *text,
+				    gssize len,
+				    EmpathyStringReplace replace_func,
+				    EmpathyStringParser *sub_parsers);
+
+struct _EmpathyStringParser {
+	EmpathyStringMatch match_func;
+	EmpathyStringReplace replace_func;
+};
 
 void
 empathy_string_parser_substr (GString *string,
@@ -130,16 +143,25 @@ empathy_string_parser_substr (GString *string,
 			      EmpathyStringParser *parsers);
 
 void
-empathy_string_parser_link (GString *string,
-			    const gchar *text,
-			    gssize len,
-			    gpointer user_data);
+empathy_string_match_link (GString *string,
+			   const gchar *text,
+			   gssize len,
+			   EmpathyStringReplace replace_func,
+			   EmpathyStringParser *sub_parsers);
 
 void
-empathy_string_parser_escape (GString *string,
-			      const gchar *text,
-			      gssize len,
-			      gpointer user_data);
+empathy_string_match_smiley (GString *string,
+			     const gchar *text,
+			     gssize len,
+			     EmpathyStringReplace replace_func,
+			     EmpathyStringParser *sub_parsers);
+
+void
+empathy_string_match_escape (GString *string,
+			     const gchar *text,
+			     gssize len,
+			     EmpathyStringReplace replace_func,
+			     EmpathyStringParser *sub_parsers);
 
 G_END_DECLS
 



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]