| 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 *XspfMREDRowsKey = @"rows"; |
| 16 |
|
|
static NSString *XspfMREDCriteriaKey = @"criteria"; |
| 17 |
|
|
static NSString *XspfMREDDisplayValuesKey = @"displayValues"; |
| 18 |
|
|
static NSString *XspfMREDRowTypeKey = @"rowType"; |
| 19 |
|
|
static NSString *XspfMREDSubrowsKey = @"subrows"; |
| 20 |
|
|
static NSString *XspfMREDValueKey = @"value"; |
| 21 |
|
|
static NSString *XspfMREDPredicateRowsKey = @"predicateRows"; |
| 22 |
|
|
static NSString *XspfMREDNameKey = @"name"; |
| 23 |
|
|
|
| 24 |
|
|
static NSString *XspfMStringPredicateIsEqualOperator = @"is"; |
| 25 |
|
|
static NSString *XspfMStringPredicateIsNotEqualOperator = @"is not"; |
| 26 |
|
|
static NSString *XspfMStringPredicateContainsOperator = @"contains"; |
| 27 |
|
|
static NSString *XspfMStringPredicateBeginsWithOperator = @"begins with"; |
| 28 |
|
|
static NSString *XspfMStringPredicateEndsWithOperator = @"ends with"; |
| 29 |
|
|
|
| 30 |
|
|
//static NSString *XspfMAbDatePredicatePicker01 = @"date"; |
| 31 |
|
|
//static NSString *XspfMAbDatePredicatePicker02 = @"beginDate"; |
| 32 |
|
|
//static NSString *XspfMAbDatePredicatePicker03 = @"endDate"; |
| 33 |
|
|
//static NSString *XspfMAbDatePredicateIsEqualOperator = @"is the date"; |
| 34 |
|
|
//static NSString *XspfMAbDatePredicateLessThanOperator = @"is after the date"; |
| 35 |
|
|
//static NSString *XspfMAbDatePredicateGreaterThanOperator = @"is before the date"; |
| 36 |
|
|
//static NSString *XspfMAbDatePredicateBetweenOperator = @"is in the range"; |
| 37 |
|
|
//static NSString *XspfMAbDatePredicateAndField = @"andField"; |
| 38 |
|
|
|
| 39 |
|
|
|
| 40 |
masaki |
137 |
- (NSExpression *)rangeUnitFromDisplayValues:(NSArray *)displayValues option:(NSNumber *)optionValue |
| 41 |
|
|
{ |
| 42 |
|
|
NSInteger option = [optionValue integerValue]; |
| 43 |
|
|
|
| 44 |
|
|
NSString *variable = nil; |
| 45 |
|
|
id value02 = [displayValues objectAtIndex:2]; |
| 46 |
|
|
id value03 = [displayValues objectAtIndex:3]; |
| 47 |
|
|
id value04 = nil, value05 = nil; |
| 48 |
|
|
switch(option) { |
| 49 |
|
|
case 0: |
| 50 |
masaki |
138 |
variable = [NSString stringWithFormat:@"%d-%@-ago", [value02 intValue], value03]; |
| 51 |
masaki |
137 |
break; |
| 52 |
|
|
case 1: |
| 53 |
masaki |
138 |
variable = [NSString stringWithFormat:@"%d-%@", [value02 intValue], value03]; |
| 54 |
|
|
break; |
| 55 |
masaki |
137 |
case 2: |
| 56 |
masaki |
138 |
variable = [NSString stringWithFormat:@"not-%d-%@", [value02 intValue], value03]; |
| 57 |
masaki |
137 |
break; |
| 58 |
|
|
case 3: |
| 59 |
|
|
value04 = [displayValues objectAtIndex:4]; |
| 60 |
|
|
value05 = [displayValues objectAtIndex:5]; |
| 61 |
masaki |
138 |
variable = [NSString stringWithFormat:@"%d-%@-%d-%@", [value02 intValue], value03, [value04 intValue], value05]; |
| 62 |
masaki |
137 |
break; |
| 63 |
|
|
} |
| 64 |
|
|
|
| 65 |
|
|
return [NSExpression expressionForVariable:variable]; |
| 66 |
|
|
} |
| 67 |
masaki |
124 |
- (NSExpression *)rangeDateFromDisplayValues:(NSArray *)displayValues |
| 68 |
|
|
{ |
| 69 |
|
|
id field01 = nil; |
| 70 |
|
|
id field02 = nil; |
| 71 |
|
|
|
| 72 |
|
|
Class datepickerclass = [NSDatePicker class]; |
| 73 |
|
|
for(id v in displayValues) { |
| 74 |
|
|
if([v isKindOfClass:datepickerclass]) { |
| 75 |
|
|
if([v tag] == 0) { |
| 76 |
|
|
field01 = v; |
| 77 |
|
|
} else { |
| 78 |
|
|
field02 = v; |
| 79 |
|
|
} |
| 80 |
|
|
} |
| 81 |
|
|
} |
| 82 |
|
|
|
| 83 |
|
|
if(!field01 || !field02) return nil; |
| 84 |
|
|
|
| 85 |
|
|
id value01, value02; |
| 86 |
|
|
value01 = [field01 dateValue]; value02 = [field02 dateValue]; |
| 87 |
|
|
if([value01 compare:value02] == NSOrderedDescending) { |
| 88 |
|
|
id t = value02; |
| 89 |
|
|
value02 = value01; |
| 90 |
|
|
value01 = t; |
| 91 |
|
|
} |
| 92 |
|
|
|
| 93 |
|
|
id expression01, expression02; |
| 94 |
masaki |
133 |
expression01 = [NSExpression expressionForConstantValue:value01]; |
| 95 |
|
|
expression02 = [NSExpression expressionForConstantValue:value02]; |
| 96 |
masaki |
124 |
|
| 97 |
|
|
return [NSExpression expressionForAggregate:[NSArray arrayWithObjects:expression01, expression02, nil]]; |
| 98 |
|
|
} |
| 99 |
masaki |
132 |
- (NSExpression *)relatedDate:(NSNumber *)typeValue |
| 100 |
|
|
{ |
| 101 |
|
|
NSString *variable = nil; |
| 102 |
|
|
NSInteger type = [typeValue integerValue]; |
| 103 |
|
|
switch(type) { |
| 104 |
|
|
case 0: |
| 105 |
|
|
variable = @"TODAY"; |
| 106 |
|
|
break; |
| 107 |
|
|
case 1: |
| 108 |
|
|
variable = @"YESTERDAY"; |
| 109 |
|
|
break; |
| 110 |
|
|
case 2: |
| 111 |
|
|
variable = @"THISWEEK"; |
| 112 |
|
|
break; |
| 113 |
|
|
case 3: |
| 114 |
|
|
variable = @"LASTWEEK"; |
| 115 |
|
|
break; |
| 116 |
|
|
} |
| 117 |
|
|
|
| 118 |
|
|
return [NSExpression expressionForVariable:variable]; |
| 119 |
|
|
} |
| 120 |
|
|
|
| 121 |
masaki |
124 |
- (NSView *)textField |
| 122 |
|
|
{ |
| 123 |
|
|
id text = [[[NSTextField alloc] initWithFrame:NSMakeRect(0,0,100,19)] autorelease]; |
| 124 |
|
|
[[text cell] setControlSize:NSSmallControlSize]; |
| 125 |
|
|
[text setFont:[NSFont controlContentFontOfSize:[NSFont systemFontSizeForControlSize:NSSmallControlSize]]]; |
| 126 |
masaki |
126 |
[text setStringValue:@"1234567890"]; |
| 127 |
masaki |
124 |
[text sizeToFit]; |
| 128 |
|
|
[text setStringValue:@""]; |
| 129 |
|
|
[text setDelegate:self]; |
| 130 |
|
|
|
| 131 |
|
|
return text; |
| 132 |
|
|
} |
| 133 |
|
|
- (NSView *)datePicker |
| 134 |
|
|
{ |
| 135 |
|
|
id date = [[[NSDatePicker alloc] initWithFrame:NSMakeRect(0,0,100,19)] autorelease]; |
| 136 |
|
|
[[date cell] setControlSize:NSSmallControlSize]; |
| 137 |
|
|
[date setFont:[NSFont controlContentFontOfSize:[NSFont systemFontSizeForControlSize:NSSmallControlSize]]]; |
| 138 |
|
|
[date setDatePickerElements:NSYearMonthDayDatePickerElementFlag]; |
| 139 |
|
|
[date setDrawsBackground:YES]; |
| 140 |
|
|
[date setDateValue:[NSDate dateWithTimeIntervalSinceNow:0.0]]; |
| 141 |
|
|
[date sizeToFit]; |
| 142 |
|
|
[date setDelegate:self]; |
| 143 |
|
|
|
| 144 |
|
|
return date; |
| 145 |
|
|
} |
| 146 |
|
|
- (NSView *)ratingIndicator |
| 147 |
|
|
{ |
| 148 |
|
|
id rate = [[[NSLevelIndicator alloc] initWithFrame:NSMakeRect(0,0,100,19)] autorelease]; |
| 149 |
|
|
id cell = [rate cell]; |
| 150 |
|
|
[cell setControlSize:NSSmallControlSize]; |
| 151 |
|
|
[rate setFont:[NSFont controlContentFontOfSize:[NSFont systemFontSizeForControlSize:NSSmallControlSize]]]; |
| 152 |
|
|
[rate setMinValue:0]; |
| 153 |
|
|
[rate setMaxValue:5]; |
| 154 |
|
|
[cell setLevelIndicatorStyle:NSRatingLevelIndicatorStyle]; |
| 155 |
|
|
[cell setEditable:YES]; |
| 156 |
|
|
[rate sizeToFit]; |
| 157 |
|
|
|
| 158 |
|
|
return rate; |
| 159 |
|
|
} |
| 160 |
|
|
- (NSView *)numberField |
| 161 |
|
|
{ |
| 162 |
masaki |
134 |
id text = [[[NSTextField alloc] initWithFrame:NSMakeRect(0,0,100,19)] autorelease]; |
| 163 |
|
|
[[text cell] setControlSize:NSSmallControlSize]; |
| 164 |
|
|
[text setFont:[NSFont controlContentFontOfSize:[NSFont systemFontSizeForControlSize:NSSmallControlSize]]]; |
| 165 |
|
|
[text setStringValue:@"123"]; |
| 166 |
|
|
NSNumberFormatter *formatter = [[[NSNumberFormatter alloc] init] autorelease]; |
| 167 |
|
|
[formatter setNumberStyle:NSNumberFormatterDecimalStyle]; |
| 168 |
|
|
[formatter setMinimum:[NSNumber numberWithInt:0]]; |
| 169 |
|
|
[text setFormatter:formatter]; |
| 170 |
|
|
[text sizeToFit]; |
| 171 |
|
|
[text setStringValue:@"1"]; |
| 172 |
|
|
[text setDelegate:self]; |
| 173 |
|
|
|
| 174 |
|
|
return text; |
| 175 |
masaki |
124 |
} |
| 176 |
|
|
|
| 177 |
masaki |
128 |
|
| 178 |
masaki |
124 |
- (NSDictionary *)buildRows:(NSArray *)template |
| 179 |
|
|
{ |
| 180 |
|
|
NSMutableDictionary *result = [NSMutableDictionary dictionary]; |
| 181 |
|
|
for(id row in template) { |
| 182 |
masaki |
125 |
id criteria = [row valueForKey:XspfMREDCriteriaKey]; |
| 183 |
|
|
id name = [row valueForKey:XspfMREDNameKey]; |
| 184 |
masaki |
143 |
[result setObject:criteria forKey:name]; |
| 185 |
masaki |
124 |
} |
| 186 |
|
|
|
| 187 |
|
|
return result; |
| 188 |
|
|
} |
| 189 |
masaki |
126 |
- (id)criteriaWithKeyPath:(NSString *)keypath |
| 190 |
|
|
{ |
| 191 |
|
|
NSString *key = nil; |
| 192 |
|
|
if([keypath isEqualToString:@"title"]) { |
| 193 |
|
|
key = @"String"; |
| 194 |
|
|
} else if([keypath isEqualToString:@"rating"]) { |
| 195 |
|
|
key = @"Rate"; |
| 196 |
|
|
} |
| 197 |
|
|
if(key) { |
| 198 |
|
|
id row = [rowTemplate valueForKey:key]; |
| 199 |
masaki |
143 |
id c = [[[row objectAtIndex:0] mutableCopy] autorelease]; |
| 200 |
|
|
[c setValue:keypath forKey:XspfMREDValueKey]; |
| 201 |
masaki |
126 |
return [NSArray arrayWithObject:c]; |
| 202 |
|
|
} |
| 203 |
|
|
|
| 204 |
masaki |
143 |
if([[NSArray arrayWithObjects:@"lastPlayDate", @"modificationDate", @"creationDate", nil] containsObject:keypath]) { |
| 205 |
|
|
id keys = [NSArray arrayWithObjects:@"AbDate", /*@"RlDate",*/ nil]; |
| 206 |
|
|
id result = [NSMutableArray array]; |
| 207 |
|
|
for(key in keys) { |
| 208 |
|
|
id row = [rowTemplate valueForKey:key]; |
| 209 |
|
|
id c = [[[row objectAtIndex:0] mutableCopy] autorelease]; |
| 210 |
|
|
[c setValue:keypath forKey:XspfMREDValueKey]; |
| 211 |
|
|
[result addObject:c]; |
| 212 |
|
|
} |
| 213 |
|
|
|
| 214 |
|
|
return result; |
| 215 |
|
|
} |
| 216 |
|
|
|
| 217 |
masaki |
126 |
return nil; |
| 218 |
|
|
} |
| 219 |
masaki |
120 |
- (void)awakeFromNib |
| 220 |
|
|
{ |
| 221 |
|
|
if(!compound) { |
| 222 |
masaki |
143 |
compound = [[XspfMCompound alloc] init]; |
| 223 |
masaki |
120 |
} |
| 224 |
masaki |
124 |
|
| 225 |
|
|
rowIDs = [[NSMutableArray array] retain]; |
| 226 |
|
|
rowFields = [[NSMutableDictionary dictionary] retain]; |
| 227 |
|
|
|
| 228 |
|
|
NSBundle *m = [NSBundle mainBundle]; |
| 229 |
|
|
NSString *path = [m pathForResource:@"LibraryRowTemplate" ofType:@"plist"]; |
| 230 |
|
|
NSArray *rowsTemplate = [NSArray arrayWithContentsOfFile:path]; |
| 231 |
|
|
if(!rowsTemplate) { |
| 232 |
|
|
exit(12345); |
| 233 |
masaki |
120 |
} |
| 234 |
masaki |
124 |
|
| 235 |
masaki |
143 |
rowTemplate = [[self buildRows:rowsTemplate] retain]; |
| 236 |
masaki |
124 |
|
| 237 |
|
|
// NSLog(@"rowTemplate =>\n%@", rowTemplate); |
| 238 |
|
|
|
| 239 |
|
|
NSMutableArray *newRows = [NSMutableArray array]; |
| 240 |
|
|
|
| 241 |
masaki |
126 |
id c = [self criteriaWithKeyPath:@"title"]; |
| 242 |
|
|
if(c) [newRows addObjectsFromArray:c]; |
| 243 |
masaki |
124 |
|
| 244 |
|
|
for(id keyPath in [NSArray arrayWithObjects:@"lastPlayDate", @"modificationDate", @"creationDate", nil]) { |
| 245 |
masaki |
126 |
c = [self criteriaWithKeyPath:keyPath]; |
| 246 |
|
|
if(c) [newRows addObjectsFromArray:c]; |
| 247 |
masaki |
124 |
} |
| 248 |
|
|
|
| 249 |
masaki |
126 |
c = [self criteriaWithKeyPath:@"rating"]; |
| 250 |
|
|
if(c) [newRows addObjectsFromArray:c]; |
| 251 |
masaki |
124 |
|
| 252 |
|
|
rows = [newRows retain]; |
| 253 |
masaki |
125 |
|
| 254 |
|
|
|
| 255 |
masaki |
143 |
|
| 256 |
masaki |
125 |
//// |
| 257 |
|
|
predicateRows = [[NSMutableArray alloc] init]; |
| 258 |
|
|
[ruleEditor bind:XspfMREDRowsKey toObject:self withKeyPath:XspfMREDPredicateRowsKey options:nil]; |
| 259 |
masaki |
120 |
} |
| 260 |
|
|
|
| 261 |
masaki |
127 |
- (NSArray *)displayValuesWithPredicate:(NSComparisonPredicate *)predicate |
| 262 |
|
|
{ |
| 263 |
|
|
id value02 = nil; id value03 = nil; |
| 264 |
|
|
id leftKeyPath = [[predicate leftExpression] keyPath]; |
| 265 |
|
|
|
| 266 |
|
|
switch([predicate predicateOperatorType]) { |
| 267 |
|
|
case NSEqualToPredicateOperatorType: |
| 268 |
|
|
value02 = XspfMStringPredicateIsEqualOperator; |
| 269 |
|
|
break; |
| 270 |
|
|
case NSNotEqualToPredicateOperatorType: |
| 271 |
|
|
value02 = XspfMStringPredicateIsNotEqualOperator; |
| 272 |
|
|
break; |
| 273 |
|
|
case NSContainsPredicateOperatorType: |
| 274 |
|
|
value02 = XspfMStringPredicateContainsOperator; |
| 275 |
|
|
break; |
| 276 |
|
|
case NSBeginsWithPredicateOperatorType: |
| 277 |
|
|
value02 = XspfMStringPredicateBeginsWithOperator; |
| 278 |
|
|
break; |
| 279 |
|
|
case NSEndsWithPredicateOperatorType: |
| 280 |
|
|
value02 = XspfMStringPredicateEndsWithOperator; |
| 281 |
|
|
break; |
| 282 |
|
|
} |
| 283 |
|
|
id rightConstant = [[predicate rightExpression] constantValue]; |
| 284 |
|
|
value03 = [self textField]; |
| 285 |
|
|
[value03 setObjectValue:rightConstant]; |
| 286 |
|
|
|
| 287 |
|
|
id disp = [NSArray arrayWithObjects:leftKeyPath, value02, value03, nil]; |
| 288 |
|
|
|
| 289 |
|
|
return disp; |
| 290 |
|
|
} |
| 291 |
|
|
- (NSArray *)ratingDisplayValuesWithPredicate:(NSComparisonPredicate *)predicate |
| 292 |
|
|
{ |
| 293 |
|
|
id value02 = nil; id value03 = nil; |
| 294 |
|
|
id leftKeyPath = [[predicate leftExpression] keyPath]; |
| 295 |
|
|
|
| 296 |
|
|
switch([predicate predicateOperatorType]) { |
| 297 |
|
|
case NSEqualToPredicateOperatorType: |
| 298 |
|
|
value02 = XspfMStringPredicateIsEqualOperator; |
| 299 |
|
|
break; |
| 300 |
|
|
case NSGreaterThanPredicateOperatorType: |
| 301 |
|
|
value02 = @"is greater than"; |
| 302 |
|
|
break; |
| 303 |
|
|
case NSLessThanPredicateOperatorType: |
| 304 |
|
|
value02 = @"is less than"; |
| 305 |
|
|
break; |
| 306 |
|
|
} |
| 307 |
|
|
id rightConstant = [[predicate rightExpression] constantValue]; |
| 308 |
|
|
value03 = [self ratingIndicator]; |
| 309 |
|
|
[value03 setObjectValue:rightConstant]; |
| 310 |
|
|
|
| 311 |
|
|
id disp = [NSArray arrayWithObjects:leftKeyPath, value02, value03, nil]; |
| 312 |
masaki |
143 |
|
| 313 |
masaki |
127 |
return disp; |
| 314 |
|
|
} |
| 315 |
masaki |
138 |
- (void)resolveVariable:(NSString *)variable value02:(id *)value02 value03:(id *)value03 value04:(id *)value04 value05:(id *)value05 value06:(id *)value06 value07:(id *)value07 |
| 316 |
|
|
{ |
| 317 |
|
|
NSArray *words = [variable componentsSeparatedByString:@"-"]; |
| 318 |
|
|
|
| 319 |
|
|
switch([words count]) { |
| 320 |
|
|
case 2: |
| 321 |
|
|
*value02 = @"is in the last"; |
| 322 |
|
|
*value03 = [self numberField]; |
| 323 |
|
|
[*value03 setTag:2000]; |
| 324 |
|
|
[*value03 setStringValue:[words objectAtIndex:0]]; |
| 325 |
|
|
*value04 = [words objectAtIndex:1]; |
| 326 |
|
|
break; |
| 327 |
|
|
case 3: |
| 328 |
|
|
if([[words objectAtIndex:0] isEqualToString:@"not"]) { |
| 329 |
|
|
*value02 = @"is not in the last"; |
| 330 |
|
|
*value03 = [self numberField]; |
| 331 |
|
|
[*value03 setTag:2000]; |
| 332 |
|
|
[*value03 setStringValue:[words objectAtIndex:1]]; |
| 333 |
|
|
*value04 = [words objectAtIndex:2]; |
| 334 |
|
|
} else { |
| 335 |
|
|
*value02 = @"is exactly"; |
| 336 |
|
|
*value03 = [self numberField]; |
| 337 |
|
|
[*value03 setTag:2000]; |
| 338 |
|
|
[*value03 setStringValue:[words objectAtIndex:0]]; |
| 339 |
|
|
*value04 = [words objectAtIndex:1]; |
| 340 |
|
|
*value05 = @"ago"; |
| 341 |
|
|
} |
| 342 |
|
|
break; |
| 343 |
|
|
case 4: |
| 344 |
|
|
*value02 = @"is between"; |
| 345 |
|
|
*value03 = [self numberField]; |
| 346 |
|
|
[*value03 setTag:2000]; |
| 347 |
|
|
[*value03 setStringValue:[words objectAtIndex:0]]; |
| 348 |
|
|
*value04 = @"and"; |
| 349 |
|
|
*value05 = [self numberField]; |
| 350 |
|
|
[*value05 setTag:2100]; |
| 351 |
|
|
[*value05 setStringValue:[words objectAtIndex:2]]; |
| 352 |
|
|
*value06 = [words objectAtIndex:3]; |
| 353 |
|
|
*value07 = @"ago"; |
| 354 |
|
|
break; |
| 355 |
|
|
} |
| 356 |
|
|
} |
| 357 |
masaki |
146 |
- (void)rangeDateDisplayValuesWithExpression:(NSExpression *)rightExp value02:(id *)value02 value03:(id *)value03 value04:(id *)value04 value05:(id *)value05 |
| 358 |
|
|
{ |
| 359 |
|
|
NSExpression *firstExp = [[rightExp collection] objectAtIndex:0]; |
| 360 |
|
|
NSExpression *secondExp = [[rightExp collection] objectAtIndex:1]; |
| 361 |
|
|
|
| 362 |
|
|
*value02 = @"is in the range"; |
| 363 |
|
|
*value03 = [self datePicker]; |
| 364 |
|
|
[*value03 setObjectValue:[firstExp constantValue]]; |
| 365 |
|
|
*value04 = @"to"; |
| 366 |
|
|
*value05 = [self datePicker]; |
| 367 |
|
|
[*value05 setObjectValue:[secondExp constantValue]]; |
| 368 |
|
|
[*value05 setTag:1000]; |
| 369 |
|
|
} |
| 370 |
masaki |
138 |
|
| 371 |
masaki |
132 |
- (NSArray *)dateRangeDisplayValuesWithPredicate:(NSComparisonPredicate *)predicate |
| 372 |
|
|
{ |
| 373 |
|
|
id leftKeyPath = [[predicate leftExpression] keyPath]; |
| 374 |
masaki |
146 |
id rightVar = nil; |
| 375 |
|
|
if([[predicate rightExpression] expressionType] == NSVariableExpressionType) { |
| 376 |
|
|
rightVar = [[predicate rightExpression] variable]; |
| 377 |
|
|
} |
| 378 |
masaki |
132 |
|
| 379 |
|
|
id value02 = nil; |
| 380 |
|
|
id value03 = nil; |
| 381 |
|
|
id value04 = nil; |
| 382 |
|
|
id value05 = nil; |
| 383 |
|
|
id value06 = nil; |
| 384 |
|
|
id value07 = nil; |
| 385 |
|
|
|
| 386 |
masaki |
146 |
if(!rightVar) { |
| 387 |
|
|
[self rangeDateDisplayValuesWithExpression:[predicate rightExpression] value02:&value02 value03:&value03 value04:&value04 value05:&value05]; |
| 388 |
|
|
} |
| 389 |
|
|
|
| 390 |
masaki |
132 |
if([rightVar isEqualToString:@"TODAY"]) { |
| 391 |
|
|
value02 = @"is today"; |
| 392 |
|
|
} else if([rightVar isEqualToString:@"YESTERDAY"]) { |
| 393 |
|
|
value02 = @"is yesterday"; |
| 394 |
|
|
} else if([rightVar isEqualToString:@"THISWEEK"]) { |
| 395 |
|
|
value02 = @"is this week"; |
| 396 |
|
|
} else if([rightVar isEqualToString:@"LASTWEEK"]) { |
| 397 |
|
|
value02 = @"is last week"; |
| 398 |
masaki |
138 |
} else { |
| 399 |
|
|
[self resolveVariable:rightVar value02:&value02 value03:&value03 value04:&value04 value05:&value05 value06:&value06 value07:&value07]; |
| 400 |
masaki |
132 |
} |
| 401 |
|
|
|
| 402 |
|
|
return [NSArray arrayWithObjects:leftKeyPath, value02, value03, value04, value05, value06, value07, nil]; |
| 403 |
|
|
} |
| 404 |
|
|
|
| 405 |
masaki |
127 |
- (NSArray *)dateDisplayValuesWithPredicate:(NSComparisonPredicate *)predicate |
| 406 |
|
|
{ |
| 407 |
|
|
id value02 = nil; id value03 = nil; |
| 408 |
|
|
id leftKeyPath = [[predicate leftExpression] keyPath]; |
| 409 |
|
|
|
| 410 |
|
|
switch([predicate predicateOperatorType]) { |
| 411 |
|
|
case NSEqualToPredicateOperatorType: |
| 412 |
|
|
value02 = @"is the date"; |
| 413 |
|
|
break; |
| 414 |
|
|
case NSGreaterThanPredicateOperatorType: |
| 415 |
|
|
value02 = @"is after the date"; |
| 416 |
|
|
break; |
| 417 |
|
|
case NSLessThanPredicateOperatorType: |
| 418 |
|
|
value02 = @"is before the date"; |
| 419 |
|
|
break; |
| 420 |
|
|
case NSBetweenPredicateOperatorType: |
| 421 |
|
|
return [self dateRangeDisplayValuesWithPredicate:predicate]; |
| 422 |
|
|
|
| 423 |
|
|
} |
| 424 |
|
|
id rightConstant = [[predicate rightExpression] constantValue]; |
| 425 |
|
|
value03 = [self datePicker]; |
| 426 |
|
|
[value03 setObjectValue:rightConstant]; |
| 427 |
|
|
|
| 428 |
|
|
id disp = [NSArray arrayWithObjects:leftKeyPath, value02, value03, nil]; |
| 429 |
|
|
|
| 430 |
|
|
return disp; |
| 431 |
|
|
} |
| 432 |
|
|
|
| 433 |
masaki |
143 |
- (id)criterionFromCriteria:(id)criteria withDisplayValues:(NSArray *)displayValues |
| 434 |
|
|
{ |
| 435 |
|
|
NSMutableArray *result = [NSMutableArray array]; |
| 436 |
|
|
|
| 437 |
|
|
NSInteger index = 0; |
| 438 |
|
|
|
| 439 |
|
|
do { |
| 440 |
|
|
id displayValue = [displayValues objectAtIndex:index]; |
| 441 |
|
|
id hitCriterion = nil; |
| 442 |
|
|
for(id criterion in criteria) { |
| 443 |
|
|
id value = [criterion valueForKey:@"value"]; |
| 444 |
|
|
if([value isEqualToString:displayValue]) { |
| 445 |
|
|
hitCriterion = criterion; |
| 446 |
|
|
break; |
| 447 |
|
|
} |
| 448 |
|
|
|
| 449 |
|
|
if(![displayValue isKindOfClass:[NSControl class]]) continue; |
| 450 |
|
|
|
| 451 |
|
|
Class fieldClass = Nil; |
| 452 |
|
|
NSInteger tag = 0; |
| 453 |
|
|
if([value hasPrefix:@"textField"]) { |
| 454 |
|
|
fieldClass = [NSTextField class]; |
| 455 |
|
|
} else if([value hasPrefix:@"dateField"]) { |
| 456 |
|
|
fieldClass = [NSDatePicker class]; |
| 457 |
|
|
if(![value isEqualToString:@"dateField"]) { // result == dateField02 |
| 458 |
|
|
tag = 1000; |
| 459 |
|
|
} |
| 460 |
|
|
} else if([value hasPrefix:@"rateField"]) { |
| 461 |
|
|
fieldClass = [NSLevelIndicator class]; |
| 462 |
|
|
} else if([value hasPrefix:@"numberField"]) { |
| 463 |
|
|
fieldClass = [NSTextField class]; |
| 464 |
|
|
if([value isEqualToString:@"numberField"]) { |
| 465 |
|
|
tag = 2000; |
| 466 |
|
|
} else { // result == numberField02 |
| 467 |
|
|
tag = 2100; |
| 468 |
|
|
} |
| 469 |
|
|
} |
| 470 |
|
|
if(!fieldClass) continue; |
| 471 |
|
|
|
| 472 |
|
|
if([displayValue isKindOfClass:fieldClass] && [displayValue tag] == tag) { |
| 473 |
|
|
hitCriterion = criterion; |
| 474 |
|
|
break; |
| 475 |
|
|
} |
| 476 |
|
|
} |
| 477 |
|
|
|
| 478 |
|
|
if(hitCriterion) { |
| 479 |
|
|
[result addObject:hitCriterion]; |
| 480 |
|
|
} |
| 481 |
|
|
|
| 482 |
|
|
criteria = [hitCriterion valueForKey:@"criteria"]; |
| 483 |
|
|
index++; |
| 484 |
|
|
} while(criteria); |
| 485 |
|
|
|
| 486 |
|
|
return result; |
| 487 |
|
|
} |
| 488 |
|
|
|
| 489 |
masaki |
125 |
- (id)buildRowsFromPredicate:(id)predicate |
| 490 |
|
|
{ |
| 491 |
|
|
if([predicate isKindOfClass:[NSCompoundPredicate class]]) { |
| 492 |
|
|
id subrows = [NSMutableArray array]; |
| 493 |
|
|
|
| 494 |
|
|
id value = nil; |
| 495 |
|
|
switch([predicate compoundPredicateType]) { |
| 496 |
|
|
case NSNotPredicateType: |
| 497 |
|
|
// ? |
| 498 |
|
|
break; |
| 499 |
|
|
case NSAndPredicateType: |
| 500 |
|
|
value = @"All"; |
| 501 |
|
|
break; |
| 502 |
|
|
case NSOrPredicateType: |
| 503 |
|
|
value = @"Any"; |
| 504 |
|
|
break; |
| 505 |
|
|
} |
| 506 |
|
|
|
| 507 |
|
|
NSArray *sub = [predicate subpredicates]; |
| 508 |
|
|
for(id p in sub) { |
| 509 |
|
|
[subrows addObject:[self buildRowsFromPredicate:p]]; |
| 510 |
|
|
} |
| 511 |
|
|
|
| 512 |
masaki |
143 |
id criteria = [NSArray arrayWithObjects:value, @"of the following are true", nil]; |
| 513 |
masaki |
127 |
id type = [NSNumber numberWithInt:NSRuleEditorRowTypeCompound]; |
| 514 |
masaki |
125 |
|
| 515 |
|
|
id result = [NSMutableDictionary dictionaryWithObjectsAndKeys: |
| 516 |
|
|
criteria, XspfMREDCriteriaKey, |
| 517 |
masaki |
143 |
criteria, XspfMREDDisplayValuesKey, |
| 518 |
masaki |
125 |
type, XspfMREDRowTypeKey, |
| 519 |
|
|
subrows, XspfMREDSubrowsKey, |
| 520 |
|
|
nil]; |
| 521 |
|
|
|
| 522 |
|
|
return result; |
| 523 |
|
|
} else if([predicate isKindOfClass:[NSComparisonPredicate class]]) { |
| 524 |
|
|
id leftKeyPath = [[predicate leftExpression] keyPath]; |
| 525 |
|
|
if(!leftKeyPath) return [NSArray array]; |
| 526 |
masaki |
128 |
|
| 527 |
|
|
NSArray *disp = nil; |
| 528 |
masaki |
127 |
if([leftKeyPath isEqualToString:@"title"]) { |
| 529 |
masaki |
128 |
disp = [self displayValuesWithPredicate:predicate]; |
| 530 |
masaki |
125 |
} |
| 531 |
masaki |
127 |
if([leftKeyPath isEqualToString:@"rating"]) { |
| 532 |
masaki |
128 |
disp = [self ratingDisplayValuesWithPredicate:predicate]; |
| 533 |
masaki |
127 |
} |
| 534 |
masaki |
132 |
if([[NSArray arrayWithObjects:@"lastPlayDate", @"modificationDate", @"creationDate", nil] containsObject:leftKeyPath]) { |
| 535 |
masaki |
128 |
disp = [self dateDisplayValuesWithPredicate:predicate]; |
| 536 |
masaki |
147 |
// NSLog(@"dispalyValues -> %@", disp); |
| 537 |
masaki |
128 |
} |
| 538 |
|
|
|
| 539 |
|
|
if(disp) { |
| 540 |
masaki |
127 |
NSArray *row = [self criteriaWithKeyPath:leftKeyPath]; |
| 541 |
masaki |
143 |
id c = [self criterionFromCriteria:row withDisplayValues:disp]; |
| 542 |
masaki |
127 |
NSMutableDictionary *criterion = [NSMutableDictionary dictionaryWithObjectsAndKeys: |
| 543 |
masaki |
143 |
c, XspfMREDCriteriaKey, |
| 544 |
masaki |
127 |
disp, XspfMREDDisplayValuesKey, |
| 545 |
|
|
[NSNumber numberWithInt:NSRuleEditorRowTypeSimple], XspfMREDRowTypeKey, |
| 546 |
|
|
nil]; |
| 547 |
|
|
return criterion; |
| 548 |
|
|
} |
| 549 |
masaki |
125 |
|
| 550 |
|
|
} else if([predicate isKindOfClass:[NSPredicate class]]) { |
| 551 |
|
|
NSLog(@"--> %@", predicate); |
| 552 |
|
|
} else { |
| 553 |
|
|
NSLog(@"???predicate class is %@", NSStringFromClass([predicate class])); |
| 554 |
|
|
} |
| 555 |
|
|
|
| 556 |
|
|
return [NSArray array]; |
| 557 |
|
|
} |
| 558 |
masaki |
150 |
- (void)resolveExpression:(id)exp |
| 559 |
|
|
{ |
| 560 |
|
|
NSString *message = nil; |
| 561 |
|
|
|
| 562 |
|
|
switch([exp expressionType]) { |
| 563 |
|
|
case NSConstantValueExpressionType: |
| 564 |
|
|
message = [NSString stringWithFormat:@"constant -> %@", [exp constantValue]]; |
| 565 |
|
|
break; |
| 566 |
|
|
case NSEvaluatedObjectExpressionType: |
| 567 |
|
|
message = [NSString stringWithFormat:@"constant -> %@", [exp constantValue]]; |
| 568 |
|
|
break; |
| 569 |
|
|
case NSVariableExpressionType: |
| 570 |
|
|
message = [NSString stringWithFormat:@"variable -> %@", [exp variable]]; |
| 571 |
|
|
break; |
| 572 |
|
|
case NSKeyPathExpressionType: |
| 573 |
|
|
message = [NSString stringWithFormat:@"keyPath -> %@", [exp keyPath]]; |
| 574 |
|
|
break; |
| 575 |
|
|
case NSFunctionExpressionType: |
| 576 |
|
|
message = [NSString stringWithFormat:@"oprand -> %@(%@)function -> %@, argumect -> %@", |
| 577 |
|
|
[exp operand], NSStringFromClass([[exp operand] class]), |
| 578 |
|
|
[exp function], [exp arguments]]; |
| 579 |
|
|
break; |
| 580 |
|
|
case NSAggregateExpressionType: |
| 581 |
|
|
message = [NSString stringWithFormat:@"collection -> %@", [exp collection]]; |
| 582 |
|
|
break; |
| 583 |
|
|
} |
| 584 |
|
|
|
| 585 |
|
|
fprintf(stderr, "%s\n", [message UTF8String]); |
| 586 |
|
|
} |
| 587 |
|
|
- (void)resolvePredicate:(id)predicate |
| 588 |
|
|
{ |
| 589 |
|
|
if([predicate isKindOfClass:[NSCompoundPredicate class]]) { |
| 590 |
|
|
NSArray *sub = [predicate subpredicates]; |
| 591 |
|
|
for(id p in sub) { |
| 592 |
|
|
[self resolvePredicate:p]; |
| 593 |
|
|
} |
| 594 |
|
|
} else if([predicate isKindOfClass:[NSComparisonPredicate class]]) { |
| 595 |
|
|
id left = [predicate leftExpression]; |
| 596 |
|
|
id right = [predicate rightExpression]; |
| 597 |
|
|
SEL sel = Nil; |
| 598 |
|
|
if([predicate predicateOperatorType] == NSCustomSelectorPredicateOperatorType) { |
| 599 |
|
|
sel = [predicate customSelector]; |
| 600 |
|
|
} |
| 601 |
|
|
fprintf(stderr, "left ->\t"); |
| 602 |
|
|
[self resolveExpression:left]; |
| 603 |
|
|
if(sel) { |
| 604 |
|
|
fprintf(stderr, "%s\n", [[NSString stringWithFormat:@"SEL -> %@", NSStringFromSelector(sel)] UTF8String]); |
| 605 |
|
|
} else { |
| 606 |
|
|
fprintf(stderr, "%s\n", [[NSString stringWithFormat:@"type -> %d, opt -> %d, mod -> %d", [predicate predicateOperatorType], [predicate options], [predicate comparisonPredicateModifier]] UTF8String]); |
| 607 |
|
|
} |
| 608 |
|
|
fprintf(stderr, "right ->\t"); |
| 609 |
|
|
[self resolveExpression:right]; |
| 610 |
|
|
fprintf(stderr, "end resolve.\n"); |
| 611 |
|
|
} |
| 612 |
|
|
} |
| 613 |
|
|
|
| 614 |
masaki |
123 |
- (void)setPredicate:(id)predicate |
| 615 |
|
|
{ |
| 616 |
masaki |
150 |
NSLog(@"predicate -> (%@) %@", NSStringFromClass([predicate class]), predicate); |
| 617 |
|
|
[self resolvePredicate:predicate]; |
| 618 |
masaki |
123 |
|
| 619 |
masaki |
150 |
|
| 620 |
masaki |
125 |
id hoge = [self buildRowsFromPredicate:predicate]; |
| 621 |
|
|
id new = [NSArray arrayWithObject:hoge]; |
| 622 |
|
|
|
| 623 |
|
|
[self willChangeValueForKey:XspfMREDPredicateRowsKey]; |
| 624 |
|
|
[predicateRows release]; |
| 625 |
|
|
predicateRows = [new retain]; |
| 626 |
|
|
[self didChangeValueForKey:XspfMREDPredicateRowsKey]; |
| 627 |
masaki |
146 |
// [ruleEditor reloadCriteria]; |
| 628 |
masaki |
123 |
} |
| 629 |
masaki |
125 |
- (void)setPredicateRows:(id)p |
| 630 |
|
|
{ |
| 631 |
masaki |
147 |
// NSLog(@"new -> %@", p); |
| 632 |
masaki |
125 |
[predicateRows release]; |
| 633 |
|
|
predicateRows = [p retain]; |
| 634 |
|
|
} |
| 635 |
|
|
|
| 636 |
|
|
#pragma mark#### NSRleEditor Delegate #### |
| 637 |
masaki |
124 |
- (NSInteger)ruleEditor:(NSRuleEditor *)editor |
| 638 |
|
|
numberOfChildrenForCriterion:(id)criterion |
| 639 |
|
|
withRowType:(NSRuleEditorRowType)rowType |
| 640 |
|
|
{ |
| 641 |
|
|
NSInteger result = 0; |
| 642 |
|
|
|
| 643 |
masaki |
143 |
if(rowType == NSRuleEditorRowTypeCompound) { |
| 644 |
|
|
return [compound numberOfChildrenForChild:criterion]; |
| 645 |
|
|
} |
| 646 |
|
|
|
| 647 |
masaki |
124 |
if(!criterion) { |
| 648 |
masaki |
143 |
result = [rows count]; |
| 649 |
|
|
} else { |
| 650 |
|
|
result = [[criterion valueForKey:XspfMREDCriteriaKey] count]; |
| 651 |
masaki |
124 |
} |
| 652 |
|
|
|
| 653 |
|
|
// NSLog(@"numner\tcriterion -> %@, type -> %d, result -> %d", criterion, rowType, result); |
| 654 |
|
|
|
| 655 |
|
|
return result; |
| 656 |
|
|
} |
| 657 |
|
|
|
| 658 |
|
|
- (id)ruleEditor:(NSRuleEditor *)editor |
| 659 |
|
|
child:(NSInteger)index |
| 660 |
|
|
forCriterion:(id)criterion |
| 661 |
|
|
withRowType:(NSRuleEditorRowType)rowType |
| 662 |
|
|
{ |
| 663 |
|
|
id result = nil; |
| 664 |
|
|
|
| 665 |
masaki |
143 |
if(rowType == NSRuleEditorRowTypeCompound) { |
| 666 |
|
|
return [compound childForChild:criterion atIndex:index]; |
| 667 |
|
|
} |
| 668 |
|
|
|
| 669 |
masaki |
124 |
if(!criterion) { |
| 670 |
masaki |
143 |
result = [rows objectAtIndex:index]; |
| 671 |
|
|
} else { |
| 672 |
|
|
result = [[criterion valueForKey:XspfMREDCriteriaKey] objectAtIndex:index]; |
| 673 |
masaki |
124 |
} |
| 674 |
|
|
|
| 675 |
|
|
// NSLog(@"child\tindex -> %d, criterion -> %@, type -> %d, result -> %@", index, criterion, rowType, result); |
| 676 |
|
|
|
| 677 |
|
|
return result; |
| 678 |
|
|
} |
| 679 |
|
|
- (id)ruleEditor:(NSRuleEditor *)editor |
| 680 |
|
|
displayValueForCriterion:(id)criterion |
| 681 |
|
|
inRow:(NSInteger)row |
| 682 |
|
|
{ |
| 683 |
|
|
id result = nil; |
| 684 |
masaki |
143 |
|
| 685 |
|
|
NSRuleEditorRowType rowType = [editor rowTypeForRow:row]; |
| 686 |
|
|
if(rowType == NSRuleEditorRowTypeCompound) { |
| 687 |
|
|
return [compound displayValueForChild:criterion]; |
| 688 |
|
|
} |
| 689 |
|
|
|
| 690 |
|
|
if(!criterion) { |
| 691 |
|
|
// |
| 692 |
|
|
} else { |
| 693 |
|
|
result = [criterion valueForKey:XspfMREDValueKey]; |
| 694 |
|
|
} |
| 695 |
|
|
|
| 696 |
|
|
if([result isEqualToString:@"separator"]) { |
| 697 |
|
|
return [NSMenuItem separatorItem]; |
| 698 |
|
|
} |
| 699 |
|
|
|
| 700 |
|
|
// create or find field object. |
| 701 |
|
|
do { |
| 702 |
|
|
Class searchClass = Nil; |
| 703 |
|
|
SEL defaultSEL = Nil; |
| 704 |
|
|
NSInteger tag = 0; |
| 705 |
|
|
|
| 706 |
|
|
if([result hasPrefix:@"textField"]) { |
| 707 |
|
|
searchClass = [NSTextField class]; |
| 708 |
|
|
defaultSEL = @selector(textField); |
| 709 |
|
|
} else if([result hasPrefix:@"dateField"]) { |
| 710 |
|
|
searchClass = [NSDatePicker class]; |
| 711 |
|
|
defaultSEL = @selector(datePicker); |
| 712 |
|
|
if(![result isEqualToString:@"dateField"]) { // result == dateField02 |
| 713 |
|
|
tag = 1000; |
| 714 |
|
|
} |
| 715 |
|
|
} else if([result hasPrefix:@"rateField"]) { |
| 716 |
|
|
searchClass = [NSLevelIndicator class]; |
| 717 |
|
|
defaultSEL = @selector(ratingIndicator); |
| 718 |
|
|
} else if([result hasPrefix:@"numberField"]) { |
| 719 |
|
|
searchClass = [NSTextField class]; |
| 720 |
|
|
defaultSEL = @selector(numberField); |
| 721 |
|
|
if([result isEqualToString:@"numberField"]) { |
| 722 |
|
|
tag = 2000; |
| 723 |
|
|
} else { // result == numberField02 |
| 724 |
|
|
tag = 2100; |
| 725 |
|
|
} |
| 726 |
|
|
} |
| 727 |
|
|
if(!searchClass) break; |
| 728 |
|
|
|
| 729 |
|
|
id displayValues = [editor displayValuesForRow:row]; |
| 730 |
|
|
id field = nil; |
| 731 |
|
|
for(id v in displayValues) { |
| 732 |
|
|
if([v isKindOfClass:searchClass] && [v tag] == tag) { |
| 733 |
|
|
field = v; |
| 734 |
|
|
} |
| 735 |
|
|
} |
| 736 |
|
|
result = field ? field :[self performSelector:defaultSEL]; |
| 737 |
|
|
if(tag != 0) [result setTag:tag]; |
| 738 |
|
|
} while(NO); |
| 739 |
|
|
|
| 740 |
masaki |
124 |
// NSLog(@"display\tcriterion -> %@, row -> %d, result -> %@", criterion, row, result); |
| 741 |
|
|
|
| 742 |
|
|
return result; |
| 743 |
|
|
} |
| 744 |
|
|
- (NSDictionary *)ruleEditor:(NSRuleEditor *)editor |
| 745 |
|
|
predicatePartsForCriterion:(id)criterion |
| 746 |
|
|
withDisplayValue:(id)displayValue |
| 747 |
|
|
inRow:(NSInteger)row |
| 748 |
|
|
{ |
| 749 |
|
|
id result = nil; |
| 750 |
|
|
|
| 751 |
masaki |
143 |
NSRuleEditorRowType rowType = [editor rowTypeForRow:row]; |
| 752 |
|
|
if(rowType == NSRuleEditorRowTypeCompound) { |
| 753 |
|
|
return [compound predicateForChild:criterion withDisplayValue:displayValue]; |
| 754 |
|
|
} |
| 755 |
|
|
|
| 756 |
|
|
if([criterion valueForKey:@"XspfMIgnoreExpression"]) return nil; |
| 757 |
|
|
|
| 758 |
|
|
result = [NSMutableDictionary dictionary]; |
| 759 |
|
|
|
| 760 |
|
|
if([criterion valueForKey:@"NSRuleEditorPredicateOperatorType"]) { |
| 761 |
|
|
[result setValue:[criterion valueForKey:@"NSRuleEditorPredicateOperatorType"] forKey:@"NSRuleEditorPredicateOperatorType"]; |
| 762 |
|
|
} |
| 763 |
|
|
if([criterion valueForKey:@"NSRuleEditorPredicateOptions"]) { |
| 764 |
|
|
[result setValue:[criterion valueForKey:@"NSRuleEditorPredicateOptions"] forKey:@"NSRuleEditorPredicateOptions"]; |
| 765 |
|
|
} |
| 766 |
|
|
if([criterion valueForKey:@"NSRuleEditorPredicateLeftExpression"]) { |
| 767 |
|
|
id value = [criterion valueForKey:@"NSRuleEditorPredicateLeftExpression"]; |
| 768 |
|
|
id exp = nil; |
| 769 |
|
|
if([value isEqual:XspfMREDValueKey]) { |
| 770 |
|
|
exp = [NSExpression expressionForKeyPath:displayValue]; |
| 771 |
|
|
} else { |
| 772 |
|
|
exp = [NSExpression expressionForKeyPath:[criterion valueForKey:@"NSRuleEditorPredicateLeft"]]; |
| 773 |
|
|
} |
| 774 |
|
|
if(exp) { |
| 775 |
|
|
[result setValue:exp forKey:@"NSRuleEditorPredicateLeftExpression"]; |
| 776 |
|
|
} |
| 777 |
|
|
} |
| 778 |
|
|
if([criterion valueForKey:@"NSRuleEditorPredicateRightExpression"]) { |
| 779 |
|
|
id selector = [criterion valueForKey:@"NSRuleEditorPredicateRightExpression"]; |
| 780 |
|
|
id exp = nil; |
| 781 |
|
|
if(NSSelectorFromString(selector)) { |
| 782 |
|
|
exp = [NSExpression expressionForConstantValue:[displayValue performSelector:NSSelectorFromString(selector)]]; |
| 783 |
|
|
} else { |
| 784 |
|
|
exp = [NSExpression expressionForConstantValue:[criterion valueForKey:@"NSRuleEditorPredicateRightExpression"]]; |
| 785 |
|
|
} |
| 786 |
|
|
if(exp) { |
| 787 |
|
|
[result setValue:exp forKey:@"NSRuleEditorPredicateRightExpression"]; |
| 788 |
|
|
} |
| 789 |
|
|
} |
| 790 |
|
|
if([criterion valueForKey:@"XspfMPredicateRightExpression"]) { |
| 791 |
|
|
SEL selector = NSSelectorFromString([criterion valueForKey:@"XspfMPredicateRightExpression"]); |
| 792 |
|
|
id arg01 = [criterion valueForKey:@"XspfMRightExpressionArg01"]; |
| 793 |
|
|
id arg02 = [criterion valueForKey:@"XspfMRightExpressionArg02"]; |
| 794 |
|
|
|
| 795 |
|
|
|
| 796 |
|
|
if(arg02 && arg01) { |
| 797 |
|
|
if([arg01 isEqual:XspfMREDDisplayValuesKey]) { |
| 798 |
|
|
arg01 = [editor displayValuesForRow:row]; |
| 799 |
|
|
} |
| 800 |
|
|
if([arg02 isEqual:XspfMREDDisplayValuesKey]) { |
| 801 |
|
|
arg02 = [editor displayValuesForRow:row]; |
| 802 |
|
|
} |
| 803 |
|
|
id r = [self performSelector:selector withObject:arg01 withObject:arg02]; |
| 804 |
|
|
[result setValue:r forKey:@"NSRuleEditorPredicateRightExpression"]; |
| 805 |
|
|
} else if(arg01) { |
| 806 |
|
|
if([arg01 isEqual:XspfMREDDisplayValuesKey]) { |
| 807 |
|
|
arg01 = [editor displayValuesForRow:row]; |
| 808 |
|
|
} |
| 809 |
|
|
id r = [self performSelector:selector withObject:arg01]; |
| 810 |
|
|
[result setValue:r forKey:@"NSRuleEditorPredicateRightExpression"]; |
| 811 |
|
|
} else { |
| 812 |
|
|
id r = [self performSelector:selector]; |
| 813 |
|
|
[result setValue:r forKey:@"NSRuleEditorPredicateRightExpression"]; |
| 814 |
|
|
} |
| 815 |
|
|
} |
| 816 |
masaki |
151 |
if([criterion valueForKey:@"XspfMCustomSelector"]) { |
| 817 |
|
|
id selName = [criterion valueForKey:@"XspfMCustomSelector"]; |
| 818 |
masaki |
150 |
id arg01 = [criterion valueForKey:@"XspfMCustomSelectorArg"]; |
| 819 |
|
|
id exp = [NSArray arrayWithObjects:[NSExpression expressionForConstantValue:arg01], nil]; |
| 820 |
|
|
|
| 821 |
|
|
id exp03 = [NSExpression expressionForConstantValue:@"DATE_RANGE_CREATOR"]; |
| 822 |
|
|
id exp02 = [NSExpression expressionForFunction:exp03 selectorName:selName arguments:exp]; |
| 823 |
|
|
[result setValue:exp02 forKey:@"NSRuleEditorPredicateRightExpression"]; |
| 824 |
|
|
// [result setValue:[criterion valueForKey:@"NSRuleEditorPredicateCustomSelector"] forKey:@"NSRuleEditorPredicateCustomSelector"]; |
| 825 |
|
|
} |
| 826 |
|
|
NSLog(@"predicate\tresult -> %@", result); |
| 827 |
|
|
|
| 828 |
masaki |
143 |
// NSLog(@"predicate\tcriterion -> %@, value -> %@, row -> %d, result -> %@", criterion, displayValue, row, result); |
| 829 |
|
|
|
| 830 |
masaki |
124 |
return result; |
| 831 |
|
|
} |
| 832 |
|
|
- (void)ruleEditorRowsDidChange:(NSNotification *)notification |
| 833 |
|
|
{ |
| 834 |
|
|
// |
| 835 |
|
|
} |
| 836 |
masaki |
150 |
@end |
| 837 |
masaki |
124 |
|
| 838 |
masaki |
150 |
@implementation NSString (XspfMTest) |
| 839 |
|
|
- (NSArray *)rangeOfToday |
| 840 |
|
|
{ |
| 841 |
|
|
NSCalendar *aCalendar = [NSCalendar currentCalendar]; |
| 842 |
|
|
NSDate *now = [NSDate dateWithTimeIntervalSinceNow:0.0]; |
| 843 |
|
|
NSDateComponents *nowComp = [aCalendar components:NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit |
| 844 |
|
|
fromDate:now]; |
| 845 |
|
|
NSDate *startOfToday = [aCalendar dateFromComponents:nowComp]; |
| 846 |
|
|
|
| 847 |
|
|
id result = [NSArray arrayWithObjects:startOfToday, now, nil]; |
| 848 |
|
|
return result; |
| 849 |
|
|
} |
| 850 |
|
|
- (NSArray *)rangeOfYesterday |
| 851 |
|
|
{ |
| 852 |
|
|
|
| 853 |
|
|
NSCalendar *aCalendar = [NSCalendar currentCalendar]; |
| 854 |
|
|
NSDate *now = [NSDate dateWithTimeIntervalSinceNow:0.0]; |
| 855 |
|
|
NSDateComponents *nowComp = [aCalendar components:NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit |
| 856 |
|
|
fromDate:now]; |
| 857 |
|
|
NSDate *startOfToday = [aCalendar dateFromComponents:nowComp]; |
| 858 |
|
|
|
| 859 |
|
|
NSDateComponents *comp = [[NSDateComponents alloc] init]; |
| 860 |
|
|
[comp setDay:-1]; |
| 861 |
|
|
NSDate *startOfYesterday = [aCalendar dateByAddingComponents:comp toDate:startOfToday options:0]; |
| 862 |
|
|
|
| 863 |
|
|
id result = [NSArray arrayWithObjects:startOfYesterday, startOfToday, nil]; |
| 864 |
|
|
return result; |
| 865 |
|
|
} |
| 866 |
|
|
- (NSArray *)rangeOfThisWeek |
| 867 |
|
|
{ |
| 868 |
|
|
NSCalendar *aCalendar = [NSCalendar currentCalendar]; |
| 869 |
|
|
NSDate *now = [NSDate dateWithTimeIntervalSinceNow:0.0]; |
| 870 |
|
|
NSDateComponents *nowComp = [aCalendar components:NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit | NSWeekdayCalendarUnit |
| 871 |
|
|
fromDate:now]; |
| 872 |
|
|
NSDate *startOfToday = [aCalendar dateFromComponents:nowComp]; |
| 873 |
|
|
|
| 874 |
|
|
NSDateComponents *comp = [[NSDateComponents alloc] init]; |
| 875 |
|
|
|
| 876 |
|
|
[comp setWeekday:-[nowComp weekday]+1]; |
| 877 |
|
|
NSDate *startOfThisWeek = [aCalendar dateByAddingComponents:comp toDate:startOfToday options:0]; |
| 878 |
|
|
|
| 879 |
|
|
id result = [NSArray arrayWithObjects:startOfThisWeek, now, nil]; |
| 880 |
|
|
return result; |
| 881 |
|
|
} |
| 882 |
|
|
- (NSArray *)rangeOfLastWeek |
| 883 |
|
|
{ |
| 884 |
|
|
NSCalendar *aCalendar = [NSCalendar currentCalendar]; |
| 885 |
|
|
NSDate *now = [NSDate dateWithTimeIntervalSinceNow:0.0]; |
| 886 |
|
|
NSDateComponents *nowComp = [aCalendar components:NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit | NSWeekdayCalendarUnit |
| 887 |
|
|
fromDate:now]; |
| 888 |
|
|
NSDate *startOfToday = [aCalendar dateFromComponents:nowComp]; |
| 889 |
|
|
|
| 890 |
|
|
NSDateComponents *comp = [[NSDateComponents alloc] init]; |
| 891 |
|
|
[comp setWeekday:-[nowComp weekday]+1]; |
| 892 |
|
|
NSDate *startOfThisWeek = [aCalendar dateByAddingComponents:comp toDate:startOfToday options:0]; |
| 893 |
|
|
|
| 894 |
|
|
[comp setWeekday:-[nowComp weekday]+1]; |
| 895 |
|
|
[comp setWeek:-1]; |
| 896 |
|
|
NSDate *startOfLastWeek = [aCalendar dateByAddingComponents:comp toDate:startOfToday options:0]; |
| 897 |
|
|
|
| 898 |
|
|
id result = [NSArray arrayWithObjects:startOfLastWeek, startOfThisWeek, nil]; |
| 899 |
|
|
return result; |
| 900 |
|
|
} |
| 901 |
|
|
- (NSArray *)dateRangeFromVariable:(NSString *)date |
| 902 |
|
|
{ |
| 903 |
|
|
NSLog(@"In function argument is %@", date); |
| 904 |
|
|
|
| 905 |
|
|
NSCalendar *aCalendar = [NSCalendar currentCalendar]; |
| 906 |
|
|
NSDate *now = [NSDate dateWithTimeIntervalSinceNow:0.0]; |
| 907 |
|
|
NSDateComponents *nowComp = [aCalendar components:NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit | NSWeekdayCalendarUnit |
| 908 |
|
|
fromDate:now]; |
| 909 |
|
|
NSDate *startOfToday = [aCalendar dateFromComponents:nowComp]; |
| 910 |
|
|
|
| 911 |
|
|
NSDateComponents *comp = [[NSDateComponents alloc] init]; |
| 912 |
|
|
[comp setDay:-1]; |
| 913 |
|
|
NSDate *startOfYesterday = [aCalendar dateByAddingComponents:comp toDate:startOfToday options:0]; |
| 914 |
|
|
|
| 915 |
|
|
[comp setDay:0]; |
| 916 |
|
|
[comp setWeekday:-[nowComp weekday]+1]; |
| 917 |
|
|
NSDate *startOfThisWeek = [aCalendar dateByAddingComponents:comp toDate:startOfToday options:0]; |
| 918 |
|
|
|
| 919 |
|
|
[comp setWeekday:-[nowComp weekday]+1]; |
| 920 |
|
|
[comp setWeek:-1]; |
| 921 |
|
|
NSDate *startOfLastWeek = [aCalendar dateByAddingComponents:comp toDate:startOfToday options:0]; |
| 922 |
|
|
|
| 923 |
|
|
NSLog(@"now -> %@\ntoday -> %@\nyesterday -> %@\nthisweek -> %@\nlastweek -> %@", |
| 924 |
|
|
now, startOfToday, startOfYesterday, startOfThisWeek, startOfLastWeek); |
| 925 |
|
|
|
| 926 |
|
|
id result = [NSArray arrayWithObjects:now, startOfToday, nil]; |
| 927 |
|
|
return result; |
| 928 |
|
|
} |
| 929 |
masaki |
120 |
@end |