]> err.no Git - scalable-opengroupware.org/blob - UI/MailerUI/UIxMailFolderActions.m
git-svn-id: http://svn.opengroupware.org/SOGo/inverse/trunk@1097 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
33 #import <SoObjects/Mailer/SOGoMailFolder.h>
34 #import <SoObjects/Mailer/SOGoTrashFolder.h>
35 #import <SoObjects/Mailer/SOGoMailAccount.h>
36
37 #import "UIxMailFolderActions.h"
38
39 @implementation UIxMailFolderActions
40
41 - (WOResponse *) createFolderAction
42 {
43   SOGoMailFolder *co;
44   WOResponse *response;
45   NGImap4Connection *connection;
46   NSException *error;
47   NSString *folderName;
48
49   co = [self clientObject];
50   response = [context response];
51
52   folderName = [[context request] formValueForKey: @"name"];
53   if ([folderName length] > 0)
54     {
55       connection = [co imap4Connection];
56       error = [connection createMailbox: folderName atURL: [co imap4URL]];
57       if (error)
58         {
59           [response setStatus: 403];
60           [response appendContentString: @"Unable to create folder."];
61         }
62       else
63         [response setStatus: 204];
64     }
65   else
66     {
67       [response setStatus: 403];
68       [response appendContentString: @"Missing 'name' parameter."];
69     }
70
71   return response;  
72 }
73
74 - (NSURL *) _urlOfFolder: (NSURL *) srcURL
75                renamedTo: (NSString *) folderName
76 {
77   NSString *path;
78   NSURL *destURL;
79
80   path = [[srcURL path] stringByDeletingLastPathComponent];
81
82   destURL = [[NSURL alloc] initWithScheme: [srcURL scheme]
83                            host: [srcURL host]
84                            path: [NSString stringWithFormat: @"%@%@",
85                                            path, folderName]];
86   [destURL autorelease];
87
88   return destURL;
89 }
90
91 - (WOResponse *) renameFolderAction
92 {
93   SOGoMailFolder *co;
94   WOResponse *response;
95   NGImap4Connection *connection;
96   NSException *error;
97   NSString *folderName;
98   NSURL *srcURL, *destURL;
99
100   co = [self clientObject];
101   response = [context response];
102
103   folderName = [[context request] formValueForKey: @"name"];
104   if ([folderName length] > 0)
105     {
106       srcURL = [co imap4URL];
107       destURL = [self _urlOfFolder: srcURL renamedTo: folderName];
108       connection = [co imap4Connection];
109       error = [connection moveMailboxAtURL: srcURL
110                           toURL: destURL];
111       if (error)
112         {
113           [response setStatus: 403];
114           [response appendContentString: @"Unable to rename folder."];
115         }
116       else
117         [response setStatus: 204];
118     }
119   else
120     {
121       [response setStatus: 403];
122       [response appendContentString: @"Missing 'name' parameter."];
123     }
124
125   return response;  
126 }
127
128 - (NSURL *) _trashedURLOfFolder: (NSURL *) srcURL
129                          withCO: (SOGoMailFolder *) co
130 {
131   NSURL *destURL;
132   NSString *trashFolderName, *folderName;
133
134   folderName = [[srcURL path] lastPathComponent];
135   trashFolderName
136     = [[co mailAccountFolder] trashFolderNameInContext: context];
137
138   destURL = [[NSURL alloc] initWithScheme: [srcURL scheme]
139                            host: [srcURL host]
140                            path: [NSString stringWithFormat: @"/%@/%@",
141                                            trashFolderName, folderName]];
142   [destURL autorelease];
143
144   return destURL;
145 }
146
147 - (WOResponse *) deleteFolderAction
148 {
149   SOGoMailFolder *co;
150   WOResponse *response;
151   NGImap4Connection *connection;
152   NSException *error;
153   NSURL *srcURL, *destURL;
154
155   co = [self clientObject];
156   response = [context response];
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 setStatus: 403];
167       [response appendContentString: @"Unable to move folder."];
168     }
169   else
170     [response setStatus: 204];
171
172   return response;
173 }
174
175 - (WOResponse *) emptyTrashAction 
176 {
177   NSException *error;
178   SOGoTrashFolder *co;
179   NSEnumerator *subfolders;
180   WOResponse *response;
181   NGImap4Connection *connection;
182   NSURL *currentURL;
183
184   co = [self clientObject];
185   response = [context response];
186
187   error = [co addFlagsToAllMessages: @"deleted"];
188   if (!error)
189     error = [co expunge];
190   if (!error)
191     {
192       [co flushMailCaches];
193       connection = [co imap4Connection];
194       subfolders = [[co subfoldersURL] objectEnumerator];
195       currentURL = [subfolders nextObject];
196       while (currentURL)
197         {
198           [connection deleteMailboxAtURL: currentURL];
199           currentURL = [subfolders nextObject];
200         }
201     }
202   if (error)
203     {
204       [response setStatus: 403];
205       [response appendContentString: @"Unable to empty the trash folder."];
206     }
207   else
208     [response setStatus: 204];
209
210   return response;
211 }
212
213 #warning here should be done what should be done: IMAP subscription
214 - (WOResponse *) _subscriptionStubAction
215 {
216   NSString *mailInvitationParam, *mailInvitationURL;
217   WOResponse *response;
218   SOGoMailFolder *clientObject;
219
220   response = [context response];
221   mailInvitationParam
222     = [[context request] formValueForKey: @"mail-invitation"];
223   if ([mailInvitationParam boolValue])
224     {
225       clientObject = [self clientObject];
226       mailInvitationURL
227         = [[clientObject soURLToBaseContainerForCurrentUser]
228             absoluteString];
229       [response setStatus: 302];
230       [response setHeader: mailInvitationURL
231                 forKey: @"location"];
232     }
233   else
234     {
235       [response setStatus: 403];
236       [response appendContentString: @"How did you end up here?"];
237     }
238
239   return response;
240 }
241
242 - (WOResponse *) subscribeAction
243 {
244   return [self _subscriptionStubAction];
245 }
246
247 - (WOResponse *) unsubscribeAction
248 {
249   return [self _subscriptionStubAction];
250 }
251
252 @end