-
Notifications
You must be signed in to change notification settings - Fork 52
/
IMBFlickrParser.m
856 lines (648 loc) · 28.8 KB
/
IMBFlickrParser.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
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
/*
iMedia Browser Framework <http://karelia.com/imedia/>
Copyright (c) 2005-2012 by Karelia Software et al.
iMedia Browser is based on code originally developed by Jason Terhorst,
further developed for Sandvox by Greg Hulands, Dan Wood, and Terrence Talbot.
The new architecture for version 2.0 was developed by Peter Baumgartner.
Contributions have also been made by Matt Gough, Martin Wennerberg and others
as indicated in source files.
The iMedia Browser Framework is licensed under the following terms:
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in all or substantial portions of the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to permit
persons to whom the Software is furnished to do so, subject to the following
conditions:
Redistributions of source code must retain the original terms stated here,
including this list of conditions, the disclaimer noted below, and the
following copyright notice: Copyright (c) 2005-2012 by Karelia Software et al.
Redistributions in binary form must include, in an end-user-visible manner,
e.g., About window, Acknowledgments window, or similar, either a) the original
terms stated here, including this list of conditions, the disclaimer noted
below, and the aforementioned copyright notice, or b) the aforementioned
copyright notice and a link to karelia.com/imedia.
Neither the name of Karelia Software, nor Sandvox, nor the names of
contributors to iMedia Browser may be used to endorse or promote products
derived from the Software without prior and express written permission from
Karelia Software or individual contributors, as appropriate.
Disclaimer: THE SOFTWARE IS PROVIDED BY THE COPYRIGHT OWNER AND CONTRIBUTORS
"AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT
LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE,
AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
LIABLE FOR ANY CLAIM, DAMAGES, OR OTHER LIABILITY, WHETHER IN AN ACTION OF
CONTRACT, TORT, OR OTHERWISE, ARISING FROM, OUT OF, OR IN CONNECTION WITH, THE
SOFTWARE OR THE USE OF, OR OTHER DEALINGS IN, THE SOFTWARE.
*/
// Author: Christoph Priebe
// SpeedLimit http://mschrag.github.com/ is a good way to debug this....
// farm1.static.flickr.com, farm2.static.flickr.com, farm3.static.flickr.com, farm4.static.flickr.com, farm5.static.flickr.com
//----------------------------------------------------------------------------------------------------------------------
// System
#import <Quartz/Quartz.h>
// iMedia
#import "IMBConfig.h"
#import "IMBFlickrNode.h"
#import "IMBFlickrObject.h"
#import "IMBFlickrParser.h"
#import "IMBFlickrHeaderViewController.h"
#import "IMBIconCache.h"
#import "IMBLibraryController.h"
#import "IMBLoadMoreObject.h"
#import "IMBObjectsPromise.h"
#import "IMBParserController.h"
#import "NSWorkspace+iMedia.h"
#import "NSImage+iMedia.h"
#import "NSString+iMedia.h"
#import "IMBConfig.h"
#import "IMBNodeObject.h"
#import "IMBSmartFolderNodeObject.h"
//#define VERBOSE
//----------------------------------------------------------------------------------------------------------------------
@interface IMBFlickrParser ()
// Flickr Request Handling:
- (void) cancelAllPendingFlickrRequests;
- (BOOL) hasFlickrRequestForNode: (IMBFlickrNode*) node;
- (void) startLoadRequestForFlickrNode: (IMBFlickrNode*) node;
- (void) startLoadMoreRequestForFlickrNode: (IMBFlickrNode*) node;
// Query Persistence:
- (NSArray*) instantiateCustomQueriesWithRoot: (IMBFlickrNode*) root;
- (NSString*) metadataDescriptionForMetadata:(NSDictionary*)inMetadata;
@end
#pragma mark -
//----------------------------------------------------------------------------------------------------------------------
@implementation IMBFlickrParser
+ (void) load {
NSAutoreleasePool* pool = [[NSAutoreleasePool alloc] init];
[IMBParserController registerParserClass:self forMediaType:kIMBMediaTypeImage];
[pool drain];
}
- (void) dealloc {
_delegate = nil;
[self cancelAllPendingFlickrRequests];
IMBRelease (_customQueries);
IMBRelease (_flickrAPIKey);
IMBRelease (_flickrContext);
IMBRelease (_flickrSharedSecret);
IMBRelease (_flickrRequests);
IMBRelease (_loadMoreButton);
[super dealloc];
}
- (BOOL) canBeUsed {
BOOL result = (self.flickrAPIKey && ![self.flickrAPIKey isEqualToString:@""]
&& self.flickrSharedSecret && ![self.flickrSharedSecret isEqualToString:@""]);
// If these aren't available (provided by app delegate parserController:didLoadParser:forMediaType:)
// then this parser can't be used, so it will be unloaded.
return result;
}
#pragma mark
#pragma mark Actions
- (IBAction) editNode: (id) sender {
NSLog (@"edit node...");
}
- (IBAction) loadMoreImages: (id) sender {
NSString* nodeIdentifier = nil;
if ([sender isKindOfClass:[IMBLoadMoreObject class]]) {
nodeIdentifier = [sender nodeIdentifier];
}
if (!nodeIdentifier && [sender isKindOfClass:[NSMenuItem class]]) {
id obj = [sender representedObject];
if ([obj isKindOfClass:[NSString class]]) {
nodeIdentifier = obj;
}
}
if (nodeIdentifier) {
IMBLibraryController* libController = [IMBLibraryController sharedLibraryControllerWithMediaType:self.mediaType];
IMBFlickrNode* node = (IMBFlickrNode*) [libController nodeWithIdentifier:nodeIdentifier];
[self startLoadMoreRequestForFlickrNode:node];
} else {
NSLog (@"Can't handle this kind of node.");
}
}
- (IBAction) openFlickrPage: (id) sender {
if (![sender isKindOfClass:[NSMenuItem class]]) return;
id obj = [sender representedObject];
if ([obj isKindOfClass:[IMBObject class]]) {
IMBObject* imbObject = (IMBObject*) obj;
NSString *webPage = [[imbObject metadata] objectForKey:@"webPageURL"];
[[NSWorkspace imb_threadSafeWorkspace] openURL:[NSURL URLWithString:webPage]];
} else {
NSLog (@"Can't handle this kind of object.");
}
}
- (IBAction) copyFlickrPageURL: (id) sender {
if (![sender isKindOfClass:[NSMenuItem class]]) return;
id obj = [sender representedObject];
if ([obj isKindOfClass:[IMBObject class]]) {
IMBObject* imbObject = (IMBObject*) obj;
NSString *webPage = [[imbObject metadata] objectForKey:@"webPageURL"];
NSPasteboard *pb = [NSPasteboard generalPasteboard];
NSArray *types = [NSArray arrayWithObjects:NSStringPboardType, nil];
[pb declareTypes:types owner:self];
[pb setString:webPage forType:NSStringPboardType];
} else {
NSLog (@"Can't handle this kind of object.");
}
}
- (IBAction) copyAttribution: (id) sender {
if (![sender isKindOfClass:[NSMenuItem class]]) return;
id obj = [sender representedObject];
if ([obj isKindOfClass:[IMBObject class]]) {
IMBObject* imbObject = (IMBObject*) obj;
NSURL *shortWebPageURL = [NSURL URLWithString:[@"http://flic.kr/p/" stringByAppendingString:
[IMBFlickrNode base58EncodedValue:[[[imbObject metadata] objectForKey:@"id"] longLongValue]]]];
NSString *credit = [[obj metadata] objectForKey:@"ownername"];
NSString *format = NSLocalizedStringWithDefaultValue(@"IMBFlickrParser.format.attribution",nil,IMBBundle(),@"Photo by %@ - %@",@"Format string for attribution credit; flickr user followed by short URL");
NSString *attribution = [NSString stringWithFormat:format, credit, [shortWebPageURL absoluteString]];
NSPasteboard *pb = [NSPasteboard generalPasteboard];
NSArray *types = [NSArray arrayWithObjects:NSStringPboardType, nil];
[pb declareTypes:types owner:self];
[pb setString:attribution forType:NSStringPboardType];
} else {
NSLog (@"Can't handle this kind of object.");
}
}
- (IBAction) removeNode: (id) sender {
if (![sender isKindOfClass:[NSMenuItem class]]) return;
id obj = [sender representedObject];
if ([obj isKindOfClass:[NSString class]]) {
if (_customQueries.count > 0) {
[_customQueries removeLastObject];
}
[self saveCustomQueries];
[self reloadCustomQueries];
} else {
NSLog (@"Can't handle this kind of node.");
}
}
#pragma mark
#pragma mark Flickr Request Handling
- (void) cancelAllPendingFlickrRequests {
for (OFFlickrAPIRequest* request in _flickrRequests.allValues) {
[request cancel];
}
}
- (void) flickrAPIRequest: (OFFlickrAPIRequest*) inRequest
didCompleteWithResponse: (NSDictionary*) inResponseDictionary {
// get the node we associated with the request in flickrRequestWithContext: ...
NSString* nodeIdentifier = inRequest.sessionInfo;
IMBLibraryController* libController = [IMBLibraryController sharedLibraryControllerWithMediaType:self.mediaType];
IMBFlickrNode* node = (IMBFlickrNode*) [libController nodeWithIdentifier:nodeIdentifier];
// if the node does not exist any more, there is not much to do...
if (!node) return;
#ifdef VERBOSE
NSLog (@"Flickr request completed for node '%@'.", nodeIdentifier);
#endif
// save Flickr response in our iMB node for later population of the browser...
[node setFlickrResponse:(inResponseDictionary) ? inResponseDictionary : [NSDictionary dictionary]];
// force reloading of the node holding the Flickr images...
[libController reloadNode:node];
}
- (void) flickrAPIRequest: (OFFlickrAPIRequest*) inRequest
didFailWithError: (NSError*) inError {
NSLog (@"flickrAPIRequest:didFailWithError: %@", inError);
// TODO: Error Handling
}
+ (NSString*) flickrMethodForMethodCode: (NSInteger) code {
if (code == IMBFlickrNodeMethod_TagSearch || code == IMBFlickrNodeMethod_TextSearch) {
return @"flickr.photos.search";
} else if (code == IMBFlickrNodeMethod_Recent) {
return @"flickr.photos.getRecent";
} else if (code == IMBFlickrNodeMethod_MostInteresting) {
return @"flickr.interestingness.getList";
} else if (code == IMBFlickrNodeMethod_GetInfo) {
return @"flickr.photos.getInfo";
}
NSLog (@"Can't find Flickr method for method code.");
return nil;
}
- (OFFlickrAPIRequest*) flickrRequestWithNode: (IMBFlickrNode*) node {
// create our dictionary of flickr request lazily...
if (_flickrRequests == nil) {
_flickrRequests = [[NSMutableDictionary alloc] init];
}
OFFlickrAPIRequest* request = [_flickrRequests objectForKey:node.identifier];
if (!request) {
// create a Flickr request for the given iMB node...
request = [[OFFlickrAPIRequest alloc] initWithAPIContext:_flickrContext];
[_flickrRequests setObject:request forKey:node.identifier];
request.requestTimeoutInterval = 60.0f;
// [request setShouldWaitUntilDone:YES];
[request release];
// we save the iMB node in the Flickr request for use in
// flickrAPIRequest:didCompleteWithResponse: ...
request.sessionInfo = node.identifier;
}
return request;
}
- (BOOL) hasFlickrRequestForNode: (IMBFlickrNode*) node {
if (!node) return NO;
OFFlickrAPIRequest* request = [_flickrRequests objectForKey:node.identifier];
return request != nil;
}
- (void) startLoadRequestForFlickrNode: (IMBFlickrNode*) node {
[self performSelectorOnMainThread:@selector(startLoadRequestForFlickrNode_onMainThread:) withObject:node waitUntilDone:NO];
}
- (void) startLoadMoreRequestForFlickrNode: (IMBFlickrNode*) node {
node.page = node.page + 1;
[self performSelectorOnMainThread:@selector(startLoadRequestForFlickrNode_onMainThread:) withObject:node waitUntilDone:NO];
}
- (void) startLoadRequestForFlickrNode_onMainThread: (IMBFlickrNode*) node {
if (!node) return;
OFFlickrAPIRequest* request = [self flickrRequestWithNode:node];
if (![request isRunning]) {
[request setDelegate:self];
// Keep the 'populateNode:' loop quiet until we got our data. Also this will
// ensure that the parser is not called again and again to populate the node.
if (node.objects == nil) {
node.subNodes = [NSArray array];
node.objects = [NSArray array];
}
// compose and start Flickr request...
NSString* method = [self.class flickrMethodForMethodCode:node.method];
NSDictionary* arguments = [node argumentsForFlickrCall];
[request callAPIMethodWithGET:method arguments:arguments];
#ifdef VERBOSE
NSLog (@"Start Flickr request for node '%@' method: '%@' and query: '%@'.", node.identifier, method, node.query);
#endif
}
}
#pragma mark
#pragma mark Parser Methods
/// Create an empty "Flickr" root node.
- (IMBFlickrNode*) createRootNode {
// load Flickr icon...
NSBundle* ourBundle = [NSBundle bundleForClass:[IMBNode class]];
NSString* pathToImage = [ourBundle pathForResource:@"Flickr" ofType:@"tiff"];
NSImage* icon = [[[NSImage alloc] initWithContentsOfFile:pathToImage] autorelease];
// create root node...
IMBFlickrNode* rootNode = [[[IMBFlickrNode alloc] init] autorelease];
rootNode.mediaSource = nil;
rootNode.identifier = [self identifierForPath:@"/"];
rootNode.name = @"Flickr";
rootNode.icon = icon;
rootNode.parser = self;
rootNode.leaf = NO;
rootNode.isTopLevelNode = YES;
rootNode.groupType = kIMBGroupTypeInternet;
// Leaving subNodes and objects nil, will trigger a populateNode:options:error:
// as soon as the root node is opened.
rootNode.subNodes = nil;
rootNode.objects = nil;
rootNode.watcherType = kIMBWatcherTypeFirstCustom;
rootNode.watchedPath = (NSString*) rootNode.mediaSource;
return rootNode;
}
- (void) didClickObject: (IMBObject*) inObject objectView: (NSView*) inView {
if ([inObject isKindOfClass:[IMBLoadMoreObject class]]) {
[self loadMoreImages:inObject];
}
}
- (IMBNode*) nodeWithOldNode: (const IMBNode*) inOldNode
options: (IMBOptions) inOptions
error: (NSError**) outError {
if (!inOldNode) return [self createRootNode];
NSError* error = nil;
IMBFlickrNode* updatedNode = [[inOldNode copy] autorelease];
// If the old node was populated, then also populate the new node...
IMBFlickrNode* inOldFlickrNode = (IMBFlickrNode*) inOldNode;
if ([self hasFlickrRequestForNode:inOldFlickrNode] || inOldNode.isPopulated) {
[self populateNewNode:updatedNode likeOldNode:inOldNode options:inOptions];
}
if (outError) *outError = error;
return updatedNode;
}
// The supplied node is a private copy which may be modified here in the background operation. Parse the
// iPhoto XML file and create subnodes as needed...
- (BOOL) populateNode: (IMBNode*) inNode options: (IMBOptions) inOptions error: (NSError**) outError {
IMBFlickrNode* inFlickrNode = (IMBFlickrNode*) inNode;
#ifdef VERBOSE
NSLog (@"Populate node '%@', query '%@'", inFlickrNode.identifier, inFlickrNode.query);
#endif
if (inFlickrNode.isTopLevelNode) {
// populate root node...
NSArray* standardNodes = [NSArray arrayWithObjects:
[IMBFlickrNode flickrNodeForRecentPhotosForRoot:inFlickrNode parser:self],
[IMBFlickrNode flickrNodeForInterestingPhotosForRoot:inFlickrNode parser:self], nil];
inFlickrNode.subNodes = [standardNodes arrayByAddingObjectsFromArray:[self instantiateCustomQueriesWithRoot:inFlickrNode]];
// Put the queries into the objects, so that selecting top-level node will show queries as smart folders
NSUInteger index = 0;
NSMutableArray* objects = [NSMutableArray array];
for (IMBFlickrNode* node in inFlickrNode.subNodes) {
IMBSmartFolderNodeObject* object = [[IMBSmartFolderNodeObject alloc] init];
object.representedNodeIdentifier = node.identifier;
object.name = node.name;
object.metadata = nil;
object.parser = self;
object.index = index++;
object.imageLocation = nil;
object.imageRepresentationType = IKImageBrowserNSImageRepresentationType;
object.imageRepresentation = [IMBSmartFolderNodeObject icon];
[objects addObject:object];
[object release];
}
inFlickrNode.objects = objects;
} else {
// populate nodes with Flickr contents...
if ([inFlickrNode hasFlickrResponse]) {
[inFlickrNode processResponseForContext:_flickrContext];
[inFlickrNode clearFlickrResponse];
} else {
// the network access needs to be started on the main thread...
[self startLoadRequestForFlickrNode:inFlickrNode];
}
}
return YES;
}
- (void) willShowContextMenu: (NSMenu*) inMenu forNode: (IMBNode*) inNode {
if (![inNode isKindOfClass:[IMBFlickrNode class]]) return;
IMBFlickrNode* flickrNode = (IMBFlickrNode*) inNode;
// 'Load More'...
NSString* title = NSLocalizedStringWithDefaultValue(@"IMBFlickrParser.menu.loadmore",nil,IMBBundle(),@"Load More",@"Flickr parser node context menu title.");
title = [NSString stringWithFormat:title, flickrNode.name];
NSMenuItem* loadMoreItem = [[NSMenuItem alloc] initWithTitle:title
action:@selector(loadMoreImages:)
keyEquivalent:@""];
[loadMoreItem setTarget:self];
[loadMoreItem setRepresentedObject:flickrNode.identifier];
[inMenu addItem:loadMoreItem];
[loadMoreItem release];
// you can edit the custom nodes only...
if (!flickrNode.isCustomNode) return;
[inMenu addItem:[NSMenuItem separatorItem]];
// 'Edit'...
title = NSLocalizedStringWithDefaultValue(@"IMBFlickrParser.menu.edit",nil,IMBBundle(),@"Edit",@"Flickr parser node context menu title.");
title = [NSString stringWithFormat:title, flickrNode.name];
NSMenuItem* editNodeItem = [[NSMenuItem alloc] initWithTitle:title
action:@selector(editNode:)
keyEquivalent:@""];
[editNodeItem setTarget:self];
[editNodeItem setRepresentedObject:flickrNode.identifier];
[inMenu addItem:editNodeItem];
[editNodeItem release];
// 'Remove'...
title = NSLocalizedStringWithDefaultValue(@"IMBFlickrParser.menu.remove",nil,IMBBundle(),@"Remove '%@'",@"Flickr parser node context menu title.");
title = [NSString stringWithFormat:title, flickrNode.name];
NSMenuItem* removeNode = [[NSMenuItem alloc] initWithTitle:title
action:@selector(removeNode:)
keyEquivalent:@""];
[removeNode setTarget:self];
[removeNode setRepresentedObject:flickrNode.identifier];
[inMenu addItem:removeNode];
[removeNode release];
}
- (void) willShowContextMenu: (NSMenu*) inMenu forObject: (IMBObject*) inObject {
// 'Open Flickr Page'...
NSString *title = nil;
NSMenuItem *item = nil;
if ([inObject isSelectable])
{
if (![[[inObject metadata] objectForKey:@"can_download"] boolValue])
{
// This will replace the Download / Open With Browser that is usually shown
title = NSLocalizedStringWithDefaultValue(@"IMBFlickrParser.menu.downloadingNotPermitted",nil,IMBBundle(),@"Downloading Not Permitted",@"Context menu item title to warn of not being downloadable");
item = [[[NSMenuItem alloc] initWithTitle:title
action:nil
keyEquivalent:@""] autorelease];
[item setTarget:self];
[item setRepresentedObject:inObject];
[inMenu insertItem:item atIndex:0];
[inMenu insertItem:[NSMenuItem separatorItem] atIndex:1];
}
title = NSLocalizedStringWithDefaultValue(@"IMBFlickrParser.menu.openflickrpage",nil,IMBBundle(),@"Open Flickr Page",@"Flickr parser node context menu title.");
item = [[[NSMenuItem alloc] initWithTitle:title
action:@selector(openFlickrPage:)
keyEquivalent:@""] autorelease];
[item setTarget:self];
[item setRepresentedObject:inObject];
[inMenu addItem:item];
title = NSLocalizedStringWithDefaultValue(@"IMBFlickrParser.menu.copyFlickrURL",nil,IMBBundle(),@"Copy Flickr Page URL",@"Flickr parser node context menu title.");
item = [[[NSMenuItem alloc] initWithTitle:title
action:@selector(copyFlickrPageURL:)
keyEquivalent:@""] autorelease];
[item setTarget:self];
[item setRepresentedObject:inObject];
[inMenu addItem:item];
title = NSLocalizedStringWithDefaultValue(@"IMBFlickrParser.menu.copyAttribution",nil,IMBBundle(),@"Copy Attribution",@"Flickr parser node context menu title.");
item = [[[NSMenuItem alloc] initWithTitle:title
action:@selector(copyAttribution:)
keyEquivalent:@""] autorelease];
[item setTarget:self];
[item setRepresentedObject:inObject];
[inMenu addItem:item];
}
}
- (void) willUseParser {
[super willUseParser];
// lazy initialize the flickr context...
if (_flickrContext == nil) {
NSAssert (self.flickrAPIKey, @"Flickr API key property not set!");
NSAssert (self.flickrSharedSecret, @"Flickr shared secret property not set!");
_flickrContext = [[OFFlickrAPIContext alloc] initWithAPIKey:self.flickrAPIKey sharedSecret:self.flickrSharedSecret];
[self loadCustomQueries];
// setup desired size to get, from delegate
self.desiredSize = [IMBConfig flickrDownloadSize];
}
// lazy initialize the 'load more' button...
if (_loadMoreButton == nil) {
_loadMoreButton = [[IMBLoadMoreObject alloc] init];
_loadMoreButton.clickAction = @selector (loadMoreImages:);
_loadMoreButton.parser = self;
_loadMoreButton.target = self;
}
}
/// For Flickr we need a remote promise that downloads the files off the internet
- (IMBObjectsPromise*) objectPromiseWithObjects: (NSArray*) inObjects {
return [[[IMBRemoteObjectsPromise alloc] initWithIMBObjects:inObjects] autorelease];
}
/// Convert metadata into human readable string.
- (void) loadMetadataForObject:(IMBObject*)inObject
{
NSDictionary* metadata = inObject.preliminaryMetadata;
NSString* description = metadata ? [self metadataDescriptionForMetadata:metadata] : @"";
if ([NSThread isMainThread])
{
inObject.metadata = metadata;
inObject.metadataDescription = description;
}
else
{
NSArray* modes = [NSArray arrayWithObject:NSRunLoopCommonModes];
[inObject performSelectorOnMainThread:@selector(setMetadata:) withObject:metadata waitUntilDone:NO modes:modes];
[inObject performSelectorOnMainThread:@selector(setMetadataDescription:) withObject:description waitUntilDone:NO modes:modes];
}
}
- (NSString*) metadataDescriptionForMetadata:(NSDictionary*)inMetadata
{
BOOL canDownload = [[inMetadata objectForKey:@"can_download"] boolValue];
NSString* ownername = [inMetadata objectForKey:@"ownername"];
NSString* tags = [inMetadata objectForKey:@"tags"];
NSString* info = [NSImage imb_imageMetadataDescriptionForMetadata:inMetadata];
NSMutableString* description = [NSMutableString string];
if (!canDownload)
{
if (description.length > 0) [description imb_appendNewline];
[description appendFormat:@"%@",
NSLocalizedStringWithDefaultValue(
@"IMBFlickrParser.menu.downloadingNotPermitted",
nil,IMBBundle(),
@"Downloading Not Permitted",
@"Context menu item title to warn of not being downloadable")];
}
if (ownername && ![ownername isEqualToString:@""])
{
NSString* artist = NSLocalizedStringWithDefaultValue(
@"Owner",
nil,IMBBundle(),
@"Owner",
@"Owner (for flickr photographs) label in metadataDescription");
if (description.length > 0) [description imb_appendNewline];
[description appendFormat:@"%@: %@",artist,ownername];
}
if (info)
{
if (description.length > 0) [description imb_appendNewline];
[description appendFormat:@"%@",info];
}
if (tags && ![tags isEqualToString:@""])
{
NSString* tagsLabel = NSLocalizedStringWithDefaultValue(
@"Tags",
nil,IMBBundle(),
@"Tags",
@"Tags (Flickr term for keywords) label in metadataDescription");
if (description.length > 0) [description imb_appendNewline];
[description appendFormat:@"%@: %@",tagsLabel,tags];
}
return description;
}
//----------------------------------------------------------------------------------------------------------------------
- (NSViewController*) customHeaderViewControllerForNode:(IMBNode*)inNode
{
IMBFlickrHeaderViewController* controller = nil;
// The root node has a custom view that we are loading from a nib file...
if ([inNode.identifier isEqualToString:[self identifierForPath:@"/"]])
{
controller = [IMBFlickrHeaderViewController headerViewControllerWithParser:self forNode:(IMBFlickrNode*)inNode];
controller.queryAction = @selector(addQuery:);
controller.buttonAction = @selector(addQuery:);
controller.buttonTitle = NSLocalizedStringWithDefaultValue(@"IMBFlickrParser.button.add",nil,IMBBundle(),@"Add",@"Button title in Flickr Options");
}
else
{
controller = [IMBFlickrHeaderViewController headerViewControllerWithParser:self forNode:(IMBFlickrNode*)inNode];
controller.queryAction = @selector(editQuery:);
controller.buttonAction = @selector(removeQuery:);
controller.buttonTitle = NSLocalizedStringWithDefaultValue(@"IMBFlickrParser.button.remove",nil,IMBBundle(),@"Remove",@"Button title in Flickr Options");
}
return controller;
}
//----------------------------------------------------------------------------------------------------------------------
#pragma mark
#pragma mark Properties
@synthesize customQueries = _customQueries;
@synthesize delegate = _delegate;
@synthesize flickrAPIKey = _flickrAPIKey;
@synthesize flickrSharedSecret = _flickrSharedSecret;
@synthesize desiredSize = _desiredSize;
- (IMBLoadMoreObject*) loadMoreButton {
return _loadMoreButton;
}
- (IMBFlickrNode*) flickrRootNode {
IMBLibraryController* libController = [IMBLibraryController sharedLibraryControllerWithMediaType:[self mediaType]];
IMBFlickrNode* root = (IMBFlickrNode*) [libController nodeWithIdentifier:[self identifierForPath:@"/"]];
return root;
}
#pragma mark
#pragma mark Query Persistence
NSString* const IMBFlickrParserPrefKey_CustomQueries = @"customQueries";
- (NSArray*) instantiateCustomQueriesWithRoot: (IMBFlickrNode*) root {
NSMutableArray* customNodes = [NSMutableArray array];
// create nodes from settings...
for (NSDictionary* dict in self.customQueries) {
// try to copy all existing nodes...
NSString* identifier = [IMBFlickrNode identifierWithQueryParams:dict];
IMBFlickrNode* node = (IMBFlickrNode*)[root subNodeWithIdentifier:identifier];
// if node does not exists yet (add operation) create a new one...
if (!node) {
node = [IMBFlickrNode flickrNodeFromDictionary:dict rootNode:root parser:self];
}
if (node) {
[customNodes addObject:node];
}
}
return customNodes;
}
- (void) addCustomQuery: (NSDictionary*) inQueryParams {
if (!inQueryParams) return;
[self.customQueries addObject:inQueryParams];
}
- (void) removeCustomQuery: (NSDictionary*) inQueryParams {
if (!inQueryParams) return;
// 1) Remove the custom query from our preferences.
NSString* queryIdentifier = [inQueryParams objectForKey:IMBFlickrNodeProperty_UUID];
NSDictionary* dictToBeRemoved = nil;
for (NSDictionary* dict in _customQueries) {
NSString* dictIdentifier = [IMBFlickrNode identifierWithQueryParams:dict];
if (dictIdentifier && [queryIdentifier hasSuffix:dictIdentifier]) {
dictToBeRemoved = dict;
}
}
if (dictToBeRemoved) {
[_customQueries removeObject:dictToBeRemoved];
}
// 2) Reload root node to show changes.
IMBLibraryController* libController = [IMBLibraryController sharedLibraryControllerWithMediaType:[self mediaType]];
[libController reloadNode:self.flickrRootNode];
}
- (void) loadCustomQueries {
NSDictionary* prefs = [IMBConfig prefsForClass:[self class]];
// try to read user defaults...
NSArray* nodes = [prefs objectForKey:IMBFlickrParserPrefKey_CustomQueries];
// setup default user nodes...
if (!nodes && _delegate && [_delegate respondsToSelector:@selector(flickrParserSetupDefaultQueries:)])
{
nodes = [_delegate flickrParserSetupDefaultQueries:self];
}
if (nodes == nil) nodes = [NSArray array];
self.customQueries = [[nodes mutableCopy] autorelease];
}
- (void) reloadCustomQueries {
IMBLibraryController* libController = [IMBLibraryController sharedLibraryControllerWithMediaType:[self mediaType]];
[libController reloadNode:self.flickrRootNode];
}
- (void) reloadCustomQuery: (NSDictionary*) inQueryParams {
if (!inQueryParams) return;
NSString* nodeIdentifier = [inQueryParams objectForKey:IMBFlickrNodeProperty_UUID];
if (!nodeIdentifier) return;
IMBLibraryController* libController = [IMBLibraryController sharedLibraryControllerWithMediaType:[self mediaType]];
// if the node does not exist any more, there is not much to do...
IMBFlickrNode* node = (IMBFlickrNode*) [libController nodeWithIdentifier:nodeIdentifier];
if (!node) return;
// update node with new query data...
[node readPropertiesFromDictionary:inQueryParams];
// clean existing results...
node.subNodes = nil;
node.objects = nil;
// force reloading of the node holding the Flickr images...
[libController reloadNode:node];
}
- (void) saveCustomQueries {
NSMutableDictionary* prefs = [NSMutableDictionary dictionaryWithDictionary:[IMBConfig prefsForClass:[self class]]];
[prefs setObject:self.customQueries forKey:IMBFlickrParserPrefKey_CustomQueries];
[IMBConfig setPrefs:prefs forClass:[self class]];
}
- (void) updateCustomQuery: (NSDictionary*) inQueryParams {
if (!inQueryParams) return;
NSString* queryIdentifier = [inQueryParams objectForKey:IMBFlickrNodeProperty_UUID];
NSUInteger count = _customQueries.count;
for (NSUInteger index = 0; index < count; index++) {
NSDictionary* dict = [_customQueries objectAtIndex:index];
NSString* dictIdentifier = [IMBFlickrNode identifierWithQueryParams:dict];
if (dictIdentifier && [queryIdentifier hasSuffix:dictIdentifier]) {
[_customQueries replaceObjectAtIndex:index withObject:inQueryParams];
break;
}
}
}
@end