2cb53e85c2869e1473d5d2317dd04383ca3139ff
[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 <tny-list.h>
39 #include <tny-simple-list.h>
40
41 #include <modest-tny-msg.h>
42 #include <modest-text-utils.h>
43 #include "modest-msg-view.h"
44 #include "modest-tny-stream-gtkhtml.h"
45 #include <modest-mail-header-view.h>
46 #include <modest-attachments-view.h>
47 #include <modest-marshal.h>
48
49
50 /* 'private'/'protected' functions */
51 static void     modest_msg_view_class_init   (ModestMsgViewClass *klass);
52 static void     modest_msg_view_init         (ModestMsgView *obj);
53 static void     modest_msg_view_finalize     (GObject *obj);
54
55 /* headers signals */
56 static void on_recpt_activated (ModestMailHeaderView *header_view, const gchar *address, ModestMsgView *msg_view);
57 static void on_attachment_activated (ModestAttachmentsView * att_view, gint index, gpointer);
58
59 /* GtkHtml signals */
60 static gboolean on_link_clicked (GtkWidget *widget, const gchar *uri, ModestMsgView *msg_view);
61 static gboolean on_url_requested (GtkWidget *widget, const gchar *uri, GtkHTMLStream *stream,
62                                   ModestMsgView *msg_view);
63 static gboolean on_link_hover (GtkWidget *widget, const gchar *uri, ModestMsgView *msg_view);
64
65 /* size allocation handlers */
66 static void size_request (GtkWidget *widget, GtkRequisition *req, gpointer userdata);
67 static void size_allocate (GtkWidget *widget, GtkAllocation *alloc, gpointer userdata);
68 static void html_adjustment_changed (GtkAdjustment *adj, ModestMsgView * view);
69
70 /* list my signals */
71 enum {
72         LINK_CLICKED_SIGNAL,
73         LINK_HOVER_SIGNAL,
74         ATTACHMENT_CLICKED_SIGNAL,
75         RECPT_ACTIVATED_SIGNAL,
76         LAST_SIGNAL
77 };
78
79 typedef struct _ModestMsgViewPrivate ModestMsgViewPrivate;
80 struct _ModestMsgViewPrivate {
81         GtkWidget   *table;
82         GtkWidget   *gtkhtml;
83         GtkWidget   *mail_header_view;
84         GtkWidget   *attachments_view;
85
86         TnyMsg      *msg;
87
88         GtkWidget   *headers_box;
89         GtkWidget   *html_scroll;
90
91         guint full_width, full_height, html_height;
92
93         gulong  sig1, sig2, sig3;
94 };
95 #define MODEST_MSG_VIEW_GET_PRIVATE(o)      (G_TYPE_INSTANCE_GET_PRIVATE((o), \
96                                                  MODEST_TYPE_MSG_VIEW, \
97                                                  ModestMsgViewPrivate))
98 /* globals */
99 static GtkContainerClass *parent_class = NULL;
100
101 /* uncomment the following if you have defined any signals */
102 static guint signals[LAST_SIGNAL] = {0};
103
104 GType
105 modest_msg_view_get_type (void)
106 {
107         static GType my_type = 0;
108         if (!my_type) {
109                 static const GTypeInfo my_info = {
110                         sizeof(ModestMsgViewClass),
111                         NULL,           /* base init */
112                         NULL,           /* base finalize */
113                         (GClassInitFunc) modest_msg_view_class_init,
114                         NULL,           /* class finalize */
115                         NULL,           /* class data */
116                         sizeof(ModestMsgView),
117                         1,              /* n_preallocs */
118                         (GInstanceInitFunc) modest_msg_view_init,
119                         NULL
120                 };
121                 my_type = g_type_register_static (GTK_TYPE_VIEWPORT,
122                                                   "ModestMsgView",
123                                                   &my_info, 0);
124         }
125         return my_type;
126 }
127
128 static void
129 modest_msg_view_class_init (ModestMsgViewClass *klass)
130 {
131         GObjectClass *gobject_class;
132         GtkWidgetClass *widget_class;
133         gobject_class = (GObjectClass*) klass;
134         widget_class = (GtkWidgetClass *) klass;
135
136         parent_class            = g_type_class_peek_parent (klass);
137         gobject_class->finalize = modest_msg_view_finalize;
138
139         g_type_class_add_private (gobject_class, sizeof(ModestMsgViewPrivate));
140
141         signals[LINK_CLICKED_SIGNAL] =
142                 g_signal_new ("link_clicked",
143                               G_TYPE_FROM_CLASS (gobject_class),
144                               G_SIGNAL_RUN_FIRST,
145                               G_STRUCT_OFFSET(ModestMsgViewClass, link_clicked),
146                               NULL, NULL,
147                               g_cclosure_marshal_VOID__STRING,
148                               G_TYPE_NONE, 1, G_TYPE_STRING);
149         
150         signals[ATTACHMENT_CLICKED_SIGNAL] =
151                 g_signal_new ("attachment_clicked",
152                               G_TYPE_FROM_CLASS (gobject_class),
153                               G_SIGNAL_RUN_FIRST,
154                               G_STRUCT_OFFSET(ModestMsgViewClass, attachment_clicked),
155                               NULL, NULL,
156                               g_cclosure_marshal_VOID__POINTER,
157                               G_TYPE_NONE, 1, G_TYPE_INT);
158         
159         signals[LINK_HOVER_SIGNAL] =
160                 g_signal_new ("link_hover",
161                               G_TYPE_FROM_CLASS (gobject_class),
162                               G_SIGNAL_RUN_FIRST,
163                               G_STRUCT_OFFSET(ModestMsgViewClass, link_hover),
164                               NULL, NULL,
165                               g_cclosure_marshal_VOID__STRING,
166                               G_TYPE_NONE, 1, G_TYPE_STRING);
167
168         signals[RECPT_ACTIVATED_SIGNAL] =
169                 g_signal_new ("recpt_activated",
170                               G_TYPE_FROM_CLASS (gobject_class),
171                               G_SIGNAL_RUN_FIRST,
172                               G_STRUCT_OFFSET(ModestMsgViewClass, recpt_activated),
173                               NULL, NULL,
174                               g_cclosure_marshal_VOID__STRING,
175                               G_TYPE_NONE, 1, G_TYPE_STRING);
176 }
177
178 /* THIS IS A HACK: we modify the size requisition and allocation system to negociate the
179  * size of the GtkHtml so that it gets the correct height, and reports it to this widget
180  * to propagate the new allocation. It should make it work when it's included in a scrolled
181  * window with a viewport */
182
183 static void
184 size_request (GtkWidget *widget,
185               GtkRequisition *req,
186               gpointer userdata)
187 {
188         ModestMsgViewPrivate *priv = MODEST_MSG_VIEW_GET_PRIVATE (widget);
189         GtkRequisition req_headers;
190
191         g_message ("SIZE REQUEST START w %d h %d", req->width, req->height);
192
193         /* tries to allocate as much as possible of the current allocation for headers box */
194
195         req_headers.height = priv->full_height;
196         req_headers.width = req->width;
197         req->height = priv->full_height;
198
199         g_message ("SIZE REQUEST HEADER START w %d h %d", req_headers.width, req_headers.height);
200         gtk_widget_size_request (priv->headers_box, &req_headers);
201         g_message ("SIZE REQUEST HEADER END w %d h %d", req_headers.width, req_headers.height);
202         g_message ("SIZE REQUEST END w %d h %d", req->width, req->height);
203 }
204
205 static void
206 size_allocate (GtkWidget *widget,
207                GtkAllocation *alloc,
208                gpointer userdata)
209 {
210         ModestMsgViewPrivate *priv = MODEST_MSG_VIEW_GET_PRIVATE (widget);
211         GtkAllocation headers_alloc;
212         GtkAllocation html_alloc;
213         GtkAdjustment *hadj, *vadj;
214
215         g_message ("SIZE_ALLOCATE START w %d h %d", alloc->width, alloc->height);
216
217         priv->full_width = alloc->width;
218         priv->full_height = alloc->height;
219
220         hadj = gtk_viewport_get_hadjustment (GTK_VIEWPORT (widget));
221         vadj = gtk_viewport_get_vadjustment (GTK_VIEWPORT (widget));
222
223         /* allocates all the visible with for the header. The height is
224            taken from the last requisition of the widget, supposing it's
225            been calculated depending on this width */
226         headers_alloc.x = alloc->x;
227         headers_alloc.y = alloc->y;
228         headers_alloc.width = alloc->width;
229         headers_alloc.height = priv->headers_box->requisition.height;
230         if (priv->html_height != priv->gtkhtml->requisition.height)
231                 gtk_widget_size_allocate (priv->headers_box, &headers_alloc);
232
233         /* allocates the gtk html space trying to negociate that it takes
234          * the available space, and as much height as it needs. To do this,
235          * it takes the internal adjustment upper value (see html_adjustment_changed)
236          */
237         html_alloc.x = alloc->x;
238         html_alloc.y = alloc->y + headers_alloc.height;
239         html_alloc.width = alloc->width;
240         html_alloc.height = MAX(alloc->height, priv->html_height);
241         gtk_widget_size_allocate (priv->gtkhtml, &html_alloc);
242
243         /* Corrects the allocation of the full widget to include the final
244          * gtkhtml height */
245         priv->full_height = headers_alloc.height + priv->html_height;
246         alloc->height = priv->full_height;
247
248         g_message ("SIZE_ALLOCATE END w %d h %d", alloc->width, alloc->height);
249
250 }
251
252
253 static void
254 html_adjustment_changed (GtkAdjustment *adj,
255                          ModestMsgView * view)
256 {
257         ModestMsgViewPrivate *priv = MODEST_MSG_VIEW_GET_PRIVATE (view);
258
259         g_message ("ADJUSTMENT CHANGED START upper %f html_height %d", adj->upper, priv->html_height);
260         
261         /* correct the html height calculation depending on the range exposed
262          * by the html vertical adjustment
263          */
264         if (((gint) adj->upper) != priv->gtkhtml->allocation.height) {
265                 priv->html_height = (gint) adj->upper;
266         }
267         gtk_widget_queue_resize (GTK_WIDGET(view));
268 }
269
270
271 static void
272 modest_msg_view_init (ModestMsgView *obj)
273 {
274         ModestMsgViewPrivate *priv;
275         
276         priv = MODEST_MSG_VIEW_GET_PRIVATE(obj);
277
278
279         priv->full_width = 0;
280         priv->full_height = 0;
281         priv->html_height = G_MAXINT;
282
283         priv->table = gtk_table_new (2, 2, FALSE);
284
285         gtk_table_set_row_spacings (GTK_TABLE (priv->table), 0);
286         gtk_table_set_col_spacings (GTK_TABLE (priv->table), 0);
287
288         priv->html_scroll = gtk_scrolled_window_new (NULL, NULL);
289         gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (priv->html_scroll), GTK_POLICY_NEVER, GTK_POLICY_NEVER);
290
291         priv->msg                     = NULL;
292
293         priv->gtkhtml                 = gtk_html_new();
294         gtk_html_set_editable        (GTK_HTML(priv->gtkhtml), FALSE);
295         gtk_html_allow_selection     (GTK_HTML(priv->gtkhtml), TRUE);
296         gtk_html_set_caret_mode      (GTK_HTML(priv->gtkhtml), FALSE);
297         gtk_html_set_blocking        (GTK_HTML(priv->gtkhtml), FALSE);
298         gtk_html_set_images_blocking (GTK_HTML(priv->gtkhtml), FALSE);
299
300         priv->mail_header_view        = GTK_WIDGET(modest_mail_header_view_new ());
301         gtk_widget_set_no_show_all (priv->mail_header_view, TRUE);
302
303         priv->attachments_view        = GTK_WIDGET(modest_attachments_view_new (NULL));
304         gtk_widget_set_no_show_all (priv->attachments_view, TRUE);
305
306         priv->sig1 = g_signal_connect (G_OBJECT(priv->gtkhtml), "link_clicked",
307                                        G_CALLBACK(on_link_clicked), obj);
308         priv->sig2 = g_signal_connect (G_OBJECT(priv->gtkhtml), "url_requested",
309                                        G_CALLBACK(on_url_requested), obj);
310         priv->sig3 = g_signal_connect (G_OBJECT(priv->gtkhtml), "on_url",
311                                        G_CALLBACK(on_link_hover), obj);
312
313         g_signal_connect (G_OBJECT (priv->mail_header_view), "recpt-activated", 
314                           G_CALLBACK (on_recpt_activated), obj);
315
316         g_signal_connect (G_OBJECT (priv->attachments_view), "activate",
317                           G_CALLBACK (on_attachment_activated), obj);
318 }
319         
320
321 static void
322 modest_msg_view_finalize (GObject *obj)
323 {       
324         ModestMsgViewPrivate *priv;
325         priv = MODEST_MSG_VIEW_GET_PRIVATE (obj);
326
327         if (priv->msg) {
328                 g_object_unref (G_OBJECT(priv->msg));
329                 priv->msg = NULL;
330         }
331         
332         /* we cannot disconnect sigs, because priv->gtkhtml is
333          * already dead */
334         
335         priv->gtkhtml = NULL;
336         priv->attachments_view = NULL;
337
338         G_OBJECT_CLASS(parent_class)->finalize (obj);           
339 }
340
341 GtkWidget*
342 modest_msg_view_new (TnyMsg *msg)
343 {
344         GObject *obj;
345         ModestMsgView* self;
346         ModestMsgViewPrivate *priv;
347         
348         obj  = G_OBJECT(g_object_new(MODEST_TYPE_MSG_VIEW, NULL));
349         self = MODEST_MSG_VIEW(obj);
350         priv = MODEST_MSG_VIEW_GET_PRIVATE (self);
351
352         priv->headers_box = gtk_vbox_new (0, FALSE);
353
354         if (priv->mail_header_view)
355                 gtk_box_pack_start (GTK_BOX(priv->headers_box), priv->mail_header_view, FALSE, FALSE, 0);
356         
357         if (priv->attachments_view)
358                 gtk_box_pack_start (GTK_BOX(priv->headers_box), priv->attachments_view, FALSE, FALSE, 0);
359
360         gtk_table_attach (GTK_TABLE (priv->table), priv->headers_box, 0, 1, 0, 1, GTK_EXPAND, 0, 0, 0);
361         gtk_table_attach (GTK_TABLE (priv->table), gtk_label_new (""), 1, 2, 0, 1, 0, GTK_FILL|GTK_SHRINK, 0, 0);
362
363         if (priv->gtkhtml) {
364                 gtk_container_add (GTK_CONTAINER (priv->html_scroll), priv->gtkhtml);
365                 gtk_table_attach (GTK_TABLE(priv->table), priv->html_scroll, 0, 2, 1, 2, GTK_EXPAND, GTK_EXPAND, 0, 0);
366         }
367
368         gtk_container_add (GTK_CONTAINER (self), priv->table);
369
370         modest_msg_view_set_message (self, msg);
371
372         g_signal_connect (G_OBJECT (self), "size-request", G_CALLBACK (size_request), NULL);
373         g_signal_connect (G_OBJECT (self), "size-allocate", G_CALLBACK (size_allocate), NULL);
374         g_signal_connect (G_OBJECT (gtk_scrolled_window_get_vadjustment(GTK_SCROLLED_WINDOW(priv->html_scroll))), 
375                           "changed", G_CALLBACK (html_adjustment_changed), self);
376         
377         return GTK_WIDGET(self);
378 }
379
380 static void
381 on_recpt_activated (ModestMailHeaderView *header_view, 
382                     const gchar *address,
383                     ModestMsgView * view)
384 {
385         g_signal_emit (G_OBJECT (view), signals[RECPT_ACTIVATED_SIGNAL], 0, address);
386 }
387
388 static void
389 on_attachment_activated (ModestAttachmentsView * att_view, gint index, gpointer msg_view)
390 {
391
392         
393         if (index == 0) {
394                 /* index is 1-based, so 0 indicates an error */
395                 g_printerr ("modest: invalid attachment index: %d\n", index);
396                 return;
397         }
398
399         g_signal_emit (G_OBJECT(msg_view), signals[ATTACHMENT_CLICKED_SIGNAL],
400                        0, index);
401 }
402
403 static gboolean
404 on_link_clicked (GtkWidget *widget, const gchar *uri, ModestMsgView *msg_view)
405 {
406         g_return_val_if_fail (msg_view, FALSE);
407         
408         g_signal_emit (G_OBJECT(msg_view), signals[LINK_CLICKED_SIGNAL],
409                        0, uri);
410
411         return FALSE;
412 }
413
414
415 static gboolean
416 on_link_hover (GtkWidget *widget, const gchar *uri, ModestMsgView *msg_view)
417 {
418         g_signal_emit (G_OBJECT(msg_view), signals[LINK_HOVER_SIGNAL],
419                        0, uri);
420
421         return FALSE;
422 }
423
424
425
426 static TnyMimePart *
427 find_cid_image (TnyMsg *msg, const gchar *cid)
428 {
429         TnyMimePart *part = NULL;
430         TnyList *parts;
431         TnyIterator *iter;
432         
433         g_return_val_if_fail (msg, NULL);
434         g_return_val_if_fail (cid, NULL);
435         
436         parts  = TNY_LIST (tny_simple_list_new());
437
438         tny_mime_part_get_parts (TNY_MIME_PART (msg), parts); 
439         iter   = tny_list_create_iterator (parts);
440         
441         while (!tny_iterator_is_done(iter)) {
442                 const gchar *part_cid;
443                 part = TNY_MIME_PART(tny_iterator_get_current(iter));
444                 part_cid = tny_mime_part_get_content_id (part);
445
446                 if (part_cid && strcmp (cid, part_cid) == 0)
447                         break;
448
449                 g_object_unref (G_OBJECT(part));
450         
451                 part = NULL;
452                 tny_iterator_next (iter);
453         }
454         
455         g_object_unref (G_OBJECT(iter));        
456         g_object_unref (G_OBJECT(parts));
457         
458         return part;
459 }
460
461
462 static gboolean
463 on_url_requested (GtkWidget *widget, const gchar *uri,
464                   GtkHTMLStream *stream, ModestMsgView *msg_view)
465 {
466         ModestMsgViewPrivate *priv;
467         priv = MODEST_MSG_VIEW_GET_PRIVATE (msg_view);
468         
469         if (g_str_has_prefix (uri, "cid:")) {
470                 /* +4 ==> skip "cid:" */
471                 TnyMimePart *part = find_cid_image (priv->msg, uri + 4);
472                 if (!part) {
473                         g_printerr ("modest: '%s' not found\n", uri + 4);
474                         gtk_html_stream_close (stream, GTK_HTML_STREAM_ERROR);
475                 } else {
476                         TnyStream *tny_stream =
477                                 TNY_STREAM(modest_tny_stream_gtkhtml_new(stream));
478                         tny_mime_part_decode_to_stream ((TnyMimePart*)part,
479                                                                   tny_stream);
480                         gtk_html_stream_close (stream, GTK_HTML_STREAM_OK);
481         
482                         g_object_unref (G_OBJECT(tny_stream));
483                         g_object_unref (G_OBJECT(part));
484                 }
485         }
486
487         return TRUE;
488 }
489
490 static gboolean
491 set_html_message (ModestMsgView *self, TnyMimePart *tny_body, TnyMsg *msg)
492 {
493         GtkHTMLStream *gtkhtml_stream;
494         TnyStream *tny_stream;  
495         ModestMsgViewPrivate *priv;
496         
497         g_return_val_if_fail (self, FALSE);
498         g_return_val_if_fail (tny_body, FALSE);
499         
500         priv = MODEST_MSG_VIEW_GET_PRIVATE(self);
501
502         gtkhtml_stream = gtk_html_begin(GTK_HTML(priv->gtkhtml));
503
504         tny_stream     = TNY_STREAM(modest_tny_stream_gtkhtml_new (gtkhtml_stream));
505         tny_stream_reset (tny_stream);
506
507         tny_mime_part_decode_to_stream ((TnyMimePart*)tny_body, tny_stream);
508         g_object_unref (G_OBJECT(tny_stream));
509         
510         gtk_html_stream_destroy (gtkhtml_stream);
511         
512         return TRUE;
513 }
514
515
516 /* FIXME: this is a hack --> we use the tny_text_buffer_stream to
517  * get the message text, then write to gtkhtml 'by hand' */
518 static gboolean
519 set_text_message (ModestMsgView *self, TnyMimePart *tny_body, TnyMsg *msg)
520 {
521         GtkTextBuffer *buf;
522         GtkTextIter begin, end;
523         TnyStream* txt_stream, *tny_stream;
524         GtkHTMLStream *gtkhtml_stream;
525         gchar *txt;
526         ModestMsgViewPrivate *priv;
527                 
528         g_return_val_if_fail (self, FALSE);
529         g_return_val_if_fail (tny_body, FALSE);
530
531         priv           = MODEST_MSG_VIEW_GET_PRIVATE(self);
532         
533         buf            = gtk_text_buffer_new (NULL);
534         txt_stream     = TNY_STREAM(tny_gtk_text_buffer_stream_new (buf));
535                 
536         tny_stream_reset (txt_stream);
537
538         gtkhtml_stream = gtk_html_begin(GTK_HTML(priv->gtkhtml)); 
539         tny_stream =  TNY_STREAM(modest_tny_stream_gtkhtml_new (gtkhtml_stream));
540         
541         // FIXME: tinymail
542         tny_mime_part_decode_to_stream ((TnyMimePart*)tny_body, txt_stream);
543         tny_stream_reset (txt_stream);          
544         
545         gtk_text_buffer_get_bounds (buf, &begin, &end);
546         txt = gtk_text_buffer_get_text (buf, &begin, &end, FALSE);
547         if (txt) {
548                 gchar *html = modest_text_utils_convert_to_html (txt);
549                 tny_stream_write (tny_stream, html, strlen(html));
550                 tny_stream_reset (tny_stream);
551                 g_free (txt);
552                 g_free (html);
553         }
554         
555         g_object_unref (G_OBJECT(tny_stream));
556         g_object_unref (G_OBJECT(txt_stream));
557         g_object_unref (G_OBJECT(buf));
558         
559         gtk_html_stream_destroy (gtkhtml_stream);
560         
561         return TRUE;
562 }
563
564
565 static gboolean
566 set_empty_message (ModestMsgView *self)
567 {
568         ModestMsgViewPrivate *priv;
569         
570         g_return_val_if_fail (self, FALSE);
571         priv           = MODEST_MSG_VIEW_GET_PRIVATE(self);
572
573         gtk_html_load_from_string (GTK_HTML(priv->gtkhtml),
574                                    "", 1);
575         
576         return TRUE;
577 }
578
579
580 void
581 modest_msg_view_set_message (ModestMsgView *self, TnyMsg *msg)
582 {
583         TnyMimePart *body;
584         ModestMsgViewPrivate *priv;
585         TnyHeader *header;
586         
587         g_return_if_fail (self);
588         
589         priv = MODEST_MSG_VIEW_GET_PRIVATE(self);
590         gtk_widget_set_no_show_all (priv->mail_header_view, FALSE);
591
592         if (msg != priv->msg) {
593                 if (priv->msg)
594                         g_object_unref (G_OBJECT(priv->msg));
595                 if (msg)
596                         g_object_ref   (G_OBJECT(msg));
597                 priv->msg = msg;
598         }
599         
600         if (!msg) {
601                 tny_header_view_clear (TNY_HEADER_VIEW (priv->mail_header_view));
602                 gtk_widget_hide_all (priv->mail_header_view);
603                 gtk_widget_set_no_show_all (priv->mail_header_view, TRUE);
604                 set_empty_message (self);
605                 return;
606         }
607
608         header = tny_msg_get_header (msg);
609         tny_header_view_set_header (TNY_HEADER_VIEW (priv->mail_header_view), header);
610         g_object_unref (header);
611
612         modest_attachments_view_set_message (MODEST_ATTACHMENTS_VIEW(priv->attachments_view),
613                                              msg);
614         
615         body = modest_tny_msg_find_body_part (msg,TRUE);
616         if (body) {
617                 if (tny_mime_part_content_type_is (body, "text/html"))
618                         set_html_message (self, body, msg);
619                 else
620                         set_text_message (self, body, msg);
621         } else 
622                 set_empty_message (self);
623
624         gtk_widget_queue_resize (GTK_WIDGET (self));
625         
626         gtk_widget_show (priv->gtkhtml);
627         gtk_widget_show_all (priv->mail_header_view);
628         gtk_widget_set_no_show_all (priv->mail_header_view, TRUE);
629
630 }
631
632
633 TnyMsg*
634 modest_msg_view_get_message (ModestMsgView *self)
635 {
636         g_return_val_if_fail (self, NULL);
637         
638         return MODEST_MSG_VIEW_GET_PRIVATE(self)->msg;
639 }
640