]> git.0d.be Git - empathy.git/blob - libempathy-gtk/empathy-webkit-utils.c
Abstract WebKit string parsers into empathy-webkit-utils
[empathy.git] / libempathy-gtk / empathy-webkit-utils.c
1 /*
2  * Copyright (C) 2008-2009 Collabora Ltd.
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2.1 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library; if not, write to the Free Software
16  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
17  *
18  * Authors: Xavier Claessens <xclaesse@gmail.com>
19  */
20
21 #include "empathy-webkit-utils.h"
22 #include "empathy-smiley-manager.h"
23
24 static void
25 empathy_webkit_match_newline (const gchar *text,
26     gssize len,
27     EmpathyStringReplace replace_func,
28     EmpathyStringParser *sub_parsers,
29     gpointer user_data)
30 {
31   GString *string = user_data;
32   gint i;
33   gint prev = 0;
34
35   if (len < 0)
36     len = G_MAXSSIZE;
37
38   /* Replace \n by <br/> */
39   for (i = 0; i < len && text[i] != '\0'; i++)
40     {
41       if (text[i] == '\n')
42         {
43           empathy_string_parser_substr (text + prev, i - prev,
44               sub_parsers, user_data);
45           g_string_append (string, "<br/>");
46           prev = i + 1;
47         }
48     }
49
50   empathy_string_parser_substr (text + prev, i - prev,
51               sub_parsers, user_data);
52 }
53
54 static void
55 empathy_webkit_replace_smiley (const gchar *text,
56     gssize len,
57     gpointer match_data,
58     gpointer user_data)
59 {
60   EmpathySmileyHit *hit = match_data;
61   GString *string = user_data;
62
63   /* Replace smiley by a <img/> tag */
64   g_string_append_printf (string,
65       "<img src=\"%s\" alt=\"%.*s\" title=\"%.*s\"/>",
66       hit->path, (int)len, text, (int)len, text);
67 }
68
69 static EmpathyStringParser string_parsers[] = {
70   { empathy_string_match_link, empathy_string_replace_link },
71   { empathy_webkit_match_newline, NULL },
72   { empathy_string_match_all, empathy_string_replace_escaped },
73   { NULL, NULL}
74 };
75
76 static EmpathyStringParser string_parsers_with_smiley[] = {
77   { empathy_string_match_link, empathy_string_replace_link },
78   { empathy_string_match_smiley, empathy_webkit_replace_smiley },
79   { empathy_webkit_match_newline, NULL },
80   { empathy_string_match_all, empathy_string_replace_escaped },
81   { NULL, NULL }
82 };
83
84 EmpathyStringParser *
85 empathy_webkit_get_string_parser (gboolean smileys)
86 {
87   if (smileys)
88     return string_parsers_with_smiley;
89   else
90     return string_parsers;
91 }