1 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
3 * Copyright (C) 2003-2007 Imendio AB
4 * Copyright (C) 2007 Collabora Ltd.
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.
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.
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.
21 * Authors: Richard Hult <richard@imendio.com>
22 * Martyn Russell <martyn@imendio.com>
23 * Xavier Claessens <xclaesse@gmail.com>
30 #include <sys/types.h>
33 #include <glib/gi18n.h>
35 #include <libxml/uri.h>
36 #include <libtelepathy/tp-helpers.h>
38 #include "empathy-debug.h"
39 #include "empathy-utils.h"
40 #include "empathy-contact-manager.h"
42 #define DEBUG_DOMAIN "Utils"
44 static void regex_init (void);
47 empathy_substring (const gchar *str,
51 return g_strndup (str + start, end - start);
55 * Regular Expression code to match urls.
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,\\\"]"
65 static regex_t dingus[EMPATHY_REGEX_ALL];
70 static gboolean inited = FALSE;
71 const gchar *expression;
78 for (i = 0; i < EMPATHY_REGEX_ALL; i++) {
80 case EMPATHY_REGEX_AS_IS:
82 SCHEME "//(" USER "@)?[" HOSTCHARS ".]+"
83 "(:[0-9]+)?(" URLPATH ")?";
85 case EMPATHY_REGEX_BROWSER:
87 "(www|ftp)[" HOSTCHARS "]*\\.[" HOSTCHARS ".]+"
88 "(:[0-9]+)?(" URLPATH ")?";
90 case EMPATHY_REGEX_EMAIL:
92 "(mailto:)?[a-z0-9][a-z0-9.-]*@[a-z0-9]"
93 "[a-z0-9-]*(\\.[a-z0-9][a-z0-9-]*)+";
95 case EMPATHY_REGEX_OTHER:
97 "news:[-A-Z\\^_a-z{|}~!\"#$%&'()*+,./0-9;:=?`]+"
98 "@[" HOSTCHARS ".]+(:[0-9]+)?";
101 /* Silence the compiler. */
106 memset (&dingus[i], 0, sizeof (regex_t));
107 regcomp (&dingus[i], expression, REG_EXTENDED | REG_ICASE);
114 empathy_regex_match (EmpathyRegExType type,
119 regmatch_t matches[1];
121 gint num_matches = 0;
125 g_return_val_if_fail (type >= 0 || type <= EMPATHY_REGEX_ALL, 0);
129 while (!ret && type != EMPATHY_REGEX_ALL) {
130 ret = regexec (&dingus[type], msg + offset, 1, matches, 0);
136 s = matches[0].rm_so + offset;
137 offset = matches[0].rm_eo + offset;
139 g_array_append_val (start, s);
140 g_array_append_val (end, offset);
144 if (type != EMPATHY_REGEX_ALL) {
145 empathy_debug (DEBUG_DOMAIN,
146 "Found %d matches for regex type:%d",
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) {
154 ret = regexec (&dingus[i], msg + offset, 1, matches, 0);
160 s = matches[0].rm_so + offset;
161 offset = matches[0].rm_eo + offset;
163 g_array_append_val (start, s);
164 g_array_append_val (end, offset);
169 empathy_debug (DEBUG_DOMAIN,
170 "Found %d matches for ALL regex types",
177 empathy_strcasecmp (const gchar *s1,
180 return empathy_strncasecmp (s1, s2, -1);
184 empathy_strncasecmp (const gchar *s1,
191 u1 = g_utf8_casefold (s1, n);
192 u2 = g_utf8_casefold (s2, n);
194 ret_val = g_utf8_collate (u1, u2);
202 empathy_xml_validate (xmlDoc *doc,
203 const gchar *dtd_filename)
205 gchar *path, *escaped;
210 path = g_build_filename (DATADIR, "empathy", dtd_filename, NULL);
212 /* The list of valid chars is taken from libxml. */
213 escaped = xmlURIEscapeStr (path, ":@&=+$,/?;");
217 memset (&cvp, 0, sizeof (cvp));
218 dtd = xmlParseDTD (NULL, escaped);
219 ret = xmlValidateDtd (&cvp, doc, dtd);
228 empathy_xml_node_get_child (xmlNodePtr node,
229 const gchar *child_name)
233 g_return_val_if_fail (node != NULL, NULL);
234 g_return_val_if_fail (child_name != NULL, NULL);
236 for (l = node->children; l; l = l->next) {
237 if (l->name && strcmp (l->name, child_name) == 0) {
246 empathy_xml_node_get_child_content (xmlNodePtr node,
247 const gchar *child_name)
251 g_return_val_if_fail (node != NULL, NULL);
252 g_return_val_if_fail (child_name != NULL, NULL);
254 l = empathy_xml_node_get_child (node, child_name);
256 return xmlNodeGetContent (l);
263 empathy_xml_node_find_child_prop_value (xmlNodePtr node,
264 const gchar *prop_name,
265 const gchar *prop_value)
268 xmlNodePtr found = NULL;
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);
274 for (l = node->children; l && !found; l = l->next) {
277 if (!xmlHasProp (l, prop_name)) {
281 prop = xmlGetProp (l, prop_name);
282 if (prop && strcmp (prop, prop_value) == 0) {
293 empathy_dbus_type_to_g_type (const gchar *dbus_type_string)
295 if (dbus_type_string == NULL)
298 if (dbus_type_string[0] == 's') {
299 return G_TYPE_STRING;
301 else if (dbus_type_string[0] == 'b') {
302 return G_TYPE_BOOLEAN;
304 else if (dbus_type_string[0] == 'q') {
307 else if (dbus_type_string[0] == 'n') {
311 g_assert_not_reached ();
316 empathy_g_type_to_dbus_type (GType g_type)
328 g_assert_not_reached ();
335 empathy_g_value_to_string (const GValue *value)
337 gchar *return_string = NULL;
338 GValue string_g_value = {0, };
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);
345 return return_string;
349 empathy_string_to_g_value (const gchar *str, GType type)
353 g_value = g_new0 (GValue, 1);
354 g_value_init (g_value, type);
358 g_value_set_string (g_value, str);
361 g_value_set_boolean (g_value, (str[0] == 'y' || str[0] == 'T'));
364 g_value_set_uint (g_value, atoi (str));
367 g_value_set_int (g_value, atoi (str));
370 g_assert_not_reached ();
377 empathy_g_value_equal (const GValue *value1,
378 const GValue *value2)
382 g_return_val_if_fail (value1 != NULL, FALSE);
383 g_return_val_if_fail (value2 != NULL, FALSE);
385 type = G_VALUE_TYPE (value1);
386 if (type != G_VALUE_TYPE (value2)) {
392 case G_TYPE_STRING: {
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));
402 return g_value_get_boolean (value1) == g_value_get_boolean (value2);
404 return g_value_get_uint (value1) == g_value_get_uint (value2);
406 return g_value_get_int (value1) == g_value_get_int (value2);
408 g_warning ("Unsupported GType in value comparaison");
415 empathy_account_hash (gconstpointer key)
417 return g_str_hash (mc_account_get_unique_name (MC_ACCOUNT (key)));
421 empathy_account_equal (gconstpointer a,
427 name_a = mc_account_get_unique_name (MC_ACCOUNT (a));
428 name_b = mc_account_get_unique_name (MC_ACCOUNT (b));
430 return g_str_equal (name_a, name_b);
434 empathy_mission_control_new (void)
436 static MissionControl *mc = NULL;
439 mc = mission_control_new (tp_get_bus ());
440 g_object_add_weak_pointer (G_OBJECT (mc), (gpointer) &mc);
449 empathy_get_channel_id (McAccount *account,
459 g_return_val_if_fail (MC_IS_ACCOUNT (account), NULL);
460 g_return_val_if_fail (TELEPATHY_IS_CHAN (tp_chan), NULL);
462 mc = empathy_mission_control_new ();
463 tp_conn = mission_control_get_connection (mc, account, NULL);
470 /* Get the handle's name */
471 handles = g_array_new (FALSE, FALSE, sizeof (guint));
472 g_array_append_val (handles, tp_chan->handle);
473 if (!tp_conn_inspect_handles (DBUS_G_PROXY (tp_conn),
474 tp_chan->handle_type,
478 empathy_debug (DEBUG_DOMAIN,
479 "Couldn't get id: %s",
480 error ? error->message : "No error given");
482 g_clear_error (&error);
483 g_array_free (handles, TRUE);
484 g_object_unref (tp_conn);
491 g_object_unref (tp_conn);