]> err.no Git - scalable-opengroupware.org/blobdiff - UI/MailerUI/UIxMailFolderActions.m
git-svn-id: http://svn.opengroupware.org/SOGo/inverse/trunk@1193 d1b88da0-ebda-0310...
[scalable-opengroupware.org] / UI / MailerUI / UIxMailFolderActions.m
index 8f971f53ade219fc3af549dfca8fac3c58e5506a..c78fade8b4cb95a61f6522c9d0083261e3fffa7e 100644 (file)
 #import <Foundation/NSDictionary.h>
 #import <Foundation/NSEnumerator.h>
 #import <Foundation/NSURL.h>
+#import <Foundation/NSUserDefaults.h>
 
 #import <NGObjWeb/WOContext.h>
+#import <NGObjWeb/WOContext+SoObjects.h>
 #import <NGObjWeb/WOResponse.h>
 #import <NGObjWeb/WORequest.h>
 #import <NGImap4/NGImap4Connection.h>
+#import <NGImap4/NGImap4Client.h>
 
 #import <SoObjects/Mailer/SOGoMailFolder.h>
 #import <SoObjects/Mailer/SOGoTrashFolder.h>
 #import <SoObjects/Mailer/SOGoMailAccount.h>
+#import <SoObjects/SOGo/NSObject+Utilities.h>
+#import <SoObjects/SOGo/SOGoUser.h>
+
+#import <UI/Common/WODirectAction+SOGo.h>
 
 #import "UIxMailFolderActions.h"
 
@@ -47,7 +54,6 @@
   NSString *folderName;
 
   co = [self clientObject];
-  response = [context response];
 
   folderName = [[context request] formValueForKey: @"name"];
   if ([folderName length] > 0)
       error = [connection createMailbox: folderName atURL: [co imap4URL]];
       if (error)
        {
-         [response setStatus: 403];
+         response = [self responseWithStatus: 500];
          [response appendContentString: @"Unable to create folder."];
        }
       else
-       [response setStatus: 204];
+       response = [self responseWith204];
     }
   else
     {
-      [response setStatus: 403];
+      response = [self responseWithStatus: 500];
       [response appendContentString: @"Missing 'name' parameter."];
     }
 
   NSURL *srcURL, *destURL;
 
   co = [self clientObject];
-  response = [context response];
 
   folderName = [[context request] formValueForKey: @"name"];
   if ([folderName length] > 0)
                          toURL: destURL];
       if (error)
        {
-         [response setStatus: 403];
+         response = [self responseWithStatus: 500];
          [response appendContentString: @"Unable to rename folder."];
        }
       else
-       [response setStatus: 204];
+       response = [self responseWith204];
     }
   else
     {
-      [response setStatus: 403];
+      response = [self responseWithStatus: 500];
       [response appendContentString: @"Missing 'name' parameter."];
     }
 
                         withCO: (SOGoMailFolder *) co
 {
   NSURL *destURL;
-  NSString *trashFolderName, *folderName;
+  NSString *trashFolderName, *folderName, *path;
 
   folderName = [[srcURL path] lastPathComponent];
   trashFolderName
     = [[co mailAccountFolder] trashFolderNameInContext: context];
-
+  path = [NSString stringWithFormat: @"/%@/%@",
+                  [trashFolderName substringFromIndex: 6], folderName];
   destURL = [[NSURL alloc] initWithScheme: [srcURL scheme]
-                          host: [srcURL host]
-                          path: [NSString stringWithFormat: @"/%@/%@",
-                                          trashFolderName, folderName]];
+                          host: [srcURL host] path: path];
   [destURL autorelease];
 
   return destURL;
   NSURL *srcURL, *destURL;
 
   co = [self clientObject];
-  response = [context response];
   connection = [co imap4Connection];
   srcURL = [co imap4URL];
   destURL = [self _trashedURLOfFolder: srcURL
                      toURL: destURL];
   if (error)
     {
-      [response setStatus: 403];
+      response = [self responseWithStatus: 500];
       [response appendContentString: @"Unable to move folder."];
     }
   else
-    [response setStatus: 204];
+    response = [self responseWith204];
+
+  return response;
+}
+
+- (WOResponse *) _setFolderPurpose: (NSString *) purpose
+{
+  SOGoMailFolder *co;
+  WOResponse *response;
+  NSUserDefaults *ud;
+  NSMutableDictionary *mailSettings;
+
+  co = [self clientObject];
+  if ([NSStringFromClass ([co class]) isEqualToString: @"SOGoMailFolder"])
+    {
+      ud = [[context activeUser] userSettings];
+      mailSettings = [ud objectForKey: @"Mail"];
+      if (!mailSettings)
+       {
+         mailSettings = [NSMutableDictionary new];
+         [mailSettings autorelease];
+       }
+      [ud setObject: mailSettings forKey: @"Mail"];
+      [mailSettings setObject: [co relativeImap4Name]
+                   forKey: [NSString stringWithFormat: @"%@Folder",
+                                     purpose]];
+      [ud synchronize];
+      response = [self responseWith204];
+    }
+  else
+    {
+      response = [self responseWithStatus: 500];
+      [response
+       appendContentString: @"Unable to change the purpose of this folder."];
+    }
+
+  return response;
+}
+
+- (WOResponse *) setAsDraftsFolderAction
+{
+  return [self _setFolderPurpose: @"Drafts"];
+}
+
+- (WOResponse *) setAsSentFolderAction
+{
+  return [self _setFolderPurpose: @"Sent"];
+}
+
+- (WOResponse *) setAsTrashFolderAction
+{
+  return [self _setFolderPurpose: @"Trash"];
+}
+
+- (WOResponse *) expungeAction 
+{
+  NSException *error;
+  SOGoTrashFolder *co;
+  WOResponse *response;
+
+  co = [self clientObject];
+
+  error = [co expunge];
+  if (error)
+    {
+      response = [self responseWithStatus: 500];
+      [response appendContentString: @"Unable to expunge folder."];
+    }
+  else
+    {
+      [co flushMailCaches];
+      response = [self responseWith204];
+    }
 
   return response;
 }
   NSURL *currentURL;
 
   co = [self clientObject];
-  response = [context response];
 
   error = [co addFlagsToAllMessages: @"deleted"];
   if (!error)
     {
       [co flushMailCaches];
       connection = [co imap4Connection];
-      subfolders = [[co subfoldersURL] objectEnumerator];
+      subfolders = [[co allFolderURLs] objectEnumerator];
       currentURL = [subfolders nextObject];
       while (currentURL)
        {
     }
   if (error)
     {
-      [response setStatus: 403];
+      response = [self responseWithStatus: 500];
       [response appendContentString: @"Unable to empty the trash folder."];
     }
   else
-    [response setStatus: 204];
+    response = [self responseWith204];
 
   return response;
 }
   WOResponse *response;
   SOGoMailFolder *clientObject;
 
-  response = [context response];
   mailInvitationParam
     = [[context request] formValueForKey: @"mail-invitation"];
   if ([mailInvitationParam boolValue])
     }
   else
     {
-      [response setStatus: 403];
+      response = [self responseWithStatus: 500];
       [response appendContentString: @"How did you end up here?"];
     }
 
   return [self _subscriptionStubAction];
 }
 
+- (WOResponse *) quotasAction
+{
+  SOGoMailFolder *folder;
+  NSURL *folderURL;
+  id infos;
+  WOResponse *response;
+  NGImap4Client *client;
+  NSString *responseString;
+
+  response = [self responseWithStatus: 200];
+  [response setHeader: @"text/plain; charset=UTF-8"
+           forKey: @"content-type"];
+
+  folder = [self clientObject];
+  folderURL = [folder imap4URL];
+  
+  client = [[folder imap4Connection] client];
+  infos = [client getQuotaRoot: [folder relativeImap4Name]];
+  responseString = [[infos objectForKey: @"quotas"] jsonRepresentation];
+  [response appendContentString: responseString];
+
+  return response;
+}
+
 @end