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