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 static void account_storage_iface_init (McpAccountStorageIface *iface);
47 G_DEFINE_TYPE_WITH_CODE (McpAccountManagerGoa,
48 mcp_account_manager_goa,
50 G_IMPLEMENT_INTERFACE (MCP_TYPE_ACCOUNT_STORAGE,
51 account_storage_iface_init))
53 struct _McpAccountManagerGoaPrivate
58 GHashTable *accounts; /* alloc'ed string -> ref'ed GoaAccount */
63 mcp_account_manager_goa_dispose (GObject *self)
65 McpAccountManagerGoaPrivate *priv = GET_PRIVATE (self);
67 tp_clear_object (&priv->client);
69 G_OBJECT_CLASS (mcp_account_manager_goa_parent_class)->dispose (self);
74 mcp_account_manager_goa_finalize (GObject *self)
76 McpAccountManagerGoaPrivate *priv = GET_PRIVATE (self);
78 g_hash_table_destroy (priv->accounts);
80 G_OBJECT_CLASS (mcp_account_manager_goa_parent_class)->finalize (self);
85 mcp_account_manager_goa_class_init (McpAccountManagerGoaClass *klass)
87 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
89 gobject_class->dispose = mcp_account_manager_goa_dispose;
90 gobject_class->finalize = mcp_account_manager_goa_finalize;
92 g_type_class_add_private (gobject_class,
93 sizeof (McpAccountManagerGoaPrivate));
98 // _enable_chat_toggled (GConfClient *client,
100 // GConfEntry *entry,
101 // gpointer user_data)
103 // McpAccountStorage *self = MCP_ACCOUNT_STORAGE (user_data);
104 // McpAccountManagerGoaPrivate *priv = GET_PRIVATE (self);
105 // gboolean enabled = gconf_value_get_bool (gconf_entry_get_value (entry));
107 // DEBUG ("%s: %s", G_STRFUNC, enabled ? "enabled" : "disabled");
109 // if (priv->ready && priv->facebook_available)
111 // DEBUG ("Emitting toggled signal");
113 // g_signal_emit_by_name (self, "toggled", FACEBOOK_ACCOUNT_NAME, enabled);
119 get_tp_parameters (GoaAccount *account)
121 GHashTable *params = g_hash_table_new_full (g_str_hash, g_str_equal,
123 const char *type = goa_account_get_provider_type (account);
125 #define PARAM(key, value) g_hash_table_insert (params, key, g_strdup (value));
127 // { "param-account", "chat.facebook.com" },
128 // { "param-server", "chat.facebook.com" },
130 if (!tp_strdiff (type, "google"))
132 PARAM ("manager", "gabble");
133 PARAM ("protocol", "jabber");
134 PARAM ("Icon", "im-google-talk");
135 PARAM ("Service", "google-talk");
137 PARAM ("param-account", goa_account_get_identity (account));
138 PARAM ("param-server", "talk.google.com");
142 /* unknown account type */
143 g_hash_table_destroy (params);
147 /* generic properties */
148 PARAM ("DisplayName", goa_account_get_presentation_identity (account));
149 PARAM ("ConnectAutomatically", "true");
158 get_tp_account_name (GoaAccount *account)
160 GHashTable *params = get_tp_parameters (account);
161 const char *type = goa_account_get_provider_type (account);
162 const char *id = goa_account_get_id (account);
168 name = g_strdup_printf ("%s/%s/goa_%s_%s",
169 (char *) g_hash_table_lookup (params, "manager"),
170 (char *) g_hash_table_lookup (params, "protocol"),
173 g_hash_table_destroy (params);
180 _new_account (McpAccountManagerGoa *self,
183 char *account_name = get_tp_account_name (account);
185 if (account_name == NULL)
188 /* @account_name now is owned by the hash table */
189 g_hash_table_insert (self->priv->accounts, account_name,
190 g_object_ref (account));
192 if (self->priv->ready)
193 g_signal_emit_by_name (self, "created", account_name);
197 DECLARE_GASYNC_CALLBACK (_goa_client_new_cb);
200 mcp_account_manager_goa_init (McpAccountManagerGoa *self)
202 DEBUG ("GOA MC plugin initialised");
204 self->priv = G_TYPE_INSTANCE_GET_PRIVATE (self,
205 MCP_TYPE_ACCOUNT_MANAGER_GOA, McpAccountManagerGoaPrivate);
207 self->priv->accounts = g_hash_table_new_full (g_str_hash, g_str_equal,
208 g_free, g_object_unref);
210 goa_client_new (NULL, _goa_client_new_cb, self);
215 _account_added_cb (GoaClient *client,
217 McpAccountManagerGoa *self)
219 GoaAccount *account = goa_object_peek_account (object);
221 _new_account (self, account);
226 _account_removed_cb (GoaClient *client,
228 McpAccountManagerGoa *self)
230 GoaAccount *account = goa_object_peek_account (object);
231 char *name = get_tp_account_name (account);
233 if (self->priv->ready)
234 g_signal_emit_by_name (self, "deleted", name);
236 g_hash_table_remove (self->priv->accounts, name);
243 _goa_client_new_cb (GObject *obj,
244 GAsyncResult *result,
247 McpAccountManagerGoa *self = user_data;
249 GList *accounts, *ptr;
250 GError *error = NULL;
252 self->priv->client = goa_client_new_finish (result, &error);
256 DEBUG ("Failed to connect to GOA");
260 accounts = goa_client_get_accounts (self->priv->client);
262 for (ptr = accounts; ptr != NULL; ptr = ptr->next)
264 GoaAccount *account = goa_object_peek_account (ptr->data);
266 _new_account (self, account);
269 g_list_free_full (accounts, g_object_unref);
271 g_signal_connect (self->priv->client, "account-added",
272 G_CALLBACK (_account_added_cb), self);
273 g_signal_connect (self->priv->client, "account-removed",
274 G_CALLBACK (_account_removed_cb), self);
275 /* FIXME: do we need to support account-changed ? */
280 mcp_account_manager_goa_list (const McpAccountStorage *self,
281 const McpAccountManager *am)
283 McpAccountManagerGoaPrivate *priv = GET_PRIVATE (self);
284 GList *accounts = NULL;
290 g_hash_table_iter_init (&iter, priv->accounts);
291 while (g_hash_table_iter_next (&iter, &key, NULL))
292 accounts = g_list_prepend (accounts, g_strdup (key));
299 get_enabled (const McpAccountStorage *self,
300 const McpAccountManager *am,
303 // McpAccountManagerGoaPrivate *priv = GET_PRIVATE (self);
304 // GError *error = NULL;
306 // const char *value;
308 // enabled = gconf_client_get_bool (priv->gconf,
309 // BISHO_FB_GCONF_ENABLE_CHAT_KEY, &error);
310 // if (error != NULL)
312 // g_warning ("Unabled to get value for %s/Enabled from GConf: %s",
313 // acct, error->message);
314 // g_clear_error (&error);
317 // value = enabled ? "true" : "false";
319 // DEBUG ("Facebook Chat enabled = %s", value);
321 mcp_account_manager_set_value (am, acct, "Enabled", "true");
326 mcp_account_manager_goa_get (const McpAccountStorage *self,
327 const McpAccountManager *am,
331 McpAccountManagerGoaPrivate *priv = GET_PRIVATE (self);
334 DEBUG ("%s: %s, %s", G_STRFUNC, acct, key);
336 account = g_hash_table_lookup (priv->accounts, acct);
344 GHashTable *params = get_tp_parameters (account);
348 g_hash_table_iter_init (&iter, params);
349 while (g_hash_table_iter_next (&iter, &key, &value))
350 mcp_account_manager_set_value (am, acct, key, value);
352 g_hash_table_destroy (params);
354 get_enabled (self, am, acct);
356 else if (!tp_strdiff (key, "Enabled"))
358 get_enabled (self, am, acct);
362 /* get a specific key */
363 GHashTable *params = get_tp_parameters (account);
365 mcp_account_manager_set_value (am, acct, key,
366 g_hash_table_lookup (params, key));
368 g_hash_table_destroy (params);
376 mcp_account_manager_goa_set (const McpAccountStorage *self,
377 const McpAccountManager *am,
382 GError *error = NULL;
384 DEBUG ("%s: (%s, %s, %s)", G_STRFUNC, acct, key, val);
386 // if (!tp_strdiff (acct, FACEBOOK_ACCOUNT_NAME))
388 // if (!tp_strdiff (key, "Enabled"))
390 // gboolean enabled = !tp_strdiff (val, "true");
392 // DEBUG ("set Enabled to %s", enabled ? "enabled" : "disabled");
394 // gconf_client_set_bool (priv->gconf, BISHO_FB_GCONF_ENABLE_CHAT_KEY,
396 // if (error != NULL)
398 // g_warning ("Unable to save %s/Enable state in GConf: %s",
399 // acct, error->message);
400 // g_clear_error (&error);
406 // /* pretend we saved everything else */
417 mcp_account_manager_goa_delete (const McpAccountStorage *self,
418 const McpAccountManager *am,
422 DEBUG ("%s: (%s, %s)", G_STRFUNC, acct, key);
424 // if (!tp_strdiff (acct, FACEBOOK_ACCOUNT_NAME))
426 // if (!tp_strdiff (key, "Enabled"))
429 // /* pretend we deleted everything else */
440 mcp_account_manager_goa_commit (const McpAccountStorage *self,
441 const McpAccountManager *am)
443 DEBUG ("%s", G_STRFUNC);
450 mcp_account_manager_goa_ready (const McpAccountStorage *self,
451 const McpAccountManager *am)
453 McpAccountManagerGoaPrivate *priv = GET_PRIVATE (self);
460 mcp_account_manager_goa_get_restrictions (const McpAccountStorage *self,
461 const gchar *account)
463 return TP_STORAGE_RESTRICTION_FLAG_CANNOT_SET_PARAMETERS |
464 TP_STORAGE_RESTRICTION_FLAG_CANNOT_SET_SERVICE;
469 mcp_account_manager_goa_get_identifier (const McpAccountStorage *self,
473 McpAccountManagerGoaPrivate *priv = GET_PRIVATE (self);
476 account = g_hash_table_lookup (priv->accounts, acct);
478 g_return_if_fail (account != NULL);
480 g_value_init (identifier, G_TYPE_STRING);
481 g_value_set_string (identifier, goa_account_get_id (account));
486 account_storage_iface_init (McpAccountStorageIface *iface)
488 mcp_account_storage_iface_set_name (iface, PLUGIN_NAME);
489 mcp_account_storage_iface_set_desc (iface, PLUGIN_DESCRIPTION);
490 mcp_account_storage_iface_set_priority (iface, PLUGIN_PRIORITY);
491 mcp_account_storage_iface_set_provider (iface, PLUGIN_PROVIDER);
493 #define IMPLEMENT(x) mcp_account_storage_iface_implement_##x(iface, \
494 mcp_account_manager_goa_##x)
501 IMPLEMENT (get_restrictions);
502 IMPLEMENT (get_identifier);