Skip to content

Commit

Permalink
Merge pull request MailCore#32 from kgrigsby59/master
Browse files Browse the repository at this point in the history
Added methods to decode data as strings
  • Loading branch information
dinhvh committed Apr 20, 2013
2 parents bf0f599 + bc6411e commit 6c13147
Show file tree
Hide file tree
Showing 9 changed files with 55 additions and 2 deletions.
13 changes: 13 additions & 0 deletions src/core/abstract/MCAbstractPart.cc
Expand Up @@ -3,6 +3,7 @@
#include <string.h>
#include <stdlib.h>
#include <libetpan/libetpan.h>
#include "MCData.h"

using namespace mailcore;

Expand Down Expand Up @@ -249,3 +250,15 @@ AbstractPart * AbstractPart::partForUniqueID(String * uniqueID)
}
}

String * AbstractPart::decodedStringForData(Data * data)
{
String *lowerMimeType = mMimeType ? mMimeType->lowercaseString() : NULL;

if (lowerMimeType && lowerMimeType->hasPrefix(MCSTR("text/"))) {
bool isHTML = lowerMimeType->isEqual(MCSTR("text/html"));
return data->stringWithDetectedCharset(mCharset, isHTML);
}
else {
return NULL;
}
}
2 changes: 2 additions & 0 deletions src/core/abstract/MCAbstractPart.h
Expand Up @@ -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();
Expand Down
11 changes: 11 additions & 0 deletions src/core/rfc822/MCAttachment.cc
Expand Up @@ -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);
Expand Down
1 change: 1 addition & 0 deletions src/core/rfc822/MCAttachment.h
Expand Up @@ -26,6 +26,7 @@ namespace mailcore {

virtual void setData(Data * data);
virtual Data * data();
virtual String * decodedString();

public: // subclass behavior
Attachment(Attachment * other);
Expand Down
3 changes: 3 additions & 0 deletions src/objc/abstract/MCOAbstractPart.h
Expand Up @@ -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
5 changes: 5 additions & 0 deletions src/objc/abstract/MCOAbstractPart.mm
Expand Up @@ -14,6 +14,7 @@

#import "NSString+MCO.h"
#import "NSObject+MCO.h"
#import "NSData+MCO.h"

@implementation MCOAbstractPart {
mailcore::AbstractPart * _part;
Expand Down Expand Up @@ -68,4 +69,8 @@ - (MCOAbstractPart *) partForUniqueID:(NSString *)uniqueID
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
3 changes: 3 additions & 0 deletions src/objc/rfc822/MCOAttachment.h
Expand Up @@ -34,6 +34,9 @@

// Decoded data of the part.
@property (nonatomic, strong) NSData * data;

// Returns string representation according to charset
- (NSString *) decodedString;

@end

Expand Down
6 changes: 6 additions & 0 deletions src/objc/rfc822/MCOAttachment.mm
Expand Up @@ -86,4 +86,10 @@ + (MCOAttachment *) attachmentWithText:(NSString *)text

MCO_OBJC_SYNTHESIZE_DATA(setData, data)

- (NSString *) decodedString
{
mailcore::String *result = MCO_NATIVE_INSTANCE->decodedString();
return [NSString mco_stringWithMCString:result];
}

@end
13 changes: 11 additions & 2 deletions tests/test-all.mm
Expand Up @@ -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];
Expand Down Expand Up @@ -341,8 +349,9 @@ void testAll()
//testAsyncSMTP(data);
//testAsyncIMAP();
//testAsyncPOP();
testAddresses();
//testObjC();
//testAddresses();
//testAttachments();
testObjC();

MCLog("pool release");
pool->release();
Expand Down

0 comments on commit 6c13147

Please sign in to comment.