1 /* modest-tny-msg-view.c */
3 /* insert (c)/licensing information) */
5 #include "modest-tny-msg-view.h"
6 #include "modest-tny-stream-gtkhtml.h"
7 #include "modest-tny-msg-actions.h"
9 #include <tny-text-buffer-stream.h>
13 #include <glib/gi18n.h>
14 #include <gtkhtml/gtkhtml.h>
15 #include <gtkhtml/gtkhtml-stream.h>
17 /* 'private'/'protected' functions */
18 static void modest_tny_msg_view_class_init (ModestTnyMsgViewClass *klass);
19 static void modest_tny_msg_view_init (ModestTnyMsgView *obj);
20 static void modest_tny_msg_view_finalize (GObject *obj);
23 static GSList* get_url_matches (GString *txt);
24 static gboolean fill_gtkhtml_with_txt (ModestTnyMsgView *self, GtkHTML* gtkhtml, const gchar* txt, TnyMsgIface *msg);
26 static gboolean on_link_clicked (GtkWidget *widget, const gchar *uri,
27 ModestTnyMsgView *msg_view);
28 static gboolean on_url_requested (GtkWidget *widget, const gchar *uri,
29 GtkHTMLStream *stream,
30 ModestTnyMsgView *msg_view);
31 static gchar *construct_virtual_filename(const gchar *filename, const gint position, const gchar *id, const gboolean active);
32 static gchar *construct_virtual_filename_from_mime_part(TnyMsgMimePartIface *msg, const gint position);
33 gint virtual_filename_get_pos(const gchar *filename);
35 * we need these regexps to find URLs in plain text e-mails
37 typedef struct _UrlMatchPattern UrlMatchPattern;
38 struct _UrlMatchPattern {
44 #define MAIL_VIEWER_URL_MATCH_PATTERNS {\
45 { "(file|http|ftp|https)://[-A-Za-z0-9_$.+!*(),;:@%&=?/~#]+[-A-Za-z0-9_$%&=?/~#]",\
47 { "www\\.[-a-z0-9.]+[-a-z0-9](:[0-9]*)?(/[-A-Za-z0-9_$.+!*(),;:@%&=?/~#]*[^]}\\),?!;:\"]?)?",\
49 { "ftp\\.[-a-z0-9.]+[-a-z0-9](:[0-9]*)?(/[-A-Za-z0-9_$.+!*(),;:@%&=?/~#]*[^]}\\),?!;:\"]?)?",\
51 { "(voipto|callto|chatto|jabberto|xmpp):[-_a-z@0-9.\\+]+", \
53 { "mailto:[-_a-z0-9.\\+]+@[-_a-z0-9.]+", \
55 { "[-_a-z0-9.\\+]+@[-_a-z0-9.]+",\
67 typedef struct _ModestTnyMsgViewPrivate ModestTnyMsgViewPrivate;
68 struct _ModestTnyMsgViewPrivate {
73 #define MODEST_TNY_MSG_VIEW_GET_PRIVATE(o) (G_TYPE_INSTANCE_GET_PRIVATE((o), \
74 MODEST_TYPE_TNY_MSG_VIEW, \
75 ModestTnyMsgViewPrivate))
77 static GtkContainerClass *parent_class = NULL;
79 /* uncomment the following if you have defined any signals */
80 /* static guint signals[LAST_SIGNAL] = {0}; */
83 modest_tny_msg_view_get_type (void)
85 static GType my_type = 0;
87 static const GTypeInfo my_info = {
88 sizeof(ModestTnyMsgViewClass),
90 NULL, /* base finalize */
91 (GClassInitFunc) modest_tny_msg_view_class_init,
92 NULL, /* class finalize */
93 NULL, /* class data */
94 sizeof(ModestTnyMsgView),
96 (GInstanceInitFunc) modest_tny_msg_view_init,
98 my_type = g_type_register_static (GTK_TYPE_SCROLLED_WINDOW,
106 modest_tny_msg_view_class_init (ModestTnyMsgViewClass *klass)
108 GObjectClass *gobject_class;
109 gobject_class = (GObjectClass*) klass;
111 parent_class = g_type_class_peek_parent (klass);
112 gobject_class->finalize = modest_tny_msg_view_finalize;
114 g_type_class_add_private (gobject_class, sizeof(ModestTnyMsgViewPrivate));
118 modest_tny_msg_view_init (ModestTnyMsgView *obj)
120 ModestTnyMsgViewPrivate *priv;
122 priv = MODEST_TNY_MSG_VIEW_GET_PRIVATE(obj);
126 priv->gtkhtml = gtk_html_new();
130 gtk_html_set_editable (GTK_HTML(priv->gtkhtml), FALSE);
131 gtk_html_allow_selection (GTK_HTML(priv->gtkhtml), TRUE);
132 gtk_html_set_caret_mode (GTK_HTML(priv->gtkhtml), FALSE);
133 gtk_html_set_blocking (GTK_HTML(priv->gtkhtml), FALSE);
134 gtk_html_set_images_blocking (GTK_HTML(priv->gtkhtml), FALSE);
136 g_signal_connect (G_OBJECT(priv->gtkhtml), "link_clicked",
137 G_CALLBACK(on_link_clicked), obj);
139 g_signal_connect (G_OBJECT(priv->gtkhtml), "url_requested",
140 G_CALLBACK(on_url_requested), obj);
145 modest_tny_msg_view_finalize (GObject *obj)
151 modest_tny_msg_view_new (TnyMsgIface *msg, ModestConf *conf)
154 ModestTnyMsgView* self;
155 ModestTnyMsgViewPrivate *priv;
157 obj = G_OBJECT(g_object_new(MODEST_TYPE_TNY_MSG_VIEW, NULL));
158 self = MODEST_TNY_MSG_VIEW(obj);
159 priv = MODEST_TNY_MSG_VIEW_GET_PRIVATE (self);
161 gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(self),
162 GTK_POLICY_AUTOMATIC,
163 GTK_POLICY_AUTOMATIC);
166 gtk_container_add (GTK_CONTAINER(obj), priv->gtkhtml);
169 modest_tny_msg_view_set_message (self, msg);
173 return GTK_WIDGET(self);
179 on_link_clicked (GtkWidget *widget, const gchar *uri,
180 ModestTnyMsgView *msg_view)
182 ModestTnyMsgViewPrivate *priv;
185 if (g_str_has_prefix(uri, "attachment:")) {
186 priv = MODEST_TNY_MSG_VIEW_GET_PRIVATE(msg_view);
187 /* toggle ...SHOW_ATTACHMENTS_INLINE */
188 modest_conf_set_bool(priv->conf,
189 MODEST_CONF_MSG_VIEW_SHOW_ATTACHMENTS_INLINE,
190 !modest_conf_get_bool(priv->conf,
191 MODEST_CONF_MSG_VIEW_SHOW_ATTACHMENTS_INLINE,
195 modest_tny_msg_view_set_message(msg_view, priv->msg);
198 g_message ("link clicked: %s", uri); /* FIXME */
205 static TnyMsgMimePartIface *
206 find_cid_image (TnyMsgIface *msg, const gchar *cid)
208 TnyMsgMimePartIface *part = NULL;
211 g_return_val_if_fail (msg, NULL);
212 g_return_val_if_fail (cid, NULL);
214 parts = (GList*) tny_msg_iface_get_parts (msg);
215 while (parts && !part) {
216 const gchar *part_cid;
217 part = TNY_MSG_MIME_PART_IFACE(parts->data);
218 part_cid = tny_msg_mime_part_iface_get_content_id (part);
219 printf("CMP:%s:%s\n", cid, part_cid);
220 if (part_cid && strcmp (cid, part_cid) == 0)
221 return part; /* we found it! */
231 static TnyMsgMimePartIface *
232 find_attachment_by_filename (TnyMsgIface *msg, const gchar *fn)
234 TnyMsgMimePartIface *part = NULL;
239 g_return_val_if_fail (msg, NULL);
240 g_return_val_if_fail (fn, NULL);
242 parts = (GList*) tny_msg_iface_get_parts (msg);
243 pos = virtual_filename_get_pos(fn);
245 g_return_val_if_fail(((pos >= 0) && (pos < g_list_length(parts))), NULL);
247 part = g_list_nth_data(parts, pos);
249 dummy = construct_virtual_filename_from_mime_part(part, pos);
250 if (strcmp(dummy, fn) == 0) {
261 on_url_requested (GtkWidget *widget, const gchar *uri,
262 GtkHTMLStream *stream,
263 ModestTnyMsgView *msg_view)
266 ModestTnyMsgViewPrivate *priv;
267 priv = MODEST_TNY_MSG_VIEW_GET_PRIVATE (msg_view);
269 g_message ("url requested: %s", uri);
271 if (g_str_has_prefix (uri, "cid:")) {
272 /* +4 ==> skip "cid:" */
274 TnyMsgMimePartIface *part = find_cid_image (priv->msg, uri + 4);
276 g_message ("%s not found", uri + 4);
277 gtk_html_stream_close (stream, GTK_HTML_STREAM_ERROR);
279 TnyStreamIface *tny_stream =
280 TNY_STREAM_IFACE(modest_tny_stream_gtkhtml_new(stream));
281 tny_msg_mime_part_iface_decode_to_stream (part,tny_stream);
282 gtk_html_stream_close (stream, GTK_HTML_STREAM_OK);
284 } else if (g_str_has_prefix (uri, "Attachment:")) {
285 TnyMsgMimePartIface *part = find_attachment_by_filename (priv->msg, uri);
287 g_message ("%s not found", uri);
288 gtk_html_stream_close (stream, GTK_HTML_STREAM_ERROR);
290 TnyStreamIface *tny_stream =
291 TNY_STREAM_IFACE(modest_tny_stream_gtkhtml_new(stream));
292 tny_msg_mime_part_iface_decode_to_stream (part,tny_stream);
293 gtk_html_stream_close (stream, GTK_HTML_STREAM_OK);
311 construct_virtual_filename(const gchar *filename, const gint position, const gchar *id, const gboolean active)
314 g_return_val_if_fail((position >= 0), "AttachmentInvalid");
316 s = g_string_new("");
318 g_string_append(s, "Attachment:");
320 g_string_append(s, "attachment:");
321 g_string_append_printf(s, "%d:", position);
323 g_string_append(s, id);
324 g_string_append_c(s, ':');
326 g_string_append(s, filename);
327 g_string_append_c(s, ':');
328 return g_string_free(s, FALSE);
333 construct_virtual_filename_from_mime_part(TnyMsgMimePartIface *msg, const gint position)
335 const gchar *id, *filename;
336 const gboolean active = TRUE;
338 filename = tny_msg_mime_part_iface_get_filename(
339 TNY_MSG_MIME_PART_IFACE(msg));
341 filename = "[unknown]";
342 id = tny_msg_mime_part_iface_get_content_id(
343 TNY_MSG_MIME_PART_IFACE(msg));
345 return construct_virtual_filename(filename, position, id, active);
349 get_next_token(const gchar *s, gint *len)
366 /* maybe I should use libregexp */
368 virtual_filename_get_pos(const gchar *filename)
370 const gchar *i1, *i2;
378 i2 = get_next_token(i2, &len);
379 if (strncmp(i1, "Attachment", len) != 0)
384 i2 = get_next_token(i2, &len);
387 dummy = g_string_new_len(i1, len);
388 pos = atoi(dummy->str);
389 g_string_free(dummy, FALSE);
395 attachments_as_html(ModestTnyMsgView *self, TnyMsgIface *msg)
397 ModestTnyMsgViewPrivate *priv;
398 gboolean attachments_found = FALSE;
400 const GList *attachment_list, *attachment;
401 const gchar *content_type, *filename, *id;
402 gchar *virtual_filename;
403 gboolean show_attachments_inline;
408 priv = MODEST_TNY_MSG_VIEW_GET_PRIVATE (self);
410 /* CLEANUP: starting a new HTML may be unsupported */
411 appendix = g_string_new("<HTML><BODY>\n<hr><h5>Attachments:</h5>\n");
413 attachment_list = tny_msg_iface_get_parts(msg);
414 attachment = attachment_list;
417 content_type = tny_msg_mime_part_iface_get_content_type(
418 TNY_MSG_MIME_PART_IFACE(attachment->data));
419 g_return_val_if_fail(content_type, NULL);
420 if ( tny_msg_mime_part_iface_content_type_is(
421 TNY_MSG_MIME_PART_IFACE(attachment->data),
423 || tny_msg_mime_part_iface_content_type_is(
424 TNY_MSG_MIME_PART_IFACE(attachment->data),
426 filename = tny_msg_mime_part_iface_get_filename(
427 TNY_MSG_MIME_PART_IFACE(attachment->data));
429 filename = "[unknown]";
431 attachments_found = TRUE;
432 id = tny_msg_mime_part_iface_get_content_id(
433 TNY_MSG_MIME_PART_IFACE(attachment->data));
434 show_attachments_inline = modest_conf_get_bool(priv->conf, MODEST_CONF_MSG_VIEW_SHOW_ATTACHMENTS_INLINE, NULL);
435 virtual_filename = construct_virtual_filename(filename,
436 g_list_position((GList *)attachment_list, (GList *) attachment),
437 id, show_attachments_inline);
438 printf("VF:%s\n", virtual_filename);
439 if (show_attachments_inline) {
440 g_string_append_printf(appendix, "<IMG src=\"%s\">\n<BR><A href=\"attachment:%s\">%s</A>\n", virtual_filename, filename, filename);
442 g_string_append_printf(appendix, "<A href=\"attachment:%s\">%s</A>: %s<BR>\n", filename, filename, content_type);
444 g_free(virtual_filename);
446 attachment = attachment->next;
448 g_string_append(appendix, "</BODY></HTML>");
449 if (!attachments_found)
450 g_string_assign(appendix, "");
451 return g_string_free(appendix, FALSE);
455 hyperlinkify_plain_text (GString *txt)
458 GSList *match_list = get_url_matches (txt);
460 /* we will work backwards, so the offsets stay valid */
461 for (cursor = match_list; cursor; cursor = cursor->next) {
463 url_match_t *match = (url_match_t*) cursor->data;
464 gchar *url = g_strndup (txt->str + match->offset, match->len);
465 gchar *repl = NULL; /* replacement */
467 /* the prefix is NULL: use the one that is already there */
468 repl = g_strdup_printf ("<a href=\"%s%s\">%s</a>",
469 match->prefix ? match->prefix : "", url, url);
471 /* replace the old thing with our hyperlink
472 * replacement thing */
473 g_string_erase (txt, match->offset, match->len);
474 g_string_insert (txt, match->offset, repl);
479 g_free (cursor->data);
482 g_slist_free (match_list);
488 convert_to_html (const gchar *data)
491 gboolean first_space = TRUE;
499 html = g_string_sized_new (len + 100); /* just a guess... */
501 g_string_append_printf (html,
504 "<meta http-equiv=\"content-type\""
505 " content=\"text/html; charset=utf8\">"
509 /* replace with special html chars where needed*/
510 for (i = 0; i != len; ++i) {
514 case 0: break; /* ignore embedded \0s */
515 case '<' : g_string_append (html, "<"); break;
516 case '>' : g_string_append (html, ">"); break;
517 case '&' : g_string_append (html, """); break;
518 case '\n': g_string_append (html, "<br>\n"); break;
521 g_string_append (html, first_space ? " " : " ");
523 } else if (kar == '\t')
524 g_string_append (html, " ");
528 /* optimization trick: accumulate 'normal' chars, then copy */
530 kar = data [++charnum + i];
532 } while ((i + charnum < len) &&
533 (kar > '>' || (kar != '<' && kar != '>'
534 && kar != '&' && kar != ' '
535 && kar != '\n' && kar != '\t')));
536 g_string_append_len (html, &data[i], charnum);
542 g_string_append (html, "</tt></body></html>");
543 hyperlinkify_plain_text (html);
545 return g_string_free (html, FALSE);
552 cmp_offsets_reverse (const url_match_t *match1, const url_match_t *match2)
554 return match2->offset - match1->offset;
560 * check if the match is inside an existing match... */
562 chk_partial_match (const url_match_t *match, int* offset)
564 if (*offset >= match->offset && *offset < match->offset + match->len)
569 get_url_matches (GString *txt)
572 int rv, i, offset = 0;
573 GSList *match_list = NULL;
575 static UrlMatchPattern patterns[] = MAIL_VIEWER_URL_MATCH_PATTERNS;
576 const size_t pattern_num = sizeof(patterns)/sizeof(UrlMatchPattern);
578 /* initalize the regexps */
579 for (i = 0; i != pattern_num; ++i) {
580 patterns[i].preg = g_new0 (regex_t,1);
581 g_assert(regcomp (patterns[i].preg, patterns[i].regex,
582 REG_ICASE|REG_EXTENDED|REG_NEWLINE) == 0);
584 /* find all the matches */
585 for (i = 0; i != pattern_num; ++i) {
589 if ((rv = regexec (patterns[i].preg, txt->str + offset, 1, &rm, 0)) != 0) {
590 g_assert (rv == REG_NOMATCH); /* this should not happen */
591 break; /* try next regexp */
596 /* FIXME: optimize this */
597 /* to avoid partial matches on something that was already found... */
598 /* check_partial_match will put -1 in the data ptr if that is the case */
599 test_offset = offset + rm.rm_so;
600 g_slist_foreach (match_list, (GFunc)chk_partial_match, &test_offset);
602 /* make a list of our matches (<offset, len, prefix> tupels)*/
603 if (test_offset != -1) {
604 url_match_t *match = g_new (url_match_t,1);
605 match->offset = offset + rm.rm_so;
606 match->len = rm.rm_eo - rm.rm_so;
607 match->prefix = patterns[i].prefix;
608 match_list = g_slist_prepend (match_list, match);
614 for (i = 0; i != pattern_num; ++i) {
615 regfree (patterns[i].preg);
616 g_free (patterns[i].preg);
617 } /* don't free patterns itself -- it's static */
619 /* now sort the list, so the matches are in reverse order of occurence.
620 * that way, we can do the replacements starting from the end, so we don't need
621 * to recalculate the offsets
623 match_list = g_slist_sort (match_list,
624 (GCompareFunc)cmp_offsets_reverse);
629 fill_gtkhtml_with_txt (ModestTnyMsgView *self, GtkHTML* gtkhtml, const gchar* txt, TnyMsgIface *msg)
632 gchar *html_attachments;
634 g_return_val_if_fail (gtkhtml, FALSE);
635 g_return_val_if_fail (txt, FALSE);
637 html = g_string_new(convert_to_html (txt));
638 html_attachments = attachments_as_html(self, msg);
639 g_string_append(html, html_attachments);
641 gtk_html_load_from_string (gtkhtml, html->str, html->len);
642 g_string_free (html, TRUE);
643 g_free(html_attachments);
651 set_html_message (ModestTnyMsgView *self, TnyMsgMimePartIface *tny_body, TnyMsgIface *msg)
653 gchar *html_attachments;
654 TnyStreamIface *gtkhtml_stream;
655 ModestTnyMsgViewPrivate *priv;
657 g_return_val_if_fail (self, FALSE);
658 g_return_val_if_fail (tny_body, FALSE);
660 priv = MODEST_TNY_MSG_VIEW_GET_PRIVATE(self);
663 TNY_STREAM_IFACE(modest_tny_stream_gtkhtml_new
664 (gtk_html_begin(GTK_HTML(priv->gtkhtml))));
666 tny_stream_iface_reset (gtkhtml_stream);
667 tny_msg_mime_part_iface_decode_to_stream (tny_body, gtkhtml_stream);
668 html_attachments = attachments_as_html(self, msg);
670 gtkhtml_write(gtkhtml_stream, html_attachments, strlen(html_attachments));
671 tny_stream_iface_reset (gtkhtml_stream);
673 g_object_unref (G_OBJECT(gtkhtml_stream));
674 g_free (html_attachments);
680 /* this is a hack --> we use the tny_text_buffer_stream to
681 * get the message text, then write to gtkhtml 'by hand' */
683 set_text_message (ModestTnyMsgView *self, TnyMsgMimePartIface *tny_body, TnyMsgIface *msg)
686 GtkTextIter begin, end;
687 TnyStreamIface* txt_stream;
689 ModestTnyMsgViewPrivate *priv;
691 g_return_val_if_fail (self, FALSE);
692 g_return_val_if_fail (tny_body, FALSE);
694 priv = MODEST_TNY_MSG_VIEW_GET_PRIVATE(self);
696 buf = gtk_text_buffer_new (NULL);
697 txt_stream = TNY_STREAM_IFACE(tny_text_buffer_stream_new (buf));
699 tny_stream_iface_reset (txt_stream);
700 tny_msg_mime_part_iface_decode_to_stream (tny_body, txt_stream);
701 tny_stream_iface_reset (txt_stream);
703 gtk_text_buffer_get_bounds (buf, &begin, &end);
704 txt = gtk_text_buffer_get_text (buf, &begin, &end, FALSE);
706 fill_gtkhtml_with_txt (self, GTK_HTML(priv->gtkhtml), txt, msg);
708 g_object_unref (G_OBJECT(txt_stream));
709 g_object_unref (G_OBJECT(buf));
716 modest_tny_msg_view_get_selected_text (ModestTnyMsgView *self)
718 ModestTnyMsgViewPrivate *priv;
724 g_return_val_if_fail (self, NULL);
725 priv = MODEST_TNY_MSG_VIEW_GET_PRIVATE(self);
726 html = priv->gtkhtml;
728 /* I'm sure there is a better way to check for selected text */
729 sel = gtk_html_get_selection_html(GTK_HTML(html), &len);
735 clip = gtk_widget_get_clipboard(html, GDK_SELECTION_PRIMARY);
736 return gtk_clipboard_wait_for_text(clip);
740 modest_tny_msg_view_set_message (ModestTnyMsgView *self, TnyMsgIface *msg)
742 TnyMsgMimePartIface *body;
743 ModestTnyMsgViewPrivate *priv;
745 g_return_if_fail (self);
747 priv = MODEST_TNY_MSG_VIEW_GET_PRIVATE(self);
751 fill_gtkhtml_with_txt (self, GTK_HTML(priv->gtkhtml), "", msg);
756 body = modest_tny_msg_actions_find_body_part (msg, "text/html");
758 set_html_message (self, body, msg);
762 body = modest_tny_msg_actions_find_body_part (msg, "text/plain");
764 set_text_message (self, body, msg);
769 fill_gtkhtml_with_txt (self, GTK_HTML(priv->gtkhtml),
770 _("Unsupported message type"), msg);