2023-11-01 21:11:11 +00:00
|
|
|
// This file Copyright © Transmission authors and contributors.
|
2022-01-20 18:27:56 +00:00
|
|
|
// It may be used under the MIT (SPDX: MIT) license.
|
|
|
|
// License text can be found in the licenses/ folder.
|
2010-04-12 00:55:31 +00:00
|
|
|
|
|
|
|
#import "AddMagnetWindowController.h"
|
|
|
|
#import "Controller.h"
|
|
|
|
#import "ExpandedPathToIconTransformer.h"
|
|
|
|
#import "GroupsController.h"
|
|
|
|
#import "NSStringAdditions.h"
|
|
|
|
#import "Torrent.h"
|
|
|
|
|
2022-10-19 19:28:21 +00:00
|
|
|
typedef NS_ENUM(NSUInteger, PopupPriority) {
|
|
|
|
PopupPriorityHigh = 0,
|
|
|
|
PopupPriorityNormal = 1,
|
|
|
|
PopupPriorityLow = 2,
|
|
|
|
};
|
2010-04-12 00:55:31 +00:00
|
|
|
|
2022-01-24 01:32:45 +00:00
|
|
|
@interface AddMagnetWindowController ()
|
2010-04-12 00:55:31 +00:00
|
|
|
|
2022-02-22 16:04:20 +00:00
|
|
|
@property(nonatomic) IBOutlet NSImageView* fLocationImageView;
|
|
|
|
@property(nonatomic) IBOutlet NSTextField* fNameField;
|
|
|
|
@property(nonatomic) IBOutlet NSTextField* fLocationField;
|
|
|
|
@property(nonatomic) IBOutlet NSButton* fStartCheck;
|
|
|
|
@property(nonatomic) IBOutlet NSPopUpButton* fGroupPopUp;
|
|
|
|
@property(nonatomic) IBOutlet NSPopUpButton* fPriorityPopUp;
|
|
|
|
|
|
|
|
@property(nonatomic, readonly) Controller* fController;
|
|
|
|
|
|
|
|
@property(nonatomic) NSString* fDestination;
|
|
|
|
|
|
|
|
@property(nonatomic) NSInteger fGroupValue;
|
|
|
|
@property(nonatomic) TorrentDeterminationType fGroupDeterminationType;
|
|
|
|
|
2010-04-12 00:55:31 +00:00
|
|
|
@end
|
|
|
|
|
|
|
|
@implementation AddMagnetWindowController
|
|
|
|
|
2021-08-15 09:41:48 +00:00
|
|
|
- (instancetype)initWithTorrent:(Torrent*)torrent destination:(NSString*)path controller:(Controller*)controller
|
2010-04-12 00:55:31 +00:00
|
|
|
{
|
2021-08-15 09:41:48 +00:00
|
|
|
if ((self = [super initWithWindowNibName:@"AddMagnetWindow"]))
|
2010-04-12 00:55:31 +00:00
|
|
|
{
|
2022-02-22 16:04:20 +00:00
|
|
|
_torrent = torrent;
|
|
|
|
_fDestination = path.stringByExpandingTildeInPath;
|
2017-01-24 17:53:16 +00:00
|
|
|
|
2022-02-22 16:04:20 +00:00
|
|
|
_fController = controller;
|
2017-01-24 17:53:16 +00:00
|
|
|
|
2022-02-22 16:04:20 +00:00
|
|
|
_fGroupValue = torrent.groupValue;
|
|
|
|
_fGroupDeterminationType = TorrentDeterminationAutomatic;
|
2010-04-12 00:55:31 +00:00
|
|
|
}
|
|
|
|
return self;
|
|
|
|
}
|
|
|
|
|
2021-08-15 09:41:48 +00:00
|
|
|
- (void)awakeFromNib
|
2010-04-12 00:55:31 +00:00
|
|
|
{
|
2021-08-15 09:41:48 +00:00
|
|
|
[NSNotificationCenter.defaultCenter addObserver:self selector:@selector(updateGroupMenu:) name:@"UpdateGroups" object:nil];
|
2017-01-24 17:53:16 +00:00
|
|
|
|
2022-02-22 16:04:20 +00:00
|
|
|
NSString* name = self.torrent.name;
|
2021-08-07 07:27:56 +00:00
|
|
|
self.window.title = name;
|
2022-02-22 16:04:20 +00:00
|
|
|
self.fNameField.stringValue = name;
|
|
|
|
self.fNameField.toolTip = name;
|
2017-01-24 17:53:16 +00:00
|
|
|
|
2022-06-16 17:55:33 +00:00
|
|
|
//disable fullscreen support
|
2022-06-29 04:20:42 +00:00
|
|
|
self.window.collectionBehavior = NSWindowCollectionBehaviorFullScreenNone;
|
2022-06-08 19:15:51 +00:00
|
|
|
|
2010-04-12 00:55:31 +00:00
|
|
|
[self setGroupsMenu];
|
2022-02-22 16:04:20 +00:00
|
|
|
[self.fGroupPopUp selectItemWithTag:self.fGroupValue];
|
2017-01-24 17:53:16 +00:00
|
|
|
|
2022-10-19 19:28:21 +00:00
|
|
|
PopupPriority priorityIndex;
|
2022-02-22 16:04:20 +00:00
|
|
|
switch (self.torrent.priority)
|
2010-04-12 00:55:31 +00:00
|
|
|
{
|
2021-08-15 09:41:48 +00:00
|
|
|
case TR_PRI_HIGH:
|
2022-10-19 19:28:21 +00:00
|
|
|
priorityIndex = PopupPriorityHigh;
|
2021-08-15 09:41:48 +00:00
|
|
|
break;
|
|
|
|
case TR_PRI_NORMAL:
|
2022-10-19 19:28:21 +00:00
|
|
|
priorityIndex = PopupPriorityNormal;
|
2021-08-15 09:41:48 +00:00
|
|
|
break;
|
|
|
|
case TR_PRI_LOW:
|
2022-10-19 19:28:21 +00:00
|
|
|
priorityIndex = PopupPriorityLow;
|
2021-08-15 09:41:48 +00:00
|
|
|
break;
|
|
|
|
default:
|
2022-02-22 16:04:20 +00:00
|
|
|
NSAssert1(NO, @"Unknown priority for adding torrent: %d", self.torrent.priority);
|
2022-10-19 19:28:21 +00:00
|
|
|
priorityIndex = PopupPriorityNormal;
|
2010-04-12 00:55:31 +00:00
|
|
|
}
|
2022-02-22 16:04:20 +00:00
|
|
|
[self.fPriorityPopUp selectItemAtIndex:priorityIndex];
|
2017-01-24 17:53:16 +00:00
|
|
|
|
2022-03-30 21:52:23 +00:00
|
|
|
self.fStartCheck.state = [NSUserDefaults.standardUserDefaults boolForKey:@"AutoStartDownload"] ? NSControlStateValueOn :
|
|
|
|
NSControlStateValueOff;
|
2017-01-24 17:53:16 +00:00
|
|
|
|
2022-02-22 16:04:20 +00:00
|
|
|
if (self.fDestination)
|
2021-08-15 09:41:48 +00:00
|
|
|
{
|
2022-02-22 16:04:20 +00:00
|
|
|
[self setDestinationPath:self.fDestination determinationType:TorrentDeterminationAutomatic];
|
2021-08-15 09:41:48 +00:00
|
|
|
}
|
2010-04-12 00:55:31 +00:00
|
|
|
else
|
|
|
|
{
|
2022-02-22 16:04:20 +00:00
|
|
|
self.fLocationField.stringValue = @"";
|
|
|
|
self.fLocationImageView.image = nil;
|
2010-04-12 00:55:31 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-08-15 09:41:48 +00:00
|
|
|
- (void)windowDidLoad
|
2010-04-12 00:55:31 +00:00
|
|
|
{
|
|
|
|
//if there is no destination, prompt for one right away
|
2022-02-22 16:04:20 +00:00
|
|
|
if (!self.fDestination)
|
2021-08-15 09:41:48 +00:00
|
|
|
{
|
|
|
|
[self setDestination:nil];
|
|
|
|
}
|
2010-04-12 00:55:31 +00:00
|
|
|
}
|
|
|
|
|
2021-08-15 09:41:48 +00:00
|
|
|
- (void)setDestination:(id)sender
|
2010-04-12 00:55:31 +00:00
|
|
|
{
|
2021-08-15 09:41:48 +00:00
|
|
|
NSOpenPanel* panel = [NSOpenPanel openPanel];
|
2010-04-12 00:55:31 +00:00
|
|
|
|
2021-08-07 07:27:56 +00:00
|
|
|
panel.prompt = NSLocalizedString(@"Select", "Open torrent -> prompt");
|
|
|
|
panel.allowsMultipleSelection = NO;
|
|
|
|
panel.canChooseFiles = NO;
|
|
|
|
panel.canChooseDirectories = YES;
|
|
|
|
panel.canCreateDirectories = YES;
|
2017-01-24 17:53:16 +00:00
|
|
|
|
2021-08-15 09:41:48 +00:00
|
|
|
panel.message = [NSString stringWithFormat:NSLocalizedString(@"Select the download folder for \"%@\"", "Add -> select destination folder"),
|
2022-02-22 16:04:20 +00:00
|
|
|
self.torrent.name];
|
2017-01-24 17:53:16 +00:00
|
|
|
|
2021-08-15 09:41:48 +00:00
|
|
|
[panel beginSheetModalForWindow:self.window completionHandler:^(NSInteger result) {
|
2021-10-31 15:18:27 +00:00
|
|
|
if (result == NSModalResponseOK)
|
2021-08-15 09:41:48 +00:00
|
|
|
{
|
|
|
|
[self setDestinationPath:panel.URLs[0].path determinationType:TorrentDeterminationUserSpecified];
|
|
|
|
}
|
2011-12-11 22:31:01 +00:00
|
|
|
else
|
|
|
|
{
|
2022-02-22 16:04:20 +00:00
|
|
|
if (!self.fDestination)
|
2021-08-15 09:41:48 +00:00
|
|
|
{
|
|
|
|
[self performSelectorOnMainThread:@selector(cancelAdd:) withObject:nil waitUntilDone:NO];
|
|
|
|
}
|
2011-12-11 22:31:01 +00:00
|
|
|
}
|
|
|
|
}];
|
2010-04-12 00:55:31 +00:00
|
|
|
}
|
|
|
|
|
2021-08-15 09:41:48 +00:00
|
|
|
- (void)add:(id)sender
|
2010-04-12 00:55:31 +00:00
|
|
|
{
|
2022-02-22 16:04:20 +00:00
|
|
|
if ([self.fDestination.lastPathComponent isEqualToString:self.torrent.name] &&
|
2021-08-15 09:41:48 +00:00
|
|
|
[NSUserDefaults.standardUserDefaults boolForKey:@"WarningFolderDataSameName"])
|
2010-04-12 00:55:31 +00:00
|
|
|
{
|
2021-08-15 09:41:48 +00:00
|
|
|
NSAlert* alert = [[NSAlert alloc] init];
|
|
|
|
alert.messageText = NSLocalizedString(@"The destination directory and root data directory have the same name.", "Add torrent -> same name -> title");
|
|
|
|
alert.informativeText = NSLocalizedString(
|
|
|
|
@"If you are attempting to use already existing data,"
|
|
|
|
" the root data directory should be inside the destination directory.",
|
|
|
|
"Add torrent -> same name -> message");
|
2021-10-31 15:18:27 +00:00
|
|
|
alert.alertStyle = NSAlertStyleWarning;
|
2021-08-15 09:41:48 +00:00
|
|
|
[alert addButtonWithTitle:NSLocalizedString(@"Cancel", "Add torrent -> same name -> button")];
|
|
|
|
[alert addButtonWithTitle:NSLocalizedString(@"Add", "Add torrent -> same name -> button")];
|
2021-08-07 07:27:56 +00:00
|
|
|
alert.showsSuppressionButton = YES;
|
2017-01-24 17:53:16 +00:00
|
|
|
|
2021-08-07 07:27:56 +00:00
|
|
|
[alert beginSheetModalForWindow:self.window completionHandler:^(NSModalResponse returnCode) {
|
2021-10-31 15:18:27 +00:00
|
|
|
if (alert.suppressionButton.state == NSControlStateValueOn)
|
2021-08-15 09:41:48 +00:00
|
|
|
{
|
|
|
|
[NSUserDefaults.standardUserDefaults setBool:NO forKey:@"WarningFolderDataSameName"];
|
|
|
|
}
|
2020-11-22 13:02:29 +00:00
|
|
|
|
|
|
|
if (returnCode == NSAlertSecondButtonReturn)
|
2021-08-15 09:41:48 +00:00
|
|
|
{
|
|
|
|
[self performSelectorOnMainThread:@selector(confirmAdd) withObject:nil waitUntilDone:NO];
|
|
|
|
}
|
2020-11-22 13:02:29 +00:00
|
|
|
}];
|
2010-04-12 00:55:31 +00:00
|
|
|
}
|
|
|
|
else
|
2021-08-15 09:41:48 +00:00
|
|
|
{
|
2010-04-12 00:55:31 +00:00
|
|
|
[self confirmAdd];
|
2021-08-15 09:41:48 +00:00
|
|
|
}
|
2010-04-12 00:55:31 +00:00
|
|
|
}
|
|
|
|
|
2021-08-15 09:41:48 +00:00
|
|
|
- (void)cancelAdd:(id)sender
|
2010-04-12 00:55:31 +00:00
|
|
|
{
|
2021-08-15 09:41:48 +00:00
|
|
|
[self.window performClose:sender];
|
2010-04-12 00:55:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
//only called on cancel
|
2021-08-15 09:41:48 +00:00
|
|
|
- (BOOL)windowShouldClose:(id)window
|
2010-04-12 00:55:31 +00:00
|
|
|
{
|
2022-02-22 16:04:20 +00:00
|
|
|
[self.fController askOpenMagnetConfirmed:self add:NO];
|
2010-04-12 00:55:31 +00:00
|
|
|
return YES;
|
|
|
|
}
|
|
|
|
|
2021-08-15 09:41:48 +00:00
|
|
|
- (void)changePriority:(id)sender
|
2010-04-12 00:55:31 +00:00
|
|
|
{
|
|
|
|
tr_priority_t priority;
|
|
|
|
switch ([sender indexOfSelectedItem])
|
|
|
|
{
|
2022-10-19 19:28:21 +00:00
|
|
|
case PopupPriorityHigh:
|
2021-08-15 09:41:48 +00:00
|
|
|
priority = TR_PRI_HIGH;
|
|
|
|
break;
|
2022-10-19 19:28:21 +00:00
|
|
|
case PopupPriorityNormal:
|
2021-08-15 09:41:48 +00:00
|
|
|
priority = TR_PRI_NORMAL;
|
|
|
|
break;
|
2022-10-19 19:28:21 +00:00
|
|
|
case PopupPriorityLow:
|
2021-08-15 09:41:48 +00:00
|
|
|
priority = TR_PRI_LOW;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
NSAssert1(NO, @"Unknown priority tag for adding torrent: %ld", [sender tag]);
|
|
|
|
priority = TR_PRI_NORMAL;
|
2010-04-12 00:55:31 +00:00
|
|
|
}
|
2022-02-22 16:04:20 +00:00
|
|
|
self.torrent.priority = priority;
|
2010-04-12 00:55:31 +00:00
|
|
|
}
|
|
|
|
|
2021-08-15 09:41:48 +00:00
|
|
|
- (void)updateGroupMenu:(NSNotification*)notification
|
2010-04-12 00:55:31 +00:00
|
|
|
{
|
|
|
|
[self setGroupsMenu];
|
2022-02-22 16:04:20 +00:00
|
|
|
if (![self.fGroupPopUp selectItemWithTag:self.fGroupValue])
|
2010-04-12 00:55:31 +00:00
|
|
|
{
|
2022-02-22 16:04:20 +00:00
|
|
|
self.fGroupValue = -1;
|
|
|
|
self.fGroupDeterminationType = TorrentDeterminationAutomatic;
|
|
|
|
[self.fGroupPopUp selectItemWithTag:self.fGroupValue];
|
2010-04-12 00:55:31 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-02-22 16:04:20 +00:00
|
|
|
#pragma mark - Private
|
|
|
|
|
2021-08-15 09:41:48 +00:00
|
|
|
- (void)confirmAdd
|
2010-04-12 00:55:31 +00:00
|
|
|
{
|
2022-02-22 16:04:20 +00:00
|
|
|
[self.torrent setGroupValue:self.fGroupValue determinationType:self.fGroupDeterminationType];
|
2017-01-24 17:53:16 +00:00
|
|
|
|
2022-02-22 16:04:20 +00:00
|
|
|
if (self.fStartCheck.state == NSControlStateValueOn)
|
2021-08-15 09:41:48 +00:00
|
|
|
{
|
2023-02-02 06:19:20 +00:00
|
|
|
[self.torrent startMagnetTransferAfterMetaDownload];
|
2021-08-15 09:41:48 +00:00
|
|
|
}
|
2017-01-24 17:53:16 +00:00
|
|
|
|
2010-04-12 00:55:31 +00:00
|
|
|
[self close];
|
2022-02-22 16:04:20 +00:00
|
|
|
[self.fController askOpenMagnetConfirmed:self add:YES];
|
2010-04-12 00:55:31 +00:00
|
|
|
}
|
|
|
|
|
2021-08-15 09:41:48 +00:00
|
|
|
- (void)setDestinationPath:(NSString*)destination determinationType:(TorrentDeterminationType)determinationType
|
2010-04-12 00:55:31 +00:00
|
|
|
{
|
2021-08-07 07:27:56 +00:00
|
|
|
destination = destination.stringByExpandingTildeInPath;
|
2022-02-22 16:04:20 +00:00
|
|
|
if (!self.fDestination || ![self.fDestination isEqualToString:destination])
|
2017-01-24 17:53:16 +00:00
|
|
|
{
|
2022-02-22 16:04:20 +00:00
|
|
|
self.fDestination = destination;
|
2017-01-24 17:53:16 +00:00
|
|
|
|
2022-02-22 16:04:20 +00:00
|
|
|
[self.torrent changeDownloadFolderBeforeUsing:self.fDestination determinationType:determinationType];
|
2010-04-12 00:55:31 +00:00
|
|
|
}
|
2017-01-24 17:53:16 +00:00
|
|
|
|
2022-02-22 16:04:20 +00:00
|
|
|
self.fLocationField.stringValue = self.fDestination.stringByAbbreviatingWithTildeInPath;
|
|
|
|
self.fLocationField.toolTip = self.fDestination;
|
2017-01-24 17:53:16 +00:00
|
|
|
|
2021-08-15 09:41:48 +00:00
|
|
|
ExpandedPathToIconTransformer* iconTransformer = [[ExpandedPathToIconTransformer alloc] init];
|
2022-02-22 16:04:20 +00:00
|
|
|
self.fLocationImageView.image = [iconTransformer transformedValue:self.fDestination];
|
2010-04-12 00:55:31 +00:00
|
|
|
}
|
|
|
|
|
2021-08-15 09:41:48 +00:00
|
|
|
- (void)setGroupsMenu
|
2010-04-12 00:55:31 +00:00
|
|
|
{
|
2021-08-15 09:41:48 +00:00
|
|
|
NSMenu* groupMenu = [GroupsController.groups groupMenuWithTarget:self action:@selector(changeGroupValue:) isSmall:NO];
|
2022-02-22 16:04:20 +00:00
|
|
|
self.fGroupPopUp.menu = groupMenu;
|
2010-04-12 00:55:31 +00:00
|
|
|
}
|
|
|
|
|
2021-08-15 09:41:48 +00:00
|
|
|
- (void)changeGroupValue:(id)sender
|
2010-04-12 00:55:31 +00:00
|
|
|
{
|
2022-02-22 16:04:20 +00:00
|
|
|
NSInteger previousGroup = self.fGroupValue;
|
|
|
|
self.fGroupValue = [sender tag];
|
|
|
|
self.fGroupDeterminationType = TorrentDeterminationUserSpecified;
|
2012-10-30 00:22:10 +00:00
|
|
|
|
2022-02-22 16:04:20 +00:00
|
|
|
if ([GroupsController.groups usesCustomDownloadLocationForIndex:self.fGroupValue])
|
2021-08-15 09:41:48 +00:00
|
|
|
{
|
2022-02-22 16:04:20 +00:00
|
|
|
[self setDestinationPath:[GroupsController.groups customDownloadLocationForIndex:self.fGroupValue]
|
2021-08-15 09:41:48 +00:00
|
|
|
determinationType:TorrentDeterminationAutomatic];
|
|
|
|
}
|
2022-02-22 16:04:20 +00:00
|
|
|
else if ([self.fDestination isEqualToString:[GroupsController.groups customDownloadLocationForIndex:previousGroup]])
|
2021-08-15 09:41:48 +00:00
|
|
|
{
|
|
|
|
[self setDestinationPath:[NSUserDefaults.standardUserDefaults stringForKey:@"DownloadFolder"]
|
|
|
|
determinationType:TorrentDeterminationAutomatic];
|
|
|
|
}
|
2010-04-12 00:55:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@end
|