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.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"
43 #define DEBUG_FLAG EMPATHY_DEBUG_OTHER
44 #include "empathy-debug.h"
49 static gboolean initialized = FALSE;
54 /* Setup glib. Threads are required for async GIO. */
59 bindtextdomain (GETTEXT_PACKAGE, LOCALEDIR);
60 bind_textdomain_codeset (GETTEXT_PACKAGE, "UTF-8");
61 textdomain (GETTEXT_PACKAGE);
63 /* Setup debug output for empathy and telepathy-glib */
64 if (g_getenv ("EMPATHY_TIMING") != NULL) {
65 g_log_set_default_handler (tp_debug_timestamped_log_handler, NULL);
67 empathy_debug_set_flags (g_getenv ("EMPATHY_DEBUG"));
68 tp_debug_divert_messages (g_getenv ("EMPATHY_LOGFILE"));
74 empathy_substring (const gchar *str,
78 return g_strndup (str + start, end - start);
82 empathy_strcasecmp (const gchar *s1,
85 return empathy_strncasecmp (s1, s2, -1);
89 empathy_strncasecmp (const gchar *s1,
96 u1 = g_utf8_casefold (s1, n);
97 u2 = g_utf8_casefold (s2, n);
99 ret_val = g_utf8_collate (u1, u2);
107 empathy_xml_validate (xmlDoc *doc,
108 const gchar *dtd_filename)
110 gchar *path, *escaped;
115 path = g_build_filename (g_getenv ("EMPATHY_SRCDIR"), "libempathy",
117 if (!g_file_test (path, G_FILE_TEST_EXISTS)) {
119 path = g_build_filename (DATADIR, "empathy", dtd_filename, NULL);
121 DEBUG ("Loading dtd file %s", path);
123 /* The list of valid chars is taken from libxml. */
124 escaped = xmlURIEscapeStr (path, ":@&=+$,/?;");
127 memset (&cvp, 0, sizeof (cvp));
128 dtd = xmlParseDTD (NULL, escaped);
129 ret = xmlValidateDtd (&cvp, doc, dtd);
138 empathy_xml_node_get_child (xmlNodePtr node,
139 const gchar *child_name)
143 g_return_val_if_fail (node != NULL, NULL);
144 g_return_val_if_fail (child_name != NULL, NULL);
146 for (l = node->children; l; l = l->next) {
147 if (l->name && strcmp (l->name, child_name) == 0) {
156 empathy_xml_node_get_child_content (xmlNodePtr node,
157 const gchar *child_name)
161 g_return_val_if_fail (node != NULL, NULL);
162 g_return_val_if_fail (child_name != NULL, NULL);
164 l = empathy_xml_node_get_child (node, child_name);
166 return xmlNodeGetContent (l);
173 empathy_xml_node_find_child_prop_value (xmlNodePtr node,
174 const gchar *prop_name,
175 const gchar *prop_value)
178 xmlNodePtr found = NULL;
180 g_return_val_if_fail (node != NULL, NULL);
181 g_return_val_if_fail (prop_name != NULL, NULL);
182 g_return_val_if_fail (prop_value != NULL, NULL);
184 for (l = node->children; l && !found; l = l->next) {
187 if (!xmlHasProp (l, prop_name)) {
191 prop = xmlGetProp (l, prop_name);
192 if (prop && strcmp (prop, prop_value) == 0) {
203 empathy_account_hash (gconstpointer key)
205 g_return_val_if_fail (MC_IS_ACCOUNT (key), 0);
207 return g_str_hash (mc_account_get_unique_name (MC_ACCOUNT (key)));
211 empathy_account_equal (gconstpointer a,
217 g_return_val_if_fail (MC_IS_ACCOUNT (a), FALSE);
218 g_return_val_if_fail (MC_IS_ACCOUNT (b), FALSE);
220 name_a = mc_account_get_unique_name (MC_ACCOUNT (a));
221 name_b = mc_account_get_unique_name (MC_ACCOUNT (b));
223 return g_str_equal (name_a, name_b);
227 empathy_mission_control_new (void)
229 static MissionControl *mc = NULL;
232 mc = mission_control_new (tp_get_bus ());
233 g_object_add_weak_pointer (G_OBJECT (mc), (gpointer) &mc);
242 empathy_presence_get_default_message (McPresence presence)
245 case MC_PRESENCE_AVAILABLE:
246 return _("Available");
247 case MC_PRESENCE_DO_NOT_DISTURB:
249 case MC_PRESENCE_AWAY:
250 case MC_PRESENCE_EXTENDED_AWAY:
252 case MC_PRESENCE_HIDDEN:
254 case MC_PRESENCE_OFFLINE:
255 case MC_PRESENCE_UNSET:
258 g_assert_not_reached ();
265 empathy_presence_to_str (McPresence presence)
268 case MC_PRESENCE_AVAILABLE:
270 case MC_PRESENCE_DO_NOT_DISTURB:
272 case MC_PRESENCE_AWAY:
274 case MC_PRESENCE_EXTENDED_AWAY:
276 case MC_PRESENCE_HIDDEN:
278 case MC_PRESENCE_OFFLINE:
280 case MC_PRESENCE_UNSET:
283 g_assert_not_reached ();
290 empathy_presence_from_str (const gchar *str)
292 if (strcmp (str, "available") == 0) {
293 return MC_PRESENCE_AVAILABLE;
294 } else if ((strcmp (str, "dnd") == 0) || (strcmp (str, "busy") == 0)) {
295 return MC_PRESENCE_DO_NOT_DISTURB;
296 } else if ((strcmp (str, "away") == 0) || (strcmp (str, "brb") == 0)) {
297 return MC_PRESENCE_AWAY;
298 } else if ((strcmp (str, "xa") == 0) || (strcmp (str, "ext_away") == 0)) {
299 return MC_PRESENCE_EXTENDED_AWAY;
300 } else if (strcmp (str, "hidden") == 0) {
301 return MC_PRESENCE_HIDDEN;
302 } else if (strcmp (str, "offline") == 0) {
303 return MC_PRESENCE_OFFLINE;
304 } else if (strcmp (str, "unset") == 0) {
305 return MC_PRESENCE_UNSET;
308 return MC_PRESENCE_UNSET;
312 empathy_file_lookup (const gchar *filename, const gchar *subdir)
320 path = g_build_filename (g_getenv ("EMPATHY_SRCDIR"), subdir, filename, NULL);
321 if (!g_file_test (path, G_FILE_TEST_EXISTS)) {
323 path = g_build_filename (DATADIR, "empathy", filename, NULL);
330 EmpathyRunUntilReadyFunc func;
337 run_until_ready_cb (RunUntilReadyData *data)
339 if (!data->func || data->func (data->object, data->user_data)) {
340 DEBUG ("Object %p is ready", data->object);
341 g_main_loop_quit (data->loop);
346 object_is_ready (GObject *object,
351 g_object_get (object, "ready", &ready, NULL);
357 empathy_run_until_ready_full (gpointer object,
359 EmpathyRunUntilReadyFunc func,
363 RunUntilReadyData data;
366 g_return_if_fail (G_IS_OBJECT (object));
367 g_return_if_fail (signal != NULL);
369 if (func && func (object, user_data)) {
373 DEBUG ("Starting run until ready for object %p", object);
376 data.user_data = user_data;
377 data.object = object;
378 data.loop = g_main_loop_new (NULL, FALSE);
380 signal_id = g_signal_connect_swapped (object, signal,
381 G_CALLBACK (run_until_ready_cb),
387 g_main_loop_run (data.loop);
393 g_signal_handler_disconnect (object, signal_id);
394 g_main_loop_unref (data.loop);
398 empathy_run_until_ready (gpointer object)
400 empathy_run_until_ready_full (object, "notify::ready", object_is_ready,
405 empathy_channel_get_account (TpChannel *channel)
407 TpConnection *connection;
411 g_object_get (channel, "connection", &connection, NULL);
412 mc = empathy_mission_control_new ();
413 account = mission_control_get_account_for_tpconnection (mc, connection, NULL);
414 g_object_unref (connection);
420 typedef void (*AccountStatusChangedFunc) (MissionControl *mc,
421 TpConnectionStatus status,
423 TpConnectionStatusReason reason,
424 const gchar *unique_name,
428 AccountStatusChangedFunc handler;
430 GClosureNotify free_func;
432 } AccountStatusChangedData;
435 TpConnectionStatus status;
437 TpConnectionStatusReason reason;
439 AccountStatusChangedData *data;
443 account_status_changed_data_free (gpointer ptr,
446 AccountStatusChangedData *data = ptr;
448 if (data->free_func) {
449 data->free_func (data->user_data, closure);
451 g_object_unref (data->mc);
452 g_slice_free (AccountStatusChangedData, data);
456 account_status_changed_invoke_callback (gpointer data)
458 InvocationData *invocation_data = data;
460 invocation_data->data->handler (invocation_data->data->mc,
461 invocation_data->status,
462 invocation_data->presence,
463 invocation_data->reason,
464 invocation_data->unique_name,
465 invocation_data->data->user_data);
467 g_free (invocation_data->unique_name);
468 g_slice_free (InvocationData, invocation_data);
474 account_status_changed_cb (MissionControl *mc,
475 TpConnectionStatus status,
477 TpConnectionStatusReason reason,
478 const gchar *unique_name,
479 AccountStatusChangedData *data)
481 InvocationData *invocation_data;
483 invocation_data = g_slice_new (InvocationData);
484 invocation_data->status = status;
485 invocation_data->presence = presence;
486 invocation_data->reason = reason;
487 invocation_data->unique_name = g_strdup (unique_name);
488 invocation_data->data = data;
490 g_idle_add_full (G_PRIORITY_HIGH,
491 account_status_changed_invoke_callback,
492 invocation_data, NULL);
496 empathy_connect_to_account_status_changed (MissionControl *mc,
499 GClosureNotify free_func)
501 AccountStatusChangedData *data;
503 g_return_val_if_fail (IS_MISSIONCONTROL (mc), NULL);
504 g_return_val_if_fail (handler != NULL, NULL);
506 data = g_slice_new (AccountStatusChangedData);
507 data->handler = (AccountStatusChangedFunc) handler;
508 data->user_data = user_data;
509 data->free_func = free_func;
510 data->mc = g_object_ref (mc);
512 dbus_g_proxy_connect_signal (DBUS_G_PROXY (mc), "AccountStatusChanged",
513 G_CALLBACK (account_status_changed_cb),
514 data, account_status_changed_data_free);
520 empathy_disconnect_account_status_changed (gpointer token)
522 AccountStatusChangedData *data = token;
524 dbus_g_proxy_disconnect_signal (DBUS_G_PROXY (data->mc),
525 "AccountStatusChanged",
526 G_CALLBACK (account_status_changed_cb),
531 empathy_proxy_hash (gconstpointer key)
533 TpProxy *proxy = TP_PROXY (key);
534 TpProxyClass *proxy_class = TP_PROXY_GET_CLASS (key);
536 g_return_val_if_fail (TP_IS_PROXY (proxy), 0);
537 g_return_val_if_fail (proxy_class->must_have_unique_name, 0);
539 return g_str_hash (proxy->object_path) ^ g_str_hash (proxy->bus_name);
543 empathy_proxy_equal (gconstpointer a,
546 TpProxy *proxy_a = TP_PROXY (a);
547 TpProxy *proxy_b = TP_PROXY (b);
548 TpProxyClass *proxy_a_class = TP_PROXY_GET_CLASS (a);
549 TpProxyClass *proxy_b_class = TP_PROXY_GET_CLASS (b);
551 g_return_val_if_fail (TP_IS_PROXY (proxy_a), FALSE);
552 g_return_val_if_fail (TP_IS_PROXY (proxy_b), FALSE);
553 g_return_val_if_fail (proxy_a_class->must_have_unique_name, 0);
554 g_return_val_if_fail (proxy_b_class->must_have_unique_name, 0);
556 return g_str_equal (proxy_a->object_path, proxy_b->object_path) &&
557 g_str_equal (proxy_a->bus_name, proxy_b->bus_name);
564 empathy_connection_callback_for_request_channel callback;
566 GDestroyNotify destroy;
568 gboolean suppress_handler;
570 } ConnectionRequestChannelData;
573 connection_request_channel_data_unref (gpointer user_data)
575 ConnectionRequestChannelData *data = (ConnectionRequestChannelData*) user_data;
577 if (--data->ref_count == 0) {
578 g_free (data->channel_type);
580 data->destroy (data->user_data);
582 g_slice_free (ConnectionRequestChannelData, data);
587 connection_request_channel_cb (TpConnection *connection,
588 const gchar *object_path,
591 GObject *weak_object)
593 ConnectionRequestChannelData *data = (ConnectionRequestChannelData*) user_data;
596 if (!data->callback) {
601 data->callback (connection, NULL, error, data->user_data, weak_object);
605 channel = tp_channel_new (connection, object_path,
610 data->callback (connection, channel, NULL, data->user_data, weak_object);
611 g_object_unref (channel);
615 connection_request_handles_cb (TpConnection *connection,
616 const GArray *handles,
619 GObject *weak_object)
621 ConnectionRequestChannelData *data = (ConnectionRequestChannelData*) user_data;
624 if (data->callback) {
625 data->callback (connection, NULL, error, data->user_data, weak_object);
630 data->handle = g_array_index (handles, guint, 0);
632 tp_cli_connection_call_request_channel (connection, data->timeout_ms,
636 data->suppress_handler,
637 connection_request_channel_cb,
639 (GDestroyNotify) connection_request_channel_data_unref,
644 empathy_connection_request_channel (TpConnection *connection,
646 const gchar *channel_type,
649 gboolean suppress_handler,
650 empathy_connection_callback_for_request_channel callback,
652 GDestroyNotify destroy,
653 GObject *weak_object)
655 const gchar *names[] = {name, NULL};
656 ConnectionRequestChannelData *data;
658 data = g_slice_new (ConnectionRequestChannelData);
659 data->timeout_ms = timeout_ms;
660 data->channel_type = g_strdup (channel_type);
661 data->handle_type = handle_type;
662 data->callback = callback;
663 data->user_data = user_data;
664 data->destroy = destroy;
666 data->suppress_handler = suppress_handler;
668 tp_cli_connection_call_request_handles (connection,
672 connection_request_handles_cb,
674 (GDestroyNotify) connection_request_channel_data_unref,