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