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