Remove spacing in headers view calendar, as it was causing a very bad
[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 #endif
254         GtkWidget   *calendar_box;
255         GtkWidget   *calendar_icon;
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, 0);
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 #endif
1247         priv->calendar_actions_box = NULL;
1248         priv->calendar_icon = gtk_image_new ();
1249         gtk_image_set_from_icon_name (GTK_IMAGE (priv->calendar_icon), MODEST_HEADER_ICON_CALENDAR, GTK_ICON_SIZE_MENU);
1250         gtk_misc_set_alignment (GTK_MISC (priv->calendar_icon), 0.0, 0.5);
1251         if (priv->calendar_icon) {
1252                 priv->calendar_box = (GtkWidget *)
1253                         modest_mail_header_view_add_custom_header (MODEST_MAIL_HEADER_VIEW (priv->mail_header_view),
1254                                                                    _("mail_fi_invitation"),
1255                                                                    priv->calendar_icon,
1256                                                                    FALSE, FALSE);
1257                                                                    
1258                 gtk_widget_hide_all (priv->calendar_box);
1259         }
1260
1261         if (priv->attachments_view) {
1262 #ifndef MODEST_TOOLKIT_HILDON2
1263                 gchar *att_label = g_strconcat (_("mcen_me_viewer_attachments"), ":", NULL);
1264 #else
1265                 gchar *att_label = g_strconcat (_("mail_va_attachment"), ":", NULL);
1266 #endif
1267
1268                 priv->attachments_box = (GtkWidget *)
1269                         modest_mail_header_view_add_custom_header (MODEST_MAIL_HEADER_VIEW (priv->mail_header_view),
1270                                                                    att_label,
1271                                                                    priv->attachments_view,
1272                                                                    FALSE, FALSE);
1273                 gtk_widget_hide_all (priv->attachments_box);
1274                 g_free (att_label);
1275         }
1276
1277         priv->calendar_actions_container = gtk_vbox_new (FALSE, 0);
1278         gtk_box_pack_start (GTK_BOX (priv->headers_box), priv->calendar_actions_container, FALSE, FALSE, 0);
1279         gtk_widget_hide (priv->calendar_actions_container);
1280         gtk_widget_hide_all (priv->calendar_actions_container);
1281         gtk_widget_set_no_show_all (priv->calendar_actions_container, TRUE);
1282
1283 #ifndef MODEST_TOOLKIT_HILDON2
1284         separator = gtk_hseparator_new ();
1285         gtk_box_pack_start (GTK_BOX(priv->headers_box), separator, FALSE, FALSE, 0);
1286 #endif
1287
1288         gtk_widget_set_parent (priv->headers_box, GTK_WIDGET (obj));
1289
1290         if (priv->body_view) {
1291                 gtk_container_add (GTK_CONTAINER (priv->html_scroll), priv->body_view);
1292                 gtk_widget_set_parent (priv->html_scroll, GTK_WIDGET(obj));
1293 #ifdef MAEMO_CHANGES
1294                 gtk_widget_tap_and_hold_setup (GTK_WIDGET (priv->body_view), NULL, NULL, 0);
1295                 g_signal_connect (G_OBJECT (priv->body_view), "tap-and-hold", G_CALLBACK (on_tap_and_hold), obj);
1296                 g_signal_connect (G_OBJECT (priv->body_view), "tap-and-hold-query", G_CALLBACK (on_tap_and_hold_query), obj);
1297 #endif
1298         }
1299         
1300 }
1301         
1302
1303 static void
1304 modest_gtkhtml_msg_view_finalize (GObject *obj)
1305 {       
1306         ModestGtkhtmlMsgViewPrivate *priv;
1307         priv = MODEST_GTKHTML_MSG_VIEW_GET_PRIVATE (obj);
1308
1309         if (priv->msg) {
1310                 g_object_unref (G_OBJECT(priv->msg));
1311                 priv->msg = NULL;
1312         }
1313
1314         if (priv->idle_resize_children_id > 0) {
1315                 g_source_remove (priv->idle_resize_children_id);
1316                 priv->idle_resize_children_id = 0;
1317         }
1318
1319         if (priv->idle_readjust_scroll_id > 0) {
1320                 g_source_remove (priv->idle_readjust_scroll_id);
1321                 priv->idle_readjust_scroll_id = 0;
1322         }
1323
1324         if (priv->idle_motion_id > 0) {
1325                 g_source_remove (priv->idle_motion_id);
1326                 priv->idle_motion_id = 0;
1327         }
1328         
1329         /* we cannot disconnect sigs, because priv->body_view is
1330          * already dead */
1331         
1332         disconnect_vadjustment (MODEST_GTKHTML_MSG_VIEW(obj));
1333         disconnect_hadjustment (MODEST_GTKHTML_MSG_VIEW(obj));
1334
1335         priv->body_view = NULL;
1336         priv->attachments_view = NULL;
1337
1338         G_OBJECT_CLASS(parent_class)->finalize (obj);           
1339 }
1340
1341 static void
1342 modest_gtkhtml_msg_view_destroy (GtkObject *obj)
1343 {       
1344         disconnect_vadjustment (MODEST_GTKHTML_MSG_VIEW(obj));
1345         disconnect_hadjustment (MODEST_GTKHTML_MSG_VIEW(obj));
1346
1347         GTK_OBJECT_CLASS(parent_class)->destroy (obj);          
1348 }
1349
1350 /* INTERNAL METHODS */
1351
1352 #ifdef MAEMO_CHANGES
1353 static gboolean 
1354 motion_notify_event (GtkWidget *widget,
1355                      GdkEventMotion *event,
1356                      gpointer userdata)
1357 {
1358         ModestGtkhtmlMsgViewPrivate *priv = MODEST_GTKHTML_MSG_VIEW_GET_PRIVATE (userdata);
1359
1360         /* Use panning information in gtkhtml widget to support also vertical panning */
1361
1362         if (GTK_HTML (widget)->panning) {
1363                 gint y, dy;
1364                 gdouble value;
1365
1366                 gdk_window_get_pointer (GTK_LAYOUT (widget)->bin_window, NULL, &y, NULL);
1367                 dy = y - GTK_HTML (widget)->lasty;
1368                 value = priv->vadj->value - (gdouble) dy;
1369
1370                 if (value < priv->vadj->lower)
1371                         value = priv->vadj->lower;
1372                 else if (value > priv->vadj->upper - priv->vadj->page_size)
1373                         value = priv->vadj->upper - priv->vadj->page_size;
1374                 gtk_adjustment_set_value (priv->vadj, value);
1375                 
1376         } 
1377         return FALSE;
1378 }
1379 #endif
1380
1381 static gboolean
1382 idle_motion (gpointer userdata)
1383 {
1384         ModestGtkhtmlMsgViewPrivate *priv = MODEST_GTKHTML_MSG_VIEW_GET_PRIVATE (userdata);
1385         if (GTK_HTML (priv->body_view)->in_selection_drag) {
1386                 gdouble offset;
1387                 GtkAdjustment *adj;
1388                 gint gdk_y;
1389                 gdk_window_get_pointer (gtk_widget_get_parent_window (priv->body_view), NULL, &gdk_y, NULL);
1390                 offset= (gdouble) (priv->headers_box->requisition.height + gdk_y);
1391                 adj = GTK_ADJUSTMENT (priv->vadj);
1392                 if (offset < adj->value + adj->step_increment) {
1393                         gtk_adjustment_set_value (adj, MAX (offset + adj->page_increment - adj->page_size, 0.0));
1394                 } else if (offset > adj->value + adj->page_increment) {
1395                         gtk_adjustment_set_value (adj, MIN (offset - adj->page_increment, adj->upper - adj->page_size));
1396                 }
1397                 gtk_widget_queue_resize (userdata);
1398         }
1399         return TRUE;
1400 }
1401
1402 static gboolean 
1403 button_press_event (GtkWidget *widget,
1404                     GdkEventButton *event,
1405                     gpointer userdata)
1406 {
1407         ModestGtkhtmlMsgViewPrivate *priv = MODEST_GTKHTML_MSG_VIEW_GET_PRIVATE (userdata);
1408
1409         if (priv->idle_motion_id == 0) {
1410                 priv->idle_motion_id = g_timeout_add (200, idle_motion, userdata);
1411         }
1412         return FALSE;
1413 }
1414
1415 static gboolean 
1416 button_release_event (GtkWidget *widget,
1417                       GdkEventButton *event,
1418                       gpointer userdata)
1419 {
1420         ModestGtkhtmlMsgViewPrivate *priv = MODEST_GTKHTML_MSG_VIEW_GET_PRIVATE (userdata);
1421
1422         if (priv->idle_motion_id > 0) {
1423                 gint gdk_y;
1424                 g_source_remove (priv->idle_motion_id);
1425                 
1426                 priv->idle_motion_id = 0;;
1427                 gdk_window_get_pointer (gtk_widget_get_parent_window (priv->body_view), NULL, &gdk_y, NULL);
1428                 event->y = (gdouble) gdk_y;
1429         }
1430         return FALSE;
1431 }
1432
1433 static GtkAdjustment *
1434 get_vadjustment (ModestGtkhtmlMsgView *self)
1435 {
1436         ModestGtkhtmlMsgViewPrivate *priv = MODEST_GTKHTML_MSG_VIEW_GET_PRIVATE (self);
1437
1438         if (!priv->vadj)
1439                 set_vadjustment (self, NULL);
1440
1441         return priv->vadj;
1442         
1443 }
1444
1445 static GtkAdjustment *
1446 get_hadjustment (ModestGtkhtmlMsgView *self)
1447 {
1448         ModestGtkhtmlMsgViewPrivate *priv = MODEST_GTKHTML_MSG_VIEW_GET_PRIVATE (self);
1449
1450         if (!priv->hadj)
1451                 set_hadjustment (self, NULL);
1452
1453         return priv->hadj;
1454         
1455 }
1456
1457 static void
1458 set_hadjustment (ModestGtkhtmlMsgView *self, GtkAdjustment *hadj)
1459 {
1460         ModestGtkhtmlMsgViewPrivate *priv = MODEST_GTKHTML_MSG_VIEW_GET_PRIVATE (self);
1461         gboolean value_changed;
1462         
1463         if (hadj && hadj == priv->hadj)
1464                 return;
1465
1466         if (!hadj)
1467                 hadj = GTK_ADJUSTMENT (gtk_adjustment_new (0.0,0.0,0.0,0.0,0.0,0.0));
1468         disconnect_hadjustment (self);
1469         g_object_ref_sink (G_OBJECT (hadj));
1470         priv->hadj = hadj;
1471         set_hadjustment_values (self, &value_changed);
1472
1473         g_signal_connect (hadj, "value_changed", G_CALLBACK (adjustment_value_changed),
1474                           self);
1475
1476         gtk_adjustment_changed (hadj);
1477         if (value_changed)
1478                 gtk_adjustment_value_changed (hadj);
1479         else
1480                 adjustment_value_changed (hadj, self);
1481
1482         g_object_notify (G_OBJECT (self), "hadjustment");
1483 }
1484
1485 static void
1486 set_vadjustment (ModestGtkhtmlMsgView *self, GtkAdjustment *vadj)
1487 {
1488         ModestGtkhtmlMsgViewPrivate *priv = MODEST_GTKHTML_MSG_VIEW_GET_PRIVATE (self);
1489         gboolean value_changed;
1490
1491         if (vadj && vadj == priv->vadj)
1492                 return;
1493
1494         if (!vadj)
1495                 vadj = (GtkAdjustment *) gtk_adjustment_new (0.0,0.0,0.0,0.0,0.0,0.0);
1496         disconnect_vadjustment (self);
1497         g_object_ref_sink (G_OBJECT (vadj));
1498         priv->vadj = vadj;
1499         set_vadjustment_values (self, &value_changed);
1500
1501         g_signal_connect (vadj, "value_changed", G_CALLBACK (adjustment_value_changed),
1502                           self);
1503
1504         gtk_adjustment_changed (vadj);
1505         if (value_changed)
1506                 gtk_adjustment_value_changed (vadj);
1507         else
1508                 adjustment_value_changed (vadj, self);
1509
1510         g_object_notify (G_OBJECT (self), "vadjustment");
1511 }
1512
1513 static void
1514 set_shadow_type (ModestGtkhtmlMsgView *self,
1515                  GtkShadowType shadow_type)
1516 {
1517         ModestGtkhtmlMsgViewPrivate *priv;
1518         g_return_if_fail (MODEST_IS_GTKHTML_MSG_VIEW (self));
1519
1520         priv = MODEST_GTKHTML_MSG_VIEW_GET_PRIVATE (self);
1521         
1522         if (priv->shadow_type != shadow_type) {
1523                 priv->shadow_type = shadow_type;
1524                 
1525                 if (GTK_WIDGET_VISIBLE (self)) {
1526                         gtk_widget_size_allocate (GTK_WIDGET (self), &(GTK_WIDGET (self)->allocation));
1527                         gtk_widget_queue_draw (GTK_WIDGET (self));
1528                 }
1529                 g_object_notify (G_OBJECT (self), "shadow-type");
1530         }
1531 }
1532
1533 static GtkShadowType
1534 get_shadow_type (ModestGtkhtmlMsgView *self)
1535 {
1536         ModestGtkhtmlMsgViewPrivate *priv;
1537         g_return_val_if_fail (MODEST_IS_GTKHTML_MSG_VIEW (self), GTK_SHADOW_NONE);
1538         priv = MODEST_GTKHTML_MSG_VIEW_GET_PRIVATE (self);
1539         
1540         return priv->shadow_type;
1541 }
1542
1543 GtkWidget*
1544 modest_msg_view_new (TnyMsg *msg)
1545 {
1546         GObject *obj;
1547         ModestGtkhtmlMsgView* self;
1548         
1549         obj  = G_OBJECT(g_object_new(MODEST_TYPE_GTKHTML_MSG_VIEW, NULL));
1550         self = MODEST_GTKHTML_MSG_VIEW(obj);
1551         tny_msg_view_set_msg (TNY_MSG_VIEW (self), msg);
1552
1553         return GTK_WIDGET(self);
1554 }
1555
1556 #ifdef MAEMO_CHANGES
1557 static void
1558 on_tap_and_hold (GtkWidget *widget,
1559                  gpointer data)
1560 {
1561         ModestGtkhtmlMsgView *self = (ModestGtkhtmlMsgView *) data;
1562         ModestGtkhtmlMsgViewPrivate *priv = MODEST_GTKHTML_MSG_VIEW_GET_PRIVATE (self);
1563
1564         g_signal_emit_by_name (G_OBJECT (self), "link-contextual", priv->last_url);
1565 }
1566
1567 static gboolean
1568 on_tap_and_hold_query (GtkWidget *widget,
1569                        GdkEvent *event,
1570                        gpointer data)
1571 {
1572         ModestGtkhtmlMsgView *self = (ModestGtkhtmlMsgView *) data;
1573         ModestGtkhtmlMsgViewPrivate *priv = MODEST_GTKHTML_MSG_VIEW_GET_PRIVATE (self);
1574
1575         /* Don't show the tap and hold animation if no url below */
1576         return (priv->last_url == NULL);
1577 }
1578 #endif
1579
1580 static void
1581 on_recpt_activated (ModestMailHeaderView *header_view, 
1582                     const gchar *address,
1583                     ModestGtkhtmlMsgView *self)
1584 {
1585         g_signal_emit_by_name (G_OBJECT (self), "recpt-activated", address);
1586 }
1587
1588 static void
1589 on_show_details (ModestMailHeaderView *header_view, 
1590                  ModestGtkhtmlMsgView *self)
1591 {
1592         g_signal_emit_by_name (G_OBJECT (self), "show-details");
1593 }
1594
1595 static void
1596 on_attachment_activated (ModestAttachmentsView * att_view, TnyMimePart *mime_part, gpointer self)
1597 {
1598
1599         g_signal_emit_by_name (G_OBJECT(self), "attachment_clicked", mime_part);
1600 }
1601
1602 static void
1603 request_fetch_images (ModestGtkhtmlMsgView *self)
1604 {
1605         ModestGtkhtmlMsgViewPrivate *priv = MODEST_GTKHTML_MSG_VIEW_GET_PRIVATE (self);
1606         TnyMimePart *part;
1607
1608         /* The message could have not been downloaded yet */
1609         if (priv->msg) {
1610                 modest_mime_part_view_set_view_images (MODEST_MIME_PART_VIEW (priv->body_view), TRUE);
1611                 priv->has_blocked_bs_images = FALSE;
1612                 part = tny_mime_part_view_get_part (TNY_MIME_PART_VIEW (priv->body_view));
1613                 if (part) {
1614                         tny_mime_part_view_set_part (TNY_MIME_PART_VIEW (priv->body_view), part);
1615                         g_object_unref (part);
1616                 }
1617                 tny_msg_set_allow_external_images (TNY_MSG (priv->msg), TRUE);
1618         }
1619 }
1620
1621 static void
1622 set_branding (ModestGtkhtmlMsgView *self, const gchar *brand_name, const GdkPixbuf *brand_icon)
1623 {
1624         ModestGtkhtmlMsgViewPrivate *priv = MODEST_GTKHTML_MSG_VIEW_GET_PRIVATE (self);
1625
1626         modest_mail_header_view_set_branding (MODEST_MAIL_HEADER_VIEW (priv->mail_header_view), brand_name, brand_icon);
1627 }
1628
1629 static gboolean
1630 has_blocked_external_images (ModestGtkhtmlMsgView *self)
1631 {
1632         ModestGtkhtmlMsgViewPrivate *priv = MODEST_GTKHTML_MSG_VIEW_GET_PRIVATE (self);
1633
1634         return ((modest_mime_part_view_has_external_images (MODEST_MIME_PART_VIEW (priv->body_view)) ||
1635                  (priv->has_blocked_bs_images)) &&
1636                 !modest_mime_part_view_get_view_images (MODEST_MIME_PART_VIEW (priv->body_view)));
1637 }
1638
1639 static gboolean
1640 on_activate_link (GtkWidget *widget, const gchar *uri, ModestGtkhtmlMsgView *self)
1641 {
1642         gboolean result;
1643         g_return_val_if_fail (self, FALSE);
1644
1645         g_signal_emit_by_name (G_OBJECT(self), "activate-link", uri, &result);
1646
1647         return result;
1648 }
1649
1650
1651 static gboolean
1652 on_link_hover (GtkWidget *widget, const gchar *uri, ModestGtkhtmlMsgView *self)
1653 {
1654         ModestGtkhtmlMsgViewPrivate *priv = MODEST_GTKHTML_MSG_VIEW_GET_PRIVATE (self);
1655         gboolean result;
1656
1657         g_free (priv->last_url);
1658         priv->last_url = g_strdup (uri);
1659
1660         g_signal_emit_by_name (G_OBJECT(self), "link-hover", uri, &result);
1661
1662         return result;
1663 }
1664
1665 static void 
1666 on_limit_error (GtkWidget *widget, ModestGtkhtmlMsgView *msg_view)
1667 {
1668         g_signal_emit_by_name (G_OBJECT (msg_view), "limit-error");
1669 }
1670
1671 static gboolean
1672 part_cids_equal (const gchar *part_cid1,
1673                  const gchar *part_cid2)
1674 {
1675         if (g_strcmp0 (part_cid1, part_cid2) == 0)
1676                 return TRUE;
1677
1678         if (part_cid2 && part_cid2[0] == '<') {
1679                 const gchar *end;
1680                 end = g_strrstr_len (part_cid2, -1, ">");
1681
1682                 if (end && strncmp (part_cid2 + 1, part_cid1, end - part_cid2 - 1) == 0)
1683                         return TRUE;
1684         }
1685         return FALSE;
1686 }
1687
1688 static TnyMimePart *
1689 find_cid_image (TnyMsg *msg, const gchar *cid)
1690 {
1691         TnyMimePart *part = NULL;
1692         TnyList *parts;
1693         TnyIterator *iter;
1694         
1695         g_return_val_if_fail (msg, NULL);
1696         g_return_val_if_fail (cid, NULL);
1697         
1698         parts  = TNY_LIST (tny_simple_list_new());
1699
1700         tny_mime_part_get_parts (TNY_MIME_PART (msg), parts); 
1701         iter   = tny_list_create_iterator (parts);
1702         
1703         while (!tny_iterator_is_done(iter)) {
1704                 const gchar *part_cid;
1705
1706                 part = TNY_MIME_PART(tny_iterator_get_current(iter));
1707                 part_cid = tny_mime_part_get_content_id (part);
1708
1709                 /* if there is no content id, try the content location;
1710                  * this is what Outlook seems to use when it converts
1711                  * it's internal richtext to html
1712                  */
1713                 if (!part_cid)
1714                         part_cid = tny_mime_part_get_content_location (part);
1715
1716                 if (part_cids_equal (cid, part_cid))
1717                         break;
1718
1719                 if (tny_mime_part_content_type_is (part, "multipart/related")) {
1720                         TnyList *related_parts = TNY_LIST (tny_simple_list_new ());
1721                         TnyIterator *related_iter = NULL;
1722                         TnyMimePart *related_part = NULL;
1723
1724                         tny_mime_part_get_parts (part, related_parts);
1725                         related_iter = tny_list_create_iterator (related_parts);
1726
1727                         while (!tny_iterator_is_done (related_iter)) {
1728                                 related_part = TNY_MIME_PART (tny_iterator_get_current (related_iter));
1729                                 part_cid = tny_mime_part_get_content_id (related_part);
1730
1731                                 if (part_cids_equal (cid, part_cid))
1732                                         break;
1733
1734                                 g_object_unref (related_part);
1735                                 related_part = NULL;
1736                                 tny_iterator_next (related_iter);
1737                         }
1738
1739                         g_object_unref (related_iter);
1740                         g_object_unref (related_parts);
1741                         if (related_part != NULL) {
1742                                 g_object_unref (part);
1743                                 part = related_part;
1744                                 break;
1745                         }
1746                 }
1747
1748                 g_object_unref (G_OBJECT(part));
1749         
1750                 part = NULL;
1751                 tny_iterator_next (iter);
1752         }
1753         
1754         g_object_unref (G_OBJECT(iter));        
1755         g_object_unref (G_OBJECT(parts));
1756         
1757         return part;
1758 }
1759
1760 static void
1761 fetch_url_decode_to_stream_cb (TnyMimePart *self, gboolean cancelled, TnyStream *stream, GError *err, gpointer user_data)
1762 {
1763         tny_stream_close (stream);
1764 }
1765
1766 static gboolean
1767 on_fetch_url (GtkWidget *widget, const gchar *uri,
1768               TnyStream *stream, ModestGtkhtmlMsgView *self)
1769 {
1770         ModestGtkhtmlMsgViewPrivate *priv;
1771         const gchar* my_uri;
1772         TnyMimePart *part = NULL;
1773         
1774
1775
1776         priv = MODEST_GTKHTML_MSG_VIEW_GET_PRIVATE (self);
1777
1778         /*
1779          * we search for either something starting with cid:, or something
1780          * with no prefix at all; this latter case occurs when sending mails
1781          * with MS Outlook in rich-text mode, and 'attach-as-object
1782          */
1783         if (g_str_has_prefix (uri, "cid:"))  
1784                 my_uri = uri + 4;  /* +4 ==> skip "cid:" */
1785         else
1786                 my_uri = uri;
1787         
1788         /* now try to find the embedded image */
1789         part = find_cid_image (priv->msg, my_uri);
1790
1791         if (!part) {
1792                 if (g_str_has_prefix (uri, "http:")) {
1793                         if (modest_mime_part_view_get_view_images (MODEST_MIME_PART_VIEW (priv->body_view))) {
1794                                 gboolean result = FALSE;
1795                                 g_signal_emit_by_name (self, "fetch-image", uri, stream, &result);
1796                                 return result;
1797                         } else {
1798                                 /* we return immediately to get a "image not found" icon */
1799                                 tny_stream_close (stream);
1800                                 return TRUE;
1801                         }
1802                 } else {
1803                         return FALSE;
1804                 }
1805         } else if (TNY_IS_CAMEL_BS_MIME_PART (part) && 
1806                    !tny_camel_bs_mime_part_is_fetched (TNY_CAMEL_BS_MIME_PART (part))){
1807           if (!modest_mime_part_view_get_view_images (MODEST_MIME_PART_VIEW (priv->body_view)) || 
1808               !tny_device_is_online (modest_runtime_get_device ())) {
1809                         priv->has_blocked_bs_images = TRUE;
1810                         tny_stream_close (stream);
1811                         return TRUE;
1812                 }
1813         }
1814
1815         tny_mime_part_decode_to_stream_async ((TnyMimePart*)part, stream, fetch_url_decode_to_stream_cb, NULL, NULL);
1816         g_object_unref (G_OBJECT(part));
1817         return TRUE;
1818 }
1819
1820 static void
1821 set_message (ModestGtkhtmlMsgView *self, TnyMsg *msg, TnyMimePart *other_body)
1822 {
1823         TnyMimePart *body;
1824         ModestGtkhtmlMsgViewPrivate *priv;
1825         TnyHeader *header;
1826         GtkAdjustment *html_vadj, *html_hadj;
1827
1828         g_return_if_fail (self);
1829
1830         priv = MODEST_GTKHTML_MSG_VIEW_GET_PRIVATE(self);
1831         priv->has_blocked_bs_images = FALSE;
1832         modest_mail_header_view_set_loading (MODEST_MAIL_HEADER_VIEW (priv->mail_header_view), FALSE);
1833         gtk_widget_set_no_show_all (priv->mail_header_view, FALSE);
1834         modest_mime_part_view_set_view_images (MODEST_MIME_PART_VIEW (priv->body_view), FALSE);
1835
1836         if (msg != priv->msg) {
1837                 if (priv->msg)
1838                         g_object_unref (G_OBJECT(priv->msg));
1839                 if (msg)
1840                         g_object_ref   (G_OBJECT(msg));
1841                 priv->msg = msg;
1842         }
1843
1844         if (!msg) {
1845                 tny_header_view_clear (TNY_HEADER_VIEW (priv->mail_header_view));
1846                 modest_attachments_view_set_message (MODEST_ATTACHMENTS_VIEW (priv->attachments_view), NULL);
1847                 gtk_widget_hide_all (priv->mail_header_view);
1848                 gtk_widget_hide_all (priv->attachments_box);
1849 #ifdef MODEST_TOOKIT_HILDON2
1850                 gtk_widget_hide_all (priv->priority_box);
1851 #endif
1852                 gtk_widget_hide_all (priv->calendar_box);
1853                 gtk_widget_hide (priv->calendar_actions_container);
1854                 gtk_widget_hide_all (priv->calendar_actions_container);
1855                 gtk_widget_set_no_show_all (priv->mail_header_view, TRUE);
1856                 tny_mime_part_view_clear (TNY_MIME_PART_VIEW (priv->body_view));
1857
1858                 html_vadj = gtk_scrolled_window_get_vadjustment (GTK_SCROLLED_WINDOW (priv->html_scroll));
1859                 html_vadj->upper = 0;
1860                 html_vadj->page_size = 0;
1861                 g_signal_emit_by_name (G_OBJECT (html_vadj), "changed");
1862
1863                 html_hadj = gtk_scrolled_window_get_hadjustment (GTK_SCROLLED_WINDOW (priv->html_scroll));
1864                 html_hadj->upper = 0;
1865                 html_hadj->page_size = 0;
1866                 g_signal_emit_by_name (G_OBJECT (html_hadj), "changed");
1867
1868                 gtk_widget_set_size_request (GTK_WIDGET (priv->body_view), 1, 1);
1869                 gtk_widget_set_size_request (GTK_WIDGET (priv->body_view), -1, -1);
1870
1871                 priv->idle_changes_count = 0;
1872                 gtk_widget_queue_resize (GTK_WIDGET (priv->body_view));
1873
1874                 gtk_widget_queue_resize (GTK_WIDGET(self));
1875                 gtk_widget_queue_draw (GTK_WIDGET(self));
1876                 return;
1877         }
1878
1879         header = tny_msg_get_header (msg);
1880         tny_header_view_set_header (TNY_HEADER_VIEW (priv->mail_header_view), header);
1881         g_object_unref (header);
1882
1883         modest_attachments_view_set_message (MODEST_ATTACHMENTS_VIEW (priv->attachments_view),
1884                                              other_body?NULL:msg);
1885
1886         modest_mime_part_view_set_view_images (MODEST_MIME_PART_VIEW (priv->body_view), tny_msg_get_allow_external_images (msg));
1887
1888         if (other_body) {
1889                 body = other_body;
1890                 g_object_ref (body);
1891         } else {
1892                 body = modest_tny_msg_find_body_part (msg, TRUE);
1893         }
1894         if (body) {
1895                 ModestAttachmentsView *widget;
1896
1897                 tny_mime_part_view_set_part (TNY_MIME_PART_VIEW (priv->body_view), body);
1898                 g_object_unref (body);
1899                 widget = MODEST_ATTACHMENTS_VIEW (priv->attachments_view);
1900
1901                 if (modest_attachments_view_has_attachments (widget)) {
1902                         GtkLabel *label;
1903                         GList *children = NULL;
1904                         gchar *text = NULL;
1905                         /* Ugly but... */
1906                         children = gtk_container_get_children (GTK_CONTAINER (priv->attachments_box));
1907                         label = GTK_LABEL (children->data);
1908                         gtk_widget_show_all (priv->attachments_box);
1909 #ifdef MODEST_TOOLKIT_HILDON2
1910                         if (modest_attachments_view_get_num_attachments (widget) > 1) {
1911                                 text = _("mail_va_attachments");
1912                         } else {
1913                                 text = _("mail_va_attachment");
1914                         }
1915 #else
1916                                 text = _("mail_va_attachment");
1917 #endif
1918                         gtk_label_set_text (label, text);
1919                 } else {
1920                         gtk_widget_hide_all (priv->attachments_box);
1921                 }
1922
1923         } else {
1924                 tny_mime_part_view_clear (TNY_MIME_PART_VIEW (priv->body_view));
1925         }
1926
1927         /* Refresh priority */
1928         set_priority (self, tny_header_get_flags (header));
1929         set_calendar (self, header, msg);
1930
1931         gtk_widget_show (priv->body_view);
1932 #ifdef MODEST_TOOLKIT_HILDON2
1933         gtk_widget_set_no_show_all (priv->priority_box, TRUE);
1934 #endif
1935         gtk_widget_set_no_show_all (priv->calendar_box, TRUE);
1936         gtk_widget_set_no_show_all (priv->calendar_actions_container, TRUE);
1937         gtk_widget_set_no_show_all (priv->attachments_box, TRUE);
1938         gtk_widget_show_all (priv->mail_header_view);
1939         gtk_widget_set_no_show_all (priv->attachments_box, FALSE);
1940 #ifdef MODEST_TOOLKIT_HILDON2
1941         gtk_widget_set_no_show_all (priv->priority_box, FALSE);
1942 #endif
1943         gtk_widget_set_no_show_all (priv->calendar_box, FALSE);
1944         gtk_widget_set_no_show_all (priv->calendar_actions_container, FALSE);
1945         gtk_widget_set_no_show_all (priv->mail_header_view, TRUE);
1946
1947         html_vadj = gtk_scrolled_window_get_vadjustment (GTK_SCROLLED_WINDOW (priv->html_scroll));
1948         html_vadj->upper = 0;
1949         html_vadj->page_size = 0;
1950         g_signal_emit_by_name (G_OBJECT (html_vadj), "changed");
1951
1952         html_hadj = gtk_scrolled_window_get_hadjustment (GTK_SCROLLED_WINDOW (priv->html_scroll));
1953         html_hadj->upper = 0;
1954         html_hadj->page_size = 0;
1955         g_signal_emit_by_name (G_OBJECT (html_hadj), "changed");
1956
1957         gtk_widget_set_size_request (GTK_WIDGET (priv->body_view), 1, 1);
1958         gtk_widget_set_size_request (GTK_WIDGET (priv->body_view), -1, -1);
1959
1960         priv->idle_changes_count = 0;
1961         gtk_widget_queue_resize (GTK_WIDGET (priv->body_view));
1962
1963         gtk_widget_queue_resize (GTK_WIDGET(self));
1964         gtk_widget_queue_draw (GTK_WIDGET(self));
1965
1966         if (priv->hadj != NULL)
1967                 priv->hadj->value = 0.0;
1968         if (priv->vadj != NULL)
1969                 priv->vadj->value = 0.0;
1970
1971         g_signal_emit_by_name (G_OBJECT (html_vadj), "changed");
1972
1973         /* This is a hack to force reallocation of scroll after drawing all the stuff. This
1974          * makes the html view get the proper and expected size and prevent being able to scroll
1975          * the buffer when it shouldn't be scrollable */
1976         if (priv->idle_readjust_scroll_id == 0) {
1977                 g_object_ref (self);
1978                 priv->idle_readjust_scroll_id = g_idle_add ((GSourceFunc) idle_readjust_scroll, self);
1979         }
1980 }
1981
1982 static void
1983 set_header (ModestGtkhtmlMsgView *self, TnyHeader *header)
1984 {
1985         ModestGtkhtmlMsgViewPrivate *priv;
1986         GtkAdjustment *html_vadj, *html_hadj;
1987         
1988         g_return_if_fail (self);
1989
1990         if (header == NULL) {
1991                 set_message (self, NULL, NULL);
1992                 return;
1993         }
1994         
1995         priv = MODEST_GTKHTML_MSG_VIEW_GET_PRIVATE(self);
1996         priv->has_blocked_bs_images = FALSE;
1997         modest_mail_header_view_set_loading (MODEST_MAIL_HEADER_VIEW (priv->mail_header_view), TRUE);
1998         gtk_widget_set_no_show_all (priv->mail_header_view, FALSE);
1999         modest_mime_part_view_set_view_images (MODEST_MIME_PART_VIEW (priv->body_view), FALSE);
2000
2001         html_vadj = gtk_scrolled_window_get_vadjustment (GTK_SCROLLED_WINDOW (priv->html_scroll));
2002         html_vadj->upper = 0;
2003         html_vadj->page_size = 0;
2004         g_signal_emit_by_name (G_OBJECT (html_vadj), "changed");
2005
2006         html_hadj = gtk_scrolled_window_get_hadjustment (GTK_SCROLLED_WINDOW (priv->html_scroll));
2007         html_hadj->upper = 0;
2008         html_hadj->page_size = 0;
2009         g_signal_emit_by_name (G_OBJECT (html_hadj), "changed");
2010
2011         priv->idle_changes_count = 0;
2012         gtk_widget_set_size_request (GTK_WIDGET (priv->body_view), 1, 1);
2013         gtk_widget_set_size_request (GTK_WIDGET (priv->body_view), -1, -1);
2014         gtk_widget_queue_resize (GTK_WIDGET (priv->body_view));
2015
2016         if (priv->msg) {
2017                 g_object_unref (G_OBJECT(priv->msg));
2018         }
2019         priv->msg = NULL;
2020         
2021         tny_header_view_set_header (TNY_HEADER_VIEW (priv->mail_header_view), header);
2022         modest_attachments_view_set_message (MODEST_ATTACHMENTS_VIEW (priv->attachments_view), NULL);
2023         gtk_widget_show_all (priv->mail_header_view);
2024         gtk_widget_hide_all (priv->attachments_box);
2025 #ifdef MODEST_TOOLKIT_HILDON2
2026         gtk_widget_hide_all (priv->priority_box);
2027 #endif
2028         gtk_widget_hide_all (priv->calendar_box);
2029         gtk_widget_hide (priv->calendar_actions_container);
2030         gtk_widget_hide_all (priv->calendar_actions_container);
2031         gtk_widget_set_no_show_all (priv->mail_header_view, TRUE);
2032         tny_mime_part_view_clear (TNY_MIME_PART_VIEW (priv->body_view));
2033         priv->idle_changes_count = 0;
2034         gtk_widget_queue_resize (GTK_WIDGET(self));
2035         gtk_widget_queue_draw (GTK_WIDGET(self));
2036 }
2037
2038
2039 static TnyMsg*
2040 get_message (ModestGtkhtmlMsgView *self)
2041 {
2042         TnyMsg *msg;
2043
2044         g_return_val_if_fail (MODEST_IS_GTKHTML_MSG_VIEW (self), NULL);
2045
2046         msg = MODEST_GTKHTML_MSG_VIEW_GET_PRIVATE(self)->msg;
2047
2048         if (msg)
2049                 g_object_ref (msg);
2050         
2051         return msg;
2052 }
2053
2054 static gboolean 
2055 is_empty (ModestGtkhtmlMsgView *self)
2056 {
2057         ModestGtkhtmlMsgViewPrivate *priv = MODEST_GTKHTML_MSG_VIEW_GET_PRIVATE (self);
2058
2059         return modest_mime_part_view_is_empty (MODEST_MIME_PART_VIEW (priv->body_view));
2060 }
2061
2062 static void
2063 set_zoom (ModestGtkhtmlMsgView *self, gdouble zoom)
2064 {
2065         ModestGtkhtmlMsgViewPrivate *priv;
2066         GtkAdjustment *html_vadj, *html_hadj;
2067
2068         g_return_if_fail (MODEST_IS_GTKHTML_MSG_VIEW (self));
2069         priv = MODEST_GTKHTML_MSG_VIEW_GET_PRIVATE (self);
2070
2071         modest_zoomable_set_zoom (MODEST_ZOOMABLE(priv->body_view), zoom);
2072
2073         html_vadj = gtk_scrolled_window_get_vadjustment (GTK_SCROLLED_WINDOW (priv->html_scroll));
2074         html_vadj->upper = 0;
2075         html_vadj->page_size = 0;
2076         g_signal_emit_by_name (G_OBJECT (html_vadj), "changed");
2077
2078         html_hadj = gtk_scrolled_window_get_hadjustment (GTK_SCROLLED_WINDOW (priv->html_scroll));
2079         html_hadj->upper = 0;
2080         html_hadj->page_size = 0;
2081         g_signal_emit_by_name (G_OBJECT (html_hadj), "changed");
2082
2083         gtk_widget_set_size_request (GTK_WIDGET (priv->body_view), 1, 1);
2084         gtk_widget_set_size_request (GTK_WIDGET (priv->body_view), -1, -1);
2085
2086         priv->idle_changes_count = 0;
2087         gtk_widget_queue_resize (priv->body_view);
2088 }
2089
2090 static gdouble
2091 get_zoom (ModestGtkhtmlMsgView *self)
2092 {
2093         ModestGtkhtmlMsgViewPrivate *priv;
2094
2095         g_return_val_if_fail (MODEST_IS_GTKHTML_MSG_VIEW (self), 1.0);
2096         priv = MODEST_GTKHTML_MSG_VIEW_GET_PRIVATE (self);
2097
2098         return modest_zoomable_get_zoom (MODEST_ZOOMABLE (priv->body_view));
2099 }
2100
2101 static TnyHeaderFlags
2102 get_priority (ModestGtkhtmlMsgView *self)
2103 {
2104         ModestGtkhtmlMsgViewPrivate *priv;
2105
2106         g_return_val_if_fail (MODEST_IS_GTKHTML_MSG_VIEW (self), 0);
2107
2108         priv = MODEST_GTKHTML_MSG_VIEW_GET_PRIVATE (self);
2109
2110         return modest_mail_header_view_get_priority (MODEST_MAIL_HEADER_VIEW (priv->mail_header_view));
2111 }
2112
2113 static void
2114 set_priority (ModestGtkhtmlMsgView *self, TnyHeaderFlags flags)
2115 {
2116         ModestGtkhtmlMsgViewPrivate *priv;
2117
2118         g_return_if_fail (MODEST_IS_GTKHTML_MSG_VIEW (self));
2119         priv = MODEST_GTKHTML_MSG_VIEW_GET_PRIVATE (self);
2120
2121         modest_mail_header_view_set_priority (MODEST_MAIL_HEADER_VIEW (priv->mail_header_view), flags);
2122
2123 #ifdef MODEST_TOOLKIT_HILDON2
2124         gboolean show_priority = FALSE;
2125         TnyHeaderFlags priority_flags;
2126
2127         priority_flags = flags & TNY_HEADER_FLAG_PRIORITY_MASK;
2128         if (priority_flags == TNY_HEADER_FLAG_HIGH_PRIORITY) {
2129                 show_priority = TRUE;
2130                 gtk_image_set_from_icon_name (GTK_IMAGE (priv->priority_icon), MODEST_HEADER_ICON_HIGH, GTK_ICON_SIZE_MENU);
2131         } else if (priority_flags == TNY_HEADER_FLAG_LOW_PRIORITY) {
2132                 show_priority = TRUE;
2133                 gtk_image_set_from_icon_name (GTK_IMAGE (priv->priority_icon), MODEST_HEADER_ICON_LOW, GTK_ICON_SIZE_MENU);
2134         }
2135
2136         if (show_priority && MODEST_IS_COMPACT_MAIL_HEADER_VIEW (priv->mail_header_view)) {
2137                 gtk_widget_show_all  (priv->priority_box);
2138         } else {
2139                 gtk_widget_hide_all (priv->priority_box);
2140         }
2141 #endif
2142
2143 }
2144
2145 /* INCREMENTAL SEARCH IMPLEMENTATION */
2146
2147 static gboolean 
2148 search (ModestGtkhtmlMsgView *self, const gchar *search)
2149 {
2150         ModestGtkhtmlMsgViewPrivate *priv;
2151         gboolean result;
2152         GtkAdjustment *vadj, *tmp_vadj;
2153         gdouble y_offset;
2154
2155         g_return_val_if_fail (MODEST_IS_GTKHTML_MSG_VIEW (self), FALSE);
2156
2157         priv = MODEST_GTKHTML_MSG_VIEW_GET_PRIVATE (self);
2158         vadj = gtk_layout_get_vadjustment (GTK_LAYOUT (priv->body_view));
2159         g_object_ref (vadj);
2160         tmp_vadj = GTK_ADJUSTMENT (gtk_adjustment_new (0.0, vadj->lower, vadj->upper, vadj->step_increment, 32.0, 32.0));
2161         gtk_layout_set_vadjustment (GTK_LAYOUT (priv->body_view), tmp_vadj);
2162         result = modest_isearch_view_search (MODEST_ISEARCH_VIEW (priv->body_view),
2163                                              search);
2164
2165         if (result) {
2166                 gint x, y, w, h;
2167                 gdouble offset_top, offset_bottom;
2168                 GtkAdjustment *adj;
2169                 if (modest_isearch_view_get_selection_area (MODEST_ISEARCH_VIEW (priv->body_view), &x, &y, &w, &h)) {
2170                         offset_top = (gdouble) (priv->headers_box->requisition.height + y);
2171                         offset_bottom = (gdouble) (priv->headers_box->requisition.height + y + h);
2172                         adj = GTK_ADJUSTMENT (priv->vadj);
2173                         if (offset_top < adj->value)
2174                                 gtk_adjustment_set_value (adj, offset_top + adj->page_increment - adj->page_size);
2175                         else if (offset_bottom > adj->value + adj->page_increment)
2176                                 gtk_adjustment_set_value (adj, offset_bottom - adj->page_increment);
2177                 }
2178         }
2179
2180         y_offset = tmp_vadj->value;
2181         gtk_layout_set_vadjustment (GTK_LAYOUT (priv->body_view), vadj);
2182         g_object_unref (vadj);
2183
2184         return result;
2185 }
2186
2187 static gboolean
2188 search_next (ModestGtkhtmlMsgView *self)
2189 {
2190         ModestGtkhtmlMsgViewPrivate *priv;
2191         gboolean result;
2192
2193         g_return_val_if_fail (MODEST_IS_GTKHTML_MSG_VIEW (self), FALSE);
2194
2195         priv = MODEST_GTKHTML_MSG_VIEW_GET_PRIVATE (self);
2196         result = modest_isearch_view_search_next (MODEST_ISEARCH_VIEW (priv->body_view));
2197
2198         if (result) {
2199                 gint x, y, w, h;
2200                 gdouble offset_top, offset_bottom;
2201                 GtkAdjustment *adj;
2202
2203                 if (modest_isearch_view_get_selection_area (MODEST_ISEARCH_VIEW (priv->body_view), &x, &y, &w, &h)) {
2204                         offset_top = (gdouble) (priv->headers_box->requisition.height + y);
2205                         offset_bottom = (gdouble) (priv->headers_box->requisition.height + y + h);
2206                         adj = GTK_ADJUSTMENT (priv->vadj);
2207                         if (offset_top < adj->value)
2208                                 gtk_adjustment_set_value (adj, offset_top + adj->page_increment - adj->page_size);
2209                         else if (offset_bottom > adj->value + adj->page_increment)
2210                                 gtk_adjustment_set_value (adj, offset_bottom - adj->page_increment);
2211                 }
2212         }
2213         return result;
2214 }
2215
2216 static TnyList *
2217 get_selected_attachments (ModestGtkhtmlMsgView *self)
2218 {
2219         ModestGtkhtmlMsgViewPrivate *priv;
2220
2221         g_return_val_if_fail (MODEST_IS_GTKHTML_MSG_VIEW (self), NULL);
2222         priv = MODEST_GTKHTML_MSG_VIEW_GET_PRIVATE (self);
2223
2224         return modest_attachments_view_get_selection (MODEST_ATTACHMENTS_VIEW (priv->attachments_view));
2225         
2226 }
2227
2228 static TnyList *
2229 get_attachments (ModestGtkhtmlMsgView *self)
2230 {
2231         ModestGtkhtmlMsgViewPrivate *priv;
2232
2233         g_return_val_if_fail (MODEST_IS_GTKHTML_MSG_VIEW (self), NULL);
2234         priv = MODEST_GTKHTML_MSG_VIEW_GET_PRIVATE (self);
2235
2236         return modest_attachments_view_get_attachments (MODEST_ATTACHMENTS_VIEW (priv->attachments_view));
2237         
2238 }
2239
2240 static void
2241 grab_focus (ModestGtkhtmlMsgView *self)
2242 {
2243         ModestGtkhtmlMsgViewPrivate *priv = NULL;
2244
2245         g_return_if_fail (MODEST_IS_GTKHTML_MSG_VIEW (self));
2246         priv = MODEST_GTKHTML_MSG_VIEW_GET_PRIVATE (self);
2247
2248         gtk_widget_grab_focus (priv->body_view);
2249 }
2250
2251 static void
2252 remove_attachment (ModestGtkhtmlMsgView *self, TnyMimePart *attachment)
2253 {
2254         ModestGtkhtmlMsgViewPrivate *priv;
2255
2256         g_return_if_fail (MODEST_IS_GTKHTML_MSG_VIEW (self));
2257         g_return_if_fail (TNY_IS_MIME_PART (attachment));
2258         priv = MODEST_GTKHTML_MSG_VIEW_GET_PRIVATE (self);
2259
2260         modest_attachments_view_remove_attachment (MODEST_ATTACHMENTS_VIEW (priv->attachments_view),
2261                                                    attachment);
2262         
2263 }
2264
2265 /* TNY HEADER VIEW IMPLEMENTATION */
2266
2267 static void
2268 tny_header_view_init (gpointer g, gpointer iface_data)
2269 {
2270         TnyHeaderViewIface *klass = (TnyHeaderViewIface *)g;
2271
2272         klass->set_header = modest_msg_view_set_header;
2273         klass->clear = modest_msg_view_clear_header;
2274
2275         return;
2276 }
2277
2278 static void
2279 modest_msg_view_set_header (TnyHeaderView *self, TnyHeader *header)
2280 {
2281         MODEST_GTKHTML_MSG_VIEW_GET_CLASS (self)->set_header_func (self, header);
2282 }
2283
2284
2285 static void
2286 modest_msg_view_set_header_default (TnyHeaderView *self, TnyHeader *header)
2287 {
2288         set_header (MODEST_GTKHTML_MSG_VIEW (self), header);
2289 }
2290
2291 static void
2292 modest_msg_view_clear_header (TnyHeaderView *self)
2293 {
2294         MODEST_GTKHTML_MSG_VIEW_GET_CLASS (self)->clear_header_func (self);
2295 }
2296
2297
2298 static void
2299 modest_msg_view_clear_header_default (TnyHeaderView *self)
2300 {
2301         set_message (MODEST_GTKHTML_MSG_VIEW (self), NULL, NULL);
2302 }
2303
2304 /* TNY MSG IMPLEMENTATION */
2305
2306 static void
2307 tny_msg_view_init (gpointer g, gpointer iface_data)
2308 {
2309         TnyMsgViewIface *klass = (TnyMsgViewIface *)g;
2310
2311         klass->get_msg = modest_msg_view_get_msg;
2312         klass->set_msg = modest_msg_view_set_msg;
2313         klass->set_unavailable = modest_msg_view_set_unavailable;
2314         klass->clear = modest_msg_view_clear;
2315         klass->create_mime_part_view_for = modest_msg_view_create_mime_part_view_for;
2316         klass->create_new_inline_viewer = modest_msg_view_create_new_inline_viewer;
2317
2318         return;
2319 }
2320
2321 static TnyMsg *
2322 modest_msg_view_get_msg (TnyMsgView *self)
2323 {
2324         return MODEST_GTKHTML_MSG_VIEW_GET_CLASS (self)->get_msg_func (self);
2325 }
2326
2327 static TnyMsg *
2328 modest_msg_view_get_msg_default (TnyMsgView *self)
2329 {
2330         return TNY_MSG (tny_mime_part_view_get_part (TNY_MIME_PART_VIEW (self)));
2331 }
2332
2333 static void
2334 modest_msg_view_set_msg (TnyMsgView *self, TnyMsg *msg)
2335 {
2336         MODEST_GTKHTML_MSG_VIEW_GET_CLASS (self)->set_msg_func (self, msg);
2337 }
2338
2339 static void 
2340 modest_msg_view_set_msg_default (TnyMsgView *self, TnyMsg *msg)
2341 {
2342
2343         tny_mime_part_view_set_part (TNY_MIME_PART_VIEW (self), TNY_MIME_PART (msg));
2344
2345         return;
2346 }
2347
2348 static void
2349 modest_msg_view_set_unavailable (TnyMsgView *self)
2350 {
2351         MODEST_GTKHTML_MSG_VIEW_GET_CLASS (self)->set_unavailable_func (self);
2352 }
2353
2354 static void
2355 modest_msg_view_set_unavailable_default (TnyMsgView *self)
2356 {
2357         tny_msg_view_clear (self);
2358
2359         return;
2360 }
2361
2362 static void
2363 modest_msg_view_clear (TnyMsgView *self)
2364 {
2365         MODEST_GTKHTML_MSG_VIEW_GET_CLASS (self)->clear_func (self);
2366 }
2367
2368 static void
2369 modest_msg_view_clear_default (TnyMsgView *self)
2370 {
2371         set_message (MODEST_GTKHTML_MSG_VIEW (self), NULL, NULL);
2372 }
2373
2374 static TnyMimePartView*
2375 modest_msg_view_create_mime_part_view_for (TnyMsgView *self, TnyMimePart *part)
2376 {
2377         return MODEST_GTKHTML_MSG_VIEW_GET_CLASS (self)->create_mime_part_view_for_func (self, part);
2378 }
2379
2380 static TnyMimePartView*
2381 modest_msg_view_create_mime_part_view_for_default (TnyMsgView *self, TnyMimePart *part)
2382 {
2383         g_warning ("modest_msg_view_create_mime_part_view_for_default is not implemented");
2384         return NULL;
2385 }
2386
2387 static TnyMsgView*
2388 modest_msg_view_create_new_inline_viewer (TnyMsgView *self)
2389 {
2390         return MODEST_GTKHTML_MSG_VIEW_GET_CLASS (self)->create_new_inline_viewer_func (self);
2391 }
2392
2393 static TnyMsgView*
2394 modest_msg_view_create_new_inline_viewer_default (TnyMsgView *self)
2395 {
2396         g_warning ("modest_msg_view_create_new_inline_viewer_default is not implemented");
2397
2398         return NULL;
2399 }
2400
2401 /* TNY MIME PART IMPLEMENTATION */
2402
2403 static void
2404 tny_mime_part_view_init (gpointer g, gpointer iface_data)
2405 {
2406         TnyMimePartViewIface *klass = (TnyMimePartViewIface *)g;
2407
2408         klass->get_part = modest_msg_view_mp_get_part;
2409         klass->set_part = modest_msg_view_mp_set_part;
2410         klass->clear = modest_msg_view_mp_clear;
2411
2412         return;
2413 }
2414
2415 static TnyMimePart* 
2416 modest_msg_view_mp_get_part (TnyMimePartView *self)
2417 {
2418         return MODEST_GTKHTML_MSG_VIEW_GET_CLASS (self)->get_part_func (self);
2419 }
2420
2421
2422 static TnyMimePart* 
2423 modest_msg_view_mp_get_part_default (TnyMimePartView *self)
2424 {
2425         return TNY_MIME_PART (get_message (MODEST_GTKHTML_MSG_VIEW (self)));
2426 }
2427
2428 static void
2429 modest_msg_view_mp_set_part (TnyMimePartView *self,
2430                              TnyMimePart *part)
2431 {
2432         MODEST_GTKHTML_MSG_VIEW_GET_CLASS (self)->set_part_func (self, part);
2433 }
2434
2435 static void
2436 modest_msg_view_mp_set_part_default (TnyMimePartView *self,
2437                                      TnyMimePart *part)
2438 {
2439         g_return_if_fail ((part == NULL) || TNY_IS_MSG (part));
2440
2441         set_message (MODEST_GTKHTML_MSG_VIEW (self), TNY_MSG (part), NULL);
2442 }
2443
2444 static void
2445 modest_msg_view_mp_clear (TnyMimePartView *self)
2446 {
2447         tny_msg_view_clear (TNY_MSG_VIEW (self));
2448 }
2449
2450 /* MODEST MIME PART VIEW IMPLEMENTATION */
2451
2452 static void
2453 modest_mime_part_view_init (gpointer g, gpointer iface_data)
2454 {
2455         ModestMimePartViewIface *klass = (ModestMimePartViewIface *)g;
2456
2457         klass->is_empty_func = modest_msg_view_mp_is_empty;
2458
2459         return;
2460 }
2461
2462 static gboolean
2463 modest_msg_view_mp_is_empty (ModestMimePartView *self)
2464 {
2465         return MODEST_GTKHTML_MSG_VIEW_GET_CLASS (self)->is_empty_func (self);
2466 }
2467
2468 static gboolean
2469 modest_msg_view_mp_is_empty_default (ModestMimePartView *self)
2470 {
2471         return is_empty (MODEST_GTKHTML_MSG_VIEW (self));
2472 }
2473
2474 /* MODEST ZOOMABLE IMPLEMENTATION */
2475 static void
2476 modest_zoomable_init (gpointer g, gpointer iface_data)
2477 {
2478         ModestZoomableIface *klass = (ModestZoomableIface *)g;
2479
2480         klass->get_zoom_func = modest_msg_view_get_zoom;
2481         klass->set_zoom_func = modest_msg_view_set_zoom;
2482         klass->zoom_minus_func = modest_msg_view_zoom_minus;
2483         klass->zoom_plus_func = modest_msg_view_zoom_plus;
2484
2485         return;
2486 }
2487
2488 static gdouble
2489 modest_msg_view_get_zoom (ModestZoomable *self)
2490 {
2491         return MODEST_GTKHTML_MSG_VIEW_GET_CLASS (self)->get_zoom_func (self);
2492 }
2493
2494 static gdouble
2495 modest_msg_view_get_zoom_default (ModestZoomable *self)
2496 {
2497         return get_zoom (MODEST_GTKHTML_MSG_VIEW (self));
2498 }
2499
2500 static void
2501 modest_msg_view_set_zoom (ModestZoomable *self, gdouble value)
2502 {
2503         MODEST_GTKHTML_MSG_VIEW_GET_CLASS (self)->set_zoom_func (self, value);
2504 }
2505
2506 static void
2507 modest_msg_view_set_zoom_default (ModestZoomable *self, gdouble value)
2508 {
2509         set_zoom (MODEST_GTKHTML_MSG_VIEW (self), value);
2510 }
2511
2512 static gboolean
2513 modest_msg_view_zoom_minus (ModestZoomable *self)
2514 {
2515         return MODEST_GTKHTML_MSG_VIEW_GET_CLASS (self)->zoom_minus_func (self);
2516 }
2517
2518 static gboolean
2519 modest_msg_view_zoom_minus_default (ModestZoomable *self)
2520 {
2521         /* operation not supported in ModestMsgView */
2522         return FALSE;
2523 }
2524
2525 static gboolean
2526 modest_msg_view_zoom_plus (ModestZoomable *self)
2527 {
2528         return MODEST_GTKHTML_MSG_VIEW_GET_CLASS (self)->zoom_plus_func (self);
2529 }
2530
2531 static gboolean
2532 modest_msg_view_zoom_plus_default (ModestZoomable *self)
2533 {
2534         /* operation not supported in ModestMsgView */
2535         return FALSE;
2536 }
2537
2538 /* MODEST ISEARCH VIEW IMPLEMENTATION */
2539 static void
2540 modest_isearch_view_init (gpointer g, gpointer iface_data)
2541 {
2542         ModestISearchViewIface *klass = (ModestISearchViewIface *)g;
2543
2544         klass->search_func = modest_msg_view_search;
2545         klass->search_next_func = modest_msg_view_search_next;
2546
2547         return;
2548 }
2549
2550 static gboolean
2551 modest_msg_view_search (ModestISearchView *self, const gchar *string)
2552 {
2553         return MODEST_GTKHTML_MSG_VIEW_GET_CLASS (self)->search_func (self, string);
2554 }
2555
2556 static gboolean
2557 modest_msg_view_search_default (ModestISearchView *self, const gchar *string)
2558 {
2559         return search (MODEST_GTKHTML_MSG_VIEW (self), string);
2560 }
2561
2562 static gboolean
2563 modest_msg_view_search_next (ModestISearchView *self)
2564 {
2565         return MODEST_GTKHTML_MSG_VIEW_GET_CLASS (self)->search_next_func (self);
2566 }
2567
2568 static gboolean
2569 modest_msg_view_search_next_default (ModestISearchView *self)
2570 {
2571         return search_next (MODEST_GTKHTML_MSG_VIEW (self));
2572 }
2573
2574 /* MODEST MSG VIEW IMPLEMENTATION */
2575 static void
2576 modest_msg_view_init (gpointer g, gpointer iface_data)
2577 {
2578         ModestMsgViewIface *klass = (ModestMsgViewIface *)g;
2579
2580         klass->set_msg_with_other_body_func = modest_gtkhtml_msg_view_set_msg_with_other_body;
2581         klass->get_vadjustment_func = modest_gtkhtml_msg_view_get_vadjustment;
2582         klass->get_hadjustment_func = modest_gtkhtml_msg_view_get_hadjustment;
2583         klass->set_vadjustment_func = modest_gtkhtml_msg_view_set_vadjustment;
2584         klass->set_hadjustment_func = modest_gtkhtml_msg_view_set_hadjustment;
2585         klass->set_shadow_type_func = modest_gtkhtml_msg_view_set_shadow_type;
2586         klass->get_shadow_type_func = modest_gtkhtml_msg_view_get_shadow_type;
2587         klass->get_priority_func = modest_gtkhtml_msg_view_get_priority;
2588         klass->set_priority_func = modest_gtkhtml_msg_view_set_priority;
2589         klass->get_selected_attachments_func = modest_gtkhtml_msg_view_get_selected_attachments;
2590         klass->get_attachments_func = modest_gtkhtml_msg_view_get_attachments;
2591         klass->grab_focus_func = modest_gtkhtml_msg_view_grab_focus;
2592         klass->remove_attachment_func = modest_gtkhtml_msg_view_remove_attachment;
2593         klass->request_fetch_images_func = modest_gtkhtml_msg_view_request_fetch_images;
2594         klass->set_branding_func = modest_gtkhtml_msg_view_set_branding;
2595         klass->has_blocked_external_images_func = modest_gtkhtml_msg_view_has_blocked_external_images;
2596
2597         return;
2598 }
2599
2600 static void
2601 modest_gtkhtml_msg_view_set_msg_with_other_body (ModestMsgView *self, TnyMsg *msg, TnyMimePart *other_body)
2602 {
2603         MODEST_GTKHTML_MSG_VIEW_GET_CLASS (self)->set_msg_with_other_body_func (self, msg, other_body);
2604 }
2605
2606 static void
2607 modest_gtkhtml_msg_view_set_msg_with_other_body_default (ModestMsgView *self, TnyMsg *msg, TnyMimePart *other_body)
2608 {
2609         set_message (MODEST_GTKHTML_MSG_VIEW (self), msg, other_body);
2610 }
2611
2612 static GtkAdjustment*
2613 modest_gtkhtml_msg_view_get_vadjustment (ModestMsgView *self)
2614 {
2615         return MODEST_GTKHTML_MSG_VIEW_GET_CLASS (self)->get_vadjustment_func (self);
2616 }
2617
2618 static GtkAdjustment*
2619 modest_gtkhtml_msg_view_get_vadjustment_default (ModestMsgView *self)
2620 {
2621         return get_vadjustment (MODEST_GTKHTML_MSG_VIEW (self));
2622 }
2623
2624 static GtkAdjustment*
2625 modest_gtkhtml_msg_view_get_hadjustment (ModestMsgView *self)
2626 {
2627         return MODEST_GTKHTML_MSG_VIEW_GET_CLASS (self)->get_hadjustment_func (self);
2628 }
2629
2630 static GtkAdjustment*
2631 modest_gtkhtml_msg_view_get_hadjustment_default (ModestMsgView *self)
2632 {
2633         return get_hadjustment (MODEST_GTKHTML_MSG_VIEW (self));
2634 }
2635
2636 static void
2637 modest_gtkhtml_msg_view_set_vadjustment (ModestMsgView *self, GtkAdjustment *adj)
2638 {
2639         MODEST_GTKHTML_MSG_VIEW_GET_CLASS (self)->set_vadjustment_func (self, adj);
2640 }
2641
2642 static void
2643 modest_gtkhtml_msg_view_set_vadjustment_default (ModestMsgView *self, GtkAdjustment *adj)
2644 {
2645         set_vadjustment (MODEST_GTKHTML_MSG_VIEW (self), adj);
2646 }
2647
2648 static void
2649 modest_gtkhtml_msg_view_set_hadjustment (ModestMsgView *self, GtkAdjustment *adj)
2650 {
2651         MODEST_GTKHTML_MSG_VIEW_GET_CLASS (self)->set_hadjustment_func (self, adj);
2652 }
2653
2654 static void
2655 modest_gtkhtml_msg_view_set_hadjustment_default (ModestMsgView *self, GtkAdjustment *adj)
2656 {
2657         set_hadjustment (MODEST_GTKHTML_MSG_VIEW (self), adj);
2658 }
2659
2660 static void
2661 modest_gtkhtml_msg_view_set_shadow_type (ModestMsgView *self, GtkShadowType type)
2662 {
2663         MODEST_GTKHTML_MSG_VIEW_GET_CLASS (self)->set_shadow_type_func (self, type);
2664 }
2665
2666 static void
2667 modest_gtkhtml_msg_view_set_shadow_type_default (ModestMsgView *self, GtkShadowType type)
2668 {
2669         set_shadow_type (MODEST_GTKHTML_MSG_VIEW (self), type);
2670 }
2671
2672 static GtkShadowType
2673 modest_gtkhtml_msg_view_get_shadow_type (ModestMsgView *self)
2674 {
2675         return MODEST_GTKHTML_MSG_VIEW_GET_CLASS (self)->get_shadow_type_func (self);
2676 }
2677
2678 static GtkShadowType
2679 modest_gtkhtml_msg_view_get_shadow_type_default (ModestMsgView *self)
2680 {
2681         return get_shadow_type (MODEST_GTKHTML_MSG_VIEW (self));
2682 }
2683
2684 static void
2685 modest_gtkhtml_msg_view_set_priority (ModestMsgView *self, TnyHeaderFlags flags)
2686 {
2687         MODEST_GTKHTML_MSG_VIEW_GET_CLASS (self)->set_priority_func (self, flags);
2688 }
2689
2690 static void
2691 modest_gtkhtml_msg_view_set_priority_default (ModestMsgView *self, TnyHeaderFlags flags)
2692 {
2693         set_priority (MODEST_GTKHTML_MSG_VIEW (self), flags);
2694 }
2695
2696 static TnyHeaderFlags
2697 modest_gtkhtml_msg_view_get_priority (ModestMsgView *self)
2698 {
2699         return MODEST_GTKHTML_MSG_VIEW_GET_CLASS (self)->get_priority_func (self);
2700 }
2701
2702 static TnyHeaderFlags
2703 modest_gtkhtml_msg_view_get_priority_default (ModestMsgView *self)
2704 {
2705         return get_priority (MODEST_GTKHTML_MSG_VIEW (self));
2706 }
2707
2708 static TnyList*
2709 modest_gtkhtml_msg_view_get_selected_attachments (ModestMsgView *self)
2710 {
2711         return MODEST_GTKHTML_MSG_VIEW_GET_CLASS (self)->get_selected_attachments_func (self);
2712 }
2713
2714 static TnyList*
2715 modest_gtkhtml_msg_view_get_selected_attachments_default (ModestMsgView *self)
2716 {
2717         return get_selected_attachments (MODEST_GTKHTML_MSG_VIEW (self));
2718 }
2719
2720 static TnyList*
2721 modest_gtkhtml_msg_view_get_attachments (ModestMsgView *self)
2722 {
2723         return MODEST_GTKHTML_MSG_VIEW_GET_CLASS (self)->get_attachments_func (self);
2724 }
2725
2726 static TnyList*
2727 modest_gtkhtml_msg_view_get_attachments_default (ModestMsgView *self)
2728 {
2729         return get_attachments (MODEST_GTKHTML_MSG_VIEW (self));
2730 }
2731
2732 static void
2733 modest_gtkhtml_msg_view_grab_focus (ModestMsgView *self)
2734 {
2735         MODEST_GTKHTML_MSG_VIEW_GET_CLASS (self)->grab_focus_func (self);
2736 }
2737
2738 static void
2739 modest_gtkhtml_msg_view_grab_focus_default (ModestMsgView *self)
2740 {
2741         grab_focus (MODEST_GTKHTML_MSG_VIEW (self));
2742 }
2743
2744 static void
2745 modest_gtkhtml_msg_view_remove_attachment (ModestMsgView *self, TnyMimePart *attachment)
2746 {
2747         MODEST_GTKHTML_MSG_VIEW_GET_CLASS (self)->remove_attachment_func (self, attachment);
2748 }
2749
2750 static void
2751 modest_gtkhtml_msg_view_remove_attachment_default (ModestMsgView *self, TnyMimePart *attachment)
2752 {
2753         remove_attachment (MODEST_GTKHTML_MSG_VIEW (self), attachment);
2754 }
2755
2756 static void
2757 modest_gtkhtml_msg_view_request_fetch_images (ModestMsgView *self)
2758 {
2759         MODEST_GTKHTML_MSG_VIEW_GET_CLASS (self)->request_fetch_images_func (self);
2760 }
2761
2762 static void
2763 modest_gtkhtml_msg_view_request_fetch_images_default (ModestMsgView *self)
2764 {
2765         request_fetch_images (MODEST_GTKHTML_MSG_VIEW (self));
2766 }
2767
2768 static void
2769 modest_gtkhtml_msg_view_set_branding (ModestMsgView *self, const gchar *brand_name, const GdkPixbuf *brand_icon)
2770 {
2771         MODEST_GTKHTML_MSG_VIEW_GET_CLASS (self)->set_branding_func (self, brand_name, brand_icon);
2772 }
2773
2774 static void
2775 modest_gtkhtml_msg_view_set_branding_default (ModestMsgView *self, const gchar *brand_name, const GdkPixbuf *brand_icon)
2776 {
2777         set_branding (MODEST_GTKHTML_MSG_VIEW (self), brand_name, brand_icon);
2778 }
2779
2780 static gboolean
2781 modest_gtkhtml_msg_view_has_blocked_external_images (ModestMsgView *self)
2782 {
2783         return MODEST_GTKHTML_MSG_VIEW_GET_CLASS (self)->has_blocked_external_images_func (self);
2784 }
2785
2786 static gboolean
2787 modest_gtkhtml_msg_view_has_blocked_external_images_default (ModestMsgView *self)
2788 {
2789         return has_blocked_external_images (MODEST_GTKHTML_MSG_VIEW (self));
2790 }
2791
2792 static void
2793 set_calendar (ModestGtkhtmlMsgView *self, TnyHeader *header, TnyMsg *msg)
2794 {
2795         ModestGtkhtmlMsgViewPrivate *priv;
2796         TnyMimePart *calendar_part;
2797
2798         g_return_if_fail (MODEST_IS_GTKHTML_MSG_VIEW (self));
2799         priv = MODEST_GTKHTML_MSG_VIEW_GET_PRIVATE (self);
2800
2801         gtk_widget_hide (priv->calendar_actions_container);
2802         gtk_widget_hide_all (priv->calendar_actions_container);
2803         if (priv->calendar_actions_box) {
2804                 gtk_widget_destroy (priv->calendar_actions_box);
2805                 priv->calendar_actions_box = NULL;
2806         }
2807
2808         calendar_part = modest_tny_msg_find_calendar (TNY_MSG (msg));
2809
2810         if (calendar_part) {
2811                 gboolean retval = FALSE;
2812                 priv->calendar_actions_box = gtk_vbox_new (FALSE, 0);
2813                 gtk_box_pack_start (GTK_BOX (priv->calendar_actions_container), priv->calendar_actions_box, FALSE, FALSE, 0);
2814                 g_signal_emit_by_name (G_OBJECT (self), "handle-calendar", calendar_part, priv->calendar_actions_box, &retval);
2815                 if (retval) {
2816                         gtk_widget_show (priv->calendar_actions_container);
2817                         gtk_widget_show_all (priv->calendar_actions_container);
2818                         gtk_widget_show (priv->calendar_actions_box);
2819                         gtk_widget_show_all (priv->calendar_actions_box);
2820                 } else {
2821                         gtk_widget_hide (priv->calendar_actions_container);
2822                         gtk_widget_hide_all (priv->calendar_actions_container);
2823                         gtk_widget_hide (priv->calendar_actions_box);
2824                         gtk_widget_hide_all (priv->calendar_actions_box);
2825                 }
2826                 gtk_widget_show_all  (priv->calendar_box);
2827                 g_object_unref (calendar_part);
2828         } else {
2829                 gtk_widget_hide_all (priv->calendar_box);
2830                 gtk_widget_hide (priv->calendar_actions_container);
2831                 gtk_widget_hide_all (priv->calendar_actions_container);
2832                 if (priv->calendar_actions_box)
2833                         gtk_widget_hide (priv->calendar_actions_box);
2834         }
2835 }