]> git.0d.be Git - empathy.git/blob - libempathy/empathy-utils.c
add support for the errors introduced in spec 0.19.11
[empathy.git] / libempathy / empathy-utils.c
1 /*
2  * Copyright (C) 2003-2007 Imendio AB
3  * Copyright (C) 2007-2008 Collabora Ltd.
4  *
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.
9  *
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.
14  *
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
19  *
20  * Authors: Richard Hult <richard@imendio.com>
21  *          Martyn Russell <martyn@imendio.com>
22  *          Xavier Claessens <xclaesse@gmail.com>
23  */
24
25 #include "config.h"
26
27 #include <string.h>
28 #include <time.h>
29 #include <sys/types.h>
30
31 #include <glib/gi18n-lib.h>
32
33 #include <libxml/uri.h>
34
35 #include <folks/folks.h>
36 #include <folks/folks-telepathy.h>
37
38 #include <telepathy-glib/account-manager.h>
39 #include <telepathy-glib/connection.h>
40 #include <telepathy-glib/channel.h>
41 #include <telepathy-glib/dbus.h>
42 #include <telepathy-glib/util.h>
43
44 #include "empathy-utils.h"
45 #include "empathy-contact-manager.h"
46 #include "empathy-individual-manager.h"
47 #include "empathy-dispatcher.h"
48 #include "empathy-dispatch-operation.h"
49 #include "empathy-idle.h"
50 #include "empathy-tp-call.h"
51 #include "empathy-tp-contact-factory.h"
52
53 #include <extensions/extensions.h>
54
55 #define DEBUG_FLAG EMPATHY_DEBUG_OTHER
56 #include "empathy-debug.h"
57
58 /* Translation between presence types and string */
59 static struct {
60         const gchar *name;
61         TpConnectionPresenceType type;
62 } presence_types[] = {
63         { "available", TP_CONNECTION_PRESENCE_TYPE_AVAILABLE },
64         { "busy",      TP_CONNECTION_PRESENCE_TYPE_BUSY },
65         { "away",      TP_CONNECTION_PRESENCE_TYPE_AWAY },
66         { "ext_away",  TP_CONNECTION_PRESENCE_TYPE_EXTENDED_AWAY },
67         { "hidden",    TP_CONNECTION_PRESENCE_TYPE_HIDDEN },
68         { "offline",   TP_CONNECTION_PRESENCE_TYPE_OFFLINE },
69         { "unset",     TP_CONNECTION_PRESENCE_TYPE_UNSET },
70         { "unknown",   TP_CONNECTION_PRESENCE_TYPE_UNKNOWN },
71         { "error",     TP_CONNECTION_PRESENCE_TYPE_ERROR },
72         /* alternative names */
73         { "dnd",      TP_CONNECTION_PRESENCE_TYPE_BUSY },
74         { "brb",      TP_CONNECTION_PRESENCE_TYPE_AWAY },
75         { "xa",       TP_CONNECTION_PRESENCE_TYPE_EXTENDED_AWAY },
76         { NULL, },
77 };
78
79
80
81 void
82 empathy_init (void)
83 {
84         static gboolean initialized = FALSE;
85
86         if (initialized)
87                 return;
88
89         g_type_init ();
90
91         /* Setup gettext */
92         bindtextdomain (GETTEXT_PACKAGE, LOCALEDIR);
93         bind_textdomain_codeset (GETTEXT_PACKAGE, "UTF-8");
94
95         /* Setup debug output for empathy and telepathy-glib */
96         if (g_getenv ("EMPATHY_TIMING") != NULL) {
97                 g_log_set_default_handler (tp_debug_timestamped_log_handler, NULL);
98         }
99         empathy_debug_set_flags (g_getenv ("EMPATHY_DEBUG"));
100         tp_debug_divert_messages (g_getenv ("EMPATHY_LOGFILE"));
101
102         emp_cli_init ();
103
104         initialized = TRUE;
105 }
106
107 gchar *
108 empathy_substring (const gchar *str,
109                   gint         start,
110                   gint         end)
111 {
112         return g_strndup (str + start, end - start);
113 }
114
115 gint
116 empathy_strcasecmp (const gchar *s1,
117                    const gchar *s2)
118 {
119         return empathy_strncasecmp (s1, s2, -1);
120 }
121
122 gint
123 empathy_strncasecmp (const gchar *s1,
124                     const gchar *s2,
125                     gsize        n)
126 {
127         gchar *u1, *u2;
128         gint   ret_val;
129
130         u1 = g_utf8_casefold (s1, n);
131         u2 = g_utf8_casefold (s2, n);
132
133         ret_val = g_utf8_collate (u1, u2);
134         g_free (u1);
135         g_free (u2);
136
137         return ret_val;
138 }
139
140 gboolean
141 empathy_xml_validate (xmlDoc      *doc,
142                      const gchar *dtd_filename)
143 {
144         gchar        *path;
145         xmlChar      *escaped;
146         xmlValidCtxt  cvp;
147         xmlDtd       *dtd;
148         gboolean      ret;
149
150         path = g_build_filename (g_getenv ("EMPATHY_SRCDIR"), "libempathy",
151                                  dtd_filename, NULL);
152         if (!g_file_test (path, G_FILE_TEST_EXISTS)) {
153                 g_free (path);
154                 path = g_build_filename (DATADIR, "empathy", dtd_filename, NULL);
155         }
156         DEBUG ("Loading dtd file %s", path);
157
158         /* The list of valid chars is taken from libxml. */
159         escaped = xmlURIEscapeStr ((const xmlChar *) path,
160                 (const xmlChar *)":@&=+$,/?;");
161         g_free (path);
162
163         memset (&cvp, 0, sizeof (cvp));
164         dtd = xmlParseDTD (NULL, escaped);
165         ret = xmlValidateDtd (&cvp, doc, dtd);
166
167         xmlFree (escaped);
168         xmlFreeDtd (dtd);
169
170         return ret;
171 }
172
173 xmlNodePtr
174 empathy_xml_node_get_child (xmlNodePtr   node,
175                            const gchar *child_name)
176 {
177         xmlNodePtr l;
178
179         g_return_val_if_fail (node != NULL, NULL);
180         g_return_val_if_fail (child_name != NULL, NULL);
181
182         for (l = node->children; l; l = l->next) {
183                 if (l->name && strcmp ((const gchar *) l->name, child_name) == 0) {
184                         return l;
185                 }
186         }
187
188         return NULL;
189 }
190
191 xmlChar *
192 empathy_xml_node_get_child_content (xmlNodePtr   node,
193                                    const gchar *child_name)
194 {
195         xmlNodePtr l;
196
197         g_return_val_if_fail (node != NULL, NULL);
198         g_return_val_if_fail (child_name != NULL, NULL);
199
200         l = empathy_xml_node_get_child (node, child_name);
201         if (l) {
202                 return xmlNodeGetContent (l);
203         }
204
205         return NULL;
206 }
207
208 xmlNodePtr
209 empathy_xml_node_find_child_prop_value (xmlNodePtr   node,
210                                        const gchar *prop_name,
211                                        const gchar *prop_value)
212 {
213         xmlNodePtr l;
214         xmlNodePtr found = NULL;
215
216         g_return_val_if_fail (node != NULL, NULL);
217         g_return_val_if_fail (prop_name != NULL, NULL);
218         g_return_val_if_fail (prop_value != NULL, NULL);
219
220         for (l = node->children; l && !found; l = l->next) {
221                 xmlChar *prop;
222
223                 if (!xmlHasProp (l, (const xmlChar *) prop_name)) {
224                         continue;
225                 }
226
227                 prop = xmlGetProp (l, (const xmlChar *) prop_name);
228                 if (prop && strcmp ((const gchar *) prop, prop_value) == 0) {
229                         found = l;
230                 }
231
232                 xmlFree (prop);
233         }
234
235         return found;
236 }
237
238 const gchar *
239 empathy_presence_get_default_message (TpConnectionPresenceType presence)
240 {
241         switch (presence) {
242         case TP_CONNECTION_PRESENCE_TYPE_AVAILABLE:
243                 return _("Available");
244         case TP_CONNECTION_PRESENCE_TYPE_BUSY:
245                 return _("Busy");
246         case TP_CONNECTION_PRESENCE_TYPE_AWAY:
247         case TP_CONNECTION_PRESENCE_TYPE_EXTENDED_AWAY:
248                 return _("Away");
249         case TP_CONNECTION_PRESENCE_TYPE_HIDDEN:
250                 return _("Invisible");
251         case TP_CONNECTION_PRESENCE_TYPE_OFFLINE:
252                 return _("Offline");
253         case TP_CONNECTION_PRESENCE_TYPE_UNKNOWN:
254                 return _("Unknown");
255         case TP_CONNECTION_PRESENCE_TYPE_UNSET:
256         case TP_CONNECTION_PRESENCE_TYPE_ERROR:
257         default:
258                 return NULL;
259         }
260
261         return NULL;
262 }
263
264 const gchar *
265 empathy_presence_to_str (TpConnectionPresenceType presence)
266 {
267         int i;
268
269         for (i = 0 ; presence_types[i].name != NULL; i++)
270                 if (presence == presence_types[i].type)
271                         return presence_types[i].name;
272
273         return NULL;
274 }
275
276 TpConnectionPresenceType
277 empathy_presence_from_str (const gchar *str)
278 {
279         int i;
280
281         for (i = 0 ; presence_types[i].name != NULL; i++)
282                 if (!tp_strdiff (str, presence_types[i].name))
283                         return presence_types[i].type;
284
285         return TP_CONNECTION_PRESENCE_TYPE_UNSET;
286 }
287
288 static const gchar *
289 empathy_status_reason_get_default_message (TpConnectionStatusReason reason)
290 {
291         switch (reason) {
292         case TP_CONNECTION_STATUS_REASON_NONE_SPECIFIED:
293                 return _("No reason specified");
294         case TP_CONNECTION_STATUS_REASON_REQUESTED:
295                 return _("Status is set to offline");
296         case TP_CONNECTION_STATUS_REASON_NETWORK_ERROR:
297                 return _("Network error");
298         case TP_CONNECTION_STATUS_REASON_AUTHENTICATION_FAILED:
299                 return _("Authentication failed");
300         case TP_CONNECTION_STATUS_REASON_ENCRYPTION_ERROR:
301                 return _("Encryption error");
302         case TP_CONNECTION_STATUS_REASON_NAME_IN_USE:
303                 return _("Name in use");
304         case TP_CONNECTION_STATUS_REASON_CERT_NOT_PROVIDED:
305                 return _("Certificate not provided");
306         case TP_CONNECTION_STATUS_REASON_CERT_UNTRUSTED:
307                 return _("Certificate untrusted");
308         case TP_CONNECTION_STATUS_REASON_CERT_EXPIRED:
309                 return _("Certificate expired");
310         case TP_CONNECTION_STATUS_REASON_CERT_NOT_ACTIVATED:
311                 return _("Certificate not activated");
312         case TP_CONNECTION_STATUS_REASON_CERT_HOSTNAME_MISMATCH:
313                 return _("Certificate hostname mismatch");
314         case TP_CONNECTION_STATUS_REASON_CERT_FINGERPRINT_MISMATCH:
315                 return _("Certificate fingerprint mismatch");
316         case TP_CONNECTION_STATUS_REASON_CERT_SELF_SIGNED:
317                 return _("Certificate self-signed");
318         case TP_CONNECTION_STATUS_REASON_CERT_OTHER_ERROR:
319                 return _("Certificate error");
320         default:
321                 return _("Unknown reason");
322         }
323 }
324
325 static GHashTable *
326 create_errors_to_message_hash (void)
327 {
328         GHashTable *errors;
329
330         errors = g_hash_table_new (g_str_hash, g_str_equal);
331         g_hash_table_insert (errors, TP_ERROR_STR_NETWORK_ERROR, _("Network error"));
332         g_hash_table_insert (errors, TP_ERROR_STR_AUTHENTICATION_FAILED,
333                 _("Authentication failed"));
334         g_hash_table_insert (errors, TP_ERROR_STR_ENCRYPTION_ERROR,
335                 _("Encryption error"));
336         g_hash_table_insert (errors, TP_ERROR_STR_CERT_NOT_PROVIDED,
337                 _("Certificate not provided"));
338         g_hash_table_insert (errors, TP_ERROR_STR_CERT_UNTRUSTED,
339                 _("Certificate untrusted"));
340         g_hash_table_insert (errors, TP_ERROR_STR_CERT_EXPIRED,
341                 _("Certificate expired"));
342         g_hash_table_insert (errors, TP_ERROR_STR_CERT_NOT_ACTIVATED,
343                 _("Certificate not activated"));
344         g_hash_table_insert (errors, TP_ERROR_STR_CERT_HOSTNAME_MISMATCH,
345                 _("Certificate hostname mismatch"));
346         g_hash_table_insert (errors, TP_ERROR_STR_CERT_FINGERPRINT_MISMATCH,
347                 _("Certificate fingerprint mismatch"));
348         g_hash_table_insert (errors, TP_ERROR_STR_CERT_SELF_SIGNED,
349                 _("Certificate self-signed"));
350         g_hash_table_insert (errors, TP_ERROR_STR_CANCELLED,
351                 _("Status is set to offline"));
352         g_hash_table_insert (errors, TP_ERROR_STR_ENCRYPTION_NOT_AVAILABLE,
353                 _("Encryption is not available"));
354         g_hash_table_insert (errors, TP_ERROR_STR_CERT_INVALID,
355                 _("Certificate is invalid"));
356         g_hash_table_insert (errors, TP_ERROR_STR_CONNECTION_REFUSED,
357                 _("Connection has been refused"));
358         g_hash_table_insert (errors, TP_ERROR_STR_CONNECTION_FAILED,
359                 _("Connection can't be established"));
360         g_hash_table_insert (errors, TP_ERROR_STR_CONNECTION_LOST,
361                 _("Connection has been lost"));
362         g_hash_table_insert (errors, TP_ERROR_STR_ALREADY_CONNECTED,
363                 _("This resource is already connected to the server"));
364         g_hash_table_insert (errors, TP_ERROR_STR_CONNECTION_REPLACED,
365                 _("Connection has been replaced by a new connection using the "
366                 "same resource"));
367         g_hash_table_insert (errors, TP_ERROR_STR_REGISTRATION_EXISTS,
368                 _("The account already exists on the server"));
369         g_hash_table_insert (errors, TP_ERROR_STR_SERVICE_BUSY,
370                 _("Server is currently too busy to handle the connection"));
371         g_hash_table_insert (errors, TP_ERROR_STR_CERT_REVOKED,
372                 _("Certificate has been revoked"));
373         g_hash_table_insert (errors, TP_ERROR_STR_CERT_INSECURE,
374                 _("Certificate uses an insecure cipher algorithm or is "
375                 "cryptographically weak"));
376         g_hash_table_insert (errors, TP_ERROR_STR_CERT_LIMIT_EXCEEDED,
377                 _("The length of the server certificate, or the depth of the "
378                 "server certificate chain exceed the limits imposed by the "
379                 "crypto library"));
380
381         return errors;
382 }
383
384 static const gchar *
385 empathy_dbus_error_name_get_default_message  (const gchar *error)
386 {
387         static GHashTable *errors_to_message = NULL;
388
389         if (error == NULL)
390                 return NULL;
391
392         if (G_UNLIKELY (errors_to_message == NULL)) {
393                 errors_to_message = create_errors_to_message_hash ();
394         }
395
396         return g_hash_table_lookup (errors_to_message, error);
397 }
398
399 const gchar *
400 empathy_account_get_error_message (TpAccount *account)
401 {
402         const gchar *dbus_error;
403         const gchar *message;
404         TpConnectionStatusReason reason;
405
406         dbus_error = tp_account_get_detailed_error (account, NULL);
407         message = empathy_dbus_error_name_get_default_message (dbus_error);
408         if (message != NULL)
409                 return message;
410
411         DEBUG ("Don't understand error '%s'; fallback to the status reason (%u)",
412                 dbus_error, reason);
413
414         tp_account_get_connection_status (account, &reason);
415
416         return empathy_status_reason_get_default_message (reason);
417 }
418
419 gchar *
420 empathy_file_lookup (const gchar *filename, const gchar *subdir)
421 {
422         gchar *path;
423
424         if (!subdir) {
425                 subdir = ".";
426         }
427
428         path = g_build_filename (g_getenv ("EMPATHY_SRCDIR"), subdir, filename, NULL);
429         if (!g_file_test (path, G_FILE_TEST_EXISTS)) {
430                 g_free (path);
431                 path = g_build_filename (DATADIR, "empathy", filename, NULL);
432         }
433
434         return path;
435 }
436
437 guint
438 empathy_proxy_hash (gconstpointer key)
439 {
440         TpProxy      *proxy = TP_PROXY (key);
441         TpProxyClass *proxy_class = TP_PROXY_GET_CLASS (key);
442
443         g_return_val_if_fail (TP_IS_PROXY (proxy), 0);
444         g_return_val_if_fail (proxy_class->must_have_unique_name, 0);
445
446         return g_str_hash (proxy->object_path) ^ g_str_hash (proxy->bus_name);
447 }
448
449 gboolean
450 empathy_proxy_equal (gconstpointer a,
451                      gconstpointer b)
452 {
453         TpProxy *proxy_a = TP_PROXY (a);
454         TpProxy *proxy_b = TP_PROXY (b);
455         TpProxyClass *proxy_a_class = TP_PROXY_GET_CLASS (a);
456         TpProxyClass *proxy_b_class = TP_PROXY_GET_CLASS (b);
457
458         g_return_val_if_fail (TP_IS_PROXY (proxy_a), FALSE);
459         g_return_val_if_fail (TP_IS_PROXY (proxy_b), FALSE);
460         g_return_val_if_fail (proxy_a_class->must_have_unique_name, 0);
461         g_return_val_if_fail (proxy_b_class->must_have_unique_name, 0);
462
463         return g_str_equal (proxy_a->object_path, proxy_b->object_path) &&
464                g_str_equal (proxy_a->bus_name, proxy_b->bus_name);
465 }
466
467 gboolean
468 empathy_check_available_state (void)
469 {
470         TpConnectionPresenceType presence;
471         EmpathyIdle *idle;
472
473         idle = empathy_idle_dup_singleton ();
474         presence = empathy_idle_get_state (idle);
475         g_object_unref (idle);
476
477         if (presence != TP_CONNECTION_PRESENCE_TYPE_AVAILABLE &&
478                 presence != TP_CONNECTION_PRESENCE_TYPE_UNSET) {
479                 return FALSE;
480         }
481
482         return TRUE;
483 }
484
485 gint
486 empathy_uint_compare (gconstpointer a,
487                       gconstpointer b)
488 {
489         return *(guint *) a - *(guint *) b;
490 }
491
492 gchar *
493 empathy_protocol_icon_name (const gchar *protocol)
494 {
495   if (!tp_strdiff (protocol, "yahoojp"))
496     /* Yahoo Japan uses the same icon as Yahoo */
497     protocol = "yahoo";
498   else if (!tp_strdiff (protocol, "simple"))
499     /* SIMPLE uses the same icon as SIP */
500     protocol = "sip";
501   else if (!tp_strdiff (protocol, "sms"))
502     return g_strdup ("phone");
503
504   return g_strdup_printf ("im-%s", protocol);
505 }
506
507 GType
508 empathy_type_dbus_ao (void)
509 {
510   static GType t = 0;
511
512   if (G_UNLIKELY (t == 0))
513      t = dbus_g_type_get_collection ("GPtrArray", DBUS_TYPE_G_OBJECT_PATH);
514
515   return t;
516 }
517
518 const char *
519 empathy_protocol_name_to_display_name (const gchar *proto_name)
520 {
521   int i;
522   static struct {
523     const gchar *proto;
524     const gchar *display;
525     gboolean translated;
526   } names[] = {
527     { "jabber", "Jabber", FALSE },
528     { "gtalk", "Google Talk", FALSE },
529     { "msn", "MSN", FALSE, },
530     { "local-xmpp", N_("People Nearby"), TRUE },
531     { "irc", "IRC", FALSE },
532     { "icq", "ICQ", FALSE },
533     { "aim", "AIM", FALSE },
534     { "yahoo", "Yahoo!", FALSE },
535     { "yahoojp", N_("Yahoo! Japan"), TRUE },
536     { "facebook", N_("Facebook Chat"), TRUE },
537     { "groupwise", "GroupWise", FALSE },
538     { "sip", "SIP", FALSE },
539     { NULL, NULL }
540   };
541
542   for (i = 0; names[i].proto != NULL; i++)
543     {
544       if (!tp_strdiff (proto_name, names[i].proto))
545         {
546           if (names[i].translated)
547             return _(names[i].display);
548           else
549             return names[i].display;
550         }
551     }
552
553   return NULL;
554 }
555
556 /* Note: this function depends on the account manager having its core feature
557  * prepared. */
558 TpAccount *
559 empathy_get_account_for_connection (TpConnection *connection)
560 {
561   TpAccountManager *manager;
562   TpAccount *account = NULL;
563   GList *accounts, *l;
564
565   manager = tp_account_manager_dup ();
566
567   accounts = tp_account_manager_get_valid_accounts (manager);
568
569   for (l = accounts; l != NULL; l = l->next)
570     {
571       TpAccount *a = l->data;
572
573       if (tp_account_get_connection (a) == connection)
574         {
575           account = a;
576           break;
577         }
578     }
579
580   g_list_free (accounts);
581   g_object_unref (manager);
582
583   return account;
584 }
585
586 gboolean
587 empathy_account_manager_get_accounts_connected (gboolean *connecting)
588 {
589   TpAccountManager *manager;
590   GList *accounts, *l;
591   gboolean out_connecting = FALSE;
592   gboolean out_connected = FALSE;
593
594   manager = tp_account_manager_dup ();
595
596   if (G_UNLIKELY (!tp_account_manager_is_prepared (manager,
597           TP_ACCOUNT_MANAGER_FEATURE_CORE)))
598     g_critical (G_STRLOC ": %s called before AccountManager ready", G_STRFUNC);
599
600   accounts = tp_account_manager_get_valid_accounts (manager);
601
602   for (l = accounts; l != NULL; l = l->next)
603     {
604       TpConnectionStatus s = tp_account_get_connection_status (
605           TP_ACCOUNT (l->data), NULL);
606
607       if (s == TP_CONNECTION_STATUS_CONNECTING)
608         out_connecting = TRUE;
609       else if (s == TP_CONNECTION_STATUS_CONNECTED)
610         out_connected = TRUE;
611
612       if (out_connecting && out_connected)
613         break;
614     }
615
616   g_list_free (accounts);
617   g_object_unref (manager);
618
619   if (connecting != NULL)
620     *connecting = out_connecting;
621
622   return out_connected;
623 }
624
625 /* Change the RequestedPresence of a newly created account to ensure that it
626  * is actually connected. */
627 void
628 empathy_connect_new_account (TpAccount *account,
629     TpAccountManager *account_manager)
630 {
631   TpConnectionPresenceType presence;
632   gchar *status, *message;
633
634   /* only force presence if presence was offline, unknown or unset */
635   presence = tp_account_get_requested_presence (account, NULL, NULL);
636   switch (presence)
637     {
638       case TP_CONNECTION_PRESENCE_TYPE_OFFLINE:
639       case TP_CONNECTION_PRESENCE_TYPE_UNKNOWN:
640       case TP_CONNECTION_PRESENCE_TYPE_UNSET:
641         presence = tp_account_manager_get_most_available_presence (
642             account_manager, &status, &message);
643
644         if (presence == TP_CONNECTION_PRESENCE_TYPE_OFFLINE)
645           /* Global presence is offline; we force it so user doesn't have to
646            * manually change the presence to connect his new account. */
647           presence = TP_CONNECTION_PRESENCE_TYPE_AVAILABLE;
648
649         tp_account_request_presence_async (account, presence,
650             status, NULL, NULL, NULL);
651
652         g_free (status);
653         g_free (message);
654         break;
655
656        case TP_CONNECTION_PRESENCE_TYPE_AVAILABLE:
657        case TP_CONNECTION_PRESENCE_TYPE_AWAY:
658        case TP_CONNECTION_PRESENCE_TYPE_EXTENDED_AWAY:
659        case TP_CONNECTION_PRESENCE_TYPE_HIDDEN:
660        case TP_CONNECTION_PRESENCE_TYPE_BUSY:
661        case TP_CONNECTION_PRESENCE_TYPE_ERROR:
662        default:
663         /* do nothing if the presence is not offline */
664         break;
665     }
666 }
667
668 /* Translate Folks' general presence type to the Tp presence type */
669 TpConnectionPresenceType
670 empathy_folks_presence_type_to_tp (FolksPresenceType type)
671 {
672   return (TpConnectionPresenceType) type;
673 }
674
675 /* Returns TRUE if the given Individual contains a TpContact */
676 gboolean
677 empathy_folks_individual_contains_contact (FolksIndividual *individual)
678 {
679   GList *personas, *l;
680
681   g_return_val_if_fail (FOLKS_IS_INDIVIDUAL (individual), FALSE);
682
683   personas = folks_individual_get_personas (individual);
684   for (l = personas; l != NULL; l = l->next)
685     {
686       if (TPF_IS_PERSONA (l->data))
687         return (tpf_persona_get_contact (TPF_PERSONA (l->data)) != NULL);
688     }
689
690   return FALSE;
691 }
692
693 /* TODO: this needs to be eliminated (and replaced in some cases with user
694  * prompts) when we break the assumption that FolksIndividuals are 1:1 with
695  * TpContacts */
696
697 /* Retrieve the EmpathyContact corresponding to the first TpContact contained
698  * within the given Individual. Note that this is a temporary convenience. See
699  * the TODO above. */
700 EmpathyContact *
701 empathy_contact_dup_from_folks_individual (FolksIndividual *individual)
702 {
703   GList *personas, *l;
704   EmpathyContact *contact = NULL;
705
706   g_return_val_if_fail (FOLKS_IS_INDIVIDUAL (individual), NULL);
707
708   personas = folks_individual_get_personas (individual);
709   for (l = personas; (l != NULL) && (contact == NULL); l = l->next)
710     {
711       TpfPersona *persona = l->data;
712
713       if (TPF_IS_PERSONA (persona))
714         {
715           TpContact *tp_contact;
716
717           tp_contact = tpf_persona_get_contact (persona);
718           contact = empathy_contact_dup_from_tp_contact (tp_contact);
719           empathy_contact_set_persona (contact, FOLKS_PERSONA (persona));
720         }
721     }
722
723   return contact;
724 }
725
726 TpChannelGroupChangeReason
727 tp_chanel_group_change_reason_from_folks_groups_change_reason (
728     FolksGroupsChangeReason reason)
729 {
730   return (TpChannelGroupChangeReason) reason;
731 }