[gnome-online-accounts/wip/rishi/gtask: 4/4] ewsclient: Port goa_ews_client_autodiscover() to GTask



commit d68a9d352b0748f9223ba2c6cc3f5a111c500a51
Author: Debarshi Ray <debarshir gnome org>
Date:   Fri Nov 23 18:37:34 2018 +0100

    ewsclient: Port goa_ews_client_autodiscover() to GTask
    
    https://bugzilla.gnome.org/show_bug.cgi?id=764157

 src/goabackend/goaewsclient.c | 68 +++++++++++++++++++++++++++++--------------
 1 file changed, 46 insertions(+), 22 deletions(-)
---
diff --git a/src/goabackend/goaewsclient.c b/src/goabackend/goaewsclient.c
index baf44450..9530b23a 100644
--- a/src/goabackend/goaewsclient.c
+++ b/src/goabackend/goaewsclient.c
@@ -64,7 +64,6 @@ goa_ews_client_new (void)
 typedef struct
 {
   GCancellable *cancellable;
-  GSimpleAsyncResult *res;
   SoupMessage *msgs[2];
   SoupSession *session;
   gboolean accept_ssl_errors;
@@ -92,7 +91,6 @@ ews_client_autodiscover_data_free (gpointer user_data)
 
   /* soup_session_queue_message stole the references to data->msgs */
   xmlOutputBufferClose (data->buf);
-  g_object_unref (data->res);
   g_object_unref (data->session);
   g_slice_free (AutodiscoverData, data);
 }
@@ -132,10 +130,12 @@ ews_client_authenticate (SoupSession *session,
 static void
 ews_client_request_started (SoupSession *session, SoupMessage *msg, SoupSocket *socket, gpointer user_data)
 {
-  AutodiscoverData *data = user_data;
+  AutodiscoverData *data;
   GError *error;
+  GTask *task = G_TASK (data);
   GTlsCertificateFlags cert_flags;
 
+  data = (AutodiscoverData *) g_task_get_task_data (task);
   error = NULL;
 
   if (!data->accept_ssl_errors
@@ -143,7 +143,7 @@ ews_client_request_started (SoupSession *session, SoupMessage *msg, SoupSocket *
       && cert_flags != 0)
     {
       goa_utils_set_error_ssl (&error, cert_flags);
-      g_simple_async_result_take_error (data->res, error);
+      g_task_return_error (task, error);
       soup_session_abort (data->session);
     }
 }
@@ -151,8 +151,16 @@ ews_client_request_started (SoupSession *session, SoupMessage *msg, SoupSocket *
 static void
 ews_client_autodiscover_cancelled_cb (GCancellable *cancellable, gpointer user_data)
 {
-  AutodiscoverData *data = user_data;
+  AutodiscoverData *data;
+  GTask *task = G_TASK (user_data);
+  gboolean cancelled;
+
+  data = g_task_get_task_data (task);
+
+  cancelled = g_task_return_error_if_cancelled (task);
   soup_session_abort (data->session);
+
+  g_return_if_fail (cancelled);
 }
 
 static gboolean
@@ -180,6 +188,7 @@ ews_client_autodiscover_response_cb (SoupSession *session, SoupMessage *msg, gpo
 {
   GError *error = NULL;
   AutodiscoverData *data = user_data;
+  GTask *task = G_TASK (user_data);
   gboolean op_res = FALSE;
   guint idx;
   guint status;
@@ -202,7 +211,8 @@ ews_client_autodiscover_response_cb (SoupSession *session, SoupMessage *msg, gpo
 
   /* status == SOUP_STATUS_CANCELLED, if we are being aborted by the
    * GCancellable, an SSL error or another message that was
-   * successful.
+   * successful. The GTask was already 'returned' by the respective
+   * callbacks.
    */
   if (status == SOUP_STATUS_CANCELLED)
     {
@@ -217,6 +227,7 @@ ews_client_autodiscover_response_cb (SoupSession *session, SoupMessage *msg, gpo
     {
       g_warning ("goa_ews_client_autodiscover() failed: %u — %s", msg->status_code, msg->reason_phrase);
       goa_utils_set_error_soup (&error, msg);
+      g_task_return_error (task, error);
       goto out;
     }
 
@@ -232,6 +243,7 @@ ews_client_autodiscover_response_cb (SoupSession *session, SoupMessage *msg, gpo
                    GOA_ERROR,
                    GOA_ERROR_FAILED, /* TODO: more specific */
                    _("Failed to parse autodiscover response XML"));
+      g_task_return_error (task, error);
       goto out;
     }
 
@@ -243,6 +255,7 @@ ews_client_autodiscover_response_cb (SoupSession *session, SoupMessage *msg, gpo
                    GOA_ERROR_FAILED, /* TODO: more specific */
                    /* Translators: the parameter is an XML element name. */
                    _("Failed to find “%s” element"), "Autodiscover");
+      g_task_return_error (task, error);
       goto out;
     }
 
@@ -258,6 +271,7 @@ ews_client_autodiscover_response_cb (SoupSession *session, SoupMessage *msg, gpo
                    GOA_ERROR_FAILED, /* TODO: more specific */
                    /* Translators: the parameter is an XML element name. */
                    _("Failed to find “%s” element"), "Response");
+      g_task_return_error (task, error);
       goto out;
     }
 
@@ -301,7 +315,7 @@ ews_client_autodiscover_response_cb (SoupSession *session, SoupMessage *msg, gpo
    * that it won't get lost when we hear from another autodiscover
    * attempt for the same GAsyncResult.
    */
-  g_simple_async_result_set_op_res_gboolean (data->res, op_res);
+  g_task_return_boolean (task, TRUE);
 
   for (idx = 0; idx < size; idx++)
     {
@@ -437,6 +451,7 @@ goa_ews_client_autodiscover (GoaEwsClient        *self,
 {
   AutodiscoverData *data;
   AutodiscoverAuthData *auth;
+  GTask *task = NULL;
   gchar *url1;
   gchar *url2;
   xmlDoc *doc;
@@ -449,6 +464,12 @@ goa_ews_client_autodiscover (GoaEwsClient        *self,
   g_return_if_fail (server != NULL && server[0] != '\0');
   g_return_if_fail (cancellable == NULL || G_IS_CANCELLABLE (cancellable));
 
+  task = g_task_new (self, cancellable, callback, user_data);
+  g_task_set_source_tag (task, goa_ews_client_autodiscover);
+
+  data = g_slice_new0 (AutodiscoverData);
+  g_task_set_task_data (task, data, ews_client_autodiscover_data_free);
+
   doc = ews_client_create_autodiscover_xml (email);
   buf = xmlAllocOutputBuffer (NULL);
   xmlNodeDumpOutput (buf, doc, xmlDocGetRootElement (doc), 0, 1, NULL);
@@ -464,9 +485,8 @@ goa_ews_client_autodiscover (GoaEwsClient        *self,
    * to time out. So run both queries in parallel and let the fastest
    * (successful) one win.
    */
-  data = g_slice_new0 (AutodiscoverData);
+
   data->buf = buf;
-  data->res = g_simple_async_result_new (G_OBJECT (self), callback, user_data, goa_ews_client_autodiscover);
   data->msgs[0] = ews_client_create_msg_for_url (url1, buf);
   data->msgs[1] = ews_client_create_msg_for_url (url2, buf);
   data->pending = sizeof (data->msgs) / sizeof (data->msgs[0]);
@@ -478,11 +498,10 @@ goa_ews_client_autodiscover (GoaEwsClient        *self,
   if (cancellable != NULL)
     {
       data->cancellable = g_object_ref (cancellable);
-      data->cancellable_id = g_cancellable_connect (data->cancellable,
+      data->cancellable_id = g_cancellable_connect (cancellable,
                                                     G_CALLBACK (ews_client_autodiscover_cancelled_cb),
-                                                    data,
+                                                    task,
                                                     NULL);
-      g_simple_async_result_set_check_cancellable (data->res, data->cancellable);
     }
 
   auth = g_slice_new0 (AutodiscoverAuthData);
@@ -495,31 +514,36 @@ goa_ews_client_autodiscover (GoaEwsClient        *self,
                          ews_client_autodiscover_auth_data_free,
                          0);
 
-  g_signal_connect (data->session, "request-started", G_CALLBACK (ews_client_request_started), data);
+  g_signal_connect (data->session, "request-started", G_CALLBACK (ews_client_request_started), task);
 
-  soup_session_queue_message (data->session, data->msgs[0], ews_client_autodiscover_response_cb, data);
-  soup_session_queue_message (data->session, data->msgs[1], ews_client_autodiscover_response_cb, data);
+  soup_session_queue_message (data->session,
+                              data->msgs[0],
+                              ews_client_autodiscover_response_cb,
+                              g_object_ref (task));
+  soup_session_queue_message (data->session,
+                              data->msgs[1],
+                              ews_client_autodiscover_response_cb,
+                              g_object_ref (task));
 
   g_free (url2);
   g_free (url1);
+  g_object_unref (task);
   xmlFreeDoc (doc);
 }
 
 gboolean
 goa_ews_client_autodiscover_finish (GoaEwsClient *self, GAsyncResult *res, GError **error)
 {
-  GSimpleAsyncResult *simple;
+  GTask *task;
 
-  g_return_val_if_fail (g_simple_async_result_is_valid (res, G_OBJECT (self), goa_ews_client_autodiscover),
-                        FALSE);
   g_return_val_if_fail (error == NULL || *error == NULL, FALSE);
 
-  simple = G_SIMPLE_ASYNC_RESULT (res);
+  g_return_val_if_fail (g_task_is_valid (res, self), FALSE);
+  task = G_TASK (res);
 
-  if (g_simple_async_result_propagate_error (simple, error))
-    return FALSE;
+  g_return_val_if_fail (g_task_get_source_tag (task) == goa_ews_client_autodiscover, FALSE);
 
-  return g_simple_async_result_get_op_res_gboolean (simple);
+  return g_task_propagate_boolean (task, error);
 }
 
 /* ---------------------------------------------------------------------------------------------------- */


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