+2005-08-04 Helge Hess <helge.hess@opengroupware.org>
+
+ * NGRuleEngine.subproj/NGRuleModel.m: added EOKeyValueArchiving, added
+ method to load from such an archive, added -addRules: to add a set
+ of rules (v4.5.170)
+
2005-08-04 Helge Hess <helge.hess@skyrix.com>
* NGRuleEngine.subproj/NGRuleContext.m: added new method
#define __NGRuleEngine_NGRuleModel_H__
#import <Foundation/NSObject.h>
+#include <EOControl/EOKeyValueArchiver.h>
/*
NGRuleModel
// TODO: need some method to join two models (two allow one model being
// configured as a default but still have fallback rules in another)
-@class NSArray, NSMutableArray;
+@class NSArray, NSMutableArray, NSURL;
@class NGRule;
-@interface NGRuleModel : NSObject
+@interface NGRuleModel : NSObject < EOKeyValueArchiving >
{
NSMutableArray *rules;
}
- (id)initWithPropertyList:(id)_plist;
- (id)initWithContentsOfFile:(NSString *)_path;
- (id)initWithContentsOfUserDefault:(NSString *)_defaultName;
+- (id)initWithKeyValueArchiveAtURL:(NSURL *)_url;
/* accessors */
- (NSArray *)rules;
- (void)addRule:(NGRule *)_rule;
- (void)removeRule:(NGRule *)_rule;
+- (void)addRules:(NSArray *)_rules;
/* operations */
#include "NGRule.h"
#include "NGRuleParser.h"
#include "EOTrueQualifier.h"
+#include <NGExtensions/NSObject+Logs.h>
#include <EOControl/EOControl.h>
#include "common.h"
return [[[self class] ruleModelWithContentsOfUserDefault:_defName] retain];
}
+- (id)initWithKeyValueArchiveAtURL:(NSURL *)_url {
+ EOKeyValueUnarchiver *unarchiver;
+ NSDictionary *plist;
+
+ if ((plist = [NSDictionary dictionaryWithContentsOfURL:_url]) == nil) {
+ [self errorWithFormat:@"Could not read plist at URL: %@", _url];
+ [self release];
+ return nil;
+ }
+
+ unarchiver = [[EOKeyValueUnarchiver alloc] initWithDictionary:plist];
+ self = [self initWithKeyValueUnarchiver:unarchiver];
+ [unarchiver release]; unarchiver = nil;
+
+ return self;
+}
+
- (void)dealloc {
[self->rules release];
[super dealloc];
- (void)setRules:(NSArray *)_rules {
[self->rules removeAllObjects];
- [self->rules addObjectsFromArray:_rules];
+ if (_rules != nil) [self->rules addObjectsFromArray:_rules];
}
- (NSArray *)rules {
return [[self->rules shallowCopy] autorelease];
}
- (void)addRule:(NGRule *)_rule {
+ if (_rule == nil) return;
[self->rules addObject:_rule];
}
- (void)removeRule:(NGRule *)_rule {
+ if (_rule == nil) return;
[self->rules removeObject:_rule];
}
+- (void)addRules:(NSArray *)_rules {
+ if (_rules != nil) [self->rules addObjectsFromArray:_rules];
+}
+
/* operations */
static int candidateSort(NGRule *rule1, NGRule *rule2, NGRuleModel *model) {
/* representations */
+/* key/value archiving */
+
+- (id)initWithKeyValueUnarchiver:(EOKeyValueUnarchiver *)_unarchiver {
+ return [self initWithRules:[_unarchiver decodeObjectForKey:@"rules"]];
+}
+- (void)encodeWithKeyValueArchiver:(EOKeyValueArchiver *)_archiver {
+ [_archiver encodeObject:[self rules] forKey:@"rules"];
+}
+
@end /* NGRuleModel */