Reverted again the gdk_threads_enter/leave sentences:
[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
90 /* GtkContainer methods */
91 static void forall (GtkContainer *container, gboolean include_internals,
92                     GtkCallback callback, gpointer userdata);
93 static void container_remove (GtkContainer *container, GtkWidget *widget);
94
95 /* list my signals */
96 enum {
97         LINK_CLICKED_SIGNAL,
98         LINK_HOVER_SIGNAL,
99         ATTACHMENT_CLICKED_SIGNAL,
100         RECPT_ACTIVATED_SIGNAL,
101         LINK_CONTEXTUAL_SIGNAL,
102         LAST_SIGNAL
103 };
104
105 /* list properties */
106 enum {
107         PROP_0,
108         PROP_HADJUSTMENT,
109         PROP_VADJUSTMENT,
110         PROP_SHADOW_TYPE
111 };
112
113 typedef struct _ModestMsgViewPrivate ModestMsgViewPrivate;
114 struct _ModestMsgViewPrivate {
115         GtkWidget   *gtkhtml;
116         GtkWidget   *mail_header_view;
117         GtkWidget   *attachments_view;
118
119         TnyMsg      *msg;
120
121         /* embedded elements */
122         GtkWidget   *headers_box;
123         GtkWidget   *html_scroll;
124         GtkWidget   *attachments_box;
125
126         /* internal adjustments for set_scroll_adjustments */
127         GtkAdjustment *hadj;
128         GtkAdjustment *vadj;
129         GtkShadowType shadow_type;
130
131         /* gdk windows for drawing */
132         GdkWindow *view_window;
133         GdkWindow *headers_window;
134         GdkWindow *html_window;
135
136         /* zoom */
137         gdouble current_zoom;
138
139         /* link click management */
140         gchar *last_url;
141
142         TnyHeaderFlags priority_flags;
143
144         gulong  sig1, sig2, sig3;
145 };
146 #define MODEST_MSG_VIEW_GET_PRIVATE(o)      (G_TYPE_INSTANCE_GET_PRIVATE((o), \
147                                                  MODEST_TYPE_MSG_VIEW, \
148                                                  ModestMsgViewPrivate))
149 /* globals */
150 static GtkContainerClass *parent_class = NULL;
151
152 /* uncomment the following if you have defined any signals */
153 static guint signals[LAST_SIGNAL] = {0};
154
155 GType
156 modest_msg_view_get_type (void)
157 {
158         static GType my_type = 0;
159         if (!my_type) {
160                 static const GTypeInfo my_info = {
161                         sizeof(ModestMsgViewClass),
162                         NULL,           /* base init */
163                         NULL,           /* base finalize */
164                         (GClassInitFunc) modest_msg_view_class_init,
165                         NULL,           /* class finalize */
166                         NULL,           /* class data */
167                         sizeof(ModestMsgView),
168                         1,              /* n_preallocs */
169                         (GInstanceInitFunc) modest_msg_view_init,
170                         NULL
171                 };
172                 my_type = g_type_register_static (GTK_TYPE_CONTAINER,
173                                                   "ModestMsgView",
174                                                   &my_info, 0);
175         }
176         return my_type;
177 }
178
179 static void
180 modest_msg_view_class_init (ModestMsgViewClass *klass)
181 {
182         GObjectClass *gobject_class;
183         GtkWidgetClass *widget_class;
184         GtkObjectClass *gtkobject_class;
185         GtkContainerClass *container_class;
186         gobject_class = (GObjectClass*) klass;
187         widget_class = (GtkWidgetClass *) klass;
188         gtkobject_class = (GtkObjectClass *) klass;
189         container_class = (GtkContainerClass *) klass;
190
191         parent_class            = g_type_class_peek_parent (klass);
192         gobject_class->finalize = modest_msg_view_finalize;
193         gobject_class->set_property = set_property;
194         gobject_class->get_property = get_property;
195         gtkobject_class->destroy = modest_msg_view_destroy;
196
197         widget_class->realize = realize;
198         widget_class->unrealize = unrealize;
199         widget_class->expose_event = expose;
200         widget_class->size_request = size_request;
201         widget_class->size_allocate = size_allocate;
202
203         container_class->forall = forall;
204         container_class->remove = container_remove;
205
206         klass->set_scroll_adjustments = set_scroll_adjustments;
207
208         g_type_class_add_private (gobject_class, sizeof(ModestMsgViewPrivate));
209
210         g_object_class_install_property (gobject_class,
211                                          PROP_HADJUSTMENT,
212                                          g_param_spec_object ("hadjustment", 
213                                                               _("Horizontal adjustment"),
214                                                               _("GtkAdjustment with information of the horizontal visible position"),
215                                                               GTK_TYPE_ADJUSTMENT,
216                                                               G_PARAM_READABLE | G_PARAM_WRITABLE | G_PARAM_CONSTRUCT));
217
218         g_object_class_install_property (gobject_class,
219                                          PROP_VADJUSTMENT,
220                                          g_param_spec_object ("vadjustment", 
221                                                               _("Vertical adjustment"),
222                                                               _("GtkAdjustment with information of the vertical visible position"),
223                                                               GTK_TYPE_ADJUSTMENT,
224                                                               G_PARAM_READABLE | G_PARAM_WRITABLE | G_PARAM_CONSTRUCT));
225
226         g_object_class_install_property (gobject_class,
227                                          PROP_SHADOW_TYPE,
228                                          g_param_spec_enum ("shadow_type", 
229                                                             _("Shadow type"),
230                                                             _("Kind of shadow that's shown around the view"),
231                                                             GTK_TYPE_SHADOW_TYPE,
232                                                             GTK_SHADOW_IN,
233                                                             G_PARAM_READABLE | G_PARAM_WRITABLE ));
234
235         signals[LINK_CLICKED_SIGNAL] =
236                 g_signal_new ("link_clicked",
237                               G_TYPE_FROM_CLASS (gobject_class),
238                               G_SIGNAL_RUN_FIRST,
239                               G_STRUCT_OFFSET(ModestMsgViewClass, link_clicked),
240                               NULL, NULL,
241                               g_cclosure_marshal_VOID__STRING,
242                               G_TYPE_NONE, 1, G_TYPE_STRING);
243         
244         signals[ATTACHMENT_CLICKED_SIGNAL] =
245                 g_signal_new ("attachment_clicked",
246                               G_TYPE_FROM_CLASS (gobject_class),
247                               G_SIGNAL_RUN_FIRST,
248                               G_STRUCT_OFFSET(ModestMsgViewClass, attachment_clicked),
249                               NULL, NULL,
250                               g_cclosure_marshal_VOID__OBJECT,
251                               G_TYPE_NONE, 1, G_TYPE_OBJECT);
252         
253         signals[LINK_HOVER_SIGNAL] =
254                 g_signal_new ("link_hover",
255                               G_TYPE_FROM_CLASS (gobject_class),
256                               G_SIGNAL_RUN_FIRST,
257                               G_STRUCT_OFFSET(ModestMsgViewClass, link_hover),
258                               NULL, NULL,
259                               g_cclosure_marshal_VOID__STRING,
260                               G_TYPE_NONE, 1, G_TYPE_STRING);
261
262         signals[RECPT_ACTIVATED_SIGNAL] =
263                 g_signal_new ("recpt_activated",
264                               G_TYPE_FROM_CLASS (gobject_class),
265                               G_SIGNAL_RUN_FIRST,
266                               G_STRUCT_OFFSET(ModestMsgViewClass, recpt_activated),
267                               NULL, NULL,
268                               g_cclosure_marshal_VOID__STRING,
269                               G_TYPE_NONE, 1, G_TYPE_STRING);
270
271         signals[LINK_CONTEXTUAL_SIGNAL] =
272                 g_signal_new ("link_contextual",
273                               G_TYPE_FROM_CLASS (gobject_class),
274                               G_SIGNAL_RUN_FIRST,
275                               G_STRUCT_OFFSET(ModestMsgViewClass, link_contextual),
276                               NULL, NULL,
277                               g_cclosure_marshal_VOID__STRING,
278                               G_TYPE_NONE, 1, G_TYPE_STRING);
279
280         widget_class->set_scroll_adjustments_signal =
281                 g_signal_new ("set_scroll_adjustments",
282                               G_OBJECT_CLASS_TYPE (gobject_class),
283                               G_SIGNAL_RUN_LAST|G_SIGNAL_ACTION,
284                               G_STRUCT_OFFSET (ModestMsgViewClass, set_scroll_adjustments),
285                               NULL, NULL,
286                               modest_marshal_VOID__POINTER_POINTER,
287                               G_TYPE_NONE, 2,
288                               GTK_TYPE_ADJUSTMENT,
289                               GTK_TYPE_ADJUSTMENT);
290 }
291
292 static void
293 set_property (GObject *object, 
294               guint prop_id, 
295               const GValue *value, 
296               GParamSpec *pspec)
297 {
298         ModestMsgView *msg_view = MODEST_MSG_VIEW (object);
299
300         switch (prop_id) {
301         case PROP_HADJUSTMENT:
302                 modest_msg_view_set_hadjustment (msg_view, g_value_get_object (value));
303                 break;
304         case PROP_VADJUSTMENT:
305                 modest_msg_view_set_vadjustment (msg_view, g_value_get_object (value));
306                 break;
307         case PROP_SHADOW_TYPE:
308                 modest_msg_view_set_shadow_type (msg_view, g_value_get_enum (value));
309                 break;
310         default:
311                 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
312                 break;
313         }
314 }
315
316 static void
317 get_property (GObject *object, 
318               guint prop_id, 
319               GValue *value, 
320               GParamSpec *pspec)
321 {
322         ModestMsgView *msg_view = MODEST_MSG_VIEW (object);
323         ModestMsgViewPrivate *priv = MODEST_MSG_VIEW_GET_PRIVATE (msg_view);
324
325         switch (prop_id) {
326         case PROP_HADJUSTMENT:
327                 g_value_set_object (value, priv->hadj);
328                 break;
329         case PROP_VADJUSTMENT:
330                 g_value_set_object (value, priv->vadj);
331                 break;
332         case PROP_SHADOW_TYPE:
333                 g_value_set_enum (value, priv->shadow_type);
334                 break;
335         default:
336                 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
337                 break;
338         }
339 }
340
341 static void
342 disconnect_hadjustment (ModestMsgView *msg_view)
343 {
344         ModestMsgViewPrivate *priv = MODEST_MSG_VIEW_GET_PRIVATE (msg_view);
345
346         if (priv->hadj) {
347                 g_signal_handlers_disconnect_by_func(priv->hadj, adjustment_value_changed, msg_view);
348                 g_object_unref (priv->hadj);
349                 priv->hadj = NULL;
350         }
351 }
352
353 static void
354 disconnect_vadjustment (ModestMsgView *msg_view)
355 {
356         ModestMsgViewPrivate *priv = MODEST_MSG_VIEW_GET_PRIVATE (msg_view);
357
358         if (priv->vadj) {
359                 g_signal_handlers_disconnect_by_func(priv->vadj, adjustment_value_changed, msg_view);
360                 g_object_unref (priv->vadj);
361                 priv->vadj = NULL;
362         }
363 }
364
365 static void 
366 get_view_allocation (ModestMsgView *msg_view, GtkAllocation *allocation)
367 {
368         /* This method gets the allocation of the widget in parent widget. It's the
369            real position and size of the widget */
370         ModestMsgViewPrivate *priv = MODEST_MSG_VIEW_GET_PRIVATE (msg_view);
371         
372         allocation->x = 0;
373         allocation->y = 0;
374
375         if (priv->shadow_type != GTK_SHADOW_NONE) {
376                 allocation->x = GTK_WIDGET (msg_view)->style->xthickness;
377                 allocation->y = GTK_WIDGET (msg_view)->style->ythickness;
378         }
379
380         allocation->width = MAX (1, (GTK_WIDGET (msg_view)->allocation.width) - allocation->x * 2);
381         allocation->height = MAX (1, (GTK_WIDGET (msg_view)->allocation.height) - allocation->y * 2);
382
383 }
384
385 static void 
386 reclamp_adjustment (GtkAdjustment *adj, 
387                     gboolean *value_changed)
388 {
389         gdouble value = adj->value;
390
391         /* Correct value to be inside the expected values of a scroll */
392
393         value = CLAMP (value, 0, adj->upper - adj->page_size);
394
395         if (value != adj->value) {
396                 adj->value = value;
397                 if (value_changed)
398                         *value_changed = TRUE;
399         } else if (value_changed) {
400                 *value_changed = FALSE;
401         }
402 }
403
404 static void 
405 set_hadjustment_values (ModestMsgView *msg_view,
406                         gboolean *value_changed)
407 {
408         GtkAllocation view_allocation;
409         GtkAdjustment *hadj = modest_msg_view_get_hadjustment (msg_view);
410
411         get_view_allocation (msg_view, &view_allocation);
412         hadj->page_size = view_allocation.width;
413         hadj->step_increment = view_allocation.width * 0.1;
414         hadj->page_increment = view_allocation.width * 0.9;
415
416         hadj->lower = 0;
417         hadj->upper = view_allocation.width;
418
419         reclamp_adjustment (hadj, value_changed);
420
421 }
422
423 static void 
424 set_vadjustment_values (ModestMsgView *msg_view,
425                         gboolean *value_changed)
426 {
427         GtkAllocation view_allocation;
428         GtkAdjustment *vadj = modest_msg_view_get_vadjustment (msg_view);
429         ModestMsgViewPrivate *priv = MODEST_MSG_VIEW_GET_PRIVATE (msg_view);
430         gint full_height = 0;
431         GtkAdjustment *html_vadj;
432
433         get_view_allocation (msg_view, &view_allocation);
434         vadj->page_size = view_allocation.height;
435         vadj->step_increment = view_allocation.height * 0.1;
436         vadj->page_increment = view_allocation.height * 0.9;
437
438         vadj->lower = 0;
439
440         if (priv->headers_box && GTK_WIDGET_VISIBLE(priv->headers_box)) {
441                 GtkRequisition child_requisition;
442
443                 gtk_widget_get_child_requisition (priv->headers_box, &child_requisition);
444                 full_height = child_requisition.height;
445         } else {
446                 full_height = 0;
447         }
448         
449         /* Get the real height of the embedded html */
450         if (priv->html_scroll && GTK_WIDGET_VISIBLE(priv->html_scroll)) {
451                 html_vadj = gtk_scrolled_window_get_vadjustment (GTK_SCROLLED_WINDOW (priv->html_scroll));
452                 full_height += html_vadj->upper;
453         }
454
455         vadj->upper = MAX (view_allocation.height, full_height);
456
457         reclamp_adjustment (vadj, value_changed);
458
459 }
460
461 static void
462 set_scroll_adjustments (ModestMsgView *msg_view,
463                         GtkAdjustment *hadj,
464                         GtkAdjustment *vadj)
465 {
466         ModestMsgViewPrivate *priv = MODEST_MSG_VIEW_GET_PRIVATE (msg_view);
467         modest_msg_view_set_hadjustment (msg_view, hadj);
468         modest_msg_view_set_vadjustment (msg_view, vadj);
469
470         gtk_container_set_focus_vadjustment (GTK_CONTAINER (priv->gtkhtml), vadj);
471 }
472
473 static void
474 realize (GtkWidget *widget)
475 {
476         ModestMsgView *msg_view = MODEST_MSG_VIEW (widget);
477         ModestMsgViewPrivate *priv = MODEST_MSG_VIEW_GET_PRIVATE (msg_view);
478         GtkAdjustment *hadj = modest_msg_view_get_hadjustment (msg_view);
479         GtkAdjustment *vadj = modest_msg_view_get_vadjustment (msg_view);
480         GdkWindowAttr attributes;
481         gint event_mask;
482         gint attributes_mask;
483         GtkAllocation view_allocation;
484
485         GTK_WIDGET_SET_FLAGS (widget, GTK_REALIZED);
486
487         /* The structure of the GdkWindow's is:
488          *    * widget->window: the shown gdkwindow embedding all the stuff inside
489          *    * view_window: a backing store gdkwindow containing the headers and contents
490          *      being scrolled. This window should have all the visible and non visible
491          *      widgets inside.
492          *    * headers_window: gdk window for headers_box.
493          *    * html_window: gdk window for html_scroll (the scrolled window containing the
494          *      gtkhtml showing the contents of the mail).
495          */
496
497         attributes.x = widget->allocation.x;
498         attributes.y = widget->allocation.y;
499         attributes.width = widget->allocation.width;
500         attributes.height = widget->allocation.height;
501         attributes.window_type = GDK_WINDOW_CHILD;
502         attributes.wclass = GDK_INPUT_OUTPUT;
503         attributes.visual = gtk_widget_get_visual (widget);
504         attributes.colormap = gtk_widget_get_colormap (widget);
505
506         event_mask = gtk_widget_get_events (widget) | GDK_EXPOSURE_MASK;
507         attributes.event_mask = event_mask | GDK_BUTTON_PRESS_MASK;
508         attributes_mask = GDK_WA_X | GDK_WA_Y | GDK_WA_VISUAL | GDK_WA_COLORMAP;
509
510         widget->window = gdk_window_new (gtk_widget_get_parent_window (widget),
511                                          &attributes, attributes_mask);
512         gdk_window_set_user_data (widget->window, msg_view);
513
514         get_view_allocation (msg_view, &view_allocation);
515
516         attributes.x = view_allocation.x;
517         attributes.y = view_allocation.y;
518         attributes.width = view_allocation.width;
519         attributes.height = view_allocation.height;
520         attributes.event_mask = 0;
521         priv->view_window = gdk_window_new (widget->window, &attributes, attributes_mask);
522         gdk_window_set_user_data (priv->view_window, msg_view);
523         gdk_window_set_back_pixmap (priv->view_window, NULL, FALSE);
524
525         attributes.x = -hadj->value;
526         attributes.y = -vadj->value;
527         attributes.width = hadj->upper;
528         if (priv->headers_box)
529                 attributes.height = GTK_WIDGET (priv->headers_box)->allocation.height;
530         else
531                 attributes.height = 1;
532         attributes.event_mask = event_mask;
533
534         priv->headers_window = gdk_window_new (priv->view_window, &attributes, attributes_mask);
535         gdk_window_set_user_data (priv->headers_window, msg_view);
536
537         if (priv->headers_box)
538                 gtk_widget_set_parent_window (priv->headers_box, priv->headers_window);
539
540         attributes.x = -hadj->value;
541         if (priv->headers_box)
542                 attributes.y = GTK_WIDGET (priv->headers_box)->allocation.height - vadj->value;
543         else 
544                 attributes.y = -vadj->value;
545         attributes.width = hadj->upper;
546         if (priv->headers_box)
547                 attributes.height = vadj->upper - GTK_WIDGET (priv->headers_box)->allocation.height;
548         else
549                 attributes.height = vadj->upper;
550         attributes.event_mask = event_mask;
551
552         priv->html_window = gdk_window_new (priv->view_window, &attributes, attributes_mask);
553         gdk_window_set_user_data (priv->html_window, msg_view);
554
555         if (priv->html_scroll)
556                 gtk_widget_set_parent_window (priv->html_scroll, priv->html_window);
557
558         widget->style = gtk_style_attach (widget->style, widget->window);
559         gtk_style_set_background (widget->style, widget->window, GTK_STATE_NORMAL);
560         gtk_style_set_background (widget->style, priv->headers_window, GTK_STATE_NORMAL);
561         gtk_style_set_background (widget->style, priv->html_window, GTK_STATE_NORMAL);
562
563         gtk_paint_flat_box(widget->style, priv->headers_window, GTK_STATE_NORMAL,
564                            GTK_SHADOW_NONE, 
565                            NULL, widget, "msgviewheaders",
566                            0,0,-1,-1);
567         gtk_paint_flat_box(widget->style, priv->html_window, GTK_STATE_NORMAL,
568                            GTK_SHADOW_NONE, 
569                            NULL, widget, "msgviewcontents",
570                            0,0,-1,-1);
571
572         gdk_window_show (priv->view_window);
573         gdk_window_show (priv->headers_window);
574         gdk_window_show (priv->html_window);
575
576 }
577
578 static void
579 unrealize (GtkWidget *widget)
580 {
581         ModestMsgView *msg_view = MODEST_MSG_VIEW (widget);
582         ModestMsgViewPrivate *priv = MODEST_MSG_VIEW_GET_PRIVATE (msg_view);
583
584         gdk_window_set_user_data (priv->view_window, NULL);
585         gdk_window_destroy (priv->view_window);
586         priv->view_window = NULL;
587
588         gdk_window_set_user_data (priv->headers_window, NULL);
589         gdk_window_destroy (priv->headers_window);
590         priv->headers_window = NULL;
591
592         gdk_window_set_user_data (priv->html_window, NULL);
593         gdk_window_destroy (priv->html_window);
594         priv->html_window = NULL;
595
596         if (GTK_WIDGET_CLASS (parent_class)->unrealize)
597                 ( * GTK_WIDGET_CLASS (parent_class)->unrealize) (widget);
598
599 }
600
601 static gint
602 expose (GtkWidget *widget, 
603         GdkEventExpose *event)
604 {
605         ModestMsgView *msg_view;
606         ModestMsgViewPrivate *priv;
607
608         if (GTK_WIDGET_DRAWABLE (widget)) {
609                 msg_view = MODEST_MSG_VIEW (widget);
610                 priv = MODEST_MSG_VIEW_GET_PRIVATE (msg_view);
611                 if (event->window == widget->window) {
612                         gtk_paint_shadow (widget->style, widget->window,
613                                           GTK_STATE_NORMAL, priv->shadow_type,
614                                           &event->area, widget, "msgview",
615                                           0,0,-1,-1);
616                 } else if (event->window == priv->headers_window) {
617                         gtk_paint_flat_box(widget->style, priv->headers_window, GTK_STATE_NORMAL,
618                                            GTK_SHADOW_NONE, 
619                                            &event->area, widget, "msgviewheaders",
620                                            0,0,-1,-1);
621                 } else if (event->window == priv->html_window) {
622                         gtk_paint_flat_box(widget->style, priv->html_window, GTK_STATE_NORMAL,
623                                            GTK_SHADOW_NONE, 
624                                            &event->area, widget, "msgviewcontents",
625                                            0,0,-1,-1);
626                 }
627                 if (priv->headers_box)
628                         gtk_container_propagate_expose (GTK_CONTAINER (msg_view), priv->headers_box, event);
629                 if (priv->html_scroll)
630                         gtk_container_propagate_expose (GTK_CONTAINER (msg_view), priv->html_scroll, event);
631                 (* GTK_WIDGET_CLASS (parent_class)->expose_event) (widget, event);
632         }
633
634         return FALSE;
635 }
636
637 static void 
638 forall (GtkContainer *container, 
639         gboolean include_internals,
640         GtkCallback callback,
641         gpointer userdata)
642 {
643         ModestMsgViewPrivate *priv = MODEST_MSG_VIEW_GET_PRIVATE (container);
644         g_return_if_fail (callback != NULL);
645
646         if (priv->headers_box)
647                 (*callback) (priv->headers_box, userdata);
648         if (priv->html_scroll)
649                 (*callback) (priv->html_scroll, userdata);
650 }
651
652 static void
653 container_remove (GtkContainer *container,
654                   GtkWidget *widget)
655 {
656         gboolean was_visible = FALSE;
657         ModestMsgViewPrivate *priv = MODEST_MSG_VIEW_GET_PRIVATE (container);
658         was_visible = GTK_WIDGET_VISIBLE (widget);
659         if (widget == priv->headers_box) {
660                 gtk_widget_unparent (priv->headers_box);
661                 priv->headers_box = NULL;
662         } else if (widget == priv->html_scroll) {
663                 gtk_widget_unparent (priv->html_scroll);
664                 priv->html_scroll = NULL;
665         } else {
666                 return;
667         }
668         if (was_visible)
669                 gtk_widget_queue_resize (GTK_WIDGET(container));
670
671 }
672
673 static void
674 size_request (GtkWidget *widget,
675               GtkRequisition *req)
676 {
677         ModestMsgViewPrivate *priv = MODEST_MSG_VIEW_GET_PRIVATE (widget);
678         GtkRequisition child_req;
679
680         req->width = 0;
681         req->height = 0;
682
683         gtk_widget_size_request (priv->headers_box, &child_req);
684         req->width = child_req.width;
685         req->height += child_req.height;
686         gtk_widget_size_request (priv->html_scroll, &child_req);
687         req->width = MAX (child_req.width, req->width);
688         req->height += child_req.height;
689
690 }
691
692 static void
693 size_allocate (GtkWidget *widget,
694                GtkAllocation *allocation)
695 {
696         ModestMsgView *msg_view = MODEST_MSG_VIEW (widget);
697         ModestMsgViewPrivate *priv = MODEST_MSG_VIEW_GET_PRIVATE (msg_view);
698         gboolean hadj_value_changed, vadj_value_changed;
699         GtkAllocation headers_allocation, html_allocation, view_allocation;
700         GtkAdjustment *html_vadj;
701
702         if (GTK_WIDGET_MAPPED (widget) &&
703             priv->shadow_type != GTK_SHADOW_NONE && 
704             (allocation->width != widget->allocation.width ||
705              allocation->height != widget->allocation.height))
706                 gdk_window_invalidate_rect (widget->window, NULL, FALSE);
707
708         widget->allocation = *allocation;
709         set_hadjustment_values (msg_view, &hadj_value_changed);
710         set_vadjustment_values (msg_view, &vadj_value_changed);
711
712         get_view_allocation (msg_view, &view_allocation);
713
714         headers_allocation.x = 0;
715         headers_allocation.y = 0;
716         headers_allocation.width = view_allocation.width;
717         if (priv->headers_box)
718                 headers_allocation.height = GTK_WIDGET (priv->headers_box)->requisition.height;
719         else
720                 headers_allocation.height = 0;
721
722         html_vadj = gtk_scrolled_window_get_vadjustment (GTK_SCROLLED_WINDOW (priv->html_scroll));
723
724         html_allocation.x = 0;
725         html_allocation.y = headers_allocation.height;
726         html_allocation.width = view_allocation.width;
727         html_allocation.height = MAX ((gint) html_vadj->upper, (gint)(priv->vadj->upper - headers_allocation.height));
728
729         if (GTK_WIDGET_REALIZED (widget)) {
730                 gdk_window_move_resize (widget->window,
731                                         allocation->x,
732                                         allocation->y,
733                                         allocation->width,
734                                         allocation->height);
735
736                 gdk_window_move_resize (priv->view_window,
737                                         view_allocation.x,
738                                         view_allocation.y,
739                                         view_allocation.width,
740                                         view_allocation.height);
741                 gdk_window_move_resize (priv->headers_window,
742                                         0,
743                                         (gint) (- priv->vadj->value),
744                                         allocation->width,
745                                         headers_allocation.height);
746                 gdk_window_move_resize (priv->html_window,
747                                         (gint) (- priv->hadj->value),
748                                         (gint) (html_allocation.y - priv->vadj->value),
749                                         (gint) priv->hadj->upper,
750                                         html_allocation.height);
751         }
752
753         if (priv->headers_box && GTK_WIDGET_VISIBLE (priv->headers_box)) {
754                 gtk_widget_size_allocate (priv->headers_box, &headers_allocation);
755         }
756         if (priv->html_scroll && GTK_WIDGET_VISIBLE (priv->html_scroll)) {
757                 html_allocation.x = 0;
758                 html_allocation.y = 0;
759                 html_allocation.width = (gint) priv->hadj->upper;
760                 html_allocation.height = (gint) priv->vadj->upper - headers_allocation.height;
761                 gtk_widget_size_allocate (priv->html_scroll, &html_allocation);
762         }
763         gtk_adjustment_changed (priv->hadj);
764         gtk_adjustment_changed (priv->vadj);
765
766         if (hadj_value_changed)
767                 gtk_adjustment_value_changed (priv->hadj);
768         if (vadj_value_changed)
769                 gtk_adjustment_value_changed (priv->vadj);
770
771 }
772
773 static void 
774 adjustment_value_changed (GtkAdjustment *adj, gpointer data)
775 {
776         ModestMsgView *msg_view = NULL;
777         ModestMsgViewPrivate *priv = NULL;
778
779         g_return_if_fail (GTK_IS_ADJUSTMENT (adj));
780         g_return_if_fail (MODEST_IS_MSG_VIEW (data));
781
782         msg_view = MODEST_MSG_VIEW (data);
783         priv = MODEST_MSG_VIEW_GET_PRIVATE (msg_view);
784
785         if (GTK_WIDGET_REALIZED (msg_view)) {
786                 GtkAdjustment *hadj = modest_msg_view_get_hadjustment (msg_view);
787                 GtkAdjustment *vadj = modest_msg_view_get_vadjustment (msg_view);
788                 gint headers_offset = 0;
789
790                 gtk_widget_queue_resize (priv->html_scroll);
791
792                 if (priv->headers_box && GTK_WIDGET_VISIBLE (priv->headers_box)) {
793                         gint old_x, old_y;
794                         gint new_x, new_y;
795                         gdk_window_get_position (priv->headers_window, &old_x, &old_y);
796                         new_x = 0;
797                         new_y = -vadj->value;
798
799                         headers_offset = GTK_WIDGET(priv->headers_box)->allocation.height;
800
801                         if (new_x != old_x || new_y != old_y) {
802                                 gdk_window_move (priv->headers_window, new_x, new_y);
803                                 gdk_window_process_updates (priv->headers_window, TRUE);
804                         }
805                 }
806                 
807                 if (priv->html_scroll && GTK_WIDGET_VISIBLE (priv->html_scroll)) {
808                         gint old_x, old_y;
809                         gint new_x, new_y;
810                         gdk_window_get_position (priv->html_window, &old_x, &old_y);
811                         new_x = -hadj->value;
812                         new_y = headers_offset - vadj->value;
813
814                         if (new_x != old_x || new_y != old_y) {
815                                 gdk_window_move (priv->html_window, new_x, new_y);
816                                 gdk_window_process_updates (priv->html_window, TRUE);
817                         }
818                 }
819                 
820         }
821 }
822
823 static void
824 html_adjustment_changed (GtkAdjustment *adj,
825                          gpointer userdata)
826 {
827         ModestMsgView *msg_view = MODEST_MSG_VIEW (userdata);
828         ModestMsgViewPrivate *priv = MODEST_MSG_VIEW_GET_PRIVATE (msg_view);
829         GtkAdjustment *html_vadj = gtk_scrolled_window_get_vadjustment (GTK_SCROLLED_WINDOW(priv->html_scroll));
830         gboolean vadj_changed;
831         gint new_height;
832
833         priv->html_scroll->requisition.height = html_vadj->upper;
834         priv->html_scroll->allocation.height = html_vadj->upper;
835
836         set_vadjustment_values (msg_view, &vadj_changed);
837
838         new_height = MAX ((gint) html_vadj->upper, (gint) (priv->vadj->upper - priv->headers_box->allocation.height));
839         
840         gtk_adjustment_changed (priv->vadj);
841         if (GTK_WIDGET_DRAWABLE (priv->html_scroll)) {
842                 gdk_window_resize (priv->html_window, (gint) priv->hadj->upper, (gint) new_height);
843                 gdk_window_process_updates (priv->view_window, TRUE);
844                 gtk_container_resize_children (GTK_CONTAINER (msg_view));
845         }
846         
847 }
848
849 static void
850 modest_msg_view_init (ModestMsgView *obj)
851 {
852         ModestMsgViewPrivate *priv;
853         GtkAdjustment *html_vadj;
854
855         GTK_WIDGET_UNSET_FLAGS (obj, GTK_NO_WINDOW);
856         gtk_widget_set_redraw_on_allocate (GTK_WIDGET (obj), TRUE);
857         gtk_container_set_reallocate_redraws (GTK_CONTAINER (obj), TRUE);
858         gtk_container_set_resize_mode (GTK_CONTAINER (obj), GTK_RESIZE_QUEUE);
859         
860         priv = MODEST_MSG_VIEW_GET_PRIVATE(obj);
861
862         priv->current_zoom = 1.0;
863         priv->priority_flags = 0;
864
865         priv->hadj = NULL;
866         priv->vadj = NULL;
867         priv->shadow_type = GTK_SHADOW_IN;
868         priv->view_window = NULL;
869         priv->headers_window = NULL;
870         priv->html_window = NULL;
871
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                 g_object_unref (G_OBJECT(part));
1201         
1202                 part = NULL;
1203                 tny_iterator_next (iter);
1204         }
1205         
1206         g_object_unref (G_OBJECT(iter));        
1207         g_object_unref (G_OBJECT(parts));
1208         
1209         return part;
1210 }
1211
1212
1213 static gboolean
1214 on_url_requested (GtkWidget *widget, const gchar *uri,
1215                   GtkHTMLStream *stream, ModestMsgView *msg_view)
1216 {
1217         ModestMsgViewPrivate *priv;
1218         priv = MODEST_MSG_VIEW_GET_PRIVATE (msg_view);
1219         
1220         if (g_str_has_prefix (uri, "cid:")) {
1221                 /* +4 ==> skip "cid:" */
1222                 TnyMimePart *part = find_cid_image (priv->msg, uri + 4);
1223                 if (!part) {
1224                         g_printerr ("modest: '%s' not found\n", uri + 4);
1225                         gtk_html_stream_close (stream, GTK_HTML_STREAM_ERROR);
1226                 } else {
1227                         TnyStream *tny_stream =
1228                                 TNY_STREAM(modest_tny_stream_gtkhtml_new(stream));
1229                         tny_mime_part_decode_to_stream ((TnyMimePart*)part,
1230                                                                   tny_stream);
1231                         gtk_html_stream_close (stream, GTK_HTML_STREAM_OK);
1232         
1233                         g_object_unref (G_OBJECT(tny_stream));
1234                         g_object_unref (G_OBJECT(part));
1235                 }
1236         }
1237
1238         return TRUE;
1239 }
1240
1241 static gboolean
1242 set_html_message (ModestMsgView *self, TnyMimePart *tny_body, TnyMsg *msg)
1243 {
1244         GtkHTMLStream *gtkhtml_stream;
1245         TnyStream *tny_stream;  
1246         ModestMsgViewPrivate *priv;
1247         
1248         g_return_val_if_fail (self, FALSE);
1249         g_return_val_if_fail (tny_body, FALSE);
1250         
1251         priv = MODEST_MSG_VIEW_GET_PRIVATE(self);
1252
1253         gtkhtml_stream = gtk_html_begin(GTK_HTML(priv->gtkhtml));
1254
1255         tny_stream     = TNY_STREAM(modest_tny_stream_gtkhtml_new (gtkhtml_stream));
1256         tny_stream_reset (tny_stream);
1257
1258         tny_mime_part_decode_to_stream ((TnyMimePart*)tny_body, tny_stream);
1259         g_object_unref (G_OBJECT(tny_stream));
1260         
1261         gtk_html_stream_destroy (gtkhtml_stream);
1262         
1263         return TRUE;
1264 }
1265
1266
1267 /* FIXME: this is a hack --> we use the tny_text_buffer_stream to
1268  * get the message text, then write to gtkhtml 'by hand' */
1269 static gboolean
1270 set_text_message (ModestMsgView *self, TnyMimePart *tny_body, TnyMsg *msg)
1271 {
1272         GtkTextBuffer *buf;
1273         GtkTextIter begin, end;
1274         TnyStream* txt_stream, *tny_stream;
1275         GtkHTMLStream *gtkhtml_stream;
1276         gchar *txt;
1277         ModestMsgViewPrivate *priv;
1278                 
1279         g_return_val_if_fail (self, FALSE);
1280         g_return_val_if_fail (tny_body, FALSE);
1281
1282         priv           = MODEST_MSG_VIEW_GET_PRIVATE(self);
1283         
1284         buf            = gtk_text_buffer_new (NULL);
1285         txt_stream     = TNY_STREAM(tny_gtk_text_buffer_stream_new (buf));
1286                 
1287         tny_stream_reset (txt_stream);
1288
1289         gtkhtml_stream = gtk_html_begin(GTK_HTML(priv->gtkhtml)); 
1290         tny_stream =  TNY_STREAM(modest_tny_stream_gtkhtml_new (gtkhtml_stream));
1291         
1292         // FIXME: tinymail
1293         tny_mime_part_decode_to_stream ((TnyMimePart*)tny_body, txt_stream);
1294         tny_stream_reset (txt_stream);          
1295         
1296         gtk_text_buffer_get_bounds (buf, &begin, &end);
1297         txt = gtk_text_buffer_get_text (buf, &begin, &end, FALSE);
1298         if (txt) {
1299                 gchar *html = modest_text_utils_convert_to_html (txt);
1300                 tny_stream_write (tny_stream, html, strlen(html));
1301                 tny_stream_reset (tny_stream);
1302                 g_free (txt);
1303                 g_free (html);
1304         }
1305         
1306         g_object_unref (G_OBJECT(tny_stream));
1307         g_object_unref (G_OBJECT(txt_stream));
1308         g_object_unref (G_OBJECT(buf));
1309         
1310         gtk_html_stream_destroy (gtkhtml_stream);
1311         
1312         return TRUE;
1313 }
1314
1315
1316 static gboolean
1317 set_empty_message (ModestMsgView *self)
1318 {
1319         ModestMsgViewPrivate *priv;
1320         
1321         g_return_val_if_fail (self, FALSE);
1322         priv           = MODEST_MSG_VIEW_GET_PRIVATE(self);
1323
1324         gtk_html_load_from_string (GTK_HTML(priv->gtkhtml),
1325                                    "", 1);
1326         
1327         return TRUE;
1328 }
1329
1330
1331 void
1332 modest_msg_view_set_message (ModestMsgView *self, TnyMsg *msg)
1333 {
1334         TnyMimePart *body;
1335         ModestMsgViewPrivate *priv;
1336         TnyHeader *header;
1337         
1338         g_return_if_fail (self);
1339         
1340         priv = MODEST_MSG_VIEW_GET_PRIVATE(self);
1341         gtk_widget_set_no_show_all (priv->mail_header_view, FALSE);
1342
1343         if (msg != priv->msg) {
1344                 if (priv->msg)
1345                         g_object_unref (G_OBJECT(priv->msg));
1346                 if (msg)
1347                         g_object_ref   (G_OBJECT(msg));
1348                 priv->msg = msg;
1349         }
1350         
1351         if (!msg) {
1352                 tny_header_view_clear (TNY_HEADER_VIEW (priv->mail_header_view));
1353                 modest_attachments_view_set_message (MODEST_ATTACHMENTS_VIEW (priv->attachments_view), NULL);
1354                 gtk_widget_hide_all (priv->mail_header_view);
1355                 gtk_widget_hide_all (priv->attachments_box);
1356                 gtk_widget_set_no_show_all (priv->mail_header_view, TRUE);
1357                 set_empty_message (self);
1358                 gtk_widget_queue_resize (GTK_WIDGET(self));
1359                 gtk_widget_queue_draw (GTK_WIDGET(self));
1360                 return;
1361         }
1362
1363         header = tny_msg_get_header (msg);
1364         tny_header_view_set_header (TNY_HEADER_VIEW (priv->mail_header_view), header);
1365         g_object_unref (header);
1366
1367         modest_attachments_view_set_message (MODEST_ATTACHMENTS_VIEW(priv->attachments_view),
1368                                              msg);
1369         
1370         body = modest_tny_msg_find_body_part (msg, TRUE);
1371         if (body) {
1372                 if (tny_mime_part_content_type_is (body, "text/html"))
1373                         set_html_message (self, body, msg);
1374                 else
1375                         set_text_message (self, body, msg);
1376
1377                 if(modest_attachments_view_has_attachments (MODEST_ATTACHMENTS_VIEW (priv->attachments_view))) {
1378                         gtk_widget_show_all (priv->attachments_box);
1379                 } else {
1380                         gtk_widget_hide_all (priv->attachments_box);
1381                 }
1382
1383 /*              g_print ("---\nfilename %s\ncontent_location %s\ncontent_id%s\ncontent_type%s\n", */
1384 /*                       tny_mime_part_get_filename (body), */
1385 /*                       tny_mime_part_get_content_location (body), */
1386 /*                       tny_mime_part_get_content_id (body), */
1387 /*                       tny_mime_part_get_content_type (body)); */
1388                         
1389         } else 
1390                 set_empty_message (self);
1391
1392         gtk_widget_show (priv->gtkhtml);
1393         gtk_widget_set_no_show_all (priv->attachments_box, TRUE);
1394         gtk_widget_show_all (priv->mail_header_view);
1395         gtk_widget_set_no_show_all (priv->attachments_box, FALSE);
1396 /*      gtk_widget_show_all (priv->attachments_box); */
1397 /*      gtk_widget_show_all (priv->attachments_box); */
1398         gtk_widget_set_no_show_all (priv->mail_header_view, TRUE);
1399         gtk_widget_queue_resize (GTK_WIDGET(self));
1400         gtk_widget_queue_draw (GTK_WIDGET(self));
1401
1402         if (priv->hadj != NULL)
1403                 priv->hadj->value = 0.0;
1404         if (priv->vadj != NULL)
1405                 priv->vadj->value = 0.0;
1406
1407 }
1408
1409
1410 TnyMsg*
1411 modest_msg_view_get_message (ModestMsgView *self)
1412 {
1413         TnyMsg *msg;
1414
1415         g_return_val_if_fail (MODEST_IS_MSG_VIEW (self), NULL);
1416
1417         msg = MODEST_MSG_VIEW_GET_PRIVATE(self)->msg;
1418
1419         if (msg)
1420                 g_object_ref (msg);
1421         
1422         return msg;
1423 }
1424
1425 gboolean 
1426 modest_msg_view_search (ModestMsgView *self, const gchar *search)
1427 {
1428         ModestMsgViewPrivate *priv;
1429         gboolean result;
1430         GtkAdjustment *vadj, *tmp_vadj;
1431         gdouble y_offset;
1432
1433         g_return_val_if_fail (MODEST_IS_MSG_VIEW (self), FALSE);
1434
1435         priv = MODEST_MSG_VIEW_GET_PRIVATE (self);
1436         vadj = gtk_layout_get_vadjustment (GTK_LAYOUT (priv->gtkhtml));
1437         g_object_ref (vadj);
1438         tmp_vadj = GTK_ADJUSTMENT (gtk_adjustment_new (0.0, vadj->lower, vadj->upper, vadj->step_increment, 32.0, 32.0));
1439         gtk_layout_set_vadjustment (GTK_LAYOUT (priv->gtkhtml), tmp_vadj);
1440         result = gtk_html_engine_search (GTK_HTML (priv->gtkhtml),
1441                                          search,
1442                                          FALSE, TRUE, TRUE);
1443 #if 0
1444         if (result) {
1445                 gint x, y, w, h;
1446                 gdouble offset_top, offset_bottom;
1447                 GtkAdjustment *adj;
1448                 gtk_html_get_selection_area (GTK_HTML (priv->gtkhtml), &x, &y, &w, &h);
1449                 offset_top = (gdouble) (priv->headers_box->requisition.height + y);
1450                 offset_bottom = (gdouble) (priv->headers_box->requisition.height + y + h);
1451                 adj = GTK_ADJUSTMENT (priv->vadj);
1452                 if (offset_top < adj->value)
1453                         gtk_adjustment_set_value (adj, offset_top + adj->page_increment - adj->page_size);
1454                 else if (offset_bottom > adj->value + adj->page_increment)
1455                         gtk_adjustment_set_value (adj, offset_bottom - adj->page_increment);
1456         }
1457 #endif
1458         y_offset = tmp_vadj->value;
1459         gtk_layout_set_vadjustment (GTK_LAYOUT (priv->gtkhtml), vadj);
1460         g_object_unref (vadj);
1461
1462         return result;
1463 }
1464
1465 gboolean
1466 modest_msg_view_search_next (ModestMsgView *self)
1467 {
1468         ModestMsgViewPrivate *priv;
1469         gboolean result;
1470
1471         g_return_val_if_fail (MODEST_IS_MSG_VIEW (self), FALSE);
1472
1473         priv = MODEST_MSG_VIEW_GET_PRIVATE (self);
1474         result = gtk_html_engine_search_next (GTK_HTML (priv->gtkhtml));
1475 #if 0
1476         if (result) {
1477                 gint x, y, w, h;
1478                 gdouble offset_top, offset_bottom;
1479                 GtkAdjustment *adj;
1480                 gtk_html_get_selection_area (GTK_HTML (priv->gtkhtml), &x, &y, &w, &h);
1481                 g_message ("SELECTION AREA x%d y%d w%d h%d", x, y, w, h);
1482                 offset_top = (gdouble) (priv->headers_box->requisition.height + y);
1483                 offset_bottom = (gdouble) (priv->headers_box->requisition.height + y + h);
1484                 adj = GTK_ADJUSTMENT (priv->vadj);
1485                 if (offset_top < adj->value)
1486                         gtk_adjustment_set_value (adj, offset_top + adj->page_increment - adj->page_size);
1487                 else if (offset_bottom > adj->value + adj->page_increment)
1488                         gtk_adjustment_set_value (adj, offset_bottom - adj->page_increment);
1489         }
1490 #endif
1491
1492         return result;
1493 }
1494
1495 void
1496 modest_msg_view_set_zoom (ModestMsgView *self, gdouble zoom)
1497 {
1498         ModestMsgViewPrivate *priv;
1499
1500         g_return_if_fail (MODEST_IS_MSG_VIEW (self));
1501
1502         priv = MODEST_MSG_VIEW_GET_PRIVATE (self);
1503         priv->current_zoom = zoom;
1504         gtk_html_set_magnification (GTK_HTML(priv->gtkhtml), zoom);
1505
1506         gtk_widget_queue_resize (priv->gtkhtml);
1507 }
1508
1509 gdouble
1510 modest_msg_view_get_zoom (ModestMsgView *self)
1511 {
1512         ModestMsgViewPrivate *priv;
1513
1514         g_return_val_if_fail (MODEST_IS_MSG_VIEW (self), 1.0);
1515
1516         priv = MODEST_MSG_VIEW_GET_PRIVATE (self);
1517
1518         return priv->current_zoom;
1519 }
1520
1521 TnyHeaderFlags
1522 modest_msg_view_get_priority (ModestMsgView *self)
1523 {
1524         ModestMsgViewPrivate *priv;
1525
1526         g_return_val_if_fail (MODEST_IS_MSG_VIEW (self), 0);
1527
1528         priv = MODEST_MSG_VIEW_GET_PRIVATE (self);
1529
1530         return priv->priority_flags;
1531 }
1532
1533 void
1534 modest_msg_view_set_priority (ModestMsgView *self, TnyHeaderFlags flags)
1535 {
1536         ModestMsgViewPrivate *priv;
1537
1538         g_return_if_fail (MODEST_IS_MSG_VIEW (self));
1539
1540         priv = MODEST_MSG_VIEW_GET_PRIVATE (self);
1541
1542         priv->priority_flags = flags & (TNY_HEADER_FLAG_HIGH_PRIORITY);
1543
1544         modest_mail_header_view_set_priority (MODEST_MAIL_HEADER_VIEW (priv->mail_header_view), flags);
1545 }
1546
1547 GList *
1548 modest_msg_view_get_selected_attachments (ModestMsgView *self)
1549 {
1550         ModestMsgViewPrivate *priv;
1551
1552         g_return_val_if_fail (MODEST_IS_MSG_VIEW (self), NULL);
1553         priv = MODEST_MSG_VIEW_GET_PRIVATE (self);
1554
1555         return modest_attachments_view_get_selection (MODEST_ATTACHMENTS_VIEW (priv->attachments_view));
1556         
1557 }
1558
1559 void
1560 modest_msg_view_grab_focus (ModestMsgView *view)
1561 {
1562         ModestMsgViewPrivate *priv = NULL;
1563
1564         g_return_if_fail (MODEST_IS_MSG_VIEW (view));
1565         priv = MODEST_MSG_VIEW_GET_PRIVATE (view);
1566
1567         gtk_widget_grab_focus (priv->gtkhtml);
1568 }
1569
1570 void
1571 modest_msg_view_remove_attachment (ModestMsgView *view, TnyMimePart *attachment)
1572 {
1573         TnyMsg *msg;
1574         ModestMsgViewPrivate *priv;
1575
1576         g_return_if_fail (MODEST_IS_MSG_VIEW (view));
1577         g_return_if_fail (TNY_IS_MIME_PART (attachment));
1578         priv = MODEST_MSG_VIEW_GET_PRIVATE (view);
1579
1580         msg = modest_msg_view_get_message (view);
1581         modest_attachments_view_remove_attachment (MODEST_ATTACHMENTS_VIEW (priv->attachments_view),
1582                                                    attachment);
1583         
1584 }