2 * Copyright (C) 2010 Collabora Ltd.
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.
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.
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
18 * Authors: Xavier Claessens <xclaesse@gmail.com>
25 #include "empathy-string-parser.h"
26 #include "empathy-smiley-manager.h"
27 #include "empathy-ui-utils.h"
29 #define SCHEMES "([a-zA-Z\\+]+)"
30 #define INVALID_CHARS "\\s\"'"
31 #define INVALID_CHARS_EXT INVALID_CHARS "\\[\\]<>(){},;:?"
32 #define BODY "([^"INVALID_CHARS"]+)"
33 #define BODY_END "([^"INVALID_CHARS"]*)[^"INVALID_CHARS_EXT".]"
34 #define BODY_STRICT "([^"INVALID_CHARS_EXT"]+)"
35 #define URI_REGEX "("SCHEMES"://"BODY_END")" \
36 "|((www|ftp)\\."BODY_END")" \
37 "|((mailto:)?"BODY_STRICT"@"BODY"\\."BODY_END")"
40 uri_regex_dup_singleton (void)
42 static GRegex
*uri_regex
= NULL
;
44 /* We intentionally leak the regex so it's not recomputed */
46 uri_regex
= g_regex_new (URI_REGEX
, 0, 0, NULL
);
49 return g_regex_ref (uri_regex
);
53 empathy_string_parser_substr (const gchar
*text
,
55 EmpathyStringParser
*parsers
,
58 if (parsers
!= NULL
&& parsers
[0].match_func
!= NULL
) {
59 parsers
[0].match_func (text
, len
,
60 parsers
[0].replace_func
, parsers
+ 1,
66 empathy_string_match_link (const gchar
*text
,
68 EmpathyStringReplace replace_func
,
69 EmpathyStringParser
*sub_parsers
,
73 GMatchInfo
*match_info
;
77 uri_regex
= uri_regex_dup_singleton ();
78 match
= g_regex_match_full (uri_regex
, text
, len
, 0, 0, &match_info
, NULL
);
83 g_match_info_fetch_pos (match_info
, 0, &s
, &e
);
86 /* Append the text between last link (or the
87 * start of the message) and this link */
88 empathy_string_parser_substr (text
+ last
,
94 replace_func (text
+ s
, e
- s
, NULL
, user_data
);
97 } while (g_match_info_next (match_info
, NULL
));
100 empathy_string_parser_substr (text
+ last
, len
- last
,
101 sub_parsers
, user_data
);
103 g_match_info_free (match_info
);
104 g_regex_unref (uri_regex
);
108 empathy_string_match_smiley (const gchar
*text
,
110 EmpathyStringReplace replace_func
,
111 EmpathyStringParser
*sub_parsers
,
115 EmpathySmileyManager
*smiley_manager
;
118 smiley_manager
= empathy_smiley_manager_dup_singleton ();
119 hits
= empathy_smiley_manager_parse_len (smiley_manager
, text
, len
);
121 for (l
= hits
; l
; l
= l
->next
) {
122 EmpathySmileyHit
*hit
= l
->data
;
124 if (hit
->start
> last
) {
125 /* Append the text between last smiley (or the
126 * start of the message) and this smiley */
127 empathy_string_parser_substr (text
+ last
,
129 sub_parsers
, user_data
);
132 replace_func (text
+ hit
->start
, hit
->end
- hit
->start
,
137 empathy_smiley_hit_free (hit
);
140 g_object_unref (smiley_manager
);
142 empathy_string_parser_substr (text
+ last
, len
- last
,
143 sub_parsers
, user_data
);
147 empathy_string_match_all (const gchar
*text
,
149 EmpathyStringReplace replace_func
,
150 EmpathyStringParser
*sub_parsers
,
153 replace_func (text
, len
, NULL
, user_data
);
157 empathy_string_replace_link (const gchar
*text
,
162 GString
*string
= user_data
;
166 real_url
= empathy_make_absolute_url_len (text
, len
);
168 /* The thing we are making a link of may contain
169 * characters which need escaping */
170 escaped
= g_markup_escape_text (text
, len
);
172 /* Append the link inside <a href=""></a> tag */
173 g_string_append_printf (string
, "<a href=\"%s\">%s</a>",
181 empathy_string_replace_escaped (const gchar
*text
,
186 GString
*string
= user_data
;
189 gssize escaped_len
, old_len
;
191 escaped
= g_markup_escape_text (text
, len
);
192 escaped_len
= strlen (escaped
);
194 /* Allocate more space to string (we really need a g_string_extend...) */
195 old_len
= string
->len
;
196 g_string_set_size (string
, old_len
+ escaped_len
);
197 g_string_truncate (string
, old_len
);
200 for (i
= 0; i
< escaped_len
; i
++) {
201 if (escaped
[i
] != '\r')
202 g_string_append_c (string
, escaped
[i
]);
209 empathy_add_link_markup (const gchar
*text
)
211 EmpathyStringParser parsers
[] = {
212 {empathy_string_match_link
, empathy_string_replace_link
},
213 {empathy_string_match_all
, empathy_string_replace_escaped
},
218 g_return_val_if_fail (text
!= NULL
, NULL
);
220 /* GtkLabel with links could make infinite loop because of
221 * GNOME bug #612066. It is fixed in GTK >= 2.18.8 and GTK >= 2.19.7.
222 * FIXME: Remove this check once we have an hard dep on GTK 2.20 */
223 if (gtk_check_version (2, 18, 8) != NULL
||
224 (gtk_minor_version
== 19 && gtk_micro_version
< 7)) {
225 return g_markup_escape_text (text
, -1);
228 string
= g_string_sized_new (strlen (text
));
229 empathy_string_parser_substr (text
, -1, parsers
, string
);
231 return g_string_free (string
, FALSE
);