Dropped the SOGoUserManager class and moved its code to SOGoCache. Cleaned up the code a little bit.

Monotone-Parent: f570fda8096334f1267b8f01d59732c4fa9943c6
Monotone-Revision: b338f502fe70efea6d0d06d3f5a745163d1dbaf7

Monotone-Author: ludovic@Sophos.ca
Monotone-Date: 2008-11-11T00:17:35
Monotone-Branch: ca.inverse.sogo
maint-2.0.2
Ludovic Marcotte 2008-11-11 00:17:35 +00:00
parent be4e239537
commit ff3e1fbb42
18 changed files with 158 additions and 296 deletions

View File

@ -418,7 +418,6 @@ static BOOL debugObjectAllocation = NO;
startDate = [NSDate date];
}
// sleep (1);
cache = [SOGoCache sharedCache];
resp = [super dispatchRequest: _request];
[SOGoCache killCache];

View File

@ -63,7 +63,6 @@
#import <SOGo/NSDictionary+Utilities.h>
#import <SOGo/SOGoPermissions.h>
#import <SOGo/SOGoUser.h>
#import <SOGo/SOGoUserManager.h>
#import <SOGo/SOGoUserFolder.h>
#import <SOGo/SOGoWebDAVAclManager.h>
@ -1627,7 +1626,9 @@ static Class sogoAppointmentFolderKlass = Nil;
{
record = [records objectAtIndex: count];
recordURL = [cnames objectForKey: [record objectForKey: @"c_name"]];
[components setObject: record forKey: recordURL];
if (recordURL)
[components setObject: record forKey: recordURL];
}
return components;

View File

@ -40,7 +40,6 @@
#import <SoObjects/SOGo/SOGoObject.h>
#import <SoObjects/SOGo/SOGoPermissions.h>
#import <SoObjects/SOGo/SOGoUser.h>
#import <SoObjects/SOGo/SOGoUserManager.h>
#import <SoObjects/SOGo/SOGoWebDAVAclManager.h>
#import <SoObjects/SOGo/SOGoWebDAVValue.h>
#import <SoObjects/SOGo/WORequest+SOGo.h>

View File

@ -46,7 +46,6 @@
#import <SoObjects/SOGo/SOGoMailer.h>
#import <SoObjects/SOGo/SOGoPermissions.h>
#import <SoObjects/SOGo/SOGoUser.h>
#import <SoObjects/SOGo/SOGoUserManager.h>
#import <SoObjects/SOGo/WORequest+SOGo.h>
#import <SoObjects/Appointments/SOGoAppointmentFolder.h>

View File

@ -31,7 +31,6 @@
#import <SaxObjC/XMLNamespaces.h>
#import <SOGo/SOGoUser.h>
#import <SOGo/SOGoUserManager.h>
#import <SOGo/NSObject+DAV.h>
#import <SOGo/NSString+DAV.h>

View File

@ -53,7 +53,6 @@ SOGo_HEADER_FILES = \
SOGoWebDAVValue.h \
SOGoMailer.h \
SOGoUser.h \
SOGoUserManager.h \
\
NSDictionary+BSJSONAdditions.h \
NSScanner+BSJSONAdditions.h \
@ -99,7 +98,6 @@ SOGo_OBJC_FILES = \
SOGoWebDAVValue.m \
SOGoMailer.m \
SOGoUser.m \
SOGoUserManager.m \
\
NSDictionary+BSJSONAdditions.m \
NSScanner+BSJSONAdditions.m \

View File

@ -3,6 +3,7 @@
* Copyright (C) 2008 Inverse inc.
*
* Author: Wolfgang Sourdeau <wsourdeau@inverse.ca>
* Ludovic Marcotte <lmarcotte@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
@ -27,6 +28,7 @@
@class NSMutableDictionary;
@class NSString;
@class NSTimer;
@class SOGoObject;
@class SOGoUser;
@ -35,6 +37,7 @@
{
NSMutableDictionary *cache;
NSMutableDictionary *users;
NSTimer *_cleanupTimer;
}
+ (SOGoCache *) sharedCache;

View File

@ -3,6 +3,7 @@
* Copyright (C) 2008 Inverse inc.
*
* Author: Wolfgang Sourdeau <wsourdeau@inverse.ca>
* Ludovic Marcotte <lmarcotte@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
@ -22,15 +23,24 @@
#import <Foundation/NSArray.h>
#import <Foundation/NSDictionary.h>
#import <Foundation/NSDistributedNotificationCenter.h>
#import <Foundation/NSEnumerator.h>
#import <Foundation/NSString.h>
#import <Foundation/NSTimer.h>
#import <Foundation/NSUserDefaults.h>
#import <NGObjWeb/SoObject.h>
#import <NGExtensions/NSObject+Logs.h>
#import "SOGoObject.h"
#import "SOGoUser.h"
#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.
static NSTimeInterval cleanupInterval = 1800;
static SOGoCache *sharedCache = nil;
@implementation SOGoCache
@ -53,8 +63,38 @@ static SOGoCache *sharedCache = nil;
{
if ((self = [super init]))
{
NSString *cleanupSetting;
cache = [NSMutableDictionary new];
users = [NSMutableDictionary new];
// We register ourself for notifications
[[NSDistributedNotificationCenter defaultCenter]
addObserver: self
selector: @selector(_userDefaultsHaveChanged:)
name: @"SOGoUserDefaultsHaveChanged"
object: nil];
[[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];
[self logWithFormat: @"cleanup interval set every %f seconds",
cleanupInterval];
}
return self;
@ -128,16 +168,75 @@ static SOGoCache *sharedCache = nil;
- (void) registerUser: (SOGoUser *) user
{
// NSLog (@"registerUser: %@", user);
[users setObject: user forKey: [user login]];
NSDate *cleanupDate;
cleanupDate = [[NSDate date] addTimeInterval: cleanupInterval];
[users setObject: [NSMutableDictionary dictionaryWithObjectsAndKeys: user, @"user", cleanupDate, @"cleanupDate", nil]
forKey: [user login]];
}
- (id) userNamed: (NSString *) name
{
// NSLog (@"userNamed: %@", name);
return [[users objectForKey: name] objectForKey: @"user"];
}
return [users objectForKey: name];
- (void) _userDefaultsHaveChanged: (NSNotification *) theNotification
{
NSMutableDictionary *d;
NSString *user;
d = [[NSMutableDictionary alloc] init];
[d addEntriesFromDictionary: [theNotification userInfo]];
user = [d objectForKey: @"uid"];
[d removeObjectForKey: @"uid"];
[SOGoUser setUserDefaultsFromDictionary: d user: user];
[d release];
}
- (void) _userSettingsHaveChanged: (NSNotification *) theNotification
{
NSMutableDictionary *d;
NSString *user;
d = [[NSMutableDictionary alloc] init];
[d addEntriesFromDictionary: [theNotification userInfo]];
user = [d objectForKey: @"uid"];
[d removeObjectForKey: @"uid"];
[SOGoUser setUserSettingsFromDictionary: d user: user];
[d release];
}
- (void) _cleanupSources
{
NSEnumerator *userIDs;
NSString *currentID;
NSDictionary *currentUser;
NSDate *now;
unsigned int count;
now = [NSDate date];
count = 0;
userIDs = [[users allKeys] objectEnumerator];
while ((currentID = [userIDs nextObject]))
{
currentUser = [users objectForKey: currentID];
if ([now earlierDate:
[currentUser objectForKey: @"cleanupDate"]] == now)
{
[users removeObjectForKey: currentID];
count++;
}
}
if (count)
[self logWithFormat: @"cleaned %d users records from cache", count];
}
@end

View File

@ -32,7 +32,6 @@
#import "LDAPUserManager.h"
#import "SOGoPermissions.h"
#import "SOGoUser.h"
#import "SOGoUserManager.h"
#import "SOGoDAVAuthenticator.h"

View File

@ -60,7 +60,6 @@
#import "SOGoParentFolder.h"
#import "SOGoPermissions.h"
#import "SOGoUser.h"
#import "SOGoUserManager.h"
#import "SOGoWebDAVAclManager.h"
#import "WORequest+SOGo.h"

View File

@ -61,7 +61,6 @@
#import "SOGoCache.h"
#import "SOGoPermissions.h"
#import "SOGoUser.h"
#import "SOGoUserManager.h"
#import "SOGoUserFolder.h"
#import "SOGoWebDAVAclManager.h"
#import "SOGoWebDAVValue.h"

View File

@ -63,8 +63,6 @@ extern NSString *SOGoWeekStartFirstFullWeek;
@interface SOGoUser : SoUser
{
SOGoUserDefaults *userDefaults;
SOGoUserDefaults *userSettings;
NSArray *allEmails;
NSString *language;
NSString *currentPassword;
@ -112,10 +110,12 @@ extern NSString *SOGoWeekStartFirstFullWeek;
/* defaults */
- (void) setUserDefaultsFromDictionary: (NSDictionary *) theDictionary;
+ (void) setUserDefaultsFromDictionary: (NSDictionary *) theDictionary
user: (NSString *) login;
- (NSUserDefaults *) userDefaults;
- (void) setUserSettingsFromDictionary: (NSDictionary *) theDictionary;
+ (void) setUserSettingsFromDictionary: (NSDictionary *) theDictionary
user: (NSString *) login;
- (NSUserDefaults *) userSettings;
- (NSString *) language;

View File

@ -46,6 +46,9 @@
#import "SOGoUser.h"
static NSMutableDictionary *userDefaults;
static NSMutableDictionary *userSettings;
static NSTimeZone *serverTimeZone = nil;
static NSString *fallbackIMAP4Server = nil;
static BOOL fallbackIsConfigured = NO;
@ -192,6 +195,9 @@ _timeValue (NSString *key)
// acceptAnyUser = ([[ud stringForKey: @"SOGoAuthentificationMethod"]
// isEqualToString: @"bypass"]);
userDefaults = [[NSMutableDictionary alloc] init];
userSettings = [[NSMutableDictionary alloc] init];
}
+ (NSString *) language
@ -260,8 +266,6 @@ _timeValue (NSString *key)
{
if ((self = [super initWithLogin: realUID roles: newRoles]))
{
userDefaults = nil;
userSettings = nil;
allEmails = nil;
language = nil;
currentPassword = nil;
@ -283,8 +287,6 @@ _timeValue (NSString *key)
- (void) dealloc
{
[userDefaults release];
[userSettings release];
[allEmails release];
[language release];
[currentPassword release];
@ -413,8 +415,8 @@ _timeValue (NSString *key)
doSave = NO;
}
if (doSave)
[userDefaults setObject: [self mailAccounts]
forKey: @"MailAccounts"];
[[self userDefaults] setObject: [self mailAccounts]
forKey: @"MailAccounts"];
else
[self logWithFormat: @"saving mail accounts is disabled until the"
@" variable(s) mentionned above are configured"];
@ -471,54 +473,58 @@ _timeValue (NSString *key)
/* defaults */
- (void) setUserDefaultsFromDictionary: (NSDictionary *) theDictionary
user: (NSString *) login
{
if (!userDefaults)
userDefaults = [[SOGoUserDefaults alloc] initWithTableURL: SOGoProfileURL
uid: login
fieldName: @"c_defaults"];
[userDefaults setValues: theDictionary];
[userDefaults setObject: theDictionary forKey: login];
}
- (NSUserDefaults *) userDefaults
{
if (!userDefaults)
{
userDefaults
= [[SOGoUserDefaults alloc] initWithTableURL: SOGoProfileURL
id o;
o = [userDefaults objectForKey: login];
if (!o)
{
o = [[SOGoUserDefaults alloc] initWithTableURL: SOGoProfileURL
uid: login
fieldName: @"c_defaults"];
/* Required parameters for the web interface */
if (![[userDefaults stringForKey: @"ReplyPlacement"] length])
[userDefaults setObject: defaultReplyPlacement forKey: @"ReplyPlacement"];
if (![[userDefaults stringForKey: @"SignaturePlacement"] length])
[userDefaults setObject: defaultSignaturePlacement forKey: @"SignaturePlacement"];
if (![[userDefaults stringForKey: @"MessageForwarding"] length])
[userDefaults setObject: defaultMessageForwarding forKey: @"MessageForwarding"];
if (![[userDefaults stringForKey: @"MessageCheck"] length])
[userDefaults setObject: defaultMessageCheck forKey: @"MessageCheck"];
if (![[o stringForKey: @"ReplyPlacement"] length])
[o setObject: defaultReplyPlacement forKey: @"ReplyPlacement"];
if (![[o stringForKey: @"SignaturePlacement"] length])
[o setObject: defaultSignaturePlacement forKey: @"SignaturePlacement"];
if (![[o stringForKey: @"MessageForwarding"] length])
[o setObject: defaultMessageForwarding forKey: @"MessageForwarding"];
if (![[o stringForKey: @"MessageCheck"] length])
[o setObject: defaultMessageCheck forKey: @"MessageCheck"];
[userDefaults setObject: o forKey: login];
}
return userDefaults;
return o;
}
- (void) setUserSettingsFromDictionary: (NSDictionary *) theDictionary
+ (void) setUserSettingsFromDictionary: (NSDictionary *) theDictionary
user: (NSString *) login
{
if (!userSettings)
userSettings = [[SOGoUserDefaults alloc] initWithTableURL: SOGoProfileURL
uid: login
fieldName: @"c_settings"];
[userSettings setValues: theDictionary];
[userSettings setObject: theDictionary forKey: login];
}
- (NSUserDefaults *) userSettings
{
if (!userSettings)
userSettings
= [[SOGoUserDefaults alloc] initWithTableURL: SOGoProfileURL
uid: login
fieldName: @"c_settings"];
id o;
o = [userSettings objectForKey: login];
return userSettings;
if (!o)
{
o = [[SOGoUserDefaults alloc] initWithTableURL: SOGoProfileURL
uid: login
fieldName: @"c_settings"];
[userSettings setObject: o forKey: login];
}
return o;
}
- (NSString *) language
@ -626,7 +632,7 @@ _timeValue (NSString *key)
NSCalendarDate *januaryFirst, *firstWeek;
unsigned int dayOfWeek;
firstWeekRule = [userDefaults objectForKey: @"FirstWeek"];
firstWeekRule = [[self userDefaults] objectForKey: @"FirstWeek"];
januaryFirst = [NSCalendarDate dateWithYear: [date yearOfCommonEra]
month: 1 day: 1 hour: 0 minute: 0 second: 0

View File

@ -275,7 +275,7 @@ static NSString *uidColumnName = @"c_uid";
[d addEntriesFromDictionary: values];
[d setObject: uid forKey: @"uid"];
#warning reenable when the code to use the SOGoUserManager is finished
#warning reenable when the code to use the SOGoCache is finished
#if 0
[[NSDistributedNotificationCenter defaultCenter]
postNotificationName: ([fieldName isEqualToString: @"c_defaults"] ? @"SOGoUserDefaultsHaveChanged" : @"SOGoUserSettingsHaveChanged")

View File

@ -52,7 +52,6 @@
#import "LDAPUserManager.h"
#import "SOGoPermissions.h"
#import "SOGoUser.h"
#import "SOGoUserManager.h"
#import "SOGoUserFolder.h"

View File

@ -1,47 +0,0 @@
/* SOGoUserManager.h - this file is part of SOGo
*
* Copyright (C) 2008 Inverse inc.
*
* Author: Ludovic Marcotte <lmarcotte@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 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.
*/
#ifndef SOGOUSERMANAGER_H
#define SOGOUSERMANAGER_H
#import <Foundation/NSObject.h>
@class NSArray;
@class NSMutableDictionary;
@class NSString;
@class NSTimer;
@class SOGoUser;
@interface SOGoUserManager : NSObject
{
@private
NSMutableDictionary *_cache;
NSTimer *_cleanupTimer;
}
+ (id) sharedManager;
- (SOGoUser *) userWithLogin: (NSString *) theLogin
roles: (NSArray *) theRoles;
@end
#endif

View File

@ -1,189 +0,0 @@
/* SOGoUserManager.m - this file is part of SOGo
*
* Copyright (C) 2008 Inverse inc.
*
* Author: Ludovic Marcotte <lmarcotte@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 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.
*/
#include "SOGoUserManager.h"
#include <Foundation/NSDictionary.h>
#include <Foundation/NSDistributedNotificationCenter.h>
#include <Foundation/NSEnumerator.h>
#include <Foundation/NSString.h>
#include <Foundation/NSTimer.h>
#include <Foundation/NSUserDefaults.h>
#import <NGExtensions/NSObject+Logs.h>
#include "SOGoUser.h"
// We define the default value for cleaning up cached
// users' preferences. This value should be relatively high
// to avoid useless database calls.
static NSTimeInterval cleanupInterval = 1800;
@implementation SOGoUserManager
- (id) init
{
NSString *cleanupSetting;
if ((self = [super init]))
{
_cache = [[NSMutableDictionary alloc] init];
}
// We register ourself for notifications
[[NSDistributedNotificationCenter defaultCenter]
addObserver: self
selector: @selector(_userDefaultsHaveChanged:)
name: @"SOGoUserDefaultsHaveChanged"
object: nil];
[[NSDistributedNotificationCenter defaultCenter]
addObserver: self
selector: @selector(_userSettingsHaveChanged:)
name: @"SOGoUserSettingsHaveChanged"
object: nil];
// We fire our timer that will cleanup cache entries
cleanupSetting = [[NSUserDefaults standardUserDefaults]
objectForKey: @"SOGoUserManagerCleanupInterval"];
if (cleanupSetting && [cleanupSetting doubleValue] > 0.0)
cleanupInterval = [cleanupSetting doubleValue];
_cleanupTimer = [NSTimer scheduledTimerWithTimeInterval: cleanupInterval
target: self
selector: @selector(_cleanupSources)
userInfo: nil
repeats: YES];
[self logWithFormat: @"cleanup interval set every %f seconds",
cleanupInterval];
return self;
}
- (void) dealloc
{
[_cache release];
[super dealloc];
}
+ (id) sharedManager
{
static id sharedManager = nil;
if (!sharedManager)
sharedManager = [self new];
return sharedManager;
}
- (SOGoUser *) userWithLogin: (NSString *) theLogin
roles: (NSArray *) theRoles
{
NSDate *cleanupDate;
SOGoUser *user;
id entry;
cleanupDate = [[NSDate date] addTimeInterval: cleanupInterval];
entry = [_cache objectForKey: theLogin];
if (!entry)
{
user = [SOGoUser userWithLogin: theLogin roles: theRoles];
entry = [NSMutableDictionary dictionaryWithObjectsAndKeys: user, @"user", cleanupDate, @"cleanupDate", nil];
[_cache setObject: entry forKey: theLogin];
}
else
{
[entry setObject: cleanupDate forKey: @"cleanupDate"];
user = [entry objectForKey: @"user"];
}
[user setPrimaryRoles: theRoles];
return user;
}
//
// Private stuff
//
- (void) _userDefaultsHaveChanged: (NSNotification *) theNotification
{
NSMutableDictionary *d;
SOGoUser *user;
d = [[NSMutableDictionary alloc] init];
[d addEntriesFromDictionary: [theNotification userInfo]];
user = [self userWithLogin: [d objectForKey: @"uid"]
roles: nil];
[d removeObjectForKey: @"uid"];
[user setUserDefaultsFromDictionary: d];
[d release];
}
- (void) _userSettingsHaveChanged: (NSNotification *) theNotification
{
NSMutableDictionary *d;
SOGoUser *user;
d = [[NSMutableDictionary alloc] init];
[d addEntriesFromDictionary: [theNotification userInfo]];
user = [self userWithLogin: [d objectForKey: @"uid"]
roles: nil];
[d removeObjectForKey: @"uid"];
[user setUserSettingsFromDictionary: d];
[d release];
}
- (void) _cleanupSources
{
NSEnumerator *userIDs;
NSString *currentID;
NSDictionary *currentUser;
NSDate *now;
unsigned int count;
now = [NSDate date];
count = 0;
userIDs = [[_cache allKeys] objectEnumerator];
while ((currentID = [userIDs nextObject]))
{
currentUser = [_cache objectForKey: currentID];
if ([now earlierDate:
[currentUser objectForKey: @"cleanupDate"]] == now)
{
[_cache removeObjectForKey: currentID];
count++;
}
}
if (count)
[self logWithFormat: @"cleaned %d users records from cache", count];
}
@end

View File

@ -40,7 +40,6 @@
#import "LDAPUserManager.h"
#import "SOGoPermissions.h"
#import "SOGoUser.h"
#import "SOGoUserManager.h"
#import "SOGoWebAuthenticator.h"