Develop and Download Open Source Software

Browse Subversion Repository

Annotation of /XspfManager/XspfMMainWindowController.m

Parent Directory Parent Directory | Revision Log Revision Log


Revision 14 - (hide annotations) (download)
Thu Nov 5 13:44:02 2009 UTC (14 years, 5 months ago) by masaki
Original Path: XspfManager/XspfManager.m
File size: 13908 byte(s)
[New] ソート条件を変更出来るようにした。
1 masaki 2 //
2     // XspfManager.m
3     // XspfManager
4     //
5     // Created by Hori,Masaki on 09/11/01.
6     // Copyright 2009 masakih. All rights reserved.
7     //
8    
9     #import "XspfManager.h"
10    
11     #import "XspfMMovieLoadRequest.h"
12    
13    
14     @implementation XspfManager
15 masaki 8 static XspfManager *sharedInstance = nil;
16 masaki 2
17 masaki 8 + (XspfManager *)sharedInstance
18 masaki 2 {
19 masaki 8 @synchronized(self) {
20     if (sharedInstance == nil) {
21     [[self alloc] init]; // assignment not done here
22     }
23     }
24     return sharedInstance;
25     }
26    
27     + (id)allocWithZone:(NSZone *)zone
28     {
29     @synchronized(self) {
30     if (sharedInstance == nil) {
31     sharedInstance = [super allocWithZone:zone];
32     return sharedInstance; // assignment and return on first allocation
33     }
34     }
35     return nil; //on subsequent allocation attempts return nil
36     }
37    
38     - (id)copyWithZone:(NSZone *)zone
39     {
40     return self;
41     }
42    
43     - (id)retain
44     {
45     return self;
46     }
47    
48     - (unsigned)retainCount
49     {
50     return UINT_MAX; //denotes an object that cannot be released
51     }
52    
53     - (void)release
54     {
55     //do nothing
56     }
57    
58     - (id)autorelease
59     {
60     return self;
61     }
62    
63    
64     - (id)init
65     {
66     [super initWithWindowNibName:@"MainWindow"];
67 masaki 2 channel = [[HMChannel alloc] initWithWorkerNum:1];
68 masaki 8
69     return self;
70 masaki 2 }
71 masaki 8 - (void)awakeFromNib
72     {
73     if(appDelegate && [self window]) {
74     [self buildFamilyNameFromFile];
75     [self showWindow:self];
76     }
77     }
78     - (NSManagedObjectContext *)managedObjectContext
79     {
80     return [appDelegate managedObjectContext];
81     }
82 masaki 2
83 masaki 14 - (NSInteger)selectedSegmentTag
84     {
85     return selectedSegmentTag;
86     }
87     - (void)setSelectedSegmentTag:(NSInteger)newTag
88     {
89     if(selectedSegmentTag == newTag) return;
90     selectedSegmentTag = newTag;
91    
92     NSString *key = nil;
93     BOOL ascending = YES;
94     switch(selectedSegmentTag) {
95     case 0:
96     key = @"title";
97     break;
98     case 1:
99     key = @"registerDate";
100     ascending = NO;
101     break;
102     case 2:
103     key = @"lastPlayDate";
104     ascending = NO;
105     break;
106     case 3:
107     key = @"lastUpdateDate";
108     ascending = NO;
109     break;
110     }
111     if(!key) return;
112    
113     NSSortDescriptor *desc = [[[NSSortDescriptor alloc] initWithKey:key ascending:ascending] autorelease];
114    
115     NSMutableArray *array = [[controller sortDescriptors] mutableCopy];
116     [array insertObject:desc atIndex:0];
117     [controller setSortDescriptors:array];
118     [controller rearrangeObjects];
119     }
120 masaki 8
121 masaki 14
122 masaki 13 #pragma mark#### Actions ####
123 masaki 10 - (IBAction)openXspf:(id)sender
124     {
125 masaki 14 id rep = [controller valueForKeyPath:@"selection.self"];
126     id filePath = [rep valueForKey:@"filePath"];
127     if([filePath isKindOfClass:[NSString class]]) {
128 masaki 10 NSWorkspace *ws = [NSWorkspace sharedWorkspace];
129 masaki 14 [ws openFile:filePath withApplication:@"XspfQT"];
130     [rep setValue:[NSDate dateWithTimeIntervalSinceNow:0.0] forKey:@"lastPlayDate"];
131 masaki 10 }
132     }
133 masaki 2 - (IBAction)add:(id)sender
134     {
135     NSOpenPanel *panel = [NSOpenPanel openPanel];
136    
137     [panel setAllowedFileTypes:[NSArray arrayWithObjects:@"xspf", @"com.masakih.xspf", nil]];
138     [panel setAllowsMultipleSelection:YES];
139 masaki 9 [panel setDelegate:self];
140 masaki 2
141     [panel beginSheetForDirectory:nil
142     file:nil
143     types:[NSArray arrayWithObjects:@"xspf", @"com.masakih.xspf", nil]
144 masaki 8 modalForWindow:[self window]
145 masaki 2 modalDelegate:self
146     didEndSelector:@selector(endOpenPanel:::)
147     contextInfo:NULL];
148     }
149    
150 masaki 13
151     - (NSInteger)registerWithURL:(NSURL *)url
152     {
153     id obj = [NSEntityDescription insertNewObjectForEntityForName:@"Xspf"
154     inManagedObjectContext:[appDelegate managedObjectContext]];
155     if(!obj) return 1;
156    
157     id info = [NSEntityDescription insertNewObjectForEntityForName:@"Info"
158     inManagedObjectContext:[appDelegate managedObjectContext]];
159     if(!info) {
160     [[appDelegate managedObjectContext] deleteObject:obj];
161     return 2;
162     }
163    
164     [obj setValue:info forKey:@"information"];
165     [obj setValue:url forKey:@"url"];
166     [obj setValue:[NSDate dateWithTimeIntervalSinceNow:0.0] forKey:@"registerDate"];
167     [obj setValue:[NSDate dateWithTimeIntervalSinceNow:0.0] forKey:@"lastUpdateDate"];
168    
169     XspfMMovieLoadRequest *request = [XspfMMovieLoadRequest requestWithObject:obj url:url];
170     [channel putRequest:request];
171    
172     [controller performSelector:@selector(setSelectedObjects:) withObject:[NSArray arrayWithObject:obj] afterDelay:0.0];
173    
174     return noErr;
175     }
176 masaki 2 - (void)endOpenPanel:(NSOpenPanel *)panel :(NSInteger)returnCode :(void *)context
177     {
178 masaki 9 [panel orderOut:nil];
179    
180 masaki 2 if(returnCode == NSCancelButton) return;
181    
182     NSArray *URLs = [panel URLs];
183     if([URLs count] == 0) return;
184    
185     [progressBar setUsesThreadedAnimation:YES];
186    
187     [NSApp beginSheet:progressPanel
188 masaki 8 modalForWindow:[self window]
189 masaki 2 modalDelegate:nil
190     didEndSelector:Nil
191     contextInfo:NULL];
192     [progressBar startAnimation:self];
193     [progressMessage setStringValue:@"During register."];
194    
195     for(id URL in URLs) {
196     [self registerWithURL:URL];
197     }
198    
199     [progressBar stopAnimation:self];
200     [progressPanel orderOut:self];
201     [NSApp endSheet:progressPanel];
202     }
203    
204    
205     - (void)addItem:(id)item
206     {
207    
208     }
209    
210    
211     - (void)removeItem:(id)item
212     {
213     [self doesNotRecognizeSelector:_cmd];
214     }
215    
216 masaki 5
217 masaki 6 #pragma mark#### load familynames ####
218     - (NSArray *)arrayFromLFSeparatedFile:(NSString *)name
219     {
220     NSString *path;
221    
222 masaki 13 path = [[appDelegate applicationSupportFolder] stringByAppendingPathComponent:name];
223     path = [path stringByAppendingPathExtension:@"txt"];
224    
225     NSFileManager *fm = [NSFileManager defaultManager];
226     BOOL isDir = NO;
227     if(![fm fileExistsAtPath:path isDirectory:&isDir] || isDir) {
228 masaki 6 path = [[NSBundle mainBundle] pathForResource:name ofType:@"txt"];
229 masaki 13 }
230 masaki 6
231     NSError *error = nil;
232     NSString *content = [NSString stringWithContentsOfFile:path
233     encoding:NSUTF8StringEncoding
234     error:&error];
235     if(error) {
236     NSLog(@"path => %@", path);
237     NSLog(@"%@", [error localizedDescription]);
238     return NO;
239     }
240    
241     return [content componentsSeparatedByString:@"\x0a"];
242     }
243 masaki 5
244 masaki 6 - (NSArray *)arrayFromTabSeparatedString:(NSString *)string
245     {
246     return [string componentsSeparatedByString:@"\t"];
247     }
248     - (BOOL)isEmptyEntityName:(NSString *)name
249     {
250     NSManagedObjectContext *moc = [appDelegate managedObjectContext];
251     NSError *error = nil;
252     NSFetchRequest *fetch;
253     NSInteger num;
254    
255     fetch = [[NSFetchRequest alloc] init];
256     [fetch setEntity:[NSEntityDescription entityForName:name
257     inManagedObjectContext:moc]];
258     num = [moc countForFetchRequest:fetch
259     error:&error];
260     [fetch release];
261     fetch = nil;
262     if(error) {
263     NSLog(@"%@", [error localizedDescription]);
264     return NO;
265     }
266    
267     return num == 0;
268     }
269     - (void) buildFamilyNameFromFile
270     {
271     NSManagedObjectContext *moc = [appDelegate managedObjectContext];
272    
273     NSString *entityName;
274     NSArray *contents;
275     entityName = @"FamilyName";
276 masaki 7 if([self isEmptyEntityName:entityName]) {
277 masaki 6 contents = [self arrayFromLFSeparatedFile:entityName];
278    
279 masaki 14 id key;
280     for(key in contents) {
281     NSArray *attr = [self arrayFromTabSeparatedString:key];
282 masaki 6 if([attr count] < 2) continue;
283    
284     id obj = [NSEntityDescription insertNewObjectForEntityForName:entityName
285     inManagedObjectContext:moc];
286     [obj setValue:[attr objectAtIndex:0] forKey:@"roman"];
287     [obj setValue:[attr objectAtIndex:1] forKey:@"japanese"];
288    
289     if([attr count] > 2) {
290     [obj setValue:[attr objectAtIndex:2] forKey:@"yomigana"];
291     }
292     }
293     }
294    
295     }
296    
297 masaki 7 - (void)truncateFamilyName
298     {
299     NSManagedObjectContext *moc = [appDelegate managedObjectContext];
300     NSFetchRequest *fetch = [[[NSFetchRequest alloc] init] autorelease];
301     NSEntityDescription *entry = [NSEntityDescription entityForName:@"FamilyName"
302     inManagedObjectContext:moc];
303     [fetch setEntity:entry];
304    
305     NSError *error = nil;
306     NSArray *objects = [moc executeFetchRequest:fetch error:&error];
307     if(!objects) {
308     if(error) {
309     NSLog(@"fail fetch reason -> %@", error);
310     }
311     }
312    
313     [moc lock];
314     for(id obj in objects) {
315     [moc deleteObject:obj];
316     }
317     [moc unlock];
318     }
319 masaki 8 #pragma mark#### NSWidnow Delegate ####
320     /**
321     Returns the NSUndoManager for the application. In this case, the manager
322     returned is that of the managed object context for the application.
323     */
324 masaki 6
325 masaki 8 - (NSUndoManager *)windowWillReturnUndoManager:(NSWindow *)window {
326     return [[appDelegate managedObjectContext] undoManager];
327     }
328    
329 masaki 9 #pragma mark#### NSOpenPanel Delegate ####
330     - (BOOL)panel:(id)sender shouldShowFilename:(NSString *)filename
331     {
332     NSManagedObjectContext *moc = [appDelegate managedObjectContext];
333     NSError *error = nil;
334     NSFetchRequest *fetch;
335     NSInteger num;
336     NSURL *url = [NSURL fileURLWithPath:filename];
337    
338     fetch = [[[NSFetchRequest alloc] init] autorelease];
339     [fetch setEntity:[NSEntityDescription entityForName:@"Xspf" inManagedObjectContext:moc]];
340     NSPredicate *predicate = [NSPredicate predicateWithFormat:@"urlString LIKE %@", [url absoluteString]];
341     [fetch setPredicate:predicate];
342     num = [moc countForFetchRequest:fetch error:&error];
343     if(error) {
344     NSLog(@"%@", [error localizedDescription]);
345     return NO;
346     }
347    
348     return num == 0;
349     }
350    
351 masaki 5 #pragma mark#### NSTokenField Delegate ####
352 masaki 6 #if 1
353 masaki 5 - (NSArray *)tokenField:(NSTokenField *)tokenField
354     completionsForSubstring:(NSString *)substring
355     indexOfToken:(NSInteger)tokenIndex
356     indexOfSelectedItem:(NSInteger *)selectedIndex
357     {
358 masaki 6 NSManagedObjectContext *moc = [appDelegate managedObjectContext];
359     NSFetchRequest *fetch = [[[NSFetchRequest alloc] init] autorelease];
360    
361     NSPredicate *predicate = [NSPredicate predicateWithFormat:
362     @"roman BEGINSWITH[cd] %@"
363     @"OR japanese BEGINSWITH[cd] %@"
364     @"OR yomigana BEGINSWITH[cd] %@", substring,substring,substring];
365     NSEntityDescription *entry = [NSEntityDescription entityForName:@"FamilyName"
366     inManagedObjectContext:moc];
367    
368     [fetch setEntity:entry];
369     [fetch setPredicate:predicate];
370    
371     NSError *error = nil;
372     NSArray *objects = [moc executeFetchRequest:fetch error:&error];
373     if(!objects) {
374     if(error) {
375     NSLog(@"fail fetch reason -> %@", error);
376     }
377     }
378    
379     NSString *entryName = @"";
380     switch([tokenField tag]) {
381     case 2000:
382     entryName = @"VoiceActor";
383     break;
384     case 2001:
385     entryName = @"Product";
386     break;
387     }
388    
389     if([objects count] > 0) {
390     NSMutableString *string = [NSMutableString string];
391     NSMutableArray *names = [NSMutableArray array];
392     for(id e in objects) {
393     if([string length]) {
394     [string appendString:@" OR "];
395     }
396     [string appendFormat:@"name BEGINSWITH[cd] %%@ "];
397     [names addObject:[e valueForKey:@"japanese"]];
398     }
399     predicate = [NSPredicate predicateWithFormat:string argumentArray:names];
400     } else {
401     predicate = [NSPredicate predicateWithFormat:@"name BEGINSWITH[cd] %@", substring];
402     }
403     entry = [NSEntityDescription entityForName:entryName inManagedObjectContext:moc];
404     [fetch setEntity:entry];
405     [fetch setPredicate:predicate];
406    
407     error = nil;
408     objects = [moc executeFetchRequest:fetch error:&error];
409     if(!objects) {
410     if(error) {
411     NSLog(@"fail fetch reason -> %@", error);
412     }
413     }
414    
415     NSMutableArray *result = [NSMutableArray arrayWithObject:substring];
416     for(id obj in objects) {
417     [result addObject:[obj valueForKey:@"name"]];
418     }
419    
420     return result;
421     }
422     #else
423     - (NSArray *)tokenField:(NSTokenField *)tokenField
424     completionsForSubstring:(NSString *)substring
425     indexOfToken:(NSInteger)tokenIndex
426     indexOfSelectedItem:(NSInteger *)selectedIndex
427     {
428 masaki 5 NSLog(@"Enter %@", NSStringFromSelector(_cmd));
429    
430 masaki 6 NSString *entryName = @"";
431     switch([tokenField tag]) {
432     case 2000:
433     entryName = @"VoiceActor";
434     break;
435     case 2001:
436     entryName = @"Product";
437     break;
438     }
439    
440 masaki 5 NSManagedObjectContext *moc = [appDelegate managedObjectContext];
441     NSPredicate *predicate = [NSPredicate predicateWithFormat:@"name BEGINSWITH[cd] %@", substring];
442 masaki 6 NSEntityDescription *entry = [NSEntityDescription entityForName:entryName
443 masaki 5 inManagedObjectContext:moc];
444     NSFetchRequest *fetch = [[[NSFetchRequest alloc] init] autorelease];
445     [fetch setEntity:entry];
446     [fetch setPredicate:predicate];
447    
448     NSError *error = nil;
449     NSArray *objects = [moc executeFetchRequest:fetch error:&error];
450     if(!objects) {
451     if(error) {
452     NSLog(@"fail fetch reason -> %@", error);
453     }
454     }
455    
456     NSMutableArray *result = [NSMutableArray array];
457     for(id obj in objects) {
458     [result addObject:[obj valueForKey:@"name"]];
459     }
460    
461     return result;
462     }
463 masaki 6 #endif
464 masaki 5
465     - (void)registerVoiceActor:(NSTokenField *)tokenField
466     {
467     id array = [tokenField objectValue];
468     if(![array isKindOfClass:[NSArray class]]) return;
469    
470 masaki 6 NSString *entryName = @"";
471     switch([tokenField tag]) {
472     case 2000:
473     entryName = @"VoiceActor";
474     break;
475     case 2001:
476     entryName = @"Product";
477     break;
478     }
479    
480 masaki 5 NSManagedObjectContext *moc = [appDelegate managedObjectContext];
481 masaki 6 NSEntityDescription *entry = [NSEntityDescription entityForName:entryName
482 masaki 5 inManagedObjectContext:moc];
483     NSFetchRequest *fetch = [[[NSFetchRequest alloc] init] autorelease];
484     [fetch setEntity:entry];
485    
486     for(id token in array) {
487     NSPredicate *predicate = [NSPredicate predicateWithFormat:@"name LIKE[cd] %@", token];
488     [fetch setPredicate:predicate];
489    
490     NSError *error = nil;
491     NSUInteger count = [moc countForFetchRequest:fetch error:&error];
492     if(error) {
493     NSLog(@"fail fetch reason -> %@", error);
494     continue;
495     }
496     if(count == 0) {
497 masaki 6 id obj = [NSEntityDescription insertNewObjectForEntityForName:entryName inManagedObjectContext:moc];
498 masaki 5 [obj setValue:token forKey:@"name"];
499     }
500     }
501     }
502     - (BOOL)control:(id)control textShouldEndEditing:(NSText *)fieldEditor
503     {
504 masaki 6 if([control tag] == 2000 || [control tag] == 2001) {
505 masaki 5 [self registerVoiceActor:control];
506     }
507    
508     return YES;
509     }
510    
511 masaki 10 #pragma mark#### XspfMCollectionView Delegate ####
512     - (void)enterAction:(XspfMCollectionView *)view
513     {
514     [self openXspf:view];
515     }
516 masaki 5
517     #pragma mark#### Test ####
518 masaki 2 - (IBAction)test01:(id)sender
519     {
520 masaki 7 [self truncateFamilyName];
521 masaki 6 [self buildFamilyNameFromFile];
522 masaki 2 }
523     - (IBAction)test02:(id)sender
524     {
525 masaki 10 NSResponder *responder = [[self window] firstResponder];
526     while(responder) {
527     NSLog(@"Responder -> %@", responder);
528     responder = [responder nextResponder];
529     }
530 masaki 2 }
531     - (IBAction)test03:(id)sender
532     {
533     NSFetchRequest *fetch = [[[NSFetchRequest alloc] init] autorelease];
534    
535     id moc = [appDelegate managedObjectContext];
536    
537     [fetch setEntity:[NSEntityDescription entityForName:@"Xspf" inManagedObjectContext:moc]];
538    
539     id objs = [moc executeFetchRequest:fetch error:NULL];
540     NSLog(@"Fetched -> %@", objs);
541     }
542     @end

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