root/trunk/libtinymailui-gtkhtml/tny-gtk-html-stream.c

Revision 3666 (checked in by jdapena, 7 months ago)

* Use GOnce registering all types in tinymail to make

registering thread-safe.

Line 
1 /* libtinymailui-webkit - The Tiny Mail UI library for Webkit
2  * Copyright (C) 2006-2007 Philip Van Hoof <pvanhoof@gnome.org>
3  *
4  * This component was developed based on Modest's ModestTnyStreamGtkHtml
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2 of the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with self library; if not, write to the
18  * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
19  * Boston, MA 02110-1301, USA.
20  */
21
22 #include <config.h>
23 #include <glib/gi18n-lib.h>
24
25 #ifndef _GNU_SOURCE
26 #define _GNU_SOURCE
27 #endif
28
29 #include <string.h>
30
31 #include <glib.h>
32 #include <glib/gstdio.h>
33 #include <stdlib.h>
34 #include <gtk/gtk.h>
35
36 #include <tny-stream.h>
37 #include <tny-gtk-html-stream.h>
38
39 static GObjectClass *parent_class = NULL;
40
41
42 struct _TnyGtkHtmlStreamPriv {
43         GtkHTMLStream *stream;
44 };
45
46 #define TNY_GTK_HTML_STREAM_GET_PRIVATE(o) ((TnyGtkHtmlStream*)(o))->priv
47
48 static ssize_t
49 tny_gtk_html_stream_write_to_stream (TnyStream *self, TnyStream *output)
50 {
51         char tmp_buf[4096];
52         ssize_t total = 0;
53         ssize_t nb_read;
54         ssize_t nb_written;
55
56         g_assert (TNY_IS_STREAM (output));
57
58         while (G_LIKELY (!tny_stream_is_eos (self)))
59         {
60                 nb_read = tny_stream_read (self, tmp_buf, sizeof (tmp_buf));
61                 if (G_UNLIKELY (nb_read < 0))
62                         return -1;
63                 else if (G_LIKELY (nb_read > 0)) {
64                         nb_written = 0;
65        
66                         while (G_LIKELY (nb_written < nb_read))
67                         {
68                                 ssize_t len = tny_stream_write (output, tmp_buf + nb_written,
69                                                                   nb_read - nb_written);
70                                 if (G_UNLIKELY (len < 0))
71                                         return -1;
72                                 nb_written += len;
73                         }
74                         total += nb_written;
75                 }
76         }
77         return total;
78 }
79
80 static ssize_t
81 tny_gtk_html_stream_read  (TnyStream *self, char *data, size_t n)
82 {
83         return -1;
84 }
85
86 static gint
87 tny_gtk_html_stream_reset (TnyStream *self)
88 {
89         return 0;
90 }
91
92 static ssize_t
93 tny_gtk_html_stream_write (TnyStream *self, const char *data, size_t n)
94 {
95         TnyGtkHtmlStreamPriv *priv = TNY_GTK_HTML_STREAM_GET_PRIVATE (self);
96
97         if (!priv->stream) {
98                 g_print ("modest: cannot write to closed stream\n");
99                 return 0;
100         }
101
102         if (n == 0 || !data)
103                 return 0;
104                
105         gtk_html_stream_write (priv->stream, data, n);
106
107         return (ssize_t) n;
108 }
109
110 static gint
111 tny_gtk_html_stream_flush (TnyStream *self)
112 {
113         return 0;
114 }
115
116 static gint
117 tny_gtk_html_stream_close (TnyStream *self)
118 {
119         TnyGtkHtmlStreamPriv *priv = TNY_GTK_HTML_STREAM_GET_PRIVATE (self);
120
121         gtk_html_stream_close (priv->stream, GTK_HTML_STREAM_OK);
122         priv->stream = NULL;
123
124         return 0;
125 }
126
127 static gboolean
128 tny_gtk_html_stream_is_eos (TnyStream *self)
129 {
130         return TRUE;
131 }
132
133
134
135 /**
136  * tny_gtk_html_stream_new:
137  * @stream: a #GtkHTMLStream
138  *
139  * Create an adaptor instance between #TnyStream and #GtkHtmlStream
140  *
141  * Return value: a new #TnyStream instance
142  **/
143 TnyStream*
144 tny_gtk_html_stream_new (GtkHTMLStream *stream)
145 {
146         TnyGtkHtmlStream *self = g_object_new (TNY_TYPE_GTK_HTML_STREAM, NULL);
147         TnyGtkHtmlStreamPriv *priv = TNY_GTK_HTML_STREAM_GET_PRIVATE (self);
148         priv->stream = stream;
149         return TNY_STREAM (self);
150 }
151
152 static void
153 tny_gtk_html_stream_instance_init (GTypeInstance *instance, gpointer g_class)
154 {
155         TnyGtkHtmlStream *self = (TnyGtkHtmlStream *)instance;
156         TnyGtkHtmlStreamPriv *priv = g_slice_new (TnyGtkHtmlStreamPriv);
157         priv->stream = NULL;
158         self->priv = priv;
159         return;
160 }
161
162 static void
163 tny_gtk_html_stream_finalize (GObject *object)
164 {
165         TnyGtkHtmlStream *self = (TnyGtkHtmlStream *)object;
166         TnyGtkHtmlStreamPriv *priv = TNY_GTK_HTML_STREAM_GET_PRIVATE (self);
167         priv->stream = NULL;
168         g_slice_free (TnyGtkHtmlStreamPriv, priv);
169         (*parent_class->finalize) (object);
170         return;
171 }
172
173 static void
174 tny_stream_init (gpointer g, gpointer iface_data)
175 {
176         TnyStreamIface *klass = (TnyStreamIface *)g;
177
178         klass->read= tny_gtk_html_stream_read;
179         klass->write= tny_gtk_html_stream_write;
180         klass->flush= tny_gtk_html_stream_flush;
181         klass->close= tny_gtk_html_stream_close;
182         klass->is_eos= tny_gtk_html_stream_is_eos;
183         klass->reset= tny_gtk_html_stream_reset;
184         klass->write_to_stream= tny_gtk_html_stream_write_to_stream;
185
186         return;
187 }
188
189 static void
190 tny_gtk_html_stream_class_init (TnyGtkHtmlStreamClass *class)
191 {
192         GObjectClass *object_class;
193
194         parent_class = g_type_class_peek_parent (class);
195         object_class = (GObjectClass*) class;
196
197         object_class->finalize = tny_gtk_html_stream_finalize;
198
199         return;
200 }
201
202 static gpointer
203 tny_gtk_html_stream_register_type (gpointer notused)
204 {
205         GType type = 0;
206
207         static const GTypeInfo info =
208                 {
209                   sizeof (TnyGtkHtmlStreamClass),
210                   NULL,   /* base_init */
211                   NULL,   /* base_finalize */
212                   (GClassInitFunc) tny_gtk_html_stream_class_init,   /* class_init */
213                   NULL,   /* class_finalize */
214                   NULL,   /* class_data */
215                   sizeof (TnyGtkHtmlStream),
216                   0,      /* n_preallocs */
217                   tny_gtk_html_stream_instance_init    /* instance_init */
218                 };
219
220         static const GInterfaceInfo tny_stream_info =
221                 {
222                   (GInterfaceInitFunc) tny_stream_init, /* interface_init */
223                   NULL,         /* interface_finalize */
224                   NULL          /* interface_data */
225                 };
226
227         type = g_type_register_static (G_TYPE_OBJECT,
228                                        "TnyGtkHtmlStream",
229                                        &info, 0);
230
231         g_type_add_interface_static (type, TNY_TYPE_STREAM,
232                                      &tny_stream_info);
233
234         return GUINT_TO_POINTER (type);
235 }
236
237 GType
238 tny_gtk_html_stream_get_type (void)
239 {
240         static GOnce once = G_ONCE_INIT;
241         g_once (&once, tny_gtk_html_stream_register_type, NULL);
242         return GPOINTER_TO_UINT (once.retval);
243 }
Note: See TracBrowser for help on using the browser.