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 <dbus/dbus-protocol.h>
45 #include "empathy-client-factory.h"
46 #include "empathy-utils.h"
47 #include "empathy-individual-manager.h"
48 #include "empathy-presence-manager.h"
49 #include "empathy-request-util.h"
51 #include <extensions/extensions.h>
53 #define DEBUG_FLAG EMPATHY_DEBUG_OTHER
54 #include "empathy-debug.h"
56 /* Translation between presence types and string */
59 TpConnectionPresenceType type;
60 } presence_types[] = {
61 { "available", TP_CONNECTION_PRESENCE_TYPE_AVAILABLE },
62 { "busy", TP_CONNECTION_PRESENCE_TYPE_BUSY },
63 { "away", TP_CONNECTION_PRESENCE_TYPE_AWAY },
64 { "ext_away", TP_CONNECTION_PRESENCE_TYPE_EXTENDED_AWAY },
65 { "hidden", TP_CONNECTION_PRESENCE_TYPE_HIDDEN },
66 { "offline", TP_CONNECTION_PRESENCE_TYPE_OFFLINE },
67 { "unset", TP_CONNECTION_PRESENCE_TYPE_UNSET },
68 { "unknown", TP_CONNECTION_PRESENCE_TYPE_UNKNOWN },
69 { "error", TP_CONNECTION_PRESENCE_TYPE_ERROR },
70 /* alternative names */
71 { "dnd", TP_CONNECTION_PRESENCE_TYPE_BUSY },
72 { "brb", TP_CONNECTION_PRESENCE_TYPE_AWAY },
73 { "xa", TP_CONNECTION_PRESENCE_TYPE_EXTENDED_AWAY },
78 properties_contains (gchar **list,
80 const gchar *property);
83 check_writeable_property (TpConnection *connection,
84 FolksIndividual *individual,
90 static gboolean initialized = FALSE;
92 EmpathyClientFactory *factory;
100 bindtextdomain (GETTEXT_PACKAGE, LOCALEDIR);
101 bind_textdomain_codeset (GETTEXT_PACKAGE, "UTF-8");
103 /* Setup debug output for empathy and telepathy-glib */
104 if (g_getenv ("EMPATHY_TIMING") != NULL)
105 g_log_set_default_handler (tp_debug_timestamped_log_handler, NULL);
107 empathy_debug_set_flags (g_getenv ("EMPATHY_DEBUG"));
108 tp_debug_divert_messages (g_getenv ("EMPATHY_LOGFILE"));
114 factory = empathy_client_factory_dup ();
115 am = tp_account_manager_new_with_factory (TP_SIMPLE_CLIENT_FACTORY (factory));
116 tp_account_manager_set_default (am);
118 g_object_unref (factory);
123 empathy_xml_validate (xmlDoc *doc,
124 const gchar *dtd_filename)
132 path = g_build_filename (g_getenv ("EMPATHY_SRCDIR"), "libempathy",
134 if (!g_file_test (path, G_FILE_TEST_EXISTS))
137 path = g_build_filename (DATADIR, "empathy", dtd_filename, NULL);
140 DEBUG ("Loading dtd file %s", path);
142 /* The list of valid chars is taken from libxml. */
143 escaped = xmlURIEscapeStr ((const xmlChar *) path,
144 (const xmlChar *)":@&=+$,/?;");
147 memset (&cvp, 0, sizeof (cvp));
148 dtd = xmlParseDTD (NULL, escaped);
149 ret = xmlValidateDtd (&cvp, doc, dtd);
158 empathy_xml_node_get_child (xmlNodePtr node,
159 const gchar *child_name)
163 g_return_val_if_fail (node != NULL, NULL);
164 g_return_val_if_fail (child_name != NULL, NULL);
166 for (l = node->children; l; l = l->next)
168 if (l->name && strcmp ((const gchar *) l->name, child_name) == 0)
176 empathy_xml_node_get_child_content (xmlNodePtr node,
177 const gchar *child_name)
181 g_return_val_if_fail (node != NULL, NULL);
182 g_return_val_if_fail (child_name != NULL, NULL);
184 l = empathy_xml_node_get_child (node, child_name);
186 return xmlNodeGetContent (l);
192 empathy_xml_node_find_child_prop_value (xmlNodePtr node,
193 const gchar *prop_name,
194 const gchar *prop_value)
197 xmlNodePtr found = NULL;
199 g_return_val_if_fail (node != NULL, NULL);
200 g_return_val_if_fail (prop_name != NULL, NULL);
201 g_return_val_if_fail (prop_value != NULL, NULL);
203 for (l = node->children; l && !found; l = l->next)
207 if (!xmlHasProp (l, (const xmlChar *) prop_name))
210 prop = xmlGetProp (l, (const xmlChar *) prop_name);
211 if (prop && strcmp ((const gchar *) prop, prop_value) == 0)
221 empathy_presence_get_default_message (TpConnectionPresenceType presence)
225 case TP_CONNECTION_PRESENCE_TYPE_AVAILABLE:
226 return _("Available");
227 case TP_CONNECTION_PRESENCE_TYPE_BUSY:
229 case TP_CONNECTION_PRESENCE_TYPE_AWAY:
230 case TP_CONNECTION_PRESENCE_TYPE_EXTENDED_AWAY:
232 case TP_CONNECTION_PRESENCE_TYPE_HIDDEN:
233 return _("Invisible");
234 case TP_CONNECTION_PRESENCE_TYPE_OFFLINE:
236 case TP_CONNECTION_PRESENCE_TYPE_UNKNOWN:
237 /* translators: presence type is unknown */
238 return C_("presence", "Unknown");
239 case TP_CONNECTION_PRESENCE_TYPE_UNSET:
240 case TP_CONNECTION_PRESENCE_TYPE_ERROR:
249 empathy_presence_to_str (TpConnectionPresenceType presence)
253 for (i = 0 ; presence_types[i].name != NULL; i++)
254 if (presence == presence_types[i].type)
255 return presence_types[i].name;
260 TpConnectionPresenceType
261 empathy_presence_from_str (const gchar *str)
265 for (i = 0 ; presence_types[i].name != NULL; i++)
266 if (!tp_strdiff (str, presence_types[i].name))
267 return presence_types[i].type;
269 return TP_CONNECTION_PRESENCE_TYPE_UNSET;
273 empathy_status_reason_get_default_message (TpConnectionStatusReason reason)
277 case TP_CONNECTION_STATUS_REASON_NONE_SPECIFIED:
278 return _("No reason specified");
279 case TP_CONNECTION_STATUS_REASON_REQUESTED:
280 return _("Status is set to offline");
281 case TP_CONNECTION_STATUS_REASON_NETWORK_ERROR:
282 return _("Network error");
283 case TP_CONNECTION_STATUS_REASON_AUTHENTICATION_FAILED:
284 return _("Authentication failed");
285 case TP_CONNECTION_STATUS_REASON_ENCRYPTION_ERROR:
286 return _("Encryption error");
287 case TP_CONNECTION_STATUS_REASON_NAME_IN_USE:
288 return _("Name in use");
289 case TP_CONNECTION_STATUS_REASON_CERT_NOT_PROVIDED:
290 return _("Certificate not provided");
291 case TP_CONNECTION_STATUS_REASON_CERT_UNTRUSTED:
292 return _("Certificate untrusted");
293 case TP_CONNECTION_STATUS_REASON_CERT_EXPIRED:
294 return _("Certificate expired");
295 case TP_CONNECTION_STATUS_REASON_CERT_NOT_ACTIVATED:
296 return _("Certificate not activated");
297 case TP_CONNECTION_STATUS_REASON_CERT_HOSTNAME_MISMATCH:
298 return _("Certificate hostname mismatch");
299 case TP_CONNECTION_STATUS_REASON_CERT_FINGERPRINT_MISMATCH:
300 return _("Certificate fingerprint mismatch");
301 case TP_CONNECTION_STATUS_REASON_CERT_SELF_SIGNED:
302 return _("Certificate self-signed");
303 case TP_CONNECTION_STATUS_REASON_CERT_OTHER_ERROR:
304 return _("Certificate error");
306 return _("Unknown reason");
311 create_errors_to_message_hash (void)
315 errors = g_hash_table_new (g_str_hash, g_str_equal);
316 g_hash_table_insert (errors, TP_ERROR_STR_NETWORK_ERROR, _("Network error"));
317 g_hash_table_insert (errors, TP_ERROR_STR_AUTHENTICATION_FAILED,
318 _("Authentication failed"));
319 g_hash_table_insert (errors, TP_ERROR_STR_ENCRYPTION_ERROR,
320 _("Encryption error"));
321 g_hash_table_insert (errors, TP_ERROR_STR_CERT_NOT_PROVIDED,
322 _("Certificate not provided"));
323 g_hash_table_insert (errors, TP_ERROR_STR_CERT_UNTRUSTED,
324 _("Certificate untrusted"));
325 g_hash_table_insert (errors, TP_ERROR_STR_CERT_EXPIRED,
326 _("Certificate expired"));
327 g_hash_table_insert (errors, TP_ERROR_STR_CERT_NOT_ACTIVATED,
328 _("Certificate not activated"));
329 g_hash_table_insert (errors, TP_ERROR_STR_CERT_HOSTNAME_MISMATCH,
330 _("Certificate hostname mismatch"));
331 g_hash_table_insert (errors, TP_ERROR_STR_CERT_FINGERPRINT_MISMATCH,
332 _("Certificate fingerprint mismatch"));
333 g_hash_table_insert (errors, TP_ERROR_STR_CERT_SELF_SIGNED,
334 _("Certificate self-signed"));
335 g_hash_table_insert (errors, TP_ERROR_STR_CANCELLED,
336 _("Status is set to offline"));
337 g_hash_table_insert (errors, TP_ERROR_STR_ENCRYPTION_NOT_AVAILABLE,
338 _("Encryption is not available"));
339 g_hash_table_insert (errors, TP_ERROR_STR_CERT_INVALID,
340 _("Certificate is invalid"));
341 g_hash_table_insert (errors, TP_ERROR_STR_CONNECTION_REFUSED,
342 _("Connection has been refused"));
343 g_hash_table_insert (errors, TP_ERROR_STR_CONNECTION_FAILED,
344 _("Connection can't be established"));
345 g_hash_table_insert (errors, TP_ERROR_STR_CONNECTION_LOST,
346 _("Connection has been lost"));
347 g_hash_table_insert (errors, TP_ERROR_STR_ALREADY_CONNECTED,
348 _("This account is already connected to the server"));
349 g_hash_table_insert (errors, TP_ERROR_STR_CONNECTION_REPLACED,
350 _("Connection has been replaced by a new connection using the "
352 g_hash_table_insert (errors, TP_ERROR_STR_REGISTRATION_EXISTS,
353 _("The account already exists on the server"));
354 g_hash_table_insert (errors, TP_ERROR_STR_SERVICE_BUSY,
355 _("Server is currently too busy to handle the connection"));
356 g_hash_table_insert (errors, TP_ERROR_STR_CERT_REVOKED,
357 _("Certificate has been revoked"));
358 g_hash_table_insert (errors, TP_ERROR_STR_CERT_INSECURE,
359 _("Certificate uses an insecure cipher algorithm or is "
360 "cryptographically weak"));
361 g_hash_table_insert (errors, TP_ERROR_STR_CERT_LIMIT_EXCEEDED,
362 _("The length of the server certificate, or the depth of the "
363 "server certificate chain, exceed the limits imposed by the "
364 "cryptography library"));
365 g_hash_table_insert (errors, TP_ERROR_STR_SOFTWARE_UPGRADE_REQUIRED,
366 _("Your software is too old"));
367 g_hash_table_insert (errors, DBUS_ERROR_NO_REPLY,
368 _("Internal error"));
374 empathy_dbus_error_name_get_default_message (const gchar *error)
376 static GHashTable *errors_to_message = NULL;
381 if (G_UNLIKELY (errors_to_message == NULL))
382 errors_to_message = create_errors_to_message_hash ();
384 return g_hash_table_lookup (errors_to_message, error);
388 empathy_account_get_error_message (TpAccount *account,
389 gboolean *user_requested)
391 const gchar *dbus_error;
392 const gchar *message;
393 const GHashTable *details = NULL;
394 TpConnectionStatusReason reason;
396 dbus_error = tp_account_get_detailed_error (account, &details);
398 if (user_requested != NULL)
400 if (tp_asv_get_boolean (details, "user-requested", NULL))
401 *user_requested = TRUE;
403 *user_requested = FALSE;
406 message = empathy_dbus_error_name_get_default_message (dbus_error);
410 tp_account_get_connection_status (account, &reason);
412 DEBUG ("Don't understand error '%s'; fallback to the status reason (%u)",
415 return empathy_status_reason_get_default_message (reason);
419 empathy_file_lookup (const gchar *filename, const gchar *subdir)
426 path = g_build_filename (g_getenv ("EMPATHY_SRCDIR"), subdir, filename, NULL);
427 if (!g_file_test (path, G_FILE_TEST_EXISTS))
430 path = g_build_filename (DATADIR, "empathy", filename, NULL);
437 empathy_check_available_state (void)
439 TpConnectionPresenceType presence;
440 EmpathyPresenceManager *presence_mgr;
442 presence_mgr = empathy_presence_manager_dup_singleton ();
443 presence = empathy_presence_manager_get_state (presence_mgr);
444 g_object_unref (presence_mgr);
446 if (presence != TP_CONNECTION_PRESENCE_TYPE_AVAILABLE &&
447 presence != TP_CONNECTION_PRESENCE_TYPE_UNSET)
454 empathy_uint_compare (gconstpointer a,
457 return *(guint *) a - *(guint *) b;
461 empathy_protocol_icon_name (const gchar *protocol)
463 if (!tp_strdiff (protocol, "yahoojp"))
464 /* Yahoo Japan uses the same icon as Yahoo */
466 else if (!tp_strdiff (protocol, "simple"))
467 /* SIMPLE uses the same icon as SIP */
469 else if (!tp_strdiff (protocol, "sms"))
470 return g_strdup ("phone");
472 return g_strdup_printf ("im-%s", protocol);
476 empathy_type_dbus_ao (void)
480 if (G_UNLIKELY (t == 0))
481 t = dbus_g_type_get_collection ("GPtrArray", DBUS_TYPE_G_OBJECT_PATH);
487 empathy_protocol_name_to_display_name (const gchar *proto_name)
492 const gchar *display;
495 { "jabber", "Jabber", FALSE },
496 { "msn", "Windows Live (MSN)", FALSE, },
497 { "local-xmpp", N_("People Nearby"), TRUE },
498 { "irc", "IRC", FALSE },
499 { "icq", "ICQ", FALSE },
500 { "aim", "AIM", FALSE },
501 { "yahoo", "Yahoo!", FALSE },
502 { "yahoojp", N_("Yahoo! Japan"), TRUE },
503 { "groupwise", "GroupWise", FALSE },
504 { "sip", "SIP", FALSE },
508 for (i = 0; names[i].proto != NULL; i++)
510 if (!tp_strdiff (proto_name, names[i].proto))
512 if (names[i].translated)
513 return gettext (names[i].display);
515 return names[i].display;
523 empathy_service_name_to_display_name (const gchar *service_name)
527 const gchar *service;
528 const gchar *display;
531 { "google-talk", N_("Google Talk"), FALSE },
532 { "facebook", N_("Facebook Chat"), TRUE },
536 for (i = 0; names[i].service != NULL; i++)
538 if (!tp_strdiff (service_name, names[i].service))
540 if (names[i].translated)
541 return gettext (names[i].display);
543 return names[i].display;
551 empathy_account_manager_get_accounts_connected (gboolean *connecting)
553 TpAccountManager *manager;
555 gboolean out_connecting = FALSE;
556 gboolean out_connected = FALSE;
558 manager = tp_account_manager_dup ();
560 if (G_UNLIKELY (!tp_account_manager_is_prepared (manager,
561 TP_ACCOUNT_MANAGER_FEATURE_CORE)))
562 g_critical (G_STRLOC ": %s called before AccountManager ready", G_STRFUNC);
564 accounts = tp_account_manager_dup_valid_accounts (manager);
566 for (l = accounts; l != NULL; l = l->next)
568 TpConnectionStatus s = tp_account_get_connection_status (
569 TP_ACCOUNT (l->data), NULL);
571 if (s == TP_CONNECTION_STATUS_CONNECTING)
572 out_connecting = TRUE;
573 else if (s == TP_CONNECTION_STATUS_CONNECTED)
574 out_connected = TRUE;
576 if (out_connecting && out_connected)
580 g_list_free_full (accounts, g_object_unref);
581 g_object_unref (manager);
583 if (connecting != NULL)
584 *connecting = out_connecting;
586 return out_connected;
589 /* Change the RequestedPresence of a newly created account to ensure that it
590 * is actually connected. */
592 empathy_connect_new_account (TpAccount *account,
593 TpAccountManager *account_manager)
595 TpConnectionPresenceType presence;
596 gchar *status, *message;
598 /* only force presence if presence was offline, unknown or unset */
599 presence = tp_account_get_requested_presence (account, NULL, NULL);
602 case TP_CONNECTION_PRESENCE_TYPE_OFFLINE:
603 case TP_CONNECTION_PRESENCE_TYPE_UNKNOWN:
604 case TP_CONNECTION_PRESENCE_TYPE_UNSET:
605 presence = tp_account_manager_get_most_available_presence (
606 account_manager, &status, &message);
608 if (presence == TP_CONNECTION_PRESENCE_TYPE_OFFLINE)
609 /* Global presence is offline; we force it so user doesn't have to
610 * manually change the presence to connect his new account. */
611 presence = TP_CONNECTION_PRESENCE_TYPE_AVAILABLE;
613 tp_account_request_presence_async (account, presence,
614 status, NULL, NULL, NULL);
620 case TP_CONNECTION_PRESENCE_TYPE_AVAILABLE:
621 case TP_CONNECTION_PRESENCE_TYPE_AWAY:
622 case TP_CONNECTION_PRESENCE_TYPE_EXTENDED_AWAY:
623 case TP_CONNECTION_PRESENCE_TYPE_HIDDEN:
624 case TP_CONNECTION_PRESENCE_TYPE_BUSY:
625 case TP_CONNECTION_PRESENCE_TYPE_ERROR:
627 /* do nothing if the presence is not offline */
632 /* Translate Folks' general presence type to the Tp presence type */
633 TpConnectionPresenceType
634 empathy_folks_presence_type_to_tp (FolksPresenceType type)
636 return (TpConnectionPresenceType) type;
639 /* Returns TRUE if the given Individual contains a TpContact */
641 empathy_folks_individual_contains_contact (FolksIndividual *individual)
645 gboolean retval = FALSE;
647 g_return_val_if_fail (FOLKS_IS_INDIVIDUAL (individual), FALSE);
649 personas = folks_individual_get_personas (individual);
650 iter = gee_iterable_iterator (GEE_ITERABLE (personas));
651 while (!retval && gee_iterator_next (iter))
653 FolksPersona *persona = gee_iterator_get (iter);
654 TpContact *contact = NULL;
656 if (empathy_folks_persona_is_interesting (persona))
657 contact = tpf_persona_get_contact (TPF_PERSONA (persona));
659 g_clear_object (&persona);
664 g_clear_object (&iter);
669 /* TODO: this needs to be eliminated (and replaced in some cases with user
670 * prompts) when we break the assumption that FolksIndividuals are 1:1 with
673 /* Retrieve the EmpathyContact corresponding to the first TpContact contained
674 * within the given Individual. Note that this is a temporary convenience. See
677 empathy_contact_dup_from_folks_individual (FolksIndividual *individual)
681 EmpathyContact *contact = NULL;
683 g_return_val_if_fail (FOLKS_IS_INDIVIDUAL (individual), NULL);
685 personas = folks_individual_get_personas (individual);
686 iter = gee_iterable_iterator (GEE_ITERABLE (personas));
687 while (gee_iterator_next (iter) && (contact == NULL))
689 TpfPersona *persona = gee_iterator_get (iter);
691 if (empathy_folks_persona_is_interesting (FOLKS_PERSONA (persona)))
693 TpContact *tp_contact;
695 tp_contact = tpf_persona_get_contact (persona);
696 if (tp_contact != NULL)
698 contact = empathy_contact_dup_from_tp_contact (tp_contact);
699 empathy_contact_set_persona (contact, FOLKS_PERSONA (persona));
702 g_clear_object (&persona);
704 g_clear_object (&iter);
708 DEBUG ("Can't create an EmpathyContact for Individual %s",
709 folks_individual_get_id (individual));
715 TpChannelGroupChangeReason
716 tp_channel_group_change_reason_from_folks_groups_change_reason (
717 FolksGroupDetailsChangeReason reason)
719 return (TpChannelGroupChangeReason) reason;
723 empathy_dup_persona_store_for_connection (TpConnection *connection)
725 FolksBackendStore *backend_store;
726 FolksBackend *backend;
727 TpfPersonaStore *result = NULL;
729 backend_store = folks_backend_store_dup ();
730 backend = folks_backend_store_dup_backend_by_name (backend_store,
735 GeeMapIterator *iter;
737 stores_map = folks_backend_get_persona_stores (backend);
738 iter = gee_map_map_iterator (stores_map);
739 while (gee_map_iterator_next (iter))
741 TpfPersonaStore *persona_store = gee_map_iterator_get_value (iter);
743 TpConnection *conn_cur;
745 account = tpf_persona_store_get_account (persona_store);
746 conn_cur = tp_account_get_connection (account);
747 if (conn_cur == connection)
748 result = persona_store;
750 g_clear_object (&iter);
753 g_object_unref (backend);
754 g_object_unref (backend_store);
760 empathy_connection_can_add_personas (TpConnection *connection)
763 FolksPersonaStore *persona_store;
765 g_return_val_if_fail (TP_IS_CONNECTION (connection), FALSE);
767 if (tp_connection_get_status (connection, NULL) !=
768 TP_CONNECTION_STATUS_CONNECTED)
771 persona_store = FOLKS_PERSONA_STORE (
772 empathy_dup_persona_store_for_connection (connection));
774 retval = (folks_persona_store_get_can_add_personas (persona_store) ==
775 FOLKS_MAYBE_BOOL_TRUE);
777 g_clear_object (&persona_store);
783 empathy_connection_can_alias_personas (TpConnection *connection,
784 FolksIndividual *individual)
788 g_return_val_if_fail (TP_IS_CONNECTION (connection), FALSE);
790 if (tp_connection_get_status (connection, NULL) !=
791 TP_CONNECTION_STATUS_CONNECTED)
794 retval = check_writeable_property (connection, individual, "alias");
800 empathy_connection_can_group_personas (TpConnection *connection,
801 FolksIndividual *individual)
805 g_return_val_if_fail (TP_IS_CONNECTION (connection), FALSE);
807 if (tp_connection_get_status (connection, NULL) !=
808 TP_CONNECTION_STATUS_CONNECTED)
811 retval = check_writeable_property (connection, individual, "groups");
817 empathy_folks_persona_is_interesting (FolksPersona *persona)
819 /* We're not interested in non-Telepathy personas */
820 if (!TPF_IS_PERSONA (persona))
823 /* We're not interested in user personas which haven't been added to the
824 * contact list (see bgo#637151). */
825 if (folks_persona_get_is_user (persona) &&
826 !tpf_persona_get_is_in_contact_list (TPF_PERSONA (persona)))
835 empathy_get_x509_certificate_hostname (gnutls_x509_crt_t cert)
842 /* this snippet is taken from GnuTLS.
843 * see gnutls/lib/x509/rfc2818_hostname.c
845 for (idx = 0; res >= 0; idx++)
847 dns_name_size = sizeof (dns_name);
848 res = gnutls_x509_crt_get_subject_alt_name (cert, idx,
849 dns_name, &dns_name_size, NULL);
851 if (res == GNUTLS_SAN_DNSNAME || res == GNUTLS_SAN_IPADDRESS)
852 return g_strndup (dns_name, dns_name_size);
855 dns_name_size = sizeof (dns_name);
856 res = gnutls_x509_crt_get_dn_by_oid (cert, GNUTLS_OID_X520_COMMON_NAME,
857 0, 0, dns_name, &dns_name_size);
860 return g_strndup (dns_name, dns_name_size);
866 empathy_format_currency (gint amount,
868 const gchar *currency)
870 #define MINUS "\342\210\222"
871 #define EURO "\342\202\254"
872 #define YEN "\302\245"
873 #define POUND "\302\243"
875 /* localised representations of currency */
876 /* FIXME: check these, especially negatives and decimals */
877 static const struct {
878 const char *currency;
879 const char *positive;
880 const char *negative;
883 /* sym positive negative decimal */
884 { "EUR", EURO "%s", MINUS EURO "%s", "." },
885 { "USD", "$%s", MINUS "$%s", "." },
886 { "JPY", YEN "%s" MINUS YEN "%s", "." },
887 { "GBP", POUND "%s", MINUS POUND "%s", "." },
888 { "PLN", "%s zl", MINUS "%s zl", "." },
889 { "BRL", "R$%s", MINUS "R$%s", "." },
890 { "SEK", "%s kr", MINUS "%s kr", "." },
891 { "DKK", "kr %s", "kr " MINUS "%s", "." },
892 { "HKD", "$%s", MINUS "$%s", "." },
893 { "CHF", "%s Fr.", MINUS "%s Fr.", "." },
894 { "NOK", "kr %s", "kr" MINUS "%s", "," },
895 { "CAD", "$%s", MINUS "$%s", "." },
896 { "TWD", "$%s", MINUS "$%s", "." },
897 { "AUD", "$%s", MINUS "$%s", "." },
900 const char *positive = "%s";
901 const char *negative = MINUS "%s";
902 const char *decimal = ".";
903 char *fmt_amount, *money;
906 /* get the localised currency format */
907 for (i = 0; i < G_N_ELEMENTS (currencies); i++)
909 if (!tp_strdiff (currency, currencies[i].currency))
911 positive = currencies[i].positive;
912 negative = currencies[i].negative;
913 decimal = currencies[i].decimal;
918 /* format the amount using the scale */
921 /* no decimal point required */
922 fmt_amount = g_strdup_printf ("%d", amount);
926 /* don't use floating point arithmatic, it's noisy;
927 * we take the absolute values, because we want the minus
928 * sign to appear before the $ */
929 int divisor = pow (10, scale);
930 int dollars = abs (amount / divisor);
931 int cents = abs (amount % divisor);
933 fmt_amount = g_strdup_printf ("%d%s%0*d",
934 dollars, decimal, scale, cents);
937 money = g_strdup_printf (amount < 0 ? negative : positive, fmt_amount);
944 empathy_account_has_uri_scheme_tel (TpAccount *account)
946 const gchar * const * uri_schemes;
949 uri_schemes = tp_account_get_uri_schemes (account);
950 if (uri_schemes == NULL)
953 for (i = 0; uri_schemes[i] != NULL; i++)
955 if (!tp_strdiff (uri_schemes[i], "tel"))
962 /* Return the TpContact on @conn associated with @individual, if any */
964 empathy_get_tp_contact_for_individual (FolksIndividual *individual,
967 TpContact *contact = NULL;
971 personas = folks_individual_get_personas (individual);
972 iter = gee_iterable_iterator (GEE_ITERABLE (personas));
973 while (contact == NULL && gee_iterator_next (iter))
975 TpfPersona *persona = gee_iterator_get (iter);
976 TpConnection *contact_conn;
977 TpContact *contact_cur = NULL;
979 if (TPF_IS_PERSONA (persona))
981 contact_cur = tpf_persona_get_contact (persona);
982 if (contact_cur != NULL)
984 contact_conn = tp_contact_get_connection (contact_cur);
986 if (!tp_strdiff (tp_proxy_get_object_path (contact_conn),
987 tp_proxy_get_object_path (conn)))
988 contact = contact_cur;
992 g_clear_object (&persona);
994 g_clear_object (&iter);
1000 properties_contains (gchar **list,
1002 const gchar *property)
1006 for (i = 0; i < length; i++)
1008 if (!tp_strdiff (list[i], property))
1016 check_writeable_property (TpConnection *connection,
1017 FolksIndividual *individual,
1022 gboolean retval = FALSE;
1025 FolksPersonaStore *persona_store;
1027 persona_store = FOLKS_PERSONA_STORE (
1028 empathy_dup_persona_store_for_connection (connection));
1031 folks_persona_store_get_always_writeable_properties (persona_store,
1033 retval = properties_contains (properties, prop_len, property);
1037 /* Lets see if the Individual contains a Persona with the given property */
1038 personas = folks_individual_get_personas (individual);
1039 iter = gee_iterable_iterator (GEE_ITERABLE (personas));
1040 while (!retval && gee_iterator_next (iter))
1042 FolksPersona *persona = gee_iterator_get (iter);
1045 folks_persona_get_writeable_properties (persona, &prop_len);
1046 retval = properties_contains (properties, prop_len, property);
1048 g_clear_object (&persona);
1053 g_clear_object (&iter);
1056 g_clear_object (&persona_store);
1060 /* Calculate whether the Individual can do audio or video calls.
1061 * FIXME: We can remove this once libfolks has grown capabilities support
1062 * again: bgo#626179. */
1064 empathy_individual_can_audio_video_call (FolksIndividual *individual,
1065 gboolean *can_audio_call,
1066 gboolean *can_video_call,
1067 EmpathyContact **out_contact)
1071 gboolean can_audio = FALSE, can_video = FALSE;
1073 personas = folks_individual_get_personas (individual);
1074 iter = gee_iterable_iterator (GEE_ITERABLE (personas));
1075 while (gee_iterator_next (iter))
1077 FolksPersona *persona = gee_iterator_get (iter);
1078 TpContact *tp_contact;
1080 if (!empathy_folks_persona_is_interesting (persona))
1083 tp_contact = tpf_persona_get_contact (TPF_PERSONA (persona));
1084 if (tp_contact != NULL)
1086 EmpathyContact *contact;
1088 contact = empathy_contact_dup_from_tp_contact (tp_contact);
1089 empathy_contact_set_persona (contact, persona);
1091 can_audio = can_audio || empathy_contact_get_capabilities (contact) &
1092 EMPATHY_CAPABILITIES_AUDIO;
1093 can_video = can_video || empathy_contact_get_capabilities (contact) &
1094 EMPATHY_CAPABILITIES_VIDEO;
1096 if (out_contact != NULL)
1097 *out_contact = g_object_ref (contact);
1099 g_object_unref (contact);
1103 g_clear_object (&persona);
1105 if (can_audio && can_video)
1109 g_clear_object (&iter);
1111 if (can_audio_call != NULL)
1112 *can_audio_call = can_audio;
1114 if (can_video_call != NULL)
1115 *can_video_call = can_video;
1118 static FolksIndividual *
1119 create_individual_from_persona (FolksPersona *persona)
1122 FolksIndividual *individual;
1124 personas = GEE_SET (
1125 gee_hash_set_new (FOLKS_TYPE_PERSONA, g_object_ref, g_object_unref,
1126 g_direct_hash, g_direct_equal));
1128 gee_collection_add (GEE_COLLECTION (personas), persona);
1130 individual = folks_individual_new (personas);
1132 g_clear_object (&personas);
1138 empathy_create_individual_from_tp_contact (TpContact *contact)
1140 TpfPersona *persona;
1141 FolksIndividual *individual;
1143 persona = tpf_persona_dup_for_contact (contact);
1144 if (persona == NULL)
1146 DEBUG ("Failed to get a persona for %s",
1147 tp_contact_get_identifier (contact));
1151 individual = create_individual_from_persona (FOLKS_PERSONA (persona));
1153 g_object_unref (persona);
1157 /* Look for a FolksIndividual containing @contact as one of his persona
1158 * and create one if needed */
1160 empathy_ensure_individual_from_tp_contact (TpContact *contact)
1162 TpfPersona *persona;
1163 FolksIndividual *individual;
1165 persona = tpf_persona_dup_for_contact (contact);
1166 if (persona == NULL)
1168 DEBUG ("Failed to get a persona for %s",
1169 tp_contact_get_identifier (contact));
1173 individual = folks_persona_get_individual (FOLKS_PERSONA (persona));
1175 if (individual != NULL)
1176 g_object_ref (individual);
1178 individual = create_individual_from_persona (FOLKS_PERSONA (persona));
1180 g_object_unref (persona);
1184 const gchar * const *
1185 empathy_individual_get_client_types (FolksIndividual *individual)
1189 const gchar * const *types = NULL;
1190 FolksPresenceType presence_type = FOLKS_PRESENCE_TYPE_UNSET;
1192 personas = folks_individual_get_personas (individual);
1193 iter = gee_iterable_iterator (GEE_ITERABLE (personas));
1194 while (gee_iterator_next (iter))
1196 FolksPresenceDetails *presence;
1197 FolksPersona *persona = gee_iterator_get (iter);
1199 /* We only want personas which have presence and a TpContact */
1200 if (!empathy_folks_persona_is_interesting (persona))
1203 presence = FOLKS_PRESENCE_DETAILS (persona);
1205 if (folks_presence_details_typecmp (
1206 folks_presence_details_get_presence_type (presence),
1209 TpContact *tp_contact;
1211 presence_type = folks_presence_details_get_presence_type (presence);
1213 tp_contact = tpf_persona_get_contact (TPF_PERSONA (persona));
1214 if (tp_contact != NULL)
1215 types = tp_contact_get_client_types (tp_contact);
1219 g_clear_object (&persona);
1221 g_clear_object (&iter);
1227 empathy_asv_to_vardict (const GHashTable *asv)
1229 return empathy_boxed_to_variant (TP_HASH_TYPE_STRING_VARIANT_MAP, "a{sv}",
1234 empathy_boxed_to_variant (GType gtype,
1235 const gchar *variant_type,
1238 GValue v = G_VALUE_INIT;
1241 g_return_val_if_fail (boxed != NULL, NULL);
1243 g_value_init (&v, gtype);
1244 g_value_set_boxed (&v, boxed);
1246 ret = dbus_g_value_build_g_variant (&v);
1247 g_return_val_if_fail (!tp_strdiff (g_variant_get_type_string (ret),
1248 variant_type), NULL);
1252 return g_variant_ref_sink (ret);