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.
|
|
|
|
// Created by Mitchell Livingston on 1/20/13.
|
2013-01-22 00:09:48 +00:00
|
|
|
|
|
|
|
#import "FileRenameSheetController.h"
|
|
|
|
#import "FileListNode.h"
|
|
|
|
#import "Torrent.h"
|
|
|
|
|
|
|
|
typedef void (^CompletionBlock)(BOOL);
|
|
|
|
|
2013-01-26 20:11:49 +00:00
|
|
|
@interface FileRenameSheetController ()
|
2013-01-22 00:09:48 +00:00
|
|
|
|
2022-02-22 16:04:20 +00:00
|
|
|
@property(nonatomic, weak) IBOutlet NSTextField* labelField;
|
|
|
|
@property(nonatomic, weak) IBOutlet NSTextField* inputField;
|
|
|
|
@property(nonatomic, weak) IBOutlet NSButton* renameButton;
|
|
|
|
@property(nonatomic, weak) IBOutlet NSButton* cancelButton;
|
|
|
|
|
2021-08-15 09:41:48 +00:00
|
|
|
@property(nonatomic) Torrent* torrent;
|
|
|
|
@property(nonatomic) FileListNode* node;
|
2013-01-22 00:09:48 +00:00
|
|
|
|
2021-08-15 09:41:48 +00:00
|
|
|
@property(nonatomic, copy) NSString* originalName;
|
2013-02-08 23:26:07 +00:00
|
|
|
|
2013-01-22 00:09:48 +00:00
|
|
|
@end
|
|
|
|
|
|
|
|
@implementation FileRenameSheetController
|
2013-01-26 19:59:56 +00:00
|
|
|
|
2021-08-15 09:41:48 +00:00
|
|
|
+ (void)presentSheetForTorrent:(Torrent*)torrent
|
|
|
|
modalForWindow:(NSWindow*)window
|
|
|
|
completionHandler:(void (^)(BOOL didRename))completionHandler
|
2013-01-22 00:09:48 +00:00
|
|
|
{
|
|
|
|
NSParameterAssert(torrent != nil);
|
|
|
|
NSParameterAssert(window != nil);
|
2017-01-24 17:53:16 +00:00
|
|
|
|
2021-08-15 09:41:48 +00:00
|
|
|
FileRenameSheetController* renamer = [[FileRenameSheetController alloc] initWithWindowNibName:@"FileRenameSheetController"];
|
2017-01-24 17:53:16 +00:00
|
|
|
|
2013-01-22 00:09:48 +00:00
|
|
|
renamer.torrent = torrent;
|
2017-01-24 17:53:16 +00:00
|
|
|
|
2022-05-14 13:20:56 +00:00
|
|
|
[self presentSheetForRenamer:renamer modalForWindow:window completionHandler:completionHandler];
|
2013-01-22 00:09:48 +00:00
|
|
|
}
|
|
|
|
|
2021-08-15 09:41:48 +00:00
|
|
|
+ (void)presentSheetForFileListNode:(FileListNode*)node
|
|
|
|
modalForWindow:(NSWindow*)window
|
|
|
|
completionHandler:(void (^)(BOOL didRename))completionHandler
|
2013-01-22 00:09:48 +00:00
|
|
|
{
|
|
|
|
NSParameterAssert(node != nil);
|
|
|
|
NSParameterAssert(window != nil);
|
2017-01-24 17:53:16 +00:00
|
|
|
|
2021-08-15 09:41:48 +00:00
|
|
|
FileRenameSheetController* renamer = [[FileRenameSheetController alloc] initWithWindowNibName:@"FileRenameSheetController"];
|
2017-01-24 17:53:16 +00:00
|
|
|
|
2021-08-07 07:27:56 +00:00
|
|
|
renamer.torrent = node.torrent;
|
2013-01-22 00:09:48 +00:00
|
|
|
renamer.node = node;
|
2017-01-24 17:53:16 +00:00
|
|
|
|
2022-05-14 13:20:56 +00:00
|
|
|
[self presentSheetForRenamer:renamer modalForWindow:window completionHandler:completionHandler];
|
2013-01-22 00:09:48 +00:00
|
|
|
}
|
|
|
|
|
2022-05-14 13:20:56 +00:00
|
|
|
+ (void)presentSheetForRenamer:(FileRenameSheetController*)renamer
|
|
|
|
modalForWindow:(NSWindow*)window
|
|
|
|
completionHandler:(void (^)(BOOL))completionHandler
|
2013-01-22 00:09:48 +00:00
|
|
|
{
|
2022-10-12 10:56:30 +00:00
|
|
|
// we capture renamer strongly to avoid it being deallocated before completionHandler
|
|
|
|
__block FileRenameSheetController* strongRenamer = renamer;
|
2022-05-14 13:20:56 +00:00
|
|
|
[window beginSheet:renamer.window completionHandler:^(NSModalResponse returnCode) {
|
|
|
|
completionHandler(returnCode == NSModalResponseOK);
|
2022-10-12 10:56:30 +00:00
|
|
|
strongRenamer = nil;
|
2022-05-14 13:20:56 +00:00
|
|
|
}];
|
2013-01-22 00:09:48 +00:00
|
|
|
}
|
|
|
|
|
2021-08-15 09:41:48 +00:00
|
|
|
- (void)windowDidLoad
|
2013-01-22 00:09:48 +00:00
|
|
|
{
|
|
|
|
[super windowDidLoad];
|
2017-01-24 17:53:16 +00:00
|
|
|
|
2021-08-07 07:27:56 +00:00
|
|
|
self.originalName = self.node.name ?: self.torrent.name;
|
2021-08-15 09:41:48 +00:00
|
|
|
NSString* label = [NSString stringWithFormat:NSLocalizedString(@"Rename the file \"%@\":", "rename sheet label"), self.originalName];
|
2021-08-07 07:27:56 +00:00
|
|
|
self.labelField.stringValue = label;
|
2017-01-24 17:53:16 +00:00
|
|
|
|
2021-08-07 07:27:56 +00:00
|
|
|
self.inputField.stringValue = self.originalName;
|
|
|
|
self.renameButton.enabled = NO;
|
2017-01-24 17:53:16 +00:00
|
|
|
|
2013-02-09 19:13:26 +00:00
|
|
|
//resize the buttons so that they're long enough and the same width
|
2021-08-15 09:41:48 +00:00
|
|
|
NSRect const oldRenameFrame = self.renameButton.frame;
|
|
|
|
NSRect const oldCancelFrame = self.cancelButton.frame;
|
2017-01-24 17:53:16 +00:00
|
|
|
|
2013-02-09 19:13:26 +00:00
|
|
|
//get the extra width of the rename button from the English xib - the width from sizeToFit is too squished
|
|
|
|
[self.renameButton sizeToFit];
|
2021-08-15 09:41:48 +00:00
|
|
|
CGFloat const extra = NSWidth(oldRenameFrame) - NSWidth(self.renameButton.frame);
|
2017-01-24 17:53:16 +00:00
|
|
|
|
2021-08-07 07:27:56 +00:00
|
|
|
self.renameButton.title = NSLocalizedString(@"Rename", "rename sheet button");
|
|
|
|
self.cancelButton.title = NSLocalizedString(@"Cancel", "rename sheet button");
|
2017-01-24 17:53:16 +00:00
|
|
|
|
2013-02-09 19:13:26 +00:00
|
|
|
[self.renameButton sizeToFit];
|
|
|
|
[self.cancelButton sizeToFit];
|
2021-08-07 07:27:56 +00:00
|
|
|
NSRect newRenameFrame = self.renameButton.frame;
|
|
|
|
NSRect newCancelFrame = self.cancelButton.frame;
|
2013-02-09 19:13:26 +00:00
|
|
|
newRenameFrame.size.width = MAX(NSWidth(newRenameFrame), NSWidth(newCancelFrame)) + extra;
|
|
|
|
newCancelFrame.size.width = MAX(NSWidth(newRenameFrame), NSWidth(newCancelFrame)) + extra;
|
2017-01-24 17:53:16 +00:00
|
|
|
|
2021-08-15 09:41:48 +00:00
|
|
|
CGFloat const renameWidthIncrease = NSWidth(newRenameFrame) - NSWidth(oldRenameFrame);
|
2013-02-09 19:13:26 +00:00
|
|
|
newRenameFrame.origin.x -= renameWidthIncrease;
|
2021-08-07 07:27:56 +00:00
|
|
|
self.renameButton.frame = newRenameFrame;
|
2017-01-24 17:53:16 +00:00
|
|
|
|
2021-08-15 09:41:48 +00:00
|
|
|
CGFloat const cancelWidthIncrease = NSWidth(newCancelFrame) - NSWidth(oldCancelFrame);
|
2013-02-09 19:13:26 +00:00
|
|
|
newCancelFrame.origin.x -= renameWidthIncrease + cancelWidthIncrease;
|
2021-08-07 07:27:56 +00:00
|
|
|
self.cancelButton.frame = newCancelFrame;
|
2013-01-22 00:09:48 +00:00
|
|
|
}
|
|
|
|
|
2021-08-15 09:41:48 +00:00
|
|
|
- (IBAction)rename:(id)sender
|
2013-01-22 00:09:48 +00:00
|
|
|
{
|
|
|
|
void (^completionHandler)(BOOL) = ^(BOOL didRename) {
|
|
|
|
if (didRename)
|
2021-08-15 09:41:48 +00:00
|
|
|
{
|
2021-10-31 15:18:27 +00:00
|
|
|
[NSApp endSheet:self.window returnCode:NSModalResponseOK];
|
2021-08-15 09:41:48 +00:00
|
|
|
}
|
2013-01-22 00:09:48 +00:00
|
|
|
else
|
|
|
|
{
|
2021-08-15 09:41:48 +00:00
|
|
|
#warning more thorough error
|
2013-01-22 00:09:48 +00:00
|
|
|
NSBeep();
|
|
|
|
}
|
|
|
|
};
|
2017-01-24 17:53:16 +00:00
|
|
|
|
2013-01-22 00:09:48 +00:00
|
|
|
if (self.node)
|
2021-08-15 09:41:48 +00:00
|
|
|
{
|
|
|
|
[self.torrent renameFileNode:self.node withName:self.inputField.stringValue completionHandler:completionHandler];
|
|
|
|
}
|
2013-01-22 00:09:48 +00:00
|
|
|
else
|
2021-08-15 09:41:48 +00:00
|
|
|
{
|
|
|
|
[self.torrent renameTorrent:self.inputField.stringValue completionHandler:completionHandler];
|
|
|
|
}
|
2013-01-22 00:09:48 +00:00
|
|
|
}
|
|
|
|
|
2021-08-15 09:41:48 +00:00
|
|
|
- (IBAction)cancelRename:(id)sender
|
2013-01-22 00:09:48 +00:00
|
|
|
{
|
2021-10-31 15:18:27 +00:00
|
|
|
[NSApp endSheet:self.window returnCode:NSModalResponseCancel];
|
2013-01-22 00:09:48 +00:00
|
|
|
}
|
|
|
|
|
2021-08-15 09:41:48 +00:00
|
|
|
- (void)controlTextDidChange:(NSNotification*)notification
|
2013-01-22 00:09:48 +00:00
|
|
|
{
|
2021-08-15 09:41:48 +00:00
|
|
|
self.renameButton.enabled = ![self.inputField.stringValue isEqualToString:@""] &&
|
|
|
|
![self.inputField.stringValue isEqualToString:self.originalName];
|
2013-01-22 00:09:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@end
|