3bacb9b39bf4297fd35335347e7e562f6eb0a4dc
[modest] / src / widgets / modest-gtkhtml-msg-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 <tny-gtk-text-buffer-stream.h>
32 #include <string.h>
33 #include <regex.h>
34 #include <ctype.h>
35 #include <stdlib.h>
36 #include <glib/gi18n.h>
37 #include <tny-list.h>
38 #include <tny-simple-list.h>
39 #include <tny-vfs-stream.h>
40
41 #include <modest-tny-msg.h>
42 #include <modest-text-utils.h>
43 #include <widgets/modest-msg-view.h>
44 #include <widgets/modest-mail-header-view.h>
45 #include <widgets/modest-attachments-view.h>
46 #include <modest-marshal.h>
47 #include <widgets/modest-gtkhtml-mime-part-view.h>
48 #include <widgets/modest-gtkhtml-msg-view.h>
49 #include <widgets/modest-isearch-view.h>
50 #include <widgets/modest-ui-constants.h>
51
52 /* FIXNE: we should have no maemo-deps in widgets/ */
53 #ifdef MODEST_PLATFORM_MAEMO
54 #include "maemo/modest-hildon-includes.h"
55 #endif /*MODEST_PLATFORM_MAEMO*/
56
57
58 /* 'private'/'protected' functions */
59 static void     modest_gtkhtml_msg_view_class_init   (ModestGtkhtmlMsgViewClass *klass);
60 static void     tny_msg_view_init (gpointer g, gpointer iface_data);
61 static void     tny_mime_part_view_init (gpointer g, gpointer iface_data);
62 static void     modest_mime_part_view_init (gpointer g, gpointer iface_data);
63 static void     modest_zoomable_init (gpointer g, gpointer iface_data);
64 static void     modest_isearch_view_init (gpointer g, gpointer iface_data);
65 static void     modest_msg_view_init (gpointer g, gpointer iface_data);
66 static void     modest_gtkhtml_msg_view_init         (ModestGtkhtmlMsgView *obj);
67 static void     modest_gtkhtml_msg_view_finalize     (GObject *obj);
68 static void     modest_gtkhtml_msg_view_destroy     (GtkObject *obj);
69 static void     set_property (GObject *object, guint prop_id, const GValue *value, GParamSpec *pspec);
70 static void     get_property (GObject *object, guint prop_id, GValue *value, GParamSpec *pspec);
71
72 /* headers signals */
73 static void on_recpt_activated (ModestMailHeaderView *header_view, const gchar *address, ModestGtkhtmlMsgView *msg_view);
74 static void on_attachment_activated (ModestAttachmentsView * att_view, TnyMimePart *mime_part, gpointer userdata);
75 static void on_view_images_clicked (GtkButton * button, gpointer self);
76
77 /* body view signals */
78 static gboolean on_activate_link (GtkWidget *widget, const gchar *uri, ModestGtkhtmlMsgView *msg_view);
79 static gboolean on_fetch_url (GtkWidget *widget, const gchar *uri, TnyStream *stream,
80                               ModestGtkhtmlMsgView *msg_view);
81 static gboolean on_link_hover (GtkWidget *widget, const gchar *uri, ModestGtkhtmlMsgView *msg_view);
82
83 #ifdef MAEMO_CHANGES
84 static void     on_tap_and_hold (GtkWidget *widget, gpointer userdata); 
85 static gboolean on_tap_and_hold_query (GtkWidget *widget, GdkEvent *event, gpointer userdata); 
86 #endif /*MAEMO_CHANGES*/
87
88
89 /* size allocation and drawing handlers */
90 static void get_view_allocation (ModestGtkhtmlMsgView *msg_view, GtkAllocation *allocation);
91 static void size_request (GtkWidget *widget, GtkRequisition *req);
92 static void size_allocate (GtkWidget *widget, GtkAllocation *alloc);
93 static void realize (GtkWidget *widget);
94 static void unrealize (GtkWidget *widget);
95 static gint expose (GtkWidget *widget, GdkEventExpose *event);
96 static void reclamp_adjustment (GtkAdjustment *adj, gboolean *value_changed);
97 static void set_hadjustment_values (ModestGtkhtmlMsgView *msg_view, gboolean *value_changed);
98 static void set_scroll_adjustments (ModestGtkhtmlMsgView *msg_view, GtkAdjustment *hadj, GtkAdjustment *vadj);
99 static void adjustment_value_changed (GtkAdjustment *adj, gpointer data);
100 static void html_adjustment_changed (GtkAdjustment *adj, gpointer data);
101 static void disconnect_vadjustment (ModestGtkhtmlMsgView *obj);
102 static void disconnect_hadjustment (ModestGtkhtmlMsgView *obj);
103 static gboolean idle_readjust_scroll (ModestGtkhtmlMsgView *obj);
104
105 /* vertical panning implementation */
106 #ifdef MAEMO_CHANGES
107 static gboolean motion_notify_event (GtkWidget *widget,
108                                      GdkEventMotion *event,
109                                      gpointer userdata);
110 #endif
111
112 static gboolean 
113 button_press_event (GtkWidget *widget,
114                     GdkEventButton *event,
115                     gpointer userdata);
116 static gboolean 
117 button_release_event (GtkWidget *widget,
118                       GdkEventButton *event,
119                       gpointer userdata);
120
121 /* GtkContainer methods */
122 static void forall (GtkContainer *container, gboolean include_internals,
123                     GtkCallback callback, gpointer userdata);
124 static void container_remove (GtkContainer *container, GtkWidget *widget);
125
126 /* TnyMimePartView implementation */
127 static void modest_msg_view_mp_clear (TnyMimePartView *self);
128 static void modest_msg_view_mp_set_part (TnyMimePartView *self, TnyMimePart *part);
129 static void modest_msg_view_mp_set_part_default (TnyMimePartView *self, TnyMimePart *part);
130 static TnyMimePart* modest_msg_view_mp_get_part (TnyMimePartView *self);
131 static TnyMimePart* modest_msg_view_mp_get_part_default (TnyMimePartView *self);
132 /* ModestMimePartView implementation */
133 static gboolean modest_msg_view_mp_is_empty (ModestMimePartView *self);
134 static gboolean modest_msg_view_mp_is_empty_default (ModestMimePartView *self);
135 /* TnyMsgView implementation */
136 static TnyMsg *modest_msg_view_get_msg (TnyMsgView *self);
137 static TnyMsg *modest_msg_view_get_msg_default (TnyMsgView *self);
138 static void modest_msg_view_set_msg (TnyMsgView *self, TnyMsg *msg);
139 static void modest_msg_view_set_msg_default (TnyMsgView *self, TnyMsg *msg);
140 static void modest_msg_view_clear (TnyMsgView *self);
141 static void modest_msg_view_clear_default (TnyMsgView *self);
142 static void modest_msg_view_set_unavailable (TnyMsgView *self);
143 static void modest_msg_view_set_unavailable_default (TnyMsgView *self);
144 static TnyMimePartView *modest_msg_view_create_mime_part_view_for (TnyMsgView *self, TnyMimePart *part);
145 static TnyMimePartView *modest_msg_view_create_mime_part_view_for_default (TnyMsgView *self, TnyMimePart *part);
146 static TnyMsgView *modest_msg_view_create_new_inline_viewer (TnyMsgView *self);
147 static TnyMsgView *modest_msg_view_create_new_inline_viewer_default (TnyMsgView *self);
148 /* ModestZoomable implementation */
149 static gdouble modest_msg_view_get_zoom (ModestZoomable *self);
150 static void modest_msg_view_set_zoom (ModestZoomable *self, gdouble value);
151 static gboolean modest_msg_view_zoom_minus (ModestZoomable *self);
152 static gboolean modest_msg_view_zoom_plus (ModestZoomable *self);
153 static gdouble modest_msg_view_get_zoom_default (ModestZoomable *self);
154 static void modest_msg_view_set_zoom_default (ModestZoomable *self, gdouble value);
155 static gboolean modest_msg_view_zoom_minus_default (ModestZoomable *self);
156 static gboolean modest_msg_view_zoom_plus_default (ModestZoomable *self);
157 /* ModestISearchView implementation */
158 static gboolean modest_msg_view_search (ModestISearchView *self, const gchar *string);
159 static gboolean modest_msg_view_search_default (ModestISearchView *self, const gchar *string);
160 static gboolean modest_msg_view_search_next (ModestISearchView *self);
161 static gboolean modest_msg_view_search_next_default (ModestISearchView *self);
162 /* ModestMsgView implementation */
163 static GtkAdjustment *modest_gtkhtml_msg_view_get_vadjustment (ModestMsgView *self);
164 static GtkAdjustment *modest_gtkhtml_msg_view_get_hadjustment (ModestMsgView *self);
165 static void modest_gtkhtml_msg_view_set_vadjustment (ModestMsgView *self, GtkAdjustment *vadj);
166 static void modest_gtkhtml_msg_view_set_hadjustment (ModestMsgView *self, GtkAdjustment *hadj);
167 static void modest_gtkhtml_msg_view_set_shadow_type (ModestMsgView *self, GtkShadowType type);
168 static GtkShadowType modest_gtkhtml_msg_view_get_shadow_type (ModestMsgView *self);
169 static TnyHeaderFlags modest_gtkhtml_msg_view_get_priority (ModestMsgView *self);
170 static void modest_gtkhtml_msg_view_set_priority (ModestMsgView *self, TnyHeaderFlags flags);
171 static TnyList *modest_gtkhtml_msg_view_get_selected_attachments (ModestMsgView *self);
172 static TnyList *modest_gtkhtml_msg_view_get_attachments (ModestMsgView *self);
173 static void modest_gtkhtml_msg_view_grab_focus (ModestMsgView *self);
174 static void modest_gtkhtml_msg_view_remove_attachment (ModestMsgView *view, TnyMimePart *attachment);
175 static GtkAdjustment *modest_gtkhtml_msg_view_get_vadjustment_default (ModestMsgView *self);
176 static GtkAdjustment *modest_gtkhtml_msg_view_get_hadjustment_default (ModestMsgView *self);
177 static void modest_gtkhtml_msg_view_set_vadjustment_default (ModestMsgView *self, GtkAdjustment *vadj);
178 static void modest_gtkhtml_msg_view_set_hadjustment_default (ModestMsgView *self, GtkAdjustment *hadj);
179 static void modest_gtkhtml_msg_view_set_shadow_type_default (ModestMsgView *self, GtkShadowType type);
180 static GtkShadowType modest_gtkhtml_msg_view_get_shadow_type_default (ModestMsgView *self);
181 static TnyHeaderFlags modest_gtkhtml_msg_view_get_priority_default (ModestMsgView *self);
182 static void modest_gtkhtml_msg_view_set_priority_default (ModestMsgView *self, TnyHeaderFlags flags);
183 static TnyList *modest_gtkhtml_msg_view_get_selected_attachments_default (ModestMsgView *self);
184 static TnyList *modest_gtkhtml_msg_view_get_attachments_default (ModestMsgView *self);
185 static void modest_gtkhtml_msg_view_grab_focus_default (ModestMsgView *self);
186 static void modest_gtkhtml_msg_view_remove_attachment_default (ModestMsgView *view, TnyMimePart *attachment);
187
188 /* internal api */
189 static TnyMsg   *get_message   (ModestGtkhtmlMsgView *self);
190 static void     set_message    (ModestGtkhtmlMsgView *self, TnyMsg *tny_msg);
191 static gboolean is_empty       (ModestGtkhtmlMsgView *self); 
192 static void     set_zoom       (ModestGtkhtmlMsgView *self, gdouble zoom);
193 static gdouble  get_zoom       (ModestGtkhtmlMsgView *self);
194 static gboolean search         (ModestGtkhtmlMsgView *self, const gchar *search);
195 static gboolean search_next    (ModestGtkhtmlMsgView *self);
196 static GtkAdjustment *get_vadjustment (ModestGtkhtmlMsgView *self);
197 static GtkAdjustment *get_hadjustment (ModestGtkhtmlMsgView *self);
198 static void set_vadjustment (ModestGtkhtmlMsgView *self, GtkAdjustment *vadj);
199 static void set_hadjustment (ModestGtkhtmlMsgView *self, GtkAdjustment *hadj);
200 static void set_shadow_type (ModestGtkhtmlMsgView *self, GtkShadowType type);
201 static GtkShadowType get_shadow_type (ModestGtkhtmlMsgView *self);
202 static TnyHeaderFlags get_priority (ModestGtkhtmlMsgView *self);
203 static void set_priority (ModestGtkhtmlMsgView *self, TnyHeaderFlags flags);
204 static TnyList *get_selected_attachments (ModestGtkhtmlMsgView *self);
205 static TnyList *get_attachments (ModestGtkhtmlMsgView *self);
206 static void grab_focus (ModestGtkhtmlMsgView *self);
207 static void remove_attachment (ModestGtkhtmlMsgView *view, TnyMimePart *attachment);
208
209 /* list properties */
210 enum {
211         PROP_0,
212         PROP_HADJUSTMENT,
213         PROP_VADJUSTMENT,
214         PROP_SHADOW_TYPE
215 };
216
217 typedef struct _ModestGtkhtmlMsgViewPrivate ModestGtkhtmlMsgViewPrivate;
218 struct _ModestGtkhtmlMsgViewPrivate {
219         GtkWidget   *body_view;
220         GtkWidget   *mail_header_view;
221         GtkWidget   *attachments_view;
222
223         TnyMsg      *msg;
224
225         /* embedded elements */
226         GtkWidget   *headers_box;
227         GtkWidget   *html_scroll;
228         GtkWidget   *attachments_box;
229         GtkWidget   *view_images_button;
230
231         /* internal adjustments for set_scroll_adjustments */
232         GtkAdjustment *hadj;
233         GtkAdjustment *vadj;
234         GtkShadowType shadow_type;
235
236         /* gdk windows for drawing */
237         GdkWindow *view_window;
238         GdkWindow *headers_window;
239         GdkWindow *html_window;
240
241         /* id handler for dragged scroll */
242         guint idle_motion_id;
243
244         /* zoom */
245         gdouble current_zoom;
246
247         /* link click management */
248         gchar *last_url;
249 };
250
251 #define MODEST_GTKHTML_MSG_VIEW_GET_PRIVATE(o)      (G_TYPE_INSTANCE_GET_PRIVATE((o), \
252                                                                                  MODEST_TYPE_GTKHTML_MSG_VIEW, \
253                                                                                  ModestGtkhtmlMsgViewPrivate))
254
255 /* globals */
256 static GtkContainerClass *parent_class = NULL;
257
258 GType
259 modest_gtkhtml_msg_view_get_type (void)
260 {
261         static GType my_type = 0;
262         if (!my_type) {
263                 static const GTypeInfo my_info = {
264                         sizeof(ModestGtkhtmlMsgViewClass),
265                         NULL,           /* base init */
266                         NULL,           /* base finalize */
267                         (GClassInitFunc) modest_gtkhtml_msg_view_class_init,
268                         NULL,           /* class finalize */
269                         NULL,           /* class data */
270                         sizeof(ModestGtkhtmlMsgView),
271                         1,              /* n_preallocs */
272                         (GInstanceInitFunc) modest_gtkhtml_msg_view_init,
273                         NULL
274                 };
275                 static const GInterfaceInfo tny_msg_view_info = 
276                 {
277                   (GInterfaceInitFunc) tny_msg_view_init, /* interface_init */
278                   NULL,         /* interface_finalize */
279                   NULL          /* interface_data */
280                 };
281
282                 static const GInterfaceInfo tny_mime_part_view_info = 
283                 {
284                   (GInterfaceInitFunc) tny_mime_part_view_init, /* interface_init */
285                   NULL,         /* interface_finalize */
286                   NULL          /* interface_data */
287                 };
288
289                 static const GInterfaceInfo modest_mime_part_view_info = 
290                 {
291                   (GInterfaceInitFunc) modest_mime_part_view_init, /* interface_init */
292                   NULL,         /* interface_finalize */
293                   NULL          /* interface_data */
294                 };
295
296                 static const GInterfaceInfo modest_zoomable_info = 
297                 {
298                   (GInterfaceInitFunc) modest_zoomable_init, /* interface_init */
299                   NULL,         /* interface_finalize */
300                   NULL          /* interface_data */
301                 };
302
303                 static const GInterfaceInfo modest_isearch_view_info = 
304                 {
305                   (GInterfaceInitFunc) modest_isearch_view_init, /* interface_init */
306                   NULL,         /* interface_finalize */
307                   NULL          /* interface_data */
308                 };
309
310                 static const GInterfaceInfo modest_msg_view_info = 
311                 {
312                   (GInterfaceInitFunc) modest_msg_view_init, /* interface_init */
313                   NULL,         /* interface_finalize */
314                   NULL          /* interface_data */
315                 };
316
317                 my_type = g_type_register_static (GTK_TYPE_CONTAINER,
318                                                   "ModestGtkhtmlMsgView",
319                                                   &my_info, 0);
320
321                 g_type_add_interface_static (my_type, TNY_TYPE_MIME_PART_VIEW, 
322                         &tny_mime_part_view_info);
323
324                 g_type_add_interface_static (my_type, MODEST_TYPE_MIME_PART_VIEW, 
325                         &modest_mime_part_view_info);
326
327                 g_type_add_interface_static (my_type, TNY_TYPE_MSG_VIEW, 
328                         &tny_msg_view_info);
329
330                 g_type_add_interface_static (my_type, MODEST_TYPE_ZOOMABLE, 
331                         &modest_zoomable_info);
332
333                 g_type_add_interface_static (my_type, MODEST_TYPE_ISEARCH_VIEW, 
334                         &modest_isearch_view_info);
335
336                 g_type_add_interface_static (my_type, MODEST_TYPE_MSG_VIEW, 
337                         &modest_msg_view_info);
338         }
339         return my_type;
340 }
341
342 static void
343 modest_gtkhtml_msg_view_class_init (ModestGtkhtmlMsgViewClass *klass)
344 {
345         GObjectClass *gobject_class;
346         GtkWidgetClass *widget_class;
347         GtkObjectClass *gtkobject_class;
348         GtkContainerClass *container_class;
349         gobject_class = (GObjectClass*) klass;
350         widget_class = (GtkWidgetClass *) klass;
351         gtkobject_class = (GtkObjectClass *) klass;
352         container_class = (GtkContainerClass *) klass;
353
354         parent_class            = g_type_class_peek_parent (klass);
355         gobject_class->finalize = modest_gtkhtml_msg_view_finalize;
356         gobject_class->set_property = set_property;
357         gobject_class->get_property = get_property;
358         gtkobject_class->destroy = modest_gtkhtml_msg_view_destroy;
359
360         widget_class->realize = realize;
361         widget_class->unrealize = unrealize;
362         widget_class->expose_event = expose;
363         widget_class->size_request = size_request;
364         widget_class->size_allocate = size_allocate;
365
366         container_class->forall = forall;
367         container_class->remove = container_remove;
368
369         klass->set_scroll_adjustments = set_scroll_adjustments;
370         klass->get_part_func = modest_msg_view_mp_get_part_default;
371         klass->set_part_func = modest_msg_view_mp_set_part_default;
372         klass->is_empty_func = modest_msg_view_mp_is_empty_default;
373         klass->get_msg_func = modest_msg_view_get_msg_default;
374         klass->set_msg_func = modest_msg_view_set_msg_default;
375         klass->set_unavailable_func = modest_msg_view_set_unavailable_default;
376         klass->clear_func = modest_msg_view_clear_default;
377         klass->create_mime_part_view_for_func = modest_msg_view_create_mime_part_view_for_default;
378         klass->create_new_inline_viewer_func = modest_msg_view_create_new_inline_viewer_default;
379         klass->get_zoom_func = modest_msg_view_get_zoom_default;
380         klass->set_zoom_func = modest_msg_view_set_zoom_default;
381         klass->zoom_minus_func = modest_msg_view_zoom_minus_default;
382         klass->zoom_plus_func = modest_msg_view_zoom_plus_default;
383         klass->search_func = modest_msg_view_search_default;
384         klass->search_next_func = modest_msg_view_search_next_default;
385         klass->get_vadjustment_func = modest_gtkhtml_msg_view_get_vadjustment_default;
386         klass->get_hadjustment_func = modest_gtkhtml_msg_view_get_hadjustment_default;
387         klass->set_vadjustment_func = modest_gtkhtml_msg_view_set_vadjustment_default;
388         klass->set_hadjustment_func = modest_gtkhtml_msg_view_set_hadjustment_default;
389         klass->get_shadow_type_func = modest_gtkhtml_msg_view_get_shadow_type_default;
390         klass->set_shadow_type_func = modest_gtkhtml_msg_view_set_shadow_type_default;
391         klass->get_priority_func = modest_gtkhtml_msg_view_get_priority_default;
392         klass->set_priority_func = modest_gtkhtml_msg_view_set_priority_default;
393         klass->get_selected_attachments_func = modest_gtkhtml_msg_view_get_selected_attachments_default;
394         klass->get_attachments_func = modest_gtkhtml_msg_view_get_attachments_default;
395         klass->grab_focus_func = modest_gtkhtml_msg_view_grab_focus_default;
396         klass->remove_attachment_func = modest_gtkhtml_msg_view_remove_attachment_default;
397
398         g_type_class_add_private (gobject_class, sizeof(ModestGtkhtmlMsgViewPrivate));
399
400         g_object_class_install_property (gobject_class,
401                                          PROP_HADJUSTMENT,
402                                          g_param_spec_object ("hadjustment", 
403                                                               _("Horizontal adjustment"),
404                                                               _("GtkAdjustment with information of the horizontal visible position"),
405                                                               GTK_TYPE_ADJUSTMENT,
406                                                               G_PARAM_READABLE | G_PARAM_WRITABLE | G_PARAM_CONSTRUCT));
407
408         g_object_class_install_property (gobject_class,
409                                          PROP_VADJUSTMENT,
410                                          g_param_spec_object ("vadjustment", 
411                                                               _("Vertical adjustment"),
412                                                               _("GtkAdjustment with information of the vertical visible position"),
413                                                               GTK_TYPE_ADJUSTMENT,
414                                                               G_PARAM_READABLE | G_PARAM_WRITABLE | G_PARAM_CONSTRUCT));
415
416         g_object_class_install_property (gobject_class,
417                                          PROP_SHADOW_TYPE,
418                                          g_param_spec_enum ("shadow_type", 
419                                                             _("Shadow type"),
420                                                             _("Kind of shadow that's shown around the view"),
421                                                             GTK_TYPE_SHADOW_TYPE,
422                                                             GTK_SHADOW_IN,
423                                                             G_PARAM_READABLE | G_PARAM_WRITABLE ));
424
425         widget_class->set_scroll_adjustments_signal =
426                 g_signal_new ("set_scroll_adjustments",
427                               G_OBJECT_CLASS_TYPE (gobject_class),
428                               G_SIGNAL_RUN_LAST|G_SIGNAL_ACTION,
429                               G_STRUCT_OFFSET (ModestGtkhtmlMsgViewClass, set_scroll_adjustments),
430                               NULL, NULL,
431                               modest_marshal_VOID__POINTER_POINTER,
432                               G_TYPE_NONE, 2,
433                               GTK_TYPE_ADJUSTMENT,
434                               GTK_TYPE_ADJUSTMENT);
435 }
436
437 static void
438 set_property (GObject *object, 
439               guint prop_id, 
440               const GValue *value, 
441               GParamSpec *pspec)
442 {
443         ModestGtkhtmlMsgView *self = MODEST_GTKHTML_MSG_VIEW (object);
444
445         switch (prop_id) {
446         case PROP_HADJUSTMENT:
447                 set_hadjustment (self, g_value_get_object (value));
448                 break;
449         case PROP_VADJUSTMENT:
450                 set_vadjustment (self, g_value_get_object (value));
451                 break;
452         case PROP_SHADOW_TYPE:
453                 set_shadow_type (self, g_value_get_enum (value));
454                 break;
455         default:
456                 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
457                 break;
458         }
459 }
460
461 static void
462 get_property (GObject *object, 
463               guint prop_id, 
464               GValue *value, 
465               GParamSpec *pspec)
466 {
467         ModestGtkhtmlMsgView *self = MODEST_GTKHTML_MSG_VIEW (object);
468         ModestGtkhtmlMsgViewPrivate *priv = MODEST_GTKHTML_MSG_VIEW_GET_PRIVATE (self);
469
470         switch (prop_id) {
471         case PROP_HADJUSTMENT:
472                 g_value_set_object (value, priv->hadj);
473                 break;
474         case PROP_VADJUSTMENT:
475                 g_value_set_object (value, priv->vadj);
476                 break;
477         case PROP_SHADOW_TYPE:
478                 g_value_set_enum (value, priv->shadow_type);
479                 break;
480         default:
481                 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
482                 break;
483         }
484 }
485
486 static void
487 disconnect_hadjustment (ModestGtkhtmlMsgView *self)
488 {
489         ModestGtkhtmlMsgViewPrivate *priv = MODEST_GTKHTML_MSG_VIEW_GET_PRIVATE (self);
490
491         if (priv->hadj) {
492                 g_signal_handlers_disconnect_by_func(priv->hadj, adjustment_value_changed, self);
493                 g_object_unref (priv->hadj);
494                 priv->hadj = NULL;
495         }
496 }
497
498 static void
499 disconnect_vadjustment (ModestGtkhtmlMsgView *self)
500 {
501         ModestGtkhtmlMsgViewPrivate *priv = MODEST_GTKHTML_MSG_VIEW_GET_PRIVATE (self);
502
503         if (priv->vadj) {
504                 g_signal_handlers_disconnect_by_func(priv->vadj, adjustment_value_changed, self);
505                 g_object_unref (priv->vadj);
506                 priv->vadj = NULL;
507         }
508 }
509
510 static void 
511 get_view_allocation (ModestGtkhtmlMsgView *self, GtkAllocation *allocation)
512 {
513         /* This method gets the allocation of the widget in parent widget. It's the
514            real position and size of the widget */
515         ModestGtkhtmlMsgViewPrivate *priv = MODEST_GTKHTML_MSG_VIEW_GET_PRIVATE (self);
516         
517         allocation->x = 0;
518         allocation->y = 0;
519
520         if (priv->shadow_type != GTK_SHADOW_NONE) {
521                 allocation->x = GTK_WIDGET (self)->style->xthickness;
522                 allocation->y = GTK_WIDGET (self)->style->ythickness;
523         }
524
525         allocation->width = MAX (1, (GTK_WIDGET (self)->allocation.width) - allocation->x * 2);
526         allocation->height = MAX (1, (GTK_WIDGET (self)->allocation.height) - allocation->y * 2);
527
528 }
529
530 static void 
531 reclamp_adjustment (GtkAdjustment *adj, 
532                     gboolean *value_changed)
533 {
534         gdouble value = adj->value;
535
536         /* Correct value to be inside the expected values of a scroll */
537
538         value = CLAMP (value, 0, adj->upper - adj->page_size);
539
540         if (value != adj->value) {
541                 adj->value = value;
542                 if (value_changed)
543                         *value_changed = TRUE;
544         } else if (value_changed) {
545                 *value_changed = FALSE;
546         }
547 }
548
549 static void 
550 set_hadjustment_values (ModestGtkhtmlMsgView *self,
551                         gboolean *value_changed)
552 {
553         GtkAllocation view_allocation;
554         GtkAdjustment *hadj = get_hadjustment (self);
555
556         get_view_allocation (self, &view_allocation);
557         hadj->page_size = view_allocation.width;
558         hadj->step_increment = view_allocation.width * 0.1;
559         hadj->page_increment = view_allocation.width * 0.9;
560
561         hadj->lower = 0;
562         hadj->upper = view_allocation.width;
563
564         reclamp_adjustment (hadj, value_changed);
565
566 }
567
568 static void 
569 set_vadjustment_values (ModestGtkhtmlMsgView *self,
570                         gboolean *value_changed)
571 {
572         GtkAllocation view_allocation;
573         GtkAdjustment *vadj = get_vadjustment (self);
574         ModestGtkhtmlMsgViewPrivate *priv = MODEST_GTKHTML_MSG_VIEW_GET_PRIVATE (self);
575         gint full_height = 0;
576         GtkAdjustment *html_vadj;
577
578         get_view_allocation (self, &view_allocation);
579         vadj->page_size = view_allocation.height;
580         vadj->step_increment = view_allocation.height * 0.1;
581         vadj->page_increment = view_allocation.height * 0.9;
582
583         vadj->lower = 0;
584
585         if (priv->headers_box && GTK_WIDGET_VISIBLE(priv->headers_box)) {
586                 GtkRequisition child_requisition;
587
588                 gtk_widget_get_child_requisition (priv->headers_box, &child_requisition);
589                 full_height = child_requisition.height;
590         } else {
591                 full_height = 0;
592         }
593         
594         /* Get the real height of the embedded html */
595         if (priv->html_scroll && GTK_WIDGET_VISIBLE(priv->html_scroll)) {
596                 html_vadj = gtk_scrolled_window_get_vadjustment (GTK_SCROLLED_WINDOW (priv->html_scroll));
597                 full_height += html_vadj->upper;
598         }
599
600         vadj->upper = MAX (view_allocation.height, full_height);
601
602         reclamp_adjustment (vadj, value_changed);
603
604 }
605
606 static void
607 set_scroll_adjustments (ModestGtkhtmlMsgView *self,
608                         GtkAdjustment *hadj,
609                         GtkAdjustment *vadj)
610 {
611         ModestGtkhtmlMsgViewPrivate *priv = MODEST_GTKHTML_MSG_VIEW_GET_PRIVATE (self);
612         set_hadjustment (self, hadj);
613         set_vadjustment (self, vadj);
614
615         gtk_container_set_focus_vadjustment (GTK_CONTAINER (priv->body_view), vadj);
616 }
617
618 static void
619 realize (GtkWidget *widget)
620 {
621         ModestGtkhtmlMsgView *self = MODEST_GTKHTML_MSG_VIEW (widget);
622         ModestGtkhtmlMsgViewPrivate *priv = MODEST_GTKHTML_MSG_VIEW_GET_PRIVATE (self);
623         GtkAdjustment *hadj = get_hadjustment (self);
624         GtkAdjustment *vadj = get_vadjustment (self);
625         GdkWindowAttr attributes;
626         gint event_mask;
627         gint attributes_mask;
628         GtkAllocation view_allocation;
629
630         GTK_WIDGET_SET_FLAGS (widget, GTK_REALIZED);
631
632         /* The structure of the GdkWindow's is:
633          *    * widget->window: the shown gdkwindow embedding all the stuff inside
634          *    * view_window: a backing store gdkwindow containing the headers and contents
635          *      being scrolled. This window should have all the visible and non visible
636          *      widgets inside.
637          *    * headers_window: gdk window for headers_box.
638          *    * html_window: gdk window for html_scroll (the scrolled window containing the
639          *      body view showing the contents of the mail).
640          */
641
642         attributes.x = widget->allocation.x;
643         attributes.y = widget->allocation.y;
644         attributes.width = widget->allocation.width;
645         attributes.height = widget->allocation.height;
646         attributes.window_type = GDK_WINDOW_CHILD;
647         attributes.wclass = GDK_INPUT_OUTPUT;
648         attributes.visual = gtk_widget_get_visual (widget);
649         attributes.colormap = gtk_widget_get_colormap (widget);
650
651         event_mask = gtk_widget_get_events (widget) | GDK_EXPOSURE_MASK;
652         attributes.event_mask = event_mask | GDK_BUTTON_PRESS_MASK;
653         attributes_mask = GDK_WA_X | GDK_WA_Y | GDK_WA_VISUAL | GDK_WA_COLORMAP;
654
655         widget->window = gdk_window_new (gtk_widget_get_parent_window (widget),
656                                          &attributes, attributes_mask);
657         gdk_window_set_user_data (widget->window, self);
658
659         get_view_allocation (self, &view_allocation);
660
661         attributes.x = view_allocation.x;
662         attributes.y = view_allocation.y;
663         attributes.width = view_allocation.width;
664         attributes.height = view_allocation.height;
665         attributes.event_mask = 0;
666         priv->view_window = gdk_window_new (widget->window, &attributes, attributes_mask);
667         gdk_window_set_user_data (priv->view_window, self);
668         gdk_window_set_back_pixmap (priv->view_window, NULL, FALSE);
669
670         attributes.x = -hadj->value;
671         attributes.y = -vadj->value;
672         attributes.width = hadj->upper;
673         if (priv->headers_box)
674                 attributes.height = GTK_WIDGET (priv->headers_box)->allocation.height;
675         else
676                 attributes.height = 1;
677         attributes.event_mask = event_mask;
678
679         priv->headers_window = gdk_window_new (priv->view_window, &attributes, attributes_mask);
680         gdk_window_set_user_data (priv->headers_window, self);
681
682         if (priv->headers_box)
683                 gtk_widget_set_parent_window (priv->headers_box, priv->headers_window);
684
685         attributes.x = -hadj->value;
686         if (priv->headers_box)
687                 attributes.y = GTK_WIDGET (priv->headers_box)->allocation.height - vadj->value;
688         else 
689                 attributes.y = -vadj->value;
690         attributes.width = hadj->upper;
691         if (priv->headers_box)
692                 attributes.height = vadj->upper - GTK_WIDGET (priv->headers_box)->allocation.height;
693         else
694                 attributes.height = vadj->upper;
695         attributes.event_mask = event_mask;
696
697         priv->html_window = gdk_window_new (priv->view_window, &attributes, attributes_mask);
698         gdk_window_set_user_data (priv->html_window, self);
699
700         if (priv->html_scroll)
701                 gtk_widget_set_parent_window (priv->html_scroll, priv->html_window);
702
703         widget->style = gtk_style_attach (widget->style, widget->window);
704         gtk_style_set_background (widget->style, widget->window, GTK_STATE_NORMAL);
705         gtk_style_set_background (widget->style, priv->headers_window, GTK_STATE_NORMAL);
706         gtk_style_set_background (widget->style, priv->html_window, GTK_STATE_NORMAL);
707
708         gtk_paint_flat_box(widget->style, priv->headers_window, GTK_STATE_NORMAL,
709                            GTK_SHADOW_NONE, 
710                            NULL, widget, "msgviewheaders",
711                            0,0,-1,-1);
712         gtk_paint_flat_box(widget->style, priv->html_window, GTK_STATE_NORMAL,
713                            GTK_SHADOW_NONE, 
714                            NULL, widget, "msgviewcontents",
715                            0,0,-1,-1);
716
717         gdk_window_show (priv->view_window);
718         gdk_window_show (priv->headers_window);
719         gdk_window_show (priv->html_window);
720
721 }
722
723 static void
724 unrealize (GtkWidget *widget)
725 {
726         ModestGtkhtmlMsgView *self = MODEST_GTKHTML_MSG_VIEW (widget);
727         ModestGtkhtmlMsgViewPrivate *priv = MODEST_GTKHTML_MSG_VIEW_GET_PRIVATE (self);
728
729         gdk_window_set_user_data (priv->view_window, NULL);
730         gdk_window_destroy (priv->view_window);
731         priv->view_window = NULL;
732
733         gdk_window_set_user_data (priv->headers_window, NULL);
734         gdk_window_destroy (priv->headers_window);
735         priv->headers_window = NULL;
736
737         gdk_window_set_user_data (priv->html_window, NULL);
738         gdk_window_destroy (priv->html_window);
739         priv->html_window = NULL;
740
741         if (GTK_WIDGET_CLASS (parent_class)->unrealize)
742                 ( * GTK_WIDGET_CLASS (parent_class)->unrealize) (widget);
743
744 }
745
746 static gint
747 expose (GtkWidget *widget, 
748         GdkEventExpose *event)
749 {
750         ModestGtkhtmlMsgView *self;
751         ModestGtkhtmlMsgViewPrivate *priv;
752
753         if (GTK_WIDGET_DRAWABLE (widget)) {
754                 self = MODEST_GTKHTML_MSG_VIEW (widget);
755                 priv = MODEST_GTKHTML_MSG_VIEW_GET_PRIVATE (self);
756                 if (event->window == widget->window) {
757                         gtk_paint_shadow (widget->style, widget->window,
758                                           GTK_STATE_NORMAL, priv->shadow_type,
759                                           &event->area, widget, "msgview",
760                                           0,0,-1,-1);
761                 } else if (event->window == priv->headers_window) {
762                         gtk_paint_flat_box(widget->style, priv->headers_window, GTK_STATE_NORMAL,
763                                            GTK_SHADOW_NONE, 
764                                            &event->area, widget, "msgviewheaders",
765                                            0,0,-1,-1);
766                 } else if (event->window == priv->html_window) {
767                         gtk_paint_flat_box(widget->style, priv->html_window, GTK_STATE_NORMAL,
768                                            GTK_SHADOW_NONE, 
769                                            &event->area, widget, "msgviewcontents",
770                                            0,0,-1,-1);
771                 }
772                 if (priv->headers_box)
773                         gtk_container_propagate_expose (GTK_CONTAINER (self), priv->headers_box, event);
774                 if (priv->html_scroll)
775                         gtk_container_propagate_expose (GTK_CONTAINER (self), priv->html_scroll, event);
776                 (* GTK_WIDGET_CLASS (parent_class)->expose_event) (widget, event);
777         }
778
779         return FALSE;
780 }
781
782 static void 
783 forall (GtkContainer *container, 
784         gboolean include_internals,
785         GtkCallback callback,
786         gpointer userdata)
787 {
788         ModestGtkhtmlMsgViewPrivate *priv = MODEST_GTKHTML_MSG_VIEW_GET_PRIVATE (container);
789         g_return_if_fail (callback != NULL);
790
791         if (priv->headers_box)
792                 (*callback) (priv->headers_box, userdata);
793         if (priv->html_scroll)
794                 (*callback) (priv->html_scroll, userdata);
795 }
796
797 static void
798 container_remove (GtkContainer *container,
799                   GtkWidget *widget)
800 {
801         gboolean was_visible = FALSE;
802         ModestGtkhtmlMsgViewPrivate *priv = MODEST_GTKHTML_MSG_VIEW_GET_PRIVATE (container);
803         was_visible = GTK_WIDGET_VISIBLE (widget);
804         if (widget == priv->headers_box) {
805                 gtk_widget_unparent (priv->headers_box);
806                 priv->headers_box = NULL;
807         } else if (widget == priv->html_scroll) {
808                 gtk_widget_unparent (priv->html_scroll);
809                 priv->html_scroll = NULL;
810         } else {
811                 return;
812         }
813         if (was_visible)
814                 gtk_widget_queue_resize (GTK_WIDGET(container));
815
816 }
817
818 static void
819 size_request (GtkWidget *widget,
820               GtkRequisition *req)
821 {
822         ModestGtkhtmlMsgViewPrivate *priv = MODEST_GTKHTML_MSG_VIEW_GET_PRIVATE (widget);
823         GtkRequisition child_req;
824
825         req->width = 0;
826         req->height = 0;
827
828         gtk_widget_size_request (priv->headers_box, &child_req);
829         req->width = child_req.width;
830         req->height += child_req.height;
831         gtk_widget_size_request (priv->html_scroll, &child_req);
832         req->width = MAX (child_req.width, req->width);
833         req->height += child_req.height;
834
835 }
836
837 static void
838 size_allocate (GtkWidget *widget,
839                GtkAllocation *allocation)
840 {
841         ModestGtkhtmlMsgView *self = MODEST_GTKHTML_MSG_VIEW (widget);
842         ModestGtkhtmlMsgViewPrivate *priv = MODEST_GTKHTML_MSG_VIEW_GET_PRIVATE (self);
843         gboolean hadj_value_changed, vadj_value_changed;
844         GtkAllocation headers_allocation, html_allocation, view_allocation;
845         GtkAdjustment *html_vadj;
846
847         if (GTK_WIDGET_MAPPED (widget) &&
848             priv->shadow_type != GTK_SHADOW_NONE && 
849             (allocation->width != widget->allocation.width ||
850              allocation->height != widget->allocation.height))
851                 gdk_window_invalidate_rect (widget->window, NULL, FALSE);
852
853         widget->allocation = *allocation;
854         set_hadjustment_values (self, &hadj_value_changed);
855         set_vadjustment_values (self, &vadj_value_changed);
856
857         get_view_allocation (self, &view_allocation);
858
859         headers_allocation.x = 0;
860         headers_allocation.y = 0;
861         headers_allocation.width = view_allocation.width;
862         if (priv->headers_box)
863                 headers_allocation.height = GTK_WIDGET (priv->headers_box)->requisition.height;
864         else
865                 headers_allocation.height = 0;
866
867         html_vadj = gtk_scrolled_window_get_vadjustment (GTK_SCROLLED_WINDOW (priv->html_scroll));
868
869         html_allocation.x = 0;
870         html_allocation.y = headers_allocation.height;
871         html_allocation.width = view_allocation.width;
872         html_allocation.height = MAX ((gint) html_vadj->upper, (gint)(priv->vadj->upper - headers_allocation.height));
873
874         if (GTK_WIDGET_REALIZED (widget)) {
875                 gdk_window_move_resize (widget->window,
876                                         allocation->x,
877                                         allocation->y,
878                                         allocation->width,
879                                         allocation->height);
880
881                 gdk_window_move_resize (priv->view_window,
882                                         view_allocation.x,
883                                         view_allocation.y,
884                                         view_allocation.width,
885                                         view_allocation.height);
886                 gdk_window_move_resize (priv->headers_window,
887                                         0,
888                                         (gint) (- priv->vadj->value),
889                                         allocation->width,
890                                         headers_allocation.height);
891                 gdk_window_move_resize (priv->html_window,
892                                         (gint) (- priv->hadj->value),
893                                         (gint) (html_allocation.y - priv->vadj->value),
894                                         (gint) priv->hadj->upper,
895                                         html_allocation.height);
896         }
897
898         if (priv->headers_box && GTK_WIDGET_VISIBLE (priv->headers_box)) {
899                 gtk_widget_size_allocate (priv->headers_box, &headers_allocation);
900         }
901         if (priv->html_scroll && GTK_WIDGET_VISIBLE (priv->html_scroll)) {
902                 html_allocation.x = 0;
903                 html_allocation.y = 0;
904                 html_allocation.width = (gint) priv->hadj->upper;
905                 html_allocation.height = (gint) priv->vadj->upper - headers_allocation.height;
906                 gtk_widget_size_allocate (priv->html_scroll, &html_allocation);
907         }
908         gtk_adjustment_changed (priv->hadj);
909         gtk_adjustment_changed (priv->vadj);
910
911         if (hadj_value_changed)
912                 gtk_adjustment_value_changed (priv->hadj);
913         if (vadj_value_changed)
914                 gtk_adjustment_value_changed (priv->vadj);
915
916 }
917
918 static void 
919 adjustment_value_changed (GtkAdjustment *adj, gpointer data)
920 {
921         ModestGtkhtmlMsgView *self = NULL;
922         ModestGtkhtmlMsgViewPrivate *priv = NULL;
923
924         g_return_if_fail (GTK_IS_ADJUSTMENT (adj));
925         g_return_if_fail (MODEST_IS_GTKHTML_MSG_VIEW (data));
926
927         self = MODEST_GTKHTML_MSG_VIEW (data);
928         priv = MODEST_GTKHTML_MSG_VIEW_GET_PRIVATE (self);
929
930         if (GTK_WIDGET_REALIZED (self)) {
931                 GtkAdjustment *hadj = get_hadjustment (self);
932                 GtkAdjustment *vadj = get_vadjustment (self);
933                 gint headers_offset = 0;
934
935                 gtk_widget_queue_resize (priv->html_scroll);
936
937                 if (priv->headers_box && GTK_WIDGET_VISIBLE (priv->headers_box)) {
938                         gint old_x, old_y;
939                         gint new_x, new_y;
940                         gdk_window_get_position (priv->headers_window, &old_x, &old_y);
941                         new_x = 0;
942                         new_y = -vadj->value;
943
944                         headers_offset = GTK_WIDGET(priv->headers_box)->allocation.height;
945
946                         if (new_x != old_x || new_y != old_y) {
947                                 gdk_window_move (priv->headers_window, new_x, new_y);
948                                 gdk_window_process_updates (priv->headers_window, TRUE);
949                         }
950                 }
951                 
952                 if (priv->html_scroll && GTK_WIDGET_VISIBLE (priv->html_scroll)) {
953                         gint old_x, old_y;
954                         gint new_x, new_y;
955                         gdk_window_get_position (priv->html_window, &old_x, &old_y);
956                         new_x = -hadj->value;
957                         new_y = headers_offset - vadj->value;
958
959                         if (new_x != old_x || new_y != old_y) {
960                                 gdk_window_move (priv->html_window, new_x, new_y);
961                                 gdk_window_process_updates (priv->html_window, TRUE);
962                         }
963                 }
964                 
965         }
966 }
967
968 static void
969 html_adjustment_changed (GtkAdjustment *adj,
970                          gpointer userdata)
971 {
972         ModestGtkhtmlMsgView *self = MODEST_GTKHTML_MSG_VIEW (userdata);
973         ModestGtkhtmlMsgViewPrivate *priv = MODEST_GTKHTML_MSG_VIEW_GET_PRIVATE (self);
974         GtkAdjustment *html_vadj = gtk_scrolled_window_get_vadjustment (GTK_SCROLLED_WINDOW(priv->html_scroll));
975         gboolean vadj_changed;
976         gint new_height;
977
978         g_signal_stop_emission_by_name (G_OBJECT (adj), "changed");
979
980         priv->html_scroll->requisition.height = html_vadj->upper;
981         if (html_vadj->upper == priv->html_scroll->allocation.height)
982                 return;
983         priv->html_scroll->allocation.height = html_vadj->upper;
984
985         set_vadjustment_values (self, &vadj_changed);
986
987         new_height = MAX ((gint) html_vadj->upper, (gint) (priv->vadj->upper - priv->headers_box->allocation.height));
988         
989         gtk_adjustment_changed (priv->vadj);
990         if (GTK_WIDGET_DRAWABLE (priv->html_scroll)) {
991                 gdk_window_resize (priv->html_window, (gint) priv->hadj->upper, (gint) new_height);
992                 gdk_window_process_updates (priv->view_window, TRUE);
993                 gtk_container_resize_children (GTK_CONTAINER (self));
994         }
995         
996 }
997
998 gboolean
999 idle_readjust_scroll (ModestGtkhtmlMsgView *self)
1000 {
1001
1002         /* We're out the main lock */
1003         gdk_threads_enter ();
1004
1005         if (GTK_WIDGET_DRAWABLE (self)) {
1006                 ModestGtkhtmlMsgViewPrivate *priv = MODEST_GTKHTML_MSG_VIEW_GET_PRIVATE (self);
1007                 GtkAdjustment *html_vadj;
1008                 html_vadj = gtk_scrolled_window_get_vadjustment (GTK_SCROLLED_WINDOW (priv->html_scroll));
1009                 html_vadj->page_size = html_vadj->upper;
1010                 gtk_adjustment_changed (html_vadj);
1011                 gtk_widget_queue_resize (GTK_WIDGET (self));
1012                 gtk_widget_queue_draw (GTK_WIDGET (self));
1013
1014                 /* Just another hack for making readjust really work. This forces an update
1015                  * of the scroll, and then, make the scroll really update properly the
1016                  * the size and not corrupt scrollable area */
1017                 gtk_adjustment_set_value (priv->vadj, 1.0);
1018                 gtk_adjustment_set_value (priv->vadj, 0.0);
1019         }
1020         g_object_unref (G_OBJECT (self));
1021
1022         gdk_threads_leave ();
1023
1024         return FALSE;
1025 }
1026
1027 static void
1028 modest_gtkhtml_msg_view_init (ModestGtkhtmlMsgView *obj)
1029 {
1030         ModestGtkhtmlMsgViewPrivate *priv;
1031         GtkAdjustment *html_vadj;
1032         GtkWidget *separator;
1033
1034         GTK_WIDGET_UNSET_FLAGS (obj, GTK_NO_WINDOW);
1035         gtk_widget_set_redraw_on_allocate (GTK_WIDGET (obj), TRUE);
1036         gtk_container_set_reallocate_redraws (GTK_CONTAINER (obj), TRUE);
1037         gtk_container_set_resize_mode (GTK_CONTAINER (obj), GTK_RESIZE_QUEUE);
1038         
1039         priv = MODEST_GTKHTML_MSG_VIEW_GET_PRIVATE(obj);
1040
1041         priv->current_zoom = 1.0;
1042
1043         priv->hadj = NULL;
1044         priv->vadj = NULL;
1045         priv->shadow_type = GTK_SHADOW_IN;
1046         priv->view_window = NULL;
1047         priv->headers_window = NULL;
1048         priv->html_window = NULL;
1049
1050         priv->idle_motion_id = 0;
1051
1052         gtk_widget_push_composite_child ();
1053         priv->html_scroll = gtk_scrolled_window_new (NULL, NULL);
1054         gtk_widget_set_composite_name (priv->html_scroll, "contents");
1055         gtk_widget_pop_composite_child ();
1056         gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (priv->html_scroll), GTK_POLICY_NEVER, GTK_POLICY_NEVER);
1057
1058         priv->msg                     = NULL;
1059
1060         priv->body_view                 = GTK_WIDGET (g_object_new (MODEST_TYPE_GTKHTML_MIME_PART_VIEW, NULL));
1061         priv->mail_header_view        = GTK_WIDGET(modest_mail_header_view_new (TRUE));
1062         priv->view_images_button = gtk_button_new_with_label (_("mail_bd_external_images"));
1063         gtk_widget_set_no_show_all (priv->mail_header_view, TRUE);
1064         gtk_widget_set_no_show_all (priv->view_images_button, TRUE);
1065         priv->attachments_view        = GTK_WIDGET(modest_attachments_view_new (NULL));
1066
1067         g_signal_connect (G_OBJECT(priv->body_view), "activate_link",
1068                                        G_CALLBACK(on_activate_link), obj);
1069         g_signal_connect (G_OBJECT(priv->body_view), "fetch_url",
1070                                        G_CALLBACK(on_fetch_url), obj);
1071         g_signal_connect (G_OBJECT(priv->body_view), "link_hover",
1072                                        G_CALLBACK(on_link_hover), obj);
1073 #ifdef MAEMO_CHANGES
1074         g_signal_connect (G_OBJECT(priv->body_view), "motion-notify-event",
1075                           G_CALLBACK (motion_notify_event), obj);
1076 #endif
1077         g_signal_connect (G_OBJECT (priv->body_view), "button-press-event",
1078                           G_CALLBACK (button_press_event), obj);
1079         g_signal_connect (G_OBJECT (priv->body_view), "button-release-event",
1080                           G_CALLBACK (button_release_event), obj);
1081
1082         g_signal_connect (G_OBJECT (priv->mail_header_view), "recpt-activated", 
1083                           G_CALLBACK (on_recpt_activated), obj);
1084
1085         g_signal_connect (G_OBJECT (priv->attachments_view), "activate",
1086                           G_CALLBACK (on_attachment_activated), obj);
1087
1088         g_signal_connect (G_OBJECT (priv->view_images_button), "clicked",
1089                           G_CALLBACK (on_view_images_clicked), obj);
1090
1091         html_vadj = gtk_scrolled_window_get_vadjustment (GTK_SCROLLED_WINDOW(priv->html_scroll));
1092
1093         g_signal_connect (G_OBJECT (html_vadj), "changed",
1094                           G_CALLBACK (html_adjustment_changed), obj);
1095
1096         gtk_widget_push_composite_child ();
1097         priv->headers_box = gtk_vbox_new (FALSE, MODEST_MARGIN_DEFAULT);
1098         gtk_widget_set_composite_name (priv->headers_box, "headers");
1099         gtk_widget_pop_composite_child ();
1100
1101         if (priv->mail_header_view)
1102                 gtk_box_pack_start (GTK_BOX(priv->headers_box), priv->mail_header_view, FALSE, FALSE, 0);
1103         if (priv->view_images_button) {
1104                 GtkWidget *hbuttonbox;
1105                 
1106                 hbuttonbox = gtk_hbutton_box_new ();
1107                 gtk_container_add (GTK_CONTAINER (hbuttonbox), priv->view_images_button);
1108                 gtk_box_pack_start (GTK_BOX (priv->headers_box), hbuttonbox, FALSE, FALSE, 0);
1109                 gtk_widget_hide (priv->view_images_button);
1110         }
1111         
1112         if (priv->attachments_view) {
1113                 gchar *att_label = g_strconcat (_("mcen_me_viewer_attachments"), ":", NULL);
1114
1115                 priv->attachments_box = (GtkWidget *)
1116                         modest_mail_header_view_add_custom_header (MODEST_MAIL_HEADER_VIEW (priv->mail_header_view),
1117                                                                    att_label,
1118                                                                    priv->attachments_view,
1119                                                                    FALSE, FALSE);
1120                 gtk_widget_hide_all (priv->attachments_box);
1121                 g_free (att_label);
1122         }
1123
1124         separator = gtk_hseparator_new ();
1125         gtk_box_pack_start (GTK_BOX(priv->headers_box), separator, FALSE, FALSE, 0);
1126
1127         gtk_widget_set_parent (priv->headers_box, GTK_WIDGET (obj));
1128
1129         if (priv->body_view) {
1130                 gtk_container_add (GTK_CONTAINER (priv->html_scroll), priv->body_view);
1131                 gtk_widget_set_parent (priv->html_scroll, GTK_WIDGET(obj));
1132 #ifdef MAEMO_CHANGES
1133                 gtk_widget_tap_and_hold_setup (GTK_WIDGET (priv->body_view), NULL, NULL, 0);
1134                 g_signal_connect (G_OBJECT (priv->body_view), "tap-and-hold", G_CALLBACK (on_tap_and_hold), obj);
1135                 g_signal_connect (G_OBJECT (priv->body_view), "tap-and-hold-query", G_CALLBACK (on_tap_and_hold_query), obj);
1136 #endif
1137         }
1138         
1139 }
1140         
1141
1142 static void
1143 modest_gtkhtml_msg_view_finalize (GObject *obj)
1144 {       
1145         ModestGtkhtmlMsgViewPrivate *priv;
1146         priv = MODEST_GTKHTML_MSG_VIEW_GET_PRIVATE (obj);
1147
1148         if (priv->msg) {
1149                 g_object_unref (G_OBJECT(priv->msg));
1150                 priv->msg = NULL;
1151         }
1152
1153         if (priv->idle_motion_id > 0) {
1154                 g_source_remove (priv->idle_motion_id);
1155                 priv->idle_motion_id = 0;
1156         }
1157         
1158         /* we cannot disconnect sigs, because priv->body_view is
1159          * already dead */
1160         
1161         disconnect_vadjustment (MODEST_GTKHTML_MSG_VIEW(obj));
1162         disconnect_hadjustment (MODEST_GTKHTML_MSG_VIEW(obj));
1163
1164         priv->body_view = NULL;
1165         priv->attachments_view = NULL;
1166
1167         G_OBJECT_CLASS(parent_class)->finalize (obj);           
1168 }
1169
1170 static void
1171 modest_gtkhtml_msg_view_destroy (GtkObject *obj)
1172 {       
1173         disconnect_vadjustment (MODEST_GTKHTML_MSG_VIEW(obj));
1174         disconnect_hadjustment (MODEST_GTKHTML_MSG_VIEW(obj));
1175
1176         GTK_OBJECT_CLASS(parent_class)->destroy (obj);          
1177 }
1178
1179 /* INTERNAL METHODS */
1180
1181 #ifdef MAEMO_CHANGES
1182 static gboolean 
1183 motion_notify_event (GtkWidget *widget,
1184                      GdkEventMotion *event,
1185                      gpointer userdata)
1186 {
1187         ModestGtkhtmlMsgViewPrivate *priv = MODEST_GTKHTML_MSG_VIEW_GET_PRIVATE (userdata);
1188
1189         /* Use panning information in gtkhtml widget to support also vertical panning */
1190
1191         if (GTK_HTML (widget)->panning) {
1192                 gint y, dy;
1193                 gdouble value;
1194
1195                 gdk_window_get_pointer (GTK_LAYOUT (widget)->bin_window, NULL, &y, NULL);
1196                 dy = y - GTK_HTML (widget)->lasty;
1197                 value = priv->vadj->value - (gdouble) dy;
1198
1199                 if (value < priv->vadj->lower)
1200                         value = priv->vadj->lower;
1201                 else if (value > priv->vadj->upper - priv->vadj->page_size)
1202                         value = priv->vadj->upper - priv->vadj->page_size;
1203                 gtk_adjustment_set_value (priv->vadj, value);
1204                 
1205         } 
1206         return FALSE;
1207 }
1208 #endif
1209
1210 static gboolean
1211 idle_motion (gpointer userdata)
1212 {
1213         ModestGtkhtmlMsgViewPrivate *priv = MODEST_GTKHTML_MSG_VIEW_GET_PRIVATE (userdata);
1214         if (GTK_HTML (priv->body_view)->in_selection_drag) {
1215                 gdouble offset;
1216                 GtkAdjustment *adj;
1217                 gint gdk_y;
1218                 gdk_window_get_pointer (gtk_widget_get_parent_window (priv->body_view), NULL, &gdk_y, NULL);
1219                 offset= (gdouble) (priv->headers_box->requisition.height + gdk_y);
1220                 adj = GTK_ADJUSTMENT (priv->vadj);
1221                 if (offset < adj->value + adj->step_increment) {
1222                         gtk_adjustment_set_value (adj, MAX (offset + adj->page_increment - adj->page_size, 0.0));
1223                 } else if (offset > adj->value + adj->page_increment) {
1224                         gtk_adjustment_set_value (adj, MIN (offset - adj->page_increment, adj->upper - adj->page_size));
1225                 }
1226                 gtk_widget_queue_resize (userdata);
1227         }
1228         return TRUE;
1229 }
1230
1231 static gboolean 
1232 button_press_event (GtkWidget *widget,
1233                     GdkEventButton *event,
1234                     gpointer userdata)
1235 {
1236         ModestGtkhtmlMsgViewPrivate *priv = MODEST_GTKHTML_MSG_VIEW_GET_PRIVATE (userdata);
1237
1238         if (priv->idle_motion_id == 0) {
1239                 priv->idle_motion_id = g_timeout_add (200, idle_motion, userdata);
1240         }
1241         return FALSE;
1242 }
1243
1244 static gboolean 
1245 button_release_event (GtkWidget *widget,
1246                       GdkEventButton *event,
1247                       gpointer userdata)
1248 {
1249         ModestGtkhtmlMsgViewPrivate *priv = MODEST_GTKHTML_MSG_VIEW_GET_PRIVATE (userdata);
1250
1251         if (priv->idle_motion_id > 0) {
1252                 gint gdk_y;
1253                 g_source_remove (priv->idle_motion_id);
1254                 
1255                 priv->idle_motion_id = 0;;
1256                 gdk_window_get_pointer (gtk_widget_get_parent_window (priv->body_view), NULL, &gdk_y, NULL);
1257                 event->y = (gdouble) gdk_y;
1258         }
1259         return FALSE;
1260 }
1261
1262 static GtkAdjustment *
1263 get_vadjustment (ModestGtkhtmlMsgView *self)
1264 {
1265         ModestGtkhtmlMsgViewPrivate *priv = MODEST_GTKHTML_MSG_VIEW_GET_PRIVATE (self);
1266
1267         if (!priv->vadj)
1268                 set_vadjustment (self, NULL);
1269
1270         return priv->vadj;
1271         
1272 }
1273
1274 static GtkAdjustment *
1275 get_hadjustment (ModestGtkhtmlMsgView *self)
1276 {
1277         ModestGtkhtmlMsgViewPrivate *priv = MODEST_GTKHTML_MSG_VIEW_GET_PRIVATE (self);
1278
1279         if (!priv->hadj)
1280                 set_hadjustment (self, NULL);
1281
1282         return priv->hadj;
1283         
1284 }
1285
1286 static void
1287 set_hadjustment (ModestGtkhtmlMsgView *self, GtkAdjustment *hadj)
1288 {
1289         ModestGtkhtmlMsgViewPrivate *priv = MODEST_GTKHTML_MSG_VIEW_GET_PRIVATE (self);
1290         gboolean value_changed;
1291         
1292         if (hadj && hadj == priv->hadj)
1293                 return;
1294
1295         if (!hadj)
1296                 hadj = GTK_ADJUSTMENT (gtk_adjustment_new (0.0,0.0,0.0,0.0,0.0,0.0));
1297         disconnect_hadjustment (self);
1298         g_object_ref (G_OBJECT (hadj));
1299         gtk_object_sink (GTK_OBJECT (hadj));
1300         priv->hadj = hadj;
1301         set_hadjustment_values (self, &value_changed);
1302
1303         g_signal_connect (hadj, "value_changed", G_CALLBACK (adjustment_value_changed),
1304                           self);
1305
1306         gtk_adjustment_changed (hadj);
1307         if (value_changed)
1308                 gtk_adjustment_value_changed (hadj);
1309         else
1310                 adjustment_value_changed (hadj, self);
1311
1312         g_object_notify (G_OBJECT (self), "hadjustment");
1313 }
1314
1315 static void
1316 set_vadjustment (ModestGtkhtmlMsgView *self, GtkAdjustment *vadj)
1317 {
1318         ModestGtkhtmlMsgViewPrivate *priv = MODEST_GTKHTML_MSG_VIEW_GET_PRIVATE (self);
1319         gboolean value_changed;
1320
1321         if (vadj && vadj == priv->vadj)
1322                 return;
1323
1324         if (!vadj)
1325                 vadj = (GtkAdjustment *) gtk_adjustment_new (0.0,0.0,0.0,0.0,0.0,0.0);
1326         disconnect_vadjustment (self);
1327         g_object_ref (G_OBJECT (vadj));
1328         gtk_object_sink (GTK_OBJECT (vadj));
1329         priv->vadj = vadj;
1330         set_vadjustment_values (self, &value_changed);
1331
1332         g_signal_connect (vadj, "value_changed", G_CALLBACK (adjustment_value_changed),
1333                           self);
1334
1335         gtk_adjustment_changed (vadj);
1336         if (value_changed)
1337                 gtk_adjustment_value_changed (vadj);
1338         else
1339                 adjustment_value_changed (vadj, self);
1340
1341         g_object_notify (G_OBJECT (self), "vadjustment");
1342 }
1343
1344 static void
1345 set_shadow_type (ModestGtkhtmlMsgView *self,
1346                  GtkShadowType shadow_type)
1347 {
1348         ModestGtkhtmlMsgViewPrivate *priv;
1349         g_return_if_fail (MODEST_IS_GTKHTML_MSG_VIEW (self));
1350
1351         priv = MODEST_GTKHTML_MSG_VIEW_GET_PRIVATE (self);
1352         
1353         if (priv->shadow_type != shadow_type) {
1354                 priv->shadow_type = shadow_type;
1355                 
1356                 if (GTK_WIDGET_VISIBLE (self)) {
1357                         gtk_widget_size_allocate (GTK_WIDGET (self), &(GTK_WIDGET (self)->allocation));
1358                         gtk_widget_queue_draw (GTK_WIDGET (self));
1359                 }
1360                 g_object_notify (G_OBJECT (self), "shadow-type");
1361         }
1362 }
1363
1364 static GtkShadowType
1365 get_shadow_type (ModestGtkhtmlMsgView *self)
1366 {
1367         ModestGtkhtmlMsgViewPrivate *priv;
1368         g_return_val_if_fail (MODEST_IS_GTKHTML_MSG_VIEW (self), GTK_SHADOW_NONE);
1369         priv = MODEST_GTKHTML_MSG_VIEW_GET_PRIVATE (self);
1370         
1371         return priv->shadow_type;
1372 }
1373
1374 GtkWidget*
1375 modest_msg_view_new (TnyMsg *msg)
1376 {
1377         GObject *obj;
1378         ModestGtkhtmlMsgView* self;
1379         
1380         obj  = G_OBJECT(g_object_new(MODEST_TYPE_GTKHTML_MSG_VIEW, NULL));
1381         self = MODEST_GTKHTML_MSG_VIEW(obj);
1382         tny_msg_view_set_msg (TNY_MSG_VIEW (self), msg);
1383
1384         return GTK_WIDGET(self);
1385 }
1386
1387 #ifdef MAEMO_CHANGES
1388 static void
1389 on_tap_and_hold (GtkWidget *widget,
1390                  gpointer data)
1391 {
1392         ModestGtkhtmlMsgView *self = (ModestGtkhtmlMsgView *) data;
1393         ModestGtkhtmlMsgViewPrivate *priv = MODEST_GTKHTML_MSG_VIEW_GET_PRIVATE (self);
1394
1395         g_signal_emit_by_name (G_OBJECT (self), "link-contextual", priv->last_url);
1396 }
1397
1398 static gboolean
1399 on_tap_and_hold_query (GtkWidget *widget,
1400                        GdkEvent *event,
1401                        gpointer data)
1402 {
1403         ModestGtkhtmlMsgView *self = (ModestGtkhtmlMsgView *) data;
1404         ModestGtkhtmlMsgViewPrivate *priv = MODEST_GTKHTML_MSG_VIEW_GET_PRIVATE (self);
1405
1406         /* Don't show the tap and hold animation if no url below */
1407         return (priv->last_url == NULL);
1408 }
1409 #endif
1410
1411 static void
1412 on_recpt_activated (ModestMailHeaderView *header_view, 
1413                     const gchar *address,
1414                     ModestGtkhtmlMsgView *self)
1415 {
1416         g_signal_emit_by_name (G_OBJECT (self), "recpt-activated", address);
1417 }
1418
1419 static void
1420 on_attachment_activated (ModestAttachmentsView * att_view, TnyMimePart *mime_part, gpointer self)
1421 {
1422
1423         g_signal_emit_by_name (G_OBJECT(self), "attachment_clicked", mime_part);
1424 }
1425
1426 static void
1427 on_view_images_clicked (GtkButton * button, gpointer self)
1428 {
1429         ModestGtkhtmlMsgViewPrivate *priv = MODEST_GTKHTML_MSG_VIEW_GET_PRIVATE (self);
1430         TnyMimePart *part;
1431
1432         modest_mime_part_view_set_view_images (MODEST_MIME_PART_VIEW (priv->body_view), TRUE);
1433         gtk_widget_hide (priv->view_images_button);
1434         part = tny_mime_part_view_get_part (TNY_MIME_PART_VIEW (priv->body_view));
1435         tny_mime_part_view_set_part (TNY_MIME_PART_VIEW (priv->body_view), part);
1436         tny_msg_set_allow_external_images (TNY_MSG (priv->msg), TRUE);
1437         g_object_unref (part);
1438         
1439
1440 }
1441
1442 static gboolean
1443 on_activate_link (GtkWidget *widget, const gchar *uri, ModestGtkhtmlMsgView *self)
1444 {
1445         gboolean result;
1446         g_return_val_if_fail (self, FALSE);
1447         
1448         g_signal_emit_by_name (G_OBJECT(self), "activate-link", uri, &result);
1449
1450         return result;
1451 }
1452
1453
1454 static gboolean
1455 on_link_hover (GtkWidget *widget, const gchar *uri, ModestGtkhtmlMsgView *self)
1456 {
1457         ModestGtkhtmlMsgViewPrivate *priv = MODEST_GTKHTML_MSG_VIEW_GET_PRIVATE (self);
1458         gboolean result;
1459
1460         g_free (priv->last_url);
1461         priv->last_url = g_strdup (uri);
1462
1463         g_signal_emit_by_name (G_OBJECT(self), "link-hover", uri, &result);
1464
1465         return result;
1466 }
1467
1468
1469
1470 static TnyMimePart *
1471 find_cid_image (TnyMsg *msg, const gchar *cid)
1472 {
1473         TnyMimePart *part = NULL;
1474         TnyList *parts;
1475         TnyIterator *iter;
1476         
1477         g_return_val_if_fail (msg, NULL);
1478         g_return_val_if_fail (cid, NULL);
1479         
1480         parts  = TNY_LIST (tny_simple_list_new());
1481
1482         tny_mime_part_get_parts (TNY_MIME_PART (msg), parts); 
1483         iter   = tny_list_create_iterator (parts);
1484         
1485         while (!tny_iterator_is_done(iter)) {
1486                 const gchar *part_cid;
1487
1488                 part = TNY_MIME_PART(tny_iterator_get_current(iter));
1489                 part_cid = tny_mime_part_get_content_id (part);
1490
1491                 /* if there is no content id, try the content location;
1492                  * this is what Outlook seems to use when it converts
1493                  * it's internal richtext to html
1494                  */
1495                 if (!part_cid)
1496                         part_cid = tny_mime_part_get_content_location (part);
1497                 
1498                 if (part_cid && strcmp (cid, part_cid) == 0)
1499                         break;
1500
1501                 if (tny_mime_part_content_type_is (part, "multipart/related")) {
1502                         TnyList *related_parts = TNY_LIST (tny_simple_list_new ());
1503                         TnyIterator *related_iter = NULL;
1504                         TnyMimePart *related_part = NULL;
1505
1506                         tny_mime_part_get_parts (part, related_parts);
1507                         related_iter = tny_list_create_iterator (related_parts);
1508
1509                         while (!tny_iterator_is_done (related_iter)) {
1510                                 related_part = TNY_MIME_PART (tny_iterator_get_current (related_iter));
1511                                 part_cid = tny_mime_part_get_content_id (related_part);
1512                                 if (part_cid && strcmp (cid, part_cid) == 0) {
1513                                         break;
1514                                 }
1515                                 g_object_unref (related_part);
1516                                 related_part = NULL;
1517                                 tny_iterator_next (related_iter);
1518                         }
1519
1520                         g_object_unref (related_iter);
1521                         g_object_unref (related_parts);
1522                         if (related_part != NULL) {
1523                                 g_object_unref (part);
1524                                 part = related_part;
1525                                 break;
1526                         }
1527                 }
1528
1529                 g_object_unref (G_OBJECT(part));
1530         
1531                 part = NULL;
1532                 tny_iterator_next (iter);
1533         }
1534         
1535         g_object_unref (G_OBJECT(iter));        
1536         g_object_unref (G_OBJECT(parts));
1537         
1538         return part;
1539 }
1540
1541
1542 static gboolean
1543 on_fetch_url (GtkWidget *widget, const gchar *uri,
1544               TnyStream *stream, ModestGtkhtmlMsgView *self)
1545 {
1546         ModestGtkhtmlMsgViewPrivate *priv;
1547         const gchar* my_uri;
1548         TnyMimePart *part = NULL;
1549         
1550
1551
1552         priv = MODEST_GTKHTML_MSG_VIEW_GET_PRIVATE (self);
1553
1554         if (modest_mime_part_view_has_external_images (MODEST_MIME_PART_VIEW (priv->body_view)))
1555                 gtk_widget_show (priv->view_images_button);
1556         /*
1557          * we search for either something starting with cid:, or something
1558          * with no prefix at all; this latter case occurs when sending mails
1559          * with MS Outlook in rich-text mode, and 'attach-as-object
1560          */
1561         if (g_str_has_prefix (uri, "cid:"))  
1562                 my_uri = uri + 4;  /* +4 ==> skip "cid:" */
1563         else
1564                 my_uri = uri;
1565         
1566         /* now try to find the embedded image */
1567         part = find_cid_image (priv->msg, my_uri);
1568
1569         if (!part) {
1570                 if (g_str_has_prefix (uri, "http:")) {
1571                         if (modest_mime_part_view_get_view_images (MODEST_MIME_PART_VIEW (priv->body_view))) {
1572                                 gboolean result = FALSE;
1573                                 g_signal_emit_by_name (self, "fetch-image", uri, stream, &result);
1574                                 return result;
1575                         } else {
1576                                 /* we return immediately to get a "image not found" icon */
1577                                 tny_stream_close (stream);
1578                                 return TRUE;
1579                         }
1580                 } else {
1581                         return FALSE;
1582                 }
1583         }
1584
1585         tny_mime_part_decode_to_stream ((TnyMimePart*)part, stream, NULL);
1586         tny_stream_close (stream);
1587         g_object_unref (G_OBJECT(part));
1588         return TRUE;
1589 }
1590
1591 static void
1592 set_message (ModestGtkhtmlMsgView *self, TnyMsg *msg)
1593 {
1594         TnyMimePart *body;
1595         ModestGtkhtmlMsgViewPrivate *priv;
1596         TnyHeader *header;
1597         GtkAdjustment *html_vadj;
1598         
1599         g_return_if_fail (self);
1600         
1601         priv = MODEST_GTKHTML_MSG_VIEW_GET_PRIVATE(self);
1602         gtk_widget_set_no_show_all (priv->mail_header_view, FALSE);
1603         modest_mime_part_view_set_view_images (MODEST_MIME_PART_VIEW (priv->body_view), FALSE);
1604
1605         html_vadj = gtk_scrolled_window_get_vadjustment (GTK_SCROLLED_WINDOW (priv->html_scroll));
1606         html_vadj->upper = 0;
1607         html_vadj->page_size = 0;
1608         g_signal_emit_by_name (G_OBJECT (html_vadj), "changed");
1609
1610
1611         if (msg != priv->msg) {
1612                 if (priv->msg)
1613                         g_object_unref (G_OBJECT(priv->msg));
1614                 if (msg)
1615                         g_object_ref   (G_OBJECT(msg));
1616                 priv->msg = msg;
1617         }
1618         
1619         if (!msg) {
1620                 tny_header_view_clear (TNY_HEADER_VIEW (priv->mail_header_view));
1621                 modest_attachments_view_set_message (MODEST_ATTACHMENTS_VIEW (priv->attachments_view), NULL);
1622                 gtk_widget_hide_all (priv->mail_header_view);
1623                 gtk_widget_hide_all (priv->attachments_box);
1624                 gtk_widget_set_no_show_all (priv->mail_header_view, TRUE);
1625                 tny_mime_part_view_clear (TNY_MIME_PART_VIEW (priv->body_view));
1626                 gtk_widget_queue_resize (GTK_WIDGET(self));
1627                 gtk_widget_queue_draw (GTK_WIDGET(self));
1628                 return;
1629         }
1630
1631         header = tny_msg_get_header (msg);
1632         tny_header_view_set_header (TNY_HEADER_VIEW (priv->mail_header_view), header);
1633         g_object_unref (header);
1634
1635         modest_attachments_view_set_message (MODEST_ATTACHMENTS_VIEW(priv->attachments_view),
1636                                              msg);
1637
1638         modest_mime_part_view_set_view_images (MODEST_MIME_PART_VIEW (priv->body_view), tny_msg_get_allow_external_images (msg));
1639         
1640         body = modest_tny_msg_find_body_part (msg, TRUE);
1641         if (body) {
1642                 tny_mime_part_view_set_part (TNY_MIME_PART_VIEW (priv->body_view), body);
1643                 g_object_unref (body);
1644
1645                 if(modest_attachments_view_has_attachments (MODEST_ATTACHMENTS_VIEW (priv->attachments_view))) {
1646                         gtk_widget_show_all (priv->attachments_box);
1647                 } else {
1648                         gtk_widget_hide_all (priv->attachments_box);
1649                 }
1650
1651         } else 
1652                 tny_mime_part_view_clear (TNY_MIME_PART_VIEW (priv->body_view));
1653
1654         if (modest_mime_part_view_has_external_images (MODEST_MIME_PART_VIEW (priv->body_view)) &&
1655             !modest_mime_part_view_get_view_images (MODEST_MIME_PART_VIEW (priv->body_view))) {
1656                 gtk_widget_show (priv->view_images_button);
1657         } else {
1658                 gtk_widget_hide (priv->view_images_button);
1659         }
1660
1661         gtk_widget_show (priv->body_view);
1662         gtk_widget_set_no_show_all (priv->attachments_box, TRUE);
1663         gtk_widget_show_all (priv->mail_header_view);
1664         gtk_widget_set_no_show_all (priv->attachments_box, FALSE);
1665         gtk_widget_set_no_show_all (priv->mail_header_view, TRUE);
1666         gtk_widget_queue_resize (GTK_WIDGET(self));
1667         gtk_widget_queue_draw (GTK_WIDGET(self));
1668
1669         if (priv->hadj != NULL)
1670                 priv->hadj->value = 0.0;
1671         if (priv->vadj != NULL)
1672                 priv->vadj->value = 0.0;
1673
1674         g_signal_emit_by_name (G_OBJECT (html_vadj), "changed");
1675
1676         /* This is a hack to force reallocation of scroll after drawing all the stuff. This
1677          * makes the html view get the proper and expected size and prevent being able to scroll
1678          * the buffer when it shouldn't be scrollable */
1679         g_object_ref (self);
1680         g_timeout_add (250, (GSourceFunc) idle_readjust_scroll, self);
1681 }
1682
1683
1684 static TnyMsg*
1685 get_message (ModestGtkhtmlMsgView *self)
1686 {
1687         TnyMsg *msg;
1688
1689         g_return_val_if_fail (MODEST_IS_GTKHTML_MSG_VIEW (self), NULL);
1690
1691         msg = MODEST_GTKHTML_MSG_VIEW_GET_PRIVATE(self)->msg;
1692
1693         if (msg)
1694                 g_object_ref (msg);
1695         
1696         return msg;
1697 }
1698
1699 static gboolean 
1700 is_empty (ModestGtkhtmlMsgView *self)
1701 {
1702         ModestGtkhtmlMsgViewPrivate *priv = MODEST_GTKHTML_MSG_VIEW_GET_PRIVATE (self);
1703
1704         return modest_mime_part_view_is_empty (MODEST_MIME_PART_VIEW (priv->body_view));
1705 }
1706
1707 static void
1708 set_zoom (ModestGtkhtmlMsgView *self, gdouble zoom)
1709 {
1710         ModestGtkhtmlMsgViewPrivate *priv;
1711
1712         g_return_if_fail (MODEST_IS_GTKHTML_MSG_VIEW (self));
1713         priv = MODEST_GTKHTML_MSG_VIEW_GET_PRIVATE (self);
1714
1715         modest_zoomable_set_zoom (MODEST_ZOOMABLE(priv->body_view), zoom);
1716
1717         gtk_widget_queue_resize (priv->body_view);
1718 }
1719
1720 static gdouble
1721 get_zoom (ModestGtkhtmlMsgView *self)
1722 {
1723         ModestGtkhtmlMsgViewPrivate *priv;
1724
1725         g_return_val_if_fail (MODEST_IS_GTKHTML_MSG_VIEW (self), 1.0);
1726         priv = MODEST_GTKHTML_MSG_VIEW_GET_PRIVATE (self);
1727
1728         return modest_zoomable_get_zoom (MODEST_ZOOMABLE (priv->body_view));
1729 }
1730
1731 static TnyHeaderFlags
1732 get_priority (ModestGtkhtmlMsgView *self)
1733 {
1734         ModestGtkhtmlMsgViewPrivate *priv;
1735
1736         g_return_val_if_fail (MODEST_IS_GTKHTML_MSG_VIEW (self), 0);
1737
1738         priv = MODEST_GTKHTML_MSG_VIEW_GET_PRIVATE (self);
1739
1740         return modest_mail_header_view_get_priority (MODEST_MAIL_HEADER_VIEW (priv->mail_header_view));
1741 }
1742
1743 static void
1744 set_priority (ModestGtkhtmlMsgView *self, TnyHeaderFlags flags)
1745 {
1746         ModestGtkhtmlMsgViewPrivate *priv;
1747
1748         g_return_if_fail (MODEST_IS_GTKHTML_MSG_VIEW (self));
1749         priv = MODEST_GTKHTML_MSG_VIEW_GET_PRIVATE (self);
1750
1751         modest_mail_header_view_set_priority (MODEST_MAIL_HEADER_VIEW (priv->mail_header_view), flags);
1752 }
1753
1754 /* INCREMENTAL SEARCH IMPLEMENTATION */
1755
1756 static gboolean 
1757 search (ModestGtkhtmlMsgView *self, const gchar *search)
1758 {
1759         ModestGtkhtmlMsgViewPrivate *priv;
1760         gboolean result;
1761         GtkAdjustment *vadj, *tmp_vadj;
1762         gdouble y_offset;
1763
1764         g_return_val_if_fail (MODEST_IS_GTKHTML_MSG_VIEW (self), FALSE);
1765
1766         priv = MODEST_GTKHTML_MSG_VIEW_GET_PRIVATE (self);
1767         vadj = gtk_layout_get_vadjustment (GTK_LAYOUT (priv->body_view));
1768         g_object_ref (vadj);
1769         tmp_vadj = GTK_ADJUSTMENT (gtk_adjustment_new (0.0, vadj->lower, vadj->upper, vadj->step_increment, 32.0, 32.0));
1770         gtk_layout_set_vadjustment (GTK_LAYOUT (priv->body_view), tmp_vadj);
1771         result = modest_isearch_view_search (MODEST_ISEARCH_VIEW (priv->body_view),
1772                                              search);
1773
1774         if (result) {
1775                 gint x, y, w, h;
1776                 gdouble offset_top, offset_bottom;
1777                 GtkAdjustment *adj;
1778                 if (modest_isearch_view_get_selection_area (MODEST_ISEARCH_VIEW (priv->body_view), &x, &y, &w, &h)) {
1779                         offset_top = (gdouble) (priv->headers_box->requisition.height + y);
1780                         offset_bottom = (gdouble) (priv->headers_box->requisition.height + y + h);
1781                         adj = GTK_ADJUSTMENT (priv->vadj);
1782                         if (offset_top < adj->value)
1783                                 gtk_adjustment_set_value (adj, offset_top + adj->page_increment - adj->page_size);
1784                         else if (offset_bottom > adj->value + adj->page_increment)
1785                                 gtk_adjustment_set_value (adj, offset_bottom - adj->page_increment);
1786                 }
1787         }
1788
1789         y_offset = tmp_vadj->value;
1790         gtk_layout_set_vadjustment (GTK_LAYOUT (priv->body_view), vadj);
1791         g_object_unref (vadj);
1792
1793         return result;
1794 }
1795
1796 static gboolean
1797 search_next (ModestGtkhtmlMsgView *self)
1798 {
1799         ModestGtkhtmlMsgViewPrivate *priv;
1800         gboolean result;
1801
1802         g_return_val_if_fail (MODEST_IS_GTKHTML_MSG_VIEW (self), FALSE);
1803
1804         priv = MODEST_GTKHTML_MSG_VIEW_GET_PRIVATE (self);
1805         result = modest_isearch_view_search_next (MODEST_ISEARCH_VIEW (priv->body_view));
1806
1807         if (result) {
1808                 gint x, y, w, h;
1809                 gdouble offset_top, offset_bottom;
1810                 GtkAdjustment *adj;
1811
1812                 if (modest_isearch_view_get_selection_area (MODEST_ISEARCH_VIEW (priv->body_view), &x, &y, &w, &h)) {
1813                         offset_top = (gdouble) (priv->headers_box->requisition.height + y);
1814                         offset_bottom = (gdouble) (priv->headers_box->requisition.height + y + h);
1815                         adj = GTK_ADJUSTMENT (priv->vadj);
1816                         if (offset_top < adj->value)
1817                                 gtk_adjustment_set_value (adj, offset_top + adj->page_increment - adj->page_size);
1818                         else if (offset_bottom > adj->value + adj->page_increment)
1819                                 gtk_adjustment_set_value (adj, offset_bottom - adj->page_increment);
1820                 }
1821         }
1822         return result;
1823 }
1824
1825 static TnyList *
1826 get_selected_attachments (ModestGtkhtmlMsgView *self)
1827 {
1828         ModestGtkhtmlMsgViewPrivate *priv;
1829
1830         g_return_val_if_fail (MODEST_IS_GTKHTML_MSG_VIEW (self), NULL);
1831         priv = MODEST_GTKHTML_MSG_VIEW_GET_PRIVATE (self);
1832
1833         return modest_attachments_view_get_selection (MODEST_ATTACHMENTS_VIEW (priv->attachments_view));
1834         
1835 }
1836
1837 static TnyList *
1838 get_attachments (ModestGtkhtmlMsgView *self)
1839 {
1840         ModestGtkhtmlMsgViewPrivate *priv;
1841
1842         g_return_val_if_fail (MODEST_IS_GTKHTML_MSG_VIEW (self), NULL);
1843         priv = MODEST_GTKHTML_MSG_VIEW_GET_PRIVATE (self);
1844
1845         return modest_attachments_view_get_attachments (MODEST_ATTACHMENTS_VIEW (priv->attachments_view));
1846         
1847 }
1848
1849 static void
1850 grab_focus (ModestGtkhtmlMsgView *self)
1851 {
1852         ModestGtkhtmlMsgViewPrivate *priv = NULL;
1853
1854         g_return_if_fail (MODEST_IS_GTKHTML_MSG_VIEW (self));
1855         priv = MODEST_GTKHTML_MSG_VIEW_GET_PRIVATE (self);
1856
1857         gtk_widget_grab_focus (priv->body_view);
1858 }
1859
1860 static void
1861 remove_attachment (ModestGtkhtmlMsgView *self, TnyMimePart *attachment)
1862 {
1863         ModestGtkhtmlMsgViewPrivate *priv;
1864
1865         g_return_if_fail (MODEST_IS_GTKHTML_MSG_VIEW (self));
1866         g_return_if_fail (TNY_IS_MIME_PART (attachment));
1867         priv = MODEST_GTKHTML_MSG_VIEW_GET_PRIVATE (self);
1868
1869         modest_attachments_view_remove_attachment (MODEST_ATTACHMENTS_VIEW (priv->attachments_view),
1870                                                    attachment);
1871         
1872 }
1873
1874 /* TNY MSG IMPLEMENTATION */
1875
1876 static void
1877 tny_msg_view_init (gpointer g, gpointer iface_data)
1878 {
1879         TnyMsgViewIface *klass = (TnyMsgViewIface *)g;
1880
1881         klass->get_msg = modest_msg_view_get_msg;
1882         klass->set_msg = modest_msg_view_set_msg;
1883         klass->set_unavailable = modest_msg_view_set_unavailable;
1884         klass->clear = modest_msg_view_clear;
1885         klass->create_mime_part_view_for = modest_msg_view_create_mime_part_view_for;
1886         klass->create_new_inline_viewer = modest_msg_view_create_new_inline_viewer;
1887
1888         return;
1889 }
1890
1891 static TnyMsg *
1892 modest_msg_view_get_msg (TnyMsgView *self)
1893 {
1894         return MODEST_GTKHTML_MSG_VIEW_GET_CLASS (self)->get_msg_func (self);
1895 }
1896
1897 static TnyMsg *
1898 modest_msg_view_get_msg_default (TnyMsgView *self)
1899 {
1900         return TNY_MSG (tny_mime_part_view_get_part (TNY_MIME_PART_VIEW (self)));
1901 }
1902
1903 static void
1904 modest_msg_view_set_msg (TnyMsgView *self, TnyMsg *msg)
1905 {
1906         MODEST_GTKHTML_MSG_VIEW_GET_CLASS (self)->set_msg_func (self, msg);
1907 }
1908
1909 static void 
1910 modest_msg_view_set_msg_default (TnyMsgView *self, TnyMsg *msg)
1911 {
1912
1913         tny_mime_part_view_set_part (TNY_MIME_PART_VIEW (self), TNY_MIME_PART (msg));
1914
1915         return;
1916 }
1917
1918 static void
1919 modest_msg_view_set_unavailable (TnyMsgView *self)
1920 {
1921         MODEST_GTKHTML_MSG_VIEW_GET_CLASS (self)->set_unavailable_func (self);
1922 }
1923
1924 static void
1925 modest_msg_view_set_unavailable_default (TnyMsgView *self)
1926 {
1927         tny_msg_view_clear (self);
1928
1929         return;
1930 }
1931
1932 static void
1933 modest_msg_view_clear (TnyMsgView *self)
1934 {
1935         MODEST_GTKHTML_MSG_VIEW_GET_CLASS (self)->clear_func (self);
1936 }
1937
1938 static void
1939 modest_msg_view_clear_default (TnyMsgView *self)
1940 {
1941         set_message (MODEST_GTKHTML_MSG_VIEW (self), NULL);
1942 }
1943
1944 static TnyMimePartView*
1945 modest_msg_view_create_mime_part_view_for (TnyMsgView *self, TnyMimePart *part)
1946 {
1947         return MODEST_GTKHTML_MSG_VIEW_GET_CLASS (self)->create_mime_part_view_for_func (self, part);
1948 }
1949
1950 static TnyMimePartView*
1951 modest_msg_view_create_mime_part_view_for_default (TnyMsgView *self, TnyMimePart *part)
1952 {
1953         g_warning ("modest_msg_view_create_mime_part_view_for_default is not implemented");
1954         return NULL;
1955 }
1956
1957 static TnyMsgView*
1958 modest_msg_view_create_new_inline_viewer (TnyMsgView *self)
1959 {
1960         return MODEST_GTKHTML_MSG_VIEW_GET_CLASS (self)->create_new_inline_viewer_func (self);
1961 }
1962
1963 static TnyMsgView*
1964 modest_msg_view_create_new_inline_viewer_default (TnyMsgView *self)
1965 {
1966         g_warning ("modest_msg_view_create_new_inline_viewer_default is not implemented");
1967
1968         return NULL;
1969 }
1970
1971 /* TNY MIME PART IMPLEMENTATION */
1972
1973 static void
1974 tny_mime_part_view_init (gpointer g, gpointer iface_data)
1975 {
1976         TnyMimePartViewIface *klass = (TnyMimePartViewIface *)g;
1977
1978         klass->get_part = modest_msg_view_mp_get_part;
1979         klass->set_part = modest_msg_view_mp_set_part;
1980         klass->clear = modest_msg_view_mp_clear;
1981
1982         return;
1983 }
1984
1985 static TnyMimePart* 
1986 modest_msg_view_mp_get_part (TnyMimePartView *self)
1987 {
1988         return MODEST_GTKHTML_MSG_VIEW_GET_CLASS (self)->get_part_func (self);
1989 }
1990
1991
1992 static TnyMimePart* 
1993 modest_msg_view_mp_get_part_default (TnyMimePartView *self)
1994 {
1995         return TNY_MIME_PART (get_message (MODEST_GTKHTML_MSG_VIEW (self)));
1996 }
1997
1998 static void
1999 modest_msg_view_mp_set_part (TnyMimePartView *self,
2000                              TnyMimePart *part)
2001 {
2002         MODEST_GTKHTML_MSG_VIEW_GET_CLASS (self)->set_part_func (self, part);
2003 }
2004
2005 static void
2006 modest_msg_view_mp_set_part_default (TnyMimePartView *self,
2007                                      TnyMimePart *part)
2008 {
2009         g_return_if_fail ((part == NULL) || TNY_IS_MSG (part));
2010
2011         set_message (MODEST_GTKHTML_MSG_VIEW (self), TNY_MSG (part));
2012 }
2013
2014 static void
2015 modest_msg_view_mp_clear (TnyMimePartView *self)
2016 {
2017         tny_msg_view_clear (TNY_MSG_VIEW (self));
2018 }
2019
2020 /* MODEST MIME PART VIEW IMPLEMENTATION */
2021
2022 static void
2023 modest_mime_part_view_init (gpointer g, gpointer iface_data)
2024 {
2025         ModestMimePartViewIface *klass = (ModestMimePartViewIface *)g;
2026
2027         klass->is_empty_func = modest_msg_view_mp_is_empty;
2028
2029         return;
2030 }
2031
2032 static gboolean
2033 modest_msg_view_mp_is_empty (ModestMimePartView *self)
2034 {
2035         return MODEST_GTKHTML_MSG_VIEW_GET_CLASS (self)->is_empty_func (self);
2036 }
2037
2038 static gboolean
2039 modest_msg_view_mp_is_empty_default (ModestMimePartView *self)
2040 {
2041         return is_empty (MODEST_GTKHTML_MSG_VIEW (self));
2042 }
2043
2044 /* MODEST ZOOMABLE IMPLEMENTATION */
2045 static void
2046 modest_zoomable_init (gpointer g, gpointer iface_data)
2047 {
2048         ModestZoomableIface *klass = (ModestZoomableIface *)g;
2049
2050         klass->get_zoom_func = modest_msg_view_get_zoom;
2051         klass->set_zoom_func = modest_msg_view_set_zoom;
2052         klass->zoom_minus_func = modest_msg_view_zoom_minus;
2053         klass->zoom_plus_func = modest_msg_view_zoom_plus;
2054
2055         return;
2056 }
2057
2058 static gdouble
2059 modest_msg_view_get_zoom (ModestZoomable *self)
2060 {
2061         return MODEST_GTKHTML_MSG_VIEW_GET_CLASS (self)->get_zoom_func (self);
2062 }
2063
2064 static gdouble
2065 modest_msg_view_get_zoom_default (ModestZoomable *self)
2066 {
2067         return get_zoom (MODEST_GTKHTML_MSG_VIEW (self));
2068 }
2069
2070 static void
2071 modest_msg_view_set_zoom (ModestZoomable *self, gdouble value)
2072 {
2073         MODEST_GTKHTML_MSG_VIEW_GET_CLASS (self)->set_zoom_func (self, value);
2074 }
2075
2076 static void
2077 modest_msg_view_set_zoom_default (ModestZoomable *self, gdouble value)
2078 {
2079         set_zoom (MODEST_GTKHTML_MSG_VIEW (self), value);
2080 }
2081
2082 static gboolean
2083 modest_msg_view_zoom_minus (ModestZoomable *self)
2084 {
2085         return MODEST_GTKHTML_MSG_VIEW_GET_CLASS (self)->zoom_minus_func (self);
2086 }
2087
2088 static gboolean
2089 modest_msg_view_zoom_minus_default (ModestZoomable *self)
2090 {
2091         /* operation not supported in ModestMsgView */
2092         return FALSE;
2093 }
2094
2095 static gboolean
2096 modest_msg_view_zoom_plus (ModestZoomable *self)
2097 {
2098         return MODEST_GTKHTML_MSG_VIEW_GET_CLASS (self)->zoom_plus_func (self);
2099 }
2100
2101 static gboolean
2102 modest_msg_view_zoom_plus_default (ModestZoomable *self)
2103 {
2104         /* operation not supported in ModestMsgView */
2105         return FALSE;
2106 }
2107
2108 /* MODEST ISEARCH VIEW IMPLEMENTATION */
2109 static void
2110 modest_isearch_view_init (gpointer g, gpointer iface_data)
2111 {
2112         ModestISearchViewIface *klass = (ModestISearchViewIface *)g;
2113
2114         klass->search_func = modest_msg_view_search;
2115         klass->search_next_func = modest_msg_view_search_next;
2116
2117         return;
2118 }
2119
2120 static gboolean
2121 modest_msg_view_search (ModestISearchView *self, const gchar *string)
2122 {
2123         return MODEST_GTKHTML_MSG_VIEW_GET_CLASS (self)->search_func (self, string);
2124 }
2125
2126 static gboolean
2127 modest_msg_view_search_default (ModestISearchView *self, const gchar *string)
2128 {
2129         return search (MODEST_GTKHTML_MSG_VIEW (self), string);
2130 }
2131
2132 static gboolean
2133 modest_msg_view_search_next (ModestISearchView *self)
2134 {
2135         return MODEST_GTKHTML_MSG_VIEW_GET_CLASS (self)->search_next_func (self);
2136 }
2137
2138 static gboolean
2139 modest_msg_view_search_next_default (ModestISearchView *self)
2140 {
2141         return search_next (MODEST_GTKHTML_MSG_VIEW (self));
2142 }
2143
2144 /* MODEST MSG VIEW IMPLEMENTATION */
2145 static void
2146 modest_msg_view_init (gpointer g, gpointer iface_data)
2147 {
2148         ModestMsgViewIface *klass = (ModestMsgViewIface *)g;
2149
2150         klass->get_vadjustment_func = modest_gtkhtml_msg_view_get_vadjustment;
2151         klass->get_hadjustment_func = modest_gtkhtml_msg_view_get_hadjustment;
2152         klass->set_vadjustment_func = modest_gtkhtml_msg_view_set_vadjustment;
2153         klass->set_hadjustment_func = modest_gtkhtml_msg_view_set_hadjustment;
2154         klass->set_shadow_type_func = modest_gtkhtml_msg_view_set_shadow_type;
2155         klass->get_shadow_type_func = modest_gtkhtml_msg_view_get_shadow_type;
2156         klass->get_priority_func = modest_gtkhtml_msg_view_get_priority;
2157         klass->set_priority_func = modest_gtkhtml_msg_view_set_priority;
2158         klass->get_selected_attachments_func = modest_gtkhtml_msg_view_get_selected_attachments;
2159         klass->get_attachments_func = modest_gtkhtml_msg_view_get_attachments;
2160         klass->grab_focus_func = modest_gtkhtml_msg_view_grab_focus;
2161         klass->remove_attachment_func = modest_gtkhtml_msg_view_remove_attachment;
2162
2163         return;
2164 }
2165
2166 static GtkAdjustment*
2167 modest_gtkhtml_msg_view_get_vadjustment (ModestMsgView *self)
2168 {
2169         return MODEST_GTKHTML_MSG_VIEW_GET_CLASS (self)->get_vadjustment_func (self);
2170 }
2171
2172 static GtkAdjustment*
2173 modest_gtkhtml_msg_view_get_vadjustment_default (ModestMsgView *self)
2174 {
2175         return get_vadjustment (MODEST_GTKHTML_MSG_VIEW (self));
2176 }
2177
2178 static GtkAdjustment*
2179 modest_gtkhtml_msg_view_get_hadjustment (ModestMsgView *self)
2180 {
2181         return MODEST_GTKHTML_MSG_VIEW_GET_CLASS (self)->get_hadjustment_func (self);
2182 }
2183
2184 static GtkAdjustment*
2185 modest_gtkhtml_msg_view_get_hadjustment_default (ModestMsgView *self)
2186 {
2187         return get_hadjustment (MODEST_GTKHTML_MSG_VIEW (self));
2188 }
2189
2190 static void
2191 modest_gtkhtml_msg_view_set_vadjustment (ModestMsgView *self, GtkAdjustment *adj)
2192 {
2193         MODEST_GTKHTML_MSG_VIEW_GET_CLASS (self)->set_vadjustment_func (self, adj);
2194 }
2195
2196 static void
2197 modest_gtkhtml_msg_view_set_vadjustment_default (ModestMsgView *self, GtkAdjustment *adj)
2198 {
2199         set_vadjustment (MODEST_GTKHTML_MSG_VIEW (self), adj);
2200 }
2201
2202 static void
2203 modest_gtkhtml_msg_view_set_hadjustment (ModestMsgView *self, GtkAdjustment *adj)
2204 {
2205         MODEST_GTKHTML_MSG_VIEW_GET_CLASS (self)->set_hadjustment_func (self, adj);
2206 }
2207
2208 static void
2209 modest_gtkhtml_msg_view_set_hadjustment_default (ModestMsgView *self, GtkAdjustment *adj)
2210 {
2211         set_hadjustment (MODEST_GTKHTML_MSG_VIEW (self), adj);
2212 }
2213
2214 static void
2215 modest_gtkhtml_msg_view_set_shadow_type (ModestMsgView *self, GtkShadowType type)
2216 {
2217         MODEST_GTKHTML_MSG_VIEW_GET_CLASS (self)->set_shadow_type_func (self, type);
2218 }
2219
2220 static void
2221 modest_gtkhtml_msg_view_set_shadow_type_default (ModestMsgView *self, GtkShadowType type)
2222 {
2223         set_shadow_type (MODEST_GTKHTML_MSG_VIEW (self), type);
2224 }
2225
2226 static GtkShadowType
2227 modest_gtkhtml_msg_view_get_shadow_type (ModestMsgView *self)
2228 {
2229         return MODEST_GTKHTML_MSG_VIEW_GET_CLASS (self)->get_shadow_type_func (self);
2230 }
2231
2232 static GtkShadowType
2233 modest_gtkhtml_msg_view_get_shadow_type_default (ModestMsgView *self)
2234 {
2235         return get_shadow_type (MODEST_GTKHTML_MSG_VIEW (self));
2236 }
2237
2238 static void
2239 modest_gtkhtml_msg_view_set_priority (ModestMsgView *self, TnyHeaderFlags flags)
2240 {
2241         MODEST_GTKHTML_MSG_VIEW_GET_CLASS (self)->set_priority_func (self, flags);
2242 }
2243
2244 static void
2245 modest_gtkhtml_msg_view_set_priority_default (ModestMsgView *self, TnyHeaderFlags flags)
2246 {
2247         set_priority (MODEST_GTKHTML_MSG_VIEW (self), flags);
2248 }
2249
2250 static TnyHeaderFlags
2251 modest_gtkhtml_msg_view_get_priority (ModestMsgView *self)
2252 {
2253         return MODEST_GTKHTML_MSG_VIEW_GET_CLASS (self)->get_priority_func (self);
2254 }
2255
2256 static TnyHeaderFlags
2257 modest_gtkhtml_msg_view_get_priority_default (ModestMsgView *self)
2258 {
2259         return get_priority (MODEST_GTKHTML_MSG_VIEW (self));
2260 }
2261
2262 static TnyList*
2263 modest_gtkhtml_msg_view_get_selected_attachments (ModestMsgView *self)
2264 {
2265         return MODEST_GTKHTML_MSG_VIEW_GET_CLASS (self)->get_selected_attachments_func (self);
2266 }
2267
2268 static TnyList*
2269 modest_gtkhtml_msg_view_get_selected_attachments_default (ModestMsgView *self)
2270 {
2271         return get_selected_attachments (MODEST_GTKHTML_MSG_VIEW (self));
2272 }
2273
2274 static TnyList*
2275 modest_gtkhtml_msg_view_get_attachments (ModestMsgView *self)
2276 {
2277         return MODEST_GTKHTML_MSG_VIEW_GET_CLASS (self)->get_attachments_func (self);
2278 }
2279
2280 static TnyList*
2281 modest_gtkhtml_msg_view_get_attachments_default (ModestMsgView *self)
2282 {
2283         return get_attachments (MODEST_GTKHTML_MSG_VIEW (self));
2284 }
2285
2286 static void
2287 modest_gtkhtml_msg_view_grab_focus (ModestMsgView *self)
2288 {
2289         MODEST_GTKHTML_MSG_VIEW_GET_CLASS (self)->grab_focus_func (self);
2290 }
2291
2292 static void
2293 modest_gtkhtml_msg_view_grab_focus_default (ModestMsgView *self)
2294 {
2295         grab_focus (MODEST_GTKHTML_MSG_VIEW (self));
2296 }
2297
2298 static void
2299 modest_gtkhtml_msg_view_remove_attachment (ModestMsgView *self, TnyMimePart *attachment)
2300 {
2301         MODEST_GTKHTML_MSG_VIEW_GET_CLASS (self)->remove_attachment_func (self, attachment);
2302 }
2303
2304 static void
2305 modest_gtkhtml_msg_view_remove_attachment_default (ModestMsgView *self, TnyMimePart *attachment)
2306 {
2307         remove_attachment (MODEST_GTKHTML_MSG_VIEW (self), attachment);
2308 }