]> err.no Git - scalable-opengroupware.org/blob - UI/MailerUI/UIxMailActions.m
Install libs to /usr/lib
[scalable-opengroupware.org] / UI / MailerUI / UIxMailActions.m
1 /* UIxMailActions.m - this file is part of SOGo
2  *
3  * Copyright (C) 2007 Inverse groupe conseil
4  *
5  * Author: Wolfgang Sourdeau <wsourdeau@inverse.ca>
6  *
7  * This file is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation; either version 2, or (at your option)
10  * any later version.
11  *
12  * This file is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; see the file COPYING.  If not, write to
19  * the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
20  * Boston, MA 02111-1307, USA.
21  */
22
23 #import <Foundation/NSArray.h>
24 #import <Foundation/NSString.h>
25
26 #import <NGObjWeb/WOContext.h>
27 #import <NGObjWeb/WORequest.h>
28 #import <NGObjWeb/WOResponse.h>
29 #import <NGObjWeb/NSException+HTTP.h>
30
31 #import <SoObjects/Mailer/SOGoDraftObject.h>
32 #import <SoObjects/Mailer/SOGoDraftsFolder.h>
33 #import <SoObjects/Mailer/SOGoMailAccount.h>
34 #import <SoObjects/Mailer/SOGoMailObject.h>
35
36 #import "../Common/WODirectAction+SOGo.h"
37
38 #import "UIxMailActions.h"
39
40 @implementation UIxMailActions
41
42 - (WOResponse *) replyToAll: (BOOL) toAll
43 {
44   SOGoMailAccount *account;
45   SOGoMailObject *co;
46   SOGoDraftsFolder *folder;
47   SOGoDraftObject *newMail;
48   NSString *newLocation;
49
50   co = [self clientObject];
51   account = [co mailAccountFolder];
52   folder = [account draftsFolderInContext: context];
53   newMail = [folder newDraft];
54   [newMail fetchMailForReplying: co toAll: toAll];
55
56   newLocation = [NSString stringWithFormat: @"%@/edit",
57                           [newMail baseURLInContext: context]];
58
59   return [self redirectToLocation: newLocation];
60 }
61
62 - (WOResponse *) replyAction
63 {
64   return [self replyToAll: NO];
65 }
66
67 - (WOResponse *) replyToAllAction
68 {
69   return [self replyToAll: YES];
70 }
71
72 - (WOResponse *) forwardAction
73 {
74   SOGoMailAccount *account;
75   SOGoMailObject *co;
76   SOGoDraftsFolder *folder;
77   SOGoDraftObject *newMail;
78   NSString *newLocation;
79
80   co = [self clientObject];
81   account = [co mailAccountFolder];
82   folder = [account draftsFolderInContext: context];
83   newMail = [folder newDraft];
84   [newMail fetchMailForForwarding: co];
85
86   newLocation = [NSString stringWithFormat: @"%@/edit",
87                           [newMail baseURLInContext: context]];
88
89   return [self redirectToLocation: newLocation];
90 }
91
92 - (id) trashAction
93 {
94   id response;
95
96   response = [[self clientObject] trashInContext: context];
97   if (!response)
98     response = [self responseWith204];
99
100   return response;
101 }
102
103 - (id) moveAction
104 {
105   NSString *destinationFolder;
106   id response;
107
108   destinationFolder = [[context request] formValueForKey: @"folder"];
109   if ([destinationFolder length] > 0)
110     {
111       response = [[self clientObject] moveToFolderNamed: destinationFolder
112                                       inContext: context];
113       if (!response)
114         response = [self responseWith204];
115     }
116   else
117     response = [NSException exceptionWithHTTPStatus: 500 /* Server Error */
118                             reason: @"No destination folder given"];
119
120   return response;
121 }
122
123 - (id) copyAction
124 {
125   NSString *destinationFolder;
126   id response;
127
128   destinationFolder = [[context request] formValueForKey: @"folder"];
129   if ([destinationFolder length] > 0)
130     {
131       response = [[self clientObject] copyToFolderNamed: destinationFolder
132                                       inContext: context];
133       if (!response)
134         response = [self responseWith204];
135     }
136   else
137     response = [NSException exceptionWithHTTPStatus: 500 /* Server Error */
138                             reason: @"No destination folder given"];
139
140   return response;
141 }
142
143 /* active message */
144
145 - (id) markMessageUnreadAction 
146 {
147   id response;
148
149   response = [[self clientObject] removeFlags: @"seen"];
150   if (!response)
151     response = [self responseWith204];
152
153   return response;
154 }
155
156 - (id) markMessageReadAction 
157 {
158   id response;
159
160   response = [[self clientObject] addFlags: @"seen"];
161   if (!response)
162     response = [self responseWith204];
163
164   return response;
165 }
166
167 /* SOGoDraftObject */
168 - (WOResponse *) editAction
169 {
170   SOGoMailAccount *account;
171   SOGoMailObject *co;
172   SOGoDraftsFolder *folder;
173   SOGoDraftObject *newMail;
174   NSString *newLocation;
175
176   co = [self clientObject];
177   account = [co mailAccountFolder];
178   folder = [account draftsFolderInContext: context];
179   newMail = [folder newDraft];
180   [newMail fetchMailForEditing: co];
181   [newMail storeInfo];
182
183   newLocation = [NSString stringWithFormat: @"%@/edit",
184                           [newMail baseURLInContext: context]];
185
186   return [self redirectToLocation: newLocation];
187 }
188
189 - (id) deleteAction
190 {
191   SOGoDraftObject *draft;
192   NSException *error;
193   id response;
194
195   draft = [self clientObject];
196   error = [draft delete];
197   if (error)
198     response = error;
199   else
200     response = [self responseWith204];
201
202   return response;
203 }
204
205 - (WOResponse *) deleteAttachmentAction
206 {
207   WOResponse *response;
208   NSString *filename;
209
210   filename = [[context request] formValueForKey: @"filename"];
211   if ([filename length] > 0)
212     {
213       response = [self responseWith204];
214       [[self clientObject] deleteAttachmentWithName: filename];
215     }
216   else
217     {
218       response = [self responseWithStatus: 500];
219       [response appendContentString: @"How did you end up here?"];
220     }
221
222   return response;
223 }
224
225 - (WOResponse *) _addLabel: (unsigned int) number
226 {
227   WOResponse *response;
228   SOGoMailObject *co;
229   NSException *error;
230   NSArray *flags;
231
232   co = [self clientObject];
233   flags = [NSArray arrayWithObject:
234                      [NSString stringWithFormat: @"$Label%u", number]];
235   error = [co addFlags: flags];
236   if (error)
237     response = (WOResponse *) error;
238   else
239     response = [self responseWith204];
240
241   return response;
242 }
243
244 - (WOResponse *) _removeLabel: (unsigned int) number
245 {
246   WOResponse *response;
247   SOGoMailObject *co;
248   NSException *error;
249   NSArray *flags;
250
251   co = [self clientObject];
252   flags = [NSArray arrayWithObject:
253                      [NSString stringWithFormat: @"$Label%u", number]];
254   error = [co removeFlags: flags];
255   if (error)
256     response = (WOResponse *) error;
257   else
258     response = [self responseWith204];
259
260   return response;
261 }
262
263 - (WOResponse *) addLabel1Action
264 {
265   return [self _addLabel: 1];
266 }
267
268 - (WOResponse *) addLabel2Action
269 {
270   return [self _addLabel: 2];
271 }
272
273 - (WOResponse *) addLabel3Action
274 {
275   return [self _addLabel: 3];
276 }
277
278 - (WOResponse *) addLabel4Action
279 {
280   return [self _addLabel: 4];
281 }
282
283 - (WOResponse *) addLabel5Action
284 {
285   return [self _addLabel: 5];
286 }
287
288 - (WOResponse *) removeLabel1Action
289 {
290   return [self _removeLabel: 1];
291 }
292
293 - (WOResponse *) removeLabel2Action
294 {
295   return [self _removeLabel: 2];
296 }
297
298 - (WOResponse *) removeLabel3Action
299 {
300   return [self _removeLabel: 3];
301 }
302
303 - (WOResponse *) removeLabel4Action
304 {
305   return [self _removeLabel: 4];
306 }
307
308 - (WOResponse *) removeLabel5Action
309 {
310   return [self _removeLabel: 5];
311 }
312
313 - (WOResponse *) removeAllLabelsAction
314 {
315   WOResponse *response;
316   SOGoMailObject *co;
317   NSException *error;
318   NSArray *flags;
319
320   co = [self clientObject];
321   flags = [NSArray arrayWithObjects: @"$Label1", @"$Label2", @"$Label3",
322                    @"$Label4", @"$Label5", nil];
323   error = [co removeFlags: flags];
324   if (error)
325     response = (WOResponse *) error;
326   else
327     response = [self responseWith204];
328
329   return response;
330 }
331
332 @end