source: trunk/macosx/FileRenameSheetController.m @ 13846

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

Move the rename menu item to the File menu.

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