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>
27 #import <Foundation/NSUserDefaults.h>
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>
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>
42 #import <UI/Common/WODirectAction+SOGo.h>
44 #import "UIxMailFolderActions.h"
46 @implementation UIxMailFolderActions
48 - (WOResponse *) createFolderAction
52 NGImap4Connection *connection;
56 co = [self clientObject];
58 folderName = [[context request] formValueForKey: @"name"];
59 if ([folderName length] > 0)
61 connection = [co imap4Connection];
62 error = [connection createMailbox: folderName atURL: [co imap4URL]];
65 response = [self responseWithStatus: 500];
66 [response appendContentString: @"Unable to create folder."];
69 response = [self responseWith204];
73 response = [self responseWithStatus: 500];
74 [response appendContentString: @"Missing 'name' parameter."];
80 - (NSURL *) _urlOfFolder: (NSURL *) srcURL
81 renamedTo: (NSString *) folderName
86 path = [[srcURL path] stringByDeletingLastPathComponent];
88 destURL = [[NSURL alloc] initWithScheme: [srcURL scheme]
90 path: [NSString stringWithFormat: @"%@%@",
92 [destURL autorelease];
97 - (WOResponse *) renameFolderAction
100 WOResponse *response;
101 NGImap4Connection *connection;
103 NSString *folderName;
104 NSURL *srcURL, *destURL;
106 co = [self clientObject];
108 folderName = [[context request] formValueForKey: @"name"];
109 if ([folderName length] > 0)
111 srcURL = [co imap4URL];
112 destURL = [self _urlOfFolder: srcURL renamedTo: folderName];
113 connection = [co imap4Connection];
114 error = [connection moveMailboxAtURL: srcURL
118 response = [self responseWithStatus: 500];
119 [response appendContentString: @"Unable to rename folder."];
122 response = [self responseWith204];
126 response = [self responseWithStatus: 500];
127 [response appendContentString: @"Missing 'name' parameter."];
133 - (NSURL *) _trashedURLOfFolder: (NSURL *) srcURL
134 withCO: (SOGoMailFolder *) co
137 NSString *trashFolderName, *folderName, *path;
139 folderName = [[srcURL path] lastPathComponent];
141 = [[co mailAccountFolder] trashFolderNameInContext: context];
142 path = [NSString stringWithFormat: @"/%@/%@",
143 trashFolderName, folderName];
144 destURL = [[NSURL alloc] initWithScheme: [srcURL scheme]
145 host: [srcURL host] path: path];
146 [destURL autorelease];
151 - (WOResponse *) deleteFolderAction
154 WOResponse *response;
155 NGImap4Connection *connection;
157 NSURL *srcURL, *destURL;
159 co = [self clientObject];
160 connection = [co imap4Connection];
161 srcURL = [co imap4URL];
162 destURL = [self _trashedURLOfFolder: srcURL
164 connection = [co imap4Connection];
165 error = [connection moveMailboxAtURL: srcURL
169 response = [self responseWithStatus: 500];
170 [response appendContentString: @"Unable to move folder."];
173 response = [self responseWith204];
178 - (WOResponse *) _setFolderPurpose: (NSString *) purpose
181 WOResponse *response;
183 NSMutableDictionary *mailSettings;
185 co = [self clientObject];
186 if ([NSStringFromClass ([co class]) isEqualToString: @"SOGoMailFolder"])
188 ud = [[context activeUser] userSettings];
189 mailSettings = [ud objectForKey: @"Mail"];
192 mailSettings = [NSMutableDictionary new];
193 [mailSettings autorelease];
195 [ud setObject: mailSettings forKey: @"Mail"];
196 [mailSettings setObject: [co traversalFromMailAccount]
197 forKey: [NSString stringWithFormat: @"%@Folder",
200 response = [self responseWith204];
204 response = [self responseWithStatus: 500];
206 appendContentString: @"Unable to change the purpose of this folder."];
212 - (WOResponse *) setAsDraftsFolderAction
214 return [self _setFolderPurpose: @"Drafts"];
217 - (WOResponse *) setAsSentFolderAction
219 return [self _setFolderPurpose: @"Sent"];
222 - (WOResponse *) setAsTrashFolderAction
224 return [self _setFolderPurpose: @"Trash"];
227 - (WOResponse *) expungeAction
231 WOResponse *response;
233 co = [self clientObject];
235 error = [co expunge];
238 response = [self responseWithStatus: 500];
239 [response appendContentString: @"Unable to expunge folder."];
243 [co flushMailCaches];
244 response = [self responseWith204];
250 - (WOResponse *) emptyTrashAction
254 NSEnumerator *subfolders;
255 WOResponse *response;
256 NGImap4Connection *connection;
259 co = [self clientObject];
261 error = [co addFlagsToAllMessages: @"deleted"];
263 error = [co expunge];
266 [co flushMailCaches];
267 connection = [co imap4Connection];
268 subfolders = [[co allFolderURLs] objectEnumerator];
269 currentURL = [subfolders nextObject];
272 [connection deleteMailboxAtURL: currentURL];
273 currentURL = [subfolders nextObject];
278 response = [self responseWithStatus: 500];
279 [response appendContentString: @"Unable to empty the trash folder."];
282 response = [self responseWith204];
287 #warning here should be done what should be done: IMAP subscription
288 - (WOResponse *) _subscriptionStubAction
290 NSString *mailInvitationParam, *mailInvitationURL;
291 WOResponse *response;
292 SOGoMailFolder *clientObject;
295 = [[context request] formValueForKey: @"mail-invitation"];
296 if ([mailInvitationParam boolValue])
298 clientObject = [self clientObject];
300 = [[clientObject soURLToBaseContainerForCurrentUser]
302 [response setStatus: 302];
303 [response setHeader: mailInvitationURL
304 forKey: @"location"];
308 response = [self responseWithStatus: 500];
309 [response appendContentString: @"How did you end up here?"];
315 - (WOResponse *) subscribeAction
317 return [self _subscriptionStubAction];
320 - (WOResponse *) unsubscribeAction
322 return [self _subscriptionStubAction];
325 - (WOResponse *) quotasAction
327 SOGoMailFolder *folder;
330 WOResponse *response;
331 NGImap4Client *client;
332 NSString *responseString;
334 response = [self responseWithStatus: 200];
335 [response setHeader: @"text/plain; charset=UTF-8"
336 forKey: @"content-type"];
338 folder = [self clientObject];
339 folderURL = [folder imap4URL];
341 client = [[folder imap4Connection] client];
342 infos = [client getQuotaRoot: [folder relativeImap4Name]];
343 responseString = [[infos objectForKey: @"quotas"] jsonRepresentation];
344 [response appendContentString: responseString];