source: trunk/macosx/FileRenameSheetController.m @ 13877

Last change on this file since 13877 was 13877, checked in by livings124, 8 years ago

more variables corresponding to properties in FileRenameSheetController?

File size: 4.2 KB
Line 
1//
2//  FileRenameSheetController.m
3//  Transmission
4//
5//  Created by Mitchell Livingston on 1/20/13.
6//  Copyright (c) 2013 The Transmission Project. All rights reserved.
7//
8
9#import "FileRenameSheetController.h"
10#import "FileListNode.h"
11#import "Torrent.h"
12
13typedef void (^CompletionBlock)(BOOL);
14
15@interface FileRenameSheetController ()
16
17@property (nonatomic, retain) Torrent * torrent;
18@property (nonatomic, retain) FileListNode * node;
19@property (nonatomic, copy) CompletionBlock completionHandler;
20
21@end
22
23@implementation FileRenameSheetController
24
25#warning remove ivars in header when 64-bit only (or it compiles in 32-bit mode)
26@synthesize torrent = _torrent;
27@synthesize node = _node;
28@synthesize completionHandler = _completionHandler;
29@synthesize labelField = _labelField;
30@synthesize inputField = _inputField;
31@synthesize renameButton = _renameButton;
32@synthesize cancelButton = _cancelButton;
33
34+ (void) presentSheetForTorrent: (Torrent *) torrent modalForWindow: (NSWindow *) window completionHandler: (void (^)(BOOL didRename)) completionHandler
35{
36    NSParameterAssert(torrent != nil);
37    NSParameterAssert(window != nil);
38   
39    FileRenameSheetController * renamer = [[FileRenameSheetController alloc] initWithWindowNibName: @"FileRenameSheetController"];
40   
41    renamer.torrent = torrent;
42    renamer.completionHandler = completionHandler;
43   
44    [NSApp beginSheet: [renamer window] modalForWindow: window modalDelegate: self didEndSelector: @selector(sheetDidEnd:returnCode:contextInfo:) contextInfo: renamer];
45}
46
47+ (void) presentSheetForFileListNode: (FileListNode *) node modalForWindow: (NSWindow *) window completionHandler: (void (^)(BOOL didRename)) completionHandler
48{
49    NSParameterAssert(node != nil);
50    NSParameterAssert(window != nil);
51   
52    FileRenameSheetController * renamer = [[FileRenameSheetController alloc] initWithWindowNibName: @"FileRenameSheetController"];
53   
54    renamer.torrent = [node torrent];
55    renamer.node = node;
56    renamer.completionHandler = completionHandler;
57   
58    [NSApp beginSheet: [renamer window] modalForWindow: window modalDelegate: self didEndSelector: @selector(sheetDidEnd:returnCode:contextInfo:) contextInfo: renamer];
59}
60
61+ (void) sheetDidEnd: (NSWindow *) sheet returnCode: (NSInteger) returnCode contextInfo: (void *) contextInfo
62{
63    FileRenameSheetController * renamer = contextInfo;
64    NSParameterAssert([renamer isKindOfClass:[FileRenameSheetController class]]);
65   
66    renamer.completionHandler(returnCode == NSOKButton);
67   
68    //TODO: retain/release logic needs to be figured out for ARC (when ARC is enabled)
69    [renamer release];
70    [sheet orderOut: self];
71}
72
73- (void) dealloc
74{
75    [_torrent release];
76    [_node release];
77    [_completionHandler release];
78    [super dealloc];
79}
80
81- (void) windowDidLoad
82{
83    [super windowDidLoad];
84   
85    NSString * name = self.node ? [self.node name] : [self.torrent name];
86    NSString * label = [NSString stringWithFormat: NSLocalizedString(@"Rename the file \"%@\":", "rename sheet label"), name];
87    [self.labelField setStringValue: label];
88   
89    [self.inputField setStringValue: name];
90    [self.renameButton setEnabled: NO];
91   
92    #warning size these
93    [self.renameButton setStringValue: NSLocalizedString(@"Rename", "rename sheet button")];
94    [self.cancelButton setStringValue: NSLocalizedString(@"Cancel", "rename sheet button")];
95}
96
97- (IBAction) rename: (id) sender;
98{
99    void (^completionHandler)(BOOL) = ^(BOOL didRename) {
100        if (didRename)
101            [NSApp endSheet: [self window] returnCode: NSOKButton];
102        else
103        {
104            #warning more thorough error
105            NSBeep();
106        }
107    };
108   
109    if (self.node)
110        [self.torrent renameFileNode: self.node withName: [self.inputField stringValue] completionHandler: completionHandler];
111    else
112        [self.torrent renameTorrent: [self.inputField stringValue] completionHandler: completionHandler];
113}
114
115- (IBAction) cancelRename: (id) sender
116{
117    [NSApp endSheet: [self window] returnCode: NSCancelButton];
118}
119
120- (void) controlTextDidChange: (NSNotification *) notification
121{
122    [self.renameButton setEnabled: ![[self.inputField stringValue] isEqualToString: @""] && ![[self.inputField stringValue] isEqualToString: [self.torrent name]]];
123}
124
125@end
Note: See TracBrowser for help on using the repository browser.