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