This repository has been archived by the owner on Jun 19, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathApertureToBlip.m
499 lines (374 loc) · 15.3 KB
/
ApertureToBlip.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
//
// Blipfoto.m
// Blipfoto
//
// Created by Graham Bradley on 06/02/2011.
// Copyright __MyCompanyName__ 2011. All rights reserved.
//
#import "ApertureToBlip.h"
#import "DataController.h"
@implementation ApertureToBlip
@synthesize dataController, api, entries;
- (id) initWithAPIManager:(id<PROAPIAccessing>)apiManager {
if (self = [super init]) {
_apiManager = apiManager;
_exportManager = [_apiManager apiForProtocol:@protocol(ApertureExportManager)];
if (!_exportManager) {
return nil;
}
_progressLock = [[NSLock alloc] init];
uploading = NO;
uploadCount = 0;
uploadedEntryId = 0;
}
return self;
}
#pragma mark -
// UI Methods
#pragma mark UI Methods
- (NSView *) settingsView {
if (nil == settingsView) {
NSBundle *myBundle = [NSBundle bundleForClass:[self class]];
NSNib *myNib = [[NSNib alloc] initWithNibNamed:@"ApertureToBlip" bundle:myBundle];
NSArray __autoreleasing *pointer = _topLevelNibObjects;
[myNib instantiateWithOwner:self topLevelObjects:&pointer];
}
return settingsView;
}
- (NSView *) firstView {
return firstView;
}
- (NSView *) lastView {
return lastView;
}
- (void) willBeActivated {
self.dataController = [[DataController alloc] init];
api = [[API alloc] initWithKey:@"key" secret:@"secret"];
if (![self.dataController.displayName isEqualToString:@""]) {
api.userToken = self.dataController.userToken;
api.userSecret = self.dataController.userSecret;
connectLabel.stringValue = [NSString stringWithFormat:@"Linked to %@'s journal", self.dataController.displayName];
connectButton.title = @"Unlink";
}
visitJournal.state = self.dataController.visitJournalAfterExport;
// fetch the server timestamp
[api generateTimestamp];
entryTitle.delegate = self;
entryDesc.delegate = self;
entryTags.delegate = self;
self.entries = [[NSMutableArray alloc] initWithCapacity:0];
// inspect the image's EXIF and IPTC fields
NSDictionary *properties;
NSDictionary *exif;
NSArray *exifKeys;
NSDictionary *iptc;
NSArray *iptcKeys;
NSArray *filename;
NSString *tmpTitle;
NSString *tmpDesc;
NSString *tmpTags;
NSString *tmpDate;
BOOL shouldBeUploaded;
NSArray *days = [NSArray arrayWithObjects:@"Sunday",
@"Monday",
@"Tuesday",
@"Wednesday",
@"Thursday",
@"Friday",
@"Saturday",
nil];
NSArray *months = [NSArray arrayWithObjects:@"January",
@"February",
@"March",
@"April",
@"May",
@"June",
@"July",
@"August",
@"September",
@"October",
@"November",
@"December",
nil];
[entrySelector removeAllItems];
int i;
int l = [_exportManager imageCount];
for (i = 0; i < l; i++) {
properties = [_exportManager propertiesWithoutThumbnailForImageAtIndex:i];
exif = [properties objectForKey:@"kExportKeyEXIFProperties"];
exifKeys = [exif allKeys];
if ([exifKeys containsObject:@"CaptureDayOfMonth"]) {
tmpDate = [NSString stringWithFormat:@"%@ %@ %@ %@", [days objectAtIndex:[[exif objectForKey:@"CaptureDayOfWeek"] intValue]], [exif objectForKey:@"CaptureDayOfMonth"], [months objectAtIndex:[[exif objectForKey:@"CaptureMonthOfYear"] intValue]-1], [exif objectForKey:@"CaptureYear"]];
uploadCount++;
shouldBeUploaded = YES;
} else {
tmpDate = @"(No date found, won't be uploaded)";
shouldBeUploaded = NO;
}
iptc = [properties objectForKey:@"kExportKeyIPTCProperties"];
iptcKeys = [iptc allKeys];
if ([iptcKeys containsObject:@"ObjectName"]) {
tmpTitle = [iptc objectForKey:@"ObjectName"];
} else {
tmpTitle = @"";
}
if ([iptcKeys containsObject:@"Caption/Abstract"]) {
tmpDesc = [iptc objectForKey:@"Caption/Abstract"];
} else {
tmpDesc= @"";
}
if ([iptcKeys containsObject:@"Keywords"]) {
tmpTags = [iptc objectForKey:@"Keywords"];
} else {
tmpTags = @"";
}
[self.entries addObject: [NSMutableDictionary dictionaryWithObjectsAndKeys:
tmpTitle, @"title",
tmpDesc, @"desc",
tmpTags, @"tags",
tmpDate, @"date",
[NSNumber numberWithBool:shouldBeUploaded], @"shouldBeUploaded",
nil]];
filename = [[properties objectForKey:kExportKeyReferencedMasterPath] componentsSeparatedByString:@"/"];
[entrySelector addItemWithTitle:[filename objectAtIndex:[filename count] - 1]];
}
[self updateEntryOptions:0];
}
#pragma mark
// Plugin UI Controls
#pragma mark Plugin UI Controls
- (void) updateEntryOptions:(int) index {
[entryThumbnail setImage:[_exportManager thumbnailForImageAtIndex:index size:kExportThumbnailSizeTiny]];
NSDictionary *entryData = [self.entries objectAtIndex:index];
entryDate.stringValue = [entryData objectForKey:@"date"];
entryTitle.stringValue = [entryData objectForKey:@"title"];
entryDesc.stringValue = [entryData objectForKey:@"desc"];
entryTags.stringValue = [entryData objectForKey:@"tags"];
}
- (IBAction) switchImage:(id)sender {
[self updateEntryOptions:[entrySelector indexOfSelectedItem]];
}
- (IBAction) connectButtonPressed:(id)sender {
if ([connectButton.title isEqualToString:@"Verify"]){
[api request:@"get" resource:@"token" params:[NSMutableDictionary dictionaryWithObjectsAndKeys:tempToken.stringValue, @"temp_token", nil] authType:APIAuthTypeApplication onSuccess:^(NSDictionary *response) {
NSDictionary *data = [response objectForKey:@"data"];
// save prefs
self.dataController.userToken = [data objectForKey:@"token"];
self.dataController.userSecret = [data objectForKey:@"secret"];
self.dataController.displayName = [data objectForKey:@"display_name"];
[self.dataController saveData];
api.userToken = self.dataController.userToken;
api.userSecret = self.dataController.userSecret;
[tempToken setHidden:YES];
connectLabel.stringValue = [NSString stringWithFormat:@"Linked to %@'s journal", self.dataController.displayName];
connectButton.title = @"Unlink";
} onFailure:^(NSError *error) {
NSAlert *alert = [[NSAlert alloc] init];
[alert addButtonWithTitle:@"OK"];
[alert setMessageText:@"Couldn't link journal"];
[alert setInformativeText:[NSString stringWithFormat:@"It looks like you copied the code incorrectly or didn't give permission to the app (error %ld).", (long)error.code]];
[alert setAlertStyle:NSInformationalAlertStyle];
[alert beginSheetModalForWindow:[[self settingsView] window] modalDelegate:self didEndSelector:nil contextInfo:nil];
}];
}
else if ([connectButton.title isEqualToString:@"Unlink"]){
NSAlert *alert = [[NSAlert alloc] init];
[alert addButtonWithTitle:@"OK"];
[alert addButtonWithTitle:@"Cancel"];
[alert setMessageText:@"Unlink journal"];
[alert setInformativeText:@"Unlinking your journal will mean you won't be able to publish to Blipfoto."];
[alert setAlertStyle:NSInformationalAlertStyle];
[alert beginSheetModalForWindow:[sender window] modalDelegate:self didEndSelector:@selector(alertDidEnd:returnCode:contextInfo:) contextInfo:nil];
}
else {
connectLabel.stringValue = @"Enter 6 digit code:";
connectButton.title = @"Verify";
[tempToken setHidden : NO];
[[tempToken window] makeFirstResponder:tempToken];
[[NSWorkspace sharedWorkspace] openURL:[NSURL URLWithString:@"http://www.blipfoto.com/getpermission/769357"]];
}
}
- (void)alertDidEnd:(NSAlert *)alert returnCode:(int)returnCode contextInfo:(void *)contextInfo{
if (returnCode == NSAlertFirstButtonReturn) {
self.dataController.userToken = @"";
self.dataController.userSecret = @"";
self.dataController.displayName = @"";
[self.dataController saveData];
connectButton.title = @"Link";
connectLabel.stringValue=@"Start by linking to your Blipfoto journal:";
}
}
- (IBAction) formattingButtonPressed:(id)sender {
int offset;
NSText *textBox = [entryDesc currentEditor];
NSString *text = textBox.string;
NSRange range = [textBox selectedRange];
int insertionPoint = range.location;
if (sender == linkButton){
[textBox setString:[NSString stringWithFormat:@"%@[url=]%@[/url]%@", [text substringToIndex:insertionPoint], [text substringWithRange:range], [text substringFromIndex:insertionPoint + range.length]]];
range.location += 5;
range.length = 0;
} else {
NSString *chr = [[((NSButton *)sender) title] lowercaseString];
offset=3;
[textBox setString:[NSString stringWithFormat:@"%@[%@]%@[/%@]%@", [text substringToIndex:insertionPoint], chr, [text substringWithRange:range], chr, [text substringFromIndex:insertionPoint + range.length]]];
range.location += range.length ? range.length + 7 : 3;
range.length = 0;
}
[textBox setSelectedRange:range];
}
- (void)controlTextDidChange:(NSNotification *) notify {
if (notify.object==entryTitle){
[[self.entries objectAtIndex:[entrySelector indexOfSelectedItem]] setObject:entryTitle.stringValue forKey:@"title"];
}
else if (notify.object==entryDesc){
[[self.entries objectAtIndex:[entrySelector indexOfSelectedItem]] setObject:entryDesc.stringValue forKey:@"desc"];
}
else if (notify.object==entryTags){
[[self.entries objectAtIndex:[entrySelector indexOfSelectedItem]] setObject:entryTags.stringValue forKey:@"tags"];
}
}
/* Allow new lines in text field - http://developer.apple.com/library/mac/#qa/qa1454/_index.html */
- (BOOL) control:(NSControl*)control textView:(NSTextView*)textView doCommandBySelector:(SEL)commandSelector {
BOOL result = NO;
if (control == entryDesc && commandSelector == @selector(insertNewline:)) {
[textView insertNewlineIgnoringFieldEditor:self];
result = YES;
}
return result;
}
- (void)willBeDeactivated {
}
#pragma mark
// Aperture UI Controls
#pragma mark Aperture UI Controls
- (BOOL)allowsOnlyPlugInPresets {
return NO;
}
- (BOOL)allowsMasterExport {
return YES;
}
- (BOOL)allowsVersionExport {
return YES;
}
- (BOOL) wantsFileNamingControls {
return NO;
}
- (void) exportManagerExportTypeDidChange {
}
#pragma mark -
// Save Path Methods
#pragma mark Save/Path Methods
- (BOOL) wantsDestinationPathPrompt {
return NO;
}
- (NSString *) destinationPath {
return [@"~/Documents" stringByExpandingTildeInPath];
}
- (NSString *) defaultDirectory {
return [@"~/Documents" stringByExpandingTildeInPath];
}
#pragma mark -
// Export Process Methods
#pragma mark Export Process Methods
- (void) exportManagerShouldBeginExport {
if ([self.dataController.displayName isEqualToString:@""]) {
NSAlert *alert = [[NSAlert alloc] init];
[alert addButtonWithTitle:@"OK"];
[alert setMessageText:@"Not linked to journal"];
[alert setInformativeText:@"You must link Aperture to your Blipfoto journal before you can export images."];
[alert setAlertStyle:NSInformationalAlertStyle];
[alert beginSheetModalForWindow:[[self settingsView] window] modalDelegate:self didEndSelector:nil contextInfo:nil];
} else {
self.dataController.visitJournalAfterExport = visitJournal.state;
[self.dataController saveData];
[self lockProgress];
exportProgress.totalValue = 100;
exportProgress.currentValue = 0;
exportProgress.indeterminateProgress = NO;
exportProgress.message = @"Starting upload...";
[self unlockProgress];
uploadLog = [[NSMutableArray alloc] initWithCapacity:0];
[_exportManager shouldBeginExport];
}
}
- (void) exportManagerWillBeginExportToPath:(NSString *)path {
}
- (BOOL) exportManagerShouldExportImageAtIndex:(unsigned)index {
return [[[entries objectAtIndex:index] objectForKey:@"shouldBeUploaded"] boolValue];
}
- (void) exportManagerWillExportImageAtIndex:(unsigned)index {
}
- (BOOL) exportManagerShouldWriteImageData:(NSData *)imageData toRelativePath:(NSString *)path forImageAtIndex:(unsigned)index {
[self lockProgress];
exportProgress.totalValue = 100;
exportProgress.currentValue = 0;
exportProgress.message = [NSString stringWithFormat:@"Uploading %d of %d", index + 1, uploadCount];
[self unlockProgress];
NSDictionary *entryData = [entries objectAtIndex:index];
NSMutableDictionary *params = [NSMutableDictionary dictionaryWithObjectsAndKeys:
[entryData objectForKey:@"title"], @"title",
[entryData objectForKey:@"desc"], @"description",
[entryData objectForKey:@"tags"], @"tags",
imageData, @"image",
nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(uploadProgressUpdated:) name:@"PublishProgressUpdated" object:nil];
uploading = YES;
[api request:@"post" resource:@"entry" params:params authType:APIAuthTypeUser onSuccess:^(NSDictionary *response) {
[[NSNotificationCenter defaultCenter] removeObserver:self name:@"PublishProgressUpdated" object:nil];
NSDictionary *data = [response objectForKey:@"data"];
uploadedEntryId = [data objectForKey:@"entry_id"];
[self uploadFinishedWithMessage:[NSString stringWithFormat:@"Image %d published succesfully", (int)[uploadLog count] + 1]];
} onFailure:^(NSError *error) {
[[NSNotificationCenter defaultCenter] removeObserver:self name:@"PublishProgressUpdated" object:nil];
[self uploadFinishedWithMessage:[NSString stringWithFormat:@"Image %d not published - %@", (int)[uploadLog count]+ 1, [error.userInfo objectForKey:NSLocalizedDescriptionKey]]];
}];
while (uploading) {
[[NSRunLoop currentRunLoop] runMode:NSDefaultRunLoopMode beforeDate:[NSDate distantFuture]];
}
return NO;
}
- (void) exportManagerDidWriteImageDataToRelativePath:(NSString *)relativePath forImageAtIndex:(unsigned)index {
}
- (void) exportManagerDidFinishExport {
[_exportManager shouldFinishExport];
if (self.dataController.visitJournalAfterExport) {
[[NSWorkspace sharedWorkspace] openURL:[NSURL URLWithString:[NSString stringWithFormat:@"http://www.blipfoto.com/entry/%@", uploadedEntryId]]];
}
}
- (void) exportManagerShouldCancelExport {
[_exportManager shouldCancelExport];
}
#pragma mark -
// Upload Progress Methods
#pragma mark Upload Progress Methods
- (void) uploadFinishedWithMessage:(NSString *) message {
[self lockProgress];
exportProgress.message = message;
[self unlockProgress];
[uploadLog addObject:message];
[NSTimer scheduledTimerWithTimeInterval:3 target:self selector:@selector(uploadNext:) userInfo:nil repeats:NO];
}
- (void) uploadNext:(NSTimer *) timer {
uploading = NO;
}
- (void) uploadProgressUpdated:(NSNotification *) notify {
[self lockProgress];
exportProgress.currentValue = floor([[notify object] floatValue] * 100);
[self unlockProgress];
}
#pragma mark -
// Progress Methods
#pragma mark Progress Methods
- (ApertureExportProgress *)progress {
return &exportProgress;
}
- (void)lockProgress {
if (!_progressLock)
_progressLock = [[NSLock alloc] init];
[_progressLock lock];
}
- (void)unlockProgress {
[_progressLock unlock];
}
@end