276f2f1ba3ccb60aaabe48483286dfdf461452f6
[modest] / src / widgets / modest-msg-view.c
1 /* Copyright (c) 2006, Nokia Corporation
2  * All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions are
6  * met:
7  *
8  * * Redistributions of source code must retain the above copyright
9  *   notice, this list of conditions and the following disclaimer.
10  * * Redistributions in binary form must reproduce the above copyright
11  *   notice, this list of conditions and the following disclaimer in the
12  *   documentation and/or other materials provided with the distribution.
13  * * Neither the name of the Nokia Corporation nor the names of its
14  *   contributors may be used to endorse or promote products derived from
15  *   this software without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
18  * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
19  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
20  * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
21  * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
22  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
23  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
24  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
25  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
26  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
27  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28  */
29
30 #include <tny-gtk-text-buffer-stream.h>
31 #include <string.h>
32 #include <regex.h>
33 #include <ctype.h>
34 #include <stdlib.h>
35 #include <glib/gi18n.h>
36 #include <gtkhtml/gtkhtml.h>
37 #include <gtkhtml/gtkhtml-stream.h>
38 #include <gtkhtml/gtkhtml-search.h>
39 #include <gtkhtml/gtkhtml-embedded.h>
40 #include <tny-list.h>
41 #include <tny-simple-list.h>
42
43 #include <modest-tny-msg.h>
44 #include <modest-text-utils.h>
45 #include "modest-msg-view.h"
46 #include "modest-tny-stream-gtkhtml.h"
47 #include <modest-mail-header-view.h>
48 #include <modest-attachments-view.h>
49 #include <modest-marshal.h>
50
51
52 /* 'private'/'protected' functions */
53 static void     modest_msg_view_class_init   (ModestMsgViewClass *klass);
54 static void     modest_msg_view_init         (ModestMsgView *obj);
55 static void     modest_msg_view_finalize     (GObject *obj);
56 static void     modest_msg_view_destroy     (GtkObject *obj);
57 static void     set_property (GObject *object, guint prop_id, const GValue *value, GParamSpec *pspec);
58 static void     get_property (GObject *object, guint prop_id, GValue *value, GParamSpec *pspec);
59
60 /* headers signals */
61 static void on_recpt_activated (ModestMailHeaderView *header_view, const gchar *address, ModestMsgView *msg_view);
62 static void on_attachment_activated (ModestAttachmentsView * att_view, TnyMimePart *mime_part, gpointer userdata);
63
64 /* GtkHtml signals */
65 static gboolean on_link_clicked (GtkWidget *widget, const gchar *uri, ModestMsgView *msg_view);
66 static gboolean on_url_requested (GtkWidget *widget, const gchar *uri, GtkHTMLStream *stream,
67                                   ModestMsgView *msg_view);
68 static gboolean on_link_hover (GtkWidget *widget, const gchar *uri, ModestMsgView *msg_view);
69
70 #ifdef MAEMO_CHANGES
71 static void     on_tap_and_hold (GtkWidget *widget, gpointer userdata); 
72 #endif /*MAEMO_CHANGES*/
73
74
75 /* size allocation and drawing handlers */
76 static void get_view_allocation (ModestMsgView *msg_view, GtkAllocation *allocation);
77 static void size_request (GtkWidget *widget, GtkRequisition *req);
78 static void size_allocate (GtkWidget *widget, GtkAllocation *alloc);
79 static void realize (GtkWidget *widget);
80 static void unrealize (GtkWidget *widget);
81 static gint expose (GtkWidget *widget, GdkEventExpose *event);
82 static void reclamp_adjustment (GtkAdjustment *adj, gboolean *value_changed);
83 static void set_hadjustment_values (ModestMsgView *msg_view, gboolean *value_changed);
84 static void set_scroll_adjustments (ModestMsgView *msg_view, GtkAdjustment *hadj, GtkAdjustment *vadj);
85 static void adjustment_value_changed (GtkAdjustment *adj, gpointer data);
86 static void html_adjustment_changed (GtkAdjustment *adj, gpointer data);
87 static void disconnect_vadjustment (ModestMsgView *obj);
88 static void disconnect_hadjustment (ModestMsgView *obj);
89 static gboolean idle_readjust_scroll (ModestMsgView *obj);
90
91 /* GtkContainer methods */
92 static void forall (GtkContainer *container, gboolean include_internals,
93                     GtkCallback callback, gpointer userdata);
94 static void container_remove (GtkContainer *container, GtkWidget *widget);
95
96 /* list my signals */
97 enum {
98         LINK_CLICKED_SIGNAL,
99         LINK_HOVER_SIGNAL,
100         ATTACHMENT_CLICKED_SIGNAL,
101         RECPT_ACTIVATED_SIGNAL,
102         LINK_CONTEXTUAL_SIGNAL,
103         LAST_SIGNAL
104 };
105
106 /* list properties */
107 enum {
108         PROP_0,
109         PROP_HADJUSTMENT,
110         PROP_VADJUSTMENT,
111         PROP_SHADOW_TYPE
112 };
113
114 typedef struct _ModestMsgViewPrivate ModestMsgViewPrivate;
115 struct _ModestMsgViewPrivate {
116         GtkWidget   *gtkhtml;
117         GtkWidget   *mail_header_view;
118         GtkWidget   *attachments_view;
119
120         TnyMsg      *msg;
121
122         /* embedded elements */
123         GtkWidget   *headers_box;
124         GtkWidget   *html_scroll;
125         GtkWidget   *attachments_box;
126
127         /* internal adjustments for set_scroll_adjustments */
128         GtkAdjustment *hadj;
129         GtkAdjustment *vadj;
130         GtkShadowType shadow_type;
131
132         /* gdk windows for drawing */
133         GdkWindow *view_window;
134         GdkWindow *headers_window;
135         GdkWindow *html_window;
136
137         /* zoom */
138         gdouble current_zoom;
139
140         /* link click management */
141         gchar *last_url;
142
143         TnyHeaderFlags priority_flags;
144
145         gulong  sig1, sig2, sig3;
146 };
147 #define MODEST_MSG_VIEW_GET_PRIVATE(o)      (G_TYPE_INSTANCE_GET_PRIVATE((o), \
148                                                  MODEST_TYPE_MSG_VIEW, \
149                                                  ModestMsgViewPrivate))
150 /* globals */
151 static GtkContainerClass *parent_class = NULL;
152
153 /* uncomment the following if you have defined any signals */
154 static guint signals[LAST_SIGNAL] = {0};
155
156 GType
157 modest_msg_view_get_type (void)
158 {
159         static GType my_type = 0;
160         if (!my_type) {
161                 static const GTypeInfo my_info = {
162                         sizeof(ModestMsgViewClass),
163                         NULL,           /* base init */
164                         NULL,           /* base finalize */
165                         (GClassInitFunc) modest_msg_view_class_init,
166                         NULL,           /* class finalize */
167                         NULL,           /* class data */
168                         sizeof(ModestMsgView),
169                         1,              /* n_preallocs */
170                         (GInstanceInitFunc) modest_msg_view_init,
171                         NULL
172                 };
173                 my_type = g_type_register_static (GTK_TYPE_CONTAINER,
174                                                   "ModestMsgView",
175                                                   &my_info, 0);
176         }
177         return my_type;
178 }
179
180 static void
181 modest_msg_view_class_init (ModestMsgViewClass *klass)
182 {
183         GObjectClass *gobject_class;
184         GtkWidgetClass *widget_class;
185         GtkObjectClass *gtkobject_class;
186         GtkContainerClass *container_class;
187         gobject_class = (GObjectClass*) klass;
188         widget_class = (GtkWidgetClass *) klass;
189         gtkobject_class = (GtkObjectClass *) klass;
190         container_class = (GtkContainerClass *) klass;
191
192         parent_class            = g_type_class_peek_parent (klass);
193         gobject_class->finalize = modest_msg_view_finalize;
194         gobject_class->set_property = set_property;
195         gobject_class->get_property = get_property;
196         gtkobject_class->destroy = modest_msg_view_destroy;
197
198         widget_class->realize = realize;
199         widget_class->unrealize = unrealize;
200         widget_class->expose_event = expose;
201         widget_class->size_request = size_request;
202         widget_class->size_allocate = size_allocate;
203
204         container_class->forall = forall;
205         container_class->remove = container_remove;
206
207         klass->set_scroll_adjustments = set_scroll_adjustments;
208
209         g_type_class_add_private (gobject_class, sizeof(ModestMsgViewPrivate));
210
211         g_object_class_install_property (gobject_class,
212                                          PROP_HADJUSTMENT,
213                                          g_param_spec_object ("hadjustment", 
214                                                               _("Horizontal adjustment"),
215                                                               _("GtkAdjustment with information of the horizontal visible position"),
216                                                               GTK_TYPE_ADJUSTMENT,
217                                                               G_PARAM_READABLE | G_PARAM_WRITABLE | G_PARAM_CONSTRUCT));
218
219         g_object_class_install_property (gobject_class,
220                                          PROP_VADJUSTMENT,
221                                          g_param_spec_object ("vadjustment", 
222                                                               _("Vertical adjustment"),
223                                                               _("GtkAdjustment with information of the vertical visible position"),
224                                                               GTK_TYPE_ADJUSTMENT,
225                                                               G_PARAM_READABLE | G_PARAM_WRITABLE | G_PARAM_CONSTRUCT));
226
227         g_object_class_install_property (gobject_class,
228                                          PROP_SHADOW_TYPE,
229                                          g_param_spec_enum ("shadow_type", 
230                                                             _("Shadow type"),
231                                                             _("Kind of shadow that's shown around the view"),
232                                                             GTK_TYPE_SHADOW_TYPE,
233                                                             GTK_SHADOW_IN,
234                                                             G_PARAM_READABLE | G_PARAM_WRITABLE ));
235
236         signals[LINK_CLICKED_SIGNAL] =
237                 g_signal_new ("link_clicked",
238                               G_TYPE_FROM_CLASS (gobject_class),
239                               G_SIGNAL_RUN_FIRST,
240                               G_STRUCT_OFFSET(ModestMsgViewClass, link_clicked),
241                               NULL, NULL,
242                               g_cclosure_marshal_VOID__STRING,
243                               G_TYPE_NONE, 1, G_TYPE_STRING);
244         
245         signals[ATTACHMENT_CLICKED_SIGNAL] =
246                 g_signal_new ("attachment_clicked",
247                               G_TYPE_FROM_CLASS (gobject_class),
248                               G_SIGNAL_RUN_FIRST,
249                               G_STRUCT_OFFSET(ModestMsgViewClass, attachment_clicked),
250                               NULL, NULL,
251                               g_cclosure_marshal_VOID__OBJECT,
252                               G_TYPE_NONE, 1, G_TYPE_OBJECT);
253         
254         signals[LINK_HOVER_SIGNAL] =
255                 g_signal_new ("link_hover",
256                               G_TYPE_FROM_CLASS (gobject_class),
257                               G_SIGNAL_RUN_FIRST,
258                               G_STRUCT_OFFSET(ModestMsgViewClass, link_hover),
259                               NULL, NULL,
260                               g_cclosure_marshal_VOID__STRING,
261                               G_TYPE_NONE, 1, G_TYPE_STRING);
262
263         signals[RECPT_ACTIVATED_SIGNAL] =
264                 g_signal_new ("recpt_activated",
265                               G_TYPE_FROM_CLASS (gobject_class),
266                               G_SIGNAL_RUN_FIRST,
267                               G_STRUCT_OFFSET(ModestMsgViewClass, recpt_activated),
268                               NULL, NULL,
269                               g_cclosure_marshal_VOID__STRING,
270                               G_TYPE_NONE, 1, G_TYPE_STRING);
271
272         signals[LINK_CONTEXTUAL_SIGNAL] =
273                 g_signal_new ("link_contextual",
274                               G_TYPE_FROM_CLASS (gobject_class),
275                               G_SIGNAL_RUN_FIRST,
276                               G_STRUCT_OFFSET(ModestMsgViewClass, link_contextual),
277                               NULL, NULL,
278                               g_cclosure_marshal_VOID__STRING,
279                               G_TYPE_NONE, 1, G_TYPE_STRING);
280
281         widget_class->set_scroll_adjustments_signal =
282                 g_signal_new ("set_scroll_adjustments",
283                               G_OBJECT_CLASS_TYPE (gobject_class),
284                               G_SIGNAL_RUN_LAST|G_SIGNAL_ACTION,
285                               G_STRUCT_OFFSET (ModestMsgViewClass, set_scroll_adjustments),
286                               NULL, NULL,
287                               modest_marshal_VOID__POINTER_POINTER,
288                               G_TYPE_NONE, 2,
289                               GTK_TYPE_ADJUSTMENT,
290                               GTK_TYPE_ADJUSTMENT);
291 }
292
293 static void
294 set_property (GObject *object, 
295               guint prop_id, 
296               const GValue *value, 
297               GParamSpec *pspec)
298 {
299         ModestMsgView *msg_view = MODEST_MSG_VIEW (object);
300
301         switch (prop_id) {
302         case PROP_HADJUSTMENT:
303                 modest_msg_view_set_hadjustment (msg_view, g_value_get_object (value));
304                 break;
305         case PROP_VADJUSTMENT:
306                 modest_msg_view_set_vadjustment (msg_view, g_value_get_object (value));
307                 break;
308         case PROP_SHADOW_TYPE:
309                 modest_msg_view_set_shadow_type (msg_view, g_value_get_enum (value));
310                 break;
311         default:
312                 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
313                 break;
314         }
315 }
316
317 static void
318 get_property (GObject *object, 
319               guint prop_id, 
320               GValue *value, 
321               GParamSpec *pspec)
322 {
323         ModestMsgView *msg_view = MODEST_MSG_VIEW (object);
324         ModestMsgViewPrivate *priv = MODEST_MSG_VIEW_GET_PRIVATE (msg_view);
325
326         switch (prop_id) {
327         case PROP_HADJUSTMENT:
328                 g_value_set_object (value, priv->hadj);
329                 break;
330         case PROP_VADJUSTMENT:
331                 g_value_set_object (value, priv->vadj);
332                 break;
333         case PROP_SHADOW_TYPE:
334                 g_value_set_enum (value, priv->shadow_type);
335                 break;
336         default:
337                 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
338                 break;
339         }
340 }
341
342 static void
343 disconnect_hadjustment (ModestMsgView *msg_view)
344 {
345         ModestMsgViewPrivate *priv = MODEST_MSG_VIEW_GET_PRIVATE (msg_view);
346
347         if (priv->hadj) {
348                 g_signal_handlers_disconnect_by_func(priv->hadj, adjustment_value_changed, msg_view);
349                 g_object_unref (priv->hadj);
350                 priv->hadj = NULL;
351         }
352 }
353
354 static void
355 disconnect_vadjustment (ModestMsgView *msg_view)
356 {
357         ModestMsgViewPrivate *priv = MODEST_MSG_VIEW_GET_PRIVATE (msg_view);
358
359         if (priv->vadj) {
360                 g_signal_handlers_disconnect_by_func(priv->vadj, adjustment_value_changed, msg_view);
361                 g_object_unref (priv->vadj);
362                 priv->vadj = NULL;
363         }
364 }
365
366 static void 
367 get_view_allocation (ModestMsgView *msg_view, GtkAllocation *allocation)
368 {
369         /* This method gets the allocation of the widget in parent widget. It's the
370            real position and size of the widget */
371         ModestMsgViewPrivate *priv = MODEST_MSG_VIEW_GET_PRIVATE (msg_view);
372         
373         allocation->x = 0;
374         allocation->y = 0;
375
376         if (priv->shadow_type != GTK_SHADOW_NONE) {
377                 allocation->x = GTK_WIDGET (msg_view)->style->xthickness;
378                 allocation->y = GTK_WIDGET (msg_view)->style->ythickness;
379         }
380
381         allocation->width = MAX (1, (GTK_WIDGET (msg_view)->allocation.width) - allocation->x * 2);
382         allocation->height = MAX (1, (GTK_WIDGET (msg_view)->allocation.height) - allocation->y * 2);
383
384 }
385
386 static void 
387 reclamp_adjustment (GtkAdjustment *adj, 
388                     gboolean *value_changed)
389 {
390         gdouble value = adj->value;
391
392         /* Correct value to be inside the expected values of a scroll */
393
394         value = CLAMP (value, 0, adj->upper - adj->page_size);
395
396         if (value != adj->value) {
397                 adj->value = value;
398                 if (value_changed)
399                         *value_changed = TRUE;
400         } else if (value_changed) {
401                 *value_changed = FALSE;
402         }
403 }
404
405 static void 
406 set_hadjustment_values (ModestMsgView *msg_view,
407                         gboolean *value_changed)
408 {
409         GtkAllocation view_allocation;
410         GtkAdjustment *hadj = modest_msg_view_get_hadjustment (msg_view);
411
412         get_view_allocation (msg_view, &view_allocation);
413         hadj->page_size = view_allocation.width;
414         hadj->step_increment = view_allocation.width * 0.1;
415         hadj->page_increment = view_allocation.width * 0.9;
416
417         hadj->lower = 0;
418         hadj->upper = view_allocation.width;
419
420         reclamp_adjustment (hadj, value_changed);
421
422 }
423
424 static void 
425 set_vadjustment_values (ModestMsgView *msg_view,
426                         gboolean *value_changed)
427 {
428         GtkAllocation view_allocation;
429         GtkAdjustment *vadj = modest_msg_view_get_vadjustment (msg_view);
430         ModestMsgViewPrivate *priv = MODEST_MSG_VIEW_GET_PRIVATE (msg_view);
431         gint full_height = 0;
432         GtkAdjustment *html_vadj;
433
434         get_view_allocation (msg_view, &view_allocation);
435         vadj->page_size = view_allocation.height;
436         vadj->step_increment = view_allocation.height * 0.1;
437         vadj->page_increment = view_allocation.height * 0.9;
438
439         vadj->lower = 0;
440
441         if (priv->headers_box && GTK_WIDGET_VISIBLE(priv->headers_box)) {
442                 GtkRequisition child_requisition;
443
444                 gtk_widget_get_child_requisition (priv->headers_box, &child_requisition);
445                 full_height = child_requisition.height;
446         } else {
447                 full_height = 0;
448         }
449         
450         /* Get the real height of the embedded html */
451         if (priv->html_scroll && GTK_WIDGET_VISIBLE(priv->html_scroll)) {
452                 html_vadj = gtk_scrolled_window_get_vadjustment (GTK_SCROLLED_WINDOW (priv->html_scroll));
453                 full_height += html_vadj->upper;
454         }
455
456         vadj->upper = MAX (view_allocation.height, full_height);
457
458         reclamp_adjustment (vadj, value_changed);
459
460 }
461
462 static void
463 set_scroll_adjustments (ModestMsgView *msg_view,
464                         GtkAdjustment *hadj,
465                         GtkAdjustment *vadj)
466 {
467         ModestMsgViewPrivate *priv = MODEST_MSG_VIEW_GET_PRIVATE (msg_view);
468         modest_msg_view_set_hadjustment (msg_view, hadj);
469         modest_msg_view_set_vadjustment (msg_view, vadj);
470
471         gtk_container_set_focus_vadjustment (GTK_CONTAINER (priv->gtkhtml), vadj);
472 }
473
474 static void
475 realize (GtkWidget *widget)
476 {
477         ModestMsgView *msg_view = MODEST_MSG_VIEW (widget);
478         ModestMsgViewPrivate *priv = MODEST_MSG_VIEW_GET_PRIVATE (msg_view);
479         GtkAdjustment *hadj = modest_msg_view_get_hadjustment (msg_view);
480         GtkAdjustment *vadj = modest_msg_view_get_vadjustment (msg_view);
481         GdkWindowAttr attributes;
482         gint event_mask;
483         gint attributes_mask;
484         GtkAllocation view_allocation;
485
486         GTK_WIDGET_SET_FLAGS (widget, GTK_REALIZED);
487
488         /* The structure of the GdkWindow's is:
489          *    * widget->window: the shown gdkwindow embedding all the stuff inside
490          *    * view_window: a backing store gdkwindow containing the headers and contents
491          *      being scrolled. This window should have all the visible and non visible
492          *      widgets inside.
493          *    * headers_window: gdk window for headers_box.
494          *    * html_window: gdk window for html_scroll (the scrolled window containing the
495          *      gtkhtml showing the contents of the mail).
496          */
497
498         attributes.x = widget->allocation.x;
499         attributes.y = widget->allocation.y;
500         attributes.width = widget->allocation.width;
501         attributes.height = widget->allocation.height;
502         attributes.window_type = GDK_WINDOW_CHILD;
503         attributes.wclass = GDK_INPUT_OUTPUT;
504         attributes.visual = gtk_widget_get_visual (widget);
505         attributes.colormap = gtk_widget_get_colormap (widget);
506
507         event_mask = gtk_widget_get_events (widget) | GDK_EXPOSURE_MASK;
508         attributes.event_mask = event_mask | GDK_BUTTON_PRESS_MASK;
509         attributes_mask = GDK_WA_X | GDK_WA_Y | GDK_WA_VISUAL | GDK_WA_COLORMAP;
510
511         widget->window = gdk_window_new (gtk_widget_get_parent_window (widget),
512                                          &attributes, attributes_mask);
513         gdk_window_set_user_data (widget->window, msg_view);
514
515         get_view_allocation (msg_view, &view_allocation);
516
517         attributes.x = view_allocation.x;
518         attributes.y = view_allocation.y;
519         attributes.width = view_allocation.width;
520         attributes.height = view_allocation.height;
521         attributes.event_mask = 0;
522         priv->view_window = gdk_window_new (widget->window, &attributes, attributes_mask);
523         gdk_window_set_user_data (priv->view_window, msg_view);
524         gdk_window_set_back_pixmap (priv->view_window, NULL, FALSE);
525
526         attributes.x = -hadj->value;
527         attributes.y = -vadj->value;
528         attributes.width = hadj->upper;
529         if (priv->headers_box)
530                 attributes.height = GTK_WIDGET (priv->headers_box)->allocation.height;
531         else
532                 attributes.height = 1;
533         attributes.event_mask = event_mask;
534
535         priv->headers_window = gdk_window_new (priv->view_window, &attributes, attributes_mask);
536         gdk_window_set_user_data (priv->headers_window, msg_view);
537
538         if (priv->headers_box)
539                 gtk_widget_set_parent_window (priv->headers_box, priv->headers_window);
540
541         attributes.x = -hadj->value;
542         if (priv->headers_box)
543                 attributes.y = GTK_WIDGET (priv->headers_box)->allocation.height - vadj->value;
544         else 
545                 attributes.y = -vadj->value;
546         attributes.width = hadj->upper;
547         if (priv->headers_box)
548                 attributes.height = vadj->upper - GTK_WIDGET (priv->headers_box)->allocation.height;
549         else
550                 attributes.height = vadj->upper;
551         attributes.event_mask = event_mask;
552
553         priv->html_window = gdk_window_new (priv->view_window, &attributes, attributes_mask);
554         gdk_window_set_user_data (priv->html_window, msg_view);
555
556         if (priv->html_scroll)
557                 gtk_widget_set_parent_window (priv->html_scroll, priv->html_window);
558
559         widget->style = gtk_style_attach (widget->style, widget->window);
560         gtk_style_set_background (widget->style, widget->window, GTK_STATE_NORMAL);
561         gtk_style_set_background (widget->style, priv->headers_window, GTK_STATE_NORMAL);
562         gtk_style_set_background (widget->style, priv->html_window, GTK_STATE_NORMAL);
563
564         gtk_paint_flat_box(widget->style, priv->headers_window, GTK_STATE_NORMAL,
565                            GTK_SHADOW_NONE, 
566                            NULL, widget, "msgviewheaders",
567                            0,0,-1,-1);
568         gtk_paint_flat_box(widget->style, priv->html_window, GTK_STATE_NORMAL,
569                            GTK_SHADOW_NONE, 
570                            NULL, widget, "msgviewcontents",
571                            0,0,-1,-1);
572
573         gdk_window_show (priv->view_window);
574         gdk_window_show (priv->headers_window);
575         gdk_window_show (priv->html_window);
576
577 }
578
579 static void
580 unrealize (GtkWidget *widget)
581 {
582         ModestMsgView *msg_view = MODEST_MSG_VIEW (widget);
583         ModestMsgViewPrivate *priv = MODEST_MSG_VIEW_GET_PRIVATE (msg_view);
584
585         gdk_window_set_user_data (priv->view_window, NULL);
586         gdk_window_destroy (priv->view_window);
587         priv->view_window = NULL;
588
589         gdk_window_set_user_data (priv->headers_window, NULL);
590         gdk_window_destroy (priv->headers_window);
591         priv->headers_window = NULL;
592
593         gdk_window_set_user_data (priv->html_window, NULL);
594         gdk_window_destroy (priv->html_window);
595         priv->html_window = NULL;
596
597         if (GTK_WIDGET_CLASS (parent_class)->unrealize)
598                 ( * GTK_WIDGET_CLASS (parent_class)->unrealize) (widget);
599
600 }
601
602 static gint
603 expose (GtkWidget *widget, 
604         GdkEventExpose *event)
605 {
606         ModestMsgView *msg_view;
607         ModestMsgViewPrivate *priv;
608
609         if (GTK_WIDGET_DRAWABLE (widget)) {
610                 msg_view = MODEST_MSG_VIEW (widget);
611                 priv = MODEST_MSG_VIEW_GET_PRIVATE (msg_view);
612                 if (event->window == widget->window) {
613                         gtk_paint_shadow (widget->style, widget->window,
614                                           GTK_STATE_NORMAL, priv->shadow_type,
615                                           &event->area, widget, "msgview",
616                                           0,0,-1,-1);
617                 } else if (event->window == priv->headers_window) {
618                         gtk_paint_flat_box(widget->style, priv->headers_window, GTK_STATE_NORMAL,
619                                            GTK_SHADOW_NONE, 
620                                            &event->area, widget, "msgviewheaders",
621                                            0,0,-1,-1);
622                 } else if (event->window == priv->html_window) {
623                         gtk_paint_flat_box(widget->style, priv->html_window, GTK_STATE_NORMAL,
624                                            GTK_SHADOW_NONE, 
625                                            &event->area, widget, "msgviewcontents",
626                                            0,0,-1,-1);
627                 }
628                 if (priv->headers_box)
629                         gtk_container_propagate_expose (GTK_CONTAINER (msg_view), priv->headers_box, event);
630                 if (priv->html_scroll)
631                         gtk_container_propagate_expose (GTK_CONTAINER (msg_view), priv->html_scroll, event);
632                 (* GTK_WIDGET_CLASS (parent_class)->expose_event) (widget, event);
633         }
634
635         return FALSE;
636 }
637
638 static void 
639 forall (GtkContainer *container, 
640         gboolean include_internals,
641         GtkCallback callback,
642         gpointer userdata)
643 {
644         ModestMsgViewPrivate *priv = MODEST_MSG_VIEW_GET_PRIVATE (container);
645         g_return_if_fail (callback != NULL);
646
647         if (priv->headers_box)
648                 (*callback) (priv->headers_box, userdata);
649         if (priv->html_scroll)
650                 (*callback) (priv->html_scroll, userdata);
651 }
652
653 static void
654 container_remove (GtkContainer *container,
655                   GtkWidget *widget)
656 {
657         gboolean was_visible = FALSE;
658         ModestMsgViewPrivate *priv = MODEST_MSG_VIEW_GET_PRIVATE (container);
659         was_visible = GTK_WIDGET_VISIBLE (widget);
660         if (widget == priv->headers_box) {
661                 gtk_widget_unparent (priv->headers_box);
662                 priv->headers_box = NULL;
663         } else if (widget == priv->html_scroll) {
664                 gtk_widget_unparent (priv->html_scroll);
665                 priv->html_scroll = NULL;
666         } else {
667                 return;
668         }
669         if (was_visible)
670                 gtk_widget_queue_resize (GTK_WIDGET(container));
671
672 }
673
674 static void
675 size_request (GtkWidget *widget,
676               GtkRequisition *req)
677 {
678         ModestMsgViewPrivate *priv = MODEST_MSG_VIEW_GET_PRIVATE (widget);
679         GtkRequisition child_req;
680
681         req->width = 0;
682         req->height = 0;
683
684         gtk_widget_size_request (priv->headers_box, &child_req);
685         req->width = child_req.width;
686         req->height += child_req.height;
687         gtk_widget_size_request (priv->html_scroll, &child_req);
688         req->width = MAX (child_req.width, req->width);
689         req->height += child_req.height;
690
691 }
692
693 static void
694 size_allocate (GtkWidget *widget,
695                GtkAllocation *allocation)
696 {
697         ModestMsgView *msg_view = MODEST_MSG_VIEW (widget);
698         ModestMsgViewPrivate *priv = MODEST_MSG_VIEW_GET_PRIVATE (msg_view);
699         gboolean hadj_value_changed, vadj_value_changed;
700         GtkAllocation headers_allocation, html_allocation, view_allocation;
701         GtkAdjustment *html_vadj;
702
703         if (GTK_WIDGET_MAPPED (widget) &&
704             priv->shadow_type != GTK_SHADOW_NONE && 
705             (allocation->width != widget->allocation.width ||
706              allocation->height != widget->allocation.height))
707                 gdk_window_invalidate_rect (widget->window, NULL, FALSE);
708
709         widget->allocation = *allocation;
710         set_hadjustment_values (msg_view, &hadj_value_changed);
711         set_vadjustment_values (msg_view, &vadj_value_changed);
712
713         get_view_allocation (msg_view, &view_allocation);
714
715         headers_allocation.x = 0;
716         headers_allocation.y = 0;
717         headers_allocation.width = view_allocation.width;
718         if (priv->headers_box)
719                 headers_allocation.height = GTK_WIDGET (priv->headers_box)->requisition.height;
720         else
721                 headers_allocation.height = 0;
722
723         html_vadj = gtk_scrolled_window_get_vadjustment (GTK_SCROLLED_WINDOW (priv->html_scroll));
724
725         html_allocation.x = 0;
726         html_allocation.y = headers_allocation.height;
727         html_allocation.width = view_allocation.width;
728         html_allocation.height = MAX ((gint) html_vadj->upper, (gint)(priv->vadj->upper - headers_allocation.height));
729
730         if (GTK_WIDGET_REALIZED (widget)) {
731                 gdk_window_move_resize (widget->window,
732                                         allocation->x,
733                                         allocation->y,
734                                         allocation->width,
735                                         allocation->height);
736
737                 gdk_window_move_resize (priv->view_window,
738                                         view_allocation.x,
739                                         view_allocation.y,
740                                         view_allocation.width,
741                                         view_allocation.height);
742                 gdk_window_move_resize (priv->headers_window,
743                                         0,
744                                         (gint) (- priv->vadj->value),
745                                         allocation->width,
746                                         headers_allocation.height);
747                 gdk_window_move_resize (priv->html_window,
748                                         (gint) (- priv->hadj->value),
749                                         (gint) (html_allocation.y - priv->vadj->value),
750                                         (gint) priv->hadj->upper,
751                                         html_allocation.height);
752         }
753
754         if (priv->headers_box && GTK_WIDGET_VISIBLE (priv->headers_box)) {
755                 gtk_widget_size_allocate (priv->headers_box, &headers_allocation);
756         }
757         if (priv->html_scroll && GTK_WIDGET_VISIBLE (priv->html_scroll)) {
758                 html_allocation.x = 0;
759                 html_allocation.y = 0;
760                 html_allocation.width = (gint) priv->hadj->upper;
761                 html_allocation.height = (gint) priv->vadj->upper - headers_allocation.height;
762                 gtk_widget_size_allocate (priv->html_scroll, &html_allocation);
763         }
764         gtk_adjustment_changed (priv->hadj);
765         gtk_adjustment_changed (priv->vadj);
766
767         if (hadj_value_changed)
768                 gtk_adjustment_value_changed (priv->hadj);
769         if (vadj_value_changed)
770                 gtk_adjustment_value_changed (priv->vadj);
771
772 }
773
774 static void 
775 adjustment_value_changed (GtkAdjustment *adj, gpointer data)
776 {
777         ModestMsgView *msg_view = NULL;
778         ModestMsgViewPrivate *priv = NULL;
779
780         g_return_if_fail (GTK_IS_ADJUSTMENT (adj));
781         g_return_if_fail (MODEST_IS_MSG_VIEW (data));
782
783         msg_view = MODEST_MSG_VIEW (data);
784         priv = MODEST_MSG_VIEW_GET_PRIVATE (msg_view);
785
786         if (GTK_WIDGET_REALIZED (msg_view)) {
787                 GtkAdjustment *hadj = modest_msg_view_get_hadjustment (msg_view);
788                 GtkAdjustment *vadj = modest_msg_view_get_vadjustment (msg_view);
789                 gint headers_offset = 0;
790
791                 gtk_widget_queue_resize (priv->html_scroll);
792
793                 if (priv->headers_box && GTK_WIDGET_VISIBLE (priv->headers_box)) {
794                         gint old_x, old_y;
795                         gint new_x, new_y;
796                         gdk_window_get_position (priv->headers_window, &old_x, &old_y);
797                         new_x = 0;
798                         new_y = -vadj->value;
799
800                         headers_offset = GTK_WIDGET(priv->headers_box)->allocation.height;
801
802                         if (new_x != old_x || new_y != old_y) {
803                                 gdk_window_move (priv->headers_window, new_x, new_y);
804                                 gdk_window_process_updates (priv->headers_window, TRUE);
805                         }
806                 }
807                 
808                 if (priv->html_scroll && GTK_WIDGET_VISIBLE (priv->html_scroll)) {
809                         gint old_x, old_y;
810                         gint new_x, new_y;
811                         gdk_window_get_position (priv->html_window, &old_x, &old_y);
812                         new_x = -hadj->value;
813                         new_y = headers_offset - vadj->value;
814
815                         if (new_x != old_x || new_y != old_y) {
816                                 gdk_window_move (priv->html_window, new_x, new_y);
817                                 gdk_window_process_updates (priv->html_window, TRUE);
818                         }
819                 }
820                 
821         }
822 }
823
824 static void
825 html_adjustment_changed (GtkAdjustment *adj,
826                          gpointer userdata)
827 {
828         ModestMsgView *msg_view = MODEST_MSG_VIEW (userdata);
829         ModestMsgViewPrivate *priv = MODEST_MSG_VIEW_GET_PRIVATE (msg_view);
830         GtkAdjustment *html_vadj = gtk_scrolled_window_get_vadjustment (GTK_SCROLLED_WINDOW(priv->html_scroll));
831         gboolean vadj_changed;
832         gint new_height;
833
834         priv->html_scroll->requisition.height = html_vadj->upper;
835         priv->html_scroll->allocation.height = html_vadj->upper;
836
837         set_vadjustment_values (msg_view, &vadj_changed);
838
839         new_height = MAX ((gint) html_vadj->upper, (gint) (priv->vadj->upper - priv->headers_box->allocation.height));
840         
841         gtk_adjustment_changed (priv->vadj);
842         if (GTK_WIDGET_DRAWABLE (priv->html_scroll)) {
843                 gdk_window_resize (priv->html_window, (gint) priv->hadj->upper, (gint) new_height);
844                 gdk_window_process_updates (priv->view_window, TRUE);
845                 gtk_container_resize_children (GTK_CONTAINER (msg_view));
846         }
847         
848 }
849
850 static void
851 modest_msg_view_init (ModestMsgView *obj)
852 {
853         ModestMsgViewPrivate *priv;
854         GtkAdjustment *html_vadj;
855
856         GTK_WIDGET_UNSET_FLAGS (obj, GTK_NO_WINDOW);
857         gtk_widget_set_redraw_on_allocate (GTK_WIDGET (obj), TRUE);
858         gtk_container_set_reallocate_redraws (GTK_CONTAINER (obj), TRUE);
859         gtk_container_set_resize_mode (GTK_CONTAINER (obj), GTK_RESIZE_QUEUE);
860         
861         priv = MODEST_MSG_VIEW_GET_PRIVATE(obj);
862
863         priv->current_zoom = 1.0;
864         priv->priority_flags = 0;
865
866         priv->hadj = NULL;
867         priv->vadj = NULL;
868         priv->shadow_type = GTK_SHADOW_IN;
869         priv->view_window = NULL;
870         priv->headers_window = NULL;
871         priv->html_window = NULL;
872
873         gtk_widget_push_composite_child ();
874         priv->html_scroll = gtk_scrolled_window_new (NULL, NULL);
875         gtk_widget_set_composite_name (priv->html_scroll, "contents");
876         gtk_widget_pop_composite_child ();
877         gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (priv->html_scroll), GTK_POLICY_NEVER, GTK_POLICY_NEVER);
878
879         priv->msg                     = NULL;
880
881         priv->gtkhtml                 = gtk_html_new();
882         gtk_html_set_editable        (GTK_HTML(priv->gtkhtml), FALSE);
883         gtk_html_allow_selection     (GTK_HTML(priv->gtkhtml), TRUE);
884         gtk_html_set_caret_mode      (GTK_HTML(priv->gtkhtml), FALSE);
885         gtk_html_set_blocking        (GTK_HTML(priv->gtkhtml), FALSE);
886         gtk_html_set_images_blocking (GTK_HTML(priv->gtkhtml), FALSE);
887
888         priv->mail_header_view        = GTK_WIDGET(modest_mail_header_view_new (TRUE));
889         gtk_widget_set_no_show_all (priv->mail_header_view, TRUE);
890
891         priv->attachments_view        = GTK_WIDGET(modest_attachments_view_new (NULL));
892
893         priv->sig1 = g_signal_connect (G_OBJECT(priv->gtkhtml), "link_clicked",
894                                        G_CALLBACK(on_link_clicked), obj);
895         priv->sig2 = g_signal_connect (G_OBJECT(priv->gtkhtml), "url_requested",
896                                        G_CALLBACK(on_url_requested), obj);
897         priv->sig3 = g_signal_connect (G_OBJECT(priv->gtkhtml), "on_url",
898                                        G_CALLBACK(on_link_hover), obj);
899
900         g_signal_connect (G_OBJECT (priv->mail_header_view), "recpt-activated", 
901                           G_CALLBACK (on_recpt_activated), obj);
902
903         g_signal_connect (G_OBJECT (priv->attachments_view), "activate",
904                           G_CALLBACK (on_attachment_activated), obj);
905
906         html_vadj = gtk_scrolled_window_get_vadjustment (GTK_SCROLLED_WINDOW(priv->html_scroll));
907
908         g_signal_connect (G_OBJECT (html_vadj), "changed",
909                           G_CALLBACK (html_adjustment_changed), obj);
910
911 }
912         
913
914 static void
915 modest_msg_view_finalize (GObject *obj)
916 {       
917         ModestMsgViewPrivate *priv;
918         priv = MODEST_MSG_VIEW_GET_PRIVATE (obj);
919
920         if (priv->msg) {
921                 g_object_unref (G_OBJECT(priv->msg));
922                 priv->msg = NULL;
923         }
924         
925         /* we cannot disconnect sigs, because priv->gtkhtml is
926          * already dead */
927         
928         disconnect_vadjustment (MODEST_MSG_VIEW(obj));
929         disconnect_hadjustment (MODEST_MSG_VIEW(obj));
930
931         priv->gtkhtml = NULL;
932         priv->attachments_view = NULL;
933
934         G_OBJECT_CLASS(parent_class)->finalize (obj);           
935 }
936
937 static void
938 modest_msg_view_destroy (GtkObject *obj)
939 {       
940         disconnect_vadjustment (MODEST_MSG_VIEW(obj));
941         disconnect_hadjustment (MODEST_MSG_VIEW(obj));
942
943         GTK_OBJECT_CLASS(parent_class)->destroy (obj);          
944 }
945
946 GtkAdjustment *
947 modest_msg_view_get_vadjustment (ModestMsgView *msg_view)
948 {
949         ModestMsgViewPrivate *priv = MODEST_MSG_VIEW_GET_PRIVATE (msg_view);
950
951         if (!priv->vadj)
952                 modest_msg_view_set_vadjustment (msg_view, NULL);
953
954         return priv->vadj;
955         
956 }
957
958 GtkAdjustment *
959 modest_msg_view_get_hadjustment (ModestMsgView *msg_view)
960 {
961         ModestMsgViewPrivate *priv = MODEST_MSG_VIEW_GET_PRIVATE (msg_view);
962
963         if (!priv->hadj)
964                 modest_msg_view_set_hadjustment (msg_view, NULL);
965
966         return priv->hadj;
967         
968 }
969
970 void
971 modest_msg_view_set_hadjustment (ModestMsgView *msg_view, GtkAdjustment *hadj)
972 {
973         ModestMsgViewPrivate *priv = MODEST_MSG_VIEW_GET_PRIVATE (msg_view);
974         gboolean value_changed;
975
976         if (hadj && hadj == priv->hadj)
977                 return;
978
979         if (!hadj)
980                 hadj = GTK_ADJUSTMENT (gtk_adjustment_new (0.0,0.0,0.0,0.0,0.0,0.0));
981         disconnect_hadjustment (msg_view);
982         g_object_ref (G_OBJECT (hadj));
983         gtk_object_sink (GTK_OBJECT (hadj));
984         priv->hadj = hadj;
985         set_hadjustment_values (msg_view, &value_changed);
986
987         g_signal_connect (hadj, "value_changed", G_CALLBACK (adjustment_value_changed),
988                           msg_view);
989
990         gtk_adjustment_changed (hadj);
991         if (value_changed)
992                 gtk_adjustment_value_changed (hadj);
993         else
994                 adjustment_value_changed (hadj, msg_view);
995
996         g_object_notify (G_OBJECT (msg_view), "hadjustment");
997 }
998
999 void
1000 modest_msg_view_set_vadjustment (ModestMsgView *msg_view, GtkAdjustment *vadj)
1001 {
1002         ModestMsgViewPrivate *priv = MODEST_MSG_VIEW_GET_PRIVATE (msg_view);
1003         gboolean value_changed;
1004
1005         if (vadj && vadj == priv->vadj)
1006                 return;
1007
1008         if (!vadj)
1009                 vadj = (GtkAdjustment *) gtk_adjustment_new (0.0,0.0,0.0,0.0,0.0,0.0);
1010         disconnect_vadjustment (msg_view);
1011         g_object_ref (G_OBJECT (vadj));
1012         gtk_object_sink (GTK_OBJECT (vadj));
1013         priv->vadj = vadj;
1014         set_vadjustment_values (msg_view, &value_changed);
1015
1016         g_signal_connect (vadj, "value_changed", G_CALLBACK (adjustment_value_changed),
1017                           msg_view);
1018
1019         gtk_adjustment_changed (vadj);
1020         if (value_changed)
1021                 gtk_adjustment_value_changed (vadj);
1022         else
1023                 adjustment_value_changed (vadj, msg_view);
1024
1025         g_object_notify (G_OBJECT (msg_view), "vadjustment");
1026 }
1027
1028 /** 
1029  * modest_msg_view_set_shadow_type:
1030  * @msg_view: a #ModestMsgView.
1031  * @shadow_type: new shadow type.
1032  *
1033  * Sets a shadow type of the message view.
1034  **/ 
1035 void
1036 modest_msg_view_set_shadow_type (ModestMsgView *msg_view,
1037                                  GtkShadowType shadow_type)
1038 {
1039         ModestMsgViewPrivate *priv;
1040         g_return_if_fail (MODEST_IS_MSG_VIEW (msg_view));
1041
1042         priv = MODEST_MSG_VIEW_GET_PRIVATE (msg_view);
1043         
1044         if (priv->shadow_type != shadow_type) {
1045                 priv->shadow_type = shadow_type;
1046                 
1047                 if (GTK_WIDGET_VISIBLE (msg_view)) {
1048                         gtk_widget_size_allocate (GTK_WIDGET (msg_view), &(GTK_WIDGET (msg_view)->allocation));
1049                         gtk_widget_queue_draw (GTK_WIDGET (msg_view));
1050                 }
1051                 g_object_notify (G_OBJECT (msg_view), "shadow-type");
1052         }
1053 }
1054
1055 /**
1056  * modest_msg_view_get_shadow_type:
1057  * @msg_view: a #ModestMsgView
1058  *
1059  * Gets the current shadow type of the #ModestMsgView.
1060  *
1061  * Return value: the shadow type 
1062  **/
1063 GtkShadowType
1064 modest_msg_view_get_shadow_type (ModestMsgView *msg_view)
1065 {
1066         ModestMsgViewPrivate *priv;
1067         g_return_val_if_fail (MODEST_IS_MSG_VIEW (msg_view), GTK_SHADOW_NONE);
1068         priv = MODEST_MSG_VIEW_GET_PRIVATE (msg_view);
1069         
1070         return priv->shadow_type;
1071 }
1072
1073 GtkWidget*
1074 modest_msg_view_new (TnyMsg *msg)
1075 {
1076         GObject *obj;
1077         ModestMsgView* self;
1078         ModestMsgViewPrivate *priv;
1079         GtkWidget *separator;
1080         
1081         obj  = G_OBJECT(g_object_new(MODEST_TYPE_MSG_VIEW, NULL));
1082         self = MODEST_MSG_VIEW(obj);
1083         priv = MODEST_MSG_VIEW_GET_PRIVATE (self);
1084
1085         gtk_widget_push_composite_child ();
1086         priv->headers_box = gtk_vbox_new (0, FALSE);
1087         gtk_widget_set_composite_name (priv->headers_box, "headers");
1088         gtk_widget_pop_composite_child ();
1089
1090         if (priv->mail_header_view)
1091                 gtk_box_pack_start (GTK_BOX(priv->headers_box), priv->mail_header_view, FALSE, FALSE, 0);
1092         
1093         if (priv->attachments_view) {
1094                 priv->attachments_box = (GtkWidget *) modest_mail_header_view_add_custom_header (MODEST_MAIL_HEADER_VIEW (priv->mail_header_view),
1095                                                                                                  _("Attachments:"), priv->attachments_view,
1096                                                                                                  FALSE, FALSE);
1097                 gtk_widget_hide_all (priv->attachments_box);
1098 /*              gtk_widget_set_no_show_all (priv->attachments_box, TRUE); */
1099         }
1100
1101         separator = gtk_hseparator_new ();
1102         gtk_box_pack_start (GTK_BOX(priv->headers_box), separator, FALSE, FALSE, 0);
1103
1104         gtk_widget_set_parent (priv->headers_box, GTK_WIDGET (self));
1105
1106         if (priv->gtkhtml) {
1107                 gtk_container_add (GTK_CONTAINER (priv->html_scroll), priv->gtkhtml);
1108                 gtk_widget_set_parent (priv->html_scroll, GTK_WIDGET(self));
1109 #ifdef MAEMO_CHANGES
1110                 gtk_widget_tap_and_hold_setup (GTK_WIDGET (priv->gtkhtml), NULL, NULL, 0);
1111                 g_signal_connect (G_OBJECT (priv->gtkhtml), "tap-and-hold", G_CALLBACK (on_tap_and_hold), obj);
1112 #endif
1113         }
1114
1115         modest_msg_view_set_message (self, msg);
1116
1117         return GTK_WIDGET(self);
1118 }
1119
1120 #ifdef MAEMO_CHANGES
1121 static void
1122 on_tap_and_hold (GtkWidget *widget,
1123                  gpointer data)
1124 {
1125         ModestMsgView *msg_view = (ModestMsgView *) data;
1126         ModestMsgViewPrivate *priv = MODEST_MSG_VIEW_GET_PRIVATE (msg_view);
1127
1128         g_signal_emit (G_OBJECT (msg_view), signals[LINK_CONTEXTUAL_SIGNAL],
1129                        0, priv->last_url);
1130 }
1131 #endif
1132
1133 static void
1134 on_recpt_activated (ModestMailHeaderView *header_view, 
1135                     const gchar *address,
1136                     ModestMsgView * view)
1137 {
1138         g_signal_emit (G_OBJECT (view), signals[RECPT_ACTIVATED_SIGNAL], 0, address);
1139 }
1140
1141 static void
1142 on_attachment_activated (ModestAttachmentsView * att_view, TnyMimePart *mime_part, gpointer msg_view)
1143 {
1144
1145         g_signal_emit (G_OBJECT(msg_view), signals[ATTACHMENT_CLICKED_SIGNAL],
1146                        0, mime_part);
1147 }
1148
1149 static gboolean
1150 on_link_clicked (GtkWidget *widget, const gchar *uri, ModestMsgView *msg_view)
1151 {
1152         g_return_val_if_fail (msg_view, FALSE);
1153         
1154         g_signal_emit (G_OBJECT(msg_view), signals[LINK_CLICKED_SIGNAL],
1155                        0, uri);
1156
1157         return FALSE;
1158 }
1159
1160
1161 static gboolean
1162 on_link_hover (GtkWidget *widget, const gchar *uri, ModestMsgView *msg_view)
1163 {
1164         ModestMsgViewPrivate *priv = MODEST_MSG_VIEW_GET_PRIVATE (msg_view);
1165
1166         g_free (priv->last_url);
1167         priv->last_url = g_strdup (uri);
1168
1169         g_signal_emit (G_OBJECT(msg_view), signals[LINK_HOVER_SIGNAL],
1170                        0, uri);
1171
1172         return FALSE;
1173 }
1174
1175
1176
1177 static TnyMimePart *
1178 find_cid_image (TnyMsg *msg, const gchar *cid)
1179 {
1180         TnyMimePart *part = NULL;
1181         TnyList *parts;
1182         TnyIterator *iter;
1183         
1184         g_return_val_if_fail (msg, NULL);
1185         g_return_val_if_fail (cid, NULL);
1186         
1187         parts  = TNY_LIST (tny_simple_list_new());
1188
1189         tny_mime_part_get_parts (TNY_MIME_PART (msg), parts); 
1190         iter   = tny_list_create_iterator (parts);
1191         
1192         while (!tny_iterator_is_done(iter)) {
1193                 const gchar *part_cid;
1194                 part = TNY_MIME_PART(tny_iterator_get_current(iter));
1195                 part_cid = tny_mime_part_get_content_id (part);
1196
1197                 if (part_cid && strcmp (cid, part_cid) == 0)
1198                         break;
1199
1200                 if (part)
1201                         g_object_unref (G_OBJECT(part));
1202         
1203                 part = NULL;
1204                 tny_iterator_next (iter);
1205         }
1206         
1207         g_object_unref (G_OBJECT(iter));        
1208         g_object_unref (G_OBJECT(parts));
1209         
1210         return part;
1211 }
1212
1213
1214 static gboolean
1215 on_url_requested (GtkWidget *widget, const gchar *uri,
1216                   GtkHTMLStream *stream, ModestMsgView *msg_view)
1217 {
1218         ModestMsgViewPrivate *priv;
1219         priv = MODEST_MSG_VIEW_GET_PRIVATE (msg_view);
1220         
1221         if (g_str_has_prefix (uri, "cid:")) {
1222                 /* +4 ==> skip "cid:" */
1223                 TnyMimePart *part = find_cid_image (priv->msg, uri + 4);
1224                 if (!part) {
1225                         g_printerr ("modest: '%s' not found\n", uri + 4);
1226                         gtk_html_stream_close (stream, GTK_HTML_STREAM_ERROR);
1227                 } else {
1228                         TnyStream *tny_stream =
1229                                 TNY_STREAM(modest_tny_stream_gtkhtml_new(stream));
1230                         tny_mime_part_decode_to_stream ((TnyMimePart*)part,
1231                                                                   tny_stream);
1232                         gtk_html_stream_close (stream, GTK_HTML_STREAM_OK);
1233         
1234                         g_object_unref (G_OBJECT(tny_stream));
1235                         g_object_unref (G_OBJECT(part));
1236                 }
1237         }
1238
1239         return TRUE;
1240 }
1241
1242 static gboolean
1243 set_html_message (ModestMsgView *self, TnyMimePart *tny_body, TnyMsg *msg)
1244 {
1245         GtkHTMLStream *gtkhtml_stream;
1246         TnyStream *tny_stream;  
1247         ModestMsgViewPrivate *priv;
1248         
1249         g_return_val_if_fail (self, FALSE);
1250         g_return_val_if_fail (tny_body, FALSE);
1251         
1252         priv = MODEST_MSG_VIEW_GET_PRIVATE(self);
1253
1254         gtkhtml_stream = gtk_html_begin(GTK_HTML(priv->gtkhtml));
1255
1256         tny_stream     = TNY_STREAM(modest_tny_stream_gtkhtml_new (gtkhtml_stream));
1257         tny_stream_reset (tny_stream);
1258
1259         tny_mime_part_decode_to_stream ((TnyMimePart*)tny_body, tny_stream);
1260         g_object_unref (G_OBJECT(tny_stream));
1261         
1262         gtk_html_stream_destroy (gtkhtml_stream);
1263         
1264         return TRUE;
1265 }
1266
1267
1268 /* FIXME: this is a hack --> we use the tny_text_buffer_stream to
1269  * get the message text, then write to gtkhtml 'by hand' */
1270 static gboolean
1271 set_text_message (ModestMsgView *self, TnyMimePart *tny_body, TnyMsg *msg)
1272 {
1273         GtkTextBuffer *buf;
1274         GtkTextIter begin, end;
1275         TnyStream* txt_stream, *tny_stream;
1276         GtkHTMLStream *gtkhtml_stream;
1277         gchar *txt;
1278         ModestMsgViewPrivate *priv;
1279                 
1280         g_return_val_if_fail (self, FALSE);
1281         g_return_val_if_fail (tny_body, FALSE);
1282
1283         priv           = MODEST_MSG_VIEW_GET_PRIVATE(self);
1284         
1285         buf            = gtk_text_buffer_new (NULL);
1286         txt_stream     = TNY_STREAM(tny_gtk_text_buffer_stream_new (buf));
1287                 
1288         tny_stream_reset (txt_stream);
1289
1290         gtkhtml_stream = gtk_html_begin(GTK_HTML(priv->gtkhtml)); 
1291         tny_stream =  TNY_STREAM(modest_tny_stream_gtkhtml_new (gtkhtml_stream));
1292         
1293         // FIXME: tinymail
1294         tny_mime_part_decode_to_stream ((TnyMimePart*)tny_body, txt_stream);
1295         tny_stream_reset (txt_stream);          
1296         
1297         gtk_text_buffer_get_bounds (buf, &begin, &end);
1298         txt = gtk_text_buffer_get_text (buf, &begin, &end, FALSE);
1299         if (txt) {
1300                 gchar *html = modest_text_utils_convert_to_html (txt);
1301                 tny_stream_write (tny_stream, html, strlen(html));
1302                 tny_stream_reset (tny_stream);
1303                 g_free (txt);
1304                 g_free (html);
1305         }
1306         
1307         g_object_unref (G_OBJECT(tny_stream));
1308         g_object_unref (G_OBJECT(txt_stream));
1309         g_object_unref (G_OBJECT(buf));
1310         
1311         gtk_html_stream_destroy (gtkhtml_stream);
1312         
1313         return TRUE;
1314 }
1315
1316
1317 static gboolean
1318 set_empty_message (ModestMsgView *self)
1319 {
1320         ModestMsgViewPrivate *priv;
1321         
1322         g_return_val_if_fail (self, FALSE);
1323         priv           = MODEST_MSG_VIEW_GET_PRIVATE(self);
1324
1325         gtk_html_load_from_string (GTK_HTML(priv->gtkhtml),
1326                                    "", 1);
1327
1328         
1329         return TRUE;
1330 }
1331
1332
1333 void
1334 modest_msg_view_set_message (ModestMsgView *self, TnyMsg *msg)
1335 {
1336         TnyMimePart *body;
1337         ModestMsgViewPrivate *priv;
1338         TnyHeader *header;
1339         GtkAdjustment *html_vadj;
1340         
1341         g_return_if_fail (self);
1342         
1343         priv = MODEST_MSG_VIEW_GET_PRIVATE(self);
1344         gtk_widget_set_no_show_all (priv->mail_header_view, FALSE);
1345
1346         if (msg != priv->msg) {
1347                 if (priv->msg)
1348                         g_object_unref (G_OBJECT(priv->msg));
1349                 if (msg)
1350                         g_object_ref   (G_OBJECT(msg));
1351                 priv->msg = msg;
1352         }
1353         
1354         if (!msg) {
1355                 tny_header_view_clear (TNY_HEADER_VIEW (priv->mail_header_view));
1356                 modest_attachments_view_set_message (MODEST_ATTACHMENTS_VIEW (priv->attachments_view), NULL);
1357                 gtk_widget_hide_all (priv->mail_header_view);
1358                 gtk_widget_hide_all (priv->attachments_box);
1359                 gtk_widget_set_no_show_all (priv->mail_header_view, TRUE);
1360                 set_empty_message (self);
1361                 gtk_widget_queue_resize (GTK_WIDGET(self));
1362                 gtk_widget_queue_draw (GTK_WIDGET(self));
1363                 return;
1364         }
1365
1366         header = tny_msg_get_header (msg);
1367         tny_header_view_set_header (TNY_HEADER_VIEW (priv->mail_header_view), header);
1368         g_object_unref (header);
1369
1370         modest_attachments_view_set_message (MODEST_ATTACHMENTS_VIEW(priv->attachments_view),
1371                                              msg);
1372         
1373         body = modest_tny_msg_find_body_part (msg, TRUE);
1374         if (body) {
1375                 if (tny_mime_part_content_type_is (body, "text/html"))
1376                         set_html_message (self, body, msg);
1377                 else
1378                         set_text_message (self, body, msg);
1379
1380                 if(modest_attachments_view_has_attachments (MODEST_ATTACHMENTS_VIEW (priv->attachments_view))) {
1381                         gtk_widget_show_all (priv->attachments_box);
1382                 } else {
1383                         gtk_widget_hide_all (priv->attachments_box);
1384                 }
1385
1386 /*              g_print ("---\nfilename %s\ncontent_location %s\ncontent_id%s\ncontent_type%s\n", */
1387 /*                       tny_mime_part_get_filename (body), */
1388 /*                       tny_mime_part_get_content_location (body), */
1389 /*                       tny_mime_part_get_content_id (body), */
1390 /*                       tny_mime_part_get_content_type (body)); */
1391                         
1392         } else 
1393                 set_empty_message (self);
1394
1395         gtk_widget_show (priv->gtkhtml);
1396         gtk_widget_set_no_show_all (priv->attachments_box, TRUE);
1397         gtk_widget_show_all (priv->mail_header_view);
1398         gtk_widget_set_no_show_all (priv->attachments_box, FALSE);
1399 /*      gtk_widget_show_all (priv->attachments_box); */
1400 /*      gtk_widget_show_all (priv->attachments_box); */
1401         gtk_widget_set_no_show_all (priv->mail_header_view, TRUE);
1402         gtk_widget_queue_resize (GTK_WIDGET(self));
1403         gtk_widget_queue_draw (GTK_WIDGET(self));
1404
1405         if (priv->hadj != NULL)
1406                 priv->hadj->value = 0.0;
1407         if (priv->vadj != NULL)
1408                 priv->vadj->value = 0.0;
1409
1410         html_vadj = gtk_scrolled_window_get_vadjustment (GTK_SCROLLED_WINDOW (priv->html_scroll));
1411
1412         g_signal_emit_by_name (G_OBJECT (html_vadj), "changed");
1413
1414         /* This is a hack to force reallocation of scroll after drawing all the stuff. This
1415          * makes the html view get the proper and expected size and prevent being able to scroll
1416          * the buffer when it shouldn't be scrollable */
1417         g_object_ref (self);
1418         g_timeout_add (250, (GSourceFunc) idle_readjust_scroll, self);
1419 }
1420
1421
1422 TnyMsg*
1423 modest_msg_view_get_message (ModestMsgView *self)
1424 {
1425         TnyMsg *msg;
1426
1427         g_return_val_if_fail (MODEST_IS_MSG_VIEW (self), NULL);
1428
1429         msg = MODEST_MSG_VIEW_GET_PRIVATE(self)->msg;
1430
1431         if (msg)
1432                 g_object_ref (msg);
1433         
1434         return msg;
1435 }
1436
1437 static gboolean
1438 has_contents_receiver (gpointer engine, const gchar *data,
1439                        size_t len, gboolean *has_contents)
1440 {
1441         if (len > 1 || ((len == 1)&&(data[0]!='\n'))) {
1442                 *has_contents = TRUE;
1443                 return FALSE;
1444         }
1445         return TRUE;
1446 }
1447
1448 gboolean 
1449 modest_msg_view_get_message_is_empty (ModestMsgView *self)
1450 {
1451         /* TODO: Find some gtkhtml API to check whether there is any (visible, non markup)
1452          * text in the message:
1453          */
1454         ModestMsgViewPrivate *priv = MODEST_MSG_VIEW_GET_PRIVATE (self);
1455         gboolean has_contents = FALSE;
1456
1457         gtk_html_export (GTK_HTML (priv->gtkhtml), "text/plain", 
1458                          (GtkHTMLSaveReceiverFn) has_contents_receiver, &has_contents);
1459         
1460         return !has_contents;
1461 }
1462
1463
1464 gboolean 
1465 modest_msg_view_search (ModestMsgView *self, const gchar *search)
1466 {
1467         ModestMsgViewPrivate *priv;
1468         gboolean result;
1469         GtkAdjustment *vadj, *tmp_vadj;
1470         gdouble y_offset;
1471
1472         g_return_val_if_fail (MODEST_IS_MSG_VIEW (self), FALSE);
1473
1474         priv = MODEST_MSG_VIEW_GET_PRIVATE (self);
1475         vadj = gtk_layout_get_vadjustment (GTK_LAYOUT (priv->gtkhtml));
1476         g_object_ref (vadj);
1477         tmp_vadj = GTK_ADJUSTMENT (gtk_adjustment_new (0.0, vadj->lower, vadj->upper, vadj->step_increment, 32.0, 32.0));
1478         gtk_layout_set_vadjustment (GTK_LAYOUT (priv->gtkhtml), tmp_vadj);
1479         result = gtk_html_engine_search (GTK_HTML (priv->gtkhtml),
1480                                          search,
1481                                          FALSE,   /* case sensitive */
1482                                          TRUE,    /* forward */
1483                                          FALSE);  /* regexp */
1484
1485 // wait for the updated gtkhtml (w27) to enable this
1486 #if 0
1487         if (result) {
1488                 gint x, y, w, h;
1489                 gdouble offset_top, offset_bottom;
1490                 GtkAdjustment *adj;
1491                 gtk_html_get_selection_area (GTK_HTML (priv->gtkhtml), &x, &y, &w, &h);
1492                 offset_top = (gdouble) (priv->headers_box->requisition.height + y);
1493                 offset_bottom = (gdouble) (priv->headers_box->requisition.height + y + h);
1494                 adj = GTK_ADJUSTMENT (priv->vadj);
1495                 if (offset_top < adj->value)
1496                         gtk_adjustment_set_value (adj, offset_top + adj->page_increment - adj->page_size);
1497                 else if (offset_bottom > adj->value + adj->page_increment)
1498                         gtk_adjustment_set_value (adj, offset_bottom - adj->page_increment);
1499         }
1500 #endif 
1501
1502         y_offset = tmp_vadj->value;
1503         gtk_layout_set_vadjustment (GTK_LAYOUT (priv->gtkhtml), vadj);
1504         g_object_unref (vadj);
1505
1506         return result;
1507 }
1508
1509 gboolean
1510 modest_msg_view_search_next (ModestMsgView *self)
1511 {
1512         ModestMsgViewPrivate *priv;
1513         gboolean result;
1514
1515         g_return_val_if_fail (MODEST_IS_MSG_VIEW (self), FALSE);
1516
1517         priv = MODEST_MSG_VIEW_GET_PRIVATE (self);
1518         result = gtk_html_engine_search_next (GTK_HTML (priv->gtkhtml));
1519
1520 // fixme wait for new gtkhtml
1521 #if 0
1522         if (result) {
1523                 gint x, y, w, h;
1524                 gdouble offset_top, offset_bottom;
1525                 GtkAdjustment *adj;
1526                 gtk_html_get_selection_area (GTK_HTML (priv->gtkhtml), &x, &y, &w, &h);
1527                 g_message ("SELECTION AREA x%d y%d w%d h%d", x, y, w, h);
1528                 offset_top = (gdouble) (priv->headers_box->requisition.height + y);
1529                 offset_bottom = (gdouble) (priv->headers_box->requisition.height + y + h);
1530                 adj = GTK_ADJUSTMENT (priv->vadj);
1531                 if (offset_top < adj->value)
1532                         gtk_adjustment_set_value (adj, offset_top + adj->page_increment - adj->page_size);
1533                 else if (offset_bottom > adj->value + adj->page_increment)
1534                         gtk_adjustment_set_value (adj, offset_bottom - adj->page_increment);
1535         }
1536 #endif
1537         return result;
1538 }
1539
1540 void
1541 modest_msg_view_set_zoom (ModestMsgView *self, gdouble zoom)
1542 {
1543         ModestMsgViewPrivate *priv;
1544
1545         g_return_if_fail (MODEST_IS_MSG_VIEW (self));
1546
1547         priv = MODEST_MSG_VIEW_GET_PRIVATE (self);
1548         priv->current_zoom = zoom;
1549         gtk_html_set_magnification (GTK_HTML(priv->gtkhtml), zoom);
1550
1551         gtk_widget_queue_resize (priv->gtkhtml);
1552 }
1553
1554 gdouble
1555 modest_msg_view_get_zoom (ModestMsgView *self)
1556 {
1557         ModestMsgViewPrivate *priv;
1558
1559         g_return_val_if_fail (MODEST_IS_MSG_VIEW (self), 1.0);
1560
1561         priv = MODEST_MSG_VIEW_GET_PRIVATE (self);
1562
1563         return priv->current_zoom;
1564 }
1565
1566 TnyHeaderFlags
1567 modest_msg_view_get_priority (ModestMsgView *self)
1568 {
1569         ModestMsgViewPrivate *priv;
1570
1571         g_return_val_if_fail (MODEST_IS_MSG_VIEW (self), 0);
1572
1573         priv = MODEST_MSG_VIEW_GET_PRIVATE (self);
1574
1575         return priv->priority_flags;
1576 }
1577
1578 void
1579 modest_msg_view_set_priority (ModestMsgView *self, TnyHeaderFlags flags)
1580 {
1581         ModestMsgViewPrivate *priv;
1582
1583         g_return_if_fail (MODEST_IS_MSG_VIEW (self));
1584
1585         priv = MODEST_MSG_VIEW_GET_PRIVATE (self);
1586
1587         priv->priority_flags = flags & (TNY_HEADER_FLAG_HIGH_PRIORITY);
1588
1589         modest_mail_header_view_set_priority (MODEST_MAIL_HEADER_VIEW (priv->mail_header_view), flags);
1590 }
1591
1592 GList *
1593 modest_msg_view_get_selected_attachments (ModestMsgView *self)
1594 {
1595         ModestMsgViewPrivate *priv;
1596
1597         g_return_val_if_fail (MODEST_IS_MSG_VIEW (self), NULL);
1598         priv = MODEST_MSG_VIEW_GET_PRIVATE (self);
1599
1600         return modest_attachments_view_get_selection (MODEST_ATTACHMENTS_VIEW (priv->attachments_view));
1601         
1602 }
1603
1604 GList *
1605 modest_msg_view_get_attachments (ModestMsgView *self)
1606 {
1607         ModestMsgViewPrivate *priv;
1608
1609         g_return_val_if_fail (MODEST_IS_MSG_VIEW (self), NULL);
1610         priv = MODEST_MSG_VIEW_GET_PRIVATE (self);
1611
1612         return modest_attachments_view_get_attachments (MODEST_ATTACHMENTS_VIEW (priv->attachments_view));
1613         
1614 }
1615
1616 void
1617 modest_msg_view_grab_focus (ModestMsgView *view)
1618 {
1619         ModestMsgViewPrivate *priv = NULL;
1620
1621         g_return_if_fail (MODEST_IS_MSG_VIEW (view));
1622         priv = MODEST_MSG_VIEW_GET_PRIVATE (view);
1623
1624         gtk_widget_grab_focus (priv->gtkhtml);
1625 }
1626
1627 void
1628 modest_msg_view_remove_attachment (ModestMsgView *view, TnyMimePart *attachment)
1629 {
1630         TnyMsg *msg;
1631         ModestMsgViewPrivate *priv;
1632
1633         g_return_if_fail (MODEST_IS_MSG_VIEW (view));
1634         g_return_if_fail (TNY_IS_MIME_PART (attachment));
1635         priv = MODEST_MSG_VIEW_GET_PRIVATE (view);
1636
1637         msg = modest_msg_view_get_message (view);
1638         modest_attachments_view_remove_attachment (MODEST_ATTACHMENTS_VIEW (priv->attachments_view),
1639                                                    attachment);
1640         
1641 }
1642
1643 gboolean
1644 idle_readjust_scroll (ModestMsgView *view)
1645 {
1646         if (GTK_WIDGET_DRAWABLE (view)) {
1647                 ModestMsgViewPrivate *priv = MODEST_MSG_VIEW_GET_PRIVATE (view);
1648                 GtkAdjustment *html_vadj;
1649                 html_vadj = gtk_scrolled_window_get_vadjustment (GTK_SCROLLED_WINDOW (priv->html_scroll));
1650                 html_vadj->page_size = html_vadj->upper;
1651                 gtk_adjustment_changed (html_vadj);
1652                 gtk_widget_queue_resize (GTK_WIDGET (view));
1653                 gtk_widget_queue_draw (GTK_WIDGET (view));
1654         }
1655         g_object_unref (G_OBJECT (view));
1656         return FALSE;
1657 }