2010-04-12 00:55:31 +00:00
|
|
|
/******************************************************************************
|
2012-01-14 17:12:04 +00:00
|
|
|
* Copyright (c) 2010-2012 Transmission authors and contributors
|
2010-04-12 00:55:31 +00:00
|
|
|
*
|
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a
|
|
|
|
* copy of this software and associated documentation files (the "Software"),
|
|
|
|
* to deal in the Software without restriction, including without limitation
|
|
|
|
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
|
|
|
|
* and/or sell copies of the Software, and to permit persons to whom the
|
|
|
|
* Software is furnished to do so, subject to the following conditions:
|
|
|
|
*
|
|
|
|
* The above copyright notice and this permission notice shall be included in
|
|
|
|
* all copies or substantial portions of the Software.
|
|
|
|
*
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
|
|
|
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
|
|
|
|
* DEALINGS IN THE SOFTWARE.
|
|
|
|
*****************************************************************************/
|
|
|
|
|
|
|
|
#import "AddMagnetWindowController.h"
|
|
|
|
#import "Controller.h"
|
|
|
|
#import "ExpandedPathToIconTransformer.h"
|
|
|
|
#import "GroupsController.h"
|
|
|
|
#import "NSStringAdditions.h"
|
|
|
|
#import "Torrent.h"
|
|
|
|
|
|
|
|
#define POPUP_PRIORITY_HIGH 0
|
|
|
|
#define POPUP_PRIORITY_NORMAL 1
|
|
|
|
#define POPUP_PRIORITY_LOW 2
|
|
|
|
|
|
|
|
@interface AddMagnetWindowController (Private)
|
|
|
|
|
|
|
|
- (void) confirmAdd;
|
|
|
|
|
2012-10-30 00:22:10 +00:00
|
|
|
- (void) setDestinationPath: (NSString *) destination determinationType: (TorrentDeterminationType) determinationType;
|
2010-04-12 00:55:31 +00:00
|
|
|
|
|
|
|
- (void) setGroupsMenu;
|
|
|
|
- (void) changeGroupValue: (id) sender;
|
|
|
|
|
|
|
|
- (void) sameNameAlertDidEnd: (NSAlert *) alert returnCode: (NSInteger) returnCode contextInfo: (void *) contextInfo;
|
|
|
|
|
|
|
|
@end
|
|
|
|
|
|
|
|
@implementation AddMagnetWindowController
|
|
|
|
|
|
|
|
- (id) initWithTorrent: (Torrent *) torrent destination: (NSString *) path controller: (Controller *) controller
|
|
|
|
{
|
|
|
|
if ((self = [super initWithWindowNibName: @"AddMagnetWindow"]))
|
|
|
|
{
|
|
|
|
fTorrent = torrent;
|
2012-03-13 02:52:11 +00:00
|
|
|
fDestination = [[path stringByExpandingTildeInPath] retain];
|
2017-01-24 17:53:16 +00:00
|
|
|
|
2010-04-12 00:55:31 +00:00
|
|
|
fController = controller;
|
2017-01-24 17:53:16 +00:00
|
|
|
|
2010-04-12 00:55:31 +00:00
|
|
|
fGroupValue = [torrent groupValue];
|
2012-10-30 00:22:10 +00:00
|
|
|
fGroupDeterminationType = TorrentDeterminationAutomatic;
|
2010-04-12 00:55:31 +00:00
|
|
|
}
|
|
|
|
return self;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void) awakeFromNib
|
|
|
|
{
|
|
|
|
[[NSNotificationCenter defaultCenter] addObserver: self selector: @selector(updateGroupMenu:)
|
|
|
|
name: @"UpdateGroups" object: nil];
|
2017-01-24 17:53:16 +00:00
|
|
|
|
2010-04-12 00:55:31 +00:00
|
|
|
NSString * name = [fTorrent name];
|
|
|
|
[[self window] setTitle: name];
|
|
|
|
[fNameField setStringValue: name];
|
2010-04-16 02:07:57 +00:00
|
|
|
[fNameField setToolTip: name];
|
2017-01-24 17:53:16 +00:00
|
|
|
|
2010-04-12 00:55:31 +00:00
|
|
|
[self setGroupsMenu];
|
|
|
|
[fGroupPopUp selectItemWithTag: fGroupValue];
|
2017-01-24 17:53:16 +00:00
|
|
|
|
2010-04-12 00:55:31 +00:00
|
|
|
NSInteger priorityIndex;
|
|
|
|
switch ([fTorrent priority])
|
|
|
|
{
|
|
|
|
case TR_PRI_HIGH: priorityIndex = POPUP_PRIORITY_HIGH; break;
|
|
|
|
case TR_PRI_NORMAL: priorityIndex = POPUP_PRIORITY_NORMAL; break;
|
|
|
|
case TR_PRI_LOW: priorityIndex = POPUP_PRIORITY_LOW; break;
|
2016-09-11 10:22:49 +00:00
|
|
|
default:
|
|
|
|
NSAssert1(NO, @"Unknown priority for adding torrent: %d", [fTorrent priority]);
|
|
|
|
priorityIndex = POPUP_PRIORITY_NORMAL;
|
2010-04-12 00:55:31 +00:00
|
|
|
}
|
|
|
|
[fPriorityPopUp selectItemAtIndex: priorityIndex];
|
2017-01-24 17:53:16 +00:00
|
|
|
|
2010-04-12 00:55:31 +00:00
|
|
|
[fStartCheck setState: [[NSUserDefaults standardUserDefaults] boolForKey: @"AutoStartDownload"] ? NSOnState : NSOffState];
|
2017-01-24 17:53:16 +00:00
|
|
|
|
2010-04-12 00:55:31 +00:00
|
|
|
if (fDestination)
|
2012-10-30 00:22:10 +00:00
|
|
|
[self setDestinationPath: fDestination determinationType: TorrentDeterminationAutomatic];
|
2010-04-12 00:55:31 +00:00
|
|
|
else
|
|
|
|
{
|
|
|
|
[fLocationField setStringValue: @""];
|
|
|
|
[fLocationImageView setImage: nil];
|
|
|
|
}
|
2017-01-24 17:53:16 +00:00
|
|
|
|
2012-09-08 03:48:12 +00:00
|
|
|
#warning when 10.7-only, switch to auto layout
|
|
|
|
[fMagnetLinkLabel sizeToFit];
|
2017-01-24 17:53:16 +00:00
|
|
|
|
2012-09-08 03:48:12 +00:00
|
|
|
const CGFloat downloadToLabelOldWidth = [fDownloadToLabel frame].size.width;
|
|
|
|
[fDownloadToLabel sizeToFit];
|
|
|
|
const CGFloat changeDestOldWidth = [fChangeDestinationButton frame].size.width;
|
|
|
|
[fChangeDestinationButton sizeToFit];
|
|
|
|
NSRect changeDestFrame = [fChangeDestinationButton frame];
|
|
|
|
changeDestFrame.origin.x -= changeDestFrame.size.width - changeDestOldWidth;
|
|
|
|
[fChangeDestinationButton setFrame: changeDestFrame];
|
2017-01-24 17:53:16 +00:00
|
|
|
|
2012-09-08 03:48:12 +00:00
|
|
|
NSRect downloadToBoxFrame = [fDownloadToBox frame];
|
|
|
|
const CGFloat downloadToBoxSizeDiff = ([fDownloadToLabel frame].size.width - downloadToLabelOldWidth) + (changeDestFrame.size.width - changeDestOldWidth);
|
|
|
|
downloadToBoxFrame.size.width -= downloadToBoxSizeDiff;
|
|
|
|
downloadToBoxFrame.origin.x -= downloadToLabelOldWidth - [fDownloadToLabel frame].size.width;
|
|
|
|
[fDownloadToBox setFrame: downloadToBoxFrame];
|
2017-01-24 17:53:16 +00:00
|
|
|
|
2012-09-08 03:48:12 +00:00
|
|
|
NSRect groupPopUpFrame = [fGroupPopUp frame];
|
|
|
|
NSRect priorityPopUpFrame = [fPriorityPopUp frame];
|
|
|
|
const CGFloat popUpOffset = groupPopUpFrame.origin.x - NSMaxX([fGroupLabel frame]);
|
|
|
|
[fGroupLabel sizeToFit];
|
|
|
|
[fPriorityLabel sizeToFit];
|
|
|
|
NSRect groupLabelFrame = [fGroupLabel frame];
|
|
|
|
NSRect priorityLabelFrame = [fPriorityLabel frame];
|
|
|
|
//first bring them both to the left edge
|
|
|
|
groupLabelFrame.origin.x = MIN(groupLabelFrame.origin.x, priorityLabelFrame.origin.x);
|
|
|
|
priorityLabelFrame.origin.x = MIN(groupLabelFrame.origin.x, priorityLabelFrame.origin.x);
|
|
|
|
//then align on the right
|
|
|
|
const CGFloat labelWidth = MAX(groupLabelFrame.size.width, priorityLabelFrame.size.width);
|
|
|
|
groupLabelFrame.origin.x += labelWidth - groupLabelFrame.size.width;
|
|
|
|
priorityLabelFrame.origin.x += labelWidth - priorityLabelFrame.size.width;
|
|
|
|
groupPopUpFrame.origin.x = NSMaxX(groupLabelFrame) + popUpOffset;
|
|
|
|
priorityPopUpFrame.origin.x = NSMaxX(priorityLabelFrame) + popUpOffset;
|
|
|
|
[fGroupLabel setFrame: groupLabelFrame];
|
|
|
|
[fGroupPopUp setFrame: groupPopUpFrame];
|
|
|
|
[fPriorityLabel setFrame: priorityLabelFrame];
|
|
|
|
[fPriorityPopUp setFrame: priorityPopUpFrame];
|
2017-01-24 17:53:16 +00:00
|
|
|
|
2012-09-08 03:48:12 +00:00
|
|
|
const CGFloat minButtonWidth = 82.0;
|
|
|
|
const CGFloat oldAddButtonWidth = [fAddButton bounds].size.width;
|
|
|
|
const CGFloat oldCancelButtonWidth = [fCancelButton bounds].size.width;
|
|
|
|
[fAddButton sizeToFit];
|
|
|
|
[fCancelButton sizeToFit];
|
|
|
|
NSRect addButtonFrame = [fAddButton frame];
|
|
|
|
NSRect cancelButtonFrame = [fCancelButton frame];
|
|
|
|
CGFloat buttonWidth = MAX(addButtonFrame.size.width, cancelButtonFrame.size.width);
|
|
|
|
buttonWidth = MAX(buttonWidth, minButtonWidth);
|
|
|
|
addButtonFrame.size.width = buttonWidth;
|
|
|
|
cancelButtonFrame.size.width = buttonWidth;
|
|
|
|
const CGFloat addButtonWidthIncrease = buttonWidth - oldAddButtonWidth;
|
|
|
|
addButtonFrame.origin.x -= addButtonWidthIncrease;
|
|
|
|
cancelButtonFrame.origin.x -= addButtonWidthIncrease + (buttonWidth - oldCancelButtonWidth);
|
|
|
|
[fAddButton setFrame: addButtonFrame];
|
|
|
|
[fCancelButton setFrame: cancelButtonFrame];
|
2017-01-24 17:53:16 +00:00
|
|
|
|
2012-09-08 03:48:12 +00:00
|
|
|
[fStartCheck sizeToFit];
|
2010-04-12 00:55:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
- (void) windowDidLoad
|
|
|
|
{
|
|
|
|
//if there is no destination, prompt for one right away
|
|
|
|
if (!fDestination)
|
|
|
|
[self setDestination: nil];
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void) dealloc
|
|
|
|
{
|
|
|
|
[[NSNotificationCenter defaultCenter] removeObserver: self];
|
2017-01-24 17:53:16 +00:00
|
|
|
|
2012-03-13 02:52:11 +00:00
|
|
|
[fDestination release];
|
2017-01-24 17:53:16 +00:00
|
|
|
|
2012-03-13 02:52:11 +00:00
|
|
|
[super dealloc];
|
2010-04-12 00:55:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
- (Torrent *) torrent
|
|
|
|
{
|
|
|
|
return fTorrent;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void) setDestination: (id) sender
|
|
|
|
{
|
|
|
|
NSOpenPanel * panel = [NSOpenPanel openPanel];
|
|
|
|
|
|
|
|
[panel setPrompt: NSLocalizedString(@"Select", "Open torrent -> prompt")];
|
|
|
|
[panel setAllowsMultipleSelection: NO];
|
|
|
|
[panel setCanChooseFiles: NO];
|
|
|
|
[panel setCanChooseDirectories: YES];
|
|
|
|
[panel setCanCreateDirectories: YES];
|
2017-01-24 17:53:16 +00:00
|
|
|
|
2010-04-12 00:55:31 +00:00
|
|
|
[panel setMessage: [NSString stringWithFormat: NSLocalizedString(@"Select the download folder for \"%@\"",
|
|
|
|
"Add -> select destination folder"), [fTorrent name]]];
|
2017-01-24 17:53:16 +00:00
|
|
|
|
2011-12-11 22:31:01 +00:00
|
|
|
[panel beginSheetModalForWindow: [self window] completionHandler: ^(NSInteger result) {
|
|
|
|
if (result == NSFileHandlingPanelOKButton)
|
2012-10-30 00:22:10 +00:00
|
|
|
[self setDestinationPath: [[[panel URLs] objectAtIndex: 0] path] determinationType:TorrentDeterminationUserSpecified];
|
2011-12-11 22:31:01 +00:00
|
|
|
else
|
|
|
|
{
|
|
|
|
if (!fDestination)
|
|
|
|
[self performSelectorOnMainThread: @selector(cancelAdd:) withObject: nil waitUntilDone: NO];
|
|
|
|
}
|
|
|
|
}];
|
2010-04-12 00:55:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
- (void) add: (id) sender
|
|
|
|
{
|
|
|
|
if ([[fDestination lastPathComponent] isEqualToString: [fTorrent name]]
|
|
|
|
&& [[NSUserDefaults standardUserDefaults] boolForKey: @"WarningFolderDataSameName"])
|
|
|
|
{
|
|
|
|
NSAlert * alert = [[NSAlert alloc] init];
|
|
|
|
[alert setMessageText: NSLocalizedString(@"The destination directory and root data directory have the same name.",
|
|
|
|
"Add torrent -> same name -> title")];
|
|
|
|
[alert setInformativeText: 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")];
|
|
|
|
[alert setAlertStyle: NSWarningAlertStyle];
|
|
|
|
[alert addButtonWithTitle: NSLocalizedString(@"Cancel", "Add torrent -> same name -> button")];
|
|
|
|
[alert addButtonWithTitle: NSLocalizedString(@"Add", "Add torrent -> same name -> button")];
|
|
|
|
[alert setShowsSuppressionButton: YES];
|
2017-01-24 17:53:16 +00:00
|
|
|
|
2010-04-12 00:55:31 +00:00
|
|
|
[alert beginSheetModalForWindow: [self window] modalDelegate: self
|
|
|
|
didEndSelector: @selector(sameNameAlertDidEnd:returnCode:contextInfo:) contextInfo: nil];
|
|
|
|
}
|
|
|
|
else
|
|
|
|
[self confirmAdd];
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void) cancelAdd: (id) sender
|
|
|
|
{
|
|
|
|
[[self window] performClose: sender];
|
|
|
|
}
|
|
|
|
|
|
|
|
//only called on cancel
|
|
|
|
- (BOOL) windowShouldClose: (id) window
|
|
|
|
{
|
|
|
|
[fController askOpenMagnetConfirmed: self add: NO];
|
|
|
|
return YES;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void) changePriority: (id) sender
|
|
|
|
{
|
|
|
|
tr_priority_t priority;
|
|
|
|
switch ([sender indexOfSelectedItem])
|
|
|
|
{
|
|
|
|
case POPUP_PRIORITY_HIGH: priority = TR_PRI_HIGH; break;
|
|
|
|
case POPUP_PRIORITY_NORMAL: priority = TR_PRI_NORMAL; break;
|
|
|
|
case POPUP_PRIORITY_LOW: priority = TR_PRI_LOW; break;
|
2016-09-11 10:22:49 +00:00
|
|
|
default:
|
|
|
|
NSAssert1(NO, @"Unknown priority tag for adding torrent: %ld", [sender tag]);
|
|
|
|
priority = TR_PRI_NORMAL;
|
2010-04-12 00:55:31 +00:00
|
|
|
}
|
|
|
|
[fTorrent setPriority: priority];
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void) updateGroupMenu: (NSNotification *) notification
|
|
|
|
{
|
|
|
|
[self setGroupsMenu];
|
|
|
|
if (![fGroupPopUp selectItemWithTag: fGroupValue])
|
|
|
|
{
|
|
|
|
fGroupValue = -1;
|
2017-01-24 17:53:16 +00:00
|
|
|
fGroupDeterminationType = TorrentDeterminationAutomatic;
|
2010-04-12 00:55:31 +00:00
|
|
|
[fGroupPopUp selectItemWithTag: fGroupValue];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@end
|
|
|
|
|
|
|
|
@implementation AddMagnetWindowController (Private)
|
|
|
|
|
|
|
|
- (void) confirmAdd
|
|
|
|
{
|
2012-10-30 00:22:10 +00:00
|
|
|
[fTorrent setGroupValue: fGroupValue determinationType: fGroupDeterminationType];
|
2017-01-24 17:53:16 +00:00
|
|
|
|
2011-08-02 12:43:26 +00:00
|
|
|
if ([fStartCheck state] == NSOnState)
|
|
|
|
[fTorrent startTransfer];
|
2017-01-24 17:53:16 +00:00
|
|
|
|
2010-04-12 00:55:31 +00:00
|
|
|
[self close];
|
|
|
|
[fController askOpenMagnetConfirmed: self add: YES]; //ensure last, since it releases this controller
|
|
|
|
}
|
|
|
|
|
2012-10-30 00:22:10 +00:00
|
|
|
- (void) setDestinationPath: (NSString *) destination determinationType: (TorrentDeterminationType) determinationType
|
2010-04-12 00:55:31 +00:00
|
|
|
{
|
|
|
|
destination = [destination stringByExpandingTildeInPath];
|
|
|
|
if (!fDestination || ![fDestination isEqualToString: destination])
|
2017-01-24 17:53:16 +00:00
|
|
|
{
|
2012-03-13 02:52:11 +00:00
|
|
|
[fDestination release];
|
|
|
|
fDestination = [destination retain];
|
2017-01-24 17:53:16 +00:00
|
|
|
|
2012-10-30 00:22:10 +00:00
|
|
|
[fTorrent changeDownloadFolderBeforeUsing: fDestination determinationType: determinationType];
|
2010-04-12 00:55:31 +00:00
|
|
|
}
|
2017-01-24 17:53:16 +00:00
|
|
|
|
2010-04-12 00:55:31 +00:00
|
|
|
[fLocationField setStringValue: [fDestination stringByAbbreviatingWithTildeInPath]];
|
|
|
|
[fLocationField setToolTip: fDestination];
|
2017-01-24 17:53:16 +00:00
|
|
|
|
2010-04-12 00:55:31 +00:00
|
|
|
ExpandedPathToIconTransformer * iconTransformer = [[ExpandedPathToIconTransformer alloc] init];
|
|
|
|
[fLocationImageView setImage: [iconTransformer transformedValue: fDestination]];
|
2012-03-13 02:52:11 +00:00
|
|
|
[iconTransformer release];
|
2010-04-12 00:55:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
- (void) setGroupsMenu
|
|
|
|
{
|
|
|
|
NSMenu * groupMenu = [[GroupsController groups] groupMenuWithTarget: self action: @selector(changeGroupValue:) isSmall: NO];
|
|
|
|
[fGroupPopUp setMenu: groupMenu];
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void) changeGroupValue: (id) sender
|
|
|
|
{
|
|
|
|
NSInteger previousGroup = fGroupValue;
|
2017-01-24 17:53:16 +00:00
|
|
|
fGroupValue = [sender tag];
|
2012-10-30 00:22:10 +00:00
|
|
|
fGroupDeterminationType = TorrentDeterminationUserSpecified;
|
|
|
|
|
2010-04-12 00:55:31 +00:00
|
|
|
if ([[GroupsController groups] usesCustomDownloadLocationForIndex: fGroupValue])
|
2012-10-30 00:22:10 +00:00
|
|
|
[self setDestinationPath: [[GroupsController groups] customDownloadLocationForIndex: fGroupValue] determinationType: TorrentDeterminationAutomatic];
|
2010-04-12 00:55:31 +00:00
|
|
|
else if ([fDestination isEqualToString: [[GroupsController groups] customDownloadLocationForIndex: previousGroup]])
|
2012-10-30 00:22:10 +00:00
|
|
|
[self setDestinationPath: [[NSUserDefaults standardUserDefaults] stringForKey: @"DownloadFolder"] determinationType: TorrentDeterminationAutomatic];
|
2010-04-12 00:55:31 +00:00
|
|
|
else;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void) sameNameAlertDidEnd: (NSAlert *) alert returnCode: (NSInteger) returnCode contextInfo: (void *) contextInfo
|
|
|
|
{
|
|
|
|
if ([[alert suppressionButton] state] == NSOnState)
|
|
|
|
[[NSUserDefaults standardUserDefaults] setBool: NO forKey: @"WarningFolderDataSameName"];
|
2017-01-24 17:53:16 +00:00
|
|
|
|
2012-03-13 02:52:11 +00:00
|
|
|
[alert release];
|
2017-01-24 17:53:16 +00:00
|
|
|
|
2010-04-12 00:55:31 +00:00
|
|
|
if (returnCode == NSAlertSecondButtonReturn)
|
|
|
|
[self performSelectorOnMainThread: @selector(confirmAdd) withObject: nil waitUntilDone: NO];
|
|
|
|
}
|
|
|
|
|
|
|
|
@end
|