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