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-dispatch-operation.h"
49 #include "empathy-idle.h"
50 #include "empathy-tp-call.h"
51 #include "empathy-tp-contact-factory.h"
53 #include <extensions/extensions.h>
55 #define DEBUG_FLAG EMPATHY_DEBUG_OTHER
56 #include "empathy-debug.h"
58 /* Translation between presence types and string */
61 TpConnectionPresenceType type;
62 } presence_types[] = {
63 { "available", TP_CONNECTION_PRESENCE_TYPE_AVAILABLE },
64 { "busy", TP_CONNECTION_PRESENCE_TYPE_BUSY },
65 { "away", TP_CONNECTION_PRESENCE_TYPE_AWAY },
66 { "ext_away", TP_CONNECTION_PRESENCE_TYPE_EXTENDED_AWAY },
67 { "hidden", TP_CONNECTION_PRESENCE_TYPE_HIDDEN },
68 { "offline", TP_CONNECTION_PRESENCE_TYPE_OFFLINE },
69 { "unset", TP_CONNECTION_PRESENCE_TYPE_UNSET },
70 { "unknown", TP_CONNECTION_PRESENCE_TYPE_UNKNOWN },
71 { "error", TP_CONNECTION_PRESENCE_TYPE_ERROR },
72 /* alternative names */
73 { "dnd", TP_CONNECTION_PRESENCE_TYPE_BUSY },
74 { "brb", TP_CONNECTION_PRESENCE_TYPE_AWAY },
75 { "xa", TP_CONNECTION_PRESENCE_TYPE_EXTENDED_AWAY },
84 static gboolean initialized = FALSE;
92 bindtextdomain (GETTEXT_PACKAGE, LOCALEDIR);
93 bind_textdomain_codeset (GETTEXT_PACKAGE, "UTF-8");
95 /* Setup debug output for empathy and telepathy-glib */
96 if (g_getenv ("EMPATHY_TIMING") != NULL) {
97 g_log_set_default_handler (tp_debug_timestamped_log_handler, NULL);
99 empathy_debug_set_flags (g_getenv ("EMPATHY_DEBUG"));
100 tp_debug_divert_messages (g_getenv ("EMPATHY_LOGFILE"));
108 empathy_substring (const gchar *str,
112 return g_strndup (str + start, end - start);
116 empathy_strcasecmp (const gchar *s1,
119 return empathy_strncasecmp (s1, s2, -1);
123 empathy_strncasecmp (const gchar *s1,
130 u1 = g_utf8_casefold (s1, n);
131 u2 = g_utf8_casefold (s2, n);
133 ret_val = g_utf8_collate (u1, u2);
141 empathy_xml_validate (xmlDoc *doc,
142 const gchar *dtd_filename)
150 path = g_build_filename (g_getenv ("EMPATHY_SRCDIR"), "libempathy",
152 if (!g_file_test (path, G_FILE_TEST_EXISTS)) {
154 path = g_build_filename (DATADIR, "empathy", dtd_filename, NULL);
156 DEBUG ("Loading dtd file %s", path);
158 /* The list of valid chars is taken from libxml. */
159 escaped = xmlURIEscapeStr ((const xmlChar *) path,
160 (const xmlChar *)":@&=+$,/?;");
163 memset (&cvp, 0, sizeof (cvp));
164 dtd = xmlParseDTD (NULL, escaped);
165 ret = xmlValidateDtd (&cvp, doc, dtd);
174 empathy_xml_node_get_child (xmlNodePtr node,
175 const gchar *child_name)
179 g_return_val_if_fail (node != NULL, NULL);
180 g_return_val_if_fail (child_name != NULL, NULL);
182 for (l = node->children; l; l = l->next) {
183 if (l->name && strcmp ((const gchar *) l->name, child_name) == 0) {
192 empathy_xml_node_get_child_content (xmlNodePtr node,
193 const gchar *child_name)
197 g_return_val_if_fail (node != NULL, NULL);
198 g_return_val_if_fail (child_name != NULL, NULL);
200 l = empathy_xml_node_get_child (node, child_name);
202 return xmlNodeGetContent (l);
209 empathy_xml_node_find_child_prop_value (xmlNodePtr node,
210 const gchar *prop_name,
211 const gchar *prop_value)
214 xmlNodePtr found = NULL;
216 g_return_val_if_fail (node != NULL, NULL);
217 g_return_val_if_fail (prop_name != NULL, NULL);
218 g_return_val_if_fail (prop_value != NULL, NULL);
220 for (l = node->children; l && !found; l = l->next) {
223 if (!xmlHasProp (l, (const xmlChar *) prop_name)) {
227 prop = xmlGetProp (l, (const xmlChar *) prop_name);
228 if (prop && strcmp ((const gchar *) prop, prop_value) == 0) {
239 empathy_presence_get_default_message (TpConnectionPresenceType presence)
242 case TP_CONNECTION_PRESENCE_TYPE_AVAILABLE:
243 return _("Available");
244 case TP_CONNECTION_PRESENCE_TYPE_BUSY:
246 case TP_CONNECTION_PRESENCE_TYPE_AWAY:
247 case TP_CONNECTION_PRESENCE_TYPE_EXTENDED_AWAY:
249 case TP_CONNECTION_PRESENCE_TYPE_HIDDEN:
250 return _("Invisible");
251 case TP_CONNECTION_PRESENCE_TYPE_OFFLINE:
253 case TP_CONNECTION_PRESENCE_TYPE_UNKNOWN:
255 case TP_CONNECTION_PRESENCE_TYPE_UNSET:
256 case TP_CONNECTION_PRESENCE_TYPE_ERROR:
265 empathy_presence_to_str (TpConnectionPresenceType presence)
269 for (i = 0 ; presence_types[i].name != NULL; i++)
270 if (presence == presence_types[i].type)
271 return presence_types[i].name;
276 TpConnectionPresenceType
277 empathy_presence_from_str (const gchar *str)
281 for (i = 0 ; presence_types[i].name != NULL; i++)
282 if (!tp_strdiff (str, presence_types[i].name))
283 return presence_types[i].type;
285 return TP_CONNECTION_PRESENCE_TYPE_UNSET;
289 empathy_status_reason_get_default_message (TpConnectionStatusReason reason)
292 case TP_CONNECTION_STATUS_REASON_NONE_SPECIFIED:
293 return _("No reason specified");
294 case TP_CONNECTION_STATUS_REASON_REQUESTED:
295 return _("Status is set to offline");
296 case TP_CONNECTION_STATUS_REASON_NETWORK_ERROR:
297 return _("Network error");
298 case TP_CONNECTION_STATUS_REASON_AUTHENTICATION_FAILED:
299 return _("Authentication failed");
300 case TP_CONNECTION_STATUS_REASON_ENCRYPTION_ERROR:
301 return _("Encryption error");
302 case TP_CONNECTION_STATUS_REASON_NAME_IN_USE:
303 return _("Name in use");
304 case TP_CONNECTION_STATUS_REASON_CERT_NOT_PROVIDED:
305 return _("Certificate not provided");
306 case TP_CONNECTION_STATUS_REASON_CERT_UNTRUSTED:
307 return _("Certificate untrusted");
308 case TP_CONNECTION_STATUS_REASON_CERT_EXPIRED:
309 return _("Certificate expired");
310 case TP_CONNECTION_STATUS_REASON_CERT_NOT_ACTIVATED:
311 return _("Certificate not activated");
312 case TP_CONNECTION_STATUS_REASON_CERT_HOSTNAME_MISMATCH:
313 return _("Certificate hostname mismatch");
314 case TP_CONNECTION_STATUS_REASON_CERT_FINGERPRINT_MISMATCH:
315 return _("Certificate fingerprint mismatch");
316 case TP_CONNECTION_STATUS_REASON_CERT_SELF_SIGNED:
317 return _("Certificate self-signed");
318 case TP_CONNECTION_STATUS_REASON_CERT_OTHER_ERROR:
319 return _("Certificate error");
321 return _("Unknown reason");
326 create_errors_to_message_hash (void)
330 errors = g_hash_table_new (g_str_hash, g_str_equal);
331 g_hash_table_insert (errors, TP_ERROR_STR_NETWORK_ERROR, _("Network error"));
332 g_hash_table_insert (errors, TP_ERROR_STR_AUTHENTICATION_FAILED,
333 _("Authentication failed"));
334 g_hash_table_insert (errors, TP_ERROR_STR_ENCRYPTION_ERROR,
335 _("Encryption error"));
336 g_hash_table_insert (errors, TP_ERROR_STR_CERT_NOT_PROVIDED,
337 _("Certificate not provided"));
338 g_hash_table_insert (errors, TP_ERROR_STR_CERT_UNTRUSTED,
339 _("Certificate untrusted"));
340 g_hash_table_insert (errors, TP_ERROR_STR_CERT_EXPIRED,
341 _("Certificate expired"));
342 g_hash_table_insert (errors, TP_ERROR_STR_CERT_NOT_ACTIVATED,
343 _("Certificate not activated"));
344 g_hash_table_insert (errors, TP_ERROR_STR_CERT_HOSTNAME_MISMATCH,
345 _("Certificate hostname mismatch"));
346 g_hash_table_insert (errors, TP_ERROR_STR_CERT_FINGERPRINT_MISMATCH,
347 _("Certificate fingerprint mismatch"));
348 g_hash_table_insert (errors, TP_ERROR_STR_CERT_SELF_SIGNED,
349 _("Certificate self-signed"));
350 g_hash_table_insert (errors, TP_ERROR_STR_CANCELLED,
351 _("Status is set to offline"));
352 g_hash_table_insert (errors, TP_ERROR_STR_ENCRYPTION_NOT_AVAILABLE,
353 _("Encryption is not available"));
354 g_hash_table_insert (errors, TP_ERROR_STR_CERT_INVALID,
355 _("Certificate is invalid"));
356 g_hash_table_insert (errors, TP_ERROR_STR_CONNECTION_REFUSED,
357 _("Connection has been refused"));
358 g_hash_table_insert (errors, TP_ERROR_STR_CONNECTION_FAILED,
359 _("Connection can't be established"));
360 g_hash_table_insert (errors, TP_ERROR_STR_CONNECTION_LOST,
361 _("Connection has been lost"));
362 g_hash_table_insert (errors, TP_ERROR_STR_ALREADY_CONNECTED,
363 _("This resource is already connected to the server"));
364 g_hash_table_insert (errors, TP_ERROR_STR_CONNECTION_REPLACED,
365 _("Connection has been replaced by a new connection using the "
367 g_hash_table_insert (errors, TP_ERROR_STR_REGISTRATION_EXISTS,
368 _("The account already exists on the server"));
369 g_hash_table_insert (errors, TP_ERROR_STR_SERVICE_BUSY,
370 _("Server is currently too busy to handle the connection"));
371 g_hash_table_insert (errors, TP_ERROR_STR_CERT_REVOKED,
372 _("Certificate has been revoked"));
373 g_hash_table_insert (errors, TP_ERROR_STR_CERT_INSECURE,
374 _("Certificate uses an insecure cipher algorithm or is "
375 "cryptographically weak"));
376 g_hash_table_insert (errors, TP_ERROR_STR_CERT_LIMIT_EXCEEDED,
377 _("The length of the server certificate, or the depth of the "
378 "server certificate chain exceed the limits imposed by the "
385 empathy_dbus_error_name_get_default_message (const gchar *error)
387 static GHashTable *errors_to_message = NULL;
392 if (G_UNLIKELY (errors_to_message == NULL)) {
393 errors_to_message = create_errors_to_message_hash ();
396 return g_hash_table_lookup (errors_to_message, error);
400 empathy_account_get_error_message (TpAccount *account)
402 const gchar *dbus_error;
403 const gchar *message;
404 TpConnectionStatusReason reason;
406 dbus_error = tp_account_get_detailed_error (account, NULL);
407 message = empathy_dbus_error_name_get_default_message (dbus_error);
411 DEBUG ("Don't understand error '%s'; fallback to the status reason (%u)",
414 tp_account_get_connection_status (account, &reason);
416 return empathy_status_reason_get_default_message (reason);
420 empathy_file_lookup (const gchar *filename, const gchar *subdir)
428 path = g_build_filename (g_getenv ("EMPATHY_SRCDIR"), subdir, filename, NULL);
429 if (!g_file_test (path, G_FILE_TEST_EXISTS)) {
431 path = g_build_filename (DATADIR, "empathy", filename, NULL);
438 empathy_proxy_hash (gconstpointer key)
440 TpProxy *proxy = TP_PROXY (key);
441 TpProxyClass *proxy_class = TP_PROXY_GET_CLASS (key);
443 g_return_val_if_fail (TP_IS_PROXY (proxy), 0);
444 g_return_val_if_fail (proxy_class->must_have_unique_name, 0);
446 return g_str_hash (proxy->object_path) ^ g_str_hash (proxy->bus_name);
450 empathy_proxy_equal (gconstpointer a,
453 TpProxy *proxy_a = TP_PROXY (a);
454 TpProxy *proxy_b = TP_PROXY (b);
455 TpProxyClass *proxy_a_class = TP_PROXY_GET_CLASS (a);
456 TpProxyClass *proxy_b_class = TP_PROXY_GET_CLASS (b);
458 g_return_val_if_fail (TP_IS_PROXY (proxy_a), FALSE);
459 g_return_val_if_fail (TP_IS_PROXY (proxy_b), FALSE);
460 g_return_val_if_fail (proxy_a_class->must_have_unique_name, 0);
461 g_return_val_if_fail (proxy_b_class->must_have_unique_name, 0);
463 return g_str_equal (proxy_a->object_path, proxy_b->object_path) &&
464 g_str_equal (proxy_a->bus_name, proxy_b->bus_name);
468 empathy_check_available_state (void)
470 TpConnectionPresenceType presence;
473 idle = empathy_idle_dup_singleton ();
474 presence = empathy_idle_get_state (idle);
475 g_object_unref (idle);
477 if (presence != TP_CONNECTION_PRESENCE_TYPE_AVAILABLE &&
478 presence != TP_CONNECTION_PRESENCE_TYPE_UNSET) {
486 empathy_uint_compare (gconstpointer a,
489 return *(guint *) a - *(guint *) b;
493 empathy_protocol_icon_name (const gchar *protocol)
495 if (!tp_strdiff (protocol, "yahoojp"))
496 /* Yahoo Japan uses the same icon as Yahoo */
498 else if (!tp_strdiff (protocol, "simple"))
499 /* SIMPLE uses the same icon as SIP */
501 else if (!tp_strdiff (protocol, "sms"))
502 return g_strdup ("phone");
504 return g_strdup_printf ("im-%s", protocol);
508 empathy_type_dbus_ao (void)
512 if (G_UNLIKELY (t == 0))
513 t = dbus_g_type_get_collection ("GPtrArray", DBUS_TYPE_G_OBJECT_PATH);
519 empathy_protocol_name_to_display_name (const gchar *proto_name)
524 const gchar *display;
527 { "jabber", "Jabber", FALSE },
528 { "gtalk", "Google Talk", FALSE },
529 { "msn", "MSN", FALSE, },
530 { "local-xmpp", N_("People Nearby"), TRUE },
531 { "irc", "IRC", FALSE },
532 { "icq", "ICQ", FALSE },
533 { "aim", "AIM", FALSE },
534 { "yahoo", "Yahoo!", FALSE },
535 { "yahoojp", N_("Yahoo! Japan"), TRUE },
536 { "facebook", N_("Facebook Chat"), TRUE },
537 { "groupwise", "GroupWise", FALSE },
538 { "sip", "SIP", FALSE },
542 for (i = 0; names[i].proto != NULL; i++)
544 if (!tp_strdiff (proto_name, names[i].proto))
546 if (names[i].translated)
547 return _(names[i].display);
549 return names[i].display;
556 /* Note: this function depends on the account manager having its core feature
559 empathy_get_account_for_connection (TpConnection *connection)
561 TpAccountManager *manager;
562 TpAccount *account = NULL;
565 manager = tp_account_manager_dup ();
567 accounts = tp_account_manager_get_valid_accounts (manager);
569 for (l = accounts; l != NULL; l = l->next)
571 TpAccount *a = l->data;
573 if (tp_account_get_connection (a) == connection)
580 g_list_free (accounts);
581 g_object_unref (manager);
587 empathy_account_manager_get_accounts_connected (gboolean *connecting)
589 TpAccountManager *manager;
591 gboolean out_connecting = FALSE;
592 gboolean out_connected = FALSE;
594 manager = tp_account_manager_dup ();
596 if (G_UNLIKELY (!tp_account_manager_is_prepared (manager,
597 TP_ACCOUNT_MANAGER_FEATURE_CORE)))
598 g_critical (G_STRLOC ": %s called before AccountManager ready", G_STRFUNC);
600 accounts = tp_account_manager_get_valid_accounts (manager);
602 for (l = accounts; l != NULL; l = l->next)
604 TpConnectionStatus s = tp_account_get_connection_status (
605 TP_ACCOUNT (l->data), NULL);
607 if (s == TP_CONNECTION_STATUS_CONNECTING)
608 out_connecting = TRUE;
609 else if (s == TP_CONNECTION_STATUS_CONNECTED)
610 out_connected = TRUE;
612 if (out_connecting && out_connected)
616 g_list_free (accounts);
617 g_object_unref (manager);
619 if (connecting != NULL)
620 *connecting = out_connecting;
622 return out_connected;
625 /* Change the RequestedPresence of a newly created account to ensure that it
626 * is actually connected. */
628 empathy_connect_new_account (TpAccount *account,
629 TpAccountManager *account_manager)
631 TpConnectionPresenceType presence;
632 gchar *status, *message;
634 /* only force presence if presence was offline, unknown or unset */
635 presence = tp_account_get_requested_presence (account, NULL, NULL);
638 case TP_CONNECTION_PRESENCE_TYPE_OFFLINE:
639 case TP_CONNECTION_PRESENCE_TYPE_UNKNOWN:
640 case TP_CONNECTION_PRESENCE_TYPE_UNSET:
641 presence = tp_account_manager_get_most_available_presence (
642 account_manager, &status, &message);
644 if (presence == TP_CONNECTION_PRESENCE_TYPE_OFFLINE)
645 /* Global presence is offline; we force it so user doesn't have to
646 * manually change the presence to connect his new account. */
647 presence = TP_CONNECTION_PRESENCE_TYPE_AVAILABLE;
649 tp_account_request_presence_async (account, presence,
650 status, NULL, NULL, NULL);
656 case TP_CONNECTION_PRESENCE_TYPE_AVAILABLE:
657 case TP_CONNECTION_PRESENCE_TYPE_AWAY:
658 case TP_CONNECTION_PRESENCE_TYPE_EXTENDED_AWAY:
659 case TP_CONNECTION_PRESENCE_TYPE_HIDDEN:
660 case TP_CONNECTION_PRESENCE_TYPE_BUSY:
661 case TP_CONNECTION_PRESENCE_TYPE_ERROR:
663 /* do nothing if the presence is not offline */
668 /* Translate Folks' general presence type to the Tp presence type */
669 TpConnectionPresenceType
670 empathy_folks_presence_type_to_tp (FolksPresenceType type)
672 return (TpConnectionPresenceType) type;
675 /* Returns TRUE if the given Individual contains a TpContact */
677 empathy_folks_individual_contains_contact (FolksIndividual *individual)
681 g_return_val_if_fail (FOLKS_IS_INDIVIDUAL (individual), FALSE);
683 personas = folks_individual_get_personas (individual);
684 for (l = personas; l != NULL; l = l->next)
686 if (TPF_IS_PERSONA (l->data))
687 return (tpf_persona_get_contact (TPF_PERSONA (l->data)) != NULL);
693 /* TODO: this needs to be eliminated (and replaced in some cases with user
694 * prompts) when we break the assumption that FolksIndividuals are 1:1 with
697 /* Retrieve the EmpathyContact corresponding to the first TpContact contained
698 * within the given Individual. Note that this is a temporary convenience. See
701 empathy_contact_dup_from_folks_individual (FolksIndividual *individual)
704 EmpathyContact *contact = NULL;
706 g_return_val_if_fail (FOLKS_IS_INDIVIDUAL (individual), NULL);
708 personas = folks_individual_get_personas (individual);
709 for (l = personas; (l != NULL) && (contact == NULL); l = l->next)
711 TpfPersona *persona = l->data;
713 if (TPF_IS_PERSONA (persona))
715 TpContact *tp_contact;
717 tp_contact = tpf_persona_get_contact (persona);
718 contact = empathy_contact_dup_from_tp_contact (tp_contact);
719 empathy_contact_set_persona (contact, FOLKS_PERSONA (persona));
726 TpChannelGroupChangeReason
727 tp_chanel_group_change_reason_from_folks_groups_change_reason (
728 FolksGroupsChangeReason reason)
730 return (TpChannelGroupChangeReason) reason;