forked from karelia/iMedia
-
Notifications
You must be signed in to change notification settings - Fork 2
/
IMBApertureParser.m
886 lines (643 loc) · 29.5 KB
/
IMBApertureParser.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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
/*
iMedia Browser Framework <http://karelia.com/imedia/>
Copyright (c) 2005-2011 by Karelia Software et al.
iMedia Browser is based on code originally developed by Jason Terhorst,
further developed for Sandvox by Greg Hulands, Dan Wood, and Terrence Talbot.
The new architecture for version 2.0 was developed by Peter Baumgartner.
Contributions have also been made by Matt Gough, Martin Wennerberg and others
as indicated in source files.
The iMedia Browser Framework is licensed under the following terms:
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in all or substantial portions of the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to permit
persons to whom the Software is furnished to do so, subject to the following
conditions:
Redistributions of source code must retain the original terms stated here,
including this list of conditions, the disclaimer noted below, and the
following copyright notice: Copyright (c) 2005-2011 by Karelia Software et al.
Redistributions in binary form must include, in an end-user-visible manner,
e.g., About window, Acknowledgments window, or similar, either a) the original
terms stated here, including this list of conditions, the disclaimer noted
below, and the aforementioned copyright notice, or b) the aforementioned
copyright notice and a link to karelia.com/imedia.
Neither the name of Karelia Software, nor Sandvox, nor the names of
contributors to iMedia Browser may be used to endorse or promote products
derived from the Software without prior and express written permission from
Karelia Software or individual contributors, as appropriate.
Disclaimer: THE SOFTWARE IS PROVIDED BY THE COPYRIGHT OWNER AND CONTRIBUTORS
"AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT
LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE,
AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
LIABLE FOR ANY CLAIM, DAMAGES, OR OTHER LIABILITY, WHETHER IN AN ACTION OF
CONTRACT, TORT, OR OTHERWISE, ARISING FROM, OUT OF, OR IN CONNECTION WITH, THE
SOFTWARE OR THE USE OF, OR OTHER DEALINGS IN, THE SOFTWARE.
*/
// Author: Peter Baumgartner
//----------------------------------------------------------------------------------------------------------------------
#pragma mark HEADERS
#import "IMBApertureParser.h"
#import "IMBApertureHeaderViewController.h"
#import "IMBParserController.h"
#import "IMBNode.h"
#import "IMBObject.h"
#import "IMBIconCache.h"
#import "NSWorkspace+iMedia.h"
#import "NSFileManager+iMedia.h"
#import "NSImage+iMedia.h"
#import <Quartz/Quartz.h>
//----------------------------------------------------------------------------------------------------------------------
#pragma mark
@interface IMBApertureParser ()
- (NSString*) identifierForId:(NSNumber*) inId inSpace:(NSString*) inIdSpace;
- (NSString*) rootNodeIdentifier;
- (BOOL) shouldUseAlbumType:(NSString*)inAlbumType;
- (BOOL) isLeafAlbumType:(NSString*)inType;
- (NSImage*) iconForAlbumType:(NSString*)inType;
- (NSArray*) keylistForAlbum:(NSDictionary*)inAlbumDict;
- (BOOL) shouldUseObject:(NSString*)inObjectType;
- (void) addSubNodesToNode:(IMBNode*)inParentNode albums:(NSArray*)inAlbums images:(NSDictionary*)inImages;
- (void) populateNode:(IMBNode*)inNode albums:(NSArray*)inAlbums images:(NSDictionary*)inImages;
@end
//----------------------------------------------------------------------------------------------------------------------
#pragma mark
@implementation IMBApertureParser
@synthesize placeholderParser = _placeholderParser;
@synthesize appPath = _appPath;
@synthesize shouldDisplayLibraryName = _shouldDisplayLibraryName;
@synthesize version = _version;
//----------------------------------------------------------------------------------------------------------------------
// Register this parser, so that it gets automatically loaded...
+ (void) load
{
NSAutoreleasePool* pool = [[NSAutoreleasePool alloc] init];
[IMBParserController registerParserClass:self forMediaType:kIMBMediaTypeImage];
[pool drain];
}
//----------------------------------------------------------------------------------------------------------------------
// Check if Aperture is installed...
+ (NSString*) aperturePath
{
return [[NSWorkspace imb_threadSafeWorkspace] absolutePathForAppBundleWithIdentifier:@"com.apple.Aperture"];
}
+ (BOOL) isInstalled
{
return [self aperturePath] != nil;
}
//----------------------------------------------------------------------------------------------------------------------
// Look at the iApps preferences file and find all iPhoto libraries. Create a parser instance for each libary...
+ (NSArray*) parserInstancesForMediaType:(NSString*)inMediaType
{
NSMutableArray* parserInstances = [NSMutableArray array];
if ([self isInstalled])
{
CFArrayRef apertureLibraries = CFPreferencesCopyAppValue((CFStringRef)@"ApertureLibraries",(CFStringRef)@"com.apple.iApps");
NSArray* libraries = (NSArray*)apertureLibraries;
for (NSString* library in libraries)
{
NSURL* url = [NSURL URLWithString:library];
NSString* path = [url path];
BOOL changed;
(void) [[NSFileManager imb_threadSafeManager] imb_fileExistsAtPath:&path wasChanged:&changed];
IMBApertureParser* parser = [[[self class] alloc] initWithMediaType:inMediaType];
parser.mediaSource = path;
parser.shouldDisplayLibraryName = libraries.count > 1;
[parserInstances addObject:parser];
[parser release];
}
if (apertureLibraries) CFRelease(apertureLibraries);
if ([parserInstances count] == 0) {
NSArray *keys = [NSArray arrayWithObjects:@"RKXMLExportManagerMode", @"LibraryPath", nil];
NSDictionary *preferences = (NSDictionary*) CFPreferencesCopyMultiple((CFArrayRef)keys,
(CFStringRef)@"com.apple.Aperture",
kCFPreferencesCurrentUser,
kCFPreferencesAnyHost);
preferences = [NSMakeCollectable(preferences) autorelease];
NSString *exportManagerMode = [preferences objectForKey:[keys objectAtIndex:0]];
NSString *libraryPath = [preferences objectForKey:[keys objectAtIndex:1]];
if ((libraryPath != nil) && ([@"RKXMLExportManagerExportNeverKey" isEqual:exportManagerMode])) {
IMBApertureParser* parser = [[[self class] alloc] initWithMediaType:inMediaType];
parser.placeholderParser = YES;
parser.mediaSource = libraryPath;
parser.shouldDisplayLibraryName = NO;
[parserInstances addObject:parser];
[parser release];
}
}
}
return parserInstances;
}
//----------------------------------------------------------------------------------------------------------------------
- (id) initWithMediaType:(NSString*)inMediaType
{
if ((self = [super initWithMediaType:inMediaType]) != nil)
{
self.appPath = [[self class] aperturePath];
self.plist = nil;
self.modificationDate = nil;
self.version = 0;
}
return self;
}
- (void) dealloc
{
IMBRelease(_appPath);
IMBRelease(_plist);
IMBRelease(_modificationDate);
[super dealloc];
}
//----------------------------------------------------------------------------------------------------------------------
#pragma mark
#pragma mark Parser Methods
- (IMBNode*) nodeWithOldNode:(const IMBNode*)inOldNode options:(IMBOptions)inOptions error:(NSError**)outError
{
NSError* error = nil;
// Oops no path, can't create a root node. This is bad...
if (self.mediaSource == nil)
{
return nil;
}
// Create a root node...
IMBNode* node = [[[IMBNode alloc] init] autorelease];
if (inOldNode == nil)
{
NSImage* icon = [[NSWorkspace imb_threadSafeWorkspace] iconForFile:self.appPath];;
[icon setScalesWhenResized:YES];
[icon setSize:NSMakeSize(16.0,16.0)];
node.mediaSource = self.mediaSource;
node.identifier = [self rootNodeIdentifier];
node.name = @"Aperture";
node.icon = icon;
node.parser = self;
node.isTopLevelNode = YES;
node.groupType = kIMBGroupTypeLibrary;
if (self.placeholderParser)
{
node.leaf = YES;
node.shouldDisplayObjectView = NO;
}
else
{
node.leaf = NO;
}
}
// Or a subnode...
else
{
node.mediaSource = self.mediaSource;
node.identifier = inOldNode.identifier;
node.name = inOldNode.name;
node.icon = inOldNode.icon;
node.groupType = inOldNode.groupType;
node.leaf = inOldNode.leaf;
node.parser = self;
}
// If we have more than one library then append the library name to the root node...
if (node.isTopLevelNode && self.shouldDisplayLibraryName)
{
NSString* path = (NSString*)node.mediaSource;
NSString* name = [[[path stringByDeletingLastPathComponent] lastPathComponent] stringByDeletingPathExtension];
node.name = [NSString stringWithFormat:@"%@ (%@)",node.name,name];
}
// Watch the XML file. Whenever something in Aperture changes, we have to replace the
// WHOLE node tree, as we have no way of finding WHAT has changed inside the library...
if (node.isTopLevelNode)
{
node.watcherType = kIMBWatcherTypeFSEvent;
node.watchedPath = [(NSString*)node.mediaSource stringByDeletingLastPathComponent];
}
else
{
node.watcherType = kIMBWatcherTypeNone;
}
// If the old node was populated, then also populate the new node...
if (inOldNode.isPopulated)
{
[self populateNewNode:node likeOldNode:inOldNode options:inOptions];
}
if (outError) *outError = error;
return node;
}
//----------------------------------------------------------------------------------------------------------------------
// The supplied node is a private copy which may be modified here in the background operation. Parse the
// iPhoto XML file and create subnodes as needed...
- (BOOL) populateNode:(IMBNode*)inNode options:(IMBOptions)inOptions error:(NSError**)outError
{
NSError* error = nil;
NSDictionary* plist = self.plist;
NSDictionary* images = [plist objectForKey:@"Master Image List"];
// Population of events and faces node fundamentally different from album node
if ([self isFacesNode:inNode]) {
NSDictionary* faces = [plist objectForKey:@"List of Faces"];
[self populateFacesNode:inNode withFaces:faces images:images];
} else {
NSArray* albums = [plist objectForKey:@"List of Albums"];
[self addSubNodesToNode:inNode albums:albums images:images];
[self populateNode:inNode albums:albums images:images];
}
if (outError) *outError = error;
return error == nil;
}
//----------------------------------------------------------------------------------------------------------------------
// When the parser is deselected, then get rid of the cached plist data. It will be loaded into memory lazily
// once it is needed again...
- (void) didStopUsingParser
{
@synchronized(self)
{
self.plist = nil;
}
}
// When the XML file has changed then get rid of our cached plist...
- (void) watchedPathDidChange:(NSString*)inWatchedPath
{
if ([inWatchedPath isEqual:self.mediaSource])
{
@synchronized(self)
{
self.plist = nil;
}
}
}
//----------------------------------------------------------------------------------------------------------------------
// Placeholder parsers provide their own custom header view...
- (NSViewController*) customHeaderViewControllerForNode:(IMBNode*)inNode
{
if (self.placeholderParser)
{
return [IMBApertureHeaderViewController headerViewControllerWithNode:inNode];
}
return nil;
}
//----------------------------------------------------------------------------------------------------------------------
#pragma mark
#pragma mark Helper Methods
//----------------------------------------------------------------------------------------------------------------------
- (NSInteger) version
{
if (_version == 0)
{
_version = [[self.plist objectForKey:@"Application Version"] integerValue];
}
return _version;
}
//----------------------------------------------------------------------------------------------------------------------
// Create a unique identifier from the library path and the AlbumID that is stored in the XML file.
// An example is "IMBApertureParser://123/Sample/AlbumId/17"...
- (NSString*) identifierForId:(NSNumber*) inId inSpace:(NSString*) inIdSpace
{
NSString* path = (NSString*) self.mediaSource;
NSString* libraryName = [[[path stringByDeletingLastPathComponent] lastPathComponent] stringByDeletingPathExtension];
NSString* nodePath = nil;
if (inIdSpace)
{
nodePath = [NSString stringWithFormat:@"/%i/%@/%@/%@",[path hash],libraryName,inIdSpace,inId];
} else {
nodePath = [NSString stringWithFormat:@"/%i/%@/%@",[path hash],libraryName,inId];
}
return [self identifierForPath:nodePath];
}
// In Aperture 2 the root node always has the hardcoded AlbumID 1. In Aperture 3 however we are choosing a
// different node as root as there always seems to be an (empty) extra node (Album Type 5) inserted between
// the root and the nodes that we like to see at the first level. So we'll look for this type 5 album and
// return its id as the root node...
- (NSString*) rootNodeIdentifier
{
// Aperture 2...
if (self.version < 3)
{
return [self identifierForId:[NSNumber numberWithInt:1] inSpace:nil];
}
// Aperture 3...
NSArray* albums = [self.plist objectForKey:@"List of Albums"];
for (NSDictionary* albumDict in albums)
{
NSAutoreleasePool* pool = [[NSAutoreleasePool alloc] init];
NSString* albumType = [albumDict objectForKey:@"Album Type"];
NSNumber* albumId = [albumDict objectForKey:@"AlbumId"];
NSString* rootNodeIdentifier = nil;
if ([albumType isEqualToString:@"5"])
{
rootNodeIdentifier = [[self identifierForId:albumId inSpace:nil] retain];
}
[pool drain];
if (rootNodeIdentifier != nil)
{
return [rootNodeIdentifier autorelease];
}
}
// Fallback if nothing is found...
return [self identifierForId:[NSNumber numberWithInt:1] inSpace:nil];
}
//----------------------------------------------------------------------------------------------------------------------
// Exclude some album types. Specifically exclude all root albums as the root node has already
// been created by the parser during its first invocation...
- (BOOL) shouldUseAlbumType:(NSString*)inAlbumType
{
if ([inAlbumType isEqualToString:@"5"]) return (self.version < 3);
if ([inAlbumType isEqualToString:@"97"]) return NO;
if ([inAlbumType isEqualToString:@"98"]) return NO;
if ([inAlbumType isEqualToString:@"99"]) return NO;
return YES;
}
// This method may be used to filter specific albums from the list
// (e.g. ones that don't have the correct media type).
// Always returns YES for now.
- (BOOL) shouldUseAlbum:(NSDictionary*)inAlbumDict images:(NSDictionary*)inImages
{
return YES;
}
// Return YES indicating that an album should be a leaf node, i.e. that it does not have a disclosure triangle
// in the IMBOutlineView...
- (BOOL) isLeafAlbumType:(NSString*)inType
{
NSInteger type = [inType integerValue];
switch (type)
{
case 1: return YES; // Album
case 2: return YES; // Smart album
case 3: return YES; // Smart album
case 4: return NO; // Project
case 5: return NO; // All projects
case 6: return NO; // Folder
case 7: return NO; // Folder
case 8: return YES; // Book
case 9: return YES; // Web page
case 10: return YES; // Web journal
case 11: return YES; // Lighttable
case 13: return YES; // Web gallery
case 19: return YES; // Slideshow
case 94: return YES; // Photos
case 95: return YES; // Flagged
case 96: return NO; // Smart albums
case 97: return NO; // Library
case 98: return NO; // Library
case 99: return NO; // Library (holding all images)
}
return [super isLeafAlbumType:inType];
}
//----------------------------------------------------------------------------------------------------------------------
// Returns whether the album dictionary provided represents the "photos" album
- (BOOL) isAllPhotosAlbum:(NSDictionary*)inAlbumDict
{
return [[inAlbumDict objectForKey:@"uuid"] isEqualToString:@"allPhotosAlbum"];
}
//----------------------------------------------------------------------------------------------------------------------
// Icons for older Aperture versions...
- (NSImage*) iconForAlbumType2:(NSString*)inType
{
static const IMBIconTypeMappingEntry kIconTypeMappingEntries[] =
{
{@"v2-1", @"Project_I_Album.tiff", @"folder", nil, nil}, // album
{@"v2-2", @"Project_I_SAlbum.tiff", @"folder", nil, nil}, // smart album
{@"v2-3", @"List_Icons_LibrarySAlbum.tiff", @"folder", nil, nil}, // library **** ... 200X
{@"v2-4", @"Project_I_Project.tiff", @"folder", nil, nil}, // project
{@"v2-5", @"List_Icons_Library.tiff", @"folder", nil, nil}, // library (top level)
{@"v2-6", @"Project_I_Folder.tiff", @"folder", nil, nil}, // folder
{@"v2-7", @"Project_I_ProjectFolder.tiff", @"folder", nil, nil}, // sub-folder of project
{@"v2-8", @"Project_I_Book.tiff", @"folder", nil, nil}, // book
{@"v2-9", @"Project_I_WebPage.tiff", @"folder", nil, nil}, // web gallery
{@"v2-9", @"Project_I_WebGallery.tiff", @"folder", nil, nil}, // web gallery (alternate image)
{@"v2-10", @"Project_I_WebJournal.tiff", @"folder", nil, nil}, // web journal
{@"v2-11", @"Project_I_LightTable.tiff", @"folder", nil, nil}, // light table
{@"v2-13", @"Project_I_SWebGallery.tiff", @"folder", nil, nil}, // smart web gallery
{@"v2-97", @"Project_I_Projects.tiff", @"folder", nil, nil}, // library
{@"v2-98", @"AppIcon.icns", @"folder", nil, nil}, // library
{@"v2-99", @"List_Icons_Library.tiff", @"folder", nil, nil}, // library (knot holding all images)
};
static const IMBIconTypeMapping kIconTypeMapping =
{
sizeof(kIconTypeMappingEntries) / sizeof(kIconTypeMappingEntries[0]),
kIconTypeMappingEntries,
{@"v2-1", @"Project_I_Album.tiff", @"folder", nil, nil} // fallback image
};
// Since icons are different for different versions of Aperture, we are adding the prefix v2- or v3-
// to the album type so that we can store different icons (for each version) in the icon cache...
NSString* type = [@"v2-" stringByAppendingString:inType];
return [[IMBIconCache sharedIconCache] iconForType:type fromBundleID:@"com.apple.Aperture" withMappingTable:&kIconTypeMapping];
}
// New icons for Aperture 3...
- (NSImage*) iconForAlbumType3:(NSString*)inType
{
static const IMBIconTypeMappingEntry kIconTypeMappingEntries[] =
{
{@"v3-Faces",@"sl-icon-small_people.tiff", @"folder", nil, nil},
{@"v3-1", @"SL-album.tiff", @"folder", nil, nil}, // album
{@"v3-2", @"SL-smartAlbum.tiff", @"folder", nil, nil}, // smart album
{@"v3-3", @"SL-smartAlbum.tiff", @"folder", nil, nil}, // library **** ... 200X
{@"v3-4", @"SL-project.tiff", @"folder", nil, nil}, // project
{@"v3-5", @"SL-allProjects.tiff", @"folder", nil, nil}, // library (top level)
{@"v3-6", @"SL-folder.tiff", @"folder", nil, nil}, // folder
{@"v3-7", @"SL-folder.tiff", @"folder", nil, nil}, // sub-folder of project
{@"v3-8", @"SL-book.tiff", @"folder", nil, nil}, // book
{@"v3-9", @"SL-webpage.tiff", @"folder", nil, nil}, // web gallery
{@"v3-9", @"Project_I_WebGallery.tiff", @"folder", nil, nil}, // web gallery (alternate image)
{@"v3-10", @"SL-webJournal.tiff", @"folder", nil, nil}, // web journal
{@"v3-11", @"SL-lightTable.tiff", @"folder", nil, nil}, // light table
{@"v3-13", @"sl-icon-small_webGallery.tiff", @"folder", nil, nil}, // smart web gallery
{@"v3-19", @"SL-slideshow.tiff", @"folder", nil, nil}, // slideshow
{@"v3-94", @"SL-photos.tiff", @"folder", nil, nil}, // photos
{@"v3-95", @"SL-flag.tif", @"folder", nil, nil}, // flagged
{@"v3-96", @"SL-smartLibrary.tiff", @"folder", nil, nil}, // library albums
{@"v3-97", @"SL-allProjects.tiff", @"folder", nil, nil}, // library
{@"v3-98", @"AppIcon.icns", @"folder", nil, nil}, // library
{@"v3-99", @"List_Icons_Library.tiff", @"folder", nil, nil}, // library (knot holding all images)
};
static const IMBIconTypeMapping kIconTypeMapping =
{
sizeof(kIconTypeMappingEntries) / sizeof(kIconTypeMappingEntries[0]),
kIconTypeMappingEntries,
{@"1", @"SL-album.tiff", @"folder", nil, nil} // fallback image
};
// Since icons are different for different versions of Aperture, we are adding the prefix v2- or v3-
// to the album type so that we can store different icons (for each version) in the icon cache...
NSString* type = [@"v3-" stringByAppendingString:inType];
return [[IMBIconCache sharedIconCache] iconForType:type fromBundleID:@"com.apple.Aperture" withMappingTable:&kIconTypeMapping];
}
- (NSImage*) iconForAlbumType:(NSString*)inType
{
if (self.version < 3)
{
return [self iconForAlbumType2:inType];
}
else
{
return [self iconForAlbumType3:inType];
}
}
//----------------------------------------------------------------------------------------------------------------------
// The xml file has a list of albums. Each album contains a KeyList entry (array of images). However this list
// does not always seem to corresponds with what one sees in the Aperture UI itself. Instead there are albums
// in the xml that we do not want to display as a node, which do contain the images we need for another node.
// For this reason we need a mapping mechanism that lets us assign the KeyList of one album to another node.
// The following two methods provide this mecahism...
- (NSArray*) _keylistForAlbumType:(NSString*)inAlbumType
{
NSArray* albums = [self.plist objectForKey:@"List of Albums"];
for (NSDictionary* albumDict in albums)
{
NSString* albumType = [albumDict objectForKey:@"Album Type"];
if ([albumType isEqualToString:inAlbumType])
{
return [albumDict objectForKey:@"KeyList"];
}
}
return nil;
}
- (NSArray*) keylistForAlbum:(NSDictionary*)inAlbumDict
{
NSString* albumType = [inAlbumDict objectForKey:@"Album Type"];
// In Aperture 3 map keyList of album 99 to root node and Photos node...
// if (self.version == 3)
// {
if ([albumType isEqualToString:@"98"]) // root node
{
return [self _keylistForAlbumType:@"99"];
}
else if ([albumType isEqualToString:@"5"]) // root node
{
return [self _keylistForAlbumType:@"99"];
}
else if ([albumType isEqualToString:@"94"]) // Photos node
{
return [self _keylistForAlbumType:@"99"];
}
// }
// All other album just use their own key list...
return [inAlbumDict objectForKey:@"KeyList"];
}
//----------------------------------------------------------------------------------------------------------------------
// Exclude everything but images...
- (BOOL) shouldUseObject:(NSString*)inObjectType
{
return inObjectType==nil || [inObjectType isEqualToString:@"Image"];
}
//----------------------------------------------------------------------------------------------------------------------
- (void) addSubNodesToNode:(IMBNode*)inParentNode albums:(NSArray*)inAlbums images:(NSDictionary*)inImages
{
// Create the subNodes array on demand - even if turns out to be empty after exiting this method,
// because without creating an array we would cause an endless loop...
NSMutableArray* subNodes = [NSMutableArray array];
// Now parse the Aperture XML plist and look for albums whose parent matches our parent node. We are
// only going to add subnodes that are direct children of inParentNode...
for (NSDictionary* albumDict in inAlbums)
{
NSAutoreleasePool* pool = [[NSAutoreleasePool alloc] init];
NSString* albumType = [albumDict objectForKey:@"Album Type"];
NSString* albumName = [albumDict objectForKey:@"AlbumName"];
NSNumber* parentId = [albumDict objectForKey:@"Parent"];
// Root node does not have an id space
NSString* parentIdSpace = [inParentNode isTopLevelNode] ? nil : [self idSpaceForAlbumType:albumType];
// parent always from same id space for non top-level albums
NSString* parentIdentifier = parentId ? [self identifierForId:parentId inSpace:parentIdSpace] : [self identifierForPath:@"/"];
if ([self shouldUseAlbumType:albumType] && [inParentNode.identifier isEqualToString:parentIdentifier])
{
// Create node for this album...
IMBNode* albumNode = [[[IMBNode alloc] init] autorelease];
albumNode.leaf = [self isLeafAlbumType:albumType];
albumNode.icon = [self iconForAlbumType:albumType];
albumNode.name = albumName;
albumNode.mediaSource = self.mediaSource;
albumNode.parser = self;
// Keep a ref to the album dictionary for later use when we populate this node
// so we don't have to loop through the whole album list again to find it.
albumNode.attributes = albumDict;
// Set the node's identifier. This is needed later to link it to the correct parent node...
NSNumber* albumId = [albumDict objectForKey:@"AlbumId"];
albumNode.identifier = [self identifierForId:albumId inSpace:[self idSpaceForAlbumType:albumType]];
// Add the new album node to its parent (inRootNode)...
[subNodes addObject:albumNode];
}
[pool drain];
}
inParentNode.subNodes = subNodes;
}
//----------------------------------------------------------------------------------------------------------------------
- (Class) objectClass
{
return [IMBObject class];
}
// Returns an empty dictionary for now.
// TODO: Verify whether we need to return some "true" values for "KeyList", "KeyPhotoKey" and "PhotoCount"
- (NSDictionary*) childrenInfoForNode:(IMBNode*)inNode images:(NSDictionary*)inImages
{
return [NSDictionary dictionaryWithObjectsAndKeys:nil];
}
//----------------------------------------------------------------------------------------------------------------------
- (void) populateNode:(IMBNode*)inNode albums:(NSArray*)inAlbums images:(NSDictionary*)inImages
{
// Create the objects array on demand - even if turns out to be empty after exiting this method, because
// without creating an array we would cause an endless loop...
NSMutableArray* objects = [[NSMutableArray alloc] initWithArray:inNode.objects];
// Look for the correct album in the Aperture XML plist. Once we find it, populate the node with IMBVisualObjects
// for each image in this album...
Class objectClass = [self objectClass];
NSUInteger index = 0;
// We saved a reference to the album dictionary when this node was created
// (ivar 'attributes') and now happily reuse it to save an outer loop (over album list) here.
NSDictionary* albumDict = inNode.attributes;
NSAutoreleasePool* pool1 = [[NSAutoreleasePool alloc] init];
// NSArray* imageKeys = [albumDict objectForKey:@"KeyList"];
NSArray* imageKeys = [self keylistForAlbum:albumDict];
for (NSString* key in imageKeys)
{
NSAutoreleasePool* pool2 = [[NSAutoreleasePool alloc] init];
NSDictionary* objectDict = [inImages objectForKey:key];
NSString* mediaType = [objectDict objectForKey:@"MediaType"];
if (objectDict!=nil && [self shouldUseObject:mediaType])
{
NSString* imagePath = [objectDict objectForKey:@"ImagePath"];
NSString* thumbPath = [objectDict objectForKey:@"ThumbPath"];
NSString* caption = [objectDict objectForKey:@"Caption"];
NSMutableDictionary* preliminaryMetadata = [NSMutableDictionary dictionaryWithDictionary:objectDict];
[preliminaryMetadata setObject:key forKey:@"VersionUUID"];
IMBObject* object = [[objectClass alloc] init];
[objects addObject:object];
[object release];
object.location = (id)imagePath;
object.name = caption;
object.preliminaryMetadata = preliminaryMetadata; // This metadata from the XML file is available immediately
object.metadata = nil; // Build lazily when needed (takes longer)
object.metadataDescription = nil; // Build lazily when needed (takes longer)
object.parser = self;
object.index = index++;
object.imageLocation = (thumbPath!=nil) ? thumbPath : imagePath;
object.imageRepresentationType = IKImageBrowserCGImageRepresentationType;
object.imageRepresentation = nil;
}
[pool2 drain];
}
[pool1 drain];
inNode.objects = objects;
[objects release];
}
//----------------------------------------------------------------------------------------------------------------------
// Loaded lazily when actually needed for display. Here we combine the metadata we got from the Aperture XML file
// (which was available immediately, but not enough information) with more information that we obtain via ImageIO.
// This takes a little longer, but since it only done laziy for those object that are actually visible it's fine.
// Please note that this method may be called on a background thread...
- (void) loadMetadataForObject:(IMBObject*)inObject
{
NSMutableDictionary* metadata = [NSMutableDictionary dictionaryWithDictionary:inObject.preliminaryMetadata];
// Do not load (key) image specific metadata for node objects
// because it doesn't represent the nature of the object well enough.
if (![inObject isKindOfClass:[IMBNodeObject class]])
{
[metadata addEntriesFromDictionary:[NSImage imb_metadataFromImageAtPath:inObject.path checkSpotlightComments:NO]];
}
NSString* description = [self metadataDescriptionForMetadata:metadata];
if ([NSThread isMainThread])
{
inObject.metadata = metadata;
inObject.metadataDescription = description;
}
else
{
NSArray* modes = [NSArray arrayWithObject:NSRunLoopCommonModes];
[inObject performSelectorOnMainThread:@selector(setMetadata:) withObject:metadata waitUntilDone:NO modes:modes];
[inObject performSelectorOnMainThread:@selector(setMetadataDescription:) withObject:description waitUntilDone:NO modes:modes];
}
}
//----------------------------------------------------------------------------------------------------------------------
@end