File: MAPIStoreObject.m

package info (click to toggle)
sogo 2.2.9%2Bgit20141017-1
  • links: PTS, VCS
  • area: main
  • in suites: jessie-kfreebsd
  • size: 43,812 kB
  • sloc: objc: 115,592; python: 5,696; sh: 1,380; perl: 861; makefile: 240; xml: 114; sql: 53; php: 43
file content (408 lines) | stat: -rw-r--r-- 10,641 bytes parent folder | download
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
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
/* MAPIStoreObject.m - this file is part of SOGo
 *
 * Copyright (C) 2011-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.
 */

#import <Foundation/NSArray.h>
#import <Foundation/NSCalendarDate.h>
#import <Foundation/NSDictionary.h>
#import <Foundation/NSTimeZone.h>
#import <NGExtensions/NSObject+Logs.h>
#import <SOGo/SOGoObject.h>
#import <SOGo/SOGoUser.h>
#import <SOGo/SOGoUserDefaults.h>

#import "MAPIStoreContext.h"
#import "MAPIStoreFolder.h"
#import "MAPIStorePropertySelectors.h"
#import "MAPIStoreTypes.h"
#import "MAPIStoreUserContext.h"
#import "NSDate+MAPIStore.h"
#import "NSData+MAPIStore.h"
#import "NSObject+MAPIStore.h"
#import "NSString+MAPIStore.h"

#import "MAPIStoreObject.h"

#undef DEBUG
#include <stdbool.h>
#include <talloc.h>
#include <gen_ndr/exchange.h>
#include <mapistore/mapistore.h>
#include <mapistore/mapistore_errors.h>

@implementation MAPIStoreObject

static Class NSExceptionK, MAPIStoreFolderK;

+ (void) initialize
{
  NSExceptionK = [NSExceptionK class];
  MAPIStoreFolderK = [MAPIStoreFolder class];
}

+ (id) mapiStoreObjectInContainer: (MAPIStoreObject *) newContainer
{
  id newObject;

  newObject = [[self alloc] initInContainer: newContainer];
  [newObject autorelease];

  return newObject;
}

- (id) init
{
  if ((self = [super init]))
    {
      classGetters = (IMP *) MAPIStorePropertyGettersForClass (isa);
      parentContainersBag = [NSMutableArray new];
      container = nil;
      properties = [NSMutableDictionary new];
      proxies = [NSMutableArray new];
    }

  // [self logWithFormat: @"-init"];

  return self;
}

- (id) initInContainer: (MAPIStoreObject *) newContainer
{
  if ((self = [self init]))
    {
      ASSIGN (container, newContainer);
    }

  return self;
}

- (void) dealloc
{
  // [self logWithFormat: @"-dealloc"];
  [proxies release];
  [properties release];
  [parentContainersBag release];
  [container release];
  [super dealloc];
}

- (MAPIStoreObject *) container
{
  return container;
}

- (MAPIStoreContext *) context
{
  return (MAPIStoreContext *) [container context];
}

- (MAPIStoreUserContext *) userContext
{
  return [[self context] userContext];
}

- (MAPIStoreMapping *) mapping
{
  return [[self userContext] mapping];
}

- (NSString *) url
{
  NSString *containerURL, *urlName, *format;

  containerURL = (NSString *) [container url];
  if ([containerURL hasSuffix: @"/"])
    format = @"%@%@";
  else
    format = @"%@/%@";

  urlName = [[self nameInContainer]
              stringByAddingPercentEscapesUsingEncoding: NSUTF8StringEncoding];
  return  [NSString stringWithFormat: format, containerURL, urlName];
}

/* helpers */

- (void) addProperties: (NSDictionary *) newNewProperties
{
  [properties addEntriesFromDictionary: newNewProperties];
}

- (NSMutableDictionary *) properties
{
  return properties;
}

- (int) getProperty: (void **) data
            withTag: (enum MAPITAGS) propTag
           inMemCtx: (TALLOC_CTX *) memCtx
{
  MAPIStorePropertyGetter method = NULL;
  uint16_t propValue;
  SEL methodSel;
  id value;
  int rc = MAPISTORE_ERR_NOT_FOUND;
  NSUInteger count, max;

  value = [properties objectForKey: MAPIPropertyKey (propTag)];
  if (value)
    rc = [value getValue: data forTag: propTag inMemCtx: memCtx];
  else
    {
      propValue = (propTag & 0xffff0000) >> 16;
      methodSel = MAPIStoreSelectorForPropertyGetter (propValue);
      method = (MAPIStorePropertyGetter) classGetters[propValue];
      if (method)
        rc = method (self, methodSel, data, memCtx);
    }

  if (rc == MAPISTORE_ERR_NOT_FOUND)
    {
      max = [proxies count];
      for (count = 0; rc == MAPISTORE_ERR_NOT_FOUND && count < max; count++)
        rc = [[proxies objectAtIndex: count]
               getProperty: data
                   withTag: propTag
                  inMemCtx: memCtx];
    }

  return rc;
}

- (int) getPidTagCreationTime: (void **) data
                     inMemCtx: (TALLOC_CTX *) memCtx
{
  *data = [[self creationTime] asFileTimeInMemCtx: memCtx];

  return MAPISTORE_SUCCESS;
}

- (int) getPidTagLastModificationTime: (void **) data
                         inMemCtx: (TALLOC_CTX *) memCtx
{
  *data = [[self lastModificationTime] asFileTimeInMemCtx: memCtx];

  return MAPISTORE_SUCCESS;
}

- (BOOL) canGetProperty: (enum MAPITAGS) propTag
{
  uint16_t propValue;
  BOOL canGetProperty;
  NSUInteger count, max;

  propValue = (propTag >> 16) & 0xffff;

  canGetProperty = (classGetters[propValue]
                    || [properties objectForKey: MAPIPropertyKey (propTag)]);
  max = [proxies count];
  for (count = 0; !canGetProperty && count < max; count++)
    canGetProperty = [[proxies objectAtIndex: count] canGetProperty: propTag];

  return canGetProperty;
}

- (int) getProperties: (struct mapistore_property_data *) data
             withTags: (enum MAPITAGS *) tags
             andCount: (uint16_t) columnCount
             inMemCtx: (TALLOC_CTX *) memCtx
{
  uint16_t count;

  for (count = 0; count < columnCount; count++)
    data[count].error = [self getProperty: &data[count].data
                                  withTag: tags[count]
                                 inMemCtx: memCtx];

  return MAPISTORE_SUCCESS;
}

- (void) addProxy: (MAPIStoreObjectProxy *) newProxy
{
  [proxies addObject: newProxy];
}

- (int) addPropertiesFromRow: (struct SRow *) aRow
{
  struct SPropValue *cValue;
  NSUInteger counter;
  NSMutableDictionary *newProperties;
  NSTimeZone *tz;
  NSInteger tzOffset;
  id value;

  tz = nil;
  tzOffset = 0;

  newProperties = [NSMutableDictionary dictionaryWithCapacity: aRow->cValues];
  for (counter = 0; counter < aRow->cValues; counter++)
    {
      cValue = aRow->lpProps + counter;
      value = NSObjectFromSPropValue (cValue);
      switch (cValue->ulPropTag & 0xffff)
        {
        case PT_STRING8:
        case PT_MV_STRING8:
          [self warnWithFormat:
                  @"attempting to set string property as PR_STRING8: %.8x",
                cValue->ulPropTag];
          break;
        case PT_SYSTIME:
          if (!tz)
            {
              tz = [[self userContext] timeZone];
              tzOffset = -[tz secondsFromGMT];
            }
          value = [value addYear: 0 month: 0 day: 0
                            hour: 0 minute: 0 second: tzOffset];
          [value setTimeZone: tz];
          break;
        }
      [newProperties setObject: value
                        forKey: MAPIPropertyKey (cValue->ulPropTag)];
    }

  [self addProperties: newProperties];

  return MAPISTORE_SUCCESS;
}

//
// The GlobCnt is 6 bytes long.
//
- (NSData *) getReplicaKeyFromGlobCnt: (uint64_t) objectCnt
{
  struct mapistore_connection_info *connInfo;
  NSMutableData *replicaKey;
  char buffer[6];
  NSUInteger count;

  connInfo = [[self context] connectionInfo];

  for (count = 0; count < 6; count++)
    {
      buffer[count] = objectCnt & 0xff;
      objectCnt >>= 8;
    }

  replicaKey = [NSMutableData dataWithCapacity: 22];
  [replicaKey appendBytes: &connInfo->replica_guid
                   length: sizeof (struct GUID)];
  [replicaKey appendBytes: buffer length: 6];

  return replicaKey;
}

- (int) getReplicaKey: (void **) data
          fromGlobCnt: (uint64_t) objectCnt
             inMemCtx: (TALLOC_CTX *) memCtx
{
  *data = [[self getReplicaKeyFromGlobCnt: objectCnt] asBinaryInMemCtx: memCtx];

  return MAPISTORE_SUCCESS;
}

/* move and copy operations */
- (void) copyPropertiesToObject: (MAPIStoreObject *) newObject  inMemCtx: (TALLOC_CTX *) memCtx
{
  //TALLOC_CTX *memCtx;
  struct SPropTagArray *availableProps;
  struct SRow row;
  enum MAPITAGS propTag;
  bool *exclusions;
  NSUInteger count;
  enum mapistore_error error;
  void *data;

  //memCtx = talloc_zero (NULL, TALLOC_CTX);

  [self getAvailableProperties: &availableProps inMemCtx: memCtx];

  /* We exclude identity and versioning properties to facilitate copy
     operations. If they need to be set (move operations), the caller will need
     to take care of them. */
  exclusions = talloc_array (memCtx, bool, 65536);
  exclusions[(PidTagRowType >> 16) & 0xffff] = true;
  exclusions[(PidTagInstanceKey >> 16) & 0xffff] = true;
  exclusions[(PidTagInstanceNum >> 16) & 0xffff] = true;
  exclusions[(PidTagInstID >> 16) & 0xffff] = true;
  exclusions[(PidTagAttachNumber >> 16) & 0xffff] = true;
  exclusions[(PidTagFolderId >> 16) & 0xffff] = true;
  exclusions[(PidTagMid >> 16) & 0xffff] = true;
  exclusions[(PidTagSourceKey >> 16) & 0xffff] = true;
  exclusions[(PidTagParentSourceKey >> 16) & 0xffff] = true;
  exclusions[(PidTagParentFolderId >> 16) & 0xffff] = true;
  exclusions[(PidTagChangeKey >> 16) & 0xffff] = true;
  exclusions[(PidTagChangeNumber >> 16) & 0xffff] = true;
  exclusions[(PidTagPredecessorChangeList >> 16) & 0xffff] = true;

  row.cValues = 0;
  row.lpProps = talloc_array (memCtx, struct SPropValue, 65535);

  for (count = 0; count < availableProps->cValues; count++)
    {
      propTag = availableProps->aulPropTag[count];
      if (!exclusions[(propTag >> 16) & 0xffff])
        {
          error = [self getProperty: &data withTag: propTag inMemCtx: memCtx];
          if (error == MAPISTORE_SUCCESS && data)
            {
              set_SPropValue_proptag (row.lpProps + row.cValues, propTag,
                                      data);
              row.cValues++;
            }
        }
    }
  [newObject addPropertiesFromRow: &row];

  //talloc_free (memCtx);
}

/* subclasses */
- (NSString *) nameInContainer
{
  [self subclassResponsibility: _cmd];

  return nil;
}

- (NSDate *) creationTime
{
  [self subclassResponsibility: _cmd];

  return nil;
}

- (NSDate *) lastModificationTime
{
  [self subclassResponsibility: _cmd];

  return nil;
}

/* logging */
- (NSString *) loggingPrefix
{
  return [NSString stringWithFormat:@"<%@:%p:%@>",
                   NSStringFromClass (isa), self, [self nameInContainer]];
}

@end