]> err.no Git - sope/blob - sope-mime/NGMime/NGMimeMultipartBody.m
minor fixes
[sope] / sope-mime / NGMime / NGMimeMultipartBody.m
1 /*
2   Copyright (C) 2000-2003 SKYRIX Software AG
3
4   This file is part of OGo
5
6   OGo 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   OGo 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 OGo; 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 // $Id$
22
23 #include "NGMimeMultipartBody.h"
24 #include "NGMimeBodyParser.h"
25 #include "common.h"
26
27 @implementation NGMimeMultipartBody
28
29 + (int)version {
30   return 2;
31 }
32
33 - (id)initWithPart:(id<NGMimePart>)_part {
34   if (_part == nil) {
35     NSLog(@"ERROR(%s): no part provided for multipart body !",
36           __PRETTY_FUNCTION__);
37     [self release];
38     return nil;
39   }
40   if ((self = [super init])) {
41     self->flags.isParsed = YES;
42   }
43   return self;
44 }
45 - (id)initWithPart:(id<NGMimePart>)_part data:(NSData *)_data 
46   delegate:(id)_del 
47 {
48   if ((self = [self initWithPart:_part])) {
49     self->flags.isParsed = NO;
50     self->rawData        = [_data retain];
51     self->delegate       = _del;
52   }
53   return self;
54 }
55 - (id)init {
56   return [self initWithPart:nil];
57 }
58
59 - (void)dealloc {
60   [self->rawData   release];
61   [self->prefix    release];
62   [self->suffix    release];
63   [self->bodyParts release];
64   [super dealloc];
65 }
66
67 /* parsing */
68
69 - (void)parse {
70   NGMimeMultipartBodyParser *parser = [[NGMimeMultipartBodyParser alloc] init];
71
72   self->flags.isParsed = YES;
73   
74   if (![parser parseBody:  self
75                ofMultipart:self->part
76                data:       self->rawData
77                delegate:   self->delegate])
78     NSLog(@"%@: error during parsing of multipart body (ignored)", self);
79
80   self->delegate = nil;
81
82   [parser release]; parser = nil;
83 }
84
85 static inline void _checkParse(NGMimeMultipartBody *self) {
86   if (!self->flags.isParsed) [self parse];
87 }
88 static inline void _checkArray(NGMimeMultipartBody *self) {
89   if (!self->flags.isParsed) [self parse];
90   if (self->bodyParts == nil)
91     self->bodyParts = [[NSMutableArray alloc] init];
92 }
93
94 /* accessors */
95
96 - (id<NGMimePart>)part { // the part the body belongs to
97   return self->part;
98 }
99
100 - (NSArray *)parts {
101   return self->bodyParts;
102 }
103
104 - (void)addBodyPart:(id<NGPart>)_part {
105   _checkArray(self);
106   [self->bodyParts addObject:_part];
107 }
108 - (void)addBodyPart:(id<NGPart>)_part atIndex:(int)_idx {
109   _checkArray(self);
110   [self->bodyParts insertObject:_part atIndex:_idx];
111 }
112
113 - (void)removeBodyPart:(id<NGPart>)_part {
114   _checkArray(self);
115   [self->bodyParts removeObject:_part];
116 }
117 - (void)removeBodyPartAtIndex:(int)_idx {
118   _checkArray(self);
119   [self->bodyParts removeObjectAtIndex:_idx];
120 }
121
122 - (void)setPrefix:(NSString *)_prefix {
123   if (self->prefix != _prefix) {
124     [self->prefix release];
125     self->prefix = [_prefix copy];
126   }
127 }
128 - (NSString *)prefix {
129   return self->prefix;
130 }
131
132 - (void)setSuffix:(NSString *)_suffix {
133   if (self->suffix != _suffix) {
134     [self->suffix release];
135     self->suffix = [_suffix copy];
136   }
137 }
138 - (NSString *)suffix {
139   return self->suffix;
140 }
141
142 /* description */
143
144 - (NSString *)description {
145   NSMutableString *d = [NSMutableString stringWithCapacity:64];
146
147   [d appendFormat:@"<%@[0x%08X]:", NSStringFromClass([self class]), self];
148   
149   if (self->flags.isParsed) {
150     if (self->prefix)    [d appendFormat:@" prefix=%@", self->prefix];
151     if (self->suffix)    [d appendFormat:@" suffix=%@", self->suffix];
152     if (self->bodyParts) [d appendFormat:@" parts=%@",  self->bodyParts];
153   }
154   if (self->rawData) [d appendFormat:@" data=%@", self->rawData];
155   
156   [d appendString:@">"];
157   return d;
158 }
159
160 @end /* NGMimeMultipartBody */