2 * Copyright (C) 2003-2007 Imendio AB
3 * Copyright (C) 2007-2011 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>
24 * Some snippets are taken from GnuTLS 2.8.6, which is distributed under the
25 * same GNU Lesser General Public License 2.1 (or later) version. See
26 * empathy_get_x509_certified_hostname ().
34 #include <sys/types.h>
36 #include <glib/gi18n-lib.h>
38 #include <libxml/uri.h>
40 #include <folks/folks.h>
41 #include <folks/folks-telepathy.h>
43 #include <telepathy-glib/account-manager.h>
44 #include <telepathy-glib/connection.h>
45 #include <telepathy-glib/channel.h>
46 #include <telepathy-glib/dbus.h>
47 #include <telepathy-glib/util.h>
49 #include "empathy-client-factory.h"
50 #include "empathy-utils.h"
51 #include "empathy-contact-manager.h"
52 #include "empathy-individual-manager.h"
53 #include "empathy-presence-manager.h"
54 #include "empathy-request-util.h"
55 #include "empathy-tp-contact-factory.h"
57 #include <extensions/extensions.h>
59 #define DEBUG_FLAG EMPATHY_DEBUG_OTHER
60 #include "empathy-debug.h"
62 /* Translation between presence types and string */
65 TpConnectionPresenceType type;
66 } presence_types[] = {
67 { "available", TP_CONNECTION_PRESENCE_TYPE_AVAILABLE },
68 { "busy", TP_CONNECTION_PRESENCE_TYPE_BUSY },
69 { "away", TP_CONNECTION_PRESENCE_TYPE_AWAY },
70 { "ext_away", TP_CONNECTION_PRESENCE_TYPE_EXTENDED_AWAY },
71 { "hidden", TP_CONNECTION_PRESENCE_TYPE_HIDDEN },
72 { "offline", TP_CONNECTION_PRESENCE_TYPE_OFFLINE },
73 { "unset", TP_CONNECTION_PRESENCE_TYPE_UNSET },
74 { "unknown", TP_CONNECTION_PRESENCE_TYPE_UNKNOWN },
75 { "error", TP_CONNECTION_PRESENCE_TYPE_ERROR },
76 /* alternative names */
77 { "dnd", TP_CONNECTION_PRESENCE_TYPE_BUSY },
78 { "brb", TP_CONNECTION_PRESENCE_TYPE_AWAY },
79 { "xa", TP_CONNECTION_PRESENCE_TYPE_EXTENDED_AWAY },
84 properties_contains (gchar **list,
86 const gchar *property);
89 check_writeable_property (TpConnection *connection,
90 FolksIndividual *individual,
96 static gboolean initialized = FALSE;
98 EmpathyClientFactory *factory;
106 bindtextdomain (GETTEXT_PACKAGE, LOCALEDIR);
107 bind_textdomain_codeset (GETTEXT_PACKAGE, "UTF-8");
109 /* Setup debug output for empathy and telepathy-glib */
110 if (g_getenv ("EMPATHY_TIMING") != NULL)
111 g_log_set_default_handler (tp_debug_timestamped_log_handler, NULL);
113 empathy_debug_set_flags (g_getenv ("EMPATHY_DEBUG"));
114 tp_debug_divert_messages (g_getenv ("EMPATHY_LOGFILE"));
120 factory = empathy_client_factory_dup ();
121 am = tp_account_manager_new_with_factory (TP_SIMPLE_CLIENT_FACTORY (factory));
122 tp_account_manager_set_default (am);
124 g_object_unref (factory);
129 empathy_xml_validate (xmlDoc *doc,
130 const gchar *dtd_filename)
138 path = g_build_filename (g_getenv ("EMPATHY_SRCDIR"), "libempathy",
140 if (!g_file_test (path, G_FILE_TEST_EXISTS))
143 path = g_build_filename (DATADIR, "empathy", dtd_filename, NULL);
146 DEBUG ("Loading dtd file %s", path);
148 /* The list of valid chars is taken from libxml. */
149 escaped = xmlURIEscapeStr ((const xmlChar *) path,
150 (const xmlChar *)":@&=+$,/?;");
153 memset (&cvp, 0, sizeof (cvp));
154 dtd = xmlParseDTD (NULL, escaped);
155 ret = xmlValidateDtd (&cvp, doc, dtd);
164 empathy_xml_node_get_child (xmlNodePtr node,
165 const gchar *child_name)
169 g_return_val_if_fail (node != NULL, NULL);
170 g_return_val_if_fail (child_name != NULL, NULL);
172 for (l = node->children; l; l = l->next)
174 if (l->name && strcmp ((const gchar *) l->name, child_name) == 0)
182 empathy_xml_node_get_child_content (xmlNodePtr node,
183 const gchar *child_name)
187 g_return_val_if_fail (node != NULL, NULL);
188 g_return_val_if_fail (child_name != NULL, NULL);
190 l = empathy_xml_node_get_child (node, child_name);
192 return xmlNodeGetContent (l);
198 empathy_xml_node_find_child_prop_value (xmlNodePtr node,
199 const gchar *prop_name,
200 const gchar *prop_value)
203 xmlNodePtr found = NULL;
205 g_return_val_if_fail (node != NULL, NULL);
206 g_return_val_if_fail (prop_name != NULL, NULL);
207 g_return_val_if_fail (prop_value != NULL, NULL);
209 for (l = node->children; l && !found; l = l->next)
213 if (!xmlHasProp (l, (const xmlChar *) prop_name))
216 prop = xmlGetProp (l, (const xmlChar *) prop_name);
217 if (prop && strcmp ((const gchar *) prop, prop_value) == 0)
227 empathy_presence_get_default_message (TpConnectionPresenceType presence)
231 case TP_CONNECTION_PRESENCE_TYPE_AVAILABLE:
232 return _("Available");
233 case TP_CONNECTION_PRESENCE_TYPE_BUSY:
235 case TP_CONNECTION_PRESENCE_TYPE_AWAY:
236 case TP_CONNECTION_PRESENCE_TYPE_EXTENDED_AWAY:
238 case TP_CONNECTION_PRESENCE_TYPE_HIDDEN:
239 return _("Invisible");
240 case TP_CONNECTION_PRESENCE_TYPE_OFFLINE:
242 case TP_CONNECTION_PRESENCE_TYPE_UNKNOWN:
243 /* translators: presence type is unknown */
244 return C_("presence", "Unknown");
245 case TP_CONNECTION_PRESENCE_TYPE_UNSET:
246 case TP_CONNECTION_PRESENCE_TYPE_ERROR:
255 empathy_presence_to_str (TpConnectionPresenceType presence)
259 for (i = 0 ; presence_types[i].name != NULL; i++)
260 if (presence == presence_types[i].type)
261 return presence_types[i].name;
266 TpConnectionPresenceType
267 empathy_presence_from_str (const gchar *str)
271 for (i = 0 ; presence_types[i].name != NULL; i++)
272 if (!tp_strdiff (str, presence_types[i].name))
273 return presence_types[i].type;
275 return TP_CONNECTION_PRESENCE_TYPE_UNSET;
279 empathy_status_reason_get_default_message (TpConnectionStatusReason reason)
283 case TP_CONNECTION_STATUS_REASON_NONE_SPECIFIED:
284 return _("No reason specified");
285 case TP_CONNECTION_STATUS_REASON_REQUESTED:
286 return _("Status is set to offline");
287 case TP_CONNECTION_STATUS_REASON_NETWORK_ERROR:
288 return _("Network error");
289 case TP_CONNECTION_STATUS_REASON_AUTHENTICATION_FAILED:
290 return _("Authentication failed");
291 case TP_CONNECTION_STATUS_REASON_ENCRYPTION_ERROR:
292 return _("Encryption error");
293 case TP_CONNECTION_STATUS_REASON_NAME_IN_USE:
294 return _("Name in use");
295 case TP_CONNECTION_STATUS_REASON_CERT_NOT_PROVIDED:
296 return _("Certificate not provided");
297 case TP_CONNECTION_STATUS_REASON_CERT_UNTRUSTED:
298 return _("Certificate untrusted");
299 case TP_CONNECTION_STATUS_REASON_CERT_EXPIRED:
300 return _("Certificate expired");
301 case TP_CONNECTION_STATUS_REASON_CERT_NOT_ACTIVATED:
302 return _("Certificate not activated");
303 case TP_CONNECTION_STATUS_REASON_CERT_HOSTNAME_MISMATCH:
304 return _("Certificate hostname mismatch");
305 case TP_CONNECTION_STATUS_REASON_CERT_FINGERPRINT_MISMATCH:
306 return _("Certificate fingerprint mismatch");
307 case TP_CONNECTION_STATUS_REASON_CERT_SELF_SIGNED:
308 return _("Certificate self-signed");
309 case TP_CONNECTION_STATUS_REASON_CERT_OTHER_ERROR:
310 return _("Certificate error");
312 return _("Unknown reason");
317 create_errors_to_message_hash (void)
321 errors = g_hash_table_new (g_str_hash, g_str_equal);
322 g_hash_table_insert (errors, TP_ERROR_STR_NETWORK_ERROR, _("Network error"));
323 g_hash_table_insert (errors, TP_ERROR_STR_AUTHENTICATION_FAILED,
324 _("Authentication failed"));
325 g_hash_table_insert (errors, TP_ERROR_STR_ENCRYPTION_ERROR,
326 _("Encryption error"));
327 g_hash_table_insert (errors, TP_ERROR_STR_CERT_NOT_PROVIDED,
328 _("Certificate not provided"));
329 g_hash_table_insert (errors, TP_ERROR_STR_CERT_UNTRUSTED,
330 _("Certificate untrusted"));
331 g_hash_table_insert (errors, TP_ERROR_STR_CERT_EXPIRED,
332 _("Certificate expired"));
333 g_hash_table_insert (errors, TP_ERROR_STR_CERT_NOT_ACTIVATED,
334 _("Certificate not activated"));
335 g_hash_table_insert (errors, TP_ERROR_STR_CERT_HOSTNAME_MISMATCH,
336 _("Certificate hostname mismatch"));
337 g_hash_table_insert (errors, TP_ERROR_STR_CERT_FINGERPRINT_MISMATCH,
338 _("Certificate fingerprint mismatch"));
339 g_hash_table_insert (errors, TP_ERROR_STR_CERT_SELF_SIGNED,
340 _("Certificate self-signed"));
341 g_hash_table_insert (errors, TP_ERROR_STR_CANCELLED,
342 _("Status is set to offline"));
343 g_hash_table_insert (errors, TP_ERROR_STR_ENCRYPTION_NOT_AVAILABLE,
344 _("Encryption is not available"));
345 g_hash_table_insert (errors, TP_ERROR_STR_CERT_INVALID,
346 _("Certificate is invalid"));
347 g_hash_table_insert (errors, TP_ERROR_STR_CONNECTION_REFUSED,
348 _("Connection has been refused"));
349 g_hash_table_insert (errors, TP_ERROR_STR_CONNECTION_FAILED,
350 _("Connection can't be established"));
351 g_hash_table_insert (errors, TP_ERROR_STR_CONNECTION_LOST,
352 _("Connection has been lost"));
353 g_hash_table_insert (errors, TP_ERROR_STR_ALREADY_CONNECTED,
354 _("This resource is already connected to the server"));
355 g_hash_table_insert (errors, TP_ERROR_STR_CONNECTION_REPLACED,
356 _("Connection has been replaced by a new connection using the "
358 g_hash_table_insert (errors, TP_ERROR_STR_REGISTRATION_EXISTS,
359 _("The account already exists on the server"));
360 g_hash_table_insert (errors, TP_ERROR_STR_SERVICE_BUSY,
361 _("Server is currently too busy to handle the connection"));
362 g_hash_table_insert (errors, TP_ERROR_STR_CERT_REVOKED,
363 _("Certificate has been revoked"));
364 g_hash_table_insert (errors, TP_ERROR_STR_CERT_INSECURE,
365 _("Certificate uses an insecure cipher algorithm or is "
366 "cryptographically weak"));
367 g_hash_table_insert (errors, TP_ERROR_STR_CERT_LIMIT_EXCEEDED,
368 _("The length of the server certificate, or the depth of the "
369 "server certificate chain, exceed the limits imposed by the "
370 "cryptography library"));
371 g_hash_table_insert (errors, TP_ERROR_STR_SOFTWARE_UPGRADE_REQUIRED,
372 _("Your software is too old"));
378 empathy_dbus_error_name_get_default_message (const gchar *error)
380 static GHashTable *errors_to_message = NULL;
385 if (G_UNLIKELY (errors_to_message == NULL))
386 errors_to_message = create_errors_to_message_hash ();
388 return g_hash_table_lookup (errors_to_message, error);
392 empathy_account_get_error_message (TpAccount *account,
393 gboolean *user_requested)
395 const gchar *dbus_error;
396 const gchar *message;
397 const GHashTable *details = NULL;
398 TpConnectionStatusReason reason;
400 dbus_error = tp_account_get_detailed_error (account, &details);
402 if (user_requested != NULL)
404 if (tp_asv_get_boolean (details, "user-requested", NULL))
405 *user_requested = TRUE;
407 *user_requested = FALSE;
410 message = empathy_dbus_error_name_get_default_message (dbus_error);
414 DEBUG ("Don't understand error '%s'; fallback to the status reason (%u)",
417 tp_account_get_connection_status (account, &reason);
419 return empathy_status_reason_get_default_message (reason);
423 empathy_file_lookup (const gchar *filename, const gchar *subdir)
430 path = g_build_filename (g_getenv ("EMPATHY_SRCDIR"), subdir, filename, NULL);
431 if (!g_file_test (path, G_FILE_TEST_EXISTS))
434 path = g_build_filename (DATADIR, "empathy", filename, NULL);
441 empathy_proxy_hash (gconstpointer key)
443 TpProxy *proxy = TP_PROXY (key);
444 TpProxyClass *proxy_class = TP_PROXY_GET_CLASS (key);
446 g_return_val_if_fail (TP_IS_PROXY (proxy), 0);
447 g_return_val_if_fail (proxy_class->must_have_unique_name, 0);
449 return g_str_hash (proxy->object_path) ^ g_str_hash (proxy->bus_name);
453 empathy_proxy_equal (gconstpointer a,
456 TpProxy *proxy_a = TP_PROXY (a);
457 TpProxy *proxy_b = TP_PROXY (b);
458 TpProxyClass *proxy_a_class = TP_PROXY_GET_CLASS (a);
459 TpProxyClass *proxy_b_class = TP_PROXY_GET_CLASS (b);
461 g_return_val_if_fail (TP_IS_PROXY (proxy_a), FALSE);
462 g_return_val_if_fail (TP_IS_PROXY (proxy_b), FALSE);
463 g_return_val_if_fail (proxy_a_class->must_have_unique_name, 0);
464 g_return_val_if_fail (proxy_b_class->must_have_unique_name, 0);
466 return g_str_equal (proxy_a->object_path, proxy_b->object_path) &&
467 g_str_equal (proxy_a->bus_name, proxy_b->bus_name);
471 empathy_check_available_state (void)
473 TpConnectionPresenceType presence;
474 EmpathyPresenceManager *presence_mgr;
476 presence_mgr = empathy_presence_manager_dup_singleton ();
477 presence = empathy_presence_manager_get_state (presence_mgr);
478 g_object_unref (presence_mgr);
480 if (presence != TP_CONNECTION_PRESENCE_TYPE_AVAILABLE &&
481 presence != TP_CONNECTION_PRESENCE_TYPE_UNSET)
488 empathy_uint_compare (gconstpointer a,
491 return *(guint *) a - *(guint *) b;
495 empathy_protocol_icon_name (const gchar *protocol)
497 if (!tp_strdiff (protocol, "yahoojp"))
498 /* Yahoo Japan uses the same icon as Yahoo */
500 else if (!tp_strdiff (protocol, "simple"))
501 /* SIMPLE uses the same icon as SIP */
503 else if (!tp_strdiff (protocol, "sms"))
504 return g_strdup ("phone");
506 return g_strdup_printf ("im-%s", protocol);
510 empathy_type_dbus_ao (void)
514 if (G_UNLIKELY (t == 0))
515 t = dbus_g_type_get_collection ("GPtrArray", DBUS_TYPE_G_OBJECT_PATH);
521 empathy_protocol_name_to_display_name (const gchar *proto_name)
526 const gchar *display;
529 { "jabber", "Jabber", FALSE },
530 { "msn", "Windows Live (MSN)", FALSE, },
531 { "local-xmpp", N_("People Nearby"), TRUE },
532 { "irc", "IRC", FALSE },
533 { "icq", "ICQ", FALSE },
534 { "aim", "AIM", FALSE },
535 { "yahoo", "Yahoo!", FALSE },
536 { "yahoojp", N_("Yahoo! Japan"), 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 gettext (names[i].display);
549 return names[i].display;
557 empathy_service_name_to_display_name (const gchar *service_name)
561 const gchar *service;
562 const gchar *display;
565 { "google-talk", N_("Google Talk"), FALSE },
566 { "facebook", N_("Facebook Chat"), TRUE },
570 for (i = 0; names[i].service != NULL; i++)
572 if (!tp_strdiff (service_name, names[i].service))
574 if (names[i].translated)
575 return gettext (names[i].display);
577 return names[i].display;
585 empathy_account_manager_get_accounts_connected (gboolean *connecting)
587 TpAccountManager *manager;
589 gboolean out_connecting = FALSE;
590 gboolean out_connected = FALSE;
592 manager = tp_account_manager_dup ();
594 if (G_UNLIKELY (!tp_account_manager_is_prepared (manager,
595 TP_ACCOUNT_MANAGER_FEATURE_CORE)))
596 g_critical (G_STRLOC ": %s called before AccountManager ready", G_STRFUNC);
598 accounts = tp_account_manager_get_valid_accounts (manager);
600 for (l = accounts; l != NULL; l = l->next)
602 TpConnectionStatus s = tp_account_get_connection_status (
603 TP_ACCOUNT (l->data), NULL);
605 if (s == TP_CONNECTION_STATUS_CONNECTING)
606 out_connecting = TRUE;
607 else if (s == TP_CONNECTION_STATUS_CONNECTED)
608 out_connected = TRUE;
610 if (out_connecting && out_connected)
614 g_list_free (accounts);
615 g_object_unref (manager);
617 if (connecting != NULL)
618 *connecting = out_connecting;
620 return out_connected;
623 /* Change the RequestedPresence of a newly created account to ensure that it
624 * is actually connected. */
626 empathy_connect_new_account (TpAccount *account,
627 TpAccountManager *account_manager)
629 TpConnectionPresenceType presence;
630 gchar *status, *message;
632 /* only force presence if presence was offline, unknown or unset */
633 presence = tp_account_get_requested_presence (account, NULL, NULL);
636 case TP_CONNECTION_PRESENCE_TYPE_OFFLINE:
637 case TP_CONNECTION_PRESENCE_TYPE_UNKNOWN:
638 case TP_CONNECTION_PRESENCE_TYPE_UNSET:
639 presence = tp_account_manager_get_most_available_presence (
640 account_manager, &status, &message);
642 if (presence == TP_CONNECTION_PRESENCE_TYPE_OFFLINE)
643 /* Global presence is offline; we force it so user doesn't have to
644 * manually change the presence to connect his new account. */
645 presence = TP_CONNECTION_PRESENCE_TYPE_AVAILABLE;
647 tp_account_request_presence_async (account, presence,
648 status, NULL, NULL, NULL);
654 case TP_CONNECTION_PRESENCE_TYPE_AVAILABLE:
655 case TP_CONNECTION_PRESENCE_TYPE_AWAY:
656 case TP_CONNECTION_PRESENCE_TYPE_EXTENDED_AWAY:
657 case TP_CONNECTION_PRESENCE_TYPE_HIDDEN:
658 case TP_CONNECTION_PRESENCE_TYPE_BUSY:
659 case TP_CONNECTION_PRESENCE_TYPE_ERROR:
661 /* do nothing if the presence is not offline */
666 /* Translate Folks' general presence type to the Tp presence type */
667 TpConnectionPresenceType
668 empathy_folks_presence_type_to_tp (FolksPresenceType type)
670 return (TpConnectionPresenceType) type;
673 /* Returns TRUE if the given Individual contains a TpContact */
675 empathy_folks_individual_contains_contact (FolksIndividual *individual)
679 gboolean retval = FALSE;
681 g_return_val_if_fail (FOLKS_IS_INDIVIDUAL (individual), FALSE);
683 personas = folks_individual_get_personas (individual);
684 iter = gee_iterable_iterator (GEE_ITERABLE (personas));
685 while (!retval && gee_iterator_next (iter))
687 FolksPersona *persona = gee_iterator_get (iter);
688 TpContact *contact = NULL;
690 if (empathy_folks_persona_is_interesting (persona))
691 contact = tpf_persona_get_contact (TPF_PERSONA (persona));
693 g_clear_object (&persona);
698 g_clear_object (&iter);
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)
715 EmpathyContact *contact = NULL;
717 g_return_val_if_fail (FOLKS_IS_INDIVIDUAL (individual), NULL);
719 personas = folks_individual_get_personas (individual);
720 iter = gee_iterable_iterator (GEE_ITERABLE (personas));
721 while (gee_iterator_next (iter) && (contact == NULL))
723 TpfPersona *persona = gee_iterator_get (iter);
725 if (empathy_folks_persona_is_interesting (FOLKS_PERSONA (persona)))
727 TpContact *tp_contact;
729 tp_contact = tpf_persona_get_contact (persona);
730 if (tp_contact != NULL)
732 contact = empathy_contact_dup_from_tp_contact (tp_contact);
733 empathy_contact_set_persona (contact, FOLKS_PERSONA (persona));
736 g_clear_object (&persona);
738 g_clear_object (&iter);
742 DEBUG ("Can't create an EmpathyContact for Individual %s",
743 folks_individual_get_id (individual));
749 TpChannelGroupChangeReason
750 tp_channel_group_change_reason_from_folks_groups_change_reason (
751 FolksGroupDetailsChangeReason reason)
753 return (TpChannelGroupChangeReason) reason;
757 empathy_dup_persona_store_for_connection (TpConnection *connection)
759 FolksBackendStore *backend_store;
760 FolksBackend *backend;
761 TpfPersonaStore *result = NULL;
763 backend_store = folks_backend_store_dup ();
764 backend = folks_backend_store_dup_backend_by_name (backend_store,
769 GeeMapIterator *iter;
771 stores_map = folks_backend_get_persona_stores (backend);
772 iter = gee_map_map_iterator (stores_map);
773 while (gee_map_iterator_next (iter))
775 TpfPersonaStore *persona_store = gee_map_iterator_get_value (iter);
777 TpConnection *conn_cur;
779 account = tpf_persona_store_get_account (persona_store);
780 conn_cur = tp_account_get_connection (account);
781 if (conn_cur == connection)
782 result = persona_store;
784 g_clear_object (&iter);
787 g_object_unref (backend);
788 g_object_unref (backend_store);
794 empathy_connection_can_add_personas (TpConnection *connection)
797 FolksPersonaStore *persona_store;
799 g_return_val_if_fail (TP_IS_CONNECTION (connection), FALSE);
801 if (tp_connection_get_status (connection, NULL) !=
802 TP_CONNECTION_STATUS_CONNECTED)
805 persona_store = FOLKS_PERSONA_STORE (
806 empathy_dup_persona_store_for_connection (connection));
808 retval = (folks_persona_store_get_can_add_personas (persona_store) ==
809 FOLKS_MAYBE_BOOL_TRUE);
811 g_clear_object (&persona_store);
817 empathy_connection_can_alias_personas (TpConnection *connection,
818 FolksIndividual *individual)
822 g_return_val_if_fail (TP_IS_CONNECTION (connection), FALSE);
824 if (tp_connection_get_status (connection, NULL) !=
825 TP_CONNECTION_STATUS_CONNECTED)
828 retval = check_writeable_property (connection, individual, "alias");
834 empathy_connection_can_group_personas (TpConnection *connection,
835 FolksIndividual *individual)
839 g_return_val_if_fail (TP_IS_CONNECTION (connection), FALSE);
841 if (tp_connection_get_status (connection, NULL) !=
842 TP_CONNECTION_STATUS_CONNECTED)
845 retval = check_writeable_property (connection, individual, "groups");
851 empathy_folks_persona_is_interesting (FolksPersona *persona)
853 /* We're not interested in non-Telepathy personas */
854 if (!TPF_IS_PERSONA (persona))
857 /* We're not interested in user personas which haven't been added to the
858 * contact list (see bgo#637151). */
859 if (folks_persona_get_is_user (persona) &&
860 !tpf_persona_get_is_in_contact_list (TPF_PERSONA (persona)))
869 empathy_get_x509_certificate_hostname (gnutls_x509_crt_t cert)
876 /* this snippet is taken from GnuTLS.
877 * see gnutls/lib/x509/rfc2818_hostname.c
879 for (idx = 0; res >= 0; idx++)
881 dns_name_size = sizeof (dns_name);
882 res = gnutls_x509_crt_get_subject_alt_name (cert, idx,
883 dns_name, &dns_name_size, NULL);
885 if (res == GNUTLS_SAN_DNSNAME || res == GNUTLS_SAN_IPADDRESS)
886 return g_strndup (dns_name, dns_name_size);
889 dns_name_size = sizeof (dns_name);
890 res = gnutls_x509_crt_get_dn_by_oid (cert, GNUTLS_OID_X520_COMMON_NAME,
891 0, 0, dns_name, &dns_name_size);
894 return g_strndup (dns_name, dns_name_size);
900 empathy_format_currency (gint amount,
902 const gchar *currency)
904 #define MINUS "\342\210\222"
905 #define EURO "\342\202\254"
906 #define YEN "\302\245"
907 #define POUND "\302\243"
909 /* localised representations of currency */
910 /* FIXME: check these, especially negatives and decimals */
911 static const struct {
912 const char *currency;
913 const char *positive;
914 const char *negative;
917 /* sym positive negative decimal */
918 { "EUR", EURO "%s", MINUS EURO "%s", "." },
919 { "USD", "$%s", MINUS "$%s", "." },
920 { "JPY", YEN "%s" MINUS YEN "%s", "." },
921 { "GBP", POUND "%s", MINUS POUND "%s", "." },
922 { "PLN", "%s zl", MINUS "%s zl", "." },
923 { "BRL", "R$%s", MINUS "R$%s", "." },
924 { "SEK", "%s kr", MINUS "%s kr", "." },
925 { "DKK", "kr %s", "kr " MINUS "%s", "." },
926 { "HKD", "$%s", MINUS "$%s", "." },
927 { "CHF", "%s Fr.", MINUS "%s Fr.", "." },
928 { "NOK", "kr %s", "kr" MINUS "%s", "," },
929 { "CAD", "$%s", MINUS "$%s", "." },
930 { "TWD", "$%s", MINUS "$%s", "." },
931 { "AUD", "$%s", MINUS "$%s", "." },
934 const char *positive = "%s";
935 const char *negative = MINUS "%s";
936 const char *decimal = ".";
937 char *fmt_amount, *money;
940 /* get the localised currency format */
941 for (i = 0; i < G_N_ELEMENTS (currencies); i++)
943 if (!tp_strdiff (currency, currencies[i].currency))
945 positive = currencies[i].positive;
946 negative = currencies[i].negative;
947 decimal = currencies[i].decimal;
952 /* format the amount using the scale */
955 /* no decimal point required */
956 fmt_amount = g_strdup_printf ("%d", amount);
960 /* don't use floating point arithmatic, it's noisy;
961 * we take the absolute values, because we want the minus
962 * sign to appear before the $ */
963 int divisor = pow (10, scale);
964 int dollars = abs (amount / divisor);
965 int cents = abs (amount % divisor);
967 fmt_amount = g_strdup_printf ("%d%s%0*d",
968 dollars, decimal, scale, cents);
971 money = g_strdup_printf (amount < 0 ? negative : positive, fmt_amount);
978 empathy_account_has_uri_scheme_tel (TpAccount *account)
980 const gchar * const * uri_schemes;
983 uri_schemes = tp_account_get_uri_schemes (account);
984 if (uri_schemes == NULL)
987 for (i = 0; uri_schemes[i] != NULL; i++)
989 if (!tp_strdiff (uri_schemes[i], "tel"))
996 /* Return the TpContact on @conn associated with @individual, if any */
998 empathy_get_tp_contact_for_individual (FolksIndividual *individual,
1001 TpContact *contact = NULL;
1005 personas = folks_individual_get_personas (individual);
1006 iter = gee_iterable_iterator (GEE_ITERABLE (personas));
1007 while (contact == NULL && gee_iterator_next (iter))
1009 TpfPersona *persona = gee_iterator_get (iter);
1010 TpConnection *contact_conn;
1011 TpContact *contact_cur = NULL;
1013 if (TPF_IS_PERSONA (persona))
1015 contact_cur = tpf_persona_get_contact (persona);
1016 if (contact_cur != NULL)
1018 contact_conn = tp_contact_get_connection (contact_cur);
1020 if (!tp_strdiff (tp_proxy_get_object_path (contact_conn),
1021 tp_proxy_get_object_path (conn)))
1022 contact = contact_cur;
1026 g_clear_object (&persona);
1028 g_clear_object (&iter);
1034 properties_contains (gchar **list,
1036 const gchar *property)
1040 for (i = 0; i < length; i++)
1042 if (!tp_strdiff (list[i], property))
1050 check_writeable_property (TpConnection *connection,
1051 FolksIndividual *individual,
1056 gboolean retval = FALSE;
1059 FolksPersonaStore *persona_store;
1061 persona_store = FOLKS_PERSONA_STORE (
1062 empathy_dup_persona_store_for_connection (connection));
1065 folks_persona_store_get_always_writeable_properties (persona_store,
1067 retval = properties_contains (properties, prop_len, property);
1071 /* Lets see if the Individual contains a Persona with the given property */
1072 personas = folks_individual_get_personas (individual);
1073 iter = gee_iterable_iterator (GEE_ITERABLE (personas));
1074 while (!retval && gee_iterator_next (iter))
1076 FolksPersona *persona = gee_iterator_get (iter);
1079 folks_persona_get_writeable_properties (persona, &prop_len);
1080 retval = properties_contains (properties, prop_len, property);
1082 g_clear_object (&persona);
1087 g_clear_object (&iter);
1090 g_clear_object (&persona_store);
1094 /* Calculate whether the Individual can do audio or video calls.
1095 * FIXME: We can remove this once libfolks has grown capabilities support
1096 * again: bgo#626179. */
1098 empathy_individual_can_audio_video_call (FolksIndividual *individual,
1099 gboolean *can_audio_call,
1100 gboolean *can_video_call,
1101 EmpathyContact **out_contact)
1105 gboolean can_audio = FALSE, can_video = FALSE;
1107 personas = folks_individual_get_personas (individual);
1108 iter = gee_iterable_iterator (GEE_ITERABLE (personas));
1109 while (gee_iterator_next (iter))
1111 FolksPersona *persona = gee_iterator_get (iter);
1112 TpContact *tp_contact;
1114 if (!empathy_folks_persona_is_interesting (persona))
1117 tp_contact = tpf_persona_get_contact (TPF_PERSONA (persona));
1118 if (tp_contact != NULL)
1120 EmpathyContact *contact;
1122 contact = empathy_contact_dup_from_tp_contact (tp_contact);
1123 empathy_contact_set_persona (contact, persona);
1125 can_audio = can_audio || empathy_contact_get_capabilities (contact) &
1126 EMPATHY_CAPABILITIES_AUDIO;
1127 can_video = can_video || empathy_contact_get_capabilities (contact) &
1128 EMPATHY_CAPABILITIES_VIDEO;
1130 if (out_contact != NULL)
1131 *out_contact = g_object_ref (contact);
1133 g_object_unref (contact);
1137 g_clear_object (&persona);
1139 if (can_audio && can_video)
1143 g_clear_object (&iter);
1145 if (can_audio_call != NULL)
1146 *can_audio_call = can_audio;
1148 if (can_video_call != NULL)
1149 *can_video_call = can_video;
1153 empathy_sasl_channel_supports_mechanism (TpChannel *channel,
1154 const gchar *mechanism)
1157 const gchar * const *available_mechanisms;
1159 props = tp_channel_borrow_immutable_properties (channel);
1160 available_mechanisms = tp_asv_get_boxed (props,
1161 TP_PROP_CHANNEL_INTERFACE_SASL_AUTHENTICATION_AVAILABLE_MECHANISMS,
1164 return tp_strv_contains (available_mechanisms, mechanism);
1168 empathy_create_individual_from_tp_contact (TpContact *contact)
1171 TpConnection *connection;
1172 TpfPersonaStore *store;
1174 TpfPersona *persona;
1175 FolksIndividual *individual;
1177 connection = tp_contact_get_connection (contact);
1178 account = tp_connection_get_account (connection);
1180 store = tpf_persona_store_new (account);
1182 personas = GEE_SET (
1183 gee_hash_set_new (FOLKS_TYPE_PERSONA, g_object_ref, g_object_unref,
1184 g_direct_hash, g_direct_equal));
1186 persona = tpf_persona_new (contact, store);
1188 gee_collection_add (GEE_COLLECTION (personas), persona);
1190 individual = folks_individual_new (personas);
1192 g_clear_object (&persona);
1193 g_clear_object (&personas);
1194 g_object_unref (store);