2010-12-13 17:28:18 +01:00
|
|
|
/* SOGoMAPIFSMessage.m - this file is part of SOGo
|
|
|
|
*
|
|
|
|
* Copyright (C) 2010 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.
|
|
|
|
*/
|
|
|
|
|
2011-01-14 14:13:32 +01:00
|
|
|
#import <Foundation/NSArray.h>
|
2011-07-27 23:53:21 +02:00
|
|
|
#import <Foundation/NSData.h>
|
2011-02-04 04:48:31 +01:00
|
|
|
#import <Foundation/NSDictionary.h>
|
2010-12-13 17:28:18 +01:00
|
|
|
#import <Foundation/NSFileManager.h>
|
|
|
|
#import <Foundation/NSException.h>
|
2011-07-27 23:53:21 +02:00
|
|
|
#import <Foundation/NSPropertyList.h>
|
2010-12-13 17:28:18 +01:00
|
|
|
#import <Foundation/NSString.h>
|
|
|
|
|
|
|
|
#import <NGExtensions/NSObject+Logs.h>
|
|
|
|
|
|
|
|
#import "SOGoMAPIFSFolder.h"
|
|
|
|
|
|
|
|
#import "SOGoMAPIFSMessage.h"
|
|
|
|
|
|
|
|
@implementation SOGoMAPIFSMessage
|
|
|
|
|
|
|
|
- (id) init
|
|
|
|
{
|
|
|
|
if ((self = [super init]))
|
|
|
|
{
|
2011-06-04 02:11:43 +02:00
|
|
|
completeFilename = nil;
|
2012-03-28 22:04:32 +02:00
|
|
|
inode = 0;
|
2012-03-19 20:24:16 +01:00
|
|
|
lastModificationTime = nil;
|
2010-12-13 17:28:18 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return self;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void) dealloc
|
|
|
|
{
|
2011-06-04 02:11:43 +02:00
|
|
|
[completeFilename release];
|
2010-12-13 17:28:18 +01:00
|
|
|
[super dealloc];
|
|
|
|
}
|
|
|
|
|
2011-09-23 17:36:49 +02:00
|
|
|
- (Class) mapistoreMessageClass
|
|
|
|
{
|
|
|
|
NSArray *dirMembers;
|
|
|
|
NSString *className;
|
|
|
|
|
|
|
|
/* FIXME: this method is a bit dirty */
|
|
|
|
dirMembers = [[container directory] componentsSeparatedByString: @"/"];
|
|
|
|
if ([dirMembers containsObject: @"fai"]) /* should not occur as FAI message
|
|
|
|
are instantiated directly in
|
|
|
|
MAPIStoreFolder */
|
|
|
|
className = @"MAPIStoreFAIMessage";
|
|
|
|
else if ([dirMembers containsObject: @"notes"])
|
|
|
|
className = @"MAPIStoreNotesMessage";
|
|
|
|
else
|
|
|
|
className = @"MAPIStoreFSMessage";
|
|
|
|
|
|
|
|
return NSClassFromString (className);
|
|
|
|
}
|
|
|
|
|
2011-06-04 02:11:43 +02:00
|
|
|
- (NSString *) completeFilename
|
2010-12-13 17:28:18 +01:00
|
|
|
{
|
2011-06-04 02:11:43 +02:00
|
|
|
if (!completeFilename)
|
|
|
|
{
|
|
|
|
completeFilename = [[container directory]
|
|
|
|
stringByAppendingPathComponent: nameInContainer];
|
|
|
|
[completeFilename retain];
|
|
|
|
}
|
|
|
|
|
|
|
|
return completeFilename;
|
|
|
|
}
|
2010-12-13 17:28:18 +01:00
|
|
|
|
2012-03-19 20:24:16 +01:00
|
|
|
- (BOOL) _readFileChangesDataWithDate: (NSDate **) newLMTime
|
2012-03-28 22:04:32 +02:00
|
|
|
andInode: (NSUInteger *) newInode
|
2012-03-19 20:24:16 +01:00
|
|
|
{
|
|
|
|
BOOL rc;
|
|
|
|
NSDictionary *attributes;
|
|
|
|
|
|
|
|
attributes = [[NSFileManager defaultManager]
|
|
|
|
fileAttributesAtPath: [self completeFilename]
|
|
|
|
traverseLink: NO];
|
|
|
|
if (attributes)
|
|
|
|
{
|
|
|
|
*newLMTime = [attributes fileModificationDate];
|
2012-03-28 22:04:32 +02:00
|
|
|
*newInode = [attributes fileSystemFileNumber];
|
2012-03-19 20:24:16 +01:00
|
|
|
rc = YES;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
rc = NO;
|
|
|
|
|
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (BOOL) _checkFileChangesDataWithDate: (NSDate **) newLMTime
|
2012-03-28 22:04:32 +02:00
|
|
|
andInode: (NSUInteger *) newInode
|
2012-03-19 20:24:16 +01:00
|
|
|
{
|
|
|
|
BOOL hasChanged = NO;
|
|
|
|
NSDate *lastLMTime;
|
2012-03-28 22:04:32 +02:00
|
|
|
NSUInteger lastInode;
|
2012-03-19 20:24:16 +01:00
|
|
|
|
|
|
|
if ([self _readFileChangesDataWithDate: &lastLMTime
|
2012-03-28 22:04:32 +02:00
|
|
|
andInode: &lastInode])
|
2012-03-19 20:24:16 +01:00
|
|
|
{
|
2012-03-28 22:04:32 +02:00
|
|
|
if (inode != lastInode
|
2012-03-19 20:24:16 +01:00
|
|
|
|| ![lastModificationTime isEqual: lastLMTime])
|
|
|
|
{
|
|
|
|
if (lastLMTime)
|
|
|
|
*newLMTime = lastLMTime;
|
2012-03-28 22:04:32 +02:00
|
|
|
if (newInode)
|
|
|
|
*newInode = lastInode;
|
2012-03-19 20:24:16 +01:00
|
|
|
hasChanged = YES;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return hasChanged;
|
|
|
|
}
|
|
|
|
|
2011-10-25 22:23:43 +02:00
|
|
|
- (NSMutableDictionary *) properties
|
2011-06-04 02:11:43 +02:00
|
|
|
{
|
2011-07-27 23:53:21 +02:00
|
|
|
NSData *content;
|
|
|
|
NSString *error;
|
|
|
|
NSPropertyListFormat format;
|
2012-03-19 20:24:16 +01:00
|
|
|
NSDate *lastLMTime;
|
2012-03-28 22:04:32 +02:00
|
|
|
NSUInteger lastInode;
|
2011-07-27 23:53:21 +02:00
|
|
|
|
2012-03-19 20:24:16 +01:00
|
|
|
if ([self _checkFileChangesDataWithDate: &lastLMTime
|
2012-03-28 22:04:32 +02:00
|
|
|
andInode: &lastInode])
|
2010-12-13 17:28:18 +01:00
|
|
|
{
|
2012-03-19 20:24:16 +01:00
|
|
|
[self logWithFormat: @"file '%@' new or modified: rereading properties",
|
|
|
|
[self completeFilename]];
|
|
|
|
[properties release];
|
|
|
|
properties = nil;
|
2011-07-27 23:53:21 +02:00
|
|
|
content = [NSData dataWithContentsOfFile: [self completeFilename]];
|
|
|
|
if (content)
|
|
|
|
{
|
|
|
|
properties = [NSPropertyListSerialization propertyListFromData: content
|
|
|
|
mutabilityOption: NSPropertyListMutableContainers
|
|
|
|
format: &format
|
|
|
|
errorDescription: &error];
|
|
|
|
[properties retain];
|
|
|
|
if (!properties)
|
|
|
|
[self logWithFormat: @"an error occurred during deserialization"
|
|
|
|
@" of message: '%@'", error];
|
|
|
|
}
|
2012-03-19 20:24:16 +01:00
|
|
|
ASSIGN (lastModificationTime, lastLMTime);
|
2012-03-28 22:04:32 +02:00
|
|
|
inode = lastInode;
|
2011-10-25 22:23:43 +02:00
|
|
|
}
|
2010-12-13 17:28:18 +01:00
|
|
|
|
2011-10-25 22:23:43 +02:00
|
|
|
return [super properties];
|
2010-12-13 17:28:18 +01:00
|
|
|
}
|
|
|
|
|
2011-02-24 21:38:01 +01:00
|
|
|
- (void) save
|
2010-12-13 17:28:18 +01:00
|
|
|
{
|
2011-07-27 23:53:21 +02:00
|
|
|
NSData *content;
|
2012-03-19 20:24:16 +01:00
|
|
|
NSDate *lastLMTime;
|
2012-03-28 22:04:32 +02:00
|
|
|
NSUInteger lastInode;
|
2011-07-27 23:53:21 +02:00
|
|
|
|
2011-01-06 04:20:24 +01:00
|
|
|
[container ensureDirectory];
|
|
|
|
|
2011-10-25 22:23:43 +02:00
|
|
|
// [self logWithFormat: @"%d props in whole dict", [properties count]];
|
2011-10-03 22:55:02 +02:00
|
|
|
|
2011-10-25 22:23:43 +02:00
|
|
|
content = [NSPropertyListSerialization
|
|
|
|
dataFromPropertyList: [self properties]
|
|
|
|
format: NSPropertyListBinaryFormat_v1_0
|
|
|
|
errorDescription: NULL];
|
2012-03-28 22:04:32 +02:00
|
|
|
if (![content writeToFile: [self completeFilename] atomically: YES])
|
2010-12-13 17:28:18 +01:00
|
|
|
[NSException raise: @"MAPIStoreIOException"
|
|
|
|
format: @"could not save message"];
|
2012-03-19 20:24:16 +01:00
|
|
|
|
2012-03-28 22:04:32 +02:00
|
|
|
[self _readFileChangesDataWithDate: &lastLMTime andInode: &lastInode];
|
2012-03-19 20:24:16 +01:00
|
|
|
ASSIGN (lastModificationTime, lastLMTime);
|
2012-03-28 22:04:32 +02:00
|
|
|
inode = lastInode;
|
2011-10-25 22:23:43 +02:00
|
|
|
// [self logWithFormat: @"fs message written to '%@'", [self completeFilename]];
|
2010-12-13 17:28:18 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
- (NSString *) davEntityTag
|
|
|
|
{
|
2012-03-19 20:24:16 +01:00
|
|
|
NSDate *lm;
|
2010-12-13 17:28:18 +01:00
|
|
|
|
2011-06-04 02:11:43 +02:00
|
|
|
lm = [self lastModificationTime];
|
2010-12-13 17:28:18 +01:00
|
|
|
|
2011-06-04 02:11:43 +02:00
|
|
|
return [NSString stringWithFormat: @"%d", (int) [lm timeIntervalSince1970]];
|
2010-12-13 17:28:18 +01:00
|
|
|
}
|
|
|
|
|
2011-02-04 04:48:31 +01:00
|
|
|
- (NSException *) delete
|
|
|
|
{
|
|
|
|
NSFileManager *fm;
|
2012-02-02 02:45:17 +01:00
|
|
|
NSException *error;
|
2011-02-04 04:48:31 +01:00
|
|
|
|
|
|
|
fm = [NSFileManager defaultManager];
|
|
|
|
|
2011-06-04 02:11:43 +02:00
|
|
|
if (![fm removeFileAtPath: [self completeFilename] handler: NULL])
|
2012-02-02 02:45:17 +01:00
|
|
|
error = [NSException exceptionWithName: @"MAPIStoreIOException"
|
|
|
|
reason: @"could not delete message"
|
|
|
|
userInfo: nil];
|
|
|
|
else
|
|
|
|
error = nil;
|
2011-02-04 04:48:31 +01:00
|
|
|
|
2012-02-02 02:45:17 +01:00
|
|
|
return error;
|
2011-02-04 04:48:31 +01:00
|
|
|
}
|
|
|
|
|
2011-06-04 02:11:43 +02:00
|
|
|
- (id) _fileAttributeForKey: (NSString *) key
|
|
|
|
{
|
|
|
|
NSDictionary *attributes;
|
|
|
|
|
|
|
|
attributes = [[NSFileManager defaultManager]
|
2011-10-25 22:23:43 +02:00
|
|
|
fileAttributesAtPath: [self completeFilename]
|
|
|
|
traverseLink: NO];
|
2011-06-04 02:11:43 +02:00
|
|
|
|
|
|
|
return [attributes objectForKey: key];
|
|
|
|
}
|
|
|
|
|
2012-03-19 20:24:16 +01:00
|
|
|
- (NSDate *) creationTime
|
2011-06-04 02:11:43 +02:00
|
|
|
{
|
|
|
|
return [self _fileAttributeForKey: NSFileCreationDate];
|
|
|
|
}
|
|
|
|
|
2012-03-19 20:24:16 +01:00
|
|
|
- (NSDate *) lastModificationTime
|
2011-06-04 02:11:43 +02:00
|
|
|
{
|
|
|
|
return [self _fileAttributeForKey: NSFileModificationDate];
|
|
|
|
}
|
|
|
|
|
2010-12-13 17:28:18 +01:00
|
|
|
@end
|