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>
26 #include "empathy-utils.h"
28 #include <glib/gi18n-lib.h>
29 #include <dbus/dbus-protocol.h>
31 #include <telepathy-glib/telepathy-glib-dbus.h>
33 #include "empathy-client-factory.h"
34 #include "extensions.h"
38 #define DEBUG_FLAG EMPATHY_DEBUG_OTHER
39 #include "empathy-debug.h"
41 /* Translation between presence types and string */
44 TpConnectionPresenceType type
;
45 } presence_types
[] = {
46 { "available", TP_CONNECTION_PRESENCE_TYPE_AVAILABLE
},
47 { "busy", TP_CONNECTION_PRESENCE_TYPE_BUSY
},
48 { "away", TP_CONNECTION_PRESENCE_TYPE_AWAY
},
49 { "ext_away", TP_CONNECTION_PRESENCE_TYPE_EXTENDED_AWAY
},
50 { "hidden", TP_CONNECTION_PRESENCE_TYPE_HIDDEN
},
51 { "offline", TP_CONNECTION_PRESENCE_TYPE_OFFLINE
},
52 { "unset", TP_CONNECTION_PRESENCE_TYPE_UNSET
},
53 { "unknown", TP_CONNECTION_PRESENCE_TYPE_UNKNOWN
},
54 { "error", TP_CONNECTION_PRESENCE_TYPE_ERROR
},
55 /* alternative names */
56 { "dnd", TP_CONNECTION_PRESENCE_TYPE_BUSY
},
57 { "brb", TP_CONNECTION_PRESENCE_TYPE_AWAY
},
58 { "xa", TP_CONNECTION_PRESENCE_TYPE_EXTENDED_AWAY
},
63 properties_contains (gchar
**list
,
65 const gchar
*property
);
68 check_writeable_property (TpConnection
*connection
,
69 FolksIndividual
*individual
,
75 static gboolean initialized
= FALSE
;
77 EmpathyClientFactory
*factory
;
85 bindtextdomain (GETTEXT_PACKAGE
, LOCALEDIR
);
86 bind_textdomain_codeset (GETTEXT_PACKAGE
, "UTF-8");
88 /* Setup debug output for empathy and telepathy-glib */
89 if (g_getenv ("EMPATHY_TIMING") != NULL
)
90 g_log_set_default_handler (tp_debug_timestamped_log_handler
, NULL
);
92 empathy_debug_set_flags (g_getenv ("EMPATHY_DEBUG"));
93 tp_debug_divert_messages (g_getenv ("EMPATHY_LOGFILE"));
99 factory
= empathy_client_factory_dup ();
100 am
= tp_account_manager_new_with_factory (TP_SIMPLE_CLIENT_FACTORY (factory
));
101 tp_account_manager_set_default (am
);
103 g_object_unref (factory
);
108 empathy_xml_node_get_child (xmlNodePtr node
,
109 const gchar
*child_name
)
113 g_return_val_if_fail (node
!= NULL
, NULL
);
114 g_return_val_if_fail (child_name
!= NULL
, NULL
);
116 for (l
= node
->children
; l
; l
= l
->next
)
118 if (l
->name
&& strcmp ((const gchar
*) l
->name
, child_name
) == 0)
126 empathy_xml_node_get_child_content (xmlNodePtr node
,
127 const gchar
*child_name
)
131 g_return_val_if_fail (node
!= NULL
, NULL
);
132 g_return_val_if_fail (child_name
!= NULL
, NULL
);
134 l
= empathy_xml_node_get_child (node
, child_name
);
136 return xmlNodeGetContent (l
);
142 empathy_xml_node_find_child_prop_value (xmlNodePtr node
,
143 const gchar
*prop_name
,
144 const gchar
*prop_value
)
147 xmlNodePtr found
= NULL
;
149 g_return_val_if_fail (node
!= NULL
, NULL
);
150 g_return_val_if_fail (prop_name
!= NULL
, NULL
);
151 g_return_val_if_fail (prop_value
!= NULL
, NULL
);
153 for (l
= node
->children
; l
&& !found
; l
= l
->next
)
157 if (!xmlHasProp (l
, (const xmlChar
*) prop_name
))
160 prop
= xmlGetProp (l
, (const xmlChar
*) prop_name
);
161 if (prop
&& strcmp ((const gchar
*) prop
, prop_value
) == 0)
171 empathy_presence_get_default_message (TpConnectionPresenceType presence
)
175 case TP_CONNECTION_PRESENCE_TYPE_AVAILABLE
:
176 return _("Available");
177 case TP_CONNECTION_PRESENCE_TYPE_BUSY
:
179 case TP_CONNECTION_PRESENCE_TYPE_AWAY
:
180 case TP_CONNECTION_PRESENCE_TYPE_EXTENDED_AWAY
:
182 case TP_CONNECTION_PRESENCE_TYPE_HIDDEN
:
183 return _("Invisible");
184 case TP_CONNECTION_PRESENCE_TYPE_OFFLINE
:
186 case TP_CONNECTION_PRESENCE_TYPE_UNKNOWN
:
187 /* translators: presence type is unknown */
188 return C_("presence", "Unknown");
189 case TP_CONNECTION_PRESENCE_TYPE_UNSET
:
190 case TP_CONNECTION_PRESENCE_TYPE_ERROR
:
199 empathy_presence_to_str (TpConnectionPresenceType presence
)
203 for (i
= 0 ; presence_types
[i
].name
!= NULL
; i
++)
204 if (presence
== presence_types
[i
].type
)
205 return presence_types
[i
].name
;
210 TpConnectionPresenceType
211 empathy_presence_from_str (const gchar
*str
)
215 for (i
= 0 ; presence_types
[i
].name
!= NULL
; i
++)
216 if (!tp_strdiff (str
, presence_types
[i
].name
))
217 return presence_types
[i
].type
;
219 return TP_CONNECTION_PRESENCE_TYPE_UNSET
;
223 empathy_status_reason_get_default_message (TpConnectionStatusReason reason
)
227 case TP_CONNECTION_STATUS_REASON_NONE_SPECIFIED
:
228 return _("No reason specified");
229 case TP_CONNECTION_STATUS_REASON_REQUESTED
:
230 return _("Status is set to offline");
231 case TP_CONNECTION_STATUS_REASON_NETWORK_ERROR
:
232 return _("Network error");
233 case TP_CONNECTION_STATUS_REASON_AUTHENTICATION_FAILED
:
234 return _("Authentication failed");
235 case TP_CONNECTION_STATUS_REASON_ENCRYPTION_ERROR
:
236 return _("Encryption error");
237 case TP_CONNECTION_STATUS_REASON_NAME_IN_USE
:
238 return _("Name in use");
239 case TP_CONNECTION_STATUS_REASON_CERT_NOT_PROVIDED
:
240 return _("Certificate not provided");
241 case TP_CONNECTION_STATUS_REASON_CERT_UNTRUSTED
:
242 return _("Certificate untrusted");
243 case TP_CONNECTION_STATUS_REASON_CERT_EXPIRED
:
244 return _("Certificate expired");
245 case TP_CONNECTION_STATUS_REASON_CERT_NOT_ACTIVATED
:
246 return _("Certificate not activated");
247 case TP_CONNECTION_STATUS_REASON_CERT_HOSTNAME_MISMATCH
:
248 return _("Certificate hostname mismatch");
249 case TP_CONNECTION_STATUS_REASON_CERT_FINGERPRINT_MISMATCH
:
250 return _("Certificate fingerprint mismatch");
251 case TP_CONNECTION_STATUS_REASON_CERT_SELF_SIGNED
:
252 return _("Certificate self-signed");
253 case TP_CONNECTION_STATUS_REASON_CERT_OTHER_ERROR
:
254 return _("Certificate error");
256 return _("Unknown reason");
261 create_errors_to_message_hash (void)
265 errors
= g_hash_table_new (g_str_hash
, g_str_equal
);
266 g_hash_table_insert (errors
, TP_ERROR_STR_NETWORK_ERROR
, _("Network error"));
267 g_hash_table_insert (errors
, TP_ERROR_STR_AUTHENTICATION_FAILED
,
268 _("Authentication failed"));
269 g_hash_table_insert (errors
, TP_ERROR_STR_ENCRYPTION_ERROR
,
270 _("Encryption error"));
271 g_hash_table_insert (errors
, TP_ERROR_STR_CERT_NOT_PROVIDED
,
272 _("Certificate not provided"));
273 g_hash_table_insert (errors
, TP_ERROR_STR_CERT_UNTRUSTED
,
274 _("Certificate untrusted"));
275 g_hash_table_insert (errors
, TP_ERROR_STR_CERT_EXPIRED
,
276 _("Certificate expired"));
277 g_hash_table_insert (errors
, TP_ERROR_STR_CERT_NOT_ACTIVATED
,
278 _("Certificate not activated"));
279 g_hash_table_insert (errors
, TP_ERROR_STR_CERT_HOSTNAME_MISMATCH
,
280 _("Certificate hostname mismatch"));
281 g_hash_table_insert (errors
, TP_ERROR_STR_CERT_FINGERPRINT_MISMATCH
,
282 _("Certificate fingerprint mismatch"));
283 g_hash_table_insert (errors
, TP_ERROR_STR_CERT_SELF_SIGNED
,
284 _("Certificate self-signed"));
285 g_hash_table_insert (errors
, TP_ERROR_STR_CANCELLED
,
286 _("Status is set to offline"));
287 g_hash_table_insert (errors
, TP_ERROR_STR_ENCRYPTION_NOT_AVAILABLE
,
288 _("Encryption is not available"));
289 g_hash_table_insert (errors
, TP_ERROR_STR_CERT_INVALID
,
290 _("Certificate is invalid"));
291 g_hash_table_insert (errors
, TP_ERROR_STR_CONNECTION_REFUSED
,
292 _("Connection has been refused"));
293 g_hash_table_insert (errors
, TP_ERROR_STR_CONNECTION_FAILED
,
294 _("Connection can’t be established"));
295 g_hash_table_insert (errors
, TP_ERROR_STR_CONNECTION_LOST
,
296 _("Connection has been lost"));
297 g_hash_table_insert (errors
, TP_ERROR_STR_ALREADY_CONNECTED
,
298 _("This account is already connected to the server"));
299 g_hash_table_insert (errors
, TP_ERROR_STR_CONNECTION_REPLACED
,
300 _("Connection has been replaced by a new connection using the "
302 g_hash_table_insert (errors
, TP_ERROR_STR_REGISTRATION_EXISTS
,
303 _("The account already exists on the server"));
304 g_hash_table_insert (errors
, TP_ERROR_STR_SERVICE_BUSY
,
305 _("Server is currently too busy to handle the connection"));
306 g_hash_table_insert (errors
, TP_ERROR_STR_CERT_REVOKED
,
307 _("Certificate has been revoked"));
308 g_hash_table_insert (errors
, TP_ERROR_STR_CERT_INSECURE
,
309 _("Certificate uses an insecure cipher algorithm or is "
310 "cryptographically weak"));
311 g_hash_table_insert (errors
, TP_ERROR_STR_CERT_LIMIT_EXCEEDED
,
312 _("The length of the server certificate, or the depth of the "
313 "server certificate chain, exceed the limits imposed by the "
314 "cryptography library"));
315 g_hash_table_insert (errors
, TP_ERROR_STR_SOFTWARE_UPGRADE_REQUIRED
,
316 _("Your software is too old"));
317 g_hash_table_insert (errors
, DBUS_ERROR_NO_REPLY
,
318 _("Internal error"));
324 empathy_dbus_error_name_get_default_message (const gchar
*error
)
326 static GHashTable
*errors_to_message
= NULL
;
331 if (G_UNLIKELY (errors_to_message
== NULL
))
332 errors_to_message
= create_errors_to_message_hash ();
334 return g_hash_table_lookup (errors_to_message
, error
);
338 empathy_account_get_error_message (TpAccount
*account
,
339 gboolean
*user_requested
)
341 const gchar
*dbus_error
;
342 const gchar
*message
;
343 const GHashTable
*details
= NULL
;
344 TpConnectionStatusReason reason
;
346 dbus_error
= tp_account_get_detailed_error (account
, &details
);
348 if (user_requested
!= NULL
)
350 if (tp_asv_get_boolean (details
, "user-requested", NULL
))
351 *user_requested
= TRUE
;
353 *user_requested
= FALSE
;
356 message
= empathy_dbus_error_name_get_default_message (dbus_error
);
360 tp_account_get_connection_status (account
, &reason
);
362 DEBUG ("Don't understand error '%s'; fallback to the status reason (%u)",
365 return empathy_status_reason_get_default_message (reason
);
369 empathy_file_lookup (const gchar
*filename
, const gchar
*subdir
)
376 path
= g_build_filename (g_getenv ("EMPATHY_SRCDIR"), subdir
, filename
, NULL
);
377 if (!g_file_test (path
, G_FILE_TEST_EXISTS
))
380 path
= g_build_filename (DATADIR
, "empathy", filename
, NULL
);
387 empathy_uint_compare (gconstpointer a
,
390 return *(guint
*) a
- *(guint
*) b
;
394 empathy_type_dbus_ao (void)
398 if (G_UNLIKELY (t
== 0))
399 t
= dbus_g_type_get_collection ("GPtrArray", DBUS_TYPE_G_OBJECT_PATH
);
405 empathy_account_manager_get_accounts_connected (gboolean
*connecting
)
407 TpAccountManager
*manager
;
409 gboolean out_connecting
= FALSE
;
410 gboolean out_connected
= FALSE
;
412 manager
= tp_account_manager_dup ();
414 if (G_UNLIKELY (!tp_proxy_is_prepared (manager
,
415 TP_ACCOUNT_MANAGER_FEATURE_CORE
)))
416 g_critical (G_STRLOC
": %s called before AccountManager ready", G_STRFUNC
);
418 accounts
= tp_account_manager_dup_valid_accounts (manager
);
420 for (l
= accounts
; l
!= NULL
; l
= l
->next
)
422 TpConnectionStatus s
= tp_account_get_connection_status (
423 TP_ACCOUNT (l
->data
), NULL
);
425 if (s
== TP_CONNECTION_STATUS_CONNECTING
)
426 out_connecting
= TRUE
;
427 else if (s
== TP_CONNECTION_STATUS_CONNECTED
)
428 out_connected
= TRUE
;
430 if (out_connecting
&& out_connected
)
434 g_list_free_full (accounts
, g_object_unref
);
435 g_object_unref (manager
);
437 if (connecting
!= NULL
)
438 *connecting
= out_connecting
;
440 return out_connected
;
443 /* Translate Folks' general presence type to the Tp presence type */
444 TpConnectionPresenceType
445 empathy_folks_presence_type_to_tp (FolksPresenceType type
)
447 return (TpConnectionPresenceType
) type
;
450 /* Returns TRUE if the given Individual contains a TpContact */
452 empathy_folks_individual_contains_contact (FolksIndividual
*individual
)
456 gboolean retval
= FALSE
;
458 g_return_val_if_fail (FOLKS_IS_INDIVIDUAL (individual
), FALSE
);
460 personas
= folks_individual_get_personas (individual
);
461 iter
= gee_iterable_iterator (GEE_ITERABLE (personas
));
462 while (!retval
&& gee_iterator_next (iter
))
464 FolksPersona
*persona
= gee_iterator_get (iter
);
465 TpContact
*contact
= NULL
;
467 if (empathy_folks_persona_is_interesting (persona
))
468 contact
= tpf_persona_get_contact (TPF_PERSONA (persona
));
470 g_clear_object (&persona
);
475 g_clear_object (&iter
);
480 /* TODO: this needs to be eliminated (and replaced in some cases with user
481 * prompts) when we break the assumption that FolksIndividuals are 1:1 with
484 /* Retrieve the EmpathyContact corresponding to the first TpContact contained
485 * within the given Individual. Note that this is a temporary convenience. See
488 empathy_contact_dup_from_folks_individual (FolksIndividual
*individual
)
492 EmpathyContact
*contact
= NULL
;
494 g_return_val_if_fail (FOLKS_IS_INDIVIDUAL (individual
), NULL
);
496 personas
= folks_individual_get_personas (individual
);
497 iter
= gee_iterable_iterator (GEE_ITERABLE (personas
));
498 while (gee_iterator_next (iter
) && (contact
== NULL
))
500 TpfPersona
*persona
= gee_iterator_get (iter
);
502 if (empathy_folks_persona_is_interesting (FOLKS_PERSONA (persona
)))
504 TpContact
*tp_contact
;
506 tp_contact
= tpf_persona_get_contact (persona
);
507 if (tp_contact
!= NULL
)
509 contact
= empathy_contact_dup_from_tp_contact (tp_contact
);
510 empathy_contact_set_persona (contact
, FOLKS_PERSONA (persona
));
513 g_clear_object (&persona
);
515 g_clear_object (&iter
);
519 DEBUG ("Can't create an EmpathyContact for Individual %s",
520 folks_individual_get_id (individual
));
526 TpChannelGroupChangeReason
527 tp_channel_group_change_reason_from_folks_groups_change_reason (
528 FolksGroupDetailsChangeReason reason
)
530 return (TpChannelGroupChangeReason
) reason
;
534 empathy_dup_persona_store_for_connection (TpConnection
*connection
)
536 FolksBackendStore
*backend_store
;
537 FolksBackend
*backend
;
538 TpfPersonaStore
*result
= NULL
;
540 backend_store
= folks_backend_store_dup ();
541 backend
= folks_backend_store_dup_backend_by_name (backend_store
,
546 GeeMapIterator
*iter
;
548 stores_map
= folks_backend_get_persona_stores (backend
);
549 iter
= gee_map_map_iterator (stores_map
);
550 while (gee_map_iterator_next (iter
))
552 TpfPersonaStore
*persona_store
= gee_map_iterator_get_value (iter
);
554 TpConnection
*conn_cur
;
556 account
= tpf_persona_store_get_account (persona_store
);
557 conn_cur
= tp_account_get_connection (account
);
558 if (conn_cur
== connection
)
559 result
= g_object_ref (persona_store
);
561 g_clear_object (&persona_store
);
563 g_clear_object (&iter
);
566 g_object_unref (backend
);
567 g_object_unref (backend_store
);
573 empathy_connection_can_add_personas (TpConnection
*connection
)
576 FolksPersonaStore
*persona_store
;
578 g_return_val_if_fail (TP_IS_CONNECTION (connection
), FALSE
);
580 if (tp_connection_get_status (connection
, NULL
) !=
581 TP_CONNECTION_STATUS_CONNECTED
)
584 persona_store
= FOLKS_PERSONA_STORE (
585 empathy_dup_persona_store_for_connection (connection
));
587 retval
= (folks_persona_store_get_can_add_personas (persona_store
) ==
588 FOLKS_MAYBE_BOOL_TRUE
);
590 g_clear_object (&persona_store
);
596 empathy_connection_can_alias_personas (TpConnection
*connection
,
597 FolksIndividual
*individual
)
601 g_return_val_if_fail (TP_IS_CONNECTION (connection
), FALSE
);
603 if (tp_connection_get_status (connection
, NULL
) !=
604 TP_CONNECTION_STATUS_CONNECTED
)
607 retval
= check_writeable_property (connection
, individual
, "alias");
613 empathy_connection_can_group_personas (TpConnection
*connection
,
614 FolksIndividual
*individual
)
618 g_return_val_if_fail (TP_IS_CONNECTION (connection
), FALSE
);
620 if (tp_connection_get_status (connection
, NULL
) !=
621 TP_CONNECTION_STATUS_CONNECTED
)
624 retval
= check_writeable_property (connection
, individual
, "groups");
630 empathy_folks_persona_is_interesting (FolksPersona
*persona
)
632 /* We're not interested in non-Telepathy personas */
633 if (!TPF_IS_PERSONA (persona
))
636 /* We're not interested in user personas which haven't been added to the
637 * contact list (see bgo#637151). */
638 if (folks_persona_get_is_user (persona
) &&
639 !tpf_persona_get_is_in_contact_list (TPF_PERSONA (persona
)))
648 empathy_format_currency (gint amount
,
650 const gchar
*currency
)
652 #define MINUS "\342\210\222"
653 #define EURO "\342\202\254"
654 #define YEN "\302\245"
655 #define POUND "\302\243"
657 /* localised representations of currency */
658 /* FIXME: check these, especially negatives and decimals */
659 static const struct {
660 const char *currency
;
661 const char *positive
;
662 const char *negative
;
665 /* sym positive negative decimal */
666 { "EUR", EURO
"%s", MINUS EURO
"%s", "." },
667 { "USD", "$%s", MINUS
"$%s", "." },
668 { "JPY", YEN
"%s" MINUS YEN
"%s", "." },
669 { "GBP", POUND
"%s", MINUS POUND
"%s", "." },
670 { "PLN", "%s zl", MINUS
"%s zl", "." },
671 { "BRL", "R$%s", MINUS
"R$%s", "." },
672 { "SEK", "%s kr", MINUS
"%s kr", "." },
673 { "DKK", "kr %s", "kr " MINUS
"%s", "." },
674 { "HKD", "$%s", MINUS
"$%s", "." },
675 { "CHF", "%s Fr.", MINUS
"%s Fr.", "." },
676 { "NOK", "kr %s", "kr" MINUS
"%s", "," },
677 { "CAD", "$%s", MINUS
"$%s", "." },
678 { "TWD", "$%s", MINUS
"$%s", "." },
679 { "AUD", "$%s", MINUS
"$%s", "." },
682 const char *positive
= "%s";
683 const char *negative
= MINUS
"%s";
684 const char *decimal
= ".";
685 char *fmt_amount
, *money
;
688 /* get the localised currency format */
689 for (i
= 0; i
< G_N_ELEMENTS (currencies
); i
++)
691 if (!tp_strdiff (currency
, currencies
[i
].currency
))
693 positive
= currencies
[i
].positive
;
694 negative
= currencies
[i
].negative
;
695 decimal
= currencies
[i
].decimal
;
700 /* format the amount using the scale */
703 /* no decimal point required */
704 fmt_amount
= g_strdup_printf ("%d", amount
);
708 /* don't use floating point arithmatic, it's noisy;
709 * we take the absolute values, because we want the minus
710 * sign to appear before the $ */
711 int divisor
= pow (10, scale
);
712 int dollars
= abs (amount
/ divisor
);
713 int cents
= abs (amount
% divisor
);
715 fmt_amount
= g_strdup_printf ("%d%s%0*d",
716 dollars
, decimal
, scale
, cents
);
719 money
= g_strdup_printf (amount
< 0 ? negative
: positive
, fmt_amount
);
725 /* Return the TpContact on @conn associated with @individual, if any */
727 empathy_get_tp_contact_for_individual (FolksIndividual
*individual
,
730 TpContact
*contact
= NULL
;
734 personas
= folks_individual_get_personas (individual
);
735 iter
= gee_iterable_iterator (GEE_ITERABLE (personas
));
736 while (contact
== NULL
&& gee_iterator_next (iter
))
738 TpfPersona
*persona
= gee_iterator_get (iter
);
739 TpConnection
*contact_conn
;
740 TpContact
*contact_cur
= NULL
;
742 if (TPF_IS_PERSONA (persona
))
744 contact_cur
= tpf_persona_get_contact (persona
);
745 if (contact_cur
!= NULL
)
747 contact_conn
= tp_contact_get_connection (contact_cur
);
749 if (!tp_strdiff (tp_proxy_get_object_path (contact_conn
),
750 tp_proxy_get_object_path (conn
)))
751 contact
= contact_cur
;
755 g_clear_object (&persona
);
757 g_clear_object (&iter
);
763 properties_contains (gchar
**list
,
765 const gchar
*property
)
769 for (i
= 0; i
< length
; i
++)
771 if (!tp_strdiff (list
[i
], property
))
779 check_writeable_property (TpConnection
*connection
,
780 FolksIndividual
*individual
,
785 gboolean retval
= FALSE
;
788 FolksPersonaStore
*persona_store
;
790 persona_store
= FOLKS_PERSONA_STORE (
791 empathy_dup_persona_store_for_connection (connection
));
794 folks_persona_store_get_always_writeable_properties (persona_store
,
796 retval
= properties_contains (properties
, prop_len
, property
);
800 /* Lets see if the Individual contains a Persona with the given property */
801 personas
= folks_individual_get_personas (individual
);
802 iter
= gee_iterable_iterator (GEE_ITERABLE (personas
));
803 while (!retval
&& gee_iterator_next (iter
))
805 FolksPersona
*persona
= gee_iterator_get (iter
);
808 folks_persona_get_writeable_properties (persona
, &prop_len
);
809 retval
= properties_contains (properties
, prop_len
, property
);
811 g_clear_object (&persona
);
816 g_clear_object (&iter
);
819 g_clear_object (&persona_store
);
823 /* Calculate whether the Individual can do audio or video calls.
824 * FIXME: We can remove this once libfolks has grown capabilities support
825 * again: bgo#626179. */
827 empathy_individual_can_audio_video_call (FolksIndividual
*individual
,
828 gboolean
*can_audio_call
,
829 gboolean
*can_video_call
,
830 EmpathyContact
**out_contact
)
834 gboolean can_audio
= FALSE
, can_video
= FALSE
;
836 personas
= folks_individual_get_personas (individual
);
837 iter
= gee_iterable_iterator (GEE_ITERABLE (personas
));
838 while (gee_iterator_next (iter
))
840 FolksPersona
*persona
= gee_iterator_get (iter
);
841 TpContact
*tp_contact
;
843 if (!empathy_folks_persona_is_interesting (persona
))
846 tp_contact
= tpf_persona_get_contact (TPF_PERSONA (persona
));
847 if (tp_contact
!= NULL
)
849 EmpathyContact
*contact
;
851 contact
= empathy_contact_dup_from_tp_contact (tp_contact
);
852 empathy_contact_set_persona (contact
, persona
);
854 can_audio
= can_audio
|| empathy_contact_get_capabilities (contact
) &
855 EMPATHY_CAPABILITIES_AUDIO
;
856 can_video
= can_video
|| empathy_contact_get_capabilities (contact
) &
857 EMPATHY_CAPABILITIES_VIDEO
;
859 if (out_contact
!= NULL
)
860 *out_contact
= g_object_ref (contact
);
862 g_object_unref (contact
);
866 g_clear_object (&persona
);
868 if (can_audio
&& can_video
)
872 g_clear_object (&iter
);
874 if (can_audio_call
!= NULL
)
875 *can_audio_call
= can_audio
;
877 if (can_video_call
!= NULL
)
878 *can_video_call
= can_video
;
882 empathy_client_types_contains_mobile_device (const GStrv types
) {
888 for (i
= 0; types
[i
] != NULL
; i
++)
889 if (!tp_strdiff (types
[i
], "phone") || !tp_strdiff (types
[i
], "handheld"))
895 static FolksIndividual
*
896 create_individual_from_persona (FolksPersona
*persona
)
899 FolksIndividual
*individual
;
902 gee_hash_set_new (FOLKS_TYPE_PERSONA
, g_object_ref
, g_object_unref
,
903 NULL
, NULL
, NULL
, NULL
, NULL
, NULL
));
905 gee_collection_add (GEE_COLLECTION (personas
), persona
);
907 individual
= folks_individual_new (personas
);
909 g_clear_object (&personas
);
914 /* Look for a FolksIndividual containing @contact as one of his persona
915 * and create one if needed */
917 empathy_ensure_individual_from_tp_contact (TpContact
*contact
)
920 FolksIndividual
*individual
;
922 persona
= tpf_persona_dup_for_contact (contact
);
925 DEBUG ("Failed to get a persona for %s",
926 tp_contact_get_identifier (contact
));
930 individual
= folks_persona_get_individual (FOLKS_PERSONA (persona
));
932 if (individual
!= NULL
)
933 g_object_ref (individual
);
935 individual
= create_individual_from_persona (FOLKS_PERSONA (persona
));
937 g_object_unref (persona
);
941 const gchar
* const *
942 empathy_individual_get_client_types (FolksIndividual
*individual
)
946 const gchar
* const *types
= NULL
;
947 FolksPresenceType presence_type
= FOLKS_PRESENCE_TYPE_UNSET
;
949 personas
= folks_individual_get_personas (individual
);
950 iter
= gee_iterable_iterator (GEE_ITERABLE (personas
));
951 while (gee_iterator_next (iter
))
953 FolksPresenceDetails
*presence
;
954 FolksPersona
*persona
= gee_iterator_get (iter
);
956 /* We only want personas which have presence and a TpContact */
957 if (!empathy_folks_persona_is_interesting (persona
))
960 presence
= FOLKS_PRESENCE_DETAILS (persona
);
962 if (folks_presence_details_typecmp (
963 folks_presence_details_get_presence_type (presence
),
966 TpContact
*tp_contact
;
968 presence_type
= folks_presence_details_get_presence_type (presence
);
970 tp_contact
= tpf_persona_get_contact (TPF_PERSONA (persona
));
971 if (tp_contact
!= NULL
)
972 types
= tp_contact_get_client_types (tp_contact
);
976 g_clear_object (&persona
);
978 g_clear_object (&iter
);
984 empathy_asv_to_vardict (const GHashTable
*asv
)
986 return empathy_boxed_to_variant (TP_HASH_TYPE_STRING_VARIANT_MAP
, "a{sv}",
991 empathy_boxed_to_variant (GType gtype
,
992 const gchar
*variant_type
,
995 GValue v
= G_VALUE_INIT
;
998 g_return_val_if_fail (boxed
!= NULL
, NULL
);
1000 g_value_init (&v
, gtype
);
1001 g_value_set_boxed (&v
, boxed
);
1003 ret
= dbus_g_value_build_g_variant (&v
);
1004 g_return_val_if_fail (!tp_strdiff (g_variant_get_type_string (ret
),
1005 variant_type
), NULL
);
1009 return g_variant_ref_sink (ret
);