-
Notifications
You must be signed in to change notification settings - Fork 0
/
ExportController.m
232 lines (195 loc) · 5.45 KB
/
ExportController.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
//
// ExportController.m
// Tagalog
//
// Created by Nathan Vander Wilt on 5/13/09.
// Copyright 2009 __MyCompanyName__. All rights reserved.
//
#import "ExportController.h"
#import "TLMainThreadPerformer.h"
@interface ExportController ()
@property (nonatomic, assign) BOOL shouldCancel;
- (void)backgroundExport;
@end
@implementation ExportController
@synthesize delegate;
@synthesize window;
@synthesize copiedItems;
@synthesize itemsWithMetadata;
@synthesize warnings;
@synthesize shouldCancel;
- (id)init {
self = [super init];
if (self) {
warnings = [NSMutableSet new];
}
return self;
}
- (void)dealloc {
NSAssert(!exportSheet, @"Export controller should not be deallocated with sheet still set");
[self setCopiedItems:nil];
[self setItemsWithMetadata:nil];
[warnings release];
[super dealloc];
}
- (void)loadSheet {
if (!window) return;
BOOL sheetLoaded = [NSBundle loadNibNamed:@"Export" owner:self];
if (!sheetLoaded) return;
[NSApp beginSheet:exportSheet
modalForWindow:window
modalDelegate:self
didEndSelector:NULL
contextInfo:NULL];
/* NOTE: while the use of 0 is not documented, it does help the
app keep from getting too antsy. This is corroborated by
http://www.cocoabuilder.com/archive/message/cocoa/2006/2/3/155978 */
[NSApp cancelUserAttentionRequest:0];
}
- (void)unloadSheet {
if (!exportSheet) return;
[NSApp endSheet:exportSheet];
[exportSheet orderOut:self];
[exportSheet release];
exportSheet = nil;
}
- (void)export {
[self loadSheet];
[progressMeter setUsesThreadedAnimation:YES];
[progressMeter startAnimation:self];
[progressMeter setIndeterminate:YES];
[self performSelectorInBackground:@selector(backgroundExport) withObject:nil];
}
- (IBAction)cancel:(id)sender {
(void)sender;
[self setShouldCancel:YES];
[progressText setStringValue:@"Cancelling export."];
[cancelButton setEnabled:NO];
[progressMeter setIndeterminate:YES];
}
- (void)noteWarning:(NSError*)warning {
[warnings addObject:warning];
}
#pragma mark Internal export implementation
- (void)notifyCancel {
if ([[self delegate] respondsToSelector:@selector(exportDidCancel:)]) {
[[[self delegate] tlMainThreadWait] exportDidCancel:self];
}
[[self tlMainThreadWait] unloadSheet];
}
- (void)notifyFail:(NSError*)err {
if ([[self delegate] respondsToSelector:@selector(exportDidFail:withError:)]) {
[[[self delegate] tlMainThreadWait] exportDidFail:self withError:err];
}
[[self tlMainThreadWait] unloadSheet];
}
- (void)notifyFinish {
if ([[self delegate] respondsToSelector:@selector(exportDidFinish:)]) {
[[[self delegate] tlMainThreadWait] exportDidFinish:self];
}
[[self tlMainThreadWait] unloadSheet];
}
- (void)backgroundExportMain {
NSProgressIndicator* progressMeterMT = [progressMeter tlMainThreadProxy];
NSTextField* progressTextMT = [progressText tlMainThreadProxy];
[progressTextMT setStringValue:@"Preparing to export."];
NSError* internalError;
BOOL prepared = [self prepareForExport:&internalError];
if ([self shouldCancel]) {
[self cancelExport];
[self notifyCancel];
return;
}
if (!prepared) {
[self notifyFail:internalError];
return;
}
[progressTextMT setStringValue:@"Exporting geotagged photos."];
[progressMeterMT setIndeterminate:NO];
NSUInteger numTotalExport = [[self itemsWithMetadata] count] + [[self copiedItems] count];
NSUInteger geotaggedIdx = 0;
for (TLPhotoSourceItem* item in [self itemsWithMetadata]) {
NSDictionary* metadata = [[self itemsWithMetadata] objectForKey:item];
BOOL exported = [self exportItem:item
withMetadata:metadata
error:&internalError];
if (!exported) {
[self noteWarning:internalError];
}
if ([self shouldCancel]) {
break;
}
++geotaggedIdx;
double exportPercent = geotaggedIdx / (double)numTotalExport;
[progressMeterMT setDoubleValue:exportPercent];
}
if ([self shouldCancel]) {
[self cancelExport];
[self notifyCancel];
return;
}
[progressTextMT setStringValue:@"Exporting other items."];
[progressMeterMT setIndeterminate:NO];
NSUInteger copiedIdx = 0;
for (TLPhotoSourceItem* item in [self copiedItems]) {
BOOL itemCopied = [self copyItem:item
error:&internalError];
if (!itemCopied) {
[self noteWarning:internalError];
}
if ([self shouldCancel]) {
break;
}
++copiedIdx;
double exportPercent = (geotaggedIdx + copiedIdx) / (double)numTotalExport;
[progressMeterMT setDoubleValue:exportPercent];
}
// prevent future cancellation, as we're about to finish export
[[cancelButton tlMainThreadWait] setEnabled:NO];
// user may have already cancelled, though
if ([self shouldCancel]) {
[self cancelExport];
[self notifyCancel];
return;
}
[progressTextMT setStringValue:@"Finishing export."];
[progressMeterMT setIndeterminate:YES];
BOOL finished = [self finishExport:&internalError];
if (!finished) {
[self notifyFail:internalError];
return;
}
[self notifyFinish];
}
- (void)backgroundExport {
NSAutoreleasePool* threadPool = [NSAutoreleasePool new];
[self backgroundExportMain];
[threadPool drain];
}
#pragma mark Export implementation stubs
- (BOOL)prepareForExport:(NSError**)err {
(void)err;
return YES;
}
- (BOOL)exportItem:(TLPhotoSourceItem*)item
withMetadata:(NSDictionary*)metadata
error:(NSError**)err
{
(void)item;
(void)metadata;
(void)err;
return YES;
}
- (BOOL)copyItem:(TLPhotoSourceItem*)item
error:(NSError**)err
{
(void)item;
(void)err;
return YES;
}
- (void)cancelExport {}
- (BOOL)finishExport:(NSError**)err {
(void)err;
return YES;
}
@end