]> err.no Git - scalable-opengroupware.org/blob - UI/MailerUI/UIxMailFolderActions.m
git-svn-id: http://svn.opengroupware.org/SOGo/inverse/trunk@1206 d1b88da0-ebda-0310...
[scalable-opengroupware.org] / UI / MailerUI / UIxMailFolderActions.m
1 /* UIxMailFolderActions.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/NSDictionary.h>
25 #import <Foundation/NSEnumerator.h>
26 #import <Foundation/NSURL.h>
27 #import <Foundation/NSUserDefaults.h>
28
29 #import <NGObjWeb/WOContext.h>
30 #import <NGObjWeb/WOContext+SoObjects.h>
31 #import <NGObjWeb/WOResponse.h>
32 #import <NGObjWeb/WORequest.h>
33 #import <NGImap4/NGImap4Connection.h>
34 #import <NGImap4/NGImap4Client.h>
35
36 #import <SoObjects/Mailer/SOGoMailFolder.h>
37 #import <SoObjects/Mailer/SOGoTrashFolder.h>
38 #import <SoObjects/Mailer/SOGoMailAccount.h>
39 #import <SoObjects/SOGo/NSObject+Utilities.h>
40 #import <SoObjects/SOGo/SOGoUser.h>
41
42 #import <UI/Common/WODirectAction+SOGo.h>
43
44 #import "UIxMailFolderActions.h"
45
46 @implementation UIxMailFolderActions
47
48 - (WOResponse *) createFolderAction
49 {
50   SOGoMailFolder *co;
51   WOResponse *response;
52   NGImap4Connection *connection;
53   NSException *error;
54   NSString *folderName;
55
56   co = [self clientObject];
57
58   folderName = [[context request] formValueForKey: @"name"];
59   if ([folderName length] > 0)
60     {
61       connection = [co imap4Connection];
62       error = [connection createMailbox: folderName atURL: [co imap4URL]];
63       if (error)
64         {
65           response = [self responseWithStatus: 500];
66           [response appendContentString: @"Unable to create folder."];
67         }
68       else
69         response = [self responseWith204];
70     }
71   else
72     {
73       response = [self responseWithStatus: 500];
74       [response appendContentString: @"Missing 'name' parameter."];
75     }
76
77   return response;  
78 }
79
80 - (NSURL *) _urlOfFolder: (NSURL *) srcURL
81                renamedTo: (NSString *) folderName
82 {
83   NSString *path;
84   NSURL *destURL;
85
86   path = [[srcURL path] stringByDeletingLastPathComponent];
87
88   destURL = [[NSURL alloc] initWithScheme: [srcURL scheme]
89                            host: [srcURL host]
90                            path: [NSString stringWithFormat: @"%@%@",
91                                            path, folderName]];
92   [destURL autorelease];
93
94   return destURL;
95 }
96
97 - (WOResponse *) renameFolderAction
98 {
99   SOGoMailFolder *co;
100   WOResponse *response;
101   NGImap4Connection *connection;
102   NSException *error;
103   NSString *folderName;
104   NSURL *srcURL, *destURL;
105
106   co = [self clientObject];
107
108   folderName = [[context request] formValueForKey: @"name"];
109   if ([folderName length] > 0)
110     {
111       srcURL = [co imap4URL];
112       destURL = [self _urlOfFolder: srcURL renamedTo: folderName];
113       connection = [co imap4Connection];
114       error = [connection moveMailboxAtURL: srcURL
115                           toURL: destURL];
116       if (error)
117         {
118           response = [self responseWithStatus: 500];
119           [response appendContentString: @"Unable to rename folder."];
120         }
121       else
122         response = [self responseWith204];
123     }
124   else
125     {
126       response = [self responseWithStatus: 500];
127       [response appendContentString: @"Missing 'name' parameter."];
128     }
129
130   return response;  
131 }
132
133 - (NSURL *) _trashedURLOfFolder: (NSURL *) srcURL
134                          withCO: (SOGoMailFolder *) co
135 {
136   NSURL *destURL;
137   NSString *trashFolderName, *folderName, *path;
138
139   folderName = [[srcURL path] lastPathComponent];
140   trashFolderName
141     = [[co mailAccountFolder] trashFolderNameInContext: context];
142   path = [NSString stringWithFormat: @"/%@/%@",
143                    [trashFolderName substringFromIndex: 6], folderName];
144   destURL = [[NSURL alloc] initWithScheme: [srcURL scheme]
145                            host: [srcURL host] path: path];
146   [destURL autorelease];
147
148   return destURL;
149 }
150
151 - (WOResponse *) deleteFolderAction
152 {
153   SOGoMailFolder *co;
154   WOResponse *response;
155   NGImap4Connection *connection;
156   NSException *error;
157   NSURL *srcURL, *destURL;
158
159   co = [self clientObject];
160   connection = [co imap4Connection];
161   srcURL = [co imap4URL];
162   destURL = [self _trashedURLOfFolder: srcURL
163                   withCO: co];
164   connection = [co imap4Connection];
165   error = [connection moveMailboxAtURL: srcURL
166                       toURL: destURL];
167   if (error)
168     {
169       response = [self responseWithStatus: 500];
170       [response appendContentString: @"Unable to move folder."];
171     }
172   else
173     response = [self responseWith204];
174
175   return response;
176 }
177
178 - (WOResponse *) _setFolderPurpose: (NSString *) purpose
179 {
180   SOGoMailFolder *co;
181   WOResponse *response;
182   NSUserDefaults *ud;
183   NSMutableDictionary *mailSettings;
184
185   co = [self clientObject];
186   if ([NSStringFromClass ([co class]) isEqualToString: @"SOGoMailFolder"])
187     {
188       ud = [[context activeUser] userSettings];
189       mailSettings = [ud objectForKey: @"Mail"];
190       if (!mailSettings)
191         {
192           mailSettings = [NSMutableDictionary new];
193           [mailSettings autorelease];
194         }
195       [ud setObject: mailSettings forKey: @"Mail"];
196       [mailSettings setObject: [co relativeImap4Name]
197                     forKey: [NSString stringWithFormat: @"%@Folder",
198                                       purpose]];
199       [ud synchronize];
200       response = [self responseWith204];
201     }
202   else
203     {
204       response = [self responseWithStatus: 500];
205       [response
206         appendContentString: @"Unable to change the purpose of this folder."];
207     }
208
209   return response;
210 }
211
212 - (WOResponse *) setAsDraftsFolderAction
213 {
214   return [self _setFolderPurpose: @"Drafts"];
215 }
216
217 - (WOResponse *) setAsSentFolderAction
218 {
219   return [self _setFolderPurpose: @"Sent"];
220 }
221
222 - (WOResponse *) setAsTrashFolderAction
223 {
224   return [self _setFolderPurpose: @"Trash"];
225 }
226
227 - (WOResponse *) expungeAction 
228 {
229   NSException *error;
230   SOGoTrashFolder *co;
231   WOResponse *response;
232
233   co = [self clientObject];
234
235   error = [co expunge];
236   if (error)
237     {
238       response = [self responseWithStatus: 500];
239       [response appendContentString: @"Unable to expunge folder."];
240     }
241   else
242     {
243       [co flushMailCaches];
244       response = [self responseWith204];
245     }
246
247   return response;
248 }
249
250 - (WOResponse *) emptyTrashAction 
251 {
252   NSException *error;
253   SOGoTrashFolder *co;
254   NSEnumerator *subfolders;
255   WOResponse *response;
256   NGImap4Connection *connection;
257   NSURL *currentURL;
258
259   co = [self clientObject];
260
261   error = [co addFlagsToAllMessages: @"deleted"];
262   if (!error)
263     error = [co expunge];
264   if (!error)
265     {
266       [co flushMailCaches];
267       connection = [co imap4Connection];
268       subfolders = [[co allFolderURLs] objectEnumerator];
269       currentURL = [subfolders nextObject];
270       while (currentURL)
271         {
272           [connection deleteMailboxAtURL: currentURL];
273           currentURL = [subfolders nextObject];
274         }
275     }
276   if (error)
277     {
278       response = [self responseWithStatus: 500];
279       [response appendContentString: @"Unable to empty the trash folder."];
280     }
281   else
282     response = [self responseWith204];
283
284   return response;
285 }
286
287 #warning here should be done what should be done: IMAP subscription
288 - (WOResponse *) _subscriptionStubAction
289 {
290   NSString *mailInvitationParam, *mailInvitationURL;
291   WOResponse *response;
292   SOGoMailFolder *clientObject;
293
294   mailInvitationParam
295     = [[context request] formValueForKey: @"mail-invitation"];
296   if ([mailInvitationParam boolValue])
297     {
298       clientObject = [self clientObject];
299       mailInvitationURL
300         = [[clientObject soURLToBaseContainerForCurrentUser]
301             absoluteString];
302       [response setStatus: 302];
303       [response setHeader: mailInvitationURL
304                 forKey: @"location"];
305     }
306   else
307     {
308       response = [self responseWithStatus: 500];
309       [response appendContentString: @"How did you end up here?"];
310     }
311
312   return response;
313 }
314
315 - (WOResponse *) subscribeAction
316 {
317   return [self _subscriptionStubAction];
318 }
319
320 - (WOResponse *) unsubscribeAction
321 {
322   return [self _subscriptionStubAction];
323 }
324
325 - (WOResponse *) quotasAction
326 {
327   SOGoMailFolder *folder;
328   NSURL *folderURL;
329   id infos;
330   WOResponse *response;
331   NGImap4Client *client;
332   NSString *responseString;
333
334   response = [self responseWithStatus: 200];
335   [response setHeader: @"text/plain; charset=UTF-8"
336             forKey: @"content-type"];
337
338   folder = [self clientObject];
339   folderURL = [folder imap4URL];
340   
341   client = [[folder imap4Connection] client];
342   infos = [client getQuotaRoot: [folder relativeImap4Name]];
343   responseString = [[infos objectForKey: @"quotas"] jsonRepresentation];
344   [response appendContentString: responseString];
345
346   return response;
347 }
348
349 @end