1/* EOQualifier+SOGoCacheObject.m - this file is part of SOGo
2 *
3 * Copyright (C) 2010-2014 Inverse inc
4 *
5 * This file is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 3, or (at your option)
8 * any later version.
9 *
10 * This file is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; see the file COPYING.  If not, write to
17 * the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 * Boston, MA 02111-1307, USA.
19 */
20
21#import <Foundation/NSArray.h>
22#import <Foundation/NSCharacterSet.h>
23#import <Foundation/NSDictionary.h>
24#import <Foundation/NSString.h>
25#import <Foundation/NSValue.h>
26
27#import <NGExtensions/NSObject+Logs.h>
28
29#import "EOBitmaskQualifier.h"
30#import "SOGoCacheGCSObject.h"
31
32#import "EOQualifier+SOGoCacheObject.h"
33
34@implementation EOQualifier (SOGoCacheObjectRestrictions)
35
36- (BOOL) _evaluateSOGoMAPIDBObject: (NSDictionary *) properties
37{
38  [self subclassResponsibility: _cmd];
39  return NO;
40}
41
42- (BOOL) evaluateSOGoMAPIDBObject: (SOGoCacheGCSObject *) object
43{
44  NSDictionary *properties;
45  BOOL rc;
46
47  //[self logWithFormat: @"evaluating object '%@'", object];
48
49  properties = [object properties];
50  rc = [self _evaluateSOGoMAPIDBObject: properties];
51
52  //[self logWithFormat: @"  evaluation result: %d", rc];
53
54  return rc;
55}
56
57@end
58
59@implementation EOAndQualifier (SOGoCacheRestrictionsPrivate)
60
61- (BOOL) _evaluateSOGoMAPIDBObject: (NSDictionary *) properties
62{
63  NSUInteger i;
64  BOOL rc;
65
66  rc = YES;
67
68  for (i = 0; rc && i < count; i++)
69    rc = [[qualifiers objectAtIndex: i]
70	   _evaluateSOGoMAPIDBObject: properties];
71
72  return rc;
73}
74
75@end
76
77@implementation EOOrQualifier (SOGoCacheObjectRestrictionsPrivate)
78
79- (BOOL) _evaluateSOGoMAPIDBObject: (NSDictionary *) properties
80{
81  NSUInteger i;
82  BOOL rc;
83
84  rc = NO;
85
86  for (i = 0; !rc && i < count; i++)
87    rc = [[qualifiers objectAtIndex: i]
88	   _evaluateSOGoMAPIDBObject: properties];
89
90  return rc;
91}
92
93@end
94
95@implementation EONotQualifier (SOGoCacheObjectRestrictionsPrivate)
96
97- (BOOL) _evaluateSOGoMAPIDBObject: (NSDictionary *) properties
98{
99  return ![qualifier _evaluateSOGoMAPIDBObject: properties];
100}
101
102@end
103
104@implementation EOKeyValueQualifier (SOGoCacheObjectRestrictionsPrivate)
105
106typedef BOOL (*EOComparator) (id, SEL, id);
107
108- (BOOL) _evaluateSOGoMAPIDBObject: (NSDictionary *) properties
109{
110  id finalKey;
111  id propValue;
112  EOComparator comparator;
113
114  if ([key isKindOfClass: [NSNumber class]])
115    finalKey = key;
116  else if ([key isKindOfClass: [NSString class]])
117    {
118      finalKey = [key stringByTrimmingCharactersInSet: [NSCharacterSet decimalDigitCharacterSet]];
119      if ([finalKey length] > 0)
120        finalKey = key;
121      else
122        finalKey = [NSNumber numberWithInt: [key intValue]];
123    }
124  else
125    finalKey = @"";
126
127  propValue = [properties objectForKey: finalKey];
128  /* sogo-openchange library stores the properties as NSString keys
129     and we have to check if the property exists using the NSString */
130  if (!propValue && [key isKindOfClass: [NSString class]])
131    propValue = [properties objectForKey: key];
132
133  comparator = (EOComparator) [propValue methodForSelector: operator];
134
135  return (comparator ? comparator (propValue, operator, value) : NO);
136}
137
138@end
139
140@implementation EOBitmaskQualifier (SOGoCacheObjectRestrictionsPrivate)
141
142- (BOOL) _evaluateSOGoMAPIDBObject: (NSDictionary *) properties
143{
144  NSNumber *propTag;
145  id propValue;
146  uint32_t intValue;
147  BOOL rc;
148
149  propTag = [NSNumber numberWithInt: [key intValue]];
150  propValue = [properties objectForKey: propTag];
151  intValue = [propValue unsignedIntValue];
152
153  rc = ((isZero && (intValue & mask) == 0)
154	|| (!isZero && (intValue & mask) != 0));
155
156  //[self logWithFormat: @"evaluation of bitmask qualifier:"
157  //	@" (%.8x & %.8x) %s 0: %d",
158  //	intValue, mask, (isZero ? "==" : "!="), rc];
159
160  return rc;
161}
162
163@end
164