]> err.no Git - sope/blob - sope-mime/NGImap4/NGImap4Envelope.m
NGImap4Envelope constructor
[sope] / sope-mime / NGImap4 / NGImap4Envelope.m
1 /*
2   Copyright (C) 2004 SKYRIX Software AG
3
4   This file is part of OpenGroupware.org.
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
22 #include "NGImap4Envelope.h"
23 #include "NGImap4EnvelopeAddress.h"
24 #include "imCommon.h"
25
26 @implementation NGImap4Envelope
27
28 - (id)newEnvelopeAddressForEMail:(id)_email {
29   if (![_email isNotNull])
30     return nil;
31   
32   if ([_email isKindOfClass:[NGImap4EnvelopeAddress class]])
33     return [_email copy];
34   
35   _email = [_email stringValue];
36   if ([_email length] == 0)
37     return nil;
38   return [[NGImap4EnvelopeAddress alloc] initWithString:_email];
39 }
40 - (NSArray *)envelopeAddressesForEMails:(NSArray *)_emails {
41   NSMutableArray *ma;
42   unsigned i, count;
43   
44   if (_emails == nil)
45     return nil;
46   if ((count = [_emails count]) == 0)
47     return [NSArray array];
48   
49   ma = [NSMutableArray arrayWithCapacity:count];
50   for (i = 0; i < count; i++) {
51     NGImap4EnvelopeAddress *envaddr;
52     
53     envaddr = [self newEnvelopeAddressForEMail:[_emails objectAtIndex:i]];
54     if (![envaddr isNotNull])
55       continue;
56     
57     [ma addObject:envaddr];
58     [envaddr release];
59   }
60   return ma;
61 }
62
63 - (id)initWithMessageID:(NSString *)_msgID subject:(NSString *)_subject
64   sender:(id)_sender replyTo:(id)_replyTo
65   to:(NSArray *)_to cc:(NSArray *)_cc bcc:(NSArray *)_bcc
66 {
67   if ((self = [self init])) {
68     self->msgId   = [_msgID copy];
69     self->subject = [_subject copy];
70     
71     self->from    = [self newEnvelopeAddressForEMail:_sender];
72     self->replyTo = [self newEnvelopeAddressForEMail:_replyTo];
73     
74     self->to  = [[self envelopeAddressesForEMails:_to]  copy];
75     self->cc  = [[self envelopeAddressesForEMails:_cc]  copy];
76     self->bcc = [[self envelopeAddressesForEMails:_bcc] copy];
77   }
78   return self;
79 }
80
81 - (void)dealloc {
82   [self->date      release];
83   [self->subject   release];
84   [self->inReplyTo release];
85   [self->msgId     release];
86   [self->from      release];
87   [self->sender    release];
88   [self->replyTo   release];
89   [self->to        release];
90   [self->cc        release];
91   [self->bcc       release];
92   [super dealloc];
93 }
94
95 /* accessors */
96
97 - (NSCalendarDate *)date {
98   return self->date;
99 }
100 - (id)subject {
101   return self->subject;
102 }
103 - (NSString *)inReplyTo {
104   return self->inReplyTo;
105 }
106 - (NSString *)messageID {
107   return self->msgId;
108 }
109 - (NGImap4EnvelopeAddress *)from {
110   return self->from;
111 }
112 - (NGImap4EnvelopeAddress *)sender {
113   return self->sender;
114 }
115 - (NGImap4EnvelopeAddress *)replyTo {
116   return self->replyTo;
117 }
118 - (NSArray *)to {
119   return self->to;
120 }
121 - (NSArray *)cc {
122   return self->cc;
123 }
124 - (NSArray *)bcc {
125   return self->bcc;
126 }
127
128 /* derived accessors */
129
130 - (BOOL)hasTo {
131   return [self->to count] > 0 ? YES : NO;
132 }
133 - (BOOL)hasCC {
134   return [self->cc count] > 0 ? YES : NO;
135 }
136 - (BOOL)hasBCC {
137   return [self->bcc count] > 0 ? YES : NO;
138 }
139
140 /* description */
141
142 - (NSString *)description {
143   NSMutableString *ms;
144   
145   ms = [NSMutableString stringWithCapacity:128];
146   [ms appendFormat:@"<0x%08X[%@]:", self, NSStringFromClass([self class])];
147
148   if (self->date)      [ms appendFormat:@" date='%@'",      self->date];
149   if (self->subject)   [ms appendFormat:@" subject='%@'",   self->subject];
150   if (self->msgId)     [ms appendFormat:@" msgid='%@'",     self->msgId];
151   if (self->inReplyTo) [ms appendFormat:@" inreplyto='%@'", self->inReplyTo];
152   
153   if (self->from)   [ms appendFormat:@" from=%@",   [self->from   email]];
154   if (self->sender) [ms appendFormat:@" sender=%@", [self->sender email]];
155   
156   if (self->to)  [ms appendFormat:@" to=%@",  self->to];
157   if (self->cc)  [ms appendFormat:@" cc=%@",  self->cc];
158   if (self->bcc) [ms appendFormat:@" bcc=%@", self->bcc];
159   
160   [ms appendString:@">"];
161   return ms;
162 }
163
164 @end /* NGImap4Envelope */