1
0
Fork 0
mirror of https://github.com/transmission/transmission synced 2024-12-29 11:06:23 +00:00
transmission/macosx/CreatorWindowController.m

522 lines
22 KiB
Mathematica
Raw Normal View History

2007-09-16 01:02:06 +00:00
/******************************************************************************
* $Id$
*
2009-01-10 23:37:37 +00:00
* Copyright (c) 2007-2009 Transmission authors and contributors
2007-09-16 01:02:06 +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 "CreatorWindowController.h"
#import "NSStringAdditions.h"
2008-11-01 22:08:02 +00:00
#import "utils.h" //tr_httpIsValidURL
2007-09-16 01:02:06 +00:00
#define TRACKER_ADD_TAG 0
#define TRACKER_REMOVE_TAG 1
2007-09-16 01:02:06 +00:00
@interface CreatorWindowController (Private)
+ (NSString *) chooseFile;
2008-11-04 01:31:24 +00:00
- (void) locationSheetClosed: (NSSavePanel *) openPanel returnCode: (NSInteger) code contextInfo: (void *) info;
2008-11-04 01:31:24 +00:00
- (void) createBlankAddressAlertDidEnd: (NSAlert *) alert returnCode: (NSInteger) returnCode contextInfo: (void *) contextInfo;
- (void) createReal;
2007-09-16 01:02:06 +00:00
- (void) checkProgress;
2008-11-04 01:31:24 +00:00
- (void) failureSheetClosed: (NSAlert *) alert returnCode: (NSInteger) code contextInfo: (void *) info;
2007-09-16 01:02:06 +00:00
@end
@implementation CreatorWindowController
+ (void) createTorrentFile: (tr_session *) handle
2007-09-16 01:02:06 +00:00
{
//get file/folder for torrent
NSString * path;
if (!(path = [CreatorWindowController chooseFile]))
return;
CreatorWindowController * creator = [[self alloc] initWithHandle: handle path: path];
2007-09-16 01:02:06 +00:00
[creator showWindow: nil];
}
+ (void) createTorrentFile: (tr_session *) handle forFile: (NSString *) file
2007-09-16 01:02:06 +00:00
{
CreatorWindowController * creator = [[self alloc] initWithHandle: handle path: file];
2007-09-16 01:02:06 +00:00
[creator showWindow: nil];
}
- (id) initWithHandle: (tr_session *) handle path: (NSString *) path
2007-09-16 01:02:06 +00:00
{
if ((self = [super initWithWindowNibName: @"Creator"]))
2009-09-12 01:07:21 +00:00
{
2007-09-16 01:02:06 +00:00
fStarted = NO;
fPath = [path retain];
fInfo = tr_metaInfoBuilderCreate([fPath UTF8String]);
2007-09-16 01:02:06 +00:00
if (fInfo->fileCount == 0)
{
NSAlert * alert = [[NSAlert alloc] init];
[alert addButtonWithTitle: NSLocalizedString(@"OK", "Create torrent -> no files -> button")];
[alert setMessageText: NSLocalizedString(@"This folder contains no files.",
"Create torrent -> no files -> title")];
[alert setInformativeText: NSLocalizedString(@"There must be at least one file in a folder to create a torrent file.",
"Create torrent -> no files -> warning")];
[alert setAlertStyle: NSWarningAlertStyle];
[alert runModal];
[alert release];
[self release];
return nil;
}
if (fInfo->totalSize == 0)
{
NSAlert * alert = [[NSAlert alloc] init];
[alert addButtonWithTitle: NSLocalizedString(@"OK", "Create torrent -> zero size -> button")];
[alert setMessageText: NSLocalizedString(@"The total file size is zero bytes.",
"Create torrent -> zero size -> title")];
[alert setInformativeText: NSLocalizedString(@"A torrent file cannot be created for files with no size.",
"Create torrent -> zero size -> warning")];
[alert setAlertStyle: NSWarningAlertStyle];
[alert runModal];
[alert release];
[self release];
return nil;
}
fDefaults = [NSUserDefaults standardUserDefaults];
//get list of trackers
if (!(fTrackers = [[fDefaults arrayForKey: @"CreatorTrackers"] mutableCopy]))
{
fTrackers = [[NSMutableArray alloc] initWithCapacity: 1];
//check for single tracker from versions before 1.3
NSString * tracker;
if ((tracker = [fDefaults stringForKey: @"CreatorTracker"]))
{
[fDefaults removeObjectForKey: @"CreatorTracker"];
if (![tracker isEqualToString: @""])
{
[fTrackers addObject: tracker];
[fDefaults setObject: fTrackers forKey: @"CreatorTrackers"];
}
}
}
//remove potentially invalid addresses
2008-10-12 22:17:27 +00:00
for (NSInteger i = [fTrackers count]-1; i >= 0; i--)
{
if (!tr_httpIsValidURL([[fTrackers objectAtIndex: i] UTF8String]))
[fTrackers removeObjectAtIndex: i];
}
2007-09-16 01:02:06 +00:00
}
return self;
}
- (void) awakeFromNib
{
NSString * name = [fPath lastPathComponent];
[[self window] setTitle: name];
[fNameField setStringValue: name];
[fNameField setToolTip: fPath];
const BOOL multifile = !fInfo->isSingleFile;
2007-09-16 01:02:06 +00:00
NSImage * icon = [[NSWorkspace sharedWorkspace] iconForFileType: multifile
? NSFileTypeForHFSTypeCode('fldr') : [fPath pathExtension]];
[icon setSize: [fIconView frame].size];
[fIconView setImage: icon];
NSString * statusString = [NSString stringForFileSize: fInfo->totalSize];
if (multifile)
{
NSString * fileString;
NSInteger count = fInfo->fileCount;
2007-09-16 01:02:06 +00:00
if (count != 1)
2008-04-07 13:29:46 +00:00
fileString = [NSString stringWithFormat: NSLocalizedString(@"%d files", "Create torrent -> info"), count];
2007-09-16 01:02:06 +00:00
else
2008-04-07 13:29:46 +00:00
fileString = NSLocalizedString(@"1 file", "Create torrent -> info");
2008-03-18 22:11:43 +00:00
statusString = [NSString stringWithFormat: @"%@, %@", fileString, statusString];
2007-09-16 01:02:06 +00:00
}
[fStatusField setStringValue: statusString];
if (fInfo->pieceCount == 1)
[fPiecesField setStringValue: [NSString stringWithFormat: NSLocalizedString(@"1 piece, %@", "Create torrent -> info"),
[NSString stringForFileSize: fInfo->pieceSize]]];
2007-09-16 01:02:06 +00:00
else
[fPiecesField setStringValue: [NSString stringWithFormat: NSLocalizedString(@"%d pieces, %@ each", "Create torrent -> info"),
fInfo->pieceCount, [NSString stringForFileSize: fInfo->pieceSize]]];
2007-09-16 01:02:06 +00:00
fLocation = [[[[fDefaults stringForKey: @"CreatorLocation"] stringByExpandingTildeInPath] stringByAppendingPathComponent:
[name stringByAppendingPathExtension: @"torrent"]] retain];
[fLocationField setStringValue: [fLocation stringByAbbreviatingWithTildeInPath]];
[fLocationField setToolTip: fLocation];
//set previously saved values
if ([fDefaults objectForKey: @"CreatorPrivate"])
[fPrivateCheck setState: [fDefaults boolForKey: @"CreatorPrivate"] ? NSOnState : NSOffState];
[fOpenCheck setState: [fDefaults boolForKey: @"CreatorOpen"] ? NSOnState : NSOffState];
2007-09-16 01:02:06 +00:00
}
- (void) dealloc
{
[fPath release];
[fLocation release];
[fTrackers release];
2007-09-16 01:02:06 +00:00
if (fInfo)
tr_metaInfoBuilderFree(fInfo);
[fTimer invalidate];
2007-09-16 01:02:06 +00:00
[super dealloc];
}
- (void) setLocation: (id) sender
{
NSSavePanel * panel = [NSSavePanel savePanel];
[panel setPrompt: NSLocalizedString(@"Select", "Create torrent -> location sheet -> button")];
[panel setMessage: NSLocalizedString(@"Select the name and location for the torrent file.",
"Create torrent -> location sheet -> message")];
[panel setAllowedFileTypes: [NSArray arrayWithObjects: @"org.bittorrent.torrent", @"torrent", nil]];
2007-09-16 01:02:06 +00:00
[panel setCanSelectHiddenExtension: YES];
[panel beginSheetForDirectory: [fLocation stringByDeletingLastPathComponent] file: [fLocation lastPathComponent]
modalForWindow: [self window] modalDelegate: self didEndSelector: @selector(locationSheetClosed:returnCode:contextInfo:)
contextInfo: nil];
2007-09-16 01:02:06 +00:00
}
- (void) create: (id) sender
{
//make sure the trackers are no longer being verified
if ([fTrackerTable editedRow] != -1)
[[self window] endEditingFor: fTrackerTable];
const BOOL isPrivate = [fPrivateCheck state] == NSOnState;
if ([fTrackers count] == 0
&& [fDefaults boolForKey: isPrivate ? @"WarningCreatorPrivateBlankAddress" : @"WarningCreatorBlankAddress"])
2007-09-16 01:02:06 +00:00
{
NSAlert * alert = [[NSAlert alloc] init];
[alert setMessageText: NSLocalizedString(@"There are no tracker addresses.", "Create torrent -> blank address -> title")];
NSString * infoString = isPrivate
? NSLocalizedString(@"A transfer marked as private with no tracker addresses will be unable to connect to peers."
" The torrent file will only be useful if you plan to upload the file to a tracker website"
" that will add the addresses for you.", "Create torrent -> blank address -> message")
: NSLocalizedString(@"The transfer will not contact trackers for peers,"
" and will have to rely solely on DHT and PEX to download and seed.",
"Create torrent -> blank address -> message");
[alert setInformativeText: infoString];
[alert addButtonWithTitle: NSLocalizedString(@"Create", "Create torrent -> blank address -> button")];
[alert addButtonWithTitle: NSLocalizedString(@"Cancel", "Create torrent -> blank address -> button")];
2008-12-26 05:57:51 +00:00
[alert setShowsSuppressionButton: YES];
[alert beginSheetModalForWindow: [self window] modalDelegate: self
didEndSelector: @selector(createBlankAddressAlertDidEnd:returnCode:contextInfo:) contextInfo: nil];
2007-09-16 01:02:06 +00:00
}
else
[self createReal];
2007-09-16 01:02:06 +00:00
}
- (void) cancelCreateWindow: (id) sender
{
[[self window] close];
}
- (void) windowWillClose: (NSNotification *) notification
{
[self release];
}
- (void) cancelCreateProgress: (id) sender
{
fInfo->abortFlag = 1;
[fTimer fire];
}
- (NSInteger) numberOfRowsInTableView: (NSTableView *) tableView
{
return [fTrackers count];
}
- (id) tableView: (NSTableView *) tableView objectValueForTableColumn: (NSTableColumn *) tableColumn row: (NSInteger) row
{
return [fTrackers objectAtIndex: row];
}
- (void) addRemoveTracker: (id) sender
{
//don't allow add/remove when currently adding - it leads to weird results
if ([fTrackerTable editedRow] != -1)
return;
if ([[sender cell] tagForSegment: [sender selectedSegment]] == TRACKER_REMOVE_TAG)
{
[fTrackers removeObjectsAtIndexes: [fTrackerTable selectedRowIndexes]];
[fTrackerTable deselectAll: self];
[fTrackerTable reloadData];
}
else
{
[fTrackers addObject: @""];
[fTrackerTable reloadData];
const NSInteger row = [fTrackers count] - 1;
[fTrackerTable selectRowIndexes: [NSIndexSet indexSetWithIndex: row] byExtendingSelection: NO];
[fTrackerTable editColumn: 0 row: row withEvent: nil select: YES];
}
}
- (void) tableView: (NSTableView *) tableView setObjectValue: (id) object forTableColumn: (NSTableColumn *) tableColumn
row: (NSInteger) row
{
NSString * tracker = (NSString *)object;
tracker = [tracker stringByTrimmingCharactersInSet: [NSCharacterSet whitespaceAndNewlineCharacterSet]];
if ([tracker rangeOfString: @"://"].location == NSNotFound)
tracker = [@"http://" stringByAppendingString: tracker];
if (!tr_httpIsValidURL([tracker UTF8String]))
{
NSBeep();
[fTrackers removeObjectAtIndex: row];
}
else
[fTrackers replaceObjectAtIndex: row withObject: tracker];
[fTrackerTable deselectAll: self];
[fTrackerTable reloadData];
}
- (void) tableViewSelectionDidChange: (NSNotification *) notification
{
[fTrackerAddRemoveControl setEnabled: [fTrackerTable numberOfSelectedRows] > 0 forSegment: TRACKER_REMOVE_TAG];
}
2007-09-16 01:02:06 +00:00
@end
@implementation CreatorWindowController (Private)
+ (NSString *) chooseFile
{
NSOpenPanel * panel = [NSOpenPanel openPanel];
[panel setTitle: NSLocalizedString(@"Create Torrent File", "Create torrent -> select file")];
[panel setPrompt: NSLocalizedString(@"Select", "Create torrent -> select file")];
[panel setAllowsMultipleSelection: NO];
[panel setCanChooseFiles: YES];
[panel setCanChooseDirectories: YES];
[panel setCanCreateDirectories: NO];
[panel setMessage: NSLocalizedString(@"Select a file or folder for the torrent file.", "Create torrent -> select file")];
BOOL success = [panel runModal] == NSOKButton;
return success ? [[panel filenames] objectAtIndex: 0] : nil;
}
2008-11-04 01:31:24 +00:00
- (void) locationSheetClosed: (NSSavePanel *) panel returnCode: (NSInteger) code contextInfo: (void *) info
2007-09-16 01:02:06 +00:00
{
if (code == NSOKButton)
{
[fLocation release];
fLocation = [[panel filename] retain];
[fLocationField setStringValue: [fLocation stringByAbbreviatingWithTildeInPath]];
[fLocationField setToolTip: fLocation];
}
}
2008-11-04 01:31:24 +00:00
- (void) createBlankAddressAlertDidEnd: (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: @"WarningCreatorBlankAddress"]; //set regardless of private/public
if ([fPrivateCheck state] == NSOnState)
[[NSUserDefaults standardUserDefaults] setBool: NO forKey: @"WarningCreatorPrivateBlankAddress"];
}
[alert release];
if (returnCode == NSAlertFirstButtonReturn)
[self performSelectorOnMainThread: @selector(createReal) withObject: nil waitUntilDone: NO];
}
- (void) createReal
{
//check if the location currently exists
if (![[NSFileManager defaultManager] fileExistsAtPath: [fLocation stringByDeletingLastPathComponent]])
{
NSAlert * alert = [[[NSAlert alloc] init] autorelease];
[alert addButtonWithTitle: NSLocalizedString(@"OK", "Create torrent -> directory doesn't exist warning -> button")];
[alert setMessageText: NSLocalizedString(@"The chosen torrent file location does not exist.",
"Create torrent -> directory doesn't exist warning -> title")];
[alert setInformativeText: [NSString stringWithFormat:
NSLocalizedString(@"The directory \"%@\" does not currently exist. "
"Create this directory or choose a different one to create the torrent file.",
"Create torrent -> directory doesn't exist warning -> warning"),
[fLocation stringByDeletingLastPathComponent]]];
[alert setAlertStyle: NSWarningAlertStyle];
[alert beginSheetModalForWindow: [self window] modalDelegate: self didEndSelector: nil contextInfo: nil];
return;
}
//check if a file with the same name and location already exists
if ([[NSFileManager defaultManager] fileExistsAtPath: fLocation])
{
NSArray * pathComponents = [fLocation pathComponents];
2008-11-04 01:31:24 +00:00
NSInteger count = [pathComponents count];
NSAlert * alert = [[[NSAlert alloc] init] autorelease];
[alert addButtonWithTitle: NSLocalizedString(@"OK", "Create torrent -> file already exists warning -> button")];
[alert setMessageText: NSLocalizedString(@"A torrent file with this name and directory cannot be created.",
"Create torrent -> file already exists warning -> title")];
[alert setInformativeText: [NSString stringWithFormat:
NSLocalizedString(@"A file with the name \"%@\" already exists in the directory \"%@\". "
"Choose a new name or directory to create the torrent file.",
"Create torrent -> file already exists warning -> warning"),
[pathComponents objectAtIndex: count-1], [pathComponents objectAtIndex: count-2]]];
[alert setAlertStyle: NSWarningAlertStyle];
[alert beginSheetModalForWindow: [self window] modalDelegate: self didEndSelector: nil contextInfo: nil];
return;
}
//parse non-empty tracker strings
tr_tracker_info * trackerInfo = tr_new0(tr_tracker_info, [fTrackers count]);
2008-10-12 22:17:27 +00:00
for (NSUInteger i = 0; i < [fTrackers count]; i++)
{
trackerInfo[i].announce = (char *)[[fTrackers objectAtIndex: i] UTF8String];
trackerInfo[i].tier = i;
}
//store values
[fDefaults setObject: fTrackers forKey: @"CreatorTrackers"];
[fDefaults setBool: [fPrivateCheck state] == NSOnState forKey: @"CreatorPrivate"];
[fDefaults setBool: [fOpenCheck state] == NSOnState forKey: @"CreatorOpen"];
[fDefaults setObject: [fLocation stringByDeletingLastPathComponent] forKey: @"CreatorLocation"];
[[NSNotificationCenter defaultCenter] postNotificationName: @"BeginCreateTorrentFile" object: fLocation userInfo: nil];
tr_makeMetaInfo(fInfo, [fLocation UTF8String], trackerInfo, [fTrackers count], [[fCommentView string] UTF8String],
[fPrivateCheck state] == NSOnState);
tr_free(trackerInfo);
fTimer = [NSTimer scheduledTimerWithTimeInterval: 0.1 target: self selector: @selector(checkProgress)
userInfo: nil repeats: YES];
}
2007-09-16 01:02:06 +00:00
- (void) checkProgress
{
if (fInfo->isDone)
{
[fTimer invalidate];
fTimer = nil;
NSAlert * alert;
switch (fInfo->result)
2007-09-16 01:02:06 +00:00
{
case TR_MAKEMETA_OK:
if ([fDefaults boolForKey: @"CreatorOpen"])
{
NSDictionary * dict = [[NSDictionary alloc] initWithObjectsAndKeys: fLocation, @"File",
[fPath stringByDeletingLastPathComponent], @"Path", nil];
[[NSNotificationCenter defaultCenter] postNotificationName: @"OpenCreatedTorrentFile" object: self userInfo: dict];
}
[[self window] close];
break;
case TR_MAKEMETA_CANCELLED:
[[self window] close];
break;
default:
alert = [[[NSAlert alloc] init] autorelease];
2007-09-16 01:02:06 +00:00
[alert addButtonWithTitle: NSLocalizedString(@"OK", "Create torrent -> failed -> button")];
[alert setMessageText: [NSString stringWithFormat: NSLocalizedString(@"Creation of \"%@\" failed.",
"Create torrent -> failed -> title"), [fLocation lastPathComponent]]];
[alert setAlertStyle: NSWarningAlertStyle];
if (fInfo->result == TR_MAKEMETA_IO_READ)
[alert setInformativeText: [NSString stringWithFormat: NSLocalizedString(@"Could not read \"%s\": %s.",
"Create torrent -> failed -> warning"), fInfo->errfile, strerror(fInfo->my_errno)]];
else if (fInfo->result == TR_MAKEMETA_IO_WRITE)
[alert setInformativeText: [NSString stringWithFormat: NSLocalizedString(@"Could not write \"%s\": %s.",
"Create torrent -> failed -> warning"), fInfo->errfile, strerror(fInfo->my_errno)]];
else //invalid url should have been caught before creating
[alert setInformativeText: [NSString stringWithFormat: @"%@ (%d)",
NSLocalizedString(@"An unknown error has occurred.", "Create torrent -> failed -> warning"), fInfo->result]];
2007-09-16 01:02:06 +00:00
[alert beginSheetModalForWindow: [self window] modalDelegate: self
didEndSelector: @selector(failureSheetClosed:returnCode:contextInfo:) contextInfo: nil];
2007-09-16 01:02:06 +00:00
}
}
else
{
[fProgressIndicator setDoubleValue: (double)fInfo->pieceIndex / fInfo->pieceCount];
if (!fStarted)
{
fStarted = YES;
[fProgressView setHidden: YES];
2007-09-16 01:02:06 +00:00
NSWindow * window = [self window];
[window setFrameAutosaveName: @""];
2007-09-16 01:02:06 +00:00
NSRect windowRect = [window frame];
CGFloat difference = [fProgressView frame].size.height - [[window contentView] frame].size.height;
2007-09-16 01:02:06 +00:00
windowRect.origin.y -= difference;
windowRect.size.height += difference;
//don't allow vertical resizing
CGFloat height = windowRect.size.height;
2007-09-16 01:02:06 +00:00
[window setMinSize: NSMakeSize([window minSize].width, height)];
[window setMaxSize: NSMakeSize([window maxSize].width, height)];
[window setContentView: fProgressView];
[window setFrame: windowRect display: YES animate: YES];
[fProgressView setHidden: NO];
[[window standardWindowButton: NSWindowCloseButton] setEnabled: NO];
2007-09-16 01:02:06 +00:00
}
}
}
2008-11-04 01:31:24 +00:00
- (void) failureSheetClosed: (NSAlert *) alert returnCode: (NSInteger) code contextInfo: (void *) info
2007-09-16 01:02:06 +00:00
{
[[alert window] orderOut: nil];
[[self window] close];
}
@end