forked from iMediaSandboxing/iMedia
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathIMBAppleMediaLibraryParserConfiguration.m
182 lines (155 loc) · 6.5 KB
/
IMBAppleMediaLibraryParserConfiguration.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
//
// IMBAppleMediaLibraryParserConfiguration.m
// iMedia
//
// Created by Jörg Jacobsen on 06.03.15.
//
//
#import "IMBAppleMediaLibraryParserConfiguration.h"
#import "IMBAppleMediaLibraryPropertySynchronizer.h"
#import "NSImage+iMedia.h"
#import "MLMediaGroup+iMedia.h"
#import "IMBAppleMediaLibraryParserMessenger.h"
@implementation IMBAppleMediaLibraryParserConfiguration
/**
Designated initiliazer.
*/
- (instancetype)initWithMediaSourceIdentifier:(NSString *)mediaSourceIdentifier
AppleMediaLibraryMediaType:(MLMediaType)mediaType
identifiersOfNonUserCreatedGroups:(NSSet *)identifiersOfNonUserCreatedGroups
{
if ((self = [super init])) {
_mediaSourceIdentifier = mediaSourceIdentifier;
_mediaType = mediaType;
_identifiersOfNonUserCreatedGroups = identifiersOfNonUserCreatedGroups;
}
return self;
}
@synthesize mediaSourceIdentifier = _mediaSourceIdentifier;
@synthesize mediaSource = _mediaSource;
@synthesize mediaType = _mediaType;
@synthesize identifiersOfNonUserCreatedGroups = _identifiersOfNonUserCreatedGroups;
/**
Returns the mediaSourceIdentifier of the receiver as the bundle identifier of the library's source app.
*/
- (NSString *)sourceAppBundleIdentifier
{
return [self mediaSourceIdentifier];
}
/**
Returns whether group (aka node) is populated with child group objects rather than real media objects.
@discussion This default implementation returns NO.
*/
- (BOOL)shouldUseChildGroupsAsMediaObjectsForMediaGroup:(MLMediaGroup *)mediaGroup
{
return NO;
}
/**
@return Type of media group provided unified across all possible media sources.
@discussion Default return value is kIMBMLMediaGroupTypeAlbum.
*/
- (IMBMLMediaGroupType *)typeForMediaGroup:(MLMediaGroup *)mediaGroup
{
return kIMBMLMediaGroupTypeAlbum;
}
/**
*/
- (NSImage *)thumbnailForMediaObject:(MLMediaObject *)mediaObject
{
NSURL *url = mediaObject.thumbnailURL;
[url startAccessingSecurityScopedResource];
NSImage *thumbnail = [[NSImage alloc] initWithContentsOfURL:url];
[url stopAccessingSecurityScopedResource];
return thumbnail;
}
/**
*/
- (NSDictionary *)metadataForMediaObject:(MLMediaObject *)mediaObject
{
// Map metadata information from media library representation to iMedia representation
NSMutableDictionary* externalMetadata = [NSMutableDictionary dictionaryWithDictionary:mediaObject.attributes];
if (mediaObject.URL) {
[mediaObject.URL startAccessingSecurityScopedResource];
[externalMetadata addEntriesFromDictionary:[NSImage imb_metadataFromImageAtURL:mediaObject.URL checkSpotlightComments:NO]];
[mediaObject.URL stopAccessingSecurityScopedResource];
}
return [NSDictionary dictionaryWithDictionary:externalMetadata];
}
/**
*/
- (NSString *)countFormatForGroup: (MLMediaGroup *)mediaGroup plural:(BOOL)plural
{
NSString *localizationKey = nil;
// Must deal with 3 dimensions:
// - media type: image, film, songs
// - object type: non-leaf / leaf node
// - cardinality: singular / plural
static NSString *nonLeaf = @"NonLeaf";
static NSString *leaf = @"Leaf";
static NSDictionary *countFormatMap;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
countFormatMap = @{
[NSNumber numberWithInteger:MLMediaTypeImage] :
@{
nonLeaf : @"IMBSkimmableObjectViewController.countFormat",
leaf : @"IMBImageViewController.countFormat"
},
[NSNumber numberWithInteger:MLMediaTypeMovie] :
@{
nonLeaf : @"IMBSkimmableObjectViewController.countFormat",
leaf : @"IMBMovieViewController.countFormat"
},
[NSNumber numberWithInteger:MLMediaTypeAudio] :
@{
nonLeaf : @"IMBSkimmableObjectViewController.countFormat",
leaf : @"IMBAudioViewController.countFormat"
}
};
});
NSNumber *mediaType = [NSNumber numberWithInteger:self.mediaType];
NSString *objectType = [self shouldUseChildGroupsAsMediaObjectsForMediaGroup:mediaGroup] ? nonLeaf : leaf;
NSString *cardinality = plural ? @"Plural" : @"Singular";
localizationKey = countFormatMap[mediaType][objectType];
if (!localizationKey) {
localizationKey = @"IMBSkimmableObjectViewController.countFormat"; // n objects
}
localizationKey = [localizationKey stringByAppendingString:cardinality];
return NSLocalizedStringWithDefaultValue(localizationKey,
nil, IMBBundle(), nil,
@"Format string for object count");
}
/**
*/
- (NSDictionary *)metadataForMediaGroup:(MLMediaGroup *)mediaGroup
{
// Map metadata information from media library representation to iMedia representation
NSMutableDictionary* metadata = [NSMutableDictionary dictionary];
NSInteger objectCount = NSNotFound;
if ([self shouldUseChildGroupsAsMediaObjectsForMediaGroup:mediaGroup]) {
objectCount = [[mediaGroup childGroups] count];
} else {
// Objects displayed in group are leafs (images and the like)
// At this point we cannot decide on how many objects we are dealing with
// since this would require actually populating the media group (node)
// where media type filters would be applied. This is too expensive.
// objectCount = [[mediaGroup imb_mediaObjectCount] integerValue];
}
if (objectCount != NSNotFound) {
NSString *objectCountFormat = [self countFormatForGroup:mediaGroup plural:objectCount != 1];
if (objectCountFormat) {
metadata[kIMBMetadataObjectCountDescriptionKey] = [NSString stringWithFormat:objectCountFormat, objectCount];
}
}
return [NSDictionary dictionaryWithDictionary:metadata];
}
/**
@return Last object in media object list of the receiver.
*/
- (MLMediaObject *)keyMediaObjectForMediaGroup:(MLMediaGroup *)mediaGroup
{
NSArray *mediaObjects = [IMBAppleMediaLibraryPropertySynchronizer mediaObjectsForMediaGroup:mediaGroup];
MLMediaObject *mediaObject = [mediaObjects lastObject];
return mediaObject;
}
@end