From b8bc2906cb1012bdfac327f37a581a7cd2d9c984 Mon Sep 17 00:00:00 2001 From: "scott%scott-macgregor.org" Date: Tue, 26 Aug 2003 06:05:47 +0000 Subject: [PATCH] Bug #134492 --> when printing a msg with attachments, also print a list of the attachments. Thanks to Kevin Teuscher for helping to drive this patch. r=mscott sr=bienvenu/brendan a=asa --- .../mime/emitters/src/nsMimeHtmlEmitter.cpp | 174 ++++++++++++++---- .../mime/emitters/src/nsMimeHtmlEmitter.h | 4 + 2 files changed, 145 insertions(+), 33 deletions(-) diff --git a/mailnews/mime/emitters/src/nsMimeHtmlEmitter.cpp b/mailnews/mime/emitters/src/nsMimeHtmlEmitter.cpp index 3e721edc1f9..b6216c5d1c9 100644 --- a/mailnews/mime/emitters/src/nsMimeHtmlEmitter.cpp +++ b/mailnews/mime/emitters/src/nsMimeHtmlEmitter.cpp @@ -68,6 +68,8 @@ static NS_DEFINE_CID(kDateTimeFormatCID, NS_DATETIMEFORMAT_CID); */ nsMimeHtmlDisplayEmitter::nsMimeHtmlDisplayEmitter() : nsMimeBaseEmitter() { + mFirst = PR_TRUE; + mSkipAttachment = PR_FALSE; } nsMimeHtmlDisplayEmitter::~nsMimeHtmlDisplayEmitter(void) @@ -346,65 +348,171 @@ nsMimeHtmlDisplayEmitter::EndHeader() } nsresult -nsMimeHtmlDisplayEmitter::StartAttachment(const char *name, const char *contentType, const char *url, +nsMimeHtmlDisplayEmitter::StartAttachment(const char *name, + const char *contentType, + const char *url, PRBool aNotDownloaded) { nsresult rv = NS_OK; nsCOMPtr headerSink; rv = GetHeaderSink(getter_AddRefs(headerSink)); - if (NS_FAILED(rv) || !headerSink) - return NS_OK; //If we don't need or cannot broadcast attachment info, just ignore it and return NS_OK - - char * escapedUrl = nsEscape(url, url_Path); - nsXPIDLCString uriString; + if (NS_SUCCEEDED(rv) && headerSink) + { + char * escapedUrl = nsEscape(url, url_Path); + nsXPIDLCString uriString; - nsCOMPtr msgurl (do_QueryInterface(mURL, &rv)); - if (NS_SUCCEEDED(rv)) - { - // HACK: news urls require us to use the originalSpec. everyone else uses GetURI - // to get the RDF resource which describes the message. - nsCOMPtr nntpUrl (do_QueryInterface(mURL, &rv)); - if (NS_SUCCEEDED(rv) && nntpUrl) - rv = msgurl->GetOriginalSpec(getter_Copies(uriString)); - else - rv = msgurl->GetUri(getter_Copies(uriString)); - } + nsCOMPtr msgurl (do_QueryInterface(mURL, &rv)); + if (NS_SUCCEEDED(rv)) + { + // HACK: news urls require us to use the originalSpec. Everyone + // else uses GetURI to get the RDF resource which describes the message. + nsCOMPtr nntpUrl (do_QueryInterface(mURL, &rv)); + if (NS_SUCCEEDED(rv) && nntpUrl) + rv = msgurl->GetOriginalSpec(getter_Copies(uriString)); + else + rv = msgurl->GetUri(getter_Copies(uriString)); + } - // we need to convert the attachment name from UTF-8 to unicode before - // we emit it... - nsXPIDLString unicodeHeaderValue; + // we need to convert the attachment name from UTF-8 to unicode before + // we emit it... + nsXPIDLString unicodeHeaderValue; - rv = NS_ERROR_FAILURE; // use failure to mean that we couldn't decode - if (mUnicodeConverter) - rv = mUnicodeConverter->DecodeMimeHeader(name, + rv = NS_ERROR_FAILURE; // use failure to mean that we couldn't decode + if (mUnicodeConverter) + rv = mUnicodeConverter->DecodeMimeHeader(name, getter_Copies(unicodeHeaderValue)); - if (NS_FAILED(rv)) - { - unicodeHeaderValue.Assign(NS_ConvertUTF8toUCS2(name)); + if (NS_FAILED(rv)) + { + unicodeHeaderValue.Assign(NS_ConvertUTF8toUCS2(name)); - // but it's not really a failure if we didn't have a converter in the first place - if ( !mUnicodeConverter ) - rv = NS_OK; + // but it's not really a failure if we didn't have a converter + // in the first place + if ( !mUnicodeConverter ) + rv = NS_OK; + } + + headerSink->HandleAttachment(contentType, url /* was escapedUrl */, + unicodeHeaderValue, uriString, + aNotDownloaded); + + nsCRT::free(escapedUrl); + mSkipAttachment = PR_TRUE; + } + else if (mFormat == nsMimeOutput::nsMimeMessagePrintOutput) + { + // then we need to deal with the attachments in the body by inserting + // them into a table.. + rv = StartAttachmentInBody(name, contentType, url); + } + else + { + // If we don't need or cannot broadcast attachment info, just ignore it + mSkipAttachment = PR_TRUE; + rv = NS_OK; } - headerSink->HandleAttachment(contentType, url /* was escapedUrl */, unicodeHeaderValue, uriString, aNotDownloaded); - - nsCRT::free(escapedUrl); - return rv; } +// Attachment handling routines +// Ok, we are changing the way we handle these now...It used to be that we output +// HTML to make a clickable link, etc... but now, this should just be informational +// and only show up during printing +// XXX should they also show up during quoting? +nsresult +nsMimeHtmlDisplayEmitter::StartAttachmentInBody(const char *name, const char *contentType, const char *url) +{ + mSkipAttachment = PR_FALSE; + + if ( (contentType) && + ((!strcmp(contentType, APPLICATION_XPKCS7_MIME)) || + (!strcmp(contentType, APPLICATION_XPKCS7_SIGNATURE)) || + (!strcmp(contentType, TEXT_VCARD))) + ) { + mSkipAttachment = PR_TRUE; + return NS_OK; + } + + if (!mFirst) + UtilityWrite("
"); + + mFirst = PR_FALSE; + + UtilityWrite("
"); + UtilityWrite(""); + UtilityWrite(""); + UtilityWrite(""); + UtilityWrite(""); + UtilityWrite(""); + + UtilityWrite("
"); + + UtilityWrite("
"); + + UtilityWrite(name); + + UtilityWrite("
"); + + UtilityWrite("
"); + UtilityWrite(""); + return NS_OK; +} + nsresult nsMimeHtmlDisplayEmitter::AddAttachmentField(const char *field, const char *value) { + if (mSkipAttachment || BroadCastHeadersAndAttachments()) + return NS_OK; + + // Don't let bad things happen + if ( !value || !*value ) + return NS_OK; + + // Don't output this ugly header... + if (!strcmp(field, HEADER_X_MOZILLA_PART_URL)) + return NS_OK; + + char *newValue = nsEscapeHTML(value); + + UtilityWrite(""); + + UtilityWrite(""); + UtilityWrite(""); + UtilityWrite(""); + + PR_Free(newValue); return NS_OK; } nsresult nsMimeHtmlDisplayEmitter::EndAttachment() { + if (mSkipAttachment) + return NS_OK; + + mSkipAttachment = PR_FALSE; // reset it for next attachment round + + if (BroadCastHeadersAndAttachments()) + return NS_OK; + + if (mFormat == nsMimeOutput::nsMimeMessagePrintOutput) { + UtilityWrite("
"); + UtilityWrite("
"); + + UtilityWrite(field); + UtilityWrite(":"); + UtilityWrite("
"); + UtilityWrite("
"); + + UtilityWrite(newValue); + + UtilityWrite("
"); + UtilityWrite("
"); + UtilityWrite("
"); + UtilityWrite("
"); + } return NS_OK; } diff --git a/mailnews/mime/emitters/src/nsMimeHtmlEmitter.h b/mailnews/mime/emitters/src/nsMimeHtmlEmitter.h index d212d1f887b..a0ccd0ef9ad 100644 --- a/mailnews/mime/emitters/src/nsMimeHtmlEmitter.h +++ b/mailnews/mime/emitters/src/nsMimeHtmlEmitter.h @@ -77,10 +77,14 @@ public: virtual nsresult WriteHeaderFieldHTMLPostfix(); protected: + PRBool mFirst; // Attachment flag... + PRBool mSkipAttachment; // attachments we shouldn't show... + nsCOMPtr mHeaderSink; nsresult GetHeaderSink(nsIMsgHeaderSink ** aHeaderSink); PRBool BroadCastHeadersAndAttachments(); + nsresult StartAttachmentInBody(const char *name, const char *contentType, const char *url); nsCOMPtr mDateFormater; nsresult GenerateDateString(const char * dateString, PRUnichar ** formattedDate);