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