diff --git a/SoObjects/SOGo/SOGoCache.h b/SoObjects/SOGo/SOGoCache.h index 8664a2394..0403078d5 100644 --- a/SoObjects/SOGo/SOGoCache.h +++ b/SoObjects/SOGo/SOGoCache.h @@ -35,11 +35,11 @@ @interface SOGoCache : NSObject { - NSMutableDictionary *cache; - NSMutableDictionary *users; - NSTimer *_cleanupTimer; + @private + NSTimer *_cleanupTimer; } ++ (NSTimeInterval) cleanupInterval; + (SOGoCache *) sharedCache; + (void) killCache; diff --git a/SoObjects/SOGo/SOGoCache.m b/SoObjects/SOGo/SOGoCache.m index 96c3bc0a0..da0cf651b 100644 --- a/SoObjects/SOGo/SOGoCache.m +++ b/SoObjects/SOGo/SOGoCache.m @@ -38,13 +38,22 @@ #import "SOGoCache.h" // We define the default value for cleaning up cached -// users' preferences. This value should be relatively high -// to avoid useless database calls. +// users' preferences. This value should be relatively +// high to avoid useless database calls. static NSTimeInterval cleanupInterval = 1800; + +static NSMutableDictionary *cache = nil; +static NSMutableDictionary *users = nil; + static SOGoCache *sharedCache = nil; @implementation SOGoCache ++ (NSTimeInterval) cleanupInterval +{ + return cleanupInterval; +} + + (SOGoCache *) sharedCache { if (!sharedCache) @@ -55,8 +64,8 @@ static SOGoCache *sharedCache = nil; + (void) killCache { - [sharedCache release]; - sharedCache = nil; + [cache removeAllObjects]; + [users removeAllObjects]; } - (id) init @@ -102,6 +111,16 @@ static SOGoCache *sharedCache = nil; - (void) dealloc { + [[NSDistributedNotificationCenter defaultCenter] + removeObserver: self + name: @"SOGoUserDefaultsHaveChanged" + object: nil]; + + [[NSDistributedNotificationCenter defaultCenter] + removeObserver: self + name: @"SOGoUserSettingsHaveChanged" + object: nil]; + [cache release]; [users release]; [super dealloc]; @@ -167,21 +186,16 @@ static SOGoCache *sharedCache = nil; } - (void) registerUser: (SOGoUser *) user -{ - NSDate *cleanupDate; - - cleanupDate = [[NSDate date] addTimeInterval: cleanupInterval]; - - [users setObject: [NSMutableDictionary dictionaryWithObjectsAndKeys: user, @"user", cleanupDate, @"cleanupDate", nil] +{ + [users setObject: user forKey: [user login]]; } - (id) userNamed: (NSString *) name { - return [[users objectForKey: name] objectForKey: @"user"]; + return [users objectForKey: name]; } - - (void) _userDefaultsHaveChanged: (NSNotification *) theNotification { NSMutableDictionary *d; @@ -214,29 +228,48 @@ static SOGoCache *sharedCache = nil; - (void) _cleanupSources { + NSDictionary *currentEntry; NSEnumerator *userIDs; NSString *currentID; - NSDictionary *currentUser; NSDate *now; + unsigned int count; now = [NSDate date]; + + // We cleanup the user defaults + userIDs = [[[SOGoUser userDefaultsCache] allKeys] objectEnumerator]; count = 0; - userIDs = [[users allKeys] objectEnumerator]; - while ((currentID = [userIDs nextObject])) { - currentUser = [users objectForKey: currentID]; - if ([now earlierDate: - [currentUser objectForKey: @"cleanupDate"]] == now) + currentEntry = [[SOGoUser userDefaultsCache] objectForKey: currentID]; + + if ([now earlierDate: [currentEntry objectForKey: @"cleanupDate"]] == now) { - [users removeObjectForKey: currentID]; + [SOGoUser setUserDefaultsFromDictionary: nil user: currentID]; count++; } } if (count) - [self logWithFormat: @"cleaned %d users records from cache", count]; + [self logWithFormat: @"cleaned %d users records from user defaults cache", count]; + + // We cleanup the user settings + userIDs = [[[SOGoUser userSettingsCache] allKeys] objectEnumerator]; + count = 0; + while ((currentID = [userIDs nextObject])) + { + currentEntry = [[SOGoUser userSettingsCache] objectForKey: currentID]; + + if ([now earlierDate: [currentEntry objectForKey: @"cleanupDate"]] == now) + { + [SOGoUser setUserDefaultsFromDictionary: nil user: currentID]; + count++; + } + } + + if (count) + [self logWithFormat: @"cleaned %d users records from user settings cache", count]; } @end diff --git a/SoObjects/SOGo/SOGoUser.h b/SoObjects/SOGo/SOGoUser.h index a453a73a6..b180af1ca 100644 --- a/SoObjects/SOGo/SOGoUser.h +++ b/SoObjects/SOGo/SOGoUser.h @@ -112,10 +112,12 @@ extern NSString *SOGoWeekStartFirstFullWeek; + (void) setUserDefaultsFromDictionary: (NSDictionary *) theDictionary user: (NSString *) login; ++ (NSDictionary *) userDefaultsCache; - (NSUserDefaults *) userDefaults; + (void) setUserSettingsFromDictionary: (NSDictionary *) theDictionary user: (NSString *) login; ++ (NSDictionary *) userSettingsCache; - (NSUserDefaults *) userSettings; - (NSString *) language; diff --git a/SoObjects/SOGo/SOGoUser.m b/SoObjects/SOGo/SOGoUser.m index 9053c1f33..2b6784b63 100644 --- a/SoObjects/SOGo/SOGoUser.m +++ b/SoObjects/SOGo/SOGoUser.m @@ -472,10 +472,26 @@ _timeValue (NSString *key) } /* defaults */ -- (void) setUserDefaultsFromDictionary: (NSDictionary *) theDictionary ++ (void) setUserDefaultsFromDictionary: (NSDictionary *) theDictionary user: (NSString *) login { - [userDefaults setObject: theDictionary forKey: login]; + if (!theDictionary) + [userDefaults removeObjectForKey: login]; + else + { + NSDate *cleanupDate; + + cleanupDate = [[NSDate date] addTimeInterval: [SOGoCache cleanupInterval]]; + [userDefaults setObject: [NSDictionary dictionaryWithObjectsAndKeys: + theDictionary, @"dictionary", + cleanupDate, @"cleanupDate", nil] + forKey: login]; + } +} + ++ (NSDictionary *) userDefaultsCache +{ + return userDefaults; } - (NSUserDefaults *) userDefaults @@ -484,7 +500,7 @@ _timeValue (NSString *key) o = [userDefaults objectForKey: login]; if (!o) - { + { o = [[SOGoUserDefaults alloc] initWithTableURL: SOGoProfileURL uid: login fieldName: @"c_defaults"]; @@ -498,16 +514,34 @@ _timeValue (NSString *key) if (![[o stringForKey: @"MessageCheck"] length]) [o setObject: defaultMessageCheck forKey: @"MessageCheck"]; - [userDefaults setObject: o forKey: login]; + [SOGoUser setUserDefaultsFromDictionary: o user: login]; + + return o; } - return o; + return [o objectForKey: @"dictionary"]; } + (void) setUserSettingsFromDictionary: (NSDictionary *) theDictionary user: (NSString *) login { - [userSettings setObject: theDictionary forKey: login]; + if (!theDictionary) + [userSettings removeObjectForKey: login]; + else + { + NSDate *cleanupDate; + + cleanupDate = [[NSDate date] addTimeInterval: [SOGoCache cleanupInterval]]; + [userSettings setObject: [NSDictionary dictionaryWithObjectsAndKeys: + theDictionary, @"dictionary", + cleanupDate, @"cleanupDate", nil] + forKey: login]; + } +} + ++ (NSDictionary *) userSettingsCache +{ + return userDefaults; } - (NSUserDefaults *) userSettings @@ -517,14 +551,16 @@ _timeValue (NSString *key) o = [userSettings objectForKey: login]; if (!o) - { + { o = [[SOGoUserDefaults alloc] initWithTableURL: SOGoProfileURL uid: login fieldName: @"c_settings"]; - [userSettings setObject: o forKey: login]; + [SOGoUser setUserSettingsFromDictionary: o user: login]; + + return o; } - return o; + return [o objectForKey: @"dictionary"]; } - (NSString *) language