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
42 SOGoMailFolder *sentFolder;
47 #include <SoObjects/Mailer/SOGoDraftObject.h>
48 #include <SoObjects/Mailer/SOGoMailFolder.h>
49 #include <SoObjects/Mailer/SOGoMailAccount.h>
50 #include <SoObjects/SOGo/WOContext+Agenor.h>
51 #include <NGMail/NGMimeMessage.h>
52 #include <NGMail/NGMimeMessageGenerator.h>
55 @interface UIxComponent (Scheduler_Privates)
56 - (NSString *)emailForUser;
59 @implementation UIxMailEditor
61 static BOOL keepMailTmpFile = NO;
62 static BOOL showInternetMarker = NO;
63 static EOQualifier *internetDetectQualifier = nil;
64 static NSDictionary *internetMailHeaders = nil;
65 static NSArray *infoKeys = nil;
68 NSUserDefaults *ud = [NSUserDefaults standardUserDefaults];
71 infoKeys = [[NSArray alloc] initWithObjects:
72 @"subject", @"text", @"to", @"cc", @"bcc",
76 keepMailTmpFile = [ud boolForKey:@"SOGoMailEditorKeepTmpFile"];
78 NSLog(@"WARNING: keeping mail files.");
80 /* Internet mail settings */
82 showInternetMarker = [ud boolForKey:@"SOGoShowInternetMarker"];
83 if (!showInternetMarker) {
84 NSLog(@"Note: visual Internet marker on mail editor disabled "
85 @"(SOGoShowInternetMarker)");
88 if ((s = [ud stringForKey:@"SOGoInternetDetectQualifier"]) != nil) {
89 internetDetectQualifier =
90 [[EOQualifier qualifierWithQualifierFormat:s] retain];
91 if (internetDetectQualifier == nil)
92 NSLog(@"UIxMailEditor: could not parse qualifier: '%@'", s);
94 if (internetDetectQualifier == nil)
95 NSLog(@"UIxMailEditor: no 'SOGoInternetDetectQualifier' configured.");
97 NSLog(@"UIxMailEditor: detect Internet access using: %@",
98 internetDetectQualifier);
101 internetMailHeaders =
102 [[ud dictionaryForKey:@"SOGoInternetMailHeaders"] copy];
103 NSLog(@"Note: specified %d headers for mails send via the Internet.",
104 [internetMailHeaders count]);
108 [self->sentFolder release];
109 [self->fromEMails release];
110 [self->from release];
111 [self->text release];
112 [self->subject release];
121 - (void)setFrom:(NSString *)_value {
122 ASSIGNCOPY(self->from, _value);
125 if (![self->from isNotNull])
126 return [self emailForUser];
130 - (void)setReplyTo:(NSString *)_ignore {
132 - (NSString *)replyTo {
133 /* we are here for future extensibility */
137 - (void)setSubject:(NSString *)_value {
138 ASSIGNCOPY(self->subject, _value);
140 - (NSString *)subject {
141 return self->subject ? self->subject : @"";
144 - (void)setText:(NSString *)_value {
145 ASSIGNCOPY(self->text, _value);
148 return [self->text isNotNull] ? self->text : @"";
151 - (void)setTo:(NSArray *)_value {
152 ASSIGNCOPY(self->to, _value);
155 return [self->to isNotNull] ? self->to : [NSArray array];
158 - (void)setCc:(NSArray *)_value {
159 ASSIGNCOPY(self->cc, _value);
162 return [self->cc isNotNull] ? self->cc : [NSArray array];
165 - (void)setBcc:(NSArray *)_value {
166 ASSIGNCOPY(self->bcc, _value);
169 return [self->bcc isNotNull] ? self->bcc : [NSArray array];
174 - (NSArray *)fromEMails {
175 NSString *primary, *uid;
178 if (self->fromEMails != nil)
179 return self->fromEMails;
181 uid = [[self user] login];
182 primary = [self emailForUser];
183 if (![[self context] isAccessFromIntranet]) {
184 self->fromEMails = [[NSArray alloc] initWithObjects:&primary count:1];
185 return self->fromEMails;
189 [[[self context] activeUser] valueForKey:@"primaryIMAP4AccountString"];
190 if ([shares count] == 0)
191 self->fromEMails = [[NSArray alloc] initWithObjects:&primary count:1];
195 tmp = [[NSArray alloc] initWithObjects:&primary count:1];
196 self->fromEMails = [[tmp arrayByAddingObjectsFromArray:shares] copy];
197 [tmp release]; tmp = nil;
199 return self->fromEMails;
204 - (NSString *)panelTitle {
205 return [self labelForKey:@"Compose Mail"];
208 /* detect webmail being accessed from the outside */
210 - (BOOL)isInternetRequest {
211 return [[self context] isAccessFromIntranet] ? NO : YES;
214 - (BOOL)showInternetMarker {
215 if (!showInternetMarker)
217 return [self isInternetRequest];
222 - (void)loadInfo:(NSDictionary *)_info {
223 if (![_info isNotNull]) return;
224 [self debugWithFormat:@"loading info ..."];
225 [self takeValuesFromDictionary:_info];
227 - (NSDictionary *)storeInfo {
228 [self debugWithFormat:@"storing info ..."];
229 return [self valuesForKeys:infoKeys];
234 - (BOOL)shouldTakeValuesFromRequest:(WORequest *)_rq inContext:(WOContext*)_c{
240 - (NSException *)patchFlagsInStore {
243 if the draft is a reply => [message markAnswered]
244 if the draft is a forward => [message addFlag:@"forwarded"]
246 This is hard, we would need to find the original message in Cyrus.
251 - (id)lookupSentFolder {
252 SOGoMailAccount *account;
253 SOGoMailFolder *folder;
255 if (self->sentFolder != nil)
258 account = [[self clientObject] mailAccountFolder];
259 if ([account isKindOfClass:[NSException class]]) return account;
261 folder = [account sentFolderInContext:[self context]];
262 if ([folder isKindOfClass:[NSException class]]) return folder;
263 return ((self->sentFolder = [folder retain]));
266 - (NSException *)storeMailInSentFolder:(NSString *)_path {
267 SOGoMailFolder *folder;
271 folder = [self lookupSentFolder];
272 if ([folder isKindOfClass:[NSException class]]) return (id)folder;
274 if ((data = [[NSData alloc] initWithContentsOfMappedFile:_path]) == nil) {
275 return [NSException exceptionWithHTTPStatus:500 /* server error */
276 reason:@"could not temporary draft file!"];
279 result = [folder postData:data flags:@"seen"];
280 [data release]; data = nil;
286 - (BOOL)_saveFormInfo {
289 if ((info = [self storeInfo]) != nil) {
292 if ((error = [[self clientObject] storeInfo:info]) != nil) {
293 [self errorWithFormat:@"failed to store draft: %@", error];
294 // TODO: improve error handling
299 // TODO: wrap content
303 - (id)failedToSaveFormResponse {
304 // TODO: improve error handling
305 return [NSException exceptionWithHTTPStatus:500 /* server error */
306 reason:@"failed to store draft object on server!"];
309 - (id)defaultAction {
310 return [self redirectToLocation:@"edit"];
315 [self logWithFormat:@"edit action, load content from: %@",
316 [self clientObject]];
319 [self loadInfo:[[self clientObject] fetchInfo]];
324 return [self _saveFormInfo] ? self : [self failedToSaveFormResponse];
332 // TODO: need to validate whether we have a To etc
334 /* first, save form data */
336 if (![self _saveFormInfo])
337 return [self failedToSaveFormResponse];
339 /* setup some extra headers if required */
341 h = [self isInternetRequest] ? internetMailHeaders : nil;
343 /* save mail to file (so that we can upload the mail to Cyrus) */
344 // TODO: all this could be handled by the SOGoDraftObject?
346 mailPath = [[self clientObject] saveMimeMessageToTemporaryFileWithHeaders:h];
348 /* then, send mail */
350 if ((error = [[self clientObject] sendMimeMessageAtPath:mailPath]) != nil) {
351 // TODO: improve error handling
352 [[NSFileManager defaultManager] removeFileAtPath:mailPath handler:nil];
356 /* patch flags in store for replies etc */
358 if ((error = [self patchFlagsInStore]) != nil)
361 /* finally store in Sent */
363 if ((error = [self storeMailInSentFolder:mailPath]) != nil)
366 /* delete temporary mail file */
369 [self warnWithFormat:@"keeping mail file: '%@'", mailPath];
371 [[NSFileManager defaultManager] removeFileAtPath:mailPath handler:nil];
376 if ((error = [[self clientObject] delete]) != nil)
379 // if everything is ok, close the window (send a JS closing the Window)
380 return [self pageWithName:@"UIxMailWindowCloser"];
387 if ((error = [[self clientObject] delete]) != nil)
391 page = [self pageWithName:@"UIxMailWindowCloser"];
392 [page takeValue:@"YES" forKey:@"refreshOpener"];
395 // TODO: if we just return nil, we produce a 500
396 return [NSException exceptionWithHTTPStatus:204 /* No Content */
397 reason:@"object was deleted."];
401 @end /* UIxMailEditor */