]> git.0d.be Git - empathy.git/blob - libempathy/empathy-utils.c
Extract protocol from the connection's object-path and correctly set the special...
[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-2008 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
32 #include <glib/gi18n-lib.h>
33
34 #include <libxml/uri.h>
35 #include <telepathy-glib/connection.h>
36 #include <telepathy-glib/channel.h>
37 #include <telepathy-glib/dbus.h>
38
39 #include "empathy-utils.h"
40 #include "empathy-contact-manager.h"
41 #include "empathy-dispatcher.h"
42 #include "empathy-dispatch-operation.h"
43 #include "empathy-idle.h"
44 #include "empathy-tp-call.h"
45
46 #include <extensions/extensions.h>
47
48 #define DEBUG_FLAG EMPATHY_DEBUG_OTHER
49 #include "empathy-debug.h"
50
51
52 void
53 empathy_init (void)
54 {
55         static gboolean initialized = FALSE;
56
57         if (initialized)
58                 return;
59
60         g_type_init ();
61
62         /* Setup gettext */
63         bindtextdomain (GETTEXT_PACKAGE, LOCALEDIR);
64         bind_textdomain_codeset (GETTEXT_PACKAGE, "UTF-8");
65
66         /* Setup debug output for empathy and telepathy-glib */
67         if (g_getenv ("EMPATHY_TIMING") != NULL) {
68                 g_log_set_default_handler (tp_debug_timestamped_log_handler, NULL);
69         }
70         empathy_debug_set_flags (g_getenv ("EMPATHY_DEBUG"));
71         tp_debug_divert_messages (g_getenv ("EMPATHY_LOGFILE"));
72
73         emp_cli_init ();
74
75         initialized = TRUE;
76 }
77
78 gchar *
79 empathy_substring (const gchar *str,
80                   gint         start,
81                   gint         end)
82 {
83         return g_strndup (str + start, end - start);
84 }
85
86 gint
87 empathy_strcasecmp (const gchar *s1,
88                    const gchar *s2)
89 {
90         return empathy_strncasecmp (s1, s2, -1);
91 }
92
93 gint
94 empathy_strncasecmp (const gchar *s1,
95                     const gchar *s2,
96                     gsize        n)
97 {
98         gchar *u1, *u2;
99         gint   ret_val;
100
101         u1 = g_utf8_casefold (s1, n);
102         u2 = g_utf8_casefold (s2, n);
103
104         ret_val = g_utf8_collate (u1, u2);
105         g_free (u1);
106         g_free (u2);
107
108         return ret_val;
109 }
110
111 gboolean
112 empathy_xml_validate (xmlDoc      *doc,
113                      const gchar *dtd_filename)
114 {
115         gchar        *path, *escaped;
116         xmlValidCtxt  cvp;
117         xmlDtd       *dtd;
118         gboolean      ret;
119
120         path = g_build_filename (g_getenv ("EMPATHY_SRCDIR"), "libempathy",
121                                  dtd_filename, NULL);
122         if (!g_file_test (path, G_FILE_TEST_EXISTS)) {
123                 g_free (path);
124                 path = g_build_filename (DATADIR, "empathy", dtd_filename, NULL);
125         }
126         DEBUG ("Loading dtd file %s", path);
127
128         /* The list of valid chars is taken from libxml. */
129         escaped = xmlURIEscapeStr (path, ":@&=+$,/?;");
130         g_free (path);
131
132         memset (&cvp, 0, sizeof (cvp));
133         dtd = xmlParseDTD (NULL, escaped);
134         ret = xmlValidateDtd (&cvp, doc, dtd);
135
136         xmlFree (escaped);
137         xmlFreeDtd (dtd);
138
139         return ret;
140 }
141
142 xmlNodePtr
143 empathy_xml_node_get_child (xmlNodePtr   node, 
144                            const gchar *child_name)
145 {
146         xmlNodePtr l;
147
148         g_return_val_if_fail (node != NULL, NULL);
149         g_return_val_if_fail (child_name != NULL, NULL);
150
151         for (l = node->children; l; l = l->next) {
152                 if (l->name && strcmp (l->name, child_name) == 0) {
153                         return l;
154                 }
155         }
156
157         return NULL;
158 }
159
160 xmlChar *
161 empathy_xml_node_get_child_content (xmlNodePtr   node, 
162                                    const gchar *child_name)
163 {
164         xmlNodePtr l;
165
166         g_return_val_if_fail (node != NULL, NULL);
167         g_return_val_if_fail (child_name != NULL, NULL);
168
169         l = empathy_xml_node_get_child (node, child_name);
170         if (l) {
171                 return xmlNodeGetContent (l);
172         }
173                 
174         return NULL;
175 }
176
177 xmlNodePtr
178 empathy_xml_node_find_child_prop_value (xmlNodePtr   node, 
179                                        const gchar *prop_name,
180                                        const gchar *prop_value)
181 {
182         xmlNodePtr l;
183         xmlNodePtr found = NULL;
184
185         g_return_val_if_fail (node != NULL, NULL);
186         g_return_val_if_fail (prop_name != NULL, NULL);
187         g_return_val_if_fail (prop_value != NULL, NULL);
188
189         for (l = node->children; l && !found; l = l->next) {
190                 xmlChar *prop;
191
192                 if (!xmlHasProp (l, prop_name)) {
193                         continue;
194                 }
195
196                 prop = xmlGetProp (l, prop_name);
197                 if (prop && strcmp (prop, prop_value) == 0) {
198                         found = l;
199                 }
200                 
201                 xmlFree (prop);
202         }
203                 
204         return found;
205 }
206
207 guint
208 empathy_account_hash (gconstpointer key)
209 {
210         g_return_val_if_fail (MC_IS_ACCOUNT (key), 0);
211
212         return g_str_hash (mc_account_get_unique_name (MC_ACCOUNT (key)));
213 }
214
215 gboolean
216 empathy_account_equal (gconstpointer a,
217                        gconstpointer b)
218 {
219         const gchar *name_a;
220         const gchar *name_b;
221
222         g_return_val_if_fail (MC_IS_ACCOUNT (a), FALSE);
223         g_return_val_if_fail (MC_IS_ACCOUNT (b), FALSE);
224
225         name_a = mc_account_get_unique_name (MC_ACCOUNT (a));
226         name_b = mc_account_get_unique_name (MC_ACCOUNT (b));
227
228         return g_str_equal (name_a, name_b);
229 }
230
231 MissionControl *
232 empathy_mission_control_dup_singleton (void)
233 {
234         static MissionControl *mc = NULL;
235
236         if (!mc) {
237                 mc = mission_control_new (tp_get_bus ());
238                 g_object_add_weak_pointer (G_OBJECT (mc), (gpointer) &mc);
239         } else {
240                 g_object_ref (mc);
241         }
242
243         return mc;
244 }
245
246 const gchar *
247 empathy_presence_get_default_message (McPresence presence)
248 {
249         switch (presence) {
250         case MC_PRESENCE_AVAILABLE:
251                 return _("Available");
252         case MC_PRESENCE_DO_NOT_DISTURB:
253                 return _("Busy");
254         case MC_PRESENCE_AWAY:
255         case MC_PRESENCE_EXTENDED_AWAY:
256                 return _("Away");
257         case MC_PRESENCE_HIDDEN:
258                 return _("Hidden");
259         case MC_PRESENCE_OFFLINE:
260         case MC_PRESENCE_UNSET:
261                 return _("Offline");
262         default:
263                 g_assert_not_reached ();
264         }
265
266         return NULL;
267 }
268
269 const gchar *
270 empathy_presence_to_str (McPresence presence)
271 {
272         switch (presence) {
273         case MC_PRESENCE_AVAILABLE:
274                 return "available";
275         case MC_PRESENCE_DO_NOT_DISTURB:
276                 return "busy";
277         case MC_PRESENCE_AWAY:
278                 return "away";
279         case MC_PRESENCE_EXTENDED_AWAY:
280                 return "ext_away";
281         case MC_PRESENCE_HIDDEN:
282                 return "hidden";
283         case MC_PRESENCE_OFFLINE:
284                 return "offline";
285         case MC_PRESENCE_UNSET:
286                 return "unset";
287         default:
288                 g_assert_not_reached ();
289         }
290
291         return NULL;
292 }
293
294 McPresence
295 empathy_presence_from_str (const gchar *str)
296 {
297         if (strcmp (str, "available") == 0) {
298                 return MC_PRESENCE_AVAILABLE;
299         } else if ((strcmp (str, "dnd") == 0) || (strcmp (str, "busy") == 0)) {
300                 return MC_PRESENCE_DO_NOT_DISTURB;
301         } else if ((strcmp (str, "away") == 0) || (strcmp (str, "brb") == 0)) {
302                 return MC_PRESENCE_AWAY;
303         } else if ((strcmp (str, "xa") == 0) || (strcmp (str, "ext_away") == 0)) {
304                 return MC_PRESENCE_EXTENDED_AWAY;
305         } else if (strcmp (str, "hidden") == 0) {
306                 return MC_PRESENCE_HIDDEN;
307         } else if (strcmp (str, "offline") == 0) {
308                 return MC_PRESENCE_OFFLINE;
309         } else if (strcmp (str, "unset") == 0) {
310                 return MC_PRESENCE_UNSET;
311         }
312
313         return MC_PRESENCE_UNSET;
314 }
315
316 gchar *
317 empathy_file_lookup (const gchar *filename, const gchar *subdir)
318 {
319         gchar *path;
320
321         if (!subdir) {
322                 subdir = ".";
323         }
324
325         path = g_build_filename (g_getenv ("EMPATHY_SRCDIR"), subdir, filename, NULL);
326         if (!g_file_test (path, G_FILE_TEST_EXISTS)) {
327                 g_free (path);
328                 path = g_build_filename (DATADIR, "empathy", filename, NULL);
329         }
330
331         return path;
332 }
333
334 guint
335 empathy_proxy_hash (gconstpointer key)
336 {
337         TpProxy      *proxy = TP_PROXY (key);
338         TpProxyClass *proxy_class = TP_PROXY_GET_CLASS (key);
339
340         g_return_val_if_fail (TP_IS_PROXY (proxy), 0);
341         g_return_val_if_fail (proxy_class->must_have_unique_name, 0);
342
343         return g_str_hash (proxy->object_path) ^ g_str_hash (proxy->bus_name);
344 }
345
346 gboolean
347 empathy_proxy_equal (gconstpointer a,
348                      gconstpointer b)
349 {
350         TpProxy *proxy_a = TP_PROXY (a);
351         TpProxy *proxy_b = TP_PROXY (b);
352         TpProxyClass *proxy_a_class = TP_PROXY_GET_CLASS (a);
353         TpProxyClass *proxy_b_class = TP_PROXY_GET_CLASS (b);
354
355         g_return_val_if_fail (TP_IS_PROXY (proxy_a), FALSE);
356         g_return_val_if_fail (TP_IS_PROXY (proxy_b), FALSE);
357         g_return_val_if_fail (proxy_a_class->must_have_unique_name, 0);
358         g_return_val_if_fail (proxy_b_class->must_have_unique_name, 0);
359
360         return g_str_equal (proxy_a->object_path, proxy_b->object_path) &&
361                g_str_equal (proxy_a->bus_name, proxy_b->bus_name);
362 }
363
364 gboolean
365 empathy_check_available_state (void)
366 {
367         McPresence presence;
368         EmpathyIdle *idle;
369
370         idle = empathy_idle_dup_singleton ();
371         presence = empathy_idle_get_state (idle);
372         g_object_unref (idle);
373
374         if (presence != MC_PRESENCE_AVAILABLE &&
375                 presence != MC_PRESENCE_UNSET) {
376                 return FALSE;    
377         }
378
379         return TRUE;
380 }
381
382 gchar *
383 empathy_connection_get_protocol (TpConnection    *connection,
384                                  gchar          **ret_cmname)
385 {
386         const gchar *object_path;
387         const gchar *cmname;
388         const gchar *proto;
389         const gchar *account;
390         gchar *ret;
391
392         g_return_val_if_fail (TP_IS_CONNECTION (connection), NULL);
393
394         /* Object path is in the form:
395          * /org/freedesktop/Telepathy/Connection/cmname/proto/account */
396         object_path = tp_proxy_get_object_path (TP_PROXY (connection));
397         cmname = object_path + strlen ("/org/freedesktop/Telepathy/Connection/");
398         proto = strstr (cmname, "/") + 1;
399         account = strstr (proto, "/") + 1;
400
401         if (ret_cmname) {
402                 *ret_cmname = g_strndup (cmname, proto - cmname - 1);
403                 g_strdelimit (*ret_cmname, "_", '-');
404         }
405
406         ret = g_strndup (proto, account - proto - 1);
407         return g_strdelimit (ret, "_", '-');
408 }
409