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-manager.h"
41 #include "empathy-dispatcher.h"
42 #include "empathy-dispatch-operation.h"
43 #include "empathy-idle.h"
44 #include "empathy-tp-call.h"
46 #include <extensions/extensions.h>
48 #define DEBUG_FLAG EMPATHY_DEBUG_OTHER
49 #include "empathy-debug.h"
55 static gboolean initialized = FALSE;
63 bindtextdomain (GETTEXT_PACKAGE, LOCALEDIR);
64 bind_textdomain_codeset (GETTEXT_PACKAGE, "UTF-8");
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);
70 empathy_debug_set_flags (g_getenv ("EMPATHY_DEBUG"));
71 tp_debug_divert_messages (g_getenv ("EMPATHY_LOGFILE"));
79 empathy_substring (const gchar *str,
83 return g_strndup (str + start, end - start);
87 empathy_strcasecmp (const gchar *s1,
90 return empathy_strncasecmp (s1, s2, -1);
94 empathy_strncasecmp (const gchar *s1,
101 u1 = g_utf8_casefold (s1, n);
102 u2 = g_utf8_casefold (s2, n);
104 ret_val = g_utf8_collate (u1, u2);
112 empathy_xml_validate (xmlDoc *doc,
113 const gchar *dtd_filename)
115 gchar *path, *escaped;
120 path = g_build_filename (g_getenv ("EMPATHY_SRCDIR"), "libempathy",
122 if (!g_file_test (path, G_FILE_TEST_EXISTS)) {
124 path = g_build_filename (DATADIR, "empathy", dtd_filename, NULL);
126 DEBUG ("Loading dtd file %s", path);
128 /* The list of valid chars is taken from libxml. */
129 escaped = xmlURIEscapeStr (path, ":@&=+$,/?;");
132 memset (&cvp, 0, sizeof (cvp));
133 dtd = xmlParseDTD (NULL, escaped);
134 ret = xmlValidateDtd (&cvp, doc, dtd);
143 empathy_xml_node_get_child (xmlNodePtr node,
144 const gchar *child_name)
148 g_return_val_if_fail (node != NULL, NULL);
149 g_return_val_if_fail (child_name != NULL, NULL);
151 for (l = node->children; l; l = l->next) {
152 if (l->name && strcmp (l->name, child_name) == 0) {
161 empathy_xml_node_get_child_content (xmlNodePtr node,
162 const gchar *child_name)
166 g_return_val_if_fail (node != NULL, NULL);
167 g_return_val_if_fail (child_name != NULL, NULL);
169 l = empathy_xml_node_get_child (node, child_name);
171 return xmlNodeGetContent (l);
178 empathy_xml_node_find_child_prop_value (xmlNodePtr node,
179 const gchar *prop_name,
180 const gchar *prop_value)
183 xmlNodePtr found = NULL;
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);
189 for (l = node->children; l && !found; l = l->next) {
192 if (!xmlHasProp (l, prop_name)) {
196 prop = xmlGetProp (l, prop_name);
197 if (prop && strcmp (prop, prop_value) == 0) {
208 empathy_account_hash (gconstpointer key)
210 g_return_val_if_fail (MC_IS_ACCOUNT (key), 0);
212 return g_str_hash (mc_account_get_unique_name (MC_ACCOUNT (key)));
216 empathy_account_equal (gconstpointer a,
222 g_return_val_if_fail (MC_IS_ACCOUNT (a), FALSE);
223 g_return_val_if_fail (MC_IS_ACCOUNT (b), FALSE);
225 name_a = mc_account_get_unique_name (MC_ACCOUNT (a));
226 name_b = mc_account_get_unique_name (MC_ACCOUNT (b));
228 return g_str_equal (name_a, name_b);
232 empathy_mission_control_dup_singleton (void)
234 static MissionControl *mc = NULL;
237 mc = mission_control_new (tp_get_bus ());
238 g_object_add_weak_pointer (G_OBJECT (mc), (gpointer) &mc);
247 empathy_presence_get_default_message (McPresence presence)
250 case MC_PRESENCE_AVAILABLE:
251 return _("Available");
252 case MC_PRESENCE_DO_NOT_DISTURB:
254 case MC_PRESENCE_AWAY:
255 case MC_PRESENCE_EXTENDED_AWAY:
257 case MC_PRESENCE_HIDDEN:
259 case MC_PRESENCE_OFFLINE:
260 case MC_PRESENCE_UNSET:
263 g_assert_not_reached ();
270 empathy_presence_to_str (McPresence presence)
273 case MC_PRESENCE_AVAILABLE:
275 case MC_PRESENCE_DO_NOT_DISTURB:
277 case MC_PRESENCE_AWAY:
279 case MC_PRESENCE_EXTENDED_AWAY:
281 case MC_PRESENCE_HIDDEN:
283 case MC_PRESENCE_OFFLINE:
285 case MC_PRESENCE_UNSET:
288 g_assert_not_reached ();
295 empathy_presence_from_str (const gchar *str)
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;
313 return MC_PRESENCE_UNSET;
317 empathy_file_lookup (const gchar *filename, const gchar *subdir)
325 path = g_build_filename (g_getenv ("EMPATHY_SRCDIR"), subdir, filename, NULL);
326 if (!g_file_test (path, G_FILE_TEST_EXISTS)) {
328 path = g_build_filename (DATADIR, "empathy", filename, NULL);
335 empathy_proxy_hash (gconstpointer key)
337 TpProxy *proxy = TP_PROXY (key);
338 TpProxyClass *proxy_class = TP_PROXY_GET_CLASS (key);
340 g_return_val_if_fail (TP_IS_PROXY (proxy), 0);
341 g_return_val_if_fail (proxy_class->must_have_unique_name, 0);
343 return g_str_hash (proxy->object_path) ^ g_str_hash (proxy->bus_name);
347 empathy_proxy_equal (gconstpointer a,
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);
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);
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);
365 empathy_check_available_state (void)
370 idle = empathy_idle_dup_singleton ();
371 presence = empathy_idle_get_state (idle);
372 g_object_unref (idle);
374 if (presence != MC_PRESENCE_AVAILABLE &&
375 presence != MC_PRESENCE_UNSET) {