2022-01-20 18:27:56 +00:00
|
|
|
// This file Copyright © 2010-2022 Transmission authors and contributors.
|
|
|
|
// It may be used under the MIT (SPDX: MIT) license.
|
|
|
|
// License text can be found in the licenses/ folder.
|
2010-03-06 23:12:30 +00:00
|
|
|
|
|
|
|
#import "InfoFileViewController.h"
|
|
|
|
#import "FileListNode.h"
|
|
|
|
#import "FileOutlineController.h"
|
|
|
|
#import "FileOutlineView.h"
|
|
|
|
#import "Torrent.h"
|
|
|
|
|
2022-01-24 01:32:45 +00:00
|
|
|
@interface InfoFileViewController ()
|
2010-03-06 23:12:30 +00:00
|
|
|
|
2022-04-29 22:51:40 +00:00
|
|
|
@property(nonatomic, copy) NSArray<Torrent*>* fTorrents;
|
2022-02-22 16:04:20 +00:00
|
|
|
|
|
|
|
@property(nonatomic) BOOL fSet;
|
|
|
|
|
|
|
|
@property(nonatomic) IBOutlet FileOutlineController* fFileController;
|
|
|
|
|
|
|
|
@property(nonatomic) IBOutlet NSSearchField* fFileFilterField;
|
|
|
|
@property(nonatomic) IBOutlet NSButton* fCheckAllButton;
|
|
|
|
@property(nonatomic) IBOutlet NSButton* fUncheckAllButton;
|
|
|
|
|
2021-08-15 09:41:48 +00:00
|
|
|
- (void)setupInfo;
|
2010-03-09 02:26:52 +00:00
|
|
|
|
2021-08-15 09:41:48 +00:00
|
|
|
- (BOOL)canQuickLookFile:(FileListNode*)item;
|
2010-03-06 23:12:30 +00:00
|
|
|
|
|
|
|
@end
|
|
|
|
|
|
|
|
@implementation InfoFileViewController
|
|
|
|
|
2021-08-15 09:41:48 +00:00
|
|
|
- (instancetype)init
|
2010-03-06 23:12:30 +00:00
|
|
|
{
|
2021-08-15 09:41:48 +00:00
|
|
|
if ((self = [super initWithNibName:@"InfoFileView" bundle:nil]))
|
2010-03-14 03:09:12 +00:00
|
|
|
{
|
2021-08-07 07:27:56 +00:00
|
|
|
self.title = NSLocalizedString(@"Files", "Inspector view -> title");
|
2010-03-14 03:09:12 +00:00
|
|
|
}
|
2017-01-24 17:53:16 +00:00
|
|
|
|
2010-03-06 23:12:30 +00:00
|
|
|
return self;
|
|
|
|
}
|
|
|
|
|
2021-08-15 09:41:48 +00:00
|
|
|
- (void)awakeFromNib
|
2010-03-06 23:12:30 +00:00
|
|
|
{
|
2021-08-15 09:41:48 +00:00
|
|
|
CGFloat const height = [NSUserDefaults.standardUserDefaults floatForKey:@"InspectorContentHeightFiles"];
|
2010-03-14 02:53:22 +00:00
|
|
|
if (height != 0.0)
|
|
|
|
{
|
2021-08-07 07:27:56 +00:00
|
|
|
NSRect viewRect = self.view.frame;
|
2010-03-14 02:53:22 +00:00
|
|
|
viewRect.size.height = height;
|
2021-08-07 07:27:56 +00:00
|
|
|
self.view.frame = viewRect;
|
2010-03-14 02:53:22 +00:00
|
|
|
}
|
2017-01-24 17:53:16 +00:00
|
|
|
|
2022-02-22 16:04:20 +00:00
|
|
|
[self.fFileFilterField.cell setPlaceholderString:NSLocalizedString(@"Filter", "inspector -> file filter")];
|
2017-01-24 17:53:16 +00:00
|
|
|
|
2012-05-14 00:24:39 +00:00
|
|
|
//localize and place all and none buttons
|
2022-02-22 16:04:20 +00:00
|
|
|
self.fCheckAllButton.title = NSLocalizedString(@"All", "inspector -> check all");
|
|
|
|
self.fUncheckAllButton.title = NSLocalizedString(@"None", "inspector -> check all");
|
2017-01-24 17:53:16 +00:00
|
|
|
|
2022-02-22 16:04:20 +00:00
|
|
|
NSRect checkAllFrame = self.fCheckAllButton.frame;
|
|
|
|
NSRect uncheckAllFrame = self.fUncheckAllButton.frame;
|
2021-08-15 09:41:48 +00:00
|
|
|
CGFloat const oldAllWidth = checkAllFrame.size.width;
|
|
|
|
CGFloat const oldNoneWidth = uncheckAllFrame.size.width;
|
2017-01-24 17:53:16 +00:00
|
|
|
|
2022-02-22 16:04:20 +00:00
|
|
|
[self.fCheckAllButton sizeToFit];
|
|
|
|
[self.fUncheckAllButton sizeToFit];
|
|
|
|
CGFloat const newWidth = MAX(self.fCheckAllButton.bounds.size.width, self.fUncheckAllButton.bounds.size.width);
|
2017-01-24 17:53:16 +00:00
|
|
|
|
2021-08-15 09:41:48 +00:00
|
|
|
CGFloat const uncheckAllChange = newWidth - oldNoneWidth;
|
2012-05-14 00:24:39 +00:00
|
|
|
uncheckAllFrame.size.width = newWidth;
|
|
|
|
uncheckAllFrame.origin.x -= uncheckAllChange;
|
2022-02-22 16:04:20 +00:00
|
|
|
self.fUncheckAllButton.frame = uncheckAllFrame;
|
2017-01-24 17:53:16 +00:00
|
|
|
|
2021-08-15 09:41:48 +00:00
|
|
|
CGFloat const checkAllChange = newWidth - oldAllWidth;
|
2012-05-14 00:24:39 +00:00
|
|
|
checkAllFrame.size.width = newWidth;
|
|
|
|
checkAllFrame.origin.x -= (checkAllChange + uncheckAllChange);
|
2022-02-22 16:04:20 +00:00
|
|
|
self.fCheckAllButton.frame = checkAllFrame;
|
2010-03-06 23:12:30 +00:00
|
|
|
}
|
|
|
|
|
2022-04-29 22:51:40 +00:00
|
|
|
- (void)setInfoForTorrents:(NSArray<Torrent*>*)torrents
|
2010-03-06 23:12:30 +00:00
|
|
|
{
|
2010-04-17 18:44:34 +00:00
|
|
|
//don't check if it's the same in case the metadata changed
|
2022-02-22 16:04:20 +00:00
|
|
|
self.fTorrents = torrents;
|
2017-01-24 17:53:16 +00:00
|
|
|
|
2022-02-22 16:04:20 +00:00
|
|
|
self.fSet = NO;
|
2010-03-06 23:12:30 +00:00
|
|
|
}
|
|
|
|
|
2021-08-15 09:41:48 +00:00
|
|
|
- (void)updateInfo
|
2010-03-06 23:12:30 +00:00
|
|
|
{
|
2022-02-22 16:04:20 +00:00
|
|
|
if (!self.fSet)
|
2021-08-15 09:41:48 +00:00
|
|
|
{
|
2010-03-09 02:26:52 +00:00
|
|
|
[self setupInfo];
|
2021-08-15 09:41:48 +00:00
|
|
|
}
|
2017-01-24 17:53:16 +00:00
|
|
|
|
2022-02-22 16:04:20 +00:00
|
|
|
if (self.fTorrents.count == 1)
|
2012-05-14 00:00:19 +00:00
|
|
|
{
|
2022-02-22 16:04:20 +00:00
|
|
|
[self.fFileController refresh];
|
2017-01-24 17:53:16 +00:00
|
|
|
|
2021-08-15 09:41:48 +00:00
|
|
|
#warning use TorrentFileCheckChange notification as well
|
2022-02-22 16:04:20 +00:00
|
|
|
Torrent* torrent = self.fTorrents[0];
|
2021-08-07 07:27:56 +00:00
|
|
|
if (torrent.folder)
|
2012-05-14 00:00:19 +00:00
|
|
|
{
|
2021-08-15 09:41:48 +00:00
|
|
|
NSInteger const filesCheckState = [torrent
|
|
|
|
checkForFiles:[NSIndexSet indexSetWithIndexesInRange:NSMakeRange(0, torrent.fileCount)]];
|
2022-02-22 16:04:20 +00:00
|
|
|
self.fCheckAllButton.enabled = filesCheckState != NSControlStateValueOn; //if anything is unchecked
|
|
|
|
self.fUncheckAllButton.enabled = !torrent.allDownloaded; //if there are any checked files that aren't finished
|
2012-05-14 00:00:19 +00:00
|
|
|
}
|
|
|
|
}
|
2010-03-06 23:12:30 +00:00
|
|
|
}
|
|
|
|
|
2021-08-15 09:41:48 +00:00
|
|
|
- (void)saveViewSize
|
2010-03-14 02:53:22 +00:00
|
|
|
{
|
2021-08-15 09:41:48 +00:00
|
|
|
[NSUserDefaults.standardUserDefaults setFloat:NSHeight(self.view.frame) forKey:@"InspectorContentHeightFiles"];
|
2010-03-14 02:53:22 +00:00
|
|
|
}
|
|
|
|
|
2021-08-15 09:41:48 +00:00
|
|
|
- (void)setFileFilterText:(id)sender
|
2010-03-06 23:12:30 +00:00
|
|
|
{
|
2022-02-22 16:04:20 +00:00
|
|
|
self.fFileController.filterText = [sender stringValue];
|
2010-03-06 23:12:30 +00:00
|
|
|
}
|
|
|
|
|
2021-08-15 09:41:48 +00:00
|
|
|
- (IBAction)checkAll:(id)sender
|
2012-05-14 00:00:19 +00:00
|
|
|
{
|
2022-02-22 16:04:20 +00:00
|
|
|
[self.fFileController checkAll];
|
2012-05-14 00:00:19 +00:00
|
|
|
}
|
|
|
|
|
2021-08-15 09:41:48 +00:00
|
|
|
- (IBAction)uncheckAll:(id)sender
|
2012-05-14 00:00:19 +00:00
|
|
|
{
|
2022-02-22 16:04:20 +00:00
|
|
|
[self.fFileController uncheckAll];
|
2012-05-14 00:00:19 +00:00
|
|
|
}
|
|
|
|
|
2022-06-19 06:00:16 +00:00
|
|
|
- (void)keyDown:(NSEvent*)event
|
|
|
|
{
|
|
|
|
unichar const firstChar = [event.charactersIgnoringModifiers characterAtIndex:0];
|
|
|
|
|
|
|
|
if (firstChar == ' ')
|
|
|
|
{
|
|
|
|
[self toggleQuickLook:nil];
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
[super keyDown:event];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)toggleQuickLook:(id)sender
|
|
|
|
{
|
|
|
|
if ([QLPreviewPanel sharedPreviewPanel].visible)
|
|
|
|
{
|
|
|
|
[[QLPreviewPanel sharedPreviewPanel] orderOut:nil];
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
[[QLPreviewPanel sharedPreviewPanel] makeKeyAndOrderFront:nil];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-04-29 22:51:40 +00:00
|
|
|
- (NSArray<NSURL*>*)quickLookURLs
|
2010-03-06 23:12:30 +00:00
|
|
|
{
|
2022-02-22 16:04:20 +00:00
|
|
|
FileOutlineView* fileOutlineView = self.fFileController.outlineView;
|
|
|
|
Torrent* torrent = self.fTorrents[0];
|
2021-08-15 09:41:48 +00:00
|
|
|
NSIndexSet* indexes = fileOutlineView.selectedRowIndexes;
|
|
|
|
NSMutableArray* urlArray = [NSMutableArray arrayWithCapacity:indexes.count];
|
2017-01-24 17:53:16 +00:00
|
|
|
|
2021-08-15 09:41:48 +00:00
|
|
|
for (NSUInteger i = indexes.firstIndex; i != NSNotFound; i = [indexes indexGreaterThanIndex:i])
|
2010-03-06 23:12:30 +00:00
|
|
|
{
|
2021-08-15 09:41:48 +00:00
|
|
|
FileListNode* item = [fileOutlineView itemAtRow:i];
|
|
|
|
if ([self canQuickLookFile:item])
|
|
|
|
{
|
|
|
|
[urlArray addObject:[NSURL fileURLWithPath:[torrent fileLocation:item]]];
|
|
|
|
}
|
2010-03-06 23:12:30 +00:00
|
|
|
}
|
2017-01-24 17:53:16 +00:00
|
|
|
|
2010-03-06 23:12:30 +00:00
|
|
|
return urlArray;
|
|
|
|
}
|
|
|
|
|
2021-08-15 09:41:48 +00:00
|
|
|
- (BOOL)canQuickLook
|
2010-03-06 23:12:30 +00:00
|
|
|
{
|
2022-02-22 16:04:20 +00:00
|
|
|
if (self.fTorrents.count != 1)
|
2021-08-15 09:41:48 +00:00
|
|
|
{
|
2010-03-06 23:12:30 +00:00
|
|
|
return NO;
|
2021-08-15 09:41:48 +00:00
|
|
|
}
|
2017-01-24 17:53:16 +00:00
|
|
|
|
2022-02-22 16:04:20 +00:00
|
|
|
Torrent* torrent = self.fTorrents[0];
|
2021-08-07 07:27:56 +00:00
|
|
|
if (!torrent.folder)
|
2021-08-15 09:41:48 +00:00
|
|
|
{
|
2010-03-06 23:12:30 +00:00
|
|
|
return NO;
|
2021-08-15 09:41:48 +00:00
|
|
|
}
|
2017-01-24 17:53:16 +00:00
|
|
|
|
2022-02-22 16:04:20 +00:00
|
|
|
FileOutlineView* fileOutlineView = self.fFileController.outlineView;
|
2021-08-15 09:41:48 +00:00
|
|
|
NSIndexSet* indexes = fileOutlineView.selectedRowIndexes;
|
2017-01-24 17:53:16 +00:00
|
|
|
|
2021-08-15 09:41:48 +00:00
|
|
|
for (NSUInteger i = indexes.firstIndex; i != NSNotFound; i = [indexes indexGreaterThanIndex:i])
|
|
|
|
{
|
|
|
|
if ([self canQuickLookFile:[fileOutlineView itemAtRow:i]])
|
|
|
|
{
|
2010-03-06 23:12:30 +00:00
|
|
|
return YES;
|
2021-08-15 09:41:48 +00:00
|
|
|
}
|
|
|
|
}
|
2017-01-24 17:53:16 +00:00
|
|
|
|
2010-03-06 23:12:30 +00:00
|
|
|
return NO;
|
|
|
|
}
|
|
|
|
|
2021-08-15 09:41:48 +00:00
|
|
|
- (NSRect)quickLookSourceFrameForPreviewItem:(id<QLPreviewItem>)item
|
2010-03-06 23:12:30 +00:00
|
|
|
{
|
2022-02-22 16:04:20 +00:00
|
|
|
FileOutlineView* fileOutlineView = self.fFileController.outlineView;
|
2017-01-24 17:53:16 +00:00
|
|
|
|
2021-08-15 09:41:48 +00:00
|
|
|
NSString* fullPath = ((NSURL*)item).path;
|
2022-02-22 16:04:20 +00:00
|
|
|
Torrent* torrent = self.fTorrents[0];
|
2021-08-15 09:41:48 +00:00
|
|
|
NSRange visibleRows = [fileOutlineView rowsInRect:fileOutlineView.bounds];
|
2017-01-24 17:53:16 +00:00
|
|
|
|
2010-03-06 23:12:30 +00:00
|
|
|
for (NSUInteger row = visibleRows.location; row < NSMaxRange(visibleRows); row++)
|
|
|
|
{
|
2021-08-15 09:41:48 +00:00
|
|
|
FileListNode* rowItem = [fileOutlineView itemAtRow:row];
|
|
|
|
if ([[torrent fileLocation:rowItem] isEqualToString:fullPath])
|
2010-03-06 23:12:30 +00:00
|
|
|
{
|
2021-08-15 09:41:48 +00:00
|
|
|
NSRect frame = [fileOutlineView iconRectForRow:row];
|
2017-01-24 17:53:16 +00:00
|
|
|
|
2021-08-07 07:27:56 +00:00
|
|
|
if (!NSIntersectsRect(fileOutlineView.visibleRect, frame))
|
2021-08-15 09:41:48 +00:00
|
|
|
{
|
2010-03-06 23:12:30 +00:00
|
|
|
return NSZeroRect;
|
2021-08-15 09:41:48 +00:00
|
|
|
}
|
2017-01-24 17:53:16 +00:00
|
|
|
|
2021-08-15 09:41:48 +00:00
|
|
|
frame.origin = [fileOutlineView convertPoint:frame.origin toView:nil];
|
|
|
|
frame = [self.view.window convertRectToScreen:frame];
|
2010-03-06 23:12:30 +00:00
|
|
|
frame.origin.y -= frame.size.height;
|
|
|
|
return frame;
|
|
|
|
}
|
|
|
|
}
|
2017-01-24 17:53:16 +00:00
|
|
|
|
2010-03-06 23:12:30 +00:00
|
|
|
return NSZeroRect;
|
|
|
|
}
|
|
|
|
|
2022-02-22 16:04:20 +00:00
|
|
|
#pragma mark - Private
|
|
|
|
|
2021-08-15 09:41:48 +00:00
|
|
|
- (void)setupInfo
|
2010-03-09 02:26:52 +00:00
|
|
|
{
|
2022-02-22 16:04:20 +00:00
|
|
|
self.fFileFilterField.stringValue = @"";
|
2017-01-24 17:53:16 +00:00
|
|
|
|
2022-02-22 16:04:20 +00:00
|
|
|
if (self.fTorrents.count == 1)
|
2010-03-09 02:26:52 +00:00
|
|
|
{
|
2022-02-22 16:04:20 +00:00
|
|
|
Torrent* torrent = self.fTorrents[0];
|
2017-01-24 17:53:16 +00:00
|
|
|
|
2022-02-22 16:04:20 +00:00
|
|
|
self.fFileController.torrent = torrent;
|
2017-01-24 17:53:16 +00:00
|
|
|
|
2021-08-15 09:41:48 +00:00
|
|
|
BOOL const isFolder = torrent.folder;
|
2022-02-22 16:04:20 +00:00
|
|
|
self.fFileFilterField.enabled = isFolder;
|
2017-01-24 17:53:16 +00:00
|
|
|
|
2012-05-14 00:00:19 +00:00
|
|
|
if (!isFolder)
|
|
|
|
{
|
2022-02-22 16:04:20 +00:00
|
|
|
self.fCheckAllButton.enabled = NO;
|
|
|
|
self.fUncheckAllButton.enabled = NO;
|
2012-05-14 00:00:19 +00:00
|
|
|
}
|
2010-03-09 02:26:52 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2022-02-22 16:04:20 +00:00
|
|
|
self.fFileController.torrent = nil;
|
2017-01-24 17:53:16 +00:00
|
|
|
|
2022-02-22 16:04:20 +00:00
|
|
|
self.fFileFilterField.enabled = NO;
|
2017-01-24 17:53:16 +00:00
|
|
|
|
2022-02-22 16:04:20 +00:00
|
|
|
self.fCheckAllButton.enabled = NO;
|
|
|
|
self.fUncheckAllButton.enabled = NO;
|
2010-03-09 02:26:52 +00:00
|
|
|
}
|
2017-01-24 17:53:16 +00:00
|
|
|
|
2022-02-22 16:04:20 +00:00
|
|
|
self.fSet = YES;
|
2010-03-09 02:26:52 +00:00
|
|
|
}
|
|
|
|
|
2021-08-15 09:41:48 +00:00
|
|
|
- (BOOL)canQuickLookFile:(FileListNode*)item
|
2010-03-06 23:12:30 +00:00
|
|
|
{
|
2022-02-22 16:04:20 +00:00
|
|
|
Torrent* torrent = self.fTorrents[0];
|
2021-08-15 09:41:48 +00:00
|
|
|
return (item.isFolder || [torrent fileProgress:item] >= 1.0) && [torrent fileLocation:item];
|
2010-03-06 23:12:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@end
|