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