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>
15 /* 'private'/'protected' functions */
16 static void modest_tny_msg_view_class_init (ModestTnyMsgViewClass *klass);
17 static void modest_tny_msg_view_init (ModestTnyMsgView *obj);
18 static void modest_tny_msg_view_finalize (GObject *obj);
21 static GSList* get_url_matches (GString *txt);
22 static gboolean fill_gtkhtml_with_txt (GtkHTML* gtkhtml, const gchar* txt);
24 static gboolean on_link_clicked (GtkWidget *widget, const gchar *uri,
25 ModestTnyMsgView *msg_view);
26 static gboolean on_url_requested (GtkWidget *widget, const gchar *uri,
27 GtkHTMLStream *stream,
28 ModestTnyMsgView *msg_view);
32 * we need these regexps to find URLs in plain text e-mails
34 typedef struct _UrlMatchPattern UrlMatchPattern;
35 struct _UrlMatchPattern {
41 #define MAIL_VIEWER_URL_MATCH_PATTERNS {\
42 { "(file|http|ftp|https)://[-A-Za-z0-9_$.+!*(),;:@%&=?/~#]+[-A-Za-z0-9_$%&=?/~#]",\
44 { "www\\.[-a-z0-9.]+[-a-z0-9](:[0-9]*)?(/[-A-Za-z0-9_$.+!*(),;:@%&=?/~#]*[^]}\\),?!;:\"]?)?",\
46 { "ftp\\.[-a-z0-9.]+[-a-z0-9](:[0-9]*)?(/[-A-Za-z0-9_$.+!*(),;:@%&=?/~#]*[^]}\\),?!;:\"]?)?",\
48 { "(voipto|callto|chatto|jabberto|xmpp):[-_a-z@0-9.\\+]+", \
50 { "mailto:[-_a-z0-9.\\+]+@[-_a-z0-9.]+", \
52 { "[-_a-z0-9.\\+]+@[-_a-z0-9.]+",\
64 typedef struct _ModestTnyMsgViewPrivate ModestTnyMsgViewPrivate;
65 struct _ModestTnyMsgViewPrivate {
69 #define MODEST_TNY_MSG_VIEW_GET_PRIVATE(o) (G_TYPE_INSTANCE_GET_PRIVATE((o), \
70 MODEST_TYPE_TNY_MSG_VIEW, \
71 ModestTnyMsgViewPrivate))
73 static GtkContainerClass *parent_class = NULL;
75 /* uncomment the following if you have defined any signals */
76 /* static guint signals[LAST_SIGNAL] = {0}; */
79 modest_tny_msg_view_get_type (void)
81 static GType my_type = 0;
83 static const GTypeInfo my_info = {
84 sizeof(ModestTnyMsgViewClass),
86 NULL, /* base finalize */
87 (GClassInitFunc) modest_tny_msg_view_class_init,
88 NULL, /* class finalize */
89 NULL, /* class data */
90 sizeof(ModestTnyMsgView),
92 (GInstanceInitFunc) modest_tny_msg_view_init,
94 my_type = g_type_register_static (GTK_TYPE_SCROLLED_WINDOW,
102 modest_tny_msg_view_class_init (ModestTnyMsgViewClass *klass)
104 GObjectClass *gobject_class;
105 gobject_class = (GObjectClass*) klass;
107 parent_class = g_type_class_peek_parent (klass);
108 gobject_class->finalize = modest_tny_msg_view_finalize;
110 g_type_class_add_private (gobject_class, sizeof(ModestTnyMsgViewPrivate));
114 modest_tny_msg_view_init (ModestTnyMsgView *obj)
116 ModestTnyMsgViewPrivate *priv;
118 priv = MODEST_TNY_MSG_VIEW_GET_PRIVATE(obj);
122 priv->gtkhtml = gtk_html_new();
124 gtk_html_set_editable (GTK_HTML(priv->gtkhtml), FALSE);
125 gtk_html_allow_selection (GTK_HTML(priv->gtkhtml), TRUE);
126 gtk_html_set_caret_mode (GTK_HTML(priv->gtkhtml), FALSE);
127 gtk_html_set_blocking (GTK_HTML(priv->gtkhtml), FALSE);
128 gtk_html_set_images_blocking (GTK_HTML(priv->gtkhtml), FALSE);
130 g_signal_connect (G_OBJECT(priv->gtkhtml), "link_clicked",
131 G_CALLBACK(on_link_clicked), obj);
133 g_signal_connect (G_OBJECT(priv->gtkhtml), "url_requested",
134 G_CALLBACK(on_url_requested), obj);
139 modest_tny_msg_view_finalize (GObject *obj)
145 modest_tny_msg_view_new (TnyMsgIface *msg)
148 ModestTnyMsgView* self;
149 ModestTnyMsgViewPrivate *priv;
151 obj = G_OBJECT(g_object_new(MODEST_TYPE_TNY_MSG_VIEW, NULL));
152 self = MODEST_TNY_MSG_VIEW(obj);
153 priv = MODEST_TNY_MSG_VIEW_GET_PRIVATE (self);
155 gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(self),
156 GTK_POLICY_AUTOMATIC,
157 GTK_POLICY_AUTOMATIC);
160 gtk_container_add (GTK_CONTAINER(obj), priv->gtkhtml);
163 modest_tny_msg_view_set_message (self, msg);
165 return GTK_WIDGET(self);
171 on_link_clicked (GtkWidget *widget, const gchar *uri,
172 ModestTnyMsgView *msg_view)
174 g_message ("link clicked: %s", uri); /* FIXME */
179 static TnyMsgMimePartIface *
180 find_cid_image (TnyMsgIface *msg, const gchar *cid)
182 TnyMsgMimePartIface *part = NULL;
185 g_return_val_if_fail (msg, NULL);
186 g_return_val_if_fail (cid, NULL);
188 parts = (GList*) tny_msg_iface_get_parts (msg);
189 while (parts && !part) {
190 const gchar *part_cid;
191 part = TNY_MSG_MIME_PART_IFACE(parts->data);
192 part_cid = tny_msg_mime_part_iface_get_content_id (part);
193 if (part_cid && strcmp (cid, part_cid) == 0)
194 return part; /* we found it! */
205 on_url_requested (GtkWidget *widget, const gchar *uri,
206 GtkHTMLStream *stream,
207 ModestTnyMsgView *msg_view)
210 ModestTnyMsgViewPrivate *priv;
211 priv = MODEST_TNY_MSG_VIEW_GET_PRIVATE (msg_view);
213 g_message ("url requested: %s", uri);
215 if (g_str_has_prefix (uri, "cid:")) {
216 /* +4 ==> skip "cid:" */
218 TnyMsgMimePartIface *part = find_cid_image (priv->msg, uri + 4);
220 g_message ("%s not found", uri + 4);
221 gtk_html_stream_close (stream, GTK_HTML_STREAM_ERROR);
223 TnyStreamIface *tny_stream =
224 TNY_STREAM_IFACE(modest_tny_stream_gtkhtml_new(stream));
225 tny_msg_mime_part_iface_decode_to_stream (part,tny_stream);
226 gtk_html_stream_close (stream, GTK_HTML_STREAM_OK);
243 hyperlinkify_plain_text (GString *txt)
246 GSList *match_list = get_url_matches (txt);
248 /* we will work backwards, so the offsets stay valid */
249 for (cursor = match_list; cursor; cursor = cursor->next) {
251 url_match_t *match = (url_match_t*) cursor->data;
252 gchar *url = g_strndup (txt->str + match->offset, match->len);
253 gchar *repl = NULL; /* replacement */
255 /* the prefix is NULL: use the one that is already there */
256 repl = g_strdup_printf ("<a href=\"%s%s\">%s</a>",
257 match->prefix ? match->prefix : "", url, url);
259 /* replace the old thing with our hyperlink
260 * replacement thing */
261 g_string_erase (txt, match->offset, match->len);
262 g_string_insert (txt, match->offset, repl);
267 g_free (cursor->data);
270 g_slist_free (match_list);
276 convert_to_html (const gchar *data)
279 gboolean first_space = TRUE;
287 html = g_string_sized_new (len + 100); /* just a guess... */
289 g_string_append_printf (html,
292 "<meta http-equiv=\"content-type\""
293 " content=\"text/html; charset=utf8\">"
297 /* replace with special html chars where needed*/
298 for (i = 0; i != len; ++i) {
302 case 0: break; /* ignore embedded \0s */
303 case '<' : g_string_append (html, "<"); break;
304 case '>' : g_string_append (html, ">"); break;
305 case '&' : g_string_append (html, """); break;
306 case '\n': g_string_append (html, "<br>\n"); break;
309 g_string_append (html, first_space ? " " : " ");
311 } else if (kar == '\t')
312 g_string_append (html, " ");
316 /* optimization trick: accumulate 'normal' chars, then copy */
318 kar = data [++charnum + i];
320 } while ((i + charnum < len) &&
321 (kar > '>' || (kar != '<' && kar != '>'
322 && kar != '&' && kar != ' '
323 && kar != '\n' && kar != '\t')));
324 g_string_append_len (html, &data[i], charnum);
330 g_string_append (html, "</tt></body></html>");
331 hyperlinkify_plain_text (html);
333 return g_string_free (html, FALSE);
340 cmp_offsets_reverse (const url_match_t *match1, const url_match_t *match2)
342 return match2->offset - match1->offset;
348 * check if the match is inside an existing match... */
350 chk_partial_match (const url_match_t *match, int* offset)
352 if (*offset >= match->offset && *offset < match->offset + match->len)
357 get_url_matches (GString *txt)
360 int rv, i, offset = 0;
361 GSList *match_list = NULL;
363 static UrlMatchPattern patterns[] = MAIL_VIEWER_URL_MATCH_PATTERNS;
364 const size_t pattern_num = sizeof(patterns)/sizeof(UrlMatchPattern);
366 /* initalize the regexps */
367 for (i = 0; i != pattern_num; ++i) {
368 patterns[i].preg = g_new0 (regex_t,1);
369 g_assert(regcomp (patterns[i].preg, patterns[i].regex,
370 REG_ICASE|REG_EXTENDED|REG_NEWLINE) == 0);
372 /* find all the matches */
373 for (i = 0; i != pattern_num; ++i) {
377 if ((rv = regexec (patterns[i].preg, txt->str + offset, 1, &rm, 0)) != 0) {
378 g_assert (rv == REG_NOMATCH); /* this should not happen */
379 break; /* try next regexp */
384 /* FIXME: optimize this */
385 /* to avoid partial matches on something that was already found... */
386 /* check_partial_match will put -1 in the data ptr if that is the case */
387 test_offset = offset + rm.rm_so;
388 g_slist_foreach (match_list, (GFunc)chk_partial_match, &test_offset);
390 /* make a list of our matches (<offset, len, prefix> tupels)*/
391 if (test_offset != -1) {
392 url_match_t *match = g_new (url_match_t,1);
393 match->offset = offset + rm.rm_so;
394 match->len = rm.rm_eo - rm.rm_so;
395 match->prefix = patterns[i].prefix;
396 match_list = g_slist_prepend (match_list, match);
402 for (i = 0; i != pattern_num; ++i) {
403 regfree (patterns[i].preg);
404 g_free (patterns[i].preg);
405 } /* don't free patterns itself -- it's static */
407 /* now sort the list, so the matches are in reverse order of occurence.
408 * that way, we can do the replacements starting from the end, so we don't need
409 * to recalculate the offsets
411 match_list = g_slist_sort (match_list,
412 (GCompareFunc)cmp_offsets_reverse);
417 fill_gtkhtml_with_txt (GtkHTML* gtkhtml, const gchar* txt)
421 g_return_val_if_fail (gtkhtml, FALSE);
422 g_return_val_if_fail (txt, FALSE);
424 html = convert_to_html (txt);
425 gtk_html_load_from_string (gtkhtml, html, strlen(html));
434 set_html_message (ModestTnyMsgView *self, TnyMsgMimePartIface *tny_body)
436 TnyStreamIface *gtkhtml_stream;
437 ModestTnyMsgViewPrivate *priv;
439 g_return_val_if_fail (self, FALSE);
440 g_return_val_if_fail (tny_body, FALSE);
442 priv = MODEST_TNY_MSG_VIEW_GET_PRIVATE(self);
445 TNY_STREAM_IFACE(modest_tny_stream_gtkhtml_new
446 (gtk_html_begin(GTK_HTML(priv->gtkhtml))));
448 tny_stream_iface_reset (gtkhtml_stream);
449 tny_msg_mime_part_iface_decode_to_stream (tny_body, gtkhtml_stream);
450 tny_stream_iface_reset (gtkhtml_stream);
452 g_object_unref (G_OBJECT(gtkhtml_stream));
458 /* this is a hack --> we use the tny_text_buffer_stream to
459 * get the message text, then write to gtkhtml 'by hand' */
461 set_text_message (ModestTnyMsgView *self, TnyMsgMimePartIface *tny_body)
464 GtkTextIter begin, end;
465 TnyStreamIface* txt_stream;
467 ModestTnyMsgViewPrivate *priv;
469 g_return_val_if_fail (self, FALSE);
470 g_return_val_if_fail (tny_body, FALSE);
472 priv = MODEST_TNY_MSG_VIEW_GET_PRIVATE(self);
474 buf = gtk_text_buffer_new (NULL);
475 txt_stream = TNY_STREAM_IFACE(tny_text_buffer_stream_new (buf));
477 tny_stream_iface_reset (txt_stream);
478 tny_msg_mime_part_iface_decode_to_stream (tny_body, txt_stream);
479 tny_stream_iface_reset (txt_stream);
481 gtk_text_buffer_get_bounds (buf, &begin, &end);
482 txt = gtk_text_buffer_get_text (buf, &begin, &end, FALSE);
484 fill_gtkhtml_with_txt (GTK_HTML(priv->gtkhtml), txt);
486 g_object_unref (G_OBJECT(txt_stream));
487 g_object_unref (G_OBJECT(buf));
494 modest_tny_msg_view_get_selected_text (ModestTnyMsgView *self)
496 ModestTnyMsgViewPrivate *priv;
504 g_return_if_fail (self);
505 priv = MODEST_TNY_MSG_VIEW_GET_PRIVATE(self);
506 html = priv->gtkhtml;
508 /* I'm sure there is a better way to check for selected text */
509 sel = gtk_html_get_selection_html(GTK_HTML(html), &len);
515 clip = gtk_widget_get_clipboard(html, GDK_SELECTION_PRIMARY);
516 return gtk_clipboard_wait_for_text(clip);
520 modest_tny_msg_view_set_message (ModestTnyMsgView *self, TnyMsgIface *msg)
522 TnyMsgMimePartIface *body;
523 ModestTnyMsgViewPrivate *priv;
525 g_return_if_fail (self);
527 priv = MODEST_TNY_MSG_VIEW_GET_PRIVATE(self);
531 fill_gtkhtml_with_txt (GTK_HTML(priv->gtkhtml), "");
536 body = modest_tny_msg_actions_find_body_part (msg, "text/html");
538 set_html_message (self, body);
542 body = modest_tny_msg_actions_find_body_part (msg, "text/plain");
544 set_text_message (self, body);
549 fill_gtkhtml_with_txt (GTK_HTML(priv->gtkhtml),
550 _("Unsupported message type"));