aboutsummaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorGravatar Ken Grigsby <grigsby@thursby.com>2013-04-19 19:10:49 -0500
committerGravatar Ken Grigsby <grigsby@thursby.com>2013-04-19 19:10:49 -0500
commit8123121157b7fd2ec27ee1b9f7f6a23357955b4e (patch)
treead1a647e5aa1141cdcec5c7c8f9383aa01bb061e
parent6397bdb1cf09503680b8b0861ea237fa8199a637 (diff)
added methods to decode data as strings
-rw-r--r--src/core/abstract/MCAbstractPart.cc10
-rw-r--r--src/core/abstract/MCAbstractPart.h2
-rw-r--r--src/core/rfc822/MCAttachment.cc11
-rw-r--r--src/core/rfc822/MCAttachment.h1
-rw-r--r--src/objc/abstract/MCOAbstractPart.h3
-rw-r--r--src/objc/abstract/MCOAbstractPart.mm5
-rw-r--r--src/objc/rfc822/MCOAttachment.h3
-rw-r--r--src/objc/rfc822/MCOAttachment.mm6
-rw-r--r--tests/test-all.mm13
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 <string.h>
#include <stdlib.h>
#include <libetpan/libetpan.h>
+#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();