summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDanielle Madeley <danielle.madeley@collabora.co.uk>2012-03-27 02:08:40 +0000
committerRoss Burton <ross.burton@intel.com>2012-03-27 11:42:40 +0100
commitcfa07569426fa4ac970c12e378c38087c2106fd1 (patch)
tree2f31b466ea3728289e1f30b1371c1ca06dad97d5
parent936aef6d6ec58f537997a1c2922ba4d34738dd18 (diff)
rest-proxy-call: add rest_proxy_call_call_async()
This is a GIO-style async function which is much easier to introspect. Should consider deprecating rest_proxy_call_async() and rest_proxy_call_cancel().
-rw-r--r--rest/Makefile.am4
-rw-r--r--rest/rest-proxy-call.c116
-rw-r--r--rest/rest-proxy-call.h8
3 files changed, 124 insertions, 4 deletions
diff --git a/rest/Makefile.am b/rest/Makefile.am
index 7fa860b..7e7ed0a 100644
--- a/rest/Makefile.am
+++ b/rest/Makefile.am
@@ -97,8 +97,8 @@ Rest_@API_VERSION_AM@_gir_FILES = \
$(lib_headers) \
$(filter-out %private.h, $(lib_sources))
Rest_@API_VERSION_AM@_gir_CFLAGS = -I$(top_srcdir)
-Rest_@API_VERSION_AM@_gir_INCLUDES = GObject-2.0
-Rest_@API_VERSION_AM@_gir_PACKAGES = gobject-2.0 libsoup-2.4 libxml-2.0
+Rest_@API_VERSION_AM@_gir_INCLUDES = GObject-2.0 Gio-2.0
+Rest_@API_VERSION_AM@_gir_PACKAGES = gobject-2.0 libsoup-2.4 libxml-2.0 gio-2.0
Rest_@API_VERSION_AM@_gir_SCANNERFLAGS = --accept-unprefixed
Rest_@API_VERSION_AM@_gir_EXPORT_PACKAGES = rest-@API_VERSION@
diff --git a/rest/rest-proxy-call.c b/rest/rest-proxy-call.c
index 72651c1..15d71d4 100644
--- a/rest/rest-proxy-call.c
+++ b/rest/rest-proxy-call.c
@@ -829,7 +829,7 @@ prepare_message (RestProxyCall *call, GError **error_out)
}
/**
- * rest_proxy_call_async:
+ * rest_proxy_call_async: (skip):
* @call: The #RestProxyCall
* @callback: a #RestProxyCallAsyncCallback to invoke on completion of the call
* @weak_object: The #GObject to weakly reference and tie the lifecycle too
@@ -903,6 +903,115 @@ error:
}
static void
+_call_message_call_cancelled_cb (GCancellable *cancellable,
+ RestProxyCall *call)
+{
+ rest_proxy_call_cancel (call);
+}
+
+static void
+_call_message_call_completed_cb (SoupSession *session,
+ SoupMessage *message,
+ gpointer user_data)
+{
+ GSimpleAsyncResult *result = user_data;
+ RestProxyCall *call;
+ GError *error = NULL;
+
+ call = REST_PROXY_CALL (
+ g_async_result_get_source_object (G_ASYNC_RESULT (result)));
+
+ // FIXME: disconnect cancellable ?
+
+ finish_call (call, message, &error);
+
+ if (error != NULL)
+ g_simple_async_result_take_error (result, error);
+ else
+ g_simple_async_result_set_op_res_gboolean (result, TRUE);
+
+ g_simple_async_result_complete (result);
+
+ g_object_unref (call);
+ g_object_unref (result);
+}
+
+/**
+ * rest_proxy_call_call_async:
+ * @call: a #RestProxyCall
+ * @cancellable: (allow-none): an optional #GCancellable that can be used to
+ * cancel the call, or %NULL
+ * @callback: (scope async): callback to call when the async call is finished
+ * @user_data: (closure): user data for the callback
+ *
+ * A GIO-style version of rest_proxy_call_async().
+ */
+void
+rest_proxy_call_call_async (RestProxyCall *call,
+ GCancellable *cancellable,
+ GAsyncReadyCallback callback,
+ gpointer user_data)
+{
+ GSimpleAsyncResult *result;
+ RestProxyCallPrivate *priv;
+ SoupMessage *message;
+ GError *error = NULL;
+
+ g_return_if_fail (REST_IS_PROXY_CALL (call));
+ priv = GET_PRIVATE (call);
+ g_assert (priv->proxy);
+
+ message = prepare_message (call, &error);
+ if (message == NULL)
+ {
+ g_simple_async_report_take_gerror_in_idle (G_OBJECT (call), callback,
+ user_data, error);
+ return;
+ }
+
+ result = g_simple_async_result_new (G_OBJECT (call), callback,
+ user_data, rest_proxy_call_call_async);
+
+ if (cancellable != NULL)
+ g_signal_connect (cancellable, "cancelled",
+ G_CALLBACK (_call_message_call_cancelled_cb), call);
+
+ _rest_proxy_queue_message (priv->proxy,
+ message,
+ _call_message_call_completed_cb,
+ result);
+}
+
+/**
+ * rest_proxy_call_call_finish:
+ * @call: a #RestProxyCall
+ * @result: the result from the #GAsyncReadyCallback
+ * @error: optional #GError
+ *
+ * Returns: %TRUE on success
+ */
+gboolean
+rest_proxy_call_call_finish (RestProxyCall *call,
+ GAsyncResult *result,
+ GError **error)
+{
+ GSimpleAsyncResult *simple;
+
+ g_return_val_if_fail (REST_IS_PROXY_CALL (call), FALSE);
+ g_return_val_if_fail (G_IS_SIMPLE_ASYNC_RESULT (result), FALSE);
+
+ simple = G_SIMPLE_ASYNC_RESULT (result);
+
+ g_return_val_if_fail (g_simple_async_result_is_valid (result,
+ G_OBJECT (call), rest_proxy_call_call_async), FALSE);
+
+ if (g_simple_async_result_propagate_error (simple, error))
+ return FALSE;
+
+ return g_simple_async_result_get_op_res_gboolean (simple);
+}
+
+static void
_continuous_call_message_got_chunk_cb (SoupMessage *msg,
SoupBuffer *chunk,
RestProxyCallContinuousClosure *closure)
@@ -1146,11 +1255,14 @@ error:
}
/**
- * rest_proxy_call_cancel:
+ * rest_proxy_call_cancel: (skip):
* @call: The #RestProxyCall
*
* Cancel this call. It may be too late to not actually send the message, but
* the callback will not be invoked.
+ *
+ * N.B. this method should only be used with rest_proxy_call_async() and NOT
+ * rest_proxy_call_call_async().
*/
gboolean
rest_proxy_call_cancel (RestProxyCall *call)
diff --git a/rest/rest-proxy-call.h b/rest/rest-proxy-call.h
index a8085d9..cc0b575 100644
--- a/rest/rest-proxy-call.h
+++ b/rest/rest-proxy-call.h
@@ -24,6 +24,7 @@
#define _REST_PROXY_CALL
#include <glib-object.h>
+#include <gio/gio.h>
#include <rest/rest-params.h>
G_BEGIN_DECLS
@@ -162,6 +163,13 @@ gboolean rest_proxy_call_async (RestProxyCall *call,
gpointer userdata,
GError **error);
+void rest_proxy_call_call_async (RestProxyCall *call,
+ GCancellable *cancellable,
+ GAsyncReadyCallback callback,
+ gpointer user_data);
+gboolean rest_proxy_call_call_finish (RestProxyCall *call,
+ GAsyncResult *result,
+ GError **error);
typedef void (*RestProxyCallContinuousCallback) (RestProxyCall *call,
const gchar *buf,