Tagged for release 2.26.0.
[empathy-mirror.git] / gnome-2-26 / libempathy / empathy-utils.c
bloba476d9e54da325cb192dd49737f0e9143710a95c
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.
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>
26 #include "config.h"
28 #include <string.h>
29 #include <time.h>
30 #include <sys/types.h>
32 #include <glib/gi18n-lib.h>
34 #include <libxml/uri.h>
35 #include <telepathy-glib/connection.h>
36 #include <telepathy-glib/channel.h>
37 #include <telepathy-glib/dbus.h>
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"
47 #include <extensions/extensions.h>
49 #define DEBUG_FLAG EMPATHY_DEBUG_OTHER
50 #include "empathy-debug.h"
53 void
54 empathy_init (void)
56 static gboolean initialized = FALSE;
58 if (initialized)
59 return;
61 g_type_init ();
63 /* Setup gettext */
64 bindtextdomain (GETTEXT_PACKAGE, LOCALEDIR);
65 bind_textdomain_codeset (GETTEXT_PACKAGE, "UTF-8");
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);
71 empathy_debug_set_flags (g_getenv ("EMPATHY_DEBUG"));
72 tp_debug_divert_messages (g_getenv ("EMPATHY_LOGFILE"));
74 emp_cli_init ();
76 initialized = TRUE;
79 gchar *
80 empathy_substring (const gchar *str,
81 gint start,
82 gint end)
84 return g_strndup (str + start, end - start);
87 gint
88 empathy_strcasecmp (const gchar *s1,
89 const gchar *s2)
91 return empathy_strncasecmp (s1, s2, -1);
94 gint
95 empathy_strncasecmp (const gchar *s1,
96 const gchar *s2,
97 gsize n)
99 gchar *u1, *u2;
100 gint ret_val;
102 u1 = g_utf8_casefold (s1, n);
103 u2 = g_utf8_casefold (s2, n);
105 ret_val = g_utf8_collate (u1, u2);
106 g_free (u1);
107 g_free (u2);
109 return ret_val;
112 gboolean
113 empathy_xml_validate (xmlDoc *doc,
114 const gchar *dtd_filename)
116 gchar *path, *escaped;
117 xmlValidCtxt cvp;
118 xmlDtd *dtd;
119 gboolean ret;
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);
127 DEBUG ("Loading dtd file %s", path);
129 /* The list of valid chars is taken from libxml. */
130 escaped = xmlURIEscapeStr (path, ":@&=+$,/?;");
131 g_free (path);
133 memset (&cvp, 0, sizeof (cvp));
134 dtd = xmlParseDTD (NULL, escaped);
135 ret = xmlValidateDtd (&cvp, doc, dtd);
137 xmlFree (escaped);
138 xmlFreeDtd (dtd);
140 return ret;
143 xmlNodePtr
144 empathy_xml_node_get_child (xmlNodePtr node,
145 const gchar *child_name)
147 xmlNodePtr l;
149 g_return_val_if_fail (node != NULL, NULL);
150 g_return_val_if_fail (child_name != NULL, NULL);
152 for (l = node->children; l; l = l->next) {
153 if (l->name && strcmp (l->name, child_name) == 0) {
154 return l;
158 return NULL;
161 xmlChar *
162 empathy_xml_node_get_child_content (xmlNodePtr node,
163 const gchar *child_name)
165 xmlNodePtr l;
167 g_return_val_if_fail (node != NULL, NULL);
168 g_return_val_if_fail (child_name != NULL, NULL);
170 l = empathy_xml_node_get_child (node, child_name);
171 if (l) {
172 return xmlNodeGetContent (l);
175 return NULL;
178 xmlNodePtr
179 empathy_xml_node_find_child_prop_value (xmlNodePtr node,
180 const gchar *prop_name,
181 const gchar *prop_value)
183 xmlNodePtr l;
184 xmlNodePtr found = NULL;
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);
190 for (l = node->children; l && !found; l = l->next) {
191 xmlChar *prop;
193 if (!xmlHasProp (l, prop_name)) {
194 continue;
197 prop = xmlGetProp (l, prop_name);
198 if (prop && strcmp (prop, prop_value) == 0) {
199 found = l;
202 xmlFree (prop);
205 return found;
208 guint
209 empathy_account_hash (gconstpointer key)
211 g_return_val_if_fail (MC_IS_ACCOUNT (key), 0);
213 return g_str_hash (mc_account_get_unique_name (MC_ACCOUNT (key)));
216 gboolean
217 empathy_account_equal (gconstpointer a,
218 gconstpointer b)
220 const gchar *name_a;
221 const gchar *name_b;
223 g_return_val_if_fail (MC_IS_ACCOUNT (a), FALSE);
224 g_return_val_if_fail (MC_IS_ACCOUNT (b), FALSE);
226 name_a = mc_account_get_unique_name (MC_ACCOUNT (a));
227 name_b = mc_account_get_unique_name (MC_ACCOUNT (b));
229 return g_str_equal (name_a, name_b);
232 MissionControl *
233 empathy_mission_control_dup_singleton (void)
235 static MissionControl *mc = NULL;
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);
244 return mc;
247 const gchar *
248 empathy_presence_get_default_message (McPresence presence)
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 ();
267 return NULL;
270 const gchar *
271 empathy_presence_to_str (McPresence presence)
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 ();
292 return NULL;
295 McPresence
296 empathy_presence_from_str (const gchar *str)
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;
314 return MC_PRESENCE_UNSET;
317 gchar *
318 empathy_file_lookup (const gchar *filename, const gchar *subdir)
320 gchar *path;
322 if (!subdir) {
323 subdir = ".";
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);
332 return path;
335 typedef struct {
336 EmpathyRunUntilReadyFunc func;
337 gpointer user_data;
338 GObject *object;
339 GMainLoop *loop;
340 } RunUntilReadyData;
342 static void
343 run_until_ready_cb (RunUntilReadyData *data)
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);
351 static gboolean
352 object_is_ready (GObject *object,
353 gpointer user_data)
355 gboolean ready;
357 g_object_get (object, "ready", &ready, NULL);
359 return ready;
362 void
363 empathy_run_until_ready_full (gpointer object,
364 const gchar *signal,
365 EmpathyRunUntilReadyFunc func,
366 gpointer user_data,
367 GMainLoop **loop)
369 RunUntilReadyData data;
370 gulong signal_id;
372 g_return_if_fail (G_IS_OBJECT (object));
373 g_return_if_fail (signal != NULL);
375 if (func && func (object, user_data)) {
376 return;
379 DEBUG ("Starting run until ready for object %p", object);
381 data.func = func;
382 data.user_data = user_data;
383 data.object = object;
384 data.loop = g_main_loop_new (NULL, FALSE);
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;
393 g_main_loop_run (data.loop);
395 if (loop != NULL) {
396 *loop = NULL;
399 g_signal_handler_disconnect (object, signal_id);
400 g_main_loop_unref (data.loop);
403 void
404 empathy_run_until_ready (gpointer object)
406 empathy_run_until_ready_full (object, "notify::ready", object_is_ready,
407 NULL, NULL);
410 McAccount *
411 empathy_channel_get_account (TpChannel *channel)
413 TpConnection *connection;
414 McAccount *account;
415 MissionControl *mc;
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);
423 return account;
426 guint
427 empathy_proxy_hash (gconstpointer key)
429 TpProxy *proxy = TP_PROXY (key);
430 TpProxyClass *proxy_class = TP_PROXY_GET_CLASS (key);
432 g_return_val_if_fail (TP_IS_PROXY (proxy), 0);
433 g_return_val_if_fail (proxy_class->must_have_unique_name, 0);
435 return g_str_hash (proxy->object_path) ^ g_str_hash (proxy->bus_name);
438 gboolean
439 empathy_proxy_equal (gconstpointer a,
440 gconstpointer b)
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);
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);
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);
456 gboolean
457 empathy_check_available_state (void)
459 McPresence presence;
460 EmpathyIdle *idle;
462 idle = empathy_idle_dup_singleton ();
463 presence = empathy_idle_get_state (idle);
464 g_object_unref (idle);
466 if (presence != MC_PRESENCE_AVAILABLE &&
467 presence != MC_PRESENCE_UNSET) {
468 return FALSE;
471 return TRUE;