Help: Use stable 'if' namespace instead of experimental
[empathy-mirror.git] / libempathy / empathy-utils.c
blob3bd394ac0741d9943b4de28169d24b892aca3706
1 /*
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>
25 #include "config.h"
26 #include "empathy-utils.h"
28 #include <glib/gi18n-lib.h>
29 #include <dbus/dbus-protocol.h>
30 #include <math.h>
31 #include <telepathy-glib/telepathy-glib-dbus.h>
33 #include "empathy-client-factory.h"
34 #include "extensions.h"
36 #include <math.h>
38 #define DEBUG_FLAG EMPATHY_DEBUG_OTHER
39 #include "empathy-debug.h"
41 /* Translation between presence types and string */
42 static struct {
43 const gchar *name;
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 },
59 { NULL, },
62 static gboolean
63 properties_contains (gchar **list,
64 gint length,
65 const gchar *property);
67 static gboolean
68 check_writeable_property (TpConnection *connection,
69 FolksIndividual *individual,
70 gchar *property);
72 void
73 empathy_init (void)
75 static gboolean initialized = FALSE;
76 TpAccountManager *am;
77 EmpathyClientFactory *factory;
79 if (initialized)
80 return;
82 g_type_init ();
84 /* Setup gettext */
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"));
95 emp_cli_init ();
97 initialized = TRUE;
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);
104 g_object_unref (am);
107 xmlNodePtr
108 empathy_xml_node_get_child (xmlNodePtr node,
109 const gchar *child_name)
111 xmlNodePtr l;
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)
119 return l;
122 return NULL;
125 xmlChar *
126 empathy_xml_node_get_child_content (xmlNodePtr node,
127 const gchar *child_name)
129 xmlNodePtr l;
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);
135 if (l != NULL)
136 return xmlNodeGetContent (l);
138 return NULL;
141 xmlNodePtr
142 empathy_xml_node_find_child_prop_value (xmlNodePtr node,
143 const gchar *prop_name,
144 const gchar *prop_value)
146 xmlNodePtr l;
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)
155 xmlChar *prop;
157 if (!xmlHasProp (l, (const xmlChar *) prop_name))
158 continue;
160 prop = xmlGetProp (l, (const xmlChar *) prop_name);
161 if (prop && strcmp ((const gchar *) prop, prop_value) == 0)
162 found = l;
164 xmlFree (prop);
167 return found;
170 const gchar *
171 empathy_presence_get_default_message (TpConnectionPresenceType presence)
173 switch (presence)
175 case TP_CONNECTION_PRESENCE_TYPE_AVAILABLE:
176 return _("Available");
177 case TP_CONNECTION_PRESENCE_TYPE_BUSY:
178 return _("Busy");
179 case TP_CONNECTION_PRESENCE_TYPE_AWAY:
180 case TP_CONNECTION_PRESENCE_TYPE_EXTENDED_AWAY:
181 return _("Away");
182 case TP_CONNECTION_PRESENCE_TYPE_HIDDEN:
183 return _("Invisible");
184 case TP_CONNECTION_PRESENCE_TYPE_OFFLINE:
185 return _("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:
191 default:
192 return NULL;
195 return NULL;
198 const gchar *
199 empathy_presence_to_str (TpConnectionPresenceType presence)
201 int i;
203 for (i = 0 ; presence_types[i].name != NULL; i++)
204 if (presence == presence_types[i].type)
205 return presence_types[i].name;
207 return NULL;
210 TpConnectionPresenceType
211 empathy_presence_from_str (const gchar *str)
213 int i;
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;
222 static const gchar *
223 empathy_status_reason_get_default_message (TpConnectionStatusReason reason)
225 switch (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");
255 default:
256 return _("Unknown reason");
260 static GHashTable *
261 create_errors_to_message_hash (void)
263 GHashTable *errors;
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 "
301 "same resource"));
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"));
320 return errors;
323 static const gchar *
324 empathy_dbus_error_name_get_default_message (const gchar *error)
326 static GHashTable *errors_to_message = NULL;
328 if (error == NULL)
329 return 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);
337 const gchar *
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;
352 else
353 *user_requested = FALSE;
356 message = empathy_dbus_error_name_get_default_message (dbus_error);
357 if (message != NULL)
358 return message;
360 tp_account_get_connection_status (account, &reason);
362 DEBUG ("Don't understand error '%s'; fallback to the status reason (%u)",
363 dbus_error, reason);
365 return empathy_status_reason_get_default_message (reason);
368 gchar *
369 empathy_file_lookup (const gchar *filename, const gchar *subdir)
371 gchar *path;
373 if (subdir == NULL)
374 subdir = ".";
376 path = g_build_filename (g_getenv ("EMPATHY_SRCDIR"), subdir, filename, NULL);
377 if (!g_file_test (path, G_FILE_TEST_EXISTS))
379 g_free (path);
380 path = g_build_filename (DATADIR, "empathy", filename, NULL);
383 return path;
386 gint
387 empathy_uint_compare (gconstpointer a,
388 gconstpointer b)
390 return *(guint *) a - *(guint *) b;
393 GType
394 empathy_type_dbus_ao (void)
396 static GType t = 0;
398 if (G_UNLIKELY (t == 0))
399 t = dbus_g_type_get_collection ("GPtrArray", DBUS_TYPE_G_OBJECT_PATH);
401 return t;
404 gboolean
405 empathy_account_manager_get_accounts_connected (gboolean *connecting)
407 TpAccountManager *manager;
408 GList *accounts, *l;
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)
431 break;
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 */
451 gboolean
452 empathy_folks_individual_contains_contact (FolksIndividual *individual)
454 GeeSet *personas;
455 GeeIterator *iter;
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);
472 if (contact != NULL)
473 retval = TRUE;
475 g_clear_object (&iter);
477 return retval;
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
482 * TpContacts */
484 /* Retrieve the EmpathyContact corresponding to the first TpContact contained
485 * within the given Individual. Note that this is a temporary convenience. See
486 * the TODO above. */
487 EmpathyContact *
488 empathy_contact_dup_from_folks_individual (FolksIndividual *individual)
490 GeeSet *personas;
491 GeeIterator *iter;
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);
517 if (contact == NULL)
519 DEBUG ("Can't create an EmpathyContact for Individual %s",
520 folks_individual_get_id (individual));
523 return contact;
526 TpChannelGroupChangeReason
527 tp_channel_group_change_reason_from_folks_groups_change_reason (
528 FolksGroupDetailsChangeReason reason)
530 return (TpChannelGroupChangeReason) reason;
533 TpfPersonaStore *
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,
542 "telepathy");
543 if (backend != NULL)
545 GeeMap *stores_map;
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);
553 TpAccount *account;
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);
569 return result;
572 gboolean
573 empathy_connection_can_add_personas (TpConnection *connection)
575 gboolean retval;
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)
582 return FALSE;
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);
592 return retval;
595 gboolean
596 empathy_connection_can_alias_personas (TpConnection *connection,
597 FolksIndividual *individual)
599 gboolean retval;
601 g_return_val_if_fail (TP_IS_CONNECTION (connection), FALSE);
603 if (tp_connection_get_status (connection, NULL) !=
604 TP_CONNECTION_STATUS_CONNECTED)
605 return FALSE;
607 retval = check_writeable_property (connection, individual, "alias");
609 return retval;
612 gboolean
613 empathy_connection_can_group_personas (TpConnection *connection,
614 FolksIndividual *individual)
616 gboolean retval;
618 g_return_val_if_fail (TP_IS_CONNECTION (connection), FALSE);
620 if (tp_connection_get_status (connection, NULL) !=
621 TP_CONNECTION_STATUS_CONNECTED)
622 return FALSE;
624 retval = check_writeable_property (connection, individual, "groups");
626 return retval;
629 gboolean
630 empathy_folks_persona_is_interesting (FolksPersona *persona)
632 /* We're not interested in non-Telepathy personas */
633 if (!TPF_IS_PERSONA (persona))
634 return FALSE;
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)))
641 return FALSE;
644 return TRUE;
647 gchar *
648 empathy_format_currency (gint amount,
649 guint scale,
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;
663 const char *decimal;
664 } currencies[] = {
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;
686 guint i;
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;
696 break;
700 /* format the amount using the scale */
701 if (scale == 0)
703 /* no decimal point required */
704 fmt_amount = g_strdup_printf ("%d", amount);
706 else
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);
720 g_free (fmt_amount);
722 return money;
725 /* Return the TpContact on @conn associated with @individual, if any */
726 TpContact *
727 empathy_get_tp_contact_for_individual (FolksIndividual *individual,
728 TpConnection *conn)
730 TpContact *contact = NULL;
731 GeeSet *personas;
732 GeeIterator *iter;
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);
759 return contact;
762 static gboolean
763 properties_contains (gchar **list,
764 gint length,
765 const gchar *property)
767 gint i;
769 for (i = 0; i < length; i++)
771 if (!tp_strdiff (list[i], property))
772 return TRUE;
775 return FALSE;
778 static gboolean
779 check_writeable_property (TpConnection *connection,
780 FolksIndividual *individual,
781 gchar *property)
783 gchar **properties;
784 gint prop_len;
785 gboolean retval = FALSE;
786 GeeSet *personas;
787 GeeIterator *iter;
788 FolksPersonaStore *persona_store;
790 persona_store = FOLKS_PERSONA_STORE (
791 empathy_dup_persona_store_for_connection (connection));
793 properties =
794 folks_persona_store_get_always_writeable_properties (persona_store,
795 &prop_len);
796 retval = properties_contains (properties, prop_len, property);
797 if (retval == TRUE)
798 goto out;
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);
807 properties =
808 folks_persona_get_writeable_properties (persona, &prop_len);
809 retval = properties_contains (properties, prop_len, property);
811 g_clear_object (&persona);
813 if (retval == TRUE)
814 break;
816 g_clear_object (&iter);
818 out:
819 g_clear_object (&persona_store);
820 return retval;
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. */
826 void
827 empathy_individual_can_audio_video_call (FolksIndividual *individual,
828 gboolean *can_audio_call,
829 gboolean *can_video_call,
830 EmpathyContact **out_contact)
832 GeeSet *personas;
833 GeeIterator *iter;
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))
844 goto while_finish;
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);
865 while_finish:
866 g_clear_object (&persona);
868 if (can_audio && can_video)
869 break;
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;
881 gboolean
882 empathy_client_types_contains_mobile_device (const GStrv types) {
883 int i;
885 if (types == NULL)
886 return FALSE;
888 for (i = 0; types[i] != NULL; i++)
889 if (!tp_strdiff (types[i], "phone") || !tp_strdiff (types[i], "handheld"))
890 return TRUE;
892 return FALSE;
895 static FolksIndividual *
896 create_individual_from_persona (FolksPersona *persona)
898 GeeSet *personas;
899 FolksIndividual *individual;
901 personas = GEE_SET (
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);
911 return individual;
914 /* Look for a FolksIndividual containing @contact as one of his persona
915 * and create one if needed */
916 FolksIndividual *
917 empathy_ensure_individual_from_tp_contact (TpContact *contact)
919 TpfPersona *persona;
920 FolksIndividual *individual;
922 persona = tpf_persona_dup_for_contact (contact);
923 if (persona == NULL)
925 DEBUG ("Failed to get a persona for %s",
926 tp_contact_get_identifier (contact));
927 return NULL;
930 individual = folks_persona_get_individual (FOLKS_PERSONA (persona));
932 if (individual != NULL)
933 g_object_ref (individual);
934 else
935 individual = create_individual_from_persona (FOLKS_PERSONA (persona));
937 g_object_unref (persona);
938 return individual;
941 const gchar * const *
942 empathy_individual_get_client_types (FolksIndividual *individual)
944 GeeSet *personas;
945 GeeIterator *iter;
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))
958 goto while_finish;
960 presence = FOLKS_PRESENCE_DETAILS (persona);
962 if (folks_presence_details_typecmp (
963 folks_presence_details_get_presence_type (presence),
964 presence_type) > 0)
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);
975 while_finish:
976 g_clear_object (&persona);
978 g_clear_object (&iter);
980 return types;
983 GVariant *
984 empathy_asv_to_vardict (const GHashTable *asv)
986 return empathy_boxed_to_variant (TP_HASH_TYPE_STRING_VARIANT_MAP, "a{sv}",
987 (gpointer) asv);
990 GVariant *
991 empathy_boxed_to_variant (GType gtype,
992 const gchar *variant_type,
993 gpointer boxed)
995 GValue v = G_VALUE_INIT;
996 GVariant *ret;
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);
1007 g_value_unset (&v);
1009 return g_variant_ref_sink (ret);