Improve default display name for Facebook accounts
[empathy-mirror.git] / libempathy / empathy-utils.c
blobfedb14b9893562049c12293c332841f415ab2a7c
1 /*
2 * Copyright (C) 2003-2007 Imendio AB
3 * Copyright (C) 2007-2008 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"
27 #include <string.h>
28 #include <time.h>
29 #include <sys/types.h>
31 #include <glib/gi18n-lib.h>
33 #include <libxml/uri.h>
35 #include <telepathy-glib/account-manager.h>
36 #include <telepathy-glib/connection.h>
37 #include <telepathy-glib/channel.h>
38 #include <telepathy-glib/dbus.h>
39 #include <telepathy-glib/util.h>
41 #include "empathy-utils.h"
42 #include "empathy-contact-manager.h"
43 #include "empathy-dispatcher.h"
44 #include "empathy-dispatch-operation.h"
45 #include "empathy-idle.h"
46 #include "empathy-tp-call.h"
48 #include <extensions/extensions.h>
50 #define DEBUG_FLAG EMPATHY_DEBUG_OTHER
51 #include "empathy-debug.h"
53 /* Translation between presence types and string */
54 static struct {
55 gchar *name;
56 TpConnectionPresenceType type;
57 } presence_types[] = {
58 { "available", TP_CONNECTION_PRESENCE_TYPE_AVAILABLE },
59 { "busy", TP_CONNECTION_PRESENCE_TYPE_BUSY },
60 { "away", TP_CONNECTION_PRESENCE_TYPE_AWAY },
61 { "ext_away", TP_CONNECTION_PRESENCE_TYPE_EXTENDED_AWAY },
62 { "hidden", TP_CONNECTION_PRESENCE_TYPE_HIDDEN },
63 { "offline", TP_CONNECTION_PRESENCE_TYPE_OFFLINE },
64 { "unset", TP_CONNECTION_PRESENCE_TYPE_UNSET },
65 { "unknown", TP_CONNECTION_PRESENCE_TYPE_UNKNOWN },
66 { "error", TP_CONNECTION_PRESENCE_TYPE_ERROR },
67 /* alternative names */
68 { "dnd", TP_CONNECTION_PRESENCE_TYPE_BUSY },
69 { "brb", TP_CONNECTION_PRESENCE_TYPE_AWAY },
70 { "xa", TP_CONNECTION_PRESENCE_TYPE_EXTENDED_AWAY },
71 { NULL, },
76 void
77 empathy_init (void)
79 static gboolean initialized = FALSE;
81 if (initialized)
82 return;
84 g_type_init ();
86 /* Setup gettext */
87 bindtextdomain (GETTEXT_PACKAGE, LOCALEDIR);
88 bind_textdomain_codeset (GETTEXT_PACKAGE, "UTF-8");
90 /* Setup debug output for empathy and telepathy-glib */
91 if (g_getenv ("EMPATHY_TIMING") != NULL) {
92 g_log_set_default_handler (tp_debug_timestamped_log_handler, NULL);
94 empathy_debug_set_flags (g_getenv ("EMPATHY_DEBUG"));
95 tp_debug_divert_messages (g_getenv ("EMPATHY_LOGFILE"));
97 emp_cli_init ();
99 initialized = TRUE;
102 gchar *
103 empathy_substring (const gchar *str,
104 gint start,
105 gint end)
107 return g_strndup (str + start, end - start);
110 gint
111 empathy_strcasecmp (const gchar *s1,
112 const gchar *s2)
114 return empathy_strncasecmp (s1, s2, -1);
117 gint
118 empathy_strncasecmp (const gchar *s1,
119 const gchar *s2,
120 gsize n)
122 gchar *u1, *u2;
123 gint ret_val;
125 u1 = g_utf8_casefold (s1, n);
126 u2 = g_utf8_casefold (s2, n);
128 ret_val = g_utf8_collate (u1, u2);
129 g_free (u1);
130 g_free (u2);
132 return ret_val;
135 gboolean
136 empathy_xml_validate (xmlDoc *doc,
137 const gchar *dtd_filename)
139 gchar *path;
140 xmlChar *escaped;
141 xmlValidCtxt cvp;
142 xmlDtd *dtd;
143 gboolean ret;
145 path = g_build_filename (g_getenv ("EMPATHY_SRCDIR"), "libempathy",
146 dtd_filename, NULL);
147 if (!g_file_test (path, G_FILE_TEST_EXISTS)) {
148 g_free (path);
149 path = g_build_filename (DATADIR, "empathy", dtd_filename, NULL);
151 DEBUG ("Loading dtd file %s", path);
153 /* The list of valid chars is taken from libxml. */
154 escaped = xmlURIEscapeStr ((const xmlChar *) path,
155 (const xmlChar *)":@&=+$,/?;");
156 g_free (path);
158 memset (&cvp, 0, sizeof (cvp));
159 dtd = xmlParseDTD (NULL, escaped);
160 ret = xmlValidateDtd (&cvp, doc, dtd);
162 xmlFree (escaped);
163 xmlFreeDtd (dtd);
165 return ret;
168 xmlNodePtr
169 empathy_xml_node_get_child (xmlNodePtr node,
170 const gchar *child_name)
172 xmlNodePtr l;
174 g_return_val_if_fail (node != NULL, NULL);
175 g_return_val_if_fail (child_name != NULL, NULL);
177 for (l = node->children; l; l = l->next) {
178 if (l->name && strcmp ((const gchar *) l->name, child_name) == 0) {
179 return l;
183 return NULL;
186 xmlChar *
187 empathy_xml_node_get_child_content (xmlNodePtr node,
188 const gchar *child_name)
190 xmlNodePtr l;
192 g_return_val_if_fail (node != NULL, NULL);
193 g_return_val_if_fail (child_name != NULL, NULL);
195 l = empathy_xml_node_get_child (node, child_name);
196 if (l) {
197 return xmlNodeGetContent (l);
200 return NULL;
203 xmlNodePtr
204 empathy_xml_node_find_child_prop_value (xmlNodePtr node,
205 const gchar *prop_name,
206 const gchar *prop_value)
208 xmlNodePtr l;
209 xmlNodePtr found = NULL;
211 g_return_val_if_fail (node != NULL, NULL);
212 g_return_val_if_fail (prop_name != NULL, NULL);
213 g_return_val_if_fail (prop_value != NULL, NULL);
215 for (l = node->children; l && !found; l = l->next) {
216 xmlChar *prop;
218 if (!xmlHasProp (l, (const xmlChar *) prop_name)) {
219 continue;
222 prop = xmlGetProp (l, (const xmlChar *) prop_name);
223 if (prop && strcmp ((const gchar *) prop, prop_value) == 0) {
224 found = l;
227 xmlFree (prop);
230 return found;
233 const gchar *
234 empathy_presence_get_default_message (TpConnectionPresenceType presence)
236 switch (presence) {
237 case TP_CONNECTION_PRESENCE_TYPE_AVAILABLE:
238 return _("Available");
239 case TP_CONNECTION_PRESENCE_TYPE_BUSY:
240 return _("Busy");
241 case TP_CONNECTION_PRESENCE_TYPE_AWAY:
242 case TP_CONNECTION_PRESENCE_TYPE_EXTENDED_AWAY:
243 return _("Away");
244 case TP_CONNECTION_PRESENCE_TYPE_HIDDEN:
245 return _("Invisible");
246 case TP_CONNECTION_PRESENCE_TYPE_OFFLINE:
247 return _("Offline");
248 case TP_CONNECTION_PRESENCE_TYPE_UNSET:
249 case TP_CONNECTION_PRESENCE_TYPE_UNKNOWN:
250 case TP_CONNECTION_PRESENCE_TYPE_ERROR:
251 return NULL;
254 return NULL;
257 const gchar *
258 empathy_presence_to_str (TpConnectionPresenceType presence)
260 int i;
262 for (i = 0 ; presence_types[i].name != NULL; i++)
263 if (presence == presence_types[i].type)
264 return presence_types[i].name;
266 return NULL;
269 TpConnectionPresenceType
270 empathy_presence_from_str (const gchar *str)
272 int i;
274 for (i = 0 ; presence_types[i].name != NULL; i++)
275 if (!tp_strdiff (str, presence_types[i].name))
276 return presence_types[i].type;
278 return TP_CONNECTION_PRESENCE_TYPE_UNSET;
281 const gchar *
282 empathy_status_reason_get_default_message (TpConnectionStatusReason reason)
284 switch (reason) {
285 case TP_CONNECTION_STATUS_REASON_NONE_SPECIFIED:
286 return _("No reason specified");
287 case TP_CONNECTION_STATUS_REASON_REQUESTED:
288 return _("Status is set to offline");
289 case TP_CONNECTION_STATUS_REASON_NETWORK_ERROR:
290 return _("Network error");
291 case TP_CONNECTION_STATUS_REASON_AUTHENTICATION_FAILED:
292 return _("Authentication failed");
293 case TP_CONNECTION_STATUS_REASON_ENCRYPTION_ERROR:
294 return _("Encryption error");
295 case TP_CONNECTION_STATUS_REASON_NAME_IN_USE:
296 return _("Name in use");
297 case TP_CONNECTION_STATUS_REASON_CERT_NOT_PROVIDED:
298 return _("Certificate not provided");
299 case TP_CONNECTION_STATUS_REASON_CERT_UNTRUSTED:
300 return _("Certificate untrusted");
301 case TP_CONNECTION_STATUS_REASON_CERT_EXPIRED:
302 return _("Certificate expired");
303 case TP_CONNECTION_STATUS_REASON_CERT_NOT_ACTIVATED:
304 return _("Certificate not activated");
305 case TP_CONNECTION_STATUS_REASON_CERT_HOSTNAME_MISMATCH:
306 return _("Certificate hostname mismatch");
307 case TP_CONNECTION_STATUS_REASON_CERT_FINGERPRINT_MISMATCH:
308 return _("Certificate fingerprint mismatch");
309 case TP_CONNECTION_STATUS_REASON_CERT_SELF_SIGNED:
310 return _("Certificate self-signed");
311 case TP_CONNECTION_STATUS_REASON_CERT_OTHER_ERROR:
312 return _("Certificate error");
313 default:
314 return _("Unknown reason");
318 gchar *
319 empathy_file_lookup (const gchar *filename, const gchar *subdir)
321 gchar *path;
323 if (!subdir) {
324 subdir = ".";
327 path = g_build_filename (g_getenv ("EMPATHY_SRCDIR"), subdir, filename, NULL);
328 if (!g_file_test (path, G_FILE_TEST_EXISTS)) {
329 g_free (path);
330 path = g_build_filename (DATADIR, "empathy", filename, NULL);
333 return path;
336 guint
337 empathy_proxy_hash (gconstpointer key)
339 TpProxy *proxy = TP_PROXY (key);
340 TpProxyClass *proxy_class = TP_PROXY_GET_CLASS (key);
342 g_return_val_if_fail (TP_IS_PROXY (proxy), 0);
343 g_return_val_if_fail (proxy_class->must_have_unique_name, 0);
345 return g_str_hash (proxy->object_path) ^ g_str_hash (proxy->bus_name);
348 gboolean
349 empathy_proxy_equal (gconstpointer a,
350 gconstpointer b)
352 TpProxy *proxy_a = TP_PROXY (a);
353 TpProxy *proxy_b = TP_PROXY (b);
354 TpProxyClass *proxy_a_class = TP_PROXY_GET_CLASS (a);
355 TpProxyClass *proxy_b_class = TP_PROXY_GET_CLASS (b);
357 g_return_val_if_fail (TP_IS_PROXY (proxy_a), FALSE);
358 g_return_val_if_fail (TP_IS_PROXY (proxy_b), FALSE);
359 g_return_val_if_fail (proxy_a_class->must_have_unique_name, 0);
360 g_return_val_if_fail (proxy_b_class->must_have_unique_name, 0);
362 return g_str_equal (proxy_a->object_path, proxy_b->object_path) &&
363 g_str_equal (proxy_a->bus_name, proxy_b->bus_name);
366 gboolean
367 empathy_check_available_state (void)
369 TpConnectionPresenceType presence;
370 EmpathyIdle *idle;
372 idle = empathy_idle_dup_singleton ();
373 presence = empathy_idle_get_state (idle);
374 g_object_unref (idle);
376 if (presence != TP_CONNECTION_PRESENCE_TYPE_AVAILABLE &&
377 presence != TP_CONNECTION_PRESENCE_TYPE_UNSET) {
378 return FALSE;
381 return TRUE;
384 gint
385 empathy_uint_compare (gconstpointer a,
386 gconstpointer b)
388 return *(guint *) a - *(guint *) b;
391 gchar *
392 empathy_protocol_icon_name (const gchar *protocol)
394 if (!tp_strdiff (protocol, "yahoojp"))
395 /* Yahoo Japan uses the same icon as Yahoo */
396 protocol = "yahoo";
397 else if (!tp_strdiff (protocol, "simple"))
398 /* SIMPLE uses the same icon as SIP */
399 protocol = "sip";
400 else if (!tp_strdiff (protocol, "sms"))
401 return g_strdup ("phone");
403 return g_strdup_printf ("im-%s", protocol);
406 GType
407 empathy_type_dbus_ao (void)
409 static GType t = 0;
411 if (G_UNLIKELY (t == 0))
412 t = dbus_g_type_get_collection ("GPtrArray", DBUS_TYPE_G_OBJECT_PATH);
414 return t;
417 const char *
418 empathy_protocol_name_to_display_name (const gchar *proto_name)
420 int i;
421 static struct {
422 const gchar *proto;
423 const gchar *display;
424 gboolean translated;
425 } names[] = {
426 { "jabber", "Jabber", FALSE },
427 { "gtalk", "Google Talk", FALSE },
428 { "msn", "MSN", FALSE, },
429 { "local-xmpp", N_("People Nearby"), TRUE },
430 { "irc", "IRC", FALSE },
431 { "icq", "ICQ", FALSE },
432 { "aim", "AIM", FALSE },
433 { "yahoo", "Yahoo!", FALSE },
434 { "yahoojp", N_("Yahoo! Japan"), TRUE },
435 { "facebook", N_("Facebook Chat"), TRUE },
436 { "groupwise", "GroupWise", FALSE },
437 { "sip", "SIP", FALSE },
438 { NULL, NULL }
441 for (i = 0; names[i].proto != NULL; i++)
443 if (!tp_strdiff (proto_name, names[i].proto))
445 if (names[i].translated)
446 return _(names[i].display);
447 else
448 return names[i].display;
452 return NULL;
455 typedef struct {
456 GObject *instance;
457 GObject *user_data;
458 gulong handler_id;
459 } WeakHandlerCtx;
461 static WeakHandlerCtx *
462 whc_new (GObject *instance,
463 GObject *user_data)
465 WeakHandlerCtx *ctx = g_slice_new0 (WeakHandlerCtx);
467 ctx->instance = instance;
468 ctx->user_data = user_data;
470 return ctx;
473 static void
474 whc_free (WeakHandlerCtx *ctx)
476 g_slice_free (WeakHandlerCtx, ctx);
479 static void user_data_destroyed_cb (gpointer, GObject *);
481 static void
482 instance_destroyed_cb (gpointer ctx_,
483 GObject *where_the_instance_was)
485 WeakHandlerCtx *ctx = ctx_;
487 DEBUG ("instance for %p destroyed; cleaning up", ctx);
489 /* No need to disconnect the signal here, the instance has gone away. */
490 g_object_weak_unref (ctx->user_data, user_data_destroyed_cb, ctx);
491 whc_free (ctx);
494 static void
495 user_data_destroyed_cb (gpointer ctx_,
496 GObject *where_the_user_data_was)
498 WeakHandlerCtx *ctx = ctx_;
500 DEBUG ("user_data for %p destroyed; disconnecting", ctx);
502 g_signal_handler_disconnect (ctx->instance, ctx->handler_id);
503 g_object_weak_unref (ctx->instance, instance_destroyed_cb, ctx);
504 whc_free (ctx);
507 /* This function is copied from telepathy-gabble: util.c */
509 * empathy_signal_connect_weak:
510 * @instance: the instance to connect to.
511 * @detailed_signal: a string of the form "signal-name::detail".
512 * @c_handler: the GCallback to connect.
513 * @user_data: an object to pass as data to c_handler calls.
515 * Connects a #GCallback function to a signal for a particular object, as if
516 * with g_signal_connect(). Additionally, arranges for the signal handler to be
517 * disconnected if @user_data is destroyed.
519 * This is intended to be a convenient way for objects to use themselves as
520 * user_data for callbacks without having to explicitly disconnect all the
521 * handlers in their finalizers.
523 void
524 empathy_signal_connect_weak (gpointer instance,
525 const gchar *detailed_signal,
526 GCallback c_handler,
527 GObject *user_data)
529 GObject *instance_obj = G_OBJECT (instance);
530 WeakHandlerCtx *ctx = whc_new (instance_obj, user_data);
532 DEBUG ("connecting to %p:%s with context %p", instance, detailed_signal, ctx);
534 ctx->handler_id = g_signal_connect (instance, detailed_signal, c_handler,
535 user_data);
537 g_object_weak_ref (instance_obj, instance_destroyed_cb, ctx);
538 g_object_weak_ref (user_data, user_data_destroyed_cb, ctx);
541 /* Note: this function depends on the account manager having its core feature
542 * prepared. */
543 TpAccount *
544 empathy_get_account_for_connection (TpConnection *connection)
546 TpAccountManager *manager;
547 TpAccount *account = NULL;
548 GList *accounts, *l;
550 manager = tp_account_manager_dup ();
552 accounts = tp_account_manager_get_valid_accounts (manager);
554 for (l = accounts; l != NULL; l = l->next)
556 TpAccount *a = l->data;
558 if (tp_account_get_connection (a) == connection)
560 account = a;
561 break;
565 g_list_free (accounts);
566 g_object_unref (manager);
568 return account;
571 gboolean
572 empathy_account_manager_get_accounts_connected (gboolean *connecting)
574 TpAccountManager *manager;
575 GList *accounts, *l;
576 gboolean out_connecting = FALSE;
577 gboolean out_connected = FALSE;
579 manager = tp_account_manager_dup ();
581 if (G_UNLIKELY (!tp_account_manager_is_prepared (manager,
582 TP_ACCOUNT_MANAGER_FEATURE_CORE)))
583 g_critical (G_STRLOC ": %s called before AccountManager ready", G_STRFUNC);
585 accounts = tp_account_manager_get_valid_accounts (manager);
587 for (l = accounts; l != NULL; l = l->next)
589 TpConnectionStatus s = tp_account_get_connection_status (
590 TP_ACCOUNT (l->data), NULL);
592 if (s == TP_CONNECTION_STATUS_CONNECTING)
593 out_connecting = TRUE;
594 else if (s == TP_CONNECTION_STATUS_CONNECTED)
595 out_connected = TRUE;
597 if (out_connecting && out_connected)
598 break;
601 g_list_free (accounts);
602 g_object_unref (manager);
604 if (connecting != NULL)
605 *connecting = out_connecting;
607 return out_connected;