collection: Port to GTask

`GSimpleAsyncResult` is deprecated in favor of `GTask`.
This commit is contained in:
Niels De Graef 2019-12-21 14:41:03 +01:00
parent 95b154f442
commit 4266889f33

View File

@ -569,50 +569,39 @@ secret_collection_initable_iface (GInitableIface *iface)
iface->init = secret_collection_initable_init; iface->init = secret_collection_initable_init;
} }
typedef struct {
GCancellable *cancellable;
} InitClosure;
static void
init_closure_free (gpointer data)
{
InitClosure *closure = data;
g_clear_object (&closure->cancellable);
g_slice_free (InitClosure, closure);
}
static void static void
on_ensure_items (GObject *source, on_ensure_items (GObject *source,
GAsyncResult *result, GAsyncResult *result,
gpointer user_data) gpointer user_data)
{ {
GSimpleAsyncResult *res = G_SIMPLE_ASYNC_RESULT (user_data); GTask *task = G_TASK (user_data);
SecretCollection *self = SECRET_COLLECTION (source); SecretCollection *self = SECRET_COLLECTION (source);
GError *error = NULL; GError *error = NULL;
if (!secret_collection_load_items_finish (self, result, &error)) if (!secret_collection_load_items_finish (self, result, &error))
g_simple_async_result_take_error (res, error); g_task_return_error (task, g_steal_pointer (&error));
else
g_task_return_pointer (task, self, g_object_unref);
g_simple_async_result_complete (res); g_clear_object (&task);
g_object_unref (res);
} }
static void static void
collection_ensure_for_flags_async (SecretCollection *self, collection_ensure_for_flags_async (SecretCollection *self,
SecretCollectionFlags flags, SecretCollectionFlags flags,
GCancellable *cancellable, GTask *task)
GSimpleAsyncResult *async)
{ {
GCancellable *cancellable = g_task_get_cancellable (task);
SecretCollectionFlags want_flags; SecretCollectionFlags want_flags;
want_flags = flags & ~secret_collection_get_flags (self); want_flags = flags & ~secret_collection_get_flags (self);
if (want_flags & SECRET_COLLECTION_LOAD_ITEMS) { if (want_flags & SECRET_COLLECTION_LOAD_ITEMS) {
secret_collection_load_items (self, cancellable, secret_collection_load_items (self, cancellable,
on_ensure_items, g_object_ref (async)); on_ensure_items, g_object_ref (task));
} else { } else {
g_simple_async_result_complete (async); g_task_return_pointer (task, self, g_object_unref);
} }
} }
@ -621,25 +610,21 @@ on_init_service (GObject *source,
GAsyncResult *result, GAsyncResult *result,
gpointer user_data) gpointer user_data)
{ {
GSimpleAsyncResult *async = G_SIMPLE_ASYNC_RESULT (user_data); GTask *task = G_TASK (user_data);
SecretCollection *self = SECRET_COLLECTION (g_async_result_get_source_object (user_data)); SecretCollection *self = SECRET_COLLECTION (g_task_get_source_object (task));
InitClosure *init = g_simple_async_result_get_op_res_gpointer (async);
SecretService *service; SecretService *service;
GError *error = NULL; GError *error = NULL;
service = secret_service_get_finish (result, &error); service = secret_service_get_finish (result, &error);
if (error == NULL) { if (error == NULL) {
collection_take_service (self, service); collection_take_service (self, g_steal_pointer (&service));
collection_ensure_for_flags_async (self, self->pv->init_flags, collection_ensure_for_flags_async (self, self->pv->init_flags, task);
init->cancellable, async);
} else { } else {
g_simple_async_result_take_error (async, error); g_task_return_error (task, g_steal_pointer (&error));
g_simple_async_result_complete (async);
} }
g_object_unref (self); g_clear_object (&task);
g_object_unref (async);
} }
static void static void
@ -647,33 +632,31 @@ on_init_base (GObject *source,
GAsyncResult *result, GAsyncResult *result,
gpointer user_data) gpointer user_data)
{ {
GSimpleAsyncResult *res = G_SIMPLE_ASYNC_RESULT (user_data); GTask *task = G_TASK (user_data);
GCancellable *cancellable = g_task_get_cancellable (task);
SecretCollection *self = SECRET_COLLECTION (source); SecretCollection *self = SECRET_COLLECTION (source);
InitClosure *init = g_simple_async_result_get_op_res_gpointer (res);
GDBusProxy *proxy = G_DBUS_PROXY (self); GDBusProxy *proxy = G_DBUS_PROXY (self);
GError *error = NULL; GError *error = NULL;
if (!secret_collection_async_initable_parent_iface->init_finish (G_ASYNC_INITABLE (self), if (!secret_collection_async_initable_parent_iface->init_finish (G_ASYNC_INITABLE (self),
result, &error)) { result, &error)) {
g_simple_async_result_take_error (res, error); g_task_return_error (task, g_steal_pointer (&error));
g_simple_async_result_complete (res);
} else if (!_secret_util_have_cached_properties (proxy)) { } else if (!_secret_util_have_cached_properties (proxy)) {
g_simple_async_result_set_error (res, G_DBUS_ERROR, G_DBUS_ERROR_UNKNOWN_METHOD, g_task_return_new_error (task, G_DBUS_ERROR,
"No such secret collection at path: %s", G_DBUS_ERROR_UNKNOWN_METHOD,
g_dbus_proxy_get_object_path (proxy)); "No such secret collection at path: %s",
g_simple_async_result_complete (res); g_dbus_proxy_get_object_path (proxy));
} else if (self->pv->service == NULL) { } else if (self->pv->service == NULL) {
secret_service_get (SECRET_SERVICE_NONE, init->cancellable, secret_service_get (SECRET_SERVICE_NONE, cancellable,
on_init_service, g_object_ref (res)); on_init_service, g_steal_pointer (&task));
} else { } else {
collection_ensure_for_flags_async (self, self->pv->init_flags, collection_ensure_for_flags_async (self, self->pv->init_flags, task);
init->cancellable, res);
} }
g_object_unref (res); g_clear_object (&task);
} }
static void static void
@ -683,21 +666,18 @@ secret_collection_async_initable_init_async (GAsyncInitable *initable,
GAsyncReadyCallback callback, GAsyncReadyCallback callback,
gpointer user_data) gpointer user_data)
{ {
GSimpleAsyncResult *res; GTask *task;
InitClosure *closure;
res = g_simple_async_result_new (G_OBJECT (initable), callback, user_data, task = g_task_new (initable, cancellable, callback, user_data);
secret_collection_async_initable_init_async); g_task_set_source_tag (task, secret_collection_async_initable_init_async);
closure = g_slice_new0 (InitClosure);
closure->cancellable = cancellable ? g_object_ref (cancellable) : NULL;
g_simple_async_result_set_op_res_gpointer (res, closure, init_closure_free);
secret_collection_async_initable_parent_iface->init_async (initable, io_priority, secret_collection_async_initable_parent_iface->init_async (initable,
io_priority,
cancellable, cancellable,
on_init_base, on_init_base,
g_object_ref (res)); g_steal_pointer (&task));
g_object_unref (res); g_clear_object (&task);
} }
static gboolean static gboolean
@ -707,11 +687,13 @@ secret_collection_async_initable_init_finish (GAsyncInitable *initable,
{ {
SecretCollection *self = SECRET_COLLECTION (initable); SecretCollection *self = SECRET_COLLECTION (initable);
g_return_val_if_fail (g_simple_async_result_is_valid (result, G_OBJECT (initable), g_return_val_if_fail (g_task_is_valid (result, initable), FALSE);
secret_collection_async_initable_init_async), FALSE);
if (_secret_util_propagate_error (G_SIMPLE_ASYNC_RESULT (result), error)) // XXX maybe we still need to ref?
if (!g_task_propagate_pointer (G_TASK (result), error)) {
_secret_util_strip_remote_error (error);
return FALSE; return FALSE;
}
self->pv->constructing = FALSE; self->pv->constructing = FALSE;
return TRUE; return TRUE;
@ -727,7 +709,6 @@ secret_collection_async_initable_iface (GAsyncInitableIface *iface)
} }
typedef struct { typedef struct {
GCancellable *cancellable;
GHashTable *items; GHashTable *items;
gint items_loading; gint items_loading;
} ItemsClosure; } ItemsClosure;
@ -736,7 +717,6 @@ static void
items_closure_free (gpointer data) items_closure_free (gpointer data)
{ {
ItemsClosure *closure = data; ItemsClosure *closure = data;
g_clear_object (&closure->cancellable);
g_hash_table_unref (closure->items); g_hash_table_unref (closure->items);
g_slice_free (ItemsClosure, closure); g_slice_free (ItemsClosure, closure);
} }
@ -746,9 +726,9 @@ on_load_item (GObject *source,
GAsyncResult *result, GAsyncResult *result,
gpointer user_data) gpointer user_data)
{ {
GSimpleAsyncResult *res = G_SIMPLE_ASYNC_RESULT (user_data); GTask *task = G_TASK (user_data);
ItemsClosure *closure = g_simple_async_result_get_op_res_gpointer (res); ItemsClosure *closure = g_task_get_task_data (task);
SecretCollection *self = SECRET_COLLECTION (g_async_result_get_source_object (user_data)); SecretCollection *self = SECRET_COLLECTION (g_task_get_source_object (task));
const gchar *path; const gchar *path;
GError *error = NULL; GError *error = NULL;
SecretItem *item; SecretItem *item;
@ -757,8 +737,11 @@ on_load_item (GObject *source,
item = secret_item_new_for_dbus_path_finish (result, &error); item = secret_item_new_for_dbus_path_finish (result, &error);
if (error != NULL) if (error != NULL) {
g_simple_async_result_take_error (res, error); g_task_return_error (task, g_steal_pointer (&error));
g_clear_object (&task);
return;
}
if (item != NULL) { if (item != NULL) {
path = g_dbus_proxy_get_object_path (G_DBUS_PROXY (item)); path = g_dbus_proxy_get_object_path (G_DBUS_PROXY (item));
@ -767,11 +750,10 @@ on_load_item (GObject *source,
if (closure->items_loading == 0) { if (closure->items_loading == 0) {
collection_update_items (self, closure->items); collection_update_items (self, closure->items);
g_simple_async_result_complete_in_idle (res); g_task_return_boolean (task, TRUE);
} }
g_object_unref (self); g_clear_object (&task);
g_object_unref (res);
} }
/** /**
@ -798,7 +780,7 @@ secret_collection_load_items (SecretCollection *self,
{ {
ItemsClosure *closure; ItemsClosure *closure;
SecretItem *item; SecretItem *item;
GSimpleAsyncResult *res; GTask *task;
const gchar *path; const gchar *path;
GVariant *paths; GVariant *paths;
GVariantIter iter; GVariantIter iter;
@ -809,12 +791,11 @@ secret_collection_load_items (SecretCollection *self,
paths = g_dbus_proxy_get_cached_property (G_DBUS_PROXY (self), "Items"); paths = g_dbus_proxy_get_cached_property (G_DBUS_PROXY (self), "Items");
g_return_if_fail (paths != NULL); g_return_if_fail (paths != NULL);
res = g_simple_async_result_new (G_OBJECT (self), callback, user_data, task = g_task_new (self, cancellable, callback, user_data);
secret_collection_load_items); g_task_set_source_tag (task, secret_collection_load_items);
closure = g_slice_new0 (ItemsClosure); closure = g_slice_new0 (ItemsClosure);
closure->cancellable = cancellable ? g_object_ref (cancellable) : NULL;
closure->items = items_table_new (); closure->items = items_table_new ();
g_simple_async_result_set_op_res_gpointer (res, closure, items_closure_free); g_task_set_task_data (task, closure, items_closure_free);
g_variant_iter_init (&iter, paths); g_variant_iter_init (&iter, paths);
while (g_variant_iter_loop (&iter, "&o", &path)) { while (g_variant_iter_loop (&iter, "&o", &path)) {
@ -822,8 +803,10 @@ secret_collection_load_items (SecretCollection *self,
/* No such collection yet create a new one */ /* No such collection yet create a new one */
if (item == NULL) { if (item == NULL) {
secret_item_new_for_dbus_path (self->pv->service, path, SECRET_ITEM_NONE, secret_item_new_for_dbus_path (self->pv->service, path,
cancellable, on_load_item, g_object_ref (res)); SECRET_ITEM_NONE,
cancellable, on_load_item,
g_object_ref (task));
closure->items_loading++; closure->items_loading++;
} else { } else {
@ -833,11 +816,11 @@ secret_collection_load_items (SecretCollection *self,
if (closure->items_loading == 0) { if (closure->items_loading == 0) {
collection_update_items (self, closure->items); collection_update_items (self, closure->items);
g_simple_async_result_complete_in_idle (res); g_task_return_boolean (task, TRUE);
} }
g_variant_unref (paths); g_variant_unref (paths);
g_object_unref (res); g_clear_object (&task);
} }
/** /**
@ -858,11 +841,12 @@ secret_collection_load_items_finish (SecretCollection *self,
{ {
g_return_val_if_fail (SECRET_IS_COLLECTION (self), FALSE); g_return_val_if_fail (SECRET_IS_COLLECTION (self), FALSE);
g_return_val_if_fail (error == NULL || *error == NULL, FALSE); g_return_val_if_fail (error == NULL || *error == NULL, FALSE);
g_return_val_if_fail (g_simple_async_result_is_valid (result, G_OBJECT (self), g_return_val_if_fail (g_task_is_valid (result, self), FALSE);
secret_collection_load_items), FALSE);
if (_secret_util_propagate_error (G_SIMPLE_ASYNC_RESULT (result), error)) if (!g_task_propagate_boolean (G_TASK (result), error)) {
_secret_util_strip_remote_error (error);
return FALSE; return FALSE;
}
return TRUE; return TRUE;
} }
@ -953,7 +937,6 @@ secret_collection_refresh (SecretCollection *self)
} }
typedef struct { typedef struct {
GCancellable *cancellable;
SecretCollection *collection; SecretCollection *collection;
GHashTable *properties; GHashTable *properties;
gchar *alias; gchar *alias;
@ -964,7 +947,6 @@ static void
create_closure_free (gpointer data) create_closure_free (gpointer data)
{ {
CreateClosure *closure = data; CreateClosure *closure = data;
g_clear_object (&closure->cancellable);
g_clear_object (&closure->collection); g_clear_object (&closure->collection);
g_hash_table_unref (closure->properties); g_hash_table_unref (closure->properties);
g_free (closure->alias); g_free (closure->alias);
@ -976,16 +958,17 @@ on_create_collection (GObject *source,
GAsyncResult *result, GAsyncResult *result,
gpointer user_data) gpointer user_data)
{ {
GSimpleAsyncResult *res = G_SIMPLE_ASYNC_RESULT (user_data); GTask *task = G_TASK (user_data);
CreateClosure *closure = g_simple_async_result_get_op_res_gpointer (res); SecretCollection *collection;
GError *error = NULL; GError *error = NULL;
closure->collection = secret_collection_new_for_dbus_path_finish (result, &error); collection = secret_collection_new_for_dbus_path_finish (result, &error);
if (error != NULL) if (error != NULL)
g_simple_async_result_take_error (res, error); g_task_return_error (task, g_steal_pointer (&error));
else
g_task_return_pointer (task, collection, g_object_unref);
g_simple_async_result_complete (res); g_clear_object (&task);
g_object_unref (res);
} }
static void static void
@ -993,8 +976,8 @@ on_create_path (GObject *source,
GAsyncResult *result, GAsyncResult *result,
gpointer user_data) gpointer user_data)
{ {
GSimpleAsyncResult *res = G_SIMPLE_ASYNC_RESULT (user_data); GTask *task = G_TASK (user_data);
CreateClosure *closure = g_simple_async_result_get_op_res_gpointer (res); GCancellable *cancellable = g_task_get_cancellable (task);
SecretService *service = SECRET_SERVICE (source); SecretService *service = SECRET_SERVICE (source);
GError *error = NULL; GError *error = NULL;
gchar *path; gchar *path;
@ -1002,15 +985,14 @@ on_create_path (GObject *source,
path = secret_service_create_collection_dbus_path_finish (service, result, &error); path = secret_service_create_collection_dbus_path_finish (service, result, &error);
if (error == NULL) { if (error == NULL) {
secret_collection_new_for_dbus_path (service, path, SECRET_COLLECTION_LOAD_ITEMS, secret_collection_new_for_dbus_path (service, path, SECRET_COLLECTION_LOAD_ITEMS,
closure->cancellable, cancellable, on_create_collection,
on_create_collection, g_object_ref (res)); g_steal_pointer (&task));
} else { } else {
g_simple_async_result_take_error (res, error); g_task_return_error (task, g_steal_pointer (&error));
g_simple_async_result_complete (res);
} }
g_object_unref (res);
g_free (path); g_free (path);
g_clear_object (&task);
} }
static void static void
@ -1018,8 +1000,9 @@ on_create_service (GObject *source,
GAsyncResult *result, GAsyncResult *result,
gpointer user_data) gpointer user_data)
{ {
GSimpleAsyncResult *async = G_SIMPLE_ASYNC_RESULT (user_data); GTask *task = G_TASK (user_data);
CreateClosure *create = g_simple_async_result_get_op_res_gpointer (async); CreateClosure *create = g_task_get_task_data (task);
GCancellable *cancellable = g_task_get_cancellable (task);
GError *error = NULL; GError *error = NULL;
SecretService *service; SecretService *service;
@ -1027,16 +1010,15 @@ on_create_service (GObject *source,
if (error == NULL) { if (error == NULL) {
secret_service_create_collection_dbus_path (service, create->properties, secret_service_create_collection_dbus_path (service, create->properties,
create->alias, create->flags, create->alias, create->flags,
create->cancellable, cancellable, on_create_path,
on_create_path, g_object_ref (async)); g_steal_pointer (&task));
g_object_unref (service); g_object_unref (service);
} else { } else {
g_simple_async_result_take_error (async, error); g_task_return_error (task, g_steal_pointer (&error));
g_simple_async_result_complete (async);
} }
g_object_unref (async); g_clear_object (&task);
} }
GHashTable * GHashTable *
@ -1079,7 +1061,6 @@ _secret_collection_properties_new (const gchar *label)
* *
* If @service is %NULL, then secret_service_get() will be called to get * If @service is %NULL, then secret_service_get() will be called to get
* the default #SecretService proxy. * the default #SecretService proxy.
*
*/ */
void void
secret_collection_create (SecretService *service, secret_collection_create (SecretService *service,
@ -1090,34 +1071,33 @@ secret_collection_create (SecretService *service,
GAsyncReadyCallback callback, GAsyncReadyCallback callback,
gpointer user_data) gpointer user_data)
{ {
GSimpleAsyncResult *res; GTask *task;
CreateClosure *closure; CreateClosure *closure;
g_return_if_fail (service == NULL || SECRET_IS_SERVICE (service)); g_return_if_fail (service == NULL || SECRET_IS_SERVICE (service));
g_return_if_fail (label != NULL); g_return_if_fail (label != NULL);
g_return_if_fail (cancellable == NULL || G_IS_CANCELLABLE (cancellable)); g_return_if_fail (cancellable == NULL || G_IS_CANCELLABLE (cancellable));
res = g_simple_async_result_new (NULL, callback, user_data, task = g_task_new (NULL, cancellable, callback, user_data);
secret_collection_create); g_task_set_source_tag (task, secret_collection_create);
closure = g_slice_new0 (CreateClosure); closure = g_slice_new0 (CreateClosure);
closure->cancellable = cancellable ? g_object_ref (cancellable) : NULL;
closure->properties = _secret_collection_properties_new (label); closure->properties = _secret_collection_properties_new (label);
closure->alias = g_strdup (alias); closure->alias = g_strdup (alias);
closure->flags = flags; closure->flags = flags;
g_simple_async_result_set_op_res_gpointer (res, closure, create_closure_free); g_task_set_task_data (task, closure, create_closure_free);
if (service == NULL) { if (service == NULL) {
secret_service_get (SECRET_SERVICE_NONE, cancellable, secret_service_get (SECRET_SERVICE_NONE, cancellable,
on_create_service, g_object_ref (res)); on_create_service, g_steal_pointer (&task));
} else { } else {
secret_service_create_collection_dbus_path (service, closure->properties, secret_service_create_collection_dbus_path (service, closure->properties,
closure->alias, closure->flags, closure->alias, closure->flags,
closure->cancellable, cancellable, on_create_path,
on_create_path, g_object_ref (res)); g_steal_pointer (&task));
} }
g_object_unref (res); g_clear_object (&task);
} }
/** /**
@ -1134,23 +1114,18 @@ SecretCollection *
secret_collection_create_finish (GAsyncResult *result, secret_collection_create_finish (GAsyncResult *result,
GError **error) GError **error)
{ {
GSimpleAsyncResult *res; SecretCollection *collection;
CreateClosure *closure;
g_return_val_if_fail (g_simple_async_result_is_valid (result, NULL, g_return_val_if_fail (g_task_is_valid (result, NULL), NULL);
secret_collection_create), NULL);
g_return_val_if_fail (error == NULL || *error == NULL, NULL); g_return_val_if_fail (error == NULL || *error == NULL, NULL);
res = G_SIMPLE_ASYNC_RESULT (result); collection = g_task_propagate_pointer (G_TASK (result), error);
if (!collection) {
if (_secret_util_propagate_error (res, error)) _secret_util_strip_remote_error (error);
return NULL; return NULL;
}
closure = g_simple_async_result_get_op_res_gpointer (res); return collection;
if (closure->collection == NULL)
return NULL;
return g_object_ref (closure->collection);
} }
/** /**
@ -1229,7 +1204,6 @@ secret_collection_create_sync (SecretService *service,
typedef struct { typedef struct {
SecretCollection *collection; SecretCollection *collection;
GCancellable *cancellable;
GHashTable *items; GHashTable *items;
gchar **paths; gchar **paths;
guint loading; guint loading;
@ -1241,7 +1215,6 @@ search_closure_free (gpointer data)
{ {
SearchClosure *closure = data; SearchClosure *closure = data;
g_object_unref (closure->collection); g_object_unref (closure->collection);
g_clear_object (&closure->cancellable);
g_hash_table_unref (closure->items); g_hash_table_unref (closure->items);
g_strfreev (closure->paths); g_strfreev (closure->paths);
g_slice_free (SearchClosure, closure); g_slice_free (SearchClosure, closure);
@ -1260,13 +1233,13 @@ on_search_secrets (GObject *source,
GAsyncResult *result, GAsyncResult *result,
gpointer user_data) gpointer user_data)
{ {
GSimpleAsyncResult *async = G_SIMPLE_ASYNC_RESULT (user_data); GTask *task = G_TASK (user_data);
/* Note that we ignore any unlock failure */ /* Note that we ignore any unlock failure */
secret_item_load_secrets_finish (result, NULL); secret_item_load_secrets_finish (result, NULL);
g_simple_async_result_complete (async); g_task_return_boolean (task, TRUE);
g_object_unref (async); g_clear_object (&task);
} }
static void static void
@ -1274,8 +1247,9 @@ on_search_unlocked (GObject *source,
GAsyncResult *result, GAsyncResult *result,
gpointer user_data) gpointer user_data)
{ {
GSimpleAsyncResult *async = G_SIMPLE_ASYNC_RESULT (user_data); GTask *task = G_TASK (user_data);
SearchClosure *search = g_simple_async_result_get_op_res_gpointer (async); SearchClosure *search = g_task_get_task_data (task);
GCancellable *cancellable = g_task_get_cancellable (task);
GList *items; GList *items;
/* Note that we ignore any unlock failure */ /* Note that we ignore any unlock failure */
@ -1284,42 +1258,43 @@ on_search_unlocked (GObject *source,
/* If loading secrets ... locked items automatically ignored */ /* If loading secrets ... locked items automatically ignored */
if (search->flags & SECRET_SEARCH_LOAD_SECRETS) { if (search->flags & SECRET_SEARCH_LOAD_SECRETS) {
items = g_hash_table_get_values (search->items); items = g_hash_table_get_values (search->items);
secret_item_load_secrets (items, search->cancellable, secret_item_load_secrets (items, cancellable,
on_search_secrets, g_object_ref (async)); on_search_secrets, g_object_ref (task));
g_list_free (items); g_list_free (items);
/* No additional options, just complete */ /* No additional options, just complete */
} else { } else {
g_simple_async_result_complete (async); g_task_return_boolean (task, TRUE);
} }
g_object_unref (async); g_clear_object (&task);
} }
static void static void
secret_search_unlock_load_or_complete (GSimpleAsyncResult *async, secret_search_unlock_load_or_complete (GTask *task,
SearchClosure *search) SearchClosure *search)
{ {
GList *items; GList *items;
GCancellable *cancellable = g_task_get_cancellable (task);
/* If unlocking then unlock all the locked items */ /* If unlocking then unlock all the locked items */
if (search->flags & SECRET_SEARCH_UNLOCK) { if (search->flags & SECRET_SEARCH_UNLOCK) {
items = g_hash_table_get_values (search->items); items = g_hash_table_get_values (search->items);
secret_service_unlock (secret_collection_get_service (search->collection), secret_service_unlock (secret_collection_get_service (search->collection),
items, search->cancellable, items, cancellable,
on_search_unlocked, g_object_ref (async)); on_search_unlocked, g_object_ref (task));
g_list_free (items); g_list_free (items);
/* If loading secrets ... locked items automatically ignored */ /* If loading secrets ... locked items automatically ignored */
} else if (search->flags & SECRET_SEARCH_LOAD_SECRETS) { } else if (search->flags & SECRET_SEARCH_LOAD_SECRETS) {
items = g_hash_table_get_values (search->items); items = g_hash_table_get_values (search->items);
secret_item_load_secrets (items, search->cancellable, secret_item_load_secrets (items, cancellable,
on_search_secrets, g_object_ref (async)); on_search_secrets, g_object_ref (task));
g_list_free (items); g_list_free (items);
/* No additional options, just complete */ /* No additional options, just complete */
} else { } else {
g_simple_async_result_complete (async); g_task_return_boolean (task, TRUE);
} }
} }
@ -1328,25 +1303,28 @@ on_search_loaded (GObject *source,
GAsyncResult *result, GAsyncResult *result,
gpointer user_data) gpointer user_data)
{ {
GSimpleAsyncResult *async = G_SIMPLE_ASYNC_RESULT (user_data); GTask *task = G_TASK (user_data);
SearchClosure *search = g_simple_async_result_get_op_res_gpointer (async); SearchClosure *search = g_task_get_task_data (task);
GError *error = NULL; GError *error = NULL;
SecretItem *item; SecretItem *item;
search->loading--; search->loading--;
item = secret_item_new_for_dbus_path_finish (result, &error); item = secret_item_new_for_dbus_path_finish (result, &error);
if (error != NULL) if (error != NULL) {
g_simple_async_result_take_error (async, error); g_task_return_error (task, g_steal_pointer (&error));
g_clear_object (&task);
return;
}
if (item != NULL) if (item != NULL)
search_closure_take_item (search, item); search_closure_take_item (search, item);
/* We're done loading, lets go to the next step */ /* We're done loading, lets go to the next step */
if (search->loading == 0) if (search->loading == 0)
secret_search_unlock_load_or_complete (async, search); secret_search_unlock_load_or_complete (task, search);
g_object_unref (async); g_clear_object (&task);
} }
static void static void
@ -1354,8 +1332,9 @@ on_search_paths (GObject *source,
GAsyncResult *result, GAsyncResult *result,
gpointer user_data) gpointer user_data)
{ {
GSimpleAsyncResult *async = G_SIMPLE_ASYNC_RESULT (user_data); GTask *task = G_TASK (user_data);
SearchClosure *search = g_simple_async_result_get_op_res_gpointer (async); SearchClosure *search = g_task_get_task_data (task);
GCancellable *cancellable = g_task_get_cancellable (task);
SecretCollection *self = search->collection; SecretCollection *self = search->collection;
SecretService *service = secret_collection_get_service (self); SecretService *service = secret_collection_get_service (self);
GError *error = NULL; GError *error = NULL;
@ -1364,34 +1343,34 @@ on_search_paths (GObject *source,
gint i; gint i;
search->paths = secret_collection_search_for_dbus_paths_finish (self, result, &error); search->paths = secret_collection_search_for_dbus_paths_finish (self, result, &error);
if (error == NULL) { if (error != NULL) {
want = 1; g_task_return_error (task, g_steal_pointer (&error));
if (search->flags & SECRET_SEARCH_ALL) g_clear_object (&task);
want = G_MAXINT; return;
for (i = 0; i < want && search->paths[i] != NULL; i++) {
item = _secret_collection_find_item_instance (self, search->paths[i]);
if (item == NULL) {
secret_item_new_for_dbus_path (service, search->paths[i], SECRET_ITEM_NONE,
search->cancellable, on_search_loaded,
g_object_ref (async));
search->loading++;
} else {
search_closure_take_item (search, item);
}
}
/* No items loading, complete operation now */
if (search->loading == 0)
secret_search_unlock_load_or_complete (async, search);
} else {
g_simple_async_result_take_error (async, error);
g_simple_async_result_complete (async);
} }
g_object_unref (async); want = 1;
if (search->flags & SECRET_SEARCH_ALL)
want = G_MAXINT;
for (i = 0; i < want && search->paths[i] != NULL; i++) {
item = _secret_collection_find_item_instance (self, search->paths[i]);
if (item == NULL) {
secret_item_new_for_dbus_path (service, search->paths[i], SECRET_ITEM_NONE,
cancellable, on_search_loaded,
g_object_ref (task));
search->loading++;
} else {
search_closure_take_item (search, item);
}
}
/* No items loading, complete operation now */
if (search->loading == 0)
secret_search_unlock_load_or_complete (task, search);
g_clear_object (&task);
} }
/** /**
@ -1429,7 +1408,7 @@ secret_collection_search (SecretCollection *self,
GAsyncReadyCallback callback, GAsyncReadyCallback callback,
gpointer user_data) gpointer user_data)
{ {
GSimpleAsyncResult *async; GTask *task;
SearchClosure *search; SearchClosure *search;
g_return_if_fail (SECRET_IS_COLLECTION (self)); g_return_if_fail (SECRET_IS_COLLECTION (self));
@ -1440,20 +1419,19 @@ secret_collection_search (SecretCollection *self,
if (schema != NULL && !_secret_attributes_validate (schema, attributes, G_STRFUNC, TRUE)) if (schema != NULL && !_secret_attributes_validate (schema, attributes, G_STRFUNC, TRUE))
return; return;
async = g_simple_async_result_new (G_OBJECT (self), callback, user_data, task = g_task_new (self, cancellable, callback, user_data);
secret_collection_search); g_task_set_source_tag (task, secret_collection_search);
search = g_slice_new0 (SearchClosure); search = g_slice_new0 (SearchClosure);
search->collection = g_object_ref (self); search->collection = g_object_ref (self);
search->cancellable = cancellable ? g_object_ref (cancellable) : NULL;
search->items = g_hash_table_new_full (g_str_hash, g_str_equal, NULL, g_object_unref); search->items = g_hash_table_new_full (g_str_hash, g_str_equal, NULL, g_object_unref);
search->flags = flags; search->flags = flags;
g_simple_async_result_set_op_res_gpointer (async, search, search_closure_free); g_task_set_task_data (task, search, search_closure_free);
secret_collection_search_for_dbus_paths (self, schema, attributes, secret_collection_search_for_dbus_paths (self, schema, attributes,
cancellable, on_search_paths, cancellable, on_search_paths,
g_object_ref (async)); g_steal_pointer (&task));
g_object_unref (async); g_clear_object (&task);
} }
/** /**
@ -1472,7 +1450,6 @@ secret_collection_search_finish (SecretCollection *self,
GAsyncResult *result, GAsyncResult *result,
GError **error) GError **error)
{ {
GSimpleAsyncResult *async;
SearchClosure *search; SearchClosure *search;
GList *items = NULL; GList *items = NULL;
SecretItem *item; SecretItem *item;
@ -1480,14 +1457,14 @@ secret_collection_search_finish (SecretCollection *self,
g_return_val_if_fail (SECRET_IS_COLLECTION (self), NULL); g_return_val_if_fail (SECRET_IS_COLLECTION (self), NULL);
g_return_val_if_fail (error == NULL || *error == NULL, NULL); g_return_val_if_fail (error == NULL || *error == NULL, NULL);
g_return_val_if_fail (g_simple_async_result_is_valid (result, G_OBJECT (self), g_return_val_if_fail (g_task_is_valid (result, self), NULL);
secret_collection_search), NULL);
async = G_SIMPLE_ASYNC_RESULT (result); if (!g_task_propagate_boolean (G_TASK (result), error)) {
if (_secret_util_propagate_error (async, error)) _secret_util_strip_remote_error (error);
return NULL; return NULL;
}
search = g_simple_async_result_get_op_res_gpointer (async); search = g_task_get_task_data (G_TASK (result));
for (i = 0; search->paths[i]; i++) { for (i = 0; search->paths[i]; i++) {
item = g_hash_table_lookup (search->items, search->paths[i]); item = g_hash_table_lookup (search->items, search->paths[i]);
@ -1613,15 +1590,18 @@ on_service_delete_path (GObject *source,
GAsyncResult *result, GAsyncResult *result,
gpointer user_data) gpointer user_data)
{ {
GSimpleAsyncResult *async = G_SIMPLE_ASYNC_RESULT (user_data); GTask *task = G_TASK (user_data);
GError *error = NULL; GError *error = NULL;
_secret_service_delete_path_finish (SECRET_SERVICE (source), result, &error); _secret_service_delete_path_finish (SECRET_SERVICE (source), result, &error);
if (error != NULL) if (error != NULL)
g_simple_async_result_take_error (async, error); g_task_return_error (task, g_steal_pointer (&error));
g_simple_async_result_complete (async); else
g_object_unref (async); g_task_return_boolean (task, TRUE);
g_clear_object (&task);
} }
/** /**
* secret_collection_delete: * secret_collection_delete:
* @self: a collection * @self: a collection
@ -1641,21 +1621,21 @@ secret_collection_delete (SecretCollection *self,
GAsyncReadyCallback callback, GAsyncReadyCallback callback,
gpointer user_data) gpointer user_data)
{ {
GSimpleAsyncResult *async; GTask *task;
const gchar *object_path; const gchar *object_path;
g_return_if_fail (SECRET_IS_COLLECTION (self)); g_return_if_fail (SECRET_IS_COLLECTION (self));
g_return_if_fail (cancellable == NULL || G_IS_CANCELLABLE (cancellable)); g_return_if_fail (cancellable == NULL || G_IS_CANCELLABLE (cancellable));
async = g_simple_async_result_new (G_OBJECT (self), callback, user_data, task = g_task_new (self, cancellable, callback, user_data);
secret_collection_delete); g_task_set_source_tag (task, secret_collection_delete);
object_path = g_dbus_proxy_get_object_path (G_DBUS_PROXY (self)); object_path = g_dbus_proxy_get_object_path (G_DBUS_PROXY (self));
_secret_service_delete_path (self->pv->service, object_path, FALSE, _secret_service_delete_path (self->pv->service, object_path, FALSE,
cancellable, on_service_delete_path, cancellable, on_service_delete_path,
g_object_ref (async)); g_steal_pointer (&task));
g_object_unref (async); g_clear_object (&task);
} }
/** /**
@ -1675,11 +1655,12 @@ secret_collection_delete_finish (SecretCollection *self,
{ {
g_return_val_if_fail (SECRET_IS_COLLECTION (self), FALSE); g_return_val_if_fail (SECRET_IS_COLLECTION (self), FALSE);
g_return_val_if_fail (error == NULL || *error == NULL, FALSE); g_return_val_if_fail (error == NULL || *error == NULL, FALSE);
g_return_val_if_fail (g_simple_async_result_is_valid (result, G_OBJECT (self), g_return_val_if_fail (g_task_is_valid (result, self), FALSE);
secret_collection_delete), FALSE);
if (_secret_util_propagate_error (G_SIMPLE_ASYNC_RESULT (result), error)) if (!g_task_propagate_boolean (G_TASK (result), error)) {
_secret_util_strip_remote_error (error);
return FALSE; return FALSE;
}
return TRUE; return TRUE;
} }
@ -1997,10 +1978,8 @@ secret_collection_get_modified (SecretCollection *self)
typedef struct { typedef struct {
GCancellable *cancellable;
gchar *alias; gchar *alias;
SecretCollectionFlags flags; SecretCollectionFlags flags;
SecretCollection *collection;
} ReadClosure; } ReadClosure;
static void static void
@ -2008,10 +1987,6 @@ read_closure_free (gpointer data)
{ {
ReadClosure *read = data; ReadClosure *read = data;
g_free (read->alias); g_free (read->alias);
if (read->collection)
g_object_unref (read->collection);
if (read->cancellable)
g_object_unref (read->cancellable);
g_slice_free (ReadClosure, read); g_slice_free (ReadClosure, read);
} }
@ -2020,16 +1995,17 @@ on_read_alias_collection (GObject *source,
GAsyncResult *result, GAsyncResult *result,
gpointer user_data) gpointer user_data)
{ {
GSimpleAsyncResult *async = G_SIMPLE_ASYNC_RESULT (user_data); GTask *task = G_TASK (user_data);
ReadClosure *read = g_simple_async_result_get_op_res_gpointer (async); SecretCollection *collection;
GError *error = NULL; GError *error = NULL;
read->collection = secret_collection_new_for_dbus_path_finish (result, &error); collection = secret_collection_new_for_dbus_path_finish (result, &error);
if (error != NULL) if (error != NULL)
g_simple_async_result_take_error (async, error); g_task_return_error (task, g_steal_pointer (&error));
else
g_task_return_pointer (task, collection, g_object_unref);
g_simple_async_result_complete (async); g_clear_object (&task);
g_object_unref (async);
} }
static void static void
@ -2037,8 +2013,9 @@ on_read_alias_path (GObject *source,
GAsyncResult *result, GAsyncResult *result,
gpointer user_data) gpointer user_data)
{ {
GSimpleAsyncResult *async = G_SIMPLE_ASYNC_RESULT (user_data); GTask *task = G_TASK (user_data);
ReadClosure *read = g_simple_async_result_get_op_res_gpointer (async); ReadClosure *read = g_task_get_task_data (task);
GCancellable *cancellable = g_task_get_cancellable (task);
SecretService *self = SECRET_SERVICE (source); SecretService *self = SECRET_SERVICE (source);
GError *error = NULL; GError *error = NULL;
gchar *collection_path; gchar *collection_path;
@ -2048,34 +2025,35 @@ on_read_alias_path (GObject *source,
/* No collection for this alias */ /* No collection for this alias */
if (collection_path == NULL) { if (collection_path == NULL) {
g_simple_async_result_complete (async); g_task_return_pointer (task, NULL, NULL);
} else { } else {
read->collection = _secret_service_find_collection_instance (self, SecretCollection *collection;
collection_path);
if (read->collection != NULL) {
collection = _secret_service_find_collection_instance (self,
collection_path);
if (collection != NULL) {
/* Make sure collection has necessary flags */ /* Make sure collection has necessary flags */
collection_ensure_for_flags_async (read->collection, read->flags, collection_ensure_for_flags_async (collection,
read->cancellable, async); read->flags,
task);
/* No collection loaded, but valid path, load */ /* No collection loaded, but valid path, load */
} else { } else {
secret_collection_new_for_dbus_path (self, collection_path, secret_collection_new_for_dbus_path (self, collection_path,
read->flags, read->flags,
read->cancellable, cancellable,
on_read_alias_collection, on_read_alias_collection,
g_object_ref (async)); g_steal_pointer (&task));
} }
} }
} else { } else {
g_simple_async_result_take_error (async, error); g_task_return_error (task, g_steal_pointer (&error));
g_simple_async_result_complete (async);
} }
g_free (collection_path); g_free (collection_path);
g_object_unref (async); g_clear_object (&task);
} }
static void static void
@ -2083,23 +2061,24 @@ on_read_alias_service (GObject *source,
GAsyncResult *result, GAsyncResult *result,
gpointer user_data) gpointer user_data)
{ {
GSimpleAsyncResult *async = G_SIMPLE_ASYNC_RESULT (user_data); GTask *task = G_TASK (user_data);
ReadClosure *read = g_simple_async_result_get_op_res_gpointer (async); ReadClosure *read = g_task_get_task_data (task);
GCancellable *cancellable = g_task_get_cancellable (task);
SecretService *service; SecretService *service;
GError *error = NULL; GError *error = NULL;
service = secret_service_get_finish (result, &error); service = secret_service_get_finish (result, &error);
if (error == NULL) { if (error == NULL) {
secret_service_read_alias_dbus_path (service, read->alias, read->cancellable, secret_service_read_alias_dbus_path (service, read->alias, cancellable,
on_read_alias_path, g_object_ref (async)); on_read_alias_path,
g_steal_pointer (&task));
g_object_unref (service); g_object_unref (service);
} else { } else {
g_simple_async_result_take_error (async, error); g_task_return_error (task, g_steal_pointer (&error));
g_simple_async_result_complete (async);
} }
g_object_unref (async); g_clear_object (&task);
} }
/** /**
@ -2127,30 +2106,30 @@ secret_collection_for_alias (SecretService *service,
GAsyncReadyCallback callback, GAsyncReadyCallback callback,
gpointer user_data) gpointer user_data)
{ {
GSimpleAsyncResult *async; GTask *task;
ReadClosure *read; ReadClosure *read;
g_return_if_fail (service == NULL || SECRET_IS_SERVICE (service)); g_return_if_fail (service == NULL || SECRET_IS_SERVICE (service));
g_return_if_fail (alias != NULL); g_return_if_fail (alias != NULL);
g_return_if_fail (cancellable == NULL || G_IS_CANCELLABLE (cancellable)); g_return_if_fail (cancellable == NULL || G_IS_CANCELLABLE (cancellable));
async = g_simple_async_result_new (NULL, callback, user_data, task = g_task_new (NULL, cancellable, callback, user_data);
secret_collection_for_alias); g_task_set_source_tag (task, secret_collection_for_alias);
read = g_slice_new0 (ReadClosure); read = g_slice_new0 (ReadClosure);
read->cancellable = cancellable ? g_object_ref (cancellable) : NULL;
read->alias = g_strdup (alias); read->alias = g_strdup (alias);
read->flags = flags; read->flags = flags;
g_simple_async_result_set_op_res_gpointer (async, read, read_closure_free); g_task_set_task_data (task, read, read_closure_free);
if (service == NULL) { if (service == NULL) {
secret_service_get (SECRET_SERVICE_NONE, cancellable, secret_service_get (SECRET_SERVICE_NONE, cancellable,
on_read_alias_service, g_object_ref (async)); on_read_alias_service, g_object_ref (task));
} else { } else {
secret_service_read_alias_dbus_path (service, read->alias, read->cancellable, secret_service_read_alias_dbus_path (service, read->alias, cancellable,
on_read_alias_path, g_object_ref (async)); on_read_alias_path,
g_steal_pointer (&task));
} }
g_object_unref (async); g_clear_object (&task);
} }
/** /**
@ -2167,20 +2146,18 @@ SecretCollection *
secret_collection_for_alias_finish (GAsyncResult *result, secret_collection_for_alias_finish (GAsyncResult *result,
GError **error) GError **error)
{ {
GSimpleAsyncResult *async; SecretCollection *collection;
ReadClosure *read;
g_return_val_if_fail (g_simple_async_result_is_valid (result, NULL, g_return_val_if_fail (g_task_is_valid (result, NULL), NULL);
secret_collection_for_alias), NULL);
g_return_val_if_fail (error == NULL || *error == NULL, NULL); g_return_val_if_fail (error == NULL || *error == NULL, NULL);
async = G_SIMPLE_ASYNC_RESULT (result); collection = g_task_propagate_pointer (G_TASK (result), error);
if (_secret_util_propagate_error (async, error)) if (error && *error) {
_secret_util_strip_remote_error (error);
return NULL; return NULL;
read = g_simple_async_result_get_op_res_gpointer (async); }
if (read->collection)
g_object_ref (read->collection); return collection;
return read->collection;
} }
/** /**