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>
|
|
|
|
#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;
|
|
|
|
|
|
|
|
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
|
2012-01-29 20:55:21 +01:00
|
|
|
withTDBIndexing: (struct tdb_wrap *) indexingTdb
|
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;
|
2012-02-24 00:34:26 +01:00
|
|
|
NSString *urlBase, *stringData, *currentName, *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
|
|
|
|
andTDBIndexing: indexingTdb];
|
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"]];
|
|
|
|
|
2012-01-29 20:55:21 +01:00
|
|
|
urlBase = [NSString stringWithFormat: @"sogo://%@:%@@mail/", userName, userName];
|
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
|
|
|
|
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);
|
|
|
|
currentName = [secondaryFolders objectAtIndex: count];
|
2012-09-07 18:46:18 +02:00
|
|
|
stringData = [NSString stringWithFormat: @"%@%@",
|
|
|
|
urlBase, [currentName stringByAddingPercentEscapesUsingEncoding: NSUTF8StringEncoding]];
|
2012-01-31 13:54:36 +01:00
|
|
|
context->url = [stringData asUnicodeInMemCtx: context];
|
2012-02-03 16:05:55 +01:00
|
|
|
stringData = [[currentName substringFromIndex: 6] fromCSSIdentifier];
|
2012-01-31 13:54:36 +01:00
|
|
|
context->name = [stringData asUnicodeInMemCtx: context];
|
|
|
|
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-03-16 21:57:35 +01:00
|
|
|
[MAPIApp setUserContext: userContext];
|
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/%@/",
|
2012-09-07 18:46:18 +02:00
|
|
|
userName, userName,
|
|
|
|
[folderName stringByAddingPercentEscapesUsingEncoding: NSUTF8StringEncoding]];
|
2012-02-03 16:05:55 +01:00
|
|
|
else
|
|
|
|
mapistoreURI = nil;
|
2012-03-16 21:57:35 +01:00
|
|
|
[MAPIApp setUserContext: nil];
|
2012-02-03 16:05:55 +01:00
|
|
|
|
|
|
|
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
|
|
|
|
withTDBIndexing: (struct tdb_wrap *) indexingTdb
|
|
|
|
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
|
|
|
|
andTDBIndexing: indexingTdb];
|
|
|
|
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"]];
|
2012-01-31 13:54:36 +01:00
|
|
|
url = [NSString stringWithFormat: @"sogo://%@:%@@outbox/%@", userName,
|
|
|
|
userName, folderName];
|
|
|
|
|
|
|
|
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
|