add move to trash menu option

keep file size (will display later)
This commit is contained in:
Kelvin Sherlock 2022-03-29 11:57:19 -04:00
parent 5a0fdd5a7e
commit 8707b4a246
2 changed files with 156 additions and 82 deletions

View File

@ -298,6 +298,12 @@
<action selector="showInFinder:" target="-2" id="ltL-yL-7EP"/> <action selector="showInFinder:" target="-2" id="ltL-yL-7EP"/>
</connections> </connections>
</menuItem> </menuItem>
<menuItem title="Move to Trash" id="x1Q-OF-5hM">
<modifierMask key="keyEquivalentModifierMask"/>
<connections>
<action selector="moveToTrash:" target="-2" id="swr-yf-hA2"/>
</connections>
</menuItem>
<menuItem title="Download" tag="2" id="ree-Zg-jYB"> <menuItem title="Download" tag="2" id="ree-Zg-jYB">
<modifierMask key="keyEquivalentModifierMask"/> <modifierMask key="keyEquivalentModifierMask"/>
<connections> <connections>

View File

@ -11,6 +11,9 @@
#import "Menu.h" #import "Menu.h"
static NSString *Extensions[] = { @"zip", @"7z" };
enum { enum {
kTagZip = 1, kTagZip = 1,
kTag7z = 2, kTag7z = 2,
@ -90,6 +93,7 @@ enum {
@property NSUInteger status; @property NSUInteger status;
@property NSUInteger index; @property NSUInteger index;
@property NSUInteger fileSize;
@property (readonly) NSColor *titleColor; @property (readonly) NSColor *titleColor;
@property (readonly) NSColor *descriptionColor; @property (readonly) NSColor *descriptionColor;
@ -99,6 +103,8 @@ enum {
-(void)beginDownloadWithTask:(NSURLSessionDownloadTask *)task; -(void)beginDownloadWithTask:(NSURLSessionDownloadTask *)task;
-(void)completeWithError: (NSError *)error; -(void)completeWithError: (NSError *)error;
-(NSString *)statusDescription; -(NSString *)statusDescription;
-(void)refresh: (NSURL *)localURL;
@end @end
@ -422,25 +428,26 @@ enum {
NSFileManager *fm = [NSFileManager defaultManager]; NSFileManager *fm = [NSFileManager defaultManager];
for (DownloadItem *item in _items) { for (DownloadItem *item in _items) {
NSDictionary *attr = nil;
NSString *value = [item value]; NSString *value = [item value];
NSString *s = [romdir stringByAppendingPathComponent: value]; NSString *s = [romdir stringByAppendingPathComponent: value];
NSString *path; NSString *path;
path = [s stringByAppendingPathExtension: @"zip"];
if ([fm fileExistsAtPath: path]) { for (unsigned i = 0; i < 2; ++i) {
[item setStatus: ItemFound]; path = [s stringByAppendingPathExtension: Extensions[i]];
[item setLocalURL: [NSURL fileURLWithPath: path]]; attr = [fm attributesOfItemAtPath: path error: nil];
continue; if (attr) break;
} }
path = [s stringByAppendingPathExtension: @"7z"]; if (attr) {
if ([fm fileExistsAtPath: path]) {
[item setStatus: ItemFound]; [item setStatus: ItemFound];
[item setLocalURL: [NSURL fileURLWithPath: path]]; [item setLocalURL: [NSURL fileURLWithPath: path]];
continue; [item setFileSize: [attr fileSize]];
} else {
[item setStatus: ItemMissing];
[item setLocalURL: nil];
[item setFileSize: 0];
} }
[item setStatus: ItemMissing];
[item setLocalURL: nil];
} }
} }
@ -454,6 +461,23 @@ enum {
[ws activateFileViewerSelectingURLs: @[url]]; [ws activateFileViewerSelectingURLs: @[url]];
} }
- (IBAction)moveToTrash:(id)sender {
NSError *error = nil;
DownloadItem *item = [self clickedItem];
if (!item) return;
NSURL *url = [item localURL];
if (!url) return;
NSFileManager *fm = [NSFileManager defaultManager];
if ([fm trashItemAtURL: url resultingItemURL: NULL error: &error]) {
[item refresh: nil];
} else {
[self presentError: error];
}
}
- (IBAction)download:(id)sender { - (IBAction)download:(id)sender {
DownloadItem *item = [self clickedItem]; DownloadItem *item = [self clickedItem];
if (!item) return; if (!item) return;
@ -588,7 +612,8 @@ static NSInteger TaskStatusCode(NSURLSessionTask *task) {
dispatch_async(dispatch_get_main_queue(), ^(void){ dispatch_async(dispatch_get_main_queue(), ^(void){
NSMutableDictionary *taskIndex = self->_taskIndex; NSMutableDictionary *taskIndex = self->_taskIndex;
DownloadItem *item = [taskIndex objectForKey: task]; DownloadItem *item = [taskIndex objectForKey: task];
[item setLocalURL: dest];
[item refresh: dest];
}); });
NSLog(@"%@", src); NSLog(@"%@", src);
@ -620,11 +645,11 @@ static NSInteger TaskStatusCode(NSURLSessionTask *task) {
}; };
status = SecItemCopyMatching((CFDictionaryRef)query, &item); status = SecItemCopyMatching((CFDictionaryRef)query, &item);
NSLog(@"%@", query); //NSLog(@"%@", query);
if (status != 0) return nil; if (status != 0) return nil;
NSDictionary *d = (__bridge NSDictionary *)item; NSDictionary *d = (__bridge NSDictionary *)item;
NSLog(@"%@", d); //NSLog(@"%@", d);
NSString *account = [d objectForKey: (id)kSecAttrAccount]; NSString *account = [d objectForKey: (id)kSecAttrAccount];
NSData *passwordData = [d objectForKey: (id)kSecValueData]; NSData *passwordData = [d objectForKey: (id)kSecValueData];
@ -697,6 +722,42 @@ static NSInteger TaskStatusCode(NSURLSessionTask *task) {
} }
} }
-(void)refresh: (NSURL *)localURL {
NSDictionary *attr = nil;
NSError * error = nil;
NSFileManager *fm = [NSFileManager defaultManager];
if (localURL) {
attr = [fm attributesOfItemAtPath: [localURL path] error: &error];
} else {
NSString *romdir = [SupportDirectoryPath() stringByAppendingPathComponent: @"roms"];
NSString *s = [romdir stringByAppendingPathComponent: _value];
NSString *path = nil;
for (unsigned i = 0; i < 2; ++i) {
path = [s stringByAppendingPathExtension: Extensions[i]];
attr = [fm attributesOfItemAtPath: path error: nil];
if (attr) {
localURL = [NSURL fileURLWithPath: path];
break;
}
}
}
if (attr && localURL) {
[self setLocalURL: localURL];
[self setFileSize: [attr fileSize]];
[self setStatus: ItemFound];
} else {
[self setLocalURL: nil];
[self setFileSize: 0];
if (_status == ItemFound || _status == ItemDownloaded)
[self setStatus: ItemMissing];
}
}
+(NSSet *)keyPathsForValuesAffectingStatusDescription { +(NSSet *)keyPathsForValuesAffectingStatusDescription {
return [NSSet setWithObjects: @"error", @"status", nil]; return [NSSet setWithObjects: @"error", @"status", nil];
} }
@ -743,26 +804,33 @@ enum {
- (BOOL)validateMenuItem:(NSMenuItem *)menuItem { - (BOOL)validateMenuItem:(NSMenuItem *)menuItem {
if ([menuItem action] == @selector(downloadExtensionChanged:)) return YES; SEL action = [menuItem action];
if (action == @selector(downloadExtensionChanged:)) return YES;
DownloadItem *item = [self clickedItem];
if (!item) return NO;
#if 0
NSInteger row = [_tableView clickedRow]; NSInteger row = [_tableView clickedRow];
if (row < 0) return NO; if (row < 0) return NO;
DownloadItem *item = [[_arrayController arrangedObjects] objectAtIndex: row]; //[_items objectAtIndex: row]; DownloadItem *item = [[_arrayController arrangedObjects] objectAtIndex: row]; //[_items objectAtIndex: row];
#endif
NSUInteger status = [item status]; NSUInteger status = [item status];
switch([menuItem tag]) {
case kOpenInFinder:
return status == ItemFound || status == ItemDownloaded;
break;
case kDownload:
return YES;
//return status == ItemMissing || status == ItemError || status == ItemCanceled;
break;
case kCancel:
return status == ItemDownloading;
break;
if (action == @selector(moveToTrash:)) {
return [item localURL] != nil;
} }
if (action == @selector(showInFinder:)) {
return [item localURL] != nil;
}
if (action == @selector(download:)) {
return YES;
}
if (action == @selector(cancel:)) {
return status == ItemDownloading;
}
return NO; return NO;
} }