-
Notifications
You must be signed in to change notification settings - Fork 1
/
PhotoSourceController.m
193 lines (173 loc) · 5.89 KB
/
PhotoSourceController.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
//
// PhotoSourceController.m
// Geotagalog
//
// Created by Nathan Vander Wilt on 6/2/09.
// Copyright 2009 __MyCompanyName__. All rights reserved.
//
#import "PhotoSourceController.h"
#import "TLImageCaptureManager.h"
#import "TLImageCapturePhotoSource.h"
#import "TLFilePhotoSource.h"
@interface PhotoSourceController ()
- (void)refreshSources;
@end
@implementation PhotoSourceController
@synthesize source;
- (void)setSource:(TLPhotoSource*)newSource {
if (newSource == source) return;
[source unleash];
source = [newSource leash];
}
- (void)awakeFromNib {
[loadingSpinner setUsesThreadedAnimation:YES];
[[TLImageCaptureManager sharedImageCaptureManager] addObserver:self
forKeyPath:@"sources"
options:(NSKeyValueObservingOptionNew |
// need old to get removed sources
NSKeyValueObservingOptionOld |
NSKeyValueObservingOptionInitial)
context:NULL];
[self addObserver:self
forKeyPath:@"source.name"
options:NSKeyValueObservingOptionNew
context:NULL];
[self addObserver:self
forKeyPath:@"source.isWorking"
options:NSKeyValueObservingOptionNew
context:NULL];
}
- (void)dealloc {
[[TLImageCaptureManager sharedImageCaptureManager] removeObserver:self forKeyPath:@"sources"];
[self removeObserver:self forKeyPath:@"source.name"];
[self removeObserver:self forKeyPath:@"source.isWorking"];
[self setSource:nil];
[menuItemSources release], menuItemSources = nil;
[super dealloc];
}
- (void)refreshSources {
NSMenu* newSourceMenu = [[NSMenu new] autorelease];
NSMenuItem* titleItem = [[NSMenuItem new] autorelease];
if ([self source]) {
[titleItem setTitle:[[self source] name]];
}
else {
[titleItem setTitle:@"Choose photo source..."];
}
[newSourceMenu addItem:titleItem];
NSSet* newSources = [[TLImageCaptureManager sharedImageCaptureManager] sources];
[menuItemSources release];
menuItemSources = [[NSMapTable mapTableWithStrongToStrongObjects] retain];
for (TLPhotoSource* newSource in newSources) {
if (newSource == [self source]) continue;
NSMenuItem* item = [[NSMenuItem new] autorelease];
[item setTitle:[newSource name]];
/* TODO: re-enable when generic ICA and folder sources get icons
NSImage* icon = [[[newSource icon] copy] autorelease];
CGFloat textSize = 1.5f * [[NSFont menuFontOfSize:0] pointSize];
[icon setSize:NSMakeSize(textSize, textSize)];
[item setImage:icon]; */
[newSourceMenu addItem:item];
[menuItemSources setObject:newSource forKey:item];
}
NSMenuItem* folderItem = [[NSMenuItem new] autorelease];
[folderItem setTitle:@"Choose files or folders..."];
[newSourceMenu addItem:folderItem];
[sourcePicker setMenu:newSourceMenu];
}
- (void)automaticallySetSourceIfDesirable {
if (![self source]) {
NSSet* currentSources = [[TLImageCaptureManager sharedImageCaptureManager] sources];
if ([currentSources count] == 1) {
[self setSource:[currentSources anyObject]];
}
}
[self refreshSources];
}
- (void)observeValueForKeyPath:(NSString*)keyPath
ofObject:(id)object
change:(NSDictionary*)change
context:(void*)context
{
(void)object;
(void)change;
(void)context;
//NSLog(@"Observed %@: %@", keyPath, change);
if ([keyPath isEqualToString:@"sources"]) {
if ([[change objectForKey:NSKeyValueChangeKindKey] intValue] == NSKeyValueChangeRemoval) {
NSSet* removedSources = [change objectForKey:NSKeyValueChangeOldKey];
if ([removedSources containsObject:[self source]]) {
[self setSource:nil];
}
}
[self performSelector:@selector(automaticallySetSourceIfDesirable)
withObject:nil
afterDelay:0.5];
[self refreshSources];
}
else if ([keyPath isEqualToString:@"source.name"]) {
[self refreshSources];
}
else if ([keyPath isEqualToString:@"source.isWorking"]) {
NSError* anError = [[self source] isWorking] ? nil : [[self source] error];
if (anError) {
[NSApp presentError:anError];
}
}
}
- (IBAction)sourceChanged:(id)sender {
(void)sender;
NSMenuItem* item = [sourcePicker selectedItem];
TLPhotoSource* pickedSource = [menuItemSources objectForKey:item];
if (pickedSource) {
[self setSource:pickedSource];
[self refreshSources];
}
else {
NSOpenPanel* filePicker = [NSOpenPanel openPanel];
[filePicker setCanChooseFiles:YES];
[filePicker setCanChooseDirectories:YES];
[filePicker setAllowsMultipleSelection:YES];
// NOTE: by including extensions instead of just UTIs, rdar://6410673 workaround is easier
NSInteger button = [filePicker runModalForTypes:
[NSArray arrayWithObjects:(id)kUTTypeImage, (id)kUTTypeJPEG, @"jpg", nil]];
if (button == NSOKButton) {
NSArray* files = [filePicker filenames];
TLFilePhotoSource* newSource = [[TLFilePhotoSource new] autorelease];
[newSource addPaths:[NSSet setWithArray:files]];
[self setSource:newSource];
[self refreshSources];
}
}
}
- (BOOL)readyForExport {
return [[self source] isCurrent];
}
- (BOOL)addItems:(NSSet*)itemPaths {
if ([[self source] isKindOfClass:[TLImageCapturePhotoSource class]]) {
NSAlert* singleDocumentAlert = [[NSAlert new] autorelease];
[singleDocumentAlert setAlertStyle:NSWarningAlertStyle];
[singleDocumentAlert setMessageText:@"Cannot add items to current photo source."];
NSString* information = (@"A non-file source is selected. "
@"Would you like to switch sources?");
[singleDocumentAlert setInformativeText:information];
(void)[singleDocumentAlert addButtonWithTitle:@"Use files"];
(void)[singleDocumentAlert addButtonWithTitle:@"Cancel"];
NSInteger choice = [singleDocumentAlert runModal];
if (choice == NSAlertSecondButtonReturn) {
return NO;
}
else {
[self setSource:nil];
}
}
if (![self source]) {
TLFilePhotoSource* newSource = [[TLFilePhotoSource new] autorelease];
[self setSource:newSource];
[self refreshSources];
}
//NSLog(@"Adding paths: %@\n", itemPaths);
[(TLFilePhotoSource*)[self source] addPaths:itemPaths];
return YES;
}
@end