| 1 |
masaki |
120 |
// |
| 2 |
masakih |
348 |
// XspfMRule.m |
| 3 |
masaki |
120 |
// XspfManager |
| 4 |
|
|
// |
| 5 |
|
|
// Created by Hori,Masaki on 09/11/29. |
| 6 |
|
|
// |
| 7 |
|
|
|
| 8 |
masakih |
350 |
/* |
| 9 |
|
|
Copyright (c) 2009-2010, masakih |
| 10 |
|
|
All rights reserved. |
| 11 |
|
|
ソースコード形式かバイナリ形式か、変更するかしないかを問わず、以下の条件を満たす場合に限り、再頒布および使用が許可されます。 |
| 12 |
|
|
|
| 13 |
|
|
1, ソースコードを再頒布する場合、上記の著作権表示、本条件一覧、および下記免責条項を含めること。 |
| 14 |
|
|
2, バイナリ形式で再頒布する場合、頒布物に付属のドキュメント等の資料に、上記の著作権表示、本条件一覧、および下記免責条項を含めること。 |
| 15 |
|
|
3, 書面による特別の許可なしに、本ソフトウェアから派生した製品の宣伝または販売促進に、コントリビューターの名前を使用してはならない。 |
| 16 |
|
|
本ソフトウェアは、著作権者およびコントリビューターによって「現状のまま」提供されており、明示黙示を問わず、商業的な使用可能性、および特定の目的に対する適合性に関する暗黙の保証も含め、またそれに限定されない、いかなる保証もありません。著作権者もコントリビューターも、事由のいかんを問わず、 損害発生の原因いかんを問わず、かつ責任の根拠が契約であるか厳格責任であるか(過失その他の)不法行為であるかを問わず、仮にそのような損害が発生する可能性を知らされていたとしても、本ソフトウェアの使用によって発生した(代替品または代用サービスの調達、使用の喪失、データの喪失、利益の喪失、業務の中断も含め、またそれに限定されない)直接損害、間接損害、偶発的な損害、特別損害、懲罰的損害、または結果損害について、一切責任を負わないものとします。 |
| 17 |
|
|
------------------------------------------------------------------- |
| 18 |
|
|
Copyright (c) 2009-2010, masakih |
| 19 |
|
|
All rights reserved. |
| 20 |
|
|
|
| 21 |
|
|
Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: |
| 22 |
|
|
|
| 23 |
|
|
1, Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. |
| 24 |
|
|
2, Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. |
| 25 |
|
|
3, The names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission. |
| 26 |
|
|
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 27 |
|
|
*/ |
| 28 |
|
|
|
| 29 |
masakih |
353 |
|
| 30 |
masakih |
348 |
#import "XspfMRule.h" |
| 31 |
masaki |
170 |
#import "XspfMRule_private.h" |
| 32 |
masaki |
120 |
|
| 33 |
masaki |
243 |
#import "XspfMLabelField.h" |
| 34 |
|
|
#import "XspfMLabelCell.h" |
| 35 |
masaki |
120 |
|
| 36 |
masaki |
243 |
|
| 37 |
masaki |
152 |
@implementation XspfMRule (XspfMAccessor) |
| 38 |
|
|
- (void)setChildren:(NSArray *)newChildren |
| 39 |
masaki |
120 |
{ |
| 40 |
masaki |
158 |
if(!newChildren) newChildren = [NSMutableArray array]; |
| 41 |
masaki |
152 |
|
| 42 |
|
|
[children autorelease]; |
| 43 |
masaki |
158 |
children = [[NSMutableArray alloc] initWithArray:newChildren copyItems:YES]; |
| 44 |
masaki |
120 |
} |
| 45 |
masaki |
152 |
- (void)addChild:(XspfMRule *)child |
| 46 |
|
|
{ |
| 47 |
|
|
[children addObject:child]; |
| 48 |
|
|
} |
| 49 |
|
|
- (void)setPredicateParts:(NSDictionary *)parts |
| 50 |
|
|
{ |
| 51 |
|
|
[predicateHints autorelease]; |
| 52 |
|
|
predicateHints = [parts mutableCopy]; |
| 53 |
|
|
} |
| 54 |
|
|
- (void)setExpression:(id)expression forKey:(id)key |
| 55 |
|
|
{ |
| 56 |
|
|
[predicateHints setObject:expression forKey:key]; |
| 57 |
|
|
} |
| 58 |
|
|
- (void)setValue:(NSString *)newValue |
| 59 |
|
|
{ |
| 60 |
masaki |
153 |
if([_value isEqualToString:newValue]) return; |
| 61 |
masaki |
152 |
|
| 62 |
masaki |
153 |
[_value autorelease]; |
| 63 |
|
|
_value = [newValue copy]; |
| 64 |
masaki |
152 |
} |
| 65 |
masaki |
153 |
- (NSString *)value { return _value; } |
| 66 |
masaki |
143 |
@end |
| 67 |
masaki |
152 |
|
| 68 |
|
|
@implementation XspfMRule |
| 69 |
|
|
@dynamic value; |
| 70 |
|
|
|
| 71 |
|
|
- (NSInteger)numberOfChildren |
| 72 |
masaki |
120 |
{ |
| 73 |
masaki |
152 |
return [children count]; |
| 74 |
masaki |
120 |
} |
| 75 |
masaki |
152 |
- (id)childAtIndex:(NSInteger)index |
| 76 |
masaki |
120 |
{ |
| 77 |
masaki |
152 |
return [children objectAtIndex:index]; |
| 78 |
|
|
} |
| 79 |
|
|
- (id)displayValueForRuleEditor:(NSRuleEditor *)ruleEditor inRow:(NSInteger)row |
| 80 |
|
|
{ |
| 81 |
masaki |
153 |
return _value; |
| 82 |
masaki |
152 |
} |
| 83 |
|
|
- (NSDictionary *)predicatePartsWithDisplayValue:(id)displayValue forRuleEditor:(NSRuleEditor *)ruleEditor inRow:(NSInteger)row |
| 84 |
|
|
{ |
| 85 |
|
|
id result = [NSMutableDictionary dictionary]; |
| 86 |
masaki |
142 |
|
| 87 |
masaki |
152 |
NSRuleEditorRowType rowType = [ruleEditor rowTypeForRow:row]; |
| 88 |
|
|
if(rowType == NSRuleEditorRowTypeCompound) { |
| 89 |
|
|
return predicateHints; |
| 90 |
|
|
} |
| 91 |
|
|
|
| 92 |
|
|
if([predicateHints valueForKey:@"XspfMIgnoreExpression"]) return nil; |
| 93 |
|
|
|
| 94 |
masaki |
166 |
id operatorType = [predicateHints valueForKey:NSRuleEditorPredicateOperatorType]; |
| 95 |
|
|
id option = [predicateHints valueForKey:NSRuleEditorPredicateOptions]; |
| 96 |
|
|
id leftExp = [predicateHints valueForKey:NSRuleEditorPredicateLeftExpression]; |
| 97 |
|
|
id rightExp = [predicateHints valueForKey:NSRuleEditorPredicateRightExpression]; |
| 98 |
masaki |
152 |
id customRightExp = [predicateHints valueForKey:@"XspfMPredicateRightExpression"]; |
| 99 |
|
|
|
| 100 |
|
|
if(operatorType) { |
| 101 |
|
|
[result setValue:operatorType forKey:NSRuleEditorPredicateOperatorType]; |
| 102 |
|
|
} |
| 103 |
|
|
if(option) { |
| 104 |
|
|
[result setValue:option forKey:NSRuleEditorPredicateOptions]; |
| 105 |
|
|
} |
| 106 |
|
|
if(leftExp) { |
| 107 |
|
|
id exp = nil; |
| 108 |
|
|
if([leftExp isEqual:@"value"]) { |
| 109 |
|
|
exp = [NSExpression expressionForKeyPath:displayValue]; |
| 110 |
|
|
} else { |
| 111 |
|
|
exp = [NSExpression expressionForKeyPath:leftExp]; |
| 112 |
|
|
} |
| 113 |
|
|
if(exp) { |
| 114 |
|
|
[result setValue:exp forKey:NSRuleEditorPredicateLeftExpression]; |
| 115 |
|
|
} |
| 116 |
|
|
} |
| 117 |
|
|
if(rightExp) { |
| 118 |
|
|
SEL selector = NSSelectorFromString(rightExp); |
| 119 |
|
|
id exp = nil; |
| 120 |
|
|
if(selector) { |
| 121 |
|
|
exp = [NSExpression expressionForConstantValue:[displayValue performSelector:selector]]; |
| 122 |
|
|
} else { |
| 123 |
|
|
exp = [NSExpression expressionForConstantValue:rightExp]; |
| 124 |
|
|
} |
| 125 |
|
|
if(exp) { |
| 126 |
|
|
[result setValue:exp forKey:NSRuleEditorPredicateRightExpression]; |
| 127 |
|
|
} |
| 128 |
|
|
} |
| 129 |
|
|
if(customRightExp) { |
| 130 |
|
|
SEL selector = NSSelectorFromString(customRightExp); |
| 131 |
|
|
id arg01 = [predicateHints valueForKey:@"XspfMRightExpressionArg01"]; |
| 132 |
|
|
id arg02 = [predicateHints valueForKey:@"XspfMRightExpressionArg02"]; |
| 133 |
masakih |
348 |
if([arg01 isEqual:@"displayValues"]) { |
| 134 |
|
|
arg01 = [ruleEditor displayValuesForRow:row]; |
| 135 |
|
|
} |
| 136 |
|
|
if([arg02 isEqual:@"displayValues"]) { |
| 137 |
|
|
arg02 = [ruleEditor displayValuesForRow:row]; |
| 138 |
|
|
} |
| 139 |
masaki |
152 |
|
| 140 |
masakih |
348 |
id rhs = nil; |
| 141 |
masaki |
152 |
if(arg02 && arg01) { |
| 142 |
masakih |
348 |
rhs = [self performSelector:selector withObject:arg01 withObject:arg02]; |
| 143 |
masaki |
152 |
} else if(arg01) { |
| 144 |
masakih |
348 |
rhs = [self performSelector:selector withObject:arg01]; |
| 145 |
masaki |
152 |
} else { |
| 146 |
masakih |
348 |
rhs = [self performSelector:selector]; |
| 147 |
|
|
} |
| 148 |
|
|
if(rhs) { |
| 149 |
masaki |
163 |
[result setValue:rhs forKey:NSRuleEditorPredicateRightExpression]; |
| 150 |
masaki |
152 |
} |
| 151 |
|
|
} |
| 152 |
|
|
|
| 153 |
masaki |
163 |
NSString *selName = [predicateHints valueForKey:@"XspfMCustomSelector"]; |
| 154 |
|
|
if(selName) { |
| 155 |
masaki |
154 |
id args = nil; |
| 156 |
|
|
NSString *argSelName = [predicateHints valueForKey:@"XspfMCustomSelectorArgumentsCteator"]; |
| 157 |
|
|
if(argSelName) { |
| 158 |
|
|
SEL argSel = NSSelectorFromString(argSelName); |
| 159 |
|
|
id argSelArg01 = [predicateHints valueForKey:@"XspfMCustomSelectorArgumentsCteatorArg01"]; |
| 160 |
|
|
if([argSelArg01 isEqual:@"displayValues"]) { |
| 161 |
|
|
argSelArg01 = [ruleEditor displayValuesForRow:row]; |
| 162 |
|
|
} |
| 163 |
|
|
id argSelArg02 = [predicateHints valueForKey:@"XspfMCustomSelectorArgumentsCteatorArg02"]; |
| 164 |
|
|
if([argSelArg02 isEqual:@"displayValues"]) { |
| 165 |
|
|
argSelArg02 = [ruleEditor displayValuesForRow:row]; |
| 166 |
|
|
} |
| 167 |
|
|
if(argSelArg02) { |
| 168 |
|
|
args = [self performSelector:argSel withObject:argSelArg01 withObject:argSelArg02]; |
| 169 |
|
|
} else if(argSelArg01) { |
| 170 |
|
|
args = [self performSelector:argSel withObject:argSelArg01]; |
| 171 |
|
|
} else { |
| 172 |
|
|
args = [self performSelector:argSel]; |
| 173 |
|
|
} |
| 174 |
|
|
} else { |
| 175 |
|
|
id arg01 = [predicateHints valueForKey:@"XspfMCustomSelectorArg01"]; |
| 176 |
|
|
args = [NSArray arrayWithObjects:[NSExpression expressionForConstantValue:arg01], nil]; |
| 177 |
|
|
} |
| 178 |
masaki |
152 |
|
| 179 |
masakih |
348 |
id target = [NSExpression expressionForConstantValue:[[self class] functionHost]]; |
| 180 |
masaki |
165 |
id rhs = [NSExpression expressionForFunction:target selectorName:selName arguments:args]; |
| 181 |
masaki |
166 |
[result setValue:rhs forKey:NSRuleEditorPredicateRightExpression]; |
| 182 |
masaki |
152 |
} |
| 183 |
|
|
|
| 184 |
masaki |
187 |
// HMLog(HMLogLevelDebug, @"predicate\tcriterion -> %@, value -> %@, row -> %d, result -> %@", predicateHints, displayValue, row, result); |
| 185 |
masaki |
152 |
|
| 186 |
|
|
return result; |
| 187 |
masaki |
142 |
} |
| 188 |
masaki |
152 |
|
| 189 |
masaki |
153 |
- (id)displayValue { return _value; } |
| 190 |
masaki |
120 |
|
| 191 |
masaki |
338 |
|
| 192 |
|
|
#pragma mark#### Variables for add/change criteria of Library. #### |
| 193 |
masakih |
348 |
static NSArray *leftKeys = nil; |
| 194 |
|
|
static NSArray *stringKeys = nil; |
| 195 |
|
|
static NSArray *dateKeys = nil; |
| 196 |
|
|
static NSArray *numberKeys = nil; |
| 197 |
masakih |
353 |
static NSString *rateingKeyPath = @"rate"; |
| 198 |
|
|
static NSString *labelKeyPath = @"label"; |
| 199 |
masakih |
348 |
static BOOL useRating = NO; |
| 200 |
|
|
static BOOL useLabel = NO; |
| 201 |
|
|
|
| 202 |
|
|
+ (void)registerStringTypeKeyPaths:(NSArray *)keyPaths |
| 203 |
|
|
{ |
| 204 |
|
|
if(stringKeys) { |
| 205 |
|
|
[stringKeys release]; |
| 206 |
|
|
[leftKeys release]; |
| 207 |
|
|
leftKeys = nil; |
| 208 |
|
|
} |
| 209 |
|
|
stringKeys = [[NSArray arrayWithArray:keyPaths] retain]; |
| 210 |
|
|
} |
| 211 |
|
|
+ (void)registerDateTypeKeyPaths:(NSArray *)keyPaths |
| 212 |
|
|
{ |
| 213 |
|
|
if(dateKeys) { |
| 214 |
|
|
[dateKeys release]; |
| 215 |
|
|
[leftKeys release]; |
| 216 |
|
|
leftKeys = nil; |
| 217 |
|
|
} |
| 218 |
|
|
dateKeys = [[NSArray arrayWithArray:keyPaths] retain]; |
| 219 |
|
|
} |
| 220 |
|
|
+ (void)registerNumberTypeKeyPaths:(NSArray *)keyPaths |
| 221 |
|
|
{ |
| 222 |
|
|
if(numberKeys) { |
| 223 |
|
|
[numberKeys release]; |
| 224 |
|
|
[leftKeys release]; |
| 225 |
|
|
leftKeys = nil; |
| 226 |
|
|
} |
| 227 |
|
|
numberKeys = [[NSArray arrayWithArray:keyPaths] retain]; |
| 228 |
|
|
} |
| 229 |
|
|
+ (void)setUseRating:(BOOL)flag |
| 230 |
|
|
{ |
| 231 |
|
|
if(flag && useRating || !flag && !useRating) { |
| 232 |
|
|
[leftKeys release]; |
| 233 |
|
|
leftKeys = nil; |
| 234 |
|
|
} |
| 235 |
|
|
useRating = flag; |
| 236 |
|
|
} |
| 237 |
|
|
+ (void)setUseLablel:(BOOL)flag |
| 238 |
|
|
{ |
| 239 |
|
|
if(flag && useLabel || !flag && !useLabel) { |
| 240 |
|
|
[leftKeys release]; |
| 241 |
|
|
leftKeys = nil; |
| 242 |
|
|
} |
| 243 |
|
|
useLabel = flag; |
| 244 |
|
|
} |
| 245 |
masakih |
353 |
+ (void)setLabelKeyPath:(NSString *)keyPath |
| 246 |
|
|
{ |
| 247 |
|
|
[labelKeyPath release]; |
| 248 |
|
|
labelKeyPath = [keyPath copy]; |
| 249 |
|
|
} |
| 250 |
|
|
|
| 251 |
masaki |
338 |
+ (NSArray *)leftKeys |
| 252 |
|
|
{ |
| 253 |
|
|
if(!leftKeys) { |
| 254 |
masakih |
348 |
|
| 255 |
|
|
id temp = [NSMutableArray array]; |
| 256 |
|
|
[temp addObjectsFromArray:stringKeys]; |
| 257 |
|
|
[temp addObjectsFromArray:dateKeys]; |
| 258 |
|
|
[temp addObjectsFromArray:numberKeys]; |
| 259 |
|
|
if(useRating) { |
| 260 |
masakih |
353 |
[temp addObject:rateingKeyPath]; |
| 261 |
masakih |
348 |
} |
| 262 |
|
|
if(useLabel) { |
| 263 |
masakih |
353 |
[temp addObject:labelKeyPath]; |
| 264 |
masakih |
348 |
} |
| 265 |
|
|
leftKeys = [[NSArray arrayWithArray:temp] retain]; |
| 266 |
masaki |
338 |
} |
| 267 |
|
|
return leftKeys; |
| 268 |
|
|
} |
| 269 |
masakih |
348 |
static inline BOOL isDateKeyPath(NSString *keyPath) |
| 270 |
masaki |
338 |
{ |
| 271 |
masakih |
348 |
return [dateKeys containsObject:keyPath]; |
| 272 |
masaki |
338 |
} |
| 273 |
masakih |
348 |
+ (BOOL)isDateKeyPath:(NSString *)keyPath |
| 274 |
masaki |
338 |
{ |
| 275 |
masakih |
348 |
return isDateKeyPath(keyPath); |
| 276 |
masaki |
338 |
} |
| 277 |
|
|
- (BOOL)isDateKeyPath:(NSString *)keyPath |
| 278 |
|
|
{ |
| 279 |
|
|
return isDateKeyPath(keyPath); |
| 280 |
|
|
} |
| 281 |
masakih |
348 |
static inline BOOL isStringKeyPath(NSString *keyPath) |
| 282 |
|
|
{ |
| 283 |
|
|
return [stringKeys containsObject:keyPath]; |
| 284 |
|
|
} |
| 285 |
|
|
+ (BOOL)isStringKeyPath:(NSString *)keyPath |
| 286 |
|
|
{ |
| 287 |
|
|
return isStringKeyPath(keyPath); |
| 288 |
|
|
} |
| 289 |
masaki |
338 |
- (BOOL)isStringKeyPath:(NSString *)keyPath |
| 290 |
|
|
{ |
| 291 |
masakih |
348 |
return isStringKeyPath(keyPath); |
| 292 |
masaki |
338 |
} |
| 293 |
masakih |
348 |
static inline BOOL isNumberKeyPath(NSString *keyPath) |
| 294 |
|
|
{ |
| 295 |
|
|
return [numberKeys containsObject:keyPath]; |
| 296 |
|
|
} |
| 297 |
|
|
+ (BOOL)isNumberKeyPath:(NSString *)keyPath |
| 298 |
|
|
{ |
| 299 |
|
|
return isNumberKeyPath(keyPath); |
| 300 |
|
|
} |
| 301 |
|
|
- (BOOL)isNumberKeyPath:(NSString *)keyPath |
| 302 |
|
|
{ |
| 303 |
|
|
return isNumberKeyPath(keyPath); |
| 304 |
|
|
} |
| 305 |
|
|
+ (BOOL)isRateKeyPath:(NSString *)keyPath |
| 306 |
|
|
{ |
| 307 |
masakih |
353 |
return [keyPath isEqualToString:rateingKeyPath]; |
| 308 |
masakih |
348 |
} |
| 309 |
masaki |
338 |
- (BOOL)isRateKeyPath:(NSString *)keyPath |
| 310 |
|
|
{ |
| 311 |
masakih |
353 |
return [keyPath isEqualToString:rateingKeyPath]; |
| 312 |
masaki |
338 |
} |
| 313 |
masakih |
348 |
+ (BOOL)isLabelKeyPath:(NSString *)keyPath |
| 314 |
masaki |
338 |
{ |
| 315 |
masakih |
353 |
return [keyPath isEqualToString:labelKeyPath]; |
| 316 |
masaki |
338 |
} |
| 317 |
masakih |
348 |
- (BOOL)isLabelKeyPath:(NSString *)keyPath |
| 318 |
masaki |
338 |
{ |
| 319 |
masakih |
353 |
return [keyPath isEqualToString:labelKeyPath]; |
| 320 |
masaki |
338 |
} |
| 321 |
|
|
|
| 322 |
masaki |
164 |
#pragma mark == NSCopying Protocol == |
| 323 |
masaki |
152 |
- (id)copyWithZone:(NSZone *)zone |
| 324 |
masaki |
142 |
{ |
| 325 |
masaki |
152 |
XspfMRule *result = [[[self class] allocWithZone:zone] init]; |
| 326 |
masaki |
158 |
[result setChildren:children]; |
| 327 |
|
|
[result setPredicateParts:predicateHints]; |
| 328 |
|
|
[result setValue:_value]; |
| 329 |
masaki |
152 |
|
| 330 |
|
|
return result; |
| 331 |
masaki |
142 |
} |
| 332 |
masaki |
152 |
|
| 333 |
masaki |
164 |
#pragma mark == NSCoding Protocol == |
| 334 |
|
|
static NSString *const XspfMRuleChildrenKey = @"XspfMRuleChildrenKey"; |
| 335 |
|
|
static NSString *const XspfMRulePredicateHintsKey = @"XspfMRulePredicateHintsKey"; |
| 336 |
|
|
static NSString *const XspfMRuleValueKey = @"XspfMRuleValueKey"; |
| 337 |
|
|
- (id)initWithCoder:(NSCoder *)decoder |
| 338 |
|
|
{ |
| 339 |
|
|
self = [self init]; |
| 340 |
|
|
|
| 341 |
|
|
[self setChildren:[decoder decodeObjectForKey:XspfMRuleChildrenKey]]; |
| 342 |
|
|
[self setPredicateParts:[decoder decodeObjectForKey:XspfMRulePredicateHintsKey]]; |
| 343 |
|
|
[self setValue:[decoder decodeObjectForKey:XspfMRuleValueKey]]; |
| 344 |
|
|
|
| 345 |
|
|
return self; |
| 346 |
|
|
} |
| 347 |
|
|
- (void)encodeWithCoder:(NSCoder *)encoder |
| 348 |
|
|
{ |
| 349 |
|
|
[encoder encodeObject:children forKey:XspfMRuleChildrenKey]; |
| 350 |
|
|
[encoder encodeObject:predicateHints forKey:XspfMRulePredicateHintsKey]; |
| 351 |
|
|
[encoder encodeObject:_value forKey:XspfMRuleValueKey]; |
| 352 |
|
|
} |
| 353 |
|
|
|
| 354 |
masaki |
152 |
- (BOOL)isEqual:(id)other |
| 355 |
masaki |
120 |
{ |
| 356 |
masaki |
152 |
if([super isEqual:other]) return YES; |
| 357 |
|
|
if(![other isKindOfClass:[XspfMRule class]]) return NO; |
| 358 |
masaki |
120 |
|
| 359 |
masaki |
152 |
XspfMRule *o = other; |
| 360 |
masaki |
153 |
if(![_value isEqualToString:o->_value]) return NO; |
| 361 |
masaki |
152 |
// if(![children isEqual:o->children]) return NO; |
| 362 |
|
|
// if(![predicateHints isEqual:o->predicateHints]) return NO; |
| 363 |
|
|
|
| 364 |
masaki |
120 |
return YES; |
| 365 |
|
|
} |
| 366 |
masaki |
152 |
- (NSUInteger)hash |
| 367 |
|
|
{ |
| 368 |
masaki |
153 |
return _value ? [_value hash] : [super hash]; |
| 369 |
masaki |
152 |
} |
| 370 |
|
|
|
| 371 |
masaki |
120 |
- (id)description |
| 372 |
|
|
{ |
| 373 |
masaki |
152 |
return [NSString stringWithFormat:@"%@ {\n\t%@ = %@;\n\t%@ = %@;\n\t%@ = %@;}", |
| 374 |
|
|
NSStringFromClass([self class]), |
| 375 |
masaki |
153 |
@"value", _value, |
| 376 |
masaki |
152 |
@"hints", predicateHints, |
| 377 |
|
|
@"children", children, |
| 378 |
|
|
nil]; |
| 379 |
masaki |
120 |
} |
| 380 |
|
|
@end |
| 381 |
|
|
|
| 382 |
masaki |
152 |
@implementation XspfMRule (XspfMCreation) |
| 383 |
masaki |
120 |
|
| 384 |
masaki |
152 |
- (id)init |
| 385 |
masaki |
120 |
{ |
| 386 |
masaki |
152 |
[super init]; |
| 387 |
|
|
|
| 388 |
|
|
children = [[NSMutableArray array] retain]; |
| 389 |
|
|
predicateHints = [[NSMutableDictionary dictionary] retain]; |
| 390 |
|
|
|
| 391 |
|
|
return self; |
| 392 |
masaki |
120 |
} |
| 393 |
masaki |
152 |
|
| 394 |
|
|
- (id)initWithValue:(NSString *)newValue children:(NSArray *)newChildren predicateHints:(NSDictionary *)parts |
| 395 |
masaki |
120 |
{ |
| 396 |
masaki |
152 |
[self init]; |
| 397 |
masaki |
120 |
|
| 398 |
masaki |
152 |
if([newValue isEqualToString:@"separator"]) { |
| 399 |
|
|
[self release]; |
| 400 |
|
|
return [[XspfMSeparatorRule alloc] initSparetorRule]; |
| 401 |
|
|
} |
| 402 |
|
|
|
| 403 |
masaki |
170 |
id fieldRule = [XspfMFieldRule fieldRuleWithValue:newValue]; |
| 404 |
|
|
if(fieldRule) { |
| 405 |
masaki |
152 |
[self release]; |
| 406 |
masaki |
170 |
self = [fieldRule retain]; |
| 407 |
masaki |
152 |
} |
| 408 |
|
|
|
| 409 |
|
|
[self setValue:newValue]; |
| 410 |
|
|
[self setChildren:newChildren]; |
| 411 |
|
|
[self setPredicateParts:parts]; |
| 412 |
|
|
|
| 413 |
masaki |
120 |
return self; |
| 414 |
|
|
} |
| 415 |
masaki |
152 |
+ (id)ruleWithValue:(NSString *)newValue children:(NSArray *)newChildren predicateHints:(NSDictionary *)parts |
| 416 |
masaki |
120 |
{ |
| 417 |
masaki |
152 |
return [[[self alloc] initWithValue:newValue children:newChildren predicateHints:parts] autorelease]; |
| 418 |
masaki |
120 |
} |
| 419 |
masaki |
152 |
|
| 420 |
|
|
+ (NSArray *)compoundRule |
| 421 |
masaki |
120 |
{ |
| 422 |
masaki |
152 |
id comp = [self ruleWithValue:@"of the following are true" children:nil predicateHints:[NSDictionary dictionary]]; |
| 423 |
|
|
|
| 424 |
|
|
id allExp = [NSNumber numberWithUnsignedInt:NSAndPredicateType]; |
| 425 |
|
|
id all = [self ruleWithValue:@"All" |
| 426 |
|
|
children:[NSArray arrayWithObject:comp] |
| 427 |
|
|
predicateHints:[NSDictionary dictionaryWithObject:allExp forKey:NSRuleEditorPredicateCompoundType]]; |
| 428 |
|
|
|
| 429 |
|
|
id anyExp = [NSNumber numberWithUnsignedInt:NSOrPredicateType]; |
| 430 |
|
|
id any = [self ruleWithValue:@"Any" |
| 431 |
|
|
children:[NSArray arrayWithObject:comp] |
| 432 |
|
|
predicateHints:[NSDictionary dictionaryWithObject:anyExp forKey:NSRuleEditorPredicateCompoundType]]; |
| 433 |
masakih |
353 |
|
| 434 |
masaki |
152 |
return [NSArray arrayWithObjects:all, any, nil]; |
| 435 |
masaki |
120 |
} |
| 436 |
masaki |
152 |
|
| 437 |
|
|
- (NSDictionary *)predicateHintsWithPlist:(NSDictionary *)plist |
| 438 |
masaki |
120 |
{ |
| 439 |
masaki |
152 |
NSMutableDictionary *result = [NSMutableDictionary dictionaryWithDictionary:plist]; |
| 440 |
|
|
[result removeObjectForKey:@"criteria"]; |
| 441 |
|
|
[result removeObjectForKey:@"value"]; |
| 442 |
|
|
|
| 443 |
|
|
return result; |
| 444 |
masaki |
120 |
} |
| 445 |
masaki |
152 |
|
| 446 |
|
|
+ (id)ruleWithPlist:(id)plist |
| 447 |
masaki |
120 |
{ |
| 448 |
masaki |
152 |
return [[[self alloc] initWithPlist:plist] autorelease]; |
| 449 |
masaki |
120 |
} |
| 450 |
masaki |
152 |
- (id)initWithPlist:(id)plist |
| 451 |
masaki |
120 |
{ |
| 452 |
masaki |
152 |
if(![plist isKindOfClass:[NSDictionary class]]) { |
| 453 |
|
|
[self init]; |
| 454 |
|
|
[self release]; |
| 455 |
|
|
return nil; |
| 456 |
|
|
} |
| 457 |
|
|
|
| 458 |
|
|
id pValue = [plist valueForKey:@"value"]; |
| 459 |
|
|
id criteria = [plist valueForKey:@"criteria"]; |
| 460 |
|
|
id pChildren = [NSMutableArray array]; |
| 461 |
|
|
for(id criterion in criteria) { |
| 462 |
|
|
id c = [[self class] ruleWithPlist:criterion]; |
| 463 |
|
|
if(c) [pChildren addObject:c]; |
| 464 |
|
|
} |
| 465 |
|
|
id hints = [self predicateHintsWithPlist:plist]; |
| 466 |
|
|
|
| 467 |
|
|
return [self initWithValue:pValue children:pChildren predicateHints:hints]; |
| 468 |
masaki |
120 |
} |
| 469 |
masaki |
152 |
|
| 470 |
|
|
- (void)dealloc |
| 471 |
masaki |
120 |
{ |
| 472 |
masaki |
152 |
[children release]; |
| 473 |
|
|
[predicateHints release]; |
| 474 |
masaki |
153 |
[_value release]; |
| 475 |
masaki |
152 |
|
| 476 |
|
|
[super dealloc]; |
| 477 |
masaki |
120 |
} |
| 478 |
masaki |
152 |
|
| 479 |
masaki |
120 |
@end |
| 480 |
|
|
|
| 481 |
masaki |
153 |
@implementation XspfMRule (XspfMPrivate) |
| 482 |
masaki |
142 |
|
| 483 |
masaki |
153 |
- (NSView *)textField |
| 484 |
|
|
{ |
| 485 |
|
|
id text = [[[NSTextField alloc] initWithFrame:NSMakeRect(0,0,100,19)] autorelease]; |
| 486 |
|
|
[[text cell] setControlSize:NSSmallControlSize]; |
| 487 |
|
|
[text setFont:[NSFont controlContentFontOfSize:[NSFont systemFontSizeForControlSize:NSSmallControlSize]]]; |
| 488 |
|
|
[text setStringValue:@"1234567890"]; |
| 489 |
|
|
[text sizeToFit]; |
| 490 |
|
|
[text setStringValue:@""]; |
| 491 |
|
|
|
| 492 |
|
|
return text; |
| 493 |
|
|
} |
| 494 |
|
|
- (NSView *)datePicker |
| 495 |
|
|
{ |
| 496 |
|
|
id date = [[[NSDatePicker alloc] initWithFrame:NSMakeRect(0,0,100,19)] autorelease]; |
| 497 |
|
|
[[date cell] setControlSize:NSSmallControlSize]; |
| 498 |
|
|
[date setFont:[NSFont controlContentFontOfSize:[NSFont systemFontSizeForControlSize:NSSmallControlSize]]]; |
| 499 |
|
|
[date setDatePickerElements:NSYearMonthDayDatePickerElementFlag]; |
| 500 |
|
|
[date setDrawsBackground:YES]; |
| 501 |
|
|
[date setDateValue:[NSDate dateWithTimeIntervalSinceNow:0.0]]; |
| 502 |
|
|
[date sizeToFit]; |
| 503 |
|
|
|
| 504 |
|
|
return date; |
| 505 |
|
|
} |
| 506 |
|
|
- (NSView *)ratingIndicator |
| 507 |
|
|
{ |
| 508 |
|
|
id rate = [[[NSLevelIndicator alloc] initWithFrame:NSMakeRect(0,0,100,19)] autorelease]; |
| 509 |
|
|
id cell = [rate cell]; |
| 510 |
|
|
[cell setControlSize:NSSmallControlSize]; |
| 511 |
|
|
[rate setFont:[NSFont controlContentFontOfSize:[NSFont systemFontSizeForControlSize:NSSmallControlSize]]]; |
| 512 |
|
|
[rate setMinValue:0]; |
| 513 |
|
|
[rate setMaxValue:5]; |
| 514 |
|
|
[cell setLevelIndicatorStyle:NSRatingLevelIndicatorStyle]; |
| 515 |
|
|
[cell setEditable:YES]; |
| 516 |
masakih |
346 |
[cell setHighlighted:YES]; |
| 517 |
masaki |
153 |
[rate sizeToFit]; |
| 518 |
|
|
|
| 519 |
masakih |
346 |
[rate setAction:@selector(continuousHighlighted:)]; |
| 520 |
masakih |
348 |
[rate setTarget:[[self class] functionHost]]; |
| 521 |
masakih |
346 |
|
| 522 |
masaki |
153 |
return rate; |
| 523 |
|
|
} |
| 524 |
masakih |
346 |
- (void)setHighlightRate:(id)rate |
| 525 |
|
|
{ |
| 526 |
|
|
[[rate cell] setHighlighted:YES]; |
| 527 |
|
|
} |
| 528 |
|
|
- (IBAction)continuousHighlighted:(id)sender |
| 529 |
|
|
{ |
| 530 |
|
|
[self performSelector:@selector(setHighlightRate:) withObject:sender afterDelay:0.0]; |
| 531 |
|
|
} |
| 532 |
masaki |
153 |
- (NSView *)numberField |
| 533 |
|
|
{ |
| 534 |
|
|
id text = [[[NSTextField alloc] initWithFrame:NSMakeRect(0,0,100,19)] autorelease]; |
| 535 |
|
|
[[text cell] setControlSize:NSSmallControlSize]; |
| 536 |
|
|
[text setFont:[NSFont controlContentFontOfSize:[NSFont systemFontSizeForControlSize:NSSmallControlSize]]]; |
| 537 |
masakih |
348 |
[text setStringValue:@"1234"]; |
| 538 |
masaki |
153 |
NSNumberFormatter *formatter = [[[NSNumberFormatter alloc] init] autorelease]; |
| 539 |
|
|
[formatter setNumberStyle:NSNumberFormatterDecimalStyle]; |
| 540 |
|
|
[formatter setMinimum:[NSNumber numberWithInt:0]]; |
| 541 |
|
|
[text setFormatter:formatter]; |
| 542 |
|
|
[text sizeToFit]; |
| 543 |
|
|
[text setStringValue:@"1"]; |
| 544 |
|
|
|
| 545 |
|
|
return text; |
| 546 |
|
|
} |
| 547 |
masaki |
243 |
- (NSView *)labelField |
| 548 |
|
|
{ |
| 549 |
masakih |
348 |
// HMLog(HMLogLevelDebug, @"Enter -> %@", NSStringFromSelector(_cmd)); |
| 550 |
masaki |
243 |
id label = [[[XspfMLabelField alloc] initWithFrame:NSMakeRect(0,0,100,19)] autorelease]; |
| 551 |
|
|
[label sizeToFit]; |
| 552 |
masaki |
251 |
[label setLabelStyle:XspfMSquareStyle]; |
| 553 |
|
|
[label setDrawX:YES]; |
| 554 |
masaki |
243 |
|
| 555 |
|
|
return label; |
| 556 |
|
|
} |
| 557 |
masaki |
153 |
@end |
| 558 |
masaki |
152 |
@implementation XspfMRule (XspfMExpressionBuilder) |
| 559 |
masakih |
348 |
+ (id)functionHost |
| 560 |
|
|
{ |
| 561 |
|
|
static id host = nil; |
| 562 |
|
|
if(host) return host; |
| 563 |
|
|
@synchronized(self) { |
| 564 |
|
|
if(!host) { |
| 565 |
|
|
host = [[self alloc] init]; |
| 566 |
|
|
} |
| 567 |
|
|
} |
| 568 |
|
|
return host; |
| 569 |
|
|
} |
| 570 |
masaki |
156 |
- (NSArray *)twoNumberAndUnitArgs:(NSArray *)displayValues |
| 571 |
|
|
{ |
| 572 |
|
|
id value03 = [displayValues objectAtIndex:2]; |
| 573 |
|
|
id arg01 = [NSNumber numberWithInt:[[value03 objectValue] intValue]]; |
| 574 |
|
|
|
| 575 |
|
|
id value05 = [displayValues objectAtIndex:4]; |
| 576 |
|
|
id arg02 = [NSNumber numberWithInt:[[value05 objectValue] intValue]]; |
| 577 |
|
|
|
| 578 |
|
|
id value06 = [displayValues objectAtIndex:5]; |
| 579 |
|
|
id arg03 = nil; |
| 580 |
masakih |
348 |
if([value06 isEqualToString:@"Hours"]) { |
| 581 |
|
|
arg03 = [NSNumber numberWithInt:XspfMHoursUnitType]; |
| 582 |
|
|
} else if([value06 isEqualToString:@"Days"]) { |
| 583 |
masaki |
163 |
arg03 = [NSNumber numberWithInt:XspfMDaysUnitType]; |
| 584 |
masaki |
156 |
} else if([value06 isEqualToString:@"Weeks"]) { |
| 585 |
masaki |
163 |
arg03 = [NSNumber numberWithInt:XpsfMWeeksUnitType]; |
| 586 |
masaki |
156 |
} else if([value06 isEqualToString:@"Months"]) { |
| 587 |
masaki |
163 |
arg03 = [NSNumber numberWithInt:XspfMMonthsUnitType]; |
| 588 |
masaki |
156 |
} else if([value06 isEqualToString:@"Years"]) { |
| 589 |
masaki |
163 |
arg03 = [NSNumber numberWithInt:XspfMYearsUnitType]; |
| 590 |
masaki |
156 |
} |
| 591 |
|
|
|
| 592 |
|
|
if([arg01 compare:arg02] == NSOrderedDescending) { |
| 593 |
|
|
id t = arg01; |
| 594 |
|
|
arg01 = arg02; |
| 595 |
|
|
arg02 = t; |
| 596 |
|
|
} |
| 597 |
|
|
|
| 598 |
|
|
return [NSArray arrayWithObjects:[NSExpression expressionForConstantValue:arg01], |
| 599 |
|
|
[NSExpression expressionForConstantValue:arg02], |
| 600 |
|
|
[NSExpression expressionForConstantValue:arg03], |
| 601 |
|
|
nil]; |
| 602 |
|
|
} |
| 603 |
masaki |
154 |
- (NSArray *)numberAndUnitArgs:(NSArray *)displayValues |
| 604 |
|
|
{ |
| 605 |
|
|
id value03 = [displayValues objectAtIndex:2]; |
| 606 |
|
|
id arg01 = [NSNumber numberWithInt:[[value03 objectValue] intValue]]; |
| 607 |
|
|
|
| 608 |
|
|
id value04 = [displayValues objectAtIndex:3]; |
| 609 |
|
|
id arg02 = nil; |
| 610 |
masakih |
348 |
if([value04 isEqualToString:@"Hours"]) { |
| 611 |
|
|
arg02 = [NSNumber numberWithInt:XspfMHoursUnitType]; |
| 612 |
|
|
} else if([value04 isEqualToString:@"Days"]) { |
| 613 |
masaki |
163 |
arg02 = [NSNumber numberWithInt:XspfMDaysUnitType]; |
| 614 |
masaki |
154 |
} else if([value04 isEqualToString:@"Weeks"]) { |
| 615 |
masaki |
163 |
arg02 = [NSNumber numberWithInt:XpsfMWeeksUnitType]; |
| 616 |
masaki |
154 |
} else if([value04 isEqualToString:@"Months"]) { |
| 617 |
masaki |
163 |
arg02 = [NSNumber numberWithInt:XspfMMonthsUnitType]; |
| 618 |
masaki |
154 |
} else if([value04 isEqualToString:@"Years"]) { |
| 619 |
masaki |
163 |
arg02 = [NSNumber numberWithInt:XspfMYearsUnitType]; |
| 620 |
masaki |
154 |
} |
| 621 |
|
|
|
| 622 |
|
|
return [NSArray arrayWithObjects:[NSExpression expressionForConstantValue:arg01], |
| 623 |
|
|
[NSExpression expressionForConstantValue:arg02], nil]; |
| 624 |
|
|
} |
| 625 |
masaki |
152 |
- (NSExpression *)rangeDateFromDisplayValues:(NSArray *)displayValues |
| 626 |
|
|
{ |
| 627 |
|
|
id field01 = nil; |
| 628 |
|
|
id field02 = nil; |
| 629 |
|
|
|
| 630 |
|
|
Class datepickerclass = [NSDatePicker class]; |
| 631 |
|
|
for(id v in displayValues) { |
| 632 |
|
|
if([v isKindOfClass:datepickerclass]) { |
| 633 |
|
|
if([v tag] == XspfMPrimaryDateFieldTag) { |
| 634 |
|
|
field01 = v; |
| 635 |
|
|
} else { |
| 636 |
|
|
field02 = v; |
| 637 |
|
|
} |
| 638 |
|
|
} |
| 639 |
masaki |
142 |
} |
| 640 |
|
|
|
| 641 |
masaki |
152 |
if(!field01 || !field02) return nil; |
| 642 |
|
|
|
| 643 |
|
|
id value01, value02; |
| 644 |
|
|
value01 = [field01 dateValue]; value02 = [field02 dateValue]; |
| 645 |
|
|
if([value01 compare:value02] == NSOrderedDescending) { |
| 646 |
|
|
id t = value02; |
| 647 |
|
|
value02 = value01; |
| 648 |
|
|
value01 = t; |
| 649 |
masaki |
142 |
} |
| 650 |
|
|
|
| 651 |
masaki |
152 |
id expression01, expression02; |
| 652 |
|
|
expression01 = [NSExpression expressionForConstantValue:value01]; |
| 653 |
|
|
expression02 = [NSExpression expressionForConstantValue:value02]; |
| 654 |
|
|
|
| 655 |
|
|
return [NSExpression expressionForAggregate:[NSArray arrayWithObjects:expression01, expression02, nil]]; |
| 656 |
masaki |
142 |
} |
| 657 |
masakih |
348 |
- (NSExpression *)rangeNumberFromDisplayValues:(NSArray *)displayValues |
| 658 |
|
|
{ |
| 659 |
|
|
id field01 = nil; |
| 660 |
|
|
id field02 = nil; |
| 661 |
|
|
|
| 662 |
|
|
Class numberFieldClass = [NSTextField class]; |
| 663 |
|
|
for(id v in displayValues) { |
| 664 |
|
|
if([v isKindOfClass:numberFieldClass]) { |
| 665 |
|
|
if([v tag] == XspfMPrimaryNumberFieldTag) { |
| 666 |
|
|
field01 = v; |
| 667 |
|
|
} else { |
| 668 |
|
|
field02 = v; |
| 669 |
|
|
} |
| 670 |
|
|
} |
| 671 |
|
|
} |
| 672 |
|
|
|
| 673 |
|
|
if(!field01 || !field02) return nil; |
| 674 |
|
|
|
| 675 |
|
|
NSInteger value01, value02; |
| 676 |
|
|
value01 = [field01 integerValue]; value02 = [field02 integerValue]; |
| 677 |
|
|
if(value01 > value02) { |
| 678 |
|
|
NSInteger t = value02; |
| 679 |
|
|
value02 = value01; |
| 680 |
|
|
value01 = t; |
| 681 |
|
|
} |
| 682 |
|
|
|
| 683 |
|
|
id expression01, expression02; |
| 684 |
|
|
expression01 = [NSExpression expressionForConstantValue:[NSNumber numberWithInteger:value01]]; |
| 685 |
|
|
expression02 = [NSExpression expressionForConstantValue:[NSNumber numberWithInteger:value02]]; |
| 686 |
|
|
|
| 687 |
|
|
return [NSExpression expressionForAggregate:[NSArray arrayWithObjects:expression01, expression02, nil]]; |
| 688 |
|
|
} |
| 689 |
masaki |
120 |
@end |
| 690 |
masaki |
152 |
|