Modified webpage: now tinymail repository is in gitorious.
[modest] / src / widgets / modest-tny-stream-webkit.c
1 /* Copyright (c) 2009, 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
31 /* modest-tny-stream-webkit.c */
32
33 #include "modest-tny-stream-webkit.h"
34 #include "modest-webkit-mime-part-view.h"
35 #include <tny-stream.h>
36
37 /* 'private'/'protected' functions */
38 static void  modest_tny_stream_webkit_class_init   (ModestTnyStreamWebkitClass *klass);
39 static void  modest_tny_stream_webkit_init         (ModestTnyStreamWebkit *obj);
40 static void  modest_tny_stream_webkit_finalize     (GObject *obj);
41
42 static void  modest_tny_stream_gtkhml_iface_init (gpointer g_iface, gpointer iface_data);
43
44 static void  stop_streams (ModestWebkitMimePartView *view, gpointer userdata);
45
46 /* list my signals */
47 enum {
48         /* MY_SIGNAL_1, */
49         /* MY_SIGNAL_2, */
50         LAST_SIGNAL
51 };
52
53 typedef struct _ModestTnyStreamWebkitPrivate ModestTnyStreamWebkitPrivate;
54 struct _ModestTnyStreamWebkitPrivate {
55         GString *buffer;
56         GtkWidget *webview;
57
58         gchar *mime_type;
59         gchar *encoding;
60
61         guint stop_streams_id;
62
63         gssize max_size;
64         gssize current_size;
65 };
66 #define MODEST_TNY_STREAM_WEBKIT_GET_PRIVATE(o)      (G_TYPE_INSTANCE_GET_PRIVATE((o), \
67                                                        MODEST_TYPE_TNY_STREAM_WEBKIT, \
68                                                        ModestTnyStreamWebkitPrivate))
69 /* globals */
70 static GObjectClass *parent_class = NULL;
71
72 /* uncomment the following if you have defined any signals */
73 /* static guint signals[LAST_SIGNAL] = {0}; */
74
75 GType
76 modest_tny_stream_webkit_get_type (void)
77 {
78         static GType my_type = 0;
79         if (!my_type) {
80                 static const GTypeInfo my_info = {
81                         sizeof(ModestTnyStreamWebkitClass),
82                         NULL,           /* base init */
83                         NULL,           /* base finalize */
84                         (GClassInitFunc) modest_tny_stream_webkit_class_init,
85                         NULL,           /* class finalize */
86                         NULL,           /* class data */
87                         sizeof(ModestTnyStreamWebkit),
88                         1,              /* n_preallocs */
89                         (GInstanceInitFunc) modest_tny_stream_webkit_init,
90                         NULL
91                 };
92
93                 static const GInterfaceInfo iface_info = {
94                         (GInterfaceInitFunc) modest_tny_stream_gtkhml_iface_init,
95                         NULL,         /* interface_finalize */
96                         NULL          /* interface_data */
97                 };
98
99                 my_type = g_type_register_static (G_TYPE_OBJECT,
100                                                   "ModestTnyStreamWebkit",
101                                                   &my_info, 0);
102
103                 g_type_add_interface_static (my_type, TNY_TYPE_STREAM,
104                                              &iface_info);
105
106         }
107         return my_type;
108 }
109
110 static void
111 modest_tny_stream_webkit_class_init (ModestTnyStreamWebkitClass *klass)
112 {
113         GObjectClass *gobject_class;
114         gobject_class = (GObjectClass*) klass;
115
116         parent_class            = g_type_class_peek_parent (klass);
117         gobject_class->finalize = modest_tny_stream_webkit_finalize;
118
119         g_type_class_add_private (gobject_class, sizeof(ModestTnyStreamWebkitPrivate));
120 }
121
122 static void
123 modest_tny_stream_webkit_init (ModestTnyStreamWebkit *obj)
124 {
125         ModestTnyStreamWebkitPrivate *priv;
126         priv = MODEST_TNY_STREAM_WEBKIT_GET_PRIVATE(obj);
127
128         priv->buffer  = NULL;
129         priv->webview = NULL;
130         priv->stop_streams_id = 0;
131
132         priv->max_size = 0;
133         priv->current_size = 0;
134
135         priv->mime_type = NULL;
136         priv->encoding = NULL;
137 }
138
139 static void
140 modest_tny_stream_webkit_finalize (GObject *obj)
141 {
142         ModestTnyStreamWebkitPrivate *priv;
143
144         priv = MODEST_TNY_STREAM_WEBKIT_GET_PRIVATE(obj);
145         if (priv->buffer)
146                 g_string_free (priv->buffer, TRUE);
147
148         g_free (priv->mime_type);
149         g_free (priv->encoding);
150
151         if (priv->stop_streams_id > 0) {
152                 g_signal_handler_disconnect (G_OBJECT (priv->webview), priv->stop_streams_id);
153                 priv->stop_streams_id = 0;
154         }
155
156         if (priv->webview) {
157                 g_object_unref (priv->webview);
158                 priv->webview = NULL;
159         }
160 }
161
162 GObject*
163 modest_tny_stream_webkit_new (WebKitWebView *webview, const gchar *mime_type, const gchar *encoding)
164 {
165         GObject *obj;
166         ModestTnyStreamWebkitPrivate *priv;
167         
168         obj  = G_OBJECT(g_object_new(MODEST_TYPE_TNY_STREAM_WEBKIT, NULL));
169         priv = MODEST_TNY_STREAM_WEBKIT_GET_PRIVATE(obj);
170
171         g_return_val_if_fail (webview, NULL);
172
173         priv->buffer = g_string_new ("");
174         
175         priv->webview = g_object_ref (webview);
176         priv->mime_type = g_strdup (mime_type);
177         priv->encoding = g_strdup (encoding);
178
179         priv->stop_streams_id = g_signal_connect (G_OBJECT (webview), "stop-streams",
180                                                   G_CALLBACK (stop_streams), obj);
181         priv->current_size = 0;
182
183         return obj;
184 }
185
186
187 /* the rest are interface functions */
188
189
190 static ssize_t
191 webkit_read (TnyStream *self, char *buffer, size_t n)
192 {
193         return -1; /* we cannot read */
194 }
195
196
197 static ssize_t
198 webkit_write (TnyStream *self, const char *buffer, size_t n)
199 {
200         ModestTnyStreamWebkitPrivate *priv;
201         
202         priv = MODEST_TNY_STREAM_WEBKIT_GET_PRIVATE(self);
203
204         if (!priv->buffer) {
205                 g_print ("modest: cannot write to closed stream\n");
206                 return 0;
207         }
208
209         if (n == 0 || !buffer)
210                 return 0;
211
212         if (!priv->webview || !GTK_WIDGET_VISIBLE (priv->webview))
213                 return -1;
214
215         if (priv->max_size > 0) {
216
217                 /* We only use the maximum size for write method, and even we
218                  * ignore and fake as we would do a successfull read */
219                 if (priv->current_size >= priv->max_size)
220                         return n;
221
222                 if (priv->current_size + n > priv->max_size)
223                         n = priv->max_size - priv->current_size;
224         }
225
226         if (!g_main_context_is_owner (NULL))
227                 gdk_threads_enter ();
228
229         priv->buffer = g_string_append_len (priv->buffer, buffer, n);
230
231         if (!g_main_context_is_owner (NULL))
232                 gdk_threads_leave ();
233         priv->current_size += n;
234
235         return n; /* hmmm */
236 }
237
238         
239 static gint
240 webkit_flush (TnyStream *self)
241 {
242         return 0;
243 }
244         
245
246 static gint
247 webkit_close (TnyStream *self)
248 {
249         ModestTnyStreamWebkitPrivate *priv;
250         g_return_val_if_fail (self, 0);
251         priv = MODEST_TNY_STREAM_WEBKIT_GET_PRIVATE(self);
252         
253         if (priv->stop_streams_id > 0) {
254                 g_signal_handler_disconnect (G_OBJECT (priv->webview), priv->stop_streams_id);
255                 priv->stop_streams_id = 0;
256         }
257         if (priv->webview) {
258                 webkit_web_view_load_string (WEBKIT_WEB_VIEW (priv->webview), priv->buffer->str, priv->mime_type, priv->encoding, NULL);
259
260                 g_object_unref (priv->webview);
261                 priv->webview = NULL;
262         }
263         if (priv->buffer)
264                 g_string_free (priv->buffer, TRUE);
265         priv->buffer = NULL;
266
267         return 0;
268 }
269
270
271 static gboolean
272 webkit_is_eos (TnyStream *self)
273 {
274         return TRUE;
275 }
276
277
278         
279 static gint
280 webkit_reset (TnyStream *self)
281 {
282         return 0;
283 }
284
285         
286 static ssize_t
287 webkit_write_to_stream (TnyStream *self, TnyStream *output)
288 {
289         return 0;
290 }
291
292 static void
293 stop_streams (ModestWebkitMimePartView *view, gpointer userdata)
294 {
295         ModestTnyStreamWebkit *self = (ModestTnyStreamWebkit *) userdata;
296         ModestTnyStreamWebkitPrivate *priv;
297         
298         g_return_if_fail (self);
299         priv = MODEST_TNY_STREAM_WEBKIT_GET_PRIVATE(self);
300
301         if (priv->webview && priv->stop_streams_id > 0) {
302                 g_signal_handler_disconnect (G_OBJECT (priv->webview), priv->stop_streams_id);
303                 priv->stop_streams_id = 0;
304         }
305         
306         if (priv->webview) {
307                 g_object_unref (priv->webview);
308                 priv->webview = NULL;
309         }
310 }
311
312 void 
313 modest_tny_stream_webkit_set_max_size (ModestTnyStreamWebkit *stream, 
314                                         gssize max_size)
315 {
316         ModestTnyStreamWebkitPrivate *priv;
317
318         g_return_if_fail (MODEST_IS_TNY_STREAM_WEBKIT (stream));
319         priv = MODEST_TNY_STREAM_WEBKIT_GET_PRIVATE (stream);
320
321         priv->max_size = max_size;
322 }
323
324 gssize 
325 modest_tny_stream_webkit_get_max_size (ModestTnyStreamWebkit *stream)
326 {
327         ModestTnyStreamWebkitPrivate *priv;
328
329         g_return_val_if_fail (MODEST_IS_TNY_STREAM_WEBKIT (stream), 0);
330         priv = MODEST_TNY_STREAM_WEBKIT_GET_PRIVATE (stream);
331
332         return priv->max_size;
333 }
334
335 gboolean
336 modest_tny_stream_webkit_limit_reached (ModestTnyStreamWebkit *self)
337 {
338         ModestTnyStreamWebkitPrivate *priv;
339
340         g_return_val_if_fail (MODEST_IS_TNY_STREAM_WEBKIT (self), 0);
341         priv = MODEST_TNY_STREAM_WEBKIT_GET_PRIVATE (self);
342
343         return (priv->max_size > 0) && (priv->current_size >= priv->max_size);
344 }
345
346 static void
347 modest_tny_stream_gtkhml_iface_init (gpointer g_iface, gpointer iface_data)
348 {
349         TnyStreamIface *klass;
350         
351         g_return_if_fail (g_iface);
352
353         klass = (TnyStreamIface*) g_iface;
354         
355         klass->read            = webkit_read;
356         klass->write           = webkit_write;
357         klass->flush           = webkit_flush;
358         klass->close           = webkit_close;
359         klass->is_eos          = webkit_is_eos;
360         klass->reset           = webkit_reset;
361         klass->write_to_stream = webkit_write_to_stream;
362 }