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>
35 #include <telepathy-glib/telepathy-glib-dbus.h>
37 #include "empathy-client-factory.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_node_get_child (xmlNodePtr node
,
113 const gchar
*child_name
)
117 g_return_val_if_fail (node
!= NULL
, NULL
);
118 g_return_val_if_fail (child_name
!= NULL
, NULL
);
120 for (l
= node
->children
; l
; l
= l
->next
)
122 if (l
->name
&& strcmp ((const gchar
*) l
->name
, child_name
) == 0)
130 empathy_xml_node_get_child_content (xmlNodePtr node
,
131 const gchar
*child_name
)
135 g_return_val_if_fail (node
!= NULL
, NULL
);
136 g_return_val_if_fail (child_name
!= NULL
, NULL
);
138 l
= empathy_xml_node_get_child (node
, child_name
);
140 return xmlNodeGetContent (l
);
146 empathy_xml_node_find_child_prop_value (xmlNodePtr node
,
147 const gchar
*prop_name
,
148 const gchar
*prop_value
)
151 xmlNodePtr found
= NULL
;
153 g_return_val_if_fail (node
!= NULL
, NULL
);
154 g_return_val_if_fail (prop_name
!= NULL
, NULL
);
155 g_return_val_if_fail (prop_value
!= NULL
, NULL
);
157 for (l
= node
->children
; l
&& !found
; l
= l
->next
)
161 if (!xmlHasProp (l
, (const xmlChar
*) prop_name
))
164 prop
= xmlGetProp (l
, (const xmlChar
*) prop_name
);
165 if (prop
&& strcmp ((const gchar
*) prop
, prop_value
) == 0)
175 empathy_presence_get_default_message (TpConnectionPresenceType presence
)
179 case TP_CONNECTION_PRESENCE_TYPE_AVAILABLE
:
180 return _("Available");
181 case TP_CONNECTION_PRESENCE_TYPE_BUSY
:
183 case TP_CONNECTION_PRESENCE_TYPE_AWAY
:
184 case TP_CONNECTION_PRESENCE_TYPE_EXTENDED_AWAY
:
186 case TP_CONNECTION_PRESENCE_TYPE_HIDDEN
:
187 return _("Invisible");
188 case TP_CONNECTION_PRESENCE_TYPE_OFFLINE
:
190 case TP_CONNECTION_PRESENCE_TYPE_UNKNOWN
:
191 /* translators: presence type is unknown */
192 return C_("presence", "Unknown");
193 case TP_CONNECTION_PRESENCE_TYPE_UNSET
:
194 case TP_CONNECTION_PRESENCE_TYPE_ERROR
:
203 empathy_presence_to_str (TpConnectionPresenceType presence
)
207 for (i
= 0 ; presence_types
[i
].name
!= NULL
; i
++)
208 if (presence
== presence_types
[i
].type
)
209 return presence_types
[i
].name
;
214 TpConnectionPresenceType
215 empathy_presence_from_str (const gchar
*str
)
219 for (i
= 0 ; presence_types
[i
].name
!= NULL
; i
++)
220 if (!tp_strdiff (str
, presence_types
[i
].name
))
221 return presence_types
[i
].type
;
223 return TP_CONNECTION_PRESENCE_TYPE_UNSET
;
227 empathy_status_reason_get_default_message (TpConnectionStatusReason reason
)
231 case TP_CONNECTION_STATUS_REASON_NONE_SPECIFIED
:
232 return _("No reason specified");
233 case TP_CONNECTION_STATUS_REASON_REQUESTED
:
234 return _("Status is set to offline");
235 case TP_CONNECTION_STATUS_REASON_NETWORK_ERROR
:
236 return _("Network error");
237 case TP_CONNECTION_STATUS_REASON_AUTHENTICATION_FAILED
:
238 return _("Authentication failed");
239 case TP_CONNECTION_STATUS_REASON_ENCRYPTION_ERROR
:
240 return _("Encryption error");
241 case TP_CONNECTION_STATUS_REASON_NAME_IN_USE
:
242 return _("Name in use");
243 case TP_CONNECTION_STATUS_REASON_CERT_NOT_PROVIDED
:
244 return _("Certificate not provided");
245 case TP_CONNECTION_STATUS_REASON_CERT_UNTRUSTED
:
246 return _("Certificate untrusted");
247 case TP_CONNECTION_STATUS_REASON_CERT_EXPIRED
:
248 return _("Certificate expired");
249 case TP_CONNECTION_STATUS_REASON_CERT_NOT_ACTIVATED
:
250 return _("Certificate not activated");
251 case TP_CONNECTION_STATUS_REASON_CERT_HOSTNAME_MISMATCH
:
252 return _("Certificate hostname mismatch");
253 case TP_CONNECTION_STATUS_REASON_CERT_FINGERPRINT_MISMATCH
:
254 return _("Certificate fingerprint mismatch");
255 case TP_CONNECTION_STATUS_REASON_CERT_SELF_SIGNED
:
256 return _("Certificate self-signed");
257 case TP_CONNECTION_STATUS_REASON_CERT_OTHER_ERROR
:
258 return _("Certificate error");
260 return _("Unknown reason");
265 create_errors_to_message_hash (void)
269 errors
= g_hash_table_new (g_str_hash
, g_str_equal
);
270 g_hash_table_insert (errors
, TP_ERROR_STR_NETWORK_ERROR
, _("Network error"));
271 g_hash_table_insert (errors
, TP_ERROR_STR_AUTHENTICATION_FAILED
,
272 _("Authentication failed"));
273 g_hash_table_insert (errors
, TP_ERROR_STR_ENCRYPTION_ERROR
,
274 _("Encryption error"));
275 g_hash_table_insert (errors
, TP_ERROR_STR_CERT_NOT_PROVIDED
,
276 _("Certificate not provided"));
277 g_hash_table_insert (errors
, TP_ERROR_STR_CERT_UNTRUSTED
,
278 _("Certificate untrusted"));
279 g_hash_table_insert (errors
, TP_ERROR_STR_CERT_EXPIRED
,
280 _("Certificate expired"));
281 g_hash_table_insert (errors
, TP_ERROR_STR_CERT_NOT_ACTIVATED
,
282 _("Certificate not activated"));
283 g_hash_table_insert (errors
, TP_ERROR_STR_CERT_HOSTNAME_MISMATCH
,
284 _("Certificate hostname mismatch"));
285 g_hash_table_insert (errors
, TP_ERROR_STR_CERT_FINGERPRINT_MISMATCH
,
286 _("Certificate fingerprint mismatch"));
287 g_hash_table_insert (errors
, TP_ERROR_STR_CERT_SELF_SIGNED
,
288 _("Certificate self-signed"));
289 g_hash_table_insert (errors
, TP_ERROR_STR_CANCELLED
,
290 _("Status is set to offline"));
291 g_hash_table_insert (errors
, TP_ERROR_STR_ENCRYPTION_NOT_AVAILABLE
,
292 _("Encryption is not available"));
293 g_hash_table_insert (errors
, TP_ERROR_STR_CERT_INVALID
,
294 _("Certificate is invalid"));
295 g_hash_table_insert (errors
, TP_ERROR_STR_CONNECTION_REFUSED
,
296 _("Connection has been refused"));
297 g_hash_table_insert (errors
, TP_ERROR_STR_CONNECTION_FAILED
,
298 _("Connection can't be established"));
299 g_hash_table_insert (errors
, TP_ERROR_STR_CONNECTION_LOST
,
300 _("Connection has been lost"));
301 g_hash_table_insert (errors
, TP_ERROR_STR_ALREADY_CONNECTED
,
302 _("This account is already connected to the server"));
303 g_hash_table_insert (errors
, TP_ERROR_STR_CONNECTION_REPLACED
,
304 _("Connection has been replaced by a new connection using the "
306 g_hash_table_insert (errors
, TP_ERROR_STR_REGISTRATION_EXISTS
,
307 _("The account already exists on the server"));
308 g_hash_table_insert (errors
, TP_ERROR_STR_SERVICE_BUSY
,
309 _("Server is currently too busy to handle the connection"));
310 g_hash_table_insert (errors
, TP_ERROR_STR_CERT_REVOKED
,
311 _("Certificate has been revoked"));
312 g_hash_table_insert (errors
, TP_ERROR_STR_CERT_INSECURE
,
313 _("Certificate uses an insecure cipher algorithm or is "
314 "cryptographically weak"));
315 g_hash_table_insert (errors
, TP_ERROR_STR_CERT_LIMIT_EXCEEDED
,
316 _("The length of the server certificate, or the depth of the "
317 "server certificate chain, exceed the limits imposed by the "
318 "cryptography library"));
319 g_hash_table_insert (errors
, TP_ERROR_STR_SOFTWARE_UPGRADE_REQUIRED
,
320 _("Your software is too old"));
321 g_hash_table_insert (errors
, DBUS_ERROR_NO_REPLY
,
322 _("Internal error"));
328 empathy_dbus_error_name_get_default_message (const gchar
*error
)
330 static GHashTable
*errors_to_message
= NULL
;
335 if (G_UNLIKELY (errors_to_message
== NULL
))
336 errors_to_message
= create_errors_to_message_hash ();
338 return g_hash_table_lookup (errors_to_message
, error
);
342 empathy_account_get_error_message (TpAccount
*account
,
343 gboolean
*user_requested
)
345 const gchar
*dbus_error
;
346 const gchar
*message
;
347 const GHashTable
*details
= NULL
;
348 TpConnectionStatusReason reason
;
350 dbus_error
= tp_account_get_detailed_error (account
, &details
);
352 if (user_requested
!= NULL
)
354 if (tp_asv_get_boolean (details
, "user-requested", NULL
))
355 *user_requested
= TRUE
;
357 *user_requested
= FALSE
;
360 message
= empathy_dbus_error_name_get_default_message (dbus_error
);
364 tp_account_get_connection_status (account
, &reason
);
366 DEBUG ("Don't understand error '%s'; fallback to the status reason (%u)",
369 return empathy_status_reason_get_default_message (reason
);
373 empathy_file_lookup (const gchar
*filename
, const gchar
*subdir
)
380 path
= g_build_filename (g_getenv ("EMPATHY_SRCDIR"), subdir
, filename
, NULL
);
381 if (!g_file_test (path
, G_FILE_TEST_EXISTS
))
384 path
= g_build_filename (DATADIR
, "empathy", filename
, NULL
);
391 empathy_uint_compare (gconstpointer a
,
394 return *(guint
*) a
- *(guint
*) b
;
398 empathy_type_dbus_ao (void)
402 if (G_UNLIKELY (t
== 0))
403 t
= dbus_g_type_get_collection ("GPtrArray", DBUS_TYPE_G_OBJECT_PATH
);
409 empathy_account_manager_get_accounts_connected (gboolean
*connecting
)
411 TpAccountManager
*manager
;
413 gboolean out_connecting
= FALSE
;
414 gboolean out_connected
= FALSE
;
416 manager
= tp_account_manager_dup ();
418 if (G_UNLIKELY (!tp_proxy_is_prepared (manager
,
419 TP_ACCOUNT_MANAGER_FEATURE_CORE
)))
420 g_critical (G_STRLOC
": %s called before AccountManager ready", G_STRFUNC
);
422 accounts
= tp_account_manager_dup_valid_accounts (manager
);
424 for (l
= accounts
; l
!= NULL
; l
= l
->next
)
426 TpConnectionStatus s
= tp_account_get_connection_status (
427 TP_ACCOUNT (l
->data
), NULL
);
429 if (s
== TP_CONNECTION_STATUS_CONNECTING
)
430 out_connecting
= TRUE
;
431 else if (s
== TP_CONNECTION_STATUS_CONNECTED
)
432 out_connected
= TRUE
;
434 if (out_connecting
&& out_connected
)
438 g_list_free_full (accounts
, g_object_unref
);
439 g_object_unref (manager
);
441 if (connecting
!= NULL
)
442 *connecting
= out_connecting
;
444 return out_connected
;
447 /* Translate Folks' general presence type to the Tp presence type */
448 TpConnectionPresenceType
449 empathy_folks_presence_type_to_tp (FolksPresenceType type
)
451 return (TpConnectionPresenceType
) type
;
454 /* Returns TRUE if the given Individual contains a TpContact */
456 empathy_folks_individual_contains_contact (FolksIndividual
*individual
)
460 gboolean retval
= FALSE
;
462 g_return_val_if_fail (FOLKS_IS_INDIVIDUAL (individual
), FALSE
);
464 personas
= folks_individual_get_personas (individual
);
465 iter
= gee_iterable_iterator (GEE_ITERABLE (personas
));
466 while (!retval
&& gee_iterator_next (iter
))
468 FolksPersona
*persona
= gee_iterator_get (iter
);
469 TpContact
*contact
= NULL
;
471 if (empathy_folks_persona_is_interesting (persona
))
472 contact
= tpf_persona_get_contact (TPF_PERSONA (persona
));
474 g_clear_object (&persona
);
479 g_clear_object (&iter
);
484 /* TODO: this needs to be eliminated (and replaced in some cases with user
485 * prompts) when we break the assumption that FolksIndividuals are 1:1 with
488 /* Retrieve the EmpathyContact corresponding to the first TpContact contained
489 * within the given Individual. Note that this is a temporary convenience. See
492 empathy_contact_dup_from_folks_individual (FolksIndividual
*individual
)
496 EmpathyContact
*contact
= NULL
;
498 g_return_val_if_fail (FOLKS_IS_INDIVIDUAL (individual
), NULL
);
500 personas
= folks_individual_get_personas (individual
);
501 iter
= gee_iterable_iterator (GEE_ITERABLE (personas
));
502 while (gee_iterator_next (iter
) && (contact
== NULL
))
504 TpfPersona
*persona
= gee_iterator_get (iter
);
506 if (empathy_folks_persona_is_interesting (FOLKS_PERSONA (persona
)))
508 TpContact
*tp_contact
;
510 tp_contact
= tpf_persona_get_contact (persona
);
511 if (tp_contact
!= NULL
)
513 contact
= empathy_contact_dup_from_tp_contact (tp_contact
);
514 empathy_contact_set_persona (contact
, FOLKS_PERSONA (persona
));
517 g_clear_object (&persona
);
519 g_clear_object (&iter
);
523 DEBUG ("Can't create an EmpathyContact for Individual %s",
524 folks_individual_get_id (individual
));
530 TpChannelGroupChangeReason
531 tp_channel_group_change_reason_from_folks_groups_change_reason (
532 FolksGroupDetailsChangeReason reason
)
534 return (TpChannelGroupChangeReason
) reason
;
538 empathy_dup_persona_store_for_connection (TpConnection
*connection
)
540 FolksBackendStore
*backend_store
;
541 FolksBackend
*backend
;
542 TpfPersonaStore
*result
= NULL
;
544 backend_store
= folks_backend_store_dup ();
545 backend
= folks_backend_store_dup_backend_by_name (backend_store
,
550 GeeMapIterator
*iter
;
552 stores_map
= folks_backend_get_persona_stores (backend
);
553 iter
= gee_map_map_iterator (stores_map
);
554 while (gee_map_iterator_next (iter
))
556 TpfPersonaStore
*persona_store
= gee_map_iterator_get_value (iter
);
558 TpConnection
*conn_cur
;
560 account
= tpf_persona_store_get_account (persona_store
);
561 conn_cur
= tp_account_get_connection (account
);
562 if (conn_cur
== connection
)
563 result
= g_object_ref (persona_store
);
565 g_clear_object (&persona_store
);
567 g_clear_object (&iter
);
570 g_object_unref (backend
);
571 g_object_unref (backend_store
);
577 empathy_connection_can_add_personas (TpConnection
*connection
)
580 FolksPersonaStore
*persona_store
;
582 g_return_val_if_fail (TP_IS_CONNECTION (connection
), FALSE
);
584 if (tp_connection_get_status (connection
, NULL
) !=
585 TP_CONNECTION_STATUS_CONNECTED
)
588 persona_store
= FOLKS_PERSONA_STORE (
589 empathy_dup_persona_store_for_connection (connection
));
591 retval
= (folks_persona_store_get_can_add_personas (persona_store
) ==
592 FOLKS_MAYBE_BOOL_TRUE
);
594 g_clear_object (&persona_store
);
600 empathy_connection_can_alias_personas (TpConnection
*connection
,
601 FolksIndividual
*individual
)
605 g_return_val_if_fail (TP_IS_CONNECTION (connection
), FALSE
);
607 if (tp_connection_get_status (connection
, NULL
) !=
608 TP_CONNECTION_STATUS_CONNECTED
)
611 retval
= check_writeable_property (connection
, individual
, "alias");
617 empathy_connection_can_group_personas (TpConnection
*connection
,
618 FolksIndividual
*individual
)
622 g_return_val_if_fail (TP_IS_CONNECTION (connection
), FALSE
);
624 if (tp_connection_get_status (connection
, NULL
) !=
625 TP_CONNECTION_STATUS_CONNECTED
)
628 retval
= check_writeable_property (connection
, individual
, "groups");
634 empathy_folks_persona_is_interesting (FolksPersona
*persona
)
636 /* We're not interested in non-Telepathy personas */
637 if (!TPF_IS_PERSONA (persona
))
640 /* We're not interested in user personas which haven't been added to the
641 * contact list (see bgo#637151). */
642 if (folks_persona_get_is_user (persona
) &&
643 !tpf_persona_get_is_in_contact_list (TPF_PERSONA (persona
)))
652 empathy_get_x509_certificate_hostname (gnutls_x509_crt_t cert
)
659 /* this snippet is taken from GnuTLS.
660 * see gnutls/lib/x509/rfc2818_hostname.c
662 for (idx
= 0; res
>= 0; idx
++)
664 dns_name_size
= sizeof (dns_name
);
665 res
= gnutls_x509_crt_get_subject_alt_name (cert
, idx
,
666 dns_name
, &dns_name_size
, NULL
);
668 if (res
== GNUTLS_SAN_DNSNAME
|| res
== GNUTLS_SAN_IPADDRESS
)
669 return g_strndup (dns_name
, dns_name_size
);
672 dns_name_size
= sizeof (dns_name
);
673 res
= gnutls_x509_crt_get_dn_by_oid (cert
, GNUTLS_OID_X520_COMMON_NAME
,
674 0, 0, dns_name
, &dns_name_size
);
677 return g_strndup (dns_name
, dns_name_size
);
683 empathy_format_currency (gint amount
,
685 const gchar
*currency
)
687 #define MINUS "\342\210\222"
688 #define EURO "\342\202\254"
689 #define YEN "\302\245"
690 #define POUND "\302\243"
692 /* localised representations of currency */
693 /* FIXME: check these, especially negatives and decimals */
694 static const struct {
695 const char *currency
;
696 const char *positive
;
697 const char *negative
;
700 /* sym positive negative decimal */
701 { "EUR", EURO
"%s", MINUS EURO
"%s", "." },
702 { "USD", "$%s", MINUS
"$%s", "." },
703 { "JPY", YEN
"%s" MINUS YEN
"%s", "." },
704 { "GBP", POUND
"%s", MINUS POUND
"%s", "." },
705 { "PLN", "%s zl", MINUS
"%s zl", "." },
706 { "BRL", "R$%s", MINUS
"R$%s", "." },
707 { "SEK", "%s kr", MINUS
"%s kr", "." },
708 { "DKK", "kr %s", "kr " MINUS
"%s", "." },
709 { "HKD", "$%s", MINUS
"$%s", "." },
710 { "CHF", "%s Fr.", MINUS
"%s Fr.", "." },
711 { "NOK", "kr %s", "kr" MINUS
"%s", "," },
712 { "CAD", "$%s", MINUS
"$%s", "." },
713 { "TWD", "$%s", MINUS
"$%s", "." },
714 { "AUD", "$%s", MINUS
"$%s", "." },
717 const char *positive
= "%s";
718 const char *negative
= MINUS
"%s";
719 const char *decimal
= ".";
720 char *fmt_amount
, *money
;
723 /* get the localised currency format */
724 for (i
= 0; i
< G_N_ELEMENTS (currencies
); i
++)
726 if (!tp_strdiff (currency
, currencies
[i
].currency
))
728 positive
= currencies
[i
].positive
;
729 negative
= currencies
[i
].negative
;
730 decimal
= currencies
[i
].decimal
;
735 /* format the amount using the scale */
738 /* no decimal point required */
739 fmt_amount
= g_strdup_printf ("%d", amount
);
743 /* don't use floating point arithmatic, it's noisy;
744 * we take the absolute values, because we want the minus
745 * sign to appear before the $ */
746 int divisor
= pow (10, scale
);
747 int dollars
= abs (amount
/ divisor
);
748 int cents
= abs (amount
% divisor
);
750 fmt_amount
= g_strdup_printf ("%d%s%0*d",
751 dollars
, decimal
, scale
, cents
);
754 money
= g_strdup_printf (amount
< 0 ? negative
: positive
, fmt_amount
);
760 /* Return the TpContact on @conn associated with @individual, if any */
762 empathy_get_tp_contact_for_individual (FolksIndividual
*individual
,
765 TpContact
*contact
= NULL
;
769 personas
= folks_individual_get_personas (individual
);
770 iter
= gee_iterable_iterator (GEE_ITERABLE (personas
));
771 while (contact
== NULL
&& gee_iterator_next (iter
))
773 TpfPersona
*persona
= gee_iterator_get (iter
);
774 TpConnection
*contact_conn
;
775 TpContact
*contact_cur
= NULL
;
777 if (TPF_IS_PERSONA (persona
))
779 contact_cur
= tpf_persona_get_contact (persona
);
780 if (contact_cur
!= NULL
)
782 contact_conn
= tp_contact_get_connection (contact_cur
);
784 if (!tp_strdiff (tp_proxy_get_object_path (contact_conn
),
785 tp_proxy_get_object_path (conn
)))
786 contact
= contact_cur
;
790 g_clear_object (&persona
);
792 g_clear_object (&iter
);
798 properties_contains (gchar
**list
,
800 const gchar
*property
)
804 for (i
= 0; i
< length
; i
++)
806 if (!tp_strdiff (list
[i
], property
))
814 check_writeable_property (TpConnection
*connection
,
815 FolksIndividual
*individual
,
820 gboolean retval
= FALSE
;
823 FolksPersonaStore
*persona_store
;
825 persona_store
= FOLKS_PERSONA_STORE (
826 empathy_dup_persona_store_for_connection (connection
));
829 folks_persona_store_get_always_writeable_properties (persona_store
,
831 retval
= properties_contains (properties
, prop_len
, property
);
835 /* Lets see if the Individual contains a Persona with the given property */
836 personas
= folks_individual_get_personas (individual
);
837 iter
= gee_iterable_iterator (GEE_ITERABLE (personas
));
838 while (!retval
&& gee_iterator_next (iter
))
840 FolksPersona
*persona
= gee_iterator_get (iter
);
843 folks_persona_get_writeable_properties (persona
, &prop_len
);
844 retval
= properties_contains (properties
, prop_len
, property
);
846 g_clear_object (&persona
);
851 g_clear_object (&iter
);
854 g_clear_object (&persona_store
);
858 /* Calculate whether the Individual can do audio or video calls.
859 * FIXME: We can remove this once libfolks has grown capabilities support
860 * again: bgo#626179. */
862 empathy_individual_can_audio_video_call (FolksIndividual
*individual
,
863 gboolean
*can_audio_call
,
864 gboolean
*can_video_call
,
865 EmpathyContact
**out_contact
)
869 gboolean can_audio
= FALSE
, can_video
= FALSE
;
871 personas
= folks_individual_get_personas (individual
);
872 iter
= gee_iterable_iterator (GEE_ITERABLE (personas
));
873 while (gee_iterator_next (iter
))
875 FolksPersona
*persona
= gee_iterator_get (iter
);
876 TpContact
*tp_contact
;
878 if (!empathy_folks_persona_is_interesting (persona
))
881 tp_contact
= tpf_persona_get_contact (TPF_PERSONA (persona
));
882 if (tp_contact
!= NULL
)
884 EmpathyContact
*contact
;
886 contact
= empathy_contact_dup_from_tp_contact (tp_contact
);
887 empathy_contact_set_persona (contact
, persona
);
889 can_audio
= can_audio
|| empathy_contact_get_capabilities (contact
) &
890 EMPATHY_CAPABILITIES_AUDIO
;
891 can_video
= can_video
|| empathy_contact_get_capabilities (contact
) &
892 EMPATHY_CAPABILITIES_VIDEO
;
894 if (out_contact
!= NULL
)
895 *out_contact
= g_object_ref (contact
);
897 g_object_unref (contact
);
901 g_clear_object (&persona
);
903 if (can_audio
&& can_video
)
907 g_clear_object (&iter
);
909 if (can_audio_call
!= NULL
)
910 *can_audio_call
= can_audio
;
912 if (can_video_call
!= NULL
)
913 *can_video_call
= can_video
;
917 empathy_client_types_contains_mobile_device (const GStrv types
) {
923 for (i
= 0; types
[i
] != NULL
; i
++)
924 if (!tp_strdiff (types
[i
], "phone") || !tp_strdiff (types
[i
], "handheld"))
930 static FolksIndividual
*
931 create_individual_from_persona (FolksPersona
*persona
)
934 FolksIndividual
*individual
;
937 gee_hash_set_new (FOLKS_TYPE_PERSONA
, g_object_ref
, g_object_unref
,
938 NULL
, NULL
, NULL
, NULL
, NULL
, NULL
));
940 gee_collection_add (GEE_COLLECTION (personas
), persona
);
942 individual
= folks_individual_new (personas
);
944 g_clear_object (&personas
);
949 /* Look for a FolksIndividual containing @contact as one of his persona
950 * and create one if needed */
952 empathy_ensure_individual_from_tp_contact (TpContact
*contact
)
955 FolksIndividual
*individual
;
957 persona
= tpf_persona_dup_for_contact (contact
);
960 DEBUG ("Failed to get a persona for %s",
961 tp_contact_get_identifier (contact
));
965 individual
= folks_persona_get_individual (FOLKS_PERSONA (persona
));
967 if (individual
!= NULL
)
968 g_object_ref (individual
);
970 individual
= create_individual_from_persona (FOLKS_PERSONA (persona
));
972 g_object_unref (persona
);
976 const gchar
* const *
977 empathy_individual_get_client_types (FolksIndividual
*individual
)
981 const gchar
* const *types
= NULL
;
982 FolksPresenceType presence_type
= FOLKS_PRESENCE_TYPE_UNSET
;
984 personas
= folks_individual_get_personas (individual
);
985 iter
= gee_iterable_iterator (GEE_ITERABLE (personas
));
986 while (gee_iterator_next (iter
))
988 FolksPresenceDetails
*presence
;
989 FolksPersona
*persona
= gee_iterator_get (iter
);
991 /* We only want personas which have presence and a TpContact */
992 if (!empathy_folks_persona_is_interesting (persona
))
995 presence
= FOLKS_PRESENCE_DETAILS (persona
);
997 if (folks_presence_details_typecmp (
998 folks_presence_details_get_presence_type (presence
),
1001 TpContact
*tp_contact
;
1003 presence_type
= folks_presence_details_get_presence_type (presence
);
1005 tp_contact
= tpf_persona_get_contact (TPF_PERSONA (persona
));
1006 if (tp_contact
!= NULL
)
1007 types
= tp_contact_get_client_types (tp_contact
);
1011 g_clear_object (&persona
);
1013 g_clear_object (&iter
);
1019 empathy_asv_to_vardict (const GHashTable
*asv
)
1021 return empathy_boxed_to_variant (TP_HASH_TYPE_STRING_VARIANT_MAP
, "a{sv}",
1026 empathy_boxed_to_variant (GType gtype
,
1027 const gchar
*variant_type
,
1030 GValue v
= G_VALUE_INIT
;
1033 g_return_val_if_fail (boxed
!= NULL
, NULL
);
1035 g_value_init (&v
, gtype
);
1036 g_value_set_boxed (&v
, boxed
);
1038 ret
= dbus_g_value_build_g_variant (&v
);
1039 g_return_val_if_fail (!tp_strdiff (g_variant_get_type_string (ret
),
1040 variant_type
), NULL
);
1044 return g_variant_ref_sink (ret
);