]> git.0d.be Git - empathy.git/blob - libempathy-gtk/empathy-string-parser.c
local-xmpp-assistant-widget: increase row-spacing
[empathy.git] / libempathy-gtk / empathy-string-parser.c
1 /*
2  * Copyright (C) 2010 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 <config.h>
22
23 #include <string.h>
24
25 #include "empathy-string-parser.h"
26 #include "empathy-smiley-manager.h"
27 #include "empathy-ui-utils.h"
28
29 #define SCHEMES           "([a-zA-Z\\+]+)"
30 #define INVALID_CHARS     "\\s\"<>"
31 #define INVALID_CHARS_EXT INVALID_CHARS "\\[\\](){},;:"
32 #define INVALID_CHARS_FULL INVALID_CHARS_EXT "?'"
33 #define BODY              "([^"INVALID_CHARS_FULL"])([^"INVALID_CHARS_EXT"]*)"
34 #define BODY_END          "([^"INVALID_CHARS"]*)[^"INVALID_CHARS_FULL".]"
35 #define URI_REGEX         "("SCHEMES"://"BODY_END")" \
36                           "|((www|ftp)\\."BODY_END")" \
37                           "|((mailto:)?"BODY"@"BODY"\\."BODY_END")"
38
39 static GRegex *
40 uri_regex_dup_singleton (void)
41 {
42         static GRegex *uri_regex = NULL;
43
44         /* We intentionally leak the regex so it's not recomputed */
45         if (!uri_regex) {
46                 GError *error = NULL;
47
48                 uri_regex = g_regex_new (URI_REGEX, 0, 0, &error);
49                 if (uri_regex == NULL) {
50                         g_warning ("Failed to create reg exp: %s", error->message);
51                         g_error_free (error);
52                         return NULL;
53                 }
54         }
55
56         return g_regex_ref (uri_regex);
57 }
58
59 void
60 empathy_string_parser_substr (const gchar *text,
61                               gssize len,
62                               EmpathyStringParser *parsers,
63                               gpointer user_data)
64 {
65         if (parsers != NULL && parsers[0].match_func != NULL) {
66                 parsers[0].match_func (text, len,
67                                        parsers[0].replace_func, parsers + 1,
68                                        user_data);
69         }
70 }
71
72 void
73 empathy_string_match_link (const gchar *text,
74                            gssize len,
75                            EmpathyStringReplace replace_func,
76                            EmpathyStringParser *sub_parsers,
77                            gpointer user_data)
78 {
79         GRegex     *uri_regex;
80         GMatchInfo *match_info;
81         gboolean    match;
82         gint        last = 0;
83
84         uri_regex = uri_regex_dup_singleton ();
85         if (uri_regex == NULL) {
86                 empathy_string_parser_substr (text, len, sub_parsers, user_data);
87                 return;
88         }
89
90         match = g_regex_match_full (uri_regex, text, len, 0, 0, &match_info, NULL);
91         if (match) {
92                 gint s = 0, e = 0;
93
94                 do {
95                         g_match_info_fetch_pos (match_info, 0, &s, &e);
96
97                         if (s > last) {
98                                 /* Append the text between last link (or the
99                                  * start of the message) and this link */
100                                 empathy_string_parser_substr (text + last,
101                                                               s - last,
102                                                               sub_parsers,
103                                                               user_data);
104                         }
105
106                         replace_func (text + s, e - s, NULL, user_data);
107
108                         last = e;
109                 } while (g_match_info_next (match_info, NULL));
110         }
111
112         empathy_string_parser_substr (text + last, len - last,
113                                       sub_parsers, user_data);
114
115         g_match_info_free (match_info);
116         g_regex_unref (uri_regex);
117 }
118
119 void
120 empathy_string_match_smiley (const gchar *text,
121                              gssize len,
122                              EmpathyStringReplace replace_func,
123                              EmpathyStringParser *sub_parsers,
124                              gpointer user_data)
125 {
126         guint last = 0;
127         EmpathySmileyManager *smiley_manager;
128         GSList *hits, *l;
129
130         smiley_manager = empathy_smiley_manager_dup_singleton ();
131         hits = empathy_smiley_manager_parse_len (smiley_manager, text, len);
132
133         for (l = hits; l; l = l->next) {
134                 EmpathySmileyHit *hit = l->data;
135
136                 if (hit->start > last) {
137                         /* Append the text between last smiley (or the
138                          * start of the message) and this smiley */
139                         empathy_string_parser_substr (text + last,
140                                                       hit->start - last,
141                                                       sub_parsers, user_data);
142                 }
143
144                 replace_func (text + hit->start, hit->end - hit->start,
145                               hit, user_data);
146
147                 last = hit->end;
148
149                 empathy_smiley_hit_free (hit);
150         }
151         g_slist_free (hits);
152         g_object_unref (smiley_manager);
153
154         empathy_string_parser_substr (text + last, len - last,
155                                       sub_parsers, user_data);
156 }
157
158 void
159 empathy_string_match_all (const gchar *text,
160                           gssize len,
161                           EmpathyStringReplace replace_func,
162                           EmpathyStringParser *sub_parsers,
163                           gpointer user_data)
164 {
165         replace_func (text, len, NULL, user_data);
166 }
167
168 void
169 empathy_string_replace_link (const gchar *text,
170                              gssize len,
171                              gpointer match_data,
172                              gpointer user_data)
173 {
174         GString *string = user_data;
175         gchar *real_url;
176         gchar *title;
177         gchar *markup;
178
179         real_url = empathy_make_absolute_url_len (text, len);
180
181         /* Need to copy manually, because g_markup_printf_escaped does not work
182          * with string precision pitfalls. */
183         title = g_strndup (text, len);
184
185         /* Append the link inside <a href=""></a> tag */
186         markup = g_markup_printf_escaped ("<a href=\"%s\">%s</a>",
187                         real_url, title);
188
189         g_string_append (string, markup);
190
191         g_free (real_url);
192         g_free (title);
193         g_free (markup);
194 }
195
196 void
197 empathy_string_replace_escaped (const gchar *text,
198                                 gssize len,
199                                 gpointer match_data,
200                                 gpointer user_data)
201 {
202         GString *string = user_data;
203         gchar *escaped;
204         guint i;
205         gsize escaped_len, old_len;
206
207         escaped = g_markup_escape_text (text, len);
208         escaped_len = strlen (escaped);
209
210         /* Allocate more space to string (we really need a g_string_extend...) */
211         old_len = string->len;
212         g_string_set_size (string, old_len + escaped_len);
213         g_string_truncate (string, old_len);
214
215         /* Remove '\r' */
216         for (i = 0; i < escaped_len; i++) {
217                 if (escaped[i] != '\r')
218                         g_string_append_c (string, escaped[i]);
219         }
220
221         g_free (escaped);
222 }
223
224 gchar *
225 empathy_add_link_markup (const gchar *text)
226 {
227         EmpathyStringParser parsers[] = {
228                 {empathy_string_match_link, empathy_string_replace_link},
229                 {empathy_string_match_all, empathy_string_replace_escaped},
230                 {NULL, NULL}
231         };
232         GString *string;
233
234         g_return_val_if_fail (text != NULL, NULL);
235
236         string = g_string_sized_new (strlen (text));
237         empathy_string_parser_substr (text, -1, parsers, string);
238
239         return g_string_free (string, FALSE);
240 }
241