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 #import <Foundation/NSException.h>
23 #import <Foundation/NSUserDefaults.h>
24 #import <NGObjWeb/NSException+HTTP.h>
25 #import <NGObjWeb/WORequest.h>
26 #import <NGObjWeb/WOResponse.h>
27 #import <NGExtensions/NSException+misc.h>
28 #import <NGExtensions/NSString+misc.h>
29 #import <NGImap4/NGImap4Envelope.h>
30 #import <NGImap4/NGImap4EnvelopeAddress.h>
31 #import <SoObjects/Mailer/SOGoMailObject.h>
32 #import <SoObjects/Mailer/SOGoMailAccount.h>
33 #import <SoObjects/Mailer/SOGoMailFolder.h>
34 #import <SOGoUI/UIxComponent.h>
35 #import <MailPartViewers/UIxMailRenderingContext.h> // cyclic
37 #import "WOContext+UIxMailer.h"
39 @interface UIxMailView : UIxComponent
44 - (BOOL)isDeletableClientObject;
48 @implementation UIxMailView
50 static NSString *mailETag = nil;
53 return [super version] + 0 /* v2 */;
57 NSUserDefaults *ud = [NSUserDefaults standardUserDefaults];
59 NSAssert2([super version] == 2,
60 @"invalid superclass (%@) version %i !",
61 NSStringFromClass([self superclass]), [super version]);
63 if ([ud boolForKey:@"SOGoDontUseETagsForMailViewer"]) {
64 NSLog(@"Note: usage of constant etag for mailer viewer is disabled.");
67 mailETag = [[NSString alloc] initWithFormat:@"\"imap4url_%d_%d_%03d\"",
68 UIX_MAILER_MAJOR_VERSION,
69 UIX_MAILER_MINOR_VERSION,
70 UIX_MAILER_SUBMINOR_VERSION];
71 NSLog(@"Note: using constant etag for mail viewer: '%@'", mailETag);
81 - (void) setCurrentAddress: (id) _addr
83 currentAddress = _addr;
88 return currentAddress;
91 - (NSString *) objectTitle
93 return [[self clientObject] subject];
96 - (NSString *) panelTitle
98 return [NSString stringWithFormat: @"%@: %@",
99 [self labelForKey: @"View Mail"],
103 /* links (DUP to UIxMailPartViewer!) */
105 - (NSString *) linkToEnvelopeAddress: (NGImap4EnvelopeAddress *) _address
107 // TODO: make some web-link, eg open a new compose panel?
108 return [NSString stringWithFormat: @"mailto: %@", [_address baseEMail]];
111 - (NSString *) currentAddressLink
113 return [self linkToEnvelopeAddress:[self currentAddress]];
120 return [[self clientObject] fetchCoreInfos];
125 return [[[self clientObject] ccEnvelopeAddresses] count] > 0 ? YES : NO;
130 - (id) contentViewerComponent
132 // TODO: I would prefer to flatten the body structure prior rendering,
133 // using some delegate to decide which parts to select for alternative.
136 info = [[self clientObject] bodyStructure];
138 return [[context mailRenderingContext] viewerForBodyInfo:info];
145 /* check etag to see whether we really must rerender */
146 if (mailETag != nil ) {
148 Note: There is one thing which *can* change for an existing message,
149 those are the IMAP4 flags (and annotations, which we do not use).
150 Since we don't render the flags, it should be OK, if this changes
151 we must embed the flagging into the etag.
155 if ((s = [[context request] headerForKey:@"if-none-match"])) {
156 if ([s rangeOfString:mailETag].length > 0) { /* not perfectly correct */
157 /* client already has the proper entity */
158 // [self logWithFormat:@"MATCH: %@ (tag %@)", s, mailETag];
160 if (![[self clientObject] doesMailExist]) {
161 return [NSException exceptionWithHTTPStatus:404 /* Not Found */
162 reason:@"message got deleted"];
165 [[context response] setStatus:304 /* Not Modified */];
166 return [context response];
171 if ([self message] == nil) {
172 // TODO: redirect to proper error
173 return [NSException exceptionWithHTTPStatus:404 /* Not Found */
174 reason:@"did not find specified message!"];
180 - (BOOL) isDeletableClientObject
182 return [[self clientObject] respondsToSelector: @selector (delete)];
185 - (BOOL) isInlineViewer
190 - (id) redirectToParentFolder
194 url = [[[self clientObject] container] baseURLInContext: context];
196 return [self redirectToLocation: url];
203 if (![self isDeletableClientObject]) {
204 return [NSException exceptionWithHTTPStatus:400 /* Bad Request */
205 reason:@"method cannot be invoked on "
206 @"the specified object"];
209 if ([self isInvokedBySafeMethod]) {
210 // TODO: fix UI to use POST for unsafe actions
211 [self logWithFormat:@"WARNING: method is invoked using safe HTTP method!"];
214 if ((ex = [[self clientObject] delete]) != nil) {
217 url = [[ex reason] stringByEscapingURL];
218 url = [@"view?error=" stringByAppendingString:url];
219 return [self redirectToLocation:url];
223 if (![self isInlineViewer]) {
224 // if everything is ok, close the window (send a JS closing the Window)
227 page = [self pageWithName:@"UIxMailWindowCloser"];
228 [page takeValue:@"YES" forKey:@"refreshOpener"];
232 return [self redirectToParentFolder];
239 if ([self isInvokedBySafeMethod]) {
240 // TODO: fix UI to use POST for unsafe actions
241 [self logWithFormat:@"WARNING: method is invoked using safe HTTP method!"];
244 if ((ex = [[self clientObject] trashInContext:context]) != nil) {
247 if ([[[context request] formValueForKey:@"jsonly"] boolValue])
248 /* called using XMLHttpRequest */
251 url = [[ex reason] stringByEscapingURL];
252 url = [@"view?error=" stringByAppendingString:url];
253 return [self redirectToLocation:url];
256 if ([[[context request] formValueForKey:@"jsonly"] boolValue]) {
257 /* called using XMLHttpRequest */
258 [[context response] setStatus:200 /* OK */];
259 return [context response];
262 if (![self isInlineViewer]) {
263 // if everything is ok, close the window (send a JS closing the Window)
266 page = [self pageWithName:@"UIxMailWindowCloser"];
267 [page takeValue:@"YES" forKey:@"refreshOpener"];
271 return [self redirectToParentFolder];
274 - (id <WOActionResults>) moveAction
276 id <WOActionResults> result;
277 NSString *destinationFolder;
280 if ([self isInvokedBySafeMethod]) {
281 // TODO: fix UI to use POST for unsafe actions
282 [self logWithFormat:@"WARNING: method is invoked using safe HTTP method!"];
285 destinationFolder = [self queryParameterForKey: @"tofolder"];
286 if ([destinationFolder length] > 0)
288 result = [[self clientObject] moveToFolderNamed: destinationFolder
292 if (![[[context request] formValueForKey:@"jsonly"] boolValue])
294 url = [NSString stringWithFormat: @"view?error=%@",
295 [[result reason] stringByEscapingURL]];
296 result = [self redirectToLocation: url];
301 result = [context response];
302 [result setStatus: 200];
306 result = [NSException exceptionWithHTTPStatus:500 /* Server Error */
307 reason: @"No destination folder given"];
312 /* generating response */
314 - (void) appendToResponse: (WOResponse *) _response
315 inContext: (WOContext *) _ctx
317 UIxMailRenderingContext *mctx;
320 [[_ctx response] setHeader:mailETag forKey:@"etag"];
322 mctx = [[UIxMailRenderingContext alloc] initWithViewer: self
325 [_ctx pushMailRenderingContext: mctx];
328 [super appendToResponse: _response inContext: _ctx];
330 [[_ctx popMailRenderingContext] reset];
333 @end /* UIxMailView */