Tizen 2.1 base
[platform/upstream/glib2.0.git] / gio / giostream.c
index d785011..593eeda 100644 (file)
@@ -30,9 +30,8 @@
 #include <gio/gsimpleasyncresult.h>
 #include <gio/gasyncresult.h>
 
-#include "gioalias.h"
 
-G_DEFINE_TYPE (GIOStream, g_io_stream, G_TYPE_OBJECT);
+G_DEFINE_ABSTRACT_TYPE (GIOStream, g_io_stream, G_TYPE_OBJECT);
 
 /**
  * SECTION:giostream
@@ -149,19 +148,6 @@ g_io_stream_get_property (GObject    *object,
 }
 
 static void
-g_io_stream_set_property (GObject      *object,
-                         guint         prop_id,
-                         const GValue *value,
-                         GParamSpec   *pspec)
-{
-  switch (prop_id)
-    {
-    default:
-      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
-    }
-}
-
-static void
 g_io_stream_class_init (GIOStreamClass *klass)
 {
   GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
@@ -170,7 +156,6 @@ g_io_stream_class_init (GIOStreamClass *klass)
 
   gobject_class->finalize = g_io_stream_finalize;
   gobject_class->dispose = g_io_stream_dispose;
-  gobject_class->set_property = g_io_stream_set_property;
   gobject_class->get_property = g_io_stream_get_property;
 
   klass->close_fn = g_io_stream_real_close;
@@ -182,7 +167,7 @@ g_io_stream_class_init (GIOStreamClass *klass)
                                                          P_("Closed"),
                                                          P_("Is the stream closed"),
                                                          FALSE,
-                                                         G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
+                                                         G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
 
   g_object_class_install_property (gobject_class, PROP_INPUT_STREAM,
                                   g_param_spec_object ("input-stream",
@@ -223,7 +208,8 @@ g_io_stream_is_closed (GIOStream *stream)
  * Gets the input stream for this object. This is used
  * for reading.
  *
- * Returns: a #GInputStream, owned by the #GIOStream. Do not free.
+ * Returns: (transfer none): a #GInputStream, owned by the #GIOStream.
+ * Do not free.
  *
  * Since: 2.22
  */
@@ -246,7 +232,8 @@ g_io_stream_get_input_stream (GIOStream *stream)
  * Gets the output stream for this object. This is used for
  * writing.
  *
- * Returns: a #GOutputStream, owned by the #GIOStream. Do not free.
+ * Returns: (transfer none): a #GOutputStream, owned by the #GIOStream.
+ * Do not free.
  *
  * Since: 2.22
  */
@@ -282,7 +269,7 @@ g_io_stream_has_pending (GIOStream *stream)
 /**
  * g_io_stream_set_pending:
  * @stream: a #GIOStream
- * @error: a #GError location to store the error occuring, or %NULL to
+ * @error: a #GError location to store the error occurring, or %NULL to
  *     ignore
  *
  * Sets @stream to have actions pending. If the pending flag is
@@ -360,8 +347,8 @@ g_io_stream_real_close (GIOStream     *stream,
 /**
  * g_io_stream_close:
  * @stream: a #GIOStream
- * @cancellable: optional #GCancellable object, %NULL to ignore
- * @error: location to store the error occuring, or %NULL to ignore
+ * @cancellable: (allow-none): optional #GCancellable object, %NULL to ignore
+ * @error: location to store the error occurring, or %NULL to ignore
  *
  * Closes the stream, releasing resources related to it. This will also
  * closes the individual input and output streams, if they are not already
@@ -453,9 +440,9 @@ async_ready_close_callback_wrapper (GObject      *source_object,
  * g_io_stream_close_async:
  * @stream: a #GIOStream
  * @io_priority: the io priority of the request
- * @callback: callback to call when the request is satisfied
- * @user_data: the data to pass to callback function
- * @cancellable: optional cancellable object
+ * @cancellable: (allow-none): optional cancellable object
+ * @callback: (scope async): callback to call when the request is satisfied
+ * @user_data: (closure): the data to pass to callback function
  *
  * Requests an asynchronous close of the stream, releasing resources
  * related to it. When the operation is finished @callback will be
@@ -496,11 +483,10 @@ g_io_stream_close_async (GIOStream           *stream,
 
   if (!g_io_stream_set_pending (stream, &error))
     {
-      g_simple_async_report_gerror_in_idle (G_OBJECT (stream),
+      g_simple_async_report_take_gerror_in_idle (G_OBJECT (stream),
                                            callback,
                                            user_data,
                                            error);
-      g_error_free (error);
       return;
     }
 
@@ -515,7 +501,7 @@ g_io_stream_close_async (GIOStream           *stream,
  * g_io_stream_close_finish:
  * @stream: a #GIOStream
  * @result: a #GAsyncResult
- * @error: a #GError location to store the error occuring, or %NULL to
+ * @error: a #GError location to store the error occurring, or %NULL to
  *    ignore
  *
  * Closes a stream.
@@ -569,10 +555,7 @@ close_async_thread (GSimpleAsyncResult *res,
     {
       result = class->close_fn (G_IO_STREAM (object), cancellable, &error);
       if (!result)
-       {
-         g_simple_async_result_set_from_error (res, error);
-         g_error_free (error);
-       }
+        g_simple_async_result_take_error (res, error);
     }
 }
 
@@ -610,5 +593,254 @@ g_io_stream_real_close_finish (GIOStream     *stream,
   return TRUE;
 }
 
-#define __G_IO_STREAM_C__
-#include "gioaliasdef.c"
+typedef struct
+{
+  GIOStream *stream1;
+  GIOStream *stream2;
+  GIOStreamSpliceFlags flags;
+  gint io_priority;
+  GCancellable *cancellable;
+  gulong cancelled_id;
+  GCancellable *op1_cancellable;
+  GCancellable *op2_cancellable;
+  guint completed;
+  GError *error;
+} SpliceContext;
+
+static void
+splice_context_free (SpliceContext *ctx)
+{
+  g_object_unref (ctx->stream1);
+  g_object_unref (ctx->stream2);
+  if (ctx->cancellable != NULL)
+    g_object_unref (ctx->cancellable);
+  g_object_unref (ctx->op1_cancellable);
+  g_object_unref (ctx->op2_cancellable);
+  g_clear_error (&ctx->error);
+  g_slice_free (SpliceContext, ctx);
+}
+
+static void
+splice_complete (GSimpleAsyncResult *simple,
+                 SpliceContext      *ctx)
+{
+  if (ctx->cancelled_id != 0)
+    g_cancellable_disconnect (ctx->cancellable, ctx->cancelled_id);
+  ctx->cancelled_id = 0;
+
+  if (ctx->error != NULL)
+    g_simple_async_result_set_from_error (simple, ctx->error);
+  g_simple_async_result_complete (simple);
+}
+
+static void
+splice_close_cb (GObject      *iostream,
+                 GAsyncResult *res,
+                 gpointer      user_data)
+{
+  GSimpleAsyncResult *simple = user_data;
+  SpliceContext *ctx;
+  GError *error = NULL;
+
+  g_io_stream_close_finish (G_IO_STREAM (iostream), res, &error);
+
+  ctx = g_simple_async_result_get_op_res_gpointer (simple);
+  ctx->completed++;
+
+  /* Keep the first error that occurred */
+  if (error != NULL && ctx->error == NULL)
+    ctx->error = error;
+  else
+    g_clear_error (&error);
+
+  /* If all operations are done, complete now */
+  if (ctx->completed == 4)
+    splice_complete (simple, ctx);
+
+  g_object_unref (simple);
+}
+
+static void
+splice_cb (GObject      *ostream,
+           GAsyncResult *res,
+           gpointer      user_data)
+{
+  GSimpleAsyncResult *simple = user_data;
+  SpliceContext *ctx;
+  GError *error = NULL;
+
+  g_output_stream_splice_finish (G_OUTPUT_STREAM (ostream), res, &error);
+
+  ctx = g_simple_async_result_get_op_res_gpointer (simple);
+  ctx->completed++;
+
+  /* ignore cancellation error if it was not requested by the user */
+  if (error != NULL &&
+      g_error_matches (error, G_IO_ERROR, G_IO_ERROR_CANCELLED) &&
+      (ctx->cancellable == NULL ||
+       !g_cancellable_is_cancelled (ctx->cancellable)))
+    g_clear_error (&error);
+
+  /* Keep the first error that occurred */
+  if (error != NULL && ctx->error == NULL)
+    ctx->error = error;
+  else
+    g_clear_error (&error);
+
+   if (ctx->completed == 1 &&
+       (ctx->flags & G_IO_STREAM_SPLICE_WAIT_FOR_BOTH) == 0)
+    {
+      /* We don't want to wait for the 2nd operation to finish, cancel it */
+      g_cancellable_cancel (ctx->op1_cancellable);
+      g_cancellable_cancel (ctx->op2_cancellable);
+    }
+  else if (ctx->completed == 2)
+    {
+      if (ctx->cancellable == NULL ||
+          !g_cancellable_is_cancelled (ctx->cancellable))
+        {
+          g_cancellable_reset (ctx->op1_cancellable);
+          g_cancellable_reset (ctx->op2_cancellable);
+        }
+
+      /* Close the IO streams if needed */
+      if ((ctx->flags & G_IO_STREAM_SPLICE_CLOSE_STREAM1) != 0)
+        g_io_stream_close_async (ctx->stream1, ctx->io_priority,
+            ctx->op1_cancellable, splice_close_cb, g_object_ref (simple));
+      else
+        ctx->completed++;
+
+      if ((ctx->flags & G_IO_STREAM_SPLICE_CLOSE_STREAM2) != 0)
+        g_io_stream_close_async (ctx->stream2, ctx->io_priority,
+            ctx->op2_cancellable, splice_close_cb, g_object_ref (simple));
+      else
+        ctx->completed++;
+
+      /* If all operations are done, complete now */
+      if (ctx->completed == 4)
+        splice_complete (simple, ctx);
+    }
+
+  g_object_unref (simple);
+}
+
+static void
+splice_cancelled_cb (GCancellable       *cancellable,
+                     GSimpleAsyncResult *simple)
+{
+  SpliceContext *ctx;
+
+  ctx = g_simple_async_result_get_op_res_gpointer (simple);
+  g_cancellable_cancel (ctx->op1_cancellable);
+  g_cancellable_cancel (ctx->op2_cancellable);
+}
+
+/**
+ * g_io_stream_splice_async:
+ * @stream1: a #GIOStream.
+ * @stream2: a #GIOStream.
+ * @flags: a set of #GIOStreamSpliceFlags.
+ * @io_priority: the io priority of the request.
+ * @cancellable: (allow-none): optional #GCancellable object, %NULL to ignore.
+ * @callback: (scope async): a #GAsyncReadyCallback.
+ * @user_data: (closure): user data passed to @callback.
+ *
+ * Asyncronously splice the output stream of @stream1 to the input stream of
+ * @stream2, and splice the output stream of @stream2 to the input stream of
+ * @stream1.
+ *
+ * When the operation is finished @callback will be called.
+ * You can then call g_io_stream_splice_finish() to get the
+ * result of the operation.
+ *
+ * Since: 2.28
+ **/
+void
+g_io_stream_splice_async (GIOStream            *stream1,
+                          GIOStream            *stream2,
+                          GIOStreamSpliceFlags  flags,
+                          gint                  io_priority,
+                          GCancellable         *cancellable,
+                          GAsyncReadyCallback   callback,
+                          gpointer              user_data)
+{
+  GSimpleAsyncResult *simple;
+  SpliceContext *ctx;
+  GInputStream *istream;
+  GOutputStream *ostream;
+
+  if (cancellable != NULL && g_cancellable_is_cancelled (cancellable))
+    {
+      g_simple_async_report_error_in_idle (NULL, callback,
+          user_data, G_IO_ERROR, G_IO_ERROR_CANCELLED,
+          "Operation has been cancelled");
+      return;
+    }
+
+  ctx = g_slice_new0 (SpliceContext);
+  ctx->stream1 = g_object_ref (stream1);
+  ctx->stream2 = g_object_ref (stream2);
+  ctx->flags = flags;
+  ctx->io_priority = io_priority;
+  ctx->op1_cancellable = g_cancellable_new ();
+  ctx->op2_cancellable = g_cancellable_new ();
+  ctx->completed = 0;
+
+  simple = g_simple_async_result_new (NULL, callback, user_data,
+      g_io_stream_splice_finish);
+  g_simple_async_result_set_op_res_gpointer (simple, ctx,
+      (GDestroyNotify) splice_context_free);
+
+  if (cancellable != NULL)
+    {
+      ctx->cancellable = g_object_ref (cancellable);
+      ctx->cancelled_id = g_cancellable_connect (cancellable,
+          G_CALLBACK (splice_cancelled_cb), g_object_ref (simple),
+          g_object_unref);
+    }
+
+  istream = g_io_stream_get_input_stream (stream1);
+  ostream = g_io_stream_get_output_stream (stream2);
+  g_output_stream_splice_async (ostream, istream, G_OUTPUT_STREAM_SPLICE_NONE,
+      io_priority, ctx->op1_cancellable, splice_cb,
+      g_object_ref (simple));
+
+  istream = g_io_stream_get_input_stream (stream2);
+  ostream = g_io_stream_get_output_stream (stream1);
+  g_output_stream_splice_async (ostream, istream, G_OUTPUT_STREAM_SPLICE_NONE,
+      io_priority, ctx->op2_cancellable, splice_cb,
+      g_object_ref (simple));
+
+  g_object_unref (simple);
+}
+
+/**
+ * g_io_stream_splice_finish:
+ * @result: a #GAsyncResult.
+ * @error: a #GError location to store the error occurring, or %NULL to
+ * ignore.
+ *
+ * Finishes an asynchronous io stream splice operation.
+ *
+ * Returns: %TRUE on success, %FALSE otherwise.
+ *
+ * Since: 2.28
+ **/
+gboolean
+g_io_stream_splice_finish (GAsyncResult  *result,
+                           GError       **error)
+{
+  GSimpleAsyncResult *simple;
+
+  g_return_val_if_fail (G_IS_SIMPLE_ASYNC_RESULT (result), FALSE);
+
+  simple = G_SIMPLE_ASYNC_RESULT (result);
+
+  if (g_simple_async_result_propagate_error (simple, error))
+    return FALSE;
+
+  g_return_val_if_fail (g_simple_async_result_is_valid (result, NULL,
+      g_io_stream_splice_finish), FALSE);
+
+  return TRUE;
+}