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