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"
|
|
|
|
#import "Controller.h"
|
2008-01-04 14:50:28 +00:00
|
|
|
#import "GroupsWindowController.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-01-09 19:52:11 +00:00
|
|
|
#define UPDATE_SECONDS 1.0
|
|
|
|
|
2008-01-04 04:45:31 +00:00
|
|
|
@interface AddWindowController (Private)
|
|
|
|
|
2008-01-11 19:54:08 +00:00
|
|
|
- (void) confirmAdd;
|
|
|
|
|
2008-01-04 04:45:31 +00:00
|
|
|
- (void) folderChoiceClosed: (NSOpenPanel *) openPanel returnCode: (int) 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-01-11 19:54:08 +00:00
|
|
|
- (void) sameNameAlertDidEnd: (NSAlert *) alert returnCode: (int) returnCode contextInfo: (void *) contextInfo;
|
|
|
|
|
2008-01-04 04:45:31 +00:00
|
|
|
@end
|
|
|
|
|
|
|
|
@implementation AddWindowController
|
|
|
|
|
|
|
|
- (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-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];
|
|
|
|
|
|
|
|
NSString * statusString = [NSString stringForFileSize: [fTorrent size]];
|
|
|
|
if ([fTorrent folder])
|
|
|
|
{
|
|
|
|
NSString * fileString;
|
|
|
|
int count = [fTorrent fileCount];
|
|
|
|
if (count != 1)
|
|
|
|
fileString = [NSString stringWithFormat: NSLocalizedString(@"%d Files, ", "Add torrent -> info"), count];
|
|
|
|
else
|
|
|
|
fileString = NSLocalizedString(@"1 File, ", "Add torrent -> info");
|
|
|
|
statusString = [fileString stringByAppendingString: statusString];
|
|
|
|
}
|
|
|
|
[fStatusField setStringValue: statusString];
|
|
|
|
|
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-01-05 21:31:05 +00:00
|
|
|
[fController askOpenConfirmed: self add: NO];
|
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-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];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void) showGroupsWindow: (id) sender
|
|
|
|
{
|
|
|
|
[fGroupPopUp selectItemWithTag: fGroupValue];
|
|
|
|
[fController showGroups: sender];
|
2008-01-04 14:56:29 +00:00
|
|
|
}
|
|
|
|
|
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];
|
|
|
|
|
|
|
|
//ensure last, since it releases this controller
|
|
|
|
[fController askOpenConfirmed: self add: YES];
|
|
|
|
}
|
|
|
|
|
2008-01-04 04:45:31 +00:00
|
|
|
- (void) folderChoiceClosed: (NSOpenPanel *) openPanel returnCode: (int) code contextInfo: (void *) contextInfo
|
|
|
|
{
|
|
|
|
if (code == NSOKButton)
|
|
|
|
{
|
|
|
|
[fDestination release];
|
|
|
|
fDestination = [[[openPanel filenames] objectAtIndex: 0] retain];
|
|
|
|
|
|
|
|
[fLocationField setStringValue: [fDestination stringByAbbreviatingWithTildeInPath]];
|
|
|
|
[fLocationField setToolTip: fDestination];
|
|
|
|
|
|
|
|
ExpandedPathToIconTransformer * iconTransformer = [[ExpandedPathToIconTransformer alloc] init];
|
|
|
|
[fLocationImageView setImage: [iconTransformer transformedValue: fDestination]];
|
|
|
|
[iconTransformer release];
|
2008-01-04 05:43:53 +00:00
|
|
|
|
|
|
|
[fTorrent changeDownloadFolder: fDestination];
|
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
|
|
|
|
{
|
|
|
|
NSMenu * menu = [fGroupPopUp menu];
|
|
|
|
|
|
|
|
int i;
|
|
|
|
for (i = [menu numberOfItems]-1 - 2; i >= 0; i--)
|
|
|
|
[menu removeItemAtIndex: i];
|
|
|
|
|
|
|
|
NSMenu * groupMenu = [[GroupsWindowController groups] groupMenuWithTarget: self action: @selector(changeGroupValue:) isSmall: NO];
|
|
|
|
[menu appendItemsFromMenu: groupMenu atIndexes: [NSIndexSet indexSetWithIndexesInRange:
|
|
|
|
NSMakeRange(0, [groupMenu numberOfItems])] atBottom: NO];
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void) changeGroupValue: (id) sender
|
|
|
|
{
|
|
|
|
fGroupValue = [sender tag];
|
|
|
|
}
|
|
|
|
|
2008-01-11 19:54:08 +00:00
|
|
|
- (void) sameNameAlertDidEnd: (NSAlert *) alert returnCode: (int) returnCode contextInfo: (void *) contextInfo
|
|
|
|
{
|
|
|
|
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
|