2007-08-17 04:44:19 +02:00
|
|
|
/* UIxMailActions.m - this file is part of SOGo
|
|
|
|
*
|
2016-02-10 22:15:00 +01:00
|
|
|
* Copyright (C) 2007-2016 Inverse inc.
|
2007-08-17 04:44:19 +02:00
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
*/
|
|
|
|
|
2007-10-22 23:59:20 +02:00
|
|
|
#import <Foundation/NSArray.h>
|
2013-11-11 16:49:58 +01:00
|
|
|
#import <Foundation/NSDictionary.h>
|
2007-08-17 04:44:19 +02:00
|
|
|
#import <Foundation/NSString.h>
|
2015-01-06 18:03:39 +01:00
|
|
|
#import <Foundation/NSValue.h>
|
2007-08-17 04:44:19 +02:00
|
|
|
|
2007-08-18 22:37:18 +02:00
|
|
|
#import <NGObjWeb/WOContext.h>
|
2014-05-23 17:23:02 +02:00
|
|
|
#import <NGObjWeb/WOContext+SoObjects.h>
|
2007-08-18 22:37:18 +02:00
|
|
|
#import <NGObjWeb/WORequest.h>
|
|
|
|
#import <NGObjWeb/WOResponse.h>
|
2007-09-10 17:08:32 +02:00
|
|
|
#import <NGObjWeb/NSException+HTTP.h>
|
|
|
|
|
2016-02-10 22:15:00 +01:00
|
|
|
#import <SoObjects/Mailer/NSString+Mail.h>
|
2007-08-17 04:44:19 +02:00
|
|
|
#import <SoObjects/Mailer/SOGoDraftObject.h>
|
|
|
|
#import <SoObjects/Mailer/SOGoDraftsFolder.h>
|
|
|
|
#import <SoObjects/Mailer/SOGoMailAccount.h>
|
|
|
|
#import <SoObjects/Mailer/SOGoMailObject.h>
|
2016-02-10 22:15:00 +01:00
|
|
|
#import <SoObjects/Mailer/SOGoMailObject+Draft.h>
|
|
|
|
#import <SoObjects/SOGo/NSArray+Utilities.h>
|
2014-12-16 17:47:10 +01:00
|
|
|
#import <SoObjects/SOGo/NSDictionary+Utilities.h>
|
2014-05-23 17:23:02 +02:00
|
|
|
#import <SoObjects/SOGo/NSString+Utilities.h>
|
|
|
|
#import <SoObjects/SOGo/SOGoUser.h>
|
2014-09-08 18:39:55 +02:00
|
|
|
#import <SoObjects/SOGo/SOGoUserSettings.h>
|
2013-11-11 16:49:58 +01:00
|
|
|
#import <SoObjects/SOGo/SOGoUserDefaults.h>
|
|
|
|
|
2007-08-17 04:44:19 +02:00
|
|
|
#import "../Common/WODirectAction+SOGo.h"
|
|
|
|
|
|
|
|
#import "UIxMailActions.h"
|
|
|
|
|
|
|
|
@implementation UIxMailActions
|
|
|
|
|
2007-09-10 17:08:32 +02:00
|
|
|
- (WOResponse *) replyToAll: (BOOL) toAll
|
2007-08-17 04:44:19 +02:00
|
|
|
{
|
|
|
|
SOGoMailAccount *account;
|
|
|
|
SOGoMailObject *co;
|
2015-01-06 04:29:00 +01:00
|
|
|
SOGoDraftsFolder *drafts;
|
2007-08-17 04:44:19 +02:00
|
|
|
SOGoDraftObject *newMail;
|
2015-01-06 04:29:00 +01:00
|
|
|
NSString *accountName, *mailboxName, *messageName;
|
|
|
|
NSDictionary *data;
|
2007-08-17 04:44:19 +02:00
|
|
|
|
|
|
|
co = [self clientObject];
|
|
|
|
account = [co mailAccountFolder];
|
2015-01-06 04:29:00 +01:00
|
|
|
drafts = [account draftsFolderInContext: context];
|
|
|
|
newMail = [drafts newDraft];
|
2007-09-10 17:08:32 +02:00
|
|
|
[newMail fetchMailForReplying: co toAll: toAll];
|
2007-08-17 04:44:19 +02:00
|
|
|
|
2015-01-06 04:29:00 +01:00
|
|
|
accountName = [account nameInContainer];
|
2016-01-18 19:04:28 +01:00
|
|
|
mailboxName = [drafts absoluteImap4Name];
|
|
|
|
mailboxName = [mailboxName substringWithRange: NSMakeRange(1, [mailboxName length] -2)];
|
2015-01-06 04:29:00 +01:00
|
|
|
messageName = [newMail nameInContainer];
|
|
|
|
|
|
|
|
data = [NSDictionary dictionaryWithObjectsAndKeys:
|
|
|
|
accountName, @"accountId",
|
|
|
|
mailboxName, @"mailboxPath",
|
|
|
|
messageName, @"draftId", nil];
|
2007-08-17 04:44:19 +02:00
|
|
|
|
2015-01-06 04:29:00 +01:00
|
|
|
return [self responseWithStatus: 201
|
|
|
|
andString: [data jsonRepresentation]];
|
2007-08-17 04:44:19 +02:00
|
|
|
}
|
|
|
|
|
2007-09-10 17:08:32 +02:00
|
|
|
- (WOResponse *) replyAction
|
|
|
|
{
|
|
|
|
return [self replyToAll: NO];
|
|
|
|
}
|
|
|
|
|
|
|
|
- (WOResponse *) replyToAllAction
|
|
|
|
{
|
2007-10-30 15:16:51 +01:00
|
|
|
return [self replyToAll: YES];
|
2007-09-10 17:08:32 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
- (WOResponse *) forwardAction
|
2007-08-17 04:44:19 +02:00
|
|
|
{
|
|
|
|
SOGoMailAccount *account;
|
|
|
|
SOGoMailObject *co;
|
2015-01-06 18:03:39 +01:00
|
|
|
SOGoDraftsFolder *drafts;
|
2007-08-17 04:44:19 +02:00
|
|
|
SOGoDraftObject *newMail;
|
2014-05-23 17:23:02 +02:00
|
|
|
SOGoUserDefaults *ud;
|
2015-01-06 18:03:39 +01:00
|
|
|
NSString *accountName, *mailboxName, *messageName;
|
|
|
|
NSDictionary *data;
|
2014-05-23 17:23:02 +02:00
|
|
|
BOOL htmlComposition;
|
2007-08-17 04:44:19 +02:00
|
|
|
|
|
|
|
co = [self clientObject];
|
|
|
|
account = [co mailAccountFolder];
|
2015-01-06 18:03:39 +01:00
|
|
|
drafts = [account draftsFolderInContext: context];
|
|
|
|
newMail = [drafts newDraft];
|
2014-05-23 17:23:02 +02:00
|
|
|
ud = [[context activeUser] userDefaults];
|
|
|
|
htmlComposition = [[ud mailComposeMessageType] isEqualToString: @"html"];
|
|
|
|
|
|
|
|
[newMail setIsHTML: htmlComposition];
|
2007-09-10 17:08:32 +02:00
|
|
|
[newMail fetchMailForForwarding: co];
|
2007-08-17 04:44:19 +02:00
|
|
|
|
2015-01-06 18:03:39 +01:00
|
|
|
accountName = [account nameInContainer];
|
2016-01-18 19:04:28 +01:00
|
|
|
mailboxName = [drafts absoluteImap4Name];
|
|
|
|
mailboxName = [mailboxName substringWithRange: NSMakeRange(1, [mailboxName length] -2)];
|
2015-01-06 18:03:39 +01:00
|
|
|
messageName = [newMail nameInContainer];
|
|
|
|
|
|
|
|
data = [NSDictionary dictionaryWithObjectsAndKeys:
|
|
|
|
accountName, @"accountId",
|
|
|
|
mailboxName, @"mailboxPath",
|
|
|
|
messageName, @"draftId",
|
|
|
|
// Message was saved ([SOGoDraftObject fetchMailForForwarding:]) so IMAP ID exists
|
|
|
|
[NSNumber numberWithInt: [newMail IMAP4ID]], @"uid", nil];
|
2007-08-17 04:44:19 +02:00
|
|
|
|
2015-01-06 18:03:39 +01:00
|
|
|
return [self responseWithStatus: 201
|
|
|
|
andString: [data jsonRepresentation]];
|
2007-08-17 04:44:19 +02:00
|
|
|
}
|
|
|
|
|
2016-02-10 22:15:00 +01:00
|
|
|
- (WOResponse *) viewPlainAction
|
|
|
|
{
|
|
|
|
BOOL htmlContent;
|
|
|
|
NSArray *acceptedTypes, *types;
|
|
|
|
NSDictionary *parts;
|
|
|
|
NSMutableArray *keys;
|
|
|
|
NSMutableDictionary *data;
|
|
|
|
NSString *rawPart, *contentKey, *subject, *content;
|
|
|
|
NSUInteger index;
|
|
|
|
SOGoMailObject *co;
|
|
|
|
|
|
|
|
co = [self clientObject];
|
|
|
|
subject = [co decodedSubject];
|
|
|
|
htmlContent = NO;
|
|
|
|
data = [NSMutableDictionary dictionary];
|
|
|
|
|
|
|
|
if (subject)
|
|
|
|
[data setObject: subject
|
|
|
|
forKey: @"subject"];
|
|
|
|
|
|
|
|
// Fetch the text parts of the message body structure
|
|
|
|
acceptedTypes = [NSArray arrayWithObjects: @"text/plain", @"text/html", nil];
|
|
|
|
keys = [NSMutableArray array];
|
|
|
|
[co addRequiredKeysOfStructure: [co bodyStructure]
|
|
|
|
path: @"" toArray: keys acceptedTypes: acceptedTypes
|
|
|
|
withPeek: NO];
|
|
|
|
|
|
|
|
// Use plain part if available, otherwise use the HTML part
|
|
|
|
types = [keys objectsForKey: @"mimeType" notFoundMarker: @""];
|
|
|
|
index = [types indexOfObject: @"text/plain"];
|
|
|
|
if (index == NSNotFound)
|
|
|
|
{
|
|
|
|
index = [types indexOfObject: @"text/html"];
|
|
|
|
htmlContent = YES;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Fetch part and convert HTML if necessary
|
|
|
|
contentKey = [keys objectAtIndex: index];
|
|
|
|
parts = [co fetchPlainTextStrings: [NSArray arrayWithObject: contentKey]];
|
|
|
|
if ([parts count] > 0)
|
|
|
|
{
|
|
|
|
rawPart = [[parts allValues] objectAtIndex: 0];
|
|
|
|
if (htmlContent)
|
|
|
|
content = [rawPart htmlToText];
|
|
|
|
else
|
|
|
|
content = rawPart;
|
|
|
|
if (content)
|
|
|
|
[data setObject: [content stringByTrimmingSpaces]
|
|
|
|
forKey: @"content"];
|
|
|
|
}
|
|
|
|
|
|
|
|
return [self responseWithStatus: 201
|
|
|
|
andString: [data jsonRepresentation]];
|
|
|
|
}
|
|
|
|
|
2007-09-11 21:41:40 +02:00
|
|
|
/* active message */
|
|
|
|
|
2009-08-19 21:47:31 +02:00
|
|
|
- (id) markMessageUnflaggedAction
|
|
|
|
{
|
|
|
|
id response;
|
|
|
|
|
|
|
|
response = [[self clientObject] removeFlags: @"\\Flagged"];
|
|
|
|
if (!response)
|
2011-07-08 19:38:27 +02:00
|
|
|
response = [self responseWith204];
|
|
|
|
|
2009-08-19 21:47:31 +02:00
|
|
|
return response;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (id) markMessageFlaggedAction
|
|
|
|
{
|
|
|
|
id response;
|
|
|
|
|
|
|
|
response = [[self clientObject] addFlags: @"\\Flagged"];
|
|
|
|
if (!response)
|
2011-07-08 19:38:27 +02:00
|
|
|
response = [self responseWith204];
|
|
|
|
|
2009-08-19 21:47:31 +02:00
|
|
|
return response;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2007-09-11 21:41:40 +02:00
|
|
|
- (id) markMessageUnreadAction
|
|
|
|
{
|
|
|
|
id response;
|
2011-07-08 19:38:27 +02:00
|
|
|
|
2007-09-11 21:41:40 +02:00
|
|
|
response = [[self clientObject] removeFlags: @"seen"];
|
|
|
|
if (!response)
|
2011-07-08 19:38:27 +02:00
|
|
|
response = [self responseWith204];
|
|
|
|
|
2007-09-11 21:41:40 +02:00
|
|
|
return response;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (id) markMessageReadAction
|
|
|
|
{
|
|
|
|
id response;
|
|
|
|
|
|
|
|
response = [[self clientObject] addFlags: @"seen"];
|
|
|
|
if (!response)
|
2011-07-08 19:38:27 +02:00
|
|
|
response = [self responseWith204];
|
|
|
|
|
2007-09-11 21:41:40 +02:00
|
|
|
return response;
|
|
|
|
}
|
|
|
|
|
2014-08-14 00:39:33 +02:00
|
|
|
- (void) collapseAction: (BOOL) isCollapsing
|
2014-08-12 23:43:01 +02:00
|
|
|
{
|
2014-09-15 16:31:51 +02:00
|
|
|
WORequest *request;
|
|
|
|
NSMutableDictionary *moduleSettings, *threadsCollapsed, *content;
|
2014-08-12 23:43:01 +02:00
|
|
|
NSMutableArray *mailboxThreadsCollapsed;
|
2014-09-15 16:31:51 +02:00
|
|
|
NSString *msguid, *keyForMsgUIDs;
|
2014-08-12 23:43:01 +02:00
|
|
|
SOGoUserSettings *us;
|
2014-08-28 23:55:35 +02:00
|
|
|
|
2014-09-15 16:31:51 +02:00
|
|
|
request = [context request];
|
|
|
|
content = [[request contentAsString] objectFromJSONString];
|
|
|
|
keyForMsgUIDs = [content objectForKey:@"currentMailbox"];
|
|
|
|
msguid = [content objectForKey:@"msguid"];
|
|
|
|
|
2014-08-12 23:43:01 +02:00
|
|
|
us = [[context activeUser] userSettings];
|
2014-09-09 19:50:42 +02:00
|
|
|
if (!(moduleSettings = [us objectForKey: @"Mail"]))
|
2015-04-26 13:33:03 +02:00
|
|
|
[us setObject:[NSMutableDictionary dictionary] forKey: @"Mail"];
|
2014-09-15 16:31:51 +02:00
|
|
|
|
2014-08-14 00:39:33 +02:00
|
|
|
if (isCollapsing)
|
2014-08-12 23:43:01 +02:00
|
|
|
{
|
2014-08-14 00:39:33 +02:00
|
|
|
// Check if the module threadsCollapsed is created in the userSettings
|
2014-09-08 18:39:55 +02:00
|
|
|
if ((threadsCollapsed = [moduleSettings objectForKey:@"threadsCollapsed"]))
|
2014-08-12 23:43:01 +02:00
|
|
|
{
|
2014-08-14 00:39:33 +02:00
|
|
|
// Check if the currentMailbox already have other threads saved and add the new collapsed thread
|
2014-09-08 18:39:55 +02:00
|
|
|
if ((mailboxThreadsCollapsed = [threadsCollapsed objectForKey:keyForMsgUIDs]))
|
2014-08-28 23:55:35 +02:00
|
|
|
{
|
|
|
|
if (![mailboxThreadsCollapsed containsObject:msguid])
|
|
|
|
[mailboxThreadsCollapsed addObject:msguid];
|
|
|
|
}
|
2014-08-14 00:39:33 +02:00
|
|
|
else
|
|
|
|
{
|
|
|
|
mailboxThreadsCollapsed = [NSMutableArray arrayWithObject:msguid];
|
|
|
|
[threadsCollapsed setObject:mailboxThreadsCollapsed forKey:keyForMsgUIDs];
|
|
|
|
}
|
2014-08-12 23:43:01 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2014-08-14 00:39:33 +02:00
|
|
|
// Created the module threadsCollapsed and add the new collapsed thread
|
2014-08-12 23:43:01 +02:00
|
|
|
mailboxThreadsCollapsed = [NSMutableArray arrayWithObject:msguid];
|
2014-08-14 00:39:33 +02:00
|
|
|
threadsCollapsed = [NSMutableDictionary dictionaryWithObject:mailboxThreadsCollapsed forKey:keyForMsgUIDs];
|
|
|
|
[moduleSettings setObject:threadsCollapsed forKey: @"threadsCollapsed"];
|
2014-08-12 23:43:01 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2014-08-14 00:39:33 +02:00
|
|
|
// Check if the module threadsCollapsed is created in the userSettings
|
2014-09-08 18:39:55 +02:00
|
|
|
if ((threadsCollapsed = [moduleSettings objectForKey:@"threadsCollapsed"]))
|
2014-08-14 00:39:33 +02:00
|
|
|
{
|
|
|
|
// Check if the currentMailbox already have other threads saved and remove the uncollapsed thread
|
2014-09-08 18:39:55 +02:00
|
|
|
if ((mailboxThreadsCollapsed = [threadsCollapsed objectForKey:keyForMsgUIDs]))
|
2014-08-14 00:39:33 +02:00
|
|
|
{
|
|
|
|
[mailboxThreadsCollapsed removeObject:msguid];
|
|
|
|
if ([mailboxThreadsCollapsed count] == 0)
|
|
|
|
[threadsCollapsed removeObjectForKey:keyForMsgUIDs];
|
|
|
|
}
|
|
|
|
}
|
2014-09-09 19:50:42 +02:00
|
|
|
// TODO : Manage errors
|
2014-08-12 23:43:01 +02:00
|
|
|
}
|
|
|
|
[us synchronize];
|
2014-08-14 00:39:33 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
- (id) markMessageCollapseAction
|
|
|
|
{
|
|
|
|
[self collapseAction: YES];
|
2014-08-12 23:43:01 +02:00
|
|
|
|
|
|
|
return [self responseWith204];
|
|
|
|
}
|
|
|
|
|
|
|
|
- (id) markMessageUncollapseAction
|
|
|
|
{
|
2014-08-14 00:39:33 +02:00
|
|
|
[self collapseAction: NO];
|
2014-08-12 23:43:01 +02:00
|
|
|
|
|
|
|
return [self responseWith204];
|
|
|
|
}
|
|
|
|
|
2007-09-10 17:08:32 +02:00
|
|
|
/* SOGoDraftObject */
|
2014-12-16 17:47:10 +01:00
|
|
|
- (id <WOActionResults>) editAction
|
2007-08-17 04:44:19 +02:00
|
|
|
{
|
2014-12-16 17:47:10 +01:00
|
|
|
id <WOActionResults> response;
|
2007-08-17 04:44:19 +02:00
|
|
|
SOGoMailAccount *account;
|
|
|
|
SOGoMailObject *co;
|
|
|
|
SOGoDraftsFolder *folder;
|
|
|
|
SOGoDraftObject *newMail;
|
2014-12-16 17:47:10 +01:00
|
|
|
NSDictionary *data;
|
2007-08-17 04:44:19 +02:00
|
|
|
|
|
|
|
co = [self clientObject];
|
|
|
|
account = [co mailAccountFolder];
|
|
|
|
folder = [account draftsFolderInContext: context];
|
|
|
|
newMail = [folder newDraft];
|
2007-09-10 17:08:32 +02:00
|
|
|
[newMail fetchMailForEditing: co];
|
|
|
|
[newMail storeInfo];
|
2007-08-17 04:44:19 +02:00
|
|
|
|
2014-12-16 17:47:10 +01:00
|
|
|
data = [NSDictionary dictionaryWithObject: [newMail nameInContainer]
|
|
|
|
forKey: @"draftId"];
|
2007-08-17 04:44:19 +02:00
|
|
|
|
2014-12-16 17:47:10 +01:00
|
|
|
response = [self responseWithStatus: 200
|
|
|
|
andString: [data jsonRepresentation]];
|
|
|
|
|
|
|
|
return response;
|
2007-08-17 04:44:19 +02:00
|
|
|
}
|
|
|
|
|
2007-08-18 22:37:18 +02:00
|
|
|
- (id) deleteAction
|
|
|
|
{
|
|
|
|
SOGoDraftObject *draft;
|
|
|
|
NSException *error;
|
|
|
|
id response;
|
|
|
|
|
|
|
|
draft = [self clientObject];
|
|
|
|
error = [draft delete];
|
|
|
|
if (error)
|
|
|
|
response = error;
|
|
|
|
else
|
2007-09-11 21:41:40 +02:00
|
|
|
response = [self responseWith204];
|
2007-08-18 22:37:18 +02:00
|
|
|
|
|
|
|
return response;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (WOResponse *) deleteAttachmentAction
|
|
|
|
{
|
|
|
|
WOResponse *response;
|
|
|
|
NSString *filename;
|
|
|
|
|
|
|
|
filename = [[context request] formValueForKey: @"filename"];
|
|
|
|
if ([filename length] > 0)
|
|
|
|
{
|
2007-09-11 21:41:40 +02:00
|
|
|
response = [self responseWith204];
|
2007-08-18 22:37:18 +02:00
|
|
|
[[self clientObject] deleteAttachmentWithName: filename];
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2007-09-11 21:41:40 +02:00
|
|
|
response = [self responseWithStatus: 500];
|
2007-08-18 22:37:18 +02:00
|
|
|
[response appendContentString: @"How did you end up here?"];
|
|
|
|
}
|
|
|
|
|
|
|
|
return response;
|
|
|
|
}
|
|
|
|
|
2007-08-17 04:44:19 +02:00
|
|
|
@end
|