source: trunk/macosx/FileRenameSheetController.m @ 13875

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

explicitly synthesize variables in FileRenameSheetController?

File size: 4.0 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
30+ (void) presentSheetForTorrent: (Torrent *) torrent modalForWindow: (NSWindow *) window completionHandler: (void (^)(BOOL didRename)) completionHandler
31{
32    NSParameterAssert(torrent != nil);
33    NSParameterAssert(window != nil);
34   
35    FileRenameSheetController * renamer = [[FileRenameSheetController alloc] initWithWindowNibName: @"FileRenameSheetController"];
36   
37    renamer.torrent = torrent;
38    renamer.completionHandler = completionHandler;
39   
40    [NSApp beginSheet: [renamer window] modalForWindow: window modalDelegate: self didEndSelector: @selector(sheetDidEnd:returnCode:contextInfo:) contextInfo: renamer];
41}
42
43+ (void) presentSheetForFileListNode: (FileListNode *) node modalForWindow: (NSWindow *) window completionHandler: (void (^)(BOOL didRename)) completionHandler
44{
45   
46    NSParameterAssert(node != nil);
47    NSParameterAssert(window != nil);
48   
49    FileRenameSheetController * renamer = [[FileRenameSheetController alloc] initWithWindowNibName: @"FileRenameSheetController"];
50   
51    renamer.torrent = [node torrent];
52    renamer.node = node;
53    renamer.completionHandler = completionHandler;
54   
55    [NSApp beginSheet: [renamer window] modalForWindow: window modalDelegate: self didEndSelector: @selector(sheetDidEnd:returnCode:contextInfo:) contextInfo: renamer];
56}
57
58+ (void) sheetDidEnd: (NSWindow *) sheet returnCode: (NSInteger) returnCode contextInfo: (void *) contextInfo
59{
60    FileRenameSheetController * renamer = contextInfo;
61    NSParameterAssert([renamer isKindOfClass:[FileRenameSheetController class]]);
62   
63    renamer.completionHandler(returnCode == NSOKButton);
64   
65    //TODO: retain/release logic needs to be figured out for ARC (when ARC is enabled)
66    [renamer release];
67    [sheet orderOut: self];
68}
69
70- (void) dealloc
71{
72    [_torrent release];
73    [_node release];
74    [_completionHandler release];
75    [super dealloc];
76}
77
78- (void) windowDidLoad
79{
80    [super windowDidLoad];
81   
82    NSString * name = self.node ? [self.node name] : [self.torrent name];
83    NSString * label = [NSString stringWithFormat: NSLocalizedString(@"Rename the file \"%@\":", "rename sheet label"), name];
84    [self.labelField setStringValue: label];
85   
86    [self.inputField setStringValue: name];
87    [self.renameButton setEnabled: NO];
88   
89    #warning size these
90    [self.renameButton setStringValue: NSLocalizedString(@"Rename", "rename sheet button")];
91    [self.cancelButton setStringValue: NSLocalizedString(@"Cancel", "rename sheet button")];
92}
93
94- (IBAction) rename: (id) sender;
95{
96    void (^completionHandler)(BOOL) = ^(BOOL didRename) {
97        if (didRename)
98            [NSApp endSheet: [self window] returnCode: NSOKButton];
99        else
100        {
101            #warning more thorough error
102            NSBeep();
103        }
104    };
105   
106    if (self.node)
107        [self.torrent renameFileNode: self.node withName: [self.inputField stringValue] completionHandler: completionHandler];
108    else
109        [self.torrent renameTorrent: [self.inputField stringValue] completionHandler: completionHandler];
110}
111
112- (IBAction) cancelRename: (id) sender
113{
114    [NSApp endSheet: [self window] returnCode: NSCancelButton];
115}
116
117- (void) controlTextDidChange: (NSNotification *) notification
118{
119    [self.renameButton setEnabled: ![[self.inputField stringValue] isEqualToString: @""] && ![[self.inputField stringValue] isEqualToString: [self.torrent name]]];
120}
121
122@end
Note: See TracBrowser for help on using the repository browser.