| 1 |
masaki |
2 |
// |
| 2 |
|
|
// XspfManager.m |
| 3 |
|
|
// XspfManager |
| 4 |
|
|
// |
| 5 |
|
|
// Created by Hori,Masaki on 09/11/01. |
| 6 |
|
|
// Copyright 2009 masakih. All rights reserved. |
| 7 |
|
|
// |
| 8 |
|
|
|
| 9 |
|
|
#import "XspfManager.h" |
| 10 |
|
|
|
| 11 |
|
|
#import "XspfMMovieLoadRequest.h" |
| 12 |
|
|
|
| 13 |
masaki |
16 |
#import "XspfMCollectionViewController.h" |
| 14 |
masaki |
25 |
#import "XspfMListViewController.h" |
| 15 |
masaki |
2 |
|
| 16 |
masaki |
16 |
@interface XspfManager(HMPrivate) |
| 17 |
|
|
- (void)buildFamilyNameFromFile; |
| 18 |
masaki |
25 |
- (void)changeViewType:(XspfMViewType)newType; |
| 19 |
|
|
- (void)setCurrentListViewType:(XspfMViewType)newType; |
| 20 |
masaki |
16 |
@end |
| 21 |
|
|
|
| 22 |
masaki |
2 |
@implementation XspfManager |
| 23 |
masaki |
36 |
|
| 24 |
|
|
@dynamic xspfList; |
| 25 |
|
|
|
| 26 |
masaki |
8 |
static XspfManager *sharedInstance = nil; |
| 27 |
masaki |
2 |
|
| 28 |
masaki |
8 |
+ (XspfManager *)sharedInstance |
| 29 |
masaki |
2 |
{ |
| 30 |
masaki |
8 |
@synchronized(self) { |
| 31 |
|
|
if (sharedInstance == nil) { |
| 32 |
|
|
[[self alloc] init]; // assignment not done here |
| 33 |
|
|
} |
| 34 |
|
|
} |
| 35 |
|
|
return sharedInstance; |
| 36 |
|
|
} |
| 37 |
|
|
|
| 38 |
|
|
+ (id)allocWithZone:(NSZone *)zone |
| 39 |
|
|
{ |
| 40 |
|
|
@synchronized(self) { |
| 41 |
|
|
if (sharedInstance == nil) { |
| 42 |
|
|
sharedInstance = [super allocWithZone:zone]; |
| 43 |
|
|
return sharedInstance; // assignment and return on first allocation |
| 44 |
|
|
} |
| 45 |
|
|
} |
| 46 |
|
|
return nil; //on subsequent allocation attempts return nil |
| 47 |
|
|
} |
| 48 |
|
|
|
| 49 |
|
|
- (id)copyWithZone:(NSZone *)zone |
| 50 |
|
|
{ |
| 51 |
|
|
return self; |
| 52 |
|
|
} |
| 53 |
|
|
|
| 54 |
|
|
- (id)retain |
| 55 |
|
|
{ |
| 56 |
|
|
return self; |
| 57 |
|
|
} |
| 58 |
|
|
|
| 59 |
|
|
- (unsigned)retainCount |
| 60 |
|
|
{ |
| 61 |
|
|
return UINT_MAX; //denotes an object that cannot be released |
| 62 |
|
|
} |
| 63 |
|
|
|
| 64 |
|
|
- (void)release |
| 65 |
|
|
{ |
| 66 |
|
|
//do nothing |
| 67 |
|
|
} |
| 68 |
|
|
|
| 69 |
|
|
- (id)autorelease |
| 70 |
|
|
{ |
| 71 |
|
|
return self; |
| 72 |
|
|
} |
| 73 |
|
|
|
| 74 |
|
|
|
| 75 |
|
|
- (id)init |
| 76 |
|
|
{ |
| 77 |
|
|
[super initWithWindowNibName:@"MainWindow"]; |
| 78 |
masaki |
2 |
channel = [[HMChannel alloc] initWithWorkerNum:1]; |
| 79 |
masaki |
8 |
|
| 80 |
masaki |
25 |
viewControllers = [[NSMutableDictionary alloc] init]; |
| 81 |
|
|
|
| 82 |
masaki |
8 |
return self; |
| 83 |
masaki |
2 |
} |
| 84 |
masaki |
8 |
- (void)awakeFromNib |
| 85 |
|
|
{ |
| 86 |
|
|
if(appDelegate && [self window]) { |
| 87 |
masaki |
36 |
[self setupLists]; |
| 88 |
masaki |
8 |
[self buildFamilyNameFromFile]; |
| 89 |
masaki |
16 |
|
| 90 |
masaki |
25 |
[self setCurrentListViewType:typeCollectionView]; |
| 91 |
masaki |
16 |
|
| 92 |
masaki |
8 |
[self showWindow:self]; |
| 93 |
|
|
} |
| 94 |
|
|
} |
| 95 |
masaki |
25 |
|
| 96 |
|
|
#pragma mark#### KVC #### |
| 97 |
masaki |
8 |
- (NSManagedObjectContext *)managedObjectContext |
| 98 |
|
|
{ |
| 99 |
|
|
return [appDelegate managedObjectContext]; |
| 100 |
|
|
} |
| 101 |
masaki |
16 |
- (NSArrayController *)arrayController |
| 102 |
|
|
{ |
| 103 |
|
|
return controller; |
| 104 |
|
|
} |
| 105 |
masaki |
2 |
|
| 106 |
masaki |
25 |
- (XspfMViewType)currentListViewType |
| 107 |
|
|
{ |
| 108 |
|
|
return currentListViewType; |
| 109 |
|
|
} |
| 110 |
|
|
- (void)setCurrentListViewType:(XspfMViewType)newType |
| 111 |
|
|
{ |
| 112 |
|
|
if(currentListViewType == newType) return; |
| 113 |
|
|
|
| 114 |
|
|
[self changeViewType:newType]; |
| 115 |
|
|
} |
| 116 |
masaki |
16 |
|
| 117 |
masaki |
36 |
- (void)setXspfList:(id)newList |
| 118 |
|
|
{ |
| 119 |
|
|
[xspfList release]; |
| 120 |
|
|
xspfList = [newList retain]; |
| 121 |
|
|
} |
| 122 |
|
|
- (id)xspfList |
| 123 |
|
|
{ |
| 124 |
|
|
return xspfList; |
| 125 |
|
|
} |
| 126 |
|
|
- (void)setListPredicate:(NSPredicate *)newPredicate |
| 127 |
|
|
{ |
| 128 |
|
|
[listPredicate release]; |
| 129 |
|
|
listPredicate = [newPredicate retain]; |
| 130 |
|
|
} |
| 131 |
|
|
- (NSPredicate *)listPredicate |
| 132 |
|
|
{ |
| 133 |
|
|
return listPredicate; |
| 134 |
|
|
} |
| 135 |
|
|
|
| 136 |
|
|
- (NSArray *)sortDescriptors |
| 137 |
|
|
{ |
| 138 |
|
|
id desc = [[NSSortDescriptor alloc] initWithKey:@"order" ascending:YES]; |
| 139 |
|
|
return [NSArray arrayWithObject:[desc autorelease]]; |
| 140 |
|
|
} |
| 141 |
|
|
|
| 142 |
masaki |
13 |
#pragma mark#### Actions #### |
| 143 |
masaki |
10 |
- (IBAction)openXspf:(id)sender |
| 144 |
|
|
{ |
| 145 |
masaki |
14 |
id rep = [controller valueForKeyPath:@"selection.self"]; |
| 146 |
|
|
id filePath = [rep valueForKey:@"filePath"]; |
| 147 |
|
|
if([filePath isKindOfClass:[NSString class]]) { |
| 148 |
masaki |
10 |
NSWorkspace *ws = [NSWorkspace sharedWorkspace]; |
| 149 |
masaki |
14 |
[ws openFile:filePath withApplication:@"XspfQT"]; |
| 150 |
|
|
[rep setValue:[NSDate dateWithTimeIntervalSinceNow:0.0] forKey:@"lastPlayDate"]; |
| 151 |
masaki |
10 |
} |
| 152 |
|
|
} |
| 153 |
masaki |
2 |
- (IBAction)add:(id)sender |
| 154 |
|
|
{ |
| 155 |
|
|
NSOpenPanel *panel = [NSOpenPanel openPanel]; |
| 156 |
|
|
|
| 157 |
|
|
[panel setAllowedFileTypes:[NSArray arrayWithObjects:@"xspf", @"com.masakih.xspf", nil]]; |
| 158 |
|
|
[panel setAllowsMultipleSelection:YES]; |
| 159 |
masaki |
9 |
[panel setDelegate:self]; |
| 160 |
masaki |
2 |
|
| 161 |
|
|
[panel beginSheetForDirectory:nil |
| 162 |
|
|
file:nil |
| 163 |
|
|
types:[NSArray arrayWithObjects:@"xspf", @"com.masakih.xspf", nil] |
| 164 |
masaki |
8 |
modalForWindow:[self window] |
| 165 |
masaki |
2 |
modalDelegate:self |
| 166 |
|
|
didEndSelector:@selector(endOpenPanel:::) |
| 167 |
|
|
contextInfo:NULL]; |
| 168 |
|
|
} |
| 169 |
|
|
|
| 170 |
masaki |
13 |
|
| 171 |
|
|
- (NSInteger)registerWithURL:(NSURL *)url |
| 172 |
|
|
{ |
| 173 |
|
|
id obj = [NSEntityDescription insertNewObjectForEntityForName:@"Xspf" |
| 174 |
|
|
inManagedObjectContext:[appDelegate managedObjectContext]]; |
| 175 |
|
|
if(!obj) return 1; |
| 176 |
|
|
|
| 177 |
|
|
id info = [NSEntityDescription insertNewObjectForEntityForName:@"Info" |
| 178 |
|
|
inManagedObjectContext:[appDelegate managedObjectContext]]; |
| 179 |
|
|
if(!info) { |
| 180 |
|
|
[[appDelegate managedObjectContext] deleteObject:obj]; |
| 181 |
|
|
return 2; |
| 182 |
|
|
} |
| 183 |
|
|
|
| 184 |
|
|
[obj setValue:info forKey:@"information"]; |
| 185 |
|
|
[obj setValue:url forKey:@"url"]; |
| 186 |
|
|
[obj setValue:[NSDate dateWithTimeIntervalSinceNow:0.0] forKey:@"registerDate"]; |
| 187 |
|
|
|
| 188 |
masaki |
33 |
// will reset in XspfMMovieLoadRequest. |
| 189 |
masaki |
35 |
// [obj setValue:[NSDate dateWithTimeIntervalSinceNow:0.0] forKey:@"modificationDate"]; |
| 190 |
|
|
// [obj setValue:[NSDate dateWithTimeIntervalSinceNow:0.0] forKey:@"creationDate"]; |
| 191 |
masaki |
33 |
|
| 192 |
masaki |
13 |
XspfMMovieLoadRequest *request = [XspfMMovieLoadRequest requestWithObject:obj url:url]; |
| 193 |
|
|
[channel putRequest:request]; |
| 194 |
|
|
|
| 195 |
masaki |
35 |
// [controller performSelector:@selector(setSelectedObjects:) withObject:[NSArray arrayWithObject:obj] afterDelay:0.0]; |
| 196 |
masaki |
13 |
|
| 197 |
|
|
return noErr; |
| 198 |
|
|
} |
| 199 |
masaki |
2 |
- (void)endOpenPanel:(NSOpenPanel *)panel :(NSInteger)returnCode :(void *)context |
| 200 |
|
|
{ |
| 201 |
masaki |
9 |
[panel orderOut:nil]; |
| 202 |
|
|
|
| 203 |
masaki |
2 |
if(returnCode == NSCancelButton) return; |
| 204 |
|
|
|
| 205 |
|
|
NSArray *URLs = [panel URLs]; |
| 206 |
|
|
if([URLs count] == 0) return; |
| 207 |
|
|
|
| 208 |
|
|
[progressBar setUsesThreadedAnimation:YES]; |
| 209 |
|
|
|
| 210 |
|
|
[NSApp beginSheet:progressPanel |
| 211 |
masaki |
8 |
modalForWindow:[self window] |
| 212 |
masaki |
2 |
modalDelegate:nil |
| 213 |
|
|
didEndSelector:Nil |
| 214 |
|
|
contextInfo:NULL]; |
| 215 |
|
|
[progressBar startAnimation:self]; |
| 216 |
|
|
[progressMessage setStringValue:@"During register."]; |
| 217 |
|
|
|
| 218 |
|
|
for(id URL in URLs) { |
| 219 |
|
|
[self registerWithURL:URL]; |
| 220 |
|
|
} |
| 221 |
|
|
|
| 222 |
|
|
[progressBar stopAnimation:self]; |
| 223 |
|
|
[progressPanel orderOut:self]; |
| 224 |
|
|
[NSApp endSheet:progressPanel]; |
| 225 |
|
|
} |
| 226 |
|
|
|
| 227 |
masaki |
25 |
#pragma mark#### Other methods #### |
| 228 |
|
|
- (void)changeViewType:(XspfMViewType)viewType |
| 229 |
|
|
{ |
| 230 |
|
|
if(currentListViewType == viewType) return; |
| 231 |
|
|
currentListViewType = viewType; |
| 232 |
|
|
|
| 233 |
|
|
NSString *className = nil; |
| 234 |
|
|
switch(currentListViewType) { |
| 235 |
|
|
case typeCollectionView: |
| 236 |
|
|
className = @"XspfMCollectionViewController"; |
| 237 |
|
|
break; |
| 238 |
|
|
case typeTableView: |
| 239 |
|
|
className = @"XspfMListViewController"; |
| 240 |
|
|
break; |
| 241 |
|
|
} |
| 242 |
|
|
if(!className) return; |
| 243 |
|
|
|
| 244 |
|
|
NSViewController *targetContorller = [viewControllers objectForKey:className]; |
| 245 |
|
|
if(!targetContorller) { |
| 246 |
|
|
targetContorller = [[[NSClassFromString(className) alloc] init] autorelease]; |
| 247 |
|
|
if(!targetContorller) return; |
| 248 |
|
|
[viewControllers setObject:targetContorller forKey:className]; |
| 249 |
masaki |
30 |
[targetContorller setRepresentedObject:controller]; |
| 250 |
masaki |
25 |
} |
| 251 |
|
|
|
| 252 |
|
|
[[listViewController view] removeFromSuperview]; |
| 253 |
|
|
listViewController = targetContorller; |
| 254 |
|
|
[listView addSubview:[listViewController view]]; |
| 255 |
|
|
[[listViewController view] setFrame:[listView bounds]]; |
| 256 |
|
|
} |
| 257 |
masaki |
2 |
|
| 258 |
masaki |
5 |
|
| 259 |
masaki |
36 |
- (void)setupLists |
| 260 |
|
|
{ |
| 261 |
|
|
NSManagedObjectContext *moc = [appDelegate managedObjectContext]; |
| 262 |
|
|
NSError *error = nil; |
| 263 |
|
|
NSFetchRequest *fetch; |
| 264 |
|
|
NSInteger num; |
| 265 |
|
|
|
| 266 |
|
|
fetch = [[NSFetchRequest alloc] init]; |
| 267 |
|
|
[fetch setEntity:[NSEntityDescription entityForName:@"XspfList" |
| 268 |
|
|
inManagedObjectContext:moc]]; |
| 269 |
|
|
num = [moc countForFetchRequest:fetch |
| 270 |
|
|
error:&error]; |
| 271 |
|
|
if(num != 0) return; |
| 272 |
|
|
|
| 273 |
|
|
id obj = [NSEntityDescription insertNewObjectForEntityForName:@"XspfList" |
| 274 |
|
|
inManagedObjectContext:moc]; |
| 275 |
|
|
NSPredicate *prediccate = [NSPredicate predicateWithFormat:@"urlString <> %@", @""]; |
| 276 |
|
|
[obj setValue:prediccate forKey:@"predicate"]; |
| 277 |
|
|
[obj setValue:NSLocalizedString(@"Library", @"Library") forKey:@"name"]; |
| 278 |
|
|
[obj setValue:[NSNumber numberWithInt:0] forKey:@"order"]; |
| 279 |
|
|
|
| 280 |
|
|
obj = [NSEntityDescription insertNewObjectForEntityForName:@"XspfList" |
| 281 |
|
|
inManagedObjectContext:moc]; |
| 282 |
|
|
prediccate = [NSPredicate predicateWithFormat:@"favorites = %@", [NSNumber numberWithBool:YES]]; |
| 283 |
|
|
[obj setValue:prediccate forKey:@"predicate"]; |
| 284 |
|
|
[obj setValue:NSLocalizedString(@"Favorites", @"Favorites") forKey:@"name"]; |
| 285 |
|
|
[obj setValue:[NSNumber numberWithInt:1] forKey:@"order"]; |
| 286 |
|
|
} |
| 287 |
|
|
- (void)tableViewSelectionDidChange:(NSNotification *)notification |
| 288 |
|
|
{ |
| 289 |
|
|
NSTableView *table = [notification object]; |
| 290 |
|
|
|
| 291 |
|
|
if([table numberOfSelectedRows] > 1) return; |
| 292 |
|
|
|
| 293 |
|
|
id obj = [listController valueForKeyPath:@"selection.xspfs"]; |
| 294 |
|
|
self.xspfList = obj; |
| 295 |
|
|
|
| 296 |
|
|
obj = [listController valueForKeyPath:@"selection.predicate"]; |
| 297 |
|
|
[self setListPredicate:obj]; |
| 298 |
|
|
} |
| 299 |
|
|
|
| 300 |
|
|
|
| 301 |
masaki |
6 |
#pragma mark#### load familynames #### |
| 302 |
|
|
- (NSArray *)arrayFromLFSeparatedFile:(NSString *)name |
| 303 |
|
|
{ |
| 304 |
|
|
NSString *path; |
| 305 |
|
|
|
| 306 |
masaki |
13 |
path = [[appDelegate applicationSupportFolder] stringByAppendingPathComponent:name]; |
| 307 |
|
|
path = [path stringByAppendingPathExtension:@"txt"]; |
| 308 |
|
|
|
| 309 |
|
|
NSFileManager *fm = [NSFileManager defaultManager]; |
| 310 |
|
|
BOOL isDir = NO; |
| 311 |
|
|
if(![fm fileExistsAtPath:path isDirectory:&isDir] || isDir) { |
| 312 |
masaki |
6 |
path = [[NSBundle mainBundle] pathForResource:name ofType:@"txt"]; |
| 313 |
masaki |
13 |
} |
| 314 |
masaki |
6 |
|
| 315 |
|
|
NSError *error = nil; |
| 316 |
|
|
NSString *content = [NSString stringWithContentsOfFile:path |
| 317 |
|
|
encoding:NSUTF8StringEncoding |
| 318 |
|
|
error:&error]; |
| 319 |
|
|
if(error) { |
| 320 |
|
|
NSLog(@"path => %@", path); |
| 321 |
|
|
NSLog(@"%@", [error localizedDescription]); |
| 322 |
|
|
return NO; |
| 323 |
|
|
} |
| 324 |
|
|
|
| 325 |
|
|
return [content componentsSeparatedByString:@"\x0a"]; |
| 326 |
|
|
} |
| 327 |
masaki |
5 |
|
| 328 |
masaki |
6 |
- (NSArray *)arrayFromTabSeparatedString:(NSString *)string |
| 329 |
|
|
{ |
| 330 |
|
|
return [string componentsSeparatedByString:@"\t"]; |
| 331 |
|
|
} |
| 332 |
|
|
- (BOOL)isEmptyEntityName:(NSString *)name |
| 333 |
|
|
{ |
| 334 |
|
|
NSManagedObjectContext *moc = [appDelegate managedObjectContext]; |
| 335 |
|
|
NSError *error = nil; |
| 336 |
|
|
NSFetchRequest *fetch; |
| 337 |
|
|
NSInteger num; |
| 338 |
|
|
|
| 339 |
|
|
fetch = [[NSFetchRequest alloc] init]; |
| 340 |
|
|
[fetch setEntity:[NSEntityDescription entityForName:name |
| 341 |
|
|
inManagedObjectContext:moc]]; |
| 342 |
|
|
num = [moc countForFetchRequest:fetch |
| 343 |
|
|
error:&error]; |
| 344 |
|
|
[fetch release]; |
| 345 |
|
|
fetch = nil; |
| 346 |
|
|
if(error) { |
| 347 |
|
|
NSLog(@"%@", [error localizedDescription]); |
| 348 |
|
|
return NO; |
| 349 |
|
|
} |
| 350 |
|
|
|
| 351 |
|
|
return num == 0; |
| 352 |
|
|
} |
| 353 |
|
|
- (void) buildFamilyNameFromFile |
| 354 |
|
|
{ |
| 355 |
|
|
NSManagedObjectContext *moc = [appDelegate managedObjectContext]; |
| 356 |
|
|
|
| 357 |
|
|
NSString *entityName; |
| 358 |
|
|
NSArray *contents; |
| 359 |
|
|
entityName = @"FamilyName"; |
| 360 |
masaki |
7 |
if([self isEmptyEntityName:entityName]) { |
| 361 |
masaki |
6 |
contents = [self arrayFromLFSeparatedFile:entityName]; |
| 362 |
|
|
|
| 363 |
masaki |
14 |
id key; |
| 364 |
|
|
for(key in contents) { |
| 365 |
|
|
NSArray *attr = [self arrayFromTabSeparatedString:key]; |
| 366 |
masaki |
6 |
if([attr count] < 2) continue; |
| 367 |
|
|
|
| 368 |
|
|
id obj = [NSEntityDescription insertNewObjectForEntityForName:entityName |
| 369 |
|
|
inManagedObjectContext:moc]; |
| 370 |
|
|
[obj setValue:[attr objectAtIndex:0] forKey:@"roman"]; |
| 371 |
|
|
[obj setValue:[attr objectAtIndex:1] forKey:@"japanese"]; |
| 372 |
|
|
|
| 373 |
|
|
if([attr count] > 2) { |
| 374 |
|
|
[obj setValue:[attr objectAtIndex:2] forKey:@"yomigana"]; |
| 375 |
|
|
} |
| 376 |
|
|
} |
| 377 |
|
|
} |
| 378 |
|
|
|
| 379 |
|
|
} |
| 380 |
|
|
|
| 381 |
masaki |
7 |
- (void)truncateFamilyName |
| 382 |
|
|
{ |
| 383 |
|
|
NSManagedObjectContext *moc = [appDelegate managedObjectContext]; |
| 384 |
|
|
NSFetchRequest *fetch = [[[NSFetchRequest alloc] init] autorelease]; |
| 385 |
|
|
NSEntityDescription *entry = [NSEntityDescription entityForName:@"FamilyName" |
| 386 |
|
|
inManagedObjectContext:moc]; |
| 387 |
|
|
[fetch setEntity:entry]; |
| 388 |
|
|
|
| 389 |
|
|
NSError *error = nil; |
| 390 |
|
|
NSArray *objects = [moc executeFetchRequest:fetch error:&error]; |
| 391 |
|
|
if(!objects) { |
| 392 |
|
|
if(error) { |
| 393 |
|
|
NSLog(@"fail fetch reason -> %@", error); |
| 394 |
|
|
} |
| 395 |
|
|
} |
| 396 |
|
|
|
| 397 |
|
|
[moc lock]; |
| 398 |
|
|
for(id obj in objects) { |
| 399 |
|
|
[moc deleteObject:obj]; |
| 400 |
|
|
} |
| 401 |
|
|
[moc unlock]; |
| 402 |
|
|
} |
| 403 |
masaki |
8 |
#pragma mark#### NSWidnow Delegate #### |
| 404 |
|
|
/** |
| 405 |
|
|
Returns the NSUndoManager for the application. In this case, the manager |
| 406 |
|
|
returned is that of the managed object context for the application. |
| 407 |
|
|
*/ |
| 408 |
masaki |
6 |
|
| 409 |
masaki |
8 |
- (NSUndoManager *)windowWillReturnUndoManager:(NSWindow *)window { |
| 410 |
|
|
return [[appDelegate managedObjectContext] undoManager]; |
| 411 |
|
|
} |
| 412 |
|
|
|
| 413 |
masaki |
9 |
#pragma mark#### NSOpenPanel Delegate #### |
| 414 |
|
|
- (BOOL)panel:(id)sender shouldShowFilename:(NSString *)filename |
| 415 |
|
|
{ |
| 416 |
|
|
NSManagedObjectContext *moc = [appDelegate managedObjectContext]; |
| 417 |
|
|
NSError *error = nil; |
| 418 |
|
|
NSFetchRequest *fetch; |
| 419 |
|
|
NSInteger num; |
| 420 |
|
|
NSURL *url = [NSURL fileURLWithPath:filename]; |
| 421 |
|
|
|
| 422 |
|
|
fetch = [[[NSFetchRequest alloc] init] autorelease]; |
| 423 |
|
|
[fetch setEntity:[NSEntityDescription entityForName:@"Xspf" inManagedObjectContext:moc]]; |
| 424 |
masaki |
16 |
NSPredicate *aPredicate = [NSPredicate predicateWithFormat:@"urlString LIKE %@", [url absoluteString]]; |
| 425 |
|
|
[fetch setPredicate:aPredicate]; |
| 426 |
masaki |
9 |
num = [moc countForFetchRequest:fetch error:&error]; |
| 427 |
|
|
if(error) { |
| 428 |
|
|
NSLog(@"%@", [error localizedDescription]); |
| 429 |
|
|
return NO; |
| 430 |
|
|
} |
| 431 |
|
|
|
| 432 |
|
|
return num == 0; |
| 433 |
|
|
} |
| 434 |
|
|
|
| 435 |
masaki |
5 |
#pragma mark#### NSTokenField Delegate #### |
| 436 |
masaki |
6 |
#if 1 |
| 437 |
masaki |
5 |
- (NSArray *)tokenField:(NSTokenField *)tokenField |
| 438 |
|
|
completionsForSubstring:(NSString *)substring |
| 439 |
|
|
indexOfToken:(NSInteger)tokenIndex |
| 440 |
|
|
indexOfSelectedItem:(NSInteger *)selectedIndex |
| 441 |
|
|
{ |
| 442 |
masaki |
6 |
NSManagedObjectContext *moc = [appDelegate managedObjectContext]; |
| 443 |
|
|
NSFetchRequest *fetch = [[[NSFetchRequest alloc] init] autorelease]; |
| 444 |
|
|
|
| 445 |
masaki |
16 |
NSPredicate *aPredicate = [NSPredicate predicateWithFormat: |
| 446 |
masaki |
6 |
@"roman BEGINSWITH[cd] %@" |
| 447 |
|
|
@"OR japanese BEGINSWITH[cd] %@" |
| 448 |
|
|
@"OR yomigana BEGINSWITH[cd] %@", substring,substring,substring]; |
| 449 |
|
|
NSEntityDescription *entry = [NSEntityDescription entityForName:@"FamilyName" |
| 450 |
|
|
inManagedObjectContext:moc]; |
| 451 |
|
|
|
| 452 |
|
|
[fetch setEntity:entry]; |
| 453 |
masaki |
16 |
[fetch setPredicate:aPredicate]; |
| 454 |
masaki |
6 |
|
| 455 |
|
|
NSError *error = nil; |
| 456 |
|
|
NSArray *objects = [moc executeFetchRequest:fetch error:&error]; |
| 457 |
|
|
if(!objects) { |
| 458 |
|
|
if(error) { |
| 459 |
|
|
NSLog(@"fail fetch reason -> %@", error); |
| 460 |
|
|
} |
| 461 |
|
|
} |
| 462 |
|
|
|
| 463 |
|
|
NSString *entryName = @""; |
| 464 |
|
|
switch([tokenField tag]) { |
| 465 |
|
|
case 2000: |
| 466 |
|
|
entryName = @"VoiceActor"; |
| 467 |
|
|
break; |
| 468 |
|
|
case 2001: |
| 469 |
|
|
entryName = @"Product"; |
| 470 |
|
|
break; |
| 471 |
|
|
} |
| 472 |
|
|
|
| 473 |
|
|
if([objects count] > 0) { |
| 474 |
|
|
NSMutableString *string = [NSMutableString string]; |
| 475 |
|
|
NSMutableArray *names = [NSMutableArray array]; |
| 476 |
|
|
for(id e in objects) { |
| 477 |
|
|
if([string length]) { |
| 478 |
|
|
[string appendString:@" OR "]; |
| 479 |
|
|
} |
| 480 |
|
|
[string appendFormat:@"name BEGINSWITH[cd] %%@ "]; |
| 481 |
|
|
[names addObject:[e valueForKey:@"japanese"]]; |
| 482 |
|
|
} |
| 483 |
masaki |
16 |
aPredicate = [NSPredicate predicateWithFormat:string argumentArray:names]; |
| 484 |
masaki |
6 |
} else { |
| 485 |
masaki |
16 |
aPredicate = [NSPredicate predicateWithFormat:@"name BEGINSWITH[cd] %@", substring]; |
| 486 |
masaki |
6 |
} |
| 487 |
|
|
entry = [NSEntityDescription entityForName:entryName inManagedObjectContext:moc]; |
| 488 |
|
|
[fetch setEntity:entry]; |
| 489 |
masaki |
16 |
[fetch setPredicate:aPredicate]; |
| 490 |
masaki |
6 |
|
| 491 |
|
|
error = nil; |
| 492 |
|
|
objects = [moc executeFetchRequest:fetch error:&error]; |
| 493 |
|
|
if(!objects) { |
| 494 |
|
|
if(error) { |
| 495 |
|
|
NSLog(@"fail fetch reason -> %@", error); |
| 496 |
|
|
} |
| 497 |
|
|
} |
| 498 |
|
|
|
| 499 |
|
|
NSMutableArray *result = [NSMutableArray arrayWithObject:substring]; |
| 500 |
|
|
for(id obj in objects) { |
| 501 |
|
|
[result addObject:[obj valueForKey:@"name"]]; |
| 502 |
|
|
} |
| 503 |
|
|
|
| 504 |
|
|
return result; |
| 505 |
|
|
} |
| 506 |
|
|
#else |
| 507 |
|
|
- (NSArray *)tokenField:(NSTokenField *)tokenField |
| 508 |
|
|
completionsForSubstring:(NSString *)substring |
| 509 |
|
|
indexOfToken:(NSInteger)tokenIndex |
| 510 |
|
|
indexOfSelectedItem:(NSInteger *)selectedIndex |
| 511 |
|
|
{ |
| 512 |
masaki |
5 |
NSLog(@"Enter %@", NSStringFromSelector(_cmd)); |
| 513 |
|
|
|
| 514 |
masaki |
6 |
NSString *entryName = @""; |
| 515 |
|
|
switch([tokenField tag]) { |
| 516 |
|
|
case 2000: |
| 517 |
|
|
entryName = @"VoiceActor"; |
| 518 |
|
|
break; |
| 519 |
|
|
case 2001: |
| 520 |
|
|
entryName = @"Product"; |
| 521 |
|
|
break; |
| 522 |
|
|
} |
| 523 |
|
|
|
| 524 |
masaki |
5 |
NSManagedObjectContext *moc = [appDelegate managedObjectContext]; |
| 525 |
|
|
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"name BEGINSWITH[cd] %@", substring]; |
| 526 |
masaki |
6 |
NSEntityDescription *entry = [NSEntityDescription entityForName:entryName |
| 527 |
masaki |
5 |
inManagedObjectContext:moc]; |
| 528 |
|
|
NSFetchRequest *fetch = [[[NSFetchRequest alloc] init] autorelease]; |
| 529 |
|
|
[fetch setEntity:entry]; |
| 530 |
|
|
[fetch setPredicate:predicate]; |
| 531 |
|
|
|
| 532 |
|
|
NSError *error = nil; |
| 533 |
|
|
NSArray *objects = [moc executeFetchRequest:fetch error:&error]; |
| 534 |
|
|
if(!objects) { |
| 535 |
|
|
if(error) { |
| 536 |
|
|
NSLog(@"fail fetch reason -> %@", error); |
| 537 |
|
|
} |
| 538 |
|
|
} |
| 539 |
|
|
|
| 540 |
|
|
NSMutableArray *result = [NSMutableArray array]; |
| 541 |
|
|
for(id obj in objects) { |
| 542 |
|
|
[result addObject:[obj valueForKey:@"name"]]; |
| 543 |
|
|
} |
| 544 |
|
|
|
| 545 |
|
|
return result; |
| 546 |
|
|
} |
| 547 |
masaki |
6 |
#endif |
| 548 |
masaki |
5 |
|
| 549 |
|
|
- (void)registerVoiceActor:(NSTokenField *)tokenField |
| 550 |
|
|
{ |
| 551 |
|
|
id array = [tokenField objectValue]; |
| 552 |
|
|
if(![array isKindOfClass:[NSArray class]]) return; |
| 553 |
|
|
|
| 554 |
masaki |
6 |
NSString *entryName = @""; |
| 555 |
|
|
switch([tokenField tag]) { |
| 556 |
|
|
case 2000: |
| 557 |
|
|
entryName = @"VoiceActor"; |
| 558 |
|
|
break; |
| 559 |
|
|
case 2001: |
| 560 |
|
|
entryName = @"Product"; |
| 561 |
|
|
break; |
| 562 |
|
|
} |
| 563 |
|
|
|
| 564 |
masaki |
5 |
NSManagedObjectContext *moc = [appDelegate managedObjectContext]; |
| 565 |
masaki |
6 |
NSEntityDescription *entry = [NSEntityDescription entityForName:entryName |
| 566 |
masaki |
5 |
inManagedObjectContext:moc]; |
| 567 |
|
|
NSFetchRequest *fetch = [[[NSFetchRequest alloc] init] autorelease]; |
| 568 |
|
|
[fetch setEntity:entry]; |
| 569 |
|
|
|
| 570 |
|
|
for(id token in array) { |
| 571 |
masaki |
16 |
NSPredicate *aPredicate = [NSPredicate predicateWithFormat:@"name LIKE[cd] %@", token]; |
| 572 |
|
|
[fetch setPredicate:aPredicate]; |
| 573 |
masaki |
5 |
|
| 574 |
|
|
NSError *error = nil; |
| 575 |
|
|
NSUInteger count = [moc countForFetchRequest:fetch error:&error]; |
| 576 |
|
|
if(error) { |
| 577 |
|
|
NSLog(@"fail fetch reason -> %@", error); |
| 578 |
|
|
continue; |
| 579 |
|
|
} |
| 580 |
|
|
if(count == 0) { |
| 581 |
masaki |
6 |
id obj = [NSEntityDescription insertNewObjectForEntityForName:entryName inManagedObjectContext:moc]; |
| 582 |
masaki |
5 |
[obj setValue:token forKey:@"name"]; |
| 583 |
|
|
} |
| 584 |
|
|
} |
| 585 |
|
|
} |
| 586 |
|
|
- (BOOL)control:(id)control textShouldEndEditing:(NSText *)fieldEditor |
| 587 |
|
|
{ |
| 588 |
masaki |
6 |
if([control tag] == 2000 || [control tag] == 2001) { |
| 589 |
masaki |
5 |
[self registerVoiceActor:control]; |
| 590 |
|
|
} |
| 591 |
|
|
|
| 592 |
|
|
return YES; |
| 593 |
|
|
} |
| 594 |
|
|
|
| 595 |
|
|
#pragma mark#### Test #### |
| 596 |
masaki |
2 |
- (IBAction)test01:(id)sender |
| 597 |
|
|
{ |
| 598 |
masaki |
7 |
[self truncateFamilyName]; |
| 599 |
masaki |
6 |
[self buildFamilyNameFromFile]; |
| 600 |
masaki |
2 |
} |
| 601 |
|
|
- (IBAction)test02:(id)sender |
| 602 |
|
|
{ |
| 603 |
masaki |
10 |
NSResponder *responder = [[self window] firstResponder]; |
| 604 |
|
|
while(responder) { |
| 605 |
|
|
NSLog(@"Responder -> %@", responder); |
| 606 |
|
|
responder = [responder nextResponder]; |
| 607 |
|
|
} |
| 608 |
masaki |
2 |
} |
| 609 |
|
|
- (IBAction)test03:(id)sender |
| 610 |
|
|
{ |
| 611 |
|
|
NSFetchRequest *fetch = [[[NSFetchRequest alloc] init] autorelease]; |
| 612 |
|
|
|
| 613 |
|
|
id moc = [appDelegate managedObjectContext]; |
| 614 |
|
|
|
| 615 |
|
|
[fetch setEntity:[NSEntityDescription entityForName:@"Xspf" inManagedObjectContext:moc]]; |
| 616 |
|
|
|
| 617 |
|
|
id objs = [moc executeFetchRequest:fetch error:NULL]; |
| 618 |
|
|
NSLog(@"Fetched -> %@", objs); |
| 619 |
|
|
} |
| 620 |
|
|
@end |