-
Notifications
You must be signed in to change notification settings - Fork 72
/
CK2NewFolderWindowController.m
164 lines (125 loc) · 4.01 KB
/
CK2NewFolderWindowController.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
//
// CK2NewFolderWindowWindowController.m
// Connection
//
// Created by Paul Kim on 1/22/13.
//
//
#import "CK2NewFolderWindowController.h"
#import "CK2OpenPanelController.h"
#import <ConnectionKit/CK2FileManager.h>
#define DEFAULT_NAME @"untitled folder"
@interface CK2NewFolderWindowController ()
@property (readwrite, retain) NSURL *folderURL;
@property (readwrite, retain) NSError *error;
@end
@implementation CK2NewFolderWindowController
@synthesize folderURL = _folderURL;
@synthesize error = _error;
- (id)initWithController:(CK2OpenPanelController *)controller
{
if ((self = [super initWithWindowNibName:@"CK2NewFolderWindow"]) != nil)
{
_controller = controller;
}
return self;
}
- (void)dealloc
{
if (_operation != nil)
{
[[_controller fileManager] cancelOperation:_operation];
}
[_operation release];
[_folderURL release];
[_error release];
[_existingNames release];
[super dealloc];
}
- (void)controlTextDidChange:(NSNotification *)aNotification
{
NSText *fieldEditor;
NSString *string;
BOOL nameExists;
fieldEditor = [[aNotification userInfo] objectForKey:@"NSFieldEditor"];
string = [fieldEditor string];
nameExists = [_existingNames containsObject:string];
[_statusField setHidden:!nameExists];
[_okButton setEnabled:([string length] != 0) && !nameExists];
//PENDING:
//Replace ':' with '-'
}
- (BOOL)runModalForURL:(NSURL *)url
{
NSArray *children;
NSInteger resultCode;
NSString *name;
NSUInteger i;
[self setFolderURL:url];
children = [_controller childrenForURL:url];
_existingNames = [[children valueForKey:@"lastPathComponent"] retain];
name = DEFAULT_NAME;
i = 2;
while ([_existingNames containsObject:name])
{
name = [NSString stringWithFormat:@"%@ %lu", DEFAULT_NAME, (unsigned long)i++];
}
// Window may not be loaded yet.
[self window];
[_nameField setStringValue:name];
resultCode = [NSApp runModalForWindow:[self window]];
return ((resultCode == NSOKButton) && (_error == nil));
}
- (void)endWithCode:(NSInteger)code
{
NSWindow *window;
[_progressIndicator stopAnimation:self];
window = [self window];
[window close];
[_operation release];
_operation = nil;
[_existingNames release];
_existingNames = nil;
if ([window isModalPanel])
{
[NSApp stopModalWithCode:code];
}
}
- (void)doIt:sender
{
[self endWithCode:NSOKButton];
}
- (IBAction)ok:(id)sender
{
NSString *folderName;
NSURL *url;
folderName = [_nameField stringValue];
url = [[self folderURL] URLByAppendingPathComponent:folderName isDirectory:YES];
[_okButton setEnabled:NO];
_operation = [[[_controller fileManager] createDirectoryAtURL:url withIntermediateDirectories:NO openingAttributes:nil completionHandler:
^(NSError *blockError)
{
NSEvent *event;
dispatch_async(dispatch_get_main_queue(),
^{
[self setError:blockError];
[self setFolderURL:url];
[self endWithCode:NSOKButton];
[_okButton setEnabled:YES];
});
// It seems that the run loop doesn't wake up after ending the modal session from a block (also happens with
// -performSelectorOnMainThread:) so we create a fake event here to "jiggle its rat" (rdar://13079612)
event = [NSEvent otherEventWithType:NSApplicationDefined location:NSZeroPoint modifierFlags:0 timestamp:0 windowNumber:[[self window] windowNumber] context:NULL subtype:NSApplicationDefined data1:0 data2:0];
[NSApp postEvent:event atStart:YES];
}] retain];
[_progressIndicator startAnimation:self];
}
- (IBAction)cancel:(id)sender
{
if (_operation != nil)
{
[[_controller fileManager] cancelOperation:_operation];
}
[self endWithCode:NSCancelButton];
}
@end