Develop and Download Open Source Software

Browse Subversion Repository

Annotation of /XspfManager/XspfMMainWindowController.m

Parent Directory Parent Directory | Revision Log Revision Log


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

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