From d9978041021c0bc85622f687943aebc0f8115c58 Mon Sep 17 00:00:00 2001 From: helge Date: Thu, 4 Aug 2005 01:50:28 +0000 Subject: [PATCH] added some key/value archiving git-svn-id: http://svn.opengroupware.org/SOPE/trunk@966 e4a50df8-12e2-0310-a44c-efbce7f8a7e3 --- sope-core/EOCoreData/ChangeLog | 5 + sope-core/EOCoreData/NSPredicate+EO.h | 5 + sope-core/EOCoreData/NSPredicate+EO.m | 154 ++++++++++++++++++++++++++ sope-core/EOCoreData/Version | 2 +- 4 files changed, 165 insertions(+), 1 deletion(-) diff --git a/sope-core/EOCoreData/ChangeLog b/sope-core/EOCoreData/ChangeLog index d11fd545..1bd10581 100644 --- a/sope-core/EOCoreData/ChangeLog +++ b/sope-core/EOCoreData/ChangeLog @@ -1,3 +1,8 @@ +2005-08-04 Helge Hess + + * NSPredicate+EO.m: added EOKeyValueArchiving to NSComparisonPredicate + (v4.5.3) + 2005-08-03 Helge Hess * v4.5.2 diff --git a/sope-core/EOCoreData/NSPredicate+EO.h b/sope-core/EOCoreData/NSPredicate+EO.h index 56c31a8b..c6720ec9 100644 --- a/sope-core/EOCoreData/NSPredicate+EO.h +++ b/sope-core/EOCoreData/NSPredicate+EO.h @@ -23,6 +23,8 @@ #define __NSPredicate_EO_H__ #import +#import +#include /* NSPredicate(EO) @@ -41,4 +43,7 @@ @end +@interface NSComparisonPredicate(EOCoreData) < EOKeyValueArchiving > +@end + #endif /* __NSPredicate_EO_H__ */ diff --git a/sope-core/EOCoreData/NSPredicate+EO.m b/sope-core/EOCoreData/NSPredicate+EO.m index 6485c2bf..4d9ecbe2 100644 --- a/sope-core/EOCoreData/NSPredicate+EO.m +++ b/sope-core/EOCoreData/NSPredicate+EO.m @@ -130,4 +130,158 @@ return (id)self; } +/* key/value archiving */ + +- (id)initWithKeyValueUnarchiver:(EOKeyValueUnarchiver *)_unarchiver { + int opt; + NSPredicateOperatorType ptype; + NSComparisonPredicateModifier mod; + NSExpression *left, *right; + NSString *selName, *s; + + /* left / right - TODO: need to check 'official' keys fo rthat */ + + left = [_unarchiver decodeObjectForKey:@"left"]; + if (left != nil && ![left isKindOfClass:[NSExpression class]]) + left = [NSExpression expressionForConstantValue:left]; + + right = [_unarchiver decodeObjectForKey:@"right"]; + if (right != nil && ![right isKindOfClass:[NSExpression class]]) + right = [NSExpression expressionForConstantValue:right]; + + /* custom selector */ + + selName = [_unarchiver decodeObjectForKey:@"selector"]; + if ([selName length] > 0) { + return [self initWithLeftExpression:left rightExpression:right + customSelector:NSSelectorFromString(selName)]; + } + + /* modifier */ + + if ((s = [_unarchiver decodeObjectForKey:@"modifier"]) != nil) { + if ([s isEqualToString:@"direct"]) mod = NSDirectPredicateModifier; + else if ([s isEqualToString:@"all"]) mod = NSAllPredicateModifier; + else if ([s isEqualToString:@"any"]) mod = NSAnyPredicateModifier; + else { + NSLog(@"WARNING(%s): could not decode modifier (trying int): %@!", + __PRETTY_FUNCTION__, s); + mod = [s intValue]; + } + } + else + mod = NSDirectPredicateModifier; + + /* type */ + + if ((s = [_unarchiver decodeObjectForKey:@"type"]) != nil) { + if ([s isEqualToString:@"<"]) + ptype = NSLessThanPredicateOperatorType; + else if ([s isEqualToString:@"=<"]) + ptype = NSLessThanOrEqualToPredicateOperatorType; + else if ([s isEqualToString:@">"]) + ptype = NSGreaterThanPredicateOperatorType; + else if ([s isEqualToString:@">="]) + ptype = NSGreaterThanOrEqualToPredicateOperatorType; + else if ([s isEqualToString:@"=="]) + ptype = NSEqualToPredicateOperatorType; + else if ([s isEqualToString:@"!="]) + ptype = NSNotEqualToPredicateOperatorType; + else if ([s isEqualToString:@"like"]) + ptype = NSLikePredicateOperatorType; + else if ([s isEqualToString:@"contains"]) + ptype = NSInPredicateOperatorType; + else if ([s isEqualToString:@"beginswith"]) + ptype = NSBeginsWithPredicateOperatorType; + else if ([s isEqualToString:@"endswith"]) + ptype = NSEndsWithPredicateOperatorType; + else if ([s isEqualToString:@"matches"]) + ptype = NSMatchesPredicateOperatorType; + else { + NSLog(@"WARNING(%s): could not decode type (trying int): %@!", + __PRETTY_FUNCTION__, s); + ptype = [s intValue]; + } + } + else + ptype = NSEqualToPredicateOperatorType; + + /* options */ + + // TODO: use bit-compare and a set? + if ((s = [_unarchiver decodeObjectForKey:@"options"]) != nil) { + if ([s isEqualToString:@"caseInsensitive"]) + opt = NSCaseInsensitivePredicateOption; + else if ([s isEqualToString:@"diacritic"]) + opt = NSDiacriticInsensitivePredicateOption; + else { + NSLog(@"WARNING(%s): could not decode options (trying int): %@!", + __PRETTY_FUNCTION__, s); + opt = [s intValue]; + } + } + else + opt = 0; + + /* create and return */ + + return [self initWithLeftExpression:left rightExpression:right + modifier:mod type:ptype options:opt]; +} + +- (void)encodeWithKeyValueArchiver:(EOKeyValueArchiver *)_archiver { + NSString *s; + + [_archiver encodeObject:[self leftExpression] forKey:@"left"]; + [_archiver encodeObject:[self rightExpression] forKey:@"right"]; + + /* type */ + + switch ([self predicateOperatorType]) { + case NSCustomSelectorPredicateOperatorType: + [_archiver encodeObject:NSStringFromSelector([self customSelector]) + forKey:@"selector"]; + return; /* no more info */ + + case NSLessThanPredicateOperatorType: s = @"<"; break; + case NSLessThanOrEqualToPredicateOperatorType: s = @"=<"; break; + case NSGreaterThanPredicateOperatorType: s = @">"; break; + case NSGreaterThanOrEqualToPredicateOperatorType: s = @">="; break; + case NSEqualToPredicateOperatorType: s = @"=="; break; + case NSNotEqualToPredicateOperatorType: s = @"!="; break; + case NSLikePredicateOperatorType: s = @"like"; break; + case NSInPredicateOperatorType: s = @"contains"; break; + case NSBeginsWithPredicateOperatorType: s = @"beginswith"; break; + case NSEndsWithPredicateOperatorType: s = @"endswith"; break; + case NSMatchesPredicateOperatorType: s = @"matches"; break; + + default: + s = [NSString stringWithFormat:@"%i", [self predicateOperatorType]]; + break; + } + if (s != nil) [_archiver encodeObject:s forKey:@"type"]; + + /* modifier */ + + switch ([self comparisonPredicateModifier]) { + case NSDirectPredicateModifier: s = nil; break; + case NSAllPredicateModifier: s = @"all"; break; + case NSAnyPredicateModifier: s = @"any"; break; + default: + s = [NSString stringWithFormat:@"%i", + [self comparisonPredicateModifier]]; + break; + } + if (s != nil) [_archiver encodeObject:s forKey:@"modifier"]; + + /* options */ + + // TODO: use bit-compare and a set? + + if ([self options] == NSCaseInsensitivePredicateOption) + [_archiver encodeObject:@"caseInsensitive" forKey:@"options"]; + else if ([self options] == NSDiacriticInsensitivePredicateOption) + [_archiver encodeObject:@"diacritic" forKey:@"options"]; +} + @end /* NSComparisonPredicate(EOCoreData) */ diff --git a/sope-core/EOCoreData/Version b/sope-core/EOCoreData/Version index ed045adf..81d0f5a4 100644 --- a/sope-core/EOCoreData/Version +++ b/sope-core/EOCoreData/Version @@ -1,3 +1,3 @@ # version file -SUBMINOR_VERSION:=2 +SUBMINOR_VERSION:=3 -- 2.39.5