| 1 |
masaki |
120 |
// |
| 2 |
|
|
// XspfMRuleEditorDelegate.m |
| 3 |
|
|
// XspfManager |
| 4 |
|
|
// |
| 5 |
|
|
// Created by Hori,Masaki on 09/11/28. |
| 6 |
|
|
// Copyright 2009 masakih. All rights reserved. |
| 7 |
|
|
// |
| 8 |
|
|
|
| 9 |
|
|
#import "XspfMRuleEditorDelegate.h" |
| 10 |
|
|
|
| 11 |
|
|
#import "XspfMRuleEditorRow.h" |
| 12 |
|
|
|
| 13 |
|
|
@implementation XspfMRuleEditorDelegate |
| 14 |
masaki |
125 |
|
| 15 |
|
|
static NSString *XspfMREDPredicateRowsKey = @"predicateRows"; |
| 16 |
|
|
|
| 17 |
masaki |
124 |
- (NSDictionary *)buildRows:(NSArray *)template |
| 18 |
|
|
{ |
| 19 |
|
|
NSMutableDictionary *result = [NSMutableDictionary dictionary]; |
| 20 |
|
|
for(id row in template) { |
| 21 |
masaki |
159 |
id name = [row valueForKey:@"name"]; |
| 22 |
masaki |
152 |
id rule = [XspfMRule ruleWithPlist:row]; |
| 23 |
|
|
[result setObject:rule forKey:name]; |
| 24 |
masaki |
124 |
} |
| 25 |
masaki |
152 |
rowTemplate = [result retain]; |
| 26 |
masaki |
124 |
return result; |
| 27 |
|
|
} |
| 28 |
masaki |
126 |
- (id)criteriaWithKeyPath:(NSString *)keypath |
| 29 |
|
|
{ |
| 30 |
|
|
NSString *key = nil; |
| 31 |
|
|
if([keypath isEqualToString:@"title"]) { |
| 32 |
|
|
key = @"String"; |
| 33 |
|
|
} else if([keypath isEqualToString:@"rating"]) { |
| 34 |
|
|
key = @"Rate"; |
| 35 |
masaki |
152 |
} else if([[NSArray arrayWithObjects:@"lastPlayDate", @"modificationDate", @"creationDate", nil] containsObject:keypath]) { |
| 36 |
|
|
key = @"AbDate"; |
| 37 |
masaki |
126 |
} |
| 38 |
|
|
if(key) { |
| 39 |
|
|
id row = [rowTemplate valueForKey:key]; |
| 40 |
masaki |
152 |
id c = [[[row childAtIndex:0] copy] autorelease]; |
| 41 |
|
|
[c setValue:keypath]; |
| 42 |
masaki |
126 |
return [NSArray arrayWithObject:c]; |
| 43 |
|
|
} |
| 44 |
|
|
|
| 45 |
|
|
return nil; |
| 46 |
|
|
} |
| 47 |
masaki |
120 |
- (void)awakeFromNib |
| 48 |
|
|
{ |
| 49 |
|
|
if(!compound) { |
| 50 |
masaki |
143 |
compound = [[XspfMCompound alloc] init]; |
| 51 |
masaki |
120 |
} |
| 52 |
masaki |
152 |
|
| 53 |
masaki |
124 |
NSBundle *m = [NSBundle mainBundle]; |
| 54 |
|
|
NSString *path = [m pathForResource:@"LibraryRowTemplate" ofType:@"plist"]; |
| 55 |
|
|
NSArray *rowsTemplate = [NSArray arrayWithContentsOfFile:path]; |
| 56 |
|
|
if(!rowsTemplate) { |
| 57 |
|
|
exit(12345); |
| 58 |
masaki |
120 |
} |
| 59 |
masaki |
124 |
|
| 60 |
masaki |
152 |
[self buildRows:rowsTemplate]; |
| 61 |
|
|
|
| 62 |
masaki |
124 |
NSMutableArray *newRows = [NSMutableArray array]; |
| 63 |
|
|
|
| 64 |
masaki |
126 |
id c = [self criteriaWithKeyPath:@"title"]; |
| 65 |
|
|
if(c) [newRows addObjectsFromArray:c]; |
| 66 |
masaki |
124 |
|
| 67 |
|
|
for(id keyPath in [NSArray arrayWithObjects:@"lastPlayDate", @"modificationDate", @"creationDate", nil]) { |
| 68 |
masaki |
126 |
c = [self criteriaWithKeyPath:keyPath]; |
| 69 |
|
|
if(c) [newRows addObjectsFromArray:c]; |
| 70 |
masaki |
124 |
} |
| 71 |
|
|
|
| 72 |
masaki |
126 |
c = [self criteriaWithKeyPath:@"rating"]; |
| 73 |
|
|
if(c) [newRows addObjectsFromArray:c]; |
| 74 |
masaki |
124 |
|
| 75 |
masaki |
152 |
simples = [newRows retain]; |
| 76 |
|
|
|
| 77 |
masaki |
125 |
//// |
| 78 |
|
|
predicateRows = [[NSMutableArray alloc] init]; |
| 79 |
masaki |
159 |
[ruleEditor bind:@"rows" toObject:self withKeyPath:XspfMREDPredicateRowsKey options:nil]; |
| 80 |
masaki |
120 |
} |
| 81 |
|
|
|
| 82 |
masaki |
150 |
- (void)resolveExpression:(id)exp |
| 83 |
|
|
{ |
| 84 |
|
|
NSString *message = nil; |
| 85 |
|
|
|
| 86 |
|
|
switch([exp expressionType]) { |
| 87 |
|
|
case NSConstantValueExpressionType: |
| 88 |
|
|
message = [NSString stringWithFormat:@"constant -> %@", [exp constantValue]]; |
| 89 |
|
|
break; |
| 90 |
|
|
case NSEvaluatedObjectExpressionType: |
| 91 |
|
|
message = [NSString stringWithFormat:@"constant -> %@", [exp constantValue]]; |
| 92 |
|
|
break; |
| 93 |
|
|
case NSVariableExpressionType: |
| 94 |
|
|
message = [NSString stringWithFormat:@"variable -> %@", [exp variable]]; |
| 95 |
|
|
break; |
| 96 |
|
|
case NSKeyPathExpressionType: |
| 97 |
|
|
message = [NSString stringWithFormat:@"keyPath -> %@", [exp keyPath]]; |
| 98 |
|
|
break; |
| 99 |
|
|
case NSFunctionExpressionType: |
| 100 |
masaki |
154 |
message = [NSString stringWithFormat:@"oprand -> %@(%@), function -> %@, arguments -> %@", |
| 101 |
masaki |
150 |
[exp operand], NSStringFromClass([[exp operand] class]), |
| 102 |
|
|
[exp function], [exp arguments]]; |
| 103 |
|
|
break; |
| 104 |
|
|
case NSAggregateExpressionType: |
| 105 |
|
|
message = [NSString stringWithFormat:@"collection -> %@", [exp collection]]; |
| 106 |
|
|
break; |
| 107 |
|
|
} |
| 108 |
|
|
|
| 109 |
|
|
fprintf(stderr, "%s\n", [message UTF8String]); |
| 110 |
|
|
} |
| 111 |
|
|
- (void)resolvePredicate:(id)predicate |
| 112 |
|
|
{ |
| 113 |
|
|
if([predicate isKindOfClass:[NSCompoundPredicate class]]) { |
| 114 |
|
|
NSArray *sub = [predicate subpredicates]; |
| 115 |
|
|
for(id p in sub) { |
| 116 |
|
|
[self resolvePredicate:p]; |
| 117 |
|
|
} |
| 118 |
|
|
} else if([predicate isKindOfClass:[NSComparisonPredicate class]]) { |
| 119 |
|
|
id left = [predicate leftExpression]; |
| 120 |
|
|
id right = [predicate rightExpression]; |
| 121 |
|
|
SEL sel = Nil; |
| 122 |
|
|
if([predicate predicateOperatorType] == NSCustomSelectorPredicateOperatorType) { |
| 123 |
|
|
sel = [predicate customSelector]; |
| 124 |
|
|
} |
| 125 |
|
|
fprintf(stderr, "left ->\t"); |
| 126 |
|
|
[self resolveExpression:left]; |
| 127 |
|
|
if(sel) { |
| 128 |
|
|
fprintf(stderr, "%s\n", [[NSString stringWithFormat:@"SEL -> %@", NSStringFromSelector(sel)] UTF8String]); |
| 129 |
|
|
} else { |
| 130 |
|
|
fprintf(stderr, "%s\n", [[NSString stringWithFormat:@"type -> %d, opt -> %d, mod -> %d", [predicate predicateOperatorType], [predicate options], [predicate comparisonPredicateModifier]] UTF8String]); |
| 131 |
|
|
} |
| 132 |
|
|
fprintf(stderr, "right ->\t"); |
| 133 |
|
|
[self resolveExpression:right]; |
| 134 |
|
|
fprintf(stderr, "end resolve.\n"); |
| 135 |
|
|
} |
| 136 |
|
|
} |
| 137 |
|
|
|
| 138 |
masaki |
123 |
- (void)setPredicate:(id)predicate |
| 139 |
|
|
{ |
| 140 |
masaki |
150 |
NSLog(@"predicate -> (%@) %@", NSStringFromClass([predicate class]), predicate); |
| 141 |
|
|
[self resolvePredicate:predicate]; |
| 142 |
masaki |
123 |
|
| 143 |
masaki |
153 |
id new = [XspfMRule ruleEditorRowsFromPredicate:predicate withRowTemplate:rowTemplate]; |
| 144 |
masaki |
125 |
|
| 145 |
|
|
[self willChangeValueForKey:XspfMREDPredicateRowsKey]; |
| 146 |
|
|
[predicateRows release]; |
| 147 |
|
|
predicateRows = [new retain]; |
| 148 |
|
|
[self didChangeValueForKey:XspfMREDPredicateRowsKey]; |
| 149 |
masaki |
146 |
// [ruleEditor reloadCriteria]; |
| 150 |
masaki |
123 |
} |
| 151 |
masaki |
125 |
- (void)setPredicateRows:(id)p |
| 152 |
|
|
{ |
| 153 |
masaki |
147 |
// NSLog(@"new -> %@", p); |
| 154 |
masaki |
125 |
[predicateRows release]; |
| 155 |
|
|
predicateRows = [p retain]; |
| 156 |
|
|
} |
| 157 |
|
|
|
| 158 |
|
|
#pragma mark#### NSRleEditor Delegate #### |
| 159 |
masaki |
152 |
|
| 160 |
masaki |
124 |
- (NSInteger)ruleEditor:(NSRuleEditor *)editor |
| 161 |
|
|
numberOfChildrenForCriterion:(id)criterion |
| 162 |
|
|
withRowType:(NSRuleEditorRowType)rowType |
| 163 |
|
|
{ |
| 164 |
|
|
NSInteger result = 0; |
| 165 |
|
|
|
| 166 |
masaki |
143 |
if(rowType == NSRuleEditorRowTypeCompound) { |
| 167 |
|
|
return [compound numberOfChildrenForChild:criterion]; |
| 168 |
|
|
} |
| 169 |
|
|
|
| 170 |
masaki |
124 |
if(!criterion) { |
| 171 |
masaki |
152 |
result = [simples count]; |
| 172 |
masaki |
143 |
} else { |
| 173 |
masaki |
152 |
result = [criterion numberOfChildren]; |
| 174 |
masaki |
124 |
} |
| 175 |
|
|
|
| 176 |
masaki |
152 |
// NSLog(@"numner\tcriterion -> %@, type -> %d, result -> %d", criterion, rowType, result); |
| 177 |
masaki |
124 |
|
| 178 |
|
|
return result; |
| 179 |
|
|
} |
| 180 |
|
|
|
| 181 |
|
|
- (id)ruleEditor:(NSRuleEditor *)editor |
| 182 |
|
|
child:(NSInteger)index |
| 183 |
|
|
forCriterion:(id)criterion |
| 184 |
|
|
withRowType:(NSRuleEditorRowType)rowType |
| 185 |
|
|
{ |
| 186 |
|
|
id result = nil; |
| 187 |
|
|
|
| 188 |
masaki |
143 |
if(rowType == NSRuleEditorRowTypeCompound) { |
| 189 |
|
|
return [compound childForChild:criterion atIndex:index]; |
| 190 |
|
|
} |
| 191 |
|
|
|
| 192 |
masaki |
124 |
if(!criterion) { |
| 193 |
masaki |
152 |
result = [simples objectAtIndex:index]; |
| 194 |
masaki |
143 |
} else { |
| 195 |
masaki |
152 |
result = [criterion childAtIndex:index]; |
| 196 |
masaki |
124 |
} |
| 197 |
|
|
|
| 198 |
masaki |
152 |
// NSLog(@"child\tindex -> %d, criterion -> %@, type -> %d, result -> %@", index, criterion, rowType, result); |
| 199 |
masaki |
124 |
|
| 200 |
|
|
return result; |
| 201 |
|
|
} |
| 202 |
|
|
- (id)ruleEditor:(NSRuleEditor *)editor |
| 203 |
|
|
displayValueForCriterion:(id)criterion |
| 204 |
|
|
inRow:(NSInteger)row |
| 205 |
|
|
{ |
| 206 |
|
|
id result = nil; |
| 207 |
masaki |
143 |
|
| 208 |
|
|
NSRuleEditorRowType rowType = [editor rowTypeForRow:row]; |
| 209 |
|
|
if(rowType == NSRuleEditorRowTypeCompound) { |
| 210 |
|
|
return [compound displayValueForChild:criterion]; |
| 211 |
|
|
} |
| 212 |
|
|
|
| 213 |
|
|
if(!criterion) { |
| 214 |
|
|
// |
| 215 |
|
|
} else { |
| 216 |
masaki |
152 |
result = [criterion displayValueForRuleEditor:editor inRow:row]; |
| 217 |
masaki |
143 |
} |
| 218 |
|
|
|
| 219 |
masaki |
152 |
// NSLog(@"display\tcriterion -> %@, row -> %d, result -> %@", criterion, row, result); |
| 220 |
masaki |
143 |
|
| 221 |
masaki |
124 |
return result; |
| 222 |
|
|
} |
| 223 |
|
|
- (NSDictionary *)ruleEditor:(NSRuleEditor *)editor |
| 224 |
|
|
predicatePartsForCriterion:(id)criterion |
| 225 |
|
|
withDisplayValue:(id)displayValue |
| 226 |
|
|
inRow:(NSInteger)row |
| 227 |
|
|
{ |
| 228 |
|
|
id result = nil; |
| 229 |
|
|
|
| 230 |
masaki |
143 |
NSRuleEditorRowType rowType = [editor rowTypeForRow:row]; |
| 231 |
|
|
if(rowType == NSRuleEditorRowTypeCompound) { |
| 232 |
|
|
return [compound predicateForChild:criterion withDisplayValue:displayValue]; |
| 233 |
|
|
} |
| 234 |
|
|
|
| 235 |
masaki |
152 |
result = [criterion predicatePartsWithDisplayValue:displayValue forRuleEditor:editor inRow:row]; |
| 236 |
masaki |
153 |
// NSLog(@"predicate\tresult -> %@", result); |
| 237 |
masaki |
143 |
|
| 238 |
masaki |
152 |
// NSLog(@"predicate\tcriterion -> %@, value -> %@, row -> %d, result -> %@", criterion, displayValue, row, result); |
| 239 |
masaki |
143 |
|
| 240 |
masaki |
124 |
return result; |
| 241 |
|
|
} |
| 242 |
|
|
- (void)ruleEditorRowsDidChange:(NSNotification *)notification |
| 243 |
|
|
{ |
| 244 |
|
|
// |
| 245 |
|
|
} |
| 246 |
masaki |
150 |
@end |