Develop and Download Open Source Software

Browse Subversion Repository

Annotation of /XspfManager/XspfMMainWindowController.m

Parent Directory Parent Directory | Revision Log Revision Log


Revision 10 - (hide annotations) (download)
Tue Nov 3 14:17:18 2009 UTC (14 years, 5 months ago) by masaki
Original Path: XspfManager/XspfManager.m
File size: 12836 byte(s)
[New] Xspfファイルを開けるようにした。
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 8
84 masaki 2 - (NSInteger)registerWithURL:(NSURL *)url
85     {
86     id obj = [NSEntityDescription insertNewObjectForEntityForName:@"Xspf"
87     inManagedObjectContext:[appDelegate managedObjectContext]];
88     if(!obj) return 1;
89    
90 masaki 5 id info = [NSEntityDescription insertNewObjectForEntityForName:@"Info"
91     inManagedObjectContext:[appDelegate managedObjectContext]];
92 masaki 9 if(!info) {
93 masaki 5 [[appDelegate managedObjectContext] deleteObject:obj];
94     return 2;
95     }
96    
97     [obj setValue:info forKey:@"information"];
98 masaki 2 [obj setValue:url forKey:@"url"];
99     [obj setValue:[NSDate dateWithTimeIntervalSinceNow:0.0] forKey:@"registerDate"];
100     [obj setValue:[NSDate dateWithTimeIntervalSinceNow:0.0] forKey:@"lastUpdateDate"];
101    
102     XspfMMovieLoadRequest *request = [XspfMMovieLoadRequest requestWithObject:obj url:url];
103     [channel putRequest:request];
104    
105 masaki 10 [controller performSelector:@selector(setSelectedObjects:) withObject:[NSArray arrayWithObject:obj] afterDelay:0.0];
106 masaki 9
107 masaki 2 return noErr;
108     }
109 masaki 10 - (IBAction)openXspf:(id)sender
110     {
111     id obj = [controller valueForKeyPath:@"selection.filePath"];
112     if([obj isKindOfClass:[NSString class]]) {
113     NSWorkspace *ws = [NSWorkspace sharedWorkspace];
114     [ws openFile:obj withApplication:@"XspfQT"];
115     }
116     }
117 masaki 2 - (IBAction)add:(id)sender
118     {
119     NSOpenPanel *panel = [NSOpenPanel openPanel];
120    
121     [panel setAllowedFileTypes:[NSArray arrayWithObjects:@"xspf", @"com.masakih.xspf", nil]];
122     [panel setAllowsMultipleSelection:YES];
123 masaki 9 [panel setDelegate:self];
124 masaki 2
125     [panel beginSheetForDirectory:nil
126     file:nil
127     types:[NSArray arrayWithObjects:@"xspf", @"com.masakih.xspf", nil]
128 masaki 8 modalForWindow:[self window]
129 masaki 2 modalDelegate:self
130     didEndSelector:@selector(endOpenPanel:::)
131     contextInfo:NULL];
132     }
133    
134     - (void)endOpenPanel:(NSOpenPanel *)panel :(NSInteger)returnCode :(void *)context
135     {
136 masaki 9 [panel orderOut:nil];
137    
138 masaki 2 if(returnCode == NSCancelButton) return;
139    
140     NSArray *URLs = [panel URLs];
141     if([URLs count] == 0) return;
142    
143     [progressBar setUsesThreadedAnimation:YES];
144    
145     [NSApp beginSheet:progressPanel
146 masaki 8 modalForWindow:[self window]
147 masaki 2 modalDelegate:nil
148     didEndSelector:Nil
149     contextInfo:NULL];
150     [progressBar startAnimation:self];
151     [progressMessage setStringValue:@"During register."];
152    
153     for(id URL in URLs) {
154     [self registerWithURL:URL];
155     }
156    
157     [progressBar stopAnimation:self];
158     [progressPanel orderOut:self];
159     [NSApp endSheet:progressPanel];
160     }
161    
162    
163     - (void)addItem:(id)item
164     {
165    
166     }
167    
168    
169     - (void)removeItem:(id)item
170     {
171     [self doesNotRecognizeSelector:_cmd];
172     }
173    
174 masaki 5
175 masaki 6 #pragma mark#### load familynames ####
176     - (NSArray *)arrayFromLFSeparatedFile:(NSString *)name
177     {
178     NSString *path;
179    
180     // path = [[appDelegate applicationSupportFolder] stringByAppendingPathComponent:name];
181     // path = [path stringByAppendingPathExtension:@"txt"];
182     // if(!path) {
183     path = [[NSBundle mainBundle] pathForResource:name ofType:@"txt"];
184     // }
185    
186     NSError *error = nil;
187     NSString *content = [NSString stringWithContentsOfFile:path
188     encoding:NSUTF8StringEncoding
189     error:&error];
190     if(error) {
191     NSLog(@"path => %@", path);
192     NSLog(@"%@", [error localizedDescription]);
193     return NO;
194     }
195    
196     return [content componentsSeparatedByString:@"\x0a"];
197     }
198 masaki 5
199 masaki 6 - (NSArray *)arrayFromTabSeparatedString:(NSString *)string
200     {
201     return [string componentsSeparatedByString:@"\t"];
202     }
203     - (BOOL)isEmptyEntityName:(NSString *)name
204     {
205     NSManagedObjectContext *moc = [appDelegate managedObjectContext];
206     NSError *error = nil;
207     NSFetchRequest *fetch;
208     NSInteger num;
209    
210     fetch = [[NSFetchRequest alloc] init];
211     [fetch setEntity:[NSEntityDescription entityForName:name
212     inManagedObjectContext:moc]];
213     num = [moc countForFetchRequest:fetch
214     error:&error];
215     [fetch release];
216     fetch = nil;
217     if(error) {
218     NSLog(@"%@", [error localizedDescription]);
219     return NO;
220     }
221    
222     return num == 0;
223     }
224     - (void) buildFamilyNameFromFile
225     {
226     NSManagedObjectContext *moc = [appDelegate managedObjectContext];
227    
228     NSString *entityName;
229     NSArray *contents;
230     entityName = @"FamilyName";
231 masaki 7 if([self isEmptyEntityName:entityName]) {
232 masaki 6 contents = [self arrayFromLFSeparatedFile:entityName];
233    
234     id attribute;
235     for(attribute in contents) {
236     NSArray *attr = [self arrayFromTabSeparatedString:attribute];
237     if([attr count] < 2) continue;
238    
239     id obj = [NSEntityDescription insertNewObjectForEntityForName:entityName
240     inManagedObjectContext:moc];
241     [obj setValue:[attr objectAtIndex:0] forKey:@"roman"];
242     [obj setValue:[attr objectAtIndex:1] forKey:@"japanese"];
243    
244     if([attr count] > 2) {
245     [obj setValue:[attr objectAtIndex:2] forKey:@"yomigana"];
246     }
247     }
248     }
249    
250     }
251    
252 masaki 7 - (void)truncateFamilyName
253     {
254     NSManagedObjectContext *moc = [appDelegate managedObjectContext];
255     NSFetchRequest *fetch = [[[NSFetchRequest alloc] init] autorelease];
256     NSEntityDescription *entry = [NSEntityDescription entityForName:@"FamilyName"
257     inManagedObjectContext:moc];
258     [fetch setEntity:entry];
259    
260     NSError *error = nil;
261     NSArray *objects = [moc executeFetchRequest:fetch error:&error];
262     if(!objects) {
263     if(error) {
264     NSLog(@"fail fetch reason -> %@", error);
265     }
266     }
267    
268     [moc lock];
269     for(id obj in objects) {
270     [moc deleteObject:obj];
271     }
272     [moc unlock];
273     }
274 masaki 8 #pragma mark#### NSWidnow Delegate ####
275     /**
276     Returns the NSUndoManager for the application. In this case, the manager
277     returned is that of the managed object context for the application.
278     */
279 masaki 6
280 masaki 8 - (NSUndoManager *)windowWillReturnUndoManager:(NSWindow *)window {
281     return [[appDelegate managedObjectContext] undoManager];
282     }
283    
284 masaki 9 #pragma mark#### NSOpenPanel Delegate ####
285     - (BOOL)panel:(id)sender shouldShowFilename:(NSString *)filename
286     {
287     NSManagedObjectContext *moc = [appDelegate managedObjectContext];
288     NSError *error = nil;
289     NSFetchRequest *fetch;
290     NSInteger num;
291     NSURL *url = [NSURL fileURLWithPath:filename];
292    
293     fetch = [[[NSFetchRequest alloc] init] autorelease];
294     [fetch setEntity:[NSEntityDescription entityForName:@"Xspf" inManagedObjectContext:moc]];
295     NSPredicate *predicate = [NSPredicate predicateWithFormat:@"urlString LIKE %@", [url absoluteString]];
296     [fetch setPredicate:predicate];
297     num = [moc countForFetchRequest:fetch error:&error];
298     if(error) {
299     NSLog(@"%@", [error localizedDescription]);
300     return NO;
301     }
302    
303     return num == 0;
304     }
305    
306 masaki 5 #pragma mark#### NSTokenField Delegate ####
307 masaki 6 #if 1
308 masaki 5 - (NSArray *)tokenField:(NSTokenField *)tokenField
309     completionsForSubstring:(NSString *)substring
310     indexOfToken:(NSInteger)tokenIndex
311     indexOfSelectedItem:(NSInteger *)selectedIndex
312     {
313 masaki 6 NSManagedObjectContext *moc = [appDelegate managedObjectContext];
314     NSFetchRequest *fetch = [[[NSFetchRequest alloc] init] autorelease];
315    
316     NSPredicate *predicate = [NSPredicate predicateWithFormat:
317     @"roman BEGINSWITH[cd] %@"
318     @"OR japanese BEGINSWITH[cd] %@"
319     @"OR yomigana BEGINSWITH[cd] %@", substring,substring,substring];
320     NSEntityDescription *entry = [NSEntityDescription entityForName:@"FamilyName"
321     inManagedObjectContext:moc];
322    
323     [fetch setEntity:entry];
324     [fetch setPredicate:predicate];
325    
326     NSError *error = nil;
327     NSArray *objects = [moc executeFetchRequest:fetch error:&error];
328     if(!objects) {
329     if(error) {
330     NSLog(@"fail fetch reason -> %@", error);
331     }
332     }
333    
334     NSString *entryName = @"";
335     switch([tokenField tag]) {
336     case 2000:
337     entryName = @"VoiceActor";
338     break;
339     case 2001:
340     entryName = @"Product";
341     break;
342     }
343    
344     if([objects count] > 0) {
345     NSMutableString *string = [NSMutableString string];
346     NSMutableArray *names = [NSMutableArray array];
347     for(id e in objects) {
348     if([string length]) {
349     [string appendString:@" OR "];
350     }
351     [string appendFormat:@"name BEGINSWITH[cd] %%@ "];
352     [names addObject:[e valueForKey:@"japanese"]];
353     }
354     predicate = [NSPredicate predicateWithFormat:string argumentArray:names];
355     } else {
356     predicate = [NSPredicate predicateWithFormat:@"name BEGINSWITH[cd] %@", substring];
357     }
358     entry = [NSEntityDescription entityForName:entryName inManagedObjectContext:moc];
359     [fetch setEntity:entry];
360     [fetch setPredicate:predicate];
361    
362     error = nil;
363     objects = [moc executeFetchRequest:fetch error:&error];
364     if(!objects) {
365     if(error) {
366     NSLog(@"fail fetch reason -> %@", error);
367     }
368     }
369    
370     NSMutableArray *result = [NSMutableArray arrayWithObject:substring];
371     for(id obj in objects) {
372     [result addObject:[obj valueForKey:@"name"]];
373     }
374    
375     return result;
376     }
377     #else
378     - (NSArray *)tokenField:(NSTokenField *)tokenField
379     completionsForSubstring:(NSString *)substring
380     indexOfToken:(NSInteger)tokenIndex
381     indexOfSelectedItem:(NSInteger *)selectedIndex
382     {
383 masaki 5 NSLog(@"Enter %@", NSStringFromSelector(_cmd));
384    
385 masaki 6 NSString *entryName = @"";
386     switch([tokenField tag]) {
387     case 2000:
388     entryName = @"VoiceActor";
389     break;
390     case 2001:
391     entryName = @"Product";
392     break;
393     }
394    
395 masaki 5 NSManagedObjectContext *moc = [appDelegate managedObjectContext];
396     NSPredicate *predicate = [NSPredicate predicateWithFormat:@"name BEGINSWITH[cd] %@", substring];
397 masaki 6 NSEntityDescription *entry = [NSEntityDescription entityForName:entryName
398 masaki 5 inManagedObjectContext:moc];
399     NSFetchRequest *fetch = [[[NSFetchRequest alloc] init] autorelease];
400     [fetch setEntity:entry];
401     [fetch setPredicate:predicate];
402    
403     NSError *error = nil;
404     NSArray *objects = [moc executeFetchRequest:fetch error:&error];
405     if(!objects) {
406     if(error) {
407     NSLog(@"fail fetch reason -> %@", error);
408     }
409     }
410    
411     NSMutableArray *result = [NSMutableArray array];
412     for(id obj in objects) {
413     [result addObject:[obj valueForKey:@"name"]];
414     }
415    
416     return result;
417     }
418 masaki 6 #endif
419 masaki 5
420     - (void)registerVoiceActor:(NSTokenField *)tokenField
421     {
422     id array = [tokenField objectValue];
423     if(![array isKindOfClass:[NSArray class]]) return;
424    
425 masaki 6 NSString *entryName = @"";
426     switch([tokenField tag]) {
427     case 2000:
428     entryName = @"VoiceActor";
429     break;
430     case 2001:
431     entryName = @"Product";
432     break;
433     }
434    
435 masaki 5 NSManagedObjectContext *moc = [appDelegate managedObjectContext];
436 masaki 6 NSEntityDescription *entry = [NSEntityDescription entityForName:entryName
437 masaki 5 inManagedObjectContext:moc];
438     NSFetchRequest *fetch = [[[NSFetchRequest alloc] init] autorelease];
439     [fetch setEntity:entry];
440    
441     for(id token in array) {
442     NSPredicate *predicate = [NSPredicate predicateWithFormat:@"name LIKE[cd] %@", token];
443     [fetch setPredicate:predicate];
444    
445     NSError *error = nil;
446     NSUInteger count = [moc countForFetchRequest:fetch error:&error];
447     if(error) {
448     NSLog(@"fail fetch reason -> %@", error);
449     continue;
450     }
451     if(count == 0) {
452 masaki 6 id obj = [NSEntityDescription insertNewObjectForEntityForName:entryName inManagedObjectContext:moc];
453 masaki 5 [obj setValue:token forKey:@"name"];
454     }
455     }
456     }
457     - (BOOL)control:(id)control textShouldEndEditing:(NSText *)fieldEditor
458     {
459 masaki 6 if([control tag] == 2000 || [control tag] == 2001) {
460 masaki 5 [self registerVoiceActor:control];
461     }
462    
463     return YES;
464     }
465    
466 masaki 10 #pragma mark#### XspfMCollectionView Delegate ####
467     - (void)enterAction:(XspfMCollectionView *)view
468     {
469     [self openXspf:view];
470     }
471 masaki 5
472     #pragma mark#### Test ####
473 masaki 2 - (IBAction)test01:(id)sender
474     {
475 masaki 7 [self truncateFamilyName];
476 masaki 6 [self buildFamilyNameFromFile];
477 masaki 2 }
478     - (IBAction)test02:(id)sender
479     {
480 masaki 10 NSResponder *responder = [[self window] firstResponder];
481     while(responder) {
482     NSLog(@"Responder -> %@", responder);
483     responder = [responder nextResponder];
484     }
485 masaki 2 }
486     - (IBAction)test03:(id)sender
487     {
488     NSFetchRequest *fetch = [[[NSFetchRequest alloc] init] autorelease];
489    
490     id moc = [appDelegate managedObjectContext];
491    
492     [fetch setEntity:[NSEntityDescription entityForName:@"Xspf" inManagedObjectContext:moc]];
493    
494     id objs = [moc executeFetchRequest:fetch error:NULL];
495     NSLog(@"Fetched -> %@", objs);
496     }
497     @end

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