2 * mcp-account-manager-goa.c
4 * McpAccountManagerGoa - a Mission Control plugin to expose GNOME Online
5 * Accounts with chat capabilities (e.g. Facebook) to Mission Control
7 * Copyright (C) 2010-2014 Collabora Ltd.
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.
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.
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/>.
23 * Danielle Madeley <danielle.madeley@collabora.co.uk>
27 #include "mcp-account-manager-goa.h"
29 #define GOA_API_IS_SUBJECT_TO_CHANGE /* awesome! */
33 #define GET_PRIVATE(self) (((McpAccountManagerGoa *) self)->priv)
34 #define DECLARE_GASYNC_CALLBACK(name) \
35 static void name (GObject *, GAsyncResult *, gpointer);
37 #define PLUGIN_NAME "goa"
38 #define PLUGIN_DESCRIPTION "Provide Telepathy Accounts from GOA"
39 #define PLUGIN_PROVIDER EMPATHY_GOA_PROVIDER
41 #define INITIAL_COMMENT "Parameters of GOA Telepathy accounts"
43 #ifdef MCP_API_VERSION_5_18
45 # define RESTRICTIONS TpStorageRestrictionFlags
46 # define WAS_CONST /* nothing */
47 /* Its historical value was based on the KEYRING priority which no longer
48 * exists. Using a large number is OK, because it uses unusual account names
49 * which are unlikely to collide. */
50 # define PLUGIN_PRIORITY (10010)
51 /* McpAccountStorageSetResult enum is defined by MC */
53 #else /* MC 5.16 or older */
55 # define RESTRICTIONS guint
56 # define WAS_CONST const
57 # define PLUGIN_PRIORITY (MCP_ACCOUNT_STORAGE_PLUGIN_PRIO_KEYRING + 10)
59 /* we use this in helper functions */
61 MCP_ACCOUNT_STORAGE_SET_RESULT_FAILED,
62 MCP_ACCOUNT_STORAGE_SET_RESULT_UNCHANGED,
63 MCP_ACCOUNT_STORAGE_SET_RESULT_CHANGED
64 } McpAccountStorageSetResult;
66 #endif /* MC 5.16 or older */
68 static void account_storage_iface_init (McpAccountStorageIface *iface);
70 G_DEFINE_TYPE_WITH_CODE (McpAccountManagerGoa,
71 mcp_account_manager_goa,
73 G_IMPLEMENT_INTERFACE (MCP_TYPE_ACCOUNT_STORAGE,
74 account_storage_iface_init))
76 struct _McpAccountManagerGoaPrivate
81 GHashTable *accounts; /* alloc'ed string -> ref'ed GoaObject */
89 mcp_account_manager_goa_dispose (GObject *self)
91 McpAccountManagerGoaPrivate *priv = GET_PRIVATE (self);
93 tp_clear_object (&priv->client);
95 G_OBJECT_CLASS (mcp_account_manager_goa_parent_class)->dispose (self);
100 mcp_account_manager_goa_finalize (GObject *self)
102 McpAccountManagerGoaPrivate *priv = GET_PRIVATE (self);
104 g_hash_table_unref (priv->accounts);
105 g_key_file_free (priv->store);
106 g_free (priv->filename);
108 G_OBJECT_CLASS (mcp_account_manager_goa_parent_class)->finalize (self);
113 mcp_account_manager_goa_class_init (McpAccountManagerGoaClass *klass)
115 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
117 gobject_class->dispose = mcp_account_manager_goa_dispose;
118 gobject_class->finalize = mcp_account_manager_goa_finalize;
120 g_type_class_add_private (gobject_class,
121 sizeof (McpAccountManagerGoaPrivate));
125 get_tp_parameters (GoaAccount *account)
127 GHashTable *params = g_hash_table_new_full (g_str_hash, g_str_equal,
129 const char *type = goa_account_get_provider_type (account);
131 #define PARAM(key, value) g_hash_table_insert (params, key, g_strdup (value));
133 if (!tp_strdiff (type, "google"))
135 PARAM ("manager", "gabble");
136 PARAM ("protocol", "jabber");
137 PARAM ("Icon", "im-google-talk");
138 PARAM ("Service", "google-talk");
140 PARAM ("param-account", goa_account_get_identity (account));
141 PARAM ("param-server", "talk.google.com");
142 PARAM ("param-fallback-servers",
143 "talkx.l.google.com;"
144 "talkx.l.google.com:443,oldssl;"
145 "talkx.l.google.com:80");
146 PARAM ("param-extra-certificate-identities", "talk.google.com");
147 PARAM ("param-require-encryption", "true");
149 else if (!tp_strdiff (type, "facebook"))
151 PARAM ("manager", "gabble");
152 PARAM ("protocol", "jabber");
153 PARAM ("Icon", "im-facebook");
154 PARAM ("Service", "facebook");
156 PARAM ("param-account", "chat.facebook.com");
157 PARAM ("param-server", "chat.facebook.com");
158 PARAM ("param-require-encryption", "true");
159 PARAM ("param-fallback-servers",
160 "chat.facebook.com:443");
164 DEBUG ("Unknown account type %s", type);
165 g_hash_table_unref (params);
169 /* generic properties */
170 PARAM ("DisplayName", goa_account_get_presentation_identity (account));
179 get_tp_account_name (GoaAccount *account)
181 GHashTable *params = get_tp_parameters (account);
182 const char *type = goa_account_get_provider_type (account);
183 const char *id = goa_account_get_id (account);
189 name = g_strdup_printf ("%s/%s/goa_%s_%s",
190 (char *) g_hash_table_lookup (params, "manager"),
191 (char *) g_hash_table_lookup (params, "protocol"),
194 g_hash_table_unref (params);
200 object_chat_changed_cb (GoaObject *object,
202 McpAccountManagerGoa *self)
204 GoaAccount *account = goa_object_peek_account (object);
205 char *name = get_tp_account_name (account);
211 enabled = (goa_object_peek_chat (object) != NULL);
213 DEBUG ("%s %s", name, enabled ? "enabled" : "disabled");
215 if (self->priv->ready)
216 mcp_account_storage_emit_toggled (MCP_ACCOUNT_STORAGE (self),
221 _new_account (McpAccountManagerGoa *self,
224 GoaAccount *account = goa_object_peek_account (object);
225 char *account_name = get_tp_account_name (account);
227 if (account_name == NULL)
230 /* @account_name now is owned by the hash table */
231 g_hash_table_insert (self->priv->accounts, account_name,
232 g_object_ref (object));
234 if (self->priv->ready)
235 mcp_account_storage_emit_created (MCP_ACCOUNT_STORAGE (self),
238 tp_g_signal_connect_object (object, "notify::chat",
239 G_CALLBACK (object_chat_changed_cb), self, 0);
243 DECLARE_GASYNC_CALLBACK (_goa_client_new_cb);
246 load_store (McpAccountManagerGoa *self)
248 GError *error = NULL;
250 if (!g_key_file_load_from_file (self->priv->store, self->priv->filename,
251 G_KEY_FILE_KEEP_COMMENTS, &error))
255 DEBUG ("Failed to load keyfile, creating a new one: %s", error->message);
257 dir = g_path_get_dirname (self->priv->filename);
259 g_mkdir_with_parents (dir, 0700);
262 g_key_file_set_comment (self->priv->store, NULL, NULL, INITIAL_COMMENT,
265 g_error_free (error);
270 mcp_account_manager_goa_init (McpAccountManagerGoa *self)
272 DEBUG ("GOA MC plugin initialised");
274 self->priv = G_TYPE_INSTANCE_GET_PRIVATE (self,
275 MCP_TYPE_ACCOUNT_MANAGER_GOA, McpAccountManagerGoaPrivate);
277 #ifdef MCP_API_VERSION_5_18
278 /* the ready callback no longer exists, we may emit signals at any time */
279 self->priv->ready = TRUE;
282 self->priv->accounts = g_hash_table_new_full (g_str_hash, g_str_equal,
283 g_free, g_object_unref);
285 goa_client_new (NULL, _goa_client_new_cb, self);
288 self->priv->store = g_key_file_new ();
289 self->priv->filename = g_build_filename (g_get_user_data_dir (), "telepathy",
290 "mission-control", "accounts-goa.cfg", NULL);
297 _account_added_cb (GoaClient *client,
299 McpAccountManagerGoa *self)
301 _new_account (self, object);
306 _account_removed_cb (GoaClient *client,
308 McpAccountManagerGoa *self)
310 GoaAccount *account = goa_object_peek_account (object);
311 char *name = get_tp_account_name (account);
316 if (self->priv->ready)
317 mcp_account_storage_emit_deleted (MCP_ACCOUNT_STORAGE (self), name);
319 g_hash_table_remove (self->priv->accounts, name);
325 _goa_client_new_cb (GObject *obj,
326 GAsyncResult *result,
329 McpAccountManagerGoa *self = user_data;
330 GList *accounts, *ptr;
331 GError *error = NULL;
333 self->priv->client = goa_client_new_finish (result, &error);
337 DEBUG ("Failed to connect to GOA");
341 accounts = goa_client_get_accounts (self->priv->client);
343 for (ptr = accounts; ptr != NULL; ptr = ptr->next)
345 _new_account (self, ptr->data);
348 g_list_free_full (accounts, g_object_unref);
350 g_signal_connect (self->priv->client, "account-added",
351 G_CALLBACK (_account_added_cb), self);
352 g_signal_connect (self->priv->client, "account-removed",
353 G_CALLBACK (_account_removed_cb), self);
358 mcp_account_manager_goa_list (WAS_CONST McpAccountStorage *self,
359 WAS_CONST McpAccountManager *am)
361 McpAccountManagerGoaPrivate *priv = GET_PRIVATE (self);
362 GList *accounts = NULL;
368 g_hash_table_iter_init (&iter, priv->accounts);
369 while (g_hash_table_iter_next (&iter, &key, NULL))
370 accounts = g_list_prepend (accounts, g_strdup (key));
376 #ifdef MCP_API_VERSION_5_18
379 get_item (McpAccountManagerGoa *self,
380 McpAccountManager *am,
383 const GVariantType *type)
389 GVariant *ret = NULL;
391 DEBUG ("%s: %s, %s", G_STRFUNC, acc, key);
393 object = g_hash_table_lookup (self->priv->accounts, acc);
395 g_return_val_if_fail (object != NULL, NULL);
397 account = goa_object_peek_account (object);
399 g_return_val_if_fail (account != NULL, NULL);
401 if (!tp_strdiff (key, "Enabled"))
403 return g_variant_ref_sink (g_variant_new_boolean (
404 !goa_account_get_chat_disabled (account)));
407 bits = get_tp_parameters (account);
409 esc = g_hash_table_lookup (bits, key);
412 esc = g_key_file_get_value (self->priv->store, acc, key, NULL);
414 esc = g_strdup (esc);
418 ret = mcp_account_manager_unescape_variant_from_keyfile (am,
424 g_hash_table_unref (bits);
429 mcp_account_manager_goa_get_attribute (McpAccountStorage *self,
430 McpAccountManager *am,
432 const gchar *attribute,
433 const GVariantType *type,
434 McpAttributeFlags *flags)
436 return get_item (MCP_ACCOUNT_MANAGER_GOA (self), am, acc, attribute, type);
440 mcp_account_manager_goa_get_parameter (McpAccountStorage *storage,
441 McpAccountManager *am,
443 const gchar *parameter,
444 const GVariantType *type,
445 McpParameterFlags *flags)
450 key = g_strdup_printf ("param-%s", parameter);
452 ret = get_item (MCP_ACCOUNT_MANAGER_GOA (storage), am, acc, key, type);
457 #else /* MC 5.16 or older */
460 get_enabled (WAS_CONST McpAccountStorage *self,
461 WAS_CONST McpAccountManager *am,
465 mcp_account_manager_set_value (am, acc, "Enabled",
466 goa_account_get_chat_disabled (account) == FALSE ? "true" : "false");
470 mcp_account_manager_goa_get (WAS_CONST McpAccountStorage *self,
471 WAS_CONST McpAccountManager *am,
475 McpAccountManagerGoaPrivate *priv = GET_PRIVATE (self);
479 DEBUG ("%s: %s, %s", G_STRFUNC, acc, key);
481 object = g_hash_table_lookup (priv->accounts, acc);
486 account = goa_object_peek_account (object);
494 GHashTable *params = get_tp_parameters (account);
501 /* Properties from GOA */
502 g_hash_table_iter_init (&iter, params);
503 while (g_hash_table_iter_next (&iter, &k, &value))
504 mcp_account_manager_set_value (am, acc, k, value);
506 g_hash_table_unref (params);
508 /* Stored properties */
509 keys = g_key_file_get_keys (priv->store, acc, &nkeys, NULL);
511 for (i = 0; i < nkeys; i++)
513 gchar *v = g_key_file_get_value (priv->store, acc, keys[i], NULL);
517 mcp_account_manager_set_value (am, acc, keys[i], v);
525 get_enabled (self, am, acc, account);
527 else if (!tp_strdiff (key, "Enabled"))
529 get_enabled (self, am, acc, account);
533 /* get a specific key */
534 GHashTable *params = get_tp_parameters (account);
537 value = g_hash_table_lookup (params, key);
540 value = g_key_file_get_value (priv->store, acc, key, NULL);
542 value = g_strdup (value);
544 mcp_account_manager_set_value (am, acc, key, value);
546 g_hash_table_unref (params);
553 #endif /* MC 5.16 or older */
557 account_is_in_goa (WAS_CONST McpAccountStorage *self,
558 const gchar *account)
560 McpAccountManagerGoaPrivate *priv = GET_PRIVATE (self);
562 return (g_hash_table_lookup (priv->accounts, account) != NULL);
565 static McpAccountStorageSetResult
566 mcp_account_manager_goa_set_enabled (McpAccountStorage *self,
567 const gchar *account,
570 McpAccountManagerGoaPrivate *priv = GET_PRIVATE (self);
574 object = g_hash_table_lookup (priv->accounts, account);
577 return MCP_ACCOUNT_STORAGE_SET_RESULT_FAILED;
579 acc = goa_object_peek_account (object);
582 return MCP_ACCOUNT_STORAGE_SET_RESULT_FAILED;
584 if (goa_account_get_chat_disabled (acc) == !enabled)
585 return MCP_ACCOUNT_STORAGE_SET_RESULT_UNCHANGED;
587 goa_account_set_chat_disabled (acc, !enabled);
588 return MCP_ACCOUNT_STORAGE_SET_RESULT_CHANGED;
591 #ifdef MCP_API_VERSION_5_18
593 static McpAccountStorageSetResult
594 mcp_account_manager_goa_set_attribute (McpAccountStorage *storage,
595 McpAccountManager *am,
596 const gchar *account_name,
597 const gchar *attribute,
599 McpAttributeFlags flags)
601 McpAccountManagerGoaPrivate *priv = GET_PRIVATE (storage);
602 McpAccountStorageSetResult ret = MCP_ACCOUNT_STORAGE_SET_RESULT_FAILED;
604 g_return_val_if_fail (account_is_in_goa (storage, account_name),
605 MCP_ACCOUNT_STORAGE_SET_RESULT_FAILED);
607 if (!tp_strdiff (attribute, "Enabled"))
609 g_return_val_if_fail (
610 g_variant_classify (value) == G_VARIANT_CLASS_BOOLEAN,
611 MCP_ACCOUNT_STORAGE_SET_RESULT_FAILED);
613 return mcp_account_manager_goa_set_enabled (storage, account_name,
614 g_variant_get_boolean (value));
616 /* FIXME: filter out manager, protocol, Icon, Service? */
617 else if (value != NULL)
619 gchar *esc = mcp_account_manager_escape_variant_for_keyfile (am,
624 return MCP_ACCOUNT_STORAGE_SET_RESULT_FAILED;
626 old = g_key_file_get_value (priv->store, account_name, attribute, NULL);
628 if (tp_strdiff (old, esc))
630 g_key_file_set_value (priv->store, account_name, attribute, esc);
631 ret = MCP_ACCOUNT_STORAGE_SET_RESULT_CHANGED;
635 ret = MCP_ACCOUNT_STORAGE_SET_RESULT_UNCHANGED;
643 if (g_key_file_has_key (priv->store, account_name, attribute, NULL))
645 g_key_file_remove_key (priv->store, account_name, attribute, NULL);
646 ret = MCP_ACCOUNT_STORAGE_SET_RESULT_CHANGED;
650 ret = MCP_ACCOUNT_STORAGE_SET_RESULT_UNCHANGED;
657 static McpAccountStorageSetResult
658 mcp_account_manager_goa_set_parameter (McpAccountStorage *storage,
659 McpAccountManager *am,
660 const gchar *account_name,
661 const gchar *parameter,
663 McpParameterFlags flags)
665 McpAccountManagerGoaPrivate *priv = GET_PRIVATE (storage);
666 McpAccountStorageSetResult ret = MCP_ACCOUNT_STORAGE_SET_RESULT_FAILED;
671 g_return_val_if_fail (account_is_in_goa (storage, account_name),
672 MCP_ACCOUNT_STORAGE_SET_RESULT_FAILED);
674 key = g_strdup_printf ("param-%s", parameter);
676 /* FIXME: filter out reserved keys for this account? */
679 esc = mcp_account_manager_escape_variant_for_keyfile (am,
685 old = g_key_file_get_value (priv->store, account_name, key, NULL);
687 if (tp_strdiff (esc, old))
689 g_key_file_set_value (priv->store, account_name, key, esc);
690 ret = MCP_ACCOUNT_STORAGE_SET_RESULT_CHANGED;
694 ret = MCP_ACCOUNT_STORAGE_SET_RESULT_UNCHANGED;
699 if (g_key_file_has_key (priv->store, account_name, key, NULL))
701 g_key_file_remove_key (priv->store, account_name, key, NULL);
702 ret = MCP_ACCOUNT_STORAGE_SET_RESULT_CHANGED;
706 ret = MCP_ACCOUNT_STORAGE_SET_RESULT_UNCHANGED;
718 #else /* MC 5.16 or older */
721 mcp_account_manager_goa_set (const McpAccountStorage *self,
722 const McpAccountManager *am,
723 const gchar *account,
727 McpAccountManagerGoaPrivate *priv = GET_PRIVATE (self);
729 if (!account_is_in_goa (self, account))
732 DEBUG ("%s: (%s, %s, %s)", G_STRFUNC, account, key, val);
734 if (!tp_strdiff (key, "Enabled"))
736 if (mcp_account_manager_goa_set_enabled ((McpAccountStorage *) self,
738 !tp_strdiff (val, "true")) != MCP_ACCOUNT_STORAGE_SET_RESULT_FAILED)
745 g_key_file_set_value (priv->store, account, key, val);
747 g_key_file_remove_key (priv->store, account, key, NULL);
749 /* Pretend we save everything so MC won't save this in accounts.cfg */
753 #endif /* MC 5.16 or older */
757 mcp_account_manager_goa_delete (WAS_CONST McpAccountStorage *self,
758 WAS_CONST McpAccountManager *am,
759 const gchar *account,
762 McpAccountManagerGoaPrivate *priv = GET_PRIVATE (self);
764 if (!account_is_in_goa (self, account))
767 DEBUG ("%s: (%s, %s)", G_STRFUNC, account, key);
771 g_key_file_remove_group (priv->store, account, NULL);
775 g_key_file_remove_key (priv->store, account, key, NULL);
778 /* Pretend we deleted everything */
783 #ifdef MCP_API_VERSION_5_18
785 mcp_account_manager_goa_delete_async (McpAccountStorage *self,
786 McpAccountManager *am,
787 const gchar *account_name,
788 GCancellable *cancellable,
789 GAsyncReadyCallback callback,
792 GTask *task = g_task_new (self, cancellable, callback, user_data);
794 if (mcp_account_manager_goa_delete (self, am, account_name, NULL))
796 g_task_return_boolean (task, TRUE);
800 g_task_return_new_error (task, TP_ERROR, TP_ERROR_DOES_NOT_EXIST,
801 "Account does not exist in GOA");
804 g_object_unref (task);
808 mcp_account_manager_goa_delete_finish (McpAccountStorage *self,
812 return g_task_propagate_boolean (G_TASK (res), error);
814 #endif /* MC >= 5.18 API */
817 commit (McpAccountManagerGoa *self)
819 McpAccountManagerGoaPrivate *priv = self->priv;
822 GError *error = NULL;
824 DEBUG ("Save config to %s", priv->filename);
826 data = g_key_file_to_data (priv->store, &len, &error);
829 DEBUG ("Failed to get data from store: %s", error->message);
831 g_error_free (error);
835 if (!g_file_set_contents (priv->filename, data, len, &error))
837 DEBUG ("Failed to write file: %s", error->message);
840 g_error_free (error);
849 #ifdef MCP_API_VERSION_5_18
851 mcp_account_manager_goa_commit (McpAccountStorage *self,
852 McpAccountManager *am,
853 const gchar *account)
855 return commit (MCP_ACCOUNT_MANAGER_GOA (self));
859 mcp_account_manager_goa_commit (const McpAccountStorage *self,
860 const McpAccountManager *am)
862 return commit (MCP_ACCOUNT_MANAGER_GOA (self));
867 #ifndef MCP_API_VERSION_5_18
868 /* removed in 5.18, MC should now be ready to receive signals at any time */
870 mcp_account_manager_goa_ready (WAS_CONST McpAccountStorage *self,
871 WAS_CONST McpAccountManager *am)
873 McpAccountManagerGoaPrivate *priv = GET_PRIVATE (self);
881 mcp_account_manager_goa_get_restrictions (WAS_CONST McpAccountStorage *self,
882 const gchar *account)
884 return TP_STORAGE_RESTRICTION_FLAG_CANNOT_SET_PARAMETERS |
885 TP_STORAGE_RESTRICTION_FLAG_CANNOT_SET_SERVICE;
890 mcp_account_manager_goa_get_identifier (WAS_CONST McpAccountStorage *self,
894 McpAccountManagerGoaPrivate *priv = GET_PRIVATE (self);
898 object = g_hash_table_lookup (priv->accounts, acc);
899 g_return_if_fail (object != NULL);
901 account = goa_object_peek_account (object);
902 g_return_if_fail (account != NULL);
904 g_value_init (identifier, G_TYPE_STRING);
905 g_value_set_string (identifier, goa_account_get_id (account));
910 account_storage_iface_init (McpAccountStorageIface *iface)
912 iface->name = PLUGIN_NAME;
913 iface->desc = PLUGIN_DESCRIPTION;
914 iface->priority = PLUGIN_PRIORITY;
915 iface->provider = PLUGIN_PROVIDER;
917 #define IMPLEMENT(x) iface->x = mcp_account_manager_goa_##x
921 IMPLEMENT (get_restrictions);
922 IMPLEMENT (get_identifier);
924 #ifdef MCP_API_VERSION_5_18
925 IMPLEMENT (delete_async);
926 IMPLEMENT (delete_finish);
927 IMPLEMENT (get_attribute);
928 IMPLEMENT (get_parameter);
929 IMPLEMENT (set_attribute);
930 IMPLEMENT (set_parameter);