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>
28 #include <glib/gi18n.h>
30 #include <telepathy-glib/util.h>
32 #define GOA_API_IS_SUBJECT_TO_CHANGE /* awesome! */
35 #include "mcp-account-manager-goa.h"
38 #define GET_PRIVATE(self) (((McpAccountManagerGoa *) self)->priv)
39 #define DECLARE_GASYNC_CALLBACK(name) \
40 static void name (GObject *, GAsyncResult *, gpointer);
42 #define PLUGIN_NAME "goa"
43 #define PLUGIN_PRIORITY (MCP_ACCOUNT_STORAGE_PLUGIN_PRIO_KEYRING + 10)
44 #define PLUGIN_DESCRIPTION "Provide Telepathy Accounts from GOA"
45 #define PLUGIN_PROVIDER EMPATHY_GOA_PROVIDER
47 #define INITIAL_COMMENT "Parameters of GOA Telepathy accounts"
49 static void account_storage_iface_init (McpAccountStorageIface *iface);
51 G_DEFINE_TYPE_WITH_CODE (McpAccountManagerGoa,
52 mcp_account_manager_goa,
54 G_IMPLEMENT_INTERFACE (MCP_TYPE_ACCOUNT_STORAGE,
55 account_storage_iface_init))
57 struct _McpAccountManagerGoaPrivate
62 GHashTable *accounts; /* alloc'ed string -> ref'ed GoaObject */
70 mcp_account_manager_goa_dispose (GObject *self)
72 McpAccountManagerGoaPrivate *priv = GET_PRIVATE (self);
74 tp_clear_object (&priv->client);
76 G_OBJECT_CLASS (mcp_account_manager_goa_parent_class)->dispose (self);
81 mcp_account_manager_goa_finalize (GObject *self)
83 McpAccountManagerGoaPrivate *priv = GET_PRIVATE (self);
85 g_hash_table_unref (priv->accounts);
86 g_key_file_free (priv->store);
87 g_free (priv->filename);
89 G_OBJECT_CLASS (mcp_account_manager_goa_parent_class)->finalize (self);
94 mcp_account_manager_goa_class_init (McpAccountManagerGoaClass *klass)
96 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
98 gobject_class->dispose = mcp_account_manager_goa_dispose;
99 gobject_class->finalize = mcp_account_manager_goa_finalize;
101 g_type_class_add_private (gobject_class,
102 sizeof (McpAccountManagerGoaPrivate));
106 get_tp_parameters (GoaAccount *account)
108 GHashTable *params = g_hash_table_new_full (g_str_hash, g_str_equal,
110 const char *type = goa_account_get_provider_type (account);
112 #define PARAM(key, value) g_hash_table_insert (params, key, g_strdup (value));
114 if (!tp_strdiff (type, "google"))
116 PARAM ("manager", "gabble");
117 PARAM ("protocol", "jabber");
118 PARAM ("Icon", "im-google-talk");
119 PARAM ("Service", "google-talk");
121 PARAM ("param-account", goa_account_get_identity (account));
122 PARAM ("param-server", "talk.google.com");
123 PARAM ("param-fallback-servers",
124 "talkx.l.google.com;"
125 "talkx.l.google.com:443,oldssl;"
126 "talkx.l.google.com:80");
127 PARAM ("param-extra-certificate-identities", "talk.google.com");
128 PARAM ("param-require-encryption", "true");
130 else if (!tp_strdiff (type, "facebook"))
132 PARAM ("manager", "gabble");
133 PARAM ("protocol", "jabber");
134 PARAM ("Icon", "im-facebook");
135 PARAM ("Service", "facebook");
137 PARAM ("param-account", "chat.facebook.com");
138 PARAM ("param-server", "chat.facebook.com");
139 PARAM ("param-require-encryption", "true");
141 else if (!tp_strdiff (type, "windows_live"))
143 PARAM ("manager", "gabble");
144 PARAM ("protocol", "jabber");
145 PARAM ("Icon", "im-msn");
146 PARAM ("Service", "windows-live");
148 PARAM ("param-account", "messenger.live.com");
149 PARAM ("param-require-encryption", "true");
153 DEBUG ("Unknown account type %s", type);
154 g_hash_table_unref (params);
158 /* generic properties */
159 PARAM ("DisplayName", goa_account_get_presentation_identity (account));
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);
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"),
183 g_hash_table_unref (params);
189 object_chat_changed_cb (GoaObject *object,
191 McpAccountManagerGoa *self)
193 GoaAccount *account = goa_object_peek_account (object);
194 char *name = get_tp_account_name (account);
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);
206 _new_account (McpAccountManagerGoa *self,
209 GoaAccount *account = goa_object_peek_account (object);
210 char *account_name = get_tp_account_name (account);
212 if (account_name == NULL)
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);
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))
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);
246 g_key_file_set_comment (self->priv->store, NULL, NULL, INITIAL_COMMENT,
249 g_error_free (error);
254 mcp_account_manager_goa_init (McpAccountManagerGoa *self)
258 DEBUG ("GOA MC plugin initialised");
260 self->priv = G_TYPE_INSTANCE_GET_PRIVATE (self,
261 MCP_TYPE_ACCOUNT_MANAGER_GOA, McpAccountManagerGoaPrivate);
263 self->priv->accounts = g_hash_table_new_full (g_str_hash, g_str_equal,
264 g_free, g_object_unref);
266 goa_client_new (NULL, _goa_client_new_cb, self);
269 self->priv->store = g_key_file_new ();
270 self->priv->filename = g_build_filename (g_get_user_data_dir (), "telepathy",
271 "mission-control", "accounts-goa.cfg", NULL);
278 _account_added_cb (GoaClient *client,
280 McpAccountManagerGoa *self)
282 _new_account (self, object);
287 _account_removed_cb (GoaClient *client,
289 McpAccountManagerGoa *self)
291 GoaAccount *account = goa_object_peek_account (object);
292 char *name = get_tp_account_name (account);
294 if (self->priv->ready)
295 g_signal_emit_by_name (self, "deleted", name);
297 g_hash_table_remove (self->priv->accounts, name);
303 _goa_client_new_cb (GObject *obj,
304 GAsyncResult *result,
307 McpAccountManagerGoa *self = user_data;
309 GList *accounts, *ptr;
310 GError *error = NULL;
312 self->priv->client = goa_client_new_finish (result, &error);
316 DEBUG ("Failed to connect to GOA");
320 accounts = goa_client_get_accounts (self->priv->client);
322 for (ptr = accounts; ptr != NULL; ptr = ptr->next)
324 _new_account (self, ptr->data);
327 g_list_free_full (accounts, g_object_unref);
329 g_signal_connect (self->priv->client, "account-added",
330 G_CALLBACK (_account_added_cb), self);
331 g_signal_connect (self->priv->client, "account-removed",
332 G_CALLBACK (_account_removed_cb), self);
337 mcp_account_manager_goa_list (const McpAccountStorage *self,
338 const McpAccountManager *am)
340 McpAccountManagerGoaPrivate *priv = GET_PRIVATE (self);
341 GList *accounts = NULL;
347 g_hash_table_iter_init (&iter, priv->accounts);
348 while (g_hash_table_iter_next (&iter, &key, NULL))
349 accounts = g_list_prepend (accounts, g_strdup (key));
356 get_enabled (const McpAccountStorage *self,
357 const McpAccountManager *am,
361 mcp_account_manager_set_value (am, acct, "Enabled",
362 goa_object_peek_chat (object) != NULL ? "true" : "false");
367 mcp_account_manager_goa_get (const McpAccountStorage *self,
368 const McpAccountManager *am,
372 McpAccountManagerGoaPrivate *priv = GET_PRIVATE (self);
376 DEBUG ("%s: %s, %s", G_STRFUNC, acct, key);
378 object = g_hash_table_lookup (priv->accounts, acct);
383 account = goa_object_peek_account (object);
391 GHashTable *params = get_tp_parameters (account);
398 /* Properties from GOA */
399 g_hash_table_iter_init (&iter, params);
400 while (g_hash_table_iter_next (&iter, &key, &value))
401 mcp_account_manager_set_value (am, acct, key, value);
403 g_hash_table_unref (params);
405 /* Stored properties */
406 keys = g_key_file_get_keys (priv->store, acct, &nkeys, NULL);
408 for (i = 0; i < nkeys; i++)
410 gchar *v = g_key_file_get_value (priv->store, acct, keys[i], NULL);
414 mcp_account_manager_set_value (am, acct, keys[i], v);
422 get_enabled (self, am, acct, object);
424 else if (!tp_strdiff (key, "Enabled"))
426 get_enabled (self, am, acct, object);
430 /* get a specific key */
431 GHashTable *params = get_tp_parameters (account);
434 value = g_hash_table_lookup (params, key);
437 value = g_key_file_get_value (priv->store, acct, key, NULL);
439 value = g_strdup (value);
441 mcp_account_manager_set_value (am, acct, key, value);
443 g_hash_table_unref (params);
451 account_is_in_goa (const McpAccountStorage *self,
452 const gchar *account)
454 McpAccountManagerGoaPrivate *priv = GET_PRIVATE (self);
456 return (g_hash_table_lookup (priv->accounts, acct) != NULL);
460 mcp_account_manager_goa_set (const McpAccountStorage *self,
461 const McpAccountManager *am,
462 const gchar *account,
466 McpAccountManagerGoaPrivate *priv = GET_PRIVATE (self);
467 GError *error = NULL;
469 if (!account_is_in_goa (self, account))
472 /* No need to save Enabled, it's up to the GOA configuration if the account
473 * is configured or not. */
474 if (!tp_strdiff (key, "Enabled"))
477 DEBUG ("%s: (%s, %s, %s)", G_STRFUNC, account, key, val);
480 g_key_file_set_value (priv->store, account, key, val);
482 g_key_file_remove_key (priv->store, account, key, NULL);
484 /* Pretend we save everything so MC won't save this in accounts.cfg */
490 mcp_account_manager_goa_delete (const McpAccountStorage *self,
491 const McpAccountManager *am,
492 const gchar *account,
495 McpAccountManagerGoaPrivate *priv = GET_PRIVATE (self);
497 if (!account_is_in_goa (self, account))
500 DEBUG ("%s: (%s, %s)", G_STRFUNC, account, key);
504 g_key_file_remove_group (priv->store, account, NULL);
508 g_key_file_remove_key (priv->store, account, key, NULL);
511 /* Pretend we deleted everything */
517 mcp_account_manager_goa_commit (const McpAccountStorage *self,
518 const McpAccountManager *am)
520 McpAccountManagerGoaPrivate *priv = GET_PRIVATE (self);
523 GError *error = NULL;
525 DEBUG ("Save config to %s", priv->filename);
527 data = g_key_file_to_data (priv->store, &len, &error);
530 DEBUG ("Failed to get data from store: %s", error->message);
532 g_error_free (error);
536 if (!g_file_set_contents (priv->filename, data, len, &error))
538 DEBUG ("Failed to write file: %s", error->message);
541 g_error_free (error);
552 mcp_account_manager_goa_ready (const McpAccountStorage *self,
553 const McpAccountManager *am)
555 McpAccountManagerGoaPrivate *priv = GET_PRIVATE (self);
562 mcp_account_manager_goa_get_restrictions (const McpAccountStorage *self,
563 const gchar *account)
565 return TP_STORAGE_RESTRICTION_FLAG_CANNOT_SET_PARAMETERS |
566 TP_STORAGE_RESTRICTION_FLAG_CANNOT_SET_SERVICE |
567 TP_STORAGE_RESTRICTION_FLAG_CANNOT_SET_ENABLED;
572 mcp_account_manager_goa_get_identifier (const McpAccountStorage *self,
576 McpAccountManagerGoaPrivate *priv = GET_PRIVATE (self);
580 object = g_hash_table_lookup (priv->accounts, acct);
581 g_return_if_fail (object != NULL);
583 account = goa_object_peek_account (object);
584 g_return_if_fail (account != NULL);
586 g_value_init (identifier, G_TYPE_STRING);
587 g_value_set_string (identifier, goa_account_get_id (account));
592 account_storage_iface_init (McpAccountStorageIface *iface)
594 mcp_account_storage_iface_set_name (iface, PLUGIN_NAME);
595 mcp_account_storage_iface_set_desc (iface, PLUGIN_DESCRIPTION);
596 mcp_account_storage_iface_set_priority (iface, PLUGIN_PRIORITY);
597 mcp_account_storage_iface_set_provider (iface, PLUGIN_PROVIDER);
599 #define IMPLEMENT(x) mcp_account_storage_iface_implement_##x(iface, \
600 mcp_account_manager_goa_##x)
607 IMPLEMENT (get_restrictions);
608 IMPLEMENT (get_identifier);