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