b54609484c9ec10724eade38be420da2a873fb81
[modest] / src / widgets / modest-gtkhtml-mime-part-view.c
1 /* Copyright (c) 2006, 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 #include <config.h>
31 #include <widgets/modest-gtkhtml-mime-part-view.h>
32 #include <string.h>
33 #include <gtkhtml/gtkhtml-stream.h>
34 #include <gtkhtml/gtkhtml-search.h>
35 #include <tny-stream.h>
36 #include <tny-mime-part-view.h>
37 #include "modest-tny-mime-part.h"
38 #include <modest-stream-text-to-html.h>
39 #include <modest-text-utils.h>
40 #include <modest-conf.h>
41 #include <modest-runtime.h>
42 #include <widgets/modest-mime-part-view.h>
43 #include <widgets/modest-zoomable.h>
44 #include <widgets/modest-tny-stream-gtkhtml.h>
45 #include <libgnomevfs/gnome-vfs.h>
46 #include <gdk/gdkkeysyms.h>
47 #include <modest-ui-constants.h>
48
49 /* gobject structure methods */
50 static void    modest_gtkhtml_mime_part_view_class_init (ModestGtkhtmlMimePartViewClass *klass);
51 static void    tny_mime_part_view_init                  (gpointer g, gpointer iface_data);
52 static void    modest_mime_part_view_init               (gpointer g, gpointer iface_data);
53 static void    modest_zoomable_init                     (gpointer g, gpointer iface_data);
54 static void    modest_isearch_view_init                 (gpointer g, gpointer iface_data);
55 static void    modest_gtkhtml_mime_part_view_init       (ModestGtkhtmlMimePartView *self);
56 static void    modest_gtkhtml_mime_part_view_finalize   (GObject *self);
57 static void    modest_gtkhtml_mime_part_view_dispose    (GObject *self);
58
59 /* GtkHTML signal handlers */
60 static gboolean  on_link_clicked  (GtkWidget *widget, const gchar *uri, ModestGtkhtmlMimePartView *self);
61 static gboolean  on_url           (GtkWidget *widget, const gchar *uri, ModestGtkhtmlMimePartView *self);
62 static gboolean  on_url_requested (GtkWidget *widget, const gchar *uri, GtkHTMLStream *stream,
63                                    ModestGtkhtmlMimePartView *self);
64 static void      on_notify_style  (GObject *obj, GParamSpec *spec, gpointer userdata);
65 static gboolean  update_style     (ModestGtkhtmlMimePartView *self);
66 /* TnyMimePartView implementation */
67 static void modest_gtkhtml_mime_part_view_clear (TnyMimePartView *self);
68 static void modest_gtkhtml_mime_part_view_clear_default (TnyMimePartView *self);
69 static void modest_gtkhtml_mime_part_view_set_part (TnyMimePartView *self, TnyMimePart *part);
70 static void modest_gtkhtml_mime_part_view_set_part_default (TnyMimePartView *self, TnyMimePart *part);
71 static TnyMimePart* modest_gtkhtml_mime_part_view_get_part (TnyMimePartView *self);
72 static TnyMimePart* modest_gtkhtml_mime_part_view_get_part_default (TnyMimePartView *self);
73 /* ModestMimePartView implementation */
74 static gboolean modest_gtkhtml_mime_part_view_is_empty (ModestMimePartView *self);
75 static gboolean modest_gtkhtml_mime_part_view_is_empty_default (ModestMimePartView *self);
76 static gboolean modest_gtkhtml_mime_part_view_get_view_images (ModestMimePartView *self);
77 static gboolean modest_gtkhtml_mime_part_view_get_view_images_default (ModestMimePartView *self);
78 static void     modest_gtkhtml_mime_part_view_set_view_images (ModestMimePartView *self, gboolean view_images);
79 static void     modest_gtkhtml_mime_part_view_set_view_images_default (ModestMimePartView *self, gboolean view_images);
80 static gboolean modest_gtkhtml_mime_part_view_has_external_images (ModestMimePartView *self);
81 static gboolean modest_gtkhtml_mime_part_view_has_external_images_default (ModestMimePartView *self);
82 /* ModestZoomable implementation */
83 static gdouble modest_gtkhtml_mime_part_view_get_zoom (ModestZoomable *self);
84 static void modest_gtkhtml_mime_part_view_set_zoom (ModestZoomable *self, gdouble value);
85 static gboolean modest_gtkhtml_mime_part_view_zoom_minus (ModestZoomable *self);
86 static gboolean modest_gtkhtml_mime_part_view_zoom_plus (ModestZoomable *self);
87 static gdouble modest_gtkhtml_mime_part_view_get_zoom_default (ModestZoomable *self);
88 static void modest_gtkhtml_mime_part_view_set_zoom_default (ModestZoomable *self, gdouble value);
89 static gboolean modest_gtkhtml_mime_part_view_zoom_minus_default (ModestZoomable *self);
90 static gboolean modest_gtkhtml_mime_part_view_zoom_plus_default (ModestZoomable *self);
91 /* ModestISearchView implementation */
92 static gboolean modest_gtkhtml_mime_part_view_search                    (ModestISearchView *self, const gchar *string);
93 static gboolean modest_gtkhtml_mime_part_view_search_next               (ModestISearchView *self);
94 static gboolean modest_gtkhtml_mime_part_view_get_selection_area        (ModestISearchView *self, gint *x, gint *y, 
95                                                                          gint *width, gint *height);
96 static gboolean modest_gtkhtml_mime_part_view_search_default            (ModestISearchView *self, const gchar *string);
97 static gboolean modest_gtkhtml_mime_part_view_search_next_default       (ModestISearchView *self);
98 static gboolean modest_gtkhtml_mime_part_view_get_selection_area_default (ModestISearchView *self, gint *x, gint *y, 
99                                                                           gint *width, gint *height);
100
101
102 /* internal api */
103 static TnyMimePart  *get_part   (ModestGtkhtmlMimePartView *self);
104 static void          set_html_part   (ModestGtkhtmlMimePartView *self, TnyMimePart *part, const gchar *encoding);
105 static void          set_text_part   (ModestGtkhtmlMimePartView *self, TnyMimePart *part);
106 static void          set_empty_part  (ModestGtkhtmlMimePartView *self);
107 static void          set_part   (ModestGtkhtmlMimePartView *self, TnyMimePart *part);
108 static gboolean      is_empty   (ModestGtkhtmlMimePartView *self);
109 static gboolean      get_view_images   (ModestGtkhtmlMimePartView *self);
110 static void          set_view_images   (ModestGtkhtmlMimePartView *self, gboolean view_images);
111 static gboolean      has_external_images   (ModestGtkhtmlMimePartView *self);
112 static void          set_zoom   (ModestGtkhtmlMimePartView *self, gdouble zoom);
113 static gdouble       get_zoom   (ModestGtkhtmlMimePartView *self);
114 static gboolean      has_contents_receiver (gpointer engine, const gchar *data,
115                                             size_t len, gboolean *has_contents);
116 static gboolean      search             (ModestGtkhtmlMimePartView *self, const gchar *string);
117 static gboolean      search_next        (ModestGtkhtmlMimePartView *self);
118 static gboolean      get_selection_area (ModestGtkhtmlMimePartView *self, gint *x, gint *y,
119                                          gint *width, gint *height);
120
121 typedef struct _ModestGtkhtmlMimePartViewPrivate ModestGtkhtmlMimePartViewPrivate;
122 struct _ModestGtkhtmlMimePartViewPrivate {
123         TnyMimePart *part;
124         gdouble current_zoom;
125         gboolean view_images;
126         gboolean has_external_images;
127         GSList *sighandlers;
128 };
129
130 #define MODEST_GTKHTML_MIME_PART_VIEW_GET_PRIVATE(o)      (G_TYPE_INSTANCE_GET_PRIVATE((o), \
131                                                                                        MODEST_TYPE_GTKHTML_MIME_PART_VIEW, \
132                                                                                        ModestGtkhtmlMimePartViewPrivate))
133
134 enum {
135         STOP_STREAMS_SIGNAL,
136         LIMIT_ERROR_SIGNAL,
137         LAST_SIGNAL
138 };
139
140 static GtkHTMLClass *parent_class = NULL;
141
142 static guint signals[LAST_SIGNAL] = {0};
143
144 GtkWidget *
145 modest_gtkhtml_mime_part_view_new ()
146 {
147         return g_object_new (MODEST_TYPE_GTKHTML_MIME_PART_VIEW, NULL);
148 }
149
150 /* GOBJECT IMPLEMENTATION */
151 GType
152 modest_gtkhtml_mime_part_view_get_type (void)
153 {
154         static GType my_type = 0;
155         if (!my_type) {
156                 static const GTypeInfo my_info = {
157                         sizeof(ModestGtkhtmlMimePartViewClass),
158                         NULL,           /* base init */
159                         NULL,           /* base finalize */
160                         (GClassInitFunc) modest_gtkhtml_mime_part_view_class_init,
161                         NULL,           /* class finalize */
162                         NULL,           /* class data */
163                         sizeof(ModestGtkhtmlMimePartView),
164                         1,              /* n_preallocs */
165                         (GInstanceInitFunc) modest_gtkhtml_mime_part_view_init,
166                         NULL
167                 };
168
169                 static const GInterfaceInfo tny_mime_part_view_info = 
170                 {
171                   (GInterfaceInitFunc) tny_mime_part_view_init, /* interface_init */
172                   NULL,         /* interface_finalize */
173                   NULL          /* interface_data */
174                 };
175
176                 static const GInterfaceInfo modest_mime_part_view_info = 
177                 {
178                   (GInterfaceInitFunc) modest_mime_part_view_init, /* interface_init */
179                   NULL,         /* interface_finalize */
180                   NULL          /* interface_data */
181                 };
182
183                 static const GInterfaceInfo modest_zoomable_info = 
184                 {
185                   (GInterfaceInitFunc) modest_zoomable_init, /* interface_init */
186                   NULL,         /* interface_finalize */
187                   NULL          /* interface_data */
188                 };
189
190                 static const GInterfaceInfo modest_isearch_view_info = 
191                 {
192                   (GInterfaceInitFunc) modest_isearch_view_init, /* interface_init */
193                   NULL,         /* interface_finalize */
194                   NULL          /* interface_data */
195                 };
196
197                 my_type = g_type_register_static (GTK_TYPE_HTML,
198                                                   "ModestGtkhtmlMimePartView",
199                                                   &my_info, 0);
200
201                 g_type_add_interface_static (my_type, TNY_TYPE_MIME_PART_VIEW, 
202                         &tny_mime_part_view_info);
203
204                 g_type_add_interface_static (my_type, MODEST_TYPE_MIME_PART_VIEW, 
205                         &modest_mime_part_view_info);
206
207                 g_type_add_interface_static (my_type, MODEST_TYPE_ZOOMABLE, 
208                         &modest_zoomable_info);
209                 g_type_add_interface_static (my_type, MODEST_TYPE_ISEARCH_VIEW, 
210                         &modest_isearch_view_info);
211         }
212         return my_type;
213 }
214
215 static void
216 modest_gtkhtml_mime_part_view_class_init (ModestGtkhtmlMimePartViewClass *klass)
217 {
218         GObjectClass *gobject_class;
219         GtkBindingSet *binding_set;
220
221         gobject_class = (GObjectClass*) klass;
222
223         parent_class            = g_type_class_peek_parent (klass);
224         gobject_class->dispose = modest_gtkhtml_mime_part_view_dispose;
225         gobject_class->finalize = modest_gtkhtml_mime_part_view_finalize;
226
227         klass->get_part_func = modest_gtkhtml_mime_part_view_get_part_default;
228         klass->set_part_func = modest_gtkhtml_mime_part_view_set_part_default;
229         klass->clear_func = modest_gtkhtml_mime_part_view_clear_default;
230         klass->is_empty_func = modest_gtkhtml_mime_part_view_is_empty_default;
231         klass->get_view_images_func = modest_gtkhtml_mime_part_view_get_view_images_default;
232         klass->set_view_images_func = modest_gtkhtml_mime_part_view_set_view_images_default;
233         klass->has_external_images_func = modest_gtkhtml_mime_part_view_has_external_images_default;
234         klass->get_zoom_func = modest_gtkhtml_mime_part_view_get_zoom_default;
235         klass->set_zoom_func = modest_gtkhtml_mime_part_view_set_zoom_default;
236         klass->zoom_minus_func = modest_gtkhtml_mime_part_view_zoom_minus_default;
237         klass->zoom_plus_func = modest_gtkhtml_mime_part_view_zoom_plus_default;
238         klass->search_func = modest_gtkhtml_mime_part_view_search_default;
239         klass->search_next_func = modest_gtkhtml_mime_part_view_search_next_default;
240         klass->get_selection_area_func = modest_gtkhtml_mime_part_view_get_selection_area_default;
241
242         binding_set = gtk_binding_set_by_class (klass);
243         gtk_binding_entry_skip (binding_set, GDK_Down, 0);
244         gtk_binding_entry_skip (binding_set, GDK_Up, 0);
245         gtk_binding_entry_skip (binding_set, GDK_KP_Up, 0);
246         gtk_binding_entry_skip (binding_set, GDK_KP_Down, 0);
247         gtk_binding_entry_skip (binding_set, GDK_Page_Down, 0);
248         gtk_binding_entry_skip (binding_set, GDK_Page_Up, 0);
249         gtk_binding_entry_skip (binding_set, GDK_KP_Page_Up, 0);
250         gtk_binding_entry_skip (binding_set, GDK_KP_Page_Down, 0);
251         gtk_binding_entry_skip (binding_set, GDK_Home, 0);
252         gtk_binding_entry_skip (binding_set, GDK_End, 0);
253         gtk_binding_entry_skip (binding_set, GDK_KP_Home, 0);
254         gtk_binding_entry_skip (binding_set, GDK_KP_End, 0);
255
256         g_type_class_add_private (gobject_class, sizeof(ModestGtkhtmlMimePartViewPrivate));
257
258         signals[STOP_STREAMS_SIGNAL] = 
259                 g_signal_new ("stop-streams",
260                               G_TYPE_FROM_CLASS (gobject_class),
261                               G_SIGNAL_RUN_FIRST,
262                               G_STRUCT_OFFSET (ModestGtkhtmlMimePartViewClass,stop_streams),
263                               NULL, NULL,
264                               g_cclosure_marshal_VOID__VOID,
265                               G_TYPE_NONE, 0);
266
267         signals[LIMIT_ERROR_SIGNAL] = 
268                 g_signal_new ("limit-error",
269                               G_TYPE_FROM_CLASS (gobject_class),
270                               G_SIGNAL_RUN_FIRST,
271                               G_STRUCT_OFFSET (ModestGtkhtmlMimePartViewClass,limit_error),
272                               NULL, NULL,
273                               g_cclosure_marshal_VOID__VOID,
274                               G_TYPE_NONE, 0);
275
276 }
277
278 static void
279 modest_gtkhtml_mime_part_view_init (ModestGtkhtmlMimePartView *self)
280 {
281         ModestGtkhtmlMimePartViewPrivate *priv = MODEST_GTKHTML_MIME_PART_VIEW_GET_PRIVATE (self);
282         GdkColor base;
283         GdkColor text;
284
285         gtk_html_set_editable        (GTK_HTML(self), FALSE);
286         gtk_html_allow_selection     (GTK_HTML(self), TRUE);
287         gtk_html_set_caret_mode      (GTK_HTML(self), FALSE);
288         gtk_html_set_blocking        (GTK_HTML(self), TRUE);
289         gtk_html_set_images_blocking (GTK_HTML(self), TRUE);
290         /* We don't need this for Hildon2 as this widget will be most
291            likely inside pannable area */
292 #ifndef MODEST_TOOLKIT_HILDON2
293 #ifndef MODEST_TOOLKIT_GTK
294         gtk_html_set_auto_panning    (GTK_HTML (self), TRUE);
295 #endif
296 #endif
297
298 #ifdef MODEST_TOOLKIT_HILDON2
299 #ifdef HAVE_GTK_HTML_SET_MAX_IMAGE_SIZE
300         /* We set a maximum width of a bit less than the width of the screen, and a
301            maximum height of 2 times the full size of the window. Should be enough */
302         gtk_html_set_max_image_size (GTK_HTML (self), 720, 880);
303 #endif
304 #ifdef HAVE_GTK_HTML_SET_ALLOW_DND
305         gtk_html_set_allow_dnd       (GTK_HTML(self), FALSE);
306 #endif
307 #endif
308
309 #ifdef HAVE_GTK_HTML_SET_DEFAULT_ENGINE
310         /* Enable Content type handling */
311         gtk_html_set_default_engine (GTK_HTML (self), TRUE);
312 #endif
313
314         gdk_color_parse ("#fff", &base);
315         gdk_color_parse ("#000", &text);
316         gtk_widget_modify_base (GTK_WIDGET (self), GTK_STATE_NORMAL, &base);
317         gtk_widget_modify_text (GTK_WIDGET (self), GTK_STATE_NORMAL, &text);
318
319         priv->sighandlers = modest_signal_mgr_connect (priv->sighandlers,
320                                                        G_OBJECT(self), "link_clicked",
321                                                        G_CALLBACK(on_link_clicked), self);
322         priv->sighandlers = modest_signal_mgr_connect (priv->sighandlers,
323                                                        G_OBJECT(self), "url_requested",
324                                                        G_CALLBACK(on_url_requested), self);
325         priv->sighandlers = modest_signal_mgr_connect (priv->sighandlers,
326                                                        G_OBJECT(self), "on_url",
327                                                        G_CALLBACK(on_url), self);
328         priv->sighandlers = modest_signal_mgr_connect (priv->sighandlers,
329                                                        G_OBJECT(self), "notify::style",
330                                                        G_CALLBACK (on_notify_style), (gpointer) self);
331
332         priv->part = NULL;
333         priv->current_zoom = 1.0;
334         priv->view_images = FALSE;
335         priv->has_external_images = FALSE;
336 }
337
338 static void
339 modest_gtkhtml_mime_part_view_finalize (GObject *obj)
340 {
341         ModestGtkhtmlMimePartViewPrivate *priv = MODEST_GTKHTML_MIME_PART_VIEW_GET_PRIVATE (obj);
342
343         modest_signal_mgr_disconnect_all_and_destroy (priv->sighandlers);
344         priv->sighandlers = NULL;
345
346         G_OBJECT_CLASS (parent_class)->finalize (obj);
347 }
348
349 static void
350 modest_gtkhtml_mime_part_view_dispose (GObject *obj)
351 {
352         ModestGtkhtmlMimePartViewPrivate *priv = MODEST_GTKHTML_MIME_PART_VIEW_GET_PRIVATE (obj);
353
354         g_signal_emit (G_OBJECT (obj), signals[STOP_STREAMS_SIGNAL], 0);
355
356         if (priv->part) {
357                 g_object_unref (priv->part);
358                 priv->part = NULL;
359         }
360
361         G_OBJECT_CLASS (parent_class)->dispose (obj);
362 }
363
364 /* GTKHTML SIGNALS HANDLERS */
365
366 static void 
367 on_notify_style (GObject *obj, GParamSpec *spec, gpointer userdata)
368 {
369         if (strcmp ("style", spec->name) == 0) {
370                 g_idle_add_full (G_PRIORITY_DEFAULT, (GSourceFunc) update_style, 
371                                  g_object_ref (obj), g_object_unref);
372                 gtk_widget_queue_draw (GTK_WIDGET (obj));
373         }
374 }
375
376 gboolean
377 same_color (GdkColor *a, GdkColor *b)
378 {
379         return ((a->red == b->red) && 
380                 (a->green == b->green) && 
381                 (a->blue == b->blue));
382 }
383
384 static gboolean
385 update_style (ModestGtkhtmlMimePartView *self)
386 {
387         GdkColor base;
388         GdkColor text;
389         GtkRcStyle *rc_style;
390
391         gdk_threads_enter ();
392
393         if (GTK_WIDGET_VISIBLE (self)) {
394                 rc_style = gtk_widget_get_modifier_style (GTK_WIDGET (self));
395
396                 gdk_color_parse ("#fff", &base);
397                 gdk_color_parse ("#000", &text);
398
399                 if (!same_color (&(rc_style->base[GTK_STATE_NORMAL]), &base) &&
400                     !same_color (&(rc_style->text[GTK_STATE_NORMAL]), &text)) {
401
402                         rc_style->base[GTK_STATE_NORMAL] = base;
403                         rc_style->text[GTK_STATE_NORMAL] = text;
404                         gtk_widget_modify_style (GTK_WIDGET (self), rc_style);
405                 }
406         }
407
408         gdk_threads_leave ();
409
410         return FALSE;
411 }
412
413
414
415 static gboolean
416 on_link_clicked (GtkWidget *widget, const gchar *uri, ModestGtkhtmlMimePartView *self)
417 {
418         gboolean result;
419         g_return_val_if_fail (MODEST_IS_GTKHTML_MIME_PART_VIEW (self), FALSE);
420
421         g_signal_emit_by_name (G_OBJECT (self), "activate-link", uri, &result);
422         return result;
423 }
424
425 static gboolean
426 on_url (GtkWidget *widget, const gchar *uri, ModestGtkhtmlMimePartView *self)
427 {
428         gboolean result;
429         g_return_val_if_fail (MODEST_IS_GTKHTML_MIME_PART_VIEW (self), FALSE);
430
431         g_signal_emit_by_name (G_OBJECT (self), "link-hover", uri, &result);
432         return result;
433 }
434
435 static gboolean
436 on_url_requested (GtkWidget *widget, const gchar *uri, GtkHTMLStream *stream, 
437                   ModestGtkhtmlMimePartView *self)
438 {
439         gboolean result;
440         TnyStream *tny_stream;
441         g_return_val_if_fail (MODEST_IS_GTKHTML_MIME_PART_VIEW (self), FALSE);
442
443         if (g_str_has_prefix (uri, "http:")) {
444                 ModestGtkhtmlMimePartViewPrivate *priv = MODEST_GTKHTML_MIME_PART_VIEW_GET_PRIVATE (self);
445
446                 if (!priv->view_images)
447                         priv->has_external_images = TRUE;
448         }
449
450         tny_stream = TNY_STREAM (modest_tny_stream_gtkhtml_new (stream, GTK_HTML (widget)));
451         g_signal_emit_by_name (MODEST_MIME_PART_VIEW (self), "fetch-url", uri, tny_stream, &result);
452         g_object_unref (tny_stream);
453         return result;
454 }
455
456 /* INTERNAL API */
457 static void
458 decode_to_stream_cb (TnyMimePart *self,
459                      gboolean cancelled,
460                      TnyStream *stream,
461                      GError *err,
462                      gpointer user_data)
463 {
464         ModestGtkhtmlMimePartView *view = (ModestGtkhtmlMimePartView *) user_data;
465
466         if (MODEST_IS_STREAM_TEXT_TO_HTML (stream)) {
467                 if (tny_stream_write (stream, "\n", 1) == -1) {
468                         g_warning ("failed to write CR in %s", __FUNCTION__);
469                 }
470                 if (modest_stream_text_to_html_limit_reached (MODEST_STREAM_TEXT_TO_HTML (stream))) {
471                         g_signal_emit (G_OBJECT (view), signals[LIMIT_ERROR_SIGNAL], 0);
472                 }
473                 tny_stream_reset (stream);
474         } else {
475                 if (modest_tny_stream_gtkhtml_limit_reached (MODEST_TNY_STREAM_GTKHTML (stream))) {
476                         g_signal_emit (G_OBJECT (view), signals[LIMIT_ERROR_SIGNAL], 0);
477                 }
478         }
479         tny_stream_close (stream);
480 }
481
482 static void
483 set_html_part (ModestGtkhtmlMimePartView *self, TnyMimePart *part, const gchar *encoding)
484 {
485         GtkHTMLStream *gtkhtml_stream;
486         TnyStream *tny_stream;
487         gchar *content_type;
488
489         g_return_if_fail (self);
490         g_return_if_fail (part);
491
492         g_signal_emit (G_OBJECT (self), signals[STOP_STREAMS_SIGNAL], 0);
493
494         content_type = g_strdup_printf ("text/html; charset=%s", encoding);
495         gtkhtml_stream = gtk_html_begin_full(GTK_HTML(self), NULL, content_type, 0);
496         g_free (content_type);
497
498         tny_stream     = TNY_STREAM(modest_tny_stream_gtkhtml_new (gtkhtml_stream, GTK_HTML (self)));
499         modest_tny_stream_gtkhtml_set_max_size (MODEST_TNY_STREAM_GTKHTML (tny_stream), 128*1024);
500         tny_stream_reset (tny_stream);
501
502         tny_mime_part_decode_to_stream_async (TNY_MIME_PART (part),
503                                               tny_stream, decode_to_stream_cb,
504                                               NULL, self);
505         g_object_unref (tny_stream);
506 }
507
508 static void
509 set_text_part (ModestGtkhtmlMimePartView *self, TnyMimePart *part)
510 {
511         TnyStream* text_to_html_stream, *tny_stream;
512         GtkHTMLStream *gtkhtml_stream;
513
514         g_return_if_fail (self);
515         g_return_if_fail (part);
516
517         g_signal_emit (G_OBJECT (self), signals[STOP_STREAMS_SIGNAL], 0);
518
519         gtkhtml_stream = gtk_html_begin(GTK_HTML(self));
520         tny_stream =  TNY_STREAM(modest_tny_stream_gtkhtml_new (gtkhtml_stream, GTK_HTML (self)));
521         modest_tny_stream_gtkhtml_set_max_size (MODEST_TNY_STREAM_GTKHTML (tny_stream), 128*1024);
522         text_to_html_stream = TNY_STREAM (modest_stream_text_to_html_new (tny_stream));
523         modest_stream_text_to_html_set_linkify_limit (MODEST_STREAM_TEXT_TO_HTML (text_to_html_stream),
524                                                       64*1024);
525         modest_stream_text_to_html_set_full_limit (MODEST_STREAM_TEXT_TO_HTML (text_to_html_stream),
526                                                    128*1024);
527         modest_stream_text_to_html_set_line_limit (MODEST_STREAM_TEXT_TO_HTML (text_to_html_stream),
528                                                    1024);
529
530         tny_mime_part_decode_to_stream_async (TNY_MIME_PART (part),
531                                               text_to_html_stream, decode_to_stream_cb,
532                                               NULL, self);
533
534         g_object_unref (G_OBJECT(text_to_html_stream));
535         g_object_unref (G_OBJECT(tny_stream));
536 }
537
538 static void
539 set_empty_part (ModestGtkhtmlMimePartView *self)
540 {
541         g_return_if_fail (self);
542
543         g_signal_emit (G_OBJECT (self), signals[STOP_STREAMS_SIGNAL], 0);
544         gtk_html_load_from_string (GTK_HTML(self), "", 1);
545 }
546
547 static void
548 set_part (ModestGtkhtmlMimePartView *self, TnyMimePart *part)
549 {
550         ModestGtkhtmlMimePartViewPrivate *priv;
551         gchar *header_content_type, *header_content_type_lower;
552         const gchar *tmp;
553         gchar *charset = NULL;
554
555         g_return_if_fail (self);
556         
557         priv = MODEST_GTKHTML_MIME_PART_VIEW_GET_PRIVATE(self);
558         priv->has_external_images = FALSE;
559
560         if (part != priv->part) {
561                 if (priv->part)
562                         g_object_unref (G_OBJECT(priv->part));
563                 if (part)
564                         g_object_ref   (G_OBJECT(part));
565                 priv->part = part;
566         }
567         
568         if (!part) {
569                 set_empty_part (self);
570                 return;
571         }
572
573         header_content_type = modest_tny_mime_part_get_header_value (part, "Content-Type");
574         if (header_content_type) {
575                 header_content_type = g_strstrip (header_content_type);
576                 header_content_type_lower = g_ascii_strdown (header_content_type, -1);
577         } else {
578                 header_content_type_lower = NULL;
579         }
580
581         if (header_content_type_lower) {
582                 tmp = strstr (header_content_type_lower, "charset=");
583                 if (tmp) {
584                         const gchar *tmp2;
585                         tmp = tmp + strlen ("charset=");
586                         
587                         tmp2 = strstr (tmp, ";");
588                         if (tmp2) {
589                                 charset = g_strndup (tmp, tmp2-tmp);
590                         } else {
591                                 charset = g_strdup (tmp);
592                         }
593                 }
594         }
595
596         if (tny_mime_part_content_type_is (part, "text/html")) {
597                 set_html_part (self, part, charset);
598         } else {
599                 if (tny_mime_part_content_type_is (part, "message/rfc822")) {
600                         if (header_content_type) {
601                                 if (g_str_has_prefix (header_content_type_lower, "text/html"))
602                                         set_html_part (self, part, charset);
603                                 else 
604                                         set_text_part (self, part);
605
606                         } else {
607                                 set_text_part (self, part);
608                         }
609                 } else {
610                         set_text_part (self, part);
611                 }
612         }
613         g_free (header_content_type_lower);
614         g_free (header_content_type);
615
616 }
617
618 static TnyMimePart*
619 get_part (ModestGtkhtmlMimePartView *self)
620 {
621         TnyMimePart *part;
622
623         g_return_val_if_fail (MODEST_IS_GTKHTML_MIME_PART_VIEW (self), NULL);
624
625         part = MODEST_GTKHTML_MIME_PART_VIEW_GET_PRIVATE(self)->part;
626
627         if (part)
628                 g_object_ref (part);
629         
630         return part;
631 }
632
633 static gboolean
634 has_contents_receiver (gpointer engine, const gchar *data,
635                        size_t len, gboolean *has_contents)
636 {
637         if (len > 1 || ((len == 1)&&(data[0]!='\n'))) {
638                 *has_contents = TRUE;
639                 return FALSE;
640         }
641         return TRUE;
642 }
643
644 static gboolean      
645 is_empty   (ModestGtkhtmlMimePartView *self)
646 {
647         /* TODO: Find some gtkhtml API to check whether there is any (visible, non markup)
648          * text in the message:
649          */
650         gboolean has_contents = FALSE;
651
652         gtk_html_export (GTK_HTML (self), "text/plain", 
653                          (GtkHTMLSaveReceiverFn) has_contents_receiver, &has_contents);
654         
655         return !has_contents;
656 }
657
658 static gboolean      
659 get_view_images   (ModestGtkhtmlMimePartView *self)
660 {
661         ModestGtkhtmlMimePartViewPrivate *priv;
662
663         g_return_val_if_fail (MODEST_IS_GTKHTML_MIME_PART_VIEW (self), FALSE);
664
665         priv = MODEST_GTKHTML_MIME_PART_VIEW_GET_PRIVATE (self);
666         return priv->view_images;
667 }
668
669 static void
670 set_view_images   (ModestGtkhtmlMimePartView *self, gboolean view_images)
671 {
672         ModestGtkhtmlMimePartViewPrivate *priv;
673
674         g_return_if_fail (MODEST_IS_GTKHTML_MIME_PART_VIEW (self));
675
676         priv = MODEST_GTKHTML_MIME_PART_VIEW_GET_PRIVATE (self);
677         priv->view_images = view_images;
678 }
679
680 static gboolean      
681 has_external_images   (ModestGtkhtmlMimePartView *self)
682 {
683         ModestGtkhtmlMimePartViewPrivate *priv;
684
685         g_return_val_if_fail (MODEST_IS_GTKHTML_MIME_PART_VIEW (self), FALSE);
686
687         priv = MODEST_GTKHTML_MIME_PART_VIEW_GET_PRIVATE (self);
688         return priv->has_external_images;
689 }
690
691 static void
692 set_zoom (ModestGtkhtmlMimePartView *self, gdouble zoom)
693 {
694         ModestGtkhtmlMimePartViewPrivate *priv;
695
696         g_return_if_fail (MODEST_IS_GTKHTML_MIME_PART_VIEW (self));
697
698         priv = MODEST_GTKHTML_MIME_PART_VIEW_GET_PRIVATE (self);
699         priv->current_zoom = zoom;
700         gtk_html_set_magnification (GTK_HTML(self), zoom);
701
702         gtk_widget_queue_resize (GTK_WIDGET (self));
703 }
704
705 static gdouble
706 get_zoom (ModestGtkhtmlMimePartView *self)
707 {
708         ModestGtkhtmlMimePartViewPrivate *priv;
709
710         g_return_val_if_fail (MODEST_IS_GTKHTML_MIME_PART_VIEW (self), 1.0);
711
712         priv = MODEST_GTKHTML_MIME_PART_VIEW_GET_PRIVATE (self);
713
714         return priv->current_zoom;
715 }
716
717 static gboolean
718 search (ModestGtkhtmlMimePartView *self, 
719         const gchar *string)
720 {
721         return gtk_html_engine_search (GTK_HTML (self),
722                                        string,
723                                        FALSE,   /* case sensitive */
724                                        TRUE,    /* forward */
725                                        FALSE);  /* regexp */
726 }
727
728 static gboolean
729 search_next (ModestGtkhtmlMimePartView *self)
730 {
731         return gtk_html_engine_search_next (GTK_HTML (self));
732 }
733
734 static gboolean
735 get_selection_area (ModestGtkhtmlMimePartView *self, 
736                     gint *x, gint *y,
737                     gint *width, gint *height)
738 {
739 #ifdef HAVE_GTK_HTML_GET_SELECTION_AREA
740         gtk_html_get_selection_area (GTK_HTML (self), x, y, width, height);
741         return TRUE;
742 #else
743         return FALSE;
744 #endif
745 }
746
747
748 /* TNY MIME PART IMPLEMENTATION */
749
750 static void
751 tny_mime_part_view_init (gpointer g, gpointer iface_data)
752 {
753         TnyMimePartViewIface *klass = (TnyMimePartViewIface *)g;
754
755         klass->get_part = modest_gtkhtml_mime_part_view_get_part;
756         klass->set_part = modest_gtkhtml_mime_part_view_set_part;
757         klass->clear = modest_gtkhtml_mime_part_view_clear;
758
759         return;
760 }
761
762 static TnyMimePart* 
763 modest_gtkhtml_mime_part_view_get_part (TnyMimePartView *self)
764 {
765         return MODEST_GTKHTML_MIME_PART_VIEW_GET_CLASS (self)->get_part_func (self);
766 }
767
768
769 static TnyMimePart* 
770 modest_gtkhtml_mime_part_view_get_part_default (TnyMimePartView *self)
771 {
772         return TNY_MIME_PART (get_part (MODEST_GTKHTML_MIME_PART_VIEW (self)));
773 }
774
775 static void
776 modest_gtkhtml_mime_part_view_set_part (TnyMimePartView *self,
777                                         TnyMimePart *part)
778 {
779         MODEST_GTKHTML_MIME_PART_VIEW_GET_CLASS (self)->set_part_func (self, part);
780 }
781
782 static void
783 modest_gtkhtml_mime_part_view_set_part_default (TnyMimePartView *self,
784                                                 TnyMimePart *part)
785 {
786         g_return_if_fail ((part == NULL) || TNY_IS_MIME_PART (part));
787
788         set_part (MODEST_GTKHTML_MIME_PART_VIEW (self), part);
789 }
790
791 static void
792 modest_gtkhtml_mime_part_view_clear (TnyMimePartView *self)
793 {
794         MODEST_GTKHTML_MIME_PART_VIEW_GET_CLASS (self)->clear_func (self);
795 }
796
797 static void
798 modest_gtkhtml_mime_part_view_clear_default (TnyMimePartView *self)
799 {
800         set_part (MODEST_GTKHTML_MIME_PART_VIEW (self), NULL);
801 }
802
803 /* MODEST MIME PART VIEW IMPLEMENTATION */
804
805 static void
806 modest_mime_part_view_init (gpointer g, gpointer iface_data)
807 {
808         ModestMimePartViewIface *klass = (ModestMimePartViewIface *)g;
809
810         klass->is_empty_func = modest_gtkhtml_mime_part_view_is_empty;
811         klass->get_view_images_func = modest_gtkhtml_mime_part_view_get_view_images;
812         klass->set_view_images_func = modest_gtkhtml_mime_part_view_set_view_images;
813         klass->has_external_images_func = modest_gtkhtml_mime_part_view_has_external_images;
814
815         return;
816 }
817
818 static gboolean
819 modest_gtkhtml_mime_part_view_is_empty (ModestMimePartView *self)
820 {
821         return MODEST_GTKHTML_MIME_PART_VIEW_GET_CLASS (self)->is_empty_func (self);
822 }
823
824 static gboolean
825 modest_gtkhtml_mime_part_view_get_view_images (ModestMimePartView *self)
826 {
827         return MODEST_GTKHTML_MIME_PART_VIEW_GET_CLASS (self)->get_view_images_func (self);
828 }
829
830 static void
831 modest_gtkhtml_mime_part_view_set_view_images (ModestMimePartView *self, gboolean view_images)
832 {
833         MODEST_GTKHTML_MIME_PART_VIEW_GET_CLASS (self)->set_view_images_func (self, view_images);
834 }
835
836 static gboolean
837 modest_gtkhtml_mime_part_view_has_external_images (ModestMimePartView *self)
838 {
839         return MODEST_GTKHTML_MIME_PART_VIEW_GET_CLASS (self)->has_external_images_func (self);
840 }
841
842 static gboolean
843 modest_gtkhtml_mime_part_view_is_empty_default (ModestMimePartView *self)
844 {
845         return is_empty (MODEST_GTKHTML_MIME_PART_VIEW (self));
846 }
847
848 static gboolean
849 modest_gtkhtml_mime_part_view_get_view_images_default (ModestMimePartView *self)
850 {
851         return get_view_images (MODEST_GTKHTML_MIME_PART_VIEW (self));
852 }
853
854 static void
855 modest_gtkhtml_mime_part_view_set_view_images_default (ModestMimePartView *self, gboolean view_images)
856 {
857         set_view_images (MODEST_GTKHTML_MIME_PART_VIEW (self), view_images);
858 }
859
860 static gboolean
861 modest_gtkhtml_mime_part_view_has_external_images_default (ModestMimePartView *self)
862 {
863         return has_external_images (MODEST_GTKHTML_MIME_PART_VIEW (self));
864 }
865
866
867 /* MODEST ZOOMABLE IMPLEMENTATION */
868 static void
869 modest_zoomable_init (gpointer g, gpointer iface_data)
870 {
871         ModestZoomableIface *klass = (ModestZoomableIface *)g;
872         
873         klass->get_zoom_func = modest_gtkhtml_mime_part_view_get_zoom;
874         klass->set_zoom_func = modest_gtkhtml_mime_part_view_set_zoom;
875         klass->zoom_minus_func = modest_gtkhtml_mime_part_view_zoom_minus;
876         klass->zoom_plus_func = modest_gtkhtml_mime_part_view_zoom_plus;
877
878         return;
879 }
880
881 static gdouble
882 modest_gtkhtml_mime_part_view_get_zoom (ModestZoomable *self)
883 {
884         return MODEST_GTKHTML_MIME_PART_VIEW_GET_CLASS (self)->get_zoom_func (self);
885 }
886
887 static gdouble
888 modest_gtkhtml_mime_part_view_get_zoom_default (ModestZoomable *self)
889 {
890         return get_zoom (MODEST_GTKHTML_MIME_PART_VIEW (self));
891 }
892
893 static void
894 modest_gtkhtml_mime_part_view_set_zoom (ModestZoomable *self, gdouble value)
895 {
896         MODEST_GTKHTML_MIME_PART_VIEW_GET_CLASS (self)->set_zoom_func (self, value);
897 }
898
899 static void
900 modest_gtkhtml_mime_part_view_set_zoom_default (ModestZoomable *self, gdouble value)
901 {
902         set_zoom (MODEST_GTKHTML_MIME_PART_VIEW (self), value);
903 }
904
905 static gboolean
906 modest_gtkhtml_mime_part_view_zoom_minus (ModestZoomable *self)
907 {
908         return MODEST_GTKHTML_MIME_PART_VIEW_GET_CLASS (self)->zoom_minus_func (self);
909 }
910
911 static gboolean
912 modest_gtkhtml_mime_part_view_zoom_minus_default (ModestZoomable *self)
913 {
914         /* operation not supported in ModestGtkhtmlMimePartView */
915         return FALSE;
916 }
917
918 static gboolean
919 modest_gtkhtml_mime_part_view_zoom_plus (ModestZoomable *self)
920 {
921         return MODEST_GTKHTML_MIME_PART_VIEW_GET_CLASS (self)->zoom_plus_func (self);
922 }
923
924 static gboolean
925 modest_gtkhtml_mime_part_view_zoom_plus_default (ModestZoomable *self)
926 {
927         /* operation not supported in ModestGtkhtmlMimePartView */
928         return FALSE;
929 }
930
931 /* ISEARCH VIEW IMPLEMENTATION */
932 static void
933 modest_isearch_view_init (gpointer g, gpointer iface_data)
934 {
935         ModestISearchViewIface *klass = (ModestISearchViewIface *)g;
936         
937         klass->search_func = modest_gtkhtml_mime_part_view_search;
938         klass->search_next_func = modest_gtkhtml_mime_part_view_search_next;
939         klass->get_selection_area_func = modest_gtkhtml_mime_part_view_get_selection_area;
940
941         return;
942 }
943
944 static gboolean 
945 modest_gtkhtml_mime_part_view_search (ModestISearchView *self, const gchar *string)
946 {
947         return MODEST_GTKHTML_MIME_PART_VIEW_GET_CLASS (self)->search_func (self, string);
948 }
949
950 static gboolean 
951 modest_gtkhtml_mime_part_view_search_default (ModestISearchView *self, const gchar *string)
952 {
953         return search (MODEST_GTKHTML_MIME_PART_VIEW (self), string);
954 }
955
956 static gboolean 
957 modest_gtkhtml_mime_part_view_search_next(ModestISearchView *self)
958 {
959         return MODEST_GTKHTML_MIME_PART_VIEW_GET_CLASS (self)->search_next_func (self);
960 }
961
962 static gboolean 
963 modest_gtkhtml_mime_part_view_search_next_default (ModestISearchView *self)
964 {
965         return search_next (MODEST_GTKHTML_MIME_PART_VIEW (self));
966 }
967
968 static gboolean 
969 modest_gtkhtml_mime_part_view_get_selection_area (ModestISearchView *self, gint *x, gint *y, 
970                                                   gint *width, gint *height)
971 {
972         return MODEST_GTKHTML_MIME_PART_VIEW_GET_CLASS (self)->get_selection_area_func (self, x, y, width, height);
973 }
974
975 static gboolean 
976 modest_gtkhtml_mime_part_view_get_selection_area_default (ModestISearchView *self, gint *x, gint *y, 
977                                                           gint *width, gint *height)
978 {
979         return get_selection_area (MODEST_GTKHTML_MIME_PART_VIEW (self), x, y, width, height);
980 }