-
Notifications
You must be signed in to change notification settings - Fork 1
/
PhotoboothAppDelegate.m
811 lines (701 loc) · 24.5 KB
/
PhotoboothAppDelegate.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
//
// PhotoboothAppDelegate.m
// Photobooth
//
// Created by Albert J. Wong on 7/20/10.
// Copyright 2010. All rights reserved.
//
#import "PhotoboothAppDelegate.h"
#import <Quartz/Quartz.h>
#import <ImageCaptureCore/ImageCaptureCore.h>
#import <ApplicationServices/ApplicationServices.h>
@implementation PhotoboothAppDelegate
@synthesize window;
@synthesize fullscreenWindow;
@synthesize countdownWindow;
@synthesize countdownView;
@synthesize countdownStatus;
@synthesize overlayImage;
@synthesize overlayView;
@synthesize mainImage;
@synthesize browserView;
@synthesize statusText;
@synthesize printButton;
@synthesize preferencesSheet;
@synthesize selectedImagesFolder;
@synthesize selectedSmilesFolder;
@synthesize setImagesFolderButton;
@synthesize setSmilesFolderButton;
@synthesize deviceBrowserView;
@synthesize imagesFolder;
@synthesize smilesFolder;
@synthesize defaultImage;
@synthesize countdownImage0;
@synthesize countdownImage1;
@synthesize countdownImage2;
@synthesize countdownImage3;
@synthesize countdownImageN1;
@synthesize imageList;
@synthesize smileList;
@synthesize beepSound;
NSString* kPrintInfoPref = @"UserPrintInfoRaw";
NSString* kImageFolderPref = @"UserImageDirectory";
NSString* kSmilesFolderPref = @"UserSmileDirectory";
- (BOOL)applicationShouldTerminateAfterLastWindowClosed: (NSApplication *)sender
{
// terminate when last window was closed
return YES;
}
- (id)init {
if (self = [super init])
{
self.defaultImage =
[[ImageInfo alloc] initWithPath:
[[NSBundle mainBundle] pathForResource:@"camera" ofType:@"png"]];
self.countdownImage0 =
[[ImageInfo alloc] initWithPath:
[[NSBundle mainBundle] pathForResource:@"film-leader-0" ofType:@"png"]];
self.countdownImage1 =
[[ImageInfo alloc] initWithPath:
[[NSBundle mainBundle] pathForResource:@"film-leader-1" ofType:@"png"]];
self.countdownImage2 =
[[ImageInfo alloc] initWithPath:
[[NSBundle mainBundle] pathForResource:@"film-leader-2" ofType:@"png"]];
self.countdownImage3 =
[[ImageInfo alloc] initWithPath:
[[NSBundle mainBundle] pathForResource:@"film-leader-3" ofType:@"png"]];
self.countdownImageN1 =
[[ImageInfo alloc] initWithPath:
[[NSBundle mainBundle] pathForResource:@"film-leader--1" ofType:@"png"]];
imageList = [[NSMutableArray alloc] init];
smileList = [[NSMutableArray alloc] init];
printSheetDidEndSelector =
@selector(printSheetDidEnd:returnCode:contextInfo:);
beepSound = [NSSound soundNamed:@"beep.wav"];
}
return self;
}
- (void)awakeFromNib {
// Load preferences.
preferences = [NSUserDefaults standardUserDefaults];
NSData* defaultPrintInfo = [preferences dataForKey:kPrintInfoPref];
NSString* defaultImageFolder = [preferences objectForKey:kImageFolderPref];
NSString* defaultSmilesFolder = [preferences objectForKey:kSmilesFolderPref];
if (defaultPrintInfo) {
printInfo =
[NSUnarchiver unarchiveObjectWithData:defaultPrintInfo];
} else {
printInfo = nil;
}
srand(time(NULL));
if (printInfo == nil) {
printInfo = [NSPrintInfo sharedPrintInfo];
[self resetPrintInfo:nil];
} else {
[NSPrintInfo setSharedPrintInfo:printInfo];
}
if (defaultImageFolder == nil) {
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSPicturesDirectory,
NSUserDomainMask, YES);
defaultImageFolder = [paths objectAtIndex:0];
}
if (defaultSmilesFolder == nil) {
defaultSmilesFolder = @"";
}
// Setup the UI.
[self setImagesFolder:defaultImageFolder];
[self setSmilesFolder:defaultSmilesFolder];
[self initializeMainImage];
[self setUninitialized];
[self createCountdownUI];
}
- (void)applicationDidFinishLaunching:(NSNotification*)notification
{
// Setup cameras.
cameras = [[NSMutableArray alloc] initWithCapacity:0];
// Get an instance of ICDeviceBrowser
deviceBrowser = [[ICDeviceBrowser alloc] init];
// Assign a delegate
deviceBrowser.delegate = self;
// We only want local caneras.
deviceBrowser.browsedDeviceTypeMask = deviceBrowser.browsedDeviceTypeMask
| ICDeviceTypeMaskCamera
| ICDeviceLocationTypeMaskLocal;
[deviceBrowser start];
}
- (void)applicationWillTerminate:(NSNotification*)notification {
deviceBrowser.delegate = NULL;
[deviceBrowser stop];
[deviceBrowser release];
[cameras release];
}
//
// ------ UI Actions -------
//
- (IBAction)takePicture:(id)pId {
[self showCountdownUI];
// Do 3 second countdown.
currentCount = 3;
[self updateCountdownUI:currentCount];
[NSTimer
scheduledTimerWithTimeInterval:1.0
target:self
selector:@selector(onCountdown:)
userInfo:nil
repeats:YES];
}
- (IBAction)onCountdown:(NSTimer*)timer {
[self updateCountdownUI:--currentCount];
if (currentCount == 1) {
snapsLeft = 3;
[NSTimer scheduledTimerWithTimeInterval:1.2
target:self
selector:@selector(onSnapTimer:)
userInfo:nil
repeats:NO];
} else if (currentCount <= 0) {
[timer invalidate];
timer = nil;
}
}
- (void)onSnapTimer:(NSTimer*)timer {
if (snapsLeft > 0) {
snapsLeft--;
[self snapCamera];
}
}
- (void)snapCamera {
state = kTakingPicture;
[activeCamera requestTakePicture];
if (cameraResetWatchdog != nil) {
[cameraResetWatchdog invalidate];
[cameraResetWatchdog release];
cameraResetWatchdog = nil;
}
cameraResetWatchdog = [[NSTimer
scheduledTimerWithTimeInterval:4.0
target:self
selector:@selector(snapshotWatchdog:)
userInfo:nil
repeats:NO]
retain];
}
- (void)updateCountdownUI:(int)count {
switch (count) {
case -1:
[self updateCountdownImage:self.countdownImageN1 overlayCountMessage:NO];
break;
default:
case 0:
{
int smileCount = [smileList count];
NSLog(@"SmileCount: %d", smileCount);
if (smileCount != 0) {
int i = rand() % smileCount;
[self updateCountdownImage:[smileList objectAtIndex:i] overlayCountMessage:YES];
} else {
[self updateCountdownImage:self.countdownImageN1 overlayCountMessage:NO];
}
}
break;
case 1:
[self updateCountdownImage:self.countdownImage1 overlayCountMessage:NO];
[beepSound play];
break;
case 2:
[self updateCountdownImage:self.countdownImage2 overlayCountMessage:NO];
[beepSound play];
break;
case 3:
[self updateCountdownImage:self.countdownImage3 overlayCountMessage:NO];
[beepSound play];
break;
}
[countdownView setNeedsDisplay:YES];
}
- (IBAction)print:(id)pId {
// If the image has edits, save to disk.
// Print the main image. The IKImageView does not seem to support printing
// directly which is strange.
NSIndexSet* indexes = [browserView selectionIndexes];
if ([indexes count] == 0) {
return;
}
ImageInfo* imageInfo = [self imageBrowser:browserView
itemAtIndex:[indexes firstIndex]];
NSImage* image = [[NSImage alloc] initByReferencingURL:[imageInfo url]];
NSSize imageSize = [image size];
NSRect frameSize = NSMakeRect(0, 0, imageSize.width, imageSize.height);
NSImageView* printableView = [[NSImageView alloc] initWithFrame:frameSize];
[printableView setImage:image];
// Force orientation here.
if (imageSize.width > imageSize.height) {
[printInfo setOrientation:NSLandscapeOrientation];
} else {
[printInfo setOrientation:NSPortraitOrientation];
}
NSPrintOperation *op =
[NSPrintOperation printOperationWithView:printableView
printInfo:printInfo];
if (pId == printButton) {
[op setShowsPrintPanel:NO];
}
[op setJobTitle:
[[NSString alloc] initWithFormat:@"Photobooth: %@", [[imageInfo url] path]]];
[op runOperationModalForWindow:window
delegate:self
didRunSelector:@selector(printOperationDidRun:success:contextInfo:)
contextInfo:NULL];
}
- (void)printSheetDidEnd:(id)sheet
returnCode:(int)returnCode
contextInfo:(void *)contextInfo {
if (returnCode == NSOKButton) {
[preferences setObject:
[NSArchiver archivedDataWithRootObject:[sheet printInfo]]
forKey:kPrintInfoPref];
[preferences synchronize];
}
}
- (void)printOperationDidRun:(NSPrintOperation *)op
success:(BOOL)success
contextInfo:(void *)contextInfo {
if (success) {
printInfo = [[op printInfo] retain];
[preferences setObject:
[NSArchiver archivedDataWithRootObject:printInfo]
forKey:kPrintInfoPref];
[preferences synchronize];
}
}
- (IBAction)showPageLayout:(id)pId {
NSPageLayout *pageLayout = [NSPageLayout pageLayout];
[pageLayout beginSheetWithPrintInfo:printInfo
modalForWindow:window
delegate:self
didEndSelector:printSheetDidEndSelector
contextInfo:nil];
}
- (IBAction)showPrintPanel:(id)pId {
NSPrintPanel *printPanel = [NSPrintPanel printPanel];
[printPanel beginSheetWithPrintInfo:printInfo
modalForWindow:window
delegate:self
didEndSelector:printSheetDidEndSelector
contextInfo:nil];
}
- (IBAction)resetPrintInfo:(id)pId {
printInfo = [[NSPrintInfo alloc] initWithDictionary:[NSDictionary dictionary]];
[NSPrintInfo setSharedPrintInfo: printInfo];
[self resetMarginsPagination:pId];
}
- (IBAction)resetMarginsPagination:(id)pId {
[printInfo setTopMargin:0];
[printInfo setLeftMargin:0];
[printInfo setRightMargin:0];
[printInfo setBottomMargin:0];
[printInfo setVerticalPagination:NSClipPagination];
[printInfo setHorizontalPagination:NSFitPagination];
[printInfo setVerticallyCentered:YES];
[printInfo setHorizontallyCentered:YES];
}
- (IBAction)enterFullscreen:(id)pId {
originalFrame = [window frame];
[window setFrame:[window frameRectForContentRect:[[window screen] frame]]
display:YES
animate:YES];
if ([[window screen] isEqual:[[NSScreen screens] objectAtIndex:0]]) {
[NSMenu setMenuBarVisible:NO];
}
fullscreenWindow = [[NSWindow alloc]
initWithContentRect:[window contentRectForFrameRect:[window frame]]
styleMask:NSBorderlessWindowMask
backing:NSBackingStoreBuffered
defer:YES
screen:[window screen]];
[fullscreenWindow setLevel:NSFloatingWindowLevel];
[fullscreenWindow setContentView:[window contentView]];
[fullscreenWindow setTitle:[window title]];
[fullscreenWindow setFrame:[[window screen] frame]
display:YES
animate:YES];
[fullscreenWindow makeKeyAndOrderFront:nil];
}
- (IBAction)leaveFullscreen:(id)pId {
if (fullscreenWindow != nil) {
[window setContentView:[fullscreenWindow contentView]];
[fullscreenWindow setLevel:NSNormalWindowLevel];
[fullscreenWindow orderOut:nil];
fullscreenWindow = nil;
[window setFrame:originalFrame
display:YES
animate:YES];
[NSMenu setMenuBarVisible:YES];
}
}
//
// ------ Preferences Sheet Actions ------
//
- (IBAction)showPreferences:(id)pId {
[NSApp beginSheet:preferencesSheet modalForWindow:window
modalDelegate:self didEndSelector:NULL contextInfo:nil];
}
- (IBAction)savePreferences:(id)pId {
[self setImagesFolder:[selectedImagesFolder stringValue]];
[self setSmilesFolder:[selectedSmilesFolder stringValue]];
[preferencesSheet orderOut:nil];
[NSApp endSheet:preferencesSheet];
activeCamera = [(ICCameraDevice*)[deviceBrowserView selectedDevice] retain];
}
- (IBAction)cancelPreferences:(id)pId {
[preferencesSheet orderOut:nil];
[NSApp endSheet:preferencesSheet];
}
- (IBAction)showImageDirectoryChooser:(id)pId {
NSOpenPanel* panel = [NSOpenPanel openPanel];
[panel setCanChooseFiles:NO];
[panel setCanChooseDirectories:YES];
[panel beginWithCompletionHandler: ^ (NSInteger result) {
if (result == NSFileHandlingPanelOKButton) {
if (pId == setImagesFolderButton) {
[selectedImagesFolder setStringValue:[panel filename]];
} else if (pId == setSmilesFolderButton) {
[selectedSmilesFolder setStringValue:[panel filename]];
}
}
}];
}
- (void)deviceBrowserView:(IKDeviceBrowserView *)deviceBrowserView
selectionDidChange:(ICDevice *)device {
}
- (void)deviceBrowserView:(IKDeviceBrowserView *)deviceBrowserView
didEncounterError:(NSError *)error {
}
//
// ------ IKImageBrowserView delegates------
//
- (NSUInteger) numberOfItemsInImageBrowser:(IKImageBrowserView *)aBrowser {
return [imageList count];
}
- (id) imageBrowser:(IKImageBrowserView *)aBrowser itemAtIndex:(NSUInteger)index {
return [imageList objectAtIndex:[imageList count] - index - 1];
}
- (void) imageBrowserSelectionDidChange:(IKImageBrowserView *) aBrowser {
NSIndexSet* indexes = [browserView selectionIndexes];
if ([indexes count] > 0) {
[self updateMainImage:[self imageBrowser:browserView itemAtIndex:[indexes firstIndex]]
shouldZoom:YES];
}
}
//
// ------ Public API -------
//
- (void)setImagesFolder:(NSString*) path {
if ([imagesFolder isEqual:path]) {
return;
}
// We write our own setting becaus we want to always update the labels when
// we set this path.
[imagesFolder release];
imagesFolder = path;
[imagesFolder retain];
[preferences setObject:imagesFolder forKey:kImageFolderPref];
[preferences synchronize];
[selectedImagesFolder setStringValue:imagesFolder];
[self rescanImagesDirectory];
};
- (void)setSmilesFolder:(NSString*) path {
if ([smilesFolder isEqual:path]) {
return;
}
// We write our own setting becaus we want to always update the labels when
// we set this path.
[smilesFolder release];
smilesFolder = path;
[smilesFolder retain];
[preferences setObject:smilesFolder forKey:kSmilesFolderPref];
[preferences synchronize];
[selectedSmilesFolder setStringValue:smilesFolder];
[self rescanSmilesFolder];
};
//
// ------ Internal functions -------
//
- (void)initializeMainImage {
[self updateMainImage:self.defaultImage shouldZoom:NO];
// customize the IKImageView...
[mainImage setDoubleClickOpensImageEditPanel: NO];
[mainImage setCurrentToolMode: IKToolModeNone];
[mainImage setDelegate: self];
}
- (void)initializeBrowseList {
[browserView setZoomValue:1.0f];
[browserView setContentResizingMask:NSViewWidthSizable]; // Don't grow veritcally.
[browserView setAllowsMultipleSelection:NO];
[browserView reloadData];
}
- (void)rescanImagesDirectory {
[imageList removeAllObjects];
[self addImagesWithPath:imagesFolder to:imageList];
[self initializeBrowseList];
}
- (void)rescanSmilesFolder {
NSLog(@"Scanning: %@", smilesFolder);
if (![smilesFolder isEqual: @""]) {
[smileList removeAllObjects];
[self addImagesWithPath:smilesFolder to:smileList];
}
}
- (void)addImagesWithPath:(NSString*)path to:(NSMutableArray*)targetList {
NSFileManager *localFileManager = [[[NSFileManager alloc] init] autorelease];
NSDirectoryEnumerator *dirEnum =
[localFileManager enumeratorAtPath:path];
NSString *file;
while (file = [dirEnum nextObject]) {
NSString* extension = [file pathExtension];
if ([extension caseInsensitiveCompare:@"jpg"] == NSOrderedSame ||
[extension caseInsensitiveCompare:@"png"] == NSOrderedSame) {
[self addAnImageWithPath:[path stringByAppendingPathComponent:file]
to:targetList];
}
}
}
- (void)addAnImageWithPath:(NSString *)path to:(NSMutableArray*)targetList
{
ImageInfo *imageInfo;
imageInfo = [[[ImageInfo alloc] initWithPath:path] autorelease];
[targetList addObject:imageInfo];
}
// ----- ICDeviceBrowser protocol -----
// Method delegates for device added and removed
//
// Device browser maintains list of cameras as key-value pairs, so delegate
// must call willChangeValueForKey to modify list
- (void)deviceBrowser:(ICDeviceBrowser*)browser didAddDevice:(ICDevice*)addedDevice moreComing:(BOOL)moreComing
{
if ( addedDevice.type & ICDeviceTypeCamera )
{
if ([addedDevice.capabilities indexOfObject:ICCameraDeviceCanTakePicture] == NSNotFound) {
NSLog(@"Device: %@ cannot take pictures", addedDevice.name);
return;
}
NSLog(@"Adding device: %@", addedDevice.name);
addedDevice.delegate = self;
// implement manual observer notification for the cameras property
[self willChangeValueForKey:@"cameras"];
[cameras addObject:addedDevice];
[self didChangeValueForKey:@"cameras"];
if (activeCamera == nil) {
[self setActiveCamera:(ICCameraDevice*)addedDevice];
}
}
}
- (void)deviceBrowser:(ICDeviceBrowser*)browser didRemoveDevice:(ICDevice*)device moreGoing:(BOOL)moreGoing
{
device.delegate = NULL;
// implement manual observer notification for the cameras property
[self willChangeValueForKey:@"cameras"];
[cameras removeObject:device];
[self didChangeValueForKey:@"cameras"];
if (activeCamera == device && [cameras count] == 0) {
[self setActiveCamera:nil];
} else {
[self setActiveCamera:[cameras lastObject]];
}
}
// Check to see if the camera has image files to download
- (void)observeValueForKeyPath:(NSString*)keyPath ofObject:(id)object change:(NSDictionary*)change context:(void*)context
{
if ( [keyPath isEqualToString:@"selectedObjects"] && (object == self) )
{
[self willChangeValueForKey:@"canDownload"];
[self didChangeValueForKey:@"canDownload"];
}
}
// ---- ICCameraDeviceDelegate ----
- (void)didRemoveDevice:(ICDevice*)device {
// Do nothing. device browser delegate handles our state.
}
- (void)didDownloadFile:(ICCameraFile*)file
error:(NSError*)error
options:(NSDictionary*)options
contextInfo:(void*)contextInfo {
NSString* filename = [options objectForKey:ICSavedFilename];
[self addAnImageWithPath:[imagesFolder stringByAppendingPathComponent:filename]
to:imageList];
[browserView reloadData];
[self imageBrowserSelectionDidChange:browserView];
// Must scroll *after* we've reloaded the data, otherwise we use the stale array.
[browserView scrollIndexToVisible:0];
NSIndexSet* selectFirst = [[NSIndexSet alloc] initWithIndex:0];
[browserView setSelectionIndexes:selectFirst byExtendingSelection:NO];
[self setReadyText:[[file device] name]];
[cameraResetWatchdog invalidate];
[cameraResetWatchdog release];
cameraResetWatchdog = nil;
NSLog(@"Snaps left %d\n", snapsLeft);
if (snapsLeft > 0) {
[NSTimer scheduledTimerWithTimeInterval:0.2
target:self
selector:@selector(onSnapTimer:)
userInfo:nil
repeats:NO];
} else {
[self hideCountdownUI];
}
}
- (void)cameraDevice:(ICCameraDevice*)camera didAddItem:(ICCameraItem*)item {
NSLog(@"Item name %@", [item name]);
if (state != kTakingPicture) {
// Ignore random item adds since we only want the result of the snapshot.
// The camera seems to like to send all its pictures at startup, so this
// is useful.
return;
}
if (![item isKindOfClass:[ICCameraFile class]]) {
// We only handle files.
return;
}
// Download it if it's an image.
if ([[item UTI] isEqualToString:(NSString*)kUTTypeImage]) {
NSDictionary* downloadOptions = [NSDictionary
dictionaryWithObject:[NSURL fileURLWithPath:imagesFolder]
forKey:ICDownloadsDirectoryURL];
[camera requestDownloadFile:(ICCameraFile*)item
options:downloadOptions
downloadDelegate:self
didDownloadSelector:@selector(didDownloadFile:error:options:contextInfo:)
contextInfo:NULL];
[self setDownloadingText:[item name]];
}
}
- (void)device:(ICDevice*)device didOpenSessionWithError:(NSError*)error {
if (error != nil) {
NSLog(@"Error %@", error);
return;
}
[self setReadyText:[device name]];
}
- (void)device:(ICDevice*)device didCloseSessionWithError:(NSError*)error {
if (error != nil) {
NSLog(@"Error %@", error);
}
// We retained this in setActiveCamera.
[device release];
}
- (void)setReadyText:(NSString*)deviceName {
state = kReady;
[statusText setStringValue:
[[NSString alloc] initWithFormat:@"%@ Ready", deviceName]];
}
- (void)setDownloadingText:(NSString*)filename {
state = kDownloadingFile;
[statusText setStringValue:
[[NSString alloc] initWithFormat:@"Downloading %@", filename]];
}
- (void)setUninitialized {
state = kUninitialized;
[statusText setStringValue:@"No Camera Found"];
}
- (void)setActiveCamera:(ICCameraDevice*)camera {
[activeCamera requestCloseSession];
activeCamera = [camera retain];
if (camera != nil) {
[activeCamera requestOpenSession];
} else {
[self setUninitialized];
}
}
- (void)snapshotWatchdog:(NSTimer*)timer {
if (activeCamera) {
[self setReadyText:[activeCamera name]];
} else {
[self setUninitialized];
}
[self hideCountdownUI];
}
- (void)updateMainImage:(ImageInfo*)info shouldZoom:(BOOL)zoom {
[mainImage setImageWithURL:info.url];
if (zoom) {
[mainImage zoomImageToFit: self];
}
[mainImage setNeedsDisplay:YES];
}
- (void)updateCountdownImage:(ImageInfo*)info overlayCountMessage:(BOOL)should_overlay {
NSImage *image = [[[NSImage alloc] initWithContentsOfURL:info.url] autorelease];
if (should_overlay) {
/*
NSImage *overlay = [[[NSImage alloc] initWithContentsOfURL:self.countdownImageN1.url] autorelease];
[image drawInRect:location fromRect:source_clip operation:NSCompositeSourceOver fraction:0.5];
[image lockFocus];
NSString *string = [NSString stringWithString:@"Text\nMore Text"];
[string drawInRect:NSMakeRect(90, 60, 200, 45)
withAttributes:nil];
[image unlockFocus];
*/
}
[image setScalesWhenResized: YES];
[image setSize: [countdownView frame].size];
[countdownView setImage:image];
[countdownView setNeedsDisplay:YES];
if (should_overlay) {
[overlayView setHidden:NO];
}
}
- (void)showCountdownUI {
// Capture the main display
if (CGDisplayCapture( kCGDirectMainDisplay ) != kCGErrorSuccess) {
NSLog( @"Couldn't capture the main display!" );
// Note: you'll probably want to display a proper error dialog here
}
[countdownWindow makeKeyAndOrderFront:nil];
}
- (void)createCountdownUI {
int windowLevel;
NSRect screenRect;
// Get the shielding window level
windowLevel = CGShieldingWindowLevel();
// Get the screen rect of our main display
screenRect = [[NSScreen mainScreen] frame];
// Put up a new window
countdownWindow = [[NSWindow alloc]
initWithContentRect:screenRect
styleMask:NSBorderlessWindowMask
backing:NSBackingStoreBuffered
defer:NO
screen:[NSScreen mainScreen]];
[countdownWindow setLevel:windowLevel];
[countdownWindow setBackgroundColor:[NSColor blackColor]];
// Create countdown view and add it
countdownView = [[NSImageView alloc] initWithFrame: screenRect];
[countdownView setAutoresizingMask:NSViewWidthSizable|NSViewHeightSizable];
[countdownWindow setContentView:countdownView];
// Add a text overlay for messages.
countdownStatus = [[NSTextField alloc] initWithFrame: screenRect];
NSImage *i =
[[[NSImage alloc]
initWithContentsOfFile:[[NSBundle mainBundle]
pathForResource:@"look_at_camera_overlay"
ofType:@"png"]] autorelease];
[self setOverlayImage:i];
[overlayImage setSize:NSMakeSize(1820, 163)];
[overlayImage setScalesWhenResized: YES];
NSRect overlay_frame = NSMakeRect(0, 0, screenRect.size.width, 163);
overlayView = [[NSImageView alloc] initWithFrame:overlay_frame];
[overlayView setImage:overlayImage];
[overlayView setAlphaValue:0.8];
[countdownView addSubview:overlayView];
[overlayView setHidden:YES];
}
- (void)hideCountdownUI
{
[self updateCountdownImage:self.countdownImage0 overlayCountMessage:NO];
[countdownWindow orderOut:self];
[overlayView setHidden:YES];
// Capture the main display
if (CGDisplayRelease( kCGDirectMainDisplay ) != kCGErrorSuccess) {
NSLog( @"Couldn't release the main display!" );
// Note: you'll probably want to display a proper error dialog here
}
}
@end