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 "
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
)
401 const gchar
*dbus_error
;
402 const gchar
*message
;
403 TpConnectionStatusReason reason
;
405 dbus_error
= tp_account_get_detailed_error (account
, NULL
);
406 message
= empathy_dbus_error_name_get_default_message (dbus_error
);
410 DEBUG ("Don't understand error '%s'; fallback to the status reason (%u)",
413 tp_account_get_connection_status (account
, &reason
);
415 return empathy_status_reason_get_default_message (reason
);
419 empathy_file_lookup (const gchar
*filename
, const gchar
*subdir
)
427 path
= g_build_filename (g_getenv ("EMPATHY_SRCDIR"), subdir
, filename
, NULL
);
428 if (!g_file_test (path
, G_FILE_TEST_EXISTS
)) {
430 path
= g_build_filename (DATADIR
, "empathy", filename
, NULL
);
437 empathy_proxy_hash (gconstpointer key
)
439 TpProxy
*proxy
= TP_PROXY (key
);
440 TpProxyClass
*proxy_class
= TP_PROXY_GET_CLASS (key
);
442 g_return_val_if_fail (TP_IS_PROXY (proxy
), 0);
443 g_return_val_if_fail (proxy_class
->must_have_unique_name
, 0);
445 return g_str_hash (proxy
->object_path
) ^ g_str_hash (proxy
->bus_name
);
449 empathy_proxy_equal (gconstpointer a
,
452 TpProxy
*proxy_a
= TP_PROXY (a
);
453 TpProxy
*proxy_b
= TP_PROXY (b
);
454 TpProxyClass
*proxy_a_class
= TP_PROXY_GET_CLASS (a
);
455 TpProxyClass
*proxy_b_class
= TP_PROXY_GET_CLASS (b
);
457 g_return_val_if_fail (TP_IS_PROXY (proxy_a
), FALSE
);
458 g_return_val_if_fail (TP_IS_PROXY (proxy_b
), FALSE
);
459 g_return_val_if_fail (proxy_a_class
->must_have_unique_name
, 0);
460 g_return_val_if_fail (proxy_b_class
->must_have_unique_name
, 0);
462 return g_str_equal (proxy_a
->object_path
, proxy_b
->object_path
) &&
463 g_str_equal (proxy_a
->bus_name
, proxy_b
->bus_name
);
467 empathy_check_available_state (void)
469 TpConnectionPresenceType presence
;
472 idle
= empathy_idle_dup_singleton ();
473 presence
= empathy_idle_get_state (idle
);
474 g_object_unref (idle
);
476 if (presence
!= TP_CONNECTION_PRESENCE_TYPE_AVAILABLE
&&
477 presence
!= TP_CONNECTION_PRESENCE_TYPE_UNSET
) {
485 empathy_uint_compare (gconstpointer a
,
488 return *(guint
*) a
- *(guint
*) b
;
492 empathy_protocol_icon_name (const gchar
*protocol
)
494 if (!tp_strdiff (protocol
, "yahoojp"))
495 /* Yahoo Japan uses the same icon as Yahoo */
497 else if (!tp_strdiff (protocol
, "simple"))
498 /* SIMPLE uses the same icon as SIP */
500 else if (!tp_strdiff (protocol
, "sms"))
501 return g_strdup ("phone");
503 return g_strdup_printf ("im-%s", protocol
);
507 empathy_type_dbus_ao (void)
511 if (G_UNLIKELY (t
== 0))
512 t
= dbus_g_type_get_collection ("GPtrArray", DBUS_TYPE_G_OBJECT_PATH
);
518 empathy_protocol_name_to_display_name (const gchar
*proto_name
)
523 const gchar
*display
;
526 { "jabber", "Jabber", FALSE
},
527 { "gtalk", "Google Talk", FALSE
},
528 { "msn", "MSN", FALSE
, },
529 { "local-xmpp", N_("People Nearby"), TRUE
},
530 { "irc", "IRC", FALSE
},
531 { "icq", "ICQ", FALSE
},
532 { "aim", "AIM", FALSE
},
533 { "yahoo", "Yahoo!", FALSE
},
534 { "yahoojp", N_("Yahoo! Japan"), TRUE
},
535 { "facebook", N_("Facebook Chat"), TRUE
},
536 { "groupwise", "GroupWise", FALSE
},
537 { "sip", "SIP", FALSE
},
541 for (i
= 0; names
[i
].proto
!= NULL
; i
++)
543 if (!tp_strdiff (proto_name
, names
[i
].proto
))
545 if (names
[i
].translated
)
546 return _(names
[i
].display
);
548 return names
[i
].display
;
555 /* Note: this function depends on the account manager having its core feature
558 empathy_get_account_for_connection (TpConnection
*connection
)
560 TpAccountManager
*manager
;
561 TpAccount
*account
= NULL
;
564 manager
= tp_account_manager_dup ();
566 accounts
= tp_account_manager_get_valid_accounts (manager
);
568 for (l
= accounts
; l
!= NULL
; l
= l
->next
)
570 TpAccount
*a
= l
->data
;
572 if (tp_account_get_connection (a
) == connection
)
579 g_list_free (accounts
);
580 g_object_unref (manager
);
586 empathy_account_manager_get_accounts_connected (gboolean
*connecting
)
588 TpAccountManager
*manager
;
590 gboolean out_connecting
= FALSE
;
591 gboolean out_connected
= FALSE
;
593 manager
= tp_account_manager_dup ();
595 if (G_UNLIKELY (!tp_account_manager_is_prepared (manager
,
596 TP_ACCOUNT_MANAGER_FEATURE_CORE
)))
597 g_critical (G_STRLOC
": %s called before AccountManager ready", G_STRFUNC
);
599 accounts
= tp_account_manager_get_valid_accounts (manager
);
601 for (l
= accounts
; l
!= NULL
; l
= l
->next
)
603 TpConnectionStatus s
= tp_account_get_connection_status (
604 TP_ACCOUNT (l
->data
), NULL
);
606 if (s
== TP_CONNECTION_STATUS_CONNECTING
)
607 out_connecting
= TRUE
;
608 else if (s
== TP_CONNECTION_STATUS_CONNECTED
)
609 out_connected
= TRUE
;
611 if (out_connecting
&& out_connected
)
615 g_list_free (accounts
);
616 g_object_unref (manager
);
618 if (connecting
!= NULL
)
619 *connecting
= out_connecting
;
621 return out_connected
;
624 /* Change the RequestedPresence of a newly created account to ensure that it
625 * is actually connected. */
627 empathy_connect_new_account (TpAccount
*account
,
628 TpAccountManager
*account_manager
)
630 TpConnectionPresenceType presence
;
631 gchar
*status
, *message
;
633 /* only force presence if presence was offline, unknown or unset */
634 presence
= tp_account_get_requested_presence (account
, NULL
, NULL
);
637 case TP_CONNECTION_PRESENCE_TYPE_OFFLINE
:
638 case TP_CONNECTION_PRESENCE_TYPE_UNKNOWN
:
639 case TP_CONNECTION_PRESENCE_TYPE_UNSET
:
640 presence
= tp_account_manager_get_most_available_presence (
641 account_manager
, &status
, &message
);
643 if (presence
== TP_CONNECTION_PRESENCE_TYPE_OFFLINE
)
644 /* Global presence is offline; we force it so user doesn't have to
645 * manually change the presence to connect his new account. */
646 presence
= TP_CONNECTION_PRESENCE_TYPE_AVAILABLE
;
648 tp_account_request_presence_async (account
, presence
,
649 status
, NULL
, NULL
, NULL
);
655 case TP_CONNECTION_PRESENCE_TYPE_AVAILABLE
:
656 case TP_CONNECTION_PRESENCE_TYPE_AWAY
:
657 case TP_CONNECTION_PRESENCE_TYPE_EXTENDED_AWAY
:
658 case TP_CONNECTION_PRESENCE_TYPE_HIDDEN
:
659 case TP_CONNECTION_PRESENCE_TYPE_BUSY
:
660 case TP_CONNECTION_PRESENCE_TYPE_ERROR
:
662 /* do nothing if the presence is not offline */
667 /* Translate Folks' general presence type to the Tp presence type */
668 TpConnectionPresenceType
669 empathy_folks_presence_type_to_tp (FolksPresenceType type
)
671 return (TpConnectionPresenceType
) type
;
674 /* Returns TRUE if the given Individual contains a TpContact */
676 empathy_folks_individual_contains_contact (FolksIndividual
*individual
)
680 g_return_val_if_fail (FOLKS_IS_INDIVIDUAL (individual
), FALSE
);
682 personas
= folks_individual_get_personas (individual
);
683 for (l
= personas
; l
!= NULL
; l
= l
->next
)
685 if (TPF_IS_PERSONA (l
->data
))
686 return (tpf_persona_get_contact (TPF_PERSONA (l
->data
)) != NULL
);
692 /* TODO: this needs to be eliminated (and replaced in some cases with user
693 * prompts) when we break the assumption that FolksIndividuals are 1:1 with
696 /* Retrieve the EmpathyContact corresponding to the first TpContact contained
697 * within the given Individual. Note that this is a temporary convenience. See
700 empathy_contact_dup_from_folks_individual (FolksIndividual
*individual
)
703 EmpathyContact
*contact
= NULL
;
705 g_return_val_if_fail (FOLKS_IS_INDIVIDUAL (individual
), NULL
);
707 personas
= folks_individual_get_personas (individual
);
708 for (l
= personas
; (l
!= NULL
) && (contact
== NULL
); l
= l
->next
)
710 TpfPersona
*persona
= l
->data
;
712 if (TPF_IS_PERSONA (persona
))
714 TpContact
*tp_contact
;
716 tp_contact
= tpf_persona_get_contact (persona
);
717 contact
= empathy_contact_dup_from_tp_contact (tp_contact
);
718 empathy_contact_set_persona (contact
, FOLKS_PERSONA (persona
));
725 TpChannelGroupChangeReason
726 tp_chanel_group_change_reason_from_folks_groups_change_reason (
727 FolksGroupsChangeReason reason
)
729 return (TpChannelGroupChangeReason
) reason
;