Permalink
Browse files

More attachment work, I added the ability to get contentType

  • Loading branch information...
1 parent eb2c007 commit 03f84a6b737fc01941cb59bd031996495c64cf40 Matt Ronge committed Aug 16, 2007
Oops, something went wrong.
Oops, something went wrong.
@@ -44,8 +44,7 @@ - (id)initWithMIMESinglePart:(CTMIME_SinglePart *)part {
if (self) {
mMIMEPart = [part retain];
self.filename = mMIMEPart.filename;
- //TODO Implement me
- //self.contentType = mMIMEPart.contentType
+ self.contentType = mMIMEPart.contentType;
}
return self;
}
View
@@ -35,9 +35,12 @@
@class CTMIME_Enumerator;
@interface CTMIME : NSObject {
+ NSString *mContentType;
}
-- (id)initWithMIMEStruct:(struct mailmime *)mime forMessage:(struct mailmessage *)message;
+- (id)initWithMIMEStruct:(struct mailmime *)mime
+ forMessage:(struct mailmessage *)message;
- (id)content;
+- (NSString *)contentType;
- (struct mailmime *)buildMIMEStruct;
- (NSString *)render;
- (CTMIME_Enumerator *)mimeEnumerator;
View
@@ -34,19 +34,66 @@
#import "CTMIME_Enumerator.h"
@implementation CTMIME
-- (id)initWithMIMEStruct:(struct mailmime *)mime forMessage:(struct mailmessage *)message {
- return nil;
+- (id)initWithMIMEStruct:(struct mailmime *)mime
+ forMessage:(struct mailmessage *)message {
+ self = [super init];
+ if (self) {
+ // We couldn't find a content-type, set it to something generic
+ NSString *mainType = @"application";
+ NSString *subType = @"octet-stream";
+ if (mime != NULL && mime->mm_content_type != NULL) {
+ struct mailmime_content *content = mime->mm_content_type;
+ if (content->ct_type != NULL) {
+ subType = [NSString stringWithCString:content->ct_subtype
+ encoding:NSASCIIStringEncoding];
+ subType = [subType lowercaseString];
+ struct mailmime_type *type = content->ct_type;
+ if (type->tp_type == MAILMIME_TYPE_DISCRETE_TYPE &&
+ type->tp_data.tp_discrete_type != NULL) {
+ switch (type->tp_data.tp_discrete_type->dt_type) {
+ case MAILMIME_DISCRETE_TYPE_TEXT:
+ mainType = @"text";
+ break;
+ case MAILMIME_DISCRETE_TYPE_IMAGE:
+ mainType = @"image";
+ break;
+ case MAILMIME_DISCRETE_TYPE_AUDIO:
+ mainType = @"audio";
+ break;
+ case MAILMIME_DISCRETE_TYPE_VIDEO:
+ mainType = @"video";
+ break;
+ case MAILMIME_DISCRETE_TYPE_APPLICATION:
+ mainType = @"application";
+ break;
+ }
+ }
+ else if (type->tp_type == MAILMIME_TYPE_COMPOSITE_TYPE &&
+ type->tp_data.tp_composite_type != NULL) {
+ switch (type->tp_data.tp_discrete_type->dt_type) {
+ case MAILMIME_COMPOSITE_TYPE_MESSAGE:
+ mainType = @"message";
+ break;
+ case MAILMIME_COMPOSITE_TYPE_MULTIPART:
+ mainType = @"multipart";
+ break;
+ }
+ }
+ }
+ }
+ mContentType = [[NSString alloc] initWithFormat:@"%@/%@", mainType, subType];
+ }
+ return self;
}
-- (void)dealloc {
- [super dealloc];
-}
-
-
- (id)content {
return nil;
}
+- (NSString *)contentType {
+ return mContentType;
+}
+
- (struct mailmime *)buildMIMEStruct {
return NULL;
}
@@ -60,7 +107,8 @@ - (NSString *)render {
mailmime_write_mem(str, &col, [self buildMIMEStruct]);
err = mmap_string_ref(str);
assert(err == 0);
- resultStr = [[NSString alloc] initWithBytes:str->str length:str->len encoding:NSASCIIStringEncoding];
+ resultStr = [[NSString alloc] initWithBytes:str->str length:str->len
+ encoding:NSASCIIStringEncoding];
mmap_string_free(str);
return [resultStr autorelease];
}
@@ -70,4 +118,9 @@ - (CTMIME_Enumerator *)mimeEnumerator {
enumerator = [[CTMIME_Enumerator alloc] initWithMIME:self];
return [enumerator autorelease];
}
+
+- (void)dealloc {
+ [mContentType release];
+ [super dealloc];
+}
@end
@@ -42,7 +42,7 @@ + (id)mimeMessagePartWithContent:(CTMIME *)mime {
- (id)initWithMIMEStruct:(struct mailmime *)mime
forMessage:(struct mailmessage *)message {
- self = [super init];
+ self = [super initWithMIMEStruct:mime forMessage:message];
if (self) {
struct mailmime *content = mime->mm_data.mm_message.mm_msg_mime;
myMessageContent = [CTMIMEFactory createMIMEWithMIMEStruct:content
@@ -41,7 +41,7 @@ + (id)mimeMultiPart {
}
- (id)initWithMIMEStruct:(struct mailmime *)mime forMessage:(struct mailmessage *)message {
- self = [super init];
+ self = [super initWithMIMEStruct:mime forMessage:message];
if (self) {
myContentList = [[NSMutableArray alloc] init];
clistiter *cur = clist_begin(mime->mm_data.mm_multipart.mm_mp_list);
@@ -42,7 +42,7 @@ @implementation CTMIME_SinglePart
- (id)initWithMIMEStruct:(struct mailmime *)mime
forMessage:(struct mailmessage *)message {
- self = [super init];
+ self = [super initWithMIMEStruct:mime forMessage:message];
if (self) {
self.data = nil;
mMime = mime;
View
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
@@ -164,9 +164,9 @@ - (void)testAttachments {
STAssertEqualObjects([[attachments objectAtIndex:0] filename], @"Picture 1.png", @"Incorrect filename");
CTBareAttachment *bareAttach = [attachments objectAtIndex:0];
CTCoreAttachment *attach = [bareAttach fetchFullAttachment];
- NSString *path = [NSString stringWithFormat:@"/tmp/%@", attach.filename];
- NSLog(@"path: %@\nobject: %@", path, attach);
- [attach writeToFile:path];
+ NSData *origData = [NSData dataWithContentsOfFile:
+ [NSString stringWithFormat:@"%@%@",filePrefix,@"TestData/Picture 1.png"]];
+ STAssertEqualObjects(origData, attach.data, @"Original data and attach data should be the same");
[msg release];
}
@end
View
@@ -141,12 +141,20 @@ - (void)testEnumerator {
NSArray *allObjects = [enumerator allObjects];
STAssertTrue([[allObjects objectAtIndex:0] isKindOfClass:[CTMIME_MessagePart class]],
@"Incorrect MIME structure found!");
+ STAssertEqualObjects([[allObjects objectAtIndex:0] contentType], @"message/rfc822",
+ @"found incorrect contentType");
STAssertTrue([[allObjects objectAtIndex:1] isKindOfClass:[CTMIME_MultiPart class]],
@"Incorrect MIME structure found!");
+ STAssertEqualObjects([[allObjects objectAtIndex:1] contentType], @"multipart/mixed",
+ @"found incorrect contentType");
STAssertTrue([[allObjects objectAtIndex:2] isKindOfClass:[CTMIME_TextPart class]],
@"Incorrect MIME structure found!");
+ STAssertEqualObjects([[allObjects objectAtIndex:2] contentType], @"text/plain",
+ @"found incorrect contentType");
STAssertTrue([[allObjects objectAtIndex:3] isKindOfClass:[CTMIME_ImagePart class]],
- @"Incorrect MIME structure found!");
+ @"Incorrect MIME structure found!");
+ STAssertEqualObjects([[allObjects objectAtIndex:3] contentType], @"image/png",
+ @"found incorrect contentType");
STAssertTrue([enumerator nextObject] == nil, @"Should have been nil");
NSArray *fullAllObjects = allObjects;

0 comments on commit 03f84a6

Please sign in to comment.