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