Develop and Download Open Source Software

Browse Subversion Repository

Annotation of /XspfManager/XspfMDetailViewController.m

Parent Directory Parent Directory | Revision Log Revision Log


Revision 187 - (hide annotations) (download)
Sat Jan 2 01:40:36 2010 UTC (14 years, 3 months ago) by masaki
File size: 8770 byte(s)
[New] HMUilitiesを使用するようにした。
1 masaki 47 //
2     // XspfMDetailViewController.m
3     // XspfManager
4     //
5     // Created by Hori,Masaki on 09/11/08.
6     // Copyright 2009 masakih. All rights reserved.
7     //
8    
9     #import "XspfMDetailViewController.h"
10    
11 masaki 183 #import "XspfManager.h"
12 masaki 47
13     @interface XspfMDetailViewController(HMPrivate)
14     - (void)buildFamilyNameFromFile;
15     @end
16    
17     @implementation XspfMDetailViewController
18    
19     - (id)init
20     {
21     [super initWithNibName:@"DetailView" bundle:nil];
22    
23     [self buildFamilyNameFromFile];
24    
25     return self;
26     }
27    
28     #pragma mark#### NSTokenField Delegate ####
29     #if 0
30     - (NSURL *)dictionayStoreURL
31     {
32     NSString *appSupport = [[NSApp delegate] applicationSupportFolder];
33     NSString *storeString = [appSupport stringByAppendingPathComponent:@"Dictionay.qdb"];
34    
35     return [NSURL fileURLWithPath:storeString];
36     }
37     - (id)dictionayStore
38     {
39     id store = [[[self managedObjectContext] persistentStoreCoordinator] persistentStoreForURL:[self dictionayStoreURL]];
40     if(!store) {
41     NSError *error = nil;
42     store = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel:[[NSApp delegate] managedObjectModel]];
43     if (![store addPersistentStoreWithType:NSSQLiteStoreType configuration:nil URL:[self dictionayStoreURL] options:nil error:&error]){
44     [[NSApplication sharedApplication] presentError:error];
45     }
46     }
47     return store;
48     }
49     #else
50     - (id)dictionayStore { return nil; }
51     #endif
52    
53     #if 1
54     - (NSArray *)tokenField:(NSTokenField *)tokenField
55     completionsForSubstring:(NSString *)substring
56     indexOfToken:(NSInteger)tokenIndex
57     indexOfSelectedItem:(NSInteger *)selectedIndex
58     {
59     NSManagedObjectContext *moc = [self managedObjectContext];
60     NSFetchRequest *fetch = [[[NSFetchRequest alloc] init] autorelease];
61    
62     NSPredicate *aPredicate = [NSPredicate predicateWithFormat:
63     @"roman BEGINSWITH[cd] %@"
64     @"OR japanese BEGINSWITH[cd] %@"
65     @"OR yomigana BEGINSWITH[cd] %@", substring,substring,substring];
66     NSEntityDescription *entry = [NSEntityDescription entityForName:@"FamilyName"
67     inManagedObjectContext:moc];
68    
69     [fetch setEntity:entry];
70     [fetch setPredicate:aPredicate];
71    
72     NSError *error = nil;
73     NSArray *objects = [moc executeFetchRequest:fetch error:&error];
74     if(!objects) {
75     if(error) {
76     NSLog(@"fail fetch reason -> %@", error);
77     }
78     }
79    
80     NSString *entryName = @"";
81     switch([tokenField tag]) {
82     case 2000:
83     entryName = @"VoiceActor";
84     break;
85     case 2001:
86     entryName = @"Product";
87     break;
88     }
89    
90     if([objects count] > 0) {
91     NSMutableString *string = [NSMutableString string];
92     NSMutableArray *names = [NSMutableArray array];
93     for(id e in objects) {
94     if([string length]) {
95     [string appendString:@" OR "];
96     }
97     [string appendFormat:@"name BEGINSWITH[cd] %%@ "];
98     [names addObject:[e valueForKey:@"japanese"]];
99     }
100     aPredicate = [NSPredicate predicateWithFormat:string argumentArray:names];
101     } else {
102     aPredicate = [NSPredicate predicateWithFormat:@"name BEGINSWITH[cd] %@", substring];
103     }
104     entry = [NSEntityDescription entityForName:entryName inManagedObjectContext:moc];
105     [fetch setEntity:entry];
106     [fetch setPredicate:aPredicate];
107    
108     error = nil;
109     objects = [moc executeFetchRequest:fetch error:&error];
110     if(!objects) {
111     if(error) {
112     NSLog(@"fail fetch reason -> %@", error);
113     }
114     }
115    
116     NSMutableArray *result = [NSMutableArray arrayWithObject:substring];
117     for(id obj in objects) {
118     [result addObject:[obj valueForKey:@"name"]];
119     }
120    
121     return result;
122     }
123     #else
124     - (NSArray *)tokenField:(NSTokenField *)tokenField
125     completionsForSubstring:(NSString *)substring
126     indexOfToken:(NSInteger)tokenIndex
127     indexOfSelectedItem:(NSInteger *)selectedIndex
128     {
129 masaki 187 HMLog(HMLogLevelDebug, @"Enter %@", NSStringFromSelector(_cmd));
130 masaki 47
131     NSString *entryName = @"";
132     switch([tokenField tag]) {
133     case 2000:
134     entryName = @"VoiceActor";
135     break;
136     case 2001:
137     entryName = @"Product";
138     break;
139     }
140    
141     NSManagedObjectContext *moc = [self managedObjectContext];
142     NSPredicate *predicate = [NSPredicate predicateWithFormat:@"name BEGINSWITH[cd] %@", substring];
143     NSEntityDescription *entry = [NSEntityDescription entityForName:entryName
144     inManagedObjectContext:moc];
145     NSFetchRequest *fetch = [[[NSFetchRequest alloc] init] autorelease];
146     [fetch setEntity:entry];
147     [fetch setPredicate:predicate];
148    
149     NSError *error = nil;
150     NSArray *objects = [moc executeFetchRequest:fetch error:&error];
151     if(!objects) {
152     if(error) {
153     NSLog(@"fail fetch reason -> %@", error);
154     }
155     }
156    
157     NSMutableArray *result = [NSMutableArray array];
158     for(id obj in objects) {
159     [result addObject:[obj valueForKey:@"name"]];
160     }
161    
162     return result;
163     }
164     #endif
165    
166     - (void)registerVoiceActor:(NSTokenField *)tokenField
167     {
168     id array = [tokenField objectValue];
169     if(![array isKindOfClass:[NSArray class]]) return;
170    
171     NSString *entryName = @"";
172     switch([tokenField tag]) {
173     case 2000:
174     entryName = @"VoiceActor";
175     break;
176     case 2001:
177     entryName = @"Product";
178     break;
179     }
180    
181     NSManagedObjectContext *moc = [self managedObjectContext];
182     NSEntityDescription *entry = [NSEntityDescription entityForName:entryName
183     inManagedObjectContext:moc];
184     NSFetchRequest *fetch = [[[NSFetchRequest alloc] init] autorelease];
185     [fetch setEntity:entry];
186    
187     for(id token in array) {
188     NSPredicate *aPredicate = [NSPredicate predicateWithFormat:@"name LIKE[cd] %@", token];
189     [fetch setPredicate:aPredicate];
190    
191     NSError *error = nil;
192     NSUInteger count = [moc countForFetchRequest:fetch error:&error];
193     if(error) {
194     NSLog(@"fail fetch reason -> %@", error);
195     continue;
196     }
197     if(count == 0) {
198     id obj = [NSEntityDescription insertNewObjectForEntityForName:entryName inManagedObjectContext:moc];
199     [obj setValue:token forKey:@"name"];
200     [moc assignObject:obj toPersistentStore:[self dictionayStore]];
201     }
202     }
203     }
204     - (BOOL)control:(id)control textShouldEndEditing:(NSText *)fieldEditor
205     {
206     if([control tag] == 2000 || [control tag] == 2001) {
207     [self registerVoiceActor:control];
208     }
209    
210     return YES;
211     }
212    
213     #pragma mark#### load familynames ####
214     - (NSArray *)arrayFromLFSeparatedFile:(NSString *)name
215     {
216     NSString *path;
217    
218     path = [[[NSApp delegate] applicationSupportFolder] stringByAppendingPathComponent:name];
219     path = [path stringByAppendingPathExtension:@"txt"];
220    
221     NSFileManager *fm = [NSFileManager defaultManager];
222     BOOL isDir = NO;
223     if(![fm fileExistsAtPath:path isDirectory:&isDir] || isDir) {
224     path = [[NSBundle mainBundle] pathForResource:name ofType:@"txt"];
225     }
226    
227     NSError *error = nil;
228     NSString *content = [NSString stringWithContentsOfFile:path
229     encoding:NSUTF8StringEncoding
230     error:&error];
231     if(error) {
232 masaki 187 HMLog(HMLogLevelDebug, @"path => %@", path);
233 masaki 47 NSLog(@"%@", [error localizedDescription]);
234     return NO;
235     }
236    
237     return [content componentsSeparatedByString:@"\x0a"];
238     }
239    
240     - (NSArray *)arrayFromTabSeparatedString:(NSString *)string
241     {
242     return [string componentsSeparatedByString:@"\t"];
243     }
244     - (BOOL)isEmptyEntityName:(NSString *)name
245     {
246     NSManagedObjectContext *moc = [self managedObjectContext];
247     NSError *error = nil;
248     NSFetchRequest *fetch;
249     NSInteger num;
250    
251     fetch = [[NSFetchRequest alloc] init];
252     [fetch setEntity:[NSEntityDescription entityForName:name
253     inManagedObjectContext:moc]];
254     num = [moc countForFetchRequest:fetch
255     error:&error];
256     [fetch release];
257     fetch = nil;
258     if(error) {
259     NSLog(@"%@", [error localizedDescription]);
260     return NO;
261     }
262    
263     return num == 0;
264     }
265     - (void)buildFamilyNameFromFile
266     {
267     NSManagedObjectContext *moc = [self managedObjectContext];
268    
269     NSString *entityName;
270     NSArray *contents;
271     entityName = @"FamilyName";
272     if([self isEmptyEntityName:entityName]) {
273     contents = [self arrayFromLFSeparatedFile:entityName];
274    
275     id key;
276     for(key in contents) {
277     NSArray *attr = [self arrayFromTabSeparatedString:key];
278     if([attr count] < 2) continue;
279    
280     id obj = [NSEntityDescription insertNewObjectForEntityForName:entityName
281     inManagedObjectContext:moc];
282     [moc assignObject:obj toPersistentStore:[self dictionayStore]];
283     [obj setValue:[attr objectAtIndex:0] forKey:@"roman"];
284     [obj setValue:[attr objectAtIndex:1] forKey:@"japanese"];
285    
286     if([attr count] > 2) {
287     [obj setValue:[attr objectAtIndex:2] forKey:@"yomigana"];
288     }
289     }
290     }
291    
292     }
293    
294     - (void)truncateFamilyName
295     {
296     NSManagedObjectContext *moc = [self managedObjectContext];
297     NSFetchRequest *fetch = [[[NSFetchRequest alloc] init] autorelease];
298     NSEntityDescription *entry = [NSEntityDescription entityForName:@"FamilyName"
299     inManagedObjectContext:moc];
300     [fetch setEntity:entry];
301    
302     NSError *error = nil;
303     NSArray *objects = [moc executeFetchRequest:fetch error:&error];
304     if(!objects) {
305     if(error) {
306     NSLog(@"fail fetch reason -> %@", error);
307     }
308     }
309    
310     [moc lock];
311     for(id obj in objects) {
312     [moc deleteObject:obj];
313     }
314     [moc unlock];
315     }
316    
317     #pragma mark#### Test ####
318     - (IBAction)test01:(id)sender
319     {
320     [self truncateFamilyName];
321     [self buildFamilyNameFromFile];
322     }
323    
324     @end

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