[libgdata] gdata: Port from GSimpleAsyncResult to GTask



commit 990055474e32efb4d5be71781c8feec67191e0a4
Author: Philip Withnall <withnall endlessm com>
Date:   Wed Aug 9 10:21:10 2017 +0100

    gdata: Port from GSimpleAsyncResult to GTask
    
    Finish porting the OAuth 1 code, port the OAuth 2 code and also
    GDataCommentable.
    
    Signed-off-by: Philip Withnall <withnall endlessm com>

 gdata/gdata-commentable.c       |  150 +++++++++++++++------------------------
 gdata/gdata-oauth1-authorizer.c |   41 ++++-------
 gdata/gdata-oauth2-authorizer.c |   61 ++++++----------
 3 files changed, 95 insertions(+), 157 deletions(-)
---
diff --git a/gdata/gdata-commentable.c b/gdata/gdata-commentable.c
index 85bec2f..e46630f 100644
--- a/gdata/gdata-commentable.c
+++ b/gdata/gdata-commentable.c
@@ -186,23 +186,18 @@ gdata_commentable_query_comments (GDataCommentable *self, GDataService *service,
 }
 
 static void
-query_comments_async_cb (GDataService *service, GAsyncResult *service_result, GSimpleAsyncResult 
*commentable_result)
+query_comments_async_cb (GDataService *service, GAsyncResult *service_result, gpointer user_data)
 {
-       GDataFeed *feed;
-       GError *error = NULL;
+       g_autoptr(GTask) commentable_task = G_TASK (user_data);
+       g_autoptr(GDataFeed) feed = NULL;
+       g_autoptr(GError) error = NULL;
 
        feed = gdata_service_query_finish (service, service_result, &error);
 
-       if (error != NULL) {
-               /* Error. */
-               g_simple_async_result_take_error (commentable_result, error);
-       } else {
-               g_simple_async_result_set_op_res_gpointer (commentable_result, g_object_ref (feed), 
(GDestroyNotify) g_object_unref);
-       }
-
-       g_simple_async_result_complete (commentable_result);
-
-       g_object_unref (commentable_result);
+       if (error != NULL)
+               g_task_return_error (commentable_task, g_steal_pointer (&error));
+       else
+               g_task_return_pointer (commentable_task, g_steal_pointer (&feed), g_object_unref);
 }
 
 /**
@@ -236,7 +231,7 @@ gdata_commentable_query_comments_async (GDataCommentable *self, GDataService *se
 {
        GDataCommentableInterface *iface;
        gchar *uri;
-       GSimpleAsyncResult *result;
+       g_autoptr(GTask) task = NULL;
        GDataAuthorizationDomain *domain = NULL;
 
        g_return_if_fail (GDATA_IS_COMMENTABLE (self));
@@ -252,16 +247,15 @@ gdata_commentable_query_comments_async (GDataCommentable *self, GDataService *se
        uri = iface->get_query_comments_uri (self);
 
        /* Build the async result. */
-       result = g_simple_async_result_new (G_OBJECT (self), callback, user_data, 
gdata_commentable_query_comments_async);
+       task = g_task_new (self, cancellable, callback, user_data);
+       g_task_set_source_tag (task, gdata_commentable_query_comments_async);
 
        /* The URI can be NULL when no comments and thus no feedLink is present in a GDataCommentable */
        if (uri == NULL) {
-               g_simple_async_result_set_error (result, GDATA_SERVICE_ERROR, GDATA_SERVICE_ERROR_FORBIDDEN,
-                                                /* Translators: This is an error message for if a user 
attempts to retrieve comments from an entry
-                                                 * (such as a video) which doesn't support comments. */
-                                                _("This entry does not support comments."));
-               g_simple_async_result_complete_in_idle (result);
-               g_object_unref (result);
+               g_task_return_new_error (task, GDATA_SERVICE_ERROR, GDATA_SERVICE_ERROR_FORBIDDEN,
+                                        /* Translators: This is an error message for if a user attempts to 
retrieve comments from an entry
+                                         * (such as a video) which doesn't support comments. */
+                                        _("This entry does not support comments."));
                return;
        }
 
@@ -272,7 +266,7 @@ gdata_commentable_query_comments_async (GDataCommentable *self, GDataService *se
 
        /* Get the comment feed. */
        gdata_service_query_async (service, domain, uri, query, get_comment_type (iface), cancellable, 
progress_callback, progress_user_data,
-                                  destroy_progress_user_data, (GAsyncReadyCallback) query_comments_async_cb, 
result);
+                                  destroy_progress_user_data, (GAsyncReadyCallback) query_comments_async_cb, 
g_object_ref (task));
        g_free (uri);
 }
 
@@ -294,15 +288,10 @@ gdata_commentable_query_comments_finish (GDataCommentable *self, GAsyncResult *r
        g_return_val_if_fail (GDATA_IS_COMMENTABLE (self), NULL);
        g_return_val_if_fail (G_IS_ASYNC_RESULT (result), NULL);
        g_return_val_if_fail (error == NULL || *error == NULL, NULL);
+       g_return_val_if_fail (g_task_is_valid (result, self), NULL);
+       g_return_val_if_fail (g_async_result_is_tagged (result, gdata_commentable_query_comments_async), 
NULL);
 
-       g_return_val_if_fail (g_simple_async_result_is_valid (result, G_OBJECT (self), 
gdata_commentable_query_comments_async) == TRUE, NULL);
-
-       if (g_simple_async_result_propagate_error (G_SIMPLE_ASYNC_RESULT (result), error) == TRUE) {
-               /* Error. */
-               return NULL;
-       }
-
-       return GDATA_FEED (g_simple_async_result_get_op_res_gpointer (G_SIMPLE_ASYNC_RESULT (result)));
+       return g_task_propagate_pointer (G_TASK (result), error);
 }
 
 /**
@@ -364,23 +353,18 @@ gdata_commentable_insert_comment (GDataCommentable *self, GDataService *service,
 }
 
 static void
-insert_comment_async_cb (GDataService *service, GAsyncResult *service_result, GSimpleAsyncResult 
*commentable_result)
+insert_comment_async_cb (GDataService *service, GAsyncResult *service_result, gpointer user_data)
 {
-       GDataEntry *new_comment;
-       GError *error = NULL;
+       g_autoptr(GTask) commentable_task = G_TASK (user_data);
+       g_autoptr(GDataEntry) new_comment = NULL;
+       g_autoptr(GError) error = NULL;
 
        new_comment = gdata_service_insert_entry_finish (service, service_result, &error);
 
-       if (error != NULL) {
-               /* Error. */
-               g_simple_async_result_take_error (commentable_result, error);
-       } else {
-               g_simple_async_result_set_op_res_gpointer (commentable_result, g_object_ref (new_comment), 
(GDestroyNotify) g_object_unref);
-       }
-
-       g_simple_async_result_complete (commentable_result);
-
-       g_object_unref (commentable_result);
+       if (error != NULL)
+               g_task_return_error (commentable_task, g_steal_pointer (&error));
+       else
+               g_task_return_pointer (commentable_task, g_steal_pointer (&new_comment), g_object_unref);
 }
 
 /**
@@ -407,8 +391,8 @@ gdata_commentable_insert_comment_async (GDataCommentable *self, GDataService *se
                                         GAsyncReadyCallback callback, gpointer user_data)
 {
        GDataCommentableInterface *iface;
-       gchar *uri;
-       GSimpleAsyncResult *result;
+       g_autofree gchar *uri = NULL;
+       g_autoptr(GTask) task = NULL;
        GDataAuthorizationDomain *domain = NULL;
 
        g_return_if_fail (GDATA_IS_COMMENTABLE (self));
@@ -425,16 +409,15 @@ gdata_commentable_insert_comment_async (GDataCommentable *self, GDataService *se
        uri = iface->get_insert_comment_uri (self, comment_);
 
        /* Build the async result. */
-       result = g_simple_async_result_new (G_OBJECT (self), callback, user_data, 
gdata_commentable_insert_comment_async);
+       task = g_task_new (self, cancellable, callback, user_data);
+       g_task_set_source_tag (task, gdata_commentable_insert_comment_async);
 
        /* The URI can be NULL when no comments and thus no feedLink is present in a GDataCommentable */
        if (uri == NULL) {
-               g_simple_async_result_set_error (result, GDATA_SERVICE_ERROR, GDATA_SERVICE_ERROR_FORBIDDEN,
-                                                /* Translators: This is an error message for if a user 
attempts to add a comment to an entry
-                                                 * (such as a video) which doesn't support comments. */
-                                                _("Comments may not be added to this entry."));
-               g_simple_async_result_complete_in_idle (result);
-               g_object_unref (result);
+               g_task_return_new_error (task, GDATA_SERVICE_ERROR, GDATA_SERVICE_ERROR_FORBIDDEN,
+                                        /* Translators: This is an error message for if a user attempts to 
add a comment to an entry
+                                         * (such as a video) which doesn't support comments. */
+                                        _("Comments may not be added to this entry."));
                return;
        }
 
@@ -445,9 +428,7 @@ gdata_commentable_insert_comment_async (GDataCommentable *self, GDataService *se
 
        /* Add the comment. */
        gdata_service_insert_entry_async (service, domain, uri, GDATA_ENTRY (comment_), cancellable, 
(GAsyncReadyCallback) insert_comment_async_cb,
-                                         result);
-
-       g_free (uri);
+                                         g_object_ref (task));
 }
 
 /**
@@ -468,15 +449,10 @@ gdata_commentable_insert_comment_finish (GDataCommentable *self, GAsyncResult *r
        g_return_val_if_fail (GDATA_IS_COMMENTABLE (self), NULL);
        g_return_val_if_fail (G_IS_ASYNC_RESULT (result), NULL);
        g_return_val_if_fail (error == NULL || *error == NULL, NULL);
+       g_return_val_if_fail (g_task_is_valid (result, self), NULL);
+       g_return_val_if_fail (g_async_result_is_tagged (result, gdata_commentable_insert_comment_async), 
NULL);
 
-       g_return_val_if_fail (g_simple_async_result_is_valid (result, G_OBJECT (self), 
gdata_commentable_insert_comment_async) == TRUE, NULL);
-
-       if (g_simple_async_result_propagate_error (G_SIMPLE_ASYNC_RESULT (result), error) == TRUE) {
-               /* Error. */
-               return NULL;
-       }
-
-       return GDATA_COMMENT (g_simple_async_result_get_op_res_gpointer (G_SIMPLE_ASYNC_RESULT (result)));
+       return g_task_propagate_pointer (G_TASK (result), error);
 }
 
 /**
@@ -529,23 +505,15 @@ gdata_commentable_delete_comment (GDataCommentable *self, GDataService *service,
 }
 
 static void
-delete_comment_async_cb (GDataService *service, GAsyncResult *service_result, GSimpleAsyncResult 
*commentable_result)
+delete_comment_async_cb (GDataService *service, GAsyncResult *service_result, gpointer user_data)
 {
-       gboolean success;
-       GError *error = NULL;
-
-       success = gdata_service_delete_entry_finish (service, service_result, &error);
+       g_autoptr(GTask) commentable_task = G_TASK (user_data);
+       g_autoptr(GError) error = NULL;
 
-       if (error != NULL) {
-               /* Error. */
-               g_simple_async_result_take_error (commentable_result, error);
-       } else {
-               g_simple_async_result_set_op_res_gboolean (commentable_result, success);
-       }
-
-       g_simple_async_result_complete (commentable_result);
-
-       g_object_unref (commentable_result);
+       if (!gdata_service_delete_entry_finish (service, service_result, &error))
+               g_task_return_error (commentable_task, g_steal_pointer (&error));
+       else
+               g_task_return_boolean (commentable_task, TRUE);
 }
 
 /**
@@ -572,7 +540,7 @@ gdata_commentable_delete_comment_async (GDataCommentable *self, GDataService *se
                                         GAsyncReadyCallback callback, gpointer user_data)
 {
        GDataCommentableInterface *iface;
-       GSimpleAsyncResult *result;
+       g_autoptr(GTask) task = NULL;
        GDataAuthorizationDomain *domain = NULL;
 
        g_return_if_fail (GDATA_IS_COMMENTABLE (self));
@@ -585,15 +553,14 @@ gdata_commentable_delete_comment_async (GDataCommentable *self, GDataService *se
        g_return_if_fail (g_type_is_a (G_OBJECT_TYPE (comment_), get_comment_type (iface)) == TRUE);
 
        /* Build the async result. */
-       result = g_simple_async_result_new (G_OBJECT (self), callback, user_data, 
gdata_commentable_delete_comment_async);
+       task = g_task_new (self, cancellable, callback, user_data);
+       g_task_set_source_tag (task, gdata_commentable_delete_comment_async);
 
        g_assert (iface->is_comment_deletable != NULL);
        if (iface->is_comment_deletable (self, comment_) == FALSE) {
-               g_simple_async_result_set_error (result, GDATA_SERVICE_ERROR, GDATA_SERVICE_ERROR_FORBIDDEN,
-                                                /* Translators: This is an error message for if a user 
attempts to delete a comment they're not allowed to delete. */
-                                                _("This comment may not be deleted."));
-               g_simple_async_result_complete_in_idle (result);
-               g_object_unref (result);
+               g_task_return_new_error (task, GDATA_SERVICE_ERROR, GDATA_SERVICE_ERROR_FORBIDDEN,
+                                        /* Translators: This is an error message for if a user attempts to 
delete a comment they're not allowed to delete. */
+                                        _("This comment may not be deleted."));
                return;
        }
 
@@ -603,7 +570,7 @@ gdata_commentable_delete_comment_async (GDataCommentable *self, GDataService *se
        }
 
        /* Delete the comment. */
-       gdata_service_delete_entry_async (service, domain, GDATA_ENTRY (comment_), cancellable, 
(GAsyncReadyCallback) delete_comment_async_cb, result);
+       gdata_service_delete_entry_async (service, domain, GDATA_ENTRY (comment_), cancellable, 
(GAsyncReadyCallback) delete_comment_async_cb, g_object_ref (task));
 }
 
 /**
@@ -624,13 +591,8 @@ gdata_commentable_delete_comment_finish (GDataCommentable *self, GAsyncResult *r
        g_return_val_if_fail (GDATA_IS_COMMENTABLE (self), FALSE);
        g_return_val_if_fail (G_IS_ASYNC_RESULT (result), FALSE);
        g_return_val_if_fail (error == NULL || *error == NULL, FALSE);
+       g_return_val_if_fail (g_task_is_valid (result, self), FALSE);
+       g_return_val_if_fail (g_async_result_is_tagged (result, gdata_commentable_delete_comment_async), 
FALSE);
 
-       g_return_val_if_fail (g_simple_async_result_is_valid (result, G_OBJECT (self), 
gdata_commentable_delete_comment_async) == TRUE, FALSE);
-
-       if (g_simple_async_result_propagate_error (G_SIMPLE_ASYNC_RESULT (result), error) == TRUE) {
-               /* Error. */
-               return FALSE;
-       }
-
-       return g_simple_async_result_get_op_res_gboolean (G_SIMPLE_ASYNC_RESULT (result));
+       return g_task_propagate_boolean (G_TASK (result), error);
 }
diff --git a/gdata/gdata-oauth1-authorizer.c b/gdata/gdata-oauth1-authorizer.c
index 8e8c5ff..8163408 100644
--- a/gdata/gdata-oauth1-authorizer.c
+++ b/gdata/gdata-oauth1-authorizer.c
@@ -1146,21 +1146,16 @@ request_authorization_async_data_free (RequestAuthorizationAsyncData *data)
 }
 
 static void
-request_authorization_thread (GSimpleAsyncResult *result, GDataOAuth1Authorizer *authorizer, GCancellable 
*cancellable)
+request_authorization_thread (GTask *task, gpointer source_object, gpointer task_data, GCancellable 
*cancellable)
 {
-       RequestAuthorizationAsyncData *data;
-       gboolean success;
-       GError *error = NULL;
-
-       data = g_simple_async_result_get_op_res_gpointer (result);
-
-       success = gdata_oauth1_authorizer_request_authorization (authorizer, data->token, data->token_secret, 
data->verifier, cancellable, &error);
-       g_simple_async_result_set_op_res_gboolean (result, success);
+       GDataOAuth1Authorizer *authorizer = GDATA_OAUTH1_AUTHORIZER (source_object);
+       RequestAuthorizationAsyncData *data = task_data;
+       g_autoptr(GError) error = NULL;
 
-       if (error != NULL) {
-               g_simple_async_result_set_from_error (result, error);
-               g_error_free (error);
-       }
+       if (!gdata_oauth1_authorizer_request_authorization (authorizer, data->token, data->token_secret, 
data->verifier, cancellable, &error))
+               g_task_return_error (task, g_steal_pointer (&error));
+       else
+               g_task_return_boolean (task, TRUE);
 }
 
 /**
@@ -1188,7 +1183,7 @@ gdata_oauth1_authorizer_request_authorization_async (GDataOAuth1Authorizer *self
                                                      const gchar *verifier,
                                                      GCancellable *cancellable, GAsyncReadyCallback 
callback, gpointer user_data)
 {
-       GSimpleAsyncResult *result;
+       g_autoptr(GTask) task = NULL;
        RequestAuthorizationAsyncData *data;
 
        g_return_if_fail (GDATA_IS_OAUTH1_AUTHORIZER (self));
@@ -1202,10 +1197,10 @@ gdata_oauth1_authorizer_request_authorization_async (GDataOAuth1Authorizer *self
        data->token_secret = _gdata_service_secure_strdup (token_secret);
        data->verifier = g_strdup (verifier);
 
-       result = g_simple_async_result_new (G_OBJECT (self), callback, user_data, 
gdata_oauth1_authorizer_request_authorization_async);
-       g_simple_async_result_set_op_res_gpointer (result, data, (GDestroyNotify) 
request_authorization_async_data_free);
-       g_simple_async_result_run_in_thread (result, (GSimpleAsyncThreadFunc) request_authorization_thread, 
G_PRIORITY_DEFAULT, cancellable);
-       g_object_unref (result);
+       task = g_task_new (self, cancellable, callback, user_data);
+       g_task_set_source_tag (task, gdata_oauth1_authorizer_request_authorization_async);
+       g_task_set_task_data (task, g_steal_pointer (&data), (GDestroyNotify) 
request_authorization_async_data_free);
+       g_task_run_in_thread (task, request_authorization_thread);
 }
 
 /**
@@ -1226,14 +1221,10 @@ gdata_oauth1_authorizer_request_authorization_finish (GDataOAuth1Authorizer *sel
        g_return_val_if_fail (GDATA_IS_OAUTH1_AUTHORIZER (self), FALSE);
        g_return_val_if_fail (G_IS_ASYNC_RESULT (async_result), FALSE);
        g_return_val_if_fail (error == NULL || *error == NULL, FALSE);
+       g_return_val_if_fail (g_task_is_valid (async_result, self), FALSE);
+       g_return_val_if_fail (g_async_result_is_tagged (async_result, 
gdata_oauth1_authorizer_request_authorization_async), FALSE);
 
-       g_warn_if_fail (g_simple_async_result_is_valid (async_result, G_OBJECT (self), 
gdata_oauth1_authorizer_request_authorization_async));
-
-       if (g_simple_async_result_propagate_error (G_SIMPLE_ASYNC_RESULT (async_result), error) == TRUE) {
-               return FALSE;
-       }
-
-       return TRUE;
+       return g_task_propagate_boolean (G_TASK (async_result), error);
 }
 
 /**
diff --git a/gdata/gdata-oauth2-authorizer.c b/gdata/gdata-oauth2-authorizer.c
index b9d5bed..a08a265 100644
--- a/gdata/gdata-oauth2-authorizer.c
+++ b/gdata/gdata-oauth2-authorizer.c
@@ -1232,22 +1232,22 @@ gdata_oauth2_authorizer_request_authorization (GDataOAuth2Authorizer *self,
 }
 
 static void
-request_authorization_thread (GSimpleAsyncResult *result,
-                              GDataOAuth2Authorizer *authorizer,
+request_authorization_thread (GTask        *task,
+                              gpointer      source_object,
+                              gpointer      task_data,
                               GCancellable *cancellable)
 {
-       GError *error = NULL;
-       const gchar *authorization_code;
-
-       authorization_code = g_simple_async_result_get_op_res_gpointer (result);
-
-       if (gdata_oauth2_authorizer_request_authorization (authorizer,
-                                                          authorization_code,
-                                                          cancellable,
-                                                          &error) == FALSE) {
-               g_simple_async_result_set_from_error (result, error);
-               g_error_free (error);
-       }
+       GDataOAuth2Authorizer *authorizer = GDATA_OAUTH2_AUTHORIZER (source_object);
+       g_autoptr(GError) error = NULL;
+       const gchar *authorization_code = task_data;
+
+       if (!gdata_oauth2_authorizer_request_authorization (authorizer,
+                                                           authorization_code,
+                                                           cancellable,
+                                                           &error))
+               g_task_return_error (task, g_steal_pointer (&error));
+       else
+               g_task_return_boolean (task, TRUE);
 }
 
 /**
@@ -1269,7 +1269,7 @@ gdata_oauth2_authorizer_request_authorization_async (GDataOAuth2Authorizer *self
                                                      GAsyncReadyCallback callback,
                                                      gpointer user_data)
 {
-       GSimpleAsyncResult *result;
+       g_autoptr(GTask) task = NULL;
 
        g_return_if_fail (GDATA_IS_OAUTH2_AUTHORIZER (self));
        g_return_if_fail (authorization_code != NULL &&
@@ -1277,16 +1277,11 @@ gdata_oauth2_authorizer_request_authorization_async (GDataOAuth2Authorizer *self
        g_return_if_fail (cancellable == NULL ||
                          G_IS_CANCELLABLE (cancellable));
 
-       result = g_simple_async_result_new (G_OBJECT (self), callback,
-                                           user_data,
-                                           gdata_oauth2_authorizer_request_authorization_async);
-       g_simple_async_result_set_op_res_gpointer (result,
-                                                  g_strdup (authorization_code),
-                                                  (GDestroyNotify) g_free);
-       g_simple_async_result_run_in_thread (result,
-                                            (GSimpleAsyncThreadFunc) request_authorization_thread,
-                                            G_PRIORITY_DEFAULT, cancellable);
-       g_object_unref (result);
+       task = g_task_new (self, cancellable, callback, user_data);
+       g_task_set_source_tag (task, gdata_oauth2_authorizer_request_authorization_async);
+       g_task_set_task_data (task, g_strdup (authorization_code),
+                             (GDestroyNotify) g_free);
+       g_task_run_in_thread (task, request_authorization_thread);
 }
 
 /**
@@ -1307,23 +1302,13 @@ gdata_oauth2_authorizer_request_authorization_finish (GDataOAuth2Authorizer *sel
                                                       GAsyncResult *async_result,
                                                       GError **error)
 {
-       GSimpleAsyncResult *result;
-
        g_return_val_if_fail (GDATA_IS_OAUTH2_AUTHORIZER (self), FALSE);
        g_return_val_if_fail (G_IS_ASYNC_RESULT (async_result), FALSE);
        g_return_val_if_fail (error == NULL || *error == NULL, FALSE);
+       g_return_val_if_fail (g_task_is_valid (async_result, self), FALSE);
+       g_return_val_if_fail (g_async_result_is_tagged (async_result, 
gdata_oauth2_authorizer_request_authorization_async), FALSE);
 
-       result = G_SIMPLE_ASYNC_RESULT (async_result);
-
-       g_warn_if_fail (g_simple_async_result_is_valid (async_result,
-                                                       G_OBJECT (self),
-                                                       gdata_oauth2_authorizer_request_authorization_async));
-
-       if (g_simple_async_result_propagate_error (result, error)) {
-               return FALSE;
-       }
-
-       return TRUE;
+       return g_task_propagate_boolean (G_TASK (async_result), error);
 }
 
 /**


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]