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-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-10-30 23:48:59 +00:00
|
|
|
__block PlaylistEntry *entry = nil;
|
|
|
|
[kPersistentContainer.viewContext performBlockAndWait:^{
|
|
|
|
entry = [NSEntityDescription insertNewObjectForEntityForName:@"PlaylistEntry" inManagedObjectContext:kPersistentContainer.viewContext];
|
|
|
|
|
|
|
|
entry.deLeted = YES;
|
|
|
|
|
|
|
|
NSMutableDictionary *dict = [[NSMutableDictionary alloc] init];
|
|
|
|
|
|
|
|
// loop through the keys we want to extract
|
|
|
|
for(NSString *mdKey in importKeys) {
|
|
|
|
if(![metadataItem valueForAttribute:mdKey]) continue;
|
|
|
|
id importTarget = [importKeys objectForKey:mdKey];
|
|
|
|
// Just copy the object from metadata
|
|
|
|
if([importTarget isKindOfClass:[NSString class]]) {
|
|
|
|
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];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// 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]];
|
|
|
|
[dict setValue:transformedValue forKey:importKey];
|
|
|
|
}
|
|
|
|
// 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
|
|
|
}
|
2022-02-07 05:49:27 +00:00
|
|
|
}
|
2022-07-15 10:02:41 +00:00
|
|
|
|
2022-10-30 23:48:59 +00:00
|
|
|
NSURL *url = [dict objectForKey:@"url"];
|
|
|
|
[dict removeObjectForKey:@"url"];
|
2022-07-15 10:02:41 +00:00
|
|
|
|
2022-10-30 23:48:59 +00:00
|
|
|
entry.url = url;
|
2022-07-15 10:02:41 +00:00
|
|
|
|
2022-10-30 23:48:59 +00:00
|
|
|
[entry setMetadata:dict];
|
|
|
|
}];
|
2022-07-15 10:02:41 +00:00
|
|
|
|
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
|