Adapted to API change for Tinymail rev 3304
[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 (TnyStreamIface *klass)
151 {
152         klass->read = modest_count_stream_read;
153         klass->write = modest_count_stream_write;
154         klass->flush = modest_count_stream_flush;
155         klass->close = modest_count_stream_close;
156         klass->is_eos = modest_count_stream_is_eos;
157         klass->reset = modest_count_stream_reset;
158         klass->write_to_stream = modest_count_stream_write_to_stream;
159 }
160
161 static void
162 modest_count_stream_class_init (ModestCountStreamClass *klass)
163 {
164         GObjectClass *object_class;
165
166         parent_class = g_type_class_peek_parent (klass);
167         object_class = (GObjectClass*) klass;
168         object_class->finalize = modest_count_stream_finalize;
169         
170         g_type_class_add_private (object_class, sizeof(ModestCountStream));
171 }
172 GType
173 modest_count_stream_get_type (void)
174 {
175         static GType type = 0;
176         if (G_UNLIKELY(type == 0))
177         {
178                 static const GTypeInfo info =
179                 {
180                         sizeof (ModestCountStreamClass),
181                         NULL,   /* base_init */
182                         NULL,   /* base_finalize */
183                         (GClassInitFunc) modest_count_stream_class_init,   /* class_init */
184                         NULL,   /* class_finalize */
185                         NULL,   /* class_data */
186                         sizeof (ModestCountStream),
187                         0,      /* n_preallocs */
188                         modest_count_stream_instance_init,    /* instance_init */
189                         NULL
190                 };
191
192
193                 static const GInterfaceInfo tny_stream_info =
194                 {
195                         (GInterfaceInitFunc) tny_stream_init, /* interface_init */
196                         NULL,         /* interface_finalize */
197                         NULL          /* interface_data */
198                 };
199
200                 type = g_type_register_static (G_TYPE_OBJECT,
201                         "ModestCountStream",
202                         &info, 0);
203
204                 /* TODO? : FIX THIS (ADD _TYPE): */
205                 g_type_add_interface_static (type, TNY_TYPE_STREAM,
206                         &tny_stream_info);
207
208         }
209         return type;
210 }