* Fixes NB#91094, removed a comment
[modest] / src / modest-count-stream.c
1 /* Copyright (c) 2007, Nokia Corporation
2  * All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions are
6  * met:
7  *
8  * * Redistributions of source code must retain the above copyright
9  *   notice, this list of conditions and the following disclaimer.
10  * * Redistributions in binary form must reproduce the above copyright
11  *   notice, this list of conditions and the following disclaimer in the
12  *   documentation and/or other materials provided with the distribution.
13  * * Neither the name of the Nokia Corporation nor the names of its
14  *   contributors may be used to endorse or promote products derived from
15  *   this software without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
18  * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
19  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
20  * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
21  * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
22  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
23  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
24  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
25  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
26  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
27  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28  */
29
30 /* modest-count-stream.h */
31
32 #include <config.h>
33 #include <glib.h>
34 #include <glib/gi18n-lib.h>
35
36 #include <tny-stream.h>
37 #include "modest-count-stream.h"
38
39 typedef struct _ModestCountStreamPrivate ModestCountStreamPrivate;
40 struct _ModestCountStreamPrivate {
41         gsize count;
42 };
43 #define MODEST_COUNT_STREAM_GET_PRIVATE(o)      (G_TYPE_INSTANCE_GET_PRIVATE((o), \
44                                                        MODEST_TYPE_COUNT_STREAM, \
45                                                        ModestCountStreamPrivate))
46
47 static GObjectClass *parent_class = NULL;
48
49 static gssize
50 modest_count_stream_read (TnyStream *self, char *buffer, gsize n)
51 {
52         return 0;
53 }
54
55 static gssize
56 modest_count_stream_write (TnyStream *self, const char *buffer, gsize n)
57 {
58         ModestCountStreamPrivate *priv;
59         priv = MODEST_COUNT_STREAM_GET_PRIVATE(self);
60
61         priv->count += n;
62         return (gssize)n;
63 }
64
65 static gint
66 modest_count_stream_flush (TnyStream *self)
67 {
68         return 0;
69 }
70
71 static gint
72 modest_count_stream_close (TnyStream *self)
73 {
74         return 0;
75 }
76
77 static gboolean
78 modest_count_stream_is_eos (TnyStream *self)
79 {
80         return TRUE;
81 }
82
83 static gint
84 modest_count_stream_reset (TnyStream *self)
85 {
86         return 0;
87 }
88
89 static gssize
90 modest_count_stream_write_to_stream (TnyStream *self, TnyStream *output)
91 {
92         return 0;
93 }
94
95 /**
96  * modest_count_stream_get_count
97  * @self: the ModestCountStream
98  * 
99  * returns the number of bytes that have been written through this stream
100  * 
101  * Returns: number of bytes that have passed through this stream
102  */
103 gsize
104 modest_count_stream_get_count (ModestCountStream *self)
105 {
106         ModestCountStreamPrivate *priv;
107         priv = MODEST_COUNT_STREAM_GET_PRIVATE(self);
108
109         return priv->count;
110 }
111
112 /**
113  * modest_count_stream_reset_count
114  * @self: the ModestCountStream
115  * 
116  * resets the internal counter
117  * 
118  * Returns:
119  */
120 void
121 modest_count_stream_reset_count (ModestCountStream *self)
122 {
123         ModestCountStreamPrivate *priv;
124         priv = MODEST_COUNT_STREAM_GET_PRIVATE(self);
125
126         priv->count = 0;
127 }
128
129 TnyStream*
130 modest_count_stream_new ()
131 {
132         return TNY_STREAM (g_object_new (MODEST_TYPE_COUNT_STREAM, NULL)); 
133 }
134
135 static void
136 modest_count_stream_finalize (GObject *object)
137 {
138         parent_class->finalize (object);
139 }
140 static void
141 modest_count_stream_instance_init (GTypeInstance *instance, gpointer g_class)
142 {
143         ModestCountStreamPrivate *priv;
144         priv = MODEST_COUNT_STREAM_GET_PRIVATE(instance);
145
146         priv->count = 0;
147 }
148
149 static void
150 tny_stream_init (gpointer g_iface, gpointer iface_data)
151 {
152         TnyStreamIface *klass = (TnyStreamIface *) g_iface;
153
154         klass->read = modest_count_stream_read;
155         klass->write = modest_count_stream_write;
156         klass->flush = modest_count_stream_flush;
157         klass->close = modest_count_stream_close;
158         klass->is_eos = modest_count_stream_is_eos;
159         klass->reset = modest_count_stream_reset;
160         klass->write_to_stream = modest_count_stream_write_to_stream;
161 }
162
163 static void
164 modest_count_stream_class_init (ModestCountStreamClass *klass)
165 {
166         GObjectClass *object_class;
167
168         parent_class = g_type_class_peek_parent (klass);
169         object_class = (GObjectClass*) klass;
170         object_class->finalize = modest_count_stream_finalize;
171         
172         g_type_class_add_private (object_class, sizeof(ModestCountStream));
173 }
174 GType
175 modest_count_stream_get_type (void)
176 {
177         static GType type = 0;
178         if (G_UNLIKELY(type == 0))
179         {
180                 static const GTypeInfo info =
181                 {
182                         sizeof (ModestCountStreamClass),
183                         NULL,   /* base_init */
184                         NULL,   /* base_finalize */
185                         (GClassInitFunc) modest_count_stream_class_init,   /* class_init */
186                         NULL,   /* class_finalize */
187                         NULL,   /* class_data */
188                         sizeof (ModestCountStream),
189                         0,      /* n_preallocs */
190                         modest_count_stream_instance_init,    /* instance_init */
191                         NULL
192                 };
193
194
195                 static const GInterfaceInfo tny_stream_info =
196                 {
197                         (GInterfaceInitFunc) tny_stream_init, /* interface_init */
198                         NULL,         /* interface_finalize */
199                         NULL          /* interface_data */
200                 };
201
202                 type = g_type_register_static (G_TYPE_OBJECT,
203                                                "ModestCountStream",
204                                                &info, 0);
205
206                 g_type_add_interface_static (type, TNY_TYPE_STREAM,
207                         &tny_stream_info);
208
209         }
210         return type;
211 }