]> git.0d.be Git - empathy.git/blob - libempathy/empathy-utils.c
Initial room list support. It does not works yet.
[empathy.git] / libempathy / empathy-utils.c
1 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
2 /*
3  * Copyright (C) 2003-2007 Imendio AB
4  * Copyright (C) 2007 Collabora Ltd.
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License as
8  * published by the Free Software Foundation; either version 2 of the
9  * License, or (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public
17  * License along with this program; if not, write to the
18  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
19  * Boston, MA 02111-1307, USA.
20  *
21  * Authors: Richard Hult <richard@imendio.com>
22  *          Martyn Russell <martyn@imendio.com>
23  *          Xavier Claessens <xclaesse@gmail.com>
24  */
25
26 #include "config.h"
27
28 #include <string.h>
29 #include <time.h>
30 #include <sys/types.h>
31 #include <regex.h>
32
33 #include <glib/gi18n.h>
34
35 #include <libxml/uri.h>
36 #include <libtelepathy/tp-helpers.h>
37
38 #include "empathy-debug.h"
39 #include "empathy-utils.h"
40 #include "empathy-contact-manager.h"
41
42 #define DEBUG_DOMAIN "Utils"
43
44 static void regex_init (void);
45
46 gchar *
47 empathy_substring (const gchar *str,
48                   gint         start,
49                   gint         end)
50 {
51         return g_strndup (str + start, end - start);
52 }
53
54 /*
55  * Regular Expression code to match urls.
56  */
57 #define USERCHARS "-A-Za-z0-9"
58 #define PASSCHARS "-A-Za-z0-9,?;.:/!%$^*&~\"#'"
59 #define HOSTCHARS "-A-Za-z0-9"
60 #define PATHCHARS "-A-Za-z0-9_$.+!*(),;:@&=?/~#%"
61 #define SCHEME    "(news:|telnet:|nntp:|file:/|https?:|ftps?:|webcal:)"
62 #define USER      "[" USERCHARS "]+(:["PASSCHARS "]+)?"
63 #define URLPATH   "/[" PATHCHARS "]*[^]'.}>) \t\r\n,\\\"]"
64
65 static regex_t dingus[EMPATHY_REGEX_ALL];
66
67 static void
68 regex_init (void)
69 {
70         static gboolean  inited = FALSE;
71         const gchar     *expression;
72         gint             i;
73
74         if (inited) {
75                 return;
76         }
77
78         for (i = 0; i < EMPATHY_REGEX_ALL; i++) {
79                 switch (i) {
80                 case EMPATHY_REGEX_AS_IS:
81                         expression =
82                                 SCHEME "//(" USER "@)?[" HOSTCHARS ".]+"
83                                 "(:[0-9]+)?(" URLPATH ")?";
84                         break;
85                 case EMPATHY_REGEX_BROWSER:
86                         expression =
87                                 "(www|ftp)[" HOSTCHARS "]*\\.[" HOSTCHARS ".]+"
88                                 "(:[0-9]+)?(" URLPATH ")?";
89                         break;
90                 case EMPATHY_REGEX_EMAIL:
91                         expression =
92                                 "(mailto:)?[a-z0-9][a-z0-9.-]*@[a-z0-9]"
93                                 "[a-z0-9-]*(\\.[a-z0-9][a-z0-9-]*)+";
94                         break;
95                 case EMPATHY_REGEX_OTHER:
96                         expression =
97                                 "news:[-A-Z\\^_a-z{|}~!\"#$%&'()*+,./0-9;:=?`]+"
98                                 "@[" HOSTCHARS ".]+(:[0-9]+)?";
99                         break;
100                 default:
101                         /* Silence the compiler. */
102                         expression = NULL;
103                         continue;
104                 }
105
106                 memset (&dingus[i], 0, sizeof (regex_t));
107                 regcomp (&dingus[i], expression, REG_EXTENDED | REG_ICASE);
108         }
109
110         inited = TRUE;
111 }
112
113 gint
114 empathy_regex_match (EmpathyRegExType  type,
115                     const gchar     *msg,
116                     GArray          *start,
117                     GArray          *end)
118 {
119         regmatch_t matches[1];
120         gint       ret = 0;
121         gint       num_matches = 0;
122         gint       offset = 0;
123         gint       i;
124
125         g_return_val_if_fail (type >= 0 || type <= EMPATHY_REGEX_ALL, 0);
126
127         regex_init ();
128
129         while (!ret && type != EMPATHY_REGEX_ALL) {
130                 ret = regexec (&dingus[type], msg + offset, 1, matches, 0);
131                 if (ret == 0) {
132                         gint s;
133
134                         num_matches++;
135
136                         s = matches[0].rm_so + offset;
137                         offset = matches[0].rm_eo + offset;
138
139                         g_array_append_val (start, s);
140                         g_array_append_val (end, offset);
141                 }
142         }
143
144         if (type != EMPATHY_REGEX_ALL) {
145                 empathy_debug (DEBUG_DOMAIN,
146                               "Found %d matches for regex type:%d",
147                               num_matches, type);
148                 return num_matches;
149         }
150
151         /* If EMPATHY_REGEX_ALL then we run ALL regex's on the string. */
152         for (i = 0; i < EMPATHY_REGEX_ALL; i++, ret = 0) {
153                 while (!ret) {
154                         ret = regexec (&dingus[i], msg + offset, 1, matches, 0);
155                         if (ret == 0) {
156                                 gint s;
157
158                                 num_matches++;
159
160                                 s = matches[0].rm_so + offset;
161                                 offset = matches[0].rm_eo + offset;
162
163                                 g_array_append_val (start, s);
164                                 g_array_append_val (end, offset);
165                         }
166                 }
167         }
168
169         empathy_debug (DEBUG_DOMAIN,
170                       "Found %d matches for ALL regex types",
171                       num_matches);
172
173         return num_matches;
174 }
175
176 gint
177 empathy_strcasecmp (const gchar *s1,
178                    const gchar *s2)
179 {
180         return empathy_strncasecmp (s1, s2, -1);
181 }
182
183 gint
184 empathy_strncasecmp (const gchar *s1,
185                     const gchar *s2,
186                     gsize        n)
187 {
188         gchar *u1, *u2;
189         gint   ret_val;
190
191         u1 = g_utf8_casefold (s1, n);
192         u2 = g_utf8_casefold (s2, n);
193
194         ret_val = g_utf8_collate (u1, u2);
195         g_free (u1);
196         g_free (u2);
197
198         return ret_val;
199 }
200
201 gboolean
202 empathy_xml_validate (xmlDoc      *doc,
203                      const gchar *dtd_filename)
204 {
205         gchar        *path, *escaped;
206         xmlValidCtxt  cvp;
207         xmlDtd       *dtd;
208         gboolean      ret;
209
210         path = g_build_filename (DATADIR, "empathy", dtd_filename, NULL);
211
212         /* The list of valid chars is taken from libxml. */
213         escaped = xmlURIEscapeStr (path, ":@&=+$,/?;");
214
215         g_free (path);
216
217         memset (&cvp, 0, sizeof (cvp));
218         dtd = xmlParseDTD (NULL, escaped);
219         ret = xmlValidateDtd (&cvp, doc, dtd);
220
221         xmlFree (escaped);
222         xmlFreeDtd (dtd);
223
224         return ret;
225 }
226
227 xmlNodePtr
228 empathy_xml_node_get_child (xmlNodePtr   node, 
229                            const gchar *child_name)
230 {
231         xmlNodePtr l;
232
233         g_return_val_if_fail (node != NULL, NULL);
234         g_return_val_if_fail (child_name != NULL, NULL);
235
236         for (l = node->children; l; l = l->next) {
237                 if (l->name && strcmp (l->name, child_name) == 0) {
238                         return l;
239                 }
240         }
241
242         return NULL;
243 }
244
245 xmlChar *
246 empathy_xml_node_get_child_content (xmlNodePtr   node, 
247                                    const gchar *child_name)
248 {
249         xmlNodePtr l;
250
251         g_return_val_if_fail (node != NULL, NULL);
252         g_return_val_if_fail (child_name != NULL, NULL);
253
254         l = empathy_xml_node_get_child (node, child_name);
255         if (l) {
256                 return xmlNodeGetContent (l);
257         }
258                 
259         return NULL;
260 }
261
262 xmlNodePtr
263 empathy_xml_node_find_child_prop_value (xmlNodePtr   node, 
264                                        const gchar *prop_name,
265                                        const gchar *prop_value)
266 {
267         xmlNodePtr l;
268         xmlNodePtr found = NULL;
269
270         g_return_val_if_fail (node != NULL, NULL);
271         g_return_val_if_fail (prop_name != NULL, NULL);
272         g_return_val_if_fail (prop_value != NULL, NULL);
273
274         for (l = node->children; l && !found; l = l->next) {
275                 xmlChar *prop;
276
277                 if (!xmlHasProp (l, prop_name)) {
278                         continue;
279                 }
280
281                 prop = xmlGetProp (l, prop_name);
282                 if (prop && strcmp (prop, prop_value) == 0) {
283                         found = l;
284                 }
285                 
286                 xmlFree (prop);
287         }
288                 
289         return found;
290 }
291
292 GType
293 empathy_dbus_type_to_g_type (const gchar *dbus_type_string)
294 {
295         if (dbus_type_string == NULL)
296                 return G_TYPE_NONE;
297
298         if (dbus_type_string[0] == 's') {
299                 return G_TYPE_STRING;
300         }
301         else if (dbus_type_string[0] == 'b') {
302                 return G_TYPE_BOOLEAN;
303         }
304         else if (dbus_type_string[0] == 'q') {
305                 return G_TYPE_UINT;
306         }
307         else if (dbus_type_string[0] == 'n') {
308                 return G_TYPE_INT;
309         }
310
311         g_assert_not_reached ();
312         return G_TYPE_NONE;
313 }
314
315 const gchar *
316 empathy_g_type_to_dbus_type (GType g_type)
317 {
318         switch (g_type) {
319         case G_TYPE_STRING:
320                 return "s";
321         case G_TYPE_BOOLEAN:
322                 return "b";
323         case G_TYPE_UINT:
324                 return "q";
325         case G_TYPE_INT:
326                 return "n";
327         default:
328                 g_assert_not_reached ();
329         }
330
331         return NULL;
332 }
333
334 gchar *
335 empathy_g_value_to_string (const GValue *value)
336 {
337         gchar  *return_string = NULL;
338         GValue  string_g_value = {0, };
339
340         g_value_init (&string_g_value, G_TYPE_STRING);
341         g_value_transform (value, &string_g_value);
342         return_string = g_value_dup_string (&string_g_value);
343         g_value_unset (&string_g_value);
344
345         return return_string;
346 }
347
348 GValue *
349 empathy_string_to_g_value (const gchar *str, GType type)
350 {
351         GValue *g_value;
352
353         g_value = g_new0 (GValue, 1);
354         g_value_init (g_value, type);
355
356         switch (type) {
357         case G_TYPE_STRING:
358                 g_value_set_string (g_value, str);
359                 break;
360         case G_TYPE_BOOLEAN:
361                 g_value_set_boolean (g_value, (str[0] == 'y' || str[0] == 'T'));
362                 break;
363         case G_TYPE_UINT:
364                 g_value_set_uint (g_value, atoi (str));
365                 break;
366         case G_TYPE_INT:
367                 g_value_set_int (g_value, atoi (str));
368                 break;
369         default:
370                 g_assert_not_reached ();
371         }
372
373         return g_value;
374 }
375
376 gboolean
377 empathy_g_value_equal (const GValue *value1,
378                       const GValue *value2)
379 {
380         GType type;
381
382         g_return_val_if_fail (value1 != NULL, FALSE);
383         g_return_val_if_fail (value2 != NULL, FALSE);
384
385         type = G_VALUE_TYPE (value1);
386         if (type != G_VALUE_TYPE (value2)) {
387                 return FALSE;
388         }
389
390         switch (type)
391         {
392         case G_TYPE_STRING: {
393                 const gchar *str1;
394                 const gchar *str2;
395
396                 str1 = g_value_get_string (value1);
397                 str2 = g_value_get_string (value2);
398                 return (str1 && str2 && strcmp (str1, str2) == 0) ||
399                        (G_STR_EMPTY (str1) && G_STR_EMPTY (str2));
400         }
401         case G_TYPE_BOOLEAN:
402                 return g_value_get_boolean (value1) == g_value_get_boolean (value2);
403         case G_TYPE_UINT:
404                 return g_value_get_uint (value1) == g_value_get_uint (value2);
405         case G_TYPE_INT:
406                 return g_value_get_int (value1) == g_value_get_int (value2);
407         default:
408                 g_warning ("Unsupported GType in value comparaison");
409         }
410
411         return FALSE;
412 }
413
414 guint
415 empathy_account_hash (gconstpointer key)
416 {
417         return g_str_hash (mc_account_get_unique_name (MC_ACCOUNT (key)));
418 }
419
420 gboolean
421 empathy_account_equal (gconstpointer a,
422                       gconstpointer b)
423 {
424         const gchar *name_a;
425         const gchar *name_b;
426
427         name_a = mc_account_get_unique_name (MC_ACCOUNT (a));
428         name_b = mc_account_get_unique_name (MC_ACCOUNT (b));
429
430         return g_str_equal (name_a, name_b);
431 }
432
433 MissionControl *
434 empathy_mission_control_new (void)
435 {
436         static MissionControl *mc = NULL;
437
438         if (!mc) {
439                 mc = mission_control_new (tp_get_bus ());
440                 g_object_add_weak_pointer (G_OBJECT (mc), (gpointer) &mc);
441         } else {
442                 g_object_ref (mc);
443         }
444
445         return mc;
446 }
447
448 gchar *
449 empathy_inspect_channel (McAccount *account,
450                          TpChan    *tp_chan)
451 {
452         g_return_val_if_fail (MC_IS_ACCOUNT (account), NULL);
453         g_return_val_if_fail (TELEPATHY_IS_CHAN (tp_chan), NULL);
454
455         return empathy_inspect_handle (account,
456                                        tp_chan->handle,
457                                        tp_chan->handle_type);
458 }
459
460 gchar *
461 empathy_inspect_handle (McAccount *account,
462                         guint      handle,
463                         guint      handle_type)
464 {
465         MissionControl  *mc;
466         TpConn          *tp_conn;
467         GArray          *handles;
468         gchar          **names;
469         gchar           *name;
470         GError          *error;
471
472         g_return_val_if_fail (MC_IS_ACCOUNT (account), NULL);
473         g_return_val_if_fail (handle != 0, NULL);
474         g_return_val_if_fail (handle_type != 0, NULL);
475
476         mc = empathy_mission_control_new ();
477         tp_conn = mission_control_get_connection (mc, account, NULL);
478         g_object_unref (mc);
479
480         if (!tp_conn) {
481                 return NULL;
482         }
483
484         /* Get the handle's name */
485         handles = g_array_new (FALSE, FALSE, sizeof (guint));
486         g_array_append_val (handles, handle);
487         if (!tp_conn_inspect_handles (DBUS_G_PROXY (tp_conn),
488                                       handle_type,
489                                       handles,
490                                       &names,
491                                       &error)) {
492                 empathy_debug (DEBUG_DOMAIN, 
493                               "Couldn't get id: %s",
494                               error ? error->message : "No error given");
495
496                 g_clear_error (&error);
497                 g_array_free (handles, TRUE);
498                 g_object_unref (tp_conn);
499                 
500                 return NULL;
501         }
502
503         name = *names;
504         g_free (names);
505         g_object_unref (tp_conn);
506
507         return name;
508 }
509
510