2008-02-11 14:10:25 +00:00
|
|
|
//
|
|
|
|
// SpotlightPlaylistEntry.m
|
|
|
|
// Cog
|
|
|
|
//
|
|
|
|
// Created by Matthew Grinshpun on 11/02/08.
|
2008-02-14 14:07:10 +00:00
|
|
|
// Copyright 2008 Matthew Leon Grinshpun. All rights reserved.
|
2008-02-11 14:10:25 +00:00
|
|
|
//
|
|
|
|
|
|
|
|
#import "SpotlightPlaylistEntry.h"
|
|
|
|
|
2008-02-18 12:59:20 +00:00
|
|
|
// dictionary that lets us translate from and mdKey to an entryKey
|
|
|
|
// if we need the help of a transformer, we use an nsarray
|
|
|
|
// with format (entryKey, transformerName)
|
|
|
|
static NSDictionary *importKeys;
|
2008-02-11 14:10:25 +00:00
|
|
|
|
2022-07-15 14:00:11 +00:00
|
|
|
extern NSLock *kPersistentContainerLock;
|
2022-06-21 07:01:07 +00:00
|
|
|
extern NSPersistentContainer *kPersistentContainer;
|
2022-06-16 14:14:33 +00:00
|
|
|
|
2008-02-11 14:10:25 +00:00
|
|
|
@implementation SpotlightPlaylistEntry
|
|
|
|
|
2022-02-07 05:49:27 +00:00
|
|
|
+ (void)initialize {
|
|
|
|
// We need to translate the path string to a full URL
|
|
|
|
NSArray *URLTransform =
|
2022-06-16 14:14:33 +00:00
|
|
|
@[@"url", @"PathToURLTransformer"];
|
2022-02-07 05:49:27 +00:00
|
|
|
|
|
|
|
// Extract the artist name from the authors array
|
|
|
|
NSArray *artistTransform =
|
|
|
|
@[@"artist", @"AuthorToArtistTransformer"];
|
|
|
|
|
2022-06-16 14:14:33 +00:00
|
|
|
importKeys = @{ @"kMDItemTitle": @"title",
|
|
|
|
@"kMDItemAlbum": @"album",
|
2022-07-15 10:02:41 +00:00
|
|
|
@"kMDItemAudioTrackNumber": @"track",
|
2022-06-16 14:14:33 +00:00
|
|
|
@"kMDItemRecordingYear": @"year",
|
|
|
|
@"kMDItemMusicalGenre": @"genre",
|
2022-07-15 10:02:41 +00:00
|
|
|
@"kMDItemDurationSeconds": @"length",
|
2022-06-16 14:14:33 +00:00
|
|
|
@"kMDItemPath": URLTransform,
|
|
|
|
@"kMDItemAuthors": artistTransform };
|
2008-02-12 10:30:32 +00:00
|
|
|
}
|
|
|
|
|
2022-06-16 14:14:33 +00:00
|
|
|
+ (PlaylistEntry *)playlistEntryWithMetadataItem:(NSMetadataItem *)metadataItem {
|
2022-07-15 14:00:11 +00:00
|
|
|
[kPersistentContainerLock lock];
|
2022-06-21 07:01:07 +00:00
|
|
|
PlaylistEntry *entry = [NSEntityDescription insertNewObjectForEntityForName:@"PlaylistEntry" inManagedObjectContext:kPersistentContainer.viewContext];
|
2022-07-15 14:00:11 +00:00
|
|
|
[kPersistentContainerLock unlock];
|
2022-02-07 05:49:27 +00:00
|
|
|
|
2022-06-17 00:37:11 +00:00
|
|
|
entry.deLeted = YES;
|
|
|
|
|
2022-07-15 10:02:41 +00:00
|
|
|
NSMutableDictionary *dict = [[NSMutableDictionary alloc] init];
|
|
|
|
|
2022-02-07 05:49:27 +00:00
|
|
|
// loop through the keys we want to extract
|
|
|
|
for(NSString *mdKey in importKeys) {
|
2022-07-15 10:02:41 +00:00
|
|
|
if(![metadataItem valueForAttribute:mdKey]) continue;
|
2022-02-07 05:49:27 +00:00
|
|
|
id importTarget = [importKeys objectForKey:mdKey];
|
|
|
|
// Just copy the object from metadata
|
|
|
|
if([importTarget isKindOfClass:[NSString class]]) {
|
2022-07-15 10:02:41 +00:00
|
|
|
if([importTarget isEqualToString:@"length"]) {
|
|
|
|
// fake it
|
|
|
|
NSNumber *number = [metadataItem valueForAttribute:mdKey];
|
|
|
|
[dict setValue:@(44100.0) forKey:@"samplerate"];
|
|
|
|
[dict setValue:@(44100.0 * [number doubleValue]) forKey:@"totalFrames"];
|
|
|
|
} else {
|
|
|
|
[dict setValue:[metadataItem valueForAttribute:mdKey]
|
|
|
|
forKey:importTarget];
|
|
|
|
}
|
2022-02-07 05:49:27 +00:00
|
|
|
}
|
|
|
|
// Transform the value in metadata before copying it in
|
|
|
|
else if([importTarget isKindOfClass:[NSArray class]]) {
|
|
|
|
NSString *importKey = [importTarget objectAtIndex:0];
|
|
|
|
NSValueTransformer *transformer =
|
|
|
|
[NSValueTransformer valueTransformerForName:[importTarget objectAtIndex:1]];
|
|
|
|
id transformedValue = [transformer transformedValue:
|
|
|
|
[metadataItem valueForAttribute:mdKey]];
|
2022-07-15 10:02:41 +00:00
|
|
|
[dict setValue:transformedValue forKey:importKey];
|
2022-02-07 05:49:27 +00:00
|
|
|
}
|
|
|
|
// The importKeys dictionary contains something strange...
|
|
|
|
else {
|
|
|
|
NSString *errString =
|
|
|
|
[NSString stringWithFormat:@"ERROR: Could not import key %@", mdKey];
|
|
|
|
NSAssert(NO, errString);
|
|
|
|
}
|
|
|
|
}
|
2022-07-15 10:02:41 +00:00
|
|
|
|
|
|
|
NSURL *url = [dict objectForKey:@"url"];
|
|
|
|
[dict removeObjectForKey:@"url"];
|
|
|
|
|
|
|
|
entry.url = url;
|
|
|
|
|
|
|
|
[entry setMetadata:dict];
|
|
|
|
|
2022-02-07 05:49:27 +00:00
|
|
|
return entry;
|
2008-02-11 14:10:25 +00:00
|
|
|
}
|
|
|
|
|
2022-01-19 02:12:57 +00:00
|
|
|
@end
|