]> git.0d.be Git - empathy.git/blob - goa-mc-plugin/mcp-account-manager-goa.c
GOA plugin: only save parameters from GOA accounts
[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   PARAM ("ConnectAutomatically", "true");
140
141 #undef PARAM
142
143   return params;
144 }
145
146
147 static char *
148 get_tp_account_name (GoaAccount *account)
149 {
150   GHashTable *params = get_tp_parameters (account);
151   const char *type = goa_account_get_provider_type (account);
152   const char *id = goa_account_get_id (account);
153   char *name;
154
155   if (params == NULL)
156     return NULL;
157
158   name = g_strdup_printf ("%s/%s/goa_%s_%s",
159       (char *) g_hash_table_lookup (params, "manager"),
160       (char *) g_hash_table_lookup (params, "protocol"),
161       type, id);
162
163   g_hash_table_destroy (params);
164
165   return name;
166 }
167
168 static void
169 object_chat_changed_cb (GoaObject *object,
170     GParamSpec *spec,
171     McpAccountManagerGoa *self)
172 {
173   GoaAccount *account = goa_object_peek_account (object);
174   char *name = get_tp_account_name (account);
175   gboolean enabled;
176
177   enabled = (goa_object_peek_chat (object) != NULL);
178
179   DEBUG ("%s %s", name, enabled ? "enabled" : "disabled");
180
181   if (self->priv->ready)
182     g_signal_emit_by_name (self, "toggled", name, enabled);
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   tp_g_signal_connect_object (object, "notify::chat",
203       G_CALLBACK (object_chat_changed_cb), self, 0);
204 }
205
206
207 DECLARE_GASYNC_CALLBACK (_goa_client_new_cb);
208
209 static void
210 load_store (McpAccountManagerGoa *self)
211 {
212   GError *error = NULL;
213
214   if (!g_key_file_load_from_file (self->priv->store, self->priv->filename,
215         G_KEY_FILE_KEEP_COMMENTS, &error))
216     {
217       gchar *dir;
218
219       DEBUG ("Failed to load keyfile, creating a new one: %s", error->message);
220
221       dir = g_path_get_dirname (self->priv->filename);
222
223       g_mkdir_with_parents (dir, 0700);
224       g_free (dir);
225
226       g_key_file_set_comment (self->priv->store, NULL, NULL, INITIAL_COMMENT,
227           NULL);
228
229       g_error_free (error);
230     }
231 }
232
233 static void
234 mcp_account_manager_goa_init (McpAccountManagerGoa *self)
235 {
236   gchar *path;
237
238   DEBUG ("GOA MC plugin initialised");
239
240   self->priv = G_TYPE_INSTANCE_GET_PRIVATE (self,
241       MCP_TYPE_ACCOUNT_MANAGER_GOA, McpAccountManagerGoaPrivate);
242
243   self->priv->accounts = g_hash_table_new_full (g_str_hash, g_str_equal,
244       g_free, g_object_unref);
245
246   goa_client_new (NULL, _goa_client_new_cb, self);
247
248   /* key file store */
249   self->priv->store = g_key_file_new ();
250   self->priv->filename = g_build_filename (g_get_user_data_dir (), "telepathy",
251       "mission-control", "accounts-goa.cfg", NULL);
252
253   load_store (self);
254 }
255
256
257 static void
258 _account_added_cb (GoaClient *client,
259     GoaObject *object,
260     McpAccountManagerGoa *self)
261 {
262   _new_account (self, object);
263 }
264
265
266 static void
267 _account_removed_cb (GoaClient *client,
268     GoaObject *object,
269     McpAccountManagerGoa *self)
270 {
271   GoaAccount *account = goa_object_peek_account (object);
272   char *name = get_tp_account_name (account);
273
274   if (self->priv->ready)
275     g_signal_emit_by_name (self, "deleted", name);
276
277   g_hash_table_remove (self->priv->accounts, name);
278
279   g_free (name);
280 }
281
282 static void
283 _goa_client_new_cb (GObject *obj,
284     GAsyncResult *result,
285     gpointer user_data)
286 {
287   McpAccountManagerGoa *self = user_data;
288   GoaClient *client;
289   GList *accounts, *ptr;
290   GError *error = NULL;
291
292   self->priv->client = goa_client_new_finish (result, &error);
293
294   if (error != NULL)
295     {
296       DEBUG ("Failed to connect to GOA");
297       return;
298     }
299
300   accounts = goa_client_get_accounts (self->priv->client);
301
302   for (ptr = accounts; ptr != NULL; ptr = ptr->next)
303     {
304       _new_account (self, ptr->data);
305     }
306
307   g_list_free_full (accounts, g_object_unref);
308
309   g_signal_connect (self->priv->client, "account-added",
310       G_CALLBACK (_account_added_cb), self);
311   g_signal_connect (self->priv->client, "account-removed",
312       G_CALLBACK (_account_removed_cb), self);
313 }
314
315
316 static GList *
317 mcp_account_manager_goa_list (const McpAccountStorage *self,
318     const McpAccountManager *am)
319 {
320   McpAccountManagerGoaPrivate *priv = GET_PRIVATE (self);
321   GList *accounts = NULL;
322   GHashTableIter iter;
323   gpointer key;
324
325   DEBUG (G_STRFUNC);
326
327   g_hash_table_iter_init (&iter, priv->accounts);
328   while (g_hash_table_iter_next (&iter, &key, NULL))
329     accounts = g_list_prepend (accounts, g_strdup (key));
330
331   return accounts;
332 }
333
334
335 static void
336 get_enabled (const McpAccountStorage *self,
337     const McpAccountManager *am,
338     const gchar *acct,
339     GoaObject *object)
340 {
341   mcp_account_manager_set_value (am, acct, "Enabled",
342       goa_object_peek_chat (object) != NULL ? "true" : "false");
343 }
344
345
346 static gboolean
347 mcp_account_manager_goa_get (const McpAccountStorage *self,
348     const McpAccountManager *am,
349     const gchar *acct,
350     const gchar *key)
351 {
352   McpAccountManagerGoaPrivate *priv = GET_PRIVATE (self);
353   GoaObject *object;
354   GoaAccount *account;
355
356   DEBUG ("%s: %s, %s", G_STRFUNC, acct, key);
357
358   object = g_hash_table_lookup (priv->accounts, acct);
359
360   if (object == NULL)
361     return FALSE;
362
363   account = goa_object_peek_account (object);
364
365   if (account == NULL)
366     return FALSE;
367
368   if (key == NULL)
369     {
370       /* load all keys */
371       GHashTable *params = get_tp_parameters (account);
372       GHashTableIter iter;
373       gpointer key, value;
374       GStrv keys;
375       guint i;
376       gssize nkeys = 0;
377
378       /* Properties from GOA */
379       g_hash_table_iter_init (&iter, params);
380       while (g_hash_table_iter_next (&iter, &key, &value))
381         mcp_account_manager_set_value (am, acct, key, value);
382
383       g_hash_table_destroy (params);
384
385       /* Stored properties */
386       keys = g_key_file_get_keys (priv->store, acct, &nkeys, NULL);
387
388       for (i = 0; i < nkeys; i++)
389         {
390           gchar *v = g_key_file_get_value (priv->store, acct, keys[i], NULL);
391
392           if (v != NULL)
393             {
394               mcp_account_manager_set_value (am, acct, keys[i], v);
395               g_free (v);
396             }
397         }
398
399       g_strfreev (keys);
400
401       /* Enabled */
402       get_enabled (self, am, acct, object);
403     }
404   else if (!tp_strdiff (key, "Enabled"))
405     {
406       get_enabled (self, am, acct, object);
407     }
408   else
409     {
410       /* get a specific key */
411       GHashTable *params = get_tp_parameters (account);
412       gchar *value;
413
414       value = g_hash_table_lookup (params, key);
415
416       if (value == NULL)
417         value = g_key_file_get_value (priv->store, acct, key, NULL);
418       else
419         value = g_strdup (value);
420
421       mcp_account_manager_set_value (am, acct, key, value);
422
423       g_hash_table_destroy (params);
424       g_free (value);
425     }
426
427   return TRUE;
428 }
429
430 static gboolean
431 account_is_in_goa (const McpAccountStorage *self,
432     const gchar *account)
433 {
434   McpAccountManagerGoaPrivate *priv = GET_PRIVATE (self);
435
436   return (g_hash_table_lookup (priv->accounts, acct) != NULL);
437 }
438
439 static gboolean
440 mcp_account_manager_goa_set (const McpAccountStorage *self,
441     const McpAccountManager *am,
442     const gchar *account,
443     const gchar *key,
444     const gchar *val)
445 {
446   McpAccountManagerGoaPrivate *priv = GET_PRIVATE (self);
447   GError *error = NULL;
448
449   if (!account_is_in_goa (self, account))
450     return FALSE;
451
452   /* No need to save Enabled, it's up to the GOA configuration if the account
453    * is configured or not. */
454   if (!tp_strdiff (key, "Enabled"))
455     return TRUE;
456
457   DEBUG ("%s: (%s, %s, %s)", G_STRFUNC, account, key, val);
458
459   if (val != NULL)
460     g_key_file_set_value (priv->store, account, key, val);
461   else
462     g_key_file_remove_key (priv->store, account, key, NULL);
463
464   /* Pretend we save everything so MC won't save this in accounts.cfg */
465   return TRUE;
466 }
467
468
469 static gboolean
470 mcp_account_manager_goa_delete (const McpAccountStorage *self,
471     const McpAccountManager *am,
472     const gchar *account,
473     const gchar *key)
474 {
475   McpAccountManagerGoaPrivate *priv = GET_PRIVATE (self);
476
477   if (!account_is_in_goa (self, account))
478     return FALSE;
479
480   DEBUG ("%s: (%s, %s)", G_STRFUNC, account, key);
481
482   if (key == NULL)
483     {
484       g_key_file_remove_group (priv->store, account, NULL);
485     }
486   else
487     {
488       g_key_file_remove_key (priv->store, account, key, NULL);
489     }
490
491   /* Pretend we deleted everything */
492   return TRUE;
493 }
494
495
496 static gboolean
497 mcp_account_manager_goa_commit (const McpAccountStorage *self,
498     const McpAccountManager *am)
499 {
500   McpAccountManagerGoaPrivate *priv = GET_PRIVATE (self);
501   gchar *data;
502   gsize len;
503   GError *error = NULL;
504
505   DEBUG ("Save config to %s", priv->filename);
506
507   data = g_key_file_to_data (priv->store, &len, &error);
508   if (data == NULL)
509     {
510       DEBUG ("Failed to get data from store: %s", error->message);
511
512       g_error_free (error);
513       return FALSE;
514     }
515
516   if (!g_file_set_contents (priv->filename, data, len, &error))
517     {
518       DEBUG ("Failed to write file: %s", error->message);
519
520       g_free (data);
521       g_error_free (error);
522       return FALSE;
523     }
524
525   g_free (data);
526
527   return TRUE;
528 }
529
530
531 static void
532 mcp_account_manager_goa_ready (const McpAccountStorage *self,
533     const McpAccountManager *am)
534 {
535   McpAccountManagerGoaPrivate *priv = GET_PRIVATE (self);
536
537   priv->ready = TRUE;
538 }
539
540
541 static guint
542 mcp_account_manager_goa_get_restrictions (const McpAccountStorage *self,
543     const gchar *account)
544 {
545   return TP_STORAGE_RESTRICTION_FLAG_CANNOT_SET_PARAMETERS |
546          TP_STORAGE_RESTRICTION_FLAG_CANNOT_SET_SERVICE |
547          TP_STORAGE_RESTRICTION_FLAG_CANNOT_SET_ENABLED;
548 }
549
550
551 static void
552 mcp_account_manager_goa_get_identifier (const McpAccountStorage *self,
553     const gchar *acct,
554     GValue *identifier)
555 {
556   McpAccountManagerGoaPrivate *priv = GET_PRIVATE (self);
557   GoaObject *object;
558   GoaAccount *account;
559
560   object = g_hash_table_lookup (priv->accounts, acct);
561   g_return_if_fail (object != NULL);
562
563   account = goa_object_peek_account (object);
564   g_return_if_fail (account != NULL);
565
566   g_value_init (identifier, G_TYPE_STRING);
567   g_value_set_string (identifier, goa_account_get_id (account));
568 }
569
570
571 static void
572 account_storage_iface_init (McpAccountStorageIface *iface)
573 {
574   mcp_account_storage_iface_set_name (iface, PLUGIN_NAME);
575   mcp_account_storage_iface_set_desc (iface, PLUGIN_DESCRIPTION);
576   mcp_account_storage_iface_set_priority (iface, PLUGIN_PRIORITY);
577   mcp_account_storage_iface_set_provider (iface, PLUGIN_PROVIDER);
578
579 #define IMPLEMENT(x) mcp_account_storage_iface_implement_##x(iface, \
580     mcp_account_manager_goa_##x)
581   IMPLEMENT (get);
582   IMPLEMENT (list);
583   IMPLEMENT (set);
584   IMPLEMENT (delete);
585   IMPLEMENT (commit);
586   IMPLEMENT (ready);
587   IMPLEMENT (get_restrictions);
588   IMPLEMENT (get_identifier);
589 #undef IMPLEMENT
590 }