2008-01-16 19:47:20 +01:00
|
|
|
/* SOGoCache.m - this file is part of SOGo
|
|
|
|
*
|
2009-02-24 02:54:59 +01:00
|
|
|
* Copyright (C) 2008-2009 Inverse inc.
|
2008-01-16 19:47:20 +01:00
|
|
|
*
|
|
|
|
* Author: Wolfgang Sourdeau <wsourdeau@inverse.ca>
|
2008-11-11 01:17:35 +01:00
|
|
|
* Ludovic Marcotte <lmarcotte@inverse.ca>
|
2008-01-16 19:47:20 +01:00
|
|
|
*
|
|
|
|
* 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 2, 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.
|
|
|
|
*/
|
|
|
|
|
2009-02-24 02:54:59 +01:00
|
|
|
/*
|
|
|
|
* [ Structure ]
|
|
|
|
* users: key = user ID value = NSMutableDictionary instance
|
|
|
|
* value: key = @"user" value = SOGOUser instance
|
|
|
|
* key = @"cleanupDate" value = NSDate instance
|
|
|
|
* key = @"defaults" value = SOGoUserDefaults instance
|
|
|
|
* key = @"settings" value = SOGoUserDefaults instance
|
|
|
|
* key = @"attributes" value = NSDictionary instance (attributes from LDAP)
|
|
|
|
*
|
|
|
|
* [ Workflows - processes A and B ]
|
|
|
|
*
|
|
|
|
* A cache user defaults and posts the notification
|
|
|
|
* B ....
|
|
|
|
*
|
|
|
|
* B crashes
|
|
|
|
* B receives a notificaion update
|
|
|
|
*/
|
|
|
|
|
2008-01-16 19:47:20 +01:00
|
|
|
#import <Foundation/NSArray.h>
|
2008-07-04 18:06:09 +02:00
|
|
|
#import <Foundation/NSDictionary.h>
|
2008-11-11 01:17:35 +01:00
|
|
|
#import <Foundation/NSDistributedNotificationCenter.h>
|
|
|
|
#import <Foundation/NSEnumerator.h>
|
2008-12-12 19:55:12 +01:00
|
|
|
#import <Foundation/NSLock.h>
|
2008-07-04 18:06:09 +02:00
|
|
|
#import <Foundation/NSString.h>
|
2008-11-11 01:17:35 +01:00
|
|
|
#import <Foundation/NSTimer.h>
|
|
|
|
#import <Foundation/NSUserDefaults.h>
|
2008-01-16 19:47:20 +01:00
|
|
|
|
|
|
|
#import <NGObjWeb/SoObject.h>
|
2008-11-11 01:17:35 +01:00
|
|
|
#import <NGExtensions/NSObject+Logs.h>
|
2008-01-16 19:47:20 +01:00
|
|
|
|
2008-07-29 18:36:16 +02:00
|
|
|
#import "SOGoObject.h"
|
2008-01-16 19:47:20 +01:00
|
|
|
#import "SOGoUser.h"
|
2008-11-12 04:30:13 +01:00
|
|
|
#import "SOGoUserDefaults.h"
|
2008-07-29 18:36:16 +02:00
|
|
|
|
2008-01-16 19:47:20 +01:00
|
|
|
#import "SOGoCache.h"
|
|
|
|
|
2008-11-11 01:17:35 +01:00
|
|
|
// We define the default value for cleaning up cached
|
2008-11-11 02:52:56 +01:00
|
|
|
// users' preferences. This value should be relatively
|
|
|
|
// high to avoid useless database calls.
|
2009-02-24 02:54:59 +01:00
|
|
|
static NSTimeInterval cleanupInterval = 300;
|
2008-11-11 02:52:56 +01:00
|
|
|
|
|
|
|
static NSMutableDictionary *cache = nil;
|
|
|
|
static NSMutableDictionary *users = nil;
|
|
|
|
|
2008-01-16 19:47:20 +01:00
|
|
|
static SOGoCache *sharedCache = nil;
|
|
|
|
|
2008-12-12 19:55:12 +01:00
|
|
|
#if defined(THREADSAFE)
|
|
|
|
static NSLock *lock;
|
|
|
|
#endif
|
|
|
|
|
2008-01-16 19:47:20 +01:00
|
|
|
@implementation SOGoCache
|
|
|
|
|
2008-12-12 19:55:12 +01:00
|
|
|
+ (void) initialize
|
|
|
|
{
|
2009-02-24 02:54:59 +01:00
|
|
|
#if defined(THREADSAFE)
|
2008-12-12 19:55:12 +01:00
|
|
|
lock = [NSLock new];
|
|
|
|
#endif
|
2008-12-15 23:23:40 +01:00
|
|
|
}
|
2008-12-12 19:55:12 +01:00
|
|
|
|
2008-11-11 02:52:56 +01:00
|
|
|
+ (NSTimeInterval) cleanupInterval
|
|
|
|
{
|
|
|
|
return cleanupInterval;
|
|
|
|
}
|
|
|
|
|
2008-01-16 19:47:20 +01:00
|
|
|
+ (SOGoCache *) sharedCache
|
|
|
|
{
|
2008-12-12 19:55:12 +01:00
|
|
|
#if defined(THREADSAFE)
|
|
|
|
[lock lock];
|
|
|
|
#endif
|
2008-01-16 19:47:20 +01:00
|
|
|
if (!sharedCache)
|
|
|
|
sharedCache = [self new];
|
2008-12-12 19:55:12 +01:00
|
|
|
#if defined(THREADSAFE)
|
|
|
|
[lock unlock];
|
|
|
|
#endif
|
2008-01-16 19:47:20 +01:00
|
|
|
|
|
|
|
return sharedCache;
|
|
|
|
}
|
|
|
|
|
|
|
|
+ (void) killCache
|
|
|
|
{
|
2008-12-12 19:55:12 +01:00
|
|
|
#if defined(THREADSAFE)
|
|
|
|
[lock lock];
|
|
|
|
#endif
|
2008-11-11 02:52:56 +01:00
|
|
|
[cache removeAllObjects];
|
2008-12-12 19:55:12 +01:00
|
|
|
#if defined(THREADSAFE)
|
|
|
|
[lock unlock];
|
|
|
|
#endif
|
2008-01-16 19:47:20 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
- (id) init
|
|
|
|
{
|
|
|
|
if ((self = [super init]))
|
|
|
|
{
|
2008-11-11 01:17:35 +01:00
|
|
|
NSString *cleanupSetting;
|
|
|
|
|
2008-11-12 04:30:13 +01:00
|
|
|
cache = [[NSMutableDictionary alloc] init];
|
|
|
|
users = [[NSMutableDictionary alloc] init];
|
|
|
|
|
2008-11-14 01:55:00 +01:00
|
|
|
// We register ourself for notifications
|
2009-02-24 02:54:59 +01:00
|
|
|
[[NSDistributedNotificationCenter defaultCenter]
|
|
|
|
addObserver: self
|
|
|
|
selector: @selector(_userAttributesHaveLoaded:)
|
|
|
|
name: @"SOGoUserAttributesHaveLoaded"
|
|
|
|
object: nil];
|
|
|
|
|
|
|
|
[[NSDistributedNotificationCenter defaultCenter]
|
|
|
|
addObserver: self
|
|
|
|
selector: @selector(_userDefaultsHaveLoaded:)
|
|
|
|
name: @"SOGoUserDefaultsHaveLoaded"
|
|
|
|
object: nil];
|
|
|
|
|
2008-11-11 01:17:35 +01:00
|
|
|
[[NSDistributedNotificationCenter defaultCenter]
|
|
|
|
addObserver: self
|
|
|
|
selector: @selector(_userDefaultsHaveChanged:)
|
|
|
|
name: @"SOGoUserDefaultsHaveChanged"
|
|
|
|
object: nil];
|
|
|
|
|
2009-02-24 02:54:59 +01:00
|
|
|
[[NSDistributedNotificationCenter defaultCenter]
|
|
|
|
addObserver: self
|
|
|
|
selector: @selector(_userSettingsHaveLoaded:)
|
|
|
|
name: @"SOGoUserSettingsHaveLoaded"
|
|
|
|
object: nil];
|
|
|
|
|
2008-11-11 01:17:35 +01:00
|
|
|
[[NSDistributedNotificationCenter defaultCenter]
|
|
|
|
addObserver: self
|
|
|
|
selector: @selector(_userSettingsHaveChanged:)
|
|
|
|
name: @"SOGoUserSettingsHaveChanged"
|
|
|
|
object: nil];
|
|
|
|
|
|
|
|
// We fire our timer that will cleanup cache entries
|
|
|
|
cleanupSetting = [[NSUserDefaults standardUserDefaults]
|
|
|
|
objectForKey: @"SOGoCacheCleanupInterval"];
|
|
|
|
|
|
|
|
if (cleanupSetting && [cleanupSetting doubleValue] > 0.0)
|
|
|
|
cleanupInterval = [cleanupSetting doubleValue];
|
|
|
|
|
|
|
|
_cleanupTimer = [NSTimer scheduledTimerWithTimeInterval: cleanupInterval
|
|
|
|
target: self
|
|
|
|
selector: @selector(_cleanupSources)
|
|
|
|
userInfo: nil
|
|
|
|
repeats: YES];
|
2009-02-24 02:54:59 +01:00
|
|
|
[self logWithFormat: @"Cache cleanup interval set every %f seconds",
|
2008-11-11 01:17:35 +01:00
|
|
|
cleanupInterval];
|
2008-01-16 19:47:20 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return self;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void) dealloc
|
|
|
|
{
|
2009-02-24 02:54:59 +01:00
|
|
|
[[NSDistributedNotificationCenter defaultCenter]
|
|
|
|
removeObserver: self
|
|
|
|
name: @"SOGoUserAttributesHaveLoaded"
|
|
|
|
object: nil];
|
|
|
|
|
|
|
|
[[NSDistributedNotificationCenter defaultCenter]
|
|
|
|
removeObserver: self
|
|
|
|
name: @"SOGoUserDefaultsHaveLoaded"
|
|
|
|
object: nil];
|
|
|
|
|
2008-11-11 02:52:56 +01:00
|
|
|
[[NSDistributedNotificationCenter defaultCenter]
|
|
|
|
removeObserver: self
|
|
|
|
name: @"SOGoUserDefaultsHaveChanged"
|
|
|
|
object: nil];
|
2009-02-24 02:54:59 +01:00
|
|
|
|
|
|
|
[[NSDistributedNotificationCenter defaultCenter]
|
|
|
|
removeObserver: self
|
|
|
|
name: @"SOGoUserSettingsHaveLoaded"
|
|
|
|
object: nil];
|
2008-11-11 02:52:56 +01:00
|
|
|
|
|
|
|
[[NSDistributedNotificationCenter defaultCenter]
|
|
|
|
removeObserver: self
|
|
|
|
name: @"SOGoUserSettingsHaveChanged"
|
|
|
|
object: nil];
|
|
|
|
|
2008-01-16 19:47:20 +01:00
|
|
|
[cache release];
|
|
|
|
[users release];
|
|
|
|
[super dealloc];
|
|
|
|
}
|
|
|
|
|
|
|
|
- (NSString *) _pathFromObject: (SOGoObject *) container
|
2009-02-24 02:54:59 +01:00
|
|
|
withName: (NSString *) name
|
2008-01-16 19:47:20 +01:00
|
|
|
{
|
|
|
|
NSString *fullPath, *nameInContainer;
|
|
|
|
NSMutableArray *names;
|
|
|
|
id currentObject;
|
|
|
|
|
|
|
|
names = [NSMutableArray new];
|
|
|
|
|
|
|
|
[names addObject: name];
|
|
|
|
currentObject = container;
|
|
|
|
while ((nameInContainer = [currentObject nameInContainer]))
|
|
|
|
{
|
|
|
|
[names addObject: nameInContainer];
|
|
|
|
currentObject = [currentObject container];
|
|
|
|
}
|
|
|
|
|
|
|
|
fullPath = [names componentsJoinedByString: @"/"];
|
|
|
|
[names release];
|
|
|
|
|
|
|
|
return fullPath;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void) registerObject: (id) object
|
|
|
|
withName: (NSString *) name
|
|
|
|
inContainer: (SOGoObject *) container
|
|
|
|
{
|
|
|
|
NSString *fullPath;
|
|
|
|
|
2008-01-16 20:52:43 +01:00
|
|
|
if (object && name)
|
2008-01-16 19:47:20 +01:00
|
|
|
{
|
2008-01-16 20:52:43 +01:00
|
|
|
[self registerObject: container
|
|
|
|
withName: [container nameInContainer]
|
|
|
|
inContainer: [container container]];
|
2008-01-16 19:47:20 +01:00
|
|
|
fullPath = [self _pathFromObject: container
|
|
|
|
withName: name];
|
2008-12-12 19:55:12 +01:00
|
|
|
#if defined(THREADSAFE)
|
|
|
|
[lock lock];
|
|
|
|
#endif
|
2008-01-16 20:52:43 +01:00
|
|
|
if (![cache objectForKey: fullPath])
|
|
|
|
{
|
|
|
|
[cache setObject: object forKey: fullPath];
|
|
|
|
}
|
2008-12-12 19:55:12 +01:00
|
|
|
#if defined(THREADSAFE)
|
|
|
|
[lock unlock];
|
|
|
|
#endif
|
2008-01-16 19:47:20 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
- (id) objectNamed: (NSString *) name
|
|
|
|
inContainer: (SOGoObject *) container
|
|
|
|
{
|
|
|
|
NSString *fullPath;
|
|
|
|
|
|
|
|
fullPath = [self _pathFromObject: container
|
|
|
|
withName: name];
|
|
|
|
|
|
|
|
return [cache objectForKey: fullPath];
|
2008-12-12 19:55:12 +01:00
|
|
|
// if (object)
|
|
|
|
// NSLog (@"found cached object '%@'", fullPath);
|
2008-01-16 19:47:20 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
- (void) registerUser: (SOGoUser *) user
|
2008-11-11 02:52:56 +01:00
|
|
|
{
|
2009-02-24 02:54:59 +01:00
|
|
|
NSData *cleanupDate;
|
|
|
|
|
2008-12-12 19:55:12 +01:00
|
|
|
#if defined(THREADSAFE)
|
|
|
|
[lock lock];
|
|
|
|
#endif
|
2009-02-24 02:54:59 +01:00
|
|
|
|
|
|
|
cleanupDate = [[NSDate date] addTimeInterval: [SOGoCache cleanupInterval]];
|
|
|
|
|
|
|
|
if (![users objectForKey: [user login]])
|
|
|
|
[users setObject: [NSMutableDictionary dictionary] forKey: [user login]];
|
|
|
|
|
|
|
|
[[users objectForKey: [user login]] setObject: user forKey: @"user"];
|
|
|
|
[[users objectForKey: [user login]] setObject: [[NSDate date] addTimeInterval: [SOGoCache cleanupInterval]]
|
|
|
|
forKey: @"cleanupDate"];
|
|
|
|
|
2008-12-12 19:55:12 +01:00
|
|
|
#if defined(THREADSAFE)
|
|
|
|
[lock unlock];
|
|
|
|
#endif
|
2008-01-16 19:47:20 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
- (id) userNamed: (NSString *) name
|
|
|
|
{
|
2009-02-24 02:54:59 +01:00
|
|
|
return [[users objectForKey: name] objectForKey: @"user"];
|
2008-11-11 01:17:35 +01:00
|
|
|
}
|
|
|
|
|
2009-02-24 02:54:59 +01:00
|
|
|
- (void) cacheAttributes: (NSDictionary *) theAttributes
|
|
|
|
forLogin: (NSString *) theLogin
|
2008-11-11 01:17:35 +01:00
|
|
|
{
|
2009-02-24 02:54:59 +01:00
|
|
|
if (![users objectForKey: theLogin])
|
|
|
|
[users setObject: [NSMutableDictionary dictionary] forKey: theLogin];
|
|
|
|
|
|
|
|
[[users objectForKey: theLogin] setObject: theAttributes forKey: @"attributes"];
|
|
|
|
[[users objectForKey: theLogin] setObject: [[NSDate date] addTimeInterval: [SOGoCache cleanupInterval]]
|
|
|
|
forKey: @"cleanupDate"];
|
2008-11-12 04:30:13 +01:00
|
|
|
}
|
2008-11-11 01:17:35 +01:00
|
|
|
|
2009-02-24 02:54:59 +01:00
|
|
|
- (NSMutableDictionary *) userAttributesForLogin: (NSString *) theLogin
|
2008-11-12 04:30:13 +01:00
|
|
|
{
|
2009-02-24 02:54:59 +01:00
|
|
|
return [[users objectForKey: theLogin] objectForKey: @"attributes"];
|
|
|
|
}
|
|
|
|
|
|
|
|
- (SOGoUserDefaults *) userDefaultsForLogin: (NSString *) theLogin
|
|
|
|
{
|
|
|
|
return [[users objectForKey: theLogin] objectForKey: @"defaults"];
|
2008-11-12 04:30:13 +01:00
|
|
|
}
|
2008-11-11 01:17:35 +01:00
|
|
|
|
2009-02-24 02:54:59 +01:00
|
|
|
- (SOGoUserDefaults *) userSettingsForLogin: (NSString *) theLogin
|
2008-11-12 04:30:13 +01:00
|
|
|
{
|
2009-02-24 02:54:59 +01:00
|
|
|
return [[users objectForKey: theLogin] objectForKey: @"settings"];
|
2008-11-12 04:30:13 +01:00
|
|
|
}
|
|
|
|
|
2009-03-03 20:08:02 +01:00
|
|
|
//
|
|
|
|
//
|
|
|
|
//
|
|
|
|
- (void) setDefaults: (SOGoUserDefaults *) theDefaults
|
|
|
|
forLogin: (NSString *) theLogin
|
|
|
|
key: (NSString *) theKey
|
|
|
|
{
|
|
|
|
if (![users objectForKey: theLogin])
|
|
|
|
[users setObject: [NSMutableDictionary dictionary] forKey: theLogin];
|
|
|
|
|
|
|
|
[[users objectForKey: theLogin] setObject: theDefaults forKey: theKey];
|
|
|
|
[[users objectForKey: theLogin] setObject: [[NSDate date] addTimeInterval: [SOGoCache cleanupInterval]]
|
|
|
|
forKey: @"cleanupDate"];
|
|
|
|
|
|
|
|
//NSLog(@"Set %@ to %@", theKey, [users objectForKey: theLogin]);
|
|
|
|
}
|
|
|
|
|
2009-02-24 02:54:59 +01:00
|
|
|
//
|
|
|
|
// Notification callbacks.
|
|
|
|
//
|
|
|
|
- (void) _cacheValues: (NSDictionary *) theValues
|
|
|
|
login: (NSString *) theLogin
|
|
|
|
url: (NSString *) theURL
|
|
|
|
key: (NSString *) theKey
|
2008-11-12 04:30:13 +01:00
|
|
|
{
|
2009-02-24 02:54:59 +01:00
|
|
|
SOGoUserDefaults *defaults;
|
|
|
|
NSURL *url;
|
2008-11-12 04:30:13 +01:00
|
|
|
|
2008-12-12 19:55:12 +01:00
|
|
|
#if defined(THREADSAFE)
|
|
|
|
[lock lock];
|
|
|
|
#endif
|
2009-02-24 02:54:59 +01:00
|
|
|
url = [[[NSURL alloc] initWithString: theURL] autorelease];
|
|
|
|
|
|
|
|
defaults = [[[SOGoUserDefaults alloc] initWithTableURL: url
|
|
|
|
uid: theLogin
|
2009-04-21 17:20:52 +02:00
|
|
|
fieldName: [NSString stringWithFormat: @"c_%@", theKey]
|
|
|
|
shouldPropagate: YES]
|
2009-02-24 02:54:59 +01:00
|
|
|
autorelease];
|
2009-03-03 20:08:02 +01:00
|
|
|
[defaults setValues: theValues];
|
|
|
|
[self setDefaults: defaults forLogin: theLogin key: theKey];
|
2009-02-24 02:54:59 +01:00
|
|
|
|
2008-12-12 19:55:12 +01:00
|
|
|
#if defined(THREADSAFE)
|
|
|
|
[lock unlock];
|
|
|
|
#endif
|
2008-11-12 04:30:13 +01:00
|
|
|
}
|
|
|
|
|
2009-03-03 20:08:02 +01:00
|
|
|
//
|
|
|
|
//
|
|
|
|
//
|
2009-02-24 02:54:59 +01:00
|
|
|
- (void) _userAttributesHaveLoaded: (NSNotification *) theNotification
|
2008-11-12 04:30:13 +01:00
|
|
|
{
|
2008-11-20 20:17:59 +01:00
|
|
|
NSString *uid;
|
2009-02-24 02:54:59 +01:00
|
|
|
|
|
|
|
uid = [[theNotification userInfo] objectForKey: @"uid"];
|
|
|
|
//NSLog(@"Caching user attributes for UID: %@", uid);
|
|
|
|
if (![self userAttributesForLogin: uid])
|
|
|
|
{
|
|
|
|
NSEnumerator *emails;
|
|
|
|
NSDictionary *values;
|
|
|
|
NSString *key;
|
|
|
|
|
|
|
|
if (![users objectForKey: uid])
|
|
|
|
[users setObject: [NSMutableDictionary dictionary] forKey: uid];
|
|
|
|
|
|
|
|
values = [[theNotification userInfo] objectForKey: @"values"];
|
|
|
|
[self cacheAttributes: values forLogin: uid];
|
|
|
|
|
|
|
|
emails = [[values objectForKey: @"emails"] objectEnumerator];
|
|
|
|
while ((key = [emails nextObject]))
|
|
|
|
{
|
|
|
|
[self cacheAttributes: values forLogin: key];
|
|
|
|
}
|
|
|
|
|
|
|
|
[[users objectForKey: uid] setObject: [[NSDate date] addTimeInterval: [SOGoCache cleanupInterval]]
|
|
|
|
forKey: @"cleanupDate"];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-03-03 20:08:02 +01:00
|
|
|
//
|
|
|
|
//
|
|
|
|
//
|
2009-02-24 02:54:59 +01:00
|
|
|
- (void) _userDefaultsHaveLoaded: (NSNotification *) theNotification
|
|
|
|
{
|
|
|
|
NSString *uid;
|
|
|
|
|
|
|
|
uid = [[theNotification userInfo] objectForKey: @"uid"];
|
|
|
|
//NSLog(@"Loading user defaults for UID: %@", uid);
|
|
|
|
if (![self userDefaultsForLogin: uid])
|
|
|
|
{
|
|
|
|
[self _cacheValues: [[theNotification userInfo] objectForKey: @"values"]
|
|
|
|
login: uid
|
|
|
|
url: [[theNotification userInfo] objectForKey: @"url"]
|
|
|
|
key: @"defaults"];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-03-03 20:08:02 +01:00
|
|
|
//
|
|
|
|
//
|
|
|
|
//
|
2009-02-24 02:54:59 +01:00
|
|
|
- (void) _userDefaultsHaveChanged: (NSNotification *) theNotification
|
|
|
|
{
|
2009-03-24 21:36:49 +01:00
|
|
|
SOGoUser *user;
|
2009-01-13 21:39:56 +01:00
|
|
|
SOGoUserDefaults *defaults;
|
2009-02-24 02:54:59 +01:00
|
|
|
NSString *uid;
|
2009-03-03 20:08:02 +01:00
|
|
|
|
2008-11-20 20:17:59 +01:00
|
|
|
uid = [[theNotification userInfo] objectForKey: @"uid"];
|
2009-03-24 21:36:49 +01:00
|
|
|
|
|
|
|
// When the user defaults changed, we must invalidate the
|
|
|
|
// ivar language for the user object.
|
|
|
|
user = [self userNamed: uid];
|
|
|
|
if (user)
|
|
|
|
[user invalidateLanguage];
|
|
|
|
|
2009-02-24 02:54:59 +01:00
|
|
|
//NSLog(@"Updating user defaults for UID: %@", uid);
|
|
|
|
defaults = (SOGoUserDefaults *)[self userDefaultsForLogin: uid];
|
|
|
|
if (defaults)
|
|
|
|
{
|
2008-12-12 19:55:12 +01:00
|
|
|
#if defined(THREADSAFE)
|
2009-02-24 02:54:59 +01:00
|
|
|
[lock lock];
|
2008-12-12 19:55:12 +01:00
|
|
|
#endif
|
2009-01-13 21:39:56 +01:00
|
|
|
[defaults setValues: [[theNotification userInfo] objectForKey: @"values"]];
|
2009-02-24 02:54:59 +01:00
|
|
|
[[users objectForKey: uid] setObject: [[NSDate date] addTimeInterval: [SOGoCache cleanupInterval]]
|
|
|
|
forKey: @"cleanupDate"];
|
|
|
|
#if defined(THREADSAFE)
|
|
|
|
[lock unlock];
|
|
|
|
#endif
|
2008-12-02 02:36:53 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2009-02-24 02:54:59 +01:00
|
|
|
[self _cacheValues: [[theNotification userInfo] objectForKey: @"values"]
|
|
|
|
login: uid
|
|
|
|
url: [[theNotification userInfo] objectForKey: @"url"]
|
|
|
|
key: @"defaults"];
|
2008-11-20 20:17:59 +01:00
|
|
|
}
|
2008-11-11 01:17:35 +01:00
|
|
|
}
|
|
|
|
|
2009-03-03 20:08:02 +01:00
|
|
|
//
|
|
|
|
//
|
|
|
|
//
|
2009-02-24 02:54:59 +01:00
|
|
|
- (void) _userSettingsHaveLoaded: (NSNotification *) theNotification
|
2008-11-11 01:17:35 +01:00
|
|
|
{
|
2008-11-20 20:17:59 +01:00
|
|
|
NSString *uid;
|
2009-02-24 02:54:59 +01:00
|
|
|
|
2008-11-20 20:17:59 +01:00
|
|
|
uid = [[theNotification userInfo] objectForKey: @"uid"];
|
2009-02-24 02:54:59 +01:00
|
|
|
//NSLog(@"Loading user settings for UID: %@", uid);
|
|
|
|
if (![self userSettingsForLogin: uid])
|
|
|
|
{
|
|
|
|
[self _cacheValues: [[theNotification userInfo] objectForKey: @"values"]
|
|
|
|
login: uid
|
|
|
|
url: [[theNotification userInfo] objectForKey: @"url"]
|
|
|
|
key: @"settings"];
|
|
|
|
}
|
|
|
|
}
|
2008-11-20 20:17:59 +01:00
|
|
|
|
2009-03-03 20:08:02 +01:00
|
|
|
//
|
|
|
|
//
|
|
|
|
//
|
2009-02-24 02:54:59 +01:00
|
|
|
- (void) _userSettingsHaveChanged: (NSNotification *) theNotification
|
|
|
|
{
|
|
|
|
SOGoUserDefaults *settings;
|
|
|
|
NSString *uid;
|
|
|
|
|
|
|
|
uid = [[theNotification userInfo] objectForKey: @"uid"];
|
|
|
|
//NSLog(@"Updating user settings for UID: %@", uid);
|
|
|
|
settings = (SOGoUserDefaults *)[self userSettingsForLogin: uid];
|
|
|
|
if (settings)
|
2008-11-20 20:17:59 +01:00
|
|
|
{
|
2009-02-24 02:54:59 +01:00
|
|
|
#if defined(THREADSAFE)
|
|
|
|
[lock lock];
|
|
|
|
#endif
|
2009-01-13 21:39:56 +01:00
|
|
|
[settings setValues: [[theNotification userInfo] objectForKey: @"values"]];
|
2009-02-24 02:54:59 +01:00
|
|
|
[[users objectForKey: uid] setObject: [[NSDate date] addTimeInterval: [SOGoCache cleanupInterval]]
|
|
|
|
forKey: @"cleanupDate"];
|
|
|
|
#if defined(THREADSAFE)
|
|
|
|
[lock unlock];
|
|
|
|
#endif
|
2008-12-02 02:36:53 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2009-02-24 02:54:59 +01:00
|
|
|
[self _cacheValues: [[theNotification userInfo] objectForKey: @"values"]
|
|
|
|
login: uid
|
|
|
|
url: [[theNotification userInfo] objectForKey: @"url"]
|
|
|
|
key: @"settings"];
|
2008-11-20 20:17:59 +01:00
|
|
|
}
|
2008-11-11 01:17:35 +01:00
|
|
|
}
|
|
|
|
|
2009-03-03 20:08:02 +01:00
|
|
|
//
|
|
|
|
//
|
|
|
|
//
|
2008-11-11 01:17:35 +01:00
|
|
|
- (void) _cleanupSources
|
|
|
|
{
|
2008-11-11 02:52:56 +01:00
|
|
|
NSDictionary *currentEntry;
|
2008-11-11 01:17:35 +01:00
|
|
|
NSEnumerator *userIDs;
|
|
|
|
NSString *currentID;
|
|
|
|
NSDate *now;
|
2008-11-11 02:52:56 +01:00
|
|
|
|
2008-11-11 01:17:35 +01:00
|
|
|
unsigned int count;
|
|
|
|
|
2008-12-12 19:55:12 +01:00
|
|
|
#if defined(THREADSAFE)
|
|
|
|
[lock lock];
|
|
|
|
#endif
|
|
|
|
|
2008-11-11 01:17:35 +01:00
|
|
|
now = [NSDate date];
|
2008-11-11 02:52:56 +01:00
|
|
|
|
2009-02-24 02:54:59 +01:00
|
|
|
// We cleanup the user cache
|
|
|
|
userIDs = [[users allKeys] objectEnumerator];
|
2008-11-11 02:52:56 +01:00
|
|
|
count = 0;
|
|
|
|
while ((currentID = [userIDs nextObject]))
|
|
|
|
{
|
2009-02-24 02:54:59 +01:00
|
|
|
currentEntry = [users objectForKey: currentID];
|
2008-11-11 02:52:56 +01:00
|
|
|
|
|
|
|
if ([now earlierDate: [currentEntry objectForKey: @"cleanupDate"]] == now)
|
|
|
|
{
|
2009-02-24 02:54:59 +01:00
|
|
|
[users removeObjectForKey: currentID];
|
2008-11-11 02:52:56 +01:00
|
|
|
count++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-11-12 04:30:13 +01:00
|
|
|
if (count)
|
2009-02-24 02:54:59 +01:00
|
|
|
[self logWithFormat: @"cleaned %d users records from users cache",
|
2008-12-12 19:55:12 +01:00
|
|
|
count];
|
|
|
|
|
|
|
|
#if defined(THREADSAFE)
|
|
|
|
[lock unlock];
|
|
|
|
#endif
|
2008-01-16 19:47:20 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
@end
|