From 6d2c382788a4042d53f49a080acd11b499aa52f6 Mon Sep 17 00:00:00 2001 From: Dan Vrátil Date: Wed, 28 Mar 2012 18:38:11 +0200 Subject: WebKit port - port formatter and mail module --- data/Makefile.am | 2 + data/webview-print.css | 62 + data/webview.css | 147 + em-format/Makefile.am | 6 +- em-format/em-format-quote.c | 792 ++-- em-format/em-format-quote.h | 3 + em-format/em-format.c | 3928 ++++++++++---------- em-format/em-format.h | 566 ++- mail/Makefile.am | 20 +- mail/e-mail-attachment-bar.c | 94 +- mail/e-mail-attachment-bar.h | 4 +- mail/e-mail-browser.c | 113 +- mail/e-mail-browser.h | 6 +- mail/e-mail-display.c | 1430 +++++++- mail/e-mail-display.h | 50 +- mail/e-mail-notebook-view.c | 18 +- mail/e-mail-paned-view.c | 82 +- mail/e-mail-paned-view.h | 1 + mail/e-mail-printer.c | 859 +++++ mail/e-mail-printer.h | 85 + mail/e-mail-reader-utils.c | 151 +- mail/e-mail-reader-utils.h | 3 +- mail/e-mail-reader.c | 445 ++- mail/e-mail-reader.h | 6 +- mail/e-mail-request.c | 771 ++++ mail/e-mail-request.h | 36 + mail/em-account-editor.c | 6 +- mail/em-composer-utils.c | 31 +- mail/em-format-hook.c | 23 +- mail/em-format-hook.h | 4 +- mail/em-format-html-display.c | 1350 +++---- mail/em-format-html-display.h | 49 +- mail/em-format-html-print.c | 681 +++- mail/em-format-html-print.h | 19 +- mail/em-format-html.c | 4457 ++++++++++------------- mail/em-format-html.h | 191 +- mail/em-html-stream.c | 182 - mail/em-html-stream.h | 77 - mail/em-utils.c | 74 +- mail/mail.error.xml | 5 + modules/Makefile.am | 1 + modules/addressbook/e-book-shell-content.c | 9 - modules/mail/e-mail-config-format-html.c | 5 + modules/mail/e-mail-config-web-view.c | 6 - modules/mail/e-mail-shell-backend.c | 57 +- modules/mail/e-mail-shell-content.c | 9 +- modules/mail/e-mail-shell-view-actions.c | 116 +- modules/mail/e-mail-shell-view-private.c | 100 +- modules/mail/em-mailer-prefs.c | 6 +- modules/web-inspector/Makefile.am | 23 + modules/web-inspector/evolution-web-inspector.c | 185 + 51 files changed, 10200 insertions(+), 7146 deletions(-) create mode 100644 data/webview-print.css create mode 100644 data/webview.css create mode 100644 mail/e-mail-printer.c create mode 100644 mail/e-mail-printer.h create mode 100644 mail/e-mail-request.c create mode 100644 mail/e-mail-request.h delete mode 100644 mail/em-html-stream.c delete mode 100644 mail/em-html-stream.h create mode 100644 modules/web-inspector/Makefile.am create mode 100644 modules/web-inspector/evolution-web-inspector.c diff --git a/data/Makefile.am b/data/Makefile.am index bb6b2c18a3..9d9a8869c4 100644 --- a/data/Makefile.am +++ b/data/Makefile.am @@ -39,6 +39,8 @@ convert_DATA = evolution.convert themedir = $(privdatadir)/theme dist_theme_DATA = \ default.css \ + webview.css \ + webview-print.css \ tab-bar-background.png \ tab-switcher.png \ tab-switcher-hover.png \ diff --git a/data/webview-print.css b/data/webview-print.css new file mode 100644 index 0000000000..a2c5292d56 --- /dev/null +++ b/data/webview-print.css @@ -0,0 +1,62 @@ +html, body { + padding: 0; + margin: 0; +} + +body { + /* Use margin so that children can safely use width=100% */ + margin: 10px; +} + +h1,h2,h3 { + color: #7f7f7f; +} + +th { + color: #7f7f7f; + text-align: left; + font-weight: normal; + vertical-align: top; +} + +.header { + color: #7f7f7f; +} + +.pre { + font-family: monospace; +} + +.part-container { + width: 100%; + background: #FFF; + margin-top: 2px; + margin-bottom: 3px; + border-width: 0px; + border-style: none; +} + +.part-container-inner-margin { + margin: 8px; +} + +/***** PRINTING *******/ + +.printing-header { + margin-bottom: 20px; +} + +.printing-header h1, +.attachments-list h1 { + font-size: 20px; +} + +.printing-header th { + text-align: right; + font-weight: bold; +} + +.attachments-list th { + font-weight: bold; +} + diff --git a/data/webview.css b/data/webview.css new file mode 100644 index 0000000000..9ff822013e --- /dev/null +++ b/data/webview.css @@ -0,0 +1,147 @@ +html, body { + padding: 0; + margin: 0; +} + +body { + /* Use margin so that children can safely use width=100% */ + margin: 10px; +} + +h1, h2, h3 { + color: #7f7f7f; +} + +th { + color: #7f7f7f; + text-align: left; + font-weight: normal; + vertical-align: top; +} + +.header { + color: #7f7f7f; +} + +.pre { + font-family: monospace; +} + +span.navigable, div.navigable, p.navigable { + cursor: pointer; + text-decoration: underline; + color: #003399; +} + +img.navigable { + cursor: pointer; + margin-right: 4px; +} + +.attachments { + background: #FFF; + border: 1px solid silver; + margin: 10px 10px 10px 10px; + border-left: 0; + border-right: 0; + border-bottom: 0; +} + +.attachment { + margin-left: 8px; + margin-right: 8px; +} + +.attachment-wrapper +{ + margin-right: 8px; +} + +.part-container { + width: 100%; + height: 100%; + background: #FFF; + margin-top: 2px; + margin-bottom: 3px; + border-width: 1px; + border-style: solid; +} + +.part-container-inner-margin { + margin: 8px; +} + +object { /* GtkWidgets */ + margin-top: 2px; + margin-bottom: 2px; +} + +.__evo-highlight { + color: purple; + font-weight: bold; +} + +/***** PRINTING *******/ + +.printing-header { + margin-bottom: 20px; +} + +.printing-header h1, +.attachments-list h1 { + font-size: 20px; +} + +.printing-header th { + text-align: right; + font-weight: bold; +} + +.attachments-list th { + font-weight: bold; +} + +/******* ITIP *********/ +.itip.icon { + float: left; + margin-right: 5px; +} + +.itip.content { + float: left; + max-width: 90%; +} + +.itip.description { + margin: 5px; +} + +.itip tr { + vertical-align: middle; + padding-top: 5px; + padding-bottom: 5px; +} + +.itip th { + color: #000; + vertical-align: middle; +} + +#table_row_summary td { + font-weight: bold; +} + +#table_row_buttons button { + line-height: 28px; + min-width: 150px; + white-space: nowrap; +} + +#table_row_buttons img { + margin-right: 5px; + vertical-align: middle; +} + +#text_row_buttons td { + text-align: center; +} diff --git a/em-format/Makefile.am b/em-format/Makefile.am index 278bcb2527..392a195044 100644 --- a/em-format/Makefile.am +++ b/em-format/Makefile.am @@ -13,7 +13,8 @@ libemformat_la_CPPFLAGS = \ -I$(top_srcdir) \ -I$(top_srcdir)/widgets \ $(EVOLUTION_DATA_SERVER_CFLAGS) \ - $(GNOME_PLATFORM_CFLAGS) + $(GNOME_PLATFORM_CFLAGS) \ + $(LIBSOUP_CFLAGS) libemformat_la_SOURCES = \ $(emformatinclude_HEADERS) \ @@ -28,6 +29,7 @@ libemformat_la_LIBADD = \ $(top_builddir)/e-util/libeutil.la \ $(top_builddir)/shell/libeshell.la \ $(EVOLUTION_DATA_SERVER_LIBS) \ - $(GNOME_PLATFORM_LIBS) + $(GNOME_PLATFORM_LIBS) \ + $(LIBSOUP_LIBS) -include $(top_srcdir)/git.mk diff --git a/em-format/em-format-quote.c b/em-format/em-format-quote.c index c3f75ec14d..4822f115d7 100644 --- a/em-format/em-format-quote.c +++ b/em-format/em-format-quote.c @@ -39,237 +39,73 @@ struct _EMFormatQuotePrivate { gchar *credits; - CamelStream *stream; EMFormatQuoteFlags flags; guint32 text_html_flags; }; static void emfq_builtin_init (EMFormatQuoteClass *efhc); -static gpointer parent_class; - -static void -emfq_dispose (GObject *object) -{ - EMFormatQuotePrivate *priv; - - priv = EM_FORMAT_QUOTE_GET_PRIVATE (object); - - if (priv->stream != NULL) { - g_object_unref (priv->stream); - priv->stream = NULL; - } - - /* Chain up to parent's dispose() method. */ - G_OBJECT_CLASS (parent_class)->dispose (object); -} - -static void -emfq_finalize (GObject *object) -{ - EMFormatQuotePrivate *priv; - - priv = EM_FORMAT_QUOTE_GET_PRIVATE (object); - - g_free (priv->credits); - - /* Chain up to parent's finalize() method. */ - G_OBJECT_CLASS (parent_class)->finalize (object); -} +static CamelMimePart * decode_inline_parts (CamelMimePart *part, GCancellable *cancellable); -static void -emfq_format_clone (EMFormat *emf, - CamelFolder *folder, - const gchar *uid, - CamelMimeMessage *msg, - EMFormat *src, - GCancellable *cancellable) -{ - EMFormatQuote *emfq = (EMFormatQuote *) emf; - const EMFormatHandler *handle; - GSettings *settings; +static void emfq_parse_text_plain (EMFormat *emf, CamelMimePart *part, GString *part_id, EMFormatParserInfo *info, GCancellable *cancellable); +static void emfq_parse_text_enriched (EMFormat *emf, CamelMimePart *part, GString *part_id, EMFormatParserInfo *info, GCancellable *cancellable); +static void emfq_parse_text_html (EMFormat *emf, CamelMimePart *part, GString *part_id, EMFormatParserInfo *info, GCancellable *cancellable); +static void emfq_parse_attachment (EMFormat *emf, CamelMimePart *part, GString *part_id, EMFormatParserInfo *info, GCancellable *cancellable); - /* Chain up to parent's format_clone() method. */ - EM_FORMAT_CLASS (parent_class)->format_clone ( - emf, folder, uid, msg, src, cancellable); +static void emfq_write_text_plain (EMFormat *emf, EMFormatPURI *puri, CamelStream *stream, EMFormatWriterInfo *info, GCancellable *cancellable); +static void emfq_write_text_enriched (EMFormat *emf, EMFormatPURI *puri, CamelStream *stream, EMFormatWriterInfo *info, GCancellable *cancellable); +static void emfq_write_text_html (EMFormat *emf, EMFormatPURI *puri, CamelStream *stream, EMFormatWriterInfo *info, GCancellable *cancellable); - g_seekable_seek ( - G_SEEKABLE (emfq->priv->stream), - 0, G_SEEK_SET, NULL, NULL); - - settings = g_settings_new ("org.gnome.evolution.mail"); - if (g_settings_get_boolean ( - settings, "composer-top-signature")) - camel_stream_write_string ( - emfq->priv->stream, "
\n", cancellable, NULL); - g_object_unref (settings); - handle = em_format_find_handler(emf, "x-evolution/message/prefix"); - if (handle) - handle->handler ( - emf, emfq->priv->stream, - CAMEL_MIME_PART (msg), - handle, cancellable, FALSE); - handle = em_format_find_handler(emf, "x-evolution/message/rfc822"); - if (handle) - handle->handler ( - emf, emfq->priv->stream, - CAMEL_MIME_PART (msg), - handle, cancellable, FALSE); - - camel_stream_flush (emfq->priv->stream, cancellable, NULL); - - g_signal_emit_by_name(emf, "complete"); -} - -static void -emfq_format_error (EMFormat *emf, - CamelStream *stream, - const gchar *errmsg) -{ - /* Nothing to do. */ -} +static gpointer parent_class; -static void -emfq_format_source (EMFormat *emf, - CamelStream *stream, - CamelMimePart *part, - GCancellable *cancellable) +/* Decodes inline encoded parts of 'part'. The returned pointer, + * if not NULL, should be unreffed with g_object_unref(). */ +static CamelMimePart * +decode_inline_parts (CamelMimePart *part, + GCancellable *cancellable) { + CamelMultipart *mp; + CamelStream *null; CamelStream *filtered_stream; - CamelMimeFilter *html_filter; + EMInlineFilter *inline_filter; - filtered_stream = camel_stream_filter_new (stream); - html_filter = camel_mime_filter_tohtml_new ( - CAMEL_MIME_FILTER_TOHTML_CONVERT_NL | - CAMEL_MIME_FILTER_TOHTML_CONVERT_SPACES | - CAMEL_MIME_FILTER_TOHTML_ESCAPE_8BIT, 0); - camel_stream_filter_add ( - CAMEL_STREAM_FILTER (filtered_stream), html_filter); - g_object_unref (html_filter); + g_return_val_if_fail (part != NULL, NULL); - em_format_format_text ( - emf, filtered_stream, - CAMEL_DATA_WRAPPER (part), cancellable); + null = camel_stream_null_new (); + filtered_stream = camel_stream_filter_new (null); + g_object_unref (null); + inline_filter = em_inline_filter_new ( + camel_mime_part_get_encoding (part), + camel_mime_part_get_content_type (part)); + camel_stream_filter_add ( + CAMEL_STREAM_FILTER (filtered_stream), + CAMEL_MIME_FILTER (inline_filter)); + camel_data_wrapper_decode_to_stream_sync ( + camel_medium_get_content (CAMEL_MEDIUM (part)), + filtered_stream, cancellable, NULL); + camel_stream_close (filtered_stream, cancellable, NULL); g_object_unref (filtered_stream); -} - -static void -emfq_format_attachment (EMFormat *emf, - CamelStream *stream, - CamelMimePart *part, - const gchar *mime_type, - const EMFormatHandler *handle, - GCancellable *cancellable) -{ - EMFormatQuote *emfq = EM_FORMAT_QUOTE (emf); - gchar *text, *html; - - if (!em_format_is_inline (emf, emf->part_id->str, part, handle)) - return; - - camel_stream_write_string ( - stream, "" - "
\n", cancellable, NULL); - - /* output some info about it */ - text = em_format_describe_part (part, mime_type); - html = camel_text_to_html ( - text, emfq->priv->text_html_flags & - CAMEL_MIME_FILTER_TOHTML_CONVERT_URLS, 0); - camel_stream_write_string (stream, html, cancellable, NULL); - g_free (html); - g_free (text); - - camel_stream_write_string ( - stream, "
", cancellable, NULL); - - handle->handler (emf, stream, part, handle, cancellable, FALSE); -} - -static void -emfq_base_init (EMFormatQuoteClass *class) -{ - emfq_builtin_init (class); -} - -static void -emfq_class_init (EMFormatQuoteClass *class) -{ - GObjectClass *object_class; - EMFormatClass *format_class; - - parent_class = g_type_class_peek_parent (class); - g_type_class_add_private (class, sizeof (EMFormatQuotePrivate)); - - object_class = G_OBJECT_CLASS (class); - object_class->dispose = emfq_dispose; - object_class->finalize = emfq_finalize; - - format_class = EM_FORMAT_CLASS (class); - format_class->format_clone = emfq_format_clone; - format_class->format_error = emfq_format_error; - format_class->format_source = emfq_format_source; - format_class->format_attachment = emfq_format_attachment; -} - -static void -emfq_init (EMFormatQuote *emfq) -{ - emfq->priv = EM_FORMAT_QUOTE_GET_PRIVATE (emfq); - - /* we want to convert url's etc */ - emfq->priv->text_html_flags = - CAMEL_MIME_FILTER_TOHTML_PRE | - CAMEL_MIME_FILTER_TOHTML_CONVERT_URLS | - CAMEL_MIME_FILTER_TOHTML_CONVERT_ADDRESSES; -} -GType -em_format_quote_get_type (void) -{ - static GType type = 0; - - if (G_UNLIKELY (type == 0)) { - static const GTypeInfo type_info = { - sizeof (EMFormatQuoteClass), - (GBaseInitFunc) emfq_base_init, - (GBaseFinalizeFunc) NULL, - (GClassInitFunc) emfq_class_init, - (GClassFinalizeFunc) NULL, - NULL, /* class_data */ - sizeof (EMFormatQuote), - 0, /* n_preallocs */ - (GInstanceInitFunc) emfq_init, - NULL /* value_table */ - }; - - type = g_type_register_static ( - EM_TYPE_FORMAT, "EMFormatQuote", &type_info, 0); + if (!em_inline_filter_found_any (inline_filter)) { + g_object_unref (inline_filter); + return NULL; } - return type; -} - -EMFormatQuote * -em_format_quote_new (const gchar *credits, - CamelStream *stream, - EMFormatQuoteFlags flags) -{ - EMFormatQuote *emfq; - - g_return_val_if_fail (CAMEL_IS_STREAM (stream), NULL); - - /* Steam must also be seekable so we can reset its position. */ - g_return_val_if_fail (G_IS_SEEKABLE (stream), NULL); + mp = em_inline_filter_get_multipart (inline_filter); - emfq = g_object_new (EM_TYPE_FORMAT_QUOTE, NULL); + g_object_unref (inline_filter); - emfq->priv->credits = g_strdup (credits); - emfq->priv->stream = g_object_ref (stream); - emfq->priv->flags = flags; + if (mp) { + part = camel_mime_part_new (); + camel_medium_set_content ( + CAMEL_MEDIUM (part), CAMEL_DATA_WRAPPER (mp)); + g_object_unref (mp); + } else { + g_object_ref (part); + } - return emfq; + return part; } static void @@ -296,18 +132,18 @@ emfq_format_text_header (EMFormatQuote *emfq, if (flags & EM_FORMAT_HEADER_BOLD) g_string_append_printf ( - buffer, "%s: %s
", label, html); + buffer, "%s: %s
", label, html); else g_string_append_printf ( - buffer, "%s: %s
", label, html); + buffer, "%s: %s
", label, html); g_free (mhtml); } static const gchar *addrspec_hdrs[] = { - "Sender", "From", "Reply-To", "To", "Cc", "Bcc", - "Resent-Sender", "Resent-from", "Resent-Reply-To", - "Resent-To", "Resent-cc", "Resent-Bcc", NULL + "Sender", "From", "Reply-To", "To", "Cc", "Bcc", + "Resent-Sender", "Resent-from", "Resent-Reply-To", + "Resent-To", "Resent-cc", "Resent-Bcc", NULL }; #if 0 @@ -315,7 +151,7 @@ static const gchar *addrspec_hdrs[] = { /* For Translators only: The following strings are * used in the header table in the preview pane. */ static gchar *i18n_hdrs[] = { - N_("From"), N_("Reply-To"), N_("To"), N_("Cc"), N_("Bcc") + N_("From"), N_("Reply-To"), N_("To"), N_("Cc"), N_("Bcc") }; #endif @@ -337,18 +173,18 @@ emfq_format_address (GString *out, if (name && *name) { gchar *real, *mailaddr; - g_string_append_printf (out, "%s <", name); - /* rfc2368 for mailto syntax and url encoding extras */ + g_string_append_printf (out, "%s <", name); + /* rfc2368 for mailto syntax and url encoding extras */ if ((real = camel_header_encode_phrase ((guchar *) a->name))) { - mailaddr = g_strdup_printf ("%s <%s>", real, a->v.addr); + mailaddr = g_strdup_printf ("%s <%s>", real, a->v.addr); g_free (real); - mailto = camel_url_encode (mailaddr, "?=&()"); + mailto = camel_url_encode (mailaddr, "?=&()"); g_free (mailaddr); } else { - mailto = camel_url_encode (a->v.addr, "?=&()"); + mailto = camel_url_encode (a->v.addr, "?=&()"); } } else { - mailto = camel_url_encode (a->v.addr, "?=&()"); + mailto = camel_url_encode (a->v.addr, "?=&()"); } addr = camel_text_to_html (a->v.addr, flags, 0); g_string_append_printf ( @@ -358,15 +194,15 @@ emfq_format_address (GString *out, g_free (addr); if (name && *name) - g_string_append (out, ">"); + g_string_append (out, ">"); break; case CAMEL_HEADER_ADDRESS_GROUP: - g_string_append_printf (out, "%s: ", name); + g_string_append_printf (out, "%s: ", name); emfq_format_address (out, a->v.members); - g_string_append_printf (out, ";"); + g_string_append_printf (out, ";"); break; default: - g_warning ("Invalid address type"); + g_warning ("Invalid address type"); break; } @@ -374,7 +210,7 @@ emfq_format_address (GString *out, a = a->next; if (a) - g_string_append (out, ", "); + g_string_append (out, ", "); } } @@ -383,20 +219,20 @@ canon_header_name (gchar *name) { gchar *inptr = name; - /* canonicalise the header name... first letter is - * capitalised and any letter following a '-' also gets - * capitalised */ + /* canonicalise the header name... first letter is + * capitalised and any letter following a '-' also gets + * capitalised */ if (g_ascii_islower (*inptr)) - *inptr = g_ascii_toupper (*inptr); + *inptr = g_ascii_toupper (*inptr); inptr++; while (*inptr) { if (inptr[-1] == '-' && g_ascii_islower (*inptr)) - *inptr = g_ascii_toupper (*inptr); + *inptr = g_ascii_toupper (*inptr); else if (g_ascii_isupper (*inptr)) - *inptr = g_ascii_tolower (*inptr); + *inptr = g_ascii_tolower (*inptr); inptr++; } @@ -422,8 +258,8 @@ emfq_format_header (EMFormat *emf, strcpy (name, namein); canon_header_name (name); - /* Never quote Bcc headers */ - if (g_str_equal (name, "Bcc") || g_str_equal (name, "Resent-Bcc")) + /* Never quote Bcc headers */ + if (g_str_equal (name, "Bcc") || g_str_equal (name, "Resent-Bcc")) return; for (i = 0; addrspec_hdrs[i]; i++) { @@ -444,8 +280,8 @@ emfq_format_header (EMFormat *emf, buf = camel_header_unfold (txt); addrs = camel_header_address_decode ( - txt, emf->charset ? - emf->charset : emf->default_charset); + txt, em_format_get_charset (emf) ? + em_format_get_charset (emf) : em_format_get_default_charset (emf)); if (addrs == NULL) { g_free (buf); return; @@ -453,29 +289,29 @@ emfq_format_header (EMFormat *emf, g_free (buf); - html = g_string_new (""); + html = g_string_new (""); emfq_format_address (html, addrs); camel_header_address_unref (addrs); txt = value = html->str; g_string_free (html, FALSE); flags |= EM_FORMAT_HEADER_BOLD; is_html = TRUE; - } else if (!strcmp (name, "Subject")) { + } else if (!strcmp (name, "Subject")) { txt = camel_mime_message_get_subject (msg); - label = _("Subject"); + label = _("Subject"); flags |= EM_FORMAT_HEADER_BOLD; - } else if (!strcmp (name, "X-Evolution-Mailer")) { /* pseudo-header */ - if (!(txt = camel_medium_get_header (part, "x-mailer"))) - if (!(txt = camel_medium_get_header (part, "user-agent"))) - if (!(txt = camel_medium_get_header (part, "x-newsreader"))) - if (!(txt = camel_medium_get_header (part, "x-mimeole"))) + } else if (!strcmp (name, "X-Evolution-Mailer")) { /* pseudo-header */ + if (!(txt = camel_medium_get_header (part, "x-mailer"))) + if (!(txt = camel_medium_get_header (part, "user-agent"))) + if (!(txt = camel_medium_get_header (part, "x-newsreader"))) + if (!(txt = camel_medium_get_header (part, "x-mimeole"))) return; txt = value = camel_header_format_ctext (txt, charset); - label = _("Mailer"); + label = _("Mailer"); flags |= EM_FORMAT_HEADER_BOLD; - } else if (!strcmp (name, "Date") || !strcmp (name, "Resent-Date")) { + } else if (!strcmp (name, "Date") || !strcmp (name, "Resent-Date")) { if (!(txt = camel_medium_get_header (part, name))) return; @@ -506,10 +342,10 @@ emfq_format_headers (EMFormatQuote *emfq, return; ct = camel_mime_part_get_content_type ((CamelMimePart *) part); - charset = camel_content_type_param (ct, "charset"); + charset = camel_content_type_param (ct, "charset"); charset = camel_iconv_charset_name (charset); - /* dump selected headers */ + /* dump selected headers */ link = g_queue_peek_head_link (&emf->header_list); while (link != NULL) { EMFormatHeader *h = link->data; @@ -518,154 +354,335 @@ emfq_format_headers (EMFormatQuote *emfq, link = g_list_next (link); } - g_string_append (buffer, "
\n"); + g_string_append (buffer, "
\n"); } static void -emfq_format_message_prefix (EMFormat *emf, - CamelStream *stream, - CamelMimePart *part, - const EMFormatHandler *info, - GCancellable *cancellable, - gboolean is_fallback) +emfq_dispose (GObject *object) { - EMFormatQuote *emfq = (EMFormatQuote *) emf; + /* Chain up to parent's dispose() method. */ + G_OBJECT_CLASS (parent_class)->dispose (object); +} - if (emfq->priv->credits != NULL) { - camel_stream_write_string ( - stream, emfq->priv->credits, NULL, NULL); - camel_stream_write_string ( - stream, "
\n", NULL, NULL); - } +static void +emfq_finalize (GObject *object) +{ + EMFormatQuotePrivate *priv; + + priv = EM_FORMAT_QUOTE_GET_PRIVATE (object); + + g_free (priv->credits); + + /* Chain up to parent's finalize() method. */ + G_OBJECT_CLASS (parent_class)->finalize (object); } +/******************************************************************************/ static void -emfq_format_message (EMFormat *emf, - CamelStream *stream, - CamelMimePart *part, - const EMFormatHandler *info, - GCancellable *cancellable, - gboolean is_fallback) +emfq_parse_text_plain (EMFormat * emf, + CamelMimePart * part, + GString * part_id, + EMFormatParserInfo * info, + GCancellable * cancellable) { - EMFormatQuote *emfq = (EMFormatQuote *) emf; - GString *buffer; + EMFormatPURI *puri; + CamelMimePart *mp; + gint len; - buffer = g_string_sized_new (1024); + len = part_id->len; + g_string_append (part_id, ".text_plain"); - if (emfq->priv->flags & EM_FORMAT_QUOTE_CITE) - g_string_append ( - buffer, - "\n" - "
\n"); + mp = decode_inline_parts (part, cancellable); + if (mp) { - if (((CamelMimePart *) emf->message) != part) { - g_string_append_printf ( - buffer, - "%s
\n", - _("-------- Forwarded Message --------")); - emfq_format_headers (emfq, buffer, (CamelMedium *) part); - } else if (emfq->priv->flags & EM_FORMAT_QUOTE_HEADERS) - emfq_format_headers (emfq, buffer, (CamelMedium *) part); + if (CAMEL_IS_MULTIPART (camel_medium_get_content (CAMEL_MEDIUM (mp)))) { + em_format_parse_part (emf, mp, part_id, info, cancellable); + } - camel_stream_write ( - stream, buffer->str, buffer->len, cancellable, NULL); + g_object_unref (mp); + } - em_format_part (emf, stream, part, cancellable); + puri = em_format_puri_new (emf, sizeof (EMFormatPURI), part, part_id->str); + puri->write_func = emfq_write_text_plain; + puri->mime_type = g_strdup ("text/html"); + em_format_add_puri (emf, puri); - if (emfq->priv->flags & EM_FORMAT_QUOTE_CITE) - camel_stream_write_string ( - stream, "
", - cancellable, NULL); + g_string_truncate (part_id, len); } -/* Decodes inline encoded parts of 'part'. The returned pointer, - * if not NULL, should be unreffed with g_object_unref(). */ -static CamelMimePart * -decode_inline_parts (CamelMimePart *part, - GCancellable *cancellable) +static void +emfq_parse_text_html (EMFormat * emf, + CamelMimePart * part, + GString * part_id, + EMFormatParserInfo * info, + GCancellable * cancellable) { - CamelMultipart *mp; - CamelStream *null; - CamelStream *filtered_stream; - EMInlineFilter *inline_filter; + EMFormatPURI *puri; + gint len; - g_return_val_if_fail (part != NULL, NULL); + len = part_id->len; + g_string_append (part_id, ".text_html"); - null = camel_stream_null_new (); - filtered_stream = camel_stream_filter_new (null); - g_object_unref (null); + puri = em_format_puri_new (emf, sizeof (EMFormatPURI), part, part_id->str); + puri->write_func = emfq_write_text_html; + puri->mime_type = g_strdup ("text/html"); + em_format_add_puri (emf, puri); - inline_filter = em_inline_filter_new ( - camel_mime_part_get_encoding (part), - camel_mime_part_get_content_type (part)); - camel_stream_filter_add ( - CAMEL_STREAM_FILTER (filtered_stream), - CAMEL_MIME_FILTER (inline_filter)); - camel_data_wrapper_decode_to_stream_sync ( - camel_medium_get_content (CAMEL_MEDIUM (part)), - filtered_stream, cancellable, NULL); - camel_stream_close (filtered_stream, cancellable, NULL); - g_object_unref (filtered_stream); + g_string_truncate (part_id, len); +} - if (!em_inline_filter_found_any (inline_filter)) { - g_object_unref (inline_filter); - return NULL; +static void +emfq_parse_text_enriched (EMFormat * emf, + CamelMimePart * part, + GString * part_id, + EMFormatParserInfo * info, + GCancellable * cancellable) +{ + EMFormatPURI *puri; + gint len; + + len = part_id->len; + g_string_append (part_id, ".text_enriched"); + + puri = em_format_puri_new (emf, sizeof (EMFormatPURI), part, part_id->str); + puri->write_func = emfq_write_text_enriched; + puri->mime_type = g_strdup ("text/html"); + em_format_add_puri (emf, puri); + + g_string_truncate (part_id, len); +} + +static void +emfq_parse_attachment (EMFormat * emf, + CamelMimePart * part, + GString * part_id, + EMFormatParserInfo * info, + GCancellable * cancellable) +{ + EMFormatPURI *puri; + gint len; + + len = part_id->len; + g_string_append (part_id, ".attachment"); + + puri = em_format_puri_new (emf, sizeof (EMFormatPURI), part, part_id->str); + puri->write_func = emfq_write_text_html; + puri->mime_type = g_strdup ("text/html"); + puri->is_attachment = TRUE; + em_format_add_puri (emf, puri); + + g_string_truncate (part_id, len); +} + +/******************************************************************************/ + +static void +emfq_write_attachment (EMFormat *emf, + EMFormatPURI *puri, + CamelStream *stream, + EMFormatWriterInfo *info, + GCancellable *cancellable) +{ + EMFormatQuote *emfq = EM_FORMAT_QUOTE (emf); + const EMFormatHandler *handler; + gchar *text, *html; + CamelContentType *ct; + const gchar *mime_type; + + ct = camel_mime_part_get_content_type (puri->part); + if (ct) { + mime_type = camel_content_type_simple (ct); + camel_content_type_unref (ct); + } else { + mime_type = "application/octet-stream"; } - mp = em_inline_filter_get_multipart (inline_filter); + handler = em_format_find_handler (emf, mime_type); - g_object_unref (inline_filter); + if (!em_format_is_inline (emf, puri->uri, puri->part, handler)) + return; - if (mp) { - part = camel_mime_part_new (); - camel_medium_set_content ( - CAMEL_MEDIUM (part), CAMEL_DATA_WRAPPER (mp)); - g_object_unref (mp); + camel_stream_write_string ( + stream, "" + "
\n", cancellable, NULL); + + /* output some info about it */ + text = em_format_describe_part (puri->part, mime_type); + html = camel_text_to_html ( + text, emfq->priv->text_html_flags & + CAMEL_MIME_FILTER_TOHTML_CONVERT_URLS, 0); + camel_stream_write_string (stream, html, cancellable, NULL); + g_free (html); + g_free (text); + + camel_stream_write_string ( + stream, "
", cancellable, NULL); + + if (handler && handler->write_func) + handler->write_func (emf, puri, stream, info, cancellable); +} + +static void +emfq_base_init (EMFormatQuoteClass *klass) +{ + emfq_builtin_init (klass); +} + +static void +emfq_class_init (EMFormatQuoteClass *klass) +{ + GObjectClass *object_class; + + parent_class = g_type_class_peek_parent (klass); + g_type_class_add_private (klass, sizeof (EMFormatQuotePrivate)); + + object_class = G_OBJECT_CLASS (klass); + object_class->dispose = emfq_dispose; + object_class->finalize = emfq_finalize; +} + +static void +emfq_init (EMFormatQuote *emfq) +{ + emfq->priv = EM_FORMAT_QUOTE_GET_PRIVATE (emfq); + + /* we want to convert url's etc */ + emfq->priv->text_html_flags = + CAMEL_MIME_FILTER_TOHTML_PRE | + CAMEL_MIME_FILTER_TOHTML_CONVERT_URLS | + CAMEL_MIME_FILTER_TOHTML_CONVERT_ADDRESSES; +} + +GType +em_format_quote_get_type (void) +{ + static GType type = 0; + + if (G_UNLIKELY (type == 0)) { + static const GTypeInfo type_info = { + sizeof (EMFormatQuoteClass), + (GBaseInitFunc) emfq_base_init, + (GBaseFinalizeFunc) NULL, + (GClassInitFunc) emfq_class_init, + (GClassFinalizeFunc) NULL, + NULL, /* class_data */ + sizeof (EMFormatQuote), + 0, /* n_preallocs */ + (GInstanceInitFunc) emfq_init, + NULL /* value_table */ + }; + + type = g_type_register_static ( + EM_TYPE_FORMAT, "EMFormatQuote", &type_info, 0); + } + + return type; +} + +EMFormatQuote * +em_format_quote_new (const gchar *credits, + CamelStream *stream, + EMFormatQuoteFlags flags) +{ + EMFormatQuote *emfq; + + g_return_val_if_fail (CAMEL_IS_STREAM (stream), NULL); + + /* Steam must also be seekable so we can reset its position. */ + g_return_val_if_fail (G_IS_SEEKABLE (stream), NULL); + + emfq = g_object_new (EM_TYPE_FORMAT_QUOTE, NULL); + + emfq->priv->credits = g_strdup (credits); + emfq->priv->flags = flags; + + return emfq; +} + +void +em_format_quote_write (EMFormatQuote * emfq, + CamelStream * stream, + GCancellable * cancellable) +{ + EMFormat *emf; + GSettings *settings; + GList *iter; + EMFormatWriterInfo info = { 0 }; + + emf = (EMFormat *) emfq; + + g_seekable_seek ( + G_SEEKABLE (stream), + 0, G_SEEK_SET, NULL, NULL); + + settings = g_settings_new ("org.gnome.evolution.mail"); + if (g_settings_get_boolean ( + settings, "composer-top-signature")) + camel_stream_write_string ( + stream, "
\n", cancellable, NULL); + g_object_unref (settings); + + if (emfq->priv->credits && *emfq->priv->credits) { + gchar *credits = g_strdup_printf ("%s
", emfq->priv->credits); + camel_stream_write_string (stream, credits, cancellable, NULL); + g_free (credits); } else { - g_object_ref (part); + camel_stream_write_string (stream, "
", cancellable, NULL); } - return part; + if (emfq->priv->flags & EM_FORMAT_QUOTE_CITE) + camel_stream_write_string (stream, + "\n" + "
\n", cancellable, NULL); + + for (iter = emf->mail_part_list; iter; iter = iter->next) { + EMFormatPURI *puri = iter->data; + + if (puri->is_attachment || !puri->write_func) + continue; + + puri = iter->data; + + if (emfq->priv->flags & EM_FORMAT_QUOTE_HEADERS) { + GString *buffer = g_string_new (""); + emfq_format_headers (emfq, buffer, (CamelMedium *) puri->part); + camel_stream_write_string (stream, buffer->str, cancellable, NULL); + g_string_free (buffer, TRUE); + } + + puri->write_func (emf, puri, stream, &info, cancellable); + } + + if (emfq->priv->flags & EM_FORMAT_QUOTE_CITE) + camel_stream_write_string ( + stream, "
", + cancellable, NULL); } static void -emfq_text_plain (EMFormat *emf, - CamelStream *stream, - CamelMimePart *part, - const EMFormatHandler *info, - GCancellable *cancellable, - gboolean is_fallback) +emfq_write_text_plain (EMFormat *emf, + EMFormatPURI *puri, + CamelStream *stream, + EMFormatWriterInfo *info, + GCancellable *cancellable) { EMFormatQuote *emfq = EM_FORMAT_QUOTE (emf); CamelStream *filtered_stream; CamelMimeFilter *html_filter; CamelMimeFilter *sig_strip; - CamelMimePart *mp; CamelContentType *type; const gchar *format; guint32 rgb = 0x737373, flags; - if (!part) + if (!puri->part) return; - mp = decode_inline_parts (part, cancellable); - if (mp) { - if (CAMEL_IS_MULTIPART (camel_medium_get_content (CAMEL_MEDIUM (mp)))) { - em_format_part (emf, stream, mp, cancellable); - g_object_unref (mp); - - return; - } - - g_object_unref (mp); - } - flags = emfq->priv->text_html_flags; /* Check for RFC 2646 flowed text. */ - type = camel_mime_part_get_content_type (part); + type = camel_mime_part_get_content_type (puri->part); if (camel_content_type_is(type, "text", "plain") && (format = camel_content_type_param(type, "format")) && !g_ascii_strcasecmp(format, "flowed")) @@ -687,25 +704,32 @@ emfq_text_plain (EMFormat *emf, em_format_format_text ( EM_FORMAT (emfq), filtered_stream, - CAMEL_DATA_WRAPPER (part), cancellable); + CAMEL_DATA_WRAPPER (puri->part), cancellable); camel_stream_flush (filtered_stream, cancellable, NULL); g_object_unref (filtered_stream); } static void -emfq_text_enriched (EMFormat *emf, - CamelStream *stream, - CamelMimePart *part, - const EMFormatHandler *info, - GCancellable *cancellable, - gboolean is_fallback) +emfq_write_text_enriched (EMFormat *emf, + EMFormatPURI *puri, + CamelStream *stream, + EMFormatWriterInfo *info, + GCancellable *cancellable) { CamelStream *filtered_stream; CamelMimeFilter *enriched; guint32 flags = 0; + CamelContentType *ct; + const gchar *mime_type = NULL; + + ct = camel_mime_part_get_content_type (puri->part); + if (ct) { + mime_type = camel_content_type_simple (ct); + camel_content_type_unref (ct); + } - if (g_strcmp0 (info->mime_type, "text/richtext") == 0) { + if (g_strcmp0 (mime_type, "text/richtext") == 0) { flags = CAMEL_MIME_FILTER_ENRICHED_IS_RICHTEXT; camel_stream_write_string ( stream, "\n\n", @@ -724,18 +748,17 @@ emfq_text_enriched (EMFormat *emf, camel_stream_write_string (stream, "


", cancellable, NULL); em_format_format_text ( - emf, filtered_stream, CAMEL_DATA_WRAPPER (part), cancellable); + emf, filtered_stream, CAMEL_DATA_WRAPPER (puri->part), cancellable); camel_stream_flush (filtered_stream, cancellable, NULL); g_object_unref (filtered_stream); } static void -emfq_text_html (EMFormat *emf, - CamelStream *stream, - CamelMimePart *part, - const EMFormatHandler *info, - GCancellable *cancellable, - gboolean is_fallback) +emfq_write_text_html (EMFormat *emf, + EMFormatPURI *puri, + CamelStream *stream, + EMFormatWriterInfo *info, + GCancellable *cancellable) { EMFormatQuotePrivate *priv; @@ -757,40 +780,29 @@ emfq_text_html (EMFormat *emf, em_format_format_text ( emf, filtered_stream, - (CamelDataWrapper *) part, cancellable); + (CamelDataWrapper *) puri->part, cancellable); camel_stream_flush (filtered_stream, cancellable, NULL); g_object_unref (filtered_stream); } else { em_format_format_text ( emf, stream, - (CamelDataWrapper *) part, cancellable); + (CamelDataWrapper *) puri->part, cancellable); } } -static void -emfq_ignore (EMFormat *emf, - CamelStream *stream, - CamelMimePart *part, - const EMFormatHandler *info, - GCancellable *cancellable, - gboolean is_fallback) -{ - /* NOOP */ -} - +/****************************************************************************/ static EMFormatHandler type_builtin_table[] = { - { (gchar *) "text/plain", emfq_text_plain }, - { (gchar *) "text/enriched", emfq_text_enriched }, - { (gchar *) "text/richtext", emfq_text_enriched }, - { (gchar *) "text/html", emfq_text_html }, - { (gchar *) "text/*", emfq_text_plain }, - { (gchar *) "message/external-body", emfq_ignore }, - { (gchar *) "multipart/appledouble", emfq_ignore }, + { (gchar *) "text/plain", emfq_parse_text_plain, emfq_write_text_plain, }, + { (gchar *) "text/enriched", emfq_parse_text_enriched, emfq_write_text_enriched, }, + { (gchar *) "text/richtext", emfq_parse_text_enriched, emfq_write_text_enriched, }, + { (gchar *) "text/html", emfq_parse_text_html, emfq_write_text_html, }, + { (gchar *) "text/*", emfq_parse_text_plain, emfq_write_text_plain, }, + { (gchar *) "message/external-body", em_format_empty_parser, em_format_empty_writer, }, + { (gchar *) "multipart/appledouble", em_format_empty_parser, em_format_empty_writer, }, /* internal evolution types */ - { (gchar *) "x-evolution/evolution-rss-feed", emfq_text_html }, - { (gchar *) "x-evolution/message/rfc822", emfq_format_message }, - { (gchar *) "x-evolution/message/prefix", emfq_format_message_prefix }, + { (gchar *) "x-evolution/evolution-rss-feed", 0, emfq_write_text_html, }, + { (gchar *) "x-evolution/message/attachment", emfq_parse_attachment, emfq_write_attachment, }, }; static void @@ -798,7 +810,9 @@ emfq_builtin_init (EMFormatQuoteClass *efhc) { gint ii; + EMFormatClass *emfc = (EMFormatClass *) efhc; + for (ii = 0; ii < G_N_ELEMENTS (type_builtin_table); ii++) em_format_class_add_handler ( - EM_FORMAT_CLASS (efhc), &type_builtin_table[ii]); + emfc, &type_builtin_table[ii]); } diff --git a/em-format/em-format-quote.h b/em-format/em-format-quote.h index 5c1882eb32..be3640735a 100644 --- a/em-format/em-format-quote.h +++ b/em-format/em-format-quote.h @@ -69,6 +69,9 @@ GType em_format_quote_get_type (void); EMFormatQuote * em_format_quote_new (const gchar *credits, CamelStream *stream, EMFormatQuoteFlags flags); +void em_format_quote_write (EMFormatQuote *emfq, + CamelStream *stream, + GCancellable *cancellable); G_END_DECLS diff --git a/em-format/em-format.c b/em-format/em-format.c index d476036f77..4abe35482c 100644 --- a/em-format/em-format.c +++ b/em-format/em-format.c @@ -25,1036 +25,1197 @@ #include #endif -#include #include - #include #include +#include #include "em-format.h" #include "e-util/e-util.h" #include "shell/e-shell.h" #include "shell/e-shell-settings.h" +#define d(x) + #define EM_FORMAT_GET_PRIVATE(obj) \ (G_TYPE_INSTANCE_GET_PRIVATE \ ((obj), EM_TYPE_FORMAT, EMFormatPrivate)) -#define d(x) - -typedef struct _EMFormatCache EMFormatCache; - struct _EMFormatPrivate { - guint redraw_idle_id; -}; - -/* Used to cache various data/info for redraws - * The validity stuff could be cached at a higher level but this is easier - * This absolutely relies on the partid being _globally unique_ - * This is still kind of yucky, we should maintain a full tree of all this data, - * along with/as part of the puri tree */ -struct _EMFormatCache { - CamelCipherValidity *valid; /* validity copy */ - CamelMimePart *secured; /* encrypted subpart */ + GNode *current_node; - guint state:2; /* inline state */ + CamelSession *session; - gchar partid[1]; -}; + CamelURL *base_url; -#define INLINE_UNSET (0) -#define INLINE_ON (1) -#define INLINE_OFF (2) + gchar *charset; + gchar *default_charset; + gboolean composer; -static void emf_builtin_init (EMFormatClass *); + gint last_error; +}; enum { - EMF_COMPLETE, - EMF_LAST_SIGNAL + PROP_0, + PROP_CHARSET, + PROP_DEFAULT_CHARSET, + PROP_COMPOSER, + PROP_BASE_URL }; -static gpointer parent_class; -static guint signals[EMF_LAST_SIGNAL]; - -static void -emf_free_cache (EMFormatCache *efc) -{ - if (efc->valid) - camel_cipher_validity_free (efc->valid); - if (efc->secured) - g_object_unref (efc->secured); - g_free (efc); -} - -static EMFormatCache * -emf_insert_cache (EMFormat *emf, - const gchar *partid) -{ - EMFormatCache *new; - - new = g_malloc0 (sizeof (*new) + strlen (partid)); - strcpy (new->partid, partid); - g_hash_table_insert (emf->inline_table, new->partid, new); +enum { + REDRAW_REQUESTED, + LAST_SIGNAL +}; - return new; -} +gint signals[LAST_SIGNAL]; -static void -emf_clone_inlines (gpointer key, - gpointer val, - gpointer data) -{ - EMFormatCache *emfc = val, *new; +static gpointer parent_class; - new = emf_insert_cache ((EMFormat *) data, emfc->partid); - new->state = emfc->state; - if (emfc->valid) - new->valid = camel_cipher_validity_clone (emfc->valid); - if (emfc->secured) - g_object_ref ((new->secured = emfc->secured)); -} +/* PARSERS */ +static void emf_parse_application_xpkcs7mime (EMFormat *emf, CamelMimePart *part, GString *part_id, EMFormatParserInfo *info, GCancellable *cancellable); +static void emf_parse_application_mbox (EMFormat *emf, CamelMimePart *part, GString *part_id, EMFormatParserInfo *info, GCancellable *cancellable); +static void emf_parse_multipart_alternative (EMFormat *emf, CamelMimePart *part, GString *part_id, EMFormatParserInfo *info, GCancellable *cancellable); +static void emf_parse_multipart_appledouble (EMFormat *emf, CamelMimePart *part, GString *part_id, EMFormatParserInfo *info, GCancellable *cancellable); +static void emf_parse_multipart_encrypted (EMFormat *emf, CamelMimePart *part, GString *part_id, EMFormatParserInfo *info, GCancellable *cancellable); +static void emf_parse_multipart_mixed (EMFormat *emf, CamelMimePart *part, GString *part_id, EMFormatParserInfo *info, GCancellable *cancellable); +static void emf_parse_multipart_signed (EMFormat *emf, CamelMimePart *part, GString *part_id, EMFormatParserInfo *info, GCancellable *cancellable); +static void emf_parse_multipart_related (EMFormat *emf, CamelMimePart *part, GString *part_id, EMFormatParserInfo *info, GCancellable *cancellable); +static void emf_parse_multipart_digest (EMFormat *emf, CamelMimePart *part, GString *part_id, EMFormatParserInfo *info, GCancellable *cancellable); +static void emf_parse_message_deliverystatus (EMFormat *emf, CamelMimePart *part, GString *part_id, EMFormatParserInfo *info, GCancellable *cancellable); +static void emf_parse_inlinepgp_signed (EMFormat *emf, CamelMimePart *part, GString *part_id, EMFormatParserInfo *info, GCancellable *cancellable); +static void emf_parse_inlinepgp_encrypted (EMFormat *emf, CamelMimePart *part, GString *part_id, EMFormatParserInfo *info, GCancellable *cancellable); +static void emf_parse_message (EMFormat *emf, CamelMimePart *part, GString *part_id, EMFormatParserInfo *info, GCancellable *cancellable); +static void emf_parse_headers (EMFormat *emf, CamelMimePart *part, GString *part_id, EMFormatParserInfo *info, GCancellable *cancellable); +static void emf_parse_post_headers (EMFormat *emf, CamelMimePart *part, GString *part_id, EMFormatParserInfo *info, GCancellable *cancellable); +static void emf_parse_source (EMFormat *emf, CamelMimePart *part, GString *part_id, EMFormatParserInfo *info, GCancellable *cancellable); + +/* WRITERS */ +static void emf_write_text (EMFormat *emf, EMFormatPURI *puri, CamelStream *stream, EMFormatWriterInfo *info, GCancellable *cancellable); +static void emf_write_source (EMFormat *emf, EMFormatPURI *puri, CamelStream *stream, EMFormatWriterInfo *info, GCancellable *cancellable); +static void emf_write_error (EMFormat *emf, EMFormatPURI *puri, CamelStream *stream, EMFormatWriterInfo *info, GCancellable *cancellable); + +/**************************************************************************/ static gboolean -emf_clear_puri_node (GNode *node) +is_secured (CamelMimePart *part) { - GQueue *queue = node->data; - EMFormatPURI *pn; - - while ((pn = g_queue_pop_head (queue)) != NULL) { - if (pn->free != NULL) - pn->free (pn); - g_free (pn->uri); - g_free (pn->cid); - g_free (pn->part_id); - if (pn->part != NULL) - g_object_unref (pn->part); - g_free (pn); - } - - g_queue_free (queue); + CamelContentType *ct = camel_mime_part_get_content_type (part); - return FALSE; + return (camel_content_type_is (ct, "multipart", "signed") || + camel_content_type_is (ct, "multipart", "encrypted") || + camel_content_type_is (ct, "application", "x-inlinepgp-signed") || + camel_content_type_is (ct, "application", "x-inlinepgp-encrypted") || + camel_content_type_is (ct, "application", "x-pkcs7-mime") || + camel_content_type_is (ct, "application", "pkcs7-mime")); } static void -emf_finalize (GObject *object) +preserve_charset_in_content_type (CamelMimePart *ipart, + CamelMimePart *opart) { - EMFormat *emf = EM_FORMAT (object); - - if (emf->priv->redraw_idle_id > 0) - g_source_remove (emf->priv->redraw_idle_id); - - if (emf->session) - g_object_unref (emf->session); + CamelDataWrapper *data_wrapper; + CamelContentType *content_type; + const gchar *charset; - if (emf->message) - g_object_unref (emf->message); + g_return_if_fail (ipart != NULL); + g_return_if_fail (opart != NULL); - g_hash_table_destroy (emf->inline_table); + data_wrapper = camel_medium_get_content (CAMEL_MEDIUM (ipart)); + content_type = camel_data_wrapper_get_mime_type_field (data_wrapper); - em_format_clear_headers (emf); - camel_cipher_validity_free (emf->valid); - g_free (emf->charset); - g_free (emf->default_charset); - g_string_free (emf->part_id, TRUE); - g_free (emf->current_message_part_id); - g_free (emf->uid); + if (content_type == NULL) + return; - if (emf->pending_uri_table != NULL) - g_hash_table_destroy (emf->pending_uri_table); + charset = camel_content_type_param (content_type, "charset"); - if (emf->pending_uri_tree != NULL) { - g_node_traverse ( - emf->pending_uri_tree, - G_IN_ORDER, G_TRAVERSE_ALL, -1, - (GNodeTraverseFunc) emf_clear_puri_node, NULL); - g_node_destroy (emf->pending_uri_tree); - } + if (charset == NULL || *charset == '\0') + return; - /* FIXME: check pending jobs */ + data_wrapper = camel_medium_get_content (CAMEL_MEDIUM (opart)); + content_type = camel_data_wrapper_get_mime_type_field (data_wrapper); - /* Chain up to parent's finalize() method. */ - G_OBJECT_CLASS (parent_class)->finalize (object); + camel_content_type_set_param (content_type, "charset", charset); } -static const EMFormatHandler * -emf_find_handler (EMFormat *emf, - const gchar *mime_type) +static CamelMimePart * +get_related_display_part (CamelMimePart *part, + gint *out_displayid) { - EMFormatClass *emfc = (EMFormatClass *) G_OBJECT_GET_CLASS (emf); - - return g_hash_table_lookup (emfc->type_handlers, mime_type); -} + CamelMultipart *mp; + CamelMimePart *body_part, *display_part = NULL; + CamelContentType *content_type; + const gchar *start; + gint i, nparts, displayid = 0; -static void -emf_format_clone (EMFormat *emf, - CamelFolder *folder, - const gchar *uid, - CamelMimeMessage *msg, - EMFormat *emfsource, - GCancellable *cancellable) -{ - /* Cancel any pending redraws. */ - if (emf->priv->redraw_idle_id > 0) { - g_source_remove (emf->priv->redraw_idle_id); - emf->priv->redraw_idle_id = 0; - } + mp = (CamelMultipart *) camel_medium_get_content ((CamelMedium *) part); - em_format_clear_puri_tree (emf); + if (!CAMEL_IS_MULTIPART (mp)) + return NULL; - if (emf != emfsource) { - g_hash_table_remove_all (emf->inline_table); - if (emfsource) { - GList *link; + nparts = camel_multipart_get_number (mp); + content_type = camel_mime_part_get_content_type (part); + start = camel_content_type_param (content_type, "start"); + if (start && strlen (start) > 2) { + gint len; + const gchar *cid; - /* We clone the current state here */ - g_hash_table_foreach (emfsource->inline_table, emf_clone_inlines, emf); - emf->mode = emfsource->mode; - g_free (emf->charset); - emf->charset = g_strdup (emfsource->charset); - g_free (emf->default_charset); - emf->default_charset = g_strdup (emfsource->default_charset); + /* strip <>'s from CID */ + len = strlen (start) - 2; + start++; - em_format_clear_headers (emf); + for (i = 0; i < nparts; i++) { + body_part = camel_multipart_get_part (mp, i); + cid = camel_mime_part_get_content_id (body_part); - link = g_queue_peek_head_link (&emfsource->header_list); - while (link != NULL) { - struct _EMFormatHeader *h = link->data; - em_format_add_header (emf, h->name, h->flags); - link = g_list_next (link); + if (cid && !strncmp (cid, start, len) && strlen (cid) == len) { + display_part = body_part; + displayid = i; + break; } } + } else { + display_part = camel_multipart_get_part (mp, 0); } - /* what a mess */ - if (folder != emf->folder) { - if (emf->folder) - g_object_unref (emf->folder); - if (folder) - g_object_ref (folder); - emf->folder = folder; - } - - if (uid != emf->uid) { - g_free (emf->uid); - emf->uid = g_strdup (uid); - } - - if (msg != emf->message) { - if (emf->message) - g_object_unref (emf->message); - if (msg) - g_object_ref (msg); - emf->message = msg; - } + if (out_displayid) + *out_displayid = displayid; - g_free (emf->current_message_part_id); - emf->current_message_part_id = g_strdup ("root-message"); - g_string_truncate (emf->part_id, 0); - if (folder != NULL) - /* TODO build some string based on the folder name/location? */ - g_string_append_printf(emf->part_id, ".%p", (gpointer) folder); - if (uid != NULL) - g_string_append_printf(emf->part_id, ".%s", uid); + return display_part; } -static void -emf_format_secure (EMFormat *emf, - CamelStream *stream, - CamelMimePart *part, - CamelCipherValidity *valid, - GCancellable *cancellable) +static gboolean +related_display_part_is_attachment (EMFormat *emf, + CamelMimePart *part) { - CamelCipherValidity *save = emf->valid_parent; - gint len; - - /* Note that this also requires support from higher up in the class chain - * - validity needs to be cleared when you start output - * - also needs to be cleared (but saved) whenever you start a new message. */ - - if (emf->valid == NULL) { - emf->valid = valid; - } else { - g_queue_push_tail (&emf->valid_parent->children, valid); - camel_cipher_validity_envelope (emf->valid_parent, valid); - } - - emf->valid_parent = valid; - - len = emf->part_id->len; - g_string_append_printf(emf->part_id, ".secured"); - em_format_part (emf, stream, part, cancellable); - g_string_truncate (emf->part_id, len); + CamelMimePart *display_part; - emf->valid_parent = save; + display_part = get_related_display_part (part, NULL); + return display_part && em_format_is_attachment (emf, display_part); } -static gboolean -emf_busy (EMFormat *emf) +/**************************************************************************/ +void +em_format_empty_parser (EMFormat *emf, + CamelMimePart *part, + GString *part_id, + EMFormatParserInfo *info, + GCancellable *cancellable) { - return FALSE; + /* DO NOTHING */ } -static gboolean -emf_is_inline (EMFormat *emf, - const gchar *part_id, - CamelMimePart *mime_part, - const EMFormatHandler *handle) +#ifdef ENABLE_SMIME +static void +emf_parse_application_xpkcs7mime (EMFormat *emf, + CamelMimePart *part, + GString *part_id, + EMFormatParserInfo *info, + GCancellable *cancellable) { - EMFormatCache *emfc; - const gchar *disposition; + CamelCipherContext *context; + CamelMimePart *opart; + CamelCipherValidity *valid; + GError *local_error = NULL; - if (handle == NULL) - return FALSE; + if (g_cancellable_is_cancelled (cancellable)) + return; - emfc = g_hash_table_lookup (emf->inline_table, part_id); - if (emfc && emfc->state != INLINE_UNSET) - return emfc->state & 1; + context = camel_smime_context_new (emf->priv->session); - /* Some types need to override the disposition. - * e.g. application/x-pkcs7-mime */ - if (handle->flags & EM_FORMAT_HANDLER_INLINE_DISPOSITION) - return TRUE; + opart = camel_mime_part_new (); + valid = camel_cipher_context_decrypt_sync ( + context, part, opart, cancellable, &local_error); + preserve_charset_in_content_type (part, opart); + if (valid == NULL) { + em_format_format_error ( + emf, "%s", + local_error->message ? local_error->message : + _("Could not parse S/MIME message: Unknown error")); + g_clear_error (&local_error); + } else { + EMFormatParserInfo encinfo = { + info->handler, + info->validity_type | EM_FORMAT_VALIDITY_FOUND_ENCRYPTED | EM_FORMAT_VALIDITY_FOUND_SMIME, + valid + }; + gint len = part_id->len; + + g_string_append (part_id, ".encrypted"); + em_format_parse_part (emf, opart, part_id, &encinfo, cancellable); + g_string_truncate (part_id, len); + + /* Add a widget with details about the encryption, but only when + * the encrypted isn't itself secured, in that case it has created + * the button itself */ + if (!is_secured (opart)) { + g_string_append (part_id, ".encrypted.button"); + em_format_parse_part_as (emf, part, part_id, &encinfo, + "x-evolution/message/x-secure-button", cancellable); + g_string_truncate (part_id, len); + } - disposition = camel_mime_part_get_disposition (mime_part); - if (disposition != NULL) - return g_ascii_strcasecmp (disposition, "inline") == 0; + camel_cipher_validity_free (valid); + } - /* Otherwise, use the default for this handler type. */ - return (handle->flags & EM_FORMAT_HANDLER_INLINE) != 0; + g_object_unref (opart); + g_object_unref (context); } +#endif +/* RFC 4155 */ static void -emf_base_init (EMFormatClass *class) +emf_parse_application_mbox (EMFormat *emf, + CamelMimePart *mime_part, + GString *part_id, + EMFormatParserInfo *info, + GCancellable *cancellable) { - class->type_handlers = g_hash_table_new (g_str_hash, g_str_equal); - emf_builtin_init (class); -} + CamelMimeParser *parser; + CamelStream *mem_stream; + camel_mime_parser_state_t state; + gint old_len; + gint messages; -static void -emf_class_init (EMFormatClass *class) -{ - GObjectClass *object_class; + if (g_cancellable_is_cancelled (cancellable)) + return; - parent_class = g_type_class_peek_parent (class); - g_type_class_add_private (class, sizeof (EMFormatPrivate)); + /* Extract messages from the application/mbox part and + * render them as a flat list of messages. */ - object_class = G_OBJECT_CLASS (class); - object_class->finalize = emf_finalize; + /* XXX If the mbox has multiple messages, maybe render them + * as a multipart/digest so each message can be expanded + * or collapsed individually. + * + * See attachment_handler_mail_x_uid_list() for example. */ - class->find_handler = emf_find_handler; - class->format_clone = emf_format_clone; - class->format_secure = emf_format_secure; - class->busy = emf_busy; - class->is_inline = emf_is_inline; + /* XXX This is based on em_utils_read_messages_from_stream(). + * Perhaps refactor that function to return an array of + * messages instead of assuming we want to append them + * to a folder? */ - signals[EMF_COMPLETE] = g_signal_new ( - "complete", - G_OBJECT_CLASS_TYPE (class), - G_SIGNAL_RUN_LAST, - G_STRUCT_OFFSET (EMFormatClass, complete), - NULL, NULL, - g_cclosure_marshal_VOID__VOID, - G_TYPE_NONE, 0); -} + parser = camel_mime_parser_new (); + camel_mime_parser_scan_from (parser, TRUE); -static void -emf_init (EMFormat *emf) -{ - EShell *shell; - EShellSettings *shell_settings; + mem_stream = camel_stream_mem_new (); + camel_data_wrapper_decode_to_stream_sync ( + camel_medium_get_content (CAMEL_MEDIUM (mime_part)), + mem_stream, NULL, NULL); + g_seekable_seek (G_SEEKABLE (mem_stream), 0, G_SEEK_SET, NULL, NULL); + camel_mime_parser_init_with_stream (parser, mem_stream, NULL); + g_object_unref (mem_stream); - emf->priv = EM_FORMAT_GET_PRIVATE (emf); + old_len = part_id->len; - emf->inline_table = g_hash_table_new_full ( - g_str_hash, g_str_equal, - (GDestroyNotify) NULL, - (GDestroyNotify) emf_free_cache); - emf->composer = FALSE; - emf->print = FALSE; - g_queue_init (&emf->header_list); - em_format_default_headers (emf); - emf->part_id = g_string_new(""); - emf->current_message_part_id = NULL; - emf->validity_found = 0; + /* Extract messages from the mbox. */ + messages = 0; + state = camel_mime_parser_step (parser, NULL, NULL); - shell = e_shell_get_default (); - shell_settings = e_shell_get_shell_settings (shell); + while (state == CAMEL_MIME_PARSER_STATE_FROM) { + CamelMimeMessage *message; - emf->session = e_shell_settings_get_pointer (shell_settings, "mail-session"); - g_return_if_fail (emf->session != NULL); + message = camel_mime_message_new (); + mime_part = CAMEL_MIME_PART (message); - g_object_ref (emf->session); -} + if (!camel_mime_part_construct_from_parser_sync ( + mime_part, parser, NULL, NULL)) { + g_object_unref (message); + break; + } -GType -em_format_get_type (void) -{ - static GType type = 0; + g_string_append_printf (part_id, ".mbox.%d", messages); + em_format_parse_part_as (emf, CAMEL_MIME_PART (message), + part_id, info, "message/rfc822", cancellable); + g_string_truncate (part_id, old_len); - if (G_UNLIKELY (type == 0)) { - static const GTypeInfo type_info = { - sizeof (EMFormatClass), - (GBaseInitFunc) emf_base_init, - (GBaseFinalizeFunc) NULL, - (GClassInitFunc) emf_class_init, - (GClassFinalizeFunc) NULL, - NULL, /* class_data */ - sizeof (EMFormat), - 0, /* n_preallocs */ - (GInstanceInitFunc) emf_init, - NULL /* value_table */ - }; + g_object_unref (message); - type = g_type_register_static ( - G_TYPE_OBJECT, "EMFormat", &type_info, 0); + /* Skip past CAMEL_MIME_PARSER_STATE_FROM_END. */ + camel_mime_parser_step (parser, NULL, NULL); + + state = camel_mime_parser_step (parser, NULL, NULL); + + messages++; } - return type; + g_object_unref (parser); } -/** - * em_format_class_add_handler: - * @emfc: EMFormatClass - * @info: Callback information. - * - * Add a mime type handler to this class. This is only used by - * implementing classes. The @info.old pointer will automatically be - * setup to point to the old handler if one was already set. This can - * be used for overrides a fallback. - * - * When a mime type described by @info is encountered, the callback will - * be invoked. Note that @info may be extended by sub-classes if - * they require additional context information. - * - * Use a mime type of "foo/ *" to insert a fallback handler for type "foo". - **/ -void -em_format_class_add_handler (EMFormatClass *emfc, - EMFormatHandler *info) +/* RFC 1740 */ +static void +emf_parse_multipart_alternative (EMFormat *emf, + CamelMimePart *part, + GString *part_id, + EMFormatParserInfo *info, + GCancellable *cancellable) { - info->old = g_hash_table_lookup (emfc->type_handlers, info->mime_type); - g_hash_table_insert (emfc->type_handlers, (gpointer) info->mime_type, info); -} + CamelMultipart *mp; + gint i, nparts, bestid = 0; + CamelMimePart *best = NULL; -struct _class_handlers { - EMFormatClass *old; - EMFormatClass *new; -}; + if (g_cancellable_is_cancelled (cancellable)) + return; -static void -merge_missing (gpointer key, - gpointer value, - gpointer userdata) -{ - struct _class_handlers *classes = (struct _class_handlers *) userdata; - EMFormatHandler *info; + mp = (CamelMultipart *) camel_medium_get_content ((CamelMedium *) part); - info = g_hash_table_lookup (classes->new->type_handlers, key); - if (!info) { - /* Might be from a plugin */ - g_hash_table_insert (classes->new->type_handlers, key, value); + if (!CAMEL_IS_MULTIPART (mp)) { + emf_parse_source (emf, part, part_id, info, cancellable); + return; } -} + /* as per rfc, find the last part we know how to display */ + nparts = camel_multipart_get_number (mp); + for (i = 0; i < nparts; i++) { + CamelMimePart *mpart; + CamelDataWrapper *data_wrapper; + CamelContentType *type; + CamelStream *null_stream; + gchar *mime_type; + gsize content_size; -void -em_format_merge_handler (EMFormat *new, - EMFormat *old) -{ - EMFormatClass *oldc = (EMFormatClass *) G_OBJECT_GET_CLASS (old); - EMFormatClass *newc = (EMFormatClass *) G_OBJECT_GET_CLASS (new); - struct _class_handlers fclasses; + if (g_cancellable_is_cancelled (cancellable)) + return; - fclasses.old = oldc; - fclasses.new = newc; + /* is it correct to use the passed in *part here? */ + mpart = camel_multipart_get_part (mp, i); - g_hash_table_foreach (oldc->type_handlers, merge_missing, &fclasses); + if (mpart == NULL) + continue; -} + /* This may block even though the stream does not. + * XXX Pretty inefficient way to test if the MIME part + * is empty. Surely there's a quicker way? */ + null_stream = camel_stream_null_new (); + data_wrapper = camel_medium_get_content (CAMEL_MEDIUM (mpart)); + camel_data_wrapper_decode_to_stream_sync ( + data_wrapper, null_stream, cancellable, NULL); + content_size = CAMEL_STREAM_NULL (null_stream)->written; + g_object_unref (null_stream); -/** - * em_format_class_remove_handler: - * @emfc: - * @info: - * - * Remove a handler. @info must be a value which was previously - * added. - **/ -void -em_format_class_remove_handler (EMFormatClass *emfc, - EMFormatHandler *info) -{ - EMFormatHandler *current; + if (content_size == 0) + continue; - /* TODO: thread issues? */ + type = camel_mime_part_get_content_type (mpart); + mime_type = camel_content_type_simple (type); - current = g_hash_table_lookup (emfc->type_handlers, info->mime_type); - if (current == info) { - current = info->old; - if (current) - g_hash_table_insert ( - emfc->type_handlers, - (gpointer) current->mime_type, current); - else - g_hash_table_remove ( - emfc->type_handlers, info->mime_type); - } else { - while (current && current->old != info) - current = current->old; - g_return_if_fail (current != NULL); - current->old = info->old; - } -} + camel_strdown (mime_type); -/** - * em_format_find_handler: - * @emf: - * @mime_type: - * - * Find a format handler by @mime_type. - * - * Return value: NULL if no handler is available. - **/ -const EMFormatHandler * -em_format_find_handler (EMFormat *emf, - const gchar *mime_type) -{ - EMFormatClass *class; + if (!em_format_is_attachment (emf, mpart) && + ((camel_content_type_is (type, "multipart", "related") == 0) || + !related_display_part_is_attachment (emf, mpart)) && + (em_format_find_handler (emf, mime_type) + || (best == NULL && em_format_fallback_handler (emf, mime_type)))) { + best = mpart; + bestid = i; + } - g_return_val_if_fail (EM_IS_FORMAT (emf), NULL); - g_return_val_if_fail (mime_type != NULL, NULL); + g_free (mime_type); + } - class = EM_FORMAT_GET_CLASS (emf); - g_return_val_if_fail (class->find_handler != NULL, NULL); + if (best) { + gint len = part_id->len; - return class->find_handler (emf, mime_type); + g_string_append_printf(part_id, ".alternative.%d", bestid); + em_format_parse_part (emf, best, part_id, info, cancellable); + g_string_truncate (part_id, len); + } else + emf_parse_multipart_mixed (emf, part, part_id, info, cancellable); } -/** - * em_format_fallback_handler: - * @emf: - * @mime_type: - * - * Try to find a format handler based on the major type of the @mime_type. - * - * The subtype is replaced with "*" and a lookup performed. - * - * Return value: - **/ -const EMFormatHandler * -em_format_fallback_handler (EMFormat *emf, - const gchar *mime_type) +/* RFC 1740 */ +static void +emf_parse_multipart_appledouble (EMFormat *emf, + CamelMimePart *part, + GString *part_id, + EMFormatParserInfo *info, + GCancellable *cancellable) { - gchar *mime, *s; + CamelMultipart *mp; + CamelMimePart *mime_part; - s = strchr (mime_type, '/'); - if (s == NULL) - mime = (gchar *) mime_type; - else { - gsize len = (s - mime_type) + 1; + if (g_cancellable_is_cancelled (cancellable)) + return; - mime = g_alloca (len + 2); - strncpy (mime, mime_type, len); - strcpy(mime+len, "*"); + mp = (CamelMultipart *) camel_medium_get_content ((CamelMedium *) part); + + if (!CAMEL_IS_MULTIPART (mp)) { + emf_parse_source (emf, part, part_id, info, cancellable); + return; } - return em_format_find_handler (emf, mime); + mime_part = camel_multipart_get_part (mp, 1); + if (mime_part) { + gint len; + /* try the data fork for something useful, doubtful but who knows */ + len = part_id->len; + g_string_append_printf(part_id, ".appledouble.1"); + em_format_parse_part (emf, mime_part, part_id, info, cancellable); + g_string_truncate (part_id, len); + } else { + emf_parse_source (emf, part, part_id, info, cancellable); + } } -/** - * em_format_add_puri: - * @emf: - * @size: - * @cid: Override the autogenerated content id. - * @part: - * @func: - * - * Add a pending-uri handler. When formatting parts that reference - * other parts, a pending-uri (PURI) can be used to track the reference. - * - * @size is used to allocate the structure, so that it can be directly - * subclassed by implementors. - * - * @cid can be used to override the key used to retreive the PURI, if NULL, - * then the content-location and the content-id of the @part are stored - * as lookup keys for the part. - * - * FIXME: This may need a free callback. - * - * Return value: A new PURI, with a referenced copy of @part, and the cid - * always set. The uri will be set if one is available. Clashes - * are resolved by forgetting the old PURI in the global index. - **/ -EMFormatPURI * -em_format_add_puri (EMFormat *emf, - gsize size, - const gchar *cid, - CamelMimePart *part, - EMFormatPURIFunc func) +static void +emf_parse_multipart_encrypted (EMFormat *emf, + CamelMimePart *part, + GString *part_id, + EMFormatParserInfo *info, + GCancellable *cancellable) { - EMFormatPURI *puri; - const gchar *tmp; + CamelCipherContext *context; + const gchar *protocol; + CamelMimePart *opart; + CamelCipherValidity *valid; + CamelMultipartEncrypted *mpe; + GError *local_error = NULL; - d(printf("adding puri for part: %s\n", emf->part_id->str)); + if (g_cancellable_is_cancelled (cancellable)) + return; - if (size < sizeof (*puri)) { - g_warning ( - "size (%" G_GSIZE_FORMAT - ") less than size of puri\n", size); - size = sizeof (*puri); + mpe = (CamelMultipartEncrypted *) camel_medium_get_content ((CamelMedium *) part); + if (!CAMEL_IS_MULTIPART_ENCRYPTED (mpe)) { + em_format_format_error ( + emf, _("Could not parse MIME message. " + "Displaying as source.")); + emf_parse_source (emf, part, part_id, info, cancellable); + return; } - puri = g_malloc0 (size); - - puri->format = emf; - puri->func = func; - puri->use_count = 0; - puri->cid = g_strdup (cid); - puri->part_id = g_strdup (emf->part_id->str); - - if (part) { - g_object_ref (part); - puri->part = part; + /* Currently we only handle RFC2015-style PGP encryption. */ + protocol = camel_content_type_param ( + ((CamelDataWrapper *)mpe)->mime_type, "protocol"); + if (!protocol || g_ascii_strcasecmp (protocol, "application/pgp-encrypted") != 0) { + em_format_format_error (emf, _("Unsupported encryption type for multipart/encrypted")); + emf_parse_multipart_mixed (emf, part, part_id, info, cancellable); + return; } - if (part != NULL && cid == NULL) { - tmp = camel_mime_part_get_content_id (part); - if (tmp) - puri->cid = g_strdup_printf("cid:%s", tmp); - else - puri->cid = g_strdup_printf("em-no-cid:%s", emf->part_id->str); + context = camel_gpg_context_new (emf->priv->session); + opart = camel_mime_part_new (); + valid = camel_cipher_context_decrypt_sync ( + context, part, opart, cancellable, &local_error); + preserve_charset_in_content_type (part, opart); + if (valid == NULL) { + em_format_format_error ( + emf, local_error->message ? + _("Could not parse PGP/MIME message") : + _("Could not parse PGP/MIME message: Unknown error")); + if (local_error->message != NULL) + em_format_format_error ( + emf, "%s", local_error->message); + g_clear_error (&local_error); + emf_parse_multipart_mixed (emf, part, part_id, info, cancellable); + } else { + gint len = part_id->len; - d(printf("built cid '%s'\n", puri->cid)); + EMFormatParserInfo encinfo = { + info->handler, + info->validity_type | EM_FORMAT_VALIDITY_FOUND_ENCRYPTED | EM_FORMAT_VALIDITY_FOUND_PGP, + }; - /* Not quite same as old behaviour, it also put in the - * relative uri and a fallback for no parent uri. */ - tmp = camel_mime_part_get_content_location (part); - puri->uri = NULL; - if (tmp == NULL) { - /* No location, don't set a uri at all, - * html parts do this themselves. */ - } else { - if (strchr (tmp, ':') == NULL && emf->base != NULL) { - CamelURL *uri; - - uri = camel_url_new_with_base (emf->base, tmp); - puri->uri = camel_url_to_string (uri, 0); - camel_url_free (uri); - } else { - puri->uri = g_strdup (tmp); - } - } - } + if (info->validity) + camel_cipher_validity_envelope (valid, info->validity); - g_return_val_if_fail (puri->cid != NULL, NULL); - g_return_val_if_fail (emf->pending_uri_level != NULL, NULL); - g_return_val_if_fail (emf->pending_uri_table != NULL, NULL); + encinfo.validity = valid; - g_queue_push_tail (emf->pending_uri_level->data, puri); + g_string_append (part_id, ".encrypted"); + em_format_parse_part (emf, opart, part_id, &encinfo, cancellable); + g_string_truncate (part_id, len); - if (puri->uri) - g_hash_table_insert (emf->pending_uri_table, puri->uri, puri); - g_hash_table_insert (emf->pending_uri_table, puri->cid, puri); + /* Add a widget with details about the encryption, but only when + * the encrypted isn't itself secured, in that case it has created + * the button itself */ + if (!is_secured (opart)) { + g_string_append (part_id, ".encrypted.button"); + em_format_parse_part_as (emf, part, part_id, &encinfo, + "x-evolution/message/x-secure-button", cancellable); + g_string_truncate (part_id, len); + } - return puri; + camel_cipher_validity_free (valid); + } + + /* TODO: Make sure when we finalize this part, it is zero'd out */ + g_object_unref (opart); + g_object_unref (context); } -/** - * em_format_push_level: - * @emf: - * - * This is used to build a hierarchy of visible PURI objects based on - * the structure of the message. Used by multipart/alternative formatter. - * - * FIXME: This could probably also take a uri so it can automatically update - * the base location. - **/ -void -em_format_push_level (EMFormat *emf) +/* RFC 2046 */ +static void +emf_parse_multipart_mixed (EMFormat *emf, + CamelMimePart *part, + GString *part_id, + EMFormatParserInfo *info, + GCancellable *cancellable) { - GNode *node; + CamelMultipart *mp; + gint i, nparts, len; - g_return_if_fail (EM_IS_FORMAT (emf)); + if (g_cancellable_is_cancelled (cancellable)) + return; - node = g_node_new (g_queue_new ()); + mp = (CamelMultipart *) camel_medium_get_content ((CamelMedium *) part); - if (emf->pending_uri_tree == NULL) - emf->pending_uri_tree = node; - else - g_node_append (emf->pending_uri_tree, node); + if (!CAMEL_IS_MULTIPART (mp)) { + emf_parse_source (emf, part, part_id, info, cancellable); + return; + } - emf->pending_uri_level = node; -} + len = part_id->len; + nparts = camel_multipart_get_number (mp); + for (i = 0; i < nparts; i++) { + CamelMimePart *subpart; -/** - * em_format_pull_level: - * @emf: - * - * Drop a level of visibility back to the parent. Note that - * no PURI values are actually freed. - **/ -void -em_format_pull_level (EMFormat *emf) -{ - g_return_if_fail (EM_IS_FORMAT (emf)); - g_return_if_fail (emf->pending_uri_level != NULL); + subpart = camel_multipart_get_part (mp, i); - emf->pending_uri_level = emf->pending_uri_level->parent; + g_string_append_printf(part_id, ".mixed.%d", i); + em_format_parse_part (emf, subpart, part_id, info, cancellable); + g_string_truncate (part_id, len); + } } -/** - * em_format_find_visible_puri: - * @emf: - * @uri: - * - * Search for a PURI based on the visibility defined by :push_level() - * and :pull_level(). - * - * Return value: - **/ -EMFormatPURI * -em_format_find_visible_puri (EMFormat *emf, - const gchar *uri) +static void +emf_parse_multipart_signed (EMFormat *emf, + CamelMimePart *part, + GString *part_id, + EMFormatParserInfo *info, + GCancellable *cancellable) { - GNode *node; - - g_return_val_if_fail (EM_IS_FORMAT (emf), NULL); - g_return_val_if_fail (uri != NULL, NULL); + CamelMimePart *cpart; + CamelMultipartSigned *mps; + CamelCipherContext *cipher = NULL; + guint32 validity_type; - node = emf->pending_uri_level; + if (g_cancellable_is_cancelled (cancellable)) + return; - while (node != NULL) { - GQueue *queue = node->data; - GList *link; + mps = (CamelMultipartSigned *) camel_medium_get_content ((CamelMedium *) part); + if (!CAMEL_IS_MULTIPART_SIGNED (mps) + || (cpart = camel_multipart_get_part ((CamelMultipart *) mps, + CAMEL_MULTIPART_SIGNED_CONTENT)) == NULL) { + em_format_format_error ( + emf, _("Could not parse MIME message. " + "Displaying as source.")); + emf_parse_source (emf, part, part_id, info, cancellable); + return; + } - link = g_queue_peek_head_link (queue); + /* FIXME: Should be done via a plugin interface */ + /* FIXME: duplicated in em-format-html-display.c */ + if (mps->protocol) { +#ifdef ENABLE_SMIME + if (g_ascii_strcasecmp("application/x-pkcs7-signature", mps->protocol) == 0 + || g_ascii_strcasecmp("application/pkcs7-signature", mps->protocol) == 0) { + cipher = camel_smime_context_new (emf->priv->session); + validity_type = EM_FORMAT_VALIDITY_FOUND_SMIME; + } else +#endif + if (g_ascii_strcasecmp("application/pgp-signature", mps->protocol) == 0) { + cipher = camel_gpg_context_new (emf->priv->session); + validity_type = EM_FORMAT_VALIDITY_FOUND_PGP; + } + } - while (link != NULL) { - EMFormatPURI *pw = link->data; + if (cipher == NULL) { + em_format_format_error(emf, _("Unsupported signature format")); + emf_parse_multipart_mixed (emf, part, part_id, info, cancellable); + } else { + CamelCipherValidity *valid; + GError *local_error = NULL; - if (g_strcmp0 (pw->uri, uri) == 0) - return pw; + valid = camel_cipher_context_verify_sync ( + cipher, part, cancellable, &local_error); + if (valid == NULL) { + em_format_format_error ( + emf, local_error->message ? + _("Error verifying signature") : + _("Unknown error verifying signature")); + if (local_error->message != NULL) + em_format_format_error ( + emf, "%s", + local_error->message); + g_clear_error (&local_error); + emf_parse_multipart_mixed (emf, part, part_id,info, cancellable); + } else { + gint i, nparts, len = part_id->len; + gboolean secured; + + EMFormatParserInfo signinfo = { + info->handler, + info->validity_type | validity_type | EM_FORMAT_VALIDITY_FOUND_SIGNED, + }; + + if (info->validity) + camel_cipher_validity_envelope (valid, info->validity); + signinfo.validity = valid; + + nparts = camel_multipart_get_number (CAMEL_MULTIPART (mps)); + secured = FALSE; + for (i = 0; i < nparts; i++) { + CamelMimePart *subpart; + subpart = camel_multipart_get_part (CAMEL_MULTIPART (mps), i); + + g_string_append_printf(part_id, ".signed.%d", i); + em_format_parse_part (emf, subpart, part_id, &signinfo, cancellable); + g_string_truncate (part_id, len); + + if (!secured) + secured = is_secured (subpart); + } - if (g_strcmp0 (pw->cid, uri) == 0) - return pw; + /* Add a widget with details about the encryption, but only when + * the encrypted isn't itself secured, in that case it has created + * the button itself */ + if (!secured) { + g_string_append (part_id, ".signed.button"); + em_format_parse_part_as (emf, part, part_id, &signinfo, + "x-evolution/message/x-secure-button", cancellable); + g_string_truncate (part_id, len); + } - link = g_list_next (link); + camel_cipher_validity_free (valid); } - - node = node->parent; } - return NULL; -} - -/** - * em_format_find_puri: - * @emf: - * @uri: - * - * Search for a PURI based on a uri. Both the content-id - * and content-location are checked. - * - * Return value: - **/ -EMFormatPURI * -em_format_find_puri (EMFormat *emf, - const gchar *uri) -{ - g_return_val_if_fail (EM_IS_FORMAT (emf), NULL); - g_return_val_if_fail (uri != NULL, NULL); - - g_return_val_if_fail (emf->pending_uri_table != NULL, NULL); - - return g_hash_table_lookup (emf->pending_uri_table, uri); + g_object_unref (cipher); } -/** - * em_format_clear_puri_tree: - * @emf: - * - * For use by implementors to clear out the message structure - * data. - **/ -void -em_format_clear_puri_tree (EMFormat *emf) +/* RFC 2046 */ +static void +emf_parse_multipart_digest (EMFormat *emf, + CamelMimePart *part, + GString *part_id, + EMFormatParserInfo *info, + GCancellable *cancellable) { - if (emf->pending_uri_table == NULL) - emf->pending_uri_table = - g_hash_table_new (g_str_hash, g_str_equal); + CamelMultipart *mp; + gint i, nparts, len; - else { - g_hash_table_remove_all (emf->pending_uri_table); + if (g_cancellable_is_cancelled (cancellable)) + return; - g_node_traverse ( - emf->pending_uri_tree, - G_IN_ORDER, G_TRAVERSE_ALL, -1, - (GNodeTraverseFunc) emf_clear_puri_node, NULL); - g_node_destroy (emf->pending_uri_tree); + mp = (CamelMultipart *) camel_medium_get_content ((CamelMedium *) part); - emf->pending_uri_tree = NULL; - emf->pending_uri_level = NULL; + if (!CAMEL_IS_MULTIPART (mp)) { + emf_parse_source (emf, part, part_id, info, cancellable); + return; } - em_format_push_level (emf); -} + len = part_id->len; + nparts = camel_multipart_get_number (mp); + for (i = 0; i < nparts; i++) { + CamelMimePart *subpart; + CamelContentType *ct; + gchar *cts; + const EMFormatHandler *handler; -/* use mime_type == NULL to force showing as application/octet-stream */ -void -em_format_part_as (EMFormat *emf, - CamelStream *stream, - CamelMimePart *part, - const gchar *mime_type, - GCancellable *cancellable) -{ - const EMFormatHandler *handle = NULL; - const gchar *snoop_save = emf->snoop_mime_type, *tmp; - CamelURL *base_save = emf->base, *base = NULL; - gchar *basestr = NULL; + subpart = camel_multipart_get_part (mp, i); - d(printf("format_part_as()\n")); + if (!subpart) + continue; - emf->snoop_mime_type = NULL; + g_string_append_printf(part_id, ".digest.%d", i); - /* RFC 2110, we keep track of content-base, and absolute content-location headers - * This is actually only required for html, but, *shrug * */ - tmp = camel_medium_get_header((CamelMedium *)part, "Content-Base"); - if (tmp == NULL) { - tmp = camel_mime_part_get_content_location (part); - if (tmp && strchr (tmp, ':') == NULL) - tmp = NULL; - } else { - tmp = basestr = camel_header_location_decode (tmp); - } - d(printf("content-base is '%s'\n", tmp?tmp:"")); - if (tmp - && (base = camel_url_new (tmp, NULL))) { - emf->base = base; - d(printf("Setting content base '%s'\n", tmp)); - } - g_free (basestr); - - if (mime_type != NULL) { - gboolean is_fallback = FALSE; - if (g_ascii_strcasecmp(mime_type, "application/octet-stream") == 0) { - emf->snoop_mime_type = mime_type = em_format_snoop_type (part); - if (mime_type == NULL) - mime_type = "application/octet-stream"; + ct = camel_mime_part_get_content_type (subpart); + /* According to RFC this shouldn't happen, but who knows... */ + if (ct && !camel_content_type_is (ct, "message", "rfc822")) { + cts = camel_content_type_simple (ct); + em_format_parse_part_as (emf, part, part_id, info, cts, cancellable); + g_free (cts); + g_string_truncate (part_id, len); + continue; } - handle = em_format_find_handler (emf, mime_type); - if (handle == NULL) { - handle = em_format_fallback_handler (emf, mime_type); - is_fallback = TRUE; - } + handler = em_format_find_handler (emf, "message/rfc822"); + if (handler && handler->parse_func) + handler->parse_func (emf, subpart, part_id, info, cancellable); - if (handle != NULL - && !em_format_is_attachment (emf, part)) { - d(printf("running handler for type '%s'\n", mime_type)); - handle->handler ( - emf, stream, part, handle, - cancellable, is_fallback); - goto finish; - } - d(printf("this type is an attachment? '%s'\n", mime_type)); - } else { - mime_type = "application/octet-stream"; + g_string_truncate (part_id, len); } +} - EM_FORMAT_GET_CLASS (emf)->format_attachment ( - emf, stream, part, mime_type, handle, cancellable); +/* RFC 2387 */ +static void +emf_parse_multipart_related (EMFormat *emf, + CamelMimePart *part, + GString *part_id, + EMFormatParserInfo *info, + GCancellable *cancellable) +{ + CamelMultipart *mp; + CamelMimePart *body_part, *display_part = NULL; + gint i, nparts, partidlen, displayid = 0; + + if (g_cancellable_is_cancelled (cancellable)) + return; + + mp = (CamelMultipart *) camel_medium_get_content ((CamelMedium *) part); + + if (!CAMEL_IS_MULTIPART (mp)) { + emf_parse_source (emf, part, part_id, info, cancellable); + return; + } + + display_part = get_related_display_part (part, &displayid); + + if (display_part == NULL) { + emf_parse_multipart_mixed ( + emf, part, part_id, info, cancellable); + return; + } -finish: - emf->base = base_save; - emf->snoop_mime_type = snoop_save; + /* The to-be-displayed part goes first */ + partidlen = part_id->len; + g_string_append_printf(part_id, ".related.%d", displayid); + em_format_parse_part (emf, display_part, part_id, info, cancellable); + g_string_truncate (part_id, partidlen); - if (base) - camel_url_free (base); + /* Process the related parts */ + nparts = camel_multipart_get_number (mp); + for (i = 0; i < nparts; i++) { + body_part = camel_multipart_get_part (mp, i); + if (body_part != display_part) { + g_string_append_printf(part_id, ".related.%d", i); + em_format_parse_part (emf, body_part, part_id, info, cancellable); + g_string_truncate (part_id, partidlen); + } + } } -void -em_format_part (EMFormat *emf, - CamelStream *stream, - CamelMimePart *mime_part, - GCancellable *cancellable) +static void +emf_parse_message_deliverystatus (EMFormat *emf, + CamelMimePart *part, + GString *part_id, + EMFormatParserInfo *info, + GCancellable *cancellable) { - gchar *mime_type; - CamelDataWrapper *dw; + EMFormatPURI *puri; + gint len; - dw = camel_medium_get_content (CAMEL_MEDIUM (mime_part)); - mime_type = camel_data_wrapper_get_mime_type (dw); - if (mime_type != NULL) { - camel_strdown (mime_type); - em_format_part_as ( - emf, stream, mime_part, mime_type, cancellable); - g_free (mime_type); - } else - em_format_part_as ( - emf, stream, mime_part, "text/plain", cancellable); + if (g_cancellable_is_cancelled (cancellable)) + return; + + len = part_id->len; + g_string_append (part_id, ".deliverystatus"); + + puri = em_format_puri_new (emf, sizeof (EMFormatPURI), part, part_id->str); + puri->write_func = emf_write_text; + puri->mime_type = g_strdup ("text/html"); + puri->validity = info->validity ? camel_cipher_validity_clone (info->validity) : NULL; + puri->validity_type = info->validity_type; + + g_string_truncate (part_id, len); + + em_format_add_puri (emf, puri); } -/** - * em_format_format_clone: - * @emf: an #EMFormat - * @folder: a #CamelFolder or %NULL - * @uid: Message UID or %NULL - * @msg: a #CamelMimeMessage or %NULL - * @emfsource: Used as a basis for user-altered layout, e.g. inline viewed - * attachments. - * @cancellable: a #GCancellable, or %NULL - * - * Format a message @msg. If @emfsource is non NULL, then the status of - * inlined expansion and so forth is copied direction from @emfsource. - * - * By passing the same value for @emf and @emfsource, you can perform - * a display refresh, or it can be used to generate an identical layout, - * e.g. to print what the user has shown inline. - **/ -void -em_format_format_clone (EMFormat *emf, - CamelFolder *folder, - const gchar *uid, - CamelMimeMessage *message, - EMFormat *source, - GCancellable *cancellable) +static void +emf_parse_inlinepgp_signed (EMFormat *emf, + CamelMimePart *ipart, + GString *part_id, + EMFormatParserInfo *info, + GCancellable *cancellable) { - EMFormatClass *class; + CamelStream *filtered_stream; + CamelMimeFilterPgp *pgp_filter; + CamelContentType *content_type; + CamelCipherContext *cipher; + CamelCipherValidity *valid; + CamelDataWrapper *dw; + CamelMimePart *opart; + CamelStream *ostream; + gchar *type; + gint len; + GError *local_error = NULL; + EMFormatParserInfo signinfo; + GByteArray *ba; - g_return_if_fail (EM_IS_FORMAT (emf)); - g_return_if_fail (folder == NULL || CAMEL_IS_FOLDER (folder)); - g_return_if_fail (message == NULL || CAMEL_IS_MIME_MESSAGE (message)); - g_return_if_fail (source == NULL || EM_IS_FORMAT (source)); + if (g_cancellable_is_cancelled (cancellable)) + return; - class = EM_FORMAT_GET_CLASS (emf); - g_return_if_fail (class->format_clone != NULL); + if (!ipart) { + em_format_format_error(emf, _("Unknown error verifying signature")); + return; + } - class->format_clone (emf, folder, uid, message, source, cancellable); + cipher = camel_gpg_context_new (emf->priv->session); + /* Verify the signature of the message */ + valid = camel_cipher_context_verify_sync ( + cipher, ipart, cancellable, &local_error); + if (!valid) { + em_format_format_error ( + emf, local_error->message ? + _("Error verifying signature") : + _("Unknown error verifying signature")); + if (local_error->message) + em_format_format_error ( + emf, "%s", local_error->message); + emf_parse_source (emf, ipart, part_id, info, cancellable); + /* XXX I think this will loop: + * em_format_part_as(emf, stream, part, "text/plain"); */ + g_clear_error (&local_error); + g_object_unref (cipher); + return; + } + + /* Setup output stream */ + ostream = camel_stream_mem_new (); + filtered_stream = camel_stream_filter_new (ostream); + + /* Add PGP header / footer filter */ + pgp_filter = (CamelMimeFilterPgp *) camel_mime_filter_pgp_new (); + camel_stream_filter_add ( + CAMEL_STREAM_FILTER (filtered_stream), + CAMEL_MIME_FILTER (pgp_filter)); + g_object_unref (pgp_filter); + + /* Pass through the filters that have been setup */ + dw = camel_medium_get_content ((CamelMedium *) ipart); + camel_data_wrapper_decode_to_stream_sync ( + dw, (CamelStream *) filtered_stream, cancellable, NULL); + camel_stream_flush ((CamelStream *) filtered_stream, cancellable, NULL); + g_object_unref (filtered_stream); + + /* Create a new text/plain MIME part containing the signed + * content preserving the original part's Content-Type params. */ + content_type = camel_mime_part_get_content_type (ipart); + type = camel_content_type_format (content_type); + content_type = camel_content_type_decode (type); + g_free (type); + + g_free (content_type->type); + content_type->type = g_strdup ("text"); + g_free (content_type->subtype); + content_type->subtype = g_strdup ("plain"); + type = camel_content_type_format (content_type); + camel_content_type_unref (content_type); + + ba = camel_stream_mem_get_byte_array ((CamelStreamMem *) ostream); + opart = camel_mime_part_new (); + camel_mime_part_set_content (opart, (gchar *) ba->data, ba->len, type); + g_free (type); + + if (info->validity) + camel_cipher_validity_envelope (valid, info->validity); + + /* Pass it off to the real formatter */ + len = part_id->len; + g_string_append (part_id, ".inlinepgp_signed"); + signinfo.handler = info->handler; + signinfo.validity_type = info->validity_type | EM_FORMAT_VALIDITY_FOUND_SIGNED | EM_FORMAT_VALIDITY_FOUND_PGP; + signinfo.validity = valid; + em_format_parse_part (emf, opart, part_id, &signinfo, cancellable); + g_string_truncate (part_id, len); + + /* Add a widget with details about the encryption, but only when + * the encrypted isn't itself secured, in that case it has created + * the button itself */ + if (!is_secured (opart)) { + g_string_append (part_id, ".inlinepgp_signed.button"); + em_format_parse_part_as (emf, opart, part_id, &signinfo, + "x-evolution/message/x-secure-button", cancellable); + g_string_truncate (part_id, len); + } + + /* Clean Up */ + camel_cipher_validity_free (valid); + g_object_unref (dw); + g_object_unref (opart); + g_object_unref (ostream); + g_object_unref (cipher); } -void -em_format_format (EMFormat *emf, - CamelFolder *folder, - const gchar *uid, - CamelMimeMessage *message, - GCancellable *cancellable) +static void +emf_parse_inlinepgp_encrypted (EMFormat *emf, + CamelMimePart *ipart, + GString *part_id, + EMFormatParserInfo *info, + GCancellable *cancellable) { - /* em_format_format_clone() will check the arguments. */ - em_format_format_clone (emf, folder, uid, message, NULL, cancellable); + CamelCipherContext *cipher; + CamelCipherValidity *valid; + CamelMimePart *opart; + CamelDataWrapper *dw; + gchar *mime_type; + gint len; + GError *local_error = NULL; + EMFormatParserInfo encinfo; + + if (g_cancellable_is_cancelled (cancellable)) + return; + + cipher = camel_gpg_context_new (emf->priv->session); + opart = camel_mime_part_new (); + + /* Decrypt the message */ + valid = camel_cipher_context_decrypt_sync ( + cipher, ipart, opart, cancellable, &local_error); + + if (!valid) { + em_format_format_error ( + emf, _("Could not parse PGP message: ")); + if (local_error->message != NULL) + em_format_format_error ( + emf, "%s", local_error->message); + else + em_format_format_error ( + emf, _("Unknown error")); + emf_parse_source (emf, ipart, part_id, info, cancellable); + /* XXX I think this will loop: + * em_format_part_as(emf, stream, part, "text/plain"); */ + + g_clear_error (&local_error); + g_object_unref (cipher); + g_object_unref (opart); + return; + } + + dw = camel_medium_get_content ((CamelMedium *) opart); + mime_type = camel_data_wrapper_get_mime_type (dw); + + /* this ensures to show the 'opart' as inlined, if possible */ + if (mime_type && g_ascii_strcasecmp (mime_type, "application/octet-stream") == 0) { + const gchar *snoop = em_format_snoop_type (opart); + + if (snoop) + camel_data_wrapper_set_mime_type (dw, snoop); + } + + preserve_charset_in_content_type (ipart, opart); + g_free (mime_type); + + if (info->validity) + camel_cipher_validity_envelope (valid, info->validity); + + /* Pass it off to the real formatter */ + len = part_id->len; + g_string_append (part_id, ".inlinepgp_encrypted"); + encinfo.handler = info->handler; + encinfo.validity_type = info->validity_type | EM_FORMAT_VALIDITY_FOUND_ENCRYPTED | EM_FORMAT_VALIDITY_FOUND_PGP; + encinfo.validity = valid; + em_format_parse_part (emf, opart, part_id, &encinfo, cancellable); + g_string_truncate (part_id, len); + + /* Add a widget with details about the encryption, but only when + * the encrypted isn't itself secured, in that case it has created + * the button itself */ + if (!is_secured (opart)) { + g_string_append (part_id, ".inlinepgp_encrypted.button"); + em_format_parse_part_as (emf, opart, part_id, &encinfo, + "x-evolution/message/x-secure-button", cancellable); + g_string_truncate (part_id, len); + } + + /* Clean Up */ + camel_cipher_validity_free (valid); + g_object_unref (opart); + g_object_unref (cipher); } -static gboolean -format_redraw_idle_cb (EMFormat *emf) +static void +emf_parse_message (EMFormat *emf, + CamelMimePart *part, + GString *part_id, + EMFormatParserInfo *info, + GCancellable *cancellable) { - emf->priv->redraw_idle_id = 0; + /* Headers */ + info->force_handler = TRUE; + em_format_parse_part_as (emf, part, part_id, info, + "x-evolution/message/headers", cancellable); - /* FIXME Not passing a GCancellable here. */ - em_format_format_clone ( - emf, emf->folder, emf->uid, emf->message, emf, NULL); + /* Anything that comes between headers and message body */ + info->force_handler = TRUE; + em_format_parse_part_as (emf, part, part_id, info, + "x-evolution/message/post-headers", cancellable); - return FALSE; + /* Begin parsing the message */ + info->force_handler = FALSE; + em_format_parse_part (emf, part, part_id, info, cancellable); } -void -em_format_queue_redraw (EMFormat *emf) +static void +emf_parse_headers (EMFormat *emf, + CamelMimePart *part, + GString *part_id, + EMFormatParserInfo *info, + GCancellable *cancellable) { - g_return_if_fail (EM_IS_FORMAT (emf)); + EMFormatPURI *puri; + gint len; + + len = part_id->len; + g_string_append (part_id, ".headers"); + + puri = em_format_puri_new (emf, sizeof (EMFormatPURI), part, part_id->str); + puri->write_func = info->handler->write_func; + puri->mime_type = g_strdup ("text/html"); + em_format_add_puri (emf, puri); - if (emf->priv->redraw_idle_id == 0) - emf->priv->redraw_idle_id = g_idle_add ( - (GSourceFunc) format_redraw_idle_cb, emf); + g_string_truncate (part_id, len); } -/** - * em_format_set_mode: - * @emf: - * @type: - * - * Set display mode, EM_FORMAT_MODE_SOURCE, EM_FORMAT_MODE_ALLHEADERS, - * or EM_FORMAT_MODE_NORMAL. - **/ -void -em_format_set_mode (EMFormat *emf, - EMFormatMode mode) +static void +emf_parse_post_headers (EMFormat *emf, + CamelMimePart *part, + GString *part_id, + EMFormatParserInfo *info, + GCancellable *cancellable) { - g_return_if_fail (EM_IS_FORMAT (emf)); + /* Add attachment bar */ + info->force_handler = TRUE; + em_format_parse_part_as (emf, part, part_id, info, + "x-evolution/message/attachment-bar", cancellable); +} + +static void +emf_parse_source (EMFormat *emf, + CamelMimePart *part, + GString *part_id, + EMFormatParserInfo *info, + GCancellable *cancellable) +{ + EMFormatPURI *puri; + gint len; - if (emf->mode == mode) + if (g_cancellable_is_cancelled (cancellable)) return; - emf->mode = mode; + len = part_id->len; + g_string_append (part_id, ".source"); + + puri = em_format_puri_new (emf, sizeof (EMFormatPURI), part, part_id->str); + puri->write_func = info->handler->write_func; + puri->mime_type = g_strdup ("text/html"); + g_string_truncate (part_id, len); - /* force redraw if type changed afterwards */ - if (emf->message != NULL) - em_format_queue_redraw (emf); + em_format_add_puri (emf, puri); } -/** - * em_format_set_charset: - * @emf: - * @charset: - * - * set override charset on formatter. message will be redisplayed if - * required. - **/ +/**************************************************************************/ + void -em_format_set_charset (EMFormat *emf, - const gchar *charset) +em_format_empty_writer (EMFormat *emf, + EMFormatPURI *puri, + CamelStream *stream, + EMFormatWriterInfo *info, + GCancellable *cancellable) { - if ((emf->charset && charset && g_ascii_strcasecmp (emf->charset, charset) == 0) - || (emf->charset == NULL && charset == NULL) - || (emf->charset == charset)) - return; - - g_free (emf->charset); - emf->charset = g_strdup (charset); + /* DO NOTHING */ +} - if (emf->message) - em_format_queue_redraw (emf); +static void +emf_write_error (EMFormat * emf, + EMFormatPURI * puri, + CamelStream * stream, + EMFormatWriterInfo * info, + GCancellable * cancellable) +{ + camel_data_wrapper_decode_to_stream_sync ((CamelDataWrapper *) puri->part, + stream, cancellable, NULL); } -/** - * em_format_set_default_charset: - * @emf: - * @charset: - * - * Set the fallback, default system charset to use when no other charsets - * are present. Message will be redisplayed if required (and sometimes - * redisplayed when it isn't). - **/ -void -em_format_set_default_charset (EMFormat *emf, - const gchar *charset) +static void +emf_write_text (EMFormat *emf, + EMFormatPURI *puri, + CamelStream *stream, + EMFormatWriterInfo *info, + GCancellable *cancellable) { - if ((emf->default_charset && charset && - g_ascii_strcasecmp (emf->default_charset, charset) == 0) - || (emf->default_charset == NULL && charset == NULL) - || (emf->default_charset == charset)) + CamelContentType *ct; + + ct = camel_mime_part_get_content_type (puri->part); + if (!camel_content_type_is (ct, "text", "plain")) { + camel_stream_write_string (stream, _("Cannot proccess non-text mime/part"), + cancellable, NULL); return; + } - g_free (emf->default_charset); - emf->default_charset = g_strdup (charset); + camel_data_wrapper_decode_to_stream_sync ((CamelDataWrapper *) puri->part, + stream, cancellable, NULL); +} - if (emf->message && emf->charset == NULL) - em_format_queue_redraw (emf); +static void +emf_write_source (EMFormat *emf, + EMFormatPURI *puri, + CamelStream *stream, + EMFormatWriterInfo *info, + GCancellable *cancellable) +{ + GByteArray *ba; + gchar *data; + + g_return_if_fail (EM_IS_FORMAT (emf)); + + ba = camel_data_wrapper_get_byte_array ((CamelDataWrapper *) puri->part); + + data = g_strndup ((gchar *) ba->data, ba->len); + camel_stream_write_string (stream, data, cancellable, NULL); + g_free (data); } -/** - * em_format_clear_headers: - * @emf: - * - * Clear the list of headers to be displayed. This will force all headers to - * be shown. - **/ -void -em_format_clear_headers (EMFormat *emf) +/**************************************************************************/ + +static gboolean +emf_is_inline (EMFormat *emf, + const gchar *part_id, + CamelMimePart *mime_part, + const EMFormatHandler *handle) { - EMFormatHeader *eh; + //EMFormatCache *emfc; + const gchar *disposition; + + if (handle == NULL) + return FALSE; + + /* Some types need to override the disposition. + * e.g. application/x-pkcs7-mime */ + if (handle->flags & EM_FORMAT_HANDLER_INLINE_DISPOSITION) + return TRUE; + + disposition = camel_mime_part_get_disposition (mime_part); + if (disposition != NULL) + return g_ascii_strcasecmp (disposition, "inline") == 0; - while ((eh = g_queue_pop_head (&emf->header_list)) != NULL) - g_free (eh); + /* Otherwise, use the default for this handler type. */ + return (handle->flags & EM_FORMAT_HANDLER_INLINE) != 0; } +/**************************************************************************/ + +static EMFormatHandler type_handlers[] = { +#ifdef ENABLE_SMIME + { (gchar *) "application/x-pkcs7-mime", emf_parse_application_xpkcs7mime, 0, EM_FORMAT_HANDLER_INLINE_DISPOSITION }, +#endif + { (gchar *) "application/mbox", emf_parse_application_mbox, 0, EM_FORMAT_HANDLER_INLINE | EM_FORMAT_HANDLER_COMPOUND_TYPE }, + { (gchar *) "multipart/alternative", emf_parse_multipart_alternative, }, + { (gchar *) "multipart/appledouble", emf_parse_multipart_appledouble, }, + { (gchar *) "multipart/encrypted", emf_parse_multipart_encrypted, }, + { (gchar *) "multipart/mixed", emf_parse_multipart_mixed, }, + { (gchar *) "multipart/signed", emf_parse_multipart_signed, }, + { (gchar *) "multipart/related", emf_parse_multipart_related, }, + { (gchar *) "multipart/digest", emf_parse_multipart_digest, 0, EM_FORMAT_HANDLER_COMPOUND_TYPE }, + { (gchar *) "multipart/*", emf_parse_multipart_mixed, 0, EM_FORMAT_HANDLER_COMPOUND_TYPE }, + { (gchar *) "message/deliverystatus", emf_parse_message_deliverystatus, 0, }, + + /* Ignore PGP signature part */ + { (gchar *) "application/pgp-signature", em_format_empty_parser, }, + + /* Insert brokenly-named parts here */ +#ifdef ENABLE_SMIME + { (gchar *) "application/pkcs7-mime", emf_parse_application_xpkcs7mime, 0, EM_FORMAT_HANDLER_INLINE_DISPOSITION }, +#endif + + /* internal types */ + { (gchar *) "application/x-inlinepgp-signed", emf_parse_inlinepgp_signed, }, + { (gchar *) "application/x-inlinepgp-encrypted", emf_parse_inlinepgp_encrypted, }, + { (gchar *) "x-evolution/message", emf_parse_message, 0, EM_FORMAT_HANDLER_COMPOUND_TYPE }, + { (gchar *) "x-evolution/message/headers", emf_parse_headers, }, + { (gchar *) "x-evolution/message/post-headers", emf_parse_post_headers, }, + { (gchar *) "x-evolution/message/source", emf_parse_source, emf_write_source }, +}; + /* note: also copied in em-mailer-prefs.c */ static const struct { const gchar *name; @@ -1071,1338 +1232,1085 @@ static const struct { { N_("Face"), 0 }, }; -/** - * em_format_default_headers: - * @emf: - * - * Set the headers to show to the default list. - * - * From, Reply-To, To, Cc, Bcc, Subject and Date. - **/ -void -em_format_default_headers (EMFormat *emf) +static void +em_format_get_property (GObject *object, + guint property_id, + GValue *value, + GParamSpec *pspec) { - gint ii; + EMFormat *emf = EM_FORMAT (object); - em_format_clear_headers (emf); + switch (property_id) { + case PROP_CHARSET: + g_value_set_string ( + value, em_format_get_charset (emf)); + return; + case PROP_DEFAULT_CHARSET: + g_value_set_string ( + value, em_format_get_default_charset (emf)); + return; + case PROP_COMPOSER: + g_value_set_boolean ( + value, em_format_get_composer (emf)); + return; + case PROP_BASE_URL: + g_value_set_object ( + value, em_format_get_base_url (emf)); + return; + } - for (ii = 0; ii < G_N_ELEMENTS (default_headers); ii++) - em_format_add_header ( - emf, default_headers[ii].name, - default_headers[ii].flags); + G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec); } -/** - * em_format_add_header: - * @emf: - * @name: The name of the header, as it will appear during output. - * @flags: EM_FORMAT_HEAD_* defines to control display attributes. - * - * Add a specific header to show. If any headers are set, they will - * be displayed in the order set by this function. Certain known - * headers included in this list will be shown using special - * formatting routines. - **/ -void -em_format_add_header (EMFormat *emf, - const gchar *name, - guint32 flags) +static void +em_format_set_property (GObject *object, + guint property_id, + const GValue *value, + GParamSpec *pspec) { - EMFormatHeader *h; + EMFormat *emf = EM_FORMAT (object); + + switch (property_id) { + case PROP_CHARSET: + em_format_set_charset (emf, + g_value_get_string (value)); + return; + case PROP_DEFAULT_CHARSET: + em_format_set_default_charset (emf, + g_value_get_string (value)); + return; + case PROP_COMPOSER: + em_format_set_composer (emf, + g_value_get_boolean (value)); + return; + case PROP_BASE_URL: + em_format_set_base_url (emf, + g_value_get_object (value)); + return; + } + + G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec); - h = g_malloc (sizeof (*h) + strlen (name)); - h->flags = flags; - strcpy (h->name, name); - g_queue_push_tail (&emf->header_list, h); } -/** - * em_format_is_attachment: - * @emf: - * @part: Part to check. - * - * Returns true if the part is an attachment. - * - * A part is not considered an attachment if it is a - * multipart, or a text part with no filename. It is used - * to determine if an attachment header should be displayed for - * the part. - * - * Content-Disposition is not checked. - * - * Return value: TRUE/FALSE - **/ -gint -em_format_is_attachment (EMFormat *emf, - CamelMimePart *part) +static void +em_format_finalize (GObject *object) { - /*CamelContentType *ct = camel_mime_part_get_content_type(part);*/ - CamelDataWrapper *dw = camel_medium_get_content ((CamelMedium *) part); + EMFormat *emf = EM_FORMAT (object); - if (!dw) - return 0; + if (emf->message_uid) { + g_free (emf->message_uid); + emf->message_uid = NULL; + } - /*printf("checking is attachment %s/%s\n", ct->type, ct->subtype);*/ - return !(camel_content_type_is (dw->mime_type, "multipart", "*") - || camel_content_type_is ( - dw->mime_type, "application", "x-pkcs7-mime") - || camel_content_type_is ( - dw->mime_type, "application", "pkcs7-mime") - || camel_content_type_is ( - dw->mime_type, "application", "x-inlinepgp-signed") - || camel_content_type_is ( - dw->mime_type, "application", "x-inlinepgp-encrypted") - || camel_content_type_is ( - dw->mime_type, "x-evolution", "evolution-rss-feed") - || camel_content_type_is (dw->mime_type, "text", "calendar") - || camel_content_type_is (dw->mime_type, "text", "x-calendar") - || (camel_content_type_is (dw->mime_type, "text", "*") - && camel_mime_part_get_filename (part) == NULL)); + if (emf->uri_base) { + g_free (emf->uri_base); + emf->uri_base = NULL; + } + + if (emf->message) { + g_object_unref (emf->message); + emf->message = NULL; + } + + if (emf->folder) { + g_object_unref (emf->folder); + emf->folder = NULL; + } + + if (emf->mail_part_table) { + /* This will destroy all the EMFormatPURI objects stored + * inside!!!! */ + g_hash_table_destroy (emf->mail_part_table); + emf->mail_part_table = NULL; + } + + if (emf->mail_part_list) { + g_list_free (emf->mail_part_list); + emf->mail_part_list = NULL; + } + + if (emf->priv->base_url) { + camel_url_free (emf->priv->base_url); + emf->priv->base_url = NULL; + } + + if (emf->priv->session) { + g_object_unref (emf->priv->session); + emf->priv->session = NULL; + } + + if (emf->priv->charset) { + g_free (emf->priv->charset); + emf->priv->charset = NULL; + } + + em_format_clear_headers (emf); + + /* Chain up to parent's finalize() method */ + G_OBJECT_CLASS (parent_class)->finalize (object); } -/** - * em_format_is_inline: - * @emf: - * @part: - * @part_id: format->part_id part id of this part. - * @handle: handler for this part - * - * Returns true if the part should be displayed inline. Any part with - * a Content-Disposition of inline, or if the @handle has a default - * inline set, will be shown inline. - * - * :set_inline() called on the same part will override any calculated - * value. - * - * Return value: - **/ -gboolean -em_format_is_inline (EMFormat *emf, - const gchar *part_id, - CamelMimePart *mime_part, - const EMFormatHandler *handle) +static void +em_format_base_init (EMFormatClass *klass) { - EMFormatClass *class; + gint i; - g_return_val_if_fail (EM_IS_FORMAT (emf), FALSE); - g_return_val_if_fail (part_id != NULL, FALSE); - g_return_val_if_fail (CAMEL_IS_MIME_PART (mime_part), FALSE); + klass->type_handlers = g_hash_table_new (g_str_hash, g_str_equal); - class = EM_FORMAT_GET_CLASS (emf); - g_return_val_if_fail (class->is_inline != NULL, FALSE); + for (i = 0; i < G_N_ELEMENTS (type_handlers); i++) { + g_hash_table_insert (klass->type_handlers, + type_handlers[i].mime_type, + &type_handlers[i]); + } +} + +static void +em_format_class_init (EMFormatClass *klass) +{ + GObjectClass *object_class; + + parent_class = g_type_class_peek_parent (klass); + + g_type_class_add_private (klass, sizeof (EMFormatPrivate)); + + klass->is_inline = emf_is_inline; + + object_class = G_OBJECT_CLASS (klass); + object_class->finalize = em_format_finalize; + object_class->get_property = em_format_get_property; + object_class->set_property = em_format_set_property; + + g_object_class_install_property (object_class, + PROP_CHARSET, + g_param_spec_string ("charset", + NULL, + NULL, + NULL, + G_PARAM_READWRITE)); + + g_object_class_install_property (object_class, + PROP_DEFAULT_CHARSET, + g_param_spec_string ("default-charset", + NULL, + NULL, + NULL, + G_PARAM_READWRITE)); + + g_object_class_install_property (object_class, + PROP_COMPOSER, + g_param_spec_boolean ("composer", + NULL, + NULL, + FALSE, + G_PARAM_READWRITE)); + + g_object_class_install_property (object_class, + PROP_BASE_URL, + g_param_spec_pointer ("base-url", + NULL, + NULL, + G_PARAM_READWRITE)); + + signals[REDRAW_REQUESTED] = g_signal_new ( + "redraw-requested", + G_TYPE_FROM_CLASS (klass), + G_SIGNAL_RUN_LAST, + G_STRUCT_OFFSET (EMFormatClass, redraw_requested), + NULL, NULL, + g_cclosure_marshal_VOID__VOID, + G_TYPE_NONE,0); +} + +static void +mail_part_table_item_free (gpointer data) +{ + GList *iter = data; + EMFormatPURI *puri = iter->data; - return class->is_inline (emf, part_id, mime_part, handle); + em_format_puri_free (puri); } -/** - * em_format_set_inline: - * @emf: - * @part_id: id of part - * @state: - * - * Force the attachment @part to be expanded or hidden explictly to match - * @state. This is used only to record the change for a redraw or - * cloned layout render and does not force a redraw. - **/ -void -em_format_set_inline (EMFormat *emf, - const gchar *part_id, - gint state) +static void +em_format_init (EMFormat *emf) { - EMFormatCache *emfc; + EShell *shell; + EShellSettings *shell_settings; - g_return_if_fail (EM_IS_FORMAT (emf)); - g_return_if_fail (part_id != NULL); + emf->priv = G_TYPE_INSTANCE_GET_PRIVATE (emf, + EM_TYPE_FORMAT, EMFormatPrivate); - emfc = g_hash_table_lookup (emf->inline_table, part_id); - if (emfc == NULL) { - emfc = emf_insert_cache (emf, part_id); - } else if (emfc->state != INLINE_UNSET && (emfc->state & 1) == state) - return; + emf->message = NULL; + emf->folder = NULL; + emf->mail_part_list = NULL; + emf->mail_part_table = g_hash_table_new_full (g_str_hash, g_str_equal, + NULL, (GDestroyNotify) mail_part_table_item_free); + /* No need to free the key, because it's owned and free'd by the PURI */ - emfc->state = state ? INLINE_ON : INLINE_OFF; + shell = e_shell_get_default (); + shell_settings = e_shell_get_shell_settings (shell); - if (emf->message) - em_format_queue_redraw (emf); -} + emf->priv->last_error = 0; -void -em_format_format_attachment (EMFormat *emf, - CamelStream *stream, - CamelMimePart *mime_part, - const gchar *mime_type, - const EMFormatHandler *info, - GCancellable *cancellable) -{ - EMFormatClass *class; + emf->priv->session = e_shell_settings_get_pointer (shell_settings, "mail-session"); + g_return_if_fail (emf->priv->session); - g_return_if_fail (EM_IS_FORMAT (emf)); - g_return_if_fail (CAMEL_IS_STREAM (stream)); - g_return_if_fail (CAMEL_IS_MIME_PART (mime_part)); - g_return_if_fail (mime_type != NULL); - g_return_if_fail (info != NULL); + g_object_ref (emf->priv->session); + + em_format_default_headers (emf); +} - class = EM_FORMAT_GET_CLASS (emf); - g_return_if_fail (class->format_attachment != NULL); +EMFormat * +em_format_new (void) +{ + EMFormat *emf = g_object_new (EM_TYPE_FORMAT, NULL); - class->format_attachment ( - emf, stream, mime_part, mime_type, info, cancellable); + return emf; } -void -em_format_format_error (EMFormat *emf, - CamelStream *stream, - const gchar *format, - ...) +GType +em_format_get_type (void) { - EMFormatClass *class; - gchar *errmsg; - va_list ap; + static GType type = 0; - g_return_if_fail (EM_IS_FORMAT (emf)); - g_return_if_fail (CAMEL_IS_STREAM (stream)); - g_return_if_fail (format != NULL); + if (G_UNLIKELY (type == 0)) { + static const GTypeInfo type_info = { + sizeof (EMFormatClass), + (GBaseInitFunc) em_format_base_init, + (GBaseFinalizeFunc) NULL, + (GClassInitFunc) em_format_class_init, + (GClassFinalizeFunc) NULL, + NULL, /* class_data */ + sizeof (EMFormat), + 0, /* n_preallocs */ + (GInstanceInitFunc) em_format_init, + NULL /* value_table */ + }; - class = EM_FORMAT_GET_CLASS (emf); - g_return_if_fail (class->format_error != NULL); + type = g_type_register_static ( + G_TYPE_OBJECT, "EMFormat", &type_info, 0); + } - va_start (ap, format); - errmsg = g_strdup_vprintf (format, ap); - class->format_error (emf, stream, errmsg); - g_free (errmsg); - va_end (ap); + return type; } void -em_format_format_secure (EMFormat *emf, - CamelStream *stream, - CamelMimePart *mime_part, - CamelCipherValidity *valid, - GCancellable *cancellable) +em_format_set_charset (EMFormat *emf, + const gchar *charset) { - EMFormatClass *class; - g_return_if_fail (EM_IS_FORMAT (emf)); - g_return_if_fail (CAMEL_IS_STREAM (stream)); - g_return_if_fail (CAMEL_IS_MIME_PART (mime_part)); - g_return_if_fail (valid != NULL); - class = EM_FORMAT_GET_CLASS (emf); - g_return_if_fail (class->format_secure != NULL); + if (emf->priv->charset) + g_free (emf->priv->charset); - class->format_secure (emf, stream, mime_part, valid, cancellable); + emf->priv->charset = g_strdup (charset); - if (emf->valid_parent == NULL && emf->valid != NULL) { - camel_cipher_validity_free (emf->valid); - emf->valid = NULL; - } + g_object_notify (G_OBJECT (emf), "charset"); } -void -em_format_format_source (EMFormat *emf, - CamelStream *stream, - CamelMimePart *mime_part, - GCancellable *cancellable) +const gchar * +em_format_get_charset (EMFormat *emf) { - EMFormatClass *class; - - g_return_if_fail (EM_IS_FORMAT (emf)); - g_return_if_fail (CAMEL_IS_STREAM (stream)); - g_return_if_fail (CAMEL_IS_MIME_PART (mime_part)); - - class = EM_FORMAT_GET_CLASS (emf); - g_return_if_fail (class->format_source != NULL); + g_return_val_if_fail (EM_IS_FORMAT (emf), NULL); - class->format_source (emf, stream, mime_part, cancellable); + return emf->priv->charset; } -gboolean -em_format_busy (EMFormat *emf) +void +em_format_set_default_charset (EMFormat *emf, + const gchar *charset) { - EMFormatClass *class; + g_return_if_fail (EM_IS_FORMAT (emf)); - g_return_val_if_fail (EM_IS_FORMAT (emf), FALSE); + if (emf->priv->default_charset) + g_free (emf->priv->default_charset); - class = EM_FORMAT_GET_CLASS (emf); - g_return_val_if_fail (class->busy != NULL, FALSE); + emf->priv->default_charset = g_strdup (charset); - return class->busy (emf); + g_object_notify (G_OBJECT (emf), "default-charset"); } -/* should this be virtual? */ -void -em_format_format_content (EMFormat *emf, - CamelStream *stream, - CamelMimePart *part, - GCancellable *cancellable) +const gchar * +em_format_get_default_charset (EMFormat *emf) { - CamelDataWrapper *dw = camel_medium_get_content ((CamelMedium *) part); + g_return_val_if_fail (EM_IS_FORMAT (emf), NULL); - if (camel_content_type_is (dw->mime_type, "text", "*")) - em_format_format_text ( - emf, stream, (CamelDataWrapper *) part, cancellable); - else - camel_data_wrapper_decode_to_stream_sync ( - dw, stream, cancellable, NULL); + return emf->priv->default_charset; } -/** - * em_format_format_content: - * @emf: - * @stream: Where to write the converted text - * @part: Part whose container is to be formatted - * @cancellable: optional #GCancellable object, or %NULL - * - * Decode/output a part's content to @stream. - **/ void -em_format_format_text (EMFormat *emf, - CamelStream *stream, - CamelDataWrapper *dw, - GCancellable *cancellable) +em_format_set_composer (EMFormat *emf, + gboolean composer) { - CamelStream *filter_stream; - CamelMimeFilter *filter; - const gchar *charset = NULL; - CamelMimeFilterWindows *windows = NULL; - CamelStream *mem_stream = NULL; - gsize size; - gsize max; - GSettings *settings; + g_return_if_fail (EM_IS_FORMAT (emf)); - if (emf->charset) { - charset = emf->charset; - } else if (dw->mime_type - && (charset = camel_content_type_param (dw->mime_type, "charset")) - && g_ascii_strncasecmp(charset, "iso-8859-", 9) == 0) { - CamelStream *null; + if (emf->priv->composer && composer) + return; - /* Since a few Windows mailers like to claim they sent - * out iso-8859-# encoded text when they really sent - * out windows-cp125#, do some simple sanity checking - * before we move on... */ + emf->priv->composer = composer; - null = camel_stream_null_new (); - filter_stream = camel_stream_filter_new (null); - g_object_unref (null); + g_object_notify (G_OBJECT (emf), "composer"); +} - windows = (CamelMimeFilterWindows *) camel_mime_filter_windows_new (charset); - camel_stream_filter_add ( - CAMEL_STREAM_FILTER (filter_stream), - CAMEL_MIME_FILTER (windows)); +gboolean +em_format_get_composer (EMFormat *emf) +{ + g_return_val_if_fail (EM_IS_FORMAT (emf), FALSE); - camel_data_wrapper_decode_to_stream_sync ( - dw, (CamelStream *) filter_stream, cancellable, NULL); - camel_stream_flush ((CamelStream *) filter_stream, cancellable, NULL); - g_object_unref (filter_stream); + return emf->priv->composer; +} - charset = camel_mime_filter_windows_real_charset (windows); - } else if (charset == NULL) { - charset = emf->default_charset; - } +void +em_format_set_base_url (EMFormat *emf, + CamelURL *url) +{ + g_return_if_fail (EM_IS_FORMAT (emf)); + g_return_if_fail (url); - mem_stream = (CamelStream *) camel_stream_mem_new (); - filter_stream = camel_stream_filter_new (mem_stream); + if (emf->priv->base_url) + camel_url_free (emf->priv->base_url); - if ((filter = camel_mime_filter_charset_new (charset, "UTF-8"))) { - camel_stream_filter_add ( - CAMEL_STREAM_FILTER (filter_stream), - CAMEL_MIME_FILTER (filter)); - g_object_unref (filter); - } + emf->priv->base_url = camel_url_copy (url); - max = -1; + g_object_notify (G_OBJECT (emf), "base-url"); +} - settings = g_settings_new ("org.gnome.evolution.mail"); - if (g_settings_get_boolean (settings, "force-message-limit")) { - max = g_settings_get_int (settings, "message-text-part-limit"); - if (max == 0) - max = -1; - } - g_object_unref (settings); +void +em_format_set_base_url_string (EMFormat *emf, + const gchar *url_string) +{ + g_return_if_fail (EM_IS_FORMAT (emf)); + g_return_if_fail (url_string && *url_string); - size = camel_data_wrapper_decode_to_stream_sync ( - emf->mode == EM_FORMAT_MODE_SOURCE ? - (CamelDataWrapper *) dw : - camel_medium_get_content ((CamelMedium *) dw), - (CamelStream *) filter_stream, cancellable, NULL); - camel_stream_flush ((CamelStream *) filter_stream, cancellable, NULL); - g_object_unref (filter_stream); + if (emf->priv->base_url) + camel_url_free (emf->priv->base_url); - g_seekable_seek (G_SEEKABLE (mem_stream), 0, G_SEEK_SET, NULL, NULL); + emf->priv->base_url = camel_url_new (url_string, NULL); - if (max == -1 || size == -1 || size < (max * 1024) || emf->composer) { - camel_stream_write_to_stream ( - mem_stream, (CamelStream *) stream, cancellable, NULL); - camel_stream_flush ((CamelStream *) stream, cancellable, NULL); - } else { - EM_FORMAT_GET_CLASS (emf)->format_optional ( - emf, stream, (CamelMimePart *) dw, - mem_stream, cancellable); - } + g_object_notify (G_OBJECT (emf), "base-url"); +} - if (windows) - g_object_unref (windows); +CamelURL * +em_format_get_base_url (EMFormat *emf) +{ + g_return_val_if_fail (EM_IS_FORMAT (emf), NULL); - g_object_unref (mem_stream); + return emf->priv->base_url; } /** - * em_format_describe_part: - * @part: - * @mimetype: - * - * Generate a simple textual description of a part, @mime_type represents the - * the content. + * em_format_clear_headers: + * @emf: * - * Return value: + * Clear the list of headers to be displayed. This will force all headers to + * be shown. **/ -gchar * -em_format_describe_part (CamelMimePart *part, - const gchar *mime_type) +void +em_format_clear_headers (EMFormat *emf) { - GString *stext; - const gchar *filename, *description; - gchar *content_type, *desc; - - stext = g_string_new(""); - content_type = g_content_type_from_mime_type (mime_type); - desc = g_content_type_get_description ( - content_type != NULL ? content_type : mime_type); - g_free (content_type); - g_string_append_printf ( - stext, _("%s attachment"), desc ? desc : mime_type); - g_free (desc); - - filename = camel_mime_part_get_filename (part); - description = camel_mime_part_get_description (part); - - if (!filename || !*filename) { - CamelDataWrapper *content; - - content = camel_medium_get_content (CAMEL_MEDIUM (part)); + EMFormatHeader *eh; - if (CAMEL_IS_MIME_MESSAGE (content)) - filename = camel_mime_message_get_subject ( - CAMEL_MIME_MESSAGE (content)); - } + g_return_if_fail (EM_IS_FORMAT (emf)); - if (filename != NULL && *filename != '\0') { - gchar *basename = g_path_get_basename (filename); - g_string_append_printf (stext, " (%s)", basename); - g_free (basename); + while ((eh = g_queue_pop_head (&emf->header_list)) != NULL) { + em_format_header_free (eh); } - if (description != NULL && *description != '\0' && - g_strcmp0 (filename, description) != 0) - g_string_append_printf (stext, ", \"%s\"", description); - - return g_string_free (stext, FALSE); } -static void -add_validity_found (EMFormat *emf, - CamelCipherValidity *valid) +void +em_format_default_headers (EMFormat *emf) { - g_return_if_fail (emf != NULL); - - if (!valid) - return; + gint ii; - if (valid->sign.status != CAMEL_CIPHER_VALIDITY_SIGN_NONE) - emf->validity_found |= EM_FORMAT_VALIDITY_FOUND_SIGNED; + g_return_if_fail (EM_IS_FORMAT (emf)); - if (valid->encrypt.status != CAMEL_CIPHER_VALIDITY_ENCRYPT_NONE) - emf->validity_found |= EM_FORMAT_VALIDITY_FOUND_ENCRYPTED; + /* Set the default headers */ + em_format_clear_headers (emf); + for (ii = 0; ii < G_N_ELEMENTS (default_headers); ii++) + em_format_add_header ( + emf, default_headers[ii].name, NULL, + default_headers[ii].flags); } -/* ********************************************************************** */ - -static void -preserve_charset_in_content_type (CamelMimePart *ipart, - CamelMimePart *opart) -{ - CamelDataWrapper *data_wrapper; - CamelContentType *content_type; - const gchar *charset; - - g_return_if_fail (ipart != NULL); - g_return_if_fail (opart != NULL); - - data_wrapper = camel_medium_get_content (CAMEL_MEDIUM (ipart)); - content_type = camel_data_wrapper_get_mime_type_field (data_wrapper); - - if (content_type == NULL) - return; - - charset = camel_content_type_param (content_type, "charset"); - - if (charset == NULL || *charset == '\0') - return; +/** + * em_format_add_header: + * @emf: + * @name: The name of the header, as it will appear during output. + * @value: Value of the header. Can be NULL. + * @flags: EM_FORMAT_HEAD_* defines to control display attributes. + * + * Add a specific header to show. If any headers are set, they will + * be displayed in the order set by this function. Certain known + * headers included in this list will be shown using special + * formatting routines. + **/ +void +em_format_add_header (EMFormat *emf, + const gchar *name, + const gchar *value, + guint32 flags) +{ + EMFormatHeader *h; - data_wrapper = camel_medium_get_content (CAMEL_MEDIUM (opart)); - content_type = camel_data_wrapper_get_mime_type_field (data_wrapper); + g_return_if_fail (EM_IS_FORMAT (emf)); + g_return_if_fail (name && *name); - camel_content_type_set_param (content_type, "charset", charset); + h = em_format_header_new (name, value); + h->flags = flags; + g_queue_push_tail (&emf->header_list, h); } -#ifdef ENABLE_SMIME -static void -emf_application_xpkcs7mime (EMFormat *emf, - CamelStream *stream, - CamelMimePart *part, - const EMFormatHandler *info, - GCancellable *cancellable, - gboolean is_fallback) +void +em_format_add_header_struct (EMFormat *emf, + EMFormatHeader *header) { - CamelCipherContext *context; - CamelMimePart *opart; - CamelCipherValidity *valid; - EMFormatCache *emfc; - GError *local_error = NULL; + g_return_if_fail (EM_IS_FORMAT (emf)); + g_return_if_fail (header && header->name); - /* should this perhaps run off a key of ".secured" ? */ - emfc = g_hash_table_lookup (emf->inline_table, emf->part_id->str); - if (emfc && emfc->valid) { - em_format_format_secure ( - emf, stream, emfc->secured, - camel_cipher_validity_clone (emfc->valid), - cancellable); - return; - } + em_format_add_header (emf, header->name, header->value, header->flags); +} - context = camel_smime_context_new (emf->session); +void +em_format_remove_header (EMFormat * emf, + const gchar *name, + const gchar *value) +{ + GList *iter = NULL; - emf->validity_found |= - EM_FORMAT_VALIDITY_FOUND_ENCRYPTED | - EM_FORMAT_VALIDITY_FOUND_SMIME; + g_return_if_fail (EM_IS_FORMAT (emf)); + g_return_if_fail (name && *name); - opart = camel_mime_part_new (); - valid = camel_cipher_context_decrypt_sync ( - context, part, opart, cancellable, &local_error); - preserve_charset_in_content_type (part, opart); - if (valid == NULL) { - em_format_format_error ( - emf, stream, "%s", - local_error->message ? local_error->message : - _("Could not parse S/MIME message: Unknown error")); - g_clear_error (&local_error); + iter = g_queue_peek_head_link (&emf->header_list); + while (iter) { + EMFormatHeader *header = iter->data; - em_format_part_as (emf, stream, part, NULL, cancellable); - } else { - if (emfc == NULL) - emfc = emf_insert_cache (emf, emf->part_id->str); + if (!header->value || !*header->value) { + GList *next = iter->next; + if (g_strcmp0 (name, header->name) == 0) + g_queue_delete_link (&emf->header_list, iter); - emfc->valid = camel_cipher_validity_clone (valid); - g_object_ref ((emfc->secured = opart)); + iter = next; + continue; + } + + if (value && *value) { + if ((g_strcmp0 (name, header->name) == 0) && + (g_strcmp0 (value, header->value) == 0)) + break; + } else { + if (g_strcmp0 (name, header->name) == 0) + break; + } - add_validity_found (emf, valid); - em_format_format_secure ( - emf, stream, opart, valid, cancellable); + iter = iter->next; } - g_object_unref (opart); - g_object_unref (context); + if (iter) { + em_format_header_free (iter->data); + g_queue_delete_link (&emf->header_list, iter); + } } -#endif -/* RFC 1740 */ -static void -emf_multipart_appledouble (EMFormat *emf, - CamelStream *stream, - CamelMimePart *part, - const EMFormatHandler *info, - GCancellable *cancellable, - gboolean is_fallback) +void +em_format_remove_header_struct (EMFormat * emf, + const EMFormatHeader * header) { - CamelMultipart *mp; - CamelMimePart *mime_part; - gint len; - - mp = (CamelMultipart *) camel_medium_get_content ((CamelMedium *) part); - - if (!CAMEL_IS_MULTIPART (mp)) { - em_format_format_source (emf, stream, part, cancellable); - return; - } - - mime_part = camel_multipart_get_part (mp, 1); - if (mime_part) { - /* try the data fork for something useful, doubtful but who knows */ - len = emf->part_id->len; - g_string_append_printf(emf->part_id, ".appledouble.1"); - em_format_part (emf, stream, mime_part, cancellable); - g_string_truncate (emf->part_id, len); - } else - em_format_format_source (emf, stream, part, cancellable); + g_return_if_fail (header); + em_format_remove_header (emf, header->name, header->value); } -/* RFC ??? */ -static void -emf_multipart_mixed (EMFormat *emf, - CamelStream *stream, - CamelMimePart *part, - const EMFormatHandler *info, - GCancellable *cancellable, - gboolean is_fallback) +void +em_format_add_puri (EMFormat *emf, + EMFormatPURI *puri) { - CamelMultipart *mp; - gint i, nparts, len; + GList *item; - mp = (CamelMultipart *) camel_medium_get_content ((CamelMedium *) part); + g_return_if_fail (EM_IS_FORMAT (emf)); + g_return_if_fail (puri != NULL); - if (!CAMEL_IS_MULTIPART (mp)) { - em_format_format_source (emf, stream, part, cancellable); - return; - } + emf->mail_part_list = g_list_append (emf->mail_part_list, puri); + item = g_list_last (emf->mail_part_list); - len = emf->part_id->len; - nparts = camel_multipart_get_number (mp); - for (i = 0; i < nparts; i++) { - part = camel_multipart_get_part (mp, i); - g_string_append_printf(emf->part_id, ".mixed.%d", i); - em_format_part (emf, stream, part, cancellable); - g_string_truncate (emf->part_id, len); - } -} + g_hash_table_insert (emf->mail_part_table, + puri->uri, item); -static gboolean related_display_part_is_attachment - (EMFormat *emf, - CamelMimePart *part); + d(printf("Added PURI %s\n", puri->uri)); +} -/* RFC 1740 */ -static void -emf_multipart_alternative (EMFormat *emf, - CamelStream *stream, - CamelMimePart *part, - const EMFormatHandler *info, - GCancellable *cancellable, - gboolean is_fallback) +EMFormatPURI * +em_format_find_puri (EMFormat *emf, + const gchar *id) { - CamelMultipart *mp; - gint i, nparts, bestid = 0; - CamelMimePart *best = NULL; - - mp = (CamelMultipart *) camel_medium_get_content ((CamelMedium *) part); + GList *list_iter; - if (!CAMEL_IS_MULTIPART (mp)) { - em_format_format_source (emf, stream, part, cancellable); - return; - } + /* First handle CIDs... */ + if (g_str_has_prefix (id, "CID:") || g_str_has_prefix (id, "cid:")) { + GHashTableIter iter; + gpointer key, value; - /* as per rfc, find the last part we know how to display */ - nparts = camel_multipart_get_number (mp); - for (i = 0; i < nparts; i++) { - CamelDataWrapper *data_wrapper; - CamelContentType *type; - CamelStream *null_stream; - gchar *mime_type; - gsize content_size; + g_hash_table_iter_init (&iter, emf->mail_part_table); + while (g_hash_table_iter_next (&iter, &key, &value)) { + EMFormatPURI *puri = ((GList *) value)->data; + if (g_strcmp0 (puri->cid, id) == 0) + return puri; + } - /* is it correct to use the passed in *part here? */ - part = camel_multipart_get_part (mp, i); + return NULL; + } - if (part == NULL) - continue; + list_iter = g_hash_table_lookup (emf->mail_part_table, id); + if (list_iter) + return list_iter->data; - /* This may block even though the stream does not. - * XXX Pretty inefficient way to test if the MIME part - * is empty. Surely there's a quicker way? */ - null_stream = camel_stream_null_new (); - data_wrapper = camel_medium_get_content (CAMEL_MEDIUM (part)); - camel_data_wrapper_decode_to_stream_sync ( - data_wrapper, null_stream, cancellable, NULL); - content_size = CAMEL_STREAM_NULL (null_stream)->written; - g_object_unref (null_stream); + return NULL; +} - if (content_size == 0) - continue; +void +em_format_class_add_handler (EMFormatClass *emfc, + EMFormatHandler *handler) +{ + EMFormatHandler *old_handler; - type = camel_mime_part_get_content_type (part); - mime_type = camel_content_type_simple (type); + g_return_if_fail (EM_IS_FORMAT_CLASS (emfc)); + g_return_if_fail (handler); - camel_strdown (mime_type); + old_handler = g_hash_table_lookup ( + emfc->type_handlers, handler->mime_type); - /*if (want_plain && !strcmp (mime_type, "text/plain")) - return part;*/ + handler->old = old_handler; - if (!em_format_is_attachment (emf, part) && - (!camel_content_type_is (type, "multipart", "related") || - !related_display_part_is_attachment (emf, part)) && - (em_format_find_handler (emf, mime_type) - || (best == NULL && em_format_fallback_handler (emf, mime_type)))) { - best = part; - bestid = i; - } + /* If parse_func or write_func of the new handler is not set, + * use function from the old handler (if it exists). + * This way we can assign a new write_func for to an existing + * parse_func */ + if (old_handler && handler->parse_func == NULL) { + handler->parse_func = old_handler->parse_func; + } - g_free (mime_type); + if (old_handler && handler->write_func == NULL) { + handler->write_func = old_handler->write_func; } - if (best) { - gint len = emf->part_id->len; + g_hash_table_insert (emfc->type_handlers, + handler->mime_type, handler); +} - g_string_append_printf(emf->part_id, ".alternative.%d", bestid); - em_format_part (emf, stream, best, cancellable); - g_string_truncate (emf->part_id, len); - } else - emf_multipart_mixed ( - emf, stream, part, info, cancellable, is_fallback); +void +em_format_class_remove_handler (EMFormatClass *emfc, + EMFormatHandler *handler) +{ + g_return_if_fail (EM_IS_FORMAT_CLASS (emfc)); + g_return_if_fail (handler); + + g_hash_table_remove (emfc->type_handlers, handler->mime_type); } -static void -emf_multipart_encrypted (EMFormat *emf, - CamelStream *stream, - CamelMimePart *part, - const EMFormatHandler *info, - GCancellable *cancellable, - gboolean is_fallback) +const EMFormatHandler * +em_format_find_handler (EMFormat *emf, + const gchar *mime_type) { - CamelCipherContext *context; - const gchar *protocol; - CamelMimePart *opart; - CamelCipherValidity *valid; - CamelMultipartEncrypted *mpe; - EMFormatCache *emfc; - GError *local_error = NULL; + EMFormatClass *emfc; + gchar *s; + const EMFormatHandler *handler; - /* should this perhaps run off a key of ".secured" ? */ - emfc = g_hash_table_lookup (emf->inline_table, emf->part_id->str); - if (emfc && emfc->valid) { - em_format_format_secure ( - emf, stream, emfc->secured, - camel_cipher_validity_clone (emfc->valid), - cancellable); - return; - } + g_return_val_if_fail (EM_IS_FORMAT (emf), NULL); + g_return_val_if_fail (mime_type && *mime_type, NULL); - mpe = (CamelMultipartEncrypted *) camel_medium_get_content ((CamelMedium *) part); - if (!CAMEL_IS_MULTIPART_ENCRYPTED (mpe)) { - em_format_format_error ( - emf, stream, _("Could not parse MIME message. " - "Displaying as source.")); - em_format_format_source (emf, stream, part, cancellable); - return; - } + emfc = (EMFormatClass *) G_OBJECT_GET_CLASS (emf); - /* Currently we only handle RFC2015-style PGP encryption. */ - protocol = camel_content_type_param ( - ((CamelDataWrapper *)mpe)->mime_type, "protocol"); - if (protocol == NULL || g_ascii_strcasecmp (protocol, "application/pgp-encrypted") != 0) { - em_format_format_error ( - emf, stream, _("Unsupported encryption " - "type for multipart/encrypted")); - em_format_part_as ( - emf, stream, part, - "multipart/mixed", cancellable); - return; - } + s = g_ascii_strdown (mime_type, -1); - emf->validity_found |= - EM_FORMAT_VALIDITY_FOUND_ENCRYPTED | - EM_FORMAT_VALIDITY_FOUND_PGP; + handler = g_hash_table_lookup ( + emfc->type_handlers, s); - context = camel_gpg_context_new (emf->session); - opart = camel_mime_part_new (); - valid = camel_cipher_context_decrypt_sync ( - context, part, opart, cancellable, &local_error); - preserve_charset_in_content_type (part, opart); - if (valid == NULL) { - em_format_format_error ( - emf, stream, local_error->message ? - _("Could not parse PGP/MIME message") : - _("Could not parse PGP/MIME message: Unknown error")); - if (local_error->message != NULL) - em_format_format_error ( - emf, stream, "%s", local_error->message); - g_clear_error (&local_error); + g_free (s); - em_format_part_as ( - emf, stream, part, - "multipart/mixed", cancellable); - } else { - if (emfc == NULL) - emfc = emf_insert_cache (emf, emf->part_id->str); + return handler; +} + +/** + * em_format_fallback_handler: + * @emf: + * @mime_type: + * + * Try to find a format handler based on the major type of the @mime_type. + * + * The subtype is replaced with "*" and a lookup performed. + * + * Return value: + **/ +const EMFormatHandler * +em_format_fallback_handler (EMFormat *emf, + const gchar *mime_type) +{ + gchar *mime, *s; - emfc->valid = camel_cipher_validity_clone (valid); - g_object_ref ((emfc->secured = opart)); + s = strchr (mime_type, '/'); + if (s == NULL) + mime = (gchar *) mime_type; + else { + gsize len = (s - mime_type) + 1; - add_validity_found (emf, valid); - em_format_format_secure ( - emf, stream, opart, valid, cancellable); + mime = g_alloca (len + 2); + strncpy (mime, mime_type, len); + strcpy(mime+len, "*"); } - /* TODO: Make sure when we finalize this part, it is zero'd out */ - g_object_unref (opart); - g_object_unref (context); + return em_format_find_handler (emf, mime); } -static CamelMimePart * -get_related_display_part (CamelMimePart *part, - gint *out_displayid) +void +em_format_parse (EMFormat *emf, + CamelMimeMessage *message, + CamelFolder *folder, + GCancellable *cancellable) { - CamelMultipart *mp; - CamelMimePart *body_part, *display_part = NULL; - CamelContentType *content_type; - const gchar *start; - gint i, nparts, displayid = 0; + GString *part_id; + EMFormatPURI *puri; + EMFormatParserInfo info = { 0 }; - mp = (CamelMultipart *) camel_medium_get_content ((CamelMedium *) part); + g_return_if_fail (EM_IS_FORMAT (emf)); - if (!CAMEL_IS_MULTIPART (mp)) - return NULL; + if (g_cancellable_is_cancelled (cancellable)) + return; - nparts = camel_multipart_get_number (mp); - content_type = camel_mime_part_get_content_type (part); - start = camel_content_type_param (content_type, "start"); - if (start && strlen (start) > 2) { - gint len; - const gchar *cid; + if (message) { + g_return_if_fail (CAMEL_IS_MIME_MESSAGE (message)); - /* strip <>'s */ - len = strlen (start) - 2; - start++; + if (emf->message) + g_object_unref (emf->message); + emf->message = g_object_ref (message); + } - for (i = 0; i < nparts; i++) { - body_part = camel_multipart_get_part (mp, i); - cid = camel_mime_part_get_content_id (body_part); + if (folder) { + g_return_if_fail (CAMEL_IS_FOLDER (folder)); - if (cid && !strncmp (cid, start, len) && strlen (cid) == len) { - display_part = body_part; - displayid = i; - break; - } - } - } else { - display_part = camel_multipart_get_part (mp, 0); + if (emf->folder) + g_object_unref (emf->folder); + emf->folder = g_object_ref (folder); } - if (out_displayid) - *out_displayid = displayid; + /* Before the actual parsing starts, let child classes prepare themselves. */ + if (EM_FORMAT_GET_CLASS (emf)->preparse) + EM_FORMAT_GET_CLASS (emf)->preparse (emf); - return display_part; + part_id = g_string_new (".message"); + + /* Create a special PURI with entire message */ + puri = em_format_puri_new (emf, sizeof (EMFormatPURI), + (CamelMimePart *) emf->message, part_id->str); + puri->mime_type = g_strdup ("text/html"); + em_format_add_puri (emf, puri); + + info.force_handler = TRUE; + em_format_parse_part_as (emf, CAMEL_MIME_PART (emf->message), part_id, &info, + "x-evolution/message", cancellable); + + g_string_free (part_id, TRUE); } -static gboolean -related_display_part_is_attachment (EMFormat *emf, - CamelMimePart *part) +void +em_format_write (EMFormat *emf, + CamelStream *stream, + EMFormatWriterInfo *info, + GCancellable *cancellable) { - CamelMimePart *display_part; + EMFormatClass *emf_class; - display_part = get_related_display_part (part, NULL); - return display_part && em_format_is_attachment (emf, display_part); + g_return_if_fail (EM_IS_FORMAT (emf)); + g_return_if_fail (CAMEL_IS_STREAM (stream)); + + emf_class = EM_FORMAT_GET_CLASS (emf); + if (emf_class->write) + emf_class->write (emf, stream, info, cancellable); } static void -emf_write_related (EMFormat *emf, - CamelStream *stream, - EMFormatPURI *puri, - GCancellable *cancellable) +emf_start_async_parser (GSimpleAsyncResult *result, + GObject *object, + GCancellable *cancellable) { - em_format_format_content (emf, stream, puri->part, cancellable); - camel_stream_close (stream, cancellable, NULL); + em_format_parse (EM_FORMAT (object), NULL, NULL, cancellable); } -/* RFC 2387 */ -static void -emf_multipart_related (EMFormat *emf, - CamelStream *stream, - CamelMimePart *part, - const EMFormatHandler *info, +void +em_format_parse_async (EMFormat *emf, + CamelMimeMessage *message, + CamelFolder *folder, GCancellable *cancellable, - gboolean is_fallback) + GAsyncReadyCallback callback, + gpointer user_data) { - CamelMultipart *mp; - CamelMimePart *body_part, *display_part = NULL; - gint i, nparts, partidlen, displayid = 0; - gchar *oldpartid; - GList *link; + GSimpleAsyncResult *result; - mp = (CamelMultipart *) camel_medium_get_content ((CamelMedium *) part); + g_return_if_fail (EM_IS_FORMAT (emf)); - if (!CAMEL_IS_MULTIPART (mp)) { - em_format_format_source (emf, stream, part, cancellable); + if (g_cancellable_is_cancelled (cancellable)) return; - } - display_part = get_related_display_part (part, &displayid); - - if (display_part == NULL) { - emf_multipart_mixed ( - emf, stream, part, info, cancellable, is_fallback); - return; - } + if (message) { + g_return_if_fail (CAMEL_IS_MIME_MESSAGE (message)); - em_format_push_level (emf); + if (emf->message) + g_object_unref (emf->message); - oldpartid = g_strdup (emf->part_id->str); - partidlen = emf->part_id->len; + emf->message = g_object_ref (message); - /* queue up the parts for possible inclusion */ - nparts = camel_multipart_get_number (mp); - for (i = 0; i < nparts; i++) { - body_part = camel_multipart_get_part (mp, i); - if (body_part != display_part) { - /* set the partid since add_puri uses it */ - g_string_append_printf(emf->part_id, ".related.%d", i); - em_format_add_puri ( - emf, sizeof (EMFormatPURI), NULL, - body_part, emf_write_related); - g_string_truncate (emf->part_id, partidlen); - } } - g_string_append_printf(emf->part_id, ".related.%d", displayid); - em_format_part (emf, stream, display_part, cancellable); - g_string_truncate (emf->part_id, partidlen); - camel_stream_flush (stream, NULL, NULL); + if (folder) { + g_return_if_fail (CAMEL_IS_FOLDER (folder)); - link = g_queue_peek_head_link (emf->pending_uri_level->data); - - while (link && link->next != NULL) { - EMFormatPURI *puri = link->data; + if (emf->folder) + g_object_unref (emf->folder); - if (puri->use_count == 0) { - if (puri->func == emf_write_related) { - g_string_printf(emf->part_id, "%s", puri->part_id); - em_format_part ( - emf, stream, puri->part, cancellable); - } - } + emf->folder = g_object_ref (folder); - link = g_list_next (link); } - g_string_printf(emf->part_id, "%s", oldpartid); - g_free (oldpartid); - - em_format_pull_level (emf); + result = g_simple_async_result_new (G_OBJECT (emf), callback, + user_data, em_format_parse_async); + g_simple_async_result_run_in_thread (result, emf_start_async_parser, + G_PRIORITY_DEFAULT, cancellable); } -static void -emf_multipart_signed (EMFormat *emf, - CamelStream *stream, - CamelMimePart *part, - const EMFormatHandler *info, - GCancellable *cancellable, - gboolean is_fallback) +void +em_format_parse_part_as (EMFormat *emf, + CamelMimePart *part, + GString *part_id, + EMFormatParserInfo *info, + const gchar *mime_type, + GCancellable *cancellable) { - CamelMimePart *cpart; - CamelMultipartSigned *mps; - CamelCipherContext *cipher = NULL; - EMFormatCache *emfc; - - /* should this perhaps run off a key of ".secured" ? */ - emfc = g_hash_table_lookup (emf->inline_table, emf->part_id->str); - if (emfc && emfc->valid) { - em_format_format_secure ( - emf, stream, emfc->secured, - camel_cipher_validity_clone (emfc->valid), - cancellable); - return; - } + const EMFormatHandler *handler; + const CamelContentDisposition *disposition; + EMFormatParserInfo ninfo = { + .handler = 0, + .validity_type = info ? info->validity_type : 0, + .validity = info ? info->validity : 0, + .force_handler = 0 + }; + + /* Let everything that claims to be an attachment or inlined part to be parsed + * as an attachment. The parser will decide how to display it. */ + disposition = camel_mime_part_get_content_disposition (part); + if (!info->force_handler && disposition && + (g_strcmp0 (disposition->disposition, "attachment") == 0)) { + ninfo.is_attachment = TRUE; + handler = em_format_find_handler (emf, "x-evolution/message/attachment"); + ninfo.handler = handler; + + if (handler && handler->parse_func) + handler->parse_func (emf, part, part_id, &ninfo, cancellable); - mps = (CamelMultipartSigned *) camel_medium_get_content ((CamelMedium *) part); - if (!CAMEL_IS_MULTIPART_SIGNED (mps) - || (cpart = camel_multipart_get_part ((CamelMultipart *) mps, - CAMEL_MULTIPART_SIGNED_CONTENT)) == NULL) { - em_format_format_error ( - emf, stream, _("Could not parse MIME message. " - "Displaying as source.")); - em_format_format_source (emf, stream, part, cancellable); return; } - /* FIXME: Should be done via a plugin interface */ - /* FIXME: duplicated in em-format-html-display.c */ - if (mps->protocol) { -#ifdef ENABLE_SMIME - if (g_ascii_strcasecmp ("application/x-pkcs7-signature", mps->protocol) == 0 - || g_ascii_strcasecmp ("application/pkcs7-signature", mps->protocol) == 0) { - cipher = camel_smime_context_new (emf->session); - emf->validity_found |= EM_FORMAT_VALIDITY_FOUND_SMIME; - } else -#endif - if (g_ascii_strcasecmp ("application/pgp-signature", mps->protocol) == 0) { - cipher = camel_gpg_context_new (emf->session); - emf->validity_found |= EM_FORMAT_VALIDITY_FOUND_PGP; - } - } - - emf->validity_found |= EM_FORMAT_VALIDITY_FOUND_SIGNED; - - if (cipher == NULL) { - em_format_format_error(emf, stream, _("Unsupported signature format")); - em_format_part_as ( - emf, stream, part, - "multipart/mixed", cancellable); + handler = em_format_find_handler (emf, mime_type); + if (handler && handler->parse_func) { + ninfo.handler = handler; + handler->parse_func (emf, part, part_id, &ninfo, cancellable); } else { - CamelCipherValidity *valid; - GError *local_error = NULL; + handler = em_format_find_handler (emf, "x-evolution/message/attachment"); + ninfo.handler = handler; - valid = camel_cipher_context_verify_sync ( - cipher, part, cancellable, &local_error); - if (valid == NULL) { - em_format_format_error ( - emf, stream, local_error->message ? - _("Error verifying signature") : - _("Unknown error verifying signature")); - if (local_error->message != NULL) - em_format_format_error ( - emf, stream, "%s", - local_error->message); - g_clear_error (&local_error); + /* When this fails, something is probably very wrong...*/ + if (handler && handler->parse_func) + handler->parse_func (emf, part, part_id, &ninfo, cancellable); + } +} - em_format_part_as ( - emf, stream, part, - "multipart/mixed", cancellable); - } else { - if (emfc == NULL) - emfc = emf_insert_cache (emf, emf->part_id->str); +void +em_format_parse_part (EMFormat *emf, + CamelMimePart *part, + GString *part_id, + EMFormatParserInfo *info, + GCancellable *cancellable) +{ + CamelContentType *ct; + gchar *mime_type; - emfc->valid = camel_cipher_validity_clone (valid); - g_object_ref ((emfc->secured = cpart)); + ct = camel_mime_part_get_content_type (part); + if (ct) { + mime_type = camel_content_type_simple (ct); + } else { + mime_type = (gchar *) "text/plain"; + } - add_validity_found (emf, valid); - em_format_format_secure ( - emf, stream, cpart, valid, cancellable); - } + em_format_parse_part_as (emf, part, part_id, info, mime_type, cancellable); - g_object_unref (cipher); - } + if (ct) + g_free (mime_type); } -/* RFC 4155 */ -static void -emf_application_mbox (EMFormat *emf, - CamelStream *stream, - CamelMimePart *mime_part, - const EMFormatHandler *info, - GCancellable *cancellable, - gboolean is_fallback) +gboolean +em_format_is_inline (EMFormat *emf, + const gchar *part_id, + CamelMimePart *part, + const EMFormatHandler *handler) { - const EMFormatHandler *handle; - CamelMimeParser *parser; - CamelStream *mem_stream; - camel_mime_parser_state_t state; - - /* Extract messages from the application/mbox part and - * render them as a flat list of messages. */ - - /* XXX If the mbox has multiple messages, maybe render them - * as a multipart/digest so each message can be expanded - * or collapsed individually. - * - * See attachment_handler_mail_x_uid_list() for example. */ + EMFormatClass *klass; - /* XXX This is based on em_utils_read_messages_from_stream(). - * Perhaps refactor that function to return an array of - * messages instead of assuming we want to append them - * to a folder? */ + g_return_val_if_fail (EM_IS_FORMAT (emf), FALSE); + g_return_val_if_fail (part_id && *part_id, FALSE); + g_return_val_if_fail (CAMEL_IS_MIME_PART (part), FALSE); + g_return_val_if_fail (handler, FALSE); - handle = em_format_find_handler (emf, "x-evolution/message/rfc822"); - g_return_if_fail (handle != NULL); + klass = EM_FORMAT_GET_CLASS (emf); + g_return_val_if_fail (klass->is_inline != NULL, FALSE); - parser = camel_mime_parser_new (); - camel_mime_parser_scan_from (parser, TRUE); + return klass->is_inline (emf, part_id, part, handler); - mem_stream = camel_stream_mem_new (); - camel_data_wrapper_decode_to_stream_sync ( - camel_medium_get_content (CAMEL_MEDIUM (mime_part)), - mem_stream, NULL, NULL); - g_seekable_seek (G_SEEKABLE (mem_stream), 0, G_SEEK_SET, NULL, NULL); - camel_mime_parser_init_with_stream (parser, mem_stream, NULL); - g_object_unref (mem_stream); +} - /* Extract messages from the mbox. */ - state = camel_mime_parser_step (parser, NULL, NULL); - while (state == CAMEL_MIME_PARSER_STATE_FROM) { - CamelMimeMessage *message; +void +em_format_format_error (EMFormat *emf, + const gchar *format, + ...) +{ + EMFormatPURI *puri; + CamelMimePart *part; + const EMFormatHandler *handler; + gchar *errmsg; + gchar *uri; + va_list ap; - message = camel_mime_message_new (); - mime_part = CAMEL_MIME_PART (message); + g_return_if_fail (EM_IS_FORMAT (emf)); + g_return_if_fail (format != NULL); - if (!camel_mime_part_construct_from_parser_sync ( - mime_part, parser, NULL, NULL)) { - g_object_unref (message); - break; - } + va_start (ap, format); + errmsg = g_strdup_vprintf (format, ap); - /* Render the message. */ - handle->handler ( - emf, stream, mime_part, - handle, cancellable, FALSE); + part = camel_mime_part_new (); + camel_mime_part_set_content (part, errmsg, strlen (errmsg), "text/plain"); + g_free (errmsg); + va_end (ap); - g_object_unref (message); + handler = em_format_find_handler (emf, "x-evolution/error"); - /* Skip past CAMEL_MIME_PARSER_STATE_FROM_END. */ - camel_mime_parser_step (parser, NULL, NULL); + emf->priv->last_error++; + uri = g_strdup_printf (".error.%d", emf->priv->last_error); + puri = em_format_puri_new (emf, sizeof (EMFormatPURI), part, uri); + puri->mime_type = g_strdup ("text/html"); + if (handler && handler->write_func) + puri->write_func = handler->write_func; + else + puri->write_func = emf_write_error; - state = camel_mime_parser_step (parser, NULL, NULL); - } + em_format_add_puri (emf, puri); - g_object_unref (parser); + g_free (uri); + g_object_unref (part); } -static void -emf_message_rfc822 (EMFormat *emf, - CamelStream *stream, - CamelMimePart *part, - const EMFormatHandler *info, - GCancellable *cancellable, - gboolean is_fallback) +/** + * em_format_format_text: + * @emf: + * @stream: Where to write the converted text + * @part: Part whose container is to be formatted + * @cancellable: optional #GCancellable object, or %NULL + * + * Decode/output a part's content to @stream. + **/ +void +em_format_format_text (EMFormat *emf, + CamelStream *stream, + CamelDataWrapper *dw, + GCancellable *cancellable) { - CamelDataWrapper *dw = camel_medium_get_content ((CamelMedium *) part); - const EMFormatHandler *handle; - gint len; - gchar *parent_message_part_id; + CamelStream *filter_stream; + CamelMimeFilter *filter; + const gchar *charset = NULL; + CamelMimeFilterWindows *windows = NULL; + CamelStream *mem_stream = NULL; + gsize size; + gsize max; + GSettings *settings; - if (!CAMEL_IS_MIME_MESSAGE (dw)) { - em_format_format_source (emf, stream, part, cancellable); + if (g_cancellable_is_cancelled (cancellable)) return; - } - - parent_message_part_id = emf->current_message_part_id; - emf->current_message_part_id = g_strdup (emf->part_id->str); - len = emf->part_id->len; - g_string_append_printf(emf->part_id, ".rfc822"); - - handle = em_format_find_handler(emf, "x-evolution/message/rfc822"); - if (handle) - handle->handler ( - emf, stream, CAMEL_MIME_PART (dw), - handle, cancellable, FALSE); + if (emf->priv->charset) { + charset = emf->priv->charset; + } else if (dw->mime_type + && (charset = camel_content_type_param (dw->mime_type, "charset")) + && g_ascii_strncasecmp(charset, "iso-8859-", 9) == 0) { + CamelStream *null; - g_string_truncate (emf->part_id, len); + /* Since a few Windows mailers like to claim they sent + * out iso-8859-# encoded text when they really sent + * out windows-cp125#, do some simple sanity checking + * before we move on... */ - g_free (emf->current_message_part_id); - emf->current_message_part_id = parent_message_part_id; -} + null = camel_stream_null_new (); + filter_stream = camel_stream_filter_new (null); + g_object_unref (null); -static void -emf_message_deliverystatus (EMFormat *emf, - CamelStream *stream, - CamelMimePart *part, - const EMFormatHandler *info, - GCancellable *cancellable, - gboolean is_fallback) -{ - em_format_format_text ( - emf, stream, (CamelDataWrapper *) part, cancellable); -} + windows = (CamelMimeFilterWindows *) camel_mime_filter_windows_new (charset); + camel_stream_filter_add ( + CAMEL_STREAM_FILTER (filter_stream), + CAMEL_MIME_FILTER (windows)); -static void -emf_inlinepgp_signed (EMFormat *emf, - CamelStream *stream, - CamelMimePart *ipart, - const EMFormatHandler *info, - GCancellable *cancellable, - gboolean is_fallback) -{ - CamelStream *filtered_stream; - CamelMimeFilterPgp *pgp_filter; - CamelContentType *content_type; - CamelCipherContext *cipher; - CamelCipherValidity *valid; - CamelDataWrapper *dw; - CamelMimePart *opart; - CamelStream *ostream; - gchar *type; - GError *local_error = NULL; + camel_data_wrapper_decode_to_stream_sync ( + dw, (CamelStream *) filter_stream, cancellable, NULL); + camel_stream_flush ((CamelStream *) filter_stream, cancellable, NULL); + g_object_unref (filter_stream); - if (!ipart) { - em_format_format_error(emf, stream, _("Unknown error verifying signature")); - return; + charset = camel_mime_filter_windows_real_charset (windows); + } else if (charset == NULL) { + charset = emf->priv->default_charset; } - emf->validity_found |= - EM_FORMAT_VALIDITY_FOUND_SIGNED | - EM_FORMAT_VALIDITY_FOUND_PGP; + mem_stream = (CamelStream *) camel_stream_mem_new (); + filter_stream = camel_stream_filter_new (mem_stream); - cipher = camel_gpg_context_new (emf->session); - /* Verify the signature of the message */ - valid = camel_cipher_context_verify_sync ( - cipher, ipart, cancellable, &local_error); - if (!valid) { - em_format_format_error ( - emf, stream, local_error->message ? - _("Error verifying signature") : - _("Unknown error verifying signature")); - if (local_error->message) - em_format_format_error ( - emf, stream, "%s", local_error->message); - em_format_format_source (emf, stream, ipart, cancellable); - /* XXX I think this will loop: - * em_format_part_as(emf, stream, part, "text/plain"); */ - g_clear_error (&local_error); - g_object_unref (cipher); - return; + if ((filter = camel_mime_filter_charset_new (charset, "UTF-8"))) { + camel_stream_filter_add ( + CAMEL_STREAM_FILTER (filter_stream), + CAMEL_MIME_FILTER (filter)); + g_object_unref (filter); } - /* Setup output stream */ - ostream = camel_stream_mem_new (); - filtered_stream = camel_stream_filter_new (ostream); - - /* Add PGP header / footer filter */ - pgp_filter = (CamelMimeFilterPgp *) camel_mime_filter_pgp_new (); - camel_stream_filter_add ( - CAMEL_STREAM_FILTER (filtered_stream), - CAMEL_MIME_FILTER (pgp_filter)); - g_object_unref (pgp_filter); - - /* Pass through the filters that have been setup */ - dw = camel_medium_get_content ((CamelMedium *) ipart); - camel_data_wrapper_decode_to_stream_sync ( - dw, (CamelStream *) filtered_stream, NULL, NULL); - camel_stream_flush ((CamelStream *) filtered_stream, NULL, NULL); - g_object_unref (filtered_stream); + max = -1; - /* Create a new text/plain MIME part containing the signed - * content preserving the original part's Content-Type params. */ - content_type = camel_mime_part_get_content_type (ipart); - type = camel_content_type_format (content_type); - content_type = camel_content_type_decode (type); - g_free (type); + settings = g_settings_new ("org.gnome.evolution.mail"); + if (g_settings_get_boolean (settings, "force-message-limit")) { + max = g_settings_get_int (settings, "message-text-part-limit"); + if (max == 0) + max = -1; + } + g_object_unref (settings); - g_free (content_type->type); - content_type->type = g_strdup ("text"); - g_free (content_type->subtype); - content_type->subtype = g_strdup ("plain"); - type = camel_content_type_format (content_type); - camel_content_type_unref (content_type); + size = camel_data_wrapper_decode_to_stream_sync ( + camel_medium_get_content ((CamelMedium *) dw), + (CamelStream *) filter_stream, cancellable, NULL); + camel_stream_flush ((CamelStream *) filter_stream, cancellable, NULL); + g_object_unref (filter_stream); - dw = camel_data_wrapper_new (); - camel_data_wrapper_construct_from_stream_sync (dw, ostream, NULL, NULL); - camel_data_wrapper_set_mime_type (dw, type); - g_free (type); + g_seekable_seek (G_SEEKABLE (mem_stream), 0, G_SEEK_SET, NULL, NULL); - opart = camel_mime_part_new (); - camel_medium_set_content ((CamelMedium *) opart, dw); - camel_data_wrapper_set_mime_type_field ( - (CamelDataWrapper *) opart, dw->mime_type); + if (max == -1 || size == -1 || size < (max * 1024) || emf->priv->composer) { + camel_stream_write_to_stream ( + mem_stream, (CamelStream *) stream, cancellable, NULL); + camel_stream_flush ((CamelStream *) mem_stream, cancellable, NULL); + } else { + /* Parse it as an attachment */ + CamelMimePart *part = camel_mime_part_new (); + EMFormatParserInfo info = { 0 }; + GString *part_id = g_string_new (".attachment"); + camel_medium_set_content ((CamelMedium *) part, dw); + + info.is_attachment = TRUE; + em_format_parse_part_as (emf, part, part_id, &info, + "x-evolution/message/attachment", cancellable); + + g_string_free (part_id, TRUE); + g_object_unref (part); + } - add_validity_found (emf, valid); - /* Pass it off to the real formatter */ - em_format_format_secure (emf, stream, opart, valid, cancellable); + if (windows) + g_object_unref (windows); - /* Clean Up */ - g_object_unref (dw); - g_object_unref (opart); - g_object_unref (ostream); - g_object_unref (cipher); + g_object_unref (mem_stream); } -static void -emf_inlinepgp_encrypted (EMFormat *emf, - CamelStream *stream, - CamelMimePart *ipart, - const EMFormatHandler *info, - GCancellable *cancellable, - gboolean is_fallback) +/** + * em_format_describe_part: + * @part: + * @mimetype: + * + * Generate a simple textual description of a part, @mime_type represents + * the content. + * + * Return value: + **/ +gchar * +em_format_describe_part (CamelMimePart *part, + const gchar *mime_type) { - CamelCipherContext *cipher; - CamelCipherValidity *valid; - CamelMimePart *opart; - CamelDataWrapper *dw; - gchar *mime_type; - GError *local_error = NULL; + GString *stext; + const gchar *filename, *description; + gchar *content_type, *desc; - emf->validity_found |= - EM_FORMAT_VALIDITY_FOUND_ENCRYPTED | - EM_FORMAT_VALIDITY_FOUND_PGP; + stext = g_string_new(""); + content_type = g_content_type_from_mime_type (mime_type); + desc = g_content_type_get_description ( + content_type != NULL ? content_type : mime_type); + g_free (content_type); + g_string_append_printf ( + stext, _("%s attachment"), desc ? desc : mime_type); + g_free (desc); - cipher = camel_gpg_context_new (emf->session); - opart = camel_mime_part_new (); + filename = camel_mime_part_get_filename (part); + description = camel_mime_part_get_description (part); - /* Decrypt the message */ - valid = camel_cipher_context_decrypt_sync ( - cipher, ipart, opart, cancellable, &local_error); + if (!filename || !*filename) { + CamelDataWrapper *content; - if (!valid) { - em_format_format_error ( - emf, stream, _("Could not parse PGP message: ")); - if (local_error->message != NULL) - em_format_format_error ( - emf, stream, "%s", local_error->message); - else - em_format_format_error ( - emf, stream, _("Unknown error")); - em_format_format_source (emf, stream, ipart, cancellable); - /* XXX I think this will loop: - * em_format_part_as(emf, stream, part, "text/plain"); */ + content = camel_medium_get_content (CAMEL_MEDIUM (part)); - g_clear_error (&local_error); - g_object_unref (cipher); - g_object_unref (opart); - return; + if (CAMEL_IS_MIME_MESSAGE (content)) + filename = camel_mime_message_get_subject ( + CAMEL_MIME_MESSAGE (content)); } - dw = camel_medium_get_content ((CamelMedium *) opart); - mime_type = camel_data_wrapper_get_mime_type (dw); - - /* this ensures to show the 'opart' as inlined, if possible */ - if (mime_type != NULL && g_ascii_strcasecmp ( - mime_type, "application/octet-stream") == 0) { - const gchar *snoop = em_format_snoop_type (opart); - - if (snoop) - camel_data_wrapper_set_mime_type (dw, snoop); + if (filename != NULL && *filename != '\0') { + gchar *basename = g_path_get_basename (filename); + g_string_append_printf (stext, " (%s)", basename); + g_free (basename); } - preserve_charset_in_content_type (ipart, opart); - g_free (mime_type); - - add_validity_found (emf, valid); - /* Pass it off to the real formatter */ - em_format_format_secure (emf, stream, opart, valid, cancellable); + if (description != NULL && *description != '\0' && + g_strcmp0 (filename, description) != 0) + g_string_append_printf (stext, ", \"%s\"", description); - /* Clean Up */ - g_object_unref (opart); - g_object_unref (cipher); + return g_string_free (stext, FALSE); } -static EMFormatHandler type_builtin_table[] = { -#ifdef ENABLE_SMIME - { (gchar *) "application/x-pkcs7-mime", - emf_application_xpkcs7mime, - EM_FORMAT_HANDLER_INLINE_DISPOSITION }, -#endif - { (gchar *) "application/mbox", emf_application_mbox, EM_FORMAT_HANDLER_INLINE }, - { (gchar *) "multipart/alternative", emf_multipart_alternative }, - { (gchar *) "multipart/appledouble", emf_multipart_appledouble }, - { (gchar *) "multipart/encrypted", emf_multipart_encrypted }, - { (gchar *) "multipart/mixed", emf_multipart_mixed }, - { (gchar *) "multipart/signed", emf_multipart_signed }, - { (gchar *) "multipart/related", emf_multipart_related }, - { (gchar *) "multipart/*", emf_multipart_mixed }, - { (gchar *) "message/rfc822", emf_message_rfc822, EM_FORMAT_HANDLER_INLINE }, - { (gchar *) "message/news", emf_message_rfc822, EM_FORMAT_HANDLER_INLINE }, - { (gchar *) "message/delivery-status", emf_message_deliverystatus }, - { (gchar *) "message/*", emf_message_rfc822, EM_FORMAT_HANDLER_INLINE }, - - /* Insert brokenly-named parts here */ -#ifdef ENABLE_SMIME - { (gchar *) "application/pkcs7-mime", - emf_application_xpkcs7mime, - EM_FORMAT_HANDLER_INLINE_DISPOSITION }, -#endif - - /* internal types */ - { (gchar *) "application/x-inlinepgp-signed", emf_inlinepgp_signed }, - { (gchar *) "application/x-inlinepgp-encrypted", emf_inlinepgp_encrypted }, -}; - -static void -emf_builtin_init (EMFormatClass *class) +/** + * em_format_is_attachment: + * @emf: + * @part: Part to check. + * + * Returns true if the part is an attachment. + * + * A part is not considered an attachment if it is a + * multipart, or a text part with no filename. It is used + * to determine if an attachment header should be displayed for + * the part. + * + * Content-Disposition is not checked. + * + * Return value: TRUE/FALSE + **/ +gint +em_format_is_attachment (EMFormat *emf, + CamelMimePart *part) { - gint ii; + /*CamelContentType *ct = camel_mime_part_get_content_type(part);*/ + CamelDataWrapper *dw = camel_medium_get_content ((CamelMedium *) part); + + if (!dw) + return 0; - for (ii = 0; ii < G_N_ELEMENTS (type_builtin_table); ii++) - g_hash_table_insert ( - class->type_handlers, - type_builtin_table[ii].mime_type, - &type_builtin_table[ii]); + d(printf("checking is attachment %s/%s\n", dw->mime_type->type, dw->mime_type->subtype)); + return !(camel_content_type_is (dw->mime_type, "multipart", "*") + || camel_content_type_is ( + dw->mime_type, "application", "x-pkcs7-mime") + || camel_content_type_is ( + dw->mime_type, "application", "pkcs7-mime") + || camel_content_type_is ( + dw->mime_type, "application", "x-inlinepgp-signed") + || camel_content_type_is ( + dw->mime_type, "application", "x-inlinepgp-encrypted") + || camel_content_type_is ( + dw->mime_type, "x-evolution", "evolution-rss-feed") + || camel_content_type_is (dw->mime_type, "text", "calendar") + || camel_content_type_is (dw->mime_type, "text", "x-calendar") + || (camel_content_type_is (dw->mime_type, "text", "*") + && camel_mime_part_get_filename (part) == NULL)); } /** @@ -2493,5 +2401,237 @@ em_format_snoop_type (CamelMimePart *part) return res; /* We used to load parts to check their type, we don't anymore, - * see bug #11778 for some discussion */ + * see bug #211778 for some discussion */ +} + +/** + * Construct a URI for message. + * + * The URI can contain multiple query parameters. The list of parameters must be + * NULL-terminated. Each query must contain name, GType of value and value. + * + * @param folder Folder wit the message + * @param message_uid ID of message within the \p folder + * @param first_param_name Name of first query parameter followed by GType of it's value and value. + */ +gchar * +em_format_build_mail_uri (CamelFolder *folder, + const gchar *message_uid, + const gchar *first_param_name, + ...) +{ + CamelStore *store; + gchar *uri, *tmp; + va_list ap; + const gchar *name; + const gchar *service_uid, *folder_name; + gchar separator; + + g_return_val_if_fail (message_uid && *message_uid, NULL); + + if (!folder) { + folder_name = "generic"; + service_uid = "generic"; + } else { + folder_name = camel_folder_get_full_name (folder); + store = camel_folder_get_parent_store (folder); + if (store) + service_uid = camel_service_get_uid (CAMEL_SERVICE (store)); + else + service_uid = "generic"; + } + + tmp = g_strdup_printf ("mail://%s/%s/%s", + service_uid, + folder_name, + message_uid); + + va_start (ap, first_param_name); + name = first_param_name; + separator = '?'; + while (name) { + gchar *tmp2; + gint type = va_arg (ap, gint); + switch (type) { + case G_TYPE_INT: + case G_TYPE_BOOLEAN: { + gint val = va_arg (ap, gint); + tmp2 = g_strdup_printf ("%s%c%s=%d", tmp, + separator, name, val); + break; + } + case G_TYPE_FLOAT: + case G_TYPE_DOUBLE: { + gdouble val = va_arg (ap, double); + tmp2 = g_strdup_printf ("%s%c%s=%f", tmp, + separator, name, val); + break; + } + case G_TYPE_STRING: { + gchar *val = va_arg (ap, gchar *); + gchar *escaped = soup_uri_encode (val, NULL); + tmp2 = g_strdup_printf ("%s%c%s=%s", tmp, + separator, name, escaped); + g_free (escaped); + break; + } + default: + g_warning ("Invalid param type %s", g_type_name (type)); + return NULL; + } + + g_free (tmp); + tmp = tmp2; + + if (separator == '?') + separator = '&'; + + name = va_arg (ap, gchar *); + } + va_end (ap); + + uri = tmp; + + /* For some reason, webkit won't accept URL with username, but + * without password (mail://store@host/folder/mail), so we + * will replace the '@' symbol by '/' to get URL like + * mail://store/host/folder/mail which is OK + */ + tmp = strchr (tmp, '@'); + if (tmp) { + tmp[0] = '/'; + } + + return uri; +} + +void +em_format_redraw (EMFormat *emf) +{ + g_return_if_fail (EM_IS_FORMAT (emf)); + + g_signal_emit (emf, signals[REDRAW_REQUESTED], 0); +} + +/**************************************************************************/ +EMFormatPURI * +em_format_puri_new (EMFormat *emf, + gsize puri_size, + CamelMimePart *part, + const gchar *uri) +{ + EMFormatPURI *puri; + + g_return_val_if_fail (EM_IS_FORMAT (emf), NULL); + g_return_val_if_fail (puri_size >= sizeof (EMFormatPURI), NULL); + + puri = (EMFormatPURI *) g_malloc0 (puri_size); + puri->emf = emf; + + if (part) + puri->part = g_object_ref (part); + + if (uri) + puri->uri = g_strdup (uri); + + return puri; +} + +void +em_format_puri_free (EMFormatPURI *puri) +{ + g_return_if_fail (puri); + + if (puri->part) + g_object_unref (puri->part); + + if (puri->uri) + g_free (puri->uri); + + if (puri->cid) + g_free (puri->cid); + + if (puri->mime_type) + g_free (puri->mime_type); + + if (puri->validity) + camel_cipher_validity_free (puri->validity); + + if (puri->validity_parent) + camel_cipher_validity_free (puri->validity_parent); + + if (puri->free) + puri->free (puri); + + g_free (puri); +} + +void +em_format_puri_write (EMFormatPURI *puri, + CamelStream *stream, + EMFormatWriterInfo *info, + GCancellable *cancellable) +{ + g_return_if_fail (puri); + g_return_if_fail (CAMEL_IS_STREAM (stream)); + + if (info->mode == EM_FORMAT_WRITE_MODE_SOURCE) { + const EMFormatHandler *handler; + handler = em_format_find_handler (puri->emf, "x-evolution/message/source"); + handler->write_func (puri->emf, puri, stream, info, cancellable); + return; + } + + if (puri->write_func) { + puri->write_func (puri->emf, puri, stream, info, cancellable); + } else { + const EMFormatHandler *handler; + const gchar *mime_type; + + if (puri->mime_type) { + mime_type = puri->mime_type; + } else { + mime_type = (gchar *) "plain/text"; + } + + handler = em_format_find_handler (puri->emf, mime_type); + if (handler && handler->write_func) { + handler->write_func (puri->emf, + puri, stream, info, cancellable); + } + } +} + +EMFormatHeader * +em_format_header_new (const gchar *name, + const gchar *value) +{ + EMFormatHeader *header; + + g_return_val_if_fail (name && *name, NULL); + + header = g_new0 (EMFormatHeader, 1); + header->name = g_strdup (name); + if (value && *value) + header->value = g_strdup (value); + + return header; +} + +void +em_format_header_free (EMFormatHeader * header) +{ + g_return_if_fail (header != NULL); + + if (header->name) { + g_free (header->name); + header->name = NULL; + } + + if (header->value) { + g_free (header->value); + header->value = NULL; + } + + g_free (header); } diff --git a/em-format/em-format.h b/em-format/em-format.h index cf214d81a5..712b41bce1 100644 --- a/em-format/em-format.h +++ b/em-format/em-format.h @@ -21,14 +21,11 @@ * */ -/* - Abstract class for formatting mime messages -*/ - #ifndef EM_FORMAT_H #define EM_FORMAT_H #include +#include /* Standard GObject macros */ #define EM_TYPE_FORMAT \ @@ -51,194 +48,139 @@ G_BEGIN_DECLS +#define EM_FORMAT_HEADER_BOLD (1<<0) +#define EM_FORMAT_HEADER_LAST (1<<4) /* reserve 4 slots */ + +#define EM_FORMAT_VALIDITY_FOUND_PGP (1<<0) +#define EM_FORMAT_VALIDITY_FOUND_SMIME (1<<1) +#define EM_FORMAT_VALIDITY_FOUND_SIGNED (1<<2) +#define EM_FORMAT_VALIDITY_FOUND_ENCRYPTED (1<<3) + typedef struct _EMFormat EMFormat; typedef struct _EMFormatClass EMFormatClass; typedef struct _EMFormatPrivate EMFormatPrivate; -typedef struct _EMFormatHandler EMFormatHandler; +typedef struct _EMFormatPURI EMFormatPURI; typedef struct _EMFormatHeader EMFormatHeader; +typedef struct _EMFormatHandler EMFormatHandler; +typedef struct _EMFormatParserInfo EMFormatParserInfo; +typedef struct _EMFormatWriterInfo EMFormatWriterInfo; +typedef struct _WebKitDOMElement WebKitDOMElement; -typedef void (*EMFormatFunc) (EMFormat *emf, +typedef void (*EMFormatParseFunc) (EMFormat *emf, + CamelMimePart *part, + GString *part_id, + EMFormatParserInfo *info, + GCancellable *cancellable); +typedef void (*EMFormatWriteFunc) (EMFormat *emf, + EMFormatPURI *puri, CamelStream *stream, - CamelMimePart *mime_part, - const EMFormatHandler *info, - GCancellable *cancellable, - gboolean is_fallback); - -typedef enum { - EM_FORMAT_MODE_NORMAL, - EM_FORMAT_MODE_ALLHEADERS, - EM_FORMAT_MODE_SOURCE -} EMFormatMode; + EMFormatWriterInfo *info, + GCancellable *cancellable); +typedef GtkWidget * (*EMFormatWidgetFunc) (EMFormat *emf, + EMFormatPURI *puri, + GCancellable *cancellable); +typedef void (*EMailDisplayBindFunc) (WebKitDOMElement *root, + EMFormatPURI *puri); -/** - * EMFormatHandlerFlags - Format handler flags. - * - * @EM_FORMAT_HANDLER_INLINE: This type should be shown expanded - * inline by default. - * @EM_FORMAT_HANDLER_INLINE_DISPOSITION: This type should always be - * shown inline, despite what the Content-Disposition suggests. - * - **/ typedef enum { EM_FORMAT_HANDLER_INLINE = 1 << 0, - EM_FORMAT_HANDLER_INLINE_DISPOSITION = 1 << 1 + EM_FORMAT_HANDLER_INLINE_DISPOSITION = 1 << 1, + EM_FORMAT_HANDLER_COMPOUND_TYPE = 1 << 2 } EMFormatHandlerFlags; -/** - * struct _EMFormatHandler - MIME type handler. - * - * @mime_type: Type this handler handles. - * @handler: The handler callback. - * @flags: Handler flags - * @old: The last handler set on this type. Allows overrides to - * fallback to previous implementation. - * - **/ +typedef enum { + EM_FORMAT_WRITE_MODE_NORMAL= 1 << 0, + EM_FORMAT_WRITE_MODE_ALL_HEADERS = 1 << 1, + EM_FORMAT_WRITE_MODE_SOURCE = 1 << 2, + EM_FORMAT_WRITE_MODE_PRINTING = 1 << 3, + EM_FORMAT_WRITE_MODE_RAW = 1 << 4 +} EMFormatWriteMode; + struct _EMFormatHandler { gchar *mime_type; - EMFormatFunc handler; + EMFormatParseFunc parse_func; + EMFormatWriteFunc write_func; EMFormatHandlerFlags flags; EMFormatHandler *old; }; -typedef struct _EMFormatPURI EMFormatPURI; -typedef void (*EMFormatPURIFunc) (EMFormat *emf, - CamelStream *stream, - EMFormatPURI *puri, - GCancellable *cancellable); - /** - * struct _EMFormatPURI - Pending URI object. - * - * @free: May be set by allocator and will be called when no longer needed. - * @format: - * @uri: Calculated URI of the part, if the part has one in its - * Content-Location field. - * @cid: The RFC2046 Content-Id of the part. If none is present, a unique value - * is calculated from @part_id. - * @part_id: A unique identifier for each part. - * @func: Callback for when the URI is requested. The callback writes - * its data to the supplied stream. - * @part: - * @use_count: - * - * This is used for multipart/related, and other formatters which may - * need to include a reference to out-of-band data in the content - * stream. - * - * This object may be subclassed as a struct. - **/ -struct _EMFormatPURI { - void (*free)(EMFormatPURI *p); /* optional callback for freeing user-fields */ - EMFormat *format; + * Use this struct to pass additional information between + * EMFormatParseFunc's. + * Much cleaner then setting public property of EMFormat. + */ +struct _EMFormatParserInfo { + const EMFormatHandler *handler; - gchar *uri; /* will be the location of the part, may be empty */ - gchar *cid; /* will always be set, a fake one created if needed */ - gchar *part_id; /* will always be set, emf->part_id->str for this part */ + /* EM_FORMAT_VALIDITY_* flags */ + guint32 validity_type; + CamelCipherValidity *validity; - EMFormatPURIFunc func; - CamelMimePart *part; + gint is_attachment : 1; + gint force_handler: 1; +}; - guint use_count; /* used by multipart/related to see if it was accessed */ +struct _EMFormatWriterInfo { + EMFormatWriteMode mode; + gboolean headers_collapsable; + gboolean headers_collapsed; }; struct _EMFormatHeader { guint32 flags; /* E_FORMAT_HEADER_ * */ - gchar name[1]; + gchar *name; + gchar *value; }; #define EM_FORMAT_HEADER_BOLD (1<<0) #define EM_FORMAT_HEADER_LAST (1<<4) /* reserve 4 slots */ -#define EM_FORMAT_VALIDITY_FOUND_PGP (1<<0) -#define EM_FORMAT_VALIDITY_FOUND_SMIME (1<<1) -#define EM_FORMAT_VALIDITY_FOUND_SIGNED (1<<2) -#define EM_FORMAT_VALIDITY_FOUND_ENCRYPTED (1<<3) +struct _EMFormatPURI { + CamelMimePart *part; + + EMFormat *emf; + EMFormatWriteFunc write_func; + EMFormatWidgetFunc widget_func; + + /** + * Called by #EMailDisplay whenever document/frame is reloaded. + * Modules and plugins can create bindings to events of DOM + * objects they created. + */ + EMailDisplayBindFunc bind_func; + + gchar *uri; + gchar *cid; + gchar *mime_type; + + /* EM_FORMAT_VALIDITY_* flags */ + guint32 validity_type; + CamelCipherValidity *validity; + CamelCipherValidity *validity_parent; + + gboolean is_attachment; + + void (*free)(EMFormatPURI *puri); /* optional callback for freeing user-fields */ +}; -/** - * struct _EMFormat - Mail formatter object. - * - * @parent: - * @priv: - * @message: - * @folder: - * @uid: - * @part_id: - * @header_list: - * @session: - * @base url: - * @snoop_mime_type: - * @valid: - * @valid_parent: - * @inline_table: - * @pending_uri_table: - * @pending_uri_tree: - * @pending_uri_level: - * @mode: - * @charset: - * @default_charset: - * - * Most fields are private or read-only. - * - * This is the base MIME formatter class. It provides no formatting - * itself, but drives most of the basic types, including multipart / * types. - **/ struct _EMFormat { GObject parent; EMFormatPrivate *priv; - /* The current message */ CamelMimeMessage *message; - CamelFolder *folder; - gchar *uid; + gchar *message_uid; + gchar *uri_base; - /* Current part ID prefix for identifying parts directly. */ - GString *part_id; - /* part_id of the currently processing message - * (when the message has message-attachments) */ - gchar *current_message_part_id; + /* Defines order in which parts should be displayed */ + GList *mail_part_list; + /* For quick search for parts by their URI/ID */ + GHashTable *mail_part_table; /* If empty, then all. */ GQueue header_list; - - /* Used for authentication when required. */ - CamelSession *session; - - /* Content-Base header or absolute Content-Location, for any part. */ - CamelURL *base; - - /* If we snooped an application/octet-stream, what we snooped. */ - const gchar *snoop_mime_type; - - /* For validity enveloping. */ - CamelCipherValidity *valid; - CamelCipherValidity *valid_parent; - - /* For checking whether we found any signed or encrypted parts. */ - guint32 validity_found; - - /* For forcing inlining. */ - GHashTable *inline_table; - - /* Global URI lookup table for message. */ - GHashTable *pending_uri_table; - - /* This structure is used internally to form a visibility tree of - * parts in the current formatting stream. This is to implement the - * part resolution rules for RFC2387 to implement multipart/related. */ - GNode *pending_uri_tree; - - /* The current level to search from. */ - GNode *pending_uri_level; - - EMFormatMode mode; /* source/headers/etc */ - gchar *charset; /* charset override */ - gchar *default_charset; /* charset fallback */ - gboolean composer; /* formatting from composer? */ - gboolean print; /* formatting for printing? */ }; struct _EMFormatClass { @@ -246,187 +188,161 @@ struct _EMFormatClass { GHashTable *type_handlers; - /* lookup handler, default falls back to hashtable above */ - const EMFormatHandler * - (*find_handler) (EMFormat *emf, - const gchar *mime_type); - - /* start formatting a message */ - void (*format_clone) (EMFormat *emf, - CamelFolder *folder, - const gchar *uid, - CamelMimeMessage *message, - EMFormat *source, - GCancellable *cancellable); - - /* some internel error/inconsistency */ - void (*format_error) (EMFormat *emf, - CamelStream *stream, - const gchar *errmsg); - - /* use for external structured parts */ - void (*format_attachment) (EMFormat *emf, - CamelStream *stream, - CamelMimePart *mime_part, - const gchar *mime_type, - const EMFormatHandler *info, - GCancellable *cancellable); + gboolean (*is_inline) (EMFormat *emf, + const gchar *part_id, + CamelMimePart *part, + const EMFormatHandler *handler); - /* use for unparsable content */ - void (*format_source) (EMFormat *emf, - CamelStream *stream, - CamelMimePart *mime_part, - GCancellable *cancellable); - /* for outputing secure(d) content */ - void (*format_secure) (EMFormat *emf, - CamelStream *stream, - CamelMimePart *mime_part, - CamelCipherValidity *validity, - GCancellable *cancellable); + /* Write the entire message to stream */ + void (*write) (EMFormat *emf, + CamelStream *stream, + EMFormatWriterInfo *info, + GCancellable *cancellable); - /* returns true if the formatter is still busy with pending stuff */ - gboolean (*busy) (EMFormat *); - - /* Shows optional way to open messages */ - void (*format_optional) (EMFormat *emf, - CamelStream *filter_stream, - CamelMimePart *mime_part, - CamelStream *mem_stream, - GCancellable *cancellable); - - gboolean (*is_inline) (EMFormat *emf, - const gchar *part_id, - CamelMimePart *mime_part, - const EMFormatHandler *handle); + void (*preparse) (EMFormat *emf); /* signals */ - /* complete, alternative to polling busy, for asynchronous work */ - void (*complete) (EMFormat *emf); -}; + void (*redraw_requested) (EMFormat *emf); -void em_format_set_mode (EMFormat *emf, - EMFormatMode mode); -void em_format_set_charset (EMFormat *emf, - const gchar *charset); -void em_format_set_default_charset (EMFormat *emf, - const gchar *charset); - -/* also indicates to show all headers */ -void em_format_clear_headers (EMFormat *emf); - -void em_format_default_headers (EMFormat *emf); -void em_format_add_header (EMFormat *emf, - const gchar *name, - guint32 flags); - -/* FIXME: Need a 'clone' api to copy details about the current view (inlines etc) - * Or maybe it should live with sub-classes? */ - -gint em_format_is_attachment (EMFormat *emf, - CamelMimePart *part); - -gboolean em_format_is_inline (EMFormat *emf, - const gchar *part_id, - CamelMimePart *mime_part, - const EMFormatHandler *handle); -void em_format_set_inline (EMFormat *emf, - const gchar *partid, - gint state); - -gchar * em_format_describe_part (CamelMimePart *part, - const gchar *mime_type); - -/* for implementers */ -GType em_format_get_type (void); - -void em_format_class_add_handler (EMFormatClass *emfc, - EMFormatHandler *info); -void em_format_class_remove_handler (EMFormatClass *emfc, - EMFormatHandler *info); -const EMFormatHandler * - em_format_find_handler (EMFormat *emf, - const gchar *mime_type); -const EMFormatHandler * - em_format_fallback_handler (EMFormat *emf, - const gchar *mime_type); - -/* puri is short for pending uri ... really */ -EMFormatPURI * em_format_add_puri (EMFormat *emf, - gsize size, - const gchar *uri, - CamelMimePart *part, - EMFormatPURIFunc func); -EMFormatPURI * em_format_find_visible_puri (EMFormat *emf, - const gchar *uri); -EMFormatPURI * em_format_find_puri (EMFormat *emf, - const gchar *uri); -void em_format_clear_puri_tree (EMFormat *emf); -void em_format_push_level (EMFormat *emf); -void em_format_pull_level (EMFormat *emf); - -/* clones inline state/view and format, or use to redraw */ -void em_format_format_clone (EMFormat *emf, - CamelFolder *folder, - const gchar *uid, - CamelMimeMessage *message, - EMFormat *source, - GCancellable *cancellable); - -/* formats a new message */ -void em_format_format (EMFormat *emf, - CamelFolder *folder, - const gchar *uid, - CamelMimeMessage *message, - GCancellable *cancellable); -void em_format_queue_redraw (EMFormat *emf); -void em_format_format_attachment (EMFormat *emf, - CamelStream *stream, - CamelMimePart *mime_part, - const gchar *mime_type, - const EMFormatHandler *info, - GCancellable *cancellable); -void em_format_format_error (EMFormat *emf, - CamelStream *stream, - const gchar *format, - ...) G_GNUC_PRINTF (3, 4); -void em_format_format_secure (EMFormat *emf, - CamelStream *stream, - CamelMimePart *mime_part, - CamelCipherValidity *valid, - GCancellable *cancellable); -void em_format_format_source (EMFormat *emf, - CamelStream *stream, - CamelMimePart *mime_part, - GCancellable *cancellable); - -gboolean em_format_busy (EMFormat *emf); - -/* raw content only */ -void em_format_format_content (EMFormat *emf, - CamelStream *stream, - CamelMimePart *part, - GCancellable *cancellable); - -/* raw content text parts - should this just be checked/done by above? */ -void em_format_format_text (EMFormat *emf, - CamelStream *stream, - CamelDataWrapper *part, - GCancellable *cancellable); - -void em_format_part_as (EMFormat *emf, - CamelStream *stream, - CamelMimePart *part, - const gchar *mime_type, - GCancellable *cancellable); -void em_format_part (EMFormat *emf, - CamelStream *stream, - CamelMimePart *mime_part, - GCancellable *cancellable); -void em_format_merge_handler (EMFormat *new, - EMFormat *old); - -const gchar * em_format_snoop_type (CamelMimePart *part); +}; -G_END_DECLS +EMFormat * em_format_new (void); + +GType em_format_get_type (void); + +void em_format_set_charset (EMFormat *emf, + const gchar *charset); +const gchar * em_format_get_charset (EMFormat *emf); + +void em_format_set_default_charset (EMFormat *emf, + const gchar *charset); +const gchar * em_format_get_default_charset (EMFormat *emf); + +void em_format_set_composer (EMFormat *emf, + gboolean composer); +gboolean em_format_get_composer (EMFormat *emf); + +void em_format_set_base_url (EMFormat *emf, + CamelURL *url); +void em_format_set_base_url_string (EMFormat *emf, + const gchar *url_string); +CamelURL * em_format_get_base_url (EMFormat *emf); + +void em_format_clear_headers (EMFormat *emf); + +void em_format_default_headers (EMFormat *emf); + +void em_format_add_header (EMFormat *emf, + const gchar *name, + const gchar *value, + guint32 flags); +void em_format_add_header_struct (EMFormat *emf, + EMFormatHeader *header); +void em_format_remove_header (EMFormat *emf, + const gchar *name, + const gchar *value); +void em_format_remove_header_struct (EMFormat *emf, + const EMFormatHeader *header); + +void em_format_add_puri (EMFormat *emf, + EMFormatPURI *puri); +EMFormatPURI * em_format_find_puri (EMFormat *emf, + const gchar *id); + +void em_format_class_add_handler (EMFormatClass *emfc, + EMFormatHandler *handler); +void em_format_class_remove_handler (EMFormatClass *emfc, + EMFormatHandler *handler); + +const EMFormatHandler * em_format_find_handler (EMFormat *emf, + const gchar *mime_type); +const EMFormatHandler * em_format_fallback_handler (EMFormat *emf, + const gchar *mime_type); + +void em_format_parse (EMFormat *emf, + CamelMimeMessage *message, + CamelFolder *folder, + GCancellable *cancellable); + +void em_format_write (EMFormat *emf, + CamelStream *stream, + EMFormatWriterInfo *info, + GCancellable *cancellable); + +void em_format_parse_async (EMFormat *emf, + CamelMimeMessage *message, + CamelFolder *folder, + GCancellable *cancellable, + GAsyncReadyCallback callback, + gpointer user_data); + +void em_format_parse_part (EMFormat *emf, + CamelMimePart *part, + GString *part_id, + EMFormatParserInfo *info, + GCancellable *cancellable); +void em_format_parse_part_as (EMFormat *emf, + CamelMimePart *part, + GString *part_id, + EMFormatParserInfo *info, + const gchar *mime_type, + GCancellable *cancellable); +gboolean em_format_is_inline (EMFormat *emf, + const gchar *part_id, + CamelMimePart *part, + const EMFormatHandler *handler); + +gchar * em_format_get_error_id (EMFormat *emf); + +void em_format_format_error (EMFormat *emf, + const gchar *format, + ...) G_GNUC_PRINTF (2, 3); +void em_format_format_text (EMFormat *emf, + CamelStream *stream, + CamelDataWrapper *dw, + GCancellable *cancellable); +gchar * em_format_describe_part (CamelMimePart *part, + const gchar *mime_type); +gint em_format_is_attachment (EMFormat *emf, + CamelMimePart *part); +const gchar * em_format_snoop_type (CamelMimePart *part); + +gchar * em_format_build_mail_uri (CamelFolder *folder, + const gchar *message_uid, + const gchar *part_uid, + ...) G_GNUC_NULL_TERMINATED; + +/* EMFormatParseFunc that does nothing. Use it to disable + * parsing of a specific mime type parts */ +void em_format_empty_parser (EMFormat *emf, + CamelMimePart *part, + GString *part_id, + EMFormatParserInfo *info, + GCancellable *cancellable); + +/* EMFormatWriteFunc that does nothing. Use it to disable + * writing of a specific mime type parts */ +void em_format_empty_writer (EMFormat *emf, + EMFormatPURI *puri, + CamelStream *stream, + EMFormatWriterInfo *info, + GCancellable *cancellable); + +void em_format_redraw (EMFormat *emf); + +EMFormatPURI * em_format_puri_new (EMFormat *emf, + gsize puri_size, + CamelMimePart *part, + const gchar *uri); +void em_format_puri_free (EMFormatPURI *puri); + +void em_format_puri_write (EMFormatPURI *puri, + CamelStream *stream, + EMFormatWriterInfo *info, + GCancellable *cancellable); + +EMFormatHeader * em_format_header_new (const gchar *name, + const gchar *value); +void em_format_header_free (EMFormatHeader *header); #endif /* EM_FORMAT_H */ diff --git a/mail/Makefile.am b/mail/Makefile.am index 3a13284668..e6eccc83fe 100644 --- a/mail/Makefile.am +++ b/mail/Makefile.am @@ -22,7 +22,9 @@ libevolution_mail_la_CPPFLAGS = \ $(CERT_UI_CFLAGS) \ $(CANBERRA_CFLAGS) \ $(CLUTTER_CFLAGS) \ - $(GTKHTML_CFLAGS) \ + $(GTKHTML_CFLAGS) \ + $(JAVASCRIPTCORE_CFLAGS) \ + $(LIBSOUP_CFLAGS) \ -DEVOLUTION_DATADIR=\""$(datadir)"\" \ -DEVOLUTION_PRIVDATADIR=\""$(privdatadir)"\" \ -DEVOLUTION_ETSPECDIR=\""$(etspecdir)"\" \ @@ -56,11 +58,13 @@ mailinclude_HEADERS = \ e-mail-migrate.h \ e-mail-notebook-view.h \ e-mail-paned-view.h \ + e-mail-printer.h \ e-mail-reader-utils.h \ e-mail-reader.h \ - e-mail-ui-session.h \ + e-mail-request.h \ e-mail-sidebar.h \ e-mail-tag-editor.h \ + e-mail-ui-session.h \ e-mail-view.h \ em-account-editor.h \ em-composer-utils.h \ @@ -81,7 +85,6 @@ mailinclude_HEADERS = \ em-format-html-display.h \ em-format-html-print.h \ em-format-html.h \ - em-html-stream.h \ em-search-context.h \ em-subscription-editor.h \ em-sync-stream.h \ @@ -121,11 +124,13 @@ libevolution_mail_la_SOURCES = \ e-mail-migrate.c \ e-mail-notebook-view.c \ e-mail-paned-view.c \ + e-mail-printer.c \ e-mail-reader-utils.c \ e-mail-reader.c \ - e-mail-ui-session.c \ + e-mail-request.c \ e-mail-sidebar.c \ e-mail-tag-editor.c \ + e-mail-ui-session.c \ e-mail-view.c \ em-account-editor.c \ em-composer-utils.c \ @@ -146,7 +151,6 @@ libevolution_mail_la_SOURCES = \ em-format-html-display.c \ em-format-html-print.c \ em-format-html.c \ - em-html-stream.c \ em-search-context.c \ em-subscription-editor.c \ em-sync-stream.c \ @@ -194,7 +198,11 @@ libevolution_mail_la_LIBADD = \ $(CANBERRA_LIBS) \ $(CLUTTER_LIBS) \ $(GTKHTML_LIBS) \ - $(SMIME_LIBS) + $(JAVASCRIPTCORE_CFLAGS) \ + $(E_WIDGETS_LIBS) \ + $(SMIME_LIBS) \ + $(LIBSOUP_LIBS) \ + $(GNOME_PLATFORM_LIBS) libevolution_mail_la_LDFLAGS = -avoid-version $(NO_UNDEFINED) diff --git a/mail/e-mail-attachment-bar.c b/mail/e-mail-attachment-bar.c index 21a298c56d..7572c664cb 100644 --- a/mail/e-mail-attachment-bar.c +++ b/mail/e-mail-attachment-bar.c @@ -60,7 +60,8 @@ enum { PROP_ACTIVE_VIEW, PROP_DRAGGING, PROP_EDITABLE, - PROP_EXPANDED + PROP_EXPANDED, + PROP_STORE }; /* Forward Declarations */ @@ -78,7 +79,6 @@ G_DEFINE_TYPE_WITH_CODE ( static void mail_attachment_bar_update_status (EMailAttachmentBar *bar) { - EAttachmentView *view; EAttachmentStore *store; GtkActivatable *activatable; GtkAction *action; @@ -88,8 +88,7 @@ mail_attachment_bar_update_status (EMailAttachmentBar *bar) gchar *display_size; gchar *markup; - view = E_ATTACHMENT_VIEW (bar); - store = e_attachment_view_get_store (view); + store = E_ATTACHMENT_STORE (bar->priv->model); label = GTK_LABEL (bar->priv->status_label); num_attachments = e_attachment_store_get_num_attachments (store); @@ -119,6 +118,31 @@ mail_attachment_bar_update_status (EMailAttachmentBar *bar) g_free (display_size); } +static void +mail_attachment_bar_set_store (EMailAttachmentBar *bar, + EAttachmentStore *store) +{ + g_return_if_fail (E_IS_ATTACHMENT_STORE (store)); + + bar->priv->model = g_object_ref (store); + + gtk_icon_view_set_model (GTK_ICON_VIEW (bar->priv->icon_view), + bar->priv->model); + gtk_tree_view_set_model (GTK_TREE_VIEW (bar->priv->tree_view), + bar->priv->model); + + g_signal_connect_swapped ( + bar->priv->model, "notify::num-attachments", + G_CALLBACK (mail_attachment_bar_update_status), bar); + + g_signal_connect_swapped ( + bar->priv->model, "notify::total-size", + G_CALLBACK (mail_attachment_bar_update_status), bar); + + /* Initialize */ + mail_attachment_bar_update_status (bar); +} + static void mail_attachment_bar_set_property (GObject *object, guint property_id, @@ -127,7 +151,7 @@ mail_attachment_bar_set_property (GObject *object, { switch (property_id) { case PROP_ACTIVE_VIEW: - e_mail_attachment_bar_set_active_view ( + e_mail_attachment_bar_set_active_view ( E_MAIL_ATTACHMENT_BAR (object), g_value_get_int (value)); return; @@ -149,6 +173,11 @@ mail_attachment_bar_set_property (GObject *object, E_MAIL_ATTACHMENT_BAR (object), g_value_get_boolean (value)); return; + case PROP_STORE: + mail_attachment_bar_set_store ( + E_MAIL_ATTACHMENT_BAR (object), + g_value_get_object (value)); + return; } G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec); @@ -188,6 +217,11 @@ mail_attachment_bar_get_property (GObject *object, e_mail_attachment_bar_get_expanded ( E_MAIL_ATTACHMENT_BAR (object))); return; + case PROP_STORE: + g_value_set_object ( + value, + e_mail_attachment_bar_get_store ( + E_MAIL_ATTACHMENT_BAR (object))); } G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec); @@ -201,8 +235,6 @@ mail_attachment_bar_dispose (GObject *object) priv = E_MAIL_ATTACHMENT_BAR_GET_PRIVATE (object); if (priv->model != NULL) { - e_attachment_store_remove_all ( - E_ATTACHMENT_STORE (priv->model)); g_object_unref (priv->model); priv->model = NULL; } @@ -347,17 +379,6 @@ mail_attachment_bar_get_private (EAttachmentView *view) return e_attachment_view_get_private (view); } -static EAttachmentStore * -mail_attachment_bar_get_store (EAttachmentView *view) -{ - EMailAttachmentBar *bar; - - bar = E_MAIL_ATTACHMENT_BAR (view); - view = E_ATTACHMENT_VIEW (bar->priv->icon_view); - - return e_attachment_view_get_store (view); -} - static GtkTreePath * mail_attachment_bar_get_path_at_pos (EAttachmentView *view, gint x, @@ -488,6 +509,17 @@ e_mail_attachment_bar_class_init (EMailAttachmentBarClass *class) G_PARAM_READWRITE | G_PARAM_CONSTRUCT)); + g_object_class_install_property ( + object_class, + PROP_STORE, + g_param_spec_object ( + "store", + "Attachment Store", + NULL, + E_TYPE_ATTACHMENT_STORE, + G_PARAM_READWRITE | + G_PARAM_CONSTRUCT_ONLY)); + g_object_class_override_property ( object_class, PROP_DRAGGING, "dragging"); @@ -499,7 +531,7 @@ static void e_mail_attachment_bar_interface_init (EAttachmentViewInterface *interface) { interface->get_private = mail_attachment_bar_get_private; - interface->get_store = mail_attachment_bar_get_store; + interface->get_store = e_mail_attachment_bar_get_store; interface->get_path_at_pos = mail_attachment_bar_get_path_at_pos; interface->get_selected_paths = mail_attachment_bar_get_selected_paths; interface->path_is_selected = mail_attachment_bar_path_is_selected; @@ -520,7 +552,6 @@ e_mail_attachment_bar_init (EMailAttachmentBar *bar) GtkAction *action; bar->priv = E_MAIL_ATTACHMENT_BAR_GET_PRIVATE (bar); - bar->priv->model = e_attachment_store_new (); gtk_box_set_spacing (GTK_BOX (bar), 6); @@ -644,23 +675,18 @@ e_mail_attachment_bar_init (EMailAttachmentBar *bar) bar->priv->status_label = g_object_ref (widget); gtk_widget_show (widget); - g_signal_connect_swapped ( - bar->priv->model, "notify::num-attachments", - G_CALLBACK (mail_attachment_bar_update_status), bar); - - g_signal_connect_swapped ( - bar->priv->model, "notify::total-size", - G_CALLBACK (mail_attachment_bar_update_status), bar); - g_object_unref (size_group); } GtkWidget * -e_mail_attachment_bar_new (void) +e_mail_attachment_bar_new (EAttachmentStore *store) { + g_return_val_if_fail (E_IS_ATTACHMENT_STORE (store), NULL); + return g_object_new ( E_TYPE_MAIL_ATTACHMENT_BAR, - "editable", FALSE, NULL); + "editable", FALSE, + "store", store, NULL); } gint @@ -729,3 +755,11 @@ e_mail_attachment_bar_set_expanded (EMailAttachmentBar *bar, g_object_notify (G_OBJECT (bar), "expanded"); } + +EAttachmentStore * +e_mail_attachment_bar_get_store (EMailAttachmentBar *bar) +{ + g_return_val_if_fail (E_IS_MAIL_ATTACHMENT_BAR (bar), NULL); + + return E_ATTACHMENT_STORE (bar->priv->model); +} diff --git a/mail/e-mail-attachment-bar.h b/mail/e-mail-attachment-bar.h index 93c1b89261..b83d9733e0 100644 --- a/mail/e-mail-attachment-bar.h +++ b/mail/e-mail-attachment-bar.h @@ -60,7 +60,7 @@ struct _EMailAttachmentBarClass { }; GType e_mail_attachment_bar_get_type (void); -GtkWidget * e_mail_attachment_bar_new (void); +GtkWidget * e_mail_attachment_bar_new (EAttachmentStore *store); gint e_mail_attachment_bar_get_active_view (EMailAttachmentBar *bar); void e_mail_attachment_bar_set_active_view @@ -71,6 +71,8 @@ gboolean e_mail_attachment_bar_get_expanded void e_mail_attachment_bar_set_expanded (EMailAttachmentBar *bar, gboolean expanded); +EAttachmentStore * + e_mail_attachment_bar_get_store (EMailAttachmentBar *bar); G_END_DECLS diff --git a/mail/e-mail-browser.c b/mail/e-mail-browser.c index 0dbb3d01e3..806980d602 100644 --- a/mail/e-mail-browser.c +++ b/mail/e-mail-browser.c @@ -55,7 +55,8 @@ struct _EMailBrowserPrivate { EMailBackend *backend; GtkUIManager *ui_manager; EFocusTracker *focus_tracker; - EMFormatHTMLDisplay *formatter; + + EMFormatWriteMode mode; GtkWidget *main_menu; GtkWidget *main_toolbar; @@ -74,7 +75,8 @@ enum { PROP_GROUP_BY_THREADS, PROP_SHOW_DELETED, PROP_REPLY_STYLE, - PROP_UI_MANAGER + PROP_UI_MANAGER, + PROP_DISPLAY_MODE, }; static gpointer parent_class; @@ -260,11 +262,10 @@ static void mail_browser_message_selected_cb (EMailBrowser *browser, const gchar *uid) { - EMFormatHTML *formatter; CamelMessageInfo *info; CamelFolder *folder; EMailReader *reader; - EWebView *web_view; + EMailDisplay *display; const gchar *title; guint32 state; @@ -276,8 +277,7 @@ mail_browser_message_selected_cb (EMailBrowser *browser, return; folder = e_mail_reader_get_folder (reader); - formatter = e_mail_reader_get_formatter (reader); - web_view = em_format_html_get_web_view (formatter); + display = e_mail_reader_get_mail_display (reader); info = camel_folder_get_message_info (folder, uid); @@ -289,7 +289,7 @@ mail_browser_message_selected_cb (EMailBrowser *browser, title = _("(No Subject)"); gtk_window_set_title (GTK_WINDOW (browser), title); - gtk_widget_grab_focus (GTK_WIDGET (web_view)); + gtk_widget_grab_focus (GTK_WIDGET (display)); camel_message_info_set_flags ( info, CAMEL_MESSAGE_SEEN, CAMEL_MESSAGE_SEEN); @@ -319,7 +319,6 @@ mail_browser_popup_event_cb (EMailBrowser *browser, GdkEventButton *event, const gchar *uri) { - EMFormatHTML *formatter; EMailReader *reader; EWebView *web_view; GtkMenu *menu; @@ -329,8 +328,7 @@ mail_browser_popup_event_cb (EMailBrowser *browser, return FALSE; reader = E_MAIL_READER (browser); - formatter = e_mail_reader_get_formatter (reader); - web_view = em_format_html_get_web_view (formatter); + web_view = E_WEB_VIEW (e_mail_reader_get_mail_display (reader)); if (e_web_view_get_cursor_image (web_view) != NULL) return FALSE; @@ -415,6 +413,11 @@ mail_browser_set_property (GObject *object, E_MAIL_BROWSER (object), g_value_get_boolean (value)); return; + + case PROP_DISPLAY_MODE: + E_MAIL_BROWSER (object)->priv->mode = + g_value_get_int (value); + return; } G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec); @@ -468,6 +471,11 @@ mail_browser_get_property (GObject *object, value, e_mail_browser_get_ui_manager ( E_MAIL_BROWSER (object))); return; + + case PROP_DISPLAY_MODE: + g_value_set_int ( + value, E_MAIL_BROWSER (object)->priv->mode); + return; } G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec); @@ -495,11 +503,6 @@ mail_browser_dispose (GObject *object) priv->focus_tracker = NULL; } - if (priv->formatter != NULL) { - g_object_unref (priv->formatter); - priv->formatter = NULL; - } - if (priv->main_menu != NULL) { g_object_unref (priv->main_menu); priv->main_menu = NULL; @@ -534,14 +537,13 @@ static void mail_browser_constructed (GObject *object) { EMailBrowser *browser; - EMFormatHTML *formatter; EMailReader *reader; EMailBackend *backend; EMailSession *session; + EMailDisplay *display; EShellBackend *shell_backend; EShell *shell; EFocusTracker *focus_tracker; - ESearchBar *search_bar; GSettings *settings; GtkAccelGroup *accel_group; GtkActionGroup *action_group; @@ -549,7 +551,6 @@ mail_browser_constructed (GObject *object) GtkUIManager *ui_manager; GtkWidget *container; GtkWidget *widget; - EWebView *web_view; const gchar *domain; const gchar *id; guint merge_id; @@ -558,7 +559,6 @@ mail_browser_constructed (GObject *object) G_OBJECT_CLASS (parent_class)->constructed (object); browser = E_MAIL_BROWSER (object); - reader = E_MAIL_READER (object); backend = e_mail_reader_get_backend (reader); session = e_mail_backend_get_session (backend); @@ -575,9 +575,6 @@ mail_browser_constructed (GObject *object) gtk_application_add_window ( GTK_APPLICATION (shell), GTK_WINDOW (object)); - formatter = e_mail_reader_get_formatter (reader); - web_view = em_format_html_get_web_view (formatter); - /* The message list is a widget, but it is not shown in the browser. * Unfortunately, the widget is inseparable from its model, and the * model is all we need. */ @@ -592,15 +589,20 @@ mail_browser_constructed (GObject *object) browser->priv->message_list, "message-list-built", G_CALLBACK (mail_browser_message_list_built_cb), object); + display = g_object_new (E_TYPE_MAIL_DISPLAY, + "mode", E_MAIL_BROWSER (object)->priv->mode, NULL); + g_signal_connect_swapped ( - web_view, "popup-event", + display, "popup-event", G_CALLBACK (mail_browser_popup_event_cb), object); g_signal_connect_swapped ( - web_view, "status-message", + display, "status-message", G_CALLBACK (mail_browser_status_message_cb), object); - /* Add action groups before initializing the reader interface. */ + widget = e_preview_pane_new (E_WEB_VIEW (display)); + browser->priv->preview_pane = g_object_ref (widget); + gtk_widget_show (widget); action_group = gtk_action_group_new (ACTION_GROUP_STANDARD); gtk_action_group_set_translation_domain (action_group, domain); @@ -613,6 +615,7 @@ mail_browser_constructed (GObject *object) gtk_ui_manager_insert_action_group (ui_manager, action_group, 0); /* For easy access. Takes ownership of the reference. */ + g_object_set_data_full ( object, ACTION_GROUP_STANDARD, action_group, (GDestroyNotify) g_object_unref); @@ -664,7 +667,6 @@ mail_browser_constructed (GObject *object) container = widget; - /* Create the status bar before connecting proxy widgets. */ widget = gtk_statusbar_new (); gtk_box_pack_end (GTK_BOX (container), widget, FALSE, FALSE, 0); browser->priv->statusbar = g_object_ref (widget); @@ -682,21 +684,9 @@ mail_browser_constructed (GObject *object) gtk_style_context_add_class ( gtk_widget_get_style_context (widget), - GTK_STYLE_CLASS_PRIMARY_TOOLBAR); - - gtk_widget_show (GTK_WIDGET (web_view)); + GTK_STYLE_CLASS_PRIMARY_TOOLBAR); - widget = e_preview_pane_new (web_view); - gtk_box_pack_start (GTK_BOX (container), widget, TRUE, TRUE, 0); - browser->priv->preview_pane = g_object_ref (widget); - gtk_widget_show (widget); - - search_bar = e_preview_pane_get_search_bar (E_PREVIEW_PANE (widget)); - - g_signal_connect_swapped ( - search_bar, "changed", - G_CALLBACK (em_format_queue_redraw), - browser->priv->formatter); + gtk_container_add (GTK_CONTAINER (container), browser->priv->preview_pane); /* Bind GObject properties to GSettings keys. */ @@ -713,8 +703,6 @@ mail_browser_constructed (GObject *object) e_plugin_ui_register_manager (ui_manager, id, object); e_plugin_ui_enable_manager (ui_manager, id); - e_mail_reader_connect_headers (E_MAIL_READER (reader)); - e_extensible_load_extensions (E_EXTENSIBLE (object)); } @@ -772,14 +760,15 @@ mail_browser_get_hide_deleted (EMailReader *reader) return !e_mail_browser_get_show_deleted (browser); } -static EMFormatHTML * -mail_browser_get_formatter (EMailReader *reader) +static EMailDisplay * +mail_browser_get_mail_display (EMailReader *reader) { - EMailBrowser *browser; + EMailBrowserPrivate *priv; - browser = E_MAIL_BROWSER (reader); + priv = E_MAIL_BROWSER_GET_PRIVATE (E_MAIL_BROWSER (reader)); - return EM_FORMAT_HTML (browser->priv->formatter); + return E_MAIL_DISPLAY (e_preview_pane_get_web_view ( + E_PREVIEW_PANE (priv->preview_pane))); } static GtkWidget * @@ -916,6 +905,18 @@ e_mail_browser_class_init (EMailBrowserClass *class) "Show deleted messages", FALSE, G_PARAM_READWRITE)); + + g_object_class_install_property ( + object_class, + PROP_DISPLAY_MODE, + g_param_spec_int ( + "display-mode", + "Display Mode", + NULL, + 0, + G_MAXINT, + EM_FORMAT_WRITE_MODE_NORMAL, + G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY)); } static void @@ -923,7 +924,7 @@ e_mail_browser_reader_init (EMailReaderInterface *interface) { interface->get_action_group = mail_browser_get_action_group; interface->get_backend = mail_browser_get_backend; - interface->get_formatter = mail_browser_get_formatter; + interface->get_mail_display = mail_browser_get_mail_display; interface->get_hide_deleted = mail_browser_get_hide_deleted; interface->get_message_list = mail_browser_get_message_list; interface->get_popup_menu = mail_browser_get_popup_menu; @@ -936,7 +937,6 @@ static void e_mail_browser_init (EMailBrowser *browser) { browser->priv = E_MAIL_BROWSER_GET_PRIVATE (browser); - browser->priv->formatter = em_format_html_display_new (); gtk_window_set_title (GTK_WINDOW (browser), _("Evolution")); gtk_window_set_default_size (GTK_WINDOW (browser), 600, 400); @@ -948,13 +948,22 @@ e_mail_browser_init (EMailBrowser *browser) } GtkWidget * -e_mail_browser_new (EMailBackend *backend) +e_mail_browser_new (EMailBackend *backend, + CamelFolder *folder, + const gchar *msg_uid, + EMFormatWriteMode mode) { + GtkWidget *widget; + g_return_val_if_fail (E_IS_MAIL_BACKEND (backend), NULL); - return g_object_new ( + widget= g_object_new ( E_TYPE_MAIL_BROWSER, - "backend", backend, NULL); + "backend", backend, + "display-mode", mode, + NULL); + + return widget; } void diff --git a/mail/e-mail-browser.h b/mail/e-mail-browser.h index c09c85b1c8..88f8174a9d 100644 --- a/mail/e-mail-browser.h +++ b/mail/e-mail-browser.h @@ -24,6 +24,7 @@ #include #include +#include /* Standard GObject macros */ #define E_TYPE_MAIL_BROWSER \ @@ -60,7 +61,10 @@ struct _EMailBrowserClass { }; GType e_mail_browser_get_type (void); -GtkWidget * e_mail_browser_new (EMailBackend *backend); +GtkWidget * e_mail_browser_new (EMailBackend *backend, + CamelFolder *folder, + const gchar *message_uid, + EMFormatWriteMode mode); void e_mail_browser_close (EMailBrowser *browser); gboolean e_mail_browser_get_show_deleted (EMailBrowser *browser); void e_mail_browser_set_show_deleted (EMailBrowser *browser, diff --git a/mail/e-mail-display.c b/mail/e-mail-display.c index 7461e595f3..07f45ad461 100644 --- a/mail/e-mail-display.c +++ b/mail/e-mail-display.c @@ -23,14 +23,33 @@ #include #endif +#define LIBSOUP_USE_UNSTABLE_REQUEST_API + #include "e-mail-display.h" #include +#include +#include "e-util/e-marshal.h" #include "e-util/e-util.h" #include "e-util/e-plugin-ui.h" #include "mail/em-composer-utils.h" #include "mail/em-utils.h" +#include "mail/e-mail-request.h" +#include "mail/em-format-html-display.h" +#include "mail/e-mail-attachment-bar.h" +#include "widgets/misc/e-attachment-button.h" + +#include + +#include +#include + +#include + +#define d(x) + +G_DEFINE_TYPE (EMailDisplay, e_mail_display, E_TYPE_WEB_VIEW) #define E_MAIL_DISPLAY_GET_PRIVATE(obj) \ (G_TYPE_INSTANCE_GET_PRIVATE \ @@ -38,13 +57,29 @@ struct _EMailDisplayPrivate { EMFormatHTML *formatter; + + EMFormatWriteMode mode; + gboolean headers_collapsable; + gboolean headers_collapsed; + + GtkActionGroup *mailto_actions; + GtkActionGroup *images_actions; + + gint force_image_load: 1; }; enum { PROP_0, - PROP_FORMATTER + PROP_FORMATTER, + PROP_MODE, + PROP_HEADERS_COLLAPSABLE, + PROP_HEADERS_COLLAPSED, }; +static gpointer parent_class; + +static CamelDataCache *emd_global_http_cache = 0; + static const gchar *ui = "" " " @@ -61,6 +96,15 @@ static const gchar *ui = " " ""; +static const gchar *image_ui = +"" +" " +" " +" " +" " +" " +""; + static GtkActionEntry mailto_entries[] = { { "add-to-address-book", @@ -88,7 +132,7 @@ static GtkActionEntry mailto_entries[] = { NULL, N_("Send _Reply To..."), NULL, - N_("Send a reply message to this address"), + N_("Send a reply message to this address"), NULL /* Handled by EMailReader */ }, /*** Menus ***/ @@ -101,7 +145,54 @@ static GtkActionEntry mailto_entries[] = { NULL } }; -G_DEFINE_TYPE (EMailDisplay, e_mail_display, E_TYPE_WEB_VIEW) +static GtkActionEntry image_entries[] = { + + { "image-save", + GTK_STOCK_SAVE, + N_("Save _Image..."), + NULL, + N_("Save the image to a file"), + NULL /* Handled by EMailReader */ }, + +}; + +static void +mail_display_webview_update_actions (EWebView *web_view, + gpointer user_data) +{ + const gchar *image_src; + gboolean visible; + GtkAction *action; + + g_return_if_fail (web_view != NULL); + + image_src = e_web_view_get_cursor_image_src (web_view); + visible = image_src && g_str_has_prefix (image_src, "cid:"); + if (!visible && image_src) { + CamelStream *image_stream; + + image_stream = camel_data_cache_get (emd_global_http_cache, "http", image_src, NULL); + + visible = image_stream != NULL; + + if (image_stream) + g_object_unref (image_stream); + } + + action = e_web_view_get_action (web_view, "image-save"); + if (action) + gtk_action_set_visible (action, visible); +} + +static void +formatter_image_loading_policy_changed_cb (GObject *object, + GParamSpec *pspec, + gpointer user_data) +{ + EMailDisplay *display = user_data; + + e_mail_display_load_images (display); +} static void mail_display_update_formatter_colors (EMailDisplay *display) @@ -115,6 +206,9 @@ mail_display_update_formatter_colors (EMailDisplay *display) state = gtk_widget_get_state (GTK_WIDGET (display)); formatter = display->priv->formatter; + if (!display->priv->formatter) + return; + style = gtk_widget_get_style (GTK_WIDGET (display)); if (style == NULL) return; @@ -156,6 +250,21 @@ mail_display_set_property (GObject *object, E_MAIL_DISPLAY (object), g_value_get_object (value)); return; + case PROP_MODE: + e_mail_display_set_mode ( + E_MAIL_DISPLAY (object), + g_value_get_int (value)); + return; + case PROP_HEADERS_COLLAPSABLE: + e_mail_display_set_headers_collapsable ( + E_MAIL_DISPLAY (object), + g_value_get_boolean (value)); + return; + case PROP_HEADERS_COLLAPSED: + e_mail_display_set_headers_collapsed ( + E_MAIL_DISPLAY (object), + g_value_get_boolean (value)); + return; } G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec); @@ -173,6 +282,21 @@ mail_display_get_property (GObject *object, value, e_mail_display_get_formatter ( E_MAIL_DISPLAY (object))); return; + case PROP_MODE: + g_value_set_int ( + value, e_mail_display_get_mode ( + E_MAIL_DISPLAY (object))); + return; + case PROP_HEADERS_COLLAPSABLE: + g_value_set_boolean ( + value, e_mail_display_get_headers_collapsable ( + E_MAIL_DISPLAY (object))); + return; + case PROP_HEADERS_COLLAPSED: + g_value_set_boolean ( + value, e_mail_display_get_headers_collapsed ( + E_MAIL_DISPLAY (object))); + return; } G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec); @@ -191,14 +315,14 @@ mail_display_dispose (GObject *object) } /* Chain up to parent's dispose() method. */ - G_OBJECT_CLASS (e_mail_display_parent_class)->dispose (object); + G_OBJECT_CLASS (parent_class)->dispose (object); } static void mail_display_realize (GtkWidget *widget) { /* Chain up to parent's realize() method. */ - GTK_WIDGET_CLASS (e_mail_display_parent_class)->realize (widget); + GTK_WIDGET_CLASS (parent_class)->realize (widget); mail_display_update_formatter_colors (E_MAIL_DISPLAY (widget)); } @@ -207,62 +331,28 @@ static void mail_display_style_set (GtkWidget *widget, GtkStyle *previous_style) { - EMailDisplayPrivate *priv; + EMailDisplay *display = E_MAIL_DISPLAY (widget); - priv = E_MAIL_DISPLAY_GET_PRIVATE (widget); + mail_display_update_formatter_colors (display); /* Chain up to parent's style_set() method. */ - GTK_WIDGET_CLASS (e_mail_display_parent_class)-> - style_set (widget, previous_style); - - mail_display_update_formatter_colors (E_MAIL_DISPLAY (widget)); - em_format_queue_redraw (EM_FORMAT (priv->formatter)); -} - -static void -mail_display_load_string (EWebView *web_view, - const gchar *string) -{ - EMailDisplayPrivate *priv; - - priv = E_MAIL_DISPLAY_GET_PRIVATE (web_view); - g_return_if_fail (priv->formatter != NULL); - - if (em_format_busy (EM_FORMAT (priv->formatter))) - return; - - /* Chain up to parent's load_string() method. */ - E_WEB_VIEW_CLASS (e_mail_display_parent_class)-> - load_string (web_view, string); -} - -static void -mail_display_url_requested (GtkHTML *html, - const gchar *uri, - GtkHTMLStream *stream) -{ - /* XXX Sadly, we must block the default method - * until EMFormatHTML is made asynchronous. */ + GTK_WIDGET_CLASS (parent_class)->style_set (widget, previous_style); } static gboolean mail_display_process_mailto (EWebView *web_view, - const gchar *mailto_uri) + const gchar *mailto_uri, + gpointer user_data) { - g_return_val_if_fail (web_view != NULL, FALSE); + g_return_val_if_fail (E_IS_WEB_VIEW (web_view), FALSE); g_return_val_if_fail (mailto_uri != NULL, FALSE); - g_return_val_if_fail (E_IS_MAIL_DISPLAY (web_view), FALSE); if (g_ascii_strncasecmp (mailto_uri, "mailto:", 7) == 0) { - EMailDisplayPrivate *priv; EMFormat *format; CamelFolder *folder = NULL; EShell *shell; - priv = E_MAIL_DISPLAY_GET_PRIVATE (web_view); - g_return_val_if_fail (priv->formatter != NULL, FALSE); - - format = EM_FORMAT (priv->formatter); + format = (EMFormat *) E_MAIL_DISPLAY (web_view)->priv->formatter; if (format != NULL && format->folder != NULL) folder = format->folder; @@ -277,79 +367,825 @@ mail_display_process_mailto (EWebView *web_view, return FALSE; } -static void -mail_display_link_clicked (GtkHTML *html, - const gchar *uri) +static gboolean +mail_display_link_clicked (WebKitWebView *web_view, + WebKitWebFrame *frame, + WebKitNetworkRequest *request, + WebKitWebNavigationAction *navigation_action, + WebKitWebPolicyDecision *policy_decision, + gpointer user_data) { - EMailDisplayPrivate *priv; + EMailDisplay *display; + const gchar *uri = webkit_network_request_get_uri (request); - priv = E_MAIL_DISPLAY_GET_PRIVATE (html); - g_return_if_fail (priv->formatter != NULL); - - if (g_str_has_prefix (uri, "##")) { - guint32 flags; - - flags = priv->formatter->header_wrap_flags; - - if (strcmp (uri, "##TO##") == 0) { - if (!(flags & EM_FORMAT_HTML_HEADER_TO)) - flags |= EM_FORMAT_HTML_HEADER_TO; - else - flags &= ~EM_FORMAT_HTML_HEADER_TO; - } else if (strcmp (uri, "##CC##") == 0) { - if (!(flags & EM_FORMAT_HTML_HEADER_CC)) - flags |= EM_FORMAT_HTML_HEADER_CC; - else - flags &= ~EM_FORMAT_HTML_HEADER_CC; - } else if (strcmp (uri, "##BCC##") == 0) { - if (!(flags & EM_FORMAT_HTML_HEADER_BCC)) - flags |= EM_FORMAT_HTML_HEADER_BCC; - else - flags &= ~EM_FORMAT_HTML_HEADER_BCC; - } else if (strcmp (uri, "##HEADERS##") == 0) { - EMFormatHTMLHeadersState state; - - state = em_format_html_get_headers_state ( - priv->formatter); - - if (state == EM_FORMAT_HTML_HEADERS_STATE_COLLAPSED) - state = EM_FORMAT_HTML_HEADERS_STATE_EXPANDED; - else - state = EM_FORMAT_HTML_HEADERS_STATE_COLLAPSED; - - em_format_html_set_headers_state ( - priv->formatter, state); - } + display = E_MAIL_DISPLAY (web_view); + if (display->priv->formatter == NULL) + return FALSE; - priv->formatter->header_wrap_flags = flags; - em_format_queue_redraw (EM_FORMAT (priv->formatter)); - - } else if (mail_display_process_mailto (E_WEB_VIEW (html), uri)) { + if (mail_display_process_mailto (E_WEB_VIEW (web_view), uri, NULL)) { /* do nothing, function handled the "mailto:" uri already */ - } else if (*uri == '#') - gtk_html_jump_to_anchor (html, uri + 1); + webkit_web_policy_decision_ignore (policy_decision); + return TRUE; - else if (g_ascii_strncasecmp (uri, "thismessage:", 12) == 0) + } else if (g_ascii_strncasecmp (uri, "thismessage:", 12) == 0) { /* ignore */ ; + webkit_web_policy_decision_ignore (policy_decision); + return TRUE; - else if (g_ascii_strncasecmp (uri, "cid:", 4) == 0) + } else if (g_ascii_strncasecmp (uri, "cid:", 4) == 0) { /* ignore */ ; + webkit_web_policy_decision_ignore (policy_decision); + return TRUE; + + } + + /* Let webkit handle it */ + return FALSE; +} + +static void +webkit_request_load_from_file (WebKitNetworkRequest *request, + const gchar *path) +{ + gchar *data = NULL; + gsize length = 0; + gboolean status; + gchar *b64, *new_uri; + gchar *ct; + + status = g_file_get_contents (path, &data, &length, NULL); + if (!status) + return; + + b64 = g_base64_encode ((guchar *) data, length); + ct = g_content_type_guess (path, NULL, 0, NULL); + + new_uri = g_strdup_printf ("data:%s;base64,%s", ct, b64); + webkit_network_request_set_uri (request, new_uri); + + g_free (b64); + g_free (new_uri); + g_free (ct); + g_free (data); +} + +static void +mail_display_resource_requested (WebKitWebView *web_view, + WebKitWebFrame *frame, + WebKitWebResource *resource, + WebKitNetworkRequest *request, + WebKitNetworkResponse *response, + gpointer user_data) +{ + EMailDisplay *display = E_MAIL_DISPLAY (web_view); + EMFormat *formatter = EM_FORMAT (display->priv->formatter); + const gchar *uri = webkit_network_request_get_uri (request); + + if (!formatter) { + webkit_network_request_set_uri (request, "invalid://uri"); + return; + } + + /* Redirect cid:part_id to mail://mail_id/cid:part_id */ + if (g_str_has_prefix (uri, "cid:")) { + + /* Always write raw content of CID object */ + gchar *new_uri = em_format_build_mail_uri (formatter->folder, + formatter->message_uid, + "part_id", G_TYPE_STRING, uri, + "mode", G_TYPE_INT, EM_FORMAT_WRITE_MODE_RAW, NULL); + + webkit_network_request_set_uri (request, new_uri); + + g_free (new_uri); + + /* WebKit won't allow to load a local file when displaing "remote" mail://, + protocol, so we need to handle this manually */ + } else if (g_str_has_prefix (uri, "file:")) { + gchar *path; + + path = g_filename_from_uri (uri, NULL, NULL); + if (!path) + return; + + webkit_request_load_from_file (request, path); + + g_free (path); + + /* Redirect http(s) request to evo-http(s) protocol. See EMailRequest for + * further details about this. */ + } else if (g_str_has_prefix (uri, "http:") || g_str_has_prefix (uri, "https")) { + + gchar *new_uri, *mail_uri, *enc; + SoupURI *soup_uri; + GHashTable *query; + gchar *uri_md5; + CamelStream *stream; + + /* Open Evolution's cache */ + uri_md5 = g_compute_checksum_for_string (G_CHECKSUM_MD5, uri, -1); + stream = camel_data_cache_get ( + emd_global_http_cache, "http", uri_md5, NULL); + g_free (uri_md5); + + /* If the URI is not cached and we are not allowed to load it + * then redirect to invalid URI, so that webkit would display + * a native placeholder for it. */ + if (!stream && !display->priv->force_image_load && + !em_format_html_can_load_images (display->priv->formatter)) { + webkit_network_request_set_uri (request, "invalid://protocol"); + return; + } + + new_uri = g_strconcat ("evo-", uri, NULL); + mail_uri = em_format_build_mail_uri (formatter->folder, + formatter->message_uid, NULL, NULL); + + soup_uri = soup_uri_new (new_uri); + if (soup_uri->query) { + query = soup_form_decode (soup_uri->query); + } else { + query = g_hash_table_new_full (g_str_hash, g_str_equal, + g_free, g_free); + } + enc = soup_uri_encode (mail_uri, NULL); + g_hash_table_insert (query, g_strdup ("__evo-mail"), enc); + + if (display->priv->force_image_load) { + g_hash_table_insert (query, + g_strdup ("__evo-load-images"), + g_strdup ("true")); + } + + g_free (mail_uri); + + soup_uri_set_query_from_form (soup_uri, query); + g_free (new_uri); + + new_uri = soup_uri_to_string (soup_uri, FALSE); + webkit_network_request_set_uri (request, new_uri); + + g_free (new_uri); + soup_uri_free (soup_uri); + g_hash_table_unref (query); + } +} + +static WebKitDOMElement * +find_element_by_id (WebKitDOMDocument *document, + const gchar *id) +{ + WebKitDOMNodeList *frames; + WebKitDOMElement *element; + gulong i, length; + + /* Try to look up the element in this DOM document */ + element = webkit_dom_document_get_element_by_id (document, id); + if (element) + return element; + + /* If the element is not here then recursively scan all frames */ + frames = webkit_dom_document_get_elements_by_tag_name(document, "iframe"); + length = webkit_dom_node_list_get_length (frames); + for (i = 0; i < length; i++) { + + WebKitDOMHTMLIFrameElement *iframe = + WEBKIT_DOM_HTML_IFRAME_ELEMENT ( + webkit_dom_node_list_item (frames, i)); + + WebKitDOMDocument *frame_doc = + webkit_dom_html_iframe_element_get_content_document (iframe); + + WebKitDOMElement *el = + find_element_by_id (frame_doc, id); + + if (el) + return el; + } + + return NULL; +} + +static void +mail_display_plugin_widget_resize (GObject *object, + gpointer dummy, + EMailDisplay *display) +{ + GtkWidget *widget; + WebKitDOMElement *parent_element; + gchar *dim; + gint height; + + widget = GTK_WIDGET (object); + gtk_widget_get_preferred_height (widget, &height, NULL); + parent_element = g_object_get_data (object, "parent_element"); + + if (!parent_element || !WEBKIT_DOM_IS_ELEMENT (parent_element)) { + d(printf("%s: %s does not have (valid) parent element!\n", + G_STRFUNC, (gchar *) g_object_get_data (object, "uri"))); + return; + } + + /* Int -> Str */ + dim = g_strdup_printf ("%d", height); + + /* Set height of the containment to match height of the + * GtkWidget it contains */ + webkit_dom_html_object_element_set_height ( + WEBKIT_DOM_HTML_OBJECT_ELEMENT (parent_element), dim); + g_free (dim); +} + +static void +mail_display_plugin_widget_realize_cb (GtkWidget *widget, + gpointer user_data) +{ + /* Initial resize of the element when the widget + * is displayed for the first time. */ + mail_display_plugin_widget_resize (G_OBJECT (widget), NULL, user_data); +} + +static void +plugin_widget_set_parent_element (GtkWidget *widget, + EMailDisplay *display) +{ + const gchar *uri; + WebKitDOMDocument *document; + WebKitDOMElement *element; + + uri = g_object_get_data (G_OBJECT (widget), "uri"); + if (!uri || !*uri) + return; + + document = webkit_web_view_get_dom_document (WEBKIT_WEB_VIEW (display)); + element = find_element_by_id (document, uri); + + if (!element || !WEBKIT_DOM_IS_ELEMENT (element)) { + g_warning ("Failed to find parent for '%s' - no ID set?", uri); + return; + } + + /* Assign the WebKitDOMElement to "parent_element" data of the GtkWidget + * and the GtkWidget to "widget" data of the DOM Element */ + g_object_set_data (G_OBJECT (widget), "parent_element", element); + g_object_set_data (G_OBJECT (element), "widget", widget); +} + +static void +attachment_button_expanded (GObject *object, + GParamSpec *pspec, + gpointer user_data) +{ + EAttachmentButton *button = E_ATTACHMENT_BUTTON (object); + WebKitDOMElement *attachment = user_data; + WebKitDOMCSSStyleDeclaration *css; + gboolean expanded; + + d(printf("Attachment button %s (%p) expansion state toggled!\n", + (gchar *) g_object_get_data (object, "uri"), object)); + + expanded = e_attachment_button_get_expanded (button) && + gtk_widget_get_visible (GTK_WIDGET (button)); + + if (!WEBKIT_DOM_IS_ELEMENT (attachment)) { + d(printf("%s: Parent element for button %s does not exist!\n", + G_STRFUNC, (gchar *) g_object_get_data (object, "uri"))); + return; + } + + /* Show or hide the DIV which contains the attachment (iframe, image...) */ + css = webkit_dom_element_get_style (attachment); + webkit_dom_css_style_declaration_set_property ( + css, "display", expanded ? "block" : "none", "", NULL); +} + +static void +constraint_widget_visibility (GObject *object, + GParamSpec *pspec, + gpointer user_data) +{ + GtkWidget *widget = GTK_WIDGET (object); + EAttachmentButton *button = user_data; + + gboolean can_show = e_attachment_button_get_expanded (button); + gboolean is_visible = gtk_widget_get_visible (widget); + + if (is_visible && !can_show) + gtk_widget_hide (widget); + else if (!is_visible && can_show) + gtk_widget_show (widget); + + /* Otherwise it's OK */ +} + +static void +bind_iframe_content_visibility (EAttachmentButton *button, + WebKitDOMElement *iframe) +{ + WebKitDOMDocument *document; + WebKitDOMNodeList *nodes; + gulong i, length; + + if (!WEBKIT_DOM_IS_HTML_IFRAME_ELEMENT (iframe)) + return; + + document = webkit_dom_html_iframe_element_get_content_document ( + WEBKIT_DOM_HTML_IFRAME_ELEMENT (iframe)); + nodes = webkit_dom_document_get_elements_by_tag_name (document, "object"); + length = webkit_dom_node_list_get_length (nodes); - else { - /* Chain up to parent's link_clicked() method. */ - GTK_HTML_CLASS (e_mail_display_parent_class)-> - link_clicked (html, uri); + d(printf("Found %ld objects within iframe %s\n", length, + webkit_dom_html_iframe_element_get_name ( + WEBKIT_DOM_HTML_IFRAME_ELEMENT (iframe)))); + + /* Iterate through all s and bind visibility of their widget + * with expanded-state of related attachment button */ + for (i = 0; i < length; i++) { + + WebKitDOMNode *node = webkit_dom_node_list_item (nodes, i); + GtkWidget *widget; + + widget = g_object_get_data (G_OBJECT (node), "widget"); + if (!widget) + continue; + + d(printf("Binding visibility of widget %s (%p) with button %s (%p)\n", + (gchar *) g_object_get_data (G_OBJECT (widget), "uri"), widget, + (gchar *) g_object_get_data (G_OBJECT (button), "uri"), button)); + + g_object_bind_property ( + button, "expanded", + widget, "visible", + G_BINDING_SYNC_CREATE); + + /* Ensure that someone won't attempt to _show() the widget when + * it is supposed to be hidden and vice versa. */ + g_signal_connect (widget, "notify::visible", + G_CALLBACK (constraint_widget_visibility), button); } } +static void +bind_attachment_iframe_visibility (GObject *object, + GParamSpec *pspec, + gpointer user_data) +{ + WebKitWebFrame *webframe; + const gchar *frame_name; + gchar *button_uri; + WebKitDOMDocument *document; + WebKitDOMElement *attachment; + WebKitDOMElement *button_element; + WebKitDOMNodeList *nodes; + gulong i, length; + GtkWidget *button; + + /* Whenever an " + "", + e_color_to_value (&efh->priv->colors[EM_FORMAT_HTML_COLOR_FRAME]), + e_color_to_value (&efh->priv->colors[EM_FORMAT_HTML_COLOR_CONTENT]), + uri); + + camel_stream_write_string (stream, str, cancellable, NULL); + + g_free (str); + g_free (uri); + } +} + +static void +efh_write_source (EMFormat *emf, + EMFormatPURI *puri, + CamelStream *stream, + EMFormatWriterInfo *info, + GCancellable *cancellable) { + EMFormatHTML *efh = (EMFormatHTML *) emf; + GString *buffer; CamelStream *filtered_stream; CamelMimeFilter *filter; - CamelDataWrapper *dw = (CamelDataWrapper *) part; + CamelDataWrapper *dw = (CamelDataWrapper *) puri->part; filtered_stream = camel_stream_filter_new (stream); @@ -805,1786 +1018,1153 @@ efh_format_source (EMFormat *emf, CAMEL_STREAM_FILTER (filtered_stream), filter); g_object_unref (filter); + buffer = g_string_new (""); + + g_string_append_printf ( + buffer, "
", + e_color_to_value ( + &efh->priv->colors[ + EM_FORMAT_HTML_COLOR_BODY]), + e_color_to_value ( + &efh->priv->colors[ + EM_FORMAT_HTML_COLOR_HEADER])); + camel_stream_write_string ( - stream, "
", cancellable, NULL); - em_format_format_text (emf, filtered_stream, dw, cancellable); + stream, buffer->str, cancellable, NULL); + camel_stream_write_string ( + stream, "", cancellable, NULL); + camel_data_wrapper_write_to_stream_sync (dw, filtered_stream, + cancellable, NULL); camel_stream_write_string ( - stream, "
", cancellable, NULL); + stream, "", cancellable, NULL); g_object_unref (filtered_stream); + g_string_free (buffer, TRUE); } static void -efh_format_attachment (EMFormat *emf, - CamelStream *stream, - CamelMimePart *part, - const gchar *mime_type, - const EMFormatHandler *handle, - GCancellable *cancellable) +efh_write_headers (EMFormat *emf, + EMFormatPURI *puri, + CamelStream *stream, + EMFormatWriterInfo *info, + GCancellable *cancellable) { - gchar *text, *html; + GString *buffer; + EMFormatHTML *efh = (EMFormatHTML *) emf; + gint bg_color; - /* we display all inlined attachments only */ + if (!puri->part) + return; - /* this could probably be cleaned up ... */ - camel_stream_write_string ( - stream, - "" - "
" - "" - "
" - "
\n", - cancellable, NULL); + buffer = g_string_new (""); - /* output some info about it */ - text = em_format_describe_part (part, mime_type); - html = camel_text_to_html ( - text, ((EMFormatHTML *) emf)->text_html_flags & - CAMEL_MIME_FILTER_TOHTML_CONVERT_URLS, 0); - camel_stream_write_string (stream, html, cancellable, NULL); - g_free (html); - g_free (text); + if (info->mode & EM_FORMAT_WRITE_MODE_PRINTING) { + GdkColor white = { 0, G_MAXUINT16, G_MAXUINT16, G_MAXUINT16 }; + bg_color = e_color_to_value (&white); + } else { + bg_color = e_color_to_value (&efh->priv->colors[EM_FORMAT_HTML_COLOR_BODY]); + } - camel_stream_write_string ( - stream, "
", cancellable, NULL); + g_string_append_printf ( + buffer, + "
" + "\n" + "
\n", + bg_color, + e_color_to_value (&efh->priv->colors[EM_FORMAT_HTML_COLOR_HEADER])); + + if (info->headers_collapsable) { + g_string_append_printf (buffer, + "" + "", + EVOLUTION_IMAGESDIR, + (info->headers_collapsed) ? "plus.png" : "minus.png"); + + efh_format_short_headers (efh, buffer, (CamelMedium *) puri->part, + info->headers_collapsed, + cancellable); + } - if (handle && em_format_is_inline (emf, emf->part_id->str, part, handle)) - handle->handler (emf, stream, part, handle, cancellable, FALSE); -} + efh_format_full_headers (efh, buffer, (CamelMedium *) puri->part, + (info->mode == EM_FORMAT_WRITE_MODE_ALL_HEADERS), + !info->headers_collapsed, + cancellable); -static gboolean -efh_busy (EMFormat *emf) -{ - EMFormatHTMLPrivate *priv; + g_string_append (buffer, "
"); - priv = EM_FORMAT_HTML_GET_PRIVATE (emf); + camel_stream_write_string (stream, buffer->str, cancellable, NULL); - return (priv->format_id != -1); + g_string_free (buffer, true); } + static void -efh_base_init (EMFormatHTMLClass *class) +efh_write_error (EMFormat *emf, + EMFormatPURI *puri, + CamelStream *stream, + EMFormatWriterInfo *info, + GCancellable *cancellable) { - efh_builtin_init (class); + CamelStream *filtered_stream; + CamelMimeFilter *filter; + CamelDataWrapper *dw; + + dw = camel_medium_get_content ((CamelMedium *) puri->part); + + camel_stream_write_string (stream, "", cancellable, NULL); + + filtered_stream = camel_stream_filter_new (stream); + filter = camel_mime_filter_tohtml_new (CAMEL_MIME_FILTER_TOHTML_CONVERT_NL | + CAMEL_MIME_FILTER_TOHTML_CONVERT_URLS, 0); + camel_stream_filter_add (CAMEL_STREAM_FILTER (filtered_stream), filter); + g_object_unref (filter); + + camel_data_wrapper_decode_to_stream_sync (dw, filtered_stream, cancellable, NULL); + + g_object_unref (filtered_stream); + + camel_stream_write_string (stream, "
", cancellable, NULL); } static void -efh_class_init (EMFormatHTMLClass *class) +efh_write_message_rfc822 (EMFormat *emf, + EMFormatPURI *puri, + CamelStream *stream, + EMFormatWriterInfo *info, + GCancellable *cancellable) { - GObjectClass *object_class; - EMFormatClass *format_class; - const gchar *user_cache_dir; + if (info->mode == EM_FORMAT_WRITE_MODE_RAW) { - parent_class = g_type_class_peek_parent (class); - g_type_class_add_private (class, sizeof (EMFormatHTMLPrivate)); + GList *puris; + GList *iter; - object_class = G_OBJECT_CLASS (class); - object_class->set_property = efh_set_property; - object_class->get_property = efh_get_property; - object_class->finalize = efh_finalize; + /* Create a new fake list of PURIs which will contain only + * PURIs from this message. */ + iter = g_hash_table_lookup (emf->mail_part_table, puri->uri); + if (!iter || !iter->next) + return; - format_class = EM_FORMAT_CLASS (class); - format_class->format_clone = efh_format_clone; - format_class->format_error = efh_format_error; - format_class->format_source = efh_format_source; - format_class->format_attachment = efh_format_attachment; - format_class->format_secure = efh_format_secure; - format_class->busy = efh_busy; + iter = iter->next; + puris = NULL; + while (iter) { - class->html_widget_type = E_TYPE_WEB_VIEW; + EMFormatPURI *p; + p = iter->data; - g_object_class_install_property ( - object_class, - PROP_BODY_COLOR, - g_param_spec_boxed ( - "body-color", - "Body Color", - NULL, - GDK_TYPE_COLOR, - G_PARAM_READWRITE)); + if (g_str_has_suffix (p->uri, ".rfc822.end")) + break; - g_object_class_install_property ( - object_class, - PROP_CITATION_COLOR, - g_param_spec_boxed ( - "citation-color", - "Citation Color", - NULL, - GDK_TYPE_COLOR, - G_PARAM_READWRITE)); + puris = g_list_append (puris, p); + iter = iter->next; - g_object_class_install_property ( - object_class, - PROP_CONTENT_COLOR, - g_param_spec_boxed ( - "content-color", - "Content Color", - NULL, - GDK_TYPE_COLOR, - G_PARAM_READWRITE)); + }; - g_object_class_install_property ( - object_class, - PROP_FRAME_COLOR, - g_param_spec_boxed ( - "frame-color", - "Frame Color", - NULL, - GDK_TYPE_COLOR, - G_PARAM_READWRITE)); + efh_write_message (emf, puris, stream, info, cancellable); - g_object_class_install_property ( - object_class, - PROP_HEADER_COLOR, - g_param_spec_boxed ( - "header-color", - "Header Color", - NULL, - GDK_TYPE_COLOR, - G_PARAM_READWRITE)); + g_list_free (puris); - /* FIXME Make this a proper enum property. */ - g_object_class_install_property ( - object_class, - PROP_IMAGE_LOADING_POLICY, - g_param_spec_enum ( - "image-loading-policy", - "Image Loading Policy", - NULL, - E_TYPE_MAIL_IMAGE_LOADING_POLICY, - E_MAIL_IMAGE_LOADING_POLICY_ALWAYS, - G_PARAM_READWRITE)); + } else if (info->mode == EM_FORMAT_WRITE_MODE_PRINTING) { - g_object_class_install_property ( - object_class, - PROP_MARK_CITATIONS, - g_param_spec_boolean ( - "mark-citations", - "Mark Citations", - NULL, - TRUE, - G_PARAM_READWRITE)); + GList *iter; + gboolean can_write = FALSE; - g_object_class_install_property ( - object_class, - PROP_ONLY_LOCAL_PHOTOS, - g_param_spec_boolean ( - "only-local-photos", - "Only Local Photos", - NULL, - TRUE, - G_PARAM_READWRITE | - G_PARAM_CONSTRUCT)); + iter = g_hash_table_lookup (emf->mail_part_table, puri->uri); + if (!iter || !iter->next) + return; - g_object_class_install_property ( - object_class, - PROP_SHOW_SENDER_PHOTO, - g_param_spec_boolean ( - "show-sender-photo", - "Show Sender Photo", - NULL, - TRUE, - G_PARAM_READWRITE | - G_PARAM_CONSTRUCT)); + /* Skip everything before attachment bar, inclusive */\ + iter = iter->next; + while (iter) { - g_object_class_install_property ( - object_class, - PROP_SHOW_REAL_DATE, - g_param_spec_boolean ( - "show-real-date", - "Show real Date header value", - NULL, - TRUE, - G_PARAM_READWRITE | - G_PARAM_CONSTRUCT)); + EMFormatPURI *p = iter->data; - g_object_class_install_property ( - object_class, - PROP_TEXT_COLOR, - g_param_spec_boxed ( - "text-color", - "Text Color", - NULL, - GDK_TYPE_COLOR, - G_PARAM_READWRITE)); + /* EMFormatHTMLPrint has registered a special writer + * for headers, try to find it and use it. */ + if (g_str_has_suffix (p->uri, ".headers")) { - g_object_class_install_property ( - object_class, - PROP_WEB_VIEW, - g_param_spec_object ( - "web-view", - "Web View", - NULL, - E_TYPE_WEB_VIEW, - G_PARAM_READABLE)); + const EMFormatHandler *handler; - g_object_class_install_property ( - object_class, - PROP_HEADERS_STATE, - g_param_spec_int ( - "headers-state", - "Headers state", - NULL, - EM_FORMAT_HTML_HEADERS_STATE_EXPANDED, - EM_FORMAT_HTML_HEADERS_STATE_COLLAPSED, - EM_FORMAT_HTML_HEADERS_STATE_EXPANDED, - G_PARAM_READWRITE)); + handler = em_format_find_handler ( + emf, "x-evolution/message/headers"); + if (handler && handler->write_func) + handler->write_func (emf, p, stream, info, cancellable); - g_object_class_install_property ( - object_class, - PROP_HEADERS_STATE, - g_param_spec_boolean ( - "headers-collapsable", - NULL, - NULL, - FALSE, - G_PARAM_READWRITE)); + iter = iter->next; + continue; + } - /* cache expiry - 2 hour access, 1 day max */ - user_cache_dir = e_get_user_cache_dir (); - emfh_http_cache = camel_data_cache_new (user_cache_dir, NULL); - if (emfh_http_cache) { - camel_data_cache_set_expire_age (emfh_http_cache, 24 *60 *60); - camel_data_cache_set_expire_access (emfh_http_cache, 2 *60 *60); - } -} + if (g_str_has_suffix (p->uri, ".rfc822.end")) + break; -static void -efh_init (EMFormatHTML *efh, - EMFormatHTMLClass *class) -{ - EWebView *web_view; - GdkColor *color; + if (g_str_has_suffix (p->uri, ".attachment-bar")) + can_write = TRUE; - efh->priv = EM_FORMAT_HTML_GET_PRIVATE (efh); + if (can_write && p->write_func) { + p->write_func ( + emf, p, stream, info, cancellable); + } - g_queue_init (&efh->pending_object_list); - g_queue_init (&efh->priv->pending_jobs); - efh->priv->lock = g_mutex_new (); - efh->priv->format_id = -1; - efh->priv->text_inline_parts = g_hash_table_new_full ( - g_str_hash, g_str_equal, - (GDestroyNotify) NULL, - (GDestroyNotify) efh_free_cache); - - web_view = g_object_new (class->html_widget_type, NULL); - efh->priv->web_view = g_object_ref_sink (web_view); - - gtk_html_set_blocking (GTK_HTML (web_view), FALSE); - gtk_html_set_caret_first_focus_anchor ( - GTK_HTML (web_view), EFM_MESSAGE_START_ANAME); - gtk_html_set_default_content_type ( - GTK_HTML (web_view), "text/html; charset=utf-8"); - e_web_view_set_editable (web_view, FALSE); - - g_signal_connect ( - web_view, "url-requested", - G_CALLBACK (efh_url_requested), efh); - g_signal_connect ( - web_view, "object-requested", - G_CALLBACK (efh_object_requested), efh); + iter = iter->next; + } - color = &efh->priv->colors[EM_FORMAT_HTML_COLOR_BODY]; - gdk_color_parse ("#eeeeee", color); + } else { + gchar *str; + gchar *uri; - color = &efh->priv->colors[EM_FORMAT_HTML_COLOR_CONTENT]; - gdk_color_parse ("#ffffff", color); + EMFormatHTML *efh = (EMFormatHTML *) emf; + EMFormatPURI *p; + GList *iter; - color = &efh->priv->colors[EM_FORMAT_HTML_COLOR_FRAME]; - gdk_color_parse ("#3f3f3f", color); + iter = g_hash_table_lookup (emf->mail_part_table, puri->uri); + if (!iter || !iter->next) + return; - color = &efh->priv->colors[EM_FORMAT_HTML_COLOR_HEADER]; - gdk_color_parse ("#eeeeee", color); + iter = iter->next; + p = iter->data; + + uri = em_format_build_mail_uri (emf->folder, emf->message_uid, + "part_id", G_TYPE_STRING, p->uri, + "mode", G_TYPE_INT, EM_FORMAT_WRITE_MODE_RAW, + NULL); + + str = g_strdup_printf ( + "
" + "
\n" + "" + "
", + e_color_to_value (&efh->priv->colors[EM_FORMAT_HTML_COLOR_FRAME]), + e_color_to_value (&efh->priv->colors[EM_FORMAT_HTML_COLOR_CONTENT]), + uri, puri->uri); + + camel_stream_write_string (stream, str, cancellable, NULL); + + g_free (str); + g_free (uri); + } - color = &efh->priv->colors[EM_FORMAT_HTML_COLOR_TEXT]; - gdk_color_parse ("#000000", color); - - efh->text_html_flags = - CAMEL_MIME_FILTER_TOHTML_CONVERT_NL | - CAMEL_MIME_FILTER_TOHTML_CONVERT_SPACES | - CAMEL_MIME_FILTER_TOHTML_MARK_CITATION; - efh->show_icon = TRUE; - efh->state = EM_FORMAT_HTML_STATE_NONE; - - e_extensible_load_extensions (E_EXTENSIBLE (efh)); -} - -GType -em_format_html_get_type (void) -{ - static GType type = 0; - - if (G_UNLIKELY (type == 0)) { - static const GTypeInfo type_info = { - sizeof (EMFormatHTMLClass), - (GBaseInitFunc) efh_base_init, - (GBaseFinalizeFunc) NULL, - (GClassInitFunc) efh_class_init, - (GClassFinalizeFunc) NULL, - NULL, /* class_data */ - sizeof (EMFormatHTML), - 0, /* n_preallocs */ - (GInstanceInitFunc) efh_init, - NULL /* value_table */ - }; - - static const GInterfaceInfo extensible_info = { - (GInterfaceInitFunc) NULL, - (GInterfaceFinalizeFunc) NULL, - NULL /* interface_data */ - }; - - type = g_type_register_static ( - em_format_get_type(), "EMFormatHTML", - &type_info, G_TYPE_FLAG_ABSTRACT); - - g_type_add_interface_static ( - type, E_TYPE_EXTENSIBLE, &extensible_info); - } - - return type; -} - -EWebView * -em_format_html_get_web_view (EMFormatHTML *efh) -{ - g_return_val_if_fail (EM_IS_FORMAT_HTML (efh), NULL); - - return efh->priv->web_view; -} - -void -em_format_html_load_images (EMFormatHTML *efh) -{ - g_return_if_fail (EM_IS_FORMAT_HTML (efh)); - - if (efh->priv->image_loading_policy == E_MAIL_IMAGE_LOADING_POLICY_ALWAYS) - return; - - /* This will remain set while we're still - * rendering the same message, then it wont be. */ - efh->priv->load_images_now = TRUE; - em_format_queue_redraw (EM_FORMAT (efh)); -} - -void -em_format_html_get_color (EMFormatHTML *efh, - EMFormatHTMLColorType type, - GdkColor *color) -{ - GdkColor *format_color; - - g_return_if_fail (EM_IS_FORMAT_HTML (efh)); - g_return_if_fail (type < EM_FORMAT_HTML_NUM_COLOR_TYPES); - g_return_if_fail (color != NULL); - - format_color = &efh->priv->colors[type]; - - color->red = format_color->red; - color->green = format_color->green; - color->blue = format_color->blue; -} - -void -em_format_html_set_color (EMFormatHTML *efh, - EMFormatHTMLColorType type, - const GdkColor *color) -{ - GdkColor *format_color; - const gchar *property_name; - - g_return_if_fail (EM_IS_FORMAT_HTML (efh)); - g_return_if_fail (type < EM_FORMAT_HTML_NUM_COLOR_TYPES); - g_return_if_fail (color != NULL); - - format_color = &efh->priv->colors[type]; - - if (gdk_color_equal (color, format_color)) - return; - - format_color->red = color->red; - format_color->green = color->green; - format_color->blue = color->blue; - - switch (type) { - case EM_FORMAT_HTML_COLOR_BODY: - property_name = "body-color"; - break; - case EM_FORMAT_HTML_COLOR_CITATION: - property_name = "citation-color"; - break; - case EM_FORMAT_HTML_COLOR_CONTENT: - property_name = "content-color"; - break; - case EM_FORMAT_HTML_COLOR_FRAME: - property_name = "frame-color"; - break; - case EM_FORMAT_HTML_COLOR_HEADER: - property_name = "header-color"; - break; - case EM_FORMAT_HTML_COLOR_TEXT: - property_name = "text-color"; - break; - default: - g_return_if_reached (); - } - - g_object_notify (G_OBJECT (efh), property_name); -} - -EMailImageLoadingPolicy -em_format_html_get_image_loading_policy (EMFormatHTML *efh) -{ - g_return_val_if_fail (EM_IS_FORMAT_HTML (efh), 0); - - return efh->priv->image_loading_policy; -} - -void -em_format_html_set_image_loading_policy (EMFormatHTML *efh, - EMailImageLoadingPolicy policy) -{ - g_return_if_fail (EM_IS_FORMAT_HTML (efh)); - - if (policy == efh->priv->image_loading_policy) - return; - - efh->priv->image_loading_policy = policy; - - g_object_notify (G_OBJECT (efh), "image-loading-policy"); -} - -gboolean -em_format_html_get_mark_citations (EMFormatHTML *efh) -{ - guint32 flags; - - g_return_val_if_fail (EM_IS_FORMAT_HTML (efh), FALSE); - - flags = efh->text_html_flags; - - return ((flags & CAMEL_MIME_FILTER_TOHTML_MARK_CITATION) != 0); -} - -void -em_format_html_set_mark_citations (EMFormatHTML *efh, - gboolean mark_citations) -{ - g_return_if_fail (EM_IS_FORMAT_HTML (efh)); - - if (mark_citations) - efh->text_html_flags |= - CAMEL_MIME_FILTER_TOHTML_MARK_CITATION; - else - efh->text_html_flags &= - ~CAMEL_MIME_FILTER_TOHTML_MARK_CITATION; - - g_object_notify (G_OBJECT (efh), "mark-citations"); -} - -gboolean -em_format_html_get_only_local_photos (EMFormatHTML *efh) -{ - g_return_val_if_fail (EM_IS_FORMAT_HTML (efh), FALSE); - - return efh->priv->only_local_photos; } -void -em_format_html_set_only_local_photos (EMFormatHTML *efh, - gboolean only_local_photos) -{ - g_return_if_fail (EM_IS_FORMAT_HTML (efh)); - - efh->priv->only_local_photos = only_local_photos; - - g_object_notify (G_OBJECT (efh), "only-local-photos"); -} +/*****************************************************************************/ -gboolean -em_format_html_get_show_sender_photo (EMFormatHTML *efh) -{ - g_return_val_if_fail (EM_IS_FORMAT_HTML (efh), FALSE); - - return efh->priv->show_sender_photo; -} - -void -em_format_html_set_show_sender_photo (EMFormatHTML *efh, - gboolean show_sender_photo) -{ - g_return_if_fail (EM_IS_FORMAT_HTML (efh)); - - efh->priv->show_sender_photo = show_sender_photo; - - g_object_notify (G_OBJECT (efh), "show-sender-photo"); -} +/* Notes: + * + * image/tiff is omitted because it's a multi-page image format, but + * gdk-pixbuf unconditionally renders the first page only, and doesn't + * even indicate through meta-data whether multiple pages are present + * (see bug 335959). Therefore, make no attempt to render TIFF images + * inline and defer to an application that can handle multi-page TIFF + * files properly like Evince or Gimp. Once the referenced bug is + * fixed we can reevaluate this policy. + */ +static EMFormatHandler type_builtin_table[] = { + { (gchar *) "image/gif", efh_parse_image, efh_write_image, }, + { (gchar *) "image/jpeg", efh_parse_image, efh_write_image, }, + { (gchar *) "image/png", efh_parse_image, efh_write_image, }, + { (gchar *) "image/x-png", efh_parse_image, efh_write_image, }, + { (gchar *) "image/x-bmp", efh_parse_image, efh_write_image, }, + { (gchar *) "image/bmp", efh_parse_image, efh_write_image, }, + { (gchar *) "image/svg", efh_parse_image, efh_write_image, }, + { (gchar *) "image/x-cmu-raster", efh_parse_image, efh_write_image, }, + { (gchar *) "image/x-ico", efh_parse_image, efh_write_image, }, + { (gchar *) "image/x-portable-anymap", efh_parse_image, efh_write_image, }, + { (gchar *) "image/x-portable-bitmap", efh_parse_image, efh_write_image, }, + { (gchar *) "image/x-portable-graymap", efh_parse_image, efh_write_image, }, + { (gchar *) "image/x-portable-pixmap", efh_parse_image, efh_write_image, }, + { (gchar *) "image/x-xpixmap", efh_parse_image, efh_write_image, }, + { (gchar *) "text/enriched", efh_parse_text_enriched, efh_write_text_enriched, }, + { (gchar *) "text/plain", efh_parse_text_plain, efh_write_text_plain, }, + { (gchar *) "text/html", efh_parse_text_html, efh_write_text_html, }, + { (gchar *) "text/richtext", efh_parse_text_enriched, efh_write_text_enriched, }, + { (gchar *) "text/*", efh_parse_text_plain, efh_write_text_plain, }, + { (gchar *) "message/rfc822", efh_parse_message_rfc822, efh_write_message_rfc822, EM_FORMAT_HANDLER_INLINE | EM_FORMAT_HANDLER_COMPOUND_TYPE }, + { (gchar *) "message/news", efh_parse_message_rfc822, 0, EM_FORMAT_HANDLER_INLINE | EM_FORMAT_HANDLER_COMPOUND_TYPE }, + { (gchar *) "message/delivery-status", efh_parse_message_deliverystatus, efh_write_text_plain, }, + { (gchar *) "message/external-body", efh_parse_message_external, efh_write_text_plain, }, + { (gchar *) "message/*", efh_parse_message_rfc822, 0, EM_FORMAT_HANDLER_INLINE }, -gboolean -em_format_html_get_show_real_date (EMFormatHTML *efh) -{ - g_return_val_if_fail (EM_IS_FORMAT_HTML (efh), FALSE); + /* This is where one adds those busted, non-registered types, + * that some idiot mailer writers out there decide to pull out + * of their proverbials at random. */ + { (gchar *) "image/jpg", efh_parse_image, efh_write_image, }, + { (gchar *) "image/pjpeg", efh_parse_image, efh_write_image, }, - return efh->priv->show_real_date; -} + /* special internal types */ + { (gchar *) "x-evolution/message/rfc822", 0, efh_write_text_plain, }, + { (gchar *) "x-evolution/message/headers", 0, efh_write_headers, }, + { (gchar *) "x-evolution/message/source", 0, efh_write_source, }, + { (gchar *) "x-evolution/message/attachment", 0, efh_write_attachment, }, + { (gchar *) "x-evolution/message/error", 0, efh_write_error, }, +}; -void -em_format_html_set_show_real_date (EMFormatHTML *efh, - gboolean show_real_date) +static void +efh_builtin_init (EMFormatHTMLClass *efhc) { - g_return_if_fail (EM_IS_FORMAT_HTML (efh)); + EMFormatClass *emfc; + gint ii; - efh->priv->show_real_date = show_real_date; + emfc = (EMFormatClass *) efhc; - g_object_notify (G_OBJECT (efh), "show-real-date"); + for (ii = 0; ii < G_N_ELEMENTS (type_builtin_table); ii++) + em_format_class_add_handler ( + emfc, &type_builtin_table[ii]); } -EMFormatHTMLHeadersState -em_format_html_get_headers_state (EMFormatHTML *efh) +static void +efh_set_property (GObject *object, + guint property_id, + const GValue *value, + GParamSpec *pspec) { - g_return_val_if_fail (EM_IS_FORMAT_HTML (efh), EM_FORMAT_HTML_HEADERS_STATE_EXPANDED); - - return efh->priv->headers_state; -} + switch (property_id) { + case PROP_BODY_COLOR: + em_format_html_set_color ( + EM_FORMAT_HTML (object), + EM_FORMAT_HTML_COLOR_BODY, + g_value_get_boxed (value)); + return; -void -em_format_html_set_headers_state (EMFormatHTML *efh, - EMFormatHTMLHeadersState state) -{ - g_return_if_fail (EM_IS_FORMAT_HTML (efh)); + case PROP_CITATION_COLOR: + em_format_html_set_color ( + EM_FORMAT_HTML (object), + EM_FORMAT_HTML_COLOR_CITATION, + g_value_get_boxed (value)); + return; - efh->priv->headers_state = state; + case PROP_CONTENT_COLOR: + em_format_html_set_color ( + EM_FORMAT_HTML (object), + EM_FORMAT_HTML_COLOR_CONTENT, + g_value_get_boxed (value)); + return; - g_object_notify (G_OBJECT (efh), "headers-state"); -} + case PROP_FRAME_COLOR: + em_format_html_set_color ( + EM_FORMAT_HTML (object), + EM_FORMAT_HTML_COLOR_FRAME, + g_value_get_boxed (value)); + return; -gboolean -em_format_html_get_headers_collapsable (EMFormatHTML *efh) -{ - g_return_val_if_fail (EM_IS_FORMAT_HTML (efh), FALSE); + case PROP_HEADER_COLOR: + em_format_html_set_color ( + EM_FORMAT_HTML (object), + EM_FORMAT_HTML_COLOR_HEADER, + g_value_get_boxed (value)); + return; - return efh->priv->headers_collapsable; -} + case PROP_IMAGE_LOADING_POLICY: + em_format_html_set_image_loading_policy ( + EM_FORMAT_HTML (object), + g_value_get_enum (value)); + return; -void -em_format_html_set_headers_collapsable (EMFormatHTML *efh, - gboolean collapsable) -{ - g_return_if_fail (EM_IS_FORMAT_HTML (efh)); + case PROP_MARK_CITATIONS: + em_format_html_set_mark_citations ( + EM_FORMAT_HTML (object), + g_value_get_boolean (value)); + return; - efh->priv->headers_collapsable = collapsable; + case PROP_ONLY_LOCAL_PHOTOS: + em_format_html_set_only_local_photos ( + EM_FORMAT_HTML (object), + g_value_get_boolean (value)); + return; - g_object_notify (G_OBJECT (efh), "headers-collapsable"); -} + case PROP_SHOW_SENDER_PHOTO: + em_format_html_set_show_sender_photo ( + EM_FORMAT_HTML (object), + g_value_get_boolean (value)); + return; -CamelMimePart * -em_format_html_file_part (EMFormatHTML *efh, - const gchar *mime_type, - const gchar *filename, - GCancellable *cancellable) -{ - CamelMimePart *part; - CamelStream *stream; - CamelDataWrapper *dw; - gchar *basename; + case PROP_SHOW_REAL_DATE: + em_format_html_set_show_real_date ( + EM_FORMAT_HTML (object), + g_value_get_boolean (value)); + return; - stream = camel_stream_fs_new_with_name (filename, O_RDONLY, 0, NULL); - if (stream == NULL) - return NULL; + case PROP_TEXT_COLOR: + em_format_html_set_color ( + EM_FORMAT_HTML (object), + EM_FORMAT_HTML_COLOR_TEXT, + g_value_get_boxed (value)); + return; - dw = camel_data_wrapper_new (); - camel_data_wrapper_construct_from_stream_sync ( - dw, stream, cancellable, NULL); - g_object_unref (stream); - if (mime_type) - camel_data_wrapper_set_mime_type (dw, mime_type); - part = camel_mime_part_new (); - camel_medium_set_content ((CamelMedium *) part, dw); - g_object_unref (dw); - basename = g_path_get_basename (filename); - camel_mime_part_set_filename (part, basename); - g_free (basename); + case PROP_ANIMATE_IMAGES: + em_format_html_set_animate_images ( + EM_FORMAT_HTML (object), + g_value_get_boolean (value)); + return; + } - return part; + G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec); } -/* all this api is a pain in the bum ... */ - -EMFormatHTMLPObject * -em_format_html_add_pobject (EMFormatHTML *efh, - gsize size, - const gchar *classid, - CamelMimePart *part, - EMFormatHTMLPObjectFunc func) +static void +efh_get_property (GObject *object, + guint property_id, + GValue *value, + GParamSpec *pspec) { - EMFormatHTMLPObject *pobj; + GdkColor color; - if (size < sizeof (EMFormatHTMLPObject)) { - g_warning ("size is less than the size of EMFormatHTMLPObject\n"); - size = sizeof (EMFormatHTMLPObject); - } + switch (property_id) { + case PROP_BODY_COLOR: + em_format_html_get_color ( + EM_FORMAT_HTML (object), + EM_FORMAT_HTML_COLOR_BODY, + &color); + g_value_set_boxed (value, &color); + return; - pobj = g_malloc0 (size); - if (classid) - pobj->classid = g_strdup (classid); - else - pobj->classid = g_strdup_printf("e-object:///%s", ((EMFormat *)efh)->part_id->str); + case PROP_CITATION_COLOR: + em_format_html_get_color ( + EM_FORMAT_HTML (object), + EM_FORMAT_HTML_COLOR_CITATION, + &color); + g_value_set_boxed (value, &color); + return; - pobj->format = efh; - pobj->func = func; - pobj->part = part; + case PROP_CONTENT_COLOR: + em_format_html_get_color ( + EM_FORMAT_HTML (object), + EM_FORMAT_HTML_COLOR_CONTENT, + &color); + g_value_set_boxed (value, &color); + return; - g_queue_push_tail (&efh->pending_object_list, pobj); + case PROP_FRAME_COLOR: + em_format_html_get_color ( + EM_FORMAT_HTML (object), + EM_FORMAT_HTML_COLOR_FRAME, + &color); + g_value_set_boxed (value, &color); + return; - return pobj; -} + case PROP_HEADER_COLOR: + em_format_html_get_color ( + EM_FORMAT_HTML (object), + EM_FORMAT_HTML_COLOR_HEADER, + &color); + g_value_set_boxed (value, &color); + return; -EMFormatHTMLPObject * -em_format_html_find_pobject (EMFormatHTML *emf, - const gchar *classid) -{ - GList *link; + case PROP_IMAGE_LOADING_POLICY: + g_value_set_enum ( + value, + em_format_html_get_image_loading_policy ( + EM_FORMAT_HTML (object))); + return; - g_return_val_if_fail (EM_IS_FORMAT_HTML (emf), NULL); - g_return_val_if_fail (classid != NULL, NULL); + case PROP_MARK_CITATIONS: + g_value_set_boolean ( + value, em_format_html_get_mark_citations ( + EM_FORMAT_HTML (object))); + return; - link = g_queue_peek_head_link (&emf->pending_object_list); + case PROP_ONLY_LOCAL_PHOTOS: + g_value_set_boolean ( + value, em_format_html_get_only_local_photos ( + EM_FORMAT_HTML (object))); + return; - while (link != NULL) { - EMFormatHTMLPObject *pw = link->data; + case PROP_SHOW_SENDER_PHOTO: + g_value_set_boolean ( + value, em_format_html_get_show_sender_photo ( + EM_FORMAT_HTML (object))); + return; - if (!strcmp (pw->classid, classid)) - return pw; + case PROP_SHOW_REAL_DATE: + g_value_set_boolean ( + value, em_format_html_get_show_real_date ( + EM_FORMAT_HTML (object))); + return; - link = g_list_next (link); + case PROP_TEXT_COLOR: + em_format_html_get_color ( + EM_FORMAT_HTML (object), + EM_FORMAT_HTML_COLOR_TEXT, + &color); + g_value_set_boxed (value, &color); + return; + case PROP_ANIMATE_IMAGES: + g_value_set_boolean ( + value, em_format_html_get_animate_images ( + EM_FORMAT_HTML (object))); + return; } - return NULL; + G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec); } -EMFormatHTMLPObject * -em_format_html_find_pobject_func (EMFormatHTML *emf, - CamelMimePart *part, - EMFormatHTMLPObjectFunc func) +static void +efh_finalize (GObject *object) { - GList *link; - - g_return_val_if_fail (EM_IS_FORMAT_HTML (emf), NULL); - - link = g_queue_peek_head_link (&emf->pending_object_list); - - while (link != NULL) { - EMFormatHTMLPObject *pw = link->data; - - if (pw->func == func && pw->part == part) - return pw; - - link = g_list_next (link); - } - - return NULL; + /* Chain up to parent's finalize() method. */ + G_OBJECT_CLASS (parent_class)->finalize (object); } -void -em_format_html_remove_pobject (EMFormatHTML *emf, - EMFormatHTMLPObject *pobject) +static void +efh_write_attachment (EMFormat *emf, + EMFormatPURI *puri, + CamelStream *stream, + EMFormatWriterInfo *info, + GCancellable *cancellable) { - g_return_if_fail (EM_IS_FORMAT_HTML (emf)); - g_return_if_fail (pobject != NULL); + gchar *text, *html; + CamelContentType *ct; + gchar *mime_type; + const EMFormatHandler *handler; - g_queue_remove (&emf->pending_object_list, pobject); + /* we display all inlined attachments only */ - if (pobject->free != NULL) - pobject->free (pobject); + /* this could probably be cleaned up ... */ + camel_stream_write_string ( + stream, + "" + "
" + "" + "
" + "
\n", + cancellable, NULL); - g_free (pobject->classid); - g_free (pobject); -} + ct = camel_mime_part_get_content_type (puri->part); + mime_type = camel_content_type_simple (ct); -void -em_format_html_clear_pobject (EMFormatHTML *emf) -{ - g_return_if_fail (EM_IS_FORMAT_HTML (emf)); + /* output some info about it */ + text = em_format_describe_part (puri->part, mime_type); + html = camel_text_to_html ( + text, ((EMFormatHTML *) emf)->text_html_flags & + CAMEL_MIME_FILTER_TOHTML_CONVERT_URLS, 0); + camel_stream_write_string (stream, html, cancellable, NULL); + g_free (html); + g_free (text); - while (!g_queue_is_empty (&emf->pending_object_list)) { - EMFormatHTMLPObject *pobj; + camel_stream_write_string ( + stream, "
", cancellable, NULL); - pobj = g_queue_pop_head (&emf->pending_object_list); - em_format_html_remove_pobject (emf, pobj); + handler = em_format_find_handler (emf, mime_type); + if (handler && handler->write_func && handler->write_func != efh_write_attachment) { + if (em_format_is_inline (emf, puri->uri, puri->part, handler)) + handler->write_func (emf, puri, stream, info, cancellable); } -} - -struct _EMFormatHTMLJob * -em_format_html_job_new (EMFormatHTML *emfh, - void (*callback) (struct _EMFormatHTMLJob *job, - GCancellable *cancellable), - gpointer data) -{ - struct _EMFormatHTMLJob *job = g_malloc0 (sizeof (*job)); - - job->format = emfh; - job->puri_level = ((EMFormat *) emfh)->pending_uri_level; - job->callback = callback; - job->u.data = data; - if (((EMFormat *) emfh)->base) - job->base = camel_url_copy (((EMFormat *) emfh)->base); - return job; + g_free (mime_type); } -void -em_format_html_job_queue (EMFormatHTML *emfh, - struct _EMFormatHTMLJob *job) +static void +efh_preparse (EMFormat *emf) { - g_mutex_lock (emfh->priv->lock); - g_queue_push_tail (&emfh->priv->pending_jobs, job); - g_mutex_unlock (emfh->priv->lock); -} + CamelInternetAddress *addr; -/* ********************************************************************** */ + EMFormatHTML *efh = EM_FORMAT_HTML (emf); -static void -emfh_getpuri (struct _EMFormatHTMLJob *job, - GCancellable *cancellable) -{ - d(printf(" running getpuri task\n")); - if (!g_cancellable_is_cancelled (cancellable)) - job->u.puri->func ( - EM_FORMAT (job->format), job->stream, - job->u.puri, cancellable); + if (!emf->message) { + efh->priv->can_load_images = FALSE; + return; + } + + addr = camel_mime_message_get_from (emf->message); + efh->priv->can_load_images = em_utils_in_addressbook (addr, FALSE); } static void -emfh_configure_stream_for_proxy (CamelHttpStream *stream, - const gchar *uri) +efh_write_message (EMFormat *emf, + GList *puris, + CamelStream *stream, + EMFormatWriterInfo *info, + GCancellable *cancellable) { - EProxy *proxy; - SoupURI *proxy_uri; - gchar *basic; - gchar *basic64; - const gchar *user = ""; - const gchar *password = ""; - - proxy = em_utils_get_proxy (); + GList *iter; + EMFormatHTML *efh; + gchar *header; - if (!e_proxy_require_proxy_for_uri (proxy, uri)) - return; + efh = (EMFormatHTML *) emf; - proxy_uri = e_proxy_peek_uri_for (proxy, uri); + header = g_strdup_printf ( + "\n\n" + "\n\n" + "Evolution Mail Display\n" + "\n" + "\n" + "", + e_color_to_value (&efh->priv->colors[ + EM_FORMAT_HTML_COLOR_BODY])); - if (proxy_uri == NULL) - return; + camel_stream_write_string (stream, header, cancellable, NULL); + g_free (header); - if (proxy_uri->user != NULL) - user = proxy_uri->user; + if (info->mode == EM_FORMAT_WRITE_MODE_SOURCE) { - if (proxy_uri->password != NULL) - password = proxy_uri->password; + efh_write_source (emf, emf->mail_part_list->data, + stream, info, cancellable); - if (*user == '\0' && *password == '\0') + camel_stream_write_string (stream, "", cancellable, NULL); return; + } - basic = g_strdup_printf ("%s:%s", user, password); - basic64 = g_base64_encode ((guchar *) basic, strlen (basic)); - camel_http_stream_set_proxy_authpass (stream, basic64); - g_free (basic64); - g_free (basic); -} + for (iter = puris; iter; iter = iter->next) { -static void -emfh_gethttp (struct _EMFormatHTMLJob *job, - GCancellable *cancellable) -{ - CamelStream *cistream = NULL, *costream = NULL, *instream = NULL; - CamelURL *url; - CamelHttpStream *tmp_stream; - gssize n, total = 0, pc_complete = 0, nread = 0; - gchar buffer[1500]; - const gchar *length; - - if (g_cancellable_is_cancelled (cancellable) - || (url = camel_url_new (job->u.uri, NULL)) == NULL) - goto badurl; - - d(printf(" running load uri task: %s\n", job->u.uri)); - - if (emfh_http_cache) - instream = cistream = camel_data_cache_get (emfh_http_cache, EMFH_HTTP_CACHE_PATH, job->u.uri, NULL); - - if (instream == NULL) { - EMailImageLoadingPolicy policy; - - policy = em_format_html_get_image_loading_policy (job->format); - - if (!(job->format->priv->load_images_now - || policy == E_MAIL_IMAGE_LOADING_POLICY_ALWAYS - || (policy == E_MAIL_IMAGE_LOADING_POLICY_SOMETIMES - && em_utils_in_addressbook ((CamelInternetAddress *) camel_mime_message_get_from (job->format->parent.message), FALSE)))) { - /* TODO: Ideally we would put the http requests into - * another queue and only send them out if the user - * selects 'load images', when they do. The problem - * is how to maintain this state with multiple - * renderings, and how to adjust the thread - * dispatch/setup routine to handle it */ - camel_url_free (url); - goto done; - } + EMFormatPURI *puri = iter->data; - instream = camel_http_stream_new (CAMEL_HTTP_METHOD_GET, ((EMFormat *) job->format)->session, url); - camel_http_stream_set_user_agent((CamelHttpStream *) instream, "CamelHttpStream/1.0 Evolution/" VERSION); - emfh_configure_stream_for_proxy ((CamelHttpStream *) instream, job->u.uri); - - camel_operation_push_message ( - cancellable, _("Retrieving '%s'"), job->u.uri); - tmp_stream = (CamelHttpStream *) instream; - if (camel_stream_read (CAMEL_STREAM (instream), NULL, 0, cancellable, NULL) == 0) { - CamelContentType *content_type; - - content_type = camel_http_stream_get_content_type (tmp_stream); - length = camel_header_raw_find(&tmp_stream->headers, "Content-Length", NULL); - d(printf(" Content-Length: %s\n", length)); - if (length != NULL) - total = atoi (length); - camel_content_type_unref (content_type); - } - } else - camel_operation_push_message ( - cancellable, _("Retrieving '%s'"), job->u.uri); + if (!puri) + continue; + + /* If current PURI has suffix .rfc822 then iterate through all + * subsequent PURIs until PURI with suffix .rfc822.end is found. + * These skipped PURIs contain entire RFC message which will + * be written in \n
\n", cid, cid); - camel_stream_write_string (stream, content, cancellable, NULL); - g_free (content); - g_free (cid); + g_object_notify (G_OBJECT (efh), property_name); } -/* This is a lot of code for something useless ... */ -static void -efh_message_external (EMFormat *emf, - CamelStream *stream, - CamelMimePart *part, - const EMFormatHandler *info, - GCancellable *cancellable, - gboolean is_fallback) +EMailImageLoadingPolicy +em_format_html_get_image_loading_policy (EMFormatHTML *efh) { - CamelContentType *type; - const gchar *access_type; - gchar *url = NULL, *desc = NULL; - gchar *content; - - if (!part) { - camel_stream_write_string ( - stream, _("Unknown external-body part."), - cancellable, NULL); - return; - } - - /* needs to be cleaner */ - type = camel_mime_part_get_content_type (part); - access_type = camel_content_type_param (type, "access-type"); - if (!access_type) { - camel_stream_write_string ( - stream, _("Malformed external-body part."), - cancellable, NULL); - return; - } - - if (!g_ascii_strcasecmp(access_type, "ftp") || - !g_ascii_strcasecmp(access_type, "anon-ftp")) { - const gchar *name, *site, *dir, *mode; - gchar *path; - gchar ftype[16]; - - name = camel_content_type_param (type, "name"); - site = camel_content_type_param (type, "site"); - dir = camel_content_type_param (type, "directory"); - mode = camel_content_type_param (type, "mode"); - if (name == NULL || site == NULL) - goto fail; + g_return_val_if_fail (EM_IS_FORMAT_HTML (efh), 0); - /* Generate the path. */ - if (dir) - path = g_strdup_printf("/%s/%s", *dir=='/'?dir+1:dir, name); - else - path = g_strdup_printf("/%s", *name=='/'?name+1:name); + return efh->priv->image_loading_policy; +} - if (mode && *mode) - sprintf(ftype, ";type=%c", *mode); - else - ftype[0] = 0; +void +em_format_html_set_image_loading_policy (EMFormatHTML *efh, + EMailImageLoadingPolicy policy) +{ + g_return_if_fail (EM_IS_FORMAT_HTML (efh)); - url = g_strdup_printf ("ftp://%s%s%s", site, path, ftype); - g_free (path); - desc = g_strdup_printf (_("Pointer to FTP site (%s)"), url); - } else if (!g_ascii_strcasecmp (access_type, "local-file")) { - const gchar *name, *site; + if (policy == efh->priv->image_loading_policy) + return; - name = camel_content_type_param (type, "name"); - site = camel_content_type_param (type, "site"); - if (name == NULL) - goto fail; + efh->priv->image_loading_policy = policy; - url = g_filename_to_uri (name, NULL, NULL); - if (site) - desc = g_strdup_printf(_("Pointer to local file (%s) valid at site \"%s\""), name, site); - else - desc = g_strdup_printf(_("Pointer to local file (%s)"), name); - } else if (!g_ascii_strcasecmp (access_type, "URL")) { - const gchar *urlparam; - gchar *s, *d; + g_object_notify (G_OBJECT (efh), "image-loading-policy"); +} - /* RFC 2017 */ +gboolean +em_format_html_get_mark_citations (EMFormatHTML *efh) +{ + guint32 flags; - urlparam = camel_content_type_param (type, "url"); - if (urlparam == NULL) - goto fail; + g_return_val_if_fail (EM_IS_FORMAT_HTML (efh), FALSE); - /* For obscure MIMEy reasons, the URL may be split into words */ - url = g_strdup (urlparam); - s = d = url; - while (*s) { - /* FIXME: use camel_isspace */ - if (!isspace ((guchar) * s)) - *d++ = *s; - s++; - } - *d = 0; - desc = g_strdup_printf (_("Pointer to remote data (%s)"), url); - } else - goto fail; + flags = efh->text_html_flags; - content = g_strdup_printf ("%s", url, desc); - camel_stream_write_string (stream, content, cancellable, NULL); - g_free (content); + return ((flags & CAMEL_MIME_FILTER_TOHTML_MARK_CITATION) != 0); +} - g_free (url); - g_free (desc); +void +em_format_html_set_mark_citations (EMFormatHTML *efh, + gboolean mark_citations) +{ + g_return_if_fail (EM_IS_FORMAT_HTML (efh)); - return; + if (mark_citations) + efh->text_html_flags |= + CAMEL_MIME_FILTER_TOHTML_MARK_CITATION; + else + efh->text_html_flags &= + ~CAMEL_MIME_FILTER_TOHTML_MARK_CITATION; -fail: - content = g_strdup_printf ( - _("Pointer to unknown external data (\"%s\" type)"), - access_type); - camel_stream_write_string (stream, content, cancellable, NULL); - g_free (content); + g_object_notify (G_OBJECT (efh), "mark-citations"); } -static void -efh_message_deliverystatus (EMFormat *emf, - CamelStream *stream, - CamelMimePart *part, - const EMFormatHandler *info, - GCancellable *cancellable, - gboolean is_fallback) +gboolean +em_format_html_get_only_local_photos (EMFormatHTML *efh) { - EMFormatHTML *efh = EM_FORMAT_HTML (emf); - CamelStream *filtered_stream; - CamelMimeFilter *html_filter; - guint32 rgb = 0x737373; - gchar *content; + g_return_val_if_fail (EM_IS_FORMAT_HTML (efh), FALSE); - /* Yuck, this is copied from efh_text_plain */ - content = g_strdup_printf ( - "
\n", - e_color_to_value ( - &efh->priv->colors[ - EM_FORMAT_HTML_COLOR_FRAME]), - e_color_to_value ( - &efh->priv->colors[ - EM_FORMAT_HTML_COLOR_CONTENT]), - e_color_to_value ( - &efh->priv->colors[ - EM_FORMAT_HTML_COLOR_TEXT])); - camel_stream_write_string (stream, content, cancellable, NULL); - g_free (content); + return efh->priv->only_local_photos; +} - filtered_stream = camel_stream_filter_new (stream); - html_filter = camel_mime_filter_tohtml_new (efh->text_html_flags, rgb); - camel_stream_filter_add ( - CAMEL_STREAM_FILTER (filtered_stream), html_filter); - g_object_unref (html_filter); +void +em_format_html_set_only_local_photos (EMFormatHTML *efh, + gboolean only_local_photos) +{ + g_return_if_fail (EM_IS_FORMAT_HTML (efh)); - camel_stream_write_string (stream, "\n" EFH_MESSAGE_START, cancellable, NULL); - em_format_format_text ( - emf, filtered_stream, - (CamelDataWrapper *) part, cancellable); - camel_stream_flush (filtered_stream, cancellable, NULL); - camel_stream_write_string (stream, "\n", cancellable, NULL); + efh->priv->only_local_photos = only_local_photos; - camel_stream_write_string (stream, "
", cancellable, NULL); + g_object_notify (G_OBJECT (efh), "only-local-photos"); } -static void -emfh_write_related (EMFormat *emf, - CamelStream *stream, - EMFormatPURI *puri, - GCancellable *cancellable) +gboolean +em_format_html_get_show_sender_photo (EMFormatHTML *efh) { - em_format_format_content (emf, stream, puri->part, cancellable); + g_return_val_if_fail (EM_IS_FORMAT_HTML (efh), FALSE); - camel_stream_close (stream, cancellable, NULL); + return efh->priv->show_sender_photo; } -static void -emfh_multipart_related_check (struct _EMFormatHTMLJob *job, - GCancellable *cancellable) +void +em_format_html_set_show_sender_photo (EMFormatHTML *efh, + gboolean show_sender_photo) { - EMFormat *format; - GList *link; - gchar *oldpartid; - - if (g_cancellable_is_cancelled (cancellable)) - return; - - format = EM_FORMAT (job->format); - - d(printf(" running multipart/related check task\n")); - oldpartid = g_strdup (format->part_id->str); - - link = g_queue_peek_head_link (job->puri_level->data); + g_return_if_fail (EM_IS_FORMAT_HTML (efh)); - if (!link) { - g_string_printf (format->part_id, "%s", oldpartid); - g_free (oldpartid); - return; - } + efh->priv->show_sender_photo = show_sender_photo; - while (link != NULL) { - EMFormatPURI *puri = link->data; - - if (puri->use_count == 0) { - d(printf("part '%s' '%s' used '%d'\n", puri->uri?puri->uri:"", puri->cid, puri->use_count)); - if (puri->func == emfh_write_related) { - g_string_printf (format->part_id, "%s", puri->part_id); - /* FIXME Not passing a GCancellable here. */ - em_format_part ( - format, CAMEL_STREAM (job->stream), - puri->part, NULL); - } - /* else it was probably added by a previous format this loop */ - } + g_object_notify (G_OBJECT (efh), "show-sender-photo"); +} - link = g_list_next (link); - } +gboolean +em_format_html_get_show_real_date (EMFormatHTML *efh) +{ + g_return_val_if_fail (EM_IS_FORMAT_HTML (efh), FALSE); - g_string_printf (format->part_id, "%s", oldpartid); - g_free (oldpartid); + return efh->priv->show_real_date; } -/* RFC 2387 */ -static void -efh_multipart_related (EMFormat *emf, - CamelStream *stream, - CamelMimePart *part, - const EMFormatHandler *info, - GCancellable *cancellable, - gboolean is_fallback) +void +em_format_html_set_show_real_date (EMFormatHTML *efh, + gboolean show_real_date) { - CamelMultipart *mp = (CamelMultipart *) camel_medium_get_content ((CamelMedium *) part); - CamelMimePart *body_part, *display_part = NULL; - CamelContentType *content_type; - const gchar *start; - gint i, nparts, partidlen, displayid = 0; - struct _EMFormatHTMLJob *job; - - if (!CAMEL_IS_MULTIPART (mp)) { - em_format_format_source (emf, stream, part, cancellable); - return; - } + g_return_if_fail (EM_IS_FORMAT_HTML (efh)); - nparts = camel_multipart_get_number (mp); - content_type = camel_mime_part_get_content_type (part); - start = camel_content_type_param (content_type, "start"); - if (start && strlen (start) > 2) { - gint len; - const gchar *cid; + efh->priv->show_real_date = show_real_date; - /* strip <>'s */ - len = strlen (start) - 2; - start++; + g_object_notify (G_OBJECT (efh), "show-real-date"); +} - for (i = 0; i < nparts; i++) { - body_part = camel_multipart_get_part (mp, i); - cid = camel_mime_part_get_content_id (body_part); +gboolean +em_format_html_get_animate_images (EMFormatHTML *efh) +{ + g_return_val_if_fail (EM_IS_FORMAT_HTML (efh), FALSE); - if (cid && !strncmp (cid, start, len) && strlen (cid) == len) { - display_part = body_part; - displayid = i; - break; - } - } - } else { - display_part = camel_multipart_get_part (mp, 0); - } + return efh->priv->animate_images; +} - if (display_part == NULL) { - em_format_part_as ( - emf, stream, part, - "multipart/mixed", cancellable); - return; - } +void +em_format_html_set_animate_images (EMFormatHTML *efh, + gboolean animate_images) +{ + g_return_if_fail (EM_IS_FORMAT_HTML (efh)); - em_format_push_level (emf); + efh->priv->animate_images = animate_images; - partidlen = emf->part_id->len; + g_object_notify (G_OBJECT (efh), "animate-images"); +} - /* queue up the parts for possible inclusion */ - for (i = 0; i < nparts; i++) { - body_part = camel_multipart_get_part (mp, i); - if (body_part != display_part) { - g_string_append_printf(emf->part_id, "related.%d", i); - em_format_add_puri (emf, sizeof (EMFormatPURI), NULL, body_part, emfh_write_related); - g_string_truncate (emf->part_id, partidlen); - d(printf(" part '%s' '%s' added\n", puri->uri?puri->uri:"", puri->cid)); - } - } +CamelMimePart * +em_format_html_file_part (EMFormatHTML *efh, + const gchar *mime_type, + const gchar *filename, + GCancellable *cancellable) +{ + CamelMimePart *part; + CamelStream *stream; + CamelDataWrapper *dw; + gchar *basename; - g_string_append_printf(emf->part_id, "related.%d", displayid); - em_format_part (emf, stream, display_part, cancellable); - g_string_truncate (emf->part_id, partidlen); - camel_stream_flush (stream, cancellable, NULL); + stream = camel_stream_fs_new_with_name (filename, O_RDONLY, 0, NULL); + if (stream == NULL) + return NULL; - /* queue a job to check for un-referenced parts to add as attachments */ - job = em_format_html_job_new ( - EM_FORMAT_HTML (emf), emfh_multipart_related_check, NULL); - job->stream = stream; - g_object_ref (stream); - em_format_html_job_queue ((EMFormatHTML *) emf, job); + dw = camel_data_wrapper_new (); + camel_data_wrapper_construct_from_stream_sync ( + dw, stream, cancellable, NULL); + g_object_unref (stream); + if (mime_type) + camel_data_wrapper_set_mime_type (dw, mime_type); + part = camel_mime_part_new (); + camel_medium_set_content ((CamelMedium *) part, dw); + g_object_unref (dw); + basename = g_path_get_basename (filename); + camel_mime_part_set_filename (part, basename); + g_free (basename); - em_format_pull_level (emf); + return part; } -static void -efh_write_image (EMFormat *emf, - CamelStream *stream, - EMFormatPURI *puri, - GCancellable *cancellable) +void +em_format_html_format_cert_infos (GQueue *cert_infos, + GString *output_buffer) { - CamelDataWrapper *dw = camel_medium_get_content ((CamelMedium *) puri->part); + GQueue valid = G_QUEUE_INIT; + GList *head, *link; + + g_return_if_fail (cert_infos != NULL); + g_return_if_fail (output_buffer != NULL); - d(printf("writing image '%s'\n", puri->cid)); - camel_data_wrapper_decode_to_stream_sync ( - dw, stream, cancellable, NULL); - camel_stream_close (stream, cancellable, NULL); -} + head = g_queue_peek_head_link (cert_infos); -static void -efh_image (EMFormat *emf, - CamelStream *stream, - CamelMimePart *part, - const EMFormatHandler *info, - GCancellable *cancellable, - gboolean is_fallback) -{ - EMFormatPURI *puri; - gchar *content; + /* Make sure we have a valid CamelCipherCertInfo before + * appending anything to the output buffer, so we don't + * end up with "()". */ + for (link = head; link != NULL; link = g_list_next (link)) { + CamelCipherCertInfo *cinfo = link->data; - puri = em_format_add_puri ( - emf, sizeof (EMFormatPURI), NULL, part, efh_write_image); + if ((cinfo->name != NULL && *cinfo->name != '\0') || + (cinfo->email != NULL && *cinfo->email != '\0')) { + g_queue_push_tail (&valid, cinfo); + } + } - content = g_strdup_printf ( - "", puri->cid); - camel_stream_write_string (stream, content, cancellable, NULL); - g_free (content); -} + if (g_queue_is_empty (&valid)) + return; -/* Notes: - * - * image/tiff is omitted because it's a multi-page image format, but - * gdk-pixbuf unconditionally renders the first page only, and doesn't - * even indicate through meta-data whether multiple pages are present - * (see bug 335959). Therefore, make no attempt to render TIFF images - * inline and defer to an application that can handle multi-page TIFF - * files properly like Evince or Gimp. Once the referenced bug is - * fixed we can reevaluate this policy. - */ -static EMFormatHandler type_builtin_table[] = { - { (gchar *) "image/gif", efh_image }, - { (gchar *) "image/jpeg", efh_image }, - { (gchar *) "image/png", efh_image }, - { (gchar *) "image/x-png", efh_image }, - { (gchar *) "image/x-bmp", efh_image }, - { (gchar *) "image/bmp", efh_image }, - { (gchar *) "image/svg", efh_image }, - { (gchar *) "image/x-cmu-raster", efh_image }, - { (gchar *) "image/x-ico", efh_image }, - { (gchar *) "image/x-portable-anymap", efh_image }, - { (gchar *) "image/x-portable-bitmap", efh_image }, - { (gchar *) "image/x-portable-graymap", efh_image }, - { (gchar *) "image/x-portable-pixmap", efh_image }, - { (gchar *) "image/x-xpixmap", efh_image }, - { (gchar *) "text/enriched", efh_text_enriched }, - { (gchar *) "text/plain", efh_text_plain }, - { (gchar *) "text/html", efh_text_html }, - { (gchar *) "text/richtext", efh_text_enriched }, - { (gchar *) "text/*", efh_text_plain }, - { (gchar *) "message/external-body", efh_message_external }, - { (gchar *) "message/delivery-status", efh_message_deliverystatus }, - { (gchar *) "multipart/related", efh_multipart_related }, + g_string_append (output_buffer, " ("); - /* This is where one adds those busted, non-registered types, - * that some idiot mailer writers out there decide to pull out - * of their proverbials at random. */ + while (!g_queue_is_empty (&valid)) { + CamelCipherCertInfo *cinfo; - { (gchar *) "image/jpg", efh_image }, - { (gchar *) "image/pjpeg", efh_image }, + cinfo = g_queue_pop_head (&valid); - /* special internal types */ + if (cinfo->name != NULL && *cinfo->name != '\0') { + g_string_append (output_buffer, cinfo->name); - { (gchar *) "x-evolution/message/rfc822", efh_format_message } -}; + if (cinfo->email != NULL && *cinfo->email != '\0') { + g_string_append (output_buffer, " <"); + g_string_append (output_buffer, cinfo->email); + g_string_append (output_buffer, ">"); + } -static void -efh_builtin_init (EMFormatHTMLClass *efhc) -{ - EMFormatClass *efc; - gint ii; + } else if (cinfo->email != NULL && *cinfo->email != '\0') { + g_string_append (output_buffer, cinfo->email); + } - efc = (EMFormatClass *) efhc; + if (!g_queue_is_empty (&valid)) + g_string_append (output_buffer, ", "); + } - for (ii = 0; ii < G_N_ELEMENTS (type_builtin_table); ii++) - em_format_class_add_handler ( - efc, &type_builtin_table[ii]); + g_string_append_c (output_buffer, ')'); } -/* ********************************************************************** */ - static void efh_format_text_header (EMFormatHTML *emfh, GString *buffer, @@ -2608,37 +2188,34 @@ efh_format_text_header (EMFormatHTML *emfh, html = value; is_rtl = gtk_widget_get_default_direction () == GTK_TEXT_DIR_RTL; - if (emfh->simple_headers) { - fmt = "%s: %s
"; + + if (flags & EM_FORMAT_HTML_HEADER_NOCOLUMNS) { + if (flags & EM_FORMAT_HEADER_BOLD) { + fmt = "%s: %s"; + } else { + fmt = "%s: %s"; + } + } else if (flags & EM_FORMAT_HTML_HEADER_NODEC) { + if (is_rtl) + fmt = "%2$s%1$s "; + else + fmt = "%s %s"; } else { - if (flags & EM_FORMAT_HTML_HEADER_NOCOLUMNS) { - if (flags & EM_FORMAT_HEADER_BOLD) { - fmt = "%s: %s"; - } else { - fmt = "%s: %s"; - } - } else if (flags & EM_FORMAT_HTML_HEADER_NODEC) { + if (flags & EM_FORMAT_HEADER_BOLD) { if (is_rtl) - fmt = "%2$s%1$s "; + fmt = "%2$s%1$s: "; else - fmt = "%s %s"; + fmt = "%s: %s"; } else { - - if (flags & EM_FORMAT_HEADER_BOLD) { - if (is_rtl) - fmt = "%2$s%1$s: "; - else - fmt = "%s: %s"; - } else { - if (is_rtl) - fmt = "%2$s%1$s: "; - else - fmt = "%s: %s"; - } + if (is_rtl) + fmt = "%2$s%1$s: "; + else + fmt = "%s: %s"; } } - g_string_append_printf (buffer, fmt, label, html); + g_string_append_printf (buffer, fmt, + (flags & EM_FORMAT_HTML_HEADER_HIDDEN ? "none" : "table-row"), label, html); g_free (mhtml); } @@ -2653,22 +2230,15 @@ static gchar * efh_format_address (EMFormatHTML *efh, GString *out, struct _camel_header_address *a, - gchar *field) + gchar *field, + gboolean no_links) { guint32 flags = CAMEL_MIME_FILTER_TOHTML_CONVERT_SPACES; gchar *name, *mailto, *addr; gint i = 0; - gboolean wrap = FALSE; gchar *str = NULL; gint limit = mail_config_get_address_count (); - if (field ) { - if ((!strcmp (field, _("To")) && !(efh->header_wrap_flags & EM_FORMAT_HTML_HEADER_TO)) - || (!strcmp (field, _("Cc")) && !(efh->header_wrap_flags & EM_FORMAT_HTML_HEADER_CC)) - || (!strcmp (field, _("Bcc")) && !(efh->header_wrap_flags & EM_FORMAT_HTML_HEADER_BCC))) - wrap = TRUE; - } - while (a) { if (a->name) name = camel_text_to_html (a->name, flags, 0); @@ -2700,7 +2270,10 @@ efh_format_address (EMFormatHTML *efh, mailto = camel_url_encode (a->v.addr, "?=&()"); } addr = camel_text_to_html (a->v.addr, flags, 0); - g_string_append_printf (out, "%s", mailto, addr); + if (no_links) + g_string_append_printf (out, "%s", addr); + else + g_string_append_printf (out, "%s", mailto, addr); g_free (mailto); g_free (addr); @@ -2709,7 +2282,7 @@ efh_format_address (EMFormatHTML *efh, break; case CAMEL_HEADER_ADDRESS_GROUP: g_string_append_printf (out, "%s: ", name); - efh_format_address (efh, out, a->v.members, field); + efh_format_address (efh, out, a->v.members, field, no_links); g_string_append_printf (out, ";"); break; default: @@ -2725,48 +2298,51 @@ efh_format_address (EMFormatHTML *efh, g_string_append (out, ", "); /* Let us add a '...' if we have more addresses */ - if (limit > 0 && wrap && a && (i > (limit - 1))) { - gchar *evolution_imagesdir = g_filename_to_uri (EVOLUTION_IMAGESDIR, NULL, NULL); - - if (!strcmp (field, _("To"))) { - g_string_append (out, "..."); - str = g_strdup_printf (" ", evolution_imagesdir); - } - else if (!strcmp (field, _("Cc"))) { - g_string_append (out, "..."); - str = g_strdup_printf (" ", evolution_imagesdir); - } - else if (!strcmp (field, _("Bcc"))) { - g_string_append (out, "..."); - str = g_strdup_printf (" ", evolution_imagesdir); + if (limit > 0 && (i == limit - 1)) { + const gchar *id = NULL; + + if (strcmp (field, _("To")) == 0) { + id = "to"; + } else if (strcmp (field, _("Cc")) == 0) { + id = "cc"; + } else if (strcmp (field, _("Bcc")) == 0) { + id = "bcc"; } - g_free (evolution_imagesdir); - - if (str) - return str; + if (id) { + g_string_append_printf (out, + "", id); + str = g_strdup_printf ( + "", + EVOLUTION_IMAGESDIR, id); + } } - } - if (limit > 0 && i > (limit)) { - gchar *evolution_imagesdir = g_filename_to_uri (EVOLUTION_IMAGESDIR, NULL, NULL); + if (str) { + const gchar *id = NULL; - if (!strcmp (field, _("To"))) { - str = g_strdup_printf (" ", evolution_imagesdir); - } - else if (!strcmp (field, _("Cc"))) { - str = g_strdup_printf (" ", evolution_imagesdir); - } - else if (!strcmp (field, _("Bcc"))) { - str = g_strdup_printf (" ", evolution_imagesdir); + if (strcmp (field, _("To")) == 0) { + id = "to"; + } else if (strcmp (field, _("Cc")) == 0) { + id = "cc"; + } else if (strcmp (field, _("Bcc")) == 0) { + id = "bcc"; } - g_free (evolution_imagesdir); + if (id) { + g_string_append_printf (out, + "" + "...", + id); + } } return str; - } static void @@ -2793,15 +2369,15 @@ canon_header_name (gchar *name) } } -static void -efh_format_header (EMFormat *emf, - GString *buffer, - CamelMedium *part, - struct _camel_header_raw *header, - guint32 flags, - const gchar *charset) +void +em_format_html_format_header (EMFormat *emf, + GString *buffer, + CamelMedium *part, + struct _camel_header_raw *header, + guint32 flags, + const gchar *charset) { - EMFormatHTML *efh = (EMFormatHTML *) emf; + EMFormatHTML *efh = EM_FORMAT_HTML (emf); gchar *name, *buf, *value = NULL; const gchar *label, *txt; gboolean addrspec = FALSE; @@ -2825,9 +2401,11 @@ efh_format_header (EMFormat *emf, struct _camel_header_address *addrs; GString *html; gchar *img; + const gchar *charset = em_format_get_charset (emf) ? + em_format_get_charset (emf) : em_format_get_default_charset (emf); buf = camel_header_unfold (header->value); - if (!(addrs = camel_header_address_decode (buf, emf->charset ? emf->charset : emf->default_charset))) { + if (!(addrs = camel_header_address_decode (buf, charset))) { g_free (buf); return; } @@ -2835,7 +2413,8 @@ efh_format_header (EMFormat *emf, g_free (buf); html = g_string_new(""); - img = efh_format_address (efh, html, addrs, (gchar *) label); + img = efh_format_address (efh, html, addrs, (gchar *) label, + (flags & EM_FORMAT_HTML_HEADER_NOLINKS)); if (img) { str_field = g_strdup_printf ("%s%s:", img, label); @@ -2921,7 +2500,11 @@ efh_format_header (EMFormat *emf, html = g_string_new(""); scan = ng; while (scan) { - g_string_append_printf(html, "%s", scan->newsgroup, scan->newsgroup); + if (flags & EM_FORMAT_HTML_HEADER_NOLINKS) + g_string_append_printf (html, "%s", scan->newsgroup); + else + g_string_append_printf(html, "%s", + scan->newsgroup, scan->newsgroup); scan = scan->next; if (scan) g_string_append_printf(html, ", "); @@ -2949,100 +2532,129 @@ efh_format_header (EMFormat *emf, } static void -efh_format_headers (EMFormatHTML *efh, - GString *buffer, - CamelMedium *part, - GCancellable *cancellable) +efh_format_short_headers (EMFormatHTML *efh, + GString *buffer, + CamelMedium *part, + gboolean visible, + GCancellable *cancellable) { - EMFormat *emf = (EMFormat *) efh; + EMFormat *emf = EM_FORMAT (efh); const gchar *charset; CamelContentType *ct; - struct _camel_header_raw *header; - gboolean have_icon = FALSE; - const gchar *photo_name = NULL; - CamelInternetAddress *cia = NULL; - gboolean face_decoded = FALSE, contact_has_photo = FALSE; - guchar *face_header_value = NULL; - gsize face_header_len = 0; - gchar *header_sender = NULL, *header_from = NULL, *name; - gboolean mail_from_delegate = FALSE; const gchar *hdr_charset; gchar *evolution_imagesdir; + gchar *subject = NULL; + struct _camel_header_address *addrs = NULL; + struct _camel_header_raw *header; + GString *from; + gboolean is_rtl; - if (!part) + if (cancellable && g_cancellable_is_cancelled (cancellable)) return; ct = camel_mime_part_get_content_type ((CamelMimePart *) part); charset = camel_content_type_param (ct, "charset"); charset = camel_iconv_charset_name (charset); + hdr_charset = em_format_get_charset (emf) ? + em_format_get_charset (emf) : em_format_get_default_charset (emf); - if (!efh->simple_headers) - g_string_append_printf ( - buffer, "\n" - "", - e_color_to_value ( - &efh->priv->colors[ - EM_FORMAT_HTML_COLOR_HEADER])); - - hdr_charset = emf->charset ? emf->charset : emf->default_charset; evolution_imagesdir = g_filename_to_uri (EVOLUTION_IMAGESDIR, NULL, NULL); + from = g_string_new (""); - /* If the header is collapsed, display just subject and sender in one row and leave */ - if (efh->priv->headers_state == EM_FORMAT_HTML_HEADERS_STATE_COLLAPSED && efh->priv->headers_collapsable) { - gchar *subject = NULL; - struct _camel_header_address *addrs = NULL; - GString *from = g_string_new (""); + g_string_append_printf (buffer, + "
", + visible ? "block" : "none"); - header = ((CamelMimePart *) part)->headers; - while (header) { - if (!g_ascii_strcasecmp (header->name, "From")) { - GString *tmp; - if (!(addrs = camel_header_address_decode (header->value, hdr_charset))) { - header = header->next; - continue; - } - tmp = g_string_new (""); - efh_format_address (efh, tmp, addrs, header->name); - - if (tmp->len) - g_string_printf (from, _("From: %s"), tmp->str); - g_string_free (tmp, TRUE); - } else if (!g_ascii_strcasecmp (header->name, "Subject")) { - gchar *buf = NULL; - buf = camel_header_unfold (header->value); - g_free (subject); - subject = camel_header_decode_string (buf, hdr_charset); - g_free (buf); + header = ((CamelMimePart *) part)->headers; + while (header) { + if (!g_ascii_strcasecmp (header->name, "From")) { + GString *tmp; + if (!(addrs = camel_header_address_decode (header->value, hdr_charset))) { + header = header->next; + continue; } - header = header->next; + tmp = g_string_new (""); + efh_format_address (efh, tmp, addrs, header->name, FALSE); + + if (tmp->len) + g_string_printf (from, _("From: %s"), tmp->str); + g_string_free (tmp, TRUE); + + } else if (!g_ascii_strcasecmp (header->name, "Subject")) { + gchar *buf = NULL; + subject = camel_header_unfold (header->value); + buf = camel_header_decode_string (subject, hdr_charset); + g_free (subject); + subject = camel_text_to_html (buf, CAMEL_MIME_FILTER_TOHTML_PRESERVE_8BIT, 0); + g_free (buf); } + header = header->next; + } + is_rtl = gtk_widget_get_default_direction () == GTK_TEXT_DIR_RTL; + if (is_rtl) { g_string_append_printf ( buffer, - "" - "" - "" - "", - evolution_imagesdir, - subject ? subject : _("(no subject)"), - from->len ? "(" : "", - from->str, - from->len ? ")" : ""); - - g_free (subject); - if (addrs) - camel_header_address_list_clear (&addrs); - g_string_free (from, TRUE); + "", + from->len ? "(" : "", from->str, from->len ? ")" : "", + subject ? subject : _("(no subject)")); + } else { + g_string_append_printf ( + buffer, + "", + subject ? subject : _("(no subject)"), + from->len ? "(" : "", from->str, from->len ? ")" : ""); + } + + g_string_append (buffer, "
" - "" - "" - "%s %s%s%s
%s%s%s %s
%s %s%s%s
"); + + g_free (subject); + if (addrs) + camel_header_address_list_clear (&addrs); - g_string_append (buffer, ""); + g_string_free (from, TRUE); + g_free (evolution_imagesdir); +} - g_free (evolution_imagesdir); +static void +efh_format_full_headers (EMFormatHTML *efh, + GString *buffer, + CamelMedium *part, + gboolean all_headers, + gboolean visible, + GCancellable *cancellable) +{ + EMFormat *emf = EM_FORMAT (efh); + const gchar *charset; + CamelContentType *ct; + struct _camel_header_raw *header; + gboolean have_icon = FALSE; + const gchar *photo_name = NULL; + CamelInternetAddress *cia = NULL; + gboolean face_decoded = FALSE, contact_has_photo = FALSE; + guchar *face_header_value = NULL; + gsize face_header_len = 0; + gchar *header_sender = NULL, *header_from = NULL, *name; + gboolean mail_from_delegate = FALSE; + const gchar *hdr_charset; + gchar *evolution_imagesdir; + if (cancellable && g_cancellable_is_cancelled (cancellable)) return; - } + + ct = camel_mime_part_get_content_type ((CamelMimePart *) part); + charset = camel_content_type_param (ct, "charset"); + charset = camel_iconv_charset_name (charset); + hdr_charset = em_format_get_charset (emf) ? + em_format_get_charset (emf) : em_format_get_default_charset (emf); + + evolution_imagesdir = g_filename_to_uri (EVOLUTION_IMAGESDIR, NULL, NULL); + + g_string_append_printf (buffer, + "", + visible ? "block" : "none"); header = ((CamelMimePart *) part)->headers; while (header) { @@ -3054,7 +2666,7 @@ efh_format_headers (EMFormatHTML *efh, break; html = g_string_new(""); - name = efh_format_address (efh, html, addrs, header->name); + name = efh_format_address (efh, html, addrs, header->name, FALSE); header_sender = html->str; camel_header_address_list_clear (&addrs); @@ -3069,7 +2681,7 @@ efh_format_headers (EMFormatHTML *efh, break; html = g_string_new(""); - name = efh_format_address (efh, html, addrs, header->name); + name = efh_format_address (efh, html, addrs, header->name, FALSE); header_from = html->str; camel_header_address_list_clear (&addrs); @@ -3113,50 +2725,15 @@ efh_format_headers (EMFormatHTML *efh, g_free (header_sender); g_free (header_from); - if (gtk_widget_get_default_direction () == GTK_TEXT_DIR_RTL) { - if (efh->priv->headers_collapsable) - g_string_append_printf ( - buffer, - "" - "" - "
" - "" - "" - "\n", - evolution_imagesdir); - else - g_string_append ( - buffer, - "
" - "\n"); - - } else { - if (efh->priv->headers_collapsable) - g_string_append_printf ( - buffer, - "" - "" - "", + classid); - handle = em_format_find_handler(emf, "x-evolution/message/post-header"); - if (handle) - handle->handler ( - emf, stream, part, handle, cancellable, FALSE); + puri = em_format_puri_new ( + emf, sizeof (EMFormatPURI), part, classid); + puri->write_func = efh_write_image; + em_format_add_puri (emf, puri); - camel_stream_write_string ( - stream, EM_FORMAT_HTML_VPAD, cancellable, NULL); - em_format_part (emf, stream, part, cancellable); + g_object_unref (part); + g_free (face_header_value); + } - if (emf->message != (CamelMimeMessage *) part) - camel_stream_write_string ( - stream, "\n", cancellable, NULL); + if (have_icon && efh->show_icon) { + GtkIconInfo *icon_info; + const gchar *classid; + CamelMimePart *iconpart = NULL; + EMFormatPURI *puri; - camel_cipher_validity_free (emf->valid); + classid = "icon:///em-format-html/header/icon"; + g_string_append_printf ( + buffer, + "", + classid); + icon_info = gtk_icon_theme_lookup_icon ( + gtk_icon_theme_get_default (), + "evolution", 16, GTK_ICON_LOOKUP_NO_SVG); + if (icon_info != NULL) { + iconpart = em_format_html_file_part ( + (EMFormatHTML *) emf, "image/png", + gtk_icon_info_get_filename (icon_info), + cancellable); + gtk_icon_info_free (icon_info); + } + if (iconpart) { + puri = em_format_puri_new ( + emf, sizeof (EMFormatPURI), iconpart, classid); + puri->write_func = efh_write_image; + em_format_add_puri (emf, puri); + g_object_unref (iconpart); + } + } - emf->valid = save; - emf->valid_parent = save_parent; + g_string_append (buffer, "
" - "" - "" - "\n", - evolution_imagesdir); - else - g_string_append ( - buffer, - ""); - if (!contact_has_photo && face_decoded) { - gchar *classid; - CamelMimePart *part; - - part = camel_mime_part_new (); - camel_mime_part_set_content ( - (CamelMimePart *) part, - (const gchar *) face_header_value, - face_header_len, "image/png"); - classid = g_strdup_printf ( - "icon:///em-format-html/face/photo/header"); - g_string_append_printf ( - buffer, - "", - classid); - em_format_add_puri ( - emf, sizeof (EMFormatPURI), - classid, part, efh_write_image); - g_object_unref (part); - } + if (photo_name) { + const gchar *classid; + CamelMimePart *photopart; + gboolean only_local_photo; - if (have_icon && efh->show_icon) { - GtkIconInfo *icon_info; - gchar *classid; - CamelMimePart *iconpart = NULL; + cia = camel_internet_address_new (); + camel_address_decode ((CamelAddress *) cia, (const gchar *) photo_name); + only_local_photo = em_format_html_get_only_local_photos (efh); + photopart = em_utils_contact_photo (cia, only_local_photo); - classid = g_strdup_printf ( - "icon:///em-format-html/%s/icon/header", - emf->part_id->str); + if (photopart) { + EMFormatPURI *puri; + contact_has_photo = TRUE; + classid = "icon:///em-format-html/headers/photo"; g_string_append_printf ( buffer, "", + "", classid); - - icon_info = gtk_icon_theme_lookup_icon ( - gtk_icon_theme_get_default (), - "evolution", 16, GTK_ICON_LOOKUP_NO_SVG); - if (icon_info != NULL) { - iconpart = em_format_html_file_part ( - (EMFormatHTML *) emf, "image/png", - gtk_icon_info_get_filename (icon_info), - cancellable); - gtk_icon_info_free (icon_info); - } - - if (iconpart) { - em_format_add_puri ( - emf, sizeof (EMFormatPURI), - classid, iconpart, efh_write_image); - g_object_unref (iconpart); - } - g_free (classid); + puri = em_format_puri_new ( + emf, sizeof (EMFormatPURI), photopart, classid); + puri->write_func = efh_write_image; + em_format_add_puri (emf, puri); + g_object_unref (photopart); } - - g_string_append (buffer, "
" - "\n"); - } + g_string_append (buffer, ""); - - if (photo_name) { - gchar *classid; - CamelMimePart *photopart; - gboolean only_local_photo; - - cia = camel_internet_address_new (); - camel_address_decode ((CamelAddress *) cia, (const gchar *) photo_name); - only_local_photo = em_format_html_get_only_local_photos (efh); - photopart = em_utils_contact_photo (cia, only_local_photo); - - if (photopart) { - contact_has_photo = TRUE; - classid = g_strdup_printf ( - "icon:///em-format-html/%s/photo/header", - emf->part_id->str); - g_string_append_printf ( - buffer, - "", - classid); - em_format_add_puri (emf, sizeof (EMFormatPURI), classid, - photopart, efh_write_image); - g_object_unref (photopart); - - g_free (classid); - } - g_object_unref (cia); - } + g_string_append (buffer, "
\n"); g_free (evolution_imagesdir); /* dump selected headers */ - if (emf->mode == EM_FORMAT_MODE_ALLHEADERS) { + if (all_headers) { header = ((CamelMimePart *) part)->headers; while (header) { - efh_format_header ( + em_format_html_format_header ( emf, buffer, part, header, EM_FORMAT_HTML_HEADER_NOCOLUMNS, charset); header = header->next; @@ -3205,7 +2782,7 @@ efh_format_headers (EMFormatHTML *efh, xmailer.value = use_header->value; mailer_shown = TRUE; - efh_format_header ( + em_format_html_format_header ( emf, buffer, part, &xmailer, h->flags, charset); if (strstr(use_header->value, "Evolution")) @@ -3226,7 +2803,7 @@ efh_format_headers (EMFormatHTML *efh, face_decoded = TRUE; /* Showing an encoded "Face" header makes little sense */ } else if (!g_ascii_strcasecmp (header->name, h->name) && !face) { - efh_format_header ( + em_format_html_format_header ( emf, buffer, part, header, h->flags, charset); } @@ -3238,214 +2815,162 @@ efh_format_headers (EMFormatHTML *efh, } } - if (!efh->simple_headers) { - g_string_append (buffer, "
" - "
" - "" - "
\n\n"); + g_object_unref (cia); } -} - -static void -efh_format_message (EMFormat *emf, - CamelStream *stream, - CamelMimePart *part, - const EMFormatHandler *info, - GCancellable *cancellable, - gboolean is_fallback) -{ - const EMFormatHandler *handle; - GString *buffer; - - /* TODO: make this validity stuff a method */ - EMFormatHTML *efh = (EMFormatHTML *) emf; - CamelCipherValidity *save = emf->valid, *save_parent = emf->valid_parent; - - emf->valid = NULL; - emf->valid_parent = NULL; - - buffer = g_string_sized_new (1024); - - if (emf->message != (CamelMimeMessage *) part) - g_string_append (buffer, "
\n"); - if (!efh->hide_headers) - efh_format_headers ( - efh, buffer, CAMEL_MEDIUM (part), cancellable); - - camel_stream_write ( - stream, buffer->str, buffer->len, cancellable, NULL); - - g_string_free (buffer, TRUE); + if (!contact_has_photo && face_decoded) { + const gchar *classid; + CamelMimePart *part; + EMFormatPURI *puri; + + part = camel_mime_part_new (); + camel_mime_part_set_content ( + (CamelMimePart *) part, + (const gchar *) face_header_value, + face_header_len, "image/png"); + classid = "icon:///em-format-html/headers/face/photo"; + g_string_append_printf ( + buffer, + "
" + "" + "
"); } -void -em_format_html_format_cert_infos (GQueue *cert_infos, - GString *output_buffer) +gboolean +em_format_html_can_load_images (EMFormatHTML *efh) { - GQueue valid = G_QUEUE_INIT; - GList *head, *link; + g_return_val_if_fail (EM_IS_FORMAT_HTML (efh), FALSE); - g_return_if_fail (cert_infos != NULL); - g_return_if_fail (output_buffer != NULL); + return ((efh->priv->image_loading_policy == E_MAIL_IMAGE_LOADING_POLICY_ALWAYS) || + ((efh->priv->image_loading_policy == E_MAIL_IMAGE_LOADING_POLICY_SOMETIMES) && + efh->priv->can_load_images)); +} - head = g_queue_peek_head_link (cert_infos); +void +em_format_html_animation_extract_frame (const GByteArray *anim, + gchar **frame, + gsize *len) +{ + GdkPixbufLoader *loader; + GdkPixbufAnimation *animation; + GdkPixbuf *frame_buf; + + /* GIF89a (GIF image signature) */ + const gchar GIF_HEADER[] = { 0x47, 0x49, 0x46, 0x38, 0x39, 0x61 }; + const gint GIF_HEADER_LEN = sizeof (GIF_HEADER); + + /* NETSCAPE2.0 (extension describing animated GIF, starts on 0x310) */ + const gchar GIF_APPEXT[] = { 0x4E, 0x45, 0x54, 0x53, 0x43, 0x41, + 0x50, 0x45, 0x32, 0x2E, 0x30 }; + const gint GIF_APPEXT_LEN = sizeof (GIF_APPEXT); + + /* Check if the image is an animated GIF. We don't care about any + * other animated formats (APNG or MNG) as WebKit does not support them + * and displays only the first frame. */ + if ((anim->len < 0x331) + || (memcmp (anim->data, GIF_HEADER, GIF_HEADER_LEN) != 0) + || (memcmp (&anim->data[0x310], GIF_APPEXT, GIF_APPEXT_LEN) != 0)) { + + *frame = g_memdup (anim->data, anim->len); + *len = anim->len; + return; + } - /* Make sure we have a valid CamelCipherCertInfo before - * appending anything to the output buffer, so we don't - * end up with "()". */ - for (link = head; link != NULL; link = g_list_next (link)) { - CamelCipherCertInfo *cinfo = link->data; + loader = gdk_pixbuf_loader_new (); + gdk_pixbuf_loader_write (loader, (guchar *) anim->data, anim->len, NULL); + gdk_pixbuf_loader_close (loader, NULL); + animation = gdk_pixbuf_loader_get_animation (loader); + if (!animation) { - if ((cinfo->name != NULL && *cinfo->name != '\0') || - (cinfo->email != NULL && *cinfo->email != '\0')) - g_queue_push_tail (&valid, cinfo); + *frame = g_memdup (anim->data, anim->len); + *len = anim->len; + g_object_unref (loader); + return; } - if (g_queue_is_empty (&valid)) + /* Extract first frame */ + frame_buf = gdk_pixbuf_animation_get_static_image (animation); + if (!frame_buf) { + *frame = g_memdup (anim->data, anim->len); + *len = anim->len; + g_object_unref (loader); + g_object_unref (animation); return; - - g_string_append (output_buffer, " ("); - - while (!g_queue_is_empty (&valid)) { - CamelCipherCertInfo *cinfo; - - cinfo = g_queue_pop_head (&valid); - - if (cinfo->name != NULL && *cinfo->name != '\0') { - g_string_append (output_buffer, cinfo->name); - - if (cinfo->email != NULL && *cinfo->email != '\0') { - g_string_append (output_buffer, " <"); - g_string_append (output_buffer, cinfo->email); - g_string_append (output_buffer, ">"); - } - - } else if (cinfo->email != NULL && *cinfo->email != '\0') { - g_string_append (output_buffer, cinfo->email); - } - - if (!g_queue_is_empty (&valid)) - g_string_append (output_buffer, ", "); } - g_string_append_c (output_buffer, ')'); -} - -/* unref returned pointer with g_object_unref(), if not NULL */ -CamelStream * -em_format_html_get_cached_image (EMFormatHTML *efh, - const gchar *image_uri) -{ - g_return_val_if_fail (efh != NULL, NULL); - g_return_val_if_fail (image_uri != NULL, NULL); - - if (!emfh_http_cache) - return NULL; + /* Unforunatelly, GdkPixbuf cannot save to GIF, but WebKit does not + * have any trouble displaying PNG image despite the part having + * image/gif mime-type */ + gdk_pixbuf_save_to_buffer (frame_buf, frame, len, "png", NULL, NULL); - return camel_data_cache_get ( - emfh_http_cache, EMFH_HTTP_CACHE_PATH, image_uri, NULL); + g_object_unref (loader); } - diff --git a/mail/em-format-html.h b/mail/em-format-html.h index bc6a171255..9749c37f31 100644 --- a/mail/em-format-html.h +++ b/mail/em-format-html.h @@ -30,7 +30,6 @@ #include #include -#include #include /* Standard GObject macros */ @@ -57,6 +56,7 @@ G_BEGIN_DECLS typedef struct _EMFormatHTML EMFormatHTML; typedef struct _EMFormatHTMLClass EMFormatHTMLClass; typedef struct _EMFormatHTMLPrivate EMFormatHTMLPrivate; +typedef struct _EMFormatWidgetPURI EMFormatWidgetPURI; enum _em_format_html_header_flags { EM_FORMAT_HTML_HEADER_TO = 1 << 0, @@ -64,16 +64,6 @@ enum _em_format_html_header_flags { EM_FORMAT_HTML_HEADER_BCC = 1 << 2 }; -typedef enum { - EM_FORMAT_HTML_STATE_NONE = 0, - EM_FORMAT_HTML_STATE_RENDERING -} EMFormatHTMLState; - -typedef enum { - EM_FORMAT_HTML_HEADERS_STATE_EXPANDED = 0, /* Default value */ - EM_FORMAT_HTML_HEADERS_STATE_COLLAPSED -} EMFormatHTMLHeadersState; - typedef enum { EM_FORMAT_HTML_COLOR_BODY, /* header area background */ EM_FORMAT_HTML_COLOR_CITATION, /* citation font color */ @@ -84,94 +74,13 @@ typedef enum { EM_FORMAT_HTML_NUM_COLOR_TYPES } EMFormatHTMLColorType; -/* A HTMLJob will be executed in another thread, in sequence. - * It's job is to write to its stream, close it if successful, - * then exit. */ - -typedef struct _EMFormatHTMLJob EMFormatHTMLJob; - -typedef void (*EMFormatHTMLJobCallback) (EMFormatHTMLJob *job, - GCancellable *cancellable); - -/** - * struct _EMFormatHTMLJob - A formatting job. - * - * @format: Set by allocation function. - * @stream: Free for use by caller. - * @puri_level: Set by allocation function. - * @base: Set by allocation function, used to save state. - * @callback: This callback will always be invoked, only once, even if the user - * cancelled the display. So the callback should free any extra data - * it allocated every time it is called. - * @u: Union data, free for caller to use. - * - * This object is used to queue a long-running-task which cannot be - * processed in the primary thread. When its turn comes, the job will - * be de-queued and the @callback invoked to perform its processing, - * restoring various state to match the original state. This is used - * for image loading and other internal tasks. - * - * This object is struct-subclassable. Only em_format_html_job_new() - * may be used to allocate these. - **/ -struct _EMFormatHTMLJob { - EMFormatHTML *format; - CamelStream *stream; - - /* We need to track the state of the visibility tree at - * the point this uri was generated */ - GNode *puri_level; - CamelURL *base; - - EMFormatHTMLJobCallback callback; - union { - gchar *uri; - CamelMedium *msg; - EMFormatPURI *puri; - GNode *puri_level; - gpointer data; - } u; -}; - -/* Pending object (classid: url) */ -typedef struct _EMFormatHTMLPObject EMFormatHTMLPObject; - -typedef gboolean - (*EMFormatHTMLPObjectFunc) (EMFormatHTML *md, - GtkHTMLEmbedded *eb, - EMFormatHTMLPObject *pobject); - -/** - * struct _EMFormatHTMLPObject - Pending object. - * - * @free: Invoked when the object is no longer needed. - * @format: The parent formatter. - * @classid: The assigned class id as passed to add_pobject(). - * @func: Callback function. - * @part: The part as passed to add_pobject(). - * - * This structure is used to track OBJECT tags which have been - * inserted into the HTML stream. When GtkHTML requests them the - * @func will be invoked to create the embedded widget. - * - * This object is struct-subclassable. Only - * em_format_html_add_pobject() may be used to allocate these. - **/ -struct _EMFormatHTMLPObject { - void (*free)(EMFormatHTMLPObject *); - EMFormatHTML *format; - - gchar *classid; - - EMFormatHTMLPObjectFunc func; - CamelMimePart *part; -}; - #define EM_FORMAT_HTML_HEADER_NOCOLUMNS (EM_FORMAT_HEADER_LAST) /* header already in html format */ #define EM_FORMAT_HTML_HEADER_HTML (EM_FORMAT_HEADER_LAST<<1) #define EM_FORMAT_HTML_HEADER_NODEC (EM_FORMAT_HEADER_LAST<<2) +#define EM_FORMAT_HTML_HEADER_NOLINKS (EM_FORMAT_HEADER_LAST<<3) +#define EM_FORMAT_HTML_HEADER_HIDDEN (EM_FORMAT_HEADER_LAST<<4) #define EM_FORMAT_HTML_HEADER_LAST (EM_FORMAT_HEADER_LAST<<8) @@ -197,14 +106,13 @@ struct _EMFormatHTMLPObject { * @load_http:2: * @load_http_now:1: * @mark_citations:1: - * @simple_headers:1: * @hide_headers:1: * @show_icon:1: * * Most of these fields are private or read-only. * * The base HTML formatter object. This object drives HTML generation - * into a GtkHTML parser. It also handles text to HTML conversion, + * into a WebKit parser. It also handles text to HTML conversion, * multipart/related objects and inline images. **/ struct _EMFormatHTML { @@ -216,12 +124,9 @@ struct _EMFormatHTML { GSList *headers; guint32 text_html_flags; /* default flags for text to html conversion */ - guint simple_headers:1; /* simple header format, no box/table */ guint hide_headers:1; /* no headers at all */ guint show_icon:1; /* show an icon when the sender used Evo */ guint32 header_wrap_flags; - - EMFormatHTMLState state; /* actual state of the object */ }; struct _EMFormatHTMLClass { @@ -231,8 +136,6 @@ struct _EMFormatHTMLClass { }; GType em_format_html_get_type (void); -EWebView * em_format_html_get_web_view (EMFormatHTML *efh); -void em_format_html_load_images (EMFormatHTML *efh); void em_format_html_get_color (EMFormatHTML *efh, EMFormatHTMLColorType type, GdkColor *color); @@ -260,65 +163,61 @@ gboolean em_format_html_get_show_sender_photo void em_format_html_set_show_sender_photo (EMFormatHTML *efh, gboolean show_sender_photo); - -/* retrieves a pseudo-part icon wrapper for a file */ -CamelMimePart * em_format_html_file_part (EMFormatHTML *efh, - const gchar *mime_type, - const gchar *filename, - GCancellable *cancellable); - -/* for implementers */ -EMFormatHTMLPObject * - em_format_html_add_pobject (EMFormatHTML *efh, - gsize size, - const gchar *classid, - CamelMimePart *part, - EMFormatHTMLPObjectFunc func); -EMFormatHTMLPObject * - em_format_html_find_pobject (EMFormatHTML *efh, - const gchar *classid); -EMFormatHTMLPObject * - em_format_html_find_pobject_func - (EMFormatHTML *efh, - CamelMimePart *part, - EMFormatHTMLPObjectFunc func); -void em_format_html_remove_pobject (EMFormatHTML *efh, - EMFormatHTMLPObject *pobject); -void em_format_html_clear_pobject (EMFormatHTML *efh); -EMFormatHTMLJob * - em_format_html_job_new (EMFormatHTML *efh, - EMFormatHTMLJobCallback callback, - gpointer data); -void em_format_html_job_queue (EMFormatHTML *efh, - EMFormatHTMLJob *job); +gboolean em_format_html_get_animate_images + (EMFormatHTML *efh); +void em_format_html_set_animate_images + (EMFormatHTML *efh, + gboolean animate_images); void em_format_html_clone_sync (CamelFolder *folder, const gchar *message_uid, CamelMimeMessage *message, EMFormatHTML *efh, EMFormat *source); - gboolean em_format_html_get_show_real_date (EMFormatHTML *efh); void em_format_html_set_show_real_date (EMFormatHTML *efh, gboolean show_real_date); -EMFormatHTMLHeadersState - em_format_html_get_headers_state - (EMFormatHTML *efh); -void em_format_html_set_headers_state - (EMFormatHTML *efh, - EMFormatHTMLHeadersState state); -gboolean em_format_html_get_headers_collapsable - (EMFormatHTML *efh); -void em_format_html_set_headers_collapsable - (EMFormatHTML *efh, - gboolean collapsable); + +/* retrieves a pseudo-part icon wrapper for a file */ +CamelMimePart * em_format_html_file_part (EMFormatHTML *efh, + const gchar *mime_type, + const gchar *filename, + GCancellable *cancellable); + void em_format_html_format_cert_infos (GQueue *cert_infos, GString *output_buffer); -CamelStream * em_format_html_get_cached_image (EMFormatHTML *efh, - const gchar *image_uri); +void em_format_html_format_message (EMFormatHTML *efh, + CamelStream *stream, + GCancellable *cancellable); + +void em_format_html_format_message_part + (EMFormatHTML *efh, + const gchar *part_id, + CamelStream *stream, + GCancellable *cancellable); + +void em_format_html_format_headers (EMFormatHTML *efh, + CamelStream *stream, + CamelMedium *part, + gboolean all_headers, + GCancellable *cancellable); +void em_format_html_format_header (EMFormat *emf, + GString *buffer, + CamelMedium *part, + struct _camel_header_raw *header, + guint32 flags, + const gchar *charset); + +gboolean em_format_html_can_load_images (EMFormatHTML *efh); + +void em_format_html_animation_extract_frame + (const GByteArray *anim, + gchar **frame, + gsize *len); + G_END_DECLS #endif /* EM_FORMAT_HTML_H */ diff --git a/mail/em-html-stream.c b/mail/em-html-stream.c deleted file mode 100644 index a633946541..0000000000 --- a/mail/em-html-stream.c +++ /dev/null @@ -1,182 +0,0 @@ -/* - * - * This program is free software; you can redistribute it and/or - * modify it under the terms of the GNU Lesser General Public - * License as published by the Free Software Foundation; either - * version 2 of the License, or (at your option) version 3. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public - * License along with the program; if not, see - * - * - * Authors: - * Jeffrey Stedfast - * Michael Zucchi - * - * Copyright (C) 1999-2008 Novell, Inc. (www.novell.com) - * - */ - -#ifdef HAVE_CONFIG_H -#include -#endif - -#include -#include -#include -#include "em-html-stream.h" - -#define d(x) - -G_DEFINE_TYPE (EMHTMLStream, em_html_stream, EM_TYPE_SYNC_STREAM) - -static void -html_stream_cleanup (EMHTMLStream *emhs) -{ - if (emhs->sync.cancel && emhs->html_stream) - gtk_html_stream_close ( - emhs->html_stream, GTK_HTML_STREAM_ERROR); - - emhs->html_stream = NULL; - emhs->sync.cancel = TRUE; - g_signal_handler_disconnect (emhs->html, emhs->destroy_id); - g_object_unref (emhs->html); - emhs->html = NULL; -} - -static void -html_stream_gtkhtml_destroy (GtkHTML *html, - EMHTMLStream *emhs) -{ - emhs->sync.cancel = TRUE; - html_stream_cleanup (emhs); -} - -static void -html_stream_dispose (GObject *object) -{ - EMHTMLStream *emhs = EM_HTML_STREAM (object); - - if (emhs->html_stream) { - /* set 'in finalize' flag */ - camel_stream_close (CAMEL_STREAM (emhs), NULL, NULL); - } -} - -static gssize -html_stream_sync_write (CamelStream *stream, - const gchar *buffer, - gsize n, - GError **error) -{ - EMHTMLStream *emhs = EM_HTML_STREAM (stream); - - if (emhs->html == NULL) { - g_set_error ( - error, CAMEL_ERROR, CAMEL_ERROR_GENERIC, - _("No HTML stream available")); - return -1; - } - - if (emhs->html_stream == NULL) - emhs->html_stream = gtk_html_begin_full ( - emhs->html, NULL, NULL, emhs->flags); - - gtk_html_stream_write (emhs->html_stream, buffer, n); - - return (gssize) n; -} - -static gint -html_stream_sync_flush (CamelStream *stream, - GError **error) -{ - EMHTMLStream *emhs = (EMHTMLStream *) stream; - - if (emhs->html_stream == NULL) { - g_set_error ( - error, CAMEL_ERROR, CAMEL_ERROR_GENERIC, - _("No HTML stream available")); - return -1; - } - - gtk_html_flush (emhs->html); - - return 0; -} - -static gint -html_stream_sync_close (CamelStream *stream, - GError **error) -{ - EMHTMLStream *emhs = (EMHTMLStream *) stream; - - if (emhs->html_stream == NULL) { - g_set_error ( - error, CAMEL_ERROR, CAMEL_ERROR_GENERIC, - _("No HTML stream available")); - return -1; - } - - gtk_html_stream_close (emhs->html_stream, GTK_HTML_STREAM_OK); - html_stream_cleanup (emhs); - - return 0; -} - -static void -em_html_stream_class_init (EMHTMLStreamClass *class) -{ - GObjectClass *object_class; - EMSyncStreamClass *sync_stream_class; - - object_class = G_OBJECT_CLASS (class); - object_class->dispose = html_stream_dispose; - - sync_stream_class = EM_SYNC_STREAM_CLASS (class); - sync_stream_class->sync_write = html_stream_sync_write; - sync_stream_class->sync_flush = html_stream_sync_flush; - sync_stream_class->sync_close = html_stream_sync_close; -} - -static void -em_html_stream_init (EMHTMLStream *emhs) -{ -} - -/* TODO: Could pass NULL for html_stream, and do a gtk_html_begin - * on first data -> less flashing */ -CamelStream * -em_html_stream_new (GtkHTML *html, - GtkHTMLStream *html_stream) -{ - EMHTMLStream *new; - - g_return_val_if_fail (GTK_IS_HTML (html), NULL); - - new = g_object_new (EM_TYPE_HTML_STREAM, NULL); - new->html_stream = html_stream; - new->html = g_object_ref (html); - new->flags = 0; - new->destroy_id = g_signal_connect ( - html, "destroy", - G_CALLBACK (html_stream_gtkhtml_destroy), new); - - em_sync_stream_set_buffer_size (&new->sync, 8192); - - return CAMEL_STREAM (new); -} - -void -em_html_stream_set_flags (EMHTMLStream *emhs, - GtkHTMLBeginFlags flags) -{ - g_return_if_fail (EM_IS_HTML_STREAM (emhs)); - - emhs->flags = flags; -} diff --git a/mail/em-html-stream.h b/mail/em-html-stream.h deleted file mode 100644 index 24d32f76d8..0000000000 --- a/mail/em-html-stream.h +++ /dev/null @@ -1,77 +0,0 @@ -/* - * - * This program is free software; you can redistribute it and/or - * modify it under the terms of the GNU Lesser General Public - * License as published by the Free Software Foundation; either - * version 2 of the License, or (at your option) version 3. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public - * License along with the program; if not, see - * - * - * Authors: - * Jeffrey Stedfast - * - * Copyright (C) 1999-2008 Novell, Inc. (www.novell.com) - * - */ - -#ifndef EM_HTML_STREAM_H -#define EM_HTML_STREAM_H - -#include -#include -#include - -/* Standard GObject macros */ -#define EM_TYPE_HTML_STREAM \ - (em_html_stream_get_type ()) -#define EM_HTML_STREAM(obj) \ - (G_TYPE_CHECK_INSTANCE_CAST \ - ((obj), EM_TYPE_HTML_STREAM, EMHTMLStream)) -#define EM_HTML_STREAM_CLASS(cls) \ - (G_TYPE_CHECK_CLASS_CAST \ - ((cls), EM_TYPE_HTML_STREAM, EMHTMLStreamClass)) -#define EM_IS_HTML_STREAM(obj) \ - (G_TYPE_CHECK_INSTANCE_TYPE \ - ((obj), EM_TYPE_HTML_STREAM)) -#define EM_IS_HTML_STREAM_CLASS(cls) \ - (G_TYPE_CHECK_CLASS_TYPE \ - ((cls), EM_TYPE_HTML_STREAM)) -#define EM_HTML_STREAM_GET_CLASS(obj) \ - (G_TYPE_INSTANCE_GET_CLASS \ - ((obj), EM_TYPE_HTML_STREAM, EMHTMLStreamClass)) - -G_BEGIN_DECLS - -typedef struct _EMHTMLStream EMHTMLStream; -typedef struct _EMHTMLStreamClass EMHTMLStreamClass; - -struct _EMHTMLStream { - EMSyncStream sync; - - guint destroy_id; - GtkHTML *html; - GtkHTMLStream *html_stream; - GtkHTMLBeginFlags flags; -}; - -struct _EMHTMLStreamClass { - EMSyncStreamClass parent_class; - -}; - -GType em_html_stream_get_type (void); -CamelStream * em_html_stream_new (GtkHTML *html, - GtkHTMLStream *html_stream); -void em_html_stream_set_flags (EMHTMLStream *emhs, - GtkHTMLBeginFlags flags); - -G_END_DECLS - -#endif /* EM_HTML_STREAM_H */ diff --git a/mail/em-utils.c b/mail/em-utils.c index 4d74b80a13..a9b57125a2 100644 --- a/mail/em-utils.c +++ b/mail/em-utils.c @@ -72,9 +72,11 @@ #include "e-mail-tag-editor.h" #include "em-composer-utils.h" -#include "em-format-quote.h" +#include "em-format-html-display.h" #include "em-format-html-print.h" #include "em-utils.h" +#include "e-mail-printer.h" +#include "em-format/em-format-quote.h" /* XXX This is a dirty hack on a dirty hack. We really need * to rework or get rid of the functions that use this. */ @@ -380,7 +382,7 @@ em_utils_flag_for_followup (EMailReader *reader, EMailBackend *backend; EShellSettings *shell_settings; EShellBackend *shell_backend; - EMFormatHTML *formatter; + EMailDisplay *display; GtkWidget *editor; GtkWindow *window; CamelTag *tags; @@ -470,8 +472,8 @@ em_utils_flag_for_followup (EMailReader *reader, camel_folder_thaw (folder); camel_tag_list_free (&tags); - formatter = e_mail_reader_get_formatter (reader); - em_format_queue_redraw (EM_FORMAT (formatter)); + display = e_mail_reader_get_mail_display (reader); + e_mail_display_reload (display); exit: /* XXX We shouldn't be freeing this. */ @@ -616,26 +618,44 @@ em_utils_write_messages_to_stream (CamelFolder *folder, return res; } +static void +do_print_msg_to_file (GObject *source, + GAsyncResult *result, + gpointer user_data) +{ + + EMFormatHTML *efh = EM_FORMAT_HTML (source); + gchar *filename = user_data; + + EMailPrinter *printer; + + printer = e_mail_printer_new (efh); + e_mail_printer_set_export_filename (printer, filename); + g_signal_connect_swapped (printer, "done", + G_CALLBACK (g_object_unref), printer); + + e_mail_printer_print (printer, TRUE, NULL); + + g_object_unref (efh); +} + static gboolean em_utils_print_messages_to_file (CamelFolder *folder, const gchar *uid, const gchar *filename) { - EMFormatHTMLPrint *efhp; + EMFormatHTMLDisplay *efhd; CamelMimeMessage *message; message = camel_folder_get_message_sync (folder, uid, NULL, NULL); if (message == NULL) return FALSE; - efhp = em_format_html_print_new (NULL, GTK_PRINT_OPERATION_ACTION_EXPORT); - efhp->export_filename = g_strdup (filename); - efhp->async = FALSE; + efhd = em_format_html_display_new (); + ((EMFormat *) efhd)->message_uid = g_strdup (uid); - em_format_html_print_message (efhp, message, folder, uid); - - g_object_unref (efhp); - g_object_unref (message); + em_format_parse_async ((EMFormat *) efhd, message, folder, NULL, + (GAsyncReadyCallback) do_print_msg_to_file, g_strdup (filename)); return TRUE; } @@ -1173,7 +1193,7 @@ em_utils_message_to_html (CamelMimeMessage *message, camel_stream_mem_set_byte_array (CAMEL_STREAM_MEM (mem), buf); emfq = em_format_quote_new (credits, mem, flags); - ((EMFormat *) emfq)->composer = TRUE; + em_format_set_composer ((EMFormat *) emfq, TRUE); if (!source) { GSettings *settings; @@ -1189,10 +1209,30 @@ em_utils_message_to_html (CamelMimeMessage *message, } /* FIXME Not passing a GCancellable here. */ - em_format_format_clone ( - EM_FORMAT (emfq), NULL, NULL, message, source, NULL); - if (validity_found) - *validity_found = ((EMFormat *)emfq)->validity_found; + em_format_parse (EM_FORMAT (emfq), message, NULL, NULL); + + if (validity_found) { + GList *iter; + EMFormat *emf = (EMFormat *) emfq; + + if (validity_found) + *validity_found = 0; + + /* Return all found validities */ + for (iter = emf->mail_part_list; iter; iter = iter->next) { + + EMFormatPURI *puri = iter->data; + if (!puri) + continue; + + if (*validity_found && puri->validity_type) + *validity_found |= puri->validity_type; + } + + } + + em_format_quote_write (emfq, mem, NULL); + g_object_unref (emfq); if (append && *append) diff --git a/mail/mail.error.xml b/mail/mail.error.xml index 2a516d9bfa..b5a714ff18 100644 --- a/mail/mail.error.xml +++ b/mail/mail.error.xml @@ -520,5 +520,10 @@ An mbox account will be created to preserve the old mbox folders. You can delete <_secondary xml:space="preserve">The attachment named {0} is a hidden file and may contain sensitive data. Please review it before sending. + + <_primary>Printing failed. + <_secondary>The printer replied "{0}". + + diff --git a/modules/Makefile.am b/modules/Makefile.am index ee3cdfdb1a..671215b952 100644 --- a/modules/Makefile.am +++ b/modules/Makefile.am @@ -35,6 +35,7 @@ SUBDIRS = \ plugin-manager \ spamassassin \ startup-wizard \ + web-inspector \ $(MONO_DIR) \ $(PYTHON_DIR) \ $(ONLINE_ACCOUNTS_DIR) \ diff --git a/modules/addressbook/e-book-shell-content.c b/modules/addressbook/e-book-shell-content.c index 0183512f5b..f3457e8999 100644 --- a/modules/addressbook/e-book-shell-content.c +++ b/modules/addressbook/e-book-shell-content.c @@ -286,19 +286,10 @@ book_shell_content_constructed (GObject *object) EAB_CONTACT_DISPLAY (widget), EAB_CONTACT_DISPLAY_RENDER_NORMAL); - eab_contact_display_set_orientation ( - EAB_CONTACT_DISPLAY (widget), - priv->orientation); - eab_contact_display_set_show_maps ( EAB_CONTACT_DISPLAY (widget), priv->preview_show_maps); - g_object_bind_property ( - object, "orientation", - widget, "orientation", - G_BINDING_SYNC_CREATE); - g_object_bind_property ( object, "preview-show-maps", widget, "show-maps", diff --git a/modules/mail/e-mail-config-format-html.c b/modules/mail/e-mail-config-format-html.c index 31ca88b744..dad5f9a518 100644 --- a/modules/mail/e-mail-config-format-html.c +++ b/modules/mail/e-mail-config-format-html.c @@ -76,6 +76,11 @@ mail_config_format_html_constructed (GObject *object) extensible, "show-real-date", G_BINDING_SYNC_CREATE); + g_object_bind_property ( + shell_settings, "mail-show-animated-images", + extensible, "animate-images", + G_BINDING_SYNC_CREATE); + /* Chain up to parent's constructed() method. */ G_OBJECT_CLASS (parent_class)->constructed (object); } diff --git a/modules/mail/e-mail-config-web-view.c b/modules/mail/e-mail-config-web-view.c index 80f3f65c0f..813256efd1 100644 --- a/modules/mail/e-mail-config-web-view.c +++ b/modules/mail/e-mail-config-web-view.c @@ -149,12 +149,6 @@ static void mail_config_web_view_realize (GtkWidget *widget, EMailConfigWebView *extension) { - g_object_bind_property ( - extension->shell_settings, - "mail-show-animated-images", - widget, "animate", - G_BINDING_SYNC_CREATE); - g_object_bind_property ( extension->shell_settings, "composer-inline-spelling", diff --git a/modules/mail/e-mail-shell-backend.c b/modules/mail/e-mail-shell-backend.c index 25902f34df..a30c0240de 100644 --- a/modules/mail/e-mail-shell-backend.c +++ b/modules/mail/e-mail-shell-backend.c @@ -835,37 +835,70 @@ e_mail_labels_get_filter_options (void) return g_slist_reverse (list); } +static void +message_parsed_cb (GObject *source_object, + GAsyncResult *res, + gpointer user_data) +{ + EMFormatHTML *formatter = EM_FORMAT_HTML (source_object); + GObject *preview = user_data; + EMailDisplay *display; + + display = g_object_get_data (preview, "mbox-imp-display"); + e_mail_display_set_formatter (display, formatter); + e_mail_display_load (display, EM_FORMAT (formatter)->uri_base); +} + /* utility functions for mbox importer */ static void mbox_create_preview_cb (GObject *preview, GtkWidget **preview_widget) { - EMFormatHTMLDisplay *format; - EWebView *web_view; + EMailDisplay *display; g_return_if_fail (preview != NULL); g_return_if_fail (preview_widget != NULL); - format = em_format_html_display_new (); - g_object_set_data_full ( - preview, "mbox-imp-formatter", format, g_object_unref); - web_view = em_format_html_get_web_view (EM_FORMAT_HTML (format)); + display = g_object_new (E_TYPE_MAIL_DISPLAY, NULL); + g_object_set_data_full (preview, "mbox-imp-display", + g_object_ref (display), g_object_unref); - *preview_widget = GTK_WIDGET (web_view); + *preview_widget = GTK_WIDGET (display); } static void mbox_fill_preview_cb (GObject *preview, CamelMimeMessage *msg) { - EMFormatHTMLDisplay *format; + EMailDisplay *display; + EMFormat *formatter; + GHashTable *formatters; + SoupSession *session; + gchar *mail_uri; g_return_if_fail (preview != NULL); g_return_if_fail (msg != NULL); - format = g_object_get_data (preview, "mbox-imp-formatter"); - g_return_if_fail (format != NULL); + display = g_object_get_data (preview, "mbox-imp-display"); + g_return_if_fail (display != NULL); + + session = webkit_get_default_session (); + formatters = g_object_get_data (G_OBJECT (session), "formatters"); + if (!formatters) { + formatters = g_hash_table_new_full (g_str_hash, g_str_equal, + (GDestroyNotify) g_free, NULL); + g_object_set_data (G_OBJECT (session), "formatters", formatters); + } + + mail_uri = em_format_build_mail_uri (NULL, msg->message_id, NULL, NULL); + + formatter = EM_FORMAT (em_format_html_display_new ()); + formatter->message_uid = g_strdup (msg->message_id); + formatter->uri_base = g_strdup (mail_uri); + + /* Don't free the mail_uri!! */ + g_hash_table_insert (formatters, mail_uri, formatter); - /* FIXME Not passing a GCancellable here. */ - em_format_format (EM_FORMAT (format), NULL, NULL, msg, NULL); + em_format_parse_async (formatter, msg, NULL, NULL, + message_parsed_cb, preview); } diff --git a/modules/mail/e-mail-shell-content.c b/modules/mail/e-mail-shell-content.c index 5bb60e3784..1bf6d4e29a 100644 --- a/modules/mail/e-mail-shell-content.c +++ b/modules/mail/e-mail-shell-content.c @@ -307,8 +307,8 @@ mail_shell_content_get_backend (EMailReader *reader) return e_mail_reader_get_backend (reader); } -static EMFormatHTML * -mail_shell_content_get_formatter (EMailReader *reader) +static EMailDisplay * +mail_shell_content_get_mail_display (EMailReader *reader) { EMailShellContent *mail_shell_content; @@ -317,8 +317,7 @@ mail_shell_content_get_formatter (EMailReader *reader) /* Forward this to our internal EMailView, which * also implements the EMailReader interface. */ reader = E_MAIL_READER (mail_shell_content->priv->mail_view); - - return e_mail_reader_get_formatter (reader); + return e_mail_reader_get_mail_display (reader); } static gboolean @@ -464,7 +463,7 @@ e_mail_shell_content_reader_init (EMailReaderInterface *interface) { interface->get_action_group = mail_shell_content_get_action_group; interface->get_backend = mail_shell_content_get_backend; - interface->get_formatter = mail_shell_content_get_formatter; + interface->get_mail_display = mail_shell_content_get_mail_display; interface->get_hide_deleted = mail_shell_content_get_hide_deleted; interface->get_message_list = mail_shell_content_get_message_list; interface->get_popup_menu = mail_shell_content_get_popup_menu; diff --git a/modules/mail/e-mail-shell-view-actions.c b/modules/mail/e-mail-shell-view-actions.c index b39d22d13d..5487920c74 100644 --- a/modules/mail/e-mail-shell-view-actions.c +++ b/modules/mail/e-mail-shell-view-actions.c @@ -891,14 +891,16 @@ action_mail_smart_backward_cb (GtkAction *action, EMailShellContent *mail_shell_content; EMailShellSidebar *mail_shell_sidebar; EMFolderTree *folder_tree; - EMFormatHTML *formatter; EMailReader *reader; EMailView *mail_view; GtkWidget *message_list; GtkToggleAction *toggle_action; - EWebView *web_view; + GtkWidget *window; + GtkAdjustment *adj; + EMailDisplay *display; gboolean caret_mode; gboolean magic_spacebar; + gdouble value; /* This implements the so-called "Magic Backspace". */ @@ -914,7 +916,7 @@ action_mail_smart_backward_cb (GtkAction *action, folder_tree = e_mail_shell_sidebar_get_folder_tree (mail_shell_sidebar); reader = E_MAIL_READER (mail_view); - formatter = e_mail_reader_get_formatter (reader); + display = e_mail_reader_get_mail_display (reader); message_list = e_mail_reader_get_message_list (reader); magic_spacebar = e_shell_settings_get_boolean ( @@ -923,32 +925,43 @@ action_mail_smart_backward_cb (GtkAction *action, toggle_action = GTK_TOGGLE_ACTION (ACTION (MAIL_CARET_MODE)); caret_mode = gtk_toggle_action_get_active (toggle_action); - web_view = em_format_html_get_web_view (formatter); - - if (e_web_view_scroll_backward (web_view)) + window = gtk_widget_get_parent (GTK_WIDGET (display)); + if (!GTK_IS_SCROLLED_WINDOW (window)) return; - if (caret_mode || !magic_spacebar) - return; + adj = gtk_scrolled_window_get_vadjustment (GTK_SCROLLED_WINDOW (window)); + value = gtk_adjustment_get_value (adj); + if (value == 0) { - /* XXX Are two separate calls really necessary? */ + if (caret_mode || !magic_spacebar) + return; - if (message_list_select ( - MESSAGE_LIST (message_list), - MESSAGE_LIST_SELECT_PREVIOUS, - 0, CAMEL_MESSAGE_SEEN)) - return; + /* XXX Are two separate calls really necessary? */ - if (message_list_select ( - MESSAGE_LIST (message_list), - MESSAGE_LIST_SELECT_PREVIOUS | - MESSAGE_LIST_SELECT_WRAP, 0, - CAMEL_MESSAGE_SEEN)) - return; + if (message_list_select ( + MESSAGE_LIST (message_list), + MESSAGE_LIST_SELECT_PREVIOUS, + 0, CAMEL_MESSAGE_SEEN)) + return; + + if (message_list_select ( + MESSAGE_LIST (message_list), + MESSAGE_LIST_SELECT_PREVIOUS | + MESSAGE_LIST_SELECT_WRAP, + 0, CAMEL_MESSAGE_SEEN)) + return; + + em_folder_tree_select_next_path (folder_tree, TRUE); - em_folder_tree_select_prev_path (folder_tree, TRUE); + gtk_widget_grab_focus (message_list); - gtk_widget_grab_focus (message_list); + } else { + + gtk_adjustment_set_value (adj, + value - gtk_adjustment_get_page_increment (adj)); + + return; + } } static void @@ -962,14 +975,17 @@ action_mail_smart_forward_cb (GtkAction *action, EMailShellContent *mail_shell_content; EMailShellSidebar *mail_shell_sidebar; EMFolderTree *folder_tree; - EMFormatHTML *formatter; EMailReader *reader; EMailView *mail_view; GtkWidget *message_list; + GtkWidget *window; + GtkAdjustment *adj; GtkToggleAction *toggle_action; - EWebView *web_view; + EMailDisplay *display; gboolean caret_mode; gboolean magic_spacebar; + gdouble value; + gdouble upper; /* This implements the so-called "Magic Spacebar". */ @@ -985,7 +1001,7 @@ action_mail_smart_forward_cb (GtkAction *action, folder_tree = e_mail_shell_sidebar_get_folder_tree (mail_shell_sidebar); reader = E_MAIL_READER (mail_view); - formatter = e_mail_reader_get_formatter (reader); + display = e_mail_reader_get_mail_display (reader); message_list = e_mail_reader_get_message_list (reader); magic_spacebar = e_shell_settings_get_boolean ( @@ -994,32 +1010,44 @@ action_mail_smart_forward_cb (GtkAction *action, toggle_action = GTK_TOGGLE_ACTION (ACTION (MAIL_CARET_MODE)); caret_mode = gtk_toggle_action_get_active (toggle_action); - web_view = em_format_html_get_web_view (formatter); - - if (e_web_view_scroll_forward (web_view)) + window = gtk_widget_get_parent (GTK_WIDGET (display)); + if (!GTK_IS_SCROLLED_WINDOW (window)) return; - if (caret_mode || !magic_spacebar) - return; + adj = gtk_scrolled_window_get_vadjustment (GTK_SCROLLED_WINDOW (window)); + value = gtk_adjustment_get_value (adj); + upper = gtk_adjustment_get_upper (adj); + if (value + gtk_adjustment_get_page_size (adj) >= upper) { - /* XXX Are two separate calls really necessary? */ + if (caret_mode || !magic_spacebar) + return; - if (message_list_select ( - MESSAGE_LIST (message_list), - MESSAGE_LIST_SELECT_NEXT, - 0, CAMEL_MESSAGE_SEEN)) - return; + /* XXX Are two separate calls really necessary? */ - if (message_list_select ( - MESSAGE_LIST (message_list), - MESSAGE_LIST_SELECT_NEXT | - MESSAGE_LIST_SELECT_WRAP, - 0, CAMEL_MESSAGE_SEEN)) - return; + if (message_list_select ( + MESSAGE_LIST (message_list), + MESSAGE_LIST_SELECT_NEXT, + 0, CAMEL_MESSAGE_SEEN)) + return; + + if (message_list_select ( + MESSAGE_LIST (message_list), + MESSAGE_LIST_SELECT_NEXT | + MESSAGE_LIST_SELECT_WRAP, + 0, CAMEL_MESSAGE_SEEN)) + return; + + em_folder_tree_select_next_path (folder_tree, TRUE); - em_folder_tree_select_next_path (folder_tree, TRUE); + gtk_widget_grab_focus (message_list); - gtk_widget_grab_focus (message_list); + } else { + + gtk_adjustment_set_value (adj, + value + gtk_adjustment_get_page_increment (adj)); + + return; + } } static void diff --git a/modules/mail/e-mail-shell-view-private.c b/modules/mail/e-mail-shell-view-private.c index 8838f1a1ae..9a58f9d2b7 100644 --- a/modules/mail/e-mail-shell-view-private.c +++ b/modules/mail/e-mail-shell-view-private.c @@ -331,11 +331,10 @@ mail_shell_view_popup_event_cb (EMailShellView *mail_shell_view, const gchar *uri) { EMailShellContent *mail_shell_content; - EMFormatHTML *formatter; + EMailDisplay *display; EShellView *shell_view; EMailReader *reader; EMailView *mail_view; - EWebView *web_view; GtkMenu *menu; if (uri != NULL) @@ -345,10 +344,9 @@ mail_shell_view_popup_event_cb (EMailShellView *mail_shell_view, mail_view = e_mail_shell_content_get_mail_view (mail_shell_content); reader = E_MAIL_READER (mail_view); - formatter = e_mail_reader_get_formatter (reader); - web_view = em_format_html_get_web_view (formatter); + display = e_mail_reader_get_mail_display (reader); - if (e_web_view_get_cursor_image (web_view) != NULL) + if (e_web_view_get_cursor_image (E_WEB_VIEW (display)) != NULL) return FALSE; menu = e_mail_reader_get_popup_menu (reader); @@ -367,77 +365,20 @@ mail_shell_view_popup_event_cb (EMailShellView *mail_shell_view, return TRUE; } -static void -mail_shell_view_scroll_cb (EMailShellView *mail_shell_view, - GtkOrientation orientation, - GtkScrollType scroll_type, - gfloat position, - GtkHTML *html) -{ - EShell *shell; - EShellView *shell_view; - EShellWindow *shell_window; - EShellSettings *shell_settings; - EMailShellContent *mail_shell_content; - EMailReader *reader; - EMailView *mail_view; - EWebView *web_view; - GtkWidget *message_list; - gboolean magic_spacebar; - - web_view = E_WEB_VIEW (html); - - if (html->binding_handled || e_web_view_get_caret_mode (web_view)) - return; - - if (orientation != GTK_ORIENTATION_VERTICAL) - return; - - shell_view = E_SHELL_VIEW (mail_shell_view); - shell_window = e_shell_view_get_shell_window (shell_view); - shell = e_shell_window_get_shell (shell_window); - shell_settings = e_shell_get_shell_settings (shell); - - magic_spacebar = e_shell_settings_get_boolean ( - shell_settings, "mail-magic-spacebar"); - - if (!magic_spacebar) - return; - - mail_shell_content = mail_shell_view->priv->mail_shell_content; - mail_view = e_mail_shell_content_get_mail_view (mail_shell_content); - - reader = E_MAIL_READER (mail_view); - message_list = e_mail_reader_get_message_list (reader); - - if (scroll_type == GTK_SCROLL_PAGE_FORWARD) - message_list_select ( - MESSAGE_LIST (message_list), - MESSAGE_LIST_SELECT_NEXT, - 0, CAMEL_MESSAGE_SEEN); - else - message_list_select ( - MESSAGE_LIST (message_list), - MESSAGE_LIST_SELECT_PREVIOUS, - 0, CAMEL_MESSAGE_SEEN); -} - static void mail_shell_view_reader_changed_cb (EMailShellView *mail_shell_view, EMailReader *reader) { GtkWidget *message_list; - EMFormatHTML *formatter; - EWebView *web_view; + EMailDisplay *display; EShellView *shell_view; EShellTaskbar *shell_taskbar; shell_view = E_SHELL_VIEW (mail_shell_view); shell_taskbar = e_shell_view_get_shell_taskbar (shell_view); - formatter = e_mail_reader_get_formatter (reader); + display = e_mail_reader_get_mail_display (reader); message_list = e_mail_reader_get_message_list (reader); - web_view = em_format_html_get_web_view (formatter); e_shell_view_update_actions (E_SHELL_VIEW (mail_shell_view)); e_mail_shell_view_update_sidebar (mail_shell_view); @@ -464,23 +405,17 @@ mail_shell_view_reader_changed_cb (EMailShellView *mail_shell_view, mail_shell_view, G_CONNECT_SWAPPED); g_signal_connect_object ( - web_view, "key-press-event", + display, "key-press-event", G_CALLBACK (mail_shell_view_key_press_event_cb), mail_shell_view, G_CONNECT_SWAPPED); g_signal_connect_object ( - web_view, "popup-event", + display, "popup-event", G_CALLBACK (mail_shell_view_popup_event_cb), mail_shell_view, G_CONNECT_SWAPPED); g_signal_connect_object ( - web_view, "scroll", - G_CALLBACK (mail_shell_view_scroll_cb), - mail_shell_view, - G_CONNECT_AFTER | G_CONNECT_SWAPPED); - - g_signal_connect_object ( - web_view, "status-message", + display, "status-message", G_CALLBACK (e_shell_taskbar_set_message), shell_taskbar, G_CONNECT_SWAPPED); } @@ -634,7 +569,6 @@ e_mail_shell_view_private_constructed (EMailShellView *mail_shell_view) EShellTaskbar *shell_taskbar; EShellWindow *shell_window; EShellSearchbar *searchbar; - EMFormatHTML *formatter; EMFolderTree *folder_tree; EActionComboBox *combo_box; ERuleContext *context; @@ -647,7 +581,7 @@ e_mail_shell_view_private_constructed (EMailShellView *mail_shell_view) EMailSession *session; EMailReader *reader; EMailView *mail_view; - EWebView *web_view; + EMailDisplay *display; const gchar *source; guint merge_id; gint ii = 0; @@ -691,7 +625,7 @@ e_mail_shell_view_private_constructed (EMailShellView *mail_shell_view) combo_box = e_shell_searchbar_get_scope_combo_box (searchbar); reader = E_MAIL_READER (shell_content); - formatter = e_mail_reader_get_formatter (reader); + display = e_mail_reader_get_mail_display (reader); message_list = e_mail_reader_get_message_list (reader); em_folder_tree_set_selectable_widget (folder_tree, message_list); @@ -710,8 +644,6 @@ e_mail_shell_view_private_constructed (EMailShellView *mail_shell_view) G_CALLBACK (mail_shell_view_search_filter_changed_cb), mail_shell_view, G_CONNECT_SWAPPED); - web_view = em_format_html_get_web_view (formatter); - g_signal_connect_object ( folder_tree, "folder-selected", G_CALLBACK (mail_shell_view_folder_tree_selected_cb), @@ -784,23 +716,17 @@ e_mail_shell_view_private_constructed (EMailShellView *mail_shell_view) mail_shell_view, G_CONNECT_SWAPPED); g_signal_connect_object ( - web_view, "key-press-event", + display, "key-press-event", G_CALLBACK (mail_shell_view_key_press_event_cb), mail_shell_view, G_CONNECT_SWAPPED); g_signal_connect_object ( - web_view, "popup-event", + display, "popup-event", G_CALLBACK (mail_shell_view_popup_event_cb), mail_shell_view, G_CONNECT_SWAPPED); g_signal_connect_object ( - web_view, "scroll", - G_CALLBACK (mail_shell_view_scroll_cb), - mail_shell_view, - G_CONNECT_AFTER | G_CONNECT_SWAPPED); - - g_signal_connect_object ( - web_view, "status-message", + display, "status-message", G_CALLBACK (e_shell_taskbar_set_message), shell_taskbar, G_CONNECT_SWAPPED); diff --git a/modules/mail/em-mailer-prefs.c b/modules/mail/em-mailer-prefs.c index 3f5e371296..d81656f997 100644 --- a/modules/mail/em-mailer-prefs.c +++ b/modules/mail/em-mailer-prefs.c @@ -529,10 +529,10 @@ toggle_button_init (EMMailerPrefs *prefs, const gchar *key, GCallback toggled) { - gboolean bool; + gboolean v_bool; - bool = g_settings_get_boolean (prefs->settings, key); - gtk_toggle_button_set_active (toggle, not ? !bool : bool); + v_bool = g_settings_get_boolean (prefs->settings, key); + gtk_toggle_button_set_active (toggle, not ? !v_bool : v_bool); if (toggled) { g_object_set_data ((GObject *) toggle, "key", (gpointer) key); diff --git a/modules/web-inspector/Makefile.am b/modules/web-inspector/Makefile.am new file mode 100644 index 0000000000..f8a1fc9340 --- /dev/null +++ b/modules/web-inspector/Makefile.am @@ -0,0 +1,23 @@ +module_LTLIBRARIES = libevolution-module-web-inspector.la + +libevolution_module_web_inspector_la_CPPFLAGS = \ + $(AM_CPPFLAGS) \ + -I$(top_srcdir) \ + -I$(top_srcdir)/widgets \ + -DG_LOG_DOMAIN=\"evolution-web-inspector\" \ + $(EVOLUTION_DATA_SERVER_CFLAGS) \ + $(GNOME_PLATFORM_CFLAGS) + +libevolution_module_web_inspector_la_SOURCES = \ + evolution-web-inspector.c + +libevolution_module_web_inspector_la_LIBADD = \ + $(top_builddir)/e-util/libeutil.la \ + $(top_builddir)/widgets/misc/libemiscwidgets.la \ + $(EVOLUTION_DATA_SERVER_LIBS) \ + $(GNOME_PLATFORM_LIBS) + +libevolution_module_web_inspector_la_LDFLAGS = \ + -module -avoid-version $(NO_UNDEFINED) + +-include $(top_srcdir)/git.mk diff --git a/modules/web-inspector/evolution-web-inspector.c b/modules/web-inspector/evolution-web-inspector.c new file mode 100644 index 0000000000..27a21e0693 --- /dev/null +++ b/modules/web-inspector/evolution-web-inspector.c @@ -0,0 +1,185 @@ +/* + * evolution-web-inspector.c + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2 of the License, or (at your option) version 3. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with the program; if not, see + * + */ + +#include +#include + +#include +#include + +/* Standard GObject macros */ +#define E_TYPE_WEB_INSPECTOR \ + (e_web_inspector_get_type ()) +#define E_WEB_INSPECTOR(obj) \ + (G_TYPE_CHECK_INSTANCE_CAST \ + ((obj), E_TYPE_WEB_INSPECTOR, EWebInspector)) + +typedef struct _EWebInspector EWebInspector; +typedef struct _EWebInspectorClass EWebInspectorClass; + +struct _EWebInspector { + EExtension parent; +}; + +struct _EWebInspectorClass { + EExtensionClass parent_class; +}; + +static const gchar *ui = +"" +" " +" " +" " +" " +" " +""; + +/* Module Entry Points */ +void e_module_load (GTypeModule *type_module); +void e_module_unload (GTypeModule *type_module); + +/* Forward Declarations */ +GType e_web_inspector_get_type (void); + +G_DEFINE_DYNAMIC_TYPE (EWebInspector, e_web_inspector, E_TYPE_EXTENSION) + +static EWebView * +web_inspector_get_web_view (EWebInspector *extension) +{ + EExtensible *extensible; + + extensible = e_extension_get_extensible (E_EXTENSION (extension)); + + return E_WEB_VIEW (extensible); +} + +static void +web_inspector_action_inspect_cb (GtkAction *action, + EWebInspector *extension) +{ + WebKitWebInspector *inspector; + EWebView *web_view; + + web_view = web_inspector_get_web_view (extension); + inspector = webkit_web_view_get_inspector (WEBKIT_WEB_VIEW (web_view)); + + webkit_web_inspector_show (inspector); +} + +static GtkActionEntry inspect_entries[] = { + + { "inspect", + NULL, + N_("_Inspect..."), + NULL, + N_("Inspect the HTML content (debugging feature)"), + G_CALLBACK (web_inspector_action_inspect_cb) } +}; + +static WebKitWebView * +web_inspector_inspect_web_view_cb (WebKitWebInspector *inspector, + EWebInspector *extension) +{ + GtkWidget *web_view; + GtkWidget *window; + const gchar *title; + + title = _("Evolution Web Inspector"); + window = gtk_window_new (GTK_WINDOW_TOPLEVEL); + gtk_window_set_title (GTK_WINDOW (window), title); + gtk_widget_set_size_request (window, 600, 400); + gtk_widget_show (window); + + web_view = webkit_web_view_new (); + gtk_container_add (GTK_CONTAINER (window), web_view); + gtk_widget_show (web_view); + + return WEBKIT_WEB_VIEW (web_view); +} + +static void +web_inspector_constructed (GObject *object) +{ + EWebInspector *extension; + WebKitWebSettings *settings; + WebKitWebInspector *inspector; + GtkActionGroup *action_group; + GtkUIManager *ui_manager; + EWebView *web_view; + GError *error = NULL; + + extension = E_WEB_INSPECTOR (object); + web_view = web_inspector_get_web_view (extension); + + ui_manager = e_web_view_get_ui_manager (web_view); + action_group = e_web_view_get_action_group (web_view, "standard"); + + settings = webkit_web_view_get_settings (WEBKIT_WEB_VIEW (web_view)); + g_object_set (settings, "enable-developer-extras", TRUE, NULL); + + inspector = webkit_web_view_get_inspector (WEBKIT_WEB_VIEW (web_view)); + + g_signal_connect ( + inspector, "inspect-web-view", + G_CALLBACK (web_inspector_inspect_web_view_cb), extension); + + gtk_action_group_add_actions ( + action_group, inspect_entries, + G_N_ELEMENTS (inspect_entries), extension); + + /* Because we are loading from a hard-coded string, there is + * no chance of I/O errors. Failure here implies a malformed + * UI definition. Full stop. */ + gtk_ui_manager_add_ui_from_string (ui_manager, ui, -1, &error); + if (error != NULL) + g_error ("%s", error->message); +} + +static void +e_web_inspector_class_init (EWebInspectorClass *class) +{ + GObjectClass *object_class; + EExtensionClass *extension_class; + + object_class = G_OBJECT_CLASS (class); + object_class->constructed = web_inspector_constructed; + + extension_class = E_EXTENSION_CLASS (class); + extension_class->extensible_type = E_TYPE_WEB_VIEW; +} + +static void +e_web_inspector_class_finalize (EWebInspectorClass *class) +{ +} + +static void +e_web_inspector_init (EWebInspector *extension) +{ +} + +G_MODULE_EXPORT void +e_module_load (GTypeModule *type_module) +{ + e_web_inspector_register_type (type_module); +} + +G_MODULE_EXPORT void +e_module_unload (GTypeModule *type_module) +{ +} -- cgit