2008-01-04 04:45:31 +00:00
|
|
|
/******************************************************************************
|
|
|
|
* $Id$
|
|
|
|
*
|
|
|
|
* Copyright (c) 2008 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"
|
2008-11-01 22:08:02 +00:00
|
|
|
#import "FileOutlineController.h"
|
2008-01-04 04:45:31 +00:00
|
|
|
#import "Controller.h"
|
2008-03-23 00:56:43 +00:00
|
|
|
#import "GroupsController.h"
|
2008-01-04 04:45:31 +00:00
|
|
|
#import "NSStringAdditions.h"
|
2008-01-05 22:32:34 +00:00
|
|
|
#import "NSMenuAdditions.h"
|
2008-01-11 19:54:08 +00:00
|
|
|
#import "NSApplicationAdditions.h"
|
2008-01-04 04:45:31 +00:00
|
|
|
#import "ExpandedPathToIconTransformer.h"
|
|
|
|
|
2008-11-01 22:08:02 +00:00
|
|
|
#define UPDATE_SECONDS 1.0
|
2008-01-09 19:52:11 +00:00
|
|
|
|
2008-01-04 04:45:31 +00:00
|
|
|
@interface AddWindowController (Private)
|
|
|
|
|
2008-01-11 19:54:08 +00:00
|
|
|
- (void) confirmAdd;
|
|
|
|
|
2008-10-28 00:08:49 +00:00
|
|
|
- (void) folderChoiceClosed: (NSOpenPanel *) openPanel returnCode: (NSInteger) code contextInfo: (void *) contextInfo;
|
2008-01-09 19:52:11 +00:00
|
|
|
|
2008-01-05 22:32:34 +00:00
|
|
|
- (void) setGroupsMenu;
|
|
|
|
- (void) changeGroupValue: (id) sender;
|
2008-01-04 04:45:31 +00:00
|
|
|
|
2008-10-28 00:08:49 +00:00
|
|
|
- (void) sameNameAlertDidEnd: (NSAlert *) alert returnCode: (NSInteger) returnCode contextInfo: (void *) contextInfo;
|
2008-01-11 19:54:08 +00:00
|
|
|
|
2008-01-04 04:45:31 +00:00
|
|
|
@end
|
|
|
|
|
|
|
|
@implementation AddWindowController
|
|
|
|
|
2008-12-01 23:09:48 +00:00
|
|
|
#warning don't let group override destination if the destination is already specified (as in adding from creation)
|
2008-01-04 04:45:31 +00:00
|
|
|
- (id) initWithTorrent: (Torrent *) torrent destination: (NSString *) path controller: (Controller *) controller
|
2008-01-04 06:31:41 +00:00
|
|
|
deleteTorrent: (torrentFileState) deleteTorrent
|
2008-01-04 04:45:31 +00:00
|
|
|
{
|
|
|
|
if ((self = [super initWithWindowNibName: @"AddWindow"]))
|
|
|
|
{
|
|
|
|
fTorrent = torrent;
|
|
|
|
if (path)
|
|
|
|
fDestination = [[path stringByExpandingTildeInPath] retain];
|
|
|
|
|
|
|
|
fController = controller;
|
2008-01-04 06:31:41 +00:00
|
|
|
|
|
|
|
fDeleteTorrent = deleteTorrent == TORRENT_FILE_DELETE || (deleteTorrent == TORRENT_FILE_DEFAULT
|
|
|
|
&& [[NSUserDefaults standardUserDefaults] boolForKey: @"DeleteOriginalTorrent"]);
|
2008-01-04 18:10:37 +00:00
|
|
|
fDeleteEnable = deleteTorrent == TORRENT_FILE_DEFAULT;
|
2008-01-05 22:32:34 +00:00
|
|
|
|
|
|
|
fGroupValue = -1;
|
2008-01-04 04:45:31 +00:00
|
|
|
}
|
|
|
|
return self;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void) awakeFromNib
|
|
|
|
{
|
2008-02-27 19:34:55 +00:00
|
|
|
[[NSNotificationCenter defaultCenter] addObserver: self selector: @selector(updateStatusField:)
|
|
|
|
name: @"TorrentFileCheckChange" object: fTorrent];
|
|
|
|
|
2008-01-04 15:05:25 +00:00
|
|
|
[[NSNotificationCenter defaultCenter] addObserver: self selector: @selector(updateGroupMenu:)
|
|
|
|
name: @"UpdateGroups" object: nil];
|
2008-01-04 14:56:29 +00:00
|
|
|
|
2008-01-04 15:28:09 +00:00
|
|
|
[fFileController setTorrent: fTorrent];
|
|
|
|
|
2008-01-04 04:45:31 +00:00
|
|
|
NSString * name = [fTorrent name];
|
|
|
|
[[self window] setTitle: name];
|
|
|
|
[fNameField setStringValue: name];
|
|
|
|
|
|
|
|
NSImage * icon = [[fTorrent icon] copy];
|
|
|
|
[icon setFlipped: NO];
|
|
|
|
[fIconView setImage: icon];
|
|
|
|
[icon release];
|
|
|
|
|
2008-02-27 19:34:55 +00:00
|
|
|
[self updateStatusField: nil];
|
2008-01-04 04:45:31 +00:00
|
|
|
|
2008-01-05 22:32:34 +00:00
|
|
|
[self setGroupsMenu];
|
|
|
|
[fGroupPopUp selectItemWithTag: -1];
|
2008-01-04 14:50:28 +00:00
|
|
|
|
2008-01-04 04:45:31 +00:00
|
|
|
[fStartCheck setState: [[NSUserDefaults standardUserDefaults] boolForKey: @"AutoStartDownload"] ? NSOnState : NSOffState];
|
|
|
|
|
2008-01-04 18:10:37 +00:00
|
|
|
[fDeleteCheck setState: fDeleteTorrent ? NSOnState : NSOffState];
|
|
|
|
[fDeleteCheck setEnabled: fDeleteEnable];
|
|
|
|
|
2008-01-04 04:45:31 +00:00
|
|
|
if (fDestination)
|
|
|
|
{
|
|
|
|
[fLocationField setStringValue: [fDestination stringByAbbreviatingWithTildeInPath]];
|
|
|
|
[fLocationField setToolTip: fDestination];
|
|
|
|
|
|
|
|
ExpandedPathToIconTransformer * iconTransformer = [[ExpandedPathToIconTransformer alloc] init];
|
|
|
|
[fLocationImageView setImage: [iconTransformer transformedValue: fDestination]];
|
|
|
|
[iconTransformer release];
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2008-01-05 03:06:20 +00:00
|
|
|
[fLocationField setStringValue: @""];
|
|
|
|
[fLocationImageView setImage: nil];
|
2008-01-04 04:45:31 +00:00
|
|
|
}
|
2008-01-09 19:52:11 +00:00
|
|
|
|
2008-01-09 21:08:43 +00:00
|
|
|
fTimer = [NSTimer scheduledTimerWithTimeInterval: UPDATE_SECONDS target: fFileController
|
|
|
|
selector: @selector(reloadData) userInfo: nil repeats: YES];
|
2008-01-04 04:45:31 +00:00
|
|
|
}
|
|
|
|
|
2008-01-05 03:06:20 +00:00
|
|
|
- (void) windowDidLoad
|
|
|
|
{
|
|
|
|
//if there is no destination, prompt for one right away
|
|
|
|
if (!fDestination)
|
|
|
|
[self setDestination: nil];
|
|
|
|
}
|
|
|
|
|
2008-01-04 04:45:31 +00:00
|
|
|
- (void) dealloc
|
2008-01-09 21:08:43 +00:00
|
|
|
{
|
2008-01-04 14:56:29 +00:00
|
|
|
[[NSNotificationCenter defaultCenter] removeObserver: self];
|
|
|
|
|
2008-01-09 19:52:11 +00:00
|
|
|
[fTimer invalidate];
|
|
|
|
|
2008-01-04 04:45:31 +00:00
|
|
|
[fDestination release];
|
|
|
|
|
|
|
|
[super dealloc];
|
|
|
|
}
|
|
|
|
|
2008-01-05 21:31:05 +00:00
|
|
|
- (Torrent *) torrent
|
|
|
|
{
|
|
|
|
return fTorrent;
|
|
|
|
}
|
|
|
|
|
2008-01-04 04:45:31 +00:00
|
|
|
- (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
|
|
|
|
{
|
2008-01-11 19:54:08 +00:00
|
|
|
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")];
|
|
|
|
|
|
|
|
if ([NSApp isOnLeopardOrBetter])
|
|
|
|
[alert setShowsSuppressionButton: YES];
|
|
|
|
else
|
|
|
|
[alert addButtonWithTitle: NSLocalizedString(@"Don't Alert Again", "Add torrent -> same name -> button")];
|
|
|
|
|
|
|
|
[alert beginSheetModalForWindow: [self window] modalDelegate: self
|
|
|
|
didEndSelector: @selector(sameNameAlertDidEnd:returnCode:contextInfo:) contextInfo: nil];
|
|
|
|
}
|
|
|
|
else
|
|
|
|
[self confirmAdd];
|
2008-01-04 04:45:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
- (void) cancelAdd: (id) sender
|
2008-01-16 23:17:36 +00:00
|
|
|
{
|
|
|
|
[[self window] performClose: sender];
|
|
|
|
}
|
|
|
|
|
|
|
|
//only called on cancel
|
|
|
|
- (BOOL) windowShouldClose: (id) window
|
2008-01-04 04:45:31 +00:00
|
|
|
{
|
2008-01-09 19:52:11 +00:00
|
|
|
[fTimer invalidate];
|
|
|
|
fTimer = nil;
|
|
|
|
|
2008-02-13 19:33:04 +00:00
|
|
|
[fFileController setTorrent: nil]; //avoid a crash when window tries to update
|
|
|
|
|
2008-01-05 21:31:05 +00:00
|
|
|
[fController askOpenConfirmed: self add: NO];
|
2008-02-13 19:33:04 +00:00
|
|
|
return YES;
|
2008-01-04 04:45:31 +00:00
|
|
|
}
|
|
|
|
|
2008-01-09 19:52:11 +00:00
|
|
|
- (void) verifyLocalData: (id) sender
|
|
|
|
{
|
|
|
|
[fTorrent resetCache];
|
2008-01-09 21:08:43 +00:00
|
|
|
[fFileController reloadData];
|
2008-01-09 19:52:11 +00:00
|
|
|
}
|
|
|
|
|
2008-02-27 19:34:55 +00:00
|
|
|
- (void) updateStatusField: (NSNotification *) notification
|
|
|
|
{
|
|
|
|
NSString * statusString = [NSString stringForFileSize: [fTorrent size]];
|
2008-05-29 22:16:03 +00:00
|
|
|
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];
|
|
|
|
}
|
|
|
|
|
2008-01-04 15:05:25 +00:00
|
|
|
- (void) updateGroupMenu: (NSNotification *) notification
|
2008-01-04 14:56:29 +00:00
|
|
|
{
|
2008-01-05 22:32:34 +00:00
|
|
|
[self setGroupsMenu];
|
|
|
|
if (![fGroupPopUp selectItemWithTag: fGroupValue])
|
|
|
|
{
|
|
|
|
fGroupValue = -1;
|
|
|
|
[fGroupPopUp selectItemWithTag: fGroupValue];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-01-04 04:45:31 +00:00
|
|
|
@end
|
|
|
|
|
|
|
|
@implementation AddWindowController (Private)
|
|
|
|
|
2008-01-11 19:54:08 +00:00
|
|
|
- (void) confirmAdd
|
|
|
|
{
|
|
|
|
[fTimer invalidate];
|
|
|
|
fTimer = nil;
|
|
|
|
|
|
|
|
[fTorrent setWaitToStart: [fStartCheck state] == NSOnState];
|
|
|
|
[fTorrent setGroupValue: [[fGroupPopUp selectedItem] tag]];
|
|
|
|
|
|
|
|
if ([fDeleteCheck state] == NSOnState)
|
|
|
|
[fTorrent trashTorrent];
|
|
|
|
|
2008-02-13 19:33:04 +00:00
|
|
|
[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-01-11 19:54:08 +00:00
|
|
|
}
|
|
|
|
|
2008-11-30 21:23:59 +00:00
|
|
|
- (void) setDestinationPath: (NSString *) destination
|
2008-11-30 19:23:15 +00:00
|
|
|
{
|
|
|
|
[fDestination release];
|
|
|
|
fDestination = [destination retain];
|
|
|
|
|
|
|
|
[fLocationField setStringValue: [fDestination stringByAbbreviatingWithTildeInPath]];
|
|
|
|
[fLocationField setToolTip: fDestination];
|
|
|
|
|
|
|
|
ExpandedPathToIconTransformer * iconTransformer = [[ExpandedPathToIconTransformer alloc] init];
|
|
|
|
[fLocationImageView setImage: [iconTransformer transformedValue: fDestination]];
|
|
|
|
[iconTransformer release];
|
|
|
|
|
|
|
|
[fTorrent changeDownloadFolder: fDestination];
|
|
|
|
}
|
|
|
|
|
2008-10-28 00:08:49 +00:00
|
|
|
- (void) folderChoiceClosed: (NSOpenPanel *) openPanel returnCode: (NSInteger) code contextInfo: (void *) contextInfo
|
2008-01-04 04:45:31 +00:00
|
|
|
{
|
|
|
|
if (code == NSOKButton)
|
2008-11-30 21:23:59 +00:00
|
|
|
[self setDestinationPath: [[openPanel filenames] objectAtIndex: 0]];
|
2008-01-04 04:45:31 +00:00
|
|
|
else
|
|
|
|
{
|
|
|
|
if (!fDestination)
|
2008-01-05 21:31:05 +00:00
|
|
|
[self performSelectorOnMainThread: @selector(cancelAdd:) withObject: nil waitUntilDone: NO];
|
2008-01-04 04:45:31 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-01-05 22:32:34 +00:00
|
|
|
- (void) setGroupsMenu
|
|
|
|
{
|
2008-03-23 00:56:43 +00:00
|
|
|
NSMenu * groupMenu = [[GroupsController groups] groupMenuWithTarget: self action: @selector(changeGroupValue:) isSmall: NO];
|
2008-11-29 20:29:54 +00:00
|
|
|
[fGroupPopUp setMenu: groupMenu];
|
2008-01-05 22:32:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
- (void) changeGroupValue: (id) sender
|
|
|
|
{
|
2008-11-30 21:23:59 +00:00
|
|
|
NSInteger previousGroup = fGroupValue;
|
2008-01-05 22:32:34 +00:00
|
|
|
fGroupValue = [sender tag];
|
2008-11-30 19:23:15 +00:00
|
|
|
if ([[GroupsController groups] usesCustomDownloadLocationForIndex: fGroupValue] &&
|
|
|
|
[[GroupsController groups] customDownloadLocationForIndex: fGroupValue])
|
2008-11-30 21:23:59 +00:00
|
|
|
[self setDestinationPath: [[GroupsController groups] customDownloadLocationForIndex: fGroupValue]];
|
|
|
|
else if ([fDestination isEqualToString: [[GroupsController groups] customDownloadLocationForIndex: previousGroup]])
|
|
|
|
[self setDestinationPath: [[NSUserDefaults standardUserDefaults] stringForKey: @"DownloadFolder"]];
|
|
|
|
else;
|
2008-01-05 22:32:34 +00:00
|
|
|
}
|
|
|
|
|
2008-10-28 00:08:49 +00:00
|
|
|
- (void) sameNameAlertDidEnd: (NSAlert *) alert returnCode: (NSInteger) returnCode contextInfo: (void *) contextInfo
|
2008-01-11 19:54:08 +00:00
|
|
|
{
|
|
|
|
if (([NSApp isOnLeopardOrBetter] ? [[alert suppressionButton] state] == NSOnState : returnCode == NSAlertThirdButtonReturn))
|
|
|
|
[[NSUserDefaults standardUserDefaults] setBool: NO forKey: @"WarningFolderDataSameName"];
|
|
|
|
|
|
|
|
[alert release];
|
|
|
|
|
|
|
|
if (returnCode == NSAlertSecondButtonReturn)
|
|
|
|
[self performSelectorOnMainThread: @selector(confirmAdd) withObject: nil waitUntilDone: NO];
|
|
|
|
}
|
|
|
|
|
2008-01-04 04:45:31 +00:00
|
|
|
@end
|