1 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
2 /*
3 * soup-cache-client-input-stream.c
4 *
5 * Copyright 2015 Igalia S.L.
6 */
7
8 #ifdef HAVE_CONFIG_H
9 #include <config.h>
10 #endif
11
12 #include "soup-cache-client-input-stream.h"
13 #include "soup.h"
14 #include "soup-message-private.h"
15
16 enum {
17 SIGNAL_EOF,
18 SIGNAL_CLOSED,
19 LAST_SIGNAL
20 };
21
22 static guint signals[LAST_SIGNAL] = { 0 };
23
G_DEFINE_TYPE(SoupCacheClientInputStream,soup_cache_client_input_stream,G_TYPE_FILTER_INPUT_STREAM)24 G_DEFINE_TYPE (SoupCacheClientInputStream, soup_cache_client_input_stream, G_TYPE_FILTER_INPUT_STREAM)
25
26 static void
27 soup_cache_client_input_stream_init (SoupCacheClientInputStream *stream)
28 {
29 }
30
31 static gssize
soup_cache_client_input_stream_read_fn(GInputStream * stream,void * buffer,gsize count,GCancellable * cancellable,GError ** error)32 soup_cache_client_input_stream_read_fn (GInputStream *stream,
33 void *buffer,
34 gsize count,
35 GCancellable *cancellable,
36 GError **error)
37 {
38 gssize nread;
39
40 nread = G_INPUT_STREAM_CLASS (soup_cache_client_input_stream_parent_class)->
41 read_fn (stream, buffer, count, cancellable, error);
42
43 if (nread == 0)
44 g_signal_emit (stream, signals[SIGNAL_EOF], 0);
45
46 return nread;
47 }
48
49
50 static gboolean
soup_cache_client_input_stream_close_fn(GInputStream * stream,GCancellable * cancellable,GError ** error)51 soup_cache_client_input_stream_close_fn (GInputStream *stream,
52 GCancellable *cancellable,
53 GError **error)
54 {
55 gboolean success;
56
57 success = G_INPUT_STREAM_CLASS (soup_cache_client_input_stream_parent_class)->
58 close_fn (stream, cancellable, error);
59
60 g_signal_emit (stream, signals[SIGNAL_CLOSED], 0);
61
62 return success;
63 }
64
65 static void
soup_cache_client_input_stream_class_init(SoupCacheClientInputStreamClass * stream_class)66 soup_cache_client_input_stream_class_init (SoupCacheClientInputStreamClass *stream_class)
67 {
68 GObjectClass *object_class = G_OBJECT_CLASS (stream_class);
69 GInputStreamClass *input_stream_class = G_INPUT_STREAM_CLASS (stream_class);
70
71 input_stream_class->read_fn = soup_cache_client_input_stream_read_fn;
72 input_stream_class->close_fn = soup_cache_client_input_stream_close_fn;
73
74 signals[SIGNAL_EOF] =
75 g_signal_new ("eof",
76 G_OBJECT_CLASS_TYPE (object_class),
77 G_SIGNAL_RUN_LAST,
78 0,
79 NULL, NULL,
80 NULL,
81 G_TYPE_NONE, 0);
82 signals[SIGNAL_CLOSED] =
83 g_signal_new ("closed",
84 G_OBJECT_CLASS_TYPE (object_class),
85 G_SIGNAL_RUN_LAST,
86 0,
87 NULL, NULL,
88 NULL,
89 G_TYPE_NONE, 0);
90 }
91
92 GInputStream *
soup_cache_client_input_stream_new(GInputStream * base_stream)93 soup_cache_client_input_stream_new (GInputStream *base_stream)
94 {
95 return g_object_new (SOUP_TYPE_CACHE_CLIENT_INPUT_STREAM,
96 "base-stream", base_stream,
97 NULL);
98 }
99