]> git.0d.be Git - empathy.git/blob - goa-mc-plugin/mcp-account-manager-goa.c
Track accounts created and destroyed from GOA
[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; /* string -> GoaAccount */
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     }
140   else
141     {
142       /* unknown account type */
143       g_hash_table_destroy (params);
144       return NULL;
145     }
146
147   /* generic properties */
148   PARAM ("DisplayName", goa_account_get_presentation_identity (account));
149   PARAM ("ConnectAutomatically", "true");
150
151 #undef PARAM
152
153   return params;
154 }
155
156
157 static char *
158 get_tp_account_name (GoaAccount *account)
159 {
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);
163   char *name;
164
165   if (params == NULL)
166     return NULL;
167
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"),
171       type, id);
172
173   g_hash_table_destroy (params);
174
175   return name;
176 }
177
178
179 static void
180 _new_account (McpAccountManagerGoa *self,
181     GoaAccount *account)
182 {
183   char *account_name = get_tp_account_name (account);
184
185   if (account_name == NULL)
186     return;
187
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));
191
192   if (self->priv->ready)
193     g_signal_emit_by_name (self, "created", account_name);
194 }
195
196 DECLARE_GASYNC_CALLBACK (_goa_client_new_cb);
197
198 static void
199 mcp_account_manager_goa_init (McpAccountManagerGoa *self)
200 {
201   DEBUG ("GOA MC plugin initialised");
202
203   self->priv = G_TYPE_INSTANCE_GET_PRIVATE (self,
204       MCP_TYPE_ACCOUNT_MANAGER_GOA, McpAccountManagerGoaPrivate);
205
206   self->priv->accounts = g_hash_table_new_full (g_str_hash, g_str_equal,
207       g_free, g_object_unref);
208
209   goa_client_new (NULL, _goa_client_new_cb, self);
210 }
211
212 static void
213 _account_added_cb (GoaClient *client,
214     GoaObject *object,
215     McpAccountManagerGoa *self)
216 {
217       GoaAccount *account = goa_object_peek_account (object);
218
219       _new_account (self, account);
220 }
221
222 static void
223 _account_removed_cb (GoaClient *client,
224     GoaObject *object,
225     McpAccountManagerGoa *self)
226 {
227   GoaAccount *account = goa_object_peek_account (object);
228   char *name = get_tp_account_name (account);
229
230   if (self->priv->ready)
231     g_signal_emit_by_name (self, "deleted", name);
232
233   g_hash_table_remove (self->priv->accounts, name);
234
235   g_free (name);
236 }
237
238
239 static void
240 _goa_client_new_cb (GObject *obj,
241     GAsyncResult *result,
242     gpointer user_data)
243 {
244   McpAccountManagerGoa *self = user_data;
245   GoaClient *client;
246   GList *accounts, *ptr;
247   GError *error = NULL;
248
249   self->priv->client = goa_client_new_finish (result, &error);
250
251   if (error != NULL)
252     {
253       DEBUG ("Failed to connect to GOA");
254       return;
255     }
256
257   accounts = goa_client_get_accounts (self->priv->client);
258
259   for (ptr = accounts; ptr != NULL; ptr = ptr->next)
260     {
261       GoaAccount *account = goa_object_peek_account (ptr->data);
262
263       _new_account (self, account);
264     }
265
266   g_list_free_full (accounts, g_object_unref);
267
268   g_signal_connect (self->priv->client, "account-added",
269       G_CALLBACK (_account_added_cb), self);
270   g_signal_connect (self->priv->client, "account-removed",
271       G_CALLBACK (_account_removed_cb), self);
272   /* FIXME: do we need to support account-changed ? */
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 {
300   // McpAccountManagerGoaPrivate *priv = GET_PRIVATE (self);
301   // GError *error = NULL;
302   // gboolean enabled;
303   // const char *value;
304
305   // enabled = gconf_client_get_bool (priv->gconf,
306   //     BISHO_FB_GCONF_ENABLE_CHAT_KEY, &error);
307   // if (error != NULL)
308   //   {
309   //     g_warning ("Unabled to get value for %s/Enabled from GConf: %s",
310   //         acct, error->message);
311   //     g_clear_error (&error);
312   //   }
313
314   // value = enabled ? "true" : "false";
315
316   // DEBUG ("Facebook Chat enabled = %s", value);
317
318   mcp_account_manager_set_value (am, acct, "Enabled", "true");
319 }
320
321
322 static gboolean
323 mcp_account_manager_goa_get (const McpAccountStorage *self,
324     const McpAccountManager *am,
325     const gchar *acct,
326     const gchar *key)
327 {
328   McpAccountManagerGoaPrivate *priv = GET_PRIVATE (self);
329   GoaAccount *account;
330
331   DEBUG ("%s: %s, %s", G_STRFUNC, acct, key);
332
333   account = g_hash_table_lookup (priv->accounts, acct);
334
335   if (account == NULL)
336     return FALSE;
337
338   if (key == NULL)
339     {
340       /* load all keys */
341       GHashTable *params = get_tp_parameters (account);
342       GHashTableIter iter;
343       gpointer key, value;
344
345       g_hash_table_iter_init (&iter, params);
346       while (g_hash_table_iter_next (&iter, &key, &value))
347         mcp_account_manager_set_value (am, acct, key, value);
348
349       g_hash_table_destroy (params);
350
351       get_enabled (self, am, acct);
352     }
353   else if (!tp_strdiff (key, "Enabled"))
354     {
355       get_enabled (self, am, acct);
356     }
357   else
358     {
359       /* get a specific key */
360       GHashTable *params = get_tp_parameters (account);
361
362       mcp_account_manager_set_value (am, acct, key,
363           g_hash_table_lookup (params, key));
364
365       g_hash_table_destroy (params);
366     }
367
368   return TRUE;
369 }
370
371
372 static gboolean
373 mcp_account_manager_goa_set (const McpAccountStorage *self,
374     const McpAccountManager *am,
375     const gchar *acct,
376     const gchar *key,
377     const gchar *val)
378 {
379   GError *error = NULL;
380
381   DEBUG ("%s: (%s, %s, %s)", G_STRFUNC, acct, key, val);
382
383   // if (!tp_strdiff (acct, FACEBOOK_ACCOUNT_NAME))
384   //   {
385   //     if (!tp_strdiff (key, "Enabled"))
386   //       {
387   //         gboolean enabled = !tp_strdiff (val, "true");
388
389   //         DEBUG ("set Enabled to %s", enabled ? "enabled" : "disabled");
390
391   //         gconf_client_set_bool (priv->gconf, BISHO_FB_GCONF_ENABLE_CHAT_KEY,
392   //             enabled, &error);
393   //         if (error != NULL)
394   //           {
395   //             g_warning ("Unable to save %s/Enable state in GConf: %s",
396   //                 acct, error->message);
397   //             g_clear_error (&error);
398   //           }
399
400   //         return TRUE;
401   //       }
402   //     else
403   //       /* pretend we saved everything else */
404   //       return TRUE;
405   //   }
406   // else
407   //   {
408   //     return FALSE;
409   //   }
410 }
411
412
413 static gboolean
414 mcp_account_manager_goa_delete (const McpAccountStorage *self,
415     const McpAccountManager *am,
416     const gchar *acct,
417     const gchar *key)
418 {
419   DEBUG ("%s: (%s, %s)", G_STRFUNC, acct, key);
420
421 //  if (!tp_strdiff (acct, FACEBOOK_ACCOUNT_NAME))
422 //    {
423 //      if (!tp_strdiff (key, "Enabled"))
424 //        return TRUE;
425 //      else
426 //        /* pretend we deleted everything else */
427 //        return TRUE;
428 //    }
429 //  else
430 //    {
431 //      return FALSE;
432 //    }
433 }
434
435
436 static gboolean
437 mcp_account_manager_goa_commit (const McpAccountStorage *self,
438     const McpAccountManager *am)
439 {
440   DEBUG ("%s", G_STRFUNC);
441
442   return TRUE;
443 }
444
445
446 static void
447 mcp_account_manager_goa_ready (const McpAccountStorage *self,
448     const McpAccountManager *am)
449 {
450   McpAccountManagerGoaPrivate *priv = GET_PRIVATE (self);
451
452   priv->ready = TRUE;
453 }
454
455
456 static guint
457 mcp_account_manager_goa_get_restrictions (const McpAccountStorage *self,
458     const gchar *account)
459 {
460   return TP_STORAGE_RESTRICTION_FLAG_CANNOT_SET_PARAMETERS |
461          TP_STORAGE_RESTRICTION_FLAG_CANNOT_SET_SERVICE;
462 }
463
464
465 static void
466 mcp_account_manager_goa_get_identifier (const McpAccountStorage *self,
467     const gchar *acct,
468     GValue *identifier)
469 {
470   McpAccountManagerGoaPrivate *priv = GET_PRIVATE (self);
471   GoaAccount *account;
472
473   account = g_hash_table_lookup (priv->accounts, acct);
474
475   g_return_if_fail (account != NULL);
476
477   g_value_init (identifier, G_TYPE_STRING);
478   g_value_set_string (identifier, goa_account_get_id (account));
479 }
480
481
482 static void
483 account_storage_iface_init (McpAccountStorageIface *iface)
484 {
485   mcp_account_storage_iface_set_name (iface, PLUGIN_NAME);
486   mcp_account_storage_iface_set_desc (iface, PLUGIN_DESCRIPTION);
487   mcp_account_storage_iface_set_priority (iface, PLUGIN_PRIORITY);
488   mcp_account_storage_iface_set_provider (iface, PLUGIN_PROVIDER);
489
490 #define IMPLEMENT(x) mcp_account_storage_iface_implement_##x(iface, \
491     mcp_account_manager_goa_##x)
492   IMPLEMENT (get);
493   IMPLEMENT (list);
494   IMPLEMENT (set);
495   IMPLEMENT (delete);
496   IMPLEMENT (commit);
497   IMPLEMENT (ready);
498   IMPLEMENT (get_restrictions);
499   IMPLEMENT (get_identifier);
500 #undef IMPLEMENT
501 }