1 /* Copyright (c) 2006, Nokia Corporation
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are
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.
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.
30 #include <tny-gtk-text-buffer-stream.h>
35 #include <glib/gi18n.h>
36 #include <gtkhtml/gtkhtml.h>
37 #include <gtkhtml/gtkhtml-stream.h>
39 #include <tny-simple-list.h>
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>
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);
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);
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);
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);
74 ATTACHMENT_CLICKED_SIGNAL,
75 RECPT_ACTIVATED_SIGNAL,
79 typedef struct _ModestMsgViewPrivate ModestMsgViewPrivate;
80 struct _ModestMsgViewPrivate {
83 GtkWidget *mail_header_view;
84 GtkWidget *attachments_view;
88 GtkWidget *headers_box;
89 GtkWidget *html_scroll;
91 guint full_width, full_height, html_height;
93 gulong sig1, sig2, sig3;
95 #define MODEST_MSG_VIEW_GET_PRIVATE(o) (G_TYPE_INSTANCE_GET_PRIVATE((o), \
96 MODEST_TYPE_MSG_VIEW, \
97 ModestMsgViewPrivate))
99 static GtkContainerClass *parent_class = NULL;
101 /* uncomment the following if you have defined any signals */
102 static guint signals[LAST_SIGNAL] = {0};
105 modest_msg_view_get_type (void)
107 static GType my_type = 0;
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),
118 (GInstanceInitFunc) modest_msg_view_init,
121 my_type = g_type_register_static (GTK_TYPE_VIEWPORT,
129 modest_msg_view_class_init (ModestMsgViewClass *klass)
131 GObjectClass *gobject_class;
132 GtkWidgetClass *widget_class;
133 gobject_class = (GObjectClass*) klass;
134 widget_class = (GtkWidgetClass *) klass;
136 parent_class = g_type_class_peek_parent (klass);
137 gobject_class->finalize = modest_msg_view_finalize;
139 g_type_class_add_private (gobject_class, sizeof(ModestMsgViewPrivate));
141 signals[LINK_CLICKED_SIGNAL] =
142 g_signal_new ("link_clicked",
143 G_TYPE_FROM_CLASS (gobject_class),
145 G_STRUCT_OFFSET(ModestMsgViewClass, link_clicked),
147 g_cclosure_marshal_VOID__STRING,
148 G_TYPE_NONE, 1, G_TYPE_STRING);
150 signals[ATTACHMENT_CLICKED_SIGNAL] =
151 g_signal_new ("attachment_clicked",
152 G_TYPE_FROM_CLASS (gobject_class),
154 G_STRUCT_OFFSET(ModestMsgViewClass, attachment_clicked),
156 g_cclosure_marshal_VOID__POINTER,
157 G_TYPE_NONE, 1, G_TYPE_INT);
159 signals[LINK_HOVER_SIGNAL] =
160 g_signal_new ("link_hover",
161 G_TYPE_FROM_CLASS (gobject_class),
163 G_STRUCT_OFFSET(ModestMsgViewClass, link_hover),
165 g_cclosure_marshal_VOID__STRING,
166 G_TYPE_NONE, 1, G_TYPE_STRING);
168 signals[RECPT_ACTIVATED_SIGNAL] =
169 g_signal_new ("recpt_activated",
170 G_TYPE_FROM_CLASS (gobject_class),
172 G_STRUCT_OFFSET(ModestMsgViewClass, recpt_activated),
174 g_cclosure_marshal_VOID__STRING,
175 G_TYPE_NONE, 1, G_TYPE_STRING);
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 */
184 size_request (GtkWidget *widget,
188 ModestMsgViewPrivate *priv = MODEST_MSG_VIEW_GET_PRIVATE (widget);
189 GtkRequisition req_headers;
191 g_message ("SIZE REQUEST START w %d h %d", req->width, req->height);
193 /* tries to allocate as much as possible of the current allocation for headers box */
195 req_headers.height = priv->full_height;
196 req_headers.width = req->width;
197 req->height = priv->full_height;
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);
206 size_allocate (GtkWidget *widget,
207 GtkAllocation *alloc,
210 ModestMsgViewPrivate *priv = MODEST_MSG_VIEW_GET_PRIVATE (widget);
211 GtkAllocation headers_alloc;
212 GtkAllocation html_alloc;
213 GtkAdjustment *hadj, *vadj;
215 g_message ("SIZE_ALLOCATE START w %d h %d", alloc->width, alloc->height);
217 priv->full_width = alloc->width;
218 priv->full_height = alloc->height;
220 hadj = gtk_viewport_get_hadjustment (GTK_VIEWPORT (widget));
221 vadj = gtk_viewport_get_vadjustment (GTK_VIEWPORT (widget));
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);
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)
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);
243 /* Corrects the allocation of the full widget to include the final
245 priv->full_height = headers_alloc.height + priv->html_height;
246 alloc->height = priv->full_height;
248 g_message ("SIZE_ALLOCATE END w %d h %d", alloc->width, alloc->height);
254 html_adjustment_changed (GtkAdjustment *adj,
255 ModestMsgView * view)
257 ModestMsgViewPrivate *priv = MODEST_MSG_VIEW_GET_PRIVATE (view);
259 g_message ("ADJUSTMENT CHANGED START upper %f html_height %d", adj->upper, priv->html_height);
261 /* correct the html height calculation depending on the range exposed
262 * by the html vertical adjustment
264 if (((gint) adj->upper) != priv->gtkhtml->allocation.height) {
265 priv->html_height = (gint) adj->upper;
267 gtk_widget_queue_resize (GTK_WIDGET(view));
272 modest_msg_view_init (ModestMsgView *obj)
274 ModestMsgViewPrivate *priv;
276 priv = MODEST_MSG_VIEW_GET_PRIVATE(obj);
279 priv->full_width = 0;
280 priv->full_height = 0;
281 priv->html_height = G_MAXINT;
283 priv->table = gtk_table_new (2, 2, FALSE);
285 gtk_table_set_row_spacings (GTK_TABLE (priv->table), 0);
286 gtk_table_set_col_spacings (GTK_TABLE (priv->table), 0);
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);
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);
300 priv->mail_header_view = GTK_WIDGET(modest_mail_header_view_new ());
301 gtk_widget_set_no_show_all (priv->mail_header_view, TRUE);
303 priv->attachments_view = GTK_WIDGET(modest_attachments_view_new (NULL));
304 gtk_widget_set_no_show_all (priv->attachments_view, TRUE);
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);
313 g_signal_connect (G_OBJECT (priv->mail_header_view), "recpt-activated",
314 G_CALLBACK (on_recpt_activated), obj);
316 g_signal_connect (G_OBJECT (priv->attachments_view), "activate",
317 G_CALLBACK (on_attachment_activated), obj);
322 modest_msg_view_finalize (GObject *obj)
324 ModestMsgViewPrivate *priv;
325 priv = MODEST_MSG_VIEW_GET_PRIVATE (obj);
328 g_object_unref (G_OBJECT(priv->msg));
332 /* we cannot disconnect sigs, because priv->gtkhtml is
335 priv->gtkhtml = NULL;
336 priv->attachments_view = NULL;
338 G_OBJECT_CLASS(parent_class)->finalize (obj);
342 modest_msg_view_new (TnyMsg *msg)
346 ModestMsgViewPrivate *priv;
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);
352 priv->headers_box = gtk_vbox_new (0, FALSE);
354 if (priv->mail_header_view)
355 gtk_box_pack_start (GTK_BOX(priv->headers_box), priv->mail_header_view, FALSE, FALSE, 0);
357 if (priv->attachments_view)
358 gtk_box_pack_start (GTK_BOX(priv->headers_box), priv->attachments_view, FALSE, FALSE, 0);
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);
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);
368 gtk_container_add (GTK_CONTAINER (self), priv->table);
370 modest_msg_view_set_message (self, msg);
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);
377 return GTK_WIDGET(self);
381 on_recpt_activated (ModestMailHeaderView *header_view,
382 const gchar *address,
383 ModestMsgView * view)
385 g_signal_emit (G_OBJECT (view), signals[RECPT_ACTIVATED_SIGNAL], 0, address);
389 on_attachment_activated (ModestAttachmentsView * att_view, gint index, gpointer msg_view)
394 /* index is 1-based, so 0 indicates an error */
395 g_printerr ("modest: invalid attachment index: %d\n", index);
399 g_signal_emit (G_OBJECT(msg_view), signals[ATTACHMENT_CLICKED_SIGNAL],
404 on_link_clicked (GtkWidget *widget, const gchar *uri, ModestMsgView *msg_view)
406 g_return_val_if_fail (msg_view, FALSE);
408 g_signal_emit (G_OBJECT(msg_view), signals[LINK_CLICKED_SIGNAL],
416 on_link_hover (GtkWidget *widget, const gchar *uri, ModestMsgView *msg_view)
418 g_signal_emit (G_OBJECT(msg_view), signals[LINK_HOVER_SIGNAL],
427 find_cid_image (TnyMsg *msg, const gchar *cid)
429 TnyMimePart *part = NULL;
433 g_return_val_if_fail (msg, NULL);
434 g_return_val_if_fail (cid, NULL);
436 parts = TNY_LIST (tny_simple_list_new());
438 tny_mime_part_get_parts (TNY_MIME_PART (msg), parts);
439 iter = tny_list_create_iterator (parts);
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);
446 if (part_cid && strcmp (cid, part_cid) == 0)
449 g_object_unref (G_OBJECT(part));
452 tny_iterator_next (iter);
455 g_object_unref (G_OBJECT(iter));
456 g_object_unref (G_OBJECT(parts));
463 on_url_requested (GtkWidget *widget, const gchar *uri,
464 GtkHTMLStream *stream, ModestMsgView *msg_view)
466 ModestMsgViewPrivate *priv;
467 priv = MODEST_MSG_VIEW_GET_PRIVATE (msg_view);
469 if (g_str_has_prefix (uri, "cid:")) {
470 /* +4 ==> skip "cid:" */
471 TnyMimePart *part = find_cid_image (priv->msg, uri + 4);
473 g_printerr ("modest: '%s' not found\n", uri + 4);
474 gtk_html_stream_close (stream, GTK_HTML_STREAM_ERROR);
476 TnyStream *tny_stream =
477 TNY_STREAM(modest_tny_stream_gtkhtml_new(stream));
478 tny_mime_part_decode_to_stream ((TnyMimePart*)part,
480 gtk_html_stream_close (stream, GTK_HTML_STREAM_OK);
482 g_object_unref (G_OBJECT(tny_stream));
483 g_object_unref (G_OBJECT(part));
491 set_html_message (ModestMsgView *self, TnyMimePart *tny_body, TnyMsg *msg)
493 GtkHTMLStream *gtkhtml_stream;
494 TnyStream *tny_stream;
495 ModestMsgViewPrivate *priv;
497 g_return_val_if_fail (self, FALSE);
498 g_return_val_if_fail (tny_body, FALSE);
500 priv = MODEST_MSG_VIEW_GET_PRIVATE(self);
502 gtkhtml_stream = gtk_html_begin(GTK_HTML(priv->gtkhtml));
504 tny_stream = TNY_STREAM(modest_tny_stream_gtkhtml_new (gtkhtml_stream));
505 tny_stream_reset (tny_stream);
507 tny_mime_part_decode_to_stream ((TnyMimePart*)tny_body, tny_stream);
508 g_object_unref (G_OBJECT(tny_stream));
510 gtk_html_stream_destroy (gtkhtml_stream);
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' */
519 set_text_message (ModestMsgView *self, TnyMimePart *tny_body, TnyMsg *msg)
522 GtkTextIter begin, end;
523 TnyStream* txt_stream, *tny_stream;
524 GtkHTMLStream *gtkhtml_stream;
526 ModestMsgViewPrivate *priv;
528 g_return_val_if_fail (self, FALSE);
529 g_return_val_if_fail (tny_body, FALSE);
531 priv = MODEST_MSG_VIEW_GET_PRIVATE(self);
533 buf = gtk_text_buffer_new (NULL);
534 txt_stream = TNY_STREAM(tny_gtk_text_buffer_stream_new (buf));
536 tny_stream_reset (txt_stream);
538 gtkhtml_stream = gtk_html_begin(GTK_HTML(priv->gtkhtml));
539 tny_stream = TNY_STREAM(modest_tny_stream_gtkhtml_new (gtkhtml_stream));
542 tny_mime_part_decode_to_stream ((TnyMimePart*)tny_body, txt_stream);
543 tny_stream_reset (txt_stream);
545 gtk_text_buffer_get_bounds (buf, &begin, &end);
546 txt = gtk_text_buffer_get_text (buf, &begin, &end, FALSE);
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);
555 g_object_unref (G_OBJECT(tny_stream));
556 g_object_unref (G_OBJECT(txt_stream));
557 g_object_unref (G_OBJECT(buf));
559 gtk_html_stream_destroy (gtkhtml_stream);
566 set_empty_message (ModestMsgView *self)
568 ModestMsgViewPrivate *priv;
570 g_return_val_if_fail (self, FALSE);
571 priv = MODEST_MSG_VIEW_GET_PRIVATE(self);
573 gtk_html_load_from_string (GTK_HTML(priv->gtkhtml),
581 modest_msg_view_set_message (ModestMsgView *self, TnyMsg *msg)
584 ModestMsgViewPrivate *priv;
587 g_return_if_fail (self);
589 priv = MODEST_MSG_VIEW_GET_PRIVATE(self);
590 gtk_widget_set_no_show_all (priv->mail_header_view, FALSE);
592 if (msg != priv->msg) {
594 g_object_unref (G_OBJECT(priv->msg));
596 g_object_ref (G_OBJECT(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);
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);
612 modest_attachments_view_set_message (MODEST_ATTACHMENTS_VIEW(priv->attachments_view),
615 body = modest_tny_msg_find_body_part (msg,TRUE);
617 if (tny_mime_part_content_type_is (body, "text/html"))
618 set_html_message (self, body, msg);
620 set_text_message (self, body, msg);
622 set_empty_message (self);
624 gtk_widget_queue_resize (GTK_WIDGET (self));
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);
634 modest_msg_view_get_message (ModestMsgView *self)
636 g_return_val_if_fail (self, NULL);
638 return MODEST_MSG_VIEW_GET_PRIVATE(self)->msg;