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
|
|
|
|
|
|
|
@property (nonatomic, retain) Torrent * torrent;
|
|
|
|
@property (nonatomic, retain) FileListNode * node;
|
|
|
|
@property (nonatomic, copy) CompletionBlock completionHandler;
|
|
|
|
|
|
|
|
@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-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);
|
|
|
|
|
|
|
|
FileRenameSheetController * renamer = [[FileRenameSheetController alloc] initWithWindowNibName: @"FileRenameSheetController"];
|
|
|
|
|
|
|
|
renamer.torrent = torrent;
|
|
|
|
renamer.completionHandler = completionHandler;
|
|
|
|
|
|
|
|
[NSApp beginSheet: [renamer window] modalForWindow: window modalDelegate: self didEndSelector: @selector(sheetDidEnd:returnCode:contextInfo:) contextInfo: renamer];
|
|
|
|
}
|
|
|
|
|
|
|
|
+ (void) presentSheetForFileListNode: (FileListNode *) node modalForWindow: (NSWindow *) window completionHandler: (void (^)(BOOL didRename)) completionHandler
|
|
|
|
{
|
|
|
|
NSParameterAssert(node != nil);
|
|
|
|
NSParameterAssert(window != nil);
|
|
|
|
|
|
|
|
FileRenameSheetController * renamer = [[FileRenameSheetController alloc] initWithWindowNibName: @"FileRenameSheetController"];
|
|
|
|
|
|
|
|
renamer.torrent = [node torrent];
|
|
|
|
renamer.node = node;
|
|
|
|
renamer.completionHandler = completionHandler;
|
|
|
|
|
|
|
|
[NSApp beginSheet: [renamer window] modalForWindow: window modalDelegate: self didEndSelector: @selector(sheetDidEnd:returnCode:contextInfo:) contextInfo: renamer];
|
|
|
|
}
|
|
|
|
|
|
|
|
+ (void) sheetDidEnd: (NSWindow *) sheet returnCode: (NSInteger) returnCode contextInfo: (void *) contextInfo
|
|
|
|
{
|
|
|
|
FileRenameSheetController * renamer = contextInfo;
|
|
|
|
NSParameterAssert([renamer isKindOfClass:[FileRenameSheetController class]]);
|
|
|
|
|
|
|
|
renamer.completionHandler(returnCode == NSOKButton);
|
|
|
|
|
|
|
|
//TODO: retain/release logic needs to be figured out for ARC (when ARC is enabled)
|
|
|
|
[renamer release];
|
|
|
|
[sheet orderOut: self];
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void) dealloc
|
|
|
|
{
|
|
|
|
[_torrent release];
|
|
|
|
[_node release];
|
|
|
|
[_completionHandler release];
|
|
|
|
[super dealloc];
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void) windowDidLoad
|
|
|
|
{
|
|
|
|
[super windowDidLoad];
|
|
|
|
|
|
|
|
NSString * name = self.node ? [self.node name] : [self.torrent name];
|
2013-01-23 03:46:04 +00:00
|
|
|
NSString * label = [NSString stringWithFormat: NSLocalizedString(@"Rename the file \"%@\":", "rename sheet label"), name];
|
2013-01-22 00:09:48 +00:00
|
|
|
[self.labelField setStringValue: label];
|
|
|
|
|
|
|
|
[self.inputField setStringValue: name];
|
|
|
|
[self.renameButton setEnabled: NO];
|
2013-01-22 00:44:58 +00:00
|
|
|
|
|
|
|
#warning size these
|
|
|
|
[self.renameButton setStringValue: NSLocalizedString(@"Rename", "rename sheet button")];
|
|
|
|
[self.cancelButton setStringValue: NSLocalizedString(@"Cancel", "rename sheet button")];
|
2013-01-22 00:09:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
- (IBAction) rename: (id) sender;
|
|
|
|
{
|
|
|
|
void (^completionHandler)(BOOL) = ^(BOOL didRename) {
|
|
|
|
if (didRename)
|
|
|
|
[NSApp endSheet: [self window] returnCode: NSOKButton];
|
|
|
|
else
|
|
|
|
{
|
|
|
|
#warning more thorough error
|
|
|
|
NSBeep();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
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
|
|
|
|
{
|
|
|
|
[self.renameButton setEnabled: ![[self.inputField stringValue] isEqualToString: @""] && ![[self.inputField stringValue] isEqualToString: [self.torrent name]]];
|
|
|
|
}
|
|
|
|
|
|
|
|
@end
|