Develop and Download Open Source Software

Browse Subversion Repository

Annotation of /XspfManager/XspfMDetailViewController.m

Parent Directory Parent Directory | Revision Log Revision Log


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

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