2011-02-25 17:41:53 +01:00
|
|
|
/* MAPIStoreMailContext.m - this file is part of SOGo
|
|
|
|
*
|
2012-08-17 21:04:57 +02:00
|
|
|
* Copyright (C) 2010-2012 Inverse inc.
|
2011-02-25 17:41:53 +01: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 3, 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.
|
|
|
|
*/
|
|
|
|
|
2012-01-31 13:54:36 +01:00
|
|
|
#import <Foundation/NSArray.h>
|
2012-01-30 22:42:03 +01:00
|
|
|
#import <Foundation/NSDictionary.h>
|
2011-02-25 17:41:53 +01:00
|
|
|
#import <Foundation/NSString.h>
|
2012-08-15 20:59:42 +02:00
|
|
|
#import <Foundation/NSURL.h>
|
2013-09-25 16:41:07 +02:00
|
|
|
#import <NGImap4/NSString+Imap4.h>
|
2012-08-15 20:59:42 +02:00
|
|
|
#import <NGExtensions/NSString+misc.h>
|
2012-01-29 20:55:21 +01:00
|
|
|
#import <Mailer/SOGoMailAccount.h>
|
|
|
|
#import <Mailer/SOGoMailFolder.h>
|
|
|
|
|
2011-02-25 17:41:53 +01:00
|
|
|
#import "MAPIStoreMailFolder.h"
|
2012-01-29 20:55:21 +01:00
|
|
|
#import "MAPIStoreUserContext.h"
|
2012-01-26 20:54:36 +01:00
|
|
|
#import "NSString+MAPIStore.h"
|
2011-02-25 17:41:53 +01:00
|
|
|
|
2012-02-03 16:05:55 +01:00
|
|
|
#import <SOGo/NSString+Utilities.h>
|
2012-03-16 21:57:35 +01:00
|
|
|
#import "MAPIApplication.h"
|
2011-02-25 17:41:53 +01:00
|
|
|
#import "MAPIStoreMailContext.h"
|
|
|
|
|
2012-01-29 20:55:21 +01:00
|
|
|
#include <dlinklist.h>
|
2012-01-26 20:54:36 +01:00
|
|
|
#undef DEBUG
|
|
|
|
#include <mapistore/mapistore.h>
|
|
|
|
|
2012-02-20 20:47:58 +01:00
|
|
|
static Class MAPIStoreMailFolderK, MAPIStoreOutboxFolderK;
|
2011-02-25 17:41:53 +01:00
|
|
|
|
2012-01-29 20:55:21 +01:00
|
|
|
@implementation MAPIStoreMailContext
|
2011-02-25 17:41:53 +01:00
|
|
|
|
2012-01-29 20:55:21 +01:00
|
|
|
+ (void) initialize
|
2012-01-26 20:54:36 +01:00
|
|
|
{
|
2012-01-29 20:55:21 +01:00
|
|
|
MAPIStoreMailFolderK = [MAPIStoreMailFolder class];
|
2012-02-20 20:47:58 +01:00
|
|
|
MAPIStoreOutboxFolderK = [MAPIStoreOutboxFolder class];
|
2012-01-26 20:54:36 +01:00
|
|
|
}
|
|
|
|
|
2011-02-25 17:41:53 +01:00
|
|
|
+ (NSString *) MAPIModuleName
|
|
|
|
{
|
2012-01-29 20:55:21 +01:00
|
|
|
return @"mail";
|
2011-02-25 17:41:53 +01:00
|
|
|
}
|
|
|
|
|
2012-02-03 16:05:55 +01:00
|
|
|
+ (enum mapistore_context_role) MAPIContextRole
|
|
|
|
{
|
|
|
|
return MAPISTORE_MAIL_ROLE;
|
|
|
|
}
|
|
|
|
|
2012-02-23 22:16:22 +01:00
|
|
|
static inline NSString *
|
|
|
|
MakeDisplayFolderName (NSString *folderName)
|
|
|
|
{
|
|
|
|
NSArray *parts;
|
|
|
|
NSString *lastFolder;
|
|
|
|
NSUInteger max;
|
2015-07-14 16:31:28 +02:00
|
|
|
|
2012-02-23 22:16:22 +01:00
|
|
|
parts = [folderName componentsSeparatedByString: @"/"];
|
|
|
|
max = [parts count];
|
|
|
|
if (max > 1)
|
|
|
|
{
|
|
|
|
lastFolder = [parts objectAtIndex: max - 1];
|
|
|
|
if ([lastFolder length] == 0)
|
|
|
|
lastFolder = [parts objectAtIndex: max - 2];
|
|
|
|
}
|
|
|
|
else
|
|
|
|
lastFolder = folderName;
|
|
|
|
|
|
|
|
return [[lastFolder substringFromIndex: 6] fromCSSIdentifier];
|
|
|
|
}
|
|
|
|
|
2012-01-26 20:54:36 +01:00
|
|
|
+ (struct mapistore_contexts_list *) listContextsForUser: (NSString *) userName
|
2013-11-08 17:02:50 +01:00
|
|
|
withIndexing: (struct indexing_context *) indexing
|
2012-01-26 20:54:36 +01:00
|
|
|
inMemCtx: (TALLOC_CTX *) memCtx
|
|
|
|
{
|
2012-01-31 13:54:36 +01:00
|
|
|
struct mapistore_contexts_list *firstContext = NULL, *context;
|
2014-07-29 18:51:37 +02:00
|
|
|
NSString *urlBase, *stringData, *currentName, *realName, *inboxName, *draftsName, *sentName, *trashName;
|
2012-02-23 22:16:22 +01:00
|
|
|
NSArray *unprefixedFolders;
|
2012-01-31 13:54:36 +01:00
|
|
|
NSMutableArray *secondaryFolders;
|
2012-01-29 20:55:21 +01:00
|
|
|
enum mapistore_context_role role[] = {MAPISTORE_MAIL_ROLE,
|
|
|
|
MAPISTORE_DRAFTS_ROLE,
|
2012-01-31 13:54:36 +01:00
|
|
|
MAPISTORE_SENTITEMS_ROLE};
|
|
|
|
NSString *folderName[3];
|
|
|
|
NSUInteger count, max;
|
2012-01-29 20:55:21 +01:00
|
|
|
SOGoMailAccount *accountFolder;
|
|
|
|
MAPIStoreUserContext *userContext;
|
|
|
|
WOContext *woContext;
|
2012-01-26 20:54:36 +01:00
|
|
|
|
2012-01-29 20:55:21 +01:00
|
|
|
userContext = [MAPIStoreUserContext userContextWithUsername: userName
|
2013-11-08 17:02:50 +01:00
|
|
|
andTDBIndexing: indexing];
|
2012-01-30 22:42:03 +01:00
|
|
|
accountFolder = [[userContext rootFolders] objectForKey: @"mail"];
|
2012-01-29 20:55:21 +01:00
|
|
|
woContext = [userContext woContext];
|
2012-01-31 13:54:36 +01:00
|
|
|
|
|
|
|
inboxName = @"folderINBOX";
|
|
|
|
folderName[0] = inboxName;
|
|
|
|
|
2012-02-23 22:16:22 +01:00
|
|
|
unprefixedFolders = [[accountFolder draftsFolderNameInContext: woContext]
|
|
|
|
componentsSeparatedByString: @"/"];
|
2012-01-31 13:54:36 +01:00
|
|
|
draftsName = [NSString stringWithFormat: @"folder%@",
|
2012-02-23 22:16:22 +01:00
|
|
|
[unprefixedFolders componentsJoinedByString: @"/folder"]];
|
2012-01-31 13:54:36 +01:00
|
|
|
folderName[1] = draftsName;
|
2012-02-23 22:16:22 +01:00
|
|
|
|
|
|
|
unprefixedFolders = [[accountFolder sentFolderNameInContext: woContext]
|
|
|
|
componentsSeparatedByString: @"/"];
|
2012-01-31 13:54:36 +01:00
|
|
|
sentName = [NSString stringWithFormat: @"folder%@",
|
2012-02-23 22:16:22 +01:00
|
|
|
[unprefixedFolders componentsJoinedByString: @"/folder"]];
|
2012-01-31 13:54:36 +01:00
|
|
|
folderName[2] = sentName;
|
2011-02-25 17:41:53 +01:00
|
|
|
|
2012-02-24 00:34:26 +01:00
|
|
|
/* Note: trash is not used as a mail folder, since "Deleted Items" makes use of
|
|
|
|
the fallback context */
|
|
|
|
unprefixedFolders = [[accountFolder trashFolderNameInContext: woContext]
|
|
|
|
componentsSeparatedByString: @"/"];
|
|
|
|
trashName = [NSString stringWithFormat: @"folder%@",
|
|
|
|
[unprefixedFolders componentsJoinedByString: @"/folder"]];
|
|
|
|
|
2015-01-19 19:04:06 +01:00
|
|
|
urlBase = [NSString stringWithFormat: @"sogo://%@:%@@mail/",
|
|
|
|
[userName stringByReplacingOccurrencesOfString: @"@"
|
|
|
|
withString: @"%40"],
|
|
|
|
[userName stringByReplacingOccurrencesOfString: @"@"
|
|
|
|
withString: @"%40"]];
|
2012-01-31 13:54:36 +01:00
|
|
|
for (count = 0; count < 3; count++)
|
2012-01-29 20:55:21 +01:00
|
|
|
{
|
|
|
|
context = talloc_zero (memCtx, struct mapistore_contexts_list);
|
2012-09-07 18:46:18 +02:00
|
|
|
stringData = [NSString stringWithFormat: @"%@%@", urlBase,
|
|
|
|
[folderName[count] stringByAddingPercentEscapesUsingEncoding: NSUTF8StringEncoding]];
|
2012-01-29 20:55:21 +01:00
|
|
|
context->url = [stringData asUnicodeInMemCtx: context];
|
|
|
|
/* remove "folder" prefix */
|
2012-02-23 22:16:22 +01:00
|
|
|
stringData = MakeDisplayFolderName (folderName[count]);
|
2012-01-29 20:55:21 +01:00
|
|
|
context->name = [stringData asUnicodeInMemCtx: context];
|
|
|
|
context->main_folder = true;
|
|
|
|
context->role = role[count];
|
|
|
|
context->tag = "tag";
|
|
|
|
DLIST_ADD_END (firstContext, context, void);
|
|
|
|
}
|
2011-02-25 17:41:53 +01:00
|
|
|
|
2015-05-05 10:46:53 +02:00
|
|
|
/* FIXME: Flush any cache before retrieving the hierarchy */
|
|
|
|
[accountFolder flushMailCaches];
|
|
|
|
|
2012-01-31 13:54:36 +01:00
|
|
|
secondaryFolders = [[accountFolder toManyRelationshipKeysWithNamespaces: NO]
|
|
|
|
mutableCopy];
|
|
|
|
[secondaryFolders autorelease];
|
|
|
|
[secondaryFolders removeObject: inboxName];
|
|
|
|
[secondaryFolders removeObject: draftsName];
|
|
|
|
[secondaryFolders removeObject: sentName];
|
|
|
|
[secondaryFolders removeObject: trashName];
|
|
|
|
max = [secondaryFolders count];
|
|
|
|
for (count = 0; count < max; count++)
|
|
|
|
{
|
|
|
|
context = talloc_zero (memCtx, struct mapistore_contexts_list);
|
2014-07-29 18:51:37 +02:00
|
|
|
// secondaryFolders has the names (1) Imap4Encoded and (2) asCSSIdentifier
|
|
|
|
// e.g.: Probl&AOg-mes_SP_de_SP_synchronisation
|
2012-01-31 13:54:36 +01:00
|
|
|
currentName = [secondaryFolders objectAtIndex: count];
|
2014-07-29 18:51:37 +02:00
|
|
|
// To get the real name we have to revert that (applying the decode functions)
|
|
|
|
// in reverse order
|
|
|
|
// e.g.: Problèmes de synchronisation
|
|
|
|
realName = [[currentName fromCSSIdentifier]
|
|
|
|
stringByDecodingImap4FolderName];
|
|
|
|
// And finally to represent that as URI we have to (1) asCSSIdentifier,
|
|
|
|
// (2) Imap4Encode and (3) AddPercentEscapes
|
|
|
|
// e.g.: Probl&AOg-mes_SP_de_SP_synchronisation
|
|
|
|
// In the example there are no percent escapes added because is already ok
|
|
|
|
stringData = [[[realName asCSSIdentifier]
|
|
|
|
stringByEncodingImap4FolderName]
|
|
|
|
stringByAddingPercentEscapesUsingEncoding: NSUTF8StringEncoding];
|
|
|
|
context->url = [[NSString stringWithFormat: @"%@%@", urlBase, stringData] asUnicodeInMemCtx: context];
|
|
|
|
context->name = [[realName substringFromIndex: 6] asUnicodeInMemCtx: context];
|
2012-01-31 13:54:36 +01:00
|
|
|
context->main_folder = false;
|
|
|
|
context->role = MAPISTORE_MAIL_ROLE;
|
|
|
|
context->tag = "tag";
|
|
|
|
DLIST_ADD_END (firstContext, context, void);
|
|
|
|
}
|
|
|
|
|
2012-01-29 20:55:21 +01:00
|
|
|
return firstContext;
|
2012-01-26 20:54:36 +01:00
|
|
|
}
|
|
|
|
|
2012-02-03 16:05:55 +01:00
|
|
|
+ (NSString *)
|
|
|
|
createRootSecondaryFolderWithFID: (uint64_t) fid
|
|
|
|
andName: (NSString *) newFolderName
|
|
|
|
forUser: (NSString *) userName
|
|
|
|
{
|
|
|
|
NSString *mapistoreURI, *folderName;
|
|
|
|
MAPIStoreUserContext *userContext;
|
|
|
|
SOGoMailAccount *accountFolder;
|
|
|
|
SOGoMailFolder *newFolder;
|
|
|
|
|
|
|
|
userContext = [MAPIStoreUserContext userContextWithUsername: userName
|
2012-02-14 15:47:06 +01:00
|
|
|
andTDBIndexing: NULL];
|
2012-02-03 16:05:55 +01:00
|
|
|
accountFolder = [[userContext rootFolders] objectForKey: @"mail"];
|
|
|
|
folderName = [NSString stringWithFormat: @"folder%@",
|
|
|
|
[newFolderName asCSSIdentifier]];
|
|
|
|
newFolder = [SOGoMailFolder objectWithName: folderName
|
|
|
|
inContainer: accountFolder];
|
|
|
|
if ([newFolder create])
|
|
|
|
mapistoreURI = [NSString stringWithFormat: @"sogo://%@:%@@mail/%@/",
|
2015-01-19 19:04:06 +01:00
|
|
|
[userName stringByReplacingOccurrencesOfString: @"@"
|
|
|
|
withString: @"%40"],
|
|
|
|
[userName stringByReplacingOccurrencesOfString: @"@"
|
|
|
|
withString: @"%40"],
|
2014-03-19 17:19:07 +01:00
|
|
|
[[folderName stringByEncodingImap4FolderName] stringByAddingPercentEscapesUsingEncoding: NSUTF8StringEncoding]];
|
2012-02-03 16:05:55 +01:00
|
|
|
else
|
|
|
|
mapistoreURI = nil;
|
|
|
|
|
|
|
|
return mapistoreURI;
|
|
|
|
}
|
|
|
|
|
2012-01-29 20:55:21 +01:00
|
|
|
- (Class) MAPIStoreFolderClass
|
2011-02-25 17:41:53 +01:00
|
|
|
{
|
2012-01-29 20:55:21 +01:00
|
|
|
return MAPIStoreMailFolderK;
|
2011-02-25 17:41:53 +01:00
|
|
|
}
|
|
|
|
|
2012-01-29 20:55:21 +01:00
|
|
|
- (id) rootSOGoFolder
|
2011-02-25 17:41:53 +01:00
|
|
|
{
|
2012-01-30 22:42:03 +01:00
|
|
|
return [[userContext rootFolders] objectForKey: @"mail"];
|
2011-02-25 17:41:53 +01:00
|
|
|
}
|
|
|
|
|
2012-08-15 20:59:42 +02:00
|
|
|
- (void) updateURLWithFolderName: (NSString *) newFolderName
|
|
|
|
{
|
2012-08-17 19:56:25 +02:00
|
|
|
NSString *urlString, *escapedName;
|
2012-08-15 20:59:42 +02:00
|
|
|
NSMutableArray *pathComponents;
|
|
|
|
BOOL hasSlash;
|
|
|
|
NSUInteger max, folderNameIdx;
|
|
|
|
NSURL *newURL;
|
|
|
|
|
2012-08-17 19:56:25 +02:00
|
|
|
/* we do not need to unescape the url here as it will be reassembled later
|
|
|
|
in the method */
|
2012-08-15 20:59:42 +02:00
|
|
|
urlString = [contextUrl absoluteString];
|
|
|
|
hasSlash = [urlString hasSuffix: @"/"];
|
|
|
|
pathComponents = [[urlString componentsSeparatedByString: @"/"]
|
|
|
|
mutableCopy];
|
|
|
|
[pathComponents autorelease];
|
|
|
|
max = [pathComponents count];
|
|
|
|
if (hasSlash)
|
|
|
|
folderNameIdx = max - 2;
|
|
|
|
else
|
|
|
|
folderNameIdx = max - 1;
|
2012-08-17 19:56:25 +02:00
|
|
|
escapedName = [newFolderName stringByAddingPercentEscapesUsingEncoding: NSUTF8StringEncoding];
|
2012-08-15 20:59:42 +02:00
|
|
|
[pathComponents replaceObjectAtIndex: folderNameIdx
|
2012-08-17 19:56:25 +02:00
|
|
|
withObject: escapedName];
|
2012-08-15 20:59:42 +02:00
|
|
|
urlString = [pathComponents componentsJoinedByString: @"/"];
|
|
|
|
newURL = [NSURL URLWithString: urlString];
|
|
|
|
ASSIGN (contextUrl, newURL);
|
|
|
|
}
|
|
|
|
|
2011-02-25 17:41:53 +01:00
|
|
|
@end
|
|
|
|
|
2012-01-31 13:54:36 +01:00
|
|
|
@implementation MAPIStoreOutboxContext
|
2011-02-25 17:41:53 +01:00
|
|
|
|
|
|
|
+ (NSString *) MAPIModuleName
|
|
|
|
{
|
2012-01-31 13:54:36 +01:00
|
|
|
return @"outbox";
|
2011-02-25 17:41:53 +01:00
|
|
|
}
|
2011-08-15 17:53:16 +02:00
|
|
|
|
2012-01-31 13:54:36 +01:00
|
|
|
+ (struct mapistore_contexts_list *) listContextsForUser: (NSString *) userName
|
2013-11-08 17:02:50 +01:00
|
|
|
withIndexing: (struct indexing_context *) indexing
|
2012-01-31 13:54:36 +01:00
|
|
|
inMemCtx: (TALLOC_CTX *) memCtx
|
2012-01-26 20:54:36 +01:00
|
|
|
{
|
2012-01-31 13:54:36 +01:00
|
|
|
struct mapistore_contexts_list *context;
|
|
|
|
NSString *url, *folderName;
|
2012-02-23 22:16:22 +01:00
|
|
|
NSArray *unprefixedFolders;
|
2012-01-31 13:54:36 +01:00
|
|
|
SOGoMailAccount *accountFolder;
|
|
|
|
MAPIStoreUserContext *userContext;
|
|
|
|
WOContext *woContext;
|
|
|
|
|
|
|
|
userContext = [MAPIStoreUserContext userContextWithUsername: userName
|
2013-11-08 17:02:50 +01:00
|
|
|
andTDBIndexing: indexing];
|
2012-01-31 13:54:36 +01:00
|
|
|
accountFolder = [[userContext rootFolders] objectForKey: @"mail"];
|
|
|
|
woContext = [userContext woContext];
|
2012-02-23 22:16:22 +01:00
|
|
|
|
|
|
|
unprefixedFolders = [[accountFolder draftsFolderNameInContext: woContext]
|
|
|
|
componentsSeparatedByString: @"/"];
|
2012-01-31 13:54:36 +01:00
|
|
|
folderName = [NSString stringWithFormat: @"folder%@",
|
2012-02-23 22:16:22 +01:00
|
|
|
[unprefixedFolders componentsJoinedByString: @"/folder"]];
|
2015-01-19 19:04:06 +01:00
|
|
|
url = [NSString stringWithFormat: @"sogo://%@:%@@outbox/%@",
|
|
|
|
[userName stringByReplacingOccurrencesOfString: @"@"
|
|
|
|
withString: @"%40"],
|
|
|
|
[userName stringByReplacingOccurrencesOfString: @"@"
|
|
|
|
withString: @"%40"],
|
|
|
|
folderName];
|
2012-01-31 13:54:36 +01:00
|
|
|
|
|
|
|
context = talloc_zero (memCtx, struct mapistore_contexts_list);
|
|
|
|
context->url = [url asUnicodeInMemCtx: context];
|
|
|
|
/* TODO: use a localized version of this display name */
|
|
|
|
context->name = [@"Outbox" asUnicodeInMemCtx: context];
|
|
|
|
context->main_folder = true;
|
|
|
|
context->role = MAPISTORE_OUTBOX_ROLE;
|
|
|
|
context->tag = "tag";
|
|
|
|
context->prev = context;
|
|
|
|
|
|
|
|
return context;
|
2012-01-26 20:54:36 +01:00
|
|
|
}
|
|
|
|
|
2012-02-20 20:47:58 +01:00
|
|
|
- (Class) MAPIStoreFolderClass
|
|
|
|
{
|
|
|
|
return MAPIStoreOutboxFolderK;
|
|
|
|
}
|
|
|
|
|
2011-02-25 17:41:53 +01:00
|
|
|
@end
|