static void get_all_providers_cb (GObject *source, GAsyncResult *res, gpointer user_data) { AddAccountData *data = user_data; GoaProvider *provider; GKeyFile *key_file; GError *error; GList *providers; GList *l; gchar *path; gchar *id; gchar *group; gchar *key_file_data; gsize length; gchar *object_path; GVariantIter iter; const gchar *key; const gchar *value; /* TODO: could check for @type */ provider = NULL; key_file = NULL; providers = NULL; path = NULL; id = NULL; group = NULL; key_file_data = NULL; object_path = NULL; if (!goa_provider_get_all_finish (&providers, res, NULL)) goto out; for (l = providers; l != NULL; l = l->next) { GoaProvider *p; const gchar *type; p = GOA_PROVIDER (l->data); type = goa_provider_get_provider_type (p); if (g_strcmp0 (type, data->provider_type) == 0) { provider = p; break; } } if (provider == NULL) { error= NULL; g_set_error (&error, GOA_ERROR, GOA_ERROR_FAILED, /* TODO: more specific */ _("Failed to find a provider for: %s"), data->provider_type); g_dbus_method_invocation_return_gerror (data->invocation, error); goto out; } key_file = g_key_file_new (); path = g_strdup_printf ("%s/goa-1.0/accounts.conf", g_get_user_config_dir ()); error = NULL; if (!g_file_get_contents (path, &key_file_data, &length, &error)) { if (error->domain == G_FILE_ERROR && error->code == G_FILE_ERROR_NOENT) { g_error_free (error); } else { g_prefix_error (&error, "Error loading file %s: ", path); g_dbus_method_invocation_return_gerror (data->invocation, error); goto out; } } else { if (length > 0) { error = NULL; if (!g_key_file_load_from_data (key_file, key_file_data, length, G_KEY_FILE_KEEP_COMMENTS, &error)) { g_prefix_error (&error, "Error parsing key-value-file %s: ", path); g_dbus_method_invocation_return_gerror (data->invocation, error); goto out; } } } id = generate_new_id (data->daemon); group = g_strdup_printf ("Account %s", id); g_key_file_set_string (key_file, group, "Provider", data->provider_type); g_key_file_set_string (key_file, group, "Identity", data->identity); g_key_file_set_string (key_file, group, "PresentationIdentity", data->presentation_identity); g_variant_iter_init (&iter, data->details); while (g_variant_iter_next (&iter, "{&s&s}", &key, &value)) { /* We treat IsTemporary special. If it's true we add in * the current session guid, so it can be ignored after * the session is over. */ if (g_strcmp0 (key, "IsTemporary") == 0) { if (g_strcmp0 (value, "true") == 0) { const char *guid; guid = g_dbus_connection_get_guid (data->daemon->connection); g_key_file_set_string (key_file, group, "SessionId", guid); } } g_key_file_set_string (key_file, group, key, value); } g_free (key_file_data); error = NULL; key_file_data = g_key_file_to_data (key_file, &length, &error); if (key_file_data == NULL) { g_prefix_error (&error, "Error generating key-value-file: "); g_dbus_method_invocation_return_gerror (data->invocation, error); goto out; } error = NULL; if (!g_file_set_contents (path, key_file_data, length, &error)) { g_prefix_error (&error, "Error writing key-value-file %s: ", path); g_dbus_method_invocation_return_gerror (data->invocation, error); goto out; } /* We don't want to fail AddAccount if we could not store the * credentials in the keyring. */ goa_utils_store_credentials_for_id_sync (provider, id, data->credentials, NULL, /* GCancellable */ NULL); goa_daemon_reload_configuration (data->daemon); object_path = g_strdup_printf ("/org/gnome/OnlineAccounts/Accounts/%s", id); goa_manager_complete_add_account (data->manager, data->invocation, object_path); out: g_free (object_path); if (providers != NULL) g_list_free_full (providers, g_object_unref); g_free (key_file_data); g_free (group); g_free (id); g_free (path); if (key_file != NULL) g_key_file_free (key_file); g_object_unref (data->daemon); g_object_unref (data->manager); g_object_unref (data->invocation); g_free (data->provider_type); g_free (data->identity); g_free (data->presentation_identity); g_variant_unref (data->credentials); g_variant_unref (data->details); g_slice_free (AddAccountData, data); }
static gboolean on_manager_handle_add_account (GoaManager *manager, GDBusMethodInvocation *invocation, const gchar *provider, const gchar *identity, const gchar *presentation_identity, GVariant *details, gpointer user_data) { GoaDaemon *daemon = GOA_DAEMON (user_data); GKeyFile *key_file; GError *error; gchar *path; gchar *id; gchar *group; gchar *data; gsize length; gchar *object_path; GVariantIter iter; const gchar *key; const gchar *value; /* TODO: could check for @type */ key_file = NULL; path = NULL; id = NULL; group = NULL; data = NULL; object_path = NULL; key_file = g_key_file_new (); path = g_strdup_printf ("%s/goa-1.0/accounts.conf", g_get_user_config_dir ()); error = NULL; if (!g_file_get_contents (path, &data, &length, &error)) { if (error->domain == G_FILE_ERROR && error->code == G_FILE_ERROR_NOENT) { g_error_free (error); } else { g_prefix_error (&error, "Error loading file %s: ", path); g_dbus_method_invocation_return_gerror (invocation, error); goto out; } } else { if (length > 0) { error = NULL; if (!g_key_file_load_from_data (key_file, data, length, G_KEY_FILE_KEEP_COMMENTS, &error)) { g_prefix_error (&error, "Error parsing key-value-file %s: ", path); g_dbus_method_invocation_return_gerror (invocation, error); goto out; } } } id = generate_new_id (daemon); group = g_strdup_printf ("Account %s", id); g_key_file_set_string (key_file, group, "Provider", provider); g_key_file_set_string (key_file, group, "Identity", identity); g_key_file_set_string (key_file, group, "PresentationIdentity", presentation_identity); g_variant_iter_init (&iter, details); while (g_variant_iter_next (&iter, "{&s&s}", &key, &value)) { g_key_file_set_string (key_file, group, key, value); } g_free (data); error = NULL; data = g_key_file_to_data (key_file, &length, &error); if (data == NULL) { g_prefix_error (&error, "Error generating key-value-file: "); g_dbus_method_invocation_return_gerror (invocation, error); goto out; } error = NULL; if (!g_file_set_contents (path, data, length, &error)) { g_prefix_error (&error, "Error writing key-value-file %s: ", path); g_dbus_method_invocation_return_gerror (invocation, error); goto out; } goa_daemon_reload_configuration (daemon); object_path = g_strdup_printf ("/org/gnome/OnlineAccounts/Accounts/%s", id); goa_manager_complete_add_account (manager, invocation, object_path); out: g_free (object_path); g_free (data); g_free (group); g_free (id); g_free (path); if (key_file != NULL) g_key_file_free (key_file); return TRUE; /* invocation was handled */ }