| 1 |
masaki |
41 |
// |
| 2 |
|
|
// XspfMLibraryViewController.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 |
41 |
#import "XspfMLibraryViewController.h" |
| 63 |
|
|
|
| 64 |
masaki |
113 |
#import "XspfMXspfListObject.h" |
| 65 |
|
|
|
| 66 |
masakih |
366 |
#import "XspfMRuleEditorDelegate.h" |
| 67 |
masaki |
113 |
|
| 68 |
masakih |
366 |
|
| 69 |
masaki |
43 |
@interface XspfMLibraryViewController (HMPrivate) |
| 70 |
|
|
- (NSArray *)sortDescriptors; |
| 71 |
|
|
- (void)setupXspfList; |
| 72 |
masakih |
366 |
- (void)setupRules; |
| 73 |
masaki |
43 |
@end |
| 74 |
masaki |
41 |
|
| 75 |
masaki |
113 |
enum { |
| 76 |
|
|
kLibraryOrder = 0, |
| 77 |
|
|
kFavoritesOrder, |
| 78 |
|
|
kSmartLibraryOrder, |
| 79 |
|
|
}; |
| 80 |
|
|
|
| 81 |
masaki |
41 |
@implementation XspfMLibraryViewController |
| 82 |
masaki |
113 |
|
| 83 |
masaki |
41 |
- (id)init |
| 84 |
|
|
{ |
| 85 |
|
|
[super initWithNibName:@"LibraryView" bundle:nil]; |
| 86 |
|
|
|
| 87 |
masaki |
43 |
[self setupXspfList]; |
| 88 |
masakih |
366 |
[self setupRules]; |
| 89 |
masaki |
43 |
|
| 90 |
masaki |
41 |
return self; |
| 91 |
|
|
} |
| 92 |
|
|
|
| 93 |
masaki |
43 |
- (void)awakeFromNib |
| 94 |
|
|
{ |
| 95 |
|
|
[[self representedObject] setSortDescriptors:[self sortDescriptors]]; |
| 96 |
|
|
} |
| 97 |
masaki |
41 |
- (NSArray *)sortDescriptors |
| 98 |
|
|
{ |
| 99 |
masaki |
61 |
id desc = [[NSSortDescriptor alloc] initWithKey:@"order" ascending:YES]; |
| 100 |
|
|
return [NSArray arrayWithObject:[desc autorelease]]; |
| 101 |
masaki |
41 |
} |
| 102 |
masaki |
233 |
- (XspfMDragControl *)dragControl |
| 103 |
|
|
{ |
| 104 |
|
|
return dragControl; |
| 105 |
|
|
} |
| 106 |
masaki |
43 |
|
| 107 |
masaki |
113 |
- (void)addSmartLibrary:(NSString *)name predicate:(NSPredicate *)predicate order:(NSInteger)order |
| 108 |
|
|
{ |
| 109 |
|
|
id obj = [NSEntityDescription insertNewObjectForEntityForName:@"XspfList" |
| 110 |
|
|
inManagedObjectContext:[self managedObjectContext]]; |
| 111 |
|
|
[obj setValue:predicate forKey:@"predicate"]; |
| 112 |
|
|
[obj setValue:name forKey:@"name"]; |
| 113 |
|
|
[obj setValue:[NSNumber numberWithInt:order] forKey:@"order"]; |
| 114 |
|
|
} |
| 115 |
masaki |
43 |
- (void)setupXspfList |
| 116 |
|
|
{ |
| 117 |
masaki |
48 |
NSManagedObjectContext *moc = [self managedObjectContext]; |
| 118 |
masaki |
43 |
NSError *error = nil; |
| 119 |
|
|
NSFetchRequest *fetch; |
| 120 |
|
|
NSInteger num; |
| 121 |
|
|
|
| 122 |
|
|
fetch = [[[NSFetchRequest alloc] init] autorelease]; |
| 123 |
|
|
[fetch setEntity:[NSEntityDescription entityForName:@"XspfList" |
| 124 |
|
|
inManagedObjectContext:moc]]; |
| 125 |
|
|
num = [moc countForFetchRequest:fetch |
| 126 |
|
|
error:&error]; |
| 127 |
|
|
if(num != 0) return; |
| 128 |
|
|
|
| 129 |
|
|
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"urlString <> %@", @""]; |
| 130 |
masaki |
268 |
[self addSmartLibrary:@"Library" |
| 131 |
masaki |
113 |
predicate:predicate |
| 132 |
|
|
order:kLibraryOrder]; |
| 133 |
masaki |
43 |
|
| 134 |
|
|
predicate = [NSPredicate predicateWithFormat:@"favorites = %@", [NSNumber numberWithBool:YES]]; |
| 135 |
masaki |
268 |
[self addSmartLibrary:@"Favorites" |
| 136 |
masaki |
113 |
predicate:predicate |
| 137 |
|
|
order:kFavoritesOrder]; |
| 138 |
masaki |
43 |
} |
| 139 |
|
|
|
| 140 |
masakih |
366 |
- (void)setupRules |
| 141 |
|
|
{ |
| 142 |
|
|
[XspfMRuleEditorDelegate registerStringTypeKeyPaths:[NSArray arrayWithObjects:@"title", @"information.voiceActorsList", @"information.productsList", nil]]; |
| 143 |
|
|
[XspfMRuleEditorDelegate registerDateTypeKeyPaths:[NSArray arrayWithObjects:@"lastPlayDate", @"modificationDate", @"creationDate", nil]]; |
| 144 |
|
|
[XspfMRuleEditorDelegate setUseRating:YES]; |
| 145 |
|
|
[XspfMRuleEditorDelegate setUseLablel:YES]; |
| 146 |
|
|
} |
| 147 |
|
|
|
| 148 |
masaki |
235 |
- (BOOL)mouseInTableView |
| 149 |
|
|
{ |
| 150 |
|
|
NSEvent *event = [[[self view] window] currentEvent]; |
| 151 |
|
|
NSPoint mouse = [[tableView superview] convertPoint:[event locationInWindow] fromView:nil]; |
| 152 |
|
|
|
| 153 |
|
|
return NSPointInRect(mouse, [tableView visibleRect]); |
| 154 |
|
|
} |
| 155 |
masaki |
113 |
- (XspfMXspfListObject *)targetObject |
| 156 |
|
|
{ |
| 157 |
|
|
id array = [[self representedObject] arrangedObjects]; |
| 158 |
|
|
|
| 159 |
|
|
NSInteger row = [tableView clickedRow]; |
| 160 |
|
|
if(row >= 0 && [array count] > row) { |
| 161 |
|
|
return [array objectAtIndex:row]; |
| 162 |
|
|
} |
| 163 |
masaki |
235 |
|
| 164 |
|
|
if(![self mouseInTableView]) { |
| 165 |
|
|
NSArray *selection = [[self representedObject] selectedObjects]; |
| 166 |
|
|
if([selection count] != 0) { |
| 167 |
|
|
return [selection objectAtIndex:0]; |
| 168 |
|
|
} |
| 169 |
|
|
} |
| 170 |
masaki |
113 |
return nil; |
| 171 |
|
|
} |
| 172 |
masaki |
235 |
|
| 173 |
|
|
|
| 174 |
masaki |
116 |
- (BOOL)canUseNewSmartLibraryName:(NSString *)newName |
| 175 |
|
|
{ |
| 176 |
|
|
NSManagedObjectContext *moc = [self managedObjectContext]; |
| 177 |
|
|
NSError *error = nil; |
| 178 |
|
|
NSFetchRequest *fetch; |
| 179 |
|
|
NSPredicate *predicate; |
| 180 |
|
|
NSInteger num; |
| 181 |
|
|
|
| 182 |
|
|
fetch = [[[NSFetchRequest alloc] init] autorelease]; |
| 183 |
|
|
[fetch setEntity:[NSEntityDescription entityForName:@"XspfList" |
| 184 |
|
|
inManagedObjectContext:moc]]; |
| 185 |
|
|
predicate = [NSPredicate predicateWithFormat:@"name = %@", newName]; |
| 186 |
|
|
[fetch setPredicate:predicate]; |
| 187 |
|
|
num = [moc countForFetchRequest:fetch |
| 188 |
|
|
error:&error]; |
| 189 |
|
|
|
| 190 |
|
|
return num == 0; |
| 191 |
|
|
} |
| 192 |
|
|
- (NSString *)newSmartLibraryName |
| 193 |
|
|
{ |
| 194 |
|
|
NSString *template = NSLocalizedString(@"Untitled Library", @"Untitled Library"); |
| 195 |
|
|
|
| 196 |
|
|
if([self canUseNewSmartLibraryName:template]) return template; |
| 197 |
|
|
|
| 198 |
|
|
NSInteger i = 1; |
| 199 |
|
|
do { |
| 200 |
|
|
NSString *name = [NSString stringWithFormat:@"%@ %d", template, i]; |
| 201 |
|
|
if([self canUseNewSmartLibraryName:name]) return name; |
| 202 |
|
|
} while (i++ < INT_MAX); |
| 203 |
|
|
|
| 204 |
|
|
return @"hoge"; |
| 205 |
|
|
} |
| 206 |
masaki |
112 |
- (IBAction)newPredicate:(id)sender |
| 207 |
masaki |
43 |
{ |
| 208 |
masaki |
112 |
if([editor numberOfRows] == 0) { |
| 209 |
|
|
[editor addRow:self]; |
| 210 |
|
|
} |
| 211 |
masaki |
147 |
|
| 212 |
masaki |
116 |
[nameField setStringValue:[self newSmartLibraryName]]; |
| 213 |
masaki |
113 |
[nameField selectText:self]; |
| 214 |
|
|
|
| 215 |
masaki |
112 |
[NSApp beginSheet:predicatePanel |
| 216 |
|
|
modalForWindow:[tableView window] |
| 217 |
|
|
modalDelegate:self |
| 218 |
|
|
didEndSelector:@selector(didEndEditPredicate:returnCode:contextInfo:) |
| 219 |
|
|
contextInfo:@"Createion"]; |
| 220 |
masaki |
43 |
} |
| 221 |
masaki |
113 |
- (IBAction)editPredicate:(id)sender |
| 222 |
|
|
{ |
| 223 |
masaki |
235 |
XspfMXspfListObject *obj = [sender representedObject]; |
| 224 |
|
|
if(!obj) { |
| 225 |
|
|
HMLog(HMLogLevelError, @"-[%@ %@] paramater's representedObject is nil.", |
| 226 |
|
|
NSStringFromClass([self class]), NSStringFromSelector(_cmd)); |
| 227 |
|
|
return; |
| 228 |
|
|
} |
| 229 |
masaki |
113 |
[nameField setStringValue:obj.name]; |
| 230 |
|
|
[nameField selectText:self]; |
| 231 |
|
|
|
| 232 |
masaki |
123 |
[ruleEditorDelegate setPredicate:obj.predicate]; |
| 233 |
masaki |
113 |
|
| 234 |
|
|
[NSApp beginSheet:predicatePanel |
| 235 |
|
|
modalForWindow:[tableView window] |
| 236 |
|
|
modalDelegate:self |
| 237 |
|
|
didEndSelector:@selector(didEndEditPredicate:returnCode:contextInfo:) |
| 238 |
|
|
contextInfo:obj]; |
| 239 |
|
|
} |
| 240 |
|
|
- (IBAction)deletePredicate:(id)sender |
| 241 |
|
|
{ |
| 242 |
masaki |
235 |
XspfMXspfListObject *obj = [sender representedObject]; |
| 243 |
|
|
if(!obj) { |
| 244 |
|
|
HMLog(HMLogLevelError, @"-[%@ %@] paramater's representedObject is nil.", |
| 245 |
|
|
NSStringFromClass([self class]), NSStringFromSelector(_cmd)); |
| 246 |
|
|
return; |
| 247 |
|
|
} |
| 248 |
masaki |
113 |
NSBeginInformationalAlertSheet(nil, nil, @"Cancel", nil, [[self view] window], |
| 249 |
|
|
self, @selector(didEndAskDelete:::), Nil, obj, |
| 250 |
masaki |
118 |
NSLocalizedString(@"Do you really delete smart library \"%@\"?", @"Do you really delete smart library \"%@\"?"), |
| 251 |
|
|
obj.name); |
| 252 |
masaki |
113 |
} |
| 253 |
masaki |
112 |
- (IBAction)didEndEditPredicate:(id)sender |
| 254 |
|
|
{ |
| 255 |
|
|
[predicatePanel orderOut:self]; |
| 256 |
|
|
[NSApp endSheet:predicatePanel returnCode:[sender tag]]; |
| 257 |
|
|
} |
| 258 |
masaki |
118 |
|
| 259 |
|
|
- (BOOL)validateMenuItem:(NSMenuItem *)menuItem |
| 260 |
|
|
{ |
| 261 |
|
|
SEL action = [menuItem action]; |
| 262 |
|
|
if(action == @selector(editPredicate:) |
| 263 |
|
|
|| action == @selector(deletePredicate:)) { |
| 264 |
|
|
XspfMXspfListObject *obj = [self targetObject]; |
| 265 |
|
|
if(!obj) return NO; |
| 266 |
|
|
if(obj.order == kLibraryOrder || obj.order == kFavoritesOrder) return NO; |
| 267 |
masaki |
235 |
[menuItem setRepresentedObject:obj]; |
| 268 |
masaki |
118 |
} |
| 269 |
|
|
|
| 270 |
|
|
return YES; |
| 271 |
|
|
} |
| 272 |
masakih |
362 |
|
| 273 |
masaki |
112 |
- (void)didEndEditPredicate:(id)sheet returnCode:(NSInteger)returnCode contextInfo:(void *)contextInfo |
| 274 |
|
|
{ |
| 275 |
|
|
if(returnCode == NSCancelButton) return; |
| 276 |
|
|
|
| 277 |
masaki |
147 |
[editor reloadPredicate]; |
| 278 |
|
|
NSPredicate *predicate = [editor predicate]; |
| 279 |
masaki |
122 |
|
| 280 |
masaki |
125 |
if(!predicate || ![predicate isKindOfClass:[NSPredicate class]]) { |
| 281 |
masaki |
188 |
HMLog(HMLogLevelError, @"Could not create NSPredicate."); |
| 282 |
masaki |
125 |
NSBeep(); |
| 283 |
|
|
return; |
| 284 |
|
|
} |
| 285 |
masaki |
126 |
if(![predicate isKindOfClass:[NSCompoundPredicate class]]) { |
| 286 |
|
|
predicate = [NSCompoundPredicate andPredicateWithSubpredicates:[NSArray arrayWithObject:predicate]]; |
| 287 |
|
|
} |
| 288 |
masaki |
150 |
|
| 289 |
masaki |
113 |
NSString *name = [nameField stringValue]; |
| 290 |
|
|
if([name length] == 0) { |
| 291 |
|
|
NSBeep(); |
| 292 |
|
|
NSBeginAlertSheet(nil, nil, nil, nil, [[self view] window], |
| 293 |
|
|
self, @selector(retryEditPredicate:::), Nil, contextInfo, |
| 294 |
masaki |
118 |
NSLocalizedString(@"Name must not be empty.", @"Name must not be empty.")); |
| 295 |
masaki |
116 |
return; |
| 296 |
masaki |
113 |
} |
| 297 |
masaki |
112 |
|
| 298 |
masaki |
113 |
if([(id)contextInfo isKindOfClass:[NSString class]]) { |
| 299 |
|
|
[self addSmartLibrary:name predicate:predicate order:kSmartLibraryOrder]; |
| 300 |
|
|
} else { |
| 301 |
|
|
XspfMXspfListObject *obj = contextInfo; |
| 302 |
|
|
obj.name = name; |
| 303 |
|
|
obj.predicate = predicate; |
| 304 |
|
|
} |
| 305 |
masaki |
112 |
} |
| 306 |
masaki |
113 |
- (void)retryEditPredicate:(NSWindow *)sheet :(NSInteger)returnCode :(void *)contextInfo |
| 307 |
|
|
{ |
| 308 |
|
|
if([(id)contextInfo isKindOfClass:[NSString class]]) { |
| 309 |
|
|
[self performSelector:@selector(newPredicate:) withObject:nil afterDelay:0.0]; |
| 310 |
|
|
} else { |
| 311 |
|
|
[self performSelector:@selector(editPredicate:) withObject:nil afterDelay:0.0]; |
| 312 |
|
|
} |
| 313 |
|
|
} |
| 314 |
|
|
- (void)didEndAskDelete:(NSWindow *)sheet :(NSInteger)returnCode :(void *)contextInfo |
| 315 |
|
|
{ |
| 316 |
|
|
if(returnCode == NSCancelButton) return; |
| 317 |
|
|
|
| 318 |
|
|
[[self managedObjectContext] deleteObject:contextInfo]; |
| 319 |
|
|
} |
| 320 |
masaki |
120 |
|
| 321 |
masaki |
112 |
- (IBAction)test01:(id)sender |
| 322 |
|
|
{ |
| 323 |
masaki |
147 |
// NSArray *array = [editor rowTemplates]; |
| 324 |
masaki |
112 |
|
| 325 |
|
|
// for(id templ in array) { |
| 326 |
masaki |
188 |
// HMLog(HMLogLevelDebug @"Views -> %@", [templ templateViews]); |
| 327 |
masaki |
112 |
// for(id v in [templ templateViews]) { |
| 328 |
|
|
// if([v respondsToSelector:@selector(tag)]) { |
| 329 |
masaki |
188 |
// HMLog(HMLogLevelDebug, @"tag -> %d", [v tag]); |
| 330 |
masaki |
112 |
// } |
| 331 |
|
|
// } |
| 332 |
|
|
// } |
| 333 |
masaki |
147 |
// for(id templ in array) { |
| 334 |
masaki |
188 |
// HMLog(HMLogLevelDebug, @"template -> %@", templ); |
| 335 |
masaki |
147 |
// } |
| 336 |
masaki |
112 |
} |
| 337 |
masaki |
43 |
|
| 338 |
masaki |
41 |
@end |