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