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-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>
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"
56 static gboolean initialized = FALSE;
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"));
80 empathy_substring (const gchar *str,
84 return g_strndup (str + start, end - start);
88 empathy_strcasecmp (const gchar *s1,
91 return empathy_strncasecmp (s1, s2, -1);
95 empathy_strncasecmp (const gchar *s1,
102 u1 = g_utf8_casefold (s1, n);
103 u2 = g_utf8_casefold (s2, n);
105 ret_val = g_utf8_collate (u1, u2);
113 empathy_xml_validate (xmlDoc *doc,
114 const gchar *dtd_filename)
116 gchar *path, *escaped;
121 path = g_build_filename (g_getenv ("EMPATHY_SRCDIR"), "libempathy",
123 if (!g_file_test (path, G_FILE_TEST_EXISTS)) {
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, ":@&=+$,/?;");
133 memset (&cvp, 0, sizeof (cvp));
134 dtd = xmlParseDTD (NULL, escaped);
135 ret = xmlValidateDtd (&cvp, doc, dtd);
144 empathy_xml_node_get_child (xmlNodePtr node,
145 const gchar *child_name)
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) {
162 empathy_xml_node_get_child_content (xmlNodePtr node,
163 const gchar *child_name)
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);
172 return xmlNodeGetContent (l);
179 empathy_xml_node_find_child_prop_value (xmlNodePtr node,
180 const gchar *prop_name,
181 const gchar *prop_value)
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) {
193 if (!xmlHasProp (l, prop_name)) {
197 prop = xmlGetProp (l, prop_name);
198 if (prop && strcmp (prop, prop_value) == 0) {
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)));
217 empathy_account_equal (gconstpointer a,
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);
233 empathy_mission_control_dup_singleton (void)
235 static MissionControl *mc = NULL;
238 mc = mission_control_new (tp_get_bus ());
239 g_object_add_weak_pointer (G_OBJECT (mc), (gpointer) &mc);
248 empathy_presence_get_default_message (McPresence presence)
251 case MC_PRESENCE_AVAILABLE:
252 return _("Available");
253 case MC_PRESENCE_DO_NOT_DISTURB:
255 case MC_PRESENCE_AWAY:
256 case MC_PRESENCE_EXTENDED_AWAY:
258 case MC_PRESENCE_HIDDEN:
260 case MC_PRESENCE_OFFLINE:
261 case MC_PRESENCE_UNSET:
264 g_assert_not_reached ();
271 empathy_presence_to_str (McPresence presence)
274 case MC_PRESENCE_AVAILABLE:
276 case MC_PRESENCE_DO_NOT_DISTURB:
278 case MC_PRESENCE_AWAY:
280 case MC_PRESENCE_EXTENDED_AWAY:
282 case MC_PRESENCE_HIDDEN:
284 case MC_PRESENCE_OFFLINE:
286 case MC_PRESENCE_UNSET:
289 g_assert_not_reached ();
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;
318 empathy_file_lookup (const gchar *filename, const gchar *subdir)
326 path = g_build_filename (g_getenv ("EMPATHY_SRCDIR"), subdir, filename, NULL);
327 if (!g_file_test (path, G_FILE_TEST_EXISTS)) {
329 path = g_build_filename (DATADIR, "empathy", filename, NULL);
336 EmpathyRunUntilReadyFunc func;
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);
352 object_is_ready (GObject *object,
357 g_object_get (object, "ready", &ready, NULL);
363 empathy_run_until_ready_full (gpointer object,
365 EmpathyRunUntilReadyFunc func,
369 RunUntilReadyData data;
372 g_return_if_fail (G_IS_OBJECT (object));
373 g_return_if_fail (signal != NULL);
375 if (func && func (object, user_data)) {
379 DEBUG ("Starting run until ready for object %p", object);
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),
393 g_main_loop_run (data.loop);
399 g_signal_handler_disconnect (object, signal_id);
400 g_main_loop_unref (data.loop);
404 empathy_run_until_ready (gpointer object)
406 empathy_run_until_ready_full (object, "notify::ready", object_is_ready,
411 empathy_channel_get_account (TpChannel *channel)
413 TpConnection *connection;
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);
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);
439 empathy_proxy_equal (gconstpointer a,
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);
457 empathy_check_available_state (void)
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) {