updated changelog
[platform/upstream/evolution-data-server.git] / camel / camel-stream-null.c
1 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8; fill-column: 160 -*- */
2 /* camel-stream.c : abstract class for a stream */
3
4 /*
5  * Author:
6  *  Michael Zucchi <notzed@ximian.com>
7  *
8  * Copyright (C) 1999-2008 Novell, Inc. (www.novell.com)
9  *
10  * This library is free software you can redistribute it and/or modify it
11  * under the terms of the GNU Lesser General Public License as published by
12  * the Free Software Foundation.
13  *
14  * This library is distributed in the hope that it will be useful, but
15  * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
16  * or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
17  *for more details.
18  *
19  * You should have received a copy of the GNU Lesser General Public License
20  * along with this program; if not, see <http://www.gnu.org/licenses/>.
21  */
22
23 #ifdef HAVE_CONFIG_H
24 #include <config.h>
25 #endif
26
27 #include <glib/gi18n-lib.h>
28
29 #include "camel-stream-null.h"
30
31 static void camel_stream_null_seekable_init (GSeekableIface *iface);
32
33 G_DEFINE_TYPE_WITH_CODE (CamelStreamNull, camel_stream_null, CAMEL_TYPE_STREAM,
34         G_IMPLEMENT_INTERFACE (G_TYPE_SEEKABLE, camel_stream_null_seekable_init))
35
36 static gssize
37 stream_null_write (CamelStream *stream,
38                    const gchar *buffer,
39                    gsize n,
40                    GCancellable *cancellable,
41                    GError **error)
42 {
43         CAMEL_STREAM_NULL (stream)->written += n;
44
45         return n;
46 }
47
48 static gboolean
49 stream_null_eos (CamelStream *stream)
50 {
51         return TRUE;
52 }
53
54 static goffset
55 stream_null_tell (GSeekable *seekable)
56 {
57         return 0;
58 }
59
60 static gboolean
61 stream_null_can_seek (GSeekable *seekable)
62 {
63         return TRUE;
64 }
65
66 static gboolean
67 stream_null_seek (GSeekable *seekable,
68                   goffset offset,
69                   GSeekType type,
70                   GCancellable *cancellable,
71                   GError **error)
72 {
73         if (type != G_SEEK_SET || offset != 0) {
74                 g_set_error_literal (
75                         error, G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
76                         _("Only reset to beginning is supported with CamelHttpStream"));
77                 return FALSE;
78         }
79
80         CAMEL_STREAM_NULL (seekable)->written = 0;
81
82         return TRUE;
83 }
84
85 static gboolean
86 stream_null_can_truncate (GSeekable *seekable)
87 {
88         return FALSE;
89 }
90
91 static gboolean
92 stream_null_truncate_fn (GSeekable *seekable,
93                          goffset offset,
94                          GCancellable *cancellable,
95                          GError **error)
96 {
97         /* XXX Don't bother translating this.  Camel never calls it. */
98         g_set_error_literal (
99                 error, G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
100                 "Truncation is not supported");
101
102         return FALSE;
103 }
104
105 static void
106 camel_stream_null_class_init (CamelStreamNullClass *class)
107 {
108         CamelStreamClass *stream_class;
109
110         stream_class = CAMEL_STREAM_CLASS (class);
111         stream_class->write = stream_null_write;
112         stream_class->eos = stream_null_eos;
113 }
114
115 static void
116 camel_stream_null_seekable_init (GSeekableIface *iface)
117 {
118         iface->tell = stream_null_tell;
119         iface->can_seek = stream_null_can_seek;
120         iface->seek = stream_null_seek;
121         iface->can_truncate = stream_null_can_truncate;
122         iface->truncate_fn = stream_null_truncate_fn;
123 }
124
125 static void
126 camel_stream_null_init (CamelStreamNull *stream_null)
127 {
128 }
129
130 /**
131  * camel_stream_null_new:
132  *
133  * Returns a null stream.  A null stream is always at eof, and
134  * always returns success for all reads and writes.
135  *
136  * Returns: a new #CamelStreamNull
137  **/
138 CamelStream *
139 camel_stream_null_new (void)
140 {
141         return g_object_new (CAMEL_TYPE_STREAM_NULL, NULL);
142 }