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 <tny-text-buffer-stream.h>
11 #include <glib/gi18n.h>
13 /* 'private'/'protected' functions */
14 static void modest_tny_msg_view_class_init (ModestTnyMsgViewClass *klass);
15 static void modest_tny_msg_view_init (ModestTnyMsgView *obj);
16 static void modest_tny_msg_view_finalize (GObject *obj);
19 static GSList* get_url_matches (GString *txt);
20 static gboolean fill_gtkhtml_with_txt (GtkHTML* gtkhtml, const gchar* txt);
22 static gboolean on_link_clicked (GtkWidget *widget, const gchar *uri,
23 ModestTnyMsgView *msg_view);
24 static gboolean on_url_requested (GtkWidget *widget, const gchar *uri,
25 GtkHTMLStream *stream,
26 ModestTnyMsgView *msg_view);
30 * we need these regexps to find URLs in plain text e-mails
32 typedef struct _UrlMatchPattern UrlMatchPattern;
33 struct _UrlMatchPattern {
39 #define MAIL_VIEWER_URL_MATCH_PATTERNS {\
40 { "(file|http|ftp|https)://[-A-Za-z0-9_$.+!*(),;:@%&=?/~#]+[-A-Za-z0-9_$%&=?/~#]",\
42 { "www\\.[-a-z0-9.]+[-a-z0-9](:[0-9]*)?(/[-A-Za-z0-9_$.+!*(),;:@%&=?/~#]*[^]}\\),?!;:\"]?)?",\
44 { "ftp\\.[-a-z0-9.]+[-a-z0-9](:[0-9]*)?(/[-A-Za-z0-9_$.+!*(),;:@%&=?/~#]*[^]}\\),?!;:\"]?)?",\
46 { "(voipto|callto|chatto|jabberto|xmpp):[-_a-z@0-9.\\+]+", \
48 { "mailto:[-_a-z0-9.\\+]+@[-_a-z0-9.]+", \
50 { "[-_a-z0-9.\\+]+@[-_a-z0-9.]+",\
62 typedef struct _ModestTnyMsgViewPrivate ModestTnyMsgViewPrivate;
63 struct _ModestTnyMsgViewPrivate {
67 #define MODEST_TNY_MSG_VIEW_GET_PRIVATE(o) (G_TYPE_INSTANCE_GET_PRIVATE((o), \
68 MODEST_TYPE_TNY_MSG_VIEW, \
69 ModestTnyMsgViewPrivate))
71 static GtkContainerClass *parent_class = NULL;
73 /* uncomment the following if you have defined any signals */
74 /* static guint signals[LAST_SIGNAL] = {0}; */
77 modest_tny_msg_view_get_type (void)
79 static GType my_type = 0;
81 static const GTypeInfo my_info = {
82 sizeof(ModestTnyMsgViewClass),
84 NULL, /* base finalize */
85 (GClassInitFunc) modest_tny_msg_view_class_init,
86 NULL, /* class finalize */
87 NULL, /* class data */
88 sizeof(ModestTnyMsgView),
90 (GInstanceInitFunc) modest_tny_msg_view_init,
92 my_type = g_type_register_static (GTK_TYPE_SCROLLED_WINDOW,
100 modest_tny_msg_view_class_init (ModestTnyMsgViewClass *klass)
102 GObjectClass *gobject_class;
103 gobject_class = (GObjectClass*) klass;
105 parent_class = g_type_class_peek_parent (klass);
106 gobject_class->finalize = modest_tny_msg_view_finalize;
108 g_type_class_add_private (gobject_class, sizeof(ModestTnyMsgViewPrivate));
112 modest_tny_msg_view_init (ModestTnyMsgView *obj)
114 ModestTnyMsgViewPrivate *priv;
116 priv = MODEST_TNY_MSG_VIEW_GET_PRIVATE(obj);
120 priv->gtkhtml = gtk_html_new();
122 gtk_html_set_editable (GTK_HTML(priv->gtkhtml), FALSE);
123 gtk_html_allow_selection (GTK_HTML(priv->gtkhtml), TRUE);
124 gtk_html_set_caret_mode (GTK_HTML(priv->gtkhtml), FALSE);
125 gtk_html_set_blocking (GTK_HTML(priv->gtkhtml), FALSE);
126 gtk_html_set_images_blocking (GTK_HTML(priv->gtkhtml), FALSE);
128 g_signal_connect (G_OBJECT(priv->gtkhtml), "link_clicked",
129 G_CALLBACK(on_link_clicked), obj);
131 g_signal_connect (G_OBJECT(priv->gtkhtml), "url_requested",
132 G_CALLBACK(on_url_requested), obj);
137 modest_tny_msg_view_finalize (GObject *obj)
143 modest_tny_msg_view_new (TnyMsgIface *msg)
146 ModestTnyMsgView* self;
147 ModestTnyMsgViewPrivate *priv;
149 obj = G_OBJECT(g_object_new(MODEST_TYPE_TNY_MSG_VIEW, NULL));
150 self = MODEST_TNY_MSG_VIEW(obj);
151 priv = MODEST_TNY_MSG_VIEW_GET_PRIVATE (self);
153 gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(self),
154 GTK_POLICY_AUTOMATIC,
155 GTK_POLICY_AUTOMATIC);
158 gtk_container_add (GTK_CONTAINER(obj), priv->gtkhtml);
161 modest_tny_msg_view_set_message (self, msg);
163 return GTK_WIDGET(self);
169 on_link_clicked (GtkWidget *widget, const gchar *uri,
170 ModestTnyMsgView *msg_view)
172 g_message ("link clicked: %s", uri); /* FIXME */
177 static TnyMsgMimePartIface *
178 find_cid_image (TnyMsgIface *msg, const gchar *cid)
180 TnyMsgMimePartIface *part = NULL;
183 g_return_val_if_fail (msg, NULL);
184 g_return_val_if_fail (cid, NULL);
186 parts = (GList*) tny_msg_iface_get_parts (msg);
187 while (parts && !part) {
188 const gchar *part_cid;
189 part = TNY_MSG_MIME_PART_IFACE(parts->data);
190 part_cid = tny_msg_mime_part_iface_get_content_id (part);
191 if (part_cid && strcmp (cid, part_cid) == 0)
192 return part; /* we found it! */
203 on_url_requested (GtkWidget *widget, const gchar *uri,
204 GtkHTMLStream *stream,
205 ModestTnyMsgView *msg_view)
208 ModestTnyMsgViewPrivate *priv;
209 priv = MODEST_TNY_MSG_VIEW_GET_PRIVATE (msg_view);
211 g_message ("url requested: %s", uri);
213 if (g_str_has_prefix (uri, "cid:")) {
214 /* +4 ==> skip "cid:" */
216 TnyMsgMimePartIface *part = find_cid_image (priv->msg, uri + 4);
218 g_message ("%s not found", uri + 4);
219 gtk_html_stream_close (stream, GTK_HTML_STREAM_ERROR);
221 TnyStreamIface *tny_stream =
222 TNY_STREAM_IFACE(modest_tny_stream_gtkhtml_new(stream));
223 tny_msg_mime_part_iface_decode_to_stream (part,tny_stream);
224 gtk_html_stream_close (stream, GTK_HTML_STREAM_OK);
241 hyperlinkify_plain_text (GString *txt)
244 GSList *match_list = get_url_matches (txt);
246 /* we will work backwards, so the offsets stay valid */
247 for (cursor = match_list; cursor; cursor = cursor->next) {
249 url_match_t *match = (url_match_t*) cursor->data;
250 gchar *url = g_strndup (txt->str + match->offset, match->len);
251 gchar *repl = NULL; /* replacement */
253 /* the prefix is NULL: use the one that is already there */
254 repl = g_strdup_printf ("<a href=\"%s%s\">%s</a>",
255 match->prefix ? match->prefix : "", url, url);
257 /* replace the old thing with our hyperlink
258 * replacement thing */
259 g_string_erase (txt, match->offset, match->len);
260 g_string_insert (txt, match->offset, repl);
266 g_slist_free (match_list);
272 convert_to_html (const gchar *data)
275 gboolean first_space = TRUE;
283 html = g_string_sized_new (len + 100); /* just a guess... */
285 g_string_append_printf (html,
288 "<meta http-equiv=\"content-type\""
289 " content=\"text/html; charset=utf8\">"
293 /* replace with special html chars where needed*/
294 for (i = 0; i != len; ++i) {
298 case 0: break; /* ignore embedded \0s */
299 case '<' : g_string_append (html, "<"); break;
300 case '>' : g_string_append (html, ">"); break;
301 case '&' : g_string_append (html, """); break;
302 case '\n': g_string_append (html, "<br>\n"); break;
305 g_string_append (html, first_space ? " " : " ");
307 } else if (kar == '\t')
308 g_string_append (html, " ");
312 /* optimization trick: accumulate 'normal' chars, then copy */
314 kar = data [++charnum + i];
316 } while ((i + charnum < len) &&
317 (kar > '>' || (kar != '<' && kar != '>'
318 && kar != '&' && kar != ' '
319 && kar != '\n' && kar != '\t')));
320 g_string_append_len (html, &data[i], charnum);
326 g_string_append (html, "</tt></body></html>");
327 hyperlinkify_plain_text (html);
329 return g_string_free (html, FALSE);
336 cmp_offsets_reverse (const url_match_t *match1, const url_match_t *match2)
338 return match2->offset - match1->offset;
344 * check if the match is inside an existing match... */
346 chk_partial_match (const url_match_t *match, int* offset)
348 if (*offset >= match->offset && *offset < match->offset + match->len)
353 get_url_matches (GString *txt)
356 int rv, i, offset = 0;
357 GSList *match_list = NULL;
359 static UrlMatchPattern patterns[] = MAIL_VIEWER_URL_MATCH_PATTERNS;
360 const size_t pattern_num = sizeof(patterns)/sizeof(UrlMatchPattern);
362 /* initalize the regexps */
363 for (i = 0; i != pattern_num; ++i) {
364 patterns[i].preg = g_new0 (regex_t,1);
365 g_assert(regcomp (patterns[i].preg, patterns[i].regex,
366 REG_ICASE|REG_EXTENDED|REG_NEWLINE) == 0);
368 /* find all the matches */
369 for (i = 0; i != pattern_num; ++i) {
373 if ((rv = regexec (patterns[i].preg, txt->str + offset, 1, &rm, 0)) != 0) {
374 g_assert (rv == REG_NOMATCH); /* this should not happen */
375 break; /* try next regexp */
380 /* FIXME: optimize this */
381 /* to avoid partial matches on something that was already found... */
382 /* check_partial_match will put -1 in the data ptr if that is the case */
383 test_offset = offset + rm.rm_so;
384 g_slist_foreach (match_list, (GFunc)chk_partial_match, &test_offset);
386 /* make a list of our matches (<offset, len, prefix> tupels)*/
387 if (test_offset != -1) {
388 url_match_t *match = g_new (url_match_t,1);
389 match->offset = offset + rm.rm_so;
390 match->len = rm.rm_eo - rm.rm_so;
391 match->prefix = patterns[i].prefix;
392 match_list = g_slist_prepend (match_list, match);
398 for (i = 0; i != pattern_num; ++i) {
399 regfree (patterns[i].preg);
400 g_free (patterns[i].preg);
401 } /* don't free patterns itself -- it's static */
403 /* now sort the list, so the matches are in reverse order of occurence.
404 * that way, we can do the replacements starting from the end, so we don't need
405 * to recalculate the offsets
407 match_list = g_slist_sort (match_list,
408 (GCompareFunc)cmp_offsets_reverse);
413 fill_gtkhtml_with_txt (GtkHTML* gtkhtml, const gchar* txt)
417 g_return_val_if_fail (gtkhtml, FALSE);
418 g_return_val_if_fail (txt, FALSE);
420 html = convert_to_html (txt);
421 gtk_html_load_from_string (gtkhtml, html, strlen(html));
429 static TnyMsgMimePartIface *
430 find_body_part (TnyMsgIface *msg, const gchar *mime_type)
432 TnyMsgMimePartIface *part = NULL;
435 g_return_val_if_fail (msg, NULL);
436 g_return_val_if_fail (mime_type, NULL);
438 parts = (GList*) tny_msg_iface_get_parts (msg);
439 while (parts && !part) {
440 part = TNY_MSG_MIME_PART_IFACE(parts->data);
441 if (!tny_msg_mime_part_iface_content_type_is (part, mime_type))
450 set_html_message (ModestTnyMsgView *self, TnyMsgMimePartIface *tny_body)
452 TnyStreamIface *gtkhtml_stream;
453 ModestTnyMsgViewPrivate *priv;
455 g_return_val_if_fail (self, FALSE);
456 g_return_val_if_fail (tny_body, FALSE);
458 priv = MODEST_TNY_MSG_VIEW_GET_PRIVATE(self);
461 TNY_STREAM_IFACE(modest_tny_stream_gtkhtml_new
462 (gtk_html_begin(GTK_HTML(priv->gtkhtml))));
464 tny_stream_iface_reset (gtkhtml_stream);
465 tny_msg_mime_part_iface_decode_to_stream (tny_body, gtkhtml_stream);
466 tny_stream_iface_reset (gtkhtml_stream);
468 g_object_unref (G_OBJECT(gtkhtml_stream));
474 /* this is a hack --> we use the tny_text_buffer_stream to
475 * get the message text, then write to gtkhtml 'by hand' */
477 set_text_message (ModestTnyMsgView *self, TnyMsgMimePartIface *tny_body)
480 GtkTextIter begin, end;
481 TnyStreamIface* txt_stream;
483 ModestTnyMsgViewPrivate *priv;
485 g_return_val_if_fail (self, FALSE);
486 g_return_val_if_fail (tny_body, FALSE);
488 priv = MODEST_TNY_MSG_VIEW_GET_PRIVATE(self);
490 buf = gtk_text_buffer_new (NULL);
491 txt_stream = TNY_STREAM_IFACE(tny_text_buffer_stream_new (buf));
493 tny_stream_iface_reset (txt_stream);
494 tny_msg_mime_part_iface_decode_to_stream (tny_body, txt_stream);
495 tny_stream_iface_reset (txt_stream);
497 gtk_text_buffer_get_bounds (buf, &begin, &end);
498 txt = gtk_text_buffer_get_text (buf, &begin, &end, FALSE);
500 fill_gtkhtml_with_txt (GTK_HTML(priv->gtkhtml), txt);
502 g_object_unref (G_OBJECT(txt_stream));
503 g_object_unref (G_OBJECT(buf));
510 modest_tny_msg_view_get_selected_text (ModestTnyMsgView *self)
512 ModestTnyMsgViewPrivate *priv;
520 g_return_if_fail (self);
521 priv = MODEST_TNY_MSG_VIEW_GET_PRIVATE(self);
522 html = priv->gtkhtml;
524 /* I'm sure there is a better way to check for selected text */
525 sel = gtk_html_get_selection_html(GTK_HTML(html), &len);
530 clip = gtk_widget_get_clipboard(html, GDK_SELECTION_PRIMARY);
531 text = gtk_clipboard_wait_for_text(clip);
535 buf = gtk_text_buffer_new(NULL);
536 gtk_text_buffer_set_text(buf, text, -1);
542 modest_tny_msg_view_set_message (ModestTnyMsgView *self, TnyMsgIface *msg)
544 TnyMsgMimePartIface *body;
545 ModestTnyMsgViewPrivate *priv;
547 g_return_if_fail (self);
549 priv = MODEST_TNY_MSG_VIEW_GET_PRIVATE(self);
553 fill_gtkhtml_with_txt (GTK_HTML(priv->gtkhtml), "");
558 body = find_body_part (msg, "text/html");
560 set_html_message (self, body);
564 body = find_body_part (msg, "text/plain");
566 set_text_message (self, body);
571 fill_gtkhtml_with_txt (GTK_HTML(priv->gtkhtml),
572 _("Unsupported message type"));