From 8123121157b7fd2ec27ee1b9f7f6a23357955b4e Mon Sep 17 00:00:00 2001 From: Ken Grigsby Date: Fri, 19 Apr 2013 19:10:49 -0500 Subject: added methods to decode data as strings --- src/core/abstract/MCAbstractPart.cc | 10 ++++++++++ src/core/abstract/MCAbstractPart.h | 2 ++ src/core/rfc822/MCAttachment.cc | 11 +++++++++++ src/core/rfc822/MCAttachment.h | 1 + src/objc/abstract/MCOAbstractPart.h | 3 +++ src/objc/abstract/MCOAbstractPart.mm | 5 +++++ src/objc/rfc822/MCOAttachment.h | 3 +++ src/objc/rfc822/MCOAttachment.mm | 6 ++++++ tests/test-all.mm | 13 +++++++++++-- 9 files changed, 52 insertions(+), 2 deletions(-) diff --git a/src/core/abstract/MCAbstractPart.cc b/src/core/abstract/MCAbstractPart.cc index b07063ad..97a8b07c 100644 --- a/src/core/abstract/MCAbstractPart.cc +++ b/src/core/abstract/MCAbstractPart.cc @@ -3,6 +3,7 @@ #include #include #include +#include "MCData.h" using namespace mailcore; @@ -249,3 +250,12 @@ AbstractPart * AbstractPart::partForUniqueID(String * uniqueID) } } +String * AbstractPart::decodedStringForData(Data * data) +{ + if (mMimeType && mMimeType->hasPrefix(MCSTR("text/"))) { + return data->stringWithDetectedCharset(mCharset, false); + } + else { + return NULL; + } +} diff --git a/src/core/abstract/MCAbstractPart.h b/src/core/abstract/MCAbstractPart.h index 970b5e04..b01ba4f4 100644 --- a/src/core/abstract/MCAbstractPart.h +++ b/src/core/abstract/MCAbstractPart.h @@ -46,6 +46,8 @@ namespace mailcore { virtual AbstractPart * partForContentID(String * contentID); virtual AbstractPart * partForUniqueID(String * uniqueID); + virtual String * decodedStringForData(Data * data); + public: // subclass behavior AbstractPart(AbstractPart * other); virtual String * description(); diff --git a/src/core/rfc822/MCAttachment.cc b/src/core/rfc822/MCAttachment.cc index c75e689a..b6167941 100644 --- a/src/core/rfc822/MCAttachment.cc +++ b/src/core/rfc822/MCAttachment.cc @@ -247,6 +247,17 @@ Data * Attachment::data() return mData; } +String * Attachment::decodedString() +{ + if (mData) { + return decodedStringForData(mData); + } + else { + return NULL; + } +} + + AbstractPart * Attachment::attachmentsWithMIME(struct mailmime * mime) { return attachmentsWithMIMEWithMain(mime, true); diff --git a/src/core/rfc822/MCAttachment.h b/src/core/rfc822/MCAttachment.h index c1fc232e..272e589d 100644 --- a/src/core/rfc822/MCAttachment.h +++ b/src/core/rfc822/MCAttachment.h @@ -26,6 +26,7 @@ namespace mailcore { virtual void setData(Data * data); virtual Data * data(); + virtual String * decodedString(); public: // subclass behavior Attachment(Attachment * other); diff --git a/src/objc/abstract/MCOAbstractPart.h b/src/objc/abstract/MCOAbstractPart.h index 6fb14fcd..5c636a19 100644 --- a/src/objc/abstract/MCOAbstractPart.h +++ b/src/objc/abstract/MCOAbstractPart.h @@ -74,6 +74,9 @@ typedef enum { // Returns the part with the given unique identifier among this part and its subparts. - (MCOAbstractPart *) partForUniqueID:(NSString *)uniqueID; +// Returns a string representation of the data according to charset. +- (NSString *) decodedStringForData:(NSData *)data; + @end #endif diff --git a/src/objc/abstract/MCOAbstractPart.mm b/src/objc/abstract/MCOAbstractPart.mm index 1d17ca20..47191ce9 100644 --- a/src/objc/abstract/MCOAbstractPart.mm +++ b/src/objc/abstract/MCOAbstractPart.mm @@ -14,6 +14,7 @@ #import "NSString+MCO.h" #import "NSObject+MCO.h" +#import "NSData+MCO.h" @implementation MCOAbstractPart { mailcore::AbstractPart * _part; @@ -68,4 +69,8 @@ MCO_OBJC_SYNTHESIZE(AbstractMessage, setMessage, message) return MCO_TO_OBJC(MCO_NATIVE_INSTANCE->partForUniqueID([uniqueID mco_mcString])); } +- (NSString *) decodedStringForData:(NSData *)data +{ + return [NSString mco_stringWithMCString:MCO_NATIVE_INSTANCE->decodedStringForData([data mco_mcData])]; +} @end diff --git a/src/objc/rfc822/MCOAttachment.h b/src/objc/rfc822/MCOAttachment.h index 7c6a0d7b..584bdece 100644 --- a/src/objc/rfc822/MCOAttachment.h +++ b/src/objc/rfc822/MCOAttachment.h @@ -34,6 +34,9 @@ // Decoded data of the part. @property (nonatomic, strong) NSData * data; + +// Returns string representation according to charset +- (NSString *) decodedString; @end diff --git a/src/objc/rfc822/MCOAttachment.mm b/src/objc/rfc822/MCOAttachment.mm index 55430f78..423ac0df 100644 --- a/src/objc/rfc822/MCOAttachment.mm +++ b/src/objc/rfc822/MCOAttachment.mm @@ -86,4 +86,10 @@ MCO_OBJC_SYNTHESIZE_DATA(setData, data) +- (NSString *) decodedString +{ + mailcore::String *result = MCO_NATIVE_INSTANCE->decodedString(); + return [NSString mco_stringWithMCString:result]; +} + @end diff --git a/tests/test-all.mm b/tests/test-all.mm index 6107730e..73981e82 100644 --- a/tests/test-all.mm +++ b/tests/test-all.mm @@ -285,6 +285,14 @@ static void testAddresses() MCLog("%s", MCUTF8DESC(str)); } +static void testAttachments() +{ + mailcore::Attachment *attachment = mailcore::Attachment::attachmentWithText(MCSTR("Hello World")); + attachment->setCharset(NULL); + mailcore::String * str = attachment->decodedString(); + MCLog("%s", MCUTF8DESC(str)); +} + void testObjC() { MCOIMAPSession *session = [[MCOIMAPSession alloc] init]; @@ -341,8 +349,9 @@ void testAll() //testAsyncSMTP(data); //testAsyncIMAP(); //testAsyncPOP(); - testAddresses(); - //testObjC(); + //testAddresses(); + //testAttachments(); + testObjC(); MCLog("pool release"); pool->release(); -- cgit v1.2.3