]> err.no Git - scalable-opengroupware.org/blob - UI/MailerUI/UIxMailEditorAction.m
e61b788ecd2174dfbce74dfbcf0dac0a4b32f5af
[scalable-opengroupware.org] / UI / MailerUI / UIxMailEditorAction.m
1 /*
2   Copyright (C) 2004-2005 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 #import <Foundation/NSDictionary.h>
23
24 #import <NGObjWeb/WOContext.h>
25 #import <NGObjWeb/WORequest.h>
26 #import <NGObjWeb/WOResponse.h>
27 #import <NGObjWeb/NSException+HTTP.h>
28 #import <NGExtensions/NSNull+misc.h>
29 #import <NGExtensions/NSException+misc.h>
30
31 #import <SoObjects/SOGo/NSString+Utilities.h>
32 #import <SoObjects/Mailer/SOGoDraftsFolder.h>
33 #import <SoObjects/Mailer/SOGoDraftObject.h>
34 #import <SoObjects/Mailer/SOGoMailAccount.h>
35 #import <SoObjects/Mailer/SOGoMailObject.h>
36
37 #import "UIxMailEditorAction.h"
38
39 @implementation UIxMailEditorAction
40
41 - (void)dealloc
42 {
43   [self->newDraft release];
44   [super dealloc];
45 }
46
47 /* caches */
48
49 - (void)reset {
50   [self->newDraft release]; self->newDraft = nil;
51 }
52
53 /* lookups */
54
55 - (SOGoDraftsFolder *)draftsFolder {
56   /* 
57      Note: we cannot use acquisition to find the nearest drafts folder, because
58            the IMAP4 server might contains an own Drafts folder.
59   */
60 //   SOGoDraftsFolder *drafts;
61   SOGoMailAccount *accountFolder;
62   
63   accountFolder = [[self clientObject] mailAccountFolder];
64
65   return [accountFolder
66            lookupName: [accountFolder draftsFolderNameInContext: context]
67            inContext: context acquire: NO];
68 }
69
70 /* errors */
71
72 - (id)didNotFindDraftsError {
73   // TODO: make a nice error page
74   return [@"did not find drafts folder in object: "
75            stringByAppendingString:[[self clientObject] description]];
76 }
77 - (id)couldNotCreateDraftError:(SOGoDraftsFolder *)_draftsFolder {
78   return [@"could not create a new draft in folder: "
79            stringByAppendingString:[_draftsFolder description]];
80 }
81 - (id)didNotFindMailError {
82   return [NSException exceptionWithHTTPStatus:404 /* Not Found */
83                       reason:@"Did not find mail for operation!"];
84 }
85
86 /* compose */
87
88 - (id) composeAction
89 {
90   SOGoDraftsFolder *drafts;
91   WOResponse *r;
92   NSString *urlBase, *url;
93   NSMutableDictionary *urlParams;
94   id parameter;
95   id returnValue;
96
97   drafts = [self draftsFolder];
98   if ([drafts isNotNull])
99     {
100       if ([drafts isKindOfClass: [NSException class]])
101         returnValue = drafts;
102       else
103         {
104           urlBase = [drafts newObjectBaseURLInContext: context];
105           if ([urlBase isNotNull])
106             {
107               urlParams = [NSMutableDictionary new];
108               [urlParams autorelease];
109
110               /* attach mail-account info */
111               parameter
112                 = [[self clientObject] valueForKey: @"mailAccountFolder"];
113               if (parameter && ![parameter isExceptionOrNull])
114                 [urlParams setObject: [parameter nameInContainer]
115                            forKey: @"account"];
116
117               parameter = [[self request] formValueForKey: @"mailto"];
118               if (parameter)
119                 [urlParams setObject: parameter
120                            forKey: @"mailto"];
121
122               url = [urlBase composeURLWithAction: @"edit"
123                              parameters: urlParams
124                              andHash: NO];
125  
126               /* perform redirect */
127               
128               [self debugWithFormat:@"compose on %@: %@", drafts, url];
129   
130               r = [context response];
131               [r setStatus: 302 /* move d */];
132               [r setHeader: url forKey: @"location"];
133               [self reset];
134
135               returnValue = r;
136             }
137           else
138             returnValue = [self couldNotCreateDraftError: drafts];
139         }
140     }
141   else
142     returnValue = [self didNotFindDraftsError];
143
144   return returnValue;
145 }
146
147 /* creating new draft object */
148
149 - (id)newDraftObject {
150   SOGoDraftsFolder *drafts;
151   
152   drafts = [self draftsFolder];
153   if (![drafts isNotNull])
154     return [self didNotFindDraftsError];
155   if ([drafts isKindOfClass:[NSException class]])
156     return drafts;
157
158   return [drafts newObjectInContext:context];
159 }
160
161 - (NSException *)_setupNewDraft {
162   SOGoDraftObject *tmp;
163   
164   /* create draft object */
165   
166   if ([(tmp = [self newDraftObject]) isKindOfClass:[NSException class]])
167     return (NSException *)tmp;
168   if (![tmp isNotNull]) { /* Note: should never happen? */
169     [self logWithFormat:@"WARNING: got no new draft object and no error!"];
170     return [self didNotFindDraftsError]; // TODO: not exact
171   }
172   
173   ASSIGN(self->newDraft, tmp);
174   //[self debugWithFormat:@"NEW DRAFT: %@", self->newDraft];
175   
176   return nil;
177 }
178
179 - (WOResponse *)redirectToEditNewDraft {
180   WOResponse *r;
181   NSString   *url;
182   
183   if (![self->newDraft isNotNull]) {
184     [self logWithFormat:@"ERROR(%s): missing new draft (already -reset?)",
185             __PRETTY_FUNCTION__];
186     return nil;
187   }
188   
189   url = [self->newDraft baseURLInContext:context];
190   if (![url hasSuffix:@"/"]) url = [url stringByAppendingString:@"/"];
191   url = [url stringByAppendingString:@"edit"];
192   
193   // TODO: debug log
194   [self logWithFormat:@"compose on %@", url];
195   
196   r = [context response];
197   [r setStatus:302 /* moved */];
198   [r setHeader:url forKey:@"location"];
199   [self reset];
200   return r;
201 }
202
203 @end /* UIxMailEditorAction */