]> err.no Git - sope/blob - sope-mime/NGMime/NGMimeBodyPart.m
synced with latest additions and bumped framework versions
[sope] / sope-mime / NGMime / NGMimeBodyPart.m
1 /*
2   Copyright (C) 2000-2005 SKYRIX Software AG
3
4   This file is part of SOPE.
5
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
9   later version.
10
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.
15
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
19   02111-1307, USA.
20 */
21
22 #include "NGMimeBodyPart.h"
23 #include "NGMimeType.h"
24 #include "common.h"
25 #include "NGMimeFileData.h"
26 #include <NGMime/NGMimePartParser.h>
27
28 @implementation NGMimeBodyPart
29
30 + (int)version {
31   return 2;
32 }
33
34 static NGMimeType *defaultType = nil;
35
36 + (void)initialize {
37   static BOOL isInitialized = NO;
38   if (!isInitialized) {
39     isInitialized = YES;
40     
41     defaultType =
42       [[NGMimeType mimeType:@"text/plain; charset=us-ascii"] retain];
43   }
44 }
45   
46 + (id)bodyPartWithHeader:(NGHashMap *)_header {
47   return [[[self alloc] initWithHeader:_header] autorelease];
48 }
49
50 - (id)initWithHeader:(NGHashMap *)_header {
51   if ((self = [super init])) {
52     self->header = [_header retain];
53     self->body   = nil;
54   }
55   return self;
56 }
57 - (id)init {
58   return [self initWithHeader:nil];
59 }
60
61 - (void)dealloc {
62   [self->header release];
63   [self->body   release];
64   [super dealloc];
65 }
66
67 /* NGPart */
68
69 - (NSEnumerator *)valuesOfHeaderFieldWithName:(NSString *)_name {
70   return [self->header objectEnumeratorForKey:_name];
71 }
72 - (NSEnumerator *)headerFieldNames {
73   return [self->header keyEnumerator];
74 }
75
76 - (void)setBody:(id)_body {
77   ASSIGN(self->body, _body);
78 }
79 - (id)body {
80   return self->body;
81 }
82
83 /* convenience */
84
85 - (NSString *)headerForKey:(NSString *)_key {
86   return [[self->header objectEnumeratorForKey:_key] nextObject];
87 }
88
89 - (NSArray *)headersForKey:(NSString *)_key {
90   NSEnumerator   *values;
91   NSMutableArray *array;
92   id value;
93   
94   if ((values = [self->header objectEnumeratorForKey:_key]) == nil)
95     return nil;
96   
97   array = [NSMutableArray arrayWithCapacity:4];
98   while ((value = [values nextObject]) != nil)
99     [array addObject:value];
100   return array;
101 }
102
103 - (NSArray *)headerKeys {
104   NSEnumerator *values;
105   NSMutableArray *array  = nil;
106   id name = nil;
107
108   if ((values = [self->header keyEnumerator]) == nil)
109     return nil;
110   
111   array = [[NSMutableArray alloc] init];
112   while ((name = [values nextObject]) != nil)
113     [array addObject:name];
114
115   name = [array copy];
116   [array release];
117   
118   return [name autorelease];
119 }
120
121 - (NSDictionary *)headers {
122   return [self->header asDictionary];
123 }
124
125 - (NSString *)headersAsString {
126   // TODO: not correct for MIME
127   NSMutableString *ms;
128   NSEnumerator *keys;
129   NSString     *key;
130   
131   ms = [NSMutableString stringWithCapacity:1024];
132   
133   /* headers */
134   keys = [[self headerKeys] objectEnumerator];
135   while ((key = [keys nextObject]) != nil) {
136     NSEnumerator *vals;
137     id val;
138     
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"];
145     }
146   }
147   return ms;
148 }
149
150 /* NGMimePart */
151
152 - (NGMimeType *)contentType {
153   id type;
154   static NGMimeHeaderNames *Fields = NULL;
155
156   if (!Fields)
157     Fields = (NGMimeHeaderNames *)[NGMimePartParser headerFieldNames];
158   
159   
160   type = [self->header objectForKey:Fields->contentType];
161   
162   if (![type isKindOfClass:[NGMimeType class]])
163     type = [NGMimeType mimeType:[type stringValue]];
164   
165   return (type != nil ? type : defaultType);
166 }
167
168 - (NSString *)contentId {
169   return [[self->header objectForKey:@"content-id"] stringValue];
170 }
171
172 - (NSArray *)contentLanguage {
173   id value;
174   
175   value = [self->header objectForKey:@"content-language"];
176   if (![value isKindOfClass:[NSArray class]])
177     value = [value componentsSeparatedByString:@","];
178
179   return value;
180 }
181
182 - (NSString *)contentMd5 {
183   return [[self->header objectForKey:@"content-md5"] stringValue];
184 }
185
186 - (NSString *)encoding {
187   return [[self->header objectForKey:@"content-transfer-encoding"]
188                         stringValue];
189 }
190
191 - (NSString *)contentDescription {
192   return [[self->header objectForKey:@"content-description"] stringValue];
193 }
194
195 /* description */
196
197 - (NSString *)description {
198   NSMutableString *d;
199   id b = [self body];
200
201   d = [NSMutableString stringWithCapacity:128];
202
203   [d appendFormat:@"<%@[0x%08X]: header=%@",
204        NSStringFromClass([self class]), self, self->header];
205
206   if (b) [d appendFormat:@" bodyClass=%@", NSStringFromClass([b class])];
207
208   if ([b isKindOfClass:[NGMimeFileData class]]) {
209     [d appendFormat:@" body=%@", b];
210   }
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];
215     }
216     else
217       [d appendFormat:@" body[len=%i]", [b length]];
218   }
219   else
220     [d appendFormat:@" body=%@", b];
221   
222   [d appendString:@">"];
223   return d;
224 }
225
226 @end /* NGMimeBodyPart */