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