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));
139 PARAM ("ConnectAutomatically", "true");
148 get_tp_account_name (GoaAccount *account)
150 GHashTable *params = get_tp_parameters (account);
151 const char *type = goa_account_get_provider_type (account);
152 const char *id = goa_account_get_id (account);
158 name = g_strdup_printf ("%s/%s/goa_%s_%s",
159 (char *) g_hash_table_lookup (params, "manager"),
160 (char *) g_hash_table_lookup (params, "protocol"),
163 g_hash_table_destroy (params);
169 object_chat_changed_cb (GoaObject *object,
171 McpAccountManagerGoa *self)
173 GoaAccount *account = goa_object_peek_account (object);
174 char *name = get_tp_account_name (account);
177 enabled = (goa_object_peek_chat (object) != NULL);
179 DEBUG ("%s %s", name, enabled ? "enabled" : "disabled");
181 if (self->priv->ready)
182 g_signal_emit_by_name (self, "toggled", name, enabled);
186 _new_account (McpAccountManagerGoa *self,
189 GoaAccount *account = goa_object_peek_account (object);
190 char *account_name = get_tp_account_name (account);
192 if (account_name == NULL)
195 /* @account_name now is owned by the hash table */
196 g_hash_table_insert (self->priv->accounts, account_name,
197 g_object_ref (object));
199 if (self->priv->ready)
200 g_signal_emit_by_name (self, "created", account_name);
202 tp_g_signal_connect_object (object, "notify::chat",
203 G_CALLBACK (object_chat_changed_cb), self, 0);
207 DECLARE_GASYNC_CALLBACK (_goa_client_new_cb);
210 load_store (McpAccountManagerGoa *self)
212 GError *error = NULL;
214 if (!g_key_file_load_from_file (self->priv->store, self->priv->filename,
215 G_KEY_FILE_KEEP_COMMENTS, &error))
219 DEBUG ("Failed to load keyfile, creating a new one: %s", error->message);
221 dir = g_path_get_dirname (self->priv->filename);
223 g_mkdir_with_parents (dir, 0700);
226 g_key_file_set_comment (self->priv->store, NULL, NULL, INITIAL_COMMENT,
229 g_error_free (error);
234 mcp_account_manager_goa_init (McpAccountManagerGoa *self)
238 DEBUG ("GOA MC plugin initialised");
240 self->priv = G_TYPE_INSTANCE_GET_PRIVATE (self,
241 MCP_TYPE_ACCOUNT_MANAGER_GOA, McpAccountManagerGoaPrivate);
243 self->priv->accounts = g_hash_table_new_full (g_str_hash, g_str_equal,
244 g_free, g_object_unref);
246 goa_client_new (NULL, _goa_client_new_cb, self);
249 self->priv->store = g_key_file_new ();
250 self->priv->filename = g_build_filename (g_get_user_data_dir (), "telepathy",
251 "mission-control", "accounts-goa.cfg", NULL);
258 _account_added_cb (GoaClient *client,
260 McpAccountManagerGoa *self)
262 _new_account (self, object);
267 _account_removed_cb (GoaClient *client,
269 McpAccountManagerGoa *self)
271 GoaAccount *account = goa_object_peek_account (object);
272 char *name = get_tp_account_name (account);
274 if (self->priv->ready)
275 g_signal_emit_by_name (self, "deleted", name);
277 g_hash_table_remove (self->priv->accounts, name);
283 _goa_client_new_cb (GObject *obj,
284 GAsyncResult *result,
287 McpAccountManagerGoa *self = user_data;
289 GList *accounts, *ptr;
290 GError *error = NULL;
292 self->priv->client = goa_client_new_finish (result, &error);
296 DEBUG ("Failed to connect to GOA");
300 accounts = goa_client_get_accounts (self->priv->client);
302 for (ptr = accounts; ptr != NULL; ptr = ptr->next)
304 _new_account (self, ptr->data);
307 g_list_free_full (accounts, g_object_unref);
309 g_signal_connect (self->priv->client, "account-added",
310 G_CALLBACK (_account_added_cb), self);
311 g_signal_connect (self->priv->client, "account-removed",
312 G_CALLBACK (_account_removed_cb), self);
317 mcp_account_manager_goa_list (const McpAccountStorage *self,
318 const McpAccountManager *am)
320 McpAccountManagerGoaPrivate *priv = GET_PRIVATE (self);
321 GList *accounts = NULL;
327 g_hash_table_iter_init (&iter, priv->accounts);
328 while (g_hash_table_iter_next (&iter, &key, NULL))
329 accounts = g_list_prepend (accounts, g_strdup (key));
336 get_enabled (const McpAccountStorage *self,
337 const McpAccountManager *am,
341 mcp_account_manager_set_value (am, acct, "Enabled",
342 goa_object_peek_chat (object) != NULL ? "true" : "false");
347 mcp_account_manager_goa_get (const McpAccountStorage *self,
348 const McpAccountManager *am,
352 McpAccountManagerGoaPrivate *priv = GET_PRIVATE (self);
356 DEBUG ("%s: %s, %s", G_STRFUNC, acct, key);
358 object = g_hash_table_lookup (priv->accounts, acct);
363 account = goa_object_peek_account (object);
371 GHashTable *params = get_tp_parameters (account);
378 /* Properties from GOA */
379 g_hash_table_iter_init (&iter, params);
380 while (g_hash_table_iter_next (&iter, &key, &value))
381 mcp_account_manager_set_value (am, acct, key, value);
383 g_hash_table_destroy (params);
385 /* Stored properties */
386 keys = g_key_file_get_keys (priv->store, acct, &n, NULL);
391 for (i = 0; i < n; i++)
393 gchar *v = g_key_file_get_value (priv->store, acct, keys[i], NULL);
397 mcp_account_manager_set_value (am, acct, keys[i], v);
403 get_enabled (self, am, acct, object);
405 else if (!tp_strdiff (key, "Enabled"))
407 get_enabled (self, am, acct, object);
411 /* get a specific key */
412 GHashTable *params = get_tp_parameters (account);
415 value = g_hash_table_lookup (params, key);
418 value = g_key_file_get_value (priv->store, acct, key, NULL);
420 value = g_strdup (value);
422 mcp_account_manager_set_value (am, acct, key, value);
424 g_hash_table_destroy (params);
433 mcp_account_manager_goa_set (const McpAccountStorage *self,
434 const McpAccountManager *am,
435 const gchar *account,
439 McpAccountManagerGoaPrivate *priv = GET_PRIVATE (self);
440 GError *error = NULL;
442 /* No need to save Enabled, it's up to the GOA configuration if the account
443 * is configured or not. */
444 if (!tp_strdiff (key, "Enabled"))
447 DEBUG ("%s: (%s, %s, %s)", G_STRFUNC, account, key, val);
450 g_key_file_set_value (priv->store, account, key, val);
452 g_key_file_remove_key (priv->store, account, key, NULL);
454 /* Pretend we save everything so MC won't save this in accounts.cfg */
460 mcp_account_manager_goa_delete (const McpAccountStorage *self,
461 const McpAccountManager *am,
462 const gchar *account,
465 McpAccountManagerGoaPrivate *priv = GET_PRIVATE (self);
467 DEBUG ("%s: (%s, %s)", G_STRFUNC, account, key);
471 g_key_file_remove_group (priv->store, account, NULL);
475 g_key_file_remove_key (priv->store, account, key, NULL);
478 /* Pretend we deleted everything */
484 mcp_account_manager_goa_commit (const McpAccountStorage *self,
485 const McpAccountManager *am)
487 McpAccountManagerGoaPrivate *priv = GET_PRIVATE (self);
490 GError *error = NULL;
492 DEBUG ("Save config to %s", priv->filename);
494 data = g_key_file_to_data (priv->store, &len, &error);
497 DEBUG ("Failed to get data from store: %s", error->message);
499 g_error_free (error);
503 if (!g_file_set_contents (priv->filename, data, len, &error))
505 DEBUG ("Failed to write file: %s", error->message);
508 g_error_free (error);
519 mcp_account_manager_goa_ready (const McpAccountStorage *self,
520 const McpAccountManager *am)
522 McpAccountManagerGoaPrivate *priv = GET_PRIVATE (self);
529 mcp_account_manager_goa_get_restrictions (const McpAccountStorage *self,
530 const gchar *account)
532 return TP_STORAGE_RESTRICTION_FLAG_CANNOT_SET_PARAMETERS |
533 TP_STORAGE_RESTRICTION_FLAG_CANNOT_SET_SERVICE |
534 TP_STORAGE_RESTRICTION_FLAG_CANNOT_SET_ENABLED;
539 mcp_account_manager_goa_get_identifier (const McpAccountStorage *self,
543 McpAccountManagerGoaPrivate *priv = GET_PRIVATE (self);
547 object = g_hash_table_lookup (priv->accounts, acct);
548 g_return_if_fail (object != NULL);
550 account = goa_object_peek_account (object);
551 g_return_if_fail (account != NULL);
553 g_value_init (identifier, G_TYPE_STRING);
554 g_value_set_string (identifier, goa_account_get_id (account));
559 account_storage_iface_init (McpAccountStorageIface *iface)
561 mcp_account_storage_iface_set_name (iface, PLUGIN_NAME);
562 mcp_account_storage_iface_set_desc (iface, PLUGIN_DESCRIPTION);
563 mcp_account_storage_iface_set_priority (iface, PLUGIN_PRIORITY);
564 mcp_account_storage_iface_set_provider (iface, PLUGIN_PROVIDER);
566 #define IMPLEMENT(x) mcp_account_storage_iface_implement_##x(iface, \
567 mcp_account_manager_goa_##x)
574 IMPLEMENT (get_restrictions);
575 IMPLEMENT (get_identifier);