]> git.0d.be Git - empathy.git/blob - libempathy-gtk/empathy-string-parser.c
Move empathy_uri_regex_dup_singleton to be private inside empathy-string-parser.c
[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
28 #define SCHEMES           "([a-zA-Z\\+]+)"
29 #define INVALID_CHARS     "\\s\"'"
30 #define INVALID_CHARS_EXT INVALID_CHARS "\\[\\]<>(){},;:?"
31 #define BODY              "([^"INVALID_CHARS"]+)"
32 #define BODY_END          "([^"INVALID_CHARS"]*)[^"INVALID_CHARS_EXT".]"
33 #define BODY_STRICT       "([^"INVALID_CHARS_EXT"]+)"
34 #define URI_REGEX         "("SCHEMES"://"BODY_END")" \
35                           "|((www|ftp)\\."BODY_END")" \
36                           "|((mailto:)?"BODY_STRICT"@"BODY"\\."BODY_END")"
37
38 static GRegex *
39 uri_regex_dup_singleton (void)
40 {
41         static GRegex *uri_regex = NULL;
42
43         /* We intentionally leak the regex so it's not recomputed */
44         if (!uri_regex) {
45                 uri_regex = g_regex_new (URI_REGEX, 0, 0, NULL);
46         }
47
48         return g_regex_ref (uri_regex);
49 }
50
51 void
52 empathy_string_parser_substr (const gchar *text,
53                               gssize len,
54                               EmpathyStringParser *parsers,
55                               gpointer user_data)
56 {
57         if (parsers != NULL && parsers[0].match_func != NULL) {
58                 parsers[0].match_func (text, len,
59                                        parsers[0].replace_func, parsers + 1,
60                                        user_data);
61         }
62 }
63
64 void
65 empathy_string_match_link (const gchar *text,
66                            gssize len,
67                            EmpathyStringReplace replace_func,
68                            EmpathyStringParser *sub_parsers,
69                            gpointer user_data)
70 {
71         GRegex     *uri_regex;
72         GMatchInfo *match_info;
73         gboolean    match;
74         gint        last = 0;
75
76         uri_regex = uri_regex_dup_singleton ();
77         match = g_regex_match_full (uri_regex, text, len, 0, 0, &match_info, NULL);
78         if (match) {
79                 gint s = 0, e = 0;
80
81                 do {
82                         g_match_info_fetch_pos (match_info, 0, &s, &e);
83
84                         if (s > last) {
85                                 /* Append the text between last link (or the
86                                  * start of the message) and this link */
87                                 empathy_string_parser_substr (text + last,
88                                                               s - last,
89                                                               sub_parsers,
90                                                               user_data);
91                         }
92
93                         replace_func (text + s, e - s, NULL, user_data);
94
95                         last = e;
96                 } while (g_match_info_next (match_info, NULL));
97         }
98
99         empathy_string_parser_substr (text + last, len - last,
100                                       sub_parsers, user_data);
101
102         g_match_info_free (match_info);
103         g_regex_unref (uri_regex);
104 }
105
106 void
107 empathy_string_match_smiley (const gchar *text,
108                              gssize len,
109                              EmpathyStringReplace replace_func,
110                              EmpathyStringParser *sub_parsers,
111                              gpointer user_data)
112 {
113         guint last = 0;
114         EmpathySmileyManager *smiley_manager;
115         GSList *hits, *l;
116
117         smiley_manager = empathy_smiley_manager_dup_singleton ();
118         hits = empathy_smiley_manager_parse_len (smiley_manager, text, len);
119
120         for (l = hits; l; l = l->next) {
121                 EmpathySmileyHit *hit = l->data;
122
123                 if (hit->start > last) {
124                         /* Append the text between last smiley (or the
125                          * start of the message) and this smiley */
126                         empathy_string_parser_substr (text + last,
127                                                       hit->start - last,
128                                                       sub_parsers, user_data);
129                 }
130
131                 replace_func (text + hit->start, hit->end - hit->start,
132                               hit, user_data);
133
134                 last = hit->end;
135
136                 empathy_smiley_hit_free (hit);
137         }
138         g_slist_free (hits);
139         g_object_unref (smiley_manager);
140
141         empathy_string_parser_substr (text + last, len - last,
142                                       sub_parsers, user_data);
143 }
144
145 void
146 empathy_string_match_all (const gchar *text,
147                           gssize len,
148                           EmpathyStringReplace replace_func,
149                           EmpathyStringParser *sub_parsers,
150                           gpointer user_data)
151 {
152         replace_func (text, len, NULL, user_data);
153 }
154