| 1 |
masaki |
120 |
// |
| 2 |
|
|
// XspfMRuleEditorDelegate.m |
| 3 |
|
|
// XspfManager |
| 4 |
|
|
// |
| 5 |
|
|
// Created by Hori,Masaki on 09/11/28. |
| 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 |
masaki |
120 |
#import "XspfMRuleEditorDelegate.h" |
| 30 |
|
|
|
| 31 |
masakih |
348 |
#import "XspfMRule.h" |
| 32 |
|
|
#import "XspfMRuleRowsBuilder.h" |
| 33 |
|
|
#import "XspfMRuleRowTemplate.h" |
| 34 |
masaki |
120 |
|
| 35 |
|
|
@implementation XspfMRuleEditorDelegate |
| 36 |
masaki |
125 |
|
| 37 |
|
|
static NSString *XspfMREDPredicateRowsKey = @"predicateRows"; |
| 38 |
|
|
|
| 39 |
masakih |
348 |
+ (void)initialize |
| 40 |
masaki |
124 |
{ |
| 41 |
masakih |
348 |
static BOOL isFirst = YES; |
| 42 |
|
|
if(isFirst) { |
| 43 |
|
|
isFirst = NO; |
| 44 |
|
|
[XspfMRule registerStringTypeKeyPaths:[NSArray arrayWithObjects:@"title", @"information.voiceActorsList", @"information.productsList", nil]]; |
| 45 |
|
|
[XspfMRule registerDateTypeKeyPaths:[NSArray arrayWithObjects:@"lastPlayDate", @"modificationDate", @"creationDate", nil]]; |
| 46 |
|
|
[XspfMRule setUseRating:YES]; |
| 47 |
|
|
[XspfMRule setUseLablel:YES]; |
| 48 |
masaki |
124 |
} |
| 49 |
|
|
} |
| 50 |
masaki |
126 |
|
| 51 |
masaki |
120 |
- (void)awakeFromNib |
| 52 |
|
|
{ |
| 53 |
masakih |
348 |
NSBundle *mainBundle = [NSBundle mainBundle]; |
| 54 |
|
|
NSString *templatePath = [mainBundle pathForResource:@"LibraryRowTemplate" ofType:@"plist"]; |
| 55 |
|
|
rowTemplate = [[XspfMRuleRowTemplate rowTemplateWithPath:templatePath] retain]; |
| 56 |
masaki |
124 |
|
| 57 |
|
|
NSMutableArray *newRows = [NSMutableArray array]; |
| 58 |
masaki |
338 |
for(id keyPath in [XspfMRule leftKeys]) { |
| 59 |
masakih |
348 |
id c = [rowTemplate criteriaForKeyPath:keyPath]; |
| 60 |
masaki |
126 |
if(c) [newRows addObjectsFromArray:c]; |
| 61 |
masaki |
124 |
} |
| 62 |
masakih |
352 |
|
| 63 |
masaki |
152 |
simples = [newRows retain]; |
| 64 |
masaki |
166 |
compounds = [[XspfMRule compoundRule] retain]; |
| 65 |
masaki |
152 |
|
| 66 |
masaki |
125 |
predicateRows = [[NSMutableArray alloc] init]; |
| 67 |
masaki |
159 |
[ruleEditor bind:@"rows" toObject:self withKeyPath:XspfMREDPredicateRowsKey options:nil]; |
| 68 |
masaki |
120 |
} |
| 69 |
masakih |
348 |
- (void)dealloc |
| 70 |
masaki |
150 |
{ |
| 71 |
masakih |
348 |
[ruleEditor unbind:@"rows"]; |
| 72 |
|
|
[simples release]; |
| 73 |
|
|
[compounds release]; |
| 74 |
|
|
[predicateRows release]; |
| 75 |
|
|
[rowTemplate release]; |
| 76 |
masaki |
150 |
|
| 77 |
masakih |
348 |
[super dealloc]; |
| 78 |
masaki |
150 |
} |
| 79 |
|
|
|
| 80 |
masakih |
348 |
- (void)setPredicateRows:(id)p |
| 81 |
masaki |
123 |
{ |
| 82 |
masakih |
348 |
if([predicateRows isEqual:p]) return; |
| 83 |
masaki |
123 |
|
| 84 |
masaki |
125 |
[predicateRows release]; |
| 85 |
masakih |
348 |
predicateRows = [p retain]; |
| 86 |
masaki |
123 |
} |
| 87 |
masakih |
348 |
- (void)setPredicate:(id)predicate |
| 88 |
masaki |
125 |
{ |
| 89 |
masakih |
348 |
XspfMRuleRowsBuilder *builder = [XspfMRuleRowsBuilder builderWithPredicate:predicate]; |
| 90 |
|
|
builder.rowTemplate = rowTemplate; |
| 91 |
|
|
[builder build]; |
| 92 |
masakih |
352 |
id new = [NSMutableArray arrayWithObject:[builder row]]; |
| 93 |
masakih |
348 |
|
| 94 |
|
|
[self setPredicateRows:new]; |
| 95 |
masaki |
125 |
} |
| 96 |
|
|
|
| 97 |
|
|
#pragma mark#### NSRleEditor Delegate #### |
| 98 |
masaki |
152 |
|
| 99 |
masaki |
124 |
- (NSInteger)ruleEditor:(NSRuleEditor *)editor |
| 100 |
|
|
numberOfChildrenForCriterion:(id)criterion |
| 101 |
|
|
withRowType:(NSRuleEditorRowType)rowType |
| 102 |
|
|
{ |
| 103 |
|
|
NSInteger result = 0; |
| 104 |
|
|
|
| 105 |
|
|
if(!criterion) { |
| 106 |
masaki |
166 |
if(rowType == NSRuleEditorRowTypeCompound) { |
| 107 |
|
|
result = [compounds count]; |
| 108 |
|
|
} else { |
| 109 |
|
|
result = [simples count]; |
| 110 |
|
|
} |
| 111 |
masaki |
143 |
} else { |
| 112 |
masaki |
152 |
result = [criterion numberOfChildren]; |
| 113 |
masaki |
124 |
} |
| 114 |
|
|
|
| 115 |
|
|
return result; |
| 116 |
|
|
} |
| 117 |
|
|
|
| 118 |
|
|
- (id)ruleEditor:(NSRuleEditor *)editor |
| 119 |
|
|
child:(NSInteger)index |
| 120 |
|
|
forCriterion:(id)criterion |
| 121 |
|
|
withRowType:(NSRuleEditorRowType)rowType |
| 122 |
|
|
{ |
| 123 |
|
|
id result = nil; |
| 124 |
|
|
|
| 125 |
|
|
if(!criterion) { |
| 126 |
masaki |
166 |
if(rowType == NSRuleEditorRowTypeCompound) { |
| 127 |
|
|
result = [compounds objectAtIndex:index]; |
| 128 |
|
|
} else { |
| 129 |
|
|
result = [simples objectAtIndex:index]; |
| 130 |
|
|
} |
| 131 |
masaki |
143 |
} else { |
| 132 |
masaki |
152 |
result = [criterion childAtIndex:index]; |
| 133 |
masaki |
124 |
} |
| 134 |
masakih |
348 |
|
| 135 |
masaki |
124 |
return result; |
| 136 |
|
|
} |
| 137 |
|
|
- (id)ruleEditor:(NSRuleEditor *)editor |
| 138 |
|
|
displayValueForCriterion:(id)criterion |
| 139 |
|
|
inRow:(NSInteger)row |
| 140 |
|
|
{ |
| 141 |
masakih |
348 |
id result = [criterion displayValueForRuleEditor:editor inRow:row]; |
| 142 |
masaki |
143 |
|
| 143 |
masaki |
124 |
return result; |
| 144 |
|
|
} |
| 145 |
|
|
- (NSDictionary *)ruleEditor:(NSRuleEditor *)editor |
| 146 |
|
|
predicatePartsForCriterion:(id)criterion |
| 147 |
|
|
withDisplayValue:(id)displayValue |
| 148 |
|
|
inRow:(NSInteger)row |
| 149 |
|
|
{ |
| 150 |
masakih |
348 |
id result = [criterion predicatePartsWithDisplayValue:displayValue forRuleEditor:editor inRow:row]; |
| 151 |
masaki |
124 |
|
| 152 |
|
|
return result; |
| 153 |
|
|
} |
| 154 |
|
|
- (void)ruleEditorRowsDidChange:(NSNotification *)notification |
| 155 |
|
|
{ |
| 156 |
|
|
// |
| 157 |
|
|
} |
| 158 |
masakih |
348 |
|
| 159 |
|
|
#pragma mark---- Debugging ---- |
| 160 |
|
|
- (void)resolveExpression:(id)exp |
| 161 |
|
|
{ |
| 162 |
|
|
NSString *message = nil; |
| 163 |
|
|
|
| 164 |
|
|
switch([exp expressionType]) { |
| 165 |
|
|
case NSConstantValueExpressionType: |
| 166 |
|
|
message = [NSString stringWithFormat:@"constant -> %@", [exp constantValue]]; |
| 167 |
|
|
break; |
| 168 |
|
|
case NSEvaluatedObjectExpressionType: |
| 169 |
|
|
message = [NSString stringWithFormat:@"constant -> %@", [exp constantValue]]; |
| 170 |
|
|
break; |
| 171 |
|
|
case NSVariableExpressionType: |
| 172 |
|
|
message = [NSString stringWithFormat:@"variable -> %@", [exp variable]]; |
| 173 |
|
|
break; |
| 174 |
|
|
case NSKeyPathExpressionType: |
| 175 |
|
|
message = [NSString stringWithFormat:@"keyPath -> %@", [exp keyPath]]; |
| 176 |
|
|
break; |
| 177 |
|
|
case NSFunctionExpressionType: |
| 178 |
|
|
message = [NSString stringWithFormat:@"oprand -> %@(%@), function -> %@, arguments -> %@", |
| 179 |
|
|
[exp operand], NSStringFromClass([[exp operand] class]), |
| 180 |
|
|
[exp function], [exp arguments]]; |
| 181 |
|
|
break; |
| 182 |
|
|
case NSAggregateExpressionType: |
| 183 |
|
|
message = [NSString stringWithFormat:@"collection -> %@", [exp collection]]; |
| 184 |
|
|
break; |
| 185 |
|
|
} |
| 186 |
|
|
|
| 187 |
|
|
fprintf(stderr, "%s\n", [message UTF8String]); |
| 188 |
|
|
} |
| 189 |
|
|
- (void)resolvePredicate:(id)predicate |
| 190 |
|
|
{ |
| 191 |
|
|
if([predicate isKindOfClass:[NSCompoundPredicate class]]) { |
| 192 |
|
|
NSArray *sub = [predicate subpredicates]; |
| 193 |
|
|
for(id p in sub) { |
| 194 |
|
|
[self resolvePredicate:p]; |
| 195 |
|
|
} |
| 196 |
|
|
} else if([predicate isKindOfClass:[NSComparisonPredicate class]]) { |
| 197 |
|
|
id left = [predicate leftExpression]; |
| 198 |
|
|
id right = [predicate rightExpression]; |
| 199 |
|
|
SEL sel = Nil; |
| 200 |
|
|
if([predicate predicateOperatorType] == NSCustomSelectorPredicateOperatorType) { |
| 201 |
|
|
sel = [predicate customSelector]; |
| 202 |
|
|
} |
| 203 |
|
|
fprintf(stderr, "left ->\t"); |
| 204 |
|
|
[self resolveExpression:left]; |
| 205 |
|
|
if(sel) { |
| 206 |
|
|
fprintf(stderr, "%s\n", [[NSString stringWithFormat:@"SEL -> %@", NSStringFromSelector(sel)] UTF8String]); |
| 207 |
|
|
} else { |
| 208 |
|
|
fprintf(stderr, "%s\n", [[NSString stringWithFormat:@"type -> %d, opt -> %d, mod -> %d", [predicate predicateOperatorType], [predicate options], [predicate comparisonPredicateModifier]] UTF8String]); |
| 209 |
|
|
} |
| 210 |
|
|
fprintf(stderr, "right ->\t"); |
| 211 |
|
|
[self resolveExpression:right]; |
| 212 |
|
|
fprintf(stderr, "end resolve.\n"); |
| 213 |
|
|
} |
| 214 |
|
|
} |
| 215 |
masaki |
150 |
@end |