2013-01-22 00:09:48 +00:00
|
|
|
//
|
|
|
|
// FileRenameSheetController.m
|
|
|
|
// Transmission
|
|
|
|
//
|
|
|
|
// Created by Mitchell Livingston on 1/20/13.
|
|
|
|
// Copyright (c) 2013 The Transmission Project. All rights reserved.
|
|
|
|
//
|
|
|
|
|
|
|
|
#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
|
|
|
|
2017-07-29 16:14:22 +00:00
|
|
|
@property (nonatomic, strong) Torrent * torrent;
|
|
|
|
@property (nonatomic, strong) FileListNode * node;
|
2013-01-22 00:09:48 +00:00
|
|
|
@property (nonatomic, copy) CompletionBlock completionHandler;
|
|
|
|
|
2013-02-08 23:26:07 +00:00
|
|
|
@property (nonatomic, copy) NSString * originalName;
|
|
|
|
|
2013-01-22 00:09:48 +00:00
|
|
|
@end
|
|
|
|
|
|
|
|
@implementation FileRenameSheetController
|
|
|
|
|
2013-01-26 19:59:56 +00:00
|
|
|
#warning remove ivars in header when 64-bit only (or it compiles in 32-bit mode)
|
|
|
|
@synthesize torrent = _torrent;
|
|
|
|
@synthesize node = _node;
|
|
|
|
@synthesize completionHandler = _completionHandler;
|
2013-02-08 23:26:07 +00:00
|
|
|
@synthesize originalName = _originalName;
|
2013-01-26 20:11:49 +00:00
|
|
|
@synthesize labelField = _labelField;
|
|
|
|
@synthesize inputField = _inputField;
|
|
|
|
@synthesize renameButton = _renameButton;
|
|
|
|
@synthesize cancelButton = _cancelButton;
|
2013-01-26 19:59:56 +00:00
|
|
|
|
2013-01-22 00:09:48 +00:00
|
|
|
+ (void) presentSheetForTorrent: (Torrent *) torrent modalForWindow: (NSWindow *) window completionHandler: (void (^)(BOOL didRename)) completionHandler
|
|
|
|
{
|
|
|
|
NSParameterAssert(torrent != nil);
|
|
|
|
NSParameterAssert(window != nil);
|
2017-01-24 17:53:16 +00:00
|
|
|
|
2013-01-22 00:09: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;
|
|
|
|
renamer.completionHandler = completionHandler;
|
2017-01-24 17:53:16 +00:00
|
|
|
|
2017-08-01 10:40:39 +00:00
|
|
|
[NSApp beginSheet: [renamer window] modalForWindow: window modalDelegate: self didEndSelector: @selector(sheetDidEnd:returnCode:contextInfo:) contextInfo: (__bridge_retained void *)(renamer)];
|
2013-01-22 00:09:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
+ (void) presentSheetForFileListNode: (FileListNode *) node modalForWindow: (NSWindow *) window completionHandler: (void (^)(BOOL didRename)) completionHandler
|
|
|
|
{
|
|
|
|
NSParameterAssert(node != nil);
|
|
|
|
NSParameterAssert(window != nil);
|
2017-01-24 17:53:16 +00:00
|
|
|
|
2013-01-22 00:09: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 = [node torrent];
|
|
|
|
renamer.node = node;
|
|
|
|
renamer.completionHandler = completionHandler;
|
2017-01-24 17:53:16 +00:00
|
|
|
|
2017-08-01 10:40:39 +00:00
|
|
|
[NSApp beginSheet: [renamer window] modalForWindow: window modalDelegate: self didEndSelector: @selector(sheetDidEnd:returnCode:contextInfo:) contextInfo: (__bridge_retained void *)(renamer)];
|
2013-01-22 00:09:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
+ (void) sheetDidEnd: (NSWindow *) sheet returnCode: (NSInteger) returnCode contextInfo: (void *) contextInfo
|
|
|
|
{
|
2017-08-01 10:40:39 +00:00
|
|
|
FileRenameSheetController * renamer = (__bridge_transfer FileRenameSheetController *)(contextInfo);
|
2013-01-22 00:09:48 +00:00
|
|
|
NSParameterAssert([renamer isKindOfClass:[FileRenameSheetController class]]);
|
2017-01-24 17:53:16 +00:00
|
|
|
|
2013-01-22 00:09:48 +00:00
|
|
|
renamer.completionHandler(returnCode == NSOKButton);
|
2017-01-24 17:53:16 +00:00
|
|
|
|
2013-01-22 00:09:48 +00:00
|
|
|
[sheet orderOut: self];
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
- (void) windowDidLoad
|
|
|
|
{
|
|
|
|
[super windowDidLoad];
|
2017-01-24 17:53:16 +00:00
|
|
|
|
2013-02-08 23:26:07 +00:00
|
|
|
self.originalName = [self.node name] ?: [self.torrent name];
|
|
|
|
NSString * label = [NSString stringWithFormat: NSLocalizedString(@"Rename the file \"%@\":", "rename sheet label"), self.originalName];
|
2013-01-22 00:09:48 +00:00
|
|
|
[self.labelField setStringValue: label];
|
2017-01-24 17:53:16 +00:00
|
|
|
|
2013-02-08 23:26:07 +00:00
|
|
|
[self.inputField setStringValue: self.originalName];
|
2013-01-22 00:09:48 +00:00
|
|
|
[self.renameButton setEnabled: 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
|
|
|
|
const NSRect oldRenameFrame = [self.renameButton frame];
|
|
|
|
const NSRect 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];
|
|
|
|
const CGFloat extra = NSWidth(oldRenameFrame) - NSWidth([self.renameButton frame]);
|
2017-01-24 17:53:16 +00:00
|
|
|
|
2014-02-01 19:13:36 +00:00
|
|
|
[self.renameButton setTitle: NSLocalizedString(@"Rename", "rename sheet button")];
|
|
|
|
[self.cancelButton setTitle: 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];
|
|
|
|
NSRect newRenameFrame = [self.renameButton frame];
|
|
|
|
NSRect newCancelFrame = [self.cancelButton frame];
|
|
|
|
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
|
|
|
|
2013-02-09 19:13:26 +00:00
|
|
|
const CGFloat renameWidthIncrease = NSWidth(newRenameFrame) - NSWidth(oldRenameFrame);
|
|
|
|
newRenameFrame.origin.x -= renameWidthIncrease;
|
|
|
|
[self.renameButton setFrame:newRenameFrame];
|
2017-01-24 17:53:16 +00:00
|
|
|
|
2013-02-09 19:13:26 +00:00
|
|
|
const CGFloat cancelWidthIncrease = NSWidth(newCancelFrame) - NSWidth(oldCancelFrame);
|
|
|
|
newCancelFrame.origin.x -= renameWidthIncrease + cancelWidthIncrease;
|
|
|
|
[self.cancelButton setFrame:newCancelFrame];
|
2013-01-22 00:09:48 +00:00
|
|
|
}
|
|
|
|
|
2016-09-11 10:22:49 +00:00
|
|
|
- (IBAction) rename: (id) sender
|
2013-01-22 00:09:48 +00:00
|
|
|
{
|
|
|
|
void (^completionHandler)(BOOL) = ^(BOOL didRename) {
|
|
|
|
if (didRename)
|
|
|
|
[NSApp endSheet: [self window] returnCode: NSOKButton];
|
|
|
|
else
|
|
|
|
{
|
|
|
|
#warning more thorough error
|
|
|
|
NSBeep();
|
|
|
|
}
|
|
|
|
};
|
2017-01-24 17:53:16 +00:00
|
|
|
|
2013-01-22 00:09:48 +00:00
|
|
|
if (self.node)
|
|
|
|
[self.torrent renameFileNode: self.node withName: [self.inputField stringValue] completionHandler: completionHandler];
|
|
|
|
else
|
|
|
|
[self.torrent renameTorrent: [self.inputField stringValue] completionHandler: completionHandler];
|
|
|
|
}
|
|
|
|
|
|
|
|
- (IBAction) cancelRename: (id) sender
|
|
|
|
{
|
|
|
|
[NSApp endSheet: [self window] returnCode: NSCancelButton];
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void) controlTextDidChange: (NSNotification *) notification
|
|
|
|
{
|
2013-02-08 23:26:07 +00:00
|
|
|
[self.renameButton setEnabled: ![[self.inputField stringValue] isEqualToString: @""] && ![[self.inputField stringValue] isEqualToString: self.originalName]];
|
2013-01-22 00:09:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@end
|