1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278
|
/* MAPIStoreMapping.m - this file is part of SOGo
*
* Copyright (C) 2010-2012 Inverse inc.
*
* 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.
*/
#include <inttypes.h>
#import <Foundation/NSArray.h>
#import <Foundation/NSDictionary.h>
#import <Foundation/NSException.h>
#import <Foundation/NSString.h>
#import <Foundation/NSValue.h>
#import <NGExtensions/NSObject+Logs.h>
#import <NGExtensions/NSObject+Values.h>
#import <SOGo/NSString+Utilities.h>
#undef DEBUG
#include <mapistore/mapistore.h>
#include <mapistore/mapistore_errors.h>
#import "MAPIStoreTypes.h"
#import "MAPIStoreMapping.h"
#include <talloc.h>
static NSMutableDictionary *mappingRegistry = nil;
@implementation MAPIStoreMapping
+ (void) initialize
{
mappingRegistry = [NSMutableDictionary new];
}
static inline id
MAPIStoreMappingKeyFromId (uint64_t idNbr)
{
return [NSString stringWithUnsignedLongLong: idNbr];
}
+ (id) mappingForUsername: (NSString *) username
withIndexing: (struct indexing_context *) indexing
{
id mapping;
mapping = [mappingRegistry objectForKey: username];
if (!mapping)
{
mapping = [[self alloc] initForUsername: username
withIndexing: indexing];
[mapping autorelease];
}
return mapping;
}
- (id) init
{
if ((self = [super init]))
{
memCtx = talloc_zero (NULL, TALLOC_CTX);
indexing = NULL;
useCount = 0;
}
return self;
}
- (void) increaseUseCount
{
if (useCount == 0)
{
[mappingRegistry setObject: self forKey: username];
[self logWithFormat: @"mapping registered (%@)", username];
}
useCount++;
}
- (void) decreaseUseCount
{
useCount--;
if (useCount == 0)
{
[mappingRegistry removeObjectForKey: username];
[self logWithFormat: @"mapping deregistered (%@)", username];
}
}
- (id) initForUsername: (NSString *) newUsername
withIndexing: (struct indexing_context *) newIndexing
{
if ((self = [self init]))
{
ASSIGN (username, newUsername);
indexing = newIndexing;
}
return self;
}
- (void) dealloc
{
[username release];
talloc_free (memCtx);
[super dealloc];
}
- (NSString *) urlFromID: (uint64_t) idNbr
{
char* url = NULL;
enum mapistore_error ret;
bool soft_delete = false;
ret = indexing->get_uri(indexing, [username UTF8String],
memCtx, idNbr, &url, &soft_delete);
if (ret != MAPISTORE_SUCCESS)
return NULL;
NSString *res = [[[NSString alloc] initWithUTF8String:url] autorelease];
talloc_free(url);
return res;
}
- (uint64_t) idFromURL: (NSString *) url
{
enum mapistore_error ret;
uint64_t idNbr;
bool softDeleted;
ret = indexing->get_fmid(indexing, [username UTF8String], [url UTF8String],
false, &idNbr, &softDeleted);
if (ret == MAPISTORE_SUCCESS && !softDeleted)
return idNbr;
else
return NSNotFound;
}
- (void) _updateFolderWithURL: (NSString *) oldURL
withURL: (NSString *) urlString
{
const char *searchURL;
uint64_t idNbr;
bool softDeleted;
NSString *current;
NSString *newURL;
if ([oldURL isEqualToString: urlString]) return;
searchURL = [[oldURL stringByAppendingString:@"*"] UTF8String];
while (indexing->get_fmid(indexing, [username UTF8String],
searchURL,true, &idNbr, &softDeleted) == MAPISTORE_SUCCESS)
{
// Ignore deleted
if (softDeleted) continue;
current = [self urlFromID:idNbr];
newURL = [current stringByReplacingPrefix: oldURL withPrefix: urlString];
indexing->update_fmid(indexing, [username UTF8String], idNbr, [newURL UTF8String]);
}
}
- (void) updateID: (uint64_t) idNbr
withURL: (NSString *) urlString
{
NSString *oldURL;
oldURL = [self urlFromID: idNbr];
if (oldURL)
{
if ([oldURL hasSuffix: @"/"]) /* is container ? */
{
if (![urlString hasSuffix: @"/"])
[NSException raise: NSInvalidArgumentException
format: @"a container url must have an ending '/'"];
[self _updateFolderWithURL: oldURL withURL: urlString];
}
else
{
if ([urlString hasSuffix: @"/"])
[NSException raise: NSInvalidArgumentException
format: @"a leaf url must not have an ending '/'"];
indexing->update_fmid(indexing, [username UTF8String],
idNbr, [urlString UTF8String]);
}
}
}
- (BOOL) registerURL: (NSString *) urlString
withID: (uint64_t) idNbr
{
NSString *oldURL;
uint64_t oldIdNbr;
bool rc;
oldURL = [self urlFromID: idNbr];
if (oldURL != NULL)
{
[self errorWithFormat:
@"url with idNbr already registered: (oldUrl='%@', newUrl='%@', id=x%.16"PRIx64")",
oldURL, urlString, idNbr];
return NO;
}
oldIdNbr = [self idFromURL: urlString];
if (oldIdNbr != NSNotFound)
{
[self errorWithFormat:
@"attempt to double register an entry with idNbr ('%@', %lld,"
@" 0x%.16"PRIx64", oldid=0x%.16"PRIx64")",
urlString, idNbr, idNbr, oldIdNbr];
return NO;
}
else
{
rc = YES;
// [self logWithFormat: @"registered url '%@' with id %lld (0x%.16"PRIx64")",
// urlString, idNbr, idNbr];
/* Add the record given its fid and mapistore_uri */
indexing->add_fmid(indexing, [username UTF8String],
idNbr, [urlString UTF8String]);
}
return rc;
}
- (void) registerURLs: (NSArray *) urlStrings
withIDs: (NSArray *) idNbrs
{
uint64_t count, max, newID;
max = [urlStrings count];
if (max == [idNbrs count])
{
for (count = 0; count < max; count++)
{
newID = [[idNbrs objectAtIndex: count]
unsignedLongLongValue];
[self registerURL: [urlStrings objectAtIndex: count]
withID: newID];
}
}
else
[NSException raise: NSInvalidArgumentException
format: @"number of urls and ids do not match"];
}
- (void) unregisterURLWithID: (uint64_t) idNbr
{
indexing->del_fmid(indexing, [username UTF8String],
idNbr, MAPISTORE_PERMANENT_DELETE);
}
@end
|