Blame libsoup/soup-io-stream.c

rpm-build 4f3c61
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
rpm-build 4f3c61
/*
rpm-build 4f3c61
 * soup-io-stream.c
rpm-build 4f3c61
 *
rpm-build 4f3c61
 * Copyright 2012 Red Hat, Inc.
rpm-build 4f3c61
 */
rpm-build 4f3c61
rpm-build 4f3c61
#ifdef HAVE_CONFIG_H
rpm-build 4f3c61
#include <config.h>
rpm-build 4f3c61
#endif
rpm-build 4f3c61
rpm-build 4f3c61
#include "soup-io-stream.h"
rpm-build 4f3c61
#include "soup.h"
rpm-build 4f3c61
#include "soup-filter-input-stream.h"
rpm-build 4f3c61
rpm-build 4f3c61
struct _SoupIOStreamPrivate {
rpm-build 4f3c61
	GIOStream *base_iostream;
rpm-build 4f3c61
	gboolean close_on_dispose;
rpm-build 4f3c61
rpm-build 4f3c61
	GInputStream *istream;
rpm-build 4f3c61
	GOutputStream *ostream;
rpm-build 4f3c61
	gboolean disposing;
rpm-build 4f3c61
};
rpm-build 4f3c61
rpm-build 4f3c61
enum {
rpm-build 4f3c61
	PROP_0,
rpm-build 4f3c61
rpm-build 4f3c61
	PROP_BASE_IOSTREAM,
rpm-build 4f3c61
	PROP_CLOSE_ON_DISPOSE
rpm-build 4f3c61
};
rpm-build 4f3c61
rpm-build 4f3c61
G_DEFINE_TYPE_WITH_PRIVATE (SoupIOStream, soup_io_stream, G_TYPE_IO_STREAM)
rpm-build 4f3c61
rpm-build 4f3c61
static void
rpm-build 4f3c61
soup_io_stream_init (SoupIOStream *stream)
rpm-build 4f3c61
{
rpm-build 4f3c61
	stream->priv = soup_io_stream_get_instance_private (stream);
rpm-build 4f3c61
}
rpm-build 4f3c61
rpm-build 4f3c61
static void
rpm-build 4f3c61
soup_io_stream_set_property (GObject *object, guint prop_id,
rpm-build 4f3c61
			     const GValue *value, GParamSpec *pspec)
rpm-build 4f3c61
{
rpm-build 4f3c61
	SoupIOStream *siostream = SOUP_IO_STREAM (object);
rpm-build 4f3c61
	GIOStream *io;
rpm-build 4f3c61
rpm-build 4f3c61
	switch (prop_id) {
rpm-build 4f3c61
	case PROP_BASE_IOSTREAM:
rpm-build 4f3c61
		io = siostream->priv->base_iostream = g_value_dup_object (value);
rpm-build 4f3c61
		if (io) {
rpm-build 4f3c61
			siostream->priv->istream =
rpm-build 4f3c61
				soup_filter_input_stream_new (g_io_stream_get_input_stream (io));
rpm-build 4f3c61
			siostream->priv->ostream =
rpm-build 4f3c61
				g_object_ref (g_io_stream_get_output_stream (io));
rpm-build 4f3c61
		} else {
rpm-build 4f3c61
			g_clear_object (&siostream->priv->istream);
rpm-build 4f3c61
			g_clear_object (&siostream->priv->ostream);
rpm-build 4f3c61
		}
rpm-build 4f3c61
		break;
rpm-build 4f3c61
	case PROP_CLOSE_ON_DISPOSE:
rpm-build 4f3c61
		siostream->priv->close_on_dispose = g_value_get_boolean (value);
rpm-build 4f3c61
		break;
rpm-build 4f3c61
	default:
rpm-build 4f3c61
		G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
rpm-build 4f3c61
		break;
rpm-build 4f3c61
	}
rpm-build 4f3c61
}
rpm-build 4f3c61
rpm-build 4f3c61
static void
rpm-build 4f3c61
soup_io_stream_get_property (GObject *object, guint prop_id,
rpm-build 4f3c61
			     GValue *value, GParamSpec *pspec)
rpm-build 4f3c61
{
rpm-build 4f3c61
	SoupIOStream *siostream = SOUP_IO_STREAM (object);
rpm-build 4f3c61
rpm-build 4f3c61
	switch (prop_id) {
rpm-build 4f3c61
	case PROP_BASE_IOSTREAM:
rpm-build 4f3c61
		g_value_set_object (value, siostream->priv->base_iostream);
rpm-build 4f3c61
		break;
rpm-build 4f3c61
	case PROP_CLOSE_ON_DISPOSE:
rpm-build 4f3c61
		g_value_set_boolean (value, siostream->priv->close_on_dispose);
rpm-build 4f3c61
		break;
rpm-build 4f3c61
	default:
rpm-build 4f3c61
		G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
rpm-build 4f3c61
		break;
rpm-build 4f3c61
	}
rpm-build 4f3c61
}
rpm-build 4f3c61
rpm-build 4f3c61
static void
rpm-build 4f3c61
soup_io_stream_dispose (GObject *object)
rpm-build 4f3c61
{
rpm-build 4f3c61
	SoupIOStream *siostream = SOUP_IO_STREAM (object);
rpm-build 4f3c61
rpm-build 4f3c61
	siostream->priv->disposing = TRUE;
rpm-build 4f3c61
rpm-build 4f3c61
	G_OBJECT_CLASS (soup_io_stream_parent_class)->dispose (object);
rpm-build 4f3c61
}
rpm-build 4f3c61
rpm-build 4f3c61
static void
rpm-build 4f3c61
soup_io_stream_finalize (GObject *object)
rpm-build 4f3c61
{
rpm-build 4f3c61
	SoupIOStream *siostream = SOUP_IO_STREAM (object);
rpm-build 4f3c61
rpm-build 4f3c61
	g_clear_object (&siostream->priv->base_iostream);
rpm-build 4f3c61
	g_clear_object (&siostream->priv->istream);
rpm-build 4f3c61
	g_clear_object (&siostream->priv->ostream);
rpm-build 4f3c61
rpm-build 4f3c61
	G_OBJECT_CLASS (soup_io_stream_parent_class)->finalize (object);
rpm-build 4f3c61
}
rpm-build 4f3c61
rpm-build 4f3c61
static GInputStream *
rpm-build 4f3c61
soup_io_stream_get_input_stream (GIOStream *stream)
rpm-build 4f3c61
{
rpm-build 4f3c61
	return SOUP_IO_STREAM (stream)->priv->istream;
rpm-build 4f3c61
}
rpm-build 4f3c61
rpm-build 4f3c61
static GOutputStream *
rpm-build 4f3c61
soup_io_stream_get_output_stream (GIOStream *stream)
rpm-build 4f3c61
{
rpm-build 4f3c61
	return SOUP_IO_STREAM (stream)->priv->ostream;
rpm-build 4f3c61
}
rpm-build 4f3c61
rpm-build 4f3c61
rpm-build 4f3c61
static gboolean
rpm-build 4f3c61
soup_io_stream_close (GIOStream     *stream,
rpm-build 4f3c61
		      GCancellable  *cancellable,
rpm-build 4f3c61
		      GError       **error)
rpm-build 4f3c61
{
rpm-build 4f3c61
	SoupIOStream *siostream = SOUP_IO_STREAM (stream);
rpm-build 4f3c61
rpm-build 4f3c61
	if (siostream->priv->disposing &&
rpm-build 4f3c61
	    !siostream->priv->close_on_dispose)
rpm-build 4f3c61
		return TRUE;
rpm-build 4f3c61
rpm-build 4f3c61
	return g_io_stream_close (siostream->priv->base_iostream,
rpm-build 4f3c61
				  cancellable, error);
rpm-build 4f3c61
}
rpm-build 4f3c61
rpm-build 4f3c61
static void
rpm-build 4f3c61
close_async_complete (GObject      *object,
rpm-build 4f3c61
		      GAsyncResult *result,
rpm-build 4f3c61
		      gpointer      user_data)
rpm-build 4f3c61
{
rpm-build 4f3c61
	GTask *task = user_data;
rpm-build 4f3c61
	GError *error = NULL;
rpm-build 4f3c61
rpm-build 4f3c61
	if (g_io_stream_close_finish (G_IO_STREAM (object), result, &error))
rpm-build 4f3c61
		g_task_return_boolean (task, TRUE);
rpm-build 4f3c61
	else
rpm-build 4f3c61
		g_task_return_error (task, error);
rpm-build 4f3c61
	g_object_unref (task);
rpm-build 4f3c61
}
rpm-build 4f3c61
rpm-build 4f3c61
static void    
rpm-build 4f3c61
soup_io_stream_close_async (GIOStream           *stream,
rpm-build 4f3c61
			    int                  io_priority,
rpm-build 4f3c61
			    GCancellable        *cancellable,
rpm-build 4f3c61
			    GAsyncReadyCallback  callback,
rpm-build 4f3c61
			    gpointer             user_data)
rpm-build 4f3c61
{
rpm-build 4f3c61
	GTask *task;
rpm-build 4f3c61
rpm-build 4f3c61
	task = g_task_new (stream, cancellable, callback, user_data);
rpm-build 4f3c61
	g_io_stream_close_async (SOUP_IO_STREAM (stream)->priv->base_iostream,
rpm-build 4f3c61
				 io_priority, cancellable,
rpm-build 4f3c61
				 close_async_complete, task);
rpm-build 4f3c61
}
rpm-build 4f3c61
rpm-build 4f3c61
static gboolean
rpm-build 4f3c61
soup_io_stream_close_finish (GIOStream     *stream,
rpm-build 4f3c61
                             GAsyncResult  *result,
rpm-build 4f3c61
			     GError       **error)
rpm-build 4f3c61
{
rpm-build 4f3c61
	return g_task_propagate_boolean (G_TASK (result), error);
rpm-build 4f3c61
}
rpm-build 4f3c61
rpm-build 4f3c61
static void
rpm-build 4f3c61
soup_io_stream_class_init (SoupIOStreamClass *stream_class)
rpm-build 4f3c61
{
rpm-build 4f3c61
	GObjectClass *object_class = G_OBJECT_CLASS (stream_class);
rpm-build 4f3c61
	GIOStreamClass *io_stream_class = G_IO_STREAM_CLASS (stream_class);
rpm-build 4f3c61
rpm-build 4f3c61
	object_class->set_property = soup_io_stream_set_property;
rpm-build 4f3c61
	object_class->get_property = soup_io_stream_get_property;
rpm-build 4f3c61
	object_class->dispose = soup_io_stream_dispose;
rpm-build 4f3c61
	object_class->finalize = soup_io_stream_finalize;
rpm-build 4f3c61
rpm-build 4f3c61
	io_stream_class->get_input_stream = soup_io_stream_get_input_stream;
rpm-build 4f3c61
	io_stream_class->get_output_stream = soup_io_stream_get_output_stream;
rpm-build 4f3c61
	io_stream_class->close_fn = soup_io_stream_close;
rpm-build 4f3c61
	io_stream_class->close_async = soup_io_stream_close_async;
rpm-build 4f3c61
	io_stream_class->close_finish = soup_io_stream_close_finish;
rpm-build 4f3c61
rpm-build 4f3c61
	g_object_class_install_property (
rpm-build 4f3c61
		object_class, PROP_BASE_IOSTREAM,
rpm-build 4f3c61
		g_param_spec_object ("base-iostream",
rpm-build 4f3c61
				     "Base IOStream",
rpm-build 4f3c61
				     "Base GIOStream",
rpm-build 4f3c61
				     G_TYPE_IO_STREAM,
rpm-build 4f3c61
				     G_PARAM_READWRITE |
rpm-build 4f3c61
				     G_PARAM_CONSTRUCT_ONLY));
rpm-build 4f3c61
	g_object_class_install_property (
rpm-build 4f3c61
		object_class, PROP_CLOSE_ON_DISPOSE,
rpm-build 4f3c61
		g_param_spec_boolean ("close-on-dispose",
rpm-build 4f3c61
				      "Close base stream",
rpm-build 4f3c61
				      "Close base GIOStream when closing",
rpm-build 4f3c61
				      TRUE,
rpm-build 4f3c61
				      G_PARAM_READWRITE |
rpm-build 4f3c61
				      G_PARAM_CONSTRUCT_ONLY));
rpm-build 4f3c61
}
rpm-build 4f3c61
rpm-build 4f3c61
GIOStream *
rpm-build 4f3c61
soup_io_stream_new (GIOStream *base_iostream,
rpm-build 4f3c61
		    gboolean   close_on_dispose)
rpm-build 4f3c61
{
rpm-build 4f3c61
	return g_object_new (SOUP_TYPE_IO_STREAM,
rpm-build 4f3c61
			     "base-iostream", base_iostream,
rpm-build 4f3c61
			     "close-on-dispose", close_on_dispose,
rpm-build 4f3c61
			     NULL);
rpm-build 4f3c61
}