Updated Czech translation
[empathy-mirror.git] / goa-mc-plugin / mcp-account-manager-goa.c
blobdc9e036ee6db02aaa47d6b26a36f681e60f8807f
1 /*
2 * mcp-account-manager-goa.c
4 * McpAccountManagerGoa - a Mission Control plugin to expose GNOME Online
5 * Accounts with chat capabilities (e.g. Facebook) to Mission Control
7 * Copyright (C) 2010-2011 Collabora Ltd.
9 * This library is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU Lesser General Public
11 * License as published by the Free Software Foundation; either
12 * version 2.1 of the License, or (at your option) any later version.
14 * This library is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * Lesser General Public License for more details.
19 * You should have received a copy of the GNU Lesser General Public
20 * License along with this library; if not, see <http://www.gnu.org/licenses/>.
22 * Authors:
23 * Danielle Madeley <danielle.madeley@collabora.co.uk>
26 #include "config.h"
27 #include "mcp-account-manager-goa.h"
29 #define GOA_API_IS_SUBJECT_TO_CHANGE /* awesome! */
30 #include <goa/goa.h>
32 #define DEBUG g_debug
33 #define GET_PRIVATE(self) (((McpAccountManagerGoa *) self)->priv)
34 #define DECLARE_GASYNC_CALLBACK(name) \
35 static void name (GObject *, GAsyncResult *, gpointer);
37 #define PLUGIN_NAME "goa"
38 #define PLUGIN_PRIORITY (MCP_ACCOUNT_STORAGE_PLUGIN_PRIO_KEYRING + 10)
39 #define PLUGIN_DESCRIPTION "Provide Telepathy Accounts from GOA"
40 #define PLUGIN_PROVIDER EMPATHY_GOA_PROVIDER
42 #define INITIAL_COMMENT "Parameters of GOA Telepathy accounts"
44 static void account_storage_iface_init (McpAccountStorageIface *iface);
46 G_DEFINE_TYPE_WITH_CODE (McpAccountManagerGoa,
47 mcp_account_manager_goa,
48 G_TYPE_OBJECT,
49 G_IMPLEMENT_INTERFACE (MCP_TYPE_ACCOUNT_STORAGE,
50 account_storage_iface_init))
52 struct _McpAccountManagerGoaPrivate
54 gboolean ready;
56 GoaClient *client;
57 GHashTable *accounts; /* alloc'ed string -> ref'ed GoaObject */
59 GKeyFile *store;
60 gchar *filename;
64 static void
65 mcp_account_manager_goa_dispose (GObject *self)
67 McpAccountManagerGoaPrivate *priv = GET_PRIVATE (self);
69 tp_clear_object (&priv->client);
71 G_OBJECT_CLASS (mcp_account_manager_goa_parent_class)->dispose (self);
75 static void
76 mcp_account_manager_goa_finalize (GObject *self)
78 McpAccountManagerGoaPrivate *priv = GET_PRIVATE (self);
80 g_hash_table_unref (priv->accounts);
81 g_key_file_free (priv->store);
82 g_free (priv->filename);
84 G_OBJECT_CLASS (mcp_account_manager_goa_parent_class)->finalize (self);
88 static void
89 mcp_account_manager_goa_class_init (McpAccountManagerGoaClass *klass)
91 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
93 gobject_class->dispose = mcp_account_manager_goa_dispose;
94 gobject_class->finalize = mcp_account_manager_goa_finalize;
96 g_type_class_add_private (gobject_class,
97 sizeof (McpAccountManagerGoaPrivate));
100 static GHashTable *
101 get_tp_parameters (GoaAccount *account)
103 GHashTable *params = g_hash_table_new_full (g_str_hash, g_str_equal,
104 NULL, g_free);
105 const char *type = goa_account_get_provider_type (account);
107 #define PARAM(key, value) g_hash_table_insert (params, key, g_strdup (value));
109 if (!tp_strdiff (type, "google"))
111 PARAM ("manager", "gabble");
112 PARAM ("protocol", "jabber");
113 PARAM ("Icon", "im-google-talk");
114 PARAM ("Service", "google-talk");
116 PARAM ("param-account", goa_account_get_identity (account));
117 PARAM ("param-server", "talk.google.com");
118 PARAM ("param-fallback-servers",
119 "talkx.l.google.com;"
120 "talkx.l.google.com:443,oldssl;"
121 "talkx.l.google.com:80");
122 PARAM ("param-extra-certificate-identities", "talk.google.com");
123 PARAM ("param-require-encryption", "true");
125 else if (!tp_strdiff (type, "facebook"))
127 PARAM ("manager", "gabble");
128 PARAM ("protocol", "jabber");
129 PARAM ("Icon", "im-facebook");
130 PARAM ("Service", "facebook");
132 PARAM ("param-account", "chat.facebook.com");
133 PARAM ("param-server", "chat.facebook.com");
134 PARAM ("param-require-encryption", "true");
135 PARAM ("param-fallback-servers",
136 "chat.facebook.com:443");
138 else if (!tp_strdiff (type, "windows_live"))
140 PARAM ("manager", "gabble");
141 PARAM ("protocol", "jabber");
142 PARAM ("Icon", "im-msn");
143 PARAM ("Service", "windows-live");
145 PARAM ("param-account", "messenger.live.com");
146 PARAM ("param-require-encryption", "true");
147 PARAM ("param-fallback-servers", "xmpp.messenger.live.com");
148 PARAM ("param-extra-certificate-identities",
149 "*.gateway.messenger.live.com");
151 else
153 DEBUG ("Unknown account type %s", type);
154 g_hash_table_unref (params);
155 return NULL;
158 /* generic properties */
159 PARAM ("DisplayName", goa_account_get_presentation_identity (account));
161 #undef PARAM
163 return params;
167 static char *
168 get_tp_account_name (GoaAccount *account)
170 GHashTable *params = get_tp_parameters (account);
171 const char *type = goa_account_get_provider_type (account);
172 const char *id = goa_account_get_id (account);
173 char *name;
175 if (params == NULL)
176 return NULL;
178 name = g_strdup_printf ("%s/%s/goa_%s_%s",
179 (char *) g_hash_table_lookup (params, "manager"),
180 (char *) g_hash_table_lookup (params, "protocol"),
181 type, id);
183 g_hash_table_unref (params);
185 return name;
188 static void
189 object_chat_changed_cb (GoaObject *object,
190 GParamSpec *spec,
191 McpAccountManagerGoa *self)
193 GoaAccount *account = goa_object_peek_account (object);
194 char *name = get_tp_account_name (account);
195 gboolean enabled;
197 enabled = (goa_object_peek_chat (object) != NULL);
199 DEBUG ("%s %s", name, enabled ? "enabled" : "disabled");
201 if (self->priv->ready)
202 g_signal_emit_by_name (self, "toggled", name, enabled);
205 static void
206 _new_account (McpAccountManagerGoa *self,
207 GoaObject *object)
209 GoaAccount *account = goa_object_peek_account (object);
210 char *account_name = get_tp_account_name (account);
212 if (account_name == NULL)
213 return;
215 /* @account_name now is owned by the hash table */
216 g_hash_table_insert (self->priv->accounts, account_name,
217 g_object_ref (object));
219 if (self->priv->ready)
220 g_signal_emit_by_name (self, "created", account_name);
222 tp_g_signal_connect_object (object, "notify::chat",
223 G_CALLBACK (object_chat_changed_cb), self, 0);
227 DECLARE_GASYNC_CALLBACK (_goa_client_new_cb);
229 static void
230 load_store (McpAccountManagerGoa *self)
232 GError *error = NULL;
234 if (!g_key_file_load_from_file (self->priv->store, self->priv->filename,
235 G_KEY_FILE_KEEP_COMMENTS, &error))
237 gchar *dir;
239 DEBUG ("Failed to load keyfile, creating a new one: %s", error->message);
241 dir = g_path_get_dirname (self->priv->filename);
243 g_mkdir_with_parents (dir, 0700);
244 g_free (dir);
246 g_key_file_set_comment (self->priv->store, NULL, NULL, INITIAL_COMMENT,
247 NULL);
249 g_error_free (error);
253 static void
254 mcp_account_manager_goa_init (McpAccountManagerGoa *self)
256 DEBUG ("GOA MC plugin initialised");
258 self->priv = G_TYPE_INSTANCE_GET_PRIVATE (self,
259 MCP_TYPE_ACCOUNT_MANAGER_GOA, McpAccountManagerGoaPrivate);
261 self->priv->accounts = g_hash_table_new_full (g_str_hash, g_str_equal,
262 g_free, g_object_unref);
264 goa_client_new (NULL, _goa_client_new_cb, self);
266 /* key file store */
267 self->priv->store = g_key_file_new ();
268 self->priv->filename = g_build_filename (g_get_user_data_dir (), "telepathy",
269 "mission-control", "accounts-goa.cfg", NULL);
271 load_store (self);
275 static void
276 _account_added_cb (GoaClient *client,
277 GoaObject *object,
278 McpAccountManagerGoa *self)
280 _new_account (self, object);
284 static void
285 _account_removed_cb (GoaClient *client,
286 GoaObject *object,
287 McpAccountManagerGoa *self)
289 GoaAccount *account = goa_object_peek_account (object);
290 char *name = get_tp_account_name (account);
292 if (self->priv->ready)
293 g_signal_emit_by_name (self, "deleted", name);
295 g_hash_table_remove (self->priv->accounts, name);
297 g_free (name);
300 static void
301 _goa_client_new_cb (GObject *obj,
302 GAsyncResult *result,
303 gpointer user_data)
305 McpAccountManagerGoa *self = user_data;
306 GList *accounts, *ptr;
307 GError *error = NULL;
309 self->priv->client = goa_client_new_finish (result, &error);
311 if (error != NULL)
313 DEBUG ("Failed to connect to GOA");
314 return;
317 accounts = goa_client_get_accounts (self->priv->client);
319 for (ptr = accounts; ptr != NULL; ptr = ptr->next)
321 _new_account (self, ptr->data);
324 g_list_free_full (accounts, g_object_unref);
326 g_signal_connect (self->priv->client, "account-added",
327 G_CALLBACK (_account_added_cb), self);
328 g_signal_connect (self->priv->client, "account-removed",
329 G_CALLBACK (_account_removed_cb), self);
333 static GList *
334 mcp_account_manager_goa_list (const McpAccountStorage *self,
335 const McpAccountManager *am)
337 McpAccountManagerGoaPrivate *priv = GET_PRIVATE (self);
338 GList *accounts = NULL;
339 GHashTableIter iter;
340 gpointer key;
342 DEBUG (G_STRFUNC);
344 g_hash_table_iter_init (&iter, priv->accounts);
345 while (g_hash_table_iter_next (&iter, &key, NULL))
346 accounts = g_list_prepend (accounts, g_strdup (key));
348 return accounts;
352 static void
353 get_enabled (const McpAccountStorage *self,
354 const McpAccountManager *am,
355 const gchar *acc,
356 GoaAccount *account)
358 mcp_account_manager_set_value (am, acc, "Enabled",
359 goa_account_get_chat_disabled (account) == FALSE ? "true" : "false");
363 static gboolean
364 mcp_account_manager_goa_get (const McpAccountStorage *self,
365 const McpAccountManager *am,
366 const gchar *acc,
367 const gchar *key)
369 McpAccountManagerGoaPrivate *priv = GET_PRIVATE (self);
370 GoaObject *object;
371 GoaAccount *account;
373 DEBUG ("%s: %s, %s", G_STRFUNC, acc, key);
375 object = g_hash_table_lookup (priv->accounts, acc);
377 if (object == NULL)
378 return FALSE;
380 account = goa_object_peek_account (object);
382 if (account == NULL)
383 return FALSE;
385 if (key == NULL)
387 /* load all keys */
388 GHashTable *params = get_tp_parameters (account);
389 GHashTableIter iter;
390 gpointer k, value;
391 GStrv keys;
392 guint i;
393 gsize nkeys = 0;
395 /* Properties from GOA */
396 g_hash_table_iter_init (&iter, params);
397 while (g_hash_table_iter_next (&iter, &k, &value))
398 mcp_account_manager_set_value (am, acc, k, value);
400 g_hash_table_unref (params);
402 /* Stored properties */
403 keys = g_key_file_get_keys (priv->store, acc, &nkeys, NULL);
405 for (i = 0; i < nkeys; i++)
407 gchar *v = g_key_file_get_value (priv->store, acc, keys[i], NULL);
409 if (v != NULL)
411 mcp_account_manager_set_value (am, acc, keys[i], v);
412 g_free (v);
416 g_strfreev (keys);
418 /* Enabled */
419 get_enabled (self, am, acc, account);
421 else if (!tp_strdiff (key, "Enabled"))
423 get_enabled (self, am, acc, account);
425 else
427 /* get a specific key */
428 GHashTable *params = get_tp_parameters (account);
429 gchar *value;
431 value = g_hash_table_lookup (params, key);
433 if (value == NULL)
434 value = g_key_file_get_value (priv->store, acc, key, NULL);
435 else
436 value = g_strdup (value);
438 mcp_account_manager_set_value (am, acc, key, value);
440 g_hash_table_unref (params);
441 g_free (value);
444 return TRUE;
447 static gboolean
448 account_is_in_goa (const McpAccountStorage *self,
449 const gchar *account)
451 McpAccountManagerGoaPrivate *priv = GET_PRIVATE (self);
453 return (g_hash_table_lookup (priv->accounts, account) != NULL);
456 static gboolean
457 mcp_account_manager_goa_set (const McpAccountStorage *self,
458 const McpAccountManager *am,
459 const gchar *account,
460 const gchar *key,
461 const gchar *val)
463 McpAccountManagerGoaPrivate *priv = GET_PRIVATE (self);
465 if (!account_is_in_goa (self, account))
466 return FALSE;
468 DEBUG ("%s: (%s, %s, %s)", G_STRFUNC, account, key, val);
470 if (!tp_strdiff (key, "Enabled"))
472 GoaObject *object;
473 GoaAccount *acc;
475 object = g_hash_table_lookup (priv->accounts, account);
477 if (object == NULL)
478 return FALSE;
480 acc = goa_object_peek_account (object);
482 if (acc == NULL)
483 return FALSE;
485 goa_account_set_chat_disabled (acc, tp_strdiff (val, "true"));
486 goto out;
489 if (val != NULL)
490 g_key_file_set_value (priv->store, account, key, val);
491 else
492 g_key_file_remove_key (priv->store, account, key, NULL);
494 out:
495 /* Pretend we save everything so MC won't save this in accounts.cfg */
496 return TRUE;
500 static gboolean
501 mcp_account_manager_goa_delete (const McpAccountStorage *self,
502 const McpAccountManager *am,
503 const gchar *account,
504 const gchar *key)
506 McpAccountManagerGoaPrivate *priv = GET_PRIVATE (self);
508 if (!account_is_in_goa (self, account))
509 return FALSE;
511 DEBUG ("%s: (%s, %s)", G_STRFUNC, account, key);
513 if (key == NULL)
515 g_key_file_remove_group (priv->store, account, NULL);
517 else
519 g_key_file_remove_key (priv->store, account, key, NULL);
522 /* Pretend we deleted everything */
523 return TRUE;
527 static gboolean
528 mcp_account_manager_goa_commit (const McpAccountStorage *self,
529 const McpAccountManager *am)
531 McpAccountManagerGoaPrivate *priv = GET_PRIVATE (self);
532 gchar *data;
533 gsize len;
534 GError *error = NULL;
536 DEBUG ("Save config to %s", priv->filename);
538 data = g_key_file_to_data (priv->store, &len, &error);
539 if (data == NULL)
541 DEBUG ("Failed to get data from store: %s", error->message);
543 g_error_free (error);
544 return FALSE;
547 if (!g_file_set_contents (priv->filename, data, len, &error))
549 DEBUG ("Failed to write file: %s", error->message);
551 g_free (data);
552 g_error_free (error);
553 return FALSE;
556 g_free (data);
558 return TRUE;
562 static void
563 mcp_account_manager_goa_ready (const McpAccountStorage *self,
564 const McpAccountManager *am)
566 McpAccountManagerGoaPrivate *priv = GET_PRIVATE (self);
568 priv->ready = TRUE;
572 static guint
573 mcp_account_manager_goa_get_restrictions (const McpAccountStorage *self,
574 const gchar *account)
576 return TP_STORAGE_RESTRICTION_FLAG_CANNOT_SET_PARAMETERS |
577 TP_STORAGE_RESTRICTION_FLAG_CANNOT_SET_SERVICE;
581 static void
582 mcp_account_manager_goa_get_identifier (const McpAccountStorage *self,
583 const gchar *acc,
584 GValue *identifier)
586 McpAccountManagerGoaPrivate *priv = GET_PRIVATE (self);
587 GoaObject *object;
588 GoaAccount *account;
590 object = g_hash_table_lookup (priv->accounts, acc);
591 g_return_if_fail (object != NULL);
593 account = goa_object_peek_account (object);
594 g_return_if_fail (account != NULL);
596 g_value_init (identifier, G_TYPE_STRING);
597 g_value_set_string (identifier, goa_account_get_id (account));
601 static void
602 account_storage_iface_init (McpAccountStorageIface *iface)
604 iface->name = PLUGIN_NAME;
605 iface->desc = PLUGIN_DESCRIPTION;
606 iface->priority = PLUGIN_PRIORITY;
607 iface->provider = PLUGIN_PROVIDER;
609 #define IMPLEMENT(x) iface->x = mcp_account_manager_goa_##x
610 IMPLEMENT (get);
611 IMPLEMENT (list);
612 IMPLEMENT (set);
613 IMPLEMENT (delete);
614 IMPLEMENT (commit);
615 IMPLEMENT (ready);
616 IMPLEMENT (get_restrictions);
617 IMPLEMENT (get_identifier);
618 #undef IMPLEMENT