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