1 /* UIxMailFolderActions.m - this file is part of SOGo
3 * Copyright (C) 2007 Inverse groupe conseil
5 * Author: Wolfgang Sourdeau <wsourdeau@inverse.ca>
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)
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.
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.
23 #import <Foundation/NSArray.h>
24 #import <Foundation/NSDictionary.h>
25 #import <Foundation/NSEnumerator.h>
26 #import <Foundation/NSURL.h>
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>
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>
39 #import "UIxMailFolderActions.h"
41 @implementation UIxMailFolderActions
43 - (WOResponse *) createFolderAction
47 NGImap4Connection *connection;
51 co = [self clientObject];
52 response = [context response];
54 folderName = [[context request] formValueForKey: @"name"];
55 if ([folderName length] > 0)
57 connection = [co imap4Connection];
58 error = [connection createMailbox: folderName atURL: [co imap4URL]];
61 [response setStatus: 403];
62 [response appendContentString: @"Unable to create folder."];
65 [response setStatus: 204];
69 [response setStatus: 403];
70 [response appendContentString: @"Missing 'name' parameter."];
76 - (NSURL *) _urlOfFolder: (NSURL *) srcURL
77 renamedTo: (NSString *) folderName
82 path = [[srcURL path] stringByDeletingLastPathComponent];
84 destURL = [[NSURL alloc] initWithScheme: [srcURL scheme]
86 path: [NSString stringWithFormat: @"%@%@",
88 [destURL autorelease];
93 - (WOResponse *) renameFolderAction
97 NGImap4Connection *connection;
100 NSURL *srcURL, *destURL;
102 co = [self clientObject];
103 response = [context response];
105 folderName = [[context request] formValueForKey: @"name"];
106 if ([folderName length] > 0)
108 srcURL = [co imap4URL];
109 destURL = [self _urlOfFolder: srcURL renamedTo: folderName];
110 connection = [co imap4Connection];
111 error = [connection moveMailboxAtURL: srcURL
115 [response setStatus: 403];
116 [response appendContentString: @"Unable to rename folder."];
119 [response setStatus: 204];
123 [response setStatus: 403];
124 [response appendContentString: @"Missing 'name' parameter."];
130 - (NSURL *) _trashedURLOfFolder: (NSURL *) srcURL
131 withCO: (SOGoMailFolder *) co
134 NSString *trashFolderName, *folderName;
136 folderName = [[srcURL path] lastPathComponent];
138 = [[co mailAccountFolder] trashFolderNameInContext: context];
140 destURL = [[NSURL alloc] initWithScheme: [srcURL scheme]
142 path: [NSString stringWithFormat: @"/%@/%@",
143 trashFolderName, folderName]];
144 [destURL autorelease];
149 - (WOResponse *) deleteFolderAction
152 WOResponse *response;
153 NGImap4Connection *connection;
155 NSURL *srcURL, *destURL;
157 co = [self clientObject];
158 response = [context response];
159 connection = [co imap4Connection];
160 srcURL = [co imap4URL];
161 destURL = [self _trashedURLOfFolder: srcURL
163 connection = [co imap4Connection];
164 error = [connection moveMailboxAtURL: srcURL
168 [response setStatus: 403];
169 [response appendContentString: @"Unable to move folder."];
172 [response setStatus: 204];
177 - (WOResponse *) emptyTrashAction
181 NSEnumerator *subfolders;
182 WOResponse *response;
183 NGImap4Connection *connection;
186 co = [self clientObject];
187 response = [context response];
189 error = [co addFlagsToAllMessages: @"deleted"];
191 error = [co expunge];
194 [co flushMailCaches];
195 connection = [co imap4Connection];
196 subfolders = [[co subfoldersURL] objectEnumerator];
197 currentURL = [subfolders nextObject];
200 [connection deleteMailboxAtURL: currentURL];
201 currentURL = [subfolders nextObject];
206 [response setStatus: 403];
207 [response appendContentString: @"Unable to empty the trash folder."];
210 [response setStatus: 204];
215 #warning here should be done what should be done: IMAP subscription
216 - (WOResponse *) _subscriptionStubAction
218 NSString *mailInvitationParam, *mailInvitationURL;
219 WOResponse *response;
220 SOGoMailFolder *clientObject;
222 response = [context response];
224 = [[context request] formValueForKey: @"mail-invitation"];
225 if ([mailInvitationParam boolValue])
227 clientObject = [self clientObject];
229 = [[clientObject soURLToBaseContainerForCurrentUser]
231 [response setStatus: 302];
232 [response setHeader: mailInvitationURL
233 forKey: @"location"];
237 [response setStatus: 403];
238 [response appendContentString: @"How did you end up here?"];
244 - (WOResponse *) subscribeAction
246 return [self _subscriptionStubAction];
249 - (WOResponse *) unsubscribeAction
251 return [self _subscriptionStubAction];
254 - (WOResponse *) quotasAction
256 SOGoMailFolder *folder;
259 WOResponse *response;
260 NGImap4Client *client;
261 NSString *responseString;
263 response = [context response];
264 [response setStatus: 200];
265 [response setHeader: @"text/plain; charset=UTF-8"
266 forKey: @"content-type"];
268 folder = [self clientObject];
269 folderURL = [folder imap4URL];
271 client = [[folder imap4Connection] client];
272 infos = [client getQuotaRoot: [folder nameInContainer]];
273 responseString = [[infos objectForKey: @"quotas"] jsonRepresentation];
274 [response appendContentString: responseString];