2010-12-30 15:37:07 +01:00
|
|
|
/* MAPIStoreMailMessageTable.m - this file is part of SOGo
|
|
|
|
*
|
2012-08-17 21:04:57 +02:00
|
|
|
* Copyright (C) 2010-2012 Inverse inc
|
2010-12-30 15:37:07 +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.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#import <Foundation/NSArray.h>
|
2011-01-14 14:13:32 +01:00
|
|
|
#import <Foundation/NSCharacterSet.h>
|
2010-12-30 15:37:07 +01:00
|
|
|
#import <Foundation/NSDictionary.h>
|
|
|
|
#import <Foundation/NSException.h>
|
2011-02-13 23:58:29 +01:00
|
|
|
#import <Foundation/NSRange.h>
|
2010-12-30 15:37:07 +01:00
|
|
|
#import <NGExtensions/NSObject+Logs.h>
|
2011-02-04 17:08:13 +01:00
|
|
|
#import <EOControl/EOQualifier.h>
|
2010-12-30 15:37:07 +01:00
|
|
|
#import <SOGo/NSArray+Utilities.h>
|
|
|
|
|
2011-02-13 23:58:29 +01:00
|
|
|
#import <Mailer/NSData+Mail.h>
|
2010-12-30 15:37:07 +01:00
|
|
|
#import <Mailer/SOGoMailFolder.h>
|
|
|
|
#import <Mailer/SOGoMailObject.h>
|
|
|
|
|
|
|
|
#import "MAPIStoreContext.h"
|
2011-02-24 21:40:07 +01:00
|
|
|
#import "MAPIStoreMailFolder.h"
|
2011-07-12 23:34:07 +02:00
|
|
|
#import "MAPIStoreMailMessage.h"
|
2010-12-30 15:37:07 +01:00
|
|
|
#import "MAPIStoreTypes.h"
|
|
|
|
#import "NSData+MAPIStore.h"
|
|
|
|
#import "NSString+MAPIStore.h"
|
|
|
|
|
|
|
|
#import "MAPIStoreMailMessageTable.h"
|
|
|
|
|
2011-02-06 23:49:33 +01:00
|
|
|
#undef DEBUG
|
|
|
|
#include <mapistore/mapistore.h>
|
2010-12-30 15:37:07 +01:00
|
|
|
#include <mapistore/mapistore_nameid.h>
|
|
|
|
|
|
|
|
@implementation MAPIStoreMailMessageTable
|
|
|
|
|
2011-07-12 23:34:07 +02:00
|
|
|
static Class MAPIStoreMailMessageK, NSDataK, NSStringK;
|
2010-12-30 15:37:07 +01:00
|
|
|
|
|
|
|
+ (void) initialize
|
|
|
|
{
|
2011-07-12 23:34:07 +02:00
|
|
|
MAPIStoreMailMessageK = [MAPIStoreMailMessage class];
|
2010-12-30 15:37:07 +01:00
|
|
|
NSDataK = [NSData class];
|
|
|
|
NSStringK = [NSString class];
|
|
|
|
}
|
|
|
|
|
2011-07-12 23:34:07 +02:00
|
|
|
+ (Class) childObjectClass
|
|
|
|
{
|
|
|
|
return MAPIStoreMailMessageK;
|
|
|
|
}
|
|
|
|
|
2011-02-06 23:49:33 +01:00
|
|
|
- (id) init
|
|
|
|
{
|
|
|
|
if ((self = [super init]))
|
|
|
|
{
|
2011-02-24 21:40:07 +01:00
|
|
|
ASSIGN (sortOrderings, [NSArray arrayWithObject: @"ARRIVAL"]);
|
2011-08-13 03:54:07 +02:00
|
|
|
fetchedCoreInfos = NO;
|
2011-02-06 23:49:33 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return self;
|
|
|
|
}
|
|
|
|
|
2011-07-28 02:52:38 +02:00
|
|
|
- (void) cleanupCaches
|
|
|
|
{
|
|
|
|
[(MAPIStoreMailFolder *) container synchroniseCache];
|
2011-08-13 03:54:07 +02:00
|
|
|
fetchedCoreInfos = NO;
|
2011-07-28 02:52:38 +02:00
|
|
|
[super cleanupCaches];
|
|
|
|
}
|
|
|
|
|
2010-12-30 15:37:07 +01:00
|
|
|
- (NSString *) backendIdentifierForProperty: (enum MAPITAGS) property
|
|
|
|
{
|
|
|
|
static NSMutableDictionary *knownProperties = nil;
|
|
|
|
|
|
|
|
if (!knownProperties)
|
|
|
|
{
|
|
|
|
knownProperties = [NSMutableDictionary new];
|
|
|
|
[knownProperties setObject: @"DATE"
|
|
|
|
forKey: MAPIPropertyKey (PR_CLIENT_SUBMIT_TIME)];
|
|
|
|
[knownProperties setObject: @"DATE"
|
|
|
|
forKey: MAPIPropertyKey (PR_MESSAGE_DELIVERY_TIME)];
|
|
|
|
[knownProperties setObject: @"MESSAGE-ID"
|
|
|
|
forKey: MAPIPropertyKey (PR_INTERNET_MESSAGE_ID_UNICODE)];
|
|
|
|
}
|
|
|
|
|
|
|
|
return [knownProperties objectForKey: MAPIPropertyKey (property)];
|
|
|
|
}
|
|
|
|
|
|
|
|
/* restrictions */
|
|
|
|
|
|
|
|
- (MAPIRestrictionState) evaluatePropertyRestriction: (struct mapi_SPropertyRestriction *) res
|
|
|
|
intoQualifier: (EOQualifier **) qualifier
|
|
|
|
{
|
|
|
|
MAPIRestrictionState rc;
|
|
|
|
id value;
|
2011-07-28 02:52:38 +02:00
|
|
|
NSNumber *modseq;
|
2010-12-30 15:37:07 +01:00
|
|
|
|
|
|
|
value = NSObjectFromMAPISPropValue (&res->lpProp);
|
2011-03-21 01:41:22 +01:00
|
|
|
switch ((uint32_t) res->ulPropTag)
|
2010-12-30 15:37:07 +01:00
|
|
|
{
|
|
|
|
case PR_MESSAGE_CLASS_UNICODE:
|
|
|
|
if ([value isEqualToString: @"IPM.Note"])
|
|
|
|
rc = MAPIRestrictionStateAlwaysTrue;
|
|
|
|
else
|
|
|
|
rc = MAPIRestrictionStateAlwaysFalse;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case PidLidAppointmentStartWhole:
|
|
|
|
case PidLidAppointmentEndWhole:
|
|
|
|
case PidLidRecurring:
|
2013-03-13 20:04:11 +01:00
|
|
|
//[self logWithFormat: @"apt restriction on mail folder?"];
|
2010-12-30 15:37:07 +01:00
|
|
|
rc = MAPIRestrictionStateAlwaysFalse;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case PidLidAutoProcessState:
|
|
|
|
if ([value intValue] == 0)
|
|
|
|
rc = MAPIRestrictionStateAlwaysTrue;
|
|
|
|
else
|
|
|
|
rc = MAPIRestrictionStateAlwaysFalse;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case PR_SEARCH_KEY:
|
|
|
|
rc = MAPIRestrictionStateAlwaysFalse;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 0x0fff00fb: /* PR_ENTRY_ID in PtyServerId form */
|
|
|
|
case 0x0ff600fb:
|
|
|
|
/* resProperty: struct mapi_SPropertyRestriction
|
|
|
|
relop : 0x04 (4)
|
|
|
|
ulPropTag : UNKNOWN_ENUM_VALUE (0xFF600FB)
|
|
|
|
lpProp: struct mapi_SPropValue
|
|
|
|
ulPropTag : UNKNOWN_ENUM_VALUE (0xFF600FB)
|
|
|
|
value : union mapi_SPropValue_CTR(case 251)
|
|
|
|
bin : SBinary_short cb=21
|
|
|
|
[0000] 01 01 00 1A 00 00 00 00 00 9C 83 E8 0F 00 00 00 ........ ........
|
|
|
|
[0010] 00 00 00 00 00 ..... */
|
|
|
|
rc = MAPIRestrictionStateAlwaysFalse;
|
|
|
|
break;
|
|
|
|
|
2012-03-02 21:44:24 +01:00
|
|
|
case PidTagConversationKey:
|
2010-12-30 15:37:07 +01:00
|
|
|
rc = MAPIRestrictionStateAlwaysFalse;
|
|
|
|
break;
|
2011-07-28 02:52:38 +02:00
|
|
|
|
2012-03-02 21:44:24 +01:00
|
|
|
case PidTagChangeNumber:
|
2011-07-28 02:52:38 +02:00
|
|
|
{
|
2013-02-14 21:22:48 +01:00
|
|
|
value = [NSString stringWithFormat: @"0x%.16llx", [value unsignedLongLongValue]];
|
2011-07-28 02:52:38 +02:00
|
|
|
modseq = [(MAPIStoreMailFolder *)
|
|
|
|
container modseqFromMessageChangeNumber: value];
|
2013-03-13 20:04:11 +01:00
|
|
|
//[self logWithFormat: @"change number from oxcfxics: %.16lx", [value unsignedLongLongValue]];
|
|
|
|
//[self logWithFormat: @" modseq: %.16lx", [modseq unsignedLongLongValue]];
|
2011-07-28 02:52:38 +02:00
|
|
|
if (modseq)
|
|
|
|
modseq = [NSNumber numberWithUnsignedLongLong:
|
|
|
|
[modseq unsignedLongLongValue] + 1];
|
|
|
|
else
|
|
|
|
modseq = [NSNumber numberWithUnsignedLongLong: 0];
|
|
|
|
*qualifier = [[EOKeyValueQualifier alloc] initWithKey: @"MODSEQ"
|
|
|
|
operatorSelector: EOQualifierOperatorGreaterThanOrEqualTo
|
|
|
|
value: modseq];
|
|
|
|
[*qualifier autorelease];
|
|
|
|
rc = MAPIRestrictionStateNeedsEval;
|
|
|
|
}
|
|
|
|
break;
|
2010-12-30 15:37:07 +01:00
|
|
|
|
|
|
|
default:
|
|
|
|
rc = [super evaluatePropertyRestriction: res intoQualifier: qualifier];
|
|
|
|
}
|
|
|
|
|
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (MAPIRestrictionState) evaluateContentRestriction: (struct mapi_SContentRestriction *) res
|
|
|
|
intoQualifier: (EOQualifier **) qualifier
|
|
|
|
{
|
|
|
|
MAPIRestrictionState rc;
|
|
|
|
id value;
|
|
|
|
|
|
|
|
value = NSObjectFromMAPISPropValue (&res->lpProp);
|
|
|
|
if ([value isKindOfClass: NSDataK])
|
|
|
|
{
|
|
|
|
value = [[NSString alloc] initWithData: value
|
|
|
|
encoding: NSUTF8StringEncoding];
|
|
|
|
[value autorelease];
|
|
|
|
}
|
|
|
|
else if (![value isKindOfClass: NSStringK])
|
|
|
|
[NSException raise: @"MAPIStoreTypeConversionException"
|
|
|
|
format: @"unhandled content restriction for class '%@'",
|
|
|
|
NSStringFromClass ([value class])];
|
|
|
|
|
|
|
|
switch (res->ulPropTag)
|
|
|
|
{
|
|
|
|
case PR_MESSAGE_CLASS_UNICODE:
|
|
|
|
if ([value isEqualToString: @"IPM.Note"])
|
|
|
|
rc = MAPIRestrictionStateAlwaysTrue;
|
|
|
|
else
|
|
|
|
rc = MAPIRestrictionStateAlwaysFalse;
|
|
|
|
break;
|
2012-03-02 21:44:24 +01:00
|
|
|
case PidTagConversationKey:
|
2010-12-30 15:37:07 +01:00
|
|
|
rc = MAPIRestrictionStateAlwaysFalse;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
rc = [super evaluateContentRestriction: res intoQualifier: qualifier];
|
|
|
|
}
|
|
|
|
|
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (MAPIRestrictionState) evaluateExistRestriction: (struct mapi_SExistRestriction *) res
|
|
|
|
intoQualifier: (EOQualifier **) qualifier
|
|
|
|
{
|
|
|
|
MAPIRestrictionState rc;
|
|
|
|
|
|
|
|
switch (res->ulPropTag)
|
|
|
|
{
|
|
|
|
case PR_MESSAGE_CLASS_UNICODE:
|
|
|
|
rc = MAPIRestrictionStateAlwaysFalse;
|
|
|
|
break;
|
|
|
|
case PR_MESSAGE_DELIVERY_TIME:
|
|
|
|
rc = MAPIRestrictionStateAlwaysTrue;
|
|
|
|
break;
|
2011-04-06 17:09:41 +02:00
|
|
|
case PR_CLIENT_SUBMIT_TIME:
|
|
|
|
rc = MAPIRestrictionStateAlwaysTrue;
|
|
|
|
break;
|
2010-12-30 15:37:07 +01:00
|
|
|
case PR_PROCESSED:
|
|
|
|
rc = MAPIRestrictionStateAlwaysFalse;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
rc = [super evaluateExistRestriction: res intoQualifier: qualifier];
|
|
|
|
}
|
|
|
|
|
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
|
2011-02-06 23:49:33 +01:00
|
|
|
/* sorting */
|
|
|
|
|
|
|
|
- (NSString *) _sortIdentifierForProperty: (enum MAPITAGS) property
|
|
|
|
{
|
|
|
|
static NSMutableDictionary *knownProperties = nil;
|
|
|
|
|
|
|
|
if (!knownProperties)
|
|
|
|
{
|
|
|
|
knownProperties = [NSMutableDictionary new];
|
|
|
|
/* ARRIVAL, CC */
|
2011-02-10 00:01:55 +01:00
|
|
|
[knownProperties setObject: @"DATE"
|
|
|
|
forKey: MAPIPropertyKey (PR_CLIENT_SUBMIT_TIME)];
|
2011-02-06 23:49:33 +01:00
|
|
|
[knownProperties setObject: @"DATE"
|
|
|
|
forKey: MAPIPropertyKey (PR_MESSAGE_DELIVERY_TIME)];
|
|
|
|
[knownProperties setObject: @"FROM"
|
|
|
|
forKey: MAPIPropertyKey (PR_SENT_REPRESENTING_NAME_UNICODE)];
|
|
|
|
[knownProperties setObject: @"SIZE"
|
|
|
|
forKey: MAPIPropertyKey (PR_MESSAGE_SIZE)];
|
|
|
|
[knownProperties setObject: @"SIZE"
|
|
|
|
forKey: MAPIPropertyKey (PidLidRemoteTransferSize)];
|
|
|
|
[knownProperties setObject: @"SUBJECT"
|
|
|
|
forKey: MAPIPropertyKey (PR_NORMALIZED_SUBJECT_UNICODE)];
|
|
|
|
[knownProperties setObject: @"TO"
|
|
|
|
forKey: MAPIPropertyKey (PR_DISPLAY_TO_UNICODE)];
|
|
|
|
}
|
|
|
|
|
|
|
|
return [knownProperties objectForKey: MAPIPropertyKey (property)];
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void) setSortOrder: (const struct SSortOrderSet *) set
|
|
|
|
{
|
2011-02-24 21:40:07 +01:00
|
|
|
NSMutableArray *newSortOrderings;
|
2011-02-06 23:49:33 +01:00
|
|
|
NSMutableString *newSortOrdering;
|
|
|
|
struct SSortOrder *sortOrder;
|
|
|
|
NSString *sortIdentifier;
|
|
|
|
const char *propName;
|
|
|
|
uint16_t count;
|
|
|
|
|
|
|
|
if (set)
|
|
|
|
{
|
|
|
|
/* TODO: */
|
|
|
|
if (set->cCategories > 0)
|
|
|
|
[self errorWithFormat: @"we don't handle sort categories yet"];
|
|
|
|
|
2011-02-24 21:40:07 +01:00
|
|
|
newSortOrderings = [NSMutableArray array];
|
2011-02-06 23:49:33 +01:00
|
|
|
|
|
|
|
for (count = 0; count < set->cSorts; count++)
|
|
|
|
{
|
|
|
|
sortOrder = set->aSort + count;
|
|
|
|
sortIdentifier
|
|
|
|
= [self _sortIdentifierForProperty: sortOrder->ulPropTag];
|
|
|
|
if (sortIdentifier)
|
|
|
|
{
|
2011-02-24 21:40:07 +01:00
|
|
|
newSortOrdering = [NSMutableString string];
|
2011-02-06 23:49:33 +01:00
|
|
|
if (sortOrder->ulOrder == TABLE_SORT_DESCEND)
|
|
|
|
[newSortOrdering appendString: @" REVERSE"];
|
|
|
|
else if (sortOrder->ulOrder == TABLE_SORT_MAXIMUM_CATEGORY)
|
|
|
|
[self errorWithFormat: @"TABLE_SORT_MAXIMUM_CATEGORY is not handled"];
|
|
|
|
[newSortOrdering appendFormat: @" %@", sortIdentifier];
|
2011-02-24 21:40:07 +01:00
|
|
|
[newSortOrderings addObject: [newSortOrdering substringFromIndex: 1]];
|
2011-02-06 23:49:33 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
propName = get_proptag_name (sortOrder->ulPropTag);
|
|
|
|
if (!propName)
|
|
|
|
propName = "<unknown>";
|
|
|
|
[self errorWithFormat:
|
|
|
|
@"sort unhandled for property: %s (0x%.8x)",
|
|
|
|
propName, sortOrder->ulPropTag];
|
|
|
|
}
|
|
|
|
}
|
2011-02-24 21:40:07 +01:00
|
|
|
if ([newSortOrderings count] > 0)
|
|
|
|
ASSIGN (sortOrderings, newSortOrderings);
|
2011-02-06 23:49:33 +01:00
|
|
|
else
|
2011-02-24 21:40:07 +01:00
|
|
|
ASSIGN (sortOrderings, [NSArray arrayWithObject: @"ARRIVAL"]);
|
2013-03-13 20:04:11 +01:00
|
|
|
//[self logWithFormat: @"new sort orderings: '%@'", sortOrderings];
|
2011-02-06 23:49:33 +01:00
|
|
|
}
|
|
|
|
else
|
2011-02-24 21:40:07 +01:00
|
|
|
ASSIGN (sortOrderings, [NSArray arrayWithObject: @"ARRIVAL"]);
|
|
|
|
|
|
|
|
[self cleanupCaches];
|
2011-02-06 23:49:33 +01:00
|
|
|
}
|
|
|
|
|
2011-08-13 03:54:07 +02:00
|
|
|
- (int) getRow: (struct mapistore_property_data **) dataP
|
|
|
|
withRowID: (uint32_t) rowId
|
2012-01-16 00:58:26 +01:00
|
|
|
andQueryType: (enum mapistore_query_type) queryType
|
2011-08-13 03:54:07 +02:00
|
|
|
inMemCtx: (TALLOC_CTX *) memCtx
|
|
|
|
{
|
|
|
|
if (!fetchedCoreInfos)
|
|
|
|
{
|
|
|
|
fetchedCoreInfos = YES;
|
2012-06-29 19:59:38 +02:00
|
|
|
[(SOGoMailFolder *) [(MAPIStoreMailFolder *) container sogoObject]
|
2011-08-13 03:54:07 +02:00
|
|
|
prefetchCoreInfosForMessageKeys: [self restrictedChildKeys]];
|
|
|
|
}
|
|
|
|
|
|
|
|
return [super getRow: dataP withRowID: rowId
|
|
|
|
andQueryType: queryType inMemCtx: memCtx];
|
|
|
|
}
|
|
|
|
|
2010-12-30 15:37:07 +01:00
|
|
|
@end
|