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