2007-05-25 23:14:53 +02:00
|
|
|
/* UIxMailAccountActions.m - this file is part of SOGo
|
|
|
|
*
|
2008-09-27 03:13:41 +02:00
|
|
|
* Copyright (C) 2007, 2008 Inverse inc.
|
2007-05-25 23:14:53 +02:00
|
|
|
*
|
|
|
|
* Author: Wolfgang Sourdeau <wsourdeau@inverse.ca>
|
|
|
|
*
|
|
|
|
* This file is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation; either version 2, or (at your option)
|
|
|
|
* any later version.
|
|
|
|
*
|
|
|
|
* This file is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program; see the file COPYING. If not, write to
|
|
|
|
* the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
|
|
|
|
* Boston, MA 02111-1307, USA.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#import <Foundation/NSArray.h>
|
|
|
|
#import <Foundation/NSDictionary.h>
|
|
|
|
#import <Foundation/NSEnumerator.h>
|
2008-09-17 22:27:46 +02:00
|
|
|
#import <Foundation/NSUserDefaults.h>
|
2008-09-18 20:05:27 +02:00
|
|
|
#import <Foundation/NSValue.h>
|
2007-05-25 23:14:53 +02:00
|
|
|
|
2007-11-19 23:44:12 +01:00
|
|
|
#import <NGObjWeb/WOContext+SoObjects.h>
|
2007-08-17 04:30:28 +02:00
|
|
|
#import <NGObjWeb/WORequest.h>
|
2007-05-25 23:14:53 +02:00
|
|
|
#import <NGObjWeb/WOResponse.h>
|
|
|
|
#import <NGImap4/NGImap4Connection.h>
|
2008-09-17 21:44:11 +02:00
|
|
|
#import <NGImap4/NGImap4Client.h>
|
2007-11-19 23:44:12 +01:00
|
|
|
|
2008-10-09 19:53:57 +02:00
|
|
|
#import <EOControl/EOQualifier.h>
|
|
|
|
|
2007-05-25 23:14:53 +02:00
|
|
|
#import <SoObjects/Mailer/SOGoMailAccount.h>
|
2007-08-17 04:30:28 +02:00
|
|
|
#import <SoObjects/Mailer/SOGoDraftObject.h>
|
|
|
|
#import <SoObjects/Mailer/SOGoDraftsFolder.h>
|
2007-11-01 13:48:19 +01:00
|
|
|
#import <SoObjects/SOGo/NSArray+Utilities.h>
|
2007-05-25 23:14:53 +02:00
|
|
|
#import <SoObjects/SOGo/NSObject+Utilities.h>
|
2007-08-17 04:30:28 +02:00
|
|
|
#import <SoObjects/SOGo/NSString+Utilities.h>
|
2007-11-19 23:44:12 +01:00
|
|
|
#import <SoObjects/SOGo/SOGoUser.h>
|
2007-08-17 04:30:28 +02:00
|
|
|
|
|
|
|
#import "../Common/WODirectAction+SOGo.h"
|
2007-05-25 23:14:53 +02:00
|
|
|
|
|
|
|
#import "UIxMailAccountActions.h"
|
|
|
|
|
|
|
|
@implementation UIxMailAccountActions
|
|
|
|
|
2007-11-01 13:48:19 +01:00
|
|
|
- (id) init
|
|
|
|
{
|
|
|
|
if ((self = [super init]))
|
|
|
|
{
|
|
|
|
inboxFolderName = nil;
|
|
|
|
draftsFolderName = nil;
|
|
|
|
sentFolderName = nil;
|
|
|
|
trashFolderName = nil;
|
|
|
|
}
|
|
|
|
|
|
|
|
return self;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void) dealloc
|
|
|
|
{
|
|
|
|
[inboxFolderName release];
|
|
|
|
[draftsFolderName release];
|
|
|
|
[sentFolderName release];
|
|
|
|
[trashFolderName release];
|
|
|
|
[super dealloc];
|
|
|
|
}
|
|
|
|
|
|
|
|
- (NSString *) _folderType: (NSString *) folderName
|
2007-05-25 23:14:53 +02:00
|
|
|
{
|
|
|
|
NSString *folderType;
|
2007-11-01 13:48:19 +01:00
|
|
|
SOGoMailAccount *co;
|
|
|
|
NSArray *specialFolders;
|
2007-05-25 23:14:53 +02:00
|
|
|
|
2007-11-01 13:48:19 +01:00
|
|
|
if (!inboxFolderName)
|
|
|
|
{
|
|
|
|
co = [self clientObject];
|
|
|
|
specialFolders = [[NSArray arrayWithObjects:
|
|
|
|
[co inboxFolderNameInContext: context],
|
|
|
|
[co draftsFolderNameInContext: context],
|
|
|
|
[co sentFolderNameInContext: context],
|
|
|
|
[co trashFolderNameInContext: context],
|
|
|
|
nil] stringsWithFormat: @"/%@"];
|
|
|
|
ASSIGN (inboxFolderName, [specialFolders objectAtIndex: 0]);
|
|
|
|
ASSIGN (draftsFolderName, [specialFolders objectAtIndex: 1]);
|
|
|
|
ASSIGN (sentFolderName, [specialFolders objectAtIndex: 2]);
|
|
|
|
ASSIGN (trashFolderName, [specialFolders objectAtIndex: 3]);
|
|
|
|
}
|
|
|
|
|
|
|
|
if ([folderName isEqualToString: inboxFolderName])
|
2007-05-25 23:14:53 +02:00
|
|
|
folderType = @"inbox";
|
2007-11-01 13:48:19 +01:00
|
|
|
else if ([folderName isEqualToString: draftsFolderName])
|
2007-05-25 23:14:53 +02:00
|
|
|
folderType = @"draft";
|
2007-11-01 13:48:19 +01:00
|
|
|
else if ([folderName isEqualToString: sentFolderName])
|
2007-05-25 23:14:53 +02:00
|
|
|
folderType = @"sent";
|
2007-11-01 13:48:19 +01:00
|
|
|
else if ([folderName isEqualToString: trashFolderName])
|
2007-05-25 23:14:53 +02:00
|
|
|
folderType = @"trash";
|
|
|
|
else
|
|
|
|
folderType = @"folder";
|
|
|
|
|
|
|
|
return folderType;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (NSArray *) _jsonFolders: (NSEnumerator *) rawFolders
|
|
|
|
{
|
|
|
|
NSMutableArray *folders;
|
|
|
|
NSString *currentFolder;
|
2007-11-01 13:48:19 +01:00
|
|
|
NSDictionary *folderData;
|
2007-05-25 23:14:53 +02:00
|
|
|
|
|
|
|
folders = [NSMutableArray array];
|
2007-11-01 13:48:19 +01:00
|
|
|
while ((currentFolder = [rawFolders nextObject]))
|
2007-05-25 23:14:53 +02:00
|
|
|
{
|
2007-11-01 13:48:19 +01:00
|
|
|
folderData = [NSDictionary dictionaryWithObjectsAndKeys:
|
|
|
|
currentFolder, @"path",
|
|
|
|
[self _folderType: currentFolder], @"type",
|
|
|
|
nil];
|
|
|
|
[folders addObject: folderData];
|
2007-05-25 23:14:53 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return folders;
|
|
|
|
}
|
|
|
|
|
2008-10-03 15:54:24 +02:00
|
|
|
- (NSDictionary *) _statusFolders
|
|
|
|
{
|
2008-10-09 19:53:57 +02:00
|
|
|
EOQualifier *searchQualifier;
|
|
|
|
NSArray *searchResult;
|
|
|
|
NSDictionary *imapResult;
|
2008-10-03 15:54:24 +02:00
|
|
|
NGImap4Client *client;
|
|
|
|
NSNumber *unseen;
|
2008-10-09 19:53:57 +02:00
|
|
|
SOGoMailFolder *inbox;
|
|
|
|
SOGoMailAccount *co;
|
2008-10-03 15:54:24 +02:00
|
|
|
|
|
|
|
co = [self clientObject];
|
|
|
|
inbox = [co inboxFolderInContext: context];
|
|
|
|
client = [[inbox imap4Connection] client];
|
2009-03-11 18:59:49 +01:00
|
|
|
unseen = nil;
|
2008-10-03 15:54:24 +02:00
|
|
|
|
2008-10-09 19:53:57 +02:00
|
|
|
if ([client select: [inbox relativeImap4Name]])
|
|
|
|
{
|
|
|
|
searchQualifier = [EOQualifier qualifierWithQualifierFormat: @"flags = %@ AND not flags = %@", @"unseen", @"deleted"];
|
|
|
|
imapResult = [client searchWithQualifier: searchQualifier];
|
|
|
|
searchResult = [[imapResult objectForKey: @"RawResponse"] objectForKey: @"search"];
|
|
|
|
unseen = [NSNumber numberWithInt: [searchResult count]];
|
|
|
|
}
|
|
|
|
|
2008-10-03 15:54:24 +02:00
|
|
|
if (!unseen)
|
|
|
|
unseen = [NSNumber numberWithInt: 0];
|
|
|
|
|
|
|
|
return [NSDictionary dictionaryWithObjectsAndKeys: unseen, @"unseen", nil];
|
|
|
|
}
|
|
|
|
|
|
|
|
- (WOResponse *) statusFoldersAction
|
|
|
|
{
|
|
|
|
WOResponse *response;
|
|
|
|
NSDictionary *data;
|
|
|
|
|
|
|
|
response = [self responseWithStatus: 200];
|
|
|
|
data = [self _statusFolders];
|
|
|
|
|
|
|
|
[response setHeader: @"text/plain; charset=utf-8"
|
|
|
|
forKey: @"content-type"];
|
|
|
|
[response appendContentString: [data jsonRepresentation]];
|
|
|
|
|
|
|
|
return response;
|
|
|
|
}
|
|
|
|
|
2007-05-25 23:14:53 +02:00
|
|
|
- (WOResponse *) listMailboxesAction
|
|
|
|
{
|
2008-09-17 22:27:46 +02:00
|
|
|
id infos, inboxQuota;
|
2007-05-28 21:05:00 +02:00
|
|
|
SOGoMailAccount *co;
|
2008-09-17 21:44:11 +02:00
|
|
|
SOGoMailFolder *inbox;
|
|
|
|
NGImap4Client *client;
|
2007-11-01 13:48:19 +01:00
|
|
|
NSEnumerator *rawFolders;
|
|
|
|
NSArray *folders;
|
2008-09-17 21:44:11 +02:00
|
|
|
NSDictionary *data;
|
|
|
|
NSString *inboxName;
|
2008-09-17 22:27:46 +02:00
|
|
|
NSUserDefaults *ud;
|
2007-05-25 23:14:53 +02:00
|
|
|
WOResponse *response;
|
2008-09-26 23:08:26 +02:00
|
|
|
float quota;
|
2007-05-25 23:14:53 +02:00
|
|
|
|
2008-09-17 22:27:46 +02:00
|
|
|
ud = [NSUserDefaults standardUserDefaults];
|
2007-05-28 21:05:00 +02:00
|
|
|
co = [self clientObject];
|
2007-11-01 13:48:19 +01:00
|
|
|
|
|
|
|
rawFolders = [[co allFolderPaths] objectEnumerator];
|
|
|
|
folders = [self _jsonFolders: rawFolders];
|
2007-05-25 23:14:53 +02:00
|
|
|
|
2008-09-17 21:44:11 +02:00
|
|
|
// Retrieve INBOX quota
|
2008-09-26 23:08:26 +02:00
|
|
|
quota = [ud floatForKey: @"SOGoSoftQuotaRatio"];
|
2008-09-17 21:44:11 +02:00
|
|
|
inbox = [co inboxFolderInContext: context];
|
|
|
|
inboxName = [NSString stringWithFormat: @"/%@", [inbox relativeImap4Name]];
|
|
|
|
client = [[inbox imap4Connection] client];
|
2008-09-17 22:27:46 +02:00
|
|
|
infos = [[client getQuotaRoot: [inbox relativeImap4Name]] objectForKey: @"quotas"];
|
|
|
|
inboxQuota = [infos objectForKey: inboxName];
|
2008-09-26 23:08:26 +02:00
|
|
|
if (quota != 0 && inboxQuota != nil)
|
|
|
|
{
|
|
|
|
// A soft quota ration is imposed for all users
|
|
|
|
quota = quota * [(NSNumber*)[inboxQuota objectForKey: @"maxQuota"] intValue];
|
|
|
|
inboxQuota = [NSDictionary dictionaryWithObjectsAndKeys:
|
|
|
|
[NSNumber numberWithFloat: (long)(quota+0.5)], @"maxQuota",
|
|
|
|
[inboxQuota objectForKey: @"usedSpace"], @"usedSpace",
|
|
|
|
nil];
|
|
|
|
}
|
2008-09-17 21:44:11 +02:00
|
|
|
data = [NSDictionary dictionaryWithObjectsAndKeys: folders, @"mailboxes",
|
2008-10-09 19:53:57 +02:00
|
|
|
[self _statusFolders], @"status",
|
2008-09-17 22:27:46 +02:00
|
|
|
inboxQuota, @"quotas",
|
2008-09-17 21:44:11 +02:00
|
|
|
nil];
|
2007-09-11 21:42:41 +02:00
|
|
|
response = [self responseWithStatus: 200];
|
2007-08-09 04:30:41 +02:00
|
|
|
[response setHeader: @"text/plain; charset=utf-8"
|
|
|
|
forKey: @"content-type"];
|
2008-09-17 21:44:11 +02:00
|
|
|
[response appendContentString: [data jsonRepresentation]];
|
2007-05-25 23:14:53 +02:00
|
|
|
|
|
|
|
return response;
|
|
|
|
}
|
|
|
|
|
2007-08-17 04:30:28 +02:00
|
|
|
/* compose */
|
|
|
|
|
|
|
|
- (WOResponse *) composeAction
|
|
|
|
{
|
|
|
|
SOGoDraftsFolder *drafts;
|
|
|
|
SOGoDraftObject *newDraftMessage;
|
2007-11-19 23:44:12 +01:00
|
|
|
NSString *urlBase, *url, *value, *signature;
|
2007-08-21 16:47:26 +02:00
|
|
|
NSArray *mailTo;
|
2008-08-26 19:16:16 +02:00
|
|
|
NSMutableDictionary *headers;
|
2007-11-19 23:44:12 +01:00
|
|
|
BOOL save;
|
2007-08-17 04:30:28 +02:00
|
|
|
|
|
|
|
drafts = [[self clientObject] draftsFolderInContext: context];
|
|
|
|
newDraftMessage = [drafts newDraft];
|
2008-08-26 19:16:16 +02:00
|
|
|
headers = [NSMutableDictionary dictionary];
|
|
|
|
|
2007-11-19 23:44:12 +01:00
|
|
|
save = NO;
|
|
|
|
|
2007-08-21 16:47:26 +02:00
|
|
|
value = [[self request] formValueForKey: @"mailto"];
|
|
|
|
if ([value length] > 0)
|
2007-08-17 04:30:28 +02:00
|
|
|
{
|
2008-01-08 21:29:51 +01:00
|
|
|
mailTo = [value componentsSeparatedByString: @","];
|
2008-08-26 19:16:16 +02:00
|
|
|
[headers setObject: mailTo forKey: @"to"];
|
2007-11-19 23:44:12 +01:00
|
|
|
save = YES;
|
|
|
|
}
|
|
|
|
|
2008-08-26 19:16:16 +02:00
|
|
|
value = [[self request] formValueForKey: @"subject"];
|
|
|
|
if ([value length] > 0)
|
|
|
|
{
|
|
|
|
[headers setObject: value forKey: @"subject"];
|
|
|
|
save = YES;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (save)
|
|
|
|
[newDraftMessage setHeaders: headers];
|
|
|
|
|
2007-11-19 23:44:12 +01:00
|
|
|
signature = [[context activeUser] signature];
|
|
|
|
if ([signature length])
|
|
|
|
{
|
|
|
|
[newDraftMessage
|
2007-11-25 18:26:56 +01:00
|
|
|
setText: [NSString stringWithFormat: @"\n-- \n%@", signature]];
|
2007-11-19 23:44:12 +01:00
|
|
|
save = YES;
|
2007-08-17 04:30:28 +02:00
|
|
|
}
|
2007-11-19 23:44:12 +01:00
|
|
|
if (save)
|
|
|
|
[newDraftMessage storeInfo];
|
2007-08-17 04:30:28 +02:00
|
|
|
|
|
|
|
urlBase = [newDraftMessage baseURLInContext: context];
|
|
|
|
url = [urlBase composeURLWithAction: @"edit"
|
|
|
|
parameters: nil
|
|
|
|
andHash: NO];
|
|
|
|
|
|
|
|
return [self redirectToLocation: url];
|
|
|
|
}
|
|
|
|
|
2007-05-25 23:14:53 +02:00
|
|
|
@end
|