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"/>
</connections>
</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">
<modifierMask key="keyEquivalentModifierMask"/>
<connections>

View File

@ -11,6 +11,9 @@
#import "Menu.h"
static NSString *Extensions[] = { @"zip", @"7z" };
enum {
kTagZip = 1,
kTag7z = 2,
@ -90,6 +93,7 @@ enum {
@property NSUInteger status;
@property NSUInteger index;
@property NSUInteger fileSize;
@property (readonly) NSColor *titleColor;
@property (readonly) NSColor *descriptionColor;
@ -99,6 +103,8 @@ enum {
-(void)beginDownloadWithTask:(NSURLSessionDownloadTask *)task;
-(void)completeWithError: (NSError *)error;
-(NSString *)statusDescription;
-(void)refresh: (NSURL *)localURL;
@end
@ -422,25 +428,26 @@ enum {
NSFileManager *fm = [NSFileManager defaultManager];
for (DownloadItem *item in _items) {
NSDictionary *attr = nil;
NSString *value = [item value];
NSString *s = [romdir stringByAppendingPathComponent: value];
NSString *path;
path = [s stringByAppendingPathExtension: @"zip"];
if ([fm fileExistsAtPath: path]) {
[item setStatus: ItemFound];
[item setLocalURL: [NSURL fileURLWithPath: path]];
continue;
for (unsigned i = 0; i < 2; ++i) {
path = [s stringByAppendingPathExtension: Extensions[i]];
attr = [fm attributesOfItemAtPath: path error: nil];
if (attr) break;
}
path = [s stringByAppendingPathExtension: @"7z"];
if ([fm fileExistsAtPath: path]) {
if (attr) {
[item setStatus: ItemFound];
[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]];
}
- (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 {
DownloadItem *item = [self clickedItem];
if (!item) return;
@ -588,7 +612,8 @@ static NSInteger TaskStatusCode(NSURLSessionTask *task) {
dispatch_async(dispatch_get_main_queue(), ^(void){
NSMutableDictionary *taskIndex = self->_taskIndex;
DownloadItem *item = [taskIndex objectForKey: task];
[item setLocalURL: dest];
[item refresh: dest];
});
NSLog(@"%@", src);
@ -620,11 +645,11 @@ static NSInteger TaskStatusCode(NSURLSessionTask *task) {
};
status = SecItemCopyMatching((CFDictionaryRef)query, &item);
NSLog(@"%@", query);
//NSLog(@"%@", query);
if (status != 0) return nil;
NSDictionary *d = (__bridge NSDictionary *)item;
NSLog(@"%@", d);
//NSLog(@"%@", d);
NSString *account = [d objectForKey: (id)kSecAttrAccount];
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 {
return [NSSet setWithObjects: @"error", @"status", nil];
}
@ -743,26 +804,33 @@ enum {
- (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];
if (row < 0) return NO;
DownloadItem *item = [[_arrayController arrangedObjects] objectAtIndex: row]; //[_items objectAtIndex: row];
#endif
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;
}