forked from cellularmitosis/ADC-reference-library-2009-july
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlisting27.html
executable file
·1136 lines (936 loc) · 41.4 KB
/
listing27.html
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
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/1999/REC-html401-19991224/loose.dtd">
<html>
<head>
<!-- BEGIN META TAG INFO -->
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
<link rel="home" href="http://developer.apple.com/">
<link rel="find" href="http://developer.apple.com/search/">
<link rel="stylesheet" type="text/css" href="../../documentation/css/adcstyle.css" title="fonts">
<script language="JavaScript" src="../../documentation/js/adc.js" type="text/javascript"></script>
<!-- END META TAG INFO -->
<!-- BEGIN TITLE -->
<title>MyPhoto - /Step8/ICAHandler.m</title>
<!-- END TITLE -->
<script language="JavaScript">
function JumpToNewPage() {
window.location=document.scpopupmenu.gotop.value;
return true;
}
</script>
</head>
<!-- BEGIN BODY OPEN -->
<body>
<!--END BODY OPEN -->
<!-- START CENTER OPEN -->
<center>
<!-- END CENTER OPEN -->
<!-- BEGIN LOGO AND SEARCH -->
<!--#include virtual="/includes/adcnavbar"-->
<!-- END LOGO AND SEARCH -->
<!-- START BREADCRUMB -->
<div id="breadcrumb">
<table width="680" border="0" cellpadding="0" cellspacing="0">
<tr>
<td scope="row"><img width="340" height="10" src="images/1dot.gif" alt=""></td>
<td><img width="340" height="10" src="images/1dot.gif" alt=""></td>
</tr>
<tr valign="middle">
<td align="left" colspan="2">
<a href="http://developer.apple.com/">ADC Home</a> > <a href="../../referencelibrary/index.html">Reference Library</a> > <a href="../../samplecode/index.html">Sample Code</a> > <a href="../../samplecode/GraphicsImaging/index.html">Graphics & Imaging</a> > <a href="../../samplecode/GraphicsImaging/idxCocoa-date.html">Cocoa</a> > <A HREF="javascript:location.replace('index.html');">MyPhoto</A> >
</td>
</tr>
<tr>
<td colspan="2" scope="row"><img width="680" height="35" src="images/1dot.gif" alt=""></td>
</tr>
</table>
</div>
<!-- END BREADCRUMB -->
<!-- START MAIN CONTENT -->
<!-- START TITLE GRAPHIC AND INTRO-->
<table width="680" border="0" cellpadding="0" cellspacing="0">
<tr align="left" valign="top">
<td><h1><div id="pagehead">MyPhoto</div></h1></td>
</tr>
</table>
<!-- END TITLE GRAPHIC AND INTRO -->
<!-- START WIDE COLUMN -->
<table width="680" border="0" cellpadding="0" cellspacing="0">
<tr align="left" valign="top">
<td id="scdetails">
<h2>/Step8/ICAHandler.m</h2>
<form name="scpopupmenu" onSubmit="return false;" method=post>
<p><strong>View Source Code:</strong>
<select name="gotop" onChange="JumpToNewPage();" style="width:340px"><option selected value="ingnore">Select File</option>
<option value="listing1.html">/Step1/ICAHandler.h</option>
<option value="listing2.html">/Step1/ICAHandler.m</option>
<option value="listing3.html">/Step1/main.m</option>
<option value="listing4.html">/Step2/ICAHandler.h</option>
<option value="listing5.html">/Step2/ICAHandler.m</option>
<option value="listing6.html">/Step2/main.m</option>
<option value="listing7.html">/Step3/ICAHandler.h</option>
<option value="listing8.html">/Step3/ICAHandler.m</option>
<option value="listing9.html">/Step3/main.m</option>
<option value="listing10.html">/Step4/ICAHandler.h</option>
<option value="listing11.html">/Step4/ICAHandler.m</option>
<option value="listing12.html">/Step4/main.m</option>
<option value="listing13.html">/Step5/ICAHandler.h</option>
<option value="listing14.html">/Step5/ICAHandler.m</option>
<option value="listing15.html">/Step5/main.m</option>
<option value="listing16.html">/Step6/ICAHandler.h</option>
<option value="listing17.html">/Step6/ICAHandler.m</option>
<option value="listing18.html">/Step6/main.m</option>
<option value="listing19.html">/Step7/ICAHandler.h</option>
<option value="listing20.html">/Step7/ICAHandler.m</option>
<option value="listing21.html">/Step7/ICTakePictureHandler.h</option>
<option value="listing22.html">/Step7/ICTakePictureHandler.m</option>
<option value="listing23.html">/Step7/main.m</option>
<option value="listing24.html">/Step8/FileBrowserView.h</option>
<option value="listing25.html">/Step8/FileBrowserView.m</option>
<option value="listing26.html">/Step8/ICAHandler.h</option>
<option value="listing27.html">/Step8/ICAHandler.m</option>
<option value="listing28.html">/Step8/ICTakePictureHandler.h</option>
<option value="listing29.html">/Step8/ICTakePictureHandler.m</option>
<option value="listing30.html">/Step8/ImageView.h</option>
<option value="listing31.html">/Step8/ImageView.m</option>
<option value="listing32.html">/Step8/main.m</option></select>
</p>
</form>
<p><strong><a href="MyPhoto.zip">Download Sample</a></strong> (“MyPhoto.zip”, 1021.7K)<BR>
<strong><a href="MyPhoto.dmg">Download Sample</a></strong> (“MyPhoto.dmg”, 1.30M)</p>
<!--
<p><strong><a href="#">Download Sample</a></strong> (“filename.sit”, 500K)</p>
-->
</td>
</tr>
<tr>
<td scope="row"><img width="680" height="10" src="images/1dot.gif" alt=""><br>
<img height="1" width="680" src="images/1dot_919699.gif" alt=""><br>
<img width="680" height="20" src="images/1dot.gif" alt=""></td>
</tr>
<tr>
<td scope="row">
<!--googleon: index -->
<pre class="sourcecodebox">/*
File: ICAHandler.m
Abstract: ICAHandler
Version: 1.0
Disclaimer: IMPORTANT: This Apple software is supplied to you by Apple
Computer, Inc. ("Apple") in consideration of your agreement to the
following terms, and your use, installation, modification or
redistribution of this Apple software constitutes acceptance of these
terms. If you do not agree with these terms, please do not use,
install, modify or redistribute this Apple software.
In consideration of your agreement to abide by the following terms, and
subject to these terms, Apple grants you a personal, non-exclusive
license, under Apple's copyrights in this original Apple software (the
"Apple Software"), to use, reproduce, modify and redistribute the Apple
Software, with or without modifications, in source and/or binary forms;
provided that if you redistribute the Apple Software in its entirety and
without modifications, you must retain this notice and the following
text and disclaimers in all such redistributions of the Apple Software.
Neither the name, trademarks, service marks or logos of Apple Computer,
Inc. may be used to endorse or promote products derived from the Apple
Software without specific prior written permission from Apple. Except
as expressly stated in this notice, no other rights or licenses, express
or implied, are granted by Apple herein, including but not limited to
any patent rights that may be infringed by your derivative works or by
other works in which the Apple Software may be incorporated.
The Apple Software is provided by Apple on an "AS IS" basis. APPLE
MAKES NO WARRANTIES, EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION
THE IMPLIED WARRANTIES OF NON-INFRINGEMENT, MERCHANTABILITY AND FITNESS
FOR A PARTICULAR PURPOSE, REGARDING THE APPLE SOFTWARE OR ITS USE AND
OPERATION ALONE OR IN COMBINATION WITH YOUR PRODUCTS.
IN NO EVENT SHALL APPLE BE LIABLE FOR ANY SPECIAL, INDIRECT, INCIDENTAL
OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
INTERRUPTION) ARISING IN ANY WAY OUT OF THE USE, REPRODUCTION,
MODIFICATION AND/OR DISTRIBUTION OF THE APPLE SOFTWARE, HOWEVER CAUSED
AND WHETHER UNDER THEORY OF CONTRACT, TORT (INCLUDING NEGLIGENCE),
STRICT LIABILITY OR OTHERWISE, EVEN IF APPLE HAS BEEN ADVISED OF THE
POSSIBILITY OF SUCH DAMAGE.
Copyright © 2005 Apple Computer, Inc., All Rights Reserved
*/
#import "ICTakePictureHandler.h"
#import "FileBrowserView.h"
#import "ICAHandler.h"
@implementation ICAHandler
// ---------------------------------------------------------------------------------------------------------------------
- (BOOL)applicationShouldTerminateAfterLastWindowClosed: (NSApplication *)sender
{
// return YES to allow the application to terminate when the user closes the last window the application has open
return YES;
}
// ---------------------------------------------------------------------------------------------------------------------
- (void)awakeFromNib
{
[mTabView selectTabViewItemAtIndex: 0];
// mDeviceNames - set it to NULL - it be set to the 'devices' array (device list property dictionary)
mDeviceListArray = NULL;
// when double-clicking on a device name, send browseDevice:
[mTableView setDoubleAction: @selector(browseDevice:)];
// time to register for Image Capture notifications
[self registerForDeviceNotification];
// find out what devices are connected
[self getDevices];
}
// ---------------------------------------------------------------------------------------------------------------------
- (void)dealloc
{
[mDeviceDictionary release];
[mDeviceListArray release];
[mImageThumbnails release];
[mImageMetaData release];
[mRowsToImport release];
[mDownloadFolderPath release];
[super dealloc];
}
// split view handling: don't change when resizing window, don't go below 150 or above 400.
// ---------------------------------------------------------------------------------------------------------------------
- (float)splitView:(NSSplitView *)sender
constrainMinCoordinate:(float)proposedCoord
ofSubviewAt:(int)offset
{
if (offset == 0)
return 150.;
else
return proposedCoord;
}
// ---------------------------------------------------------------------------------------------------------------------
- (float)splitView:(NSSplitView *)sender
constrainMaxCoordinate:(float)proposedCoord
ofSubviewAt:(int)offset
{
if (offset == 0)
return 400.;
else
return proposedCoord;
}
// ---------------------------------------------------------------------------------------------------------------------
- (void)splitView: (NSSplitView *)sender
resizeSubviewsWithOldSize: (NSSize)oldSize
{
NSView * view = [[sender subviews] objectAtIndex: 0];
NSRect oldFrame = [view frame];
[sender adjustSubviews];
NSRect newFrame = [view frame];
[view setFrameSize: NSMakeSize(oldFrame.size.width, newFrame.size.height)];
}
// NSTableDataSource
// ---------------------------------------------------------------------------------------------------------------------
- (int)numberOfRowsInTableView:(NSTableView *)tableView
{
if (mTableView == tableView)
{
// return "device count" plus 2 - at lease 3 rows...
return MAX(1, [mDeviceListArray count]) + 2;
} else
{
// return the number of images for in the mDeviceDictionary
return [[mDeviceDictionary objectForKey: @"data"] count];
}
}
// ---------------------------------------------------------------------------------------------------------------------
- (id)mainTableViewValueForIdentifier: (NSString *)identifier
row: (int)row
{
// for the main table view
id result = NULL;
int count = [mDeviceListArray count];
if (row < count)
{
if ([identifier isEqualToString: @"name"]) // name
result = [[mDeviceListArray objectAtIndex: row] objectForKey: @"ifil"];
else if ([identifier isEqualToString: @"icon"]) // icon
result = [self imageNamed: @"icon_camera"];
} else
{
switch (row - (count ? (count-1) : count))
{
case 0:
// if no camera is connected...
if ([identifier isEqualToString: @"name"]) // name
result = @"No camera connected";
else if ([identifier isEqualToString: @"icon"]) // icon
result = [self imageNamed: @"icon_no_camera"];
break;
case 1:
// allways have a 'Library' item
if ([identifier isEqualToString: @"name"])
result = @"Library";
else
result = [self imageNamed: @"icon_library"];
break;
case 2:
// allways have a 'Last Roll' item
if ([identifier isEqualToString: @"name"])
result = @"Last Roll";
else
result = [self imageNamed: @"icon_roll"];
break;
}
}
return result;
}
// ---------------------------------------------------------------------------------------------------------------------
- (id)browserTableViewValueForIdentifier: (NSString *)identifier
row: (int)row
{
// for the image table view
id result = NULL;
NSArray * imageArray = [mDeviceDictionary objectForKey: @"data"];
if ([identifier isEqualToString: @"row"]) // row
{
result = [NSString stringWithFormat: @"%d", row];
} else if ([identifier isEqualToString: @"thumbnail"]) // thumbnail
{
if (row < [mImageThumbnails count])
result = [mImageThumbnails objectAtIndex: row];
} else
{
// easy matching - in nib file, the table colum identifier are set to match keys in imageArray
result = [[imageArray objectAtIndex: row] objectForKey: identifier];
// if it was not int the imageArray, try the mImageMetaData
// same easy matching - in nib file, the table colum identifier are set to match keys in imageArray
if ((NULL == result) && (row < [mImageMetaData count]))
result = [[mImageMetaData objectAtIndex: row] objectForKey: identifier];
}
return result;
}
// ---------------------------------------------------------------------------------------------------------------------
- (id)tableView: (NSTableView *)tableView
objectValueForTableColumn: (NSTableColumn *)tableColumn
row: (int)row
{
NSString * identifier = [tableColumn identifier];
id result = NULL;
if (mTableView == tableView)
{
// for the main table view
result = [self mainTableViewValueForIdentifier: identifier
row: row];
} else
{
// for the image table view
result = [self browserTableViewValueForIdentifier: identifier
row: row];
}
return result;
}
@end
#pragma mark -
@implementation ICAHandler(Step1)
// Image Capture ...
// ---------------------------------------------------------------------------------------------------------------------
- (void)getDevices
{
OSErr err;
ICAGetDeviceListPB pb = {};
// call ICAGetDeviceList (synchronous: callback proc is NULL)
err = ICAGetDeviceList(&pb, NULL);
if (noErr == err)
{
// save the device list object for later use
mDeviceList = pb.object;
// get more information about the device list
[self getDeviceListPropertyDictionary];
}
}
// ---------------------------------------------------------------------------------------------------------------------
- (void)getDeviceListPropertyDictionary
{
OSErr err;
ICACopyObjectPropertyDictionaryPB pb = {};
NSDictionary* devDict;
// get the property dictionary for the mDeviceList ICAObject
pb.object = mDeviceList;
pb.theDict = (CFDictionaryRef*)&devDict;
err = ICACopyObjectPropertyDictionary(&pb, NULL);
if (noErr == err)
{
// NSLog(@"%@", devDict);
// note: this dictionary contains an array "devices"
// the "devices" array contains a dictionary for each connected device
mDeviceListArray = [[devDict objectForKey: @"devices"] retain];
// update the table view
[mTableView reloadData];
[devDict release];
// update UI
[self tableViewSelectionDidChange: [NSNotification notificationWithName: NSTableViewSelectionDidChangeNotification
object: mTableView]];
}
}
@end
#pragma mark -
@implementation ICAHandler(Step2)
// ---------------------------------------------------------------------------------------------------------------------
static void DeviceNotificationCallback(ICAHeader* pbPtr)
{
// DeviceNotificationCallback gets called when there's an Image Capture event notification
// we use the refcon to get back to the ICAHandler
ICAHandler * handler = (ICAHandler*)pbPtr->refcon;
if (handler)
[handler deviceNotification: (ICAExtendedRegisterEventNotificationPB*)pbPtr];
}
// ---------------------------------------------------------------------------------------------------------------------
- (void)registerForDeviceNotification
{
OSErr err;
ICARegisterEventNotificationPB pb = {};
pb.header.refcon = (UInt32)self; // set the refcon so we can dispatch back to 'self'
pb.notifyProc = DeviceNotificationCallback; // global callback proc
pb.object = 0; // 0 = all objects
pb.notifyType = 0; // 0 = all types
err = ICARegisterEventNotification(&pb, NULL);
// ... error handling ...
}
// ---------------------------------------------------------------------------------------------------------------------
- (void)deviceNotification: (ICAExtendedRegisterEventNotificationPB*)pbPtr
{
if (kExtendedNotificationPB == pbPtr->extd)
{
switch (pbPtr->eventType)
{
case kICAEventDeviceRemoved: // device removed : update device
[self getDeviceListPropertyDictionary];
break;
case kICAEventDeviceAdded: // device added : update device
[self getDeviceListPropertyDictionary];
break;
default:
break;
}
}
}
@end
#pragma mark -
@implementation ICAHandler(Step3)
// ---------------------------------------------------------------------------------------------------------------------
- (void)tableViewSelectionDidChange: (NSNotification *)notification
{
// if selection in mTableView did changed
if (mTableView == [notification object])
{
[mTabView selectTabViewItemAtIndex: 0];
int selectedRow = [mTableView selectedRow];
[mThumbnailSlider setHidden: YES];
[mNextImage setHidden: YES];
[mPrevImage setHidden: YES];
if (-1 != selectedRow)
{
if (selectedRow < [mDeviceListArray count])
{
// conneceted device selected...
[self deviceSelected: [mDeviceListArray objectAtIndex: selectedRow]];
} else
{
// else - switch to 'browser' view
[mTabView selectTabViewItemAtIndex: 2];
[mThumbnailSlider setHidden: NO];
[mFileBrowserView updateFiles];
}
} else
{
// nothing selected ...
[mImportButton setHidden: YES];
[mImportText setHidden: YES];
[mCameraImage setHidden: YES];
}
} else
{
switch ([mBrowseTableView numberOfSelectedRows])
{
case 0:
// update info text...
[mBrowseImportText setStringValue: @"no image selected"];
[mBrowseImportButton setEnabled: NO];
break;
default:
// update info text - display number of selected images / total number
[mBrowseImportText setStringValue: [NSString stringWithFormat: @"%d of %d images selected",
[mBrowseTableView numberOfSelectedRows],
[[mDeviceDictionary objectForKey: @"data"] count]]];
[mBrowseImportButton setEnabled: YES];
break;
}
}
}
// ---------------------------------------------------------------------------------------------------------------------
static void GotDeviceDictionary(ICAHeader* pbPtr)
{
// we use the refcon to get back to the ICAHandler
ICAHandler * handler = (ICAHandler*)pbPtr->refcon;
if (handler)
[handler gotDeviceDictionary: (ICACopyObjectPropertyDictionaryPB*)pbPtr];
}
// ---------------------------------------------------------------------------------------------------------------------
- (void)gotDeviceDictionary: (ICACopyObjectPropertyDictionaryPB*)pbPtr
{
if (noErr == pbPtr->header.err)
{
// NSLog(@"mDeviceDictionary = %@", mDeviceDictionary);
// If we want to handle the 'take-picture' - make sure that the camera supports it...
// check 'capa' array - find out if camera supports take picture
NSArray * capabilities = [mDeviceDictionary objectForKey: @"capa"];
if ([capabilities containsObject: [NSNumber numberWithUnsignedLong: kICAMessageCameraCaptureNewImage]])
[mTakePictureButton setHidden: NO];
else
[mTakePictureButton setHidden: YES];
NSArray * imageArray = [mDeviceDictionary objectForKey: @"data"];
// note: the 'data' array contains the flattened-out array of all images/movies/music files
if ([imageArray count])
{
// we have at least one file on the device
[mImportText setStringValue: [NSString stringWithFormat: @"Ready to import %d images.", [imageArray count]]];
[mImportText setHidden: NO];
[mImportButton setHidden: NO];
// get the camera thumbnail
[self getCameraThumbnail];
} else
{
// devices contains no files...
[mImportText setStringValue: @"No images."];
[mImportText setHidden: NO];
[mImportButton setHidden: YES];
[mCameraImage setHidden: YES];
}
}
}
// ---------------------------------------------------------------------------------------------------------------------
- (void)deviceSelected: (NSDictionary*)dictionary
{
OSErr err;
ICACopyObjectPropertyDictionaryPB pb = {};
// release the device dictionary
[mDeviceDictionary release];
// ... and ask for the new one
pb.header.refcon = (UInt32)self;
pb.object = (ICAObject)[[dictionary objectForKey: @"icao"] unsignedLongValue];
pb.theDict = (CFDictionaryRef*)&mDeviceDictionary;
// asynchronous call - callback proc will get called when call completes
err = ICACopyObjectPropertyDictionary(&pb, GotDeviceDictionary);
// ... error handling ...
}
// ---------------------------------------------------------------------------------------------------------------------
static void GotCameraThumbnailCallback (ICAHeader* pbHeader)
{
// we use the refcon to get back to the ICAHandler
ICAHandler * handler = (ICAHandler*)pbHeader->refcon;
if (handler)
[handler gotCameraThumbnailCallback: (ICACopyObjectThumbnailPB*) pbHeader];
}
// ---------------------------------------------------------------------------------------------------------------------
- (void) gotCameraThumbnailCallback: (ICACopyObjectThumbnailPB*)pbPtr
{
if (noErr == pbPtr->header.err)
{
// got the thumbnail data, now create an image...
NSData * data = (NSData*)*(pbPtr->thumbnailData);
NSImage* image = [[NSImage alloc] initWithData: data];
// show NSImageView and set image
[mCameraImage setHidden: NO];
[mCameraImage setImage: image];
[image release];
[data release];
}
}
// ---------------------------------------------------------------------------------------------------------------------
- (void) getCameraThumbnail
{
OSErr err;
ICACopyObjectThumbnailPB pb = {};
pb.header.refcon = (UInt32)self;
// for Image Capture devices: best to use 'TIFF' - (will have alpha channel / mask)
pb.thumbnailFormat = kICAThumbnailFormatTIFF;
// use the ICAObject out of the mDeviceDictionary
pb.object = (ICAObject)[[mDeviceDictionary objectForKey: @"icao"] unsignedLongValue];
// asynchronous call - callback proc will get called when call completes
err = ICACopyObjectThumbnail(&pb, GotCameraThumbnailCallback);
// ... error handling ...
}
@end
#pragma mark -
@implementation ICAHandler(Step4)
// ---------------------------------------------------------------------------------------------------------------------
- (void)browseDevice: (id)sender
{
// when user double-clicks on a camera name - switch to second tab view item
if ([mDeviceListArray count])
{
[mTabView selectTabViewItemAtIndex: 1];
int selectedRow = [mTableView selectedRow];
if (-1 != selectedRow)
{
// if there's a device selected, get image thumbnails and meta data...
// mImageThumbnails is an NSMutableArray that contains thumbnails (NSImages) for all images on device
[mImageThumbnails release];
mImageThumbnails = [[NSMutableArray alloc] init];
// mImageMetaData is an NSMutableArray that contains meta-data for all images on device
[mImageMetaData release];
mImageMetaData = [[NSMutableArray alloc] init];
[self fetchImageThumbnails];
}
}
}
@end
#pragma mark -
@implementation ICAHandler(Step5)
// ---------------------------------------------------------------------------------------------------------------------
static void FetchImageThumbnailCallback (ICAHeader* pbHeader)
{
// we use the refcon to get back to the ICAHandler
ICAHandler * obj = (ICAHandler*)pbHeader->refcon;
if (obj)
{
[obj fetchImageThumbnailCallback: (ICACopyObjectThumbnailPB*) pbHeader];
}
}
// ---------------------------------------------------------------------------------------------------------------------
- (void) fetchImageThumbnailCallback: (ICACopyObjectThumbnailPB*)pbPtr
{
NSImage * image;
if (noErr != pbPtr->header.err)
{
// if we run into an error: add generic icon
image = [[NSWorkspace sharedWorkspace] iconForFileType: @"JPEG"];
[mImageThumbnails addObject: image];
} else
{
// got the thumbnail data, now create an image...
NSData * data = (NSData*)*(pbPtr->thumbnailData);
NSImage* image = [[NSImage alloc] initWithData: data];
// add the image to the mImageThumbnails array
[mImageThumbnails addObject: image];
[image release];
[data release];
}
// update the mBrowseTableView
[mBrowseTableView reloadData];
// get the next thumbnail - one at a time...
[self fetchImageThumbnails];
}
// ---------------------------------------------------------------------------------------------------------------------
- (void) fetchImageThumbnails
{
// thumbnailCount = number of thumbnails that we already got
int thumbnailCount = [mImageThumbnails count];
NSArray * imageArray = [mDeviceDictionary objectForKey: @"data"];
// did we fetch all thumbnails ?
if (thumbnailCount < [imageArray count])
{
NSDictionary * imageDictionary = [imageArray objectAtIndex: thumbnailCount];
OSErr err;
ICACopyObjectThumbnailPB pb = {};
pb.header.refcon = (UInt32)self;
// for images: best to use 'JPEF' - alpha channel / mask not needed + JPEG is compressed
pb.thumbnailFormat = kICAThumbnailFormatJPEG;
// use the ICAObject out of the current image
pb.object = (ICAObject)[[imageDictionary objectForKey: @"icao"] unsignedLongValue];
// asynchronous call - callback proc will get called when call completes
err = ICACopyObjectThumbnail(&pb, FetchImageThumbnailCallback);
// ... error handling ...
} else
{
// here we're done fetching thumbnails - get the image meta-data
[self fetchImageMetaData];
}
}
// ---------------------------------------------------------------------------------------------------------------------
static void FetchImageMetaDataCallback (ICAHeader* pbHeader)
{
// we use the refcon to get back to the ICAHandler
ICAHandler * obj = (ICAHandler*)pbHeader->refcon;
if (obj)
{
[obj fetchImageMetaDataCallback: (ICACopyObjectPropertyDictionaryPB*) pbHeader];
}
}
// ---------------------------------------------------------------------------------------------------------------------
- (void) fetchImageMetaDataCallback: (ICACopyObjectPropertyDictionaryPB*)pbPtr
{
if (noErr != pbPtr->header.err)
{
// handle error: add empty dictionary
[mImageMetaData addObject: [NSDictionary dictionaryWithObjectsAndKeys: NULL]];
} else
{
// add the dictionary to the mImageMetaData array
[mImageMetaData addObject: mImageDictionary];
[mImageDictionary release];
mImageDictionary = NULL;
}
// update the mBrowseTableView
[mBrowseTableView reloadData];
// get next meta-data - one at a time...
[self fetchImageMetaData];
}
// ---------------------------------------------------------------------------------------------------------------------
- (void) fetchImageMetaData
{
// metaDataCount = number of meta-data dictionaries that we already got
int metaDataCount = [mImageMetaData count];
NSArray * imageArray = [mDeviceDictionary objectForKey: @"data"];
// are we done ?
if (metaDataCount < [imageArray count])
{
NSDictionary * imageDictionary = [imageArray objectAtIndex: metaDataCount];
OSErr err;
ICACopyObjectPropertyDictionaryPB pb = {};
pb.header.refcon = (UInt32)self;
pb.theDict = (CFDictionaryRef*)&mImageDictionary;
pb.object = (ICAObject)[[imageDictionary objectForKey: @"icao"] unsignedLongValue];
// asynchronous call - callback proc will get called when call completes
err = ICACopyObjectPropertyDictionary(&pb, FetchImageMetaDataCallback);
// ... error handling ...
} else
{
// here we're done fetching meta-data
}
}
@end
#pragma mark -
@implementation ICAHandler(Step6)
// ---------------------------------------------------------------------------------------------------------------------
- (NSImage *)imageNamed: (NSString *) name
{
// load a tiff file from our Resource folder
NSString * path = [[NSBundle mainBundle] pathForResource: name
ofType: @"tiff"];
NSImage * image = [[[NSImage alloc] initWithContentsOfFile: path] autorelease];
return image;
}
// ---------------------------------------------------------------------------------------------------------------------
- (void) createDownloadFolder
{
// use ~/Pictures/myPhoto/ as the default download folder...
if (NULL == mDownloadFolderPath)
{
NSString * defaultDownloadFolder = @"~/Pictures/myPhoto/";
mDownloadFolderPath = [[defaultDownloadFolder stringByExpandingTildeInPath] retain];
// if it does not exist...
if (![[NSFileManager defaultManager] fileExistsAtPath: mDownloadFolderPath])
{
// ... create a new one
BOOL didCreateFolder = [[NSFileManager defaultManager] createDirectoryAtPath: mDownloadFolderPath
attributes: NULL];
if (!didCreateFolder)
{
// ... error handling ...
}
}
}
}
// ---------------------------------------------------------------------------------------------------------------------
static void ICADownloadFileCallback (ICAHeader* pbHeader)
{
// we use the refcon to get back to the ICAHandler
ICAHandler * obj = (ICAHandler*)pbHeader->refcon;
if (obj)
{
[obj downloadFileCallback: (ICADownloadFilePB*) pbHeader];
}
}
// ---------------------------------------------------------------------------------------------------------------------
- (void) downloadFileCallback: (ICADownloadFilePB*)pbPtr
{
if (noErr != pbPtr->header.err)
{
// ... error handling ...
} else
{
// file was downloaded...
UInt8 * path[512];
OSErr err = FSRefMakePath(&mDownloadedFile, (UInt8 *)path, 512);
if (noErr != err)
{
// ... error handling ...
} else
{
// just log the file name
NSLog(@"downloaded: %@", [NSString stringWithUTF8String: (const char *)path]);
}
}
// update progress indicator and info text
[mProgressDL setDoubleValue: [mProgressDL doubleValue] + 1.];
[mProgressTextDL setStringValue: [NSString stringWithFormat:
@"%.0f remaining", [mProgressDL maxValue] - [mProgressDL doubleValue]]];
// download next file - one at a time...
[self importImages];
}
// ---------------------------------------------------------------------------------------------------------------------
- (void) downloadFileAtIndex: (UInt32)index
{
OSErr err;
ICADownloadFilePB pb = {};
NSArray * imageArray = [mDeviceDictionary objectForKey: @"data"];
NSDictionary * imageDictionary = [imageArray objectAtIndex: index];
FSRef downloadFolder;
// create a FSRef for our mDownloadFolderPath
FSPathMakeRef((const UInt8 *)[mDownloadFolderPath fileSystemRepresentation], &downloadFolder, NULL);
pb.header.refcon = (UInt32)self;
pb.object = (ICAObject)[[imageDictionary objectForKey: @"icao"] unsignedLongValue];
// as flags: create a custom icon ('icns' resource - so that Finder displays the image nicely)
// adjust modification date: use the EXIF information of camera images and adjust the image mod date
pb.flags = kCreateCustomIcon | kAdjustCreationDate;
// set the download folder FSRef
pb.dirFSRef = &downloadFolder;
// pass mDownloadedFile - that way we'll get the actual filename (Image Capture may add a ' 1' or ' 2' if there's
// a file naming conflict
pb.fileFSRef = &mDownloadedFile;
// asynchronous call - callback proc will get called when call completes
err = ICADownloadFile(&pb, ICADownloadFileCallback);
// ... error handling ...
}
// ---------------------------------------------------------------------------------------------------------------------
- (void)importImages
{
// mRowsToImport contains indexes of all outstanding images to download
if ([mRowsToImport count])
{
// if we are not done: get the next index, remove it from mRowsToImport, and download...
UInt32 firstIndex = [mRowsToImport firstIndex];
[mRowsToImport removeIndex: firstIndex];
[self downloadFileAtIndex: firstIndex];
} else
{
// we are done - hide progress information
[mProgressDL setHidden: YES];
[mProgressTextDL setHidden: YES];
[mImportButton setEnabled: YES];
[mBrowseImportButton setEnabled: YES];
}
}
// ---------------------------------------------------------------------------------------------------------------------
- (void)setupProgressHandling
{
// update and show progress information
// progress indicator
[mProgressDL setMinValue: 0.];
[mProgressDL setMaxValue: [mRowsToImport count]];
[mProgressDL setDoubleValue: 0.];
// and text
[mProgressTextDL setStringValue: [NSString stringWithFormat:
@"%.0f remaining", [mProgressDL maxValue] - [mProgressDL doubleValue]]];
[mProgressDL setHidden: NO];
[mProgressTextDL setHidden: NO];
}
// ---------------------------------------------------------------------------------------------------------------------
- (IBAction)importAll: (id)sender
{
// make sure we have a download folder....
[self createDownloadFolder];
if (mDownloadFolderPath)
{
NSArray * imageArray = [mDeviceDictionary objectForKey: @"data"];