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/>.
23 * Danielle Madeley <danielle.madeley@collabora.co.uk>
26 #include <glib/gi18n.h>
28 #include <telepathy-glib/util.h>
30 #define GOA_API_IS_SUBJECT_TO_CHANGE /* awesome! */
33 #include "mcp-account-manager-goa.h"
36 #define GET_PRIVATE(self) (((McpAccountManagerGoa *) self)->priv)
37 #define DECLARE_GASYNC_CALLBACK(name) \
38 static void name (GObject *, GAsyncResult *, gpointer);
40 #define PLUGIN_NAME "goa"
41 #define PLUGIN_PRIORITY (MCP_ACCOUNT_STORAGE_PLUGIN_PRIO_KEYRING + 10)
42 #define PLUGIN_DESCRIPTION "Provide Telepathy Accounts from GOA"
43 #define PLUGIN_PROVIDER "org.gnome.OnlineAccounts"
45 #define INITIAL_COMMENT "Parameters of GOA Telepathy accounts"
47 static void account_storage_iface_init (McpAccountStorageIface *iface);
49 G_DEFINE_TYPE_WITH_CODE (McpAccountManagerGoa,
50 mcp_account_manager_goa,
52 G_IMPLEMENT_INTERFACE (MCP_TYPE_ACCOUNT_STORAGE,
53 account_storage_iface_init))
55 struct _McpAccountManagerGoaPrivate
60 GHashTable *accounts; /* alloc'ed string -> ref'ed GoaObject */
68 mcp_account_manager_goa_dispose (GObject *self)
70 McpAccountManagerGoaPrivate *priv = GET_PRIVATE (self);
72 tp_clear_object (&priv->client);
74 G_OBJECT_CLASS (mcp_account_manager_goa_parent_class)->dispose (self);
79 mcp_account_manager_goa_finalize (GObject *self)
81 McpAccountManagerGoaPrivate *priv = GET_PRIVATE (self);
83 g_hash_table_destroy (priv->accounts);
84 g_key_file_free (priv->store);
85 g_free (priv->filename);
87 G_OBJECT_CLASS (mcp_account_manager_goa_parent_class)->finalize (self);
92 mcp_account_manager_goa_class_init (McpAccountManagerGoaClass *klass)
94 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
96 gobject_class->dispose = mcp_account_manager_goa_dispose;
97 gobject_class->finalize = mcp_account_manager_goa_finalize;
99 g_type_class_add_private (gobject_class,
100 sizeof (McpAccountManagerGoaPrivate));
104 get_tp_parameters (GoaAccount *account)
106 GHashTable *params = g_hash_table_new_full (g_str_hash, g_str_equal,
108 const char *type = goa_account_get_provider_type (account);
110 #define PARAM(key, value) g_hash_table_insert (params, key, g_strdup (value));
112 if (!tp_strdiff (type, "google"))
114 PARAM ("manager", "gabble");
115 PARAM ("protocol", "jabber");
116 PARAM ("Icon", "im-google-talk");
117 PARAM ("Service", "google-talk");
119 PARAM ("param-account", goa_account_get_identity (account));
120 PARAM ("param-server", "talk.google.com");
121 PARAM ("param-fallback-servers",
122 "talkx.l.google.com;"
123 "talkx.l.google.com:443,oldssl;"
124 "talkx.l.google.com:80");
125 PARAM ("param-extra-certificate-identities", "talk.google.com");
126 PARAM ("param-require-encryption", "true");
130 /* unknown account type */
131 g_hash_table_destroy (params);
135 /* TODO: add Facebook support */
137 /* generic properties */
138 PARAM ("DisplayName", goa_account_get_presentation_identity (account));
147 get_tp_account_name (GoaAccount *account)
149 GHashTable *params = get_tp_parameters (account);
150 const char *type = goa_account_get_provider_type (account);
151 const char *id = goa_account_get_id (account);
157 name = g_strdup_printf ("%s/%s/goa_%s_%s",
158 (char *) g_hash_table_lookup (params, "manager"),
159 (char *) g_hash_table_lookup (params, "protocol"),
162 g_hash_table_destroy (params);
168 object_chat_changed_cb (GoaObject *object,
170 McpAccountManagerGoa *self)
172 GoaAccount *account = goa_object_peek_account (object);
173 char *name = get_tp_account_name (account);
176 enabled = (goa_object_peek_chat (object) != NULL);
178 DEBUG ("%s %s", name, enabled ? "enabled" : "disabled");
180 if (self->priv->ready)
181 g_signal_emit_by_name (self, "toggled", name, enabled);
185 _new_account (McpAccountManagerGoa *self,
188 GoaAccount *account = goa_object_peek_account (object);
189 char *account_name = get_tp_account_name (account);
191 if (account_name == NULL)
194 /* @account_name now is owned by the hash table */
195 g_hash_table_insert (self->priv->accounts, account_name,
196 g_object_ref (object));
198 if (self->priv->ready)
199 g_signal_emit_by_name (self, "created", account_name);
201 tp_g_signal_connect_object (object, "notify::chat",
202 G_CALLBACK (object_chat_changed_cb), self, 0);
206 DECLARE_GASYNC_CALLBACK (_goa_client_new_cb);
209 load_store (McpAccountManagerGoa *self)
211 GError *error = NULL;
213 if (!g_key_file_load_from_file (self->priv->store, self->priv->filename,
214 G_KEY_FILE_KEEP_COMMENTS, &error))
218 DEBUG ("Failed to load keyfile, creating a new one: %s", error->message);
220 dir = g_path_get_dirname (self->priv->filename);
222 g_mkdir_with_parents (dir, 0700);
225 g_key_file_set_comment (self->priv->store, NULL, NULL, INITIAL_COMMENT,
228 g_error_free (error);
233 mcp_account_manager_goa_init (McpAccountManagerGoa *self)
237 DEBUG ("GOA MC plugin initialised");
239 self->priv = G_TYPE_INSTANCE_GET_PRIVATE (self,
240 MCP_TYPE_ACCOUNT_MANAGER_GOA, McpAccountManagerGoaPrivate);
242 self->priv->accounts = g_hash_table_new_full (g_str_hash, g_str_equal,
243 g_free, g_object_unref);
245 goa_client_new (NULL, _goa_client_new_cb, self);
248 self->priv->store = g_key_file_new ();
249 self->priv->filename = g_build_filename (g_get_user_data_dir (), "telepathy",
250 "mission-control", "accounts-goa.cfg", NULL);
257 _account_added_cb (GoaClient *client,
259 McpAccountManagerGoa *self)
261 _new_account (self, object);
266 _account_removed_cb (GoaClient *client,
268 McpAccountManagerGoa *self)
270 GoaAccount *account = goa_object_peek_account (object);
271 char *name = get_tp_account_name (account);
273 if (self->priv->ready)
274 g_signal_emit_by_name (self, "deleted", name);
276 g_hash_table_remove (self->priv->accounts, name);
282 _goa_client_new_cb (GObject *obj,
283 GAsyncResult *result,
286 McpAccountManagerGoa *self = user_data;
288 GList *accounts, *ptr;
289 GError *error = NULL;
291 self->priv->client = goa_client_new_finish (result, &error);
295 DEBUG ("Failed to connect to GOA");
299 accounts = goa_client_get_accounts (self->priv->client);
301 for (ptr = accounts; ptr != NULL; ptr = ptr->next)
303 _new_account (self, ptr->data);
306 g_list_free_full (accounts, g_object_unref);
308 g_signal_connect (self->priv->client, "account-added",
309 G_CALLBACK (_account_added_cb), self);
310 g_signal_connect (self->priv->client, "account-removed",
311 G_CALLBACK (_account_removed_cb), self);
316 mcp_account_manager_goa_list (const McpAccountStorage *self,
317 const McpAccountManager *am)
319 McpAccountManagerGoaPrivate *priv = GET_PRIVATE (self);
320 GList *accounts = NULL;
326 g_hash_table_iter_init (&iter, priv->accounts);
327 while (g_hash_table_iter_next (&iter, &key, NULL))
328 accounts = g_list_prepend (accounts, g_strdup (key));
335 get_enabled (const McpAccountStorage *self,
336 const McpAccountManager *am,
340 mcp_account_manager_set_value (am, acct, "Enabled",
341 goa_object_peek_chat (object) != NULL ? "true" : "false");
346 mcp_account_manager_goa_get (const McpAccountStorage *self,
347 const McpAccountManager *am,
351 McpAccountManagerGoaPrivate *priv = GET_PRIVATE (self);
355 DEBUG ("%s: %s, %s", G_STRFUNC, acct, key);
357 object = g_hash_table_lookup (priv->accounts, acct);
362 account = goa_object_peek_account (object);
370 GHashTable *params = get_tp_parameters (account);
377 /* Properties from GOA */
378 g_hash_table_iter_init (&iter, params);
379 while (g_hash_table_iter_next (&iter, &key, &value))
380 mcp_account_manager_set_value (am, acct, key, value);
382 g_hash_table_destroy (params);
384 /* Stored properties */
385 keys = g_key_file_get_keys (priv->store, acct, &nkeys, NULL);
387 for (i = 0; i < nkeys; i++)
389 gchar *v = g_key_file_get_value (priv->store, acct, keys[i], NULL);
393 mcp_account_manager_set_value (am, acct, keys[i], v);
401 get_enabled (self, am, acct, object);
403 else if (!tp_strdiff (key, "Enabled"))
405 get_enabled (self, am, acct, object);
409 /* get a specific key */
410 GHashTable *params = get_tp_parameters (account);
413 value = g_hash_table_lookup (params, key);
416 value = g_key_file_get_value (priv->store, acct, key, NULL);
418 value = g_strdup (value);
420 mcp_account_manager_set_value (am, acct, key, value);
422 g_hash_table_destroy (params);
430 account_is_in_goa (const McpAccountStorage *self,
431 const gchar *account)
433 McpAccountManagerGoaPrivate *priv = GET_PRIVATE (self);
435 return (g_hash_table_lookup (priv->accounts, acct) != NULL);
439 mcp_account_manager_goa_set (const McpAccountStorage *self,
440 const McpAccountManager *am,
441 const gchar *account,
445 McpAccountManagerGoaPrivate *priv = GET_PRIVATE (self);
446 GError *error = NULL;
448 if (!account_is_in_goa (self, account))
451 /* No need to save Enabled, it's up to the GOA configuration if the account
452 * is configured or not. */
453 if (!tp_strdiff (key, "Enabled"))
456 DEBUG ("%s: (%s, %s, %s)", G_STRFUNC, account, key, val);
459 g_key_file_set_value (priv->store, account, key, val);
461 g_key_file_remove_key (priv->store, account, key, NULL);
463 /* Pretend we save everything so MC won't save this in accounts.cfg */
469 mcp_account_manager_goa_delete (const McpAccountStorage *self,
470 const McpAccountManager *am,
471 const gchar *account,
474 McpAccountManagerGoaPrivate *priv = GET_PRIVATE (self);
476 if (!account_is_in_goa (self, account))
479 DEBUG ("%s: (%s, %s)", G_STRFUNC, account, key);
483 g_key_file_remove_group (priv->store, account, NULL);
487 g_key_file_remove_key (priv->store, account, key, NULL);
490 /* Pretend we deleted everything */
496 mcp_account_manager_goa_commit (const McpAccountStorage *self,
497 const McpAccountManager *am)
499 McpAccountManagerGoaPrivate *priv = GET_PRIVATE (self);
502 GError *error = NULL;
504 DEBUG ("Save config to %s", priv->filename);
506 data = g_key_file_to_data (priv->store, &len, &error);
509 DEBUG ("Failed to get data from store: %s", error->message);
511 g_error_free (error);
515 if (!g_file_set_contents (priv->filename, data, len, &error))
517 DEBUG ("Failed to write file: %s", error->message);
520 g_error_free (error);
531 mcp_account_manager_goa_ready (const McpAccountStorage *self,
532 const McpAccountManager *am)
534 McpAccountManagerGoaPrivate *priv = GET_PRIVATE (self);
541 mcp_account_manager_goa_get_restrictions (const McpAccountStorage *self,
542 const gchar *account)
544 return TP_STORAGE_RESTRICTION_FLAG_CANNOT_SET_PARAMETERS |
545 TP_STORAGE_RESTRICTION_FLAG_CANNOT_SET_SERVICE |
546 TP_STORAGE_RESTRICTION_FLAG_CANNOT_SET_ENABLED;
551 mcp_account_manager_goa_get_identifier (const McpAccountStorage *self,
555 McpAccountManagerGoaPrivate *priv = GET_PRIVATE (self);
559 object = g_hash_table_lookup (priv->accounts, acct);
560 g_return_if_fail (object != NULL);
562 account = goa_object_peek_account (object);
563 g_return_if_fail (account != NULL);
565 g_value_init (identifier, G_TYPE_STRING);
566 g_value_set_string (identifier, goa_account_get_id (account));
571 account_storage_iface_init (McpAccountStorageIface *iface)
573 mcp_account_storage_iface_set_name (iface, PLUGIN_NAME);
574 mcp_account_storage_iface_set_desc (iface, PLUGIN_DESCRIPTION);
575 mcp_account_storage_iface_set_priority (iface, PLUGIN_PRIORITY);
576 mcp_account_storage_iface_set_provider (iface, PLUGIN_PROVIDER);
578 #define IMPLEMENT(x) mcp_account_storage_iface_implement_##x(iface, \
579 mcp_account_manager_goa_##x)
586 IMPLEMENT (get_restrictions);
587 IMPLEMENT (get_identifier);