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 ().
30 #include "empathy-utils.h"
32 #include <glib/gi18n-lib.h>
33 #include <dbus/dbus-protocol.h>
36 #include "empathy-client-factory.h"
37 #include "empathy-presence-manager.h"
38 #include "extensions.h"
42 #define DEBUG_FLAG EMPATHY_DEBUG_OTHER
43 #include "empathy-debug.h"
45 /* Translation between presence types and string */
48 TpConnectionPresenceType type;
49 } presence_types[] = {
50 { "available", TP_CONNECTION_PRESENCE_TYPE_AVAILABLE },
51 { "busy", TP_CONNECTION_PRESENCE_TYPE_BUSY },
52 { "away", TP_CONNECTION_PRESENCE_TYPE_AWAY },
53 { "ext_away", TP_CONNECTION_PRESENCE_TYPE_EXTENDED_AWAY },
54 { "hidden", TP_CONNECTION_PRESENCE_TYPE_HIDDEN },
55 { "offline", TP_CONNECTION_PRESENCE_TYPE_OFFLINE },
56 { "unset", TP_CONNECTION_PRESENCE_TYPE_UNSET },
57 { "unknown", TP_CONNECTION_PRESENCE_TYPE_UNKNOWN },
58 { "error", TP_CONNECTION_PRESENCE_TYPE_ERROR },
59 /* alternative names */
60 { "dnd", TP_CONNECTION_PRESENCE_TYPE_BUSY },
61 { "brb", TP_CONNECTION_PRESENCE_TYPE_AWAY },
62 { "xa", TP_CONNECTION_PRESENCE_TYPE_EXTENDED_AWAY },
67 properties_contains (gchar **list,
69 const gchar *property);
72 check_writeable_property (TpConnection *connection,
73 FolksIndividual *individual,
79 static gboolean initialized = FALSE;
81 EmpathyClientFactory *factory;
89 bindtextdomain (GETTEXT_PACKAGE, LOCALEDIR);
90 bind_textdomain_codeset (GETTEXT_PACKAGE, "UTF-8");
92 /* Setup debug output for empathy and telepathy-glib */
93 if (g_getenv ("EMPATHY_TIMING") != NULL)
94 g_log_set_default_handler (tp_debug_timestamped_log_handler, NULL);
96 empathy_debug_set_flags (g_getenv ("EMPATHY_DEBUG"));
97 tp_debug_divert_messages (g_getenv ("EMPATHY_LOGFILE"));
103 factory = empathy_client_factory_dup ();
104 am = tp_account_manager_new_with_factory (TP_SIMPLE_CLIENT_FACTORY (factory));
105 tp_account_manager_set_default (am);
107 g_object_unref (factory);
112 empathy_xml_validate_from_resource (xmlDoc *doc,
113 const gchar *dtd_resourcename)
115 GBytes *resourcecontents;
116 gconstpointer resourcedata;
118 xmlParserInputBufferPtr buffer;
121 GError *error = NULL;
124 DEBUG ("Loading dtd resource %s", dtd_resourcename);
126 resourcecontents = g_resources_lookup_data (dtd_resourcename, G_RESOURCE_LOOKUP_FLAGS_NONE, &error);
129 g_warning ("Unable to load dtd resource '%s': %s", dtd_resourcename, error->message);
130 g_error_free (error);
133 resourcedata = g_bytes_get_data (resourcecontents, &resourcesize);
134 buffer = xmlParserInputBufferCreateStatic (resourcedata, resourcesize, XML_CHAR_ENCODING_UTF8);
136 memset (&cvp, 0, sizeof (cvp));
137 dtd = xmlIOParseDTD (NULL, buffer, XML_CHAR_ENCODING_UTF8);
138 ret = xmlValidateDtd (&cvp, doc, dtd);
141 g_bytes_unref (resourcecontents);
147 empathy_xml_node_get_child (xmlNodePtr node,
148 const gchar *child_name)
152 g_return_val_if_fail (node != NULL, NULL);
153 g_return_val_if_fail (child_name != NULL, NULL);
155 for (l = node->children; l; l = l->next)
157 if (l->name && strcmp ((const gchar *) l->name, child_name) == 0)
165 empathy_xml_node_get_child_content (xmlNodePtr node,
166 const gchar *child_name)
170 g_return_val_if_fail (node != NULL, NULL);
171 g_return_val_if_fail (child_name != NULL, NULL);
173 l = empathy_xml_node_get_child (node, child_name);
175 return xmlNodeGetContent (l);
181 empathy_xml_node_find_child_prop_value (xmlNodePtr node,
182 const gchar *prop_name,
183 const gchar *prop_value)
186 xmlNodePtr found = NULL;
188 g_return_val_if_fail (node != NULL, NULL);
189 g_return_val_if_fail (prop_name != NULL, NULL);
190 g_return_val_if_fail (prop_value != NULL, NULL);
192 for (l = node->children; l && !found; l = l->next)
196 if (!xmlHasProp (l, (const xmlChar *) prop_name))
199 prop = xmlGetProp (l, (const xmlChar *) prop_name);
200 if (prop && strcmp ((const gchar *) prop, prop_value) == 0)
210 empathy_presence_get_default_message (TpConnectionPresenceType presence)
214 case TP_CONNECTION_PRESENCE_TYPE_AVAILABLE:
215 return _("Available");
216 case TP_CONNECTION_PRESENCE_TYPE_BUSY:
218 case TP_CONNECTION_PRESENCE_TYPE_AWAY:
219 case TP_CONNECTION_PRESENCE_TYPE_EXTENDED_AWAY:
221 case TP_CONNECTION_PRESENCE_TYPE_HIDDEN:
222 return _("Invisible");
223 case TP_CONNECTION_PRESENCE_TYPE_OFFLINE:
225 case TP_CONNECTION_PRESENCE_TYPE_UNKNOWN:
226 /* translators: presence type is unknown */
227 return C_("presence", "Unknown");
228 case TP_CONNECTION_PRESENCE_TYPE_UNSET:
229 case TP_CONNECTION_PRESENCE_TYPE_ERROR:
238 empathy_presence_to_str (TpConnectionPresenceType presence)
242 for (i = 0 ; presence_types[i].name != NULL; i++)
243 if (presence == presence_types[i].type)
244 return presence_types[i].name;
249 TpConnectionPresenceType
250 empathy_presence_from_str (const gchar *str)
254 for (i = 0 ; presence_types[i].name != NULL; i++)
255 if (!tp_strdiff (str, presence_types[i].name))
256 return presence_types[i].type;
258 return TP_CONNECTION_PRESENCE_TYPE_UNSET;
262 empathy_status_reason_get_default_message (TpConnectionStatusReason reason)
266 case TP_CONNECTION_STATUS_REASON_NONE_SPECIFIED:
267 return _("No reason specified");
268 case TP_CONNECTION_STATUS_REASON_REQUESTED:
269 return _("Status is set to offline");
270 case TP_CONNECTION_STATUS_REASON_NETWORK_ERROR:
271 return _("Network error");
272 case TP_CONNECTION_STATUS_REASON_AUTHENTICATION_FAILED:
273 return _("Authentication failed");
274 case TP_CONNECTION_STATUS_REASON_ENCRYPTION_ERROR:
275 return _("Encryption error");
276 case TP_CONNECTION_STATUS_REASON_NAME_IN_USE:
277 return _("Name in use");
278 case TP_CONNECTION_STATUS_REASON_CERT_NOT_PROVIDED:
279 return _("Certificate not provided");
280 case TP_CONNECTION_STATUS_REASON_CERT_UNTRUSTED:
281 return _("Certificate untrusted");
282 case TP_CONNECTION_STATUS_REASON_CERT_EXPIRED:
283 return _("Certificate expired");
284 case TP_CONNECTION_STATUS_REASON_CERT_NOT_ACTIVATED:
285 return _("Certificate not activated");
286 case TP_CONNECTION_STATUS_REASON_CERT_HOSTNAME_MISMATCH:
287 return _("Certificate hostname mismatch");
288 case TP_CONNECTION_STATUS_REASON_CERT_FINGERPRINT_MISMATCH:
289 return _("Certificate fingerprint mismatch");
290 case TP_CONNECTION_STATUS_REASON_CERT_SELF_SIGNED:
291 return _("Certificate self-signed");
292 case TP_CONNECTION_STATUS_REASON_CERT_OTHER_ERROR:
293 return _("Certificate error");
295 return _("Unknown reason");
300 create_errors_to_message_hash (void)
304 errors = g_hash_table_new (g_str_hash, g_str_equal);
305 g_hash_table_insert (errors, TP_ERROR_STR_NETWORK_ERROR, _("Network error"));
306 g_hash_table_insert (errors, TP_ERROR_STR_AUTHENTICATION_FAILED,
307 _("Authentication failed"));
308 g_hash_table_insert (errors, TP_ERROR_STR_ENCRYPTION_ERROR,
309 _("Encryption error"));
310 g_hash_table_insert (errors, TP_ERROR_STR_CERT_NOT_PROVIDED,
311 _("Certificate not provided"));
312 g_hash_table_insert (errors, TP_ERROR_STR_CERT_UNTRUSTED,
313 _("Certificate untrusted"));
314 g_hash_table_insert (errors, TP_ERROR_STR_CERT_EXPIRED,
315 _("Certificate expired"));
316 g_hash_table_insert (errors, TP_ERROR_STR_CERT_NOT_ACTIVATED,
317 _("Certificate not activated"));
318 g_hash_table_insert (errors, TP_ERROR_STR_CERT_HOSTNAME_MISMATCH,
319 _("Certificate hostname mismatch"));
320 g_hash_table_insert (errors, TP_ERROR_STR_CERT_FINGERPRINT_MISMATCH,
321 _("Certificate fingerprint mismatch"));
322 g_hash_table_insert (errors, TP_ERROR_STR_CERT_SELF_SIGNED,
323 _("Certificate self-signed"));
324 g_hash_table_insert (errors, TP_ERROR_STR_CANCELLED,
325 _("Status is set to offline"));
326 g_hash_table_insert (errors, TP_ERROR_STR_ENCRYPTION_NOT_AVAILABLE,
327 _("Encryption is not available"));
328 g_hash_table_insert (errors, TP_ERROR_STR_CERT_INVALID,
329 _("Certificate is invalid"));
330 g_hash_table_insert (errors, TP_ERROR_STR_CONNECTION_REFUSED,
331 _("Connection has been refused"));
332 g_hash_table_insert (errors, TP_ERROR_STR_CONNECTION_FAILED,
333 _("Connection can't be established"));
334 g_hash_table_insert (errors, TP_ERROR_STR_CONNECTION_LOST,
335 _("Connection has been lost"));
336 g_hash_table_insert (errors, TP_ERROR_STR_ALREADY_CONNECTED,
337 _("This account is already connected to the server"));
338 g_hash_table_insert (errors, TP_ERROR_STR_CONNECTION_REPLACED,
339 _("Connection has been replaced by a new connection using the "
341 g_hash_table_insert (errors, TP_ERROR_STR_REGISTRATION_EXISTS,
342 _("The account already exists on the server"));
343 g_hash_table_insert (errors, TP_ERROR_STR_SERVICE_BUSY,
344 _("Server is currently too busy to handle the connection"));
345 g_hash_table_insert (errors, TP_ERROR_STR_CERT_REVOKED,
346 _("Certificate has been revoked"));
347 g_hash_table_insert (errors, TP_ERROR_STR_CERT_INSECURE,
348 _("Certificate uses an insecure cipher algorithm or is "
349 "cryptographically weak"));
350 g_hash_table_insert (errors, TP_ERROR_STR_CERT_LIMIT_EXCEEDED,
351 _("The length of the server certificate, or the depth of the "
352 "server certificate chain, exceed the limits imposed by the "
353 "cryptography library"));
354 g_hash_table_insert (errors, TP_ERROR_STR_SOFTWARE_UPGRADE_REQUIRED,
355 _("Your software is too old"));
356 g_hash_table_insert (errors, DBUS_ERROR_NO_REPLY,
357 _("Internal error"));
363 empathy_dbus_error_name_get_default_message (const gchar *error)
365 static GHashTable *errors_to_message = NULL;
370 if (G_UNLIKELY (errors_to_message == NULL))
371 errors_to_message = create_errors_to_message_hash ();
373 return g_hash_table_lookup (errors_to_message, error);
377 empathy_account_get_error_message (TpAccount *account,
378 gboolean *user_requested)
380 const gchar *dbus_error;
381 const gchar *message;
382 const GHashTable *details = NULL;
383 TpConnectionStatusReason reason;
385 dbus_error = tp_account_get_detailed_error (account, &details);
387 if (user_requested != NULL)
389 if (tp_asv_get_boolean (details, "user-requested", NULL))
390 *user_requested = TRUE;
392 *user_requested = FALSE;
395 message = empathy_dbus_error_name_get_default_message (dbus_error);
399 tp_account_get_connection_status (account, &reason);
401 DEBUG ("Don't understand error '%s'; fallback to the status reason (%u)",
404 return empathy_status_reason_get_default_message (reason);
408 empathy_file_lookup (const gchar *filename, const gchar *subdir)
415 path = g_build_filename (g_getenv ("EMPATHY_SRCDIR"), subdir, filename, NULL);
416 if (!g_file_test (path, G_FILE_TEST_EXISTS))
419 path = g_build_filename (DATADIR, "empathy", filename, NULL);
426 empathy_check_available_state (void)
428 TpConnectionPresenceType presence;
429 EmpathyPresenceManager *presence_mgr;
431 presence_mgr = empathy_presence_manager_dup_singleton ();
432 presence = empathy_presence_manager_get_state (presence_mgr);
433 g_object_unref (presence_mgr);
435 if (presence != TP_CONNECTION_PRESENCE_TYPE_AVAILABLE &&
436 presence != TP_CONNECTION_PRESENCE_TYPE_UNSET)
443 empathy_uint_compare (gconstpointer a,
446 return *(guint *) a - *(guint *) b;
450 empathy_protocol_icon_name (const gchar *protocol)
452 if (!tp_strdiff (protocol, "yahoojp"))
453 /* Yahoo Japan uses the same icon as Yahoo */
455 else if (!tp_strdiff (protocol, "simple"))
456 /* SIMPLE uses the same icon as SIP */
458 else if (!tp_strdiff (protocol, "sms"))
459 return g_strdup ("phone");
461 return g_strdup_printf ("im-%s", protocol);
465 empathy_type_dbus_ao (void)
469 if (G_UNLIKELY (t == 0))
470 t = dbus_g_type_get_collection ("GPtrArray", DBUS_TYPE_G_OBJECT_PATH);
476 empathy_protocol_name_to_display_name (const gchar *proto_name)
481 const gchar *display;
484 { "jabber", "Jabber", FALSE },
485 { "msn", "Windows Live (MSN)", FALSE, },
486 { "local-xmpp", N_("People Nearby"), TRUE },
487 { "irc", "IRC", FALSE },
488 { "icq", "ICQ", FALSE },
489 { "aim", "AIM", FALSE },
490 { "yahoo", "Yahoo!", FALSE },
491 { "yahoojp", N_("Yahoo! Japan"), TRUE },
492 { "groupwise", "GroupWise", FALSE },
493 { "sip", "SIP", FALSE },
494 { "gadugadu", "Gadu-Gadu", FALSE },
495 { "mxit", "Mxit", FALSE },
496 { "myspace", "Myspace", FALSE },
497 { "sametime", "Sametime", FALSE },
498 { "skype-dbus", "Skype (D-BUS)", FALSE },
499 { "skype-x11", "Skype (X11)", FALSE },
500 { "zephyr", "Zephyr", FALSE },
504 for (i = 0; names[i].proto != NULL; i++)
506 if (!tp_strdiff (proto_name, names[i].proto))
508 if (names[i].translated)
509 return gettext (names[i].display);
511 return names[i].display;
519 empathy_service_name_to_display_name (const gchar *service_name)
523 const gchar *service;
524 const gchar *display;
527 { "google-talk", N_("Google Talk"), FALSE },
528 { "facebook", N_("Facebook Chat"), TRUE },
532 for (i = 0; names[i].service != NULL; i++)
534 if (!tp_strdiff (service_name, names[i].service))
536 if (names[i].translated)
537 return gettext (names[i].display);
539 return names[i].display;
547 empathy_account_manager_get_accounts_connected (gboolean *connecting)
549 TpAccountManager *manager;
551 gboolean out_connecting = FALSE;
552 gboolean out_connected = FALSE;
554 manager = tp_account_manager_dup ();
556 if (G_UNLIKELY (!tp_account_manager_is_prepared (manager,
557 TP_ACCOUNT_MANAGER_FEATURE_CORE)))
558 g_critical (G_STRLOC ": %s called before AccountManager ready", G_STRFUNC);
560 accounts = tp_account_manager_dup_valid_accounts (manager);
562 for (l = accounts; l != NULL; l = l->next)
564 TpConnectionStatus s = tp_account_get_connection_status (
565 TP_ACCOUNT (l->data), NULL);
567 if (s == TP_CONNECTION_STATUS_CONNECTING)
568 out_connecting = TRUE;
569 else if (s == TP_CONNECTION_STATUS_CONNECTED)
570 out_connected = TRUE;
572 if (out_connecting && out_connected)
576 g_list_free_full (accounts, g_object_unref);
577 g_object_unref (manager);
579 if (connecting != NULL)
580 *connecting = out_connecting;
582 return out_connected;
585 /* Change the RequestedPresence of a newly created account to ensure that it
586 * is actually connected. */
588 empathy_connect_new_account (TpAccount *account,
589 TpAccountManager *account_manager)
591 TpConnectionPresenceType presence;
592 gchar *status, *message;
594 /* only force presence if presence was offline, unknown or unset */
595 presence = tp_account_get_requested_presence (account, NULL, NULL);
598 case TP_CONNECTION_PRESENCE_TYPE_OFFLINE:
599 case TP_CONNECTION_PRESENCE_TYPE_UNKNOWN:
600 case TP_CONNECTION_PRESENCE_TYPE_UNSET:
601 presence = tp_account_manager_get_most_available_presence (
602 account_manager, &status, &message);
604 if (presence == TP_CONNECTION_PRESENCE_TYPE_OFFLINE)
605 /* Global presence is offline; we force it so user doesn't have to
606 * manually change the presence to connect his new account. */
607 presence = TP_CONNECTION_PRESENCE_TYPE_AVAILABLE;
609 tp_account_request_presence_async (account, presence,
610 status, NULL, NULL, NULL);
616 case TP_CONNECTION_PRESENCE_TYPE_AVAILABLE:
617 case TP_CONNECTION_PRESENCE_TYPE_AWAY:
618 case TP_CONNECTION_PRESENCE_TYPE_EXTENDED_AWAY:
619 case TP_CONNECTION_PRESENCE_TYPE_HIDDEN:
620 case TP_CONNECTION_PRESENCE_TYPE_BUSY:
621 case TP_CONNECTION_PRESENCE_TYPE_ERROR:
623 /* do nothing if the presence is not offline */
628 /* Translate Folks' general presence type to the Tp presence type */
629 TpConnectionPresenceType
630 empathy_folks_presence_type_to_tp (FolksPresenceType type)
632 return (TpConnectionPresenceType) type;
635 /* Returns TRUE if the given Individual contains a TpContact */
637 empathy_folks_individual_contains_contact (FolksIndividual *individual)
641 gboolean retval = FALSE;
643 g_return_val_if_fail (FOLKS_IS_INDIVIDUAL (individual), FALSE);
645 personas = folks_individual_get_personas (individual);
646 iter = gee_iterable_iterator (GEE_ITERABLE (personas));
647 while (!retval && gee_iterator_next (iter))
649 FolksPersona *persona = gee_iterator_get (iter);
650 TpContact *contact = NULL;
652 if (empathy_folks_persona_is_interesting (persona))
653 contact = tpf_persona_get_contact (TPF_PERSONA (persona));
655 g_clear_object (&persona);
660 g_clear_object (&iter);
665 /* TODO: this needs to be eliminated (and replaced in some cases with user
666 * prompts) when we break the assumption that FolksIndividuals are 1:1 with
669 /* Retrieve the EmpathyContact corresponding to the first TpContact contained
670 * within the given Individual. Note that this is a temporary convenience. See
673 empathy_contact_dup_from_folks_individual (FolksIndividual *individual)
677 EmpathyContact *contact = NULL;
679 g_return_val_if_fail (FOLKS_IS_INDIVIDUAL (individual), NULL);
681 personas = folks_individual_get_personas (individual);
682 iter = gee_iterable_iterator (GEE_ITERABLE (personas));
683 while (gee_iterator_next (iter) && (contact == NULL))
685 TpfPersona *persona = gee_iterator_get (iter);
687 if (empathy_folks_persona_is_interesting (FOLKS_PERSONA (persona)))
689 TpContact *tp_contact;
691 tp_contact = tpf_persona_get_contact (persona);
692 if (tp_contact != NULL)
694 contact = empathy_contact_dup_from_tp_contact (tp_contact);
695 empathy_contact_set_persona (contact, FOLKS_PERSONA (persona));
698 g_clear_object (&persona);
700 g_clear_object (&iter);
704 DEBUG ("Can't create an EmpathyContact for Individual %s",
705 folks_individual_get_id (individual));
711 TpChannelGroupChangeReason
712 tp_channel_group_change_reason_from_folks_groups_change_reason (
713 FolksGroupDetailsChangeReason reason)
715 return (TpChannelGroupChangeReason) reason;
719 empathy_dup_persona_store_for_connection (TpConnection *connection)
721 FolksBackendStore *backend_store;
722 FolksBackend *backend;
723 TpfPersonaStore *result = NULL;
725 backend_store = folks_backend_store_dup ();
726 backend = folks_backend_store_dup_backend_by_name (backend_store,
731 GeeMapIterator *iter;
733 stores_map = folks_backend_get_persona_stores (backend);
734 iter = gee_map_map_iterator (stores_map);
735 while (gee_map_iterator_next (iter))
737 TpfPersonaStore *persona_store = gee_map_iterator_get_value (iter);
739 TpConnection *conn_cur;
741 account = tpf_persona_store_get_account (persona_store);
742 conn_cur = tp_account_get_connection (account);
743 if (conn_cur == connection)
744 result = persona_store;
746 g_clear_object (&iter);
749 g_object_unref (backend);
750 g_object_unref (backend_store);
756 empathy_connection_can_add_personas (TpConnection *connection)
759 FolksPersonaStore *persona_store;
761 g_return_val_if_fail (TP_IS_CONNECTION (connection), FALSE);
763 if (tp_connection_get_status (connection, NULL) !=
764 TP_CONNECTION_STATUS_CONNECTED)
767 persona_store = FOLKS_PERSONA_STORE (
768 empathy_dup_persona_store_for_connection (connection));
770 retval = (folks_persona_store_get_can_add_personas (persona_store) ==
771 FOLKS_MAYBE_BOOL_TRUE);
773 g_clear_object (&persona_store);
779 empathy_connection_can_alias_personas (TpConnection *connection,
780 FolksIndividual *individual)
784 g_return_val_if_fail (TP_IS_CONNECTION (connection), FALSE);
786 if (tp_connection_get_status (connection, NULL) !=
787 TP_CONNECTION_STATUS_CONNECTED)
790 retval = check_writeable_property (connection, individual, "alias");
796 empathy_connection_can_group_personas (TpConnection *connection,
797 FolksIndividual *individual)
801 g_return_val_if_fail (TP_IS_CONNECTION (connection), FALSE);
803 if (tp_connection_get_status (connection, NULL) !=
804 TP_CONNECTION_STATUS_CONNECTED)
807 retval = check_writeable_property (connection, individual, "groups");
813 empathy_folks_persona_is_interesting (FolksPersona *persona)
815 /* We're not interested in non-Telepathy personas */
816 if (!TPF_IS_PERSONA (persona))
819 /* We're not interested in user personas which haven't been added to the
820 * contact list (see bgo#637151). */
821 if (folks_persona_get_is_user (persona) &&
822 !tpf_persona_get_is_in_contact_list (TPF_PERSONA (persona)))
831 empathy_get_x509_certificate_hostname (gnutls_x509_crt_t cert)
838 /* this snippet is taken from GnuTLS.
839 * see gnutls/lib/x509/rfc2818_hostname.c
841 for (idx = 0; res >= 0; idx++)
843 dns_name_size = sizeof (dns_name);
844 res = gnutls_x509_crt_get_subject_alt_name (cert, idx,
845 dns_name, &dns_name_size, NULL);
847 if (res == GNUTLS_SAN_DNSNAME || res == GNUTLS_SAN_IPADDRESS)
848 return g_strndup (dns_name, dns_name_size);
851 dns_name_size = sizeof (dns_name);
852 res = gnutls_x509_crt_get_dn_by_oid (cert, GNUTLS_OID_X520_COMMON_NAME,
853 0, 0, dns_name, &dns_name_size);
856 return g_strndup (dns_name, dns_name_size);
862 empathy_format_currency (gint amount,
864 const gchar *currency)
866 #define MINUS "\342\210\222"
867 #define EURO "\342\202\254"
868 #define YEN "\302\245"
869 #define POUND "\302\243"
871 /* localised representations of currency */
872 /* FIXME: check these, especially negatives and decimals */
873 static const struct {
874 const char *currency;
875 const char *positive;
876 const char *negative;
879 /* sym positive negative decimal */
880 { "EUR", EURO "%s", MINUS EURO "%s", "." },
881 { "USD", "$%s", MINUS "$%s", "." },
882 { "JPY", YEN "%s" MINUS YEN "%s", "." },
883 { "GBP", POUND "%s", MINUS POUND "%s", "." },
884 { "PLN", "%s zl", MINUS "%s zl", "." },
885 { "BRL", "R$%s", MINUS "R$%s", "." },
886 { "SEK", "%s kr", MINUS "%s kr", "." },
887 { "DKK", "kr %s", "kr " MINUS "%s", "." },
888 { "HKD", "$%s", MINUS "$%s", "." },
889 { "CHF", "%s Fr.", MINUS "%s Fr.", "." },
890 { "NOK", "kr %s", "kr" MINUS "%s", "," },
891 { "CAD", "$%s", MINUS "$%s", "." },
892 { "TWD", "$%s", MINUS "$%s", "." },
893 { "AUD", "$%s", MINUS "$%s", "." },
896 const char *positive = "%s";
897 const char *negative = MINUS "%s";
898 const char *decimal = ".";
899 char *fmt_amount, *money;
902 /* get the localised currency format */
903 for (i = 0; i < G_N_ELEMENTS (currencies); i++)
905 if (!tp_strdiff (currency, currencies[i].currency))
907 positive = currencies[i].positive;
908 negative = currencies[i].negative;
909 decimal = currencies[i].decimal;
914 /* format the amount using the scale */
917 /* no decimal point required */
918 fmt_amount = g_strdup_printf ("%d", amount);
922 /* don't use floating point arithmatic, it's noisy;
923 * we take the absolute values, because we want the minus
924 * sign to appear before the $ */
925 int divisor = pow (10, scale);
926 int dollars = abs (amount / divisor);
927 int cents = abs (amount % divisor);
929 fmt_amount = g_strdup_printf ("%d%s%0*d",
930 dollars, decimal, scale, cents);
933 money = g_strdup_printf (amount < 0 ? negative : positive, fmt_amount);
940 empathy_account_has_uri_scheme_tel (TpAccount *account)
942 const gchar * const * uri_schemes;
945 uri_schemes = tp_account_get_uri_schemes (account);
946 if (uri_schemes == NULL)
949 for (i = 0; uri_schemes[i] != NULL; i++)
951 if (!tp_strdiff (uri_schemes[i], "tel"))
958 /* Return the TpContact on @conn associated with @individual, if any */
960 empathy_get_tp_contact_for_individual (FolksIndividual *individual,
963 TpContact *contact = NULL;
967 personas = folks_individual_get_personas (individual);
968 iter = gee_iterable_iterator (GEE_ITERABLE (personas));
969 while (contact == NULL && gee_iterator_next (iter))
971 TpfPersona *persona = gee_iterator_get (iter);
972 TpConnection *contact_conn;
973 TpContact *contact_cur = NULL;
975 if (TPF_IS_PERSONA (persona))
977 contact_cur = tpf_persona_get_contact (persona);
978 if (contact_cur != NULL)
980 contact_conn = tp_contact_get_connection (contact_cur);
982 if (!tp_strdiff (tp_proxy_get_object_path (contact_conn),
983 tp_proxy_get_object_path (conn)))
984 contact = contact_cur;
988 g_clear_object (&persona);
990 g_clear_object (&iter);
996 properties_contains (gchar **list,
998 const gchar *property)
1002 for (i = 0; i < length; i++)
1004 if (!tp_strdiff (list[i], property))
1012 check_writeable_property (TpConnection *connection,
1013 FolksIndividual *individual,
1018 gboolean retval = FALSE;
1021 FolksPersonaStore *persona_store;
1023 persona_store = FOLKS_PERSONA_STORE (
1024 empathy_dup_persona_store_for_connection (connection));
1027 folks_persona_store_get_always_writeable_properties (persona_store,
1029 retval = properties_contains (properties, prop_len, property);
1033 /* Lets see if the Individual contains a Persona with the given property */
1034 personas = folks_individual_get_personas (individual);
1035 iter = gee_iterable_iterator (GEE_ITERABLE (personas));
1036 while (!retval && gee_iterator_next (iter))
1038 FolksPersona *persona = gee_iterator_get (iter);
1041 folks_persona_get_writeable_properties (persona, &prop_len);
1042 retval = properties_contains (properties, prop_len, property);
1044 g_clear_object (&persona);
1049 g_clear_object (&iter);
1052 g_clear_object (&persona_store);
1056 /* Calculate whether the Individual can do audio or video calls.
1057 * FIXME: We can remove this once libfolks has grown capabilities support
1058 * again: bgo#626179. */
1060 empathy_individual_can_audio_video_call (FolksIndividual *individual,
1061 gboolean *can_audio_call,
1062 gboolean *can_video_call,
1063 EmpathyContact **out_contact)
1067 gboolean can_audio = FALSE, can_video = FALSE;
1069 personas = folks_individual_get_personas (individual);
1070 iter = gee_iterable_iterator (GEE_ITERABLE (personas));
1071 while (gee_iterator_next (iter))
1073 FolksPersona *persona = gee_iterator_get (iter);
1074 TpContact *tp_contact;
1076 if (!empathy_folks_persona_is_interesting (persona))
1079 tp_contact = tpf_persona_get_contact (TPF_PERSONA (persona));
1080 if (tp_contact != NULL)
1082 EmpathyContact *contact;
1084 contact = empathy_contact_dup_from_tp_contact (tp_contact);
1085 empathy_contact_set_persona (contact, persona);
1087 can_audio = can_audio || empathy_contact_get_capabilities (contact) &
1088 EMPATHY_CAPABILITIES_AUDIO;
1089 can_video = can_video || empathy_contact_get_capabilities (contact) &
1090 EMPATHY_CAPABILITIES_VIDEO;
1092 if (out_contact != NULL)
1093 *out_contact = g_object_ref (contact);
1095 g_object_unref (contact);
1099 g_clear_object (&persona);
1101 if (can_audio && can_video)
1105 g_clear_object (&iter);
1107 if (can_audio_call != NULL)
1108 *can_audio_call = can_audio;
1110 if (can_video_call != NULL)
1111 *can_video_call = can_video;
1115 empathy_client_types_contains_mobile_device (const GStrv types) {
1121 for (i = 0; types[i] != NULL; i++)
1122 if (!tp_strdiff (types[i], "phone") || !tp_strdiff (types[i], "handheld"))
1128 static FolksIndividual *
1129 create_individual_from_persona (FolksPersona *persona)
1132 FolksIndividual *individual;
1134 personas = GEE_SET (
1135 gee_hash_set_new (FOLKS_TYPE_PERSONA, g_object_ref, g_object_unref,
1136 NULL, NULL, NULL, NULL, NULL, NULL));
1138 gee_collection_add (GEE_COLLECTION (personas), persona);
1140 individual = folks_individual_new (personas);
1142 g_clear_object (&personas);
1148 empathy_create_individual_from_tp_contact (TpContact *contact)
1150 TpfPersona *persona;
1151 FolksIndividual *individual;
1153 persona = tpf_persona_dup_for_contact (contact);
1154 if (persona == NULL)
1156 DEBUG ("Failed to get a persona for %s",
1157 tp_contact_get_identifier (contact));
1161 individual = create_individual_from_persona (FOLKS_PERSONA (persona));
1163 g_object_unref (persona);
1167 /* Look for a FolksIndividual containing @contact as one of his persona
1168 * and create one if needed */
1170 empathy_ensure_individual_from_tp_contact (TpContact *contact)
1172 TpfPersona *persona;
1173 FolksIndividual *individual;
1175 persona = tpf_persona_dup_for_contact (contact);
1176 if (persona == NULL)
1178 DEBUG ("Failed to get a persona for %s",
1179 tp_contact_get_identifier (contact));
1183 individual = folks_persona_get_individual (FOLKS_PERSONA (persona));
1185 if (individual != NULL)
1186 g_object_ref (individual);
1188 individual = create_individual_from_persona (FOLKS_PERSONA (persona));
1190 g_object_unref (persona);
1194 const gchar * const *
1195 empathy_individual_get_client_types (FolksIndividual *individual)
1199 const gchar * const *types = NULL;
1200 FolksPresenceType presence_type = FOLKS_PRESENCE_TYPE_UNSET;
1202 personas = folks_individual_get_personas (individual);
1203 iter = gee_iterable_iterator (GEE_ITERABLE (personas));
1204 while (gee_iterator_next (iter))
1206 FolksPresenceDetails *presence;
1207 FolksPersona *persona = gee_iterator_get (iter);
1209 /* We only want personas which have presence and a TpContact */
1210 if (!empathy_folks_persona_is_interesting (persona))
1213 presence = FOLKS_PRESENCE_DETAILS (persona);
1215 if (folks_presence_details_typecmp (
1216 folks_presence_details_get_presence_type (presence),
1219 TpContact *tp_contact;
1221 presence_type = folks_presence_details_get_presence_type (presence);
1223 tp_contact = tpf_persona_get_contact (TPF_PERSONA (persona));
1224 if (tp_contact != NULL)
1225 types = tp_contact_get_client_types (tp_contact);
1229 g_clear_object (&persona);
1231 g_clear_object (&iter);
1237 empathy_asv_to_vardict (const GHashTable *asv)
1239 return empathy_boxed_to_variant (TP_HASH_TYPE_STRING_VARIANT_MAP, "a{sv}",
1244 empathy_boxed_to_variant (GType gtype,
1245 const gchar *variant_type,
1248 GValue v = G_VALUE_INIT;
1251 g_return_val_if_fail (boxed != NULL, NULL);
1253 g_value_init (&v, gtype);
1254 g_value_set_boxed (&v, boxed);
1256 ret = dbus_g_value_build_g_variant (&v);
1257 g_return_val_if_fail (!tp_strdiff (g_variant_get_type_string (ret),
1258 variant_type), NULL);
1262 return g_variant_ref_sink (ret);