]> git.0d.be Git - empathy.git/blob - goa-mc-plugin/mcp-account-manager-goa.c
goa-mc-plugin: do not set the ConnectAutomatically Account property
[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 #define INITIAL_COMMENT "Parameters of GOA Telepathy accounts"
46
47 static void account_storage_iface_init (McpAccountStorageIface *iface);
48
49 G_DEFINE_TYPE_WITH_CODE (McpAccountManagerGoa,
50     mcp_account_manager_goa,
51     G_TYPE_OBJECT,
52     G_IMPLEMENT_INTERFACE (MCP_TYPE_ACCOUNT_STORAGE,
53       account_storage_iface_init))
54
55 struct _McpAccountManagerGoaPrivate
56 {
57   gboolean ready;
58
59   GoaClient *client;
60   GHashTable *accounts; /* alloc'ed string -> ref'ed GoaObject */
61
62   GKeyFile *store;
63   gchar *filename;
64 };
65
66
67 static void
68 mcp_account_manager_goa_dispose (GObject *self)
69 {
70   McpAccountManagerGoaPrivate *priv = GET_PRIVATE (self);
71
72   tp_clear_object (&priv->client);
73
74   G_OBJECT_CLASS (mcp_account_manager_goa_parent_class)->dispose (self);
75 }
76
77
78 static void
79 mcp_account_manager_goa_finalize (GObject *self)
80 {
81   McpAccountManagerGoaPrivate *priv = GET_PRIVATE (self);
82
83   g_hash_table_destroy (priv->accounts);
84   g_key_file_free (priv->store);
85   g_free (priv->filename);
86
87   G_OBJECT_CLASS (mcp_account_manager_goa_parent_class)->finalize (self);
88 }
89
90
91 static void
92 mcp_account_manager_goa_class_init (McpAccountManagerGoaClass *klass)
93 {
94   GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
95
96   gobject_class->dispose = mcp_account_manager_goa_dispose;
97   gobject_class->finalize = mcp_account_manager_goa_finalize;
98
99   g_type_class_add_private (gobject_class,
100       sizeof (McpAccountManagerGoaPrivate));
101 }
102
103 static GHashTable *
104 get_tp_parameters (GoaAccount *account)
105 {
106   GHashTable *params = g_hash_table_new_full (g_str_hash, g_str_equal,
107       NULL, g_free);
108   const char *type = goa_account_get_provider_type (account);
109
110 #define PARAM(key, value) g_hash_table_insert (params, key, g_strdup (value));
111
112   if (!tp_strdiff (type, "google"))
113     {
114       PARAM ("manager", "gabble");
115       PARAM ("protocol", "jabber");
116       PARAM ("Icon", "im-google-talk");
117       PARAM ("Service", "google-talk");
118
119       PARAM ("param-account", goa_account_get_identity (account));
120       PARAM ("param-server", "talk.google.com");
121       PARAM ("param-fallback-servers",
122           "talkx.l.google.com;"
123           "talkx.l.google.com:443,oldssl;"
124           "talkx.l.google.com:80");
125       PARAM ("param-extra-certificate-identities", "talk.google.com");
126       PARAM ("param-require-encryption", "true");
127     }
128   else
129     {
130       /* unknown account type */
131       g_hash_table_destroy (params);
132       return NULL;
133     }
134
135   /* TODO: add Facebook support */
136
137   /* generic properties */
138   PARAM ("DisplayName", goa_account_get_presentation_identity (account));
139
140 #undef PARAM
141
142   return params;
143 }
144
145
146 static char *
147 get_tp_account_name (GoaAccount *account)
148 {
149   GHashTable *params = get_tp_parameters (account);
150   const char *type = goa_account_get_provider_type (account);
151   const char *id = goa_account_get_id (account);
152   char *name;
153
154   if (params == NULL)
155     return NULL;
156
157   name = g_strdup_printf ("%s/%s/goa_%s_%s",
158       (char *) g_hash_table_lookup (params, "manager"),
159       (char *) g_hash_table_lookup (params, "protocol"),
160       type, id);
161
162   g_hash_table_destroy (params);
163
164   return name;
165 }
166
167 static void
168 object_chat_changed_cb (GoaObject *object,
169     GParamSpec *spec,
170     McpAccountManagerGoa *self)
171 {
172   GoaAccount *account = goa_object_peek_account (object);
173   char *name = get_tp_account_name (account);
174   gboolean enabled;
175
176   enabled = (goa_object_peek_chat (object) != NULL);
177
178   DEBUG ("%s %s", name, enabled ? "enabled" : "disabled");
179
180   if (self->priv->ready)
181     g_signal_emit_by_name (self, "toggled", name, enabled);
182 }
183
184 static void
185 _new_account (McpAccountManagerGoa *self,
186     GoaObject *object)
187 {
188   GoaAccount *account = goa_object_peek_account (object);
189   char *account_name = get_tp_account_name (account);
190
191   if (account_name == NULL)
192     return;
193
194   /* @account_name now is owned by the hash table */
195   g_hash_table_insert (self->priv->accounts, account_name,
196       g_object_ref (object));
197
198   if (self->priv->ready)
199     g_signal_emit_by_name (self, "created", account_name);
200
201   tp_g_signal_connect_object (object, "notify::chat",
202       G_CALLBACK (object_chat_changed_cb), self, 0);
203 }
204
205
206 DECLARE_GASYNC_CALLBACK (_goa_client_new_cb);
207
208 static void
209 load_store (McpAccountManagerGoa *self)
210 {
211   GError *error = NULL;
212
213   if (!g_key_file_load_from_file (self->priv->store, self->priv->filename,
214         G_KEY_FILE_KEEP_COMMENTS, &error))
215     {
216       gchar *dir;
217
218       DEBUG ("Failed to load keyfile, creating a new one: %s", error->message);
219
220       dir = g_path_get_dirname (self->priv->filename);
221
222       g_mkdir_with_parents (dir, 0700);
223       g_free (dir);
224
225       g_key_file_set_comment (self->priv->store, NULL, NULL, INITIAL_COMMENT,
226           NULL);
227
228       g_error_free (error);
229     }
230 }
231
232 static void
233 mcp_account_manager_goa_init (McpAccountManagerGoa *self)
234 {
235   gchar *path;
236
237   DEBUG ("GOA MC plugin initialised");
238
239   self->priv = G_TYPE_INSTANCE_GET_PRIVATE (self,
240       MCP_TYPE_ACCOUNT_MANAGER_GOA, McpAccountManagerGoaPrivate);
241
242   self->priv->accounts = g_hash_table_new_full (g_str_hash, g_str_equal,
243       g_free, g_object_unref);
244
245   goa_client_new (NULL, _goa_client_new_cb, self);
246
247   /* key file store */
248   self->priv->store = g_key_file_new ();
249   self->priv->filename = g_build_filename (g_get_user_data_dir (), "telepathy",
250       "mission-control", "accounts-goa.cfg", NULL);
251
252   load_store (self);
253 }
254
255
256 static void
257 _account_added_cb (GoaClient *client,
258     GoaObject *object,
259     McpAccountManagerGoa *self)
260 {
261   _new_account (self, object);
262 }
263
264
265 static void
266 _account_removed_cb (GoaClient *client,
267     GoaObject *object,
268     McpAccountManagerGoa *self)
269 {
270   GoaAccount *account = goa_object_peek_account (object);
271   char *name = get_tp_account_name (account);
272
273   if (self->priv->ready)
274     g_signal_emit_by_name (self, "deleted", name);
275
276   g_hash_table_remove (self->priv->accounts, name);
277
278   g_free (name);
279 }
280
281 static void
282 _goa_client_new_cb (GObject *obj,
283     GAsyncResult *result,
284     gpointer user_data)
285 {
286   McpAccountManagerGoa *self = user_data;
287   GoaClient *client;
288   GList *accounts, *ptr;
289   GError *error = NULL;
290
291   self->priv->client = goa_client_new_finish (result, &error);
292
293   if (error != NULL)
294     {
295       DEBUG ("Failed to connect to GOA");
296       return;
297     }
298
299   accounts = goa_client_get_accounts (self->priv->client);
300
301   for (ptr = accounts; ptr != NULL; ptr = ptr->next)
302     {
303       _new_account (self, ptr->data);
304     }
305
306   g_list_free_full (accounts, g_object_unref);
307
308   g_signal_connect (self->priv->client, "account-added",
309       G_CALLBACK (_account_added_cb), self);
310   g_signal_connect (self->priv->client, "account-removed",
311       G_CALLBACK (_account_removed_cb), self);
312 }
313
314
315 static GList *
316 mcp_account_manager_goa_list (const McpAccountStorage *self,
317     const McpAccountManager *am)
318 {
319   McpAccountManagerGoaPrivate *priv = GET_PRIVATE (self);
320   GList *accounts = NULL;
321   GHashTableIter iter;
322   gpointer key;
323
324   DEBUG (G_STRFUNC);
325
326   g_hash_table_iter_init (&iter, priv->accounts);
327   while (g_hash_table_iter_next (&iter, &key, NULL))
328     accounts = g_list_prepend (accounts, g_strdup (key));
329
330   return accounts;
331 }
332
333
334 static void
335 get_enabled (const McpAccountStorage *self,
336     const McpAccountManager *am,
337     const gchar *acct,
338     GoaObject *object)
339 {
340   mcp_account_manager_set_value (am, acct, "Enabled",
341       goa_object_peek_chat (object) != NULL ? "true" : "false");
342 }
343
344
345 static gboolean
346 mcp_account_manager_goa_get (const McpAccountStorage *self,
347     const McpAccountManager *am,
348     const gchar *acct,
349     const gchar *key)
350 {
351   McpAccountManagerGoaPrivate *priv = GET_PRIVATE (self);
352   GoaObject *object;
353   GoaAccount *account;
354
355   DEBUG ("%s: %s, %s", G_STRFUNC, acct, key);
356
357   object = g_hash_table_lookup (priv->accounts, acct);
358
359   if (object == NULL)
360     return FALSE;
361
362   account = goa_object_peek_account (object);
363
364   if (account == NULL)
365     return FALSE;
366
367   if (key == NULL)
368     {
369       /* load all keys */
370       GHashTable *params = get_tp_parameters (account);
371       GHashTableIter iter;
372       gpointer key, value;
373       GStrv keys;
374       guint i;
375       gssize nkeys = 0;
376
377       /* Properties from GOA */
378       g_hash_table_iter_init (&iter, params);
379       while (g_hash_table_iter_next (&iter, &key, &value))
380         mcp_account_manager_set_value (am, acct, key, value);
381
382       g_hash_table_destroy (params);
383
384       /* Stored properties */
385       keys = g_key_file_get_keys (priv->store, acct, &nkeys, NULL);
386
387       for (i = 0; i < nkeys; i++)
388         {
389           gchar *v = g_key_file_get_value (priv->store, acct, keys[i], NULL);
390
391           if (v != NULL)
392             {
393               mcp_account_manager_set_value (am, acct, keys[i], v);
394               g_free (v);
395             }
396         }
397
398       g_strfreev (keys);
399
400       /* Enabled */
401       get_enabled (self, am, acct, object);
402     }
403   else if (!tp_strdiff (key, "Enabled"))
404     {
405       get_enabled (self, am, acct, object);
406     }
407   else
408     {
409       /* get a specific key */
410       GHashTable *params = get_tp_parameters (account);
411       gchar *value;
412
413       value = g_hash_table_lookup (params, key);
414
415       if (value == NULL)
416         value = g_key_file_get_value (priv->store, acct, key, NULL);
417       else
418         value = g_strdup (value);
419
420       mcp_account_manager_set_value (am, acct, key, value);
421
422       g_hash_table_destroy (params);
423       g_free (value);
424     }
425
426   return TRUE;
427 }
428
429 static gboolean
430 account_is_in_goa (const McpAccountStorage *self,
431     const gchar *account)
432 {
433   McpAccountManagerGoaPrivate *priv = GET_PRIVATE (self);
434
435   return (g_hash_table_lookup (priv->accounts, acct) != NULL);
436 }
437
438 static gboolean
439 mcp_account_manager_goa_set (const McpAccountStorage *self,
440     const McpAccountManager *am,
441     const gchar *account,
442     const gchar *key,
443     const gchar *val)
444 {
445   McpAccountManagerGoaPrivate *priv = GET_PRIVATE (self);
446   GError *error = NULL;
447
448   if (!account_is_in_goa (self, account))
449     return FALSE;
450
451   /* No need to save Enabled, it's up to the GOA configuration if the account
452    * is configured or not. */
453   if (!tp_strdiff (key, "Enabled"))
454     return TRUE;
455
456   DEBUG ("%s: (%s, %s, %s)", G_STRFUNC, account, key, val);
457
458   if (val != NULL)
459     g_key_file_set_value (priv->store, account, key, val);
460   else
461     g_key_file_remove_key (priv->store, account, key, NULL);
462
463   /* Pretend we save everything so MC won't save this in accounts.cfg */
464   return TRUE;
465 }
466
467
468 static gboolean
469 mcp_account_manager_goa_delete (const McpAccountStorage *self,
470     const McpAccountManager *am,
471     const gchar *account,
472     const gchar *key)
473 {
474   McpAccountManagerGoaPrivate *priv = GET_PRIVATE (self);
475
476   if (!account_is_in_goa (self, account))
477     return FALSE;
478
479   DEBUG ("%s: (%s, %s)", G_STRFUNC, account, key);
480
481   if (key == NULL)
482     {
483       g_key_file_remove_group (priv->store, account, NULL);
484     }
485   else
486     {
487       g_key_file_remove_key (priv->store, account, key, NULL);
488     }
489
490   /* Pretend we deleted everything */
491   return TRUE;
492 }
493
494
495 static gboolean
496 mcp_account_manager_goa_commit (const McpAccountStorage *self,
497     const McpAccountManager *am)
498 {
499   McpAccountManagerGoaPrivate *priv = GET_PRIVATE (self);
500   gchar *data;
501   gsize len;
502   GError *error = NULL;
503
504   DEBUG ("Save config to %s", priv->filename);
505
506   data = g_key_file_to_data (priv->store, &len, &error);
507   if (data == NULL)
508     {
509       DEBUG ("Failed to get data from store: %s", error->message);
510
511       g_error_free (error);
512       return FALSE;
513     }
514
515   if (!g_file_set_contents (priv->filename, data, len, &error))
516     {
517       DEBUG ("Failed to write file: %s", error->message);
518
519       g_free (data);
520       g_error_free (error);
521       return FALSE;
522     }
523
524   g_free (data);
525
526   return TRUE;
527 }
528
529
530 static void
531 mcp_account_manager_goa_ready (const McpAccountStorage *self,
532     const McpAccountManager *am)
533 {
534   McpAccountManagerGoaPrivate *priv = GET_PRIVATE (self);
535
536   priv->ready = TRUE;
537 }
538
539
540 static guint
541 mcp_account_manager_goa_get_restrictions (const McpAccountStorage *self,
542     const gchar *account)
543 {
544   return TP_STORAGE_RESTRICTION_FLAG_CANNOT_SET_PARAMETERS |
545          TP_STORAGE_RESTRICTION_FLAG_CANNOT_SET_SERVICE |
546          TP_STORAGE_RESTRICTION_FLAG_CANNOT_SET_ENABLED;
547 }
548
549
550 static void
551 mcp_account_manager_goa_get_identifier (const McpAccountStorage *self,
552     const gchar *acct,
553     GValue *identifier)
554 {
555   McpAccountManagerGoaPrivate *priv = GET_PRIVATE (self);
556   GoaObject *object;
557   GoaAccount *account;
558
559   object = g_hash_table_lookup (priv->accounts, acct);
560   g_return_if_fail (object != NULL);
561
562   account = goa_object_peek_account (object);
563   g_return_if_fail (account != NULL);
564
565   g_value_init (identifier, G_TYPE_STRING);
566   g_value_set_string (identifier, goa_account_get_id (account));
567 }
568
569
570 static void
571 account_storage_iface_init (McpAccountStorageIface *iface)
572 {
573   mcp_account_storage_iface_set_name (iface, PLUGIN_NAME);
574   mcp_account_storage_iface_set_desc (iface, PLUGIN_DESCRIPTION);
575   mcp_account_storage_iface_set_priority (iface, PLUGIN_PRIORITY);
576   mcp_account_storage_iface_set_provider (iface, PLUGIN_PROVIDER);
577
578 #define IMPLEMENT(x) mcp_account_storage_iface_implement_##x(iface, \
579     mcp_account_manager_goa_##x)
580   IMPLEMENT (get);
581   IMPLEMENT (list);
582   IMPLEMENT (set);
583   IMPLEMENT (delete);
584   IMPLEMENT (commit);
585   IMPLEMENT (ready);
586   IMPLEMENT (get_restrictions);
587   IMPLEMENT (get_identifier);
588 #undef IMPLEMENT
589 }