2023-11-01 21:11:11 +00:00
|
|
|
// This file Copyright © Transmission authors and contributors.
|
2022-01-20 18:27:56 +00:00
|
|
|
// It may be used under the MIT (SPDX: MIT) license.
|
|
|
|
// License text can be found in the licenses/ folder.
|
2007-09-16 01:02:06 +00:00
|
|
|
|
2022-08-02 19:46:08 +00:00
|
|
|
#include <chrono>
|
|
|
|
#include <cmath>
|
|
|
|
#include <future>
|
|
|
|
#include <optional>
|
2022-08-17 16:08:36 +00:00
|
|
|
#include <utility>
|
2022-08-02 19:46:08 +00:00
|
|
|
|
2018-09-30 10:37:30 +00:00
|
|
|
#include <libtransmission/transmission.h>
|
2022-08-02 19:46:08 +00:00
|
|
|
|
|
|
|
#include <libtransmission/error.h>
|
2022-02-22 16:04:20 +00:00
|
|
|
#include <libtransmission/makemeta.h>
|
2021-11-09 03:30:03 +00:00
|
|
|
#include <libtransmission/utils.h>
|
|
|
|
#include <libtransmission/web-utils.h> // tr_urlIsValidTracker()
|
2018-09-30 10:37:30 +00:00
|
|
|
|
2007-09-16 01:02:06 +00:00
|
|
|
#import "CreatorWindowController.h"
|
2012-05-28 14:28:50 +00:00
|
|
|
#import "Controller.h"
|
2007-09-16 01:02:06 +00:00
|
|
|
#import "NSStringAdditions.h"
|
2010-04-23 16:59:14 +00:00
|
|
|
|
2022-10-19 19:28:21 +00:00
|
|
|
typedef NS_ENUM(NSUInteger, TrackerSegmentTag) {
|
|
|
|
TrackerSegmentTagAdd = 0,
|
|
|
|
TrackerSegmentTagRemove = 1,
|
|
|
|
};
|
2008-06-04 20:55:38 +00:00
|
|
|
|
2023-09-26 02:48:23 +00:00
|
|
|
@interface CreatorWindowController ()<NSWindowRestoration>
|
2007-09-16 01:02:06 +00:00
|
|
|
|
2022-02-22 16:04:20 +00:00
|
|
|
@property(nonatomic) IBOutlet NSImageView* fIconView;
|
|
|
|
@property(nonatomic) IBOutlet NSTextField* fNameField;
|
|
|
|
@property(nonatomic) IBOutlet NSTextField* fStatusField;
|
|
|
|
@property(nonatomic) IBOutlet NSTextField* fPiecesField;
|
|
|
|
@property(nonatomic) IBOutlet NSTextField* fLocationField;
|
|
|
|
@property(nonatomic) IBOutlet NSTableView* fTrackerTable;
|
|
|
|
@property(nonatomic) IBOutlet NSSegmentedControl* fTrackerAddRemoveControl;
|
|
|
|
@property(nonatomic) IBOutlet NSTextView* fCommentView;
|
|
|
|
@property(nonatomic) IBOutlet NSButton* fPrivateCheck;
|
|
|
|
@property(nonatomic) IBOutlet NSButton* fOpenCheck;
|
|
|
|
@property(nonatomic) IBOutlet NSTextField* fSource;
|
2022-03-25 03:26:21 +00:00
|
|
|
@property(nonatomic) IBOutlet NSStepper* fPieceSizeStepper;
|
2022-02-22 16:04:20 +00:00
|
|
|
|
|
|
|
@property(nonatomic) IBOutlet NSView* fProgressView;
|
|
|
|
@property(nonatomic) IBOutlet NSProgressIndicator* fProgressIndicator;
|
|
|
|
|
2022-08-02 19:46:08 +00:00
|
|
|
@property(nonatomic, readonly) std::shared_ptr<tr_metainfo_builder> fBuilder;
|
2022-02-22 16:04:20 +00:00
|
|
|
@property(nonatomic, readonly) NSURL* fPath;
|
2023-11-04 16:39:41 +00:00
|
|
|
@property(nonatomic) std::shared_future<tr_error> fFuture;
|
2022-08-02 19:46:08 +00:00
|
|
|
@property(nonatomic) NSURL* fLocation; // path to new torrent file
|
2022-04-29 22:51:40 +00:00
|
|
|
@property(nonatomic) NSMutableArray<NSString*>* fTrackers;
|
2022-02-22 16:04:20 +00:00
|
|
|
|
|
|
|
@property(nonatomic) NSTimer* fTimer;
|
|
|
|
@property(nonatomic) BOOL fStarted;
|
|
|
|
@property(nonatomic) BOOL fOpenWhenCreated;
|
|
|
|
|
|
|
|
@property(nonatomic, readonly) NSUserDefaults* fDefaults;
|
|
|
|
|
2007-09-16 01:02:06 +00:00
|
|
|
@end
|
|
|
|
|
2024-01-29 04:49:06 +00:00
|
|
|
static NSMutableSet* creatorWindowControllerSet;
|
2021-08-15 09:41:48 +00:00
|
|
|
|
2007-09-16 01:02:06 +00:00
|
|
|
@implementation CreatorWindowController
|
|
|
|
|
2021-08-15 09:41:48 +00:00
|
|
|
+ (CreatorWindowController*)createTorrentFile:(tr_session*)handle
|
2007-09-16 01:02:06 +00:00
|
|
|
{
|
|
|
|
//get file/folder for torrent
|
2021-08-15 09:41:48 +00:00
|
|
|
NSURL* path;
|
2007-09-16 01:02:06 +00:00
|
|
|
if (!(path = [CreatorWindowController chooseFile]))
|
2021-08-15 09:41:48 +00:00
|
|
|
{
|
2012-05-28 14:28:50 +00:00
|
|
|
return nil;
|
2021-08-15 09:41:48 +00:00
|
|
|
}
|
2017-01-24 17:53:16 +00:00
|
|
|
|
2021-08-15 09:41:48 +00:00
|
|
|
CreatorWindowController* creator = [[self alloc] initWithHandle:handle path:path];
|
|
|
|
[creator showWindow:nil];
|
2012-05-28 14:28:50 +00:00
|
|
|
return creator;
|
2007-09-16 01:02:06 +00:00
|
|
|
}
|
|
|
|
|
2021-08-15 09:41:48 +00:00
|
|
|
+ (CreatorWindowController*)createTorrentFile:(tr_session*)handle forFile:(NSURL*)file
|
2007-09-16 01:02:06 +00:00
|
|
|
{
|
2021-08-15 09:41:48 +00:00
|
|
|
CreatorWindowController* creator = [[self alloc] initWithHandle:handle path:file];
|
|
|
|
[creator showWindow:nil];
|
2012-05-28 14:28:50 +00:00
|
|
|
return creator;
|
2007-09-16 01:02:06 +00:00
|
|
|
}
|
|
|
|
|
2021-08-15 09:41:48 +00:00
|
|
|
- (instancetype)initWithHandle:(tr_session*)handle path:(NSURL*)path
|
2007-09-16 01:02:06 +00:00
|
|
|
{
|
2021-08-15 09:41:48 +00:00
|
|
|
if ((self = [super initWithWindowNibName:@"Creator"]))
|
2009-09-12 01:07:21 +00:00
|
|
|
{
|
2021-08-15 09:41:48 +00:00
|
|
|
if (!creatorWindowControllerSet)
|
|
|
|
{
|
2017-07-29 16:14:22 +00:00
|
|
|
creatorWindowControllerSet = [NSMutableSet set];
|
|
|
|
}
|
2021-08-15 09:41:48 +00:00
|
|
|
|
2022-02-22 16:04:20 +00:00
|
|
|
_fStarted = NO;
|
2017-01-24 17:53:16 +00:00
|
|
|
|
2022-02-22 16:04:20 +00:00
|
|
|
_fPath = path;
|
2022-08-02 19:46:08 +00:00
|
|
|
_fBuilder = std::make_shared<tr_metainfo_builder>(_fPath.path.UTF8String);
|
2017-01-24 17:53:16 +00:00
|
|
|
|
2023-05-06 04:11:05 +00:00
|
|
|
if (_fBuilder->file_count() == 0U)
|
2007-09-16 01:02:06 +00:00
|
|
|
{
|
2021-08-15 09:41:48 +00:00
|
|
|
NSAlert* alert = [[NSAlert alloc] init];
|
|
|
|
[alert addButtonWithTitle:NSLocalizedString(@"OK", "Create torrent -> no files -> button")];
|
|
|
|
alert.messageText = NSLocalizedString(@"This folder contains no files.", "Create torrent -> no files -> title");
|
|
|
|
alert.informativeText = NSLocalizedString(
|
|
|
|
@"There must be at least one file in a folder to create a torrent file.",
|
2021-08-07 07:27:56 +00:00
|
|
|
"Create torrent -> no files -> warning");
|
2021-10-31 15:18:27 +00:00
|
|
|
alert.alertStyle = NSAlertStyleWarning;
|
2017-01-24 17:53:16 +00:00
|
|
|
|
2007-09-16 01:02:06 +00:00
|
|
|
[alert runModal];
|
2017-01-24 17:53:16 +00:00
|
|
|
|
2007-09-16 01:02:06 +00:00
|
|
|
return nil;
|
|
|
|
}
|
2023-05-06 04:11:05 +00:00
|
|
|
if (_fBuilder->total_size() == 0U)
|
2007-09-16 01:02:06 +00:00
|
|
|
{
|
2021-08-15 09:41:48 +00:00
|
|
|
NSAlert* alert = [[NSAlert alloc] init];
|
|
|
|
[alert addButtonWithTitle:NSLocalizedString(@"OK", "Create torrent -> zero size -> button")];
|
|
|
|
alert.messageText = NSLocalizedString(@"The total file size is zero bytes.", "Create torrent -> zero size -> title");
|
|
|
|
alert.informativeText = NSLocalizedString(@"A torrent file cannot be created for files with no size.", "Create torrent -> zero size -> warning");
|
2021-10-31 15:18:27 +00:00
|
|
|
alert.alertStyle = NSAlertStyleWarning;
|
2017-01-24 17:53:16 +00:00
|
|
|
|
2007-09-16 01:02:06 +00:00
|
|
|
[alert runModal];
|
2017-01-24 17:53:16 +00:00
|
|
|
|
2007-09-16 01:02:06 +00:00
|
|
|
return nil;
|
|
|
|
}
|
2017-01-24 17:53:16 +00:00
|
|
|
|
2022-02-22 16:04:20 +00:00
|
|
|
_fDefaults = NSUserDefaults.standardUserDefaults;
|
2017-01-24 17:53:16 +00:00
|
|
|
|
2008-06-04 20:55:38 +00:00
|
|
|
//get list of trackers
|
2022-02-22 16:04:20 +00:00
|
|
|
if (!(_fTrackers = [[_fDefaults arrayForKey:@"CreatorTrackers"] mutableCopy]))
|
2008-06-04 20:55:38 +00:00
|
|
|
{
|
2022-02-22 16:04:20 +00:00
|
|
|
_fTrackers = [[NSMutableArray alloc] init];
|
2017-01-24 17:53:16 +00:00
|
|
|
|
2008-08-14 21:47:23 +00:00
|
|
|
//check for single tracker from versions before 1.3
|
2021-08-15 09:41:48 +00:00
|
|
|
NSString* tracker;
|
2022-02-22 16:04:20 +00:00
|
|
|
if ((tracker = [_fDefaults stringForKey:@"CreatorTracker"]))
|
2008-06-04 20:55:38 +00:00
|
|
|
{
|
2022-02-22 16:04:20 +00:00
|
|
|
[_fDefaults removeObjectForKey:@"CreatorTracker"];
|
2021-08-15 09:41:48 +00:00
|
|
|
if (![tracker isEqualToString:@""])
|
2008-08-17 22:50:02 +00:00
|
|
|
{
|
2022-02-22 16:04:20 +00:00
|
|
|
[_fTrackers addObject:tracker];
|
|
|
|
[_fDefaults setObject:_fTrackers forKey:@"CreatorTrackers"];
|
2008-08-17 22:50:02 +00:00
|
|
|
}
|
2008-06-04 20:55:38 +00:00
|
|
|
}
|
2008-06-24 03:59:34 +00:00
|
|
|
}
|
2017-01-24 17:53:16 +00:00
|
|
|
|
2008-08-14 21:47:23 +00:00
|
|
|
//remove potentially invalid addresses
|
2022-02-22 16:04:20 +00:00
|
|
|
for (NSInteger i = _fTrackers.count - 1; i >= 0; i--)
|
2008-08-14 21:47:23 +00:00
|
|
|
{
|
2022-06-29 04:20:42 +00:00
|
|
|
if (!tr_urlIsValidTracker(_fTrackers[i].UTF8String))
|
2021-08-15 09:41:48 +00:00
|
|
|
{
|
2022-02-22 16:04:20 +00:00
|
|
|
[_fTrackers removeObjectAtIndex:i];
|
2021-08-15 09:41:48 +00:00
|
|
|
}
|
2008-08-14 21:47:23 +00:00
|
|
|
}
|
2021-08-15 09:41:48 +00:00
|
|
|
|
2017-07-29 16:14:22 +00:00
|
|
|
[creatorWindowControllerSet addObject:self];
|
2007-09-16 01:02:06 +00:00
|
|
|
}
|
|
|
|
return self;
|
|
|
|
}
|
|
|
|
|
2021-08-15 09:41:48 +00:00
|
|
|
- (void)awakeFromNib
|
2007-09-16 01:02:06 +00:00
|
|
|
{
|
2023-11-05 20:35:22 +00:00
|
|
|
[super awakeFromNib];
|
2021-08-07 07:27:56 +00:00
|
|
|
self.window.restorationClass = [self class];
|
2017-01-24 17:53:16 +00:00
|
|
|
|
2022-02-22 16:04:20 +00:00
|
|
|
NSString* name = self.fPath.lastPathComponent;
|
2017-01-24 17:53:16 +00:00
|
|
|
|
2021-08-07 07:27:56 +00:00
|
|
|
self.window.title = name;
|
2017-01-24 17:53:16 +00:00
|
|
|
|
2022-06-16 17:55:33 +00:00
|
|
|
//disable fullscreen support
|
2022-06-29 04:20:42 +00:00
|
|
|
self.window.collectionBehavior = NSWindowCollectionBehaviorFullScreenNone;
|
2022-06-08 19:15:51 +00:00
|
|
|
|
2022-02-22 16:04:20 +00:00
|
|
|
self.fNameField.stringValue = name;
|
|
|
|
self.fNameField.toolTip = self.fPath.path;
|
2017-01-24 17:53:16 +00:00
|
|
|
|
2023-06-30 14:49:58 +00:00
|
|
|
auto const is_folder = self.fBuilder->file_count() > 1 || tr_strv_contains(self.fBuilder->path(0), '/');
|
2017-01-24 17:53:16 +00:00
|
|
|
|
2021-08-15 09:41:48 +00:00
|
|
|
NSImage* icon = [NSWorkspace.sharedWorkspace
|
2022-08-02 19:46:08 +00:00
|
|
|
iconForFileType:is_folder ? NSFileTypeForHFSTypeCode(kGenericFolderIcon) : self.fPath.pathExtension];
|
2022-02-22 16:04:20 +00:00
|
|
|
icon.size = self.fIconView.frame.size;
|
|
|
|
self.fIconView.image = icon;
|
2017-01-24 17:53:16 +00:00
|
|
|
|
2023-05-06 04:11:05 +00:00
|
|
|
NSString* status_string = [NSString stringForFileSize:self.fBuilder->total_size()];
|
2022-08-02 19:46:08 +00:00
|
|
|
if (is_folder)
|
2007-09-16 01:02:06 +00:00
|
|
|
{
|
2023-05-06 04:11:05 +00:00
|
|
|
NSUInteger const count = self.fBuilder->file_count();
|
2022-08-02 19:46:08 +00:00
|
|
|
NSString* const fileString = count != 1 ?
|
2022-11-14 18:30:03 +00:00
|
|
|
[NSString localizedStringWithFormat:NSLocalizedString(@"%lu files", "Create torrent -> info"), count] :
|
2022-08-02 19:46:08 +00:00
|
|
|
NSLocalizedString(@"1 file", "Create torrent -> info");
|
|
|
|
status_string = [NSString stringWithFormat:@"%@, %@", fileString, status_string];
|
2007-09-16 01:02:06 +00:00
|
|
|
}
|
2022-08-02 19:46:08 +00:00
|
|
|
self.fStatusField.stringValue = status_string;
|
2017-01-24 17:53:16 +00:00
|
|
|
|
2022-03-25 03:26:21 +00:00
|
|
|
[self updatePiecesField];
|
2023-05-06 04:11:05 +00:00
|
|
|
self.fPieceSizeStepper.intValue = static_cast<int>(log2(self.fBuilder->piece_size()));
|
2017-01-24 17:53:16 +00:00
|
|
|
|
2022-03-30 21:52:23 +00:00
|
|
|
self.fLocation = [[self.fDefaults URLForKey:@"CreatorLocationURL"]
|
|
|
|
URLByAppendingPathComponent:[name stringByAppendingPathExtension:@"torrent"]];
|
2022-02-22 16:04:20 +00:00
|
|
|
if (!self.fLocation)
|
2012-05-28 14:28:50 +00:00
|
|
|
{
|
2022-10-13 14:35:10 +00:00
|
|
|
//Compatibility with Transmission 2.5 and earlier,
|
|
|
|
//when it was "CreatorLocation" and not "CreatorLocationURL"
|
2022-02-22 16:04:20 +00:00
|
|
|
NSString* location = [self.fDefaults stringForKey:@"CreatorLocation"];
|
2022-03-30 21:52:23 +00:00
|
|
|
self.fLocation = [[NSURL alloc]
|
|
|
|
initFileURLWithPath:[location.stringByExpandingTildeInPath
|
|
|
|
stringByAppendingPathComponent:[name stringByAppendingPathExtension:@"torrent"]]];
|
2012-05-28 14:28:50 +00:00
|
|
|
}
|
2012-05-28 19:43:53 +00:00
|
|
|
[self updateLocationField];
|
2017-01-24 17:53:16 +00:00
|
|
|
|
2007-09-16 01:02:06 +00:00
|
|
|
//set previously saved values
|
2022-02-22 16:04:20 +00:00
|
|
|
if ([self.fDefaults objectForKey:@"CreatorPrivate"])
|
2021-08-15 09:41:48 +00:00
|
|
|
{
|
2022-02-22 16:04:20 +00:00
|
|
|
self.fPrivateCheck.state = [self.fDefaults boolForKey:@"CreatorPrivate"] ? NSControlStateValueOn : NSControlStateValueOff;
|
2021-08-15 09:41:48 +00:00
|
|
|
}
|
2017-01-24 17:53:16 +00:00
|
|
|
|
2022-02-22 16:04:20 +00:00
|
|
|
if ([self.fDefaults objectForKey:@"CreatorSource"])
|
2021-10-18 23:05:39 +00:00
|
|
|
{
|
2022-02-22 16:04:20 +00:00
|
|
|
self.fSource.stringValue = [self.fDefaults stringForKey:@"CreatorSource"];
|
2021-10-18 23:05:39 +00:00
|
|
|
}
|
|
|
|
|
2022-02-22 16:04:20 +00:00
|
|
|
self.fOpenCheck.state = [self.fDefaults boolForKey:@"CreatorOpen"] ? NSControlStateValueOn : NSControlStateValueOff;
|
2022-06-05 16:14:33 +00:00
|
|
|
|
|
|
|
//set tracker table column width to table width
|
|
|
|
[self.fTrackerTable sizeToFit];
|
2007-09-16 01:02:06 +00:00
|
|
|
}
|
|
|
|
|
2021-08-15 09:41:48 +00:00
|
|
|
- (void)dealloc
|
2007-09-16 01:02:06 +00:00
|
|
|
{
|
2022-08-02 19:46:08 +00:00
|
|
|
_fBuilder.reset();
|
2017-01-24 17:53:16 +00:00
|
|
|
|
2022-02-22 16:04:20 +00:00
|
|
|
[_fTimer invalidate];
|
2007-09-16 01:02:06 +00:00
|
|
|
}
|
|
|
|
|
2021-08-15 09:41:48 +00:00
|
|
|
+ (void)restoreWindowWithIdentifier:(NSString*)identifier
|
|
|
|
state:(NSCoder*)state
|
|
|
|
completionHandler:(void (^)(NSWindow*, NSError*))completionHandler
|
2012-05-28 14:28:50 +00:00
|
|
|
{
|
2021-08-15 09:41:48 +00:00
|
|
|
NSURL* path = [state decodeObjectForKey:@"TRCreatorPath"];
|
|
|
|
if (!path || ![path checkResourceIsReachableAndReturnError:nil])
|
2012-05-28 14:28:50 +00:00
|
|
|
{
|
2021-08-15 09:41:48 +00:00
|
|
|
completionHandler(nil, [NSError errorWithDomain:NSURLErrorDomain code:NSURLErrorCannotOpenFile userInfo:nil]);
|
2012-05-28 14:28:50 +00:00
|
|
|
return;
|
|
|
|
}
|
2017-01-24 17:53:16 +00:00
|
|
|
|
2021-08-15 09:41:48 +00:00
|
|
|
NSWindow* window = [self createTorrentFile:((Controller*)NSApp.delegate).sessionHandle forFile:path].window;
|
2012-05-28 14:28:50 +00:00
|
|
|
completionHandler(window, nil);
|
|
|
|
}
|
|
|
|
|
2021-08-15 09:41:48 +00:00
|
|
|
- (void)window:(NSWindow*)window willEncodeRestorableState:(NSCoder*)state
|
2012-05-28 14:28:50 +00:00
|
|
|
{
|
2022-02-22 16:04:20 +00:00
|
|
|
[state encodeObject:self.fPath forKey:@"TRCreatorPath"];
|
|
|
|
[state encodeObject:self.fLocation forKey:@"TRCreatorLocation"];
|
|
|
|
[state encodeObject:self.fTrackers forKey:@"TRCreatorTrackers"];
|
|
|
|
[state encodeInteger:self.fOpenCheck.state forKey:@"TRCreatorOpenCheck"];
|
|
|
|
[state encodeInteger:self.fPrivateCheck.state forKey:@"TRCreatorPrivateCheck"];
|
|
|
|
[state encodeObject:self.fSource.stringValue forKey:@"TRCreatorSource"];
|
|
|
|
[state encodeObject:self.fCommentView.string forKey:@"TRCreatorPrivateComment"];
|
2012-05-28 14:28:50 +00:00
|
|
|
}
|
|
|
|
|
2021-08-15 09:41:48 +00:00
|
|
|
- (void)window:(NSWindow*)window didDecodeRestorableState:(NSCoder*)coder
|
2012-05-28 14:28:50 +00:00
|
|
|
{
|
2022-02-22 16:04:20 +00:00
|
|
|
self.fLocation = [coder decodeObjectForKey:@"TRCreatorLocation"];
|
2012-05-28 19:43:53 +00:00
|
|
|
[self updateLocationField];
|
2017-01-24 17:53:16 +00:00
|
|
|
|
2022-02-22 16:04:20 +00:00
|
|
|
self.fTrackers = [coder decodeObjectForKey:@"TRCreatorTrackers"];
|
|
|
|
[self.fTrackerTable reloadData];
|
2017-01-24 17:53:16 +00:00
|
|
|
|
2022-02-22 16:04:20 +00:00
|
|
|
self.fOpenCheck.state = [coder decodeIntegerForKey:@"TRCreatorOpenCheck"];
|
|
|
|
self.fPrivateCheck.state = [coder decodeIntegerForKey:@"TRCreatorPrivateCheck"];
|
|
|
|
self.fSource.stringValue = [coder decodeObjectForKey:@"TRCreatorSource"];
|
|
|
|
self.fCommentView.string = [coder decodeObjectForKey:@"TRCreatorPrivateComment"];
|
2012-05-28 14:28:50 +00:00
|
|
|
}
|
|
|
|
|
2021-08-15 09:41:48 +00:00
|
|
|
- (IBAction)setLocation:(id)sender
|
2007-09-16 01:02:06 +00:00
|
|
|
{
|
2021-08-15 09:41:48 +00:00
|
|
|
NSSavePanel* panel = [NSSavePanel savePanel];
|
2007-09-16 01:02:06 +00:00
|
|
|
|
2021-08-07 07:27:56 +00:00
|
|
|
panel.prompt = NSLocalizedString(@"Select", "Create torrent -> location sheet -> button");
|
2021-08-15 09:41:48 +00:00
|
|
|
panel.message = NSLocalizedString(@"Select the name and location for the torrent file.", "Create torrent -> location sheet -> message");
|
2017-01-24 17:53:16 +00:00
|
|
|
|
2021-08-15 09:41:48 +00:00
|
|
|
panel.allowedFileTypes = @[ @"org.bittorrent.torrent", @"torrent" ];
|
2021-08-07 07:27:56 +00:00
|
|
|
panel.canSelectHiddenExtension = YES;
|
2017-01-24 17:53:16 +00:00
|
|
|
|
2022-02-22 16:04:20 +00:00
|
|
|
panel.directoryURL = self.fLocation.URLByDeletingLastPathComponent;
|
|
|
|
panel.nameFieldStringValue = self.fLocation.lastPathComponent;
|
2017-01-24 17:53:16 +00:00
|
|
|
|
2021-08-15 09:41:48 +00:00
|
|
|
[panel beginSheetModalForWindow:self.window completionHandler:^(NSInteger result) {
|
2021-10-31 15:18:27 +00:00
|
|
|
if (result == NSModalResponseOK)
|
2011-12-11 22:31:01 +00:00
|
|
|
{
|
2022-02-22 16:04:20 +00:00
|
|
|
self.fLocation = panel.URL;
|
2012-05-28 19:43:53 +00:00
|
|
|
[self updateLocationField];
|
2011-12-11 22:31:01 +00:00
|
|
|
}
|
|
|
|
}];
|
2007-09-16 01:02:06 +00:00
|
|
|
}
|
|
|
|
|
2021-08-15 09:41:48 +00:00
|
|
|
- (IBAction)create:(id)sender
|
2007-09-16 01:02:06 +00:00
|
|
|
{
|
2009-09-12 00:23:13 +00:00
|
|
|
//make sure the trackers are no longer being verified
|
2022-02-22 16:04:20 +00:00
|
|
|
if (self.fTrackerTable.editedRow != -1)
|
2021-08-15 09:41:48 +00:00
|
|
|
{
|
2022-02-22 16:04:20 +00:00
|
|
|
[self.window endEditingFor:self.fTrackerTable];
|
2021-08-15 09:41:48 +00:00
|
|
|
}
|
2017-01-24 17:53:16 +00:00
|
|
|
|
2022-02-22 16:04:20 +00:00
|
|
|
BOOL const isPrivate = self.fPrivateCheck.state == NSControlStateValueOn;
|
|
|
|
if (self.fTrackers.count == 0 && [self.fDefaults boolForKey:isPrivate ? @"WarningCreatorPrivateBlankAddress" : @"WarningCreatorBlankAddress"])
|
2007-09-16 01:02:06 +00:00
|
|
|
{
|
2021-08-15 09:41:48 +00:00
|
|
|
NSAlert* alert = [[NSAlert alloc] init];
|
2021-08-07 07:27:56 +00:00
|
|
|
alert.messageText = NSLocalizedString(@"There are no tracker addresses.", "Create torrent -> blank address -> title");
|
2017-01-24 17:53:16 +00:00
|
|
|
|
2021-08-15 09:41:48 +00:00
|
|
|
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"
|
|
|
|
" non-tracker peer discovery methods such as PEX and DHT to download and seed.",
|
|
|
|
"Create torrent -> blank address -> message");
|
2017-01-24 17:53:16 +00:00
|
|
|
|
2021-08-07 07:27:56 +00:00
|
|
|
alert.informativeText = infoString;
|
2021-08-15 09:41:48 +00:00
|
|
|
[alert addButtonWithTitle:NSLocalizedString(@"Create", "Create torrent -> blank address -> button")];
|
|
|
|
[alert addButtonWithTitle:NSLocalizedString(@"Cancel", "Create torrent -> blank address -> button")];
|
2021-08-07 07:27:56 +00:00
|
|
|
alert.showsSuppressionButton = YES;
|
2008-05-29 00:18:10 +00:00
|
|
|
|
2021-08-07 07:27:56 +00:00
|
|
|
[alert beginSheetModalForWindow:self.window completionHandler:^(NSModalResponse returnCode) {
|
2021-10-31 15:18:27 +00:00
|
|
|
if (alert.suppressionButton.state == NSControlStateValueOn)
|
2020-11-22 13:02:29 +00:00
|
|
|
{
|
2021-08-15 09:41:48 +00:00
|
|
|
[NSUserDefaults.standardUserDefaults setBool:NO forKey:@"WarningCreatorBlankAddress"]; //set regardless of private/public
|
2022-02-22 16:04:20 +00:00
|
|
|
if (self.fPrivateCheck.state == NSControlStateValueOn)
|
2021-08-15 09:41:48 +00:00
|
|
|
{
|
|
|
|
[NSUserDefaults.standardUserDefaults setBool:NO forKey:@"WarningCreatorPrivateBlankAddress"];
|
|
|
|
}
|
2020-11-22 13:02:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (returnCode == NSAlertFirstButtonReturn)
|
2021-08-15 09:41:48 +00:00
|
|
|
{
|
|
|
|
[self performSelectorOnMainThread:@selector(createReal) withObject:nil waitUntilDone:NO];
|
|
|
|
}
|
2020-11-22 13:02:29 +00:00
|
|
|
}];
|
2007-09-16 01:02:06 +00:00
|
|
|
}
|
2008-05-29 00:18:10 +00:00
|
|
|
else
|
2021-08-15 09:41:48 +00:00
|
|
|
{
|
2008-05-28 23:57:25 +00:00
|
|
|
[self createReal];
|
2021-08-15 09:41:48 +00:00
|
|
|
}
|
2007-09-16 01:02:06 +00:00
|
|
|
}
|
|
|
|
|
2021-08-15 09:41:48 +00:00
|
|
|
- (IBAction)cancelCreateWindow:(id)sender
|
2007-09-16 01:02:06 +00:00
|
|
|
{
|
2021-08-07 07:27:56 +00:00
|
|
|
[self.window close];
|
2007-09-16 01:02:06 +00:00
|
|
|
}
|
|
|
|
|
2021-08-15 09:41:48 +00:00
|
|
|
- (void)windowWillClose:(NSNotification*)notification
|
2012-03-13 02:52:11 +00:00
|
|
|
{
|
2017-07-29 16:14:22 +00:00
|
|
|
[creatorWindowControllerSet removeObject:self];
|
2012-03-13 02:52:11 +00:00
|
|
|
}
|
|
|
|
|
2021-08-15 09:41:48 +00:00
|
|
|
- (IBAction)cancelCreateProgress:(id)sender
|
2007-09-16 01:02:06 +00:00
|
|
|
{
|
2023-05-06 04:11:05 +00:00
|
|
|
self.fBuilder->cancel_checksums();
|
2022-02-22 16:04:20 +00:00
|
|
|
[self.fTimer fire];
|
2007-09-16 01:02:06 +00:00
|
|
|
}
|
|
|
|
|
2022-03-25 03:26:21 +00:00
|
|
|
- (IBAction)incrementOrDecrementPieceSize:(id)sender
|
|
|
|
{
|
2022-11-13 19:53:25 +00:00
|
|
|
uint32_t const piece_size = 1U << [(NSStepper*)sender intValue];
|
2022-08-02 19:46:08 +00:00
|
|
|
|
2023-05-06 04:11:05 +00:00
|
|
|
if (self.fBuilder->set_piece_size(piece_size))
|
2022-03-30 21:52:23 +00:00
|
|
|
{
|
2022-03-25 03:26:21 +00:00
|
|
|
[self updatePiecesField];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-08-15 09:41:48 +00:00
|
|
|
- (NSInteger)numberOfRowsInTableView:(NSTableView*)tableView
|
2008-06-04 20:55:38 +00:00
|
|
|
{
|
2022-02-22 16:04:20 +00:00
|
|
|
return self.fTrackers.count;
|
2008-06-04 20:55:38 +00:00
|
|
|
}
|
|
|
|
|
2021-08-15 09:41:48 +00:00
|
|
|
- (id)tableView:(NSTableView*)tableView objectValueForTableColumn:(NSTableColumn*)tableColumn row:(NSInteger)row
|
2008-06-04 20:55:38 +00:00
|
|
|
{
|
2022-02-22 16:04:20 +00:00
|
|
|
return self.fTrackers[row];
|
2008-06-04 20:55:38 +00:00
|
|
|
}
|
|
|
|
|
2021-08-15 09:41:48 +00:00
|
|
|
- (IBAction)addRemoveTracker:(id)sender
|
2008-06-04 20:55:38 +00:00
|
|
|
{
|
|
|
|
//don't allow add/remove when currently adding - it leads to weird results
|
2022-02-22 16:04:20 +00:00
|
|
|
if (self.fTrackerTable.editedRow != -1)
|
2021-08-15 09:41:48 +00:00
|
|
|
{
|
2008-06-04 20:55:38 +00:00
|
|
|
return;
|
2021-08-15 09:41:48 +00:00
|
|
|
}
|
2017-01-24 17:53:16 +00:00
|
|
|
|
2022-10-19 19:28:21 +00:00
|
|
|
if ([[sender cell] tagForSegment:[sender selectedSegment]] == TrackerSegmentTagRemove)
|
2008-06-04 20:55:38 +00:00
|
|
|
{
|
2022-02-22 16:04:20 +00:00
|
|
|
[self.fTrackers removeObjectsAtIndexes:self.fTrackerTable.selectedRowIndexes];
|
2017-01-24 17:53:16 +00:00
|
|
|
|
2022-02-22 16:04:20 +00:00
|
|
|
[self.fTrackerTable deselectAll:self];
|
|
|
|
[self.fTrackerTable reloadData];
|
2008-06-04 20:55:38 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2022-02-22 16:04:20 +00:00
|
|
|
[self.fTrackers addObject:@""];
|
|
|
|
[self.fTrackerTable reloadData];
|
2017-01-24 17:53:16 +00:00
|
|
|
|
2022-02-22 16:04:20 +00:00
|
|
|
NSInteger const row = self.fTrackers.count - 1;
|
|
|
|
[self.fTrackerTable selectRowIndexes:[NSIndexSet indexSetWithIndex:row] byExtendingSelection:NO];
|
|
|
|
[self.fTrackerTable editColumn:0 row:row withEvent:nil select:YES];
|
2008-06-04 20:55:38 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-08-15 09:41:48 +00:00
|
|
|
- (void)tableView:(NSTableView*)tableView
|
|
|
|
setObjectValue:(id)object
|
|
|
|
forTableColumn:(NSTableColumn*)tableColumn
|
|
|
|
row:(NSInteger)row
|
2008-06-04 20:55:38 +00:00
|
|
|
{
|
2021-08-15 09:41:48 +00:00
|
|
|
NSString* tracker = (NSString*)object;
|
2017-01-24 17:53:16 +00:00
|
|
|
|
2021-08-15 09:41:48 +00:00
|
|
|
tracker = [tracker stringByTrimmingCharactersInSet:NSCharacterSet.whitespaceAndNewlineCharacterSet];
|
2017-01-24 17:53:16 +00:00
|
|
|
|
2021-08-15 09:41:48 +00:00
|
|
|
if ([tracker rangeOfString:@"://"].location == NSNotFound)
|
|
|
|
{
|
|
|
|
tracker = [@"http://" stringByAppendingString:tracker];
|
|
|
|
}
|
2017-01-24 17:53:16 +00:00
|
|
|
|
2021-08-07 07:27:56 +00:00
|
|
|
if (!tr_urlIsValidTracker(tracker.UTF8String))
|
2008-06-04 20:55:38 +00:00
|
|
|
{
|
|
|
|
NSBeep();
|
2022-02-22 16:04:20 +00:00
|
|
|
[self.fTrackers removeObjectAtIndex:row];
|
2008-06-04 20:55:38 +00:00
|
|
|
}
|
|
|
|
else
|
2021-08-15 09:41:48 +00:00
|
|
|
{
|
2022-02-22 16:04:20 +00:00
|
|
|
self.fTrackers[row] = tracker;
|
2021-08-15 09:41:48 +00:00
|
|
|
}
|
2017-01-24 17:53:16 +00:00
|
|
|
|
2022-02-22 16:04:20 +00:00
|
|
|
[self.fTrackerTable deselectAll:self];
|
|
|
|
[self.fTrackerTable reloadData];
|
2008-06-04 20:55:38 +00:00
|
|
|
}
|
|
|
|
|
2021-08-15 09:41:48 +00:00
|
|
|
- (void)tableViewSelectionDidChange:(NSNotification*)notification
|
2008-06-04 20:55:38 +00:00
|
|
|
{
|
2022-10-19 19:28:21 +00:00
|
|
|
[self.fTrackerAddRemoveControl setEnabled:self.fTrackerTable.numberOfSelectedRows > 0 forSegment:TrackerSegmentTagRemove];
|
2008-06-04 20:55:38 +00:00
|
|
|
}
|
|
|
|
|
2021-08-15 09:41:48 +00:00
|
|
|
- (void)copy:(id)sender
|
2010-04-23 02:13:54 +00:00
|
|
|
{
|
2022-02-22 16:04:20 +00:00
|
|
|
NSArray* addresses = [self.fTrackers objectsAtIndexes:self.fTrackerTable.selectedRowIndexes];
|
2021-08-15 09:41:48 +00:00
|
|
|
NSString* text = [addresses componentsJoinedByString:@"\n"];
|
2017-01-24 17:53:16 +00:00
|
|
|
|
2021-08-15 09:41:48 +00:00
|
|
|
NSPasteboard* pb = NSPasteboard.generalPasteboard;
|
2011-10-06 00:30:40 +00:00
|
|
|
[pb clearContents];
|
2021-08-15 09:41:48 +00:00
|
|
|
[pb writeObjects:@[ text ]];
|
2010-04-23 02:13:54 +00:00
|
|
|
}
|
|
|
|
|
2021-08-15 09:41:48 +00:00
|
|
|
- (BOOL)validateMenuItem:(NSMenuItem*)menuItem
|
2010-04-23 02:05:34 +00:00
|
|
|
{
|
2021-08-15 09:41:48 +00:00
|
|
|
SEL const action = menuItem.action;
|
2017-01-24 17:53:16 +00:00
|
|
|
|
2010-04-23 02:13:54 +00:00
|
|
|
if (action == @selector(copy:))
|
2021-08-15 09:41:48 +00:00
|
|
|
{
|
2022-02-22 16:04:20 +00:00
|
|
|
return self.window.firstResponder == self.fTrackerTable && self.fTrackerTable.numberOfSelectedRows > 0;
|
2021-08-15 09:41:48 +00:00
|
|
|
}
|
2017-01-24 17:53:16 +00:00
|
|
|
|
2010-04-23 02:05:34 +00:00
|
|
|
if (action == @selector(paste:))
|
2021-08-15 09:41:48 +00:00
|
|
|
{
|
2022-02-22 16:04:20 +00:00
|
|
|
return self.window.firstResponder == self.fTrackerTable &&
|
2021-08-15 09:41:48 +00:00
|
|
|
[NSPasteboard.generalPasteboard canReadObjectForClasses:@[ [NSString class] ] options:nil];
|
|
|
|
}
|
2017-01-24 17:53:16 +00:00
|
|
|
|
2010-04-23 02:05:34 +00:00
|
|
|
return YES;
|
|
|
|
}
|
|
|
|
|
2021-08-15 09:41:48 +00:00
|
|
|
- (void)paste:(id)sender
|
2010-04-23 02:05:34 +00:00
|
|
|
{
|
2021-08-15 09:41:48 +00:00
|
|
|
NSMutableArray* tempTrackers = [NSMutableArray array];
|
2017-01-24 17:53:16 +00:00
|
|
|
|
2021-08-15 09:41:48 +00:00
|
|
|
NSArray* items = [NSPasteboard.generalPasteboard readObjectsForClasses:@[ [NSString class] ] options:nil];
|
2011-10-06 00:30:40 +00:00
|
|
|
NSAssert(items != nil, @"no string items to paste; should not be able to call this method");
|
2017-01-24 17:53:16 +00:00
|
|
|
|
2021-08-15 09:41:48 +00:00
|
|
|
for (NSString* pbItem in items)
|
2010-04-23 02:05:34 +00:00
|
|
|
{
|
2021-08-15 09:41:48 +00:00
|
|
|
for (NSString* tracker in [pbItem componentsSeparatedByString:@"\n"])
|
|
|
|
{
|
|
|
|
[tempTrackers addObject:tracker];
|
|
|
|
}
|
2010-04-23 02:05:34 +00:00
|
|
|
}
|
2017-01-24 17:53:16 +00:00
|
|
|
|
2010-04-23 02:05:34 +00:00
|
|
|
BOOL added = NO;
|
2017-01-24 17:53:16 +00:00
|
|
|
|
2021-08-15 09:41:48 +00:00
|
|
|
for (__strong NSString* tracker in tempTrackers)
|
2010-04-23 02:05:34 +00:00
|
|
|
{
|
2021-08-15 09:41:48 +00:00
|
|
|
tracker = [tracker stringByTrimmingCharactersInSet:NSCharacterSet.whitespaceAndNewlineCharacterSet];
|
2017-01-24 17:53:16 +00:00
|
|
|
|
2021-08-15 09:41:48 +00:00
|
|
|
if ([tracker rangeOfString:@"://"].location == NSNotFound)
|
|
|
|
{
|
|
|
|
tracker = [@"http://" stringByAppendingString:tracker];
|
|
|
|
}
|
2017-01-24 17:53:16 +00:00
|
|
|
|
2021-08-07 07:27:56 +00:00
|
|
|
if (tr_urlIsValidTracker(tracker.UTF8String))
|
2010-04-23 02:05:34 +00:00
|
|
|
{
|
2022-02-22 16:04:20 +00:00
|
|
|
[self.fTrackers addObject:tracker];
|
2010-04-23 02:05:34 +00:00
|
|
|
added = YES;
|
|
|
|
}
|
|
|
|
}
|
2017-01-24 17:53:16 +00:00
|
|
|
|
2010-04-23 02:05:34 +00:00
|
|
|
if (added)
|
|
|
|
{
|
2022-02-22 16:04:20 +00:00
|
|
|
[self.fTrackerTable deselectAll:self];
|
|
|
|
[self.fTrackerTable reloadData];
|
2010-04-23 02:05:34 +00:00
|
|
|
}
|
|
|
|
else
|
2021-08-15 09:41:48 +00:00
|
|
|
{
|
2010-04-23 02:05:34 +00:00
|
|
|
NSBeep();
|
2021-08-15 09:41:48 +00:00
|
|
|
}
|
2010-04-23 02:05:34 +00:00
|
|
|
}
|
|
|
|
|
2022-02-22 16:04:20 +00:00
|
|
|
#pragma mark - Private
|
|
|
|
|
2022-03-25 03:26:21 +00:00
|
|
|
- (void)updatePiecesField
|
|
|
|
{
|
2023-05-06 04:11:05 +00:00
|
|
|
auto const piece_size = self.fBuilder->piece_size();
|
|
|
|
auto const piece_count = self.fBuilder->piece_count();
|
2022-08-02 19:46:08 +00:00
|
|
|
|
|
|
|
if (piece_count == 1U)
|
2022-03-25 03:26:21 +00:00
|
|
|
{
|
|
|
|
self.fPiecesField.stringValue = [NSString stringWithFormat:NSLocalizedString(@"1 piece, %@", "Create torrent -> info"),
|
2022-08-02 19:46:08 +00:00
|
|
|
[NSString stringForFileSize:piece_size]];
|
2022-03-25 03:26:21 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2022-05-14 05:31:24 +00:00
|
|
|
self.fPiecesField.stringValue = [NSString stringWithFormat:NSLocalizedString(@"%u pieces, %@ each", "Create torrent -> info"),
|
2022-08-02 19:46:08 +00:00
|
|
|
piece_count,
|
|
|
|
[NSString stringForFileSize:piece_size]];
|
2022-03-25 03:26:21 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-08-15 09:41:48 +00:00
|
|
|
- (void)updateLocationField
|
2012-05-28 19:43:53 +00:00
|
|
|
{
|
2022-02-22 16:04:20 +00:00
|
|
|
NSString* pathString = self.fLocation.path;
|
|
|
|
self.fLocationField.stringValue = pathString.stringByAbbreviatingWithTildeInPath;
|
|
|
|
self.fLocationField.toolTip = pathString;
|
2012-05-28 19:43:53 +00:00
|
|
|
}
|
|
|
|
|
2021-08-15 09:41:48 +00:00
|
|
|
+ (NSURL*)chooseFile
|
2007-09-16 01:02:06 +00:00
|
|
|
{
|
2021-08-15 09:41:48 +00:00
|
|
|
NSOpenPanel* panel = [NSOpenPanel openPanel];
|
2017-01-24 17:53:16 +00:00
|
|
|
|
2021-08-07 07:27:56 +00:00
|
|
|
panel.title = NSLocalizedString(@"Create Torrent File", "Create torrent -> select file");
|
|
|
|
panel.prompt = NSLocalizedString(@"Select", "Create torrent -> select file");
|
|
|
|
panel.allowsMultipleSelection = NO;
|
|
|
|
panel.canChooseFiles = YES;
|
|
|
|
panel.canChooseDirectories = YES;
|
|
|
|
panel.canCreateDirectories = NO;
|
2007-09-16 01:02:06 +00:00
|
|
|
|
2021-08-07 07:27:56 +00:00
|
|
|
panel.message = NSLocalizedString(@"Select a file or folder for the torrent file.", "Create torrent -> select file");
|
2017-01-24 17:53:16 +00:00
|
|
|
|
2021-01-03 04:29:08 +00:00
|
|
|
BOOL success = [panel runModal] == NSModalResponseOK;
|
2021-08-07 07:27:56 +00:00
|
|
|
return success ? panel.URLs[0] : nil;
|
2007-09-16 01:02:06 +00:00
|
|
|
}
|
|
|
|
|
2021-08-15 09:41:48 +00:00
|
|
|
- (void)createReal
|
2008-05-28 23:57:25 +00:00
|
|
|
{
|
2009-09-26 20:13:15 +00:00
|
|
|
//check if the location currently exists
|
2022-02-22 16:04:20 +00:00
|
|
|
if (![self.fLocation.URLByDeletingLastPathComponent checkResourceIsReachableAndReturnError:NULL])
|
2009-09-26 20:13:15 +00:00
|
|
|
{
|
2021-08-15 09:41:48 +00:00
|
|
|
NSAlert* alert = [[NSAlert alloc] init];
|
|
|
|
[alert addButtonWithTitle:NSLocalizedString(@"OK", "Create torrent -> directory doesn't exist warning -> button")];
|
|
|
|
alert.messageText = NSLocalizedString(@"The chosen torrent file location does not exist.", "Create torrent -> directory doesn't exist warning -> title");
|
|
|
|
alert.informativeText = [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"),
|
2022-02-22 16:04:20 +00:00
|
|
|
self.fLocation.URLByDeletingLastPathComponent.path];
|
2021-10-31 15:18:27 +00:00
|
|
|
alert.alertStyle = NSAlertStyleWarning;
|
2017-01-24 17:53:16 +00:00
|
|
|
|
2021-08-07 07:27:56 +00:00
|
|
|
[alert beginSheetModalForWindow:self.window completionHandler:nil];
|
2009-09-26 20:13:15 +00:00
|
|
|
return;
|
|
|
|
}
|
2017-01-24 17:53:16 +00:00
|
|
|
|
2008-06-02 01:55:15 +00:00
|
|
|
//check if a file with the same name and location already exists
|
2022-02-22 16:04:20 +00:00
|
|
|
if ([self.fLocation checkResourceIsReachableAndReturnError:NULL])
|
2008-06-02 01:55:15 +00:00
|
|
|
{
|
2022-02-22 16:04:20 +00:00
|
|
|
NSArray* pathComponents = self.fLocation.pathComponents;
|
2021-08-07 07:27:56 +00:00
|
|
|
NSInteger count = pathComponents.count;
|
2017-01-24 17:53:16 +00:00
|
|
|
|
2021-08-15 09:41:48 +00:00
|
|
|
NSAlert* alert = [[NSAlert alloc] init];
|
|
|
|
[alert addButtonWithTitle:NSLocalizedString(@"OK", "Create torrent -> file already exists warning -> button")];
|
|
|
|
alert.messageText = NSLocalizedString(
|
|
|
|
@"A torrent file with this name and directory cannot be created.",
|
2021-08-07 07:27:56 +00:00
|
|
|
"Create torrent -> file already exists warning -> title");
|
2021-08-15 09:41:48 +00:00
|
|
|
alert.informativeText = [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[count - 1],
|
|
|
|
pathComponents[count - 2]];
|
2021-10-31 15:18:27 +00:00
|
|
|
alert.alertStyle = NSAlertStyleWarning;
|
2017-01-24 17:53:16 +00:00
|
|
|
|
2021-08-07 07:27:56 +00:00
|
|
|
[alert beginSheetModalForWindow:self.window completionHandler:nil];
|
2008-06-02 01:55:15 +00:00
|
|
|
return;
|
|
|
|
}
|
2017-01-24 17:53:16 +00:00
|
|
|
|
2022-08-02 19:46:08 +00:00
|
|
|
// trackers
|
|
|
|
auto trackers = tr_announce_list{};
|
|
|
|
for (NSUInteger i = 0; i < self.fTrackers.count; ++i)
|
2009-10-10 12:58:40 +00:00
|
|
|
{
|
2022-08-02 19:46:08 +00:00
|
|
|
trackers.add((char*)(self.fTrackers[i]).UTF8String, trackers.nextTier());
|
2009-10-10 12:58:40 +00:00
|
|
|
}
|
2023-05-06 04:11:05 +00:00
|
|
|
self.fBuilder->set_announce_list(std::move(trackers));
|
2017-01-24 17:53:16 +00:00
|
|
|
|
2008-05-28 23:57:25 +00:00
|
|
|
//store values
|
2022-02-22 16:04:20 +00:00
|
|
|
[self.fDefaults setObject:self.fTrackers forKey:@"CreatorTrackers"];
|
|
|
|
[self.fDefaults setBool:self.fPrivateCheck.state == NSControlStateValueOn forKey:@"CreatorPrivate"];
|
2022-06-29 04:20:42 +00:00
|
|
|
[self.fDefaults setObject:self.fSource.stringValue forKey:@"CreatorSource"];
|
2022-02-22 16:04:20 +00:00
|
|
|
[self.fDefaults setBool:self.fOpenCheck.state == NSControlStateValueOn forKey:@"CreatorOpen"];
|
2022-03-30 21:52:23 +00:00
|
|
|
self.fOpenWhenCreated = self.fOpenCheck.state ==
|
|
|
|
NSControlStateValueOn; //need this since the check box might not exist, and value in prefs might have changed from another creator window
|
2022-02-22 16:04:20 +00:00
|
|
|
[self.fDefaults setURL:self.fLocation.URLByDeletingLastPathComponent forKey:@"CreatorLocationURL"];
|
2016-01-06 11:05:37 +00:00
|
|
|
|
2021-08-07 07:27:56 +00:00
|
|
|
self.window.restorable = NO;
|
2017-01-24 17:53:16 +00:00
|
|
|
|
2022-02-22 16:04:20 +00:00
|
|
|
[NSNotificationCenter.defaultCenter postNotificationName:@"BeginCreateTorrentFile" object:self.fLocation userInfo:nil];
|
2017-01-24 17:53:16 +00:00
|
|
|
|
2023-05-06 04:11:05 +00:00
|
|
|
self.fBuilder->set_comment(self.fCommentView.string.UTF8String);
|
|
|
|
self.fBuilder->set_private(self.fPrivateCheck.state == NSControlStateValueOn);
|
|
|
|
self.fBuilder->set_source(self.fSource.stringValue.UTF8String);
|
2022-08-02 19:46:08 +00:00
|
|
|
|
2023-05-06 04:11:05 +00:00
|
|
|
self.fFuture = self.fBuilder->make_checksums();
|
2022-03-30 21:52:23 +00:00
|
|
|
self.fTimer = [NSTimer scheduledTimerWithTimeInterval:0.1 target:self selector:@selector(checkProgress) userInfo:nil
|
|
|
|
repeats:YES];
|
2008-05-28 23:57:25 +00:00
|
|
|
}
|
|
|
|
|
2021-08-15 09:41:48 +00:00
|
|
|
- (void)checkProgress
|
2007-09-16 01:02:06 +00:00
|
|
|
{
|
2022-08-02 19:46:08 +00:00
|
|
|
auto const is_done = !self.fFuture.valid() || self.fFuture.wait_for(std::chrono::seconds(0)) == std::future_status::ready;
|
2021-08-15 09:41:48 +00:00
|
|
|
|
2022-08-02 19:46:08 +00:00
|
|
|
if (!is_done)
|
2007-09-16 01:02:06 +00:00
|
|
|
{
|
2023-05-06 04:11:05 +00:00
|
|
|
auto const [current, total] = self.fBuilder->checksum_status();
|
2022-08-02 19:46:08 +00:00
|
|
|
self.fProgressIndicator.doubleValue = static_cast<double>(current) / total;
|
2017-01-24 17:53:16 +00:00
|
|
|
|
2022-02-22 16:04:20 +00:00
|
|
|
if (!self.fStarted)
|
2007-09-16 01:02:06 +00:00
|
|
|
{
|
2022-02-22 16:04:20 +00:00
|
|
|
self.fStarted = YES;
|
2017-01-24 17:53:16 +00:00
|
|
|
|
2022-02-22 16:04:20 +00:00
|
|
|
self.fProgressView.hidden = YES;
|
2017-01-24 17:53:16 +00:00
|
|
|
|
2021-08-15 09:41:48 +00:00
|
|
|
NSWindow* window = self.window;
|
2021-08-07 07:27:56 +00:00
|
|
|
window.frameAutosaveName = @"";
|
2017-01-24 17:53:16 +00:00
|
|
|
|
2021-08-07 07:27:56 +00:00
|
|
|
NSRect windowRect = window.frame;
|
2022-02-22 16:04:20 +00:00
|
|
|
CGFloat difference = self.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;
|
2017-01-24 17:53:16 +00:00
|
|
|
|
2007-09-16 01:02:06 +00:00
|
|
|
//don't allow vertical resizing
|
2008-09-07 17:57:58 +00:00
|
|
|
CGFloat height = windowRect.size.height;
|
2021-08-07 07:27:56 +00:00
|
|
|
window.minSize = NSMakeSize(window.minSize.width, height);
|
|
|
|
window.maxSize = NSMakeSize(window.maxSize.width, height);
|
2017-01-24 17:53:16 +00:00
|
|
|
|
2022-02-22 16:04:20 +00:00
|
|
|
window.contentView = self.fProgressView;
|
2021-08-15 09:41:48 +00:00
|
|
|
[window setFrame:windowRect display:YES animate:YES];
|
2022-02-22 16:04:20 +00:00
|
|
|
self.fProgressView.hidden = NO;
|
2017-01-24 17:53:16 +00:00
|
|
|
|
2021-08-15 09:41:48 +00:00
|
|
|
[window standardWindowButton:NSWindowCloseButton].enabled = NO;
|
2007-09-16 01:02:06 +00:00
|
|
|
}
|
2022-08-02 19:46:08 +00:00
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// stop the timer
|
|
|
|
[self.fTimer invalidate];
|
|
|
|
self.fTimer = nil;
|
|
|
|
|
2023-11-04 16:39:41 +00:00
|
|
|
auto error = self.fFuture.get();
|
|
|
|
if (!error)
|
2022-08-02 19:46:08 +00:00
|
|
|
{
|
|
|
|
self.fBuilder->save(self.fLocation.path.UTF8String, &error);
|
|
|
|
}
|
|
|
|
|
2023-11-04 16:39:41 +00:00
|
|
|
if (error)
|
2022-08-02 19:46:08 +00:00
|
|
|
{
|
|
|
|
auto* const alert = [[NSAlert alloc] init];
|
|
|
|
[alert addButtonWithTitle:NSLocalizedString(@"OK", "Create torrent -> failed -> button")];
|
|
|
|
alert.messageText = [NSString stringWithFormat:NSLocalizedString(@"Creation of \"%@\" failed.", "Create torrent -> failed -> title"),
|
|
|
|
self.fLocation.lastPathComponent];
|
|
|
|
alert.alertStyle = NSAlertStyleWarning;
|
|
|
|
|
2023-11-04 16:39:41 +00:00
|
|
|
alert.informativeText = [NSString
|
|
|
|
stringWithFormat:@"%.*s (%d)", static_cast<int>(std::size(error.message())), std::data(error.message()), error.code()];
|
2022-12-21 20:21:16 +00:00
|
|
|
[alert beginSheetModalForWindow:self.window completionHandler:^(NSModalResponse /*returnCode*/) {
|
2022-08-02 19:46:08 +00:00
|
|
|
[self.window close];
|
|
|
|
}];
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if (self.fOpenWhenCreated)
|
|
|
|
{
|
|
|
|
NSDictionary* dict = @{ @"File" : self.fLocation.path, @"Path" : self.fPath.URLByDeletingLastPathComponent.path };
|
|
|
|
[NSNotificationCenter.defaultCenter postNotificationName:@"OpenCreatedTorrentFile" object:self userInfo:dict];
|
|
|
|
}
|
|
|
|
|
|
|
|
[self.window close];
|
2007-09-16 01:02:06 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@end
|