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