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