2 Copyright (C) 2004-2005 SKYRIX Software AG
4 This file is part of OpenGroupware.org.
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
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.
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
22 #include <SOGoUI/UIxComponent.h>
27 An mail editor component which works on SOGoDraftObject's.
30 @class NSArray, NSString;
31 @class SOGoMailFolder;
33 @interface UIxMailEditor : UIxComponent
40 SOGoMailFolder *sentFolder;
45 #include <SOGo/SoObjects/Mailer/SOGoDraftObject.h>
46 #include <SOGo/SoObjects/Mailer/SOGoMailFolder.h>
47 #include <SOGo/SoObjects/Mailer/SOGoMailAccount.h>
48 #include <NGMail/NGMimeMessage.h>
49 #include <NGMail/NGMimeMessageGenerator.h>
52 @interface UIxComponent (Scheduler_Privates)
53 - (NSString *)emailForUser;
56 @implementation UIxMailEditor
58 static BOOL keepMailTmpFile = NO;
59 static NSArray *infoKeys = nil;
62 infoKeys = [[NSArray alloc] initWithObjects:
63 @"subject", @"text", @"to", @"cc", @"bcc",
68 NSLog(@"WARNING: keeping mail files.");
72 [self->sentFolder release];
74 [self->subject release];
83 - (void)setFrom:(NSString *)_ignore {
86 return [self emailForUser];
88 - (void)setReplyTo:(NSString *)_ignore {
90 - (NSString *)replyTo {
91 /* we are here for future extensibility */
95 - (void)setSubject:(NSString *)_value {
96 ASSIGNCOPY(self->subject, _value);
98 - (NSString *)subject {
99 return self->subject ? self->subject : @"";
102 - (void)setText:(NSString *)_value {
103 ASSIGNCOPY(self->text, _value);
106 return [self->text isNotNull] ? self->text : @"";
109 - (void)setTo:(NSArray *)_value {
110 ASSIGNCOPY(self->to, _value);
113 return [self->to isNotNull] ? self->to : [NSArray array];
116 - (void)setCc:(NSArray *)_value {
117 ASSIGNCOPY(self->cc, _value);
120 return [self->cc isNotNull] ? self->cc : [NSArray array];
123 - (void)setBcc:(NSArray *)_value {
124 ASSIGNCOPY(self->bcc, _value);
127 return [self->bcc isNotNull] ? self->bcc : [NSArray array];
132 - (void)loadInfo:(NSDictionary *)_info {
133 if (![_info isNotNull]) return;
134 [self debugWithFormat:@"loading info ..."];
135 [self takeValuesFromDictionary:_info];
137 - (NSDictionary *)storeInfo {
138 [self debugWithFormat:@"storing info ..."];
139 return [self valuesForKeys:infoKeys];
144 - (BOOL)shouldTakeValuesFromRequest:(WORequest *)_rq inContext:(WOContext*)_c{
150 - (NSException *)patchFlagsInStore {
153 if the draft is a reply => [message markAnswered]
154 if the draft is a forward => [message addFlag:@"forwarded"]
156 This is hard, we would need to find the original message in Cyrus.
161 - (id)lookupSentFolder {
162 /* lookup INBOX/Sent folder */
163 SOGoMailAccount *account;
164 SOGoMailFolder *folder;
166 if (self->sentFolder != nil)
169 account = [[self clientObject] mailAccountFolder];
170 if ([account isKindOfClass:[NSException class]]) return account;
172 folder = [account lookupName:@"INBOX" inContext:[self context] acquire:NO];
173 if ([folder isKindOfClass:[NSException class]]) return folder;
175 folder = [folder lookupName:@"Sent" inContext:[self context] acquire:NO];
176 if ([folder isKindOfClass:[NSException class]]) return folder;
178 if (![folder isNotNull]) {
179 return [NSException exceptionWithHTTPStatus:500 /* server error */
180 reason:@"did not find Sent folder!"];
183 self->sentFolder = [folder retain];
184 return self->sentFolder;
187 - (NSException *)storeMailInSentFolder:(NSString *)_path {
188 SOGoMailFolder *folder;
192 folder = [self lookupSentFolder];
193 if ([folder isKindOfClass:[NSException class]]) return (id)folder;
195 if ((data = [[NSData alloc] initWithContentsOfMappedFile:_path]) == nil) {
196 return [NSException exceptionWithHTTPStatus:500 /* server error */
197 reason:@"could not temporary draft file!"];
200 result = [folder postData:data flags:@"seen"];
201 [data release]; data = nil;
207 - (BOOL)_saveFormInfo {
210 if ((info = [self storeInfo]) != nil) {
211 if (![[self clientObject] storeInfo:info]) {
212 [self errorWithFormat:@"failed to store draft!"];
213 // TODO: improve error handling
218 // TODO: wrap content
222 - (id)failedToSaveFormResponse {
223 // TODO: improve error handling
224 return [NSException exceptionWithHTTPStatus:500 /* server error */
225 reason:@"failed to store draft object on server!"];
228 - (id)defaultAction {
229 return [self redirectToLocation:@"edit"];
233 [self logWithFormat:@"edit action, load content from: %@",
234 [self clientObject]];
236 [self loadInfo:[[self clientObject] fetchInfo]];
241 return [self _saveFormInfo] ? self : [self failedToSaveFormResponse];
248 /* first, save form data */
250 if (![self _saveFormInfo])
251 return [self failedToSaveFormResponse];
253 /* save mail to file (so that we can upload the mail to Cyrus */
255 mailPath = [[self clientObject] saveMimeMessageToTemporaryFile];
257 /* then, send mail */
259 if ((error = [[self clientObject] sendMimeMessageAtPath:mailPath]) != nil) {
260 // TODO: improve error handling
261 [[NSFileManager defaultManager] removeFileAtPath:mailPath handler:nil];
265 /* patch flags in store for replies etc */
267 if ((error = [self patchFlagsInStore]) != nil)
270 /* finally store in Sent */
272 if ((error = [self storeMailInSentFolder:mailPath]) != nil)
275 /* delete temporary mail file */
278 [self warnWithFormat:@"keeping mail file: '%@'", mailPath];
280 [[NSFileManager defaultManager] removeFileAtPath:mailPath handler:nil];
285 if ((error = [[self clientObject] delete]) != nil)
288 // if everything is ok, close the window (send a JS closing the Window)
289 return [self pageWithName:@"UIxMailWindowCloser"];
295 if ((error = [[self clientObject] delete]) != nil)
301 @end /* UIxMailEditor */