]> git.0d.be Git - empathy.git/blob - goa-mc-plugin/mcp-account-manager-goa.c
store the GoaObject rather than the GoaAccount
[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
97 // static void
98 // _enable_chat_toggled (GConfClient *client,
99 //     guint cnxn_id,
100 //     GConfEntry *entry,
101 //     gpointer user_data)
102 // {
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));
106 // 
107 //   DEBUG ("%s: %s", G_STRFUNC, enabled ? "enabled" : "disabled");
108 // 
109 //   if (priv->ready && priv->facebook_available)
110 //     {
111 //       DEBUG ("Emitting toggled signal");
112 // 
113 //       g_signal_emit_by_name (self, "toggled", FACEBOOK_ACCOUNT_NAME, enabled);
114 //     }
115 // }
116
117
118 static GHashTable *
119 get_tp_parameters (GoaAccount *account)
120 {
121   GHashTable *params = g_hash_table_new_full (g_str_hash, g_str_equal,
122       NULL, g_free);
123   const char *type = goa_account_get_provider_type (account);
124
125 #define PARAM(key, value) g_hash_table_insert (params, key, g_strdup (value));
126
127  // { "param-account", "chat.facebook.com" },
128  // { "param-server", "chat.facebook.com" },
129
130   if (!tp_strdiff (type, "google"))
131     {
132       PARAM ("manager", "gabble");
133       PARAM ("protocol", "jabber");
134       PARAM ("Icon", "im-google-talk");
135       PARAM ("Service", "google-talk");
136
137       PARAM ("param-account", goa_account_get_identity (account));
138       PARAM ("param-server", "talk.google.com");
139       PARAM ("param-fallback-servers",
140           "talkx.l.google.com;"
141           "talkx.l.google.com:443,oldssl;"
142           "talkx.l.google.com:80");
143       PARAM ("param-extra-certificate-identities", "talk.google.com");
144       PARAM ("param-require-encryption", "true");
145     }
146   else
147     {
148       /* unknown account type */
149       g_hash_table_destroy (params);
150       return NULL;
151     }
152
153   /* generic properties */
154   PARAM ("DisplayName", goa_account_get_presentation_identity (account));
155   PARAM ("ConnectAutomatically", "true");
156
157 #undef PARAM
158
159   return params;
160 }
161
162
163 static char *
164 get_tp_account_name (GoaAccount *account)
165 {
166   GHashTable *params = get_tp_parameters (account);
167   const char *type = goa_account_get_provider_type (account);
168   const char *id = goa_account_get_id (account);
169   char *name;
170
171   if (params == NULL)
172     return NULL;
173
174   name = g_strdup_printf ("%s/%s/goa_%s_%s",
175       (char *) g_hash_table_lookup (params, "manager"),
176       (char *) g_hash_table_lookup (params, "protocol"),
177       type, id);
178
179   g_hash_table_destroy (params);
180
181   return name;
182 }
183
184
185 static void
186 _new_account (McpAccountManagerGoa *self,
187     GoaObject *object)
188 {
189   GoaAccount *account = goa_object_peek_account (object);
190   char *account_name = get_tp_account_name (account);
191
192   if (account_name == NULL)
193     return;
194
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));
198
199   if (self->priv->ready)
200     g_signal_emit_by_name (self, "created", account_name);
201 }
202
203
204 DECLARE_GASYNC_CALLBACK (_goa_client_new_cb);
205
206 static void
207 mcp_account_manager_goa_init (McpAccountManagerGoa *self)
208 {
209   DEBUG ("GOA MC plugin initialised");
210
211   self->priv = G_TYPE_INSTANCE_GET_PRIVATE (self,
212       MCP_TYPE_ACCOUNT_MANAGER_GOA, McpAccountManagerGoaPrivate);
213
214   self->priv->accounts = g_hash_table_new_full (g_str_hash, g_str_equal,
215       g_free, g_object_unref);
216
217   goa_client_new (NULL, _goa_client_new_cb, self);
218 }
219
220
221 static void
222 _account_added_cb (GoaClient *client,
223     GoaObject *object,
224     McpAccountManagerGoa *self)
225 {
226   _new_account (self, object);
227 }
228
229
230 static void
231 _account_removed_cb (GoaClient *client,
232     GoaObject *object,
233     McpAccountManagerGoa *self)
234 {
235   GoaAccount *account = goa_object_peek_account (object);
236   char *name = get_tp_account_name (account);
237
238   if (self->priv->ready)
239     g_signal_emit_by_name (self, "deleted", name);
240
241   g_hash_table_remove (self->priv->accounts, name);
242
243   g_free (name);
244 }
245
246
247 static void
248 _goa_client_new_cb (GObject *obj,
249     GAsyncResult *result,
250     gpointer user_data)
251 {
252   McpAccountManagerGoa *self = user_data;
253   GoaClient *client;
254   GList *accounts, *ptr;
255   GError *error = NULL;
256
257   self->priv->client = goa_client_new_finish (result, &error);
258
259   if (error != NULL)
260     {
261       DEBUG ("Failed to connect to GOA");
262       return;
263     }
264
265   accounts = goa_client_get_accounts (self->priv->client);
266
267   for (ptr = accounts; ptr != NULL; ptr = ptr->next)
268     {
269       _new_account (self, ptr->data);
270     }
271
272   g_list_free_full (accounts, g_object_unref);
273
274   g_signal_connect (self->priv->client, "account-added",
275       G_CALLBACK (_account_added_cb), self);
276   g_signal_connect (self->priv->client, "account-removed",
277       G_CALLBACK (_account_removed_cb), self);
278   /* FIXME: do we need to support account-changed ? */
279 }
280
281
282 static GList *
283 mcp_account_manager_goa_list (const McpAccountStorage *self,
284     const McpAccountManager *am)
285 {
286   McpAccountManagerGoaPrivate *priv = GET_PRIVATE (self);
287   GList *accounts = NULL;
288   GHashTableIter iter;
289   gpointer key;
290
291   DEBUG (G_STRFUNC);
292
293   g_hash_table_iter_init (&iter, priv->accounts);
294   while (g_hash_table_iter_next (&iter, &key, NULL))
295     accounts = g_list_prepend (accounts, g_strdup (key));
296
297   return accounts;
298 }
299
300
301 static void
302 get_enabled (const McpAccountStorage *self,
303     const McpAccountManager *am,
304     const gchar *acct)
305 {
306   // McpAccountManagerGoaPrivate *priv = GET_PRIVATE (self);
307   // GError *error = NULL;
308   // gboolean enabled;
309   // const char *value;
310
311   // enabled = gconf_client_get_bool (priv->gconf,
312   //     BISHO_FB_GCONF_ENABLE_CHAT_KEY, &error);
313   // if (error != NULL)
314   //   {
315   //     g_warning ("Unabled to get value for %s/Enabled from GConf: %s",
316   //         acct, error->message);
317   //     g_clear_error (&error);
318   //   }
319
320   // value = enabled ? "true" : "false";
321
322   // DEBUG ("Facebook Chat enabled = %s", value);
323
324   mcp_account_manager_set_value (am, acct, "Enabled", "true");
325 }
326
327
328 static gboolean
329 mcp_account_manager_goa_get (const McpAccountStorage *self,
330     const McpAccountManager *am,
331     const gchar *acct,
332     const gchar *key)
333 {
334   McpAccountManagerGoaPrivate *priv = GET_PRIVATE (self);
335   GoaObject *object;
336   GoaAccount *account;
337
338   DEBUG ("%s: %s, %s", G_STRFUNC, acct, key);
339
340   object = g_hash_table_lookup (priv->accounts, acct);
341
342   if (object == NULL)
343     return FALSE;
344
345   account = goa_object_peek_account (object);
346
347   if (account == NULL)
348     return FALSE;
349
350   if (key == NULL)
351     {
352       /* load all keys */
353       GHashTable *params = get_tp_parameters (account);
354       GHashTableIter iter;
355       gpointer key, value;
356
357       g_hash_table_iter_init (&iter, params);
358       while (g_hash_table_iter_next (&iter, &key, &value))
359         mcp_account_manager_set_value (am, acct, key, value);
360
361       g_hash_table_destroy (params);
362
363       get_enabled (self, am, acct);
364     }
365   else if (!tp_strdiff (key, "Enabled"))
366     {
367       get_enabled (self, am, acct);
368     }
369   else
370     {
371       /* get a specific key */
372       GHashTable *params = get_tp_parameters (account);
373
374       mcp_account_manager_set_value (am, acct, key,
375           g_hash_table_lookup (params, key));
376
377       g_hash_table_destroy (params);
378     }
379
380   return TRUE;
381 }
382
383
384 static gboolean
385 mcp_account_manager_goa_set (const McpAccountStorage *self,
386     const McpAccountManager *am,
387     const gchar *acct,
388     const gchar *key,
389     const gchar *val)
390 {
391   GError *error = NULL;
392
393   DEBUG ("%s: (%s, %s, %s)", G_STRFUNC, acct, key, val);
394
395   // if (!tp_strdiff (acct, FACEBOOK_ACCOUNT_NAME))
396   //   {
397   //     if (!tp_strdiff (key, "Enabled"))
398   //       {
399   //         gboolean enabled = !tp_strdiff (val, "true");
400
401   //         DEBUG ("set Enabled to %s", enabled ? "enabled" : "disabled");
402
403   //         gconf_client_set_bool (priv->gconf, BISHO_FB_GCONF_ENABLE_CHAT_KEY,
404   //             enabled, &error);
405   //         if (error != NULL)
406   //           {
407   //             g_warning ("Unable to save %s/Enable state in GConf: %s",
408   //                 acct, error->message);
409   //             g_clear_error (&error);
410   //           }
411
412   //         return TRUE;
413   //       }
414   //     else
415   //       /* pretend we saved everything else */
416   //       return TRUE;
417   //   }
418   // else
419   //   {
420   //     return FALSE;
421   //   }
422 }
423
424
425 static gboolean
426 mcp_account_manager_goa_delete (const McpAccountStorage *self,
427     const McpAccountManager *am,
428     const gchar *acct,
429     const gchar *key)
430 {
431   DEBUG ("%s: (%s, %s)", G_STRFUNC, acct, key);
432
433 //  if (!tp_strdiff (acct, FACEBOOK_ACCOUNT_NAME))
434 //    {
435 //      if (!tp_strdiff (key, "Enabled"))
436 //        return TRUE;
437 //      else
438 //        /* pretend we deleted everything else */
439 //        return TRUE;
440 //    }
441 //  else
442 //    {
443 //      return FALSE;
444 //    }
445 }
446
447
448 static gboolean
449 mcp_account_manager_goa_commit (const McpAccountStorage *self,
450     const McpAccountManager *am)
451 {
452   DEBUG ("%s", G_STRFUNC);
453
454   return TRUE;
455 }
456
457
458 static void
459 mcp_account_manager_goa_ready (const McpAccountStorage *self,
460     const McpAccountManager *am)
461 {
462   McpAccountManagerGoaPrivate *priv = GET_PRIVATE (self);
463
464   priv->ready = TRUE;
465 }
466
467
468 static guint
469 mcp_account_manager_goa_get_restrictions (const McpAccountStorage *self,
470     const gchar *account)
471 {
472   return TP_STORAGE_RESTRICTION_FLAG_CANNOT_SET_PARAMETERS |
473          TP_STORAGE_RESTRICTION_FLAG_CANNOT_SET_SERVICE;
474 }
475
476
477 static void
478 mcp_account_manager_goa_get_identifier (const McpAccountStorage *self,
479     const gchar *acct,
480     GValue *identifier)
481 {
482   McpAccountManagerGoaPrivate *priv = GET_PRIVATE (self);
483   GoaObject *object;
484   GoaAccount *account;
485
486   object = g_hash_table_lookup (priv->accounts, acct);
487   g_return_if_fail (object != NULL);
488
489   account = goa_object_peek_account (object);
490   g_return_if_fail (account != NULL);
491
492   g_value_init (identifier, G_TYPE_STRING);
493   g_value_set_string (identifier, goa_account_get_id (account));
494 }
495
496
497 static void
498 account_storage_iface_init (McpAccountStorageIface *iface)
499 {
500   mcp_account_storage_iface_set_name (iface, PLUGIN_NAME);
501   mcp_account_storage_iface_set_desc (iface, PLUGIN_DESCRIPTION);
502   mcp_account_storage_iface_set_priority (iface, PLUGIN_PRIORITY);
503   mcp_account_storage_iface_set_provider (iface, PLUGIN_PROVIDER);
504
505 #define IMPLEMENT(x) mcp_account_storage_iface_implement_##x(iface, \
506     mcp_account_manager_goa_##x)
507   IMPLEMENT (get);
508   IMPLEMENT (list);
509   IMPLEMENT (set);
510   IMPLEMENT (delete);
511   IMPLEMENT (commit);
512   IMPLEMENT (ready);
513   IMPLEMENT (get_restrictions);
514   IMPLEMENT (get_identifier);
515 #undef IMPLEMENT
516 }