-
Notifications
You must be signed in to change notification settings - Fork 52
/
IMBImageCaptureParser.m
1000 lines (794 loc) · 32.2 KB
/
IMBImageCaptureParser.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
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
iMedia Browser Framework <http://karelia.com/imedia/>
Copyright (c) 2005-2012 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-2012 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: Thomas Engelmeier
// Known bugs:
// When the first camera is attached, the device node gets added twice
// The generic folder Icon does not scale correctly until the tableview cell is touched
// Drag and Drop: Files are copied multiple times
// Drag and Drop: Download destination is in /var/tmp/..
//----------------------------------------------------------------------------------------------------------------------
#import "IMBImageCaptureParser.h"
#import "IMBParserController.h"
#import "IMBNode.h"
#import "IMBObject.h"
#import "IMBConfig.h"
#import "IMBLibraryController.h"
#import "IMBNodeObject.h"
#import "IMBObjectsPromise.h"
#import "IMBOperationQueue.h"
#import "NSFileManager+iMedia.h"
#import "NSImage+iMedia.h"
#import <Carbon/Carbon.h>
#import <Quartz/Quartz.h>
#import "NSWorkspace+iMedia.h"
//----------------------------------------------------------------------------------------------------------------------
// Internal classes:
// Purpose: protocol to keep the user informed of download progress
@class IMBMTPDownloadOperation;
@protocol IMBMTPDownloadOperationDelegate
- (void) operationDidDownloadFile:(IMBMTPDownloadOperation*)inOperation;
- (void) operationDidFinish:(IMBMTPDownloadOperation*)inOperation;
- (void) operation:(IMBMTPDownloadOperation*)inOperation didReceiveError:(NSError *) anError;
@end
// Purpose: Gets created from drag'n drop. Triggers copying of files to a local destination
@interface IMBMTPObjectPromise : IMBRemoteObjectsPromise<IMBMTPDownloadOperationDelegate>
{
long long _bytesTotal;
long long _bytesDone;
}
@property (assign,readonly) long long bytesTotal;
@property (assign,readonly) long long bytesDone;
@end
// Purpose: Copy a bunch of files
@interface IMBMTPDownloadOperation : NSOperation
{
// input parameters
id _delegate;
NSArray *_objectsToLoad;
NSString *_downloadFolderPath;
// changed during operation
NSMutableArray *_receivedFilePaths;
long long _receivedBytes;
}
@property (assign) id<IMBMTPDownloadOperationDelegate> delegate;
@property (copy) NSArray *objectsToLoad;
@property (copy) NSString *downloadFolderPath;
@property (retain) NSMutableArray *receivedFilePaths;
@property (assign) long long receivedBytes;
@property (assign, readonly) long long totalBytes;
@end
// Purpose: proxy thumbnails that get lazily downloaded
@interface MTPVisualObject: IMBObject
{
}
- (void) _gotThumbnailCallback: (ICACopyObjectThumbnailPB*)pbPtr;
@end
//----------------------------------------------------------------------------------------------------------------------
@interface IMBImageCaptureParser (internal)
- (void) _installNotification;
- (void) _uninstallNotification;
- (BOOL) _isAppropriateICAType:(uint32_t) inType;
- (BOOL) _addICATree:(NSArray *)subItems toNode:(IMBNode *)inNode;
- (void) _addICAObject:(NSDictionary *) anItem toObjectArray:(NSMutableArray *) objectArray;
- (void) _gotICANotification:(NSString *) aNotification withDictionary:(NSDictionary *) aDictionary;
- (IMBNode *) _nodeForDevicelist;
- (IMBNode *) _nodeForDevice:(NSDictionary *) anDevice;
- (IMBNode *) _nodeForTempDevice:(NSDictionary *) anDevice;
- (NSString *) _identifierForICAObject:(id) anObjectID;
- (NSImage *) _getThumbnailSync:(id) anObject;
- (void) _addICADeviceList:(NSArray *) devices toNode:(IMBNode *)inNode;
@end
// Hmm, is this an internal symbol?
//#ifdef __DEBUGGING__
#if 0
#define DEBUGLOG( fmt, ... ) NSLog( fmt, __VA_ARGS__ )
#else
#define DEBUGLOG( fmt, ... ) {}
#endif
//----------------------------------------------------------------------------------------------------------------------
@implementation IMBImageCaptureParser
@synthesize loadingDevices = _loadingDevices;
+ (void) load
{
NSAutoreleasePool* pool = [[NSAutoreleasePool alloc] init];
[IMBParserController registerParserClass:self forMediaType:kIMBMediaTypeImage];
[pool drain];
}
#pragma mark
#pragma mark Parser Methods
- (id) initWithMediaType:(NSString *) anType
{
self = [super initWithMediaType:anType];
if (self != nil) {
// we prepopulate the root mediasource with the ICA ID of the list of cameras
ICAGetDeviceListPB deviceListPB;
memset( &deviceListPB, 0, sizeof( ICAGetDeviceListPB ));
OSStatus err = ICAGetDeviceList((ICAGetDeviceListPB *)&deviceListPB, NULL);
if( err == noErr )
{
self.loadingDevices = [NSMutableDictionary dictionary];
self.mediaSource = [[NSNumber numberWithInt:deviceListPB.object] stringValue];
[self _installNotification];
}
}
return self;
}
- (void) dealloc
{
[self _uninstallNotification];
self.loadingDevices = nil;
self.mediaSource = nil;
[super dealloc];
}
- (IMBObjectsPromise*) objectPromiseWithObjects:(NSArray*)inObjects
{
IMBMTPObjectPromise *promise = [([IMBMTPObjectPromise alloc]) initWithIMBObjects:inObjects];
return [promise autorelease];
}
// copy a given node
- (IMBNode *) nodeCopy:(const IMBNode*)inOldNode
{
IMBNode* newNode = [[IMBNode alloc] init];
newNode.mediaSource = inOldNode.mediaSource;
newNode.identifier = inOldNode.identifier;
newNode.name = inOldNode.name;
newNode.icon = inOldNode.icon;
newNode.parser = self;
newNode.leaf = inOldNode.leaf;
newNode.group = inOldNode.group;
newNode.groupType = kIMBGroupTypeNone;
newNode.isTopLevelNode = inOldNode.isTopLevelNode;
// Enable ICA Event watching for all nodes...
newNode.watcherType = kIMBWatcherTypeFirstCustom;
newNode.watchedPath = inOldNode.watchedPath;
[self populateNode:newNode options:0 error:nil];
return [newNode autorelease];
}
// Scan the given node "folder" for subfolders and add a subnode for each one we find...
- (BOOL) populateNode:(IMBNode*)inNode options:(IMBOptions)inOptions error:(NSError**)outError
{
DEBUGLOG( @"%s\n inNode %@",__PRETTY_FUNCTION__, inNode );
if( !inNode.objects ||
!inNode.subNodes )
{ // only populate if empty
ICAObject object = [inNode.mediaSource intValue];
ICACopyObjectPropertyDictionaryPB copyObjectsPB;
memset( ©ObjectsPB, 0, sizeof(ICACopyObjectPropertyDictionaryPB));
NSDictionary *propertiesDict = NULL;
copyObjectsPB.object = object;
copyObjectsPB.theDict = (CFDictionaryRef *) &propertiesDict;
OSStatus err = ICACopyObjectPropertyDictionary(©ObjectsPB, NULL);
if (err == noErr && noErr == copyObjectsPB.header.err )
{
// DEBUGLOG( @"%@", propertiesDict );
NSArray *devices = [propertiesDict valueForKey:(NSString *)kICADevicesArrayKey];
if( devices )
[self _addICADeviceList:devices toNode:inNode];
else
[self _addICATree:[(NSDictionary *)propertiesDict valueForKey:@"tree"] toNode:inNode];
CFRelease( propertiesDict );
}
}
return YES;
}
- (IMBNode*) nodeWithOldNode:(const IMBNode*)inOldNode options:(IMBOptions)inOptions error:(NSError**)outError
{
DEBUGLOG( @"%s: old %@",__PRETTY_FUNCTION__, inOldNode );
NSError* error = nil;
// note: if inOldNode is nil, this represents the device list root object
IMBNode* newNode = nil;
if( inOldNode )
{
newNode = [self nodeCopy:inOldNode];
// already done in copyNode:
// if ( [inOldNode.subNodes count] || [inOldNode.objects count] )
// { // If the old node had subnodes, then look for subnodes in the new node...
// [self populateNode:newNode options:inOptions error:&error];
// }
}
else
newNode = [self _nodeForDevicelist];
if (outError) *outError = error;
return newNode;
}
#pragma mark -
#pragma mark Internal handling
#pragma mark Populate nodes with data returned from ICA:
- (void) _addICADeviceList:(NSArray *) devices toNode:(IMBNode *)inNode
{
// we retrieved a device list
NSMutableArray *subnodes = [NSMutableArray array];
NSMutableArray *deviceIdentifiers = [NSMutableArray array];
for( NSDictionary *anDevice in devices )
{
[deviceIdentifiers addObject:[anDevice valueForKey:@"icao"]];
IMBNode* deviceNode = [self _nodeForDevice:anDevice];
[subnodes addObject:deviceNode];
}
inNode.subNodes = subnodes;
inNode.objects = [NSArray array]; // prevent endless loop
// now add any unlisted devices with an placeholder node
NSArray *loadingIdentifiers = [self.loadingDevices allKeys];
for( id anKey in loadingIdentifiers )
{
if( ![deviceIdentifiers containsObject:anKey] )
{
[subnodes addObject:[self _nodeForTempDevice:[self.loadingDevices objectForKey:anKey]]];
}
}
}
// recursive creation of subtree nodes
// R: object had children
- (BOOL) _addICATree:(NSArray *)subItems toNode:(IMBNode *)inNode
{
DEBUGLOG( @"%s\n inNode %@",__PRETTY_FUNCTION__, inNode );
NSMutableArray *subnodes = [NSMutableArray array];
NSMutableArray *objects = [NSMutableArray array];
for( NSDictionary *anItem in subItems )
{ // subitems
uint32_t type = [[anItem valueForKey:@"file"] intValue];
NSString *name = [anItem valueForKey:@"ifil"];
if( type == kICADirectory )
{
if( [name length] )
{
NSString *imageCaptureID = [anItem valueForKey:@"icao"];
IMBNode* subnode = [IMBNode new];
subnode.mediaSource = imageCaptureID;
subnode.identifier = [self _identifierForICAObject:imageCaptureID];
subnode.name = name;
subnode.parser = self;
subnode.attributes = anItem;
// retrieve the thumbnail. Fallback is the generic folder icon
if( [anItem valueForKey:@"thuP"] )
subnode.icon = [self _getThumbnailSync:imageCaptureID];
if( !subnode.icon )
subnode.icon = [NSImage imb_sharedGenericFolderIcon];
// subnode.icon = [[NSWorkspace imb_threadSafeWorkspace] iconForFileType:@"'fldr'"];
BOOL hasSubnodes = [self _addICATree:[anItem valueForKey:@"tree"] toNode:subnode];
subnode.leaf = !hasSubnodes;
subnode.wantsRecursiveObjects = YES;
[subnodes addObject:subnode];
[subnode release];
}
else
{
// TE: On my Lumix, nameless containers are basically an logical group of related media.
// i.e. Movie plus poster image
// options are:
// a.) synthesize an name from the top level object
// b.) strip the top level object and use the UTI filter to insert it in the current node
// As iMedia is using a filtered rep, option b makes more sense
NSArray *subObjects = [anItem valueForKey:@"tree"];
NSMutableArray *tmpObjects = [NSMutableArray array];
for( NSDictionary *tmpObject in subObjects )
[self _addICAObject:tmpObject toObjectArray:tmpObjects];
// TODO: test if really only one object remained
// TODO: test if an problem occurs with later handled removal notificatons
[objects addObjectsFromArray:tmpObjects];
}
}
else
{
[self _addICAObject:anItem toObjectArray:objects];
}
}
inNode.subNodes = subnodes;
inNode.objects = objects;
return [subnodes count] > 0;
}
- (void) _addICAObject:(NSDictionary *) anItem toObjectArray:(NSMutableArray *) objectArray
{
uint32_t type = [[anItem valueForKey:@"file"] intValue];
NSUInteger index = 0;
if( [self _isAppropriateICAType:type] )
{
MTPVisualObject* object = [MTPVisualObject new];
object.location = [[anItem valueForKey:@"icao"] stringValue];
object.name = [anItem valueForKey:@"ifil"];
object.metadata = anItem;
object.parser = self;
object.index = index+1;
[objectArray addObject:object];
[object release];
}
}
#pragma mark Create nodes prepared for different use cases:
// populate the node for the device list
- (IMBNode *) _nodeForDevicelist
{
DEBUGLOG( @"%s",__PRETTY_FUNCTION__ );
BOOL showsGroupNodes = [IMBConfig showsGroupNodes];
NSString* path = [self.mediaSource stringByStandardizingPath];
NSString* name = NSLocalizedStringWithDefaultValue(
@"IMBImageCaptureParser.Devices",
nil,IMBBundle(),
@"Devices",
@"Caption for Image Capture Root node");
// Create an empty root node (unpopulated and without subnodes)...
IMBNode* newNode = [[IMBNode alloc] init];
newNode.mediaSource = path;
newNode.identifier = [self _identifierForICAObject:self.mediaSource];
newNode.name = showsGroupNodes ? [name uppercaseString] : name;
newNode.parser = self;
newNode.leaf = NO;
newNode.isTopLevelNode = YES;
if( !showsGroupNodes )
newNode.icon = [[NSWorkspace imb_threadSafeWorkspace] iconForFile:@"/Applications/Image Capture.app"];
newNode.group = showsGroupNodes;
newNode.groupType = kIMBGroupTypeNone;
// Enable ICA Event watching for all nodes...
newNode.watcherType = kIMBWatcherTypeFirstCustom;
newNode.watchedPath = path;
[self populateNode:newNode options:0 error:nil];
return [newNode autorelease];
}
// populate the node for an single device
- (IMBNode *) _nodeForDevice:(NSDictionary *) anDevice
{
DEBUGLOG( @"%s: device %@",__PRETTY_FUNCTION__, anDevice );
NSString* parserClassName = NSStringFromClass([self class]);
IMBNode* subnode = [IMBNode new];
subnode.mediaSource = [anDevice valueForKey:@"icao"];
subnode.identifier = [NSString stringWithFormat:@"%@:/%@",parserClassName,subnode.mediaSource];
NSString *name = [anDevice valueForKey:@"ICAUserdefinedName"];
if( !name || ![name length] )
name = [anDevice valueForKey:@"ifil"];
subnode.name = name;
subnode.icon = [self _getThumbnailSync:subnode.mediaSource];
subnode.parser = self;
subnode.leaf = NO;
subnode.wantsRecursiveObjects = YES;
subnode.attributes = anDevice;
return [subnode autorelease];
}
- (IMBNode *) _nodeForTempDevice:(NSDictionary *) anDevice
{
DEBUGLOG( @"%s\n temp %@",__PRETTY_FUNCTION__, anDevice );
NSString* parserClassName = NSStringFromClass([self class]);
IMBNode* subnode = [IMBNode new];
subnode.mediaSource = [anDevice valueForKey:@"icao"];
subnode.identifier = [NSString stringWithFormat:@"%@:/%@",parserClassName,subnode.mediaSource];
NSString *name = NSLocalizedStringWithDefaultValue(@"IMBImageCaptureParser.loading",nil,IMBBundle(),@"Loading…",@"Caption for loading camera");
subnode.name = name;
// subnode.icon = [self _getThumbnailSync:subnode.mediaSource];
subnode.parser = self;
subnode.group = NO;
subnode.leaf = YES;
subnode.wantsRecursiveObjects = NO;
subnode.loading = YES;
subnode.attributes = anDevice;
return [subnode autorelease];
}
//----------------------------------------------------------------------------------------------------------------------
- (NSString *) _identifierForICAObject:(id) anObjectID
{
NSString* parserClassName = NSStringFromClass([self class]);
if( ![anObjectID isKindOfClass:[NSString class]] )
anObjectID = [anObjectID stringValue];
NSString *path = [anObjectID stringByStandardizingPath];
return [NSString stringWithFormat:@"%@:/%@",parserClassName,path];
}
- (BOOL) _isAppropriateICAType:(uint32_t) inType
{
BOOL isOurType = NO;
switch( inType )
{
// case kICAList:
// case kICADirectory:
// case kICAFile:
// case kICAFileFirmware:
// case kICAFileOther:
case kICAFileImage:
isOurType = [self.mediaType isEqualTo:kIMBMediaTypeImage];
break;
case kICAFileMovie:
isOurType = [self.mediaType isEqualTo:kIMBMediaTypeMovie];
break;
case kICAFileAudio:
isOurType = [self.mediaType isEqualTo:kIMBMediaTypeAudio];
break;
}
return isOurType;
}
- (NSImage *) _getThumbnailSync:(id) anObject
{
NSImage *image = NULL;
NSData *data = NULL;
ICACopyObjectThumbnailPB pb = { 0 };
pb.header.refcon = 0L;
pb.thumbnailFormat = kICAThumbnailFormatTIFF; // gives transparency
pb.object = [anObject intValue];
pb.thumbnailData = (CFDataRef*) &data;
/*err =*/ ICACopyObjectThumbnail( &pb, NULL );
if (noErr == pb.header.err)
{
// got the thumbnail data, now create an image...
image = [[NSImage alloc] initWithData:data];
[image setScalesWhenResized:YES];
[image setSize:NSMakeSize(16.0,16.0)];
[data release];
DEBUGLOG( @"%s -> %@", __PRETTY_FUNCTION__, self );
}
return [image autorelease];
}
#pragma mark Image Capture Notification Handling:
// watching:
static void ICANotificationCallback(CFStringRef notificationType, CFDictionaryRef notificationDictionary)
{
id self_ = [[(NSDictionary *)notificationDictionary valueForKey:@"ICARefconKey"] pointerValue];
[self_ _gotICANotification:(NSString *)notificationType withDictionary:(NSDictionary *)notificationDictionary];
}
// ———————————————————————————————————————————————————————————————————————————
// - installNotification:
// ———————————————————————————————————————————————————————————————————————————
- (void)_installNotification
{
NSAssert( sizeof(id) <= sizeof(unsigned long), @"Need enough space to stope an pointer in the ImageCapture Refcon" );
ICARegisterForEventNotificationPB pb;
OSErr err;
memset(&pb, 0, sizeof(ICARegisterForEventNotificationPB));
pb.header.refcon = (unsigned long)self;
pb.objectOfInterest = 0; // all objects
pb.eventsOfInterest = (CFArrayRef)[NSArray arrayWithObjects:
// object level:
/// kICANotificationTypeObjectAdded, => when the device is connected, first a flood of those notifications arrives
(NSString *) kICANotificationTypeObjectRemoved,
(NSString *) kICANotificationTypeObjectInfoChanged,
// memory card level:
(NSString *) kICANotificationTypeStoreAdded,
(NSString *) kICANotificationTypeStoreRemoved,
// device level:
(NSString *) kICANotificationTypeDeviceAdded, // issued after all object info is retrieved
(NSString *) kICANotificationTypeDeviceRemoved, // issued immediately when the device goes off
(NSString *) kICANotificationTypeDeviceInfoChanged, // issued when another driver takes over
(NSString *) kICANotificationTypeDeviceWasReset,
// the following two constants are tagged as appearing from 10.5 on in the 10.6
// but in reality they appear in the 10.6 SDK
// (NSString *) kICANotificationTypeDeviceStatusInfo,
// (NSString *) kICANotificationTypeDeviceStatusError,
(NSString *) kICANotificationTypeUnreportedStatus,
(NSString *) kICANotificationTypeDeviceConnectionProgress,
nil];
pb.notificationProc = ICANotificationCallback;
pb.options = NULL;
err = ICARegisterForEventNotification(&pb, NULL);
if (noErr == err)
{
DEBUGLOG(@"@", @"ICA notification callback registered");
}
}
- (void) _uninstallNotification
{
ICARegisterForEventNotificationPB pb;
OSErr err;
memset(&pb, 0, sizeof(ICARegisterForEventNotificationPB));
pb.header.refcon = (unsigned long)self;
pb.objectOfInterest = 0; // all objects
pb.eventsOfInterest = NULL;
pb.notificationProc = ICANotificationCallback;
pb.options = NULL;
err = ICARegisterForEventNotification(&pb, NULL);
if (noErr == err)
{
NSLog(@"ICA notification callback unregistered");
}
}
// ———————————————————————————————————————————————————————————————————————————
// - handleNotification:
// ———————————————————————————————————————————————————————————————————————————
- (void) _gotICANotification:(NSString *) aNotification withDictionary:(NSDictionary *) aDictionary
{
DEBUGLOG( @"%s: %@",__PRETTY_FUNCTION__, aNotification );
DEBUGLOG( @"%@", aDictionary );
id objectID = nil;
// for those hardcoded string constants, probably a == comparison instead of a full isEqualTo: would be sufficient
// trigger node change notifications:
if( [aNotification isEqualTo:(NSString *)kICANotificationTypeDeviceConnectionProgress] ||
[aNotification isEqualTo:(NSString *)kICANotificationTypeDeviceAdded] ||
[aNotification isEqualTo:(NSString *)kICANotificationTypeDeviceRemoved] )
{
// reload the whole device list:
objectID = [aDictionary valueForKey:(NSString *)kICANotificationDeviceListICAObjectKey];
}
if( [aNotification isEqualTo:(NSString *)kICANotificationTypeDeviceConnectionProgress] )
{
@synchronized( _loadingDevices )
{
// it seems the 100% read notification arrives directly AFTER the device added notification.
// prevent overriding the newly added device:
id percentCompleted = [aDictionary valueForKey:(NSString *) kICANotificationPercentDownloadedKey];
if( [percentCompleted intValue] < 100 )
{
// from some point ICAUserdefinedName is also set. reload the node contents?
id deviceID = [aDictionary objectForKey:(NSString *)kICANotificationDeviceICAObjectKey];
NSDictionary *deviceDict =
[NSDictionary dictionaryWithObjectsAndKeys:
percentCompleted, kICANotificationPercentDownloadedKey,
deviceID, @"icao",
nil];
[_loadingDevices setObject:deviceDict forKey:deviceID];
}
else if (objectID != nil)
{
[_loadingDevices removeObjectForKey:objectID];
}
} // end lock
}
if( (!objectID && [aNotification isEqualTo:(NSString *)kICANotificationTypeStoreAdded]) ||
[aNotification isEqualTo:(NSString *)kICANotificationTypeStoreRemoved] )
{
// Reload the device
// TODO: test with devices with two card slots
objectID = [aDictionary valueForKey:(NSString *)kICANotificationDeviceICAObjectKey];
}
if( objectID )
{
// find it in the node tree
IMBLibraryController *libController = [IMBLibraryController sharedLibraryControllerWithMediaType:[self mediaType]];
IMBNode *rootNode = [libController topLevelNodeForParser:self];
[libController reloadNode:rootNode parser:self];
}
}
@end
/******************************** Visuals objects for the browser *******************************/
#pragma mark -
@implementation MTPVisualObject
// ---------------------------------------------------------------------------------------------------------------------
static void ICAThumbnailCallback (ICAHeader* pbHeader)
{
// we use the refcon to get back to the ICAHandler
MTPVisualObject * handler = (MTPVisualObject *)pbHeader->refcon;
if (handler)
[handler _gotThumbnailCallback: (ICACopyObjectThumbnailPB*) pbHeader];
}
- (id) init
{
self = [super init];
if (self != nil) {
// properties of the baseclass -> no need for implementing dealloc
self.imageRepresentationType = IKImageBrowserNSDataRepresentationType;
self.imageVersion = 1;
}
return self;
}
// ---------------------------------------------------------------------------------------------------------------------
- (void) _gotThumbnailCallback: (ICACopyObjectThumbnailPB*)pbPtr
{
self.isLoadingThumbnail = NO;
if (noErr == pbPtr->header.err)
{
// got the thumbnail data, now create an image...
NSData * data = (NSData*)*(pbPtr->thumbnailData);
self.imageRepresentation = data;
[data release];
self.imageVersion = self.imageVersion + 1;
DEBUGLOG( @"Received Thumbnail %@", self );
}
}
- (void) _getThumbnail
{
self.isLoadingThumbnail = YES;
ICACopyObjectThumbnailPB pb = { 0 };
pb.header.refcon = (unsigned long) self;
pb.thumbnailFormat = kICAThumbnailFormatJPEG;
pb.object = (ICAObject)[self.location integerValue];
ICACopyObjectThumbnail(&pb, ICAThumbnailCallback );
DEBUGLOG( @"Loading Thumbnail %@", self );
}
// ---------------------------------------------------------------------------------------------------------------------
- (NSImage *) imageRepresentation
{
if ( !_imageRepresentation && !self.isLoadingThumbnail ) {
[self _getThumbnail];
}
return _imageRepresentation;
}
// UNEXPECTED behavior of the baseclass
- (NSString*) imageRepresentationType
{
return _imageRepresentationType;
}
@end
/***************************** Drag and drop support ***********************************/
#pragma mark -
@implementation IMBMTPDownloadOperation
@synthesize delegate = _delegate;
@synthesize objectsToLoad = _objectsToLoad;
@synthesize downloadFolderPath = _downloadFolderPath;
@synthesize receivedBytes = _receivedBytes;
@synthesize receivedFilePaths = _receivedFilePaths;
- (id) initWithArrayOfObjects:(NSArray *) anArray delegate:(id<IMBMTPDownloadOperationDelegate>) aDelegate
{
self = [super init];
if (self != nil) {
self.delegate = aDelegate;
self.objectsToLoad = anArray;
self.receivedFilePaths = [NSMutableArray arrayWithCapacity:[anArray count]];
}
return self;
}
- (void) dealloc
{
self.objectsToLoad = nil;
self.downloadFolderPath = nil;
self.receivedFilePaths = nil;
[super dealloc];
}
- (void) main
{
NSAutoreleasePool* pool = [[NSAutoreleasePool alloc] init];
FSRef downloadFolder, fileFSRef;
ICADownloadFilePB pb = { 0 };
if( !CFURLGetFSRef ( (CFURLRef)[NSURL fileURLWithPath:self.downloadFolderPath], &downloadFolder ) )
{
NSLog( @"IMBMTPDownloadOperation: Can not download without dastination folder" );
return;
}
for( IMBObject *anObject in self.objectsToLoad )
{
if( [anObject isKindOfClass:[IMBNodeObject class]] )
continue;
if( [self isCancelled] )
break;
OSStatus err;
pb.header.refcon = 0L; // not necessary - sync version;
pb.object = [anObject.location intValue];
pb.dirFSRef = &downloadFolder; // FSRef of destination directiory
pb.fileType = pb.fileCreator = 0; // not used any more
pb.rotationAngle = 0; // Rotation angle in steps of 90 degress.
pb.fileFSRef = &fileFSRef; // we want to know where the file ends up
DEBUGLOG( @"Downloading file %x", pb.object );
err = ICADownloadFile(&pb, nil);
if( !err )
{
NSURL *fileURL = NSMakeCollectable(CFURLCreateFromFSRef( kCFAllocatorDefault, &fileFSRef ));
if( fileURL )
{
[self.receivedFilePaths addObject:[fileURL path]];
[fileURL release];
}
self.receivedBytes += [[anObject.metadata valueForKey:@"isiz"] longLongValue];
if( [(id)self.delegate respondsToSelector:@selector( operationDidDownloadFile: )] )
[self.delegate operationDidDownloadFile:self];
DEBUGLOG( @"Did download file %x to %@", pb.object, [self.receivedFilePaths lastObject] );
}
if( err )
{
DEBUGLOG( @"Failed to download file %x: Error %i", pb.object, err );
if( [(id)self.delegate respondsToSelector:@selector( operation:didReceiveError: )])
[self.delegate operation:self didReceiveError:[NSError errorWithDomain:NSOSStatusErrorDomain code:err userInfo:nil]];
}
}
if( [(id)self.delegate respondsToSelector:@selector( operationDidFinish: )] )
[self.delegate operationDidFinish:self];
[pool drain];
}
- (long long) totalBytes
{
long long resultSize = 0LL;
for( IMBObject *anObject in self.objectsToLoad )
resultSize += [[anObject.metadata valueForKey:@"isiz"] longLongValue];
return resultSize;
}
@end
//----------------------------------------------------------------------------------------------------------------------
#pragma mark -
@implementation IMBMTPObjectPromise
@synthesize bytesTotal = _bytesTotal;
@synthesize bytesDone = _bytesDone;
- (void) loadObjects:(NSArray*)inObjects
{
// Retain self until all download operations have finished. We are going to release self in the
// didFinish: and didReceiveError: delegate messages...
[self retain];
// Show the progress, which is indeterminate for now as we do not know the file sizes yet...
[self prepareProgress];
// Create ONE download operations...
IMBMTPDownloadOperation* op = [[[IMBMTPDownloadOperation alloc] initWithArrayOfObjects:inObjects delegate:self] autorelease];
op.downloadFolderPath = self.destinationDirectoryPath;
[self.downloadOperations addObject:op];
// Get combined file sizes so that the progress bar can be configured...
_totalBytes = [op totalBytes];
// Start downloading...
[[IMBOperationQueue sharedQueue] addOperation:op];
}
//----------------------------------------------------------------------------------------------------------------------
- (IBAction) cancel:(id)inSender
{
NSArray *receivedFiles = nil;
// Cancel outstanding operations...
for (IMBMTPDownloadOperation* op in self.downloadOperations)
{
[op cancel];
receivedFiles = op.receivedFilePaths; // We have only one operation ;-)
}
// Trash any files that we already have...
NSFileManager* mgr = [NSFileManager imb_threadSafeManager];
for (NSString *path in receivedFiles)
{
NSError* error = nil;
[mgr removeItemAtPath:path error:&error];
}
// Cleanup...
[self performSelectorOnMainThread:@selector(_didFinish)
withObject:nil
waitUntilDone:YES
modes:[NSArray arrayWithObject:NSRunLoopCommonModes]];
[self release];
}
//----------------------------------------------------------------------------------------------------------------------
// We received some data, so display the current progress...
- (void) operationDidDownloadFile:(IMBMTPDownloadOperation*)inOperation
{
double fraction = (double)inOperation.receivedBytes / (double)_totalBytes;
[self displayProgress:fraction];
}
// A download has finished. Store the path to the downloaded file. Once all downloads are complete, we can hide
// the progress UI, Notify the delegate and release self...
- (void) operationDidFinish:(IMBMTPDownloadOperation*)inOperation
{
// [self.localFiles addObject:inOperation.localPath];
_objectCountLoaded++;
if (_objectCountLoaded >= _objectCountTotal)
{
NSLog(@"%s",__FUNCTION__);
[self performSelectorOnMainThread:@selector(_didFinish)
withObject:nil
waitUntilDone:YES
modes:[NSArray arrayWithObject:NSRunLoopCommonModes]];
[self release];
}
}
// If an error has occured in one of the downloads, then store the error instead of the file path, but everything
// else is the same as in the previous method...
- (void) operation:(IMBMTPDownloadOperation*)inOperation didReceiveError:(NSError *) anError
{
// [self.localFiles addObject:anError];
self.error = anError;
_objectCountLoaded++;
if (_objectCountLoaded >= _objectCountTotal)
{
NSLog(@"%s",__FUNCTION__);
[self performSelectorOnMainThread:@selector(_didFinish)
withObject:nil
waitUntilDone:YES
modes:[NSArray arrayWithObject:NSRunLoopCommonModes]];
[self release];
}
}
@end