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