]> git.0d.be Git - empathy.git/blob - goa-mc-plugin/mcp-account-manager-goa.c
set TP_STORAGE_RESTRICTION_FLAG_CANNOT_SET_ENABLED restriction
[empathy.git] / goa-mc-plugin / mcp-account-manager-goa.c
1 /*
2  * mcp-account-manager-goa.c
3  *
4  * McpAccountManagerGoa - a Mission Control plugin to expose GNOME Online
5  * Accounts with chat capabilities (e.g. Facebook) to Mission Control
6  *
7  * Copyright (C) 2010-2011 Collabora Ltd.
8  *
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.
13  *
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.
18  *
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/>.
21  *
22  * Authors:
23  *    Danielle Madeley <danielle.madeley@collabora.co.uk>
24  */
25
26 #include <glib/gi18n.h>
27
28 #include <telepathy-glib/util.h>
29
30 #define GOA_API_IS_SUBJECT_TO_CHANGE /* awesome! */
31 #include <goa/goa.h>
32
33 #include "mcp-account-manager-goa.h"
34
35 #define DEBUG g_debug
36 #define GET_PRIVATE(self) (((McpAccountManagerGoa *) self)->priv)
37 #define DECLARE_GASYNC_CALLBACK(name) \
38   static void name (GObject *, GAsyncResult *, gpointer);
39
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"
44
45 static void account_storage_iface_init (McpAccountStorageIface *iface);
46
47 G_DEFINE_TYPE_WITH_CODE (McpAccountManagerGoa,
48     mcp_account_manager_goa,
49     G_TYPE_OBJECT,
50     G_IMPLEMENT_INTERFACE (MCP_TYPE_ACCOUNT_STORAGE,
51       account_storage_iface_init))
52
53 struct _McpAccountManagerGoaPrivate
54 {
55   gboolean ready;
56
57   GoaClient *client;
58   GHashTable *accounts; /* alloc'ed string -> ref'ed GoaObject */
59 };
60
61
62 static void
63 mcp_account_manager_goa_dispose (GObject *self)
64 {
65   McpAccountManagerGoaPrivate *priv = GET_PRIVATE (self);
66
67   tp_clear_object (&priv->client);
68
69   G_OBJECT_CLASS (mcp_account_manager_goa_parent_class)->dispose (self);
70 }
71
72
73 static void
74 mcp_account_manager_goa_finalize (GObject *self)
75 {
76   McpAccountManagerGoaPrivate *priv = GET_PRIVATE (self);
77
78   g_hash_table_destroy (priv->accounts);
79
80   G_OBJECT_CLASS (mcp_account_manager_goa_parent_class)->finalize (self);
81 }
82
83
84 static void
85 mcp_account_manager_goa_class_init (McpAccountManagerGoaClass *klass)
86 {
87   GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
88
89   gobject_class->dispose = mcp_account_manager_goa_dispose;
90   gobject_class->finalize = mcp_account_manager_goa_finalize;
91
92   g_type_class_add_private (gobject_class,
93       sizeof (McpAccountManagerGoaPrivate));
94 }
95
96 static GHashTable *
97 get_tp_parameters (GoaAccount *account)
98 {
99   GHashTable *params = g_hash_table_new_full (g_str_hash, g_str_equal,
100       NULL, g_free);
101   const char *type = goa_account_get_provider_type (account);
102
103 #define PARAM(key, value) g_hash_table_insert (params, key, g_strdup (value));
104
105   if (!tp_strdiff (type, "google"))
106     {
107       PARAM ("manager", "gabble");
108       PARAM ("protocol", "jabber");
109       PARAM ("Icon", "im-google-talk");
110       PARAM ("Service", "google-talk");
111
112       PARAM ("param-account", goa_account_get_identity (account));
113       PARAM ("param-server", "talk.google.com");
114       PARAM ("param-fallback-servers",
115           "talkx.l.google.com;"
116           "talkx.l.google.com:443,oldssl;"
117           "talkx.l.google.com:80");
118       PARAM ("param-extra-certificate-identities", "talk.google.com");
119       PARAM ("param-require-encryption", "true");
120     }
121   else
122     {
123       /* unknown account type */
124       g_hash_table_destroy (params);
125       return NULL;
126     }
127
128   /* TODO: add Facebook support */
129
130   /* generic properties */
131   PARAM ("DisplayName", goa_account_get_presentation_identity (account));
132   PARAM ("ConnectAutomatically", "true");
133
134 #undef PARAM
135
136   return params;
137 }
138
139
140 static char *
141 get_tp_account_name (GoaAccount *account)
142 {
143   GHashTable *params = get_tp_parameters (account);
144   const char *type = goa_account_get_provider_type (account);
145   const char *id = goa_account_get_id (account);
146   char *name;
147
148   if (params == NULL)
149     return NULL;
150
151   name = g_strdup_printf ("%s/%s/goa_%s_%s",
152       (char *) g_hash_table_lookup (params, "manager"),
153       (char *) g_hash_table_lookup (params, "protocol"),
154       type, id);
155
156   g_hash_table_destroy (params);
157
158   return name;
159 }
160
161
162 static void
163 _new_account (McpAccountManagerGoa *self,
164     GoaObject *object)
165 {
166   GoaAccount *account = goa_object_peek_account (object);
167   char *account_name = get_tp_account_name (account);
168
169   if (account_name == NULL)
170     return;
171
172   /* @account_name now is owned by the hash table */
173   g_hash_table_insert (self->priv->accounts, account_name,
174       g_object_ref (object));
175
176   if (self->priv->ready)
177     g_signal_emit_by_name (self, "created", account_name);
178 }
179
180
181 DECLARE_GASYNC_CALLBACK (_goa_client_new_cb);
182
183 static void
184 mcp_account_manager_goa_init (McpAccountManagerGoa *self)
185 {
186   DEBUG ("GOA MC plugin initialised");
187
188   self->priv = G_TYPE_INSTANCE_GET_PRIVATE (self,
189       MCP_TYPE_ACCOUNT_MANAGER_GOA, McpAccountManagerGoaPrivate);
190
191   self->priv->accounts = g_hash_table_new_full (g_str_hash, g_str_equal,
192       g_free, g_object_unref);
193
194   goa_client_new (NULL, _goa_client_new_cb, self);
195 }
196
197
198 static void
199 _account_added_cb (GoaClient *client,
200     GoaObject *object,
201     McpAccountManagerGoa *self)
202 {
203   _new_account (self, object);
204 }
205
206
207 static void
208 _account_removed_cb (GoaClient *client,
209     GoaObject *object,
210     McpAccountManagerGoa *self)
211 {
212   GoaAccount *account = goa_object_peek_account (object);
213   char *name = get_tp_account_name (account);
214
215   if (self->priv->ready)
216     g_signal_emit_by_name (self, "deleted", name);
217
218   g_hash_table_remove (self->priv->accounts, name);
219
220   g_free (name);
221 }
222
223 static void
224 _account_changed_cb (GoaClient *client,
225     GoaObject *object,
226     McpAccountManagerGoa *self)
227 {
228   GoaAccount *account = goa_object_peek_account (object);
229   char *name = get_tp_account_name (account);
230   gboolean enabled;
231
232   enabled = (goa_object_peek_chat (object) != NULL);
233
234   DEBUG ("%s %s", name, enabled ? "enabled" : "disabled");
235
236   if (self->priv->ready)
237     g_signal_emit_by_name (self, "toggled", name, enabled);
238 }
239
240 static void
241 _goa_client_new_cb (GObject *obj,
242     GAsyncResult *result,
243     gpointer user_data)
244 {
245   McpAccountManagerGoa *self = user_data;
246   GoaClient *client;
247   GList *accounts, *ptr;
248   GError *error = NULL;
249
250   self->priv->client = goa_client_new_finish (result, &error);
251
252   if (error != NULL)
253     {
254       DEBUG ("Failed to connect to GOA");
255       return;
256     }
257
258   accounts = goa_client_get_accounts (self->priv->client);
259
260   for (ptr = accounts; ptr != NULL; ptr = ptr->next)
261     {
262       _new_account (self, ptr->data);
263     }
264
265   g_list_free_full (accounts, g_object_unref);
266
267   g_signal_connect (self->priv->client, "account-added",
268       G_CALLBACK (_account_added_cb), self);
269   g_signal_connect (self->priv->client, "account-removed",
270       G_CALLBACK (_account_removed_cb), self);
271   g_signal_connect (self->priv->client, "account-changed",
272       G_CALLBACK (_account_changed_cb), self);
273 }
274
275
276 static GList *
277 mcp_account_manager_goa_list (const McpAccountStorage *self,
278     const McpAccountManager *am)
279 {
280   McpAccountManagerGoaPrivate *priv = GET_PRIVATE (self);
281   GList *accounts = NULL;
282   GHashTableIter iter;
283   gpointer key;
284
285   DEBUG (G_STRFUNC);
286
287   g_hash_table_iter_init (&iter, priv->accounts);
288   while (g_hash_table_iter_next (&iter, &key, NULL))
289     accounts = g_list_prepend (accounts, g_strdup (key));
290
291   return accounts;
292 }
293
294
295 static void
296 get_enabled (const McpAccountStorage *self,
297     const McpAccountManager *am,
298     const gchar *acct,
299     GoaObject *object)
300 {
301   mcp_account_manager_set_value (am, acct, "Enabled",
302       goa_object_peek_chat (object) != NULL ? "true" : "false");
303 }
304
305
306 static gboolean
307 mcp_account_manager_goa_get (const McpAccountStorage *self,
308     const McpAccountManager *am,
309     const gchar *acct,
310     const gchar *key)
311 {
312   McpAccountManagerGoaPrivate *priv = GET_PRIVATE (self);
313   GoaObject *object;
314   GoaAccount *account;
315
316   DEBUG ("%s: %s, %s", G_STRFUNC, acct, key);
317
318   object = g_hash_table_lookup (priv->accounts, acct);
319
320   if (object == NULL)
321     return FALSE;
322
323   account = goa_object_peek_account (object);
324
325   if (account == NULL)
326     return FALSE;
327
328   if (key == NULL)
329     {
330       /* load all keys */
331       GHashTable *params = get_tp_parameters (account);
332       GHashTableIter iter;
333       gpointer key, value;
334
335       g_hash_table_iter_init (&iter, params);
336       while (g_hash_table_iter_next (&iter, &key, &value))
337         mcp_account_manager_set_value (am, acct, key, value);
338
339       g_hash_table_destroy (params);
340
341       get_enabled (self, am, acct, object);
342     }
343   else if (!tp_strdiff (key, "Enabled"))
344     {
345       get_enabled (self, am, acct, object);
346     }
347   else
348     {
349       /* get a specific key */
350       GHashTable *params = get_tp_parameters (account);
351
352       mcp_account_manager_set_value (am, acct, key,
353           g_hash_table_lookup (params, key));
354
355       g_hash_table_destroy (params);
356     }
357
358   return TRUE;
359 }
360
361
362 static gboolean
363 mcp_account_manager_goa_set (const McpAccountStorage *self,
364     const McpAccountManager *am,
365     const gchar *acct,
366     const gchar *key,
367     const gchar *val)
368 {
369   GError *error = NULL;
370
371   DEBUG ("%s: (%s, %s, %s)", G_STRFUNC, acct, key, val);
372
373   /* Pretend we save everything so MC won't save this in accounts.cfg */
374   return TRUE;
375 }
376
377
378 static gboolean
379 mcp_account_manager_goa_delete (const McpAccountStorage *self,
380     const McpAccountManager *am,
381     const gchar *acct,
382     const gchar *key)
383 {
384   DEBUG ("%s: (%s, %s)", G_STRFUNC, acct, key);
385
386   /* Pretend we deleted everything */
387   return TRUE;
388 }
389
390
391 static gboolean
392 mcp_account_manager_goa_commit (const McpAccountStorage *self,
393     const McpAccountManager *am)
394 {
395   DEBUG ("%s", G_STRFUNC);
396
397   return TRUE;
398 }
399
400
401 static void
402 mcp_account_manager_goa_ready (const McpAccountStorage *self,
403     const McpAccountManager *am)
404 {
405   McpAccountManagerGoaPrivate *priv = GET_PRIVATE (self);
406
407   priv->ready = TRUE;
408 }
409
410
411 static guint
412 mcp_account_manager_goa_get_restrictions (const McpAccountStorage *self,
413     const gchar *account)
414 {
415   return TP_STORAGE_RESTRICTION_FLAG_CANNOT_SET_PARAMETERS |
416          TP_STORAGE_RESTRICTION_FLAG_CANNOT_SET_SERVICE |
417          TP_STORAGE_RESTRICTION_FLAG_CANNOT_SET_ENABLED;
418 }
419
420
421 static void
422 mcp_account_manager_goa_get_identifier (const McpAccountStorage *self,
423     const gchar *acct,
424     GValue *identifier)
425 {
426   McpAccountManagerGoaPrivate *priv = GET_PRIVATE (self);
427   GoaObject *object;
428   GoaAccount *account;
429
430   object = g_hash_table_lookup (priv->accounts, acct);
431   g_return_if_fail (object != NULL);
432
433   account = goa_object_peek_account (object);
434   g_return_if_fail (account != NULL);
435
436   g_value_init (identifier, G_TYPE_STRING);
437   g_value_set_string (identifier, goa_account_get_id (account));
438 }
439
440
441 static void
442 account_storage_iface_init (McpAccountStorageIface *iface)
443 {
444   mcp_account_storage_iface_set_name (iface, PLUGIN_NAME);
445   mcp_account_storage_iface_set_desc (iface, PLUGIN_DESCRIPTION);
446   mcp_account_storage_iface_set_priority (iface, PLUGIN_PRIORITY);
447   mcp_account_storage_iface_set_provider (iface, PLUGIN_PROVIDER);
448
449 #define IMPLEMENT(x) mcp_account_storage_iface_implement_##x(iface, \
450     mcp_account_manager_goa_##x)
451   IMPLEMENT (get);
452   IMPLEMENT (list);
453   IMPLEMENT (set);
454   IMPLEMENT (delete);
455   IMPLEMENT (commit);
456   IMPLEMENT (ready);
457   IMPLEMENT (get_restrictions);
458   IMPLEMENT (get_identifier);
459 #undef IMPLEMENT
460 }