2 Copyright (C) 2000-2005 SKYRIX Software AG
4 This file is part of SOPE.
6 SOPE is free software; you can redistribute it and/or modify it under
7 the terms of the GNU Lesser General Public License as published by the
8 Free Software Foundation; either version 2, or (at your option) any
11 SOPE is distributed in the hope that it will be useful, but WITHOUT ANY
12 WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
14 License for more details.
16 You should have received a copy of the GNU Lesser General Public
17 License along with SOPE; see the file COPYING. If not, write to the
18 Free Software Foundation, 59 Temple Place - Suite 330, Boston, MA
22 #include "NGMimeBodyPart.h"
23 #include "NGMimeType.h"
25 #include "NGMimeFileData.h"
26 #include <NGMime/NGMimePartParser.h>
28 @implementation NGMimeBodyPart
34 static NGMimeType *defaultType = nil;
37 static BOOL isInitialized = NO;
42 [[NGMimeType mimeType:@"text/plain; charset=us-ascii"] retain];
46 + (id)bodyPartWithHeader:(NGHashMap *)_header {
47 return [[[self alloc] initWithHeader:_header] autorelease];
50 - (id)initWithHeader:(NGHashMap *)_header {
51 if ((self = [super init])) {
52 self->header = [_header retain];
58 return [self initWithHeader:nil];
62 [self->header release];
69 - (NSEnumerator *)valuesOfHeaderFieldWithName:(NSString *)_name {
70 return [self->header objectEnumeratorForKey:_name];
72 - (NSEnumerator *)headerFieldNames {
73 return [self->header keyEnumerator];
76 - (void)setBody:(id)_body {
77 ASSIGN(self->body, _body);
85 - (NSString *)headerForKey:(NSString *)_key {
86 return [[self->header objectEnumeratorForKey:_key] nextObject];
89 - (NSArray *)headersForKey:(NSString *)_key {
91 NSMutableArray *array;
94 if ((values = [self->header objectEnumeratorForKey:_key]) == nil)
97 array = [NSMutableArray arrayWithCapacity:4];
98 while ((value = [values nextObject]) != nil)
99 [array addObject:value];
103 - (NSArray *)headerKeys {
104 NSEnumerator *values;
105 NSMutableArray *array = nil;
108 if ((values = [self->header keyEnumerator]) == nil)
111 array = [[NSMutableArray alloc] init];
112 while ((name = [values nextObject]) != nil)
113 [array addObject:name];
118 return [name autorelease];
121 - (NSDictionary *)headers {
122 return [self->header asDictionary];
125 - (NSString *)headersAsString {
126 // TODO: not correct for MIME
131 ms = [NSMutableString stringWithCapacity:1024];
134 keys = [[self headerKeys] objectEnumerator];
135 while ((key = [keys nextObject]) != nil) {
139 vals = [[self headersForKey:key] objectEnumerator];
140 while ((val = [vals nextObject])) {
141 [ms appendString:key];
142 [ms appendString:@": "];
143 [ms appendString:[val stringValue]];
144 [ms appendString:@"\r\n"];
152 - (NGMimeType *)contentType {
154 static NGMimeHeaderNames *Fields = NULL;
157 Fields = (NGMimeHeaderNames *)[NGMimePartParser headerFieldNames];
160 type = [self->header objectForKey:Fields->contentType];
162 if (![type isKindOfClass:[NGMimeType class]])
163 type = [NGMimeType mimeType:[type stringValue]];
165 return (type != nil ? type : defaultType);
168 - (NSString *)contentId {
169 return [[self->header objectForKey:@"content-id"] stringValue];
172 - (NSArray *)contentLanguage {
175 value = [self->header objectForKey:@"content-language"];
176 if (![value isKindOfClass:[NSArray class]])
177 value = [value componentsSeparatedByString:@","];
182 - (NSString *)contentMd5 {
183 return [[self->header objectForKey:@"content-md5"] stringValue];
186 - (NSString *)encoding {
187 return [[self->header objectForKey:@"content-transfer-encoding"]
191 - (NSString *)contentDescription {
192 return [[self->header objectForKey:@"content-description"] stringValue];
197 - (NSString *)description {
201 d = [NSMutableString stringWithCapacity:128];
203 [d appendFormat:@"<%@[0x%08X]: header=%@",
204 NSStringFromClass([self class]), self, self->header];
206 if (b) [d appendFormat:@" bodyClass=%@", NSStringFromClass([b class])];
208 if ([b isKindOfClass:[NGMimeFileData class]]) {
209 [d appendFormat:@" body=%@", b];
211 else if ([b isKindOfClass:[NSString class]] ||
212 [b isKindOfClass:[NSData class]]) {
213 if ([b length] < 512) {
214 [d appendFormat:@" bodyLen=%i body=%@", [b length], b];
217 [d appendFormat:@" body[len=%i]", [b length]];
220 [d appendFormat:@" body=%@", b];
222 [d appendString:@">"];
226 @end /* NGMimeBodyPart */