aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorXavier Claessens <xclaesse@gmail.com>2009-11-01 18:04:00 +0800
committerXavier Claessens <xclaesse@gmail.com>2009-11-25 01:29:44 +0800
commit6ea436c8b313974d94eb9fe97974edcf4f47e46d (patch)
treea3712e0599afc03b44fa49d4b0614278b3f68257
parentb15f926089b6b718004467c8bb2ddf6f6cd3a9ac (diff)
downloadgsoc2013-empathy-6ea436c8b313974d94eb9fe97974edcf4f47e46d.tar
gsoc2013-empathy-6ea436c8b313974d94eb9fe97974edcf4f47e46d.tar.gz
gsoc2013-empathy-6ea436c8b313974d94eb9fe97974edcf4f47e46d.tar.bz2
gsoc2013-empathy-6ea436c8b313974d94eb9fe97974edcf4f47e46d.tar.lz
gsoc2013-empathy-6ea436c8b313974d94eb9fe97974edcf4f47e46d.tar.xz
gsoc2013-empathy-6ea436c8b313974d94eb9fe97974edcf4f47e46d.tar.zst
gsoc2013-empathy-6ea436c8b313974d94eb9fe97974edcf4f47e46d.zip
Create API for generic string parser
-rw-r--r--libempathy-gtk/empathy-theme-adium.c41
-rw-r--r--libempathy-gtk/empathy-ui-utils.c14
-rw-r--r--libempathy-gtk/empathy-ui-utils.h12
3 files changed, 54 insertions, 13 deletions
diff --git a/libempathy-gtk/empathy-theme-adium.c b/libempathy-gtk/empathy-theme-adium.c
index a83b8da86..014cd87f0 100644
--- a/libempathy-gtk/empathy-theme-adium.c
+++ b/libempathy-gtk/empathy-theme-adium.c
@@ -194,7 +194,8 @@ theme_adium_open_address_cb (GtkMenuItem *menuitem,
static void
theme_adium_parser_escape (GString *string,
const gchar *text,
- gssize len)
+ gssize len,
+ gpointer user_data)
{
gchar *escaped;
@@ -206,7 +207,8 @@ theme_adium_parser_escape (GString *string,
static void
theme_adium_parser_newline (GString *string,
const gchar *text,
- gssize len)
+ gssize len,
+ gpointer user_data)
{
gint i;
gint prev = 0;
@@ -218,18 +220,20 @@ theme_adium_parser_newline (GString *string,
/* Replace \n by <br/> */
for (i = 0; i < len && text[i] != '\0'; i++) {
if (text[i] == '\n') {
- theme_adium_parser_escape (string, text + prev, i - prev);
+ empathy_string_parser_substr (string, text + prev,
+ i - prev, user_data);
g_string_append (string, "<br/>");
prev = i + 1;
}
}
- theme_adium_parser_escape (string, text + prev, i - prev);
+ empathy_string_parser_substr (string, text + prev, i - prev, user_data);
}
static void
theme_adium_parser_smiley (GString *string,
const gchar *text,
- gssize len)
+ gssize len,
+ gpointer user_data)
{
gboolean use_smileys = FALSE;
gint last = 0;
@@ -251,8 +255,9 @@ theme_adium_parser_smiley (GString *string,
if (hit->start > last) {
/* Append the text between last smiley (or the
* start of the message) and this smiley */
- theme_adium_parser_newline (string, text + last,
- hit->start - last);
+ empathy_string_parser_substr (string, text + last,
+ hit->start - last,
+ user_data);
}
/* Replace smileys by a <img/> tag */
@@ -273,13 +278,14 @@ theme_adium_parser_smiley (GString *string,
g_object_unref (smiley_manager);
}
- theme_adium_parser_newline (string, text + last, len - last);
+ empathy_string_parser_substr (string, text + last, len - last, user_data);
}
static void
theme_adium_parser_url (GString *string,
const gchar *text,
- gssize len)
+ gssize len,
+ gpointer user_data)
{
GRegex *uri_regex;
GMatchInfo *match_info;
@@ -300,8 +306,9 @@ theme_adium_parser_url (GString *string,
if (s > last) {
/* Append the text between last link (or the
* start of the message) and this link */
- theme_adium_parser_smiley (string, text + last,
- s - last);
+ empathy_string_parser_substr (string, text + last,
+ s - last,
+ user_data);
}
/* Append the link inside <a href=""></a> tag */
@@ -317,12 +324,20 @@ theme_adium_parser_url (GString *string,
} while (g_match_info_next (match_info, NULL));
}
- theme_adium_parser_smiley (string, text + last, len - last);
+ empathy_string_parser_substr (string, text + last, len - last, user_data);
g_match_info_free (match_info);
g_regex_unref (uri_regex);
}
+static EmpathyStringParser string_parsers[] = {
+ theme_adium_parser_url,
+ theme_adium_parser_smiley,
+ theme_adium_parser_newline,
+ theme_adium_parser_escape,
+ NULL,
+};
+
static gchar *
theme_adium_parse_body (const gchar *text)
{
@@ -343,7 +358,7 @@ theme_adium_parse_body (const gchar *text)
*/
string = g_string_sized_new (strlen (text));
- theme_adium_parser_url (string, text, -1);
+ empathy_string_parser_substr (string, text, -1, string_parsers);
return g_string_free (string, FALSE);
}
diff --git a/libempathy-gtk/empathy-ui-utils.c b/libempathy-gtk/empathy-ui-utils.c
index 97fd95c1d..376d1f9d8 100644
--- a/libempathy-gtk/empathy-ui-utils.c
+++ b/libempathy-gtk/empathy-ui-utils.c
@@ -1569,3 +1569,17 @@ empathy_receive_file_with_file_chooser (EmpathyFTHandler *handler)
gtk_widget_show (widget);
}
+
+void
+empathy_string_parser_substr (GString *string,
+ const gchar *text,
+ gssize len,
+ EmpathyStringParser *parsers)
+{
+ if (parsers != NULL && parsers[0] != NULL) {
+ parsers[0] (string, text, len, parsers + 1);
+ } else {
+ g_string_append_len (string, text, len);
+ }
+}
+
diff --git a/libempathy-gtk/empathy-ui-utils.h b/libempathy-gtk/empathy-ui-utils.h
index 7bec0884e..58960c305 100644
--- a/libempathy-gtk/empathy-ui-utils.h
+++ b/libempathy-gtk/empathy-ui-utils.h
@@ -117,6 +117,18 @@ gchar * empathy_make_absolute_url (const gchar *url);
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);
+
+void
+empathy_string_parser_substr (GString *string,
+ const gchar *text,
+ gssize len,
+ EmpathyStringParser *parsers);
+
G_END_DECLS
#endif /* __EMPATHY_UI_UTILS_H__ */