From 9bac5c102fc95dc13852ebd5c9982d40dc5a3e0b Mon Sep 17 00:00:00 2001 From: Michael Starke Date: Wed, 6 Feb 2019 18:07:53 +0100 Subject: [PATCH] Plugin repository browser now supports downloading and showing plugins. --- .../MPPluginRepositoryBrowserViewController.m | 31 ++++++++++++------- 1 file changed, 19 insertions(+), 12 deletions(-) diff --git a/MacPass/MPPluginRepositoryBrowserViewController.m b/MacPass/MPPluginRepositoryBrowserViewController.m index 623cf9af..096de07b 100644 --- a/MacPass/MPPluginRepositoryBrowserViewController.m +++ b/MacPass/MPPluginRepositoryBrowserViewController.m @@ -21,17 +21,10 @@ typedef NS_ENUM(NSUInteger, MPPluginTableColumn) { MPPluginTableColumnStatus }; -typedef NS_ENUM(NSUInteger, MPPluginActionState) { - MPPluginActionStateStartDownload, - MPPluginActionStateDownloadInProgress, - MPPluginActionStateDownloadFinished, - MPPluginActionStateDownloadFailed -}; - @interface MPPluginRepositoryBrowserViewController () @property (copy) NSArray* repositoryItems; -@property (copy) NSMutableDictionary *itemStatus; +@property (strong) NSMutableSet *downloadedItems; @property (strong) IBOutlet NSTableView *itemTable; @property (strong) IBOutlet NSTextField *updatedAtTextField; @@ -45,7 +38,7 @@ typedef NS_ENUM(NSUInteger, MPPluginActionState) { - (void)viewDidLoad { [super viewDidLoad]; - self.itemStatus = [[NSMutableDictionary alloc] init]; + self.downloadedItems = [[NSMutableSet alloc] init]; [self.updatedAtTextField bind:NSValueBinding toObject:MPPluginRepository.defaultRepository withKeyPath:NSStringFromSelector(@selector(updatedAt)) options:nil]; [self _refreshRepository]; } @@ -57,7 +50,14 @@ typedef NS_ENUM(NSUInteger, MPPluginActionState) { - (void)executePluginAction:(id)sender { NSInteger tableRow = [self.itemTable rowForView:sender]; if(tableRow > -1 && tableRow < self.repositoryItems.count) { - [self _downloadPluginForRow:tableRow]; + MPPluginRepositoryItem *item = self.repositoryItems[tableRow]; + if([self.downloadedItems containsObject:item.bundleIdentifier]) { + NSURL *downloadsURL = [NSFileManager.defaultManager URLsForDirectory:NSDownloadsDirectory inDomains:NSUserDomainMask].firstObject; + [NSWorkspace.sharedWorkspace openURL:downloadsURL]; + } + else { + [self _downloadPluginForRow:tableRow]; + } } } @@ -133,9 +133,16 @@ typedef NS_ENUM(NSUInteger, MPPluginActionState) { if(httpResponse.statusCode == 200 && location != nil) { NSURL *downloadFolderURL = [NSFileManager.defaultManager URLsForDirectory:NSDownloadsDirectory inDomains:NSUserDomainMask].firstObject; NSURL *fileURL = [downloadFolderURL URLByAppendingPathComponent:httpResponse.suggestedFilename]; - BOOL movedFile = [NSFileManager.defaultManager moveItemAtURL:location toURL:fileURL error:&error]; - if(movedFile) { + if([fileURL checkResourceIsReachableAndReturnError:&error]) { title = NSLocalizedString(@"PLUGIN_BROWSER_ACTION_SHOW_DOWNLOADED_FILE", "Label for the button to show a downloaded file"); + [self.downloadedItems addObject:item.bundleIdentifier]; + } + else if([NSFileManager.defaultManager moveItemAtURL:location toURL:fileURL error:&error]) { + title = NSLocalizedString(@"PLUGIN_BROWSER_ACTION_SHOW_DOWNLOADED_FILE", "Label for the button to show a downloaded file"); + [self.downloadedItems addObject:item.bundleIdentifier]; + } + else { + // more error handling } } dispatch_async(dispatch_get_main_queue(), ^{