Develop and Download Open Source Software

Browse Subversion Repository

Annotation of /XspfManager/XspfMDetailViewController.m

Parent Directory Parent Directory | Revision Log Revision Log


Revision 408 - (hide annotations) (download)
Fri Jun 11 15:56:27 2010 UTC (13 years, 10 months ago) by masakih
File size: 12383 byte(s)
IBでバインディングを行うようにしたので不要になった部分を削除。
1 masaki 47 //
2     // XspfMDetailViewController.m
3     // XspfManager
4     //
5     // Created by Hori,Masaki on 09/11/08.
6     //
7    
8 masakih 350 /*
9 masakih 365 This source code is release under the New BSD License.
10 masakih 350 Copyright (c) 2009-2010, masakih
11     All rights reserved.
12    
13 masakih 365 ソースコード形式かバイナリ形式か、変更するかしないかを問わず、以下の条件を満たす場合に
14     限り、再頒布および使用が許可されます。
15    
16     1, ソースコードを再頒布する場合、上記の著作権表示、本条件一覧、および下記免責条項を含
17     めること。
18     2, バイナリ形式で再頒布する場合、頒布物に付属のドキュメント等の資料に、上記の著作権表
19     示、本条件一覧、および下記免責条項を含めること。
20     3, 書面による特別の許可なしに、本ソフトウェアから派生した製品の宣伝または販売促進に、
21     コントリビューターの名前を使用してはならない。
22     本ソフトウェアは、著作権者およびコントリビューターによって「現状のまま」提供されており、
23     明示黙示を問わず、商業的な使用可能性、および特定の目的に対する適合性に関する暗黙の保証
24     も含め、またそれに限定されない、いかなる保証もありません。著作権者もコントリビューター
25     も、事由のいかんを問わず、 損害発生の原因いかんを問わず、かつ責任の根拠が契約であるか
26     厳格責任であるか(過失その他の)不法行為であるかを問わず、仮にそのような損害が発生する
27     可能性を知らされていたとしても、本ソフトウェアの使用によって発生した(代替品または代用
28     サービスの調達、使用の喪失、データの喪失、利益の喪失、業務の中断も含め、またそれに限定
29     されない)直接損害、間接損害、偶発的な損害、特別損害、懲罰的損害、または結果損害につい
30     て、一切責任を負わないものとします。
31 masakih 350 -------------------------------------------------------------------
32     Copyright (c) 2009-2010, masakih
33     All rights reserved.
34    
35 masakih 365 Redistribution and use in source and binary forms, with or without
36     modification, are permitted provided that the following conditions
37     are met:
38 masakih 350
39 masakih 365 1, Redistributions of source code must retain the above copyright
40     notice, this list of conditions and the following disclaimer.
41     2, Redistributions in binary form must reproduce the above copyright
42     notice, this list of conditions and the following disclaimer in
43     the documentation and/or other materials provided with the
44     distribution.
45     3, The names of its contributors may be used to endorse or promote
46     products derived from this software without specific prior
47     written permission.
48     THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
49     "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
50     LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
51     FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
52     COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
53     INCIDENTAL, SPECIAL,EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
54     BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
55     LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
56     CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
57     LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
58     ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
59     POSSIBILITY OF SUCH DAMAGE.
60 masakih 350 */
61    
62 masaki 47 #import "XspfMDetailViewController.h"
63    
64 masaki 183 #import "XspfManager.h"
65 masakih 396 #import "XspfMLabelControl.h"
66 masaki 47
67     @interface XspfMDetailViewController(HMPrivate)
68     - (void)buildFamilyNameFromFile;
69     @end
70    
71     @implementation XspfMDetailViewController
72    
73     - (id)init
74     {
75     [super initWithNibName:@"DetailView" bundle:nil];
76    
77     [self buildFamilyNameFromFile];
78    
79     return self;
80     }
81    
82     #pragma mark#### NSTokenField Delegate ####
83     #if 0
84     - (NSURL *)dictionayStoreURL
85     {
86     NSString *appSupport = [[NSApp delegate] applicationSupportFolder];
87     NSString *storeString = [appSupport stringByAppendingPathComponent:@"Dictionay.qdb"];
88    
89     return [NSURL fileURLWithPath:storeString];
90     }
91     - (id)dictionayStore
92     {
93     id store = [[[self managedObjectContext] persistentStoreCoordinator] persistentStoreForURL:[self dictionayStoreURL]];
94     if(!store) {
95     NSError *error = nil;
96     store = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel:[[NSApp delegate] managedObjectModel]];
97     if (![store addPersistentStoreWithType:NSSQLiteStoreType configuration:nil URL:[self dictionayStoreURL] options:nil error:&error]){
98     [[NSApplication sharedApplication] presentError:error];
99     }
100     }
101     return store;
102     }
103     #else
104     - (id)dictionayStore { return nil; }
105     #endif
106    
107     #if 1
108     - (NSArray *)tokenField:(NSTokenField *)tokenField
109     completionsForSubstring:(NSString *)substring
110     indexOfToken:(NSInteger)tokenIndex
111     indexOfSelectedItem:(NSInteger *)selectedIndex
112     {
113     NSManagedObjectContext *moc = [self managedObjectContext];
114     NSFetchRequest *fetch = [[[NSFetchRequest alloc] init] autorelease];
115    
116     NSPredicate *aPredicate = [NSPredicate predicateWithFormat:
117     @"roman BEGINSWITH[cd] %@"
118     @"OR japanese BEGINSWITH[cd] %@"
119     @"OR yomigana BEGINSWITH[cd] %@", substring,substring,substring];
120     NSEntityDescription *entry = [NSEntityDescription entityForName:@"FamilyName"
121     inManagedObjectContext:moc];
122    
123     [fetch setEntity:entry];
124     [fetch setPredicate:aPredicate];
125    
126     NSError *error = nil;
127     NSArray *objects = [moc executeFetchRequest:fetch error:&error];
128     if(!objects) {
129     if(error) {
130 masaki 188 HMLog(HMLogLevelError, @"fail fetch reason -> %@", error);
131 masaki 47 }
132     }
133    
134     NSString *entryName = @"";
135     switch([tokenField tag]) {
136     case 2000:
137     entryName = @"VoiceActor";
138     break;
139     case 2001:
140     entryName = @"Product";
141     break;
142     }
143    
144     if([objects count] > 0) {
145     NSMutableString *string = [NSMutableString string];
146     NSMutableArray *names = [NSMutableArray array];
147     for(id e in objects) {
148     if([string length]) {
149     [string appendString:@" OR "];
150     }
151     [string appendFormat:@"name BEGINSWITH[cd] %%@ "];
152     [names addObject:[e valueForKey:@"japanese"]];
153     }
154     aPredicate = [NSPredicate predicateWithFormat:string argumentArray:names];
155     } else {
156     aPredicate = [NSPredicate predicateWithFormat:@"name BEGINSWITH[cd] %@", substring];
157     }
158     entry = [NSEntityDescription entityForName:entryName inManagedObjectContext:moc];
159     [fetch setEntity:entry];
160     [fetch setPredicate:aPredicate];
161    
162     error = nil;
163     objects = [moc executeFetchRequest:fetch error:&error];
164     if(!objects) {
165     if(error) {
166 masaki 188 HMLog(HMLogLevelError, @"fail fetch reason -> %@", error);
167 masaki 47 }
168     }
169    
170     NSMutableArray *result = [NSMutableArray arrayWithObject:substring];
171     for(id obj in objects) {
172     [result addObject:[obj valueForKey:@"name"]];
173     }
174    
175     return result;
176     }
177     #else
178     - (NSArray *)tokenField:(NSTokenField *)tokenField
179     completionsForSubstring:(NSString *)substring
180     indexOfToken:(NSInteger)tokenIndex
181     indexOfSelectedItem:(NSInteger *)selectedIndex
182     {
183 masaki 187 HMLog(HMLogLevelDebug, @"Enter %@", NSStringFromSelector(_cmd));
184 masaki 47
185     NSString *entryName = @"";
186     switch([tokenField tag]) {
187     case 2000:
188     entryName = @"VoiceActor";
189     break;
190     case 2001:
191     entryName = @"Product";
192     break;
193     }
194    
195     NSManagedObjectContext *moc = [self managedObjectContext];
196     NSPredicate *predicate = [NSPredicate predicateWithFormat:@"name BEGINSWITH[cd] %@", substring];
197     NSEntityDescription *entry = [NSEntityDescription entityForName:entryName
198     inManagedObjectContext:moc];
199     NSFetchRequest *fetch = [[[NSFetchRequest alloc] init] autorelease];
200     [fetch setEntity:entry];
201     [fetch setPredicate:predicate];
202    
203     NSError *error = nil;
204     NSArray *objects = [moc executeFetchRequest:fetch error:&error];
205     if(!objects) {
206     if(error) {
207 masaki 188 HMLog(HMLogLevelError, @"fail fetch reason -> %@", error);
208 masaki 47 }
209     }
210    
211     NSMutableArray *result = [NSMutableArray array];
212     for(id obj in objects) {
213     [result addObject:[obj valueForKey:@"name"]];
214     }
215    
216     return result;
217     }
218     #endif
219    
220     - (void)registerVoiceActor:(NSTokenField *)tokenField
221     {
222     id array = [tokenField objectValue];
223     if(![array isKindOfClass:[NSArray class]]) return;
224    
225     NSString *entryName = @"";
226     switch([tokenField tag]) {
227     case 2000:
228     entryName = @"VoiceActor";
229     break;
230     case 2001:
231     entryName = @"Product";
232     break;
233     }
234    
235     NSManagedObjectContext *moc = [self managedObjectContext];
236     NSEntityDescription *entry = [NSEntityDescription entityForName:entryName
237     inManagedObjectContext:moc];
238     NSFetchRequest *fetch = [[[NSFetchRequest alloc] init] autorelease];
239     [fetch setEntity:entry];
240    
241     for(id token in array) {
242     NSPredicate *aPredicate = [NSPredicate predicateWithFormat:@"name LIKE[cd] %@", token];
243     [fetch setPredicate:aPredicate];
244    
245     NSError *error = nil;
246     NSUInteger count = [moc countForFetchRequest:fetch error:&error];
247     if(error) {
248 masaki 188 HMLog(HMLogLevelError, @"fail fetch reason -> %@", error);
249 masaki 47 continue;
250     }
251     if(count == 0) {
252     id obj = [NSEntityDescription insertNewObjectForEntityForName:entryName inManagedObjectContext:moc];
253     [obj setValue:token forKey:@"name"];
254     [moc assignObject:obj toPersistentStore:[self dictionayStore]];
255     }
256     }
257     }
258     - (BOOL)control:(id)control textShouldEndEditing:(NSText *)fieldEditor
259     {
260     if([control tag] == 2000 || [control tag] == 2001) {
261     [self registerVoiceActor:control];
262     }
263    
264     return YES;
265     }
266    
267     #pragma mark#### load familynames ####
268     - (NSArray *)arrayFromLFSeparatedFile:(NSString *)name
269     {
270     NSString *path;
271    
272     path = [[[NSApp delegate] applicationSupportFolder] stringByAppendingPathComponent:name];
273     path = [path stringByAppendingPathExtension:@"txt"];
274    
275     NSFileManager *fm = [NSFileManager defaultManager];
276     BOOL isDir = NO;
277     if(![fm fileExistsAtPath:path isDirectory:&isDir] || isDir) {
278     path = [[NSBundle mainBundle] pathForResource:name ofType:@"txt"];
279     }
280    
281     NSError *error = nil;
282     NSString *content = [NSString stringWithContentsOfFile:path
283     encoding:NSUTF8StringEncoding
284     error:&error];
285     if(error) {
286 masaki 187 HMLog(HMLogLevelDebug, @"path => %@", path);
287 masaki 188 HMLog(HMLogLevelError, @"%@", [error localizedDescription]);
288 masaki 47 return NO;
289     }
290    
291     return [content componentsSeparatedByString:@"\x0a"];
292     }
293    
294     - (NSArray *)arrayFromTabSeparatedString:(NSString *)string
295     {
296     return [string componentsSeparatedByString:@"\t"];
297     }
298     - (BOOL)isEmptyEntityName:(NSString *)name
299     {
300     NSManagedObjectContext *moc = [self managedObjectContext];
301     NSError *error = nil;
302     NSFetchRequest *fetch;
303     NSInteger num;
304    
305     fetch = [[NSFetchRequest alloc] init];
306     [fetch setEntity:[NSEntityDescription entityForName:name
307     inManagedObjectContext:moc]];
308     num = [moc countForFetchRequest:fetch
309     error:&error];
310     [fetch release];
311     fetch = nil;
312     if(error) {
313 masaki 188 HMLog(HMLogLevelError, @"%@", [error localizedDescription]);
314 masaki 47 return NO;
315     }
316    
317     return num == 0;
318     }
319     - (void)buildFamilyNameFromFile
320     {
321     NSManagedObjectContext *moc = [self managedObjectContext];
322    
323     NSString *entityName;
324     NSArray *contents;
325     entityName = @"FamilyName";
326     if([self isEmptyEntityName:entityName]) {
327     contents = [self arrayFromLFSeparatedFile:entityName];
328    
329     id key;
330     for(key in contents) {
331     NSArray *attr = [self arrayFromTabSeparatedString:key];
332     if([attr count] < 2) continue;
333    
334     id obj = [NSEntityDescription insertNewObjectForEntityForName:entityName
335     inManagedObjectContext:moc];
336     [moc assignObject:obj toPersistentStore:[self dictionayStore]];
337     [obj setValue:[attr objectAtIndex:0] forKey:@"roman"];
338     [obj setValue:[attr objectAtIndex:1] forKey:@"japanese"];
339    
340     if([attr count] > 2) {
341     [obj setValue:[attr objectAtIndex:2] forKey:@"yomigana"];
342     }
343     }
344     }
345    
346     }
347    
348     - (void)truncateFamilyName
349     {
350     NSManagedObjectContext *moc = [self managedObjectContext];
351     NSFetchRequest *fetch = [[[NSFetchRequest alloc] init] autorelease];
352     NSEntityDescription *entry = [NSEntityDescription entityForName:@"FamilyName"
353     inManagedObjectContext:moc];
354     [fetch setEntity:entry];
355    
356     NSError *error = nil;
357     NSArray *objects = [moc executeFetchRequest:fetch error:&error];
358     if(!objects) {
359     if(error) {
360 masaki 188 HMLog(HMLogLevelError, @"fail fetch reason -> %@", error);
361 masaki 47 }
362     }
363    
364     [moc lock];
365     for(id obj in objects) {
366     [moc deleteObject:obj];
367     }
368     [moc unlock];
369     }
370    
371     #pragma mark#### Test ####
372     - (IBAction)test01:(id)sender
373     {
374     [self truncateFamilyName];
375     [self buildFamilyNameFromFile];
376     }
377    
378     @end

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