Develop and Download Open Source Software

Browse Subversion Repository

Annotation of /XspfManager/XspfMRuleEditorDelegate.m

Parent Directory Parent Directory | Revision Log Revision Log


Revision 155 - (hide annotations) (download)
Fri Dec 18 13:53:10 2009 UTC (14 years, 4 months ago) by masaki
File size: 13313 byte(s)
[New] N[日|週|月|年][以内|以内ではない]を実装。
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 *XspfMREDPredicateRowsKey = @"predicateRows";
17     static NSString *XspfMREDNameKey = @"name";
18    
19    
20 masaki 124 - (NSDictionary *)buildRows:(NSArray *)template
21     {
22     NSMutableDictionary *result = [NSMutableDictionary dictionary];
23     for(id row in template) {
24 masaki 125 id name = [row valueForKey:XspfMREDNameKey];
25 masaki 152 id rule = [XspfMRule ruleWithPlist:row];
26     [result setObject:rule forKey:name];
27 masaki 124 }
28 masaki 152 rowTemplate = [result retain];
29 masaki 124 return result;
30     }
31 masaki 126 - (id)criteriaWithKeyPath:(NSString *)keypath
32     {
33     NSString *key = nil;
34     if([keypath isEqualToString:@"title"]) {
35     key = @"String";
36     } else if([keypath isEqualToString:@"rating"]) {
37     key = @"Rate";
38 masaki 152 } else if([[NSArray arrayWithObjects:@"lastPlayDate", @"modificationDate", @"creationDate", nil] containsObject:keypath]) {
39     key = @"AbDate";
40 masaki 126 }
41     if(key) {
42     id row = [rowTemplate valueForKey:key];
43 masaki 152 id c = [[[row childAtIndex:0] copy] autorelease];
44     [c setValue:keypath];
45 masaki 126 return [NSArray arrayWithObject:c];
46     }
47    
48     return nil;
49     }
50 masaki 120 - (void)awakeFromNib
51     {
52     if(!compound) {
53 masaki 143 compound = [[XspfMCompound alloc] init];
54 masaki 120 }
55 masaki 152
56 masaki 124 NSBundle *m = [NSBundle mainBundle];
57     NSString *path = [m pathForResource:@"LibraryRowTemplate" ofType:@"plist"];
58     NSArray *rowsTemplate = [NSArray arrayWithContentsOfFile:path];
59     if(!rowsTemplate) {
60     exit(12345);
61 masaki 120 }
62 masaki 124
63 masaki 152 [self buildRows:rowsTemplate];
64    
65 masaki 124 NSMutableArray *newRows = [NSMutableArray array];
66    
67 masaki 126 id c = [self criteriaWithKeyPath:@"title"];
68     if(c) [newRows addObjectsFromArray:c];
69 masaki 124
70     for(id keyPath in [NSArray arrayWithObjects:@"lastPlayDate", @"modificationDate", @"creationDate", nil]) {
71 masaki 126 c = [self criteriaWithKeyPath:keyPath];
72     if(c) [newRows addObjectsFromArray:c];
73 masaki 124 }
74    
75 masaki 126 c = [self criteriaWithKeyPath:@"rating"];
76     if(c) [newRows addObjectsFromArray:c];
77 masaki 124
78 masaki 152 simples = [newRows retain];
79    
80 masaki 125 ////
81     predicateRows = [[NSMutableArray alloc] init];
82     [ruleEditor bind:XspfMREDRowsKey toObject:self withKeyPath:XspfMREDPredicateRowsKey options:nil];
83 masaki 120 }
84    
85 masaki 150 - (void)resolveExpression:(id)exp
86     {
87     NSString *message = nil;
88    
89     switch([exp expressionType]) {
90     case NSConstantValueExpressionType:
91     message = [NSString stringWithFormat:@"constant -> %@", [exp constantValue]];
92     break;
93     case NSEvaluatedObjectExpressionType:
94     message = [NSString stringWithFormat:@"constant -> %@", [exp constantValue]];
95     break;
96     case NSVariableExpressionType:
97     message = [NSString stringWithFormat:@"variable -> %@", [exp variable]];
98     break;
99     case NSKeyPathExpressionType:
100     message = [NSString stringWithFormat:@"keyPath -> %@", [exp keyPath]];
101     break;
102     case NSFunctionExpressionType:
103 masaki 154 message = [NSString stringWithFormat:@"oprand -> %@(%@), function -> %@, arguments -> %@",
104 masaki 150 [exp operand], NSStringFromClass([[exp operand] class]),
105     [exp function], [exp arguments]];
106     break;
107     case NSAggregateExpressionType:
108     message = [NSString stringWithFormat:@"collection -> %@", [exp collection]];
109     break;
110     }
111    
112     fprintf(stderr, "%s\n", [message UTF8String]);
113     }
114     - (void)resolvePredicate:(id)predicate
115     {
116     if([predicate isKindOfClass:[NSCompoundPredicate class]]) {
117     NSArray *sub = [predicate subpredicates];
118     for(id p in sub) {
119     [self resolvePredicate:p];
120     }
121     } else if([predicate isKindOfClass:[NSComparisonPredicate class]]) {
122     id left = [predicate leftExpression];
123     id right = [predicate rightExpression];
124     SEL sel = Nil;
125     if([predicate predicateOperatorType] == NSCustomSelectorPredicateOperatorType) {
126     sel = [predicate customSelector];
127     }
128     fprintf(stderr, "left ->\t");
129     [self resolveExpression:left];
130     if(sel) {
131     fprintf(stderr, "%s\n", [[NSString stringWithFormat:@"SEL -> %@", NSStringFromSelector(sel)] UTF8String]);
132     } else {
133     fprintf(stderr, "%s\n", [[NSString stringWithFormat:@"type -> %d, opt -> %d, mod -> %d", [predicate predicateOperatorType], [predicate options], [predicate comparisonPredicateModifier]] UTF8String]);
134     }
135     fprintf(stderr, "right ->\t");
136     [self resolveExpression:right];
137     fprintf(stderr, "end resolve.\n");
138     }
139     }
140    
141 masaki 123 - (void)setPredicate:(id)predicate
142     {
143 masaki 150 NSLog(@"predicate -> (%@) %@", NSStringFromClass([predicate class]), predicate);
144     [self resolvePredicate:predicate];
145 masaki 123
146 masaki 153 id new = [XspfMRule ruleEditorRowsFromPredicate:predicate withRowTemplate:rowTemplate];
147 masaki 125
148     [self willChangeValueForKey:XspfMREDPredicateRowsKey];
149     [predicateRows release];
150     predicateRows = [new retain];
151     [self didChangeValueForKey:XspfMREDPredicateRowsKey];
152 masaki 146 // [ruleEditor reloadCriteria];
153 masaki 123 }
154 masaki 125 - (void)setPredicateRows:(id)p
155     {
156 masaki 147 // NSLog(@"new -> %@", p);
157 masaki 125 [predicateRows release];
158     predicateRows = [p retain];
159     }
160    
161     #pragma mark#### NSRleEditor Delegate ####
162 masaki 152
163 masaki 124 - (NSInteger)ruleEditor:(NSRuleEditor *)editor
164     numberOfChildrenForCriterion:(id)criterion
165     withRowType:(NSRuleEditorRowType)rowType
166     {
167     NSInteger result = 0;
168    
169 masaki 143 if(rowType == NSRuleEditorRowTypeCompound) {
170     return [compound numberOfChildrenForChild:criterion];
171     }
172    
173 masaki 124 if(!criterion) {
174 masaki 152 result = [simples count];
175 masaki 143 } else {
176 masaki 152 result = [criterion numberOfChildren];
177 masaki 124 }
178    
179 masaki 152 // NSLog(@"numner\tcriterion -> %@, type -> %d, result -> %d", criterion, rowType, result);
180 masaki 124
181     return result;
182     }
183    
184     - (id)ruleEditor:(NSRuleEditor *)editor
185     child:(NSInteger)index
186     forCriterion:(id)criterion
187     withRowType:(NSRuleEditorRowType)rowType
188     {
189     id result = nil;
190    
191 masaki 143 if(rowType == NSRuleEditorRowTypeCompound) {
192     return [compound childForChild:criterion atIndex:index];
193     }
194    
195 masaki 124 if(!criterion) {
196 masaki 152 result = [simples objectAtIndex:index];
197 masaki 143 } else {
198 masaki 152 result = [criterion childAtIndex:index];
199 masaki 124 }
200    
201 masaki 152 // NSLog(@"child\tindex -> %d, criterion -> %@, type -> %d, result -> %@", index, criterion, rowType, result);
202 masaki 124
203     return result;
204     }
205     - (id)ruleEditor:(NSRuleEditor *)editor
206     displayValueForCriterion:(id)criterion
207     inRow:(NSInteger)row
208     {
209     id result = nil;
210 masaki 143
211     NSRuleEditorRowType rowType = [editor rowTypeForRow:row];
212     if(rowType == NSRuleEditorRowTypeCompound) {
213     return [compound displayValueForChild:criterion];
214     }
215    
216     if(!criterion) {
217     //
218     } else {
219 masaki 152 result = [criterion displayValueForRuleEditor:editor inRow:row];
220 masaki 143 }
221    
222 masaki 152 // NSLog(@"display\tcriterion -> %@, row -> %d, result -> %@", criterion, row, result);
223 masaki 143
224 masaki 124 return result;
225     }
226     - (NSDictionary *)ruleEditor:(NSRuleEditor *)editor
227     predicatePartsForCriterion:(id)criterion
228     withDisplayValue:(id)displayValue
229     inRow:(NSInteger)row
230     {
231     id result = nil;
232    
233 masaki 143 NSRuleEditorRowType rowType = [editor rowTypeForRow:row];
234     if(rowType == NSRuleEditorRowTypeCompound) {
235     return [compound predicateForChild:criterion withDisplayValue:displayValue];
236     }
237    
238 masaki 152 result = [criterion predicatePartsWithDisplayValue:displayValue forRuleEditor:editor inRow:row];
239 masaki 153 // NSLog(@"predicate\tresult -> %@", result);
240 masaki 143
241 masaki 152 // NSLog(@"predicate\tcriterion -> %@, value -> %@, row -> %d, result -> %@", criterion, displayValue, row, result);
242 masaki 143
243 masaki 124 return result;
244     }
245     - (void)ruleEditorRowsDidChange:(NSNotification *)notification
246     {
247     //
248     }
249 masaki 150 @end
250 masaki 124
251 masaki 150 @implementation NSString (XspfMTest)
252     - (NSArray *)rangeOfToday
253     {
254     NSCalendar *aCalendar = [NSCalendar currentCalendar];
255     NSDate *now = [NSDate dateWithTimeIntervalSinceNow:0.0];
256     NSDateComponents *nowComp = [aCalendar components:NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit
257     fromDate:now];
258     NSDate *startOfToday = [aCalendar dateFromComponents:nowComp];
259    
260     id result = [NSArray arrayWithObjects:startOfToday, now, nil];
261     return result;
262     }
263     - (NSArray *)rangeOfYesterday
264     {
265     NSCalendar *aCalendar = [NSCalendar currentCalendar];
266     NSDate *now = [NSDate dateWithTimeIntervalSinceNow:0.0];
267     NSDateComponents *nowComp = [aCalendar components:NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit
268     fromDate:now];
269     NSDate *startOfToday = [aCalendar dateFromComponents:nowComp];
270    
271     NSDateComponents *comp = [[NSDateComponents alloc] init];
272     [comp setDay:-1];
273     NSDate *startOfYesterday = [aCalendar dateByAddingComponents:comp toDate:startOfToday options:0];
274    
275     id result = [NSArray arrayWithObjects:startOfYesterday, startOfToday, nil];
276     return result;
277     }
278     - (NSArray *)rangeOfThisWeek
279     {
280     NSCalendar *aCalendar = [NSCalendar currentCalendar];
281     NSDate *now = [NSDate dateWithTimeIntervalSinceNow:0.0];
282     NSDateComponents *nowComp = [aCalendar components:NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit | NSWeekdayCalendarUnit
283     fromDate:now];
284     NSDate *startOfToday = [aCalendar dateFromComponents:nowComp];
285    
286     NSDateComponents *comp = [[NSDateComponents alloc] init];
287    
288     [comp setWeekday:-[nowComp weekday]+1];
289     NSDate *startOfThisWeek = [aCalendar dateByAddingComponents:comp toDate:startOfToday options:0];
290    
291     id result = [NSArray arrayWithObjects:startOfThisWeek, now, nil];
292     return result;
293     }
294     - (NSArray *)rangeOfLastWeek
295     {
296     NSCalendar *aCalendar = [NSCalendar currentCalendar];
297     NSDate *now = [NSDate dateWithTimeIntervalSinceNow:0.0];
298     NSDateComponents *nowComp = [aCalendar components:NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit | NSWeekdayCalendarUnit
299     fromDate:now];
300     NSDate *startOfToday = [aCalendar dateFromComponents:nowComp];
301    
302     NSDateComponents *comp = [[NSDateComponents alloc] init];
303     [comp setWeekday:-[nowComp weekday]+1];
304     NSDate *startOfThisWeek = [aCalendar dateByAddingComponents:comp toDate:startOfToday options:0];
305    
306     [comp setWeekday:-[nowComp weekday]+1];
307     [comp setWeek:-1];
308     NSDate *startOfLastWeek = [aCalendar dateByAddingComponents:comp toDate:startOfToday options:0];
309    
310     id result = [NSArray arrayWithObjects:startOfLastWeek, startOfThisWeek, nil];
311     return result;
312     }
313 masaki 154
314 masaki 155 - (NSArray *)rangeOfDateByNumber:(NSNumber *)numberValue unit:(NSNumber *)unitValue
315 masaki 154 {
316     NSInteger number = [numberValue integerValue];
317     NSInteger unit = [unitValue integerValue];
318    
319     NSDateComponents *comp01 = [[NSDateComponents alloc] init];
320     NSDateComponents *comp02 = [[NSDateComponents alloc] init];
321     NSUInteger unitFlag = 0;
322     switch(unit) {
323     case 0:
324     unitFlag = NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit;
325     [comp01 setDay:-number];
326     [comp02 setDay:-number+1];
327     break;
328     case 1:
329     unitFlag = NSYearCalendarUnit | NSMonthCalendarUnit | NSWeekCalendarUnit;
330     [comp01 setWeek:-number];
331     [comp02 setDay:-1];
332     break;
333     case 2:
334     unitFlag = NSYearCalendarUnit | NSMonthCalendarUnit;
335     [comp01 setMonth:-number];
336     [comp02 setDay:-1];
337     break;
338     case 3:
339     unitFlag = NSYearCalendarUnit;
340     [comp01 setYear:-number];
341     [comp02 setDay:-1];
342     break;
343     }
344     NSCalendar *aCalendar = [NSCalendar currentCalendar];
345     NSDate *now = [NSDate dateWithTimeIntervalSinceNow:0.0];
346     NSDateComponents *nowComp = [aCalendar components:unitFlag fromDate:now];
347     NSDate *aDay = [aCalendar dateFromComponents:nowComp];
348    
349     NSDate *pastDay01 = [aCalendar dateByAddingComponents:comp01 toDate:aDay options:0];
350     NSDate *pastDay02 = [aCalendar dateByAddingComponents:comp02 toDate:aDay options:0];
351    
352     id result = [NSArray arrayWithObjects:pastDay01, pastDay02, nil];
353     return result;
354     }
355 masaki 155 - (NSDate *)dateByNumber:(NSNumber *)numberValue unit:(NSNumber *)unitValue
356     {
357     NSInteger number = [numberValue integerValue];
358     NSInteger unit = [unitValue integerValue];
359    
360     NSDateComponents *comp = [[NSDateComponents alloc] init];
361     NSUInteger unitFlag = 0;
362     switch(unit) {
363     case 0:
364     unitFlag = NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit;
365     [comp setDay:-number];
366     break;
367     case 1:
368     unitFlag = NSYearCalendarUnit | NSMonthCalendarUnit | NSWeekCalendarUnit;
369     [comp setWeek:-number];
370     break;
371     case 2:
372     unitFlag = NSYearCalendarUnit | NSMonthCalendarUnit;
373     [comp setMonth:-number];
374     break;
375     case 3:
376     unitFlag = NSYearCalendarUnit;
377     [comp setYear:-number];
378     break;
379     }
380     NSCalendar *aCalendar = [NSCalendar currentCalendar];
381     NSDate *now = [NSDate dateWithTimeIntervalSinceNow:0.0];
382     NSDateComponents *nowComp = [aCalendar components:unitFlag fromDate:now];
383     NSDate *aDay = [aCalendar dateFromComponents:nowComp];
384    
385     NSDate *pastDay = [aCalendar dateByAddingComponents:comp toDate:aDay options:0];
386    
387     return pastDay;
388     }
389 masaki 150 - (NSArray *)dateRangeFromVariable:(NSString *)date
390     {
391     NSLog(@"In function argument is %@", date);
392    
393     NSCalendar *aCalendar = [NSCalendar currentCalendar];
394     NSDate *now = [NSDate dateWithTimeIntervalSinceNow:0.0];
395     NSDateComponents *nowComp = [aCalendar components:NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit | NSWeekdayCalendarUnit
396     fromDate:now];
397     NSDate *startOfToday = [aCalendar dateFromComponents:nowComp];
398    
399     NSDateComponents *comp = [[NSDateComponents alloc] init];
400     [comp setDay:-1];
401     NSDate *startOfYesterday = [aCalendar dateByAddingComponents:comp toDate:startOfToday options:0];
402    
403     [comp setDay:0];
404     [comp setWeekday:-[nowComp weekday]+1];
405     NSDate *startOfThisWeek = [aCalendar dateByAddingComponents:comp toDate:startOfToday options:0];
406    
407     [comp setWeekday:-[nowComp weekday]+1];
408     [comp setWeek:-1];
409     NSDate *startOfLastWeek = [aCalendar dateByAddingComponents:comp toDate:startOfToday options:0];
410    
411     NSLog(@"now -> %@\ntoday -> %@\nyesterday -> %@\nthisweek -> %@\nlastweek -> %@",
412     now, startOfToday, startOfYesterday, startOfThisWeek, startOfLastWeek);
413    
414     id result = [NSArray arrayWithObjects:now, startOfToday, nil];
415     return result;
416     }
417 masaki 120 @end

Back to OSDN">Back to OSDN
ViewVC Help
Powered by ViewVC 1.1.26