Develop and Download Open Source Software

Browse Subversion Repository

Annotation of /XspfManager/XspfMDetailViewController.m

Parent Directory Parent Directory | Revision Log Revision Log


Revision 365 - (hide annotations) (download)
Fri May 21 13:38:42 2010 UTC (13 years, 11 months ago) by masakih
File size: 12353 byte(s)
ライセンス表示の行が長過ぎるので適当に行を分けた。
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 masaki 47
66     @interface XspfMDetailViewController(HMPrivate)
67     - (void)buildFamilyNameFromFile;
68     @end
69    
70     @implementation XspfMDetailViewController
71    
72     - (id)init
73     {
74     [super initWithNibName:@"DetailView" bundle:nil];
75    
76     [self buildFamilyNameFromFile];
77    
78     return self;
79     }
80    
81     #pragma mark#### NSTokenField Delegate ####
82     #if 0
83     - (NSURL *)dictionayStoreURL
84     {
85     NSString *appSupport = [[NSApp delegate] applicationSupportFolder];
86     NSString *storeString = [appSupport stringByAppendingPathComponent:@"Dictionay.qdb"];
87    
88     return [NSURL fileURLWithPath:storeString];
89     }
90     - (id)dictionayStore
91     {
92     id store = [[[self managedObjectContext] persistentStoreCoordinator] persistentStoreForURL:[self dictionayStoreURL]];
93     if(!store) {
94     NSError *error = nil;
95     store = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel:[[NSApp delegate] managedObjectModel]];
96     if (![store addPersistentStoreWithType:NSSQLiteStoreType configuration:nil URL:[self dictionayStoreURL] options:nil error:&error]){
97     [[NSApplication sharedApplication] presentError:error];
98     }
99     }
100     return store;
101     }
102     #else
103     - (id)dictionayStore { return nil; }
104     #endif
105    
106     #if 1
107     - (NSArray *)tokenField:(NSTokenField *)tokenField
108     completionsForSubstring:(NSString *)substring
109     indexOfToken:(NSInteger)tokenIndex
110     indexOfSelectedItem:(NSInteger *)selectedIndex
111     {
112     NSManagedObjectContext *moc = [self managedObjectContext];
113     NSFetchRequest *fetch = [[[NSFetchRequest alloc] init] autorelease];
114    
115     NSPredicate *aPredicate = [NSPredicate predicateWithFormat:
116     @"roman BEGINSWITH[cd] %@"
117     @"OR japanese BEGINSWITH[cd] %@"
118     @"OR yomigana BEGINSWITH[cd] %@", substring,substring,substring];
119     NSEntityDescription *entry = [NSEntityDescription entityForName:@"FamilyName"
120     inManagedObjectContext:moc];
121    
122     [fetch setEntity:entry];
123     [fetch setPredicate:aPredicate];
124    
125     NSError *error = nil;
126     NSArray *objects = [moc executeFetchRequest:fetch error:&error];
127     if(!objects) {
128     if(error) {
129 masaki 188 HMLog(HMLogLevelError, @"fail fetch reason -> %@", error);
130 masaki 47 }
131     }
132    
133     NSString *entryName = @"";
134     switch([tokenField tag]) {
135     case 2000:
136     entryName = @"VoiceActor";
137     break;
138     case 2001:
139     entryName = @"Product";
140     break;
141     }
142    
143     if([objects count] > 0) {
144     NSMutableString *string = [NSMutableString string];
145     NSMutableArray *names = [NSMutableArray array];
146     for(id e in objects) {
147     if([string length]) {
148     [string appendString:@" OR "];
149     }
150     [string appendFormat:@"name BEGINSWITH[cd] %%@ "];
151     [names addObject:[e valueForKey:@"japanese"]];
152     }
153     aPredicate = [NSPredicate predicateWithFormat:string argumentArray:names];
154     } else {
155     aPredicate = [NSPredicate predicateWithFormat:@"name BEGINSWITH[cd] %@", substring];
156     }
157     entry = [NSEntityDescription entityForName:entryName inManagedObjectContext:moc];
158     [fetch setEntity:entry];
159     [fetch setPredicate:aPredicate];
160    
161     error = nil;
162     objects = [moc executeFetchRequest:fetch error:&error];
163     if(!objects) {
164     if(error) {
165 masaki 188 HMLog(HMLogLevelError, @"fail fetch reason -> %@", error);
166 masaki 47 }
167     }
168    
169     NSMutableArray *result = [NSMutableArray arrayWithObject:substring];
170     for(id obj in objects) {
171     [result addObject:[obj valueForKey:@"name"]];
172     }
173    
174     return result;
175     }
176     #else
177     - (NSArray *)tokenField:(NSTokenField *)tokenField
178     completionsForSubstring:(NSString *)substring
179     indexOfToken:(NSInteger)tokenIndex
180     indexOfSelectedItem:(NSInteger *)selectedIndex
181     {
182 masaki 187 HMLog(HMLogLevelDebug, @"Enter %@", NSStringFromSelector(_cmd));
183 masaki 47
184     NSString *entryName = @"";
185     switch([tokenField tag]) {
186     case 2000:
187     entryName = @"VoiceActor";
188     break;
189     case 2001:
190     entryName = @"Product";
191     break;
192     }
193    
194     NSManagedObjectContext *moc = [self managedObjectContext];
195     NSPredicate *predicate = [NSPredicate predicateWithFormat:@"name BEGINSWITH[cd] %@", substring];
196     NSEntityDescription *entry = [NSEntityDescription entityForName:entryName
197     inManagedObjectContext:moc];
198     NSFetchRequest *fetch = [[[NSFetchRequest alloc] init] autorelease];
199     [fetch setEntity:entry];
200     [fetch setPredicate:predicate];
201    
202     NSError *error = nil;
203     NSArray *objects = [moc executeFetchRequest:fetch error:&error];
204     if(!objects) {
205     if(error) {
206 masaki 188 HMLog(HMLogLevelError, @"fail fetch reason -> %@", error);
207 masaki 47 }
208     }
209    
210     NSMutableArray *result = [NSMutableArray array];
211     for(id obj in objects) {
212     [result addObject:[obj valueForKey:@"name"]];
213     }
214    
215     return result;
216     }
217     #endif
218    
219     - (void)registerVoiceActor:(NSTokenField *)tokenField
220     {
221     id array = [tokenField objectValue];
222     if(![array isKindOfClass:[NSArray class]]) return;
223    
224     NSString *entryName = @"";
225     switch([tokenField tag]) {
226     case 2000:
227     entryName = @"VoiceActor";
228     break;
229     case 2001:
230     entryName = @"Product";
231     break;
232     }
233    
234     NSManagedObjectContext *moc = [self managedObjectContext];
235     NSEntityDescription *entry = [NSEntityDescription entityForName:entryName
236     inManagedObjectContext:moc];
237     NSFetchRequest *fetch = [[[NSFetchRequest alloc] init] autorelease];
238     [fetch setEntity:entry];
239    
240     for(id token in array) {
241     NSPredicate *aPredicate = [NSPredicate predicateWithFormat:@"name LIKE[cd] %@", token];
242     [fetch setPredicate:aPredicate];
243    
244     NSError *error = nil;
245     NSUInteger count = [moc countForFetchRequest:fetch error:&error];
246     if(error) {
247 masaki 188 HMLog(HMLogLevelError, @"fail fetch reason -> %@", error);
248 masaki 47 continue;
249     }
250     if(count == 0) {
251     id obj = [NSEntityDescription insertNewObjectForEntityForName:entryName inManagedObjectContext:moc];
252     [obj setValue:token forKey:@"name"];
253     [moc assignObject:obj toPersistentStore:[self dictionayStore]];
254     }
255     }
256     }
257     - (BOOL)control:(id)control textShouldEndEditing:(NSText *)fieldEditor
258     {
259     if([control tag] == 2000 || [control tag] == 2001) {
260     [self registerVoiceActor:control];
261     }
262    
263     return YES;
264     }
265    
266     #pragma mark#### load familynames ####
267     - (NSArray *)arrayFromLFSeparatedFile:(NSString *)name
268     {
269     NSString *path;
270    
271     path = [[[NSApp delegate] applicationSupportFolder] stringByAppendingPathComponent:name];
272     path = [path stringByAppendingPathExtension:@"txt"];
273    
274     NSFileManager *fm = [NSFileManager defaultManager];
275     BOOL isDir = NO;
276     if(![fm fileExistsAtPath:path isDirectory:&isDir] || isDir) {
277     path = [[NSBundle mainBundle] pathForResource:name ofType:@"txt"];
278     }
279    
280     NSError *error = nil;
281     NSString *content = [NSString stringWithContentsOfFile:path
282     encoding:NSUTF8StringEncoding
283     error:&error];
284     if(error) {
285 masaki 187 HMLog(HMLogLevelDebug, @"path => %@", path);
286 masaki 188 HMLog(HMLogLevelError, @"%@", [error localizedDescription]);
287 masaki 47 return NO;
288     }
289    
290     return [content componentsSeparatedByString:@"\x0a"];
291     }
292    
293     - (NSArray *)arrayFromTabSeparatedString:(NSString *)string
294     {
295     return [string componentsSeparatedByString:@"\t"];
296     }
297     - (BOOL)isEmptyEntityName:(NSString *)name
298     {
299     NSManagedObjectContext *moc = [self managedObjectContext];
300     NSError *error = nil;
301     NSFetchRequest *fetch;
302     NSInteger num;
303    
304     fetch = [[NSFetchRequest alloc] init];
305     [fetch setEntity:[NSEntityDescription entityForName:name
306     inManagedObjectContext:moc]];
307     num = [moc countForFetchRequest:fetch
308     error:&error];
309     [fetch release];
310     fetch = nil;
311     if(error) {
312 masaki 188 HMLog(HMLogLevelError, @"%@", [error localizedDescription]);
313 masaki 47 return NO;
314     }
315    
316     return num == 0;
317     }
318     - (void)buildFamilyNameFromFile
319     {
320     NSManagedObjectContext *moc = [self managedObjectContext];
321    
322     NSString *entityName;
323     NSArray *contents;
324     entityName = @"FamilyName";
325     if([self isEmptyEntityName:entityName]) {
326     contents = [self arrayFromLFSeparatedFile:entityName];
327    
328     id key;
329     for(key in contents) {
330     NSArray *attr = [self arrayFromTabSeparatedString:key];
331     if([attr count] < 2) continue;
332    
333     id obj = [NSEntityDescription insertNewObjectForEntityForName:entityName
334     inManagedObjectContext:moc];
335     [moc assignObject:obj toPersistentStore:[self dictionayStore]];
336     [obj setValue:[attr objectAtIndex:0] forKey:@"roman"];
337     [obj setValue:[attr objectAtIndex:1] forKey:@"japanese"];
338    
339     if([attr count] > 2) {
340     [obj setValue:[attr objectAtIndex:2] forKey:@"yomigana"];
341     }
342     }
343     }
344    
345     }
346    
347     - (void)truncateFamilyName
348     {
349     NSManagedObjectContext *moc = [self managedObjectContext];
350     NSFetchRequest *fetch = [[[NSFetchRequest alloc] init] autorelease];
351     NSEntityDescription *entry = [NSEntityDescription entityForName:@"FamilyName"
352     inManagedObjectContext:moc];
353     [fetch setEntity:entry];
354    
355     NSError *error = nil;
356     NSArray *objects = [moc executeFetchRequest:fetch error:&error];
357     if(!objects) {
358     if(error) {
359 masaki 188 HMLog(HMLogLevelError, @"fail fetch reason -> %@", error);
360 masaki 47 }
361     }
362    
363     [moc lock];
364     for(id obj in objects) {
365     [moc deleteObject:obj];
366     }
367     [moc unlock];
368     }
369    
370     #pragma mark#### Test ####
371     - (IBAction)test01:(id)sender
372     {
373     [self truncateFamilyName];
374     [self buildFamilyNameFromFile];
375     }
376    
377     @end

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