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-individual-manager.h"
52 #include "empathy-presence-manager.h"
53 #include "empathy-request-util.h"
54 #include "empathy-tp-contact-factory.h"
56 #include <extensions/extensions.h>
58 #define DEBUG_FLAG EMPATHY_DEBUG_OTHER
59 #include "empathy-debug.h"
61 /* Translation between presence types and string */
64 TpConnectionPresenceType type;
65 } presence_types[] = {
66 { "available", TP_CONNECTION_PRESENCE_TYPE_AVAILABLE },
67 { "busy", TP_CONNECTION_PRESENCE_TYPE_BUSY },
68 { "away", TP_CONNECTION_PRESENCE_TYPE_AWAY },
69 { "ext_away", TP_CONNECTION_PRESENCE_TYPE_EXTENDED_AWAY },
70 { "hidden", TP_CONNECTION_PRESENCE_TYPE_HIDDEN },
71 { "offline", TP_CONNECTION_PRESENCE_TYPE_OFFLINE },
72 { "unset", TP_CONNECTION_PRESENCE_TYPE_UNSET },
73 { "unknown", TP_CONNECTION_PRESENCE_TYPE_UNKNOWN },
74 { "error", TP_CONNECTION_PRESENCE_TYPE_ERROR },
75 /* alternative names */
76 { "dnd", TP_CONNECTION_PRESENCE_TYPE_BUSY },
77 { "brb", TP_CONNECTION_PRESENCE_TYPE_AWAY },
78 { "xa", TP_CONNECTION_PRESENCE_TYPE_EXTENDED_AWAY },
83 properties_contains (gchar **list,
85 const gchar *property);
88 check_writeable_property (TpConnection *connection,
89 FolksIndividual *individual,
95 static gboolean initialized = FALSE;
97 EmpathyClientFactory *factory;
105 bindtextdomain (GETTEXT_PACKAGE, LOCALEDIR);
106 bind_textdomain_codeset (GETTEXT_PACKAGE, "UTF-8");
108 /* Setup debug output for empathy and telepathy-glib */
109 if (g_getenv ("EMPATHY_TIMING") != NULL)
110 g_log_set_default_handler (tp_debug_timestamped_log_handler, NULL);
112 empathy_debug_set_flags (g_getenv ("EMPATHY_DEBUG"));
113 tp_debug_divert_messages (g_getenv ("EMPATHY_LOGFILE"));
119 factory = empathy_client_factory_dup ();
120 am = tp_account_manager_new_with_factory (TP_SIMPLE_CLIENT_FACTORY (factory));
121 tp_account_manager_set_default (am);
123 g_object_unref (factory);
128 empathy_xml_validate (xmlDoc *doc,
129 const gchar *dtd_filename)
137 path = g_build_filename (g_getenv ("EMPATHY_SRCDIR"), "libempathy",
139 if (!g_file_test (path, G_FILE_TEST_EXISTS))
142 path = g_build_filename (DATADIR, "empathy", dtd_filename, NULL);
145 DEBUG ("Loading dtd file %s", path);
147 /* The list of valid chars is taken from libxml. */
148 escaped = xmlURIEscapeStr ((const xmlChar *) path,
149 (const xmlChar *)":@&=+$,/?;");
152 memset (&cvp, 0, sizeof (cvp));
153 dtd = xmlParseDTD (NULL, escaped);
154 ret = xmlValidateDtd (&cvp, doc, dtd);
163 empathy_xml_node_get_child (xmlNodePtr node,
164 const gchar *child_name)
168 g_return_val_if_fail (node != NULL, NULL);
169 g_return_val_if_fail (child_name != NULL, NULL);
171 for (l = node->children; l; l = l->next)
173 if (l->name && strcmp ((const gchar *) l->name, child_name) == 0)
181 empathy_xml_node_get_child_content (xmlNodePtr node,
182 const gchar *child_name)
186 g_return_val_if_fail (node != NULL, NULL);
187 g_return_val_if_fail (child_name != NULL, NULL);
189 l = empathy_xml_node_get_child (node, child_name);
191 return xmlNodeGetContent (l);
197 empathy_xml_node_find_child_prop_value (xmlNodePtr node,
198 const gchar *prop_name,
199 const gchar *prop_value)
202 xmlNodePtr found = NULL;
204 g_return_val_if_fail (node != NULL, NULL);
205 g_return_val_if_fail (prop_name != NULL, NULL);
206 g_return_val_if_fail (prop_value != NULL, NULL);
208 for (l = node->children; l && !found; l = l->next)
212 if (!xmlHasProp (l, (const xmlChar *) prop_name))
215 prop = xmlGetProp (l, (const xmlChar *) prop_name);
216 if (prop && strcmp ((const gchar *) prop, prop_value) == 0)
226 empathy_presence_get_default_message (TpConnectionPresenceType presence)
230 case TP_CONNECTION_PRESENCE_TYPE_AVAILABLE:
231 return _("Available");
232 case TP_CONNECTION_PRESENCE_TYPE_BUSY:
234 case TP_CONNECTION_PRESENCE_TYPE_AWAY:
235 case TP_CONNECTION_PRESENCE_TYPE_EXTENDED_AWAY:
237 case TP_CONNECTION_PRESENCE_TYPE_HIDDEN:
238 return _("Invisible");
239 case TP_CONNECTION_PRESENCE_TYPE_OFFLINE:
241 case TP_CONNECTION_PRESENCE_TYPE_UNKNOWN:
242 /* translators: presence type is unknown */
243 return C_("presence", "Unknown");
244 case TP_CONNECTION_PRESENCE_TYPE_UNSET:
245 case TP_CONNECTION_PRESENCE_TYPE_ERROR:
254 empathy_presence_to_str (TpConnectionPresenceType presence)
258 for (i = 0 ; presence_types[i].name != NULL; i++)
259 if (presence == presence_types[i].type)
260 return presence_types[i].name;
265 TpConnectionPresenceType
266 empathy_presence_from_str (const gchar *str)
270 for (i = 0 ; presence_types[i].name != NULL; i++)
271 if (!tp_strdiff (str, presence_types[i].name))
272 return presence_types[i].type;
274 return TP_CONNECTION_PRESENCE_TYPE_UNSET;
278 empathy_status_reason_get_default_message (TpConnectionStatusReason reason)
282 case TP_CONNECTION_STATUS_REASON_NONE_SPECIFIED:
283 return _("No reason specified");
284 case TP_CONNECTION_STATUS_REASON_REQUESTED:
285 return _("Status is set to offline");
286 case TP_CONNECTION_STATUS_REASON_NETWORK_ERROR:
287 return _("Network error");
288 case TP_CONNECTION_STATUS_REASON_AUTHENTICATION_FAILED:
289 return _("Authentication failed");
290 case TP_CONNECTION_STATUS_REASON_ENCRYPTION_ERROR:
291 return _("Encryption error");
292 case TP_CONNECTION_STATUS_REASON_NAME_IN_USE:
293 return _("Name in use");
294 case TP_CONNECTION_STATUS_REASON_CERT_NOT_PROVIDED:
295 return _("Certificate not provided");
296 case TP_CONNECTION_STATUS_REASON_CERT_UNTRUSTED:
297 return _("Certificate untrusted");
298 case TP_CONNECTION_STATUS_REASON_CERT_EXPIRED:
299 return _("Certificate expired");
300 case TP_CONNECTION_STATUS_REASON_CERT_NOT_ACTIVATED:
301 return _("Certificate not activated");
302 case TP_CONNECTION_STATUS_REASON_CERT_HOSTNAME_MISMATCH:
303 return _("Certificate hostname mismatch");
304 case TP_CONNECTION_STATUS_REASON_CERT_FINGERPRINT_MISMATCH:
305 return _("Certificate fingerprint mismatch");
306 case TP_CONNECTION_STATUS_REASON_CERT_SELF_SIGNED:
307 return _("Certificate self-signed");
308 case TP_CONNECTION_STATUS_REASON_CERT_OTHER_ERROR:
309 return _("Certificate error");
311 return _("Unknown reason");
316 create_errors_to_message_hash (void)
320 errors = g_hash_table_new (g_str_hash, g_str_equal);
321 g_hash_table_insert (errors, TP_ERROR_STR_NETWORK_ERROR, _("Network error"));
322 g_hash_table_insert (errors, TP_ERROR_STR_AUTHENTICATION_FAILED,
323 _("Authentication failed"));
324 g_hash_table_insert (errors, TP_ERROR_STR_ENCRYPTION_ERROR,
325 _("Encryption error"));
326 g_hash_table_insert (errors, TP_ERROR_STR_CERT_NOT_PROVIDED,
327 _("Certificate not provided"));
328 g_hash_table_insert (errors, TP_ERROR_STR_CERT_UNTRUSTED,
329 _("Certificate untrusted"));
330 g_hash_table_insert (errors, TP_ERROR_STR_CERT_EXPIRED,
331 _("Certificate expired"));
332 g_hash_table_insert (errors, TP_ERROR_STR_CERT_NOT_ACTIVATED,
333 _("Certificate not activated"));
334 g_hash_table_insert (errors, TP_ERROR_STR_CERT_HOSTNAME_MISMATCH,
335 _("Certificate hostname mismatch"));
336 g_hash_table_insert (errors, TP_ERROR_STR_CERT_FINGERPRINT_MISMATCH,
337 _("Certificate fingerprint mismatch"));
338 g_hash_table_insert (errors, TP_ERROR_STR_CERT_SELF_SIGNED,
339 _("Certificate self-signed"));
340 g_hash_table_insert (errors, TP_ERROR_STR_CANCELLED,
341 _("Status is set to offline"));
342 g_hash_table_insert (errors, TP_ERROR_STR_ENCRYPTION_NOT_AVAILABLE,
343 _("Encryption is not available"));
344 g_hash_table_insert (errors, TP_ERROR_STR_CERT_INVALID,
345 _("Certificate is invalid"));
346 g_hash_table_insert (errors, TP_ERROR_STR_CONNECTION_REFUSED,
347 _("Connection has been refused"));
348 g_hash_table_insert (errors, TP_ERROR_STR_CONNECTION_FAILED,
349 _("Connection can't be established"));
350 g_hash_table_insert (errors, TP_ERROR_STR_CONNECTION_LOST,
351 _("Connection has been lost"));
352 g_hash_table_insert (errors, TP_ERROR_STR_ALREADY_CONNECTED,
353 _("This resource is already connected to the server"));
354 g_hash_table_insert (errors, TP_ERROR_STR_CONNECTION_REPLACED,
355 _("Connection has been replaced by a new connection using the "
357 g_hash_table_insert (errors, TP_ERROR_STR_REGISTRATION_EXISTS,
358 _("The account already exists on the server"));
359 g_hash_table_insert (errors, TP_ERROR_STR_SERVICE_BUSY,
360 _("Server is currently too busy to handle the connection"));
361 g_hash_table_insert (errors, TP_ERROR_STR_CERT_REVOKED,
362 _("Certificate has been revoked"));
363 g_hash_table_insert (errors, TP_ERROR_STR_CERT_INSECURE,
364 _("Certificate uses an insecure cipher algorithm or is "
365 "cryptographically weak"));
366 g_hash_table_insert (errors, TP_ERROR_STR_CERT_LIMIT_EXCEEDED,
367 _("The length of the server certificate, or the depth of the "
368 "server certificate chain, exceed the limits imposed by the "
369 "cryptography library"));
370 g_hash_table_insert (errors, TP_ERROR_STR_SOFTWARE_UPGRADE_REQUIRED,
371 _("Your software is too old"));
377 empathy_dbus_error_name_get_default_message (const gchar *error)
379 static GHashTable *errors_to_message = NULL;
384 if (G_UNLIKELY (errors_to_message == NULL))
385 errors_to_message = create_errors_to_message_hash ();
387 return g_hash_table_lookup (errors_to_message, error);
391 empathy_account_get_error_message (TpAccount *account,
392 gboolean *user_requested)
394 const gchar *dbus_error;
395 const gchar *message;
396 const GHashTable *details = NULL;
397 TpConnectionStatusReason reason;
399 dbus_error = tp_account_get_detailed_error (account, &details);
401 if (user_requested != NULL)
403 if (tp_asv_get_boolean (details, "user-requested", NULL))
404 *user_requested = TRUE;
406 *user_requested = FALSE;
409 message = empathy_dbus_error_name_get_default_message (dbus_error);
413 tp_account_get_connection_status (account, &reason);
415 DEBUG ("Don't understand error '%s'; fallback to the status reason (%u)",
418 return empathy_status_reason_get_default_message (reason);
422 empathy_file_lookup (const gchar *filename, const gchar *subdir)
429 path = g_build_filename (g_getenv ("EMPATHY_SRCDIR"), subdir, filename, NULL);
430 if (!g_file_test (path, G_FILE_TEST_EXISTS))
433 path = g_build_filename (DATADIR, "empathy", filename, NULL);
440 empathy_proxy_hash (gconstpointer key)
442 TpProxy *proxy = TP_PROXY (key);
443 TpProxyClass *proxy_class = TP_PROXY_GET_CLASS (key);
445 g_return_val_if_fail (TP_IS_PROXY (proxy), 0);
446 g_return_val_if_fail (proxy_class->must_have_unique_name, 0);
448 return g_str_hash (proxy->object_path) ^ g_str_hash (proxy->bus_name);
452 empathy_proxy_equal (gconstpointer a,
455 TpProxy *proxy_a = TP_PROXY (a);
456 TpProxy *proxy_b = TP_PROXY (b);
457 TpProxyClass *proxy_a_class = TP_PROXY_GET_CLASS (a);
458 TpProxyClass *proxy_b_class = TP_PROXY_GET_CLASS (b);
460 g_return_val_if_fail (TP_IS_PROXY (proxy_a), FALSE);
461 g_return_val_if_fail (TP_IS_PROXY (proxy_b), FALSE);
462 g_return_val_if_fail (proxy_a_class->must_have_unique_name, 0);
463 g_return_val_if_fail (proxy_b_class->must_have_unique_name, 0);
465 return g_str_equal (proxy_a->object_path, proxy_b->object_path) &&
466 g_str_equal (proxy_a->bus_name, proxy_b->bus_name);
470 empathy_check_available_state (void)
472 TpConnectionPresenceType presence;
473 EmpathyPresenceManager *presence_mgr;
475 presence_mgr = empathy_presence_manager_dup_singleton ();
476 presence = empathy_presence_manager_get_state (presence_mgr);
477 g_object_unref (presence_mgr);
479 if (presence != TP_CONNECTION_PRESENCE_TYPE_AVAILABLE &&
480 presence != TP_CONNECTION_PRESENCE_TYPE_UNSET)
487 empathy_uint_compare (gconstpointer a,
490 return *(guint *) a - *(guint *) b;
494 empathy_protocol_icon_name (const gchar *protocol)
496 if (!tp_strdiff (protocol, "yahoojp"))
497 /* Yahoo Japan uses the same icon as Yahoo */
499 else if (!tp_strdiff (protocol, "simple"))
500 /* SIMPLE uses the same icon as SIP */
502 else if (!tp_strdiff (protocol, "sms"))
503 return g_strdup ("phone");
505 return g_strdup_printf ("im-%s", protocol);
509 empathy_type_dbus_ao (void)
513 if (G_UNLIKELY (t == 0))
514 t = dbus_g_type_get_collection ("GPtrArray", DBUS_TYPE_G_OBJECT_PATH);
520 empathy_protocol_name_to_display_name (const gchar *proto_name)
525 const gchar *display;
528 { "jabber", "Jabber", FALSE },
529 { "msn", "Windows Live (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 { "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 gettext (names[i].display);
548 return names[i].display;
556 empathy_service_name_to_display_name (const gchar *service_name)
560 const gchar *service;
561 const gchar *display;
564 { "google-talk", N_("Google Talk"), FALSE },
565 { "facebook", N_("Facebook Chat"), TRUE },
569 for (i = 0; names[i].service != NULL; i++)
571 if (!tp_strdiff (service_name, names[i].service))
573 if (names[i].translated)
574 return gettext (names[i].display);
576 return names[i].display;
584 empathy_account_manager_get_accounts_connected (gboolean *connecting)
586 TpAccountManager *manager;
588 gboolean out_connecting = FALSE;
589 gboolean out_connected = FALSE;
591 manager = tp_account_manager_dup ();
593 if (G_UNLIKELY (!tp_account_manager_is_prepared (manager,
594 TP_ACCOUNT_MANAGER_FEATURE_CORE)))
595 g_critical (G_STRLOC ": %s called before AccountManager ready", G_STRFUNC);
597 accounts = tp_account_manager_get_valid_accounts (manager);
599 for (l = accounts; l != NULL; l = l->next)
601 TpConnectionStatus s = tp_account_get_connection_status (
602 TP_ACCOUNT (l->data), NULL);
604 if (s == TP_CONNECTION_STATUS_CONNECTING)
605 out_connecting = TRUE;
606 else if (s == TP_CONNECTION_STATUS_CONNECTED)
607 out_connected = TRUE;
609 if (out_connecting && out_connected)
613 g_list_free (accounts);
614 g_object_unref (manager);
616 if (connecting != NULL)
617 *connecting = out_connecting;
619 return out_connected;
622 /* Change the RequestedPresence of a newly created account to ensure that it
623 * is actually connected. */
625 empathy_connect_new_account (TpAccount *account,
626 TpAccountManager *account_manager)
628 TpConnectionPresenceType presence;
629 gchar *status, *message;
631 /* only force presence if presence was offline, unknown or unset */
632 presence = tp_account_get_requested_presence (account, NULL, NULL);
635 case TP_CONNECTION_PRESENCE_TYPE_OFFLINE:
636 case TP_CONNECTION_PRESENCE_TYPE_UNKNOWN:
637 case TP_CONNECTION_PRESENCE_TYPE_UNSET:
638 presence = tp_account_manager_get_most_available_presence (
639 account_manager, &status, &message);
641 if (presence == TP_CONNECTION_PRESENCE_TYPE_OFFLINE)
642 /* Global presence is offline; we force it so user doesn't have to
643 * manually change the presence to connect his new account. */
644 presence = TP_CONNECTION_PRESENCE_TYPE_AVAILABLE;
646 tp_account_request_presence_async (account, presence,
647 status, NULL, NULL, NULL);
653 case TP_CONNECTION_PRESENCE_TYPE_AVAILABLE:
654 case TP_CONNECTION_PRESENCE_TYPE_AWAY:
655 case TP_CONNECTION_PRESENCE_TYPE_EXTENDED_AWAY:
656 case TP_CONNECTION_PRESENCE_TYPE_HIDDEN:
657 case TP_CONNECTION_PRESENCE_TYPE_BUSY:
658 case TP_CONNECTION_PRESENCE_TYPE_ERROR:
660 /* do nothing if the presence is not offline */
665 /* Translate Folks' general presence type to the Tp presence type */
666 TpConnectionPresenceType
667 empathy_folks_presence_type_to_tp (FolksPresenceType type)
669 return (TpConnectionPresenceType) type;
672 /* Returns TRUE if the given Individual contains a TpContact */
674 empathy_folks_individual_contains_contact (FolksIndividual *individual)
678 gboolean retval = FALSE;
680 g_return_val_if_fail (FOLKS_IS_INDIVIDUAL (individual), FALSE);
682 personas = folks_individual_get_personas (individual);
683 iter = gee_iterable_iterator (GEE_ITERABLE (personas));
684 while (!retval && gee_iterator_next (iter))
686 FolksPersona *persona = gee_iterator_get (iter);
687 TpContact *contact = NULL;
689 if (empathy_folks_persona_is_interesting (persona))
690 contact = tpf_persona_get_contact (TPF_PERSONA (persona));
692 g_clear_object (&persona);
697 g_clear_object (&iter);
702 /* TODO: this needs to be eliminated (and replaced in some cases with user
703 * prompts) when we break the assumption that FolksIndividuals are 1:1 with
706 /* Retrieve the EmpathyContact corresponding to the first TpContact contained
707 * within the given Individual. Note that this is a temporary convenience. See
710 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 iter = gee_iterable_iterator (GEE_ITERABLE (personas));
720 while (gee_iterator_next (iter) && (contact == NULL))
722 TpfPersona *persona = gee_iterator_get (iter);
724 if (empathy_folks_persona_is_interesting (FOLKS_PERSONA (persona)))
726 TpContact *tp_contact;
728 tp_contact = tpf_persona_get_contact (persona);
729 if (tp_contact != NULL)
731 contact = empathy_contact_dup_from_tp_contact (tp_contact);
732 empathy_contact_set_persona (contact, FOLKS_PERSONA (persona));
735 g_clear_object (&persona);
737 g_clear_object (&iter);
741 DEBUG ("Can't create an EmpathyContact for Individual %s",
742 folks_individual_get_id (individual));
748 TpChannelGroupChangeReason
749 tp_channel_group_change_reason_from_folks_groups_change_reason (
750 FolksGroupDetailsChangeReason reason)
752 return (TpChannelGroupChangeReason) reason;
756 empathy_dup_persona_store_for_connection (TpConnection *connection)
758 FolksBackendStore *backend_store;
759 FolksBackend *backend;
760 TpfPersonaStore *result = NULL;
762 backend_store = folks_backend_store_dup ();
763 backend = folks_backend_store_dup_backend_by_name (backend_store,
768 GeeMapIterator *iter;
770 stores_map = folks_backend_get_persona_stores (backend);
771 iter = gee_map_map_iterator (stores_map);
772 while (gee_map_iterator_next (iter))
774 TpfPersonaStore *persona_store = gee_map_iterator_get_value (iter);
776 TpConnection *conn_cur;
778 account = tpf_persona_store_get_account (persona_store);
779 conn_cur = tp_account_get_connection (account);
780 if (conn_cur == connection)
781 result = persona_store;
783 g_clear_object (&iter);
786 g_object_unref (backend);
787 g_object_unref (backend_store);
793 empathy_connection_can_add_personas (TpConnection *connection)
796 FolksPersonaStore *persona_store;
798 g_return_val_if_fail (TP_IS_CONNECTION (connection), FALSE);
800 if (tp_connection_get_status (connection, NULL) !=
801 TP_CONNECTION_STATUS_CONNECTED)
804 persona_store = FOLKS_PERSONA_STORE (
805 empathy_dup_persona_store_for_connection (connection));
807 retval = (folks_persona_store_get_can_add_personas (persona_store) ==
808 FOLKS_MAYBE_BOOL_TRUE);
810 g_clear_object (&persona_store);
816 empathy_connection_can_alias_personas (TpConnection *connection,
817 FolksIndividual *individual)
821 g_return_val_if_fail (TP_IS_CONNECTION (connection), FALSE);
823 if (tp_connection_get_status (connection, NULL) !=
824 TP_CONNECTION_STATUS_CONNECTED)
827 retval = check_writeable_property (connection, individual, "alias");
833 empathy_connection_can_group_personas (TpConnection *connection,
834 FolksIndividual *individual)
838 g_return_val_if_fail (TP_IS_CONNECTION (connection), FALSE);
840 if (tp_connection_get_status (connection, NULL) !=
841 TP_CONNECTION_STATUS_CONNECTED)
844 retval = check_writeable_property (connection, individual, "groups");
850 empathy_folks_persona_is_interesting (FolksPersona *persona)
852 /* We're not interested in non-Telepathy personas */
853 if (!TPF_IS_PERSONA (persona))
856 /* We're not interested in user personas which haven't been added to the
857 * contact list (see bgo#637151). */
858 if (folks_persona_get_is_user (persona) &&
859 !tpf_persona_get_is_in_contact_list (TPF_PERSONA (persona)))
868 empathy_get_x509_certificate_hostname (gnutls_x509_crt_t cert)
875 /* this snippet is taken from GnuTLS.
876 * see gnutls/lib/x509/rfc2818_hostname.c
878 for (idx = 0; res >= 0; idx++)
880 dns_name_size = sizeof (dns_name);
881 res = gnutls_x509_crt_get_subject_alt_name (cert, idx,
882 dns_name, &dns_name_size, NULL);
884 if (res == GNUTLS_SAN_DNSNAME || res == GNUTLS_SAN_IPADDRESS)
885 return g_strndup (dns_name, dns_name_size);
888 dns_name_size = sizeof (dns_name);
889 res = gnutls_x509_crt_get_dn_by_oid (cert, GNUTLS_OID_X520_COMMON_NAME,
890 0, 0, dns_name, &dns_name_size);
893 return g_strndup (dns_name, dns_name_size);
899 empathy_format_currency (gint amount,
901 const gchar *currency)
903 #define MINUS "\342\210\222"
904 #define EURO "\342\202\254"
905 #define YEN "\302\245"
906 #define POUND "\302\243"
908 /* localised representations of currency */
909 /* FIXME: check these, especially negatives and decimals */
910 static const struct {
911 const char *currency;
912 const char *positive;
913 const char *negative;
916 /* sym positive negative decimal */
917 { "EUR", EURO "%s", MINUS EURO "%s", "." },
918 { "USD", "$%s", MINUS "$%s", "." },
919 { "JPY", YEN "%s" MINUS YEN "%s", "." },
920 { "GBP", POUND "%s", MINUS POUND "%s", "." },
921 { "PLN", "%s zl", MINUS "%s zl", "." },
922 { "BRL", "R$%s", MINUS "R$%s", "." },
923 { "SEK", "%s kr", MINUS "%s kr", "." },
924 { "DKK", "kr %s", "kr " MINUS "%s", "." },
925 { "HKD", "$%s", MINUS "$%s", "." },
926 { "CHF", "%s Fr.", MINUS "%s Fr.", "." },
927 { "NOK", "kr %s", "kr" MINUS "%s", "," },
928 { "CAD", "$%s", MINUS "$%s", "." },
929 { "TWD", "$%s", MINUS "$%s", "." },
930 { "AUD", "$%s", MINUS "$%s", "." },
933 const char *positive = "%s";
934 const char *negative = MINUS "%s";
935 const char *decimal = ".";
936 char *fmt_amount, *money;
939 /* get the localised currency format */
940 for (i = 0; i < G_N_ELEMENTS (currencies); i++)
942 if (!tp_strdiff (currency, currencies[i].currency))
944 positive = currencies[i].positive;
945 negative = currencies[i].negative;
946 decimal = currencies[i].decimal;
951 /* format the amount using the scale */
954 /* no decimal point required */
955 fmt_amount = g_strdup_printf ("%d", amount);
959 /* don't use floating point arithmatic, it's noisy;
960 * we take the absolute values, because we want the minus
961 * sign to appear before the $ */
962 int divisor = pow (10, scale);
963 int dollars = abs (amount / divisor);
964 int cents = abs (amount % divisor);
966 fmt_amount = g_strdup_printf ("%d%s%0*d",
967 dollars, decimal, scale, cents);
970 money = g_strdup_printf (amount < 0 ? negative : positive, fmt_amount);
977 empathy_account_has_uri_scheme_tel (TpAccount *account)
979 const gchar * const * uri_schemes;
982 uri_schemes = tp_account_get_uri_schemes (account);
983 if (uri_schemes == NULL)
986 for (i = 0; uri_schemes[i] != NULL; i++)
988 if (!tp_strdiff (uri_schemes[i], "tel"))
995 /* Return the TpContact on @conn associated with @individual, if any */
997 empathy_get_tp_contact_for_individual (FolksIndividual *individual,
1000 TpContact *contact = NULL;
1004 personas = folks_individual_get_personas (individual);
1005 iter = gee_iterable_iterator (GEE_ITERABLE (personas));
1006 while (contact == NULL && gee_iterator_next (iter))
1008 TpfPersona *persona = gee_iterator_get (iter);
1009 TpConnection *contact_conn;
1010 TpContact *contact_cur = NULL;
1012 if (TPF_IS_PERSONA (persona))
1014 contact_cur = tpf_persona_get_contact (persona);
1015 if (contact_cur != NULL)
1017 contact_conn = tp_contact_get_connection (contact_cur);
1019 if (!tp_strdiff (tp_proxy_get_object_path (contact_conn),
1020 tp_proxy_get_object_path (conn)))
1021 contact = contact_cur;
1025 g_clear_object (&persona);
1027 g_clear_object (&iter);
1033 properties_contains (gchar **list,
1035 const gchar *property)
1039 for (i = 0; i < length; i++)
1041 if (!tp_strdiff (list[i], property))
1049 check_writeable_property (TpConnection *connection,
1050 FolksIndividual *individual,
1055 gboolean retval = FALSE;
1058 FolksPersonaStore *persona_store;
1060 persona_store = FOLKS_PERSONA_STORE (
1061 empathy_dup_persona_store_for_connection (connection));
1064 folks_persona_store_get_always_writeable_properties (persona_store,
1066 retval = properties_contains (properties, prop_len, property);
1070 /* Lets see if the Individual contains a Persona with the given property */
1071 personas = folks_individual_get_personas (individual);
1072 iter = gee_iterable_iterator (GEE_ITERABLE (personas));
1073 while (!retval && gee_iterator_next (iter))
1075 FolksPersona *persona = gee_iterator_get (iter);
1078 folks_persona_get_writeable_properties (persona, &prop_len);
1079 retval = properties_contains (properties, prop_len, property);
1081 g_clear_object (&persona);
1086 g_clear_object (&iter);
1089 g_clear_object (&persona_store);
1093 /* Calculate whether the Individual can do audio or video calls.
1094 * FIXME: We can remove this once libfolks has grown capabilities support
1095 * again: bgo#626179. */
1097 empathy_individual_can_audio_video_call (FolksIndividual *individual,
1098 gboolean *can_audio_call,
1099 gboolean *can_video_call,
1100 EmpathyContact **out_contact)
1104 gboolean can_audio = FALSE, can_video = FALSE;
1106 personas = folks_individual_get_personas (individual);
1107 iter = gee_iterable_iterator (GEE_ITERABLE (personas));
1108 while (gee_iterator_next (iter))
1110 FolksPersona *persona = gee_iterator_get (iter);
1111 TpContact *tp_contact;
1113 if (!empathy_folks_persona_is_interesting (persona))
1116 tp_contact = tpf_persona_get_contact (TPF_PERSONA (persona));
1117 if (tp_contact != NULL)
1119 EmpathyContact *contact;
1121 contact = empathy_contact_dup_from_tp_contact (tp_contact);
1122 empathy_contact_set_persona (contact, persona);
1124 can_audio = can_audio || empathy_contact_get_capabilities (contact) &
1125 EMPATHY_CAPABILITIES_AUDIO;
1126 can_video = can_video || empathy_contact_get_capabilities (contact) &
1127 EMPATHY_CAPABILITIES_VIDEO;
1129 if (out_contact != NULL)
1130 *out_contact = g_object_ref (contact);
1132 g_object_unref (contact);
1136 g_clear_object (&persona);
1138 if (can_audio && can_video)
1142 g_clear_object (&iter);
1144 if (can_audio_call != NULL)
1145 *can_audio_call = can_audio;
1147 if (can_video_call != NULL)
1148 *can_video_call = can_video;
1152 empathy_sasl_channel_supports_mechanism (TpChannel *channel,
1153 const gchar *mechanism)
1156 const gchar * const *available_mechanisms;
1158 props = tp_channel_borrow_immutable_properties (channel);
1159 available_mechanisms = tp_asv_get_boxed (props,
1160 TP_PROP_CHANNEL_INTERFACE_SASL_AUTHENTICATION_AVAILABLE_MECHANISMS,
1163 return tp_strv_contains (available_mechanisms, mechanism);
1167 empathy_create_individual_from_tp_contact (TpContact *contact)
1170 TpfPersona *persona;
1171 FolksIndividual *individual;
1173 persona = tpf_persona_dup_for_contact (contact);
1174 if (persona == NULL)
1176 DEBUG ("Failed to get a persona for %s",
1177 tp_contact_get_identifier (contact));
1181 personas = GEE_SET (
1182 gee_hash_set_new (FOLKS_TYPE_PERSONA, g_object_ref, g_object_unref,
1183 g_direct_hash, g_direct_equal));
1185 gee_collection_add (GEE_COLLECTION (personas), persona);
1187 individual = folks_individual_new (personas);
1189 g_clear_object (&persona);
1190 g_clear_object (&personas);