Develop and Download Open Source Software

Browse Subversion Repository

Annotation of /XspfManager/XspfMMainWindowController.m

Parent Directory Parent Directory | Revision Log Revision Log


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

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