]> git.0d.be Git - empathy.git/commitdiff
Create API for generic string parser
authorXavier Claessens <xclaesse@gmail.com>
Sun, 1 Nov 2009 10:04:00 +0000 (11:04 +0100)
committerXavier Claessens <xclaesse@gmail.com>
Tue, 24 Nov 2009 17:29:44 +0000 (18:29 +0100)
libempathy-gtk/empathy-theme-adium.c
libempathy-gtk/empathy-ui-utils.c
libempathy-gtk/empathy-ui-utils.h

index a83b8da861cfaa5032fe40afb60cda3d3309b8ce..014cd87f0ee6c22f1f8eaa86fed5d2924de0227f 100644 (file)
@@ -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);
 }
index 97fd95c1d529a606b4fd6ba6ec9075ace91f0186..376d1f9d896807c5b6eb0b15d87435122283d7c6 100644 (file)
@@ -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);
+       }
+}
+
index 7bec0884e9e9f46990e5c3d6f875d1af2046a6c3..58960c305339cd18dd853db46436c86162c12646 100644 (file)
@@ -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__ */