2 * Copyright (C) 2003-2007 Imendio AB
3 * Copyright (C) 2007-2008 Collabora Ltd.
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License as
7 * published by the Free Software Foundation; either version 2 of the
8 * License, or (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * General Public License for more details.
15 * You should have received a copy of the GNU General Public
16 * License along with this program; if not, write to the
17 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
18 * Boston, MA 02110-1301 USA
20 * Authors: Richard Hult <richard@imendio.com>
21 * Martyn Russell <martyn@imendio.com>
22 * Xavier Claessens <xclaesse@gmail.com>
29 #include <sys/types.h>
31 #include <glib/gi18n-lib.h>
33 #include <libxml/uri.h>
35 #include <folks/folks.h>
36 #include <folks/folks-telepathy.h>
38 #include <telepathy-glib/account-manager.h>
39 #include <telepathy-glib/connection.h>
40 #include <telepathy-glib/channel.h>
41 #include <telepathy-glib/dbus.h>
42 #include <telepathy-glib/util.h>
44 #include "empathy-utils.h"
45 #include "empathy-contact-manager.h"
46 #include "empathy-individual-manager.h"
47 #include "empathy-dispatcher.h"
48 #include "empathy-idle.h"
49 #include "empathy-tp-call.h"
50 #include "empathy-tp-contact-factory.h"
52 #include <extensions/extensions.h>
54 #define DEBUG_FLAG EMPATHY_DEBUG_OTHER
55 #include "empathy-debug.h"
57 /* Translation between presence types and string */
60 TpConnectionPresenceType type;
61 } presence_types[] = {
62 { "available", TP_CONNECTION_PRESENCE_TYPE_AVAILABLE },
63 { "busy", TP_CONNECTION_PRESENCE_TYPE_BUSY },
64 { "away", TP_CONNECTION_PRESENCE_TYPE_AWAY },
65 { "ext_away", TP_CONNECTION_PRESENCE_TYPE_EXTENDED_AWAY },
66 { "hidden", TP_CONNECTION_PRESENCE_TYPE_HIDDEN },
67 { "offline", TP_CONNECTION_PRESENCE_TYPE_OFFLINE },
68 { "unset", TP_CONNECTION_PRESENCE_TYPE_UNSET },
69 { "unknown", TP_CONNECTION_PRESENCE_TYPE_UNKNOWN },
70 { "error", TP_CONNECTION_PRESENCE_TYPE_ERROR },
71 /* alternative names */
72 { "dnd", TP_CONNECTION_PRESENCE_TYPE_BUSY },
73 { "brb", TP_CONNECTION_PRESENCE_TYPE_AWAY },
74 { "xa", TP_CONNECTION_PRESENCE_TYPE_EXTENDED_AWAY },
83 static gboolean initialized = FALSE;
91 bindtextdomain (GETTEXT_PACKAGE, LOCALEDIR);
92 bind_textdomain_codeset (GETTEXT_PACKAGE, "UTF-8");
94 /* Setup debug output for empathy and telepathy-glib */
95 if (g_getenv ("EMPATHY_TIMING") != NULL) {
96 g_log_set_default_handler (tp_debug_timestamped_log_handler, NULL);
98 empathy_debug_set_flags (g_getenv ("EMPATHY_DEBUG"));
99 tp_debug_divert_messages (g_getenv ("EMPATHY_LOGFILE"));
107 empathy_substring (const gchar *str,
111 return g_strndup (str + start, end - start);
115 empathy_strcasecmp (const gchar *s1,
118 return empathy_strncasecmp (s1, s2, -1);
122 empathy_strncasecmp (const gchar *s1,
129 u1 = g_utf8_casefold (s1, n);
130 u2 = g_utf8_casefold (s2, n);
132 ret_val = g_utf8_collate (u1, u2);
140 empathy_xml_validate (xmlDoc *doc,
141 const gchar *dtd_filename)
149 path = g_build_filename (g_getenv ("EMPATHY_SRCDIR"), "libempathy",
151 if (!g_file_test (path, G_FILE_TEST_EXISTS)) {
153 path = g_build_filename (DATADIR, "empathy", dtd_filename, NULL);
155 DEBUG ("Loading dtd file %s", path);
157 /* The list of valid chars is taken from libxml. */
158 escaped = xmlURIEscapeStr ((const xmlChar *) path,
159 (const xmlChar *)":@&=+$,/?;");
162 memset (&cvp, 0, sizeof (cvp));
163 dtd = xmlParseDTD (NULL, escaped);
164 ret = xmlValidateDtd (&cvp, doc, dtd);
173 empathy_xml_node_get_child (xmlNodePtr node,
174 const gchar *child_name)
178 g_return_val_if_fail (node != NULL, NULL);
179 g_return_val_if_fail (child_name != NULL, NULL);
181 for (l = node->children; l; l = l->next) {
182 if (l->name && strcmp ((const gchar *) l->name, child_name) == 0) {
191 empathy_xml_node_get_child_content (xmlNodePtr node,
192 const gchar *child_name)
196 g_return_val_if_fail (node != NULL, NULL);
197 g_return_val_if_fail (child_name != NULL, NULL);
199 l = empathy_xml_node_get_child (node, child_name);
201 return xmlNodeGetContent (l);
208 empathy_xml_node_find_child_prop_value (xmlNodePtr node,
209 const gchar *prop_name,
210 const gchar *prop_value)
213 xmlNodePtr found = NULL;
215 g_return_val_if_fail (node != NULL, NULL);
216 g_return_val_if_fail (prop_name != NULL, NULL);
217 g_return_val_if_fail (prop_value != NULL, NULL);
219 for (l = node->children; l && !found; l = l->next) {
222 if (!xmlHasProp (l, (const xmlChar *) prop_name)) {
226 prop = xmlGetProp (l, (const xmlChar *) prop_name);
227 if (prop && strcmp ((const gchar *) prop, prop_value) == 0) {
238 empathy_presence_get_default_message (TpConnectionPresenceType presence)
241 case TP_CONNECTION_PRESENCE_TYPE_AVAILABLE:
242 return _("Available");
243 case TP_CONNECTION_PRESENCE_TYPE_BUSY:
245 case TP_CONNECTION_PRESENCE_TYPE_AWAY:
246 case TP_CONNECTION_PRESENCE_TYPE_EXTENDED_AWAY:
248 case TP_CONNECTION_PRESENCE_TYPE_HIDDEN:
249 return _("Invisible");
250 case TP_CONNECTION_PRESENCE_TYPE_OFFLINE:
252 case TP_CONNECTION_PRESENCE_TYPE_UNKNOWN:
254 case TP_CONNECTION_PRESENCE_TYPE_UNSET:
255 case TP_CONNECTION_PRESENCE_TYPE_ERROR:
264 empathy_presence_to_str (TpConnectionPresenceType presence)
268 for (i = 0 ; presence_types[i].name != NULL; i++)
269 if (presence == presence_types[i].type)
270 return presence_types[i].name;
275 TpConnectionPresenceType
276 empathy_presence_from_str (const gchar *str)
280 for (i = 0 ; presence_types[i].name != NULL; i++)
281 if (!tp_strdiff (str, presence_types[i].name))
282 return presence_types[i].type;
284 return TP_CONNECTION_PRESENCE_TYPE_UNSET;
288 empathy_status_reason_get_default_message (TpConnectionStatusReason reason)
291 case TP_CONNECTION_STATUS_REASON_NONE_SPECIFIED:
292 return _("No reason specified");
293 case TP_CONNECTION_STATUS_REASON_REQUESTED:
294 return _("Status is set to offline");
295 case TP_CONNECTION_STATUS_REASON_NETWORK_ERROR:
296 return _("Network error");
297 case TP_CONNECTION_STATUS_REASON_AUTHENTICATION_FAILED:
298 return _("Authentication failed");
299 case TP_CONNECTION_STATUS_REASON_ENCRYPTION_ERROR:
300 return _("Encryption error");
301 case TP_CONNECTION_STATUS_REASON_NAME_IN_USE:
302 return _("Name in use");
303 case TP_CONNECTION_STATUS_REASON_CERT_NOT_PROVIDED:
304 return _("Certificate not provided");
305 case TP_CONNECTION_STATUS_REASON_CERT_UNTRUSTED:
306 return _("Certificate untrusted");
307 case TP_CONNECTION_STATUS_REASON_CERT_EXPIRED:
308 return _("Certificate expired");
309 case TP_CONNECTION_STATUS_REASON_CERT_NOT_ACTIVATED:
310 return _("Certificate not activated");
311 case TP_CONNECTION_STATUS_REASON_CERT_HOSTNAME_MISMATCH:
312 return _("Certificate hostname mismatch");
313 case TP_CONNECTION_STATUS_REASON_CERT_FINGERPRINT_MISMATCH:
314 return _("Certificate fingerprint mismatch");
315 case TP_CONNECTION_STATUS_REASON_CERT_SELF_SIGNED:
316 return _("Certificate self-signed");
317 case TP_CONNECTION_STATUS_REASON_CERT_OTHER_ERROR:
318 return _("Certificate error");
320 return _("Unknown reason");
325 create_errors_to_message_hash (void)
329 errors = g_hash_table_new (g_str_hash, g_str_equal);
330 g_hash_table_insert (errors, TP_ERROR_STR_NETWORK_ERROR, _("Network error"));
331 g_hash_table_insert (errors, TP_ERROR_STR_AUTHENTICATION_FAILED,
332 _("Authentication failed"));
333 g_hash_table_insert (errors, TP_ERROR_STR_ENCRYPTION_ERROR,
334 _("Encryption error"));
335 g_hash_table_insert (errors, TP_ERROR_STR_CERT_NOT_PROVIDED,
336 _("Certificate not provided"));
337 g_hash_table_insert (errors, TP_ERROR_STR_CERT_UNTRUSTED,
338 _("Certificate untrusted"));
339 g_hash_table_insert (errors, TP_ERROR_STR_CERT_EXPIRED,
340 _("Certificate expired"));
341 g_hash_table_insert (errors, TP_ERROR_STR_CERT_NOT_ACTIVATED,
342 _("Certificate not activated"));
343 g_hash_table_insert (errors, TP_ERROR_STR_CERT_HOSTNAME_MISMATCH,
344 _("Certificate hostname mismatch"));
345 g_hash_table_insert (errors, TP_ERROR_STR_CERT_FINGERPRINT_MISMATCH,
346 _("Certificate fingerprint mismatch"));
347 g_hash_table_insert (errors, TP_ERROR_STR_CERT_SELF_SIGNED,
348 _("Certificate self-signed"));
349 g_hash_table_insert (errors, TP_ERROR_STR_CANCELLED,
350 _("Status is set to offline"));
351 g_hash_table_insert (errors, TP_ERROR_STR_ENCRYPTION_NOT_AVAILABLE,
352 _("Encryption is not available"));
353 g_hash_table_insert (errors, TP_ERROR_STR_CERT_INVALID,
354 _("Certificate is invalid"));
355 g_hash_table_insert (errors, TP_ERROR_STR_CONNECTION_REFUSED,
356 _("Connection has been refused"));
357 g_hash_table_insert (errors, TP_ERROR_STR_CONNECTION_FAILED,
358 _("Connection can't be established"));
359 g_hash_table_insert (errors, TP_ERROR_STR_CONNECTION_LOST,
360 _("Connection has been lost"));
361 g_hash_table_insert (errors, TP_ERROR_STR_ALREADY_CONNECTED,
362 _("This resource is already connected to the server"));
363 g_hash_table_insert (errors, TP_ERROR_STR_CONNECTION_REPLACED,
364 _("Connection has been replaced by a new connection using the "
366 g_hash_table_insert (errors, TP_ERROR_STR_REGISTRATION_EXISTS,
367 _("The account already exists on the server"));
368 g_hash_table_insert (errors, TP_ERROR_STR_SERVICE_BUSY,
369 _("Server is currently too busy to handle the connection"));
370 g_hash_table_insert (errors, TP_ERROR_STR_CERT_REVOKED,
371 _("Certificate has been revoked"));
372 g_hash_table_insert (errors, TP_ERROR_STR_CERT_INSECURE,
373 _("Certificate uses an insecure cipher algorithm or is "
374 "cryptographically weak"));
375 g_hash_table_insert (errors, TP_ERROR_STR_CERT_LIMIT_EXCEEDED,
376 _("The length of the server certificate, or the depth of the "
377 "server certificate chain, exceed the limits imposed by the "
378 "cryptography library"));
384 empathy_dbus_error_name_get_default_message (const gchar *error)
386 static GHashTable *errors_to_message = NULL;
391 if (G_UNLIKELY (errors_to_message == NULL)) {
392 errors_to_message = create_errors_to_message_hash ();
395 return g_hash_table_lookup (errors_to_message, error);
399 empathy_account_get_error_message (TpAccount *account,
400 gboolean *user_requested)
402 const gchar *dbus_error;
403 const gchar *message;
404 const GHashTable *details = NULL;
405 TpConnectionStatusReason reason;
407 dbus_error = tp_account_get_detailed_error (account, &details);
409 if (user_requested != NULL)
411 if (tp_asv_get_boolean (details, "user-requested", NULL))
412 *user_requested = TRUE;
414 *user_requested = FALSE;
417 message = empathy_dbus_error_name_get_default_message (dbus_error);
421 DEBUG ("Don't understand error '%s'; fallback to the status reason (%u)",
424 tp_account_get_connection_status (account, &reason);
426 return empathy_status_reason_get_default_message (reason);
430 empathy_file_lookup (const gchar *filename, const gchar *subdir)
438 path = g_build_filename (g_getenv ("EMPATHY_SRCDIR"), subdir, filename, NULL);
439 if (!g_file_test (path, G_FILE_TEST_EXISTS)) {
441 path = g_build_filename (DATADIR, "empathy", filename, NULL);
448 empathy_proxy_hash (gconstpointer key)
450 TpProxy *proxy = TP_PROXY (key);
451 TpProxyClass *proxy_class = TP_PROXY_GET_CLASS (key);
453 g_return_val_if_fail (TP_IS_PROXY (proxy), 0);
454 g_return_val_if_fail (proxy_class->must_have_unique_name, 0);
456 return g_str_hash (proxy->object_path) ^ g_str_hash (proxy->bus_name);
460 empathy_proxy_equal (gconstpointer a,
463 TpProxy *proxy_a = TP_PROXY (a);
464 TpProxy *proxy_b = TP_PROXY (b);
465 TpProxyClass *proxy_a_class = TP_PROXY_GET_CLASS (a);
466 TpProxyClass *proxy_b_class = TP_PROXY_GET_CLASS (b);
468 g_return_val_if_fail (TP_IS_PROXY (proxy_a), FALSE);
469 g_return_val_if_fail (TP_IS_PROXY (proxy_b), FALSE);
470 g_return_val_if_fail (proxy_a_class->must_have_unique_name, 0);
471 g_return_val_if_fail (proxy_b_class->must_have_unique_name, 0);
473 return g_str_equal (proxy_a->object_path, proxy_b->object_path) &&
474 g_str_equal (proxy_a->bus_name, proxy_b->bus_name);
478 empathy_check_available_state (void)
480 TpConnectionPresenceType presence;
483 idle = empathy_idle_dup_singleton ();
484 presence = empathy_idle_get_state (idle);
485 g_object_unref (idle);
487 if (presence != TP_CONNECTION_PRESENCE_TYPE_AVAILABLE &&
488 presence != TP_CONNECTION_PRESENCE_TYPE_UNSET) {
496 empathy_uint_compare (gconstpointer a,
499 return *(guint *) a - *(guint *) b;
503 empathy_protocol_icon_name (const gchar *protocol)
505 if (!tp_strdiff (protocol, "yahoojp"))
506 /* Yahoo Japan uses the same icon as Yahoo */
508 else if (!tp_strdiff (protocol, "simple"))
509 /* SIMPLE uses the same icon as SIP */
511 else if (!tp_strdiff (protocol, "sms"))
512 return g_strdup ("phone");
514 return g_strdup_printf ("im-%s", protocol);
518 empathy_type_dbus_ao (void)
522 if (G_UNLIKELY (t == 0))
523 t = dbus_g_type_get_collection ("GPtrArray", DBUS_TYPE_G_OBJECT_PATH);
529 empathy_protocol_name_to_display_name (const gchar *proto_name)
534 const gchar *display;
537 { "jabber", "Jabber", FALSE },
538 { "gtalk", "Google Talk", FALSE },
539 { "msn", "MSN", FALSE, },
540 { "local-xmpp", N_("People Nearby"), TRUE },
541 { "irc", "IRC", FALSE },
542 { "icq", "ICQ", FALSE },
543 { "aim", "AIM", FALSE },
544 { "yahoo", "Yahoo!", FALSE },
545 { "yahoojp", N_("Yahoo! Japan"), TRUE },
546 { "facebook", N_("Facebook Chat"), TRUE },
547 { "groupwise", "GroupWise", FALSE },
548 { "sip", "SIP", FALSE },
552 for (i = 0; names[i].proto != NULL; i++)
554 if (!tp_strdiff (proto_name, names[i].proto))
556 if (names[i].translated)
557 return _(names[i].display);
559 return names[i].display;
566 /* Note: this function depends on the account manager having its core feature
569 empathy_get_account_for_connection (TpConnection *connection)
571 TpAccountManager *manager;
572 TpAccount *account = NULL;
575 manager = tp_account_manager_dup ();
577 accounts = tp_account_manager_get_valid_accounts (manager);
579 for (l = accounts; l != NULL; l = l->next)
581 TpAccount *a = l->data;
583 if (tp_account_get_connection (a) == connection)
590 g_list_free (accounts);
591 g_object_unref (manager);
597 empathy_account_manager_get_accounts_connected (gboolean *connecting)
599 TpAccountManager *manager;
601 gboolean out_connecting = FALSE;
602 gboolean out_connected = FALSE;
604 manager = tp_account_manager_dup ();
606 if (G_UNLIKELY (!tp_account_manager_is_prepared (manager,
607 TP_ACCOUNT_MANAGER_FEATURE_CORE)))
608 g_critical (G_STRLOC ": %s called before AccountManager ready", G_STRFUNC);
610 accounts = tp_account_manager_get_valid_accounts (manager);
612 for (l = accounts; l != NULL; l = l->next)
614 TpConnectionStatus s = tp_account_get_connection_status (
615 TP_ACCOUNT (l->data), NULL);
617 if (s == TP_CONNECTION_STATUS_CONNECTING)
618 out_connecting = TRUE;
619 else if (s == TP_CONNECTION_STATUS_CONNECTED)
620 out_connected = TRUE;
622 if (out_connecting && out_connected)
626 g_list_free (accounts);
627 g_object_unref (manager);
629 if (connecting != NULL)
630 *connecting = out_connecting;
632 return out_connected;
635 /* Change the RequestedPresence of a newly created account to ensure that it
636 * is actually connected. */
638 empathy_connect_new_account (TpAccount *account,
639 TpAccountManager *account_manager)
641 TpConnectionPresenceType presence;
642 gchar *status, *message;
644 /* only force presence if presence was offline, unknown or unset */
645 presence = tp_account_get_requested_presence (account, NULL, NULL);
648 case TP_CONNECTION_PRESENCE_TYPE_OFFLINE:
649 case TP_CONNECTION_PRESENCE_TYPE_UNKNOWN:
650 case TP_CONNECTION_PRESENCE_TYPE_UNSET:
651 presence = tp_account_manager_get_most_available_presence (
652 account_manager, &status, &message);
654 if (presence == TP_CONNECTION_PRESENCE_TYPE_OFFLINE)
655 /* Global presence is offline; we force it so user doesn't have to
656 * manually change the presence to connect his new account. */
657 presence = TP_CONNECTION_PRESENCE_TYPE_AVAILABLE;
659 tp_account_request_presence_async (account, presence,
660 status, NULL, NULL, NULL);
666 case TP_CONNECTION_PRESENCE_TYPE_AVAILABLE:
667 case TP_CONNECTION_PRESENCE_TYPE_AWAY:
668 case TP_CONNECTION_PRESENCE_TYPE_EXTENDED_AWAY:
669 case TP_CONNECTION_PRESENCE_TYPE_HIDDEN:
670 case TP_CONNECTION_PRESENCE_TYPE_BUSY:
671 case TP_CONNECTION_PRESENCE_TYPE_ERROR:
673 /* do nothing if the presence is not offline */
678 /* Translate Folks' general presence type to the Tp presence type */
679 TpConnectionPresenceType
680 empathy_folks_presence_type_to_tp (FolksPresenceType type)
682 return (TpConnectionPresenceType) type;
685 /* Returns TRUE if the given Individual contains a TpContact */
687 empathy_folks_individual_contains_contact (FolksIndividual *individual)
691 g_return_val_if_fail (FOLKS_IS_INDIVIDUAL (individual), FALSE);
693 personas = folks_individual_get_personas (individual);
694 for (l = personas; l != NULL; l = l->next)
696 if (TPF_IS_PERSONA (l->data))
697 return (tpf_persona_get_contact (TPF_PERSONA (l->data)) != NULL);
703 /* TODO: this needs to be eliminated (and replaced in some cases with user
704 * prompts) when we break the assumption that FolksIndividuals are 1:1 with
707 /* Retrieve the EmpathyContact corresponding to the first TpContact contained
708 * within the given Individual. Note that this is a temporary convenience. See
711 empathy_contact_dup_from_folks_individual (FolksIndividual *individual)
714 EmpathyContact *contact = NULL;
716 g_return_val_if_fail (FOLKS_IS_INDIVIDUAL (individual), NULL);
718 personas = folks_individual_get_personas (individual);
719 for (l = personas; (l != NULL) && (contact == NULL); l = l->next)
721 TpfPersona *persona = l->data;
723 if (TPF_IS_PERSONA (persona))
725 TpContact *tp_contact;
727 tp_contact = tpf_persona_get_contact (persona);
728 contact = empathy_contact_dup_from_tp_contact (tp_contact);
729 empathy_contact_set_persona (contact, FOLKS_PERSONA (persona));
736 TpChannelGroupChangeReason
737 tp_chanel_group_change_reason_from_folks_groups_change_reason (
738 FolksGroupsChangeReason reason)
740 return (TpChannelGroupChangeReason) reason;