|
| 1 | +// |
| 2 | +// FinderSync.m |
| 3 | +// FinderSync |
| 4 | +// |
| 5 | +// Created by Gabriel on 6/10/15. |
| 6 | +// Copyright (c) 2015 Keybase. All rights reserved. |
| 7 | +// |
| 8 | + |
| 9 | +#import "FinderSync.h" |
| 10 | + |
| 11 | +@interface FinderSync () |
| 12 | + |
| 13 | +@property NSURL *myFolderURL; |
| 14 | + |
| 15 | +@end |
| 16 | + |
| 17 | +@implementation FinderSync |
| 18 | + |
| 19 | +- (instancetype)init { |
| 20 | + self = [super init]; |
| 21 | + |
| 22 | + NSLog(@"%s launched from %@ ; compiled at %s", __PRETTY_FUNCTION__, [[NSBundle mainBundle] bundlePath], __TIME__); |
| 23 | + |
| 24 | + // Set up the directory we are syncing. |
| 25 | + self.myFolderURL = [NSURL fileURLWithPath:@"/Users/Shared/MySyncExtension Documents"]; |
| 26 | + [FIFinderSyncController defaultController].directoryURLs = [NSSet setWithObject:self.myFolderURL]; |
| 27 | + |
| 28 | + // Set up images for our badge identifiers. For demonstration purposes, this uses off-the-shelf images. |
| 29 | + [[FIFinderSyncController defaultController] setBadgeImage:[NSImage imageNamed: NSImageNameColorPanel] label:@"Status One" forBadgeIdentifier:@"One"]; |
| 30 | + [[FIFinderSyncController defaultController] setBadgeImage:[NSImage imageNamed: NSImageNameCaution] label:@"Status Two" forBadgeIdentifier:@"Two"]; |
| 31 | + |
| 32 | + return self; |
| 33 | +} |
| 34 | + |
| 35 | +#pragma mark - Primary Finder Sync protocol methods |
| 36 | + |
| 37 | +- (void)beginObservingDirectoryAtURL:(NSURL *)url { |
| 38 | + // The user is now seeing the container's contents. |
| 39 | + // If they see it in more than one view at a time, we're only told once. |
| 40 | + NSLog(@"beginObservingDirectoryAtURL:%@", url.filePathURL); |
| 41 | +} |
| 42 | + |
| 43 | + |
| 44 | +- (void)endObservingDirectoryAtURL:(NSURL *)url { |
| 45 | + // The user is no longer seeing the container's contents. |
| 46 | + NSLog(@"endObservingDirectoryAtURL:%@", url.filePathURL); |
| 47 | +} |
| 48 | + |
| 49 | +- (void)requestBadgeIdentifierForURL:(NSURL *)url { |
| 50 | + NSLog(@"requestBadgeIdentifierForURL:%@", url.filePathURL); |
| 51 | + |
| 52 | + // For demonstration purposes, this picks one of our two badges, or no badge at all, based on the filename. |
| 53 | + NSInteger whichBadge = [url.filePathURL hash] % 3; |
| 54 | + NSString* badgeIdentifier = @[@"", @"One", @"Two"][whichBadge]; |
| 55 | + [[FIFinderSyncController defaultController] setBadgeIdentifier:badgeIdentifier forURL:url]; |
| 56 | +} |
| 57 | + |
| 58 | +#pragma mark - Menu and toolbar item support |
| 59 | + |
| 60 | +- (NSString *)toolbarItemName { |
| 61 | + return @"FinderSync"; |
| 62 | +} |
| 63 | + |
| 64 | +- (NSString *)toolbarItemToolTip { |
| 65 | + return @"FinderSync: Click the toolbar item for a menu."; |
| 66 | +} |
| 67 | + |
| 68 | +- (NSImage *)toolbarItemImage { |
| 69 | + return [NSImage imageNamed:NSImageNameCaution]; |
| 70 | +} |
| 71 | + |
| 72 | +- (NSMenu *)menuForMenuKind:(FIMenuKind)whichMenu { |
| 73 | + // Produce a menu for the extension. |
| 74 | + NSMenu *menu = [[NSMenu alloc] initWithTitle:@""]; |
| 75 | + [menu addItemWithTitle:@"Example Menu Item" action:@selector(sampleAction:) keyEquivalent:@""]; |
| 76 | + |
| 77 | + return menu; |
| 78 | +} |
| 79 | + |
| 80 | +- (IBAction)sampleAction:(id)sender { |
| 81 | + NSURL* target = [[FIFinderSyncController defaultController] targetedURL]; |
| 82 | + NSArray* items = [[FIFinderSyncController defaultController] selectedItemURLs]; |
| 83 | + |
| 84 | + NSLog(@"sampleAction: menu item: %@, target = %@, items = ", [sender title], [target filePathURL]); |
| 85 | + [items enumerateObjectsUsingBlock: ^(id obj, NSUInteger idx, BOOL *stop) { |
| 86 | + NSLog(@" %@", [obj filePathURL]); |
| 87 | + }]; |
| 88 | +} |
| 89 | + |
| 90 | +@end |
| 91 | + |
0 commit comments