transmission/macosx/AddWindowController.m

372 lines
12 KiB
Mathematica
Raw Normal View History

/******************************************************************************
* $Id$
*
2010-01-01 21:12:04 +00:00
* Copyright (c) 2008-2010 Transmission authors and contributors
*
* 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 "AddWindowController.h"
#import "Controller.h"
2009-10-11 03:36:50 +00:00
#import "ExpandedPathToIconTransformer.h"
#import "FileOutlineController.h"
#import "GroupsController.h"
#import "NSStringAdditions.h"
2009-10-11 03:36:50 +00:00
#import "Torrent.h"
2008-11-01 22:08:02 +00:00
#define UPDATE_SECONDS 1.0
#define POPUP_PRIORITY_HIGH 0
#define POPUP_PRIORITY_NORMAL 1
#define POPUP_PRIORITY_LOW 2
@interface AddWindowController (Private)
- (void) updateFiles;
- (void) confirmAdd;
- (void) setDestinationPath: (NSString *) destination;
2008-10-28 00:08:49 +00:00
- (void) folderChoiceClosed: (NSOpenPanel *) openPanel returnCode: (NSInteger) code contextInfo: (void *) contextInfo;
- (void) setGroupsMenu;
- (void) changeGroupValue: (id) sender;
2008-10-28 00:08:49 +00:00
- (void) sameNameAlertDidEnd: (NSAlert *) alert returnCode: (NSInteger) returnCode contextInfo: (void *) contextInfo;
@end
@implementation AddWindowController
- (id) initWithTorrent: (Torrent *) torrent destination: (NSString *) path lockDestination: (BOOL) lockDestination
controller: (Controller *) controller torrentFile: (NSString *) torrentFile
deleteTorrent: (BOOL) deleteTorrent canToggleDelete: (BOOL) canToggleDelete
{
if ((self = [super initWithWindowNibName: @"AddWindow"]))
{
fTorrent = torrent;
fDestination = [[path stringByExpandingTildeInPath] retain];
fLockDestination = lockDestination;
fController = controller;
fTorrentFile = [[torrentFile stringByExpandingTildeInPath] retain];
fDeleteTorrent = deleteTorrent;
fDeleteEnable = canToggleDelete;
fGroupValue = [torrent groupValue];
[fVerifyIndicator setUsesThreadedAnimation: YES];
}
return self;
}
- (void) awakeFromNib
{
2008-02-27 19:34:55 +00:00
[[NSNotificationCenter defaultCenter] addObserver: self selector: @selector(updateStatusField:)
name: @"TorrentFileCheckChange" object: fTorrent];
[[NSNotificationCenter defaultCenter] addObserver: self selector: @selector(updateGroupMenu:)
name: @"UpdateGroups" object: nil];
2008-01-04 15:28:09 +00:00
[fFileController setTorrent: fTorrent];
NSString * name = [fTorrent name];
[[self window] setTitle: name];
[fNameField setStringValue: name];
[fIconView setImage: [fTorrent icon]];
2008-02-27 19:34:55 +00:00
[self updateStatusField: nil];
[self setGroupsMenu];
[fGroupPopUp selectItemWithTag: fGroupValue];
2008-01-04 14:50:28 +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;
default: NSAssert1(NO, @"Unknown priority for adding torrent: %d", [fTorrent priority]);
}
[fPriorityPopUp selectItemAtIndex: priorityIndex];
[fStartCheck setState: [[NSUserDefaults standardUserDefaults] boolForKey: @"AutoStartDownload"] ? NSOnState : NSOffState];
[fDeleteCheck setState: fDeleteTorrent ? NSOnState : NSOffState];
[fDeleteCheck setEnabled: fDeleteEnable];
if (fDestination)
[self setDestinationPath: fDestination];
else
{
[fLocationField setStringValue: @""];
[fLocationImageView setImage: nil];
}
fTimer = [NSTimer scheduledTimerWithTimeInterval: UPDATE_SECONDS target: self
selector: @selector(updateFiles) userInfo: nil repeats: YES];
[self updateFiles];
}
- (void) windowDidLoad
{
//if there is no destination, prompt for one right away
if (!fDestination)
[self setDestination: nil];
}
- (void) dealloc
{
[[NSNotificationCenter defaultCenter] removeObserver: self];
[fTimer invalidate];
[fDestination release];
[fTorrentFile release];
[super dealloc];
}
- (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];
[panel setMessage: [NSString stringWithFormat: NSLocalizedString(@"Select the download folder for \"%@\"",
"Add -> select destination folder"), [fTorrent name]]];
[panel beginSheetForDirectory: nil file: nil types: nil modalForWindow: [self window] modalDelegate: self
didEndSelector: @selector(folderChoiceClosed:returnCode:contextInfo:) contextInfo: nil];
}
- (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")];
2008-12-26 05:57:51 +00:00
[alert setShowsSuppressionButton: YES];
[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
{
[fTimer invalidate];
fTimer = nil;
[fFileController setTorrent: nil]; //avoid a crash when window tries to update
[fController askOpenConfirmed: self add: NO];
return YES;
}
- (void) verifyLocalData: (id) sender
{
[fTorrent resetCache];
[self updateFiles];
}
- (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;
default: NSAssert1(NO, @"Unknown priority tag for adding torrent: %d", [sender tag]);
}
[fTorrent setPriority: priority];
}
2008-02-27 19:34:55 +00:00
- (void) updateStatusField: (NSNotification *) notification
{
NSString * statusString = [NSString stringForFileSize: [fTorrent size]];
if ([fTorrent isFolder])
2008-02-27 19:34:55 +00:00
{
NSString * fileString;
2008-10-28 00:08:49 +00:00
NSInteger count = [fTorrent fileCount];
2008-02-27 19:34:55 +00:00
if (count != 1)
2008-04-07 13:29:46 +00:00
fileString = [NSString stringWithFormat: NSLocalizedString(@"%d files", "Add torrent -> info"), count];
2008-02-27 19:34:55 +00:00
else
2008-04-07 13:29:46 +00:00
fileString = NSLocalizedString(@"1 file", "Add torrent -> info");
2008-02-27 19:58:15 +00:00
2008-04-06 04:16:05 +00:00
NSString * selectedString = [NSString stringWithFormat: NSLocalizedString(@"%@ selected", "Add torrent -> info"),
[NSString stringForFileSize: [fTorrent totalSizeSelected]]];
statusString = [NSString stringWithFormat: @"%@, %@ (%@)", fileString, statusString, selectedString];
2008-02-27 19:34:55 +00:00
}
[fStatusField setStringValue: statusString];
}
- (void) updateGroupMenu: (NSNotification *) notification
{
[self setGroupsMenu];
if (![fGroupPopUp selectItemWithTag: fGroupValue])
{
fGroupValue = -1;
[fGroupPopUp selectItemWithTag: fGroupValue];
}
}
@end
@implementation AddWindowController (Private)
- (void) updateFiles
{
[fTorrent update];
[fFileController reloadData];
if ([fTorrent isChecking])
{
const BOOL waiting = [fTorrent isCheckingWaiting];
[fVerifyIndicator setIndeterminate: waiting];
if (!waiting)
[fVerifyIndicator setDoubleValue: [fTorrent checkingProgress]];
else
[fVerifyIndicator startAnimation: self];
[fVerifyIndicator setHidden: NO];
}
else
{
[fVerifyIndicator stopAnimation: self];
[fVerifyIndicator setHidden: YES];
}
}
- (void) confirmAdd
{
[fTimer invalidate];
fTimer = nil;
[fTorrent setWaitToStart: [fStartCheck state] == NSOnState];
[fTorrent setGroupValue: fGroupValue];
if (fTorrentFile && [fDeleteCheck state] == NSOnState)
[Torrent trashFile: fTorrentFile];
[fFileController setTorrent: nil]; //avoid a crash when window tries to update
[self close];
[fController askOpenConfirmed: self add: YES]; //ensure last, since it releases this controller
}
2008-11-30 21:23:59 +00:00
- (void) setDestinationPath: (NSString *) destination
{
destination = [destination stringByExpandingTildeInPath];
if (!fDestination || ![fDestination isEqualToString: destination])
{
[fDestination release];
fDestination = [destination retain];
[fTorrent changeDownloadFolderBeforeUsing: fDestination];
}
[fLocationField setStringValue: [fDestination stringByAbbreviatingWithTildeInPath]];
[fLocationField setToolTip: fDestination];
ExpandedPathToIconTransformer * iconTransformer = [[ExpandedPathToIconTransformer alloc] init];
[fLocationImageView setImage: [iconTransformer transformedValue: fDestination]];
[iconTransformer release];
}
2008-10-28 00:08:49 +00:00
- (void) folderChoiceClosed: (NSOpenPanel *) openPanel returnCode: (NSInteger) code contextInfo: (void *) contextInfo
{
if (code == NSOKButton)
{
fLockDestination = NO;
2008-11-30 21:23:59 +00:00
[self setDestinationPath: [[openPanel filenames] objectAtIndex: 0]];
}
else
{
if (!fDestination)
[self performSelectorOnMainThread: @selector(cancelAdd:) withObject: nil waitUntilDone: NO];
}
}
- (void) setGroupsMenu
{
NSMenu * groupMenu = [[GroupsController groups] groupMenuWithTarget: self action: @selector(changeGroupValue:) isSmall: NO];
[fGroupPopUp setMenu: groupMenu];
}
- (void) changeGroupValue: (id) sender
{
2008-11-30 21:23:59 +00:00
NSInteger previousGroup = fGroupValue;
fGroupValue = [sender tag];
if (!fLockDestination)
{
if ([[GroupsController groups] usesCustomDownloadLocationForIndex: fGroupValue])
[self setDestinationPath: [[GroupsController groups] customDownloadLocationForIndex: fGroupValue]];
else if ([fDestination isEqualToString: [[GroupsController groups] customDownloadLocationForIndex: previousGroup]])
[self setDestinationPath: [[NSUserDefaults standardUserDefaults] stringForKey: @"DownloadFolder"]];
else;
}
}
2008-10-28 00:08:49 +00:00
- (void) sameNameAlertDidEnd: (NSAlert *) alert returnCode: (NSInteger) returnCode contextInfo: (void *) contextInfo
{
2008-12-26 05:57:51 +00:00
if ([[alert suppressionButton] state] == NSOnState)
[[NSUserDefaults standardUserDefaults] setBool: NO forKey: @"WarningFolderDataSameName"];
[alert release];
if (returnCode == NSAlertSecondButtonReturn)
[self performSelectorOnMainThread: @selector(confirmAdd) withObject: nil waitUntilDone: NO];
}
@end