-
Notifications
You must be signed in to change notification settings - Fork 5
/
SetupAssistantController.m
346 lines (283 loc) · 10.6 KB
/
SetupAssistantController.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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
/******************************************************************************
* Nativa - MacOS X UI for rtorrent
* http://www.aramzamzam.net
*
* Copyright Solomenchuk V. 2010.
* Solomenchuk Vladimir <[email protected]>
*
* Licensed under the GPL, Version 3.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.gnu.org/licenses/gpl-3.0.html
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*****************************************************************************/
#import "SetupAssistantController.h"
#import "SynthesizeSingleton.h"
#import "NIHostPort.h"
#import "ProcessesController.h"
#import <netinet/in.h>
@interface SetupAssistantController(Private)
- (int) findFreePort:(int) startPort endPort:(int)endPort;
- (void) checkSettings:(BOOL) checkSSH checkSCGI:(BOOL) checkSCGI handler:(void (^)(BOOL success))handler;
- (void) downloadsPathClosed: (NSOpenPanel *) openPanel returnCode: (int) code contextInfo: (void *) info;
- (void) showError:(NSString *) error;
- (void) closeTestConnection;
@end
@implementation SetupAssistantController
@dynamic currentView;
@synthesize sshHost, sshUsername, sshPassword, useSSHKeyLogin, errorMessage, checking, sshLocalPort, scgiHost, openSetupAssistantHandler, localDownloadsFolder;
;
SYNTHESIZE_SINGLETON_FOR_CLASS(SetupAssistantController);
- (id) init
{
if ((self = [super initWithWindowNibName: @"SetupAssistant"]))
{
pc = [ProcessesController sharedProcessesController];
currentProcessIndex = [pc addProcess];
}
return self;
}
- (void) openSetupAssistant:(void (^)(id sender))handler
{
[self setOpenSetupAssistantHandler:handler];
NSWindow* window = [self window];
if (![window isVisible])
[window center];
[window makeKeyAndOrderFront: nil];
}
- (void)awakeFromNib
{
NSView *contentView = [[self window] contentView];
[contentView setWantsLayer:YES];
[self setCurrentView:startView];
[contentView addSubview:[self currentView]];
transition = [CATransition animation];
[transition setType:kCATransitionPush];
[transition setSubtype:kCATransitionFromLeft];
NSDictionary *ani = [NSDictionary dictionaryWithObject:transition forKey:@"subviews"];
[contentView setAnimations:ani];
}
- (void)setCurrentView:(NSView*)newView
{
if (!currentView) {
currentView = newView;
return;
}
NSView *contentView = [[self window] contentView];
[[contentView animator] replaceSubview:currentView with:newView];
currentView = newView;
}
- (NSView*) currentView
{
return currentView;
}
- (IBAction)showStartView:(id)sender
{
useSSH = NO;
[self setChecking:NO];
[self setErrorMessage:nil];
[transition setSubtype:kCATransitionFromLeft];
[self setCurrentView:startView];
}
- (IBAction)showConfigureSSHView:(id)sender
{
[self setChecking:NO];
useSSH = NO;
[transition setSubtype:kCATransitionFromRight];
[self setCurrentView:configureSSHView];
[[self window] makeFirstResponder:sshFirstResponder];
}
- (IBAction)showConfigureSCGIView:(id)sender
{
[self setErrorMessage: nil];
[transition setSubtype:kCATransitionFromRight];
[self setCurrentView:configureSCGIView];
[[self window] makeFirstResponder:scgiFirstResponder];
}
- (IBAction)checkSSH:(id)sender
{
sshLocalPort = [self findFreePort:5000 endPort:5010];
if (scgiHost == nil || [scgiHost isEqualToString:@""])
[self setScgiHost: @"127.0.0.1:5000"];
[self checkSettings:YES checkSCGI:NO handler:^(BOOL success){
if (success)
{
useSSH = YES;
[self showConfigureSCGIView:nil];
}
}];
}
- (IBAction)checkSCGI:(id)sender
{
[self checkSettings:useSSH checkSCGI:YES handler:^(BOOL success){
if (success)
{
[pc saveProcesses];
[[self window] close];
if (openSetupAssistantHandler != nil)
openSetupAssistantHandler(self);
}
}];
}
//show folder doalog for downloads path
- (void) downloadsPathShow: (id) sender
{
NSOpenPanel * panel = [NSOpenPanel openPanel];
[panel setPrompt: NSLocalizedString(@"Select", "Preferences -> Open panel prompt")];
[panel setAllowsMultipleSelection: NO];
[panel setCanChooseFiles: NO];
[panel setCanChooseDirectories: YES];
[panel setCanCreateDirectories: YES];
[panel beginSheetForDirectory: nil file: nil types: nil
modalForWindow: [self window] modalDelegate: self didEndSelector:
@selector(downloadsPathClosed:returnCode:contextInfo:) contextInfo: nil];
}
@end
@implementation SetupAssistantController(Private)
-(int) findFreePort:(int) startPort endPort:(int)endPort
{
CFSocketRef socket = CFSocketCreate(kCFAllocatorDefault, PF_INET, SOCK_STREAM,
IPPROTO_TCP, 0, NULL, NULL);
if (!socket)
{
NSLog(@"unable to create socket");
return 0;
}
int fileDescriptor = CFSocketGetNative(socket);
int reuse = false;
if (setsockopt(fileDescriptor, SOL_SOCKET, SO_REUSEADDR,
(void *)&reuse, sizeof(int)) != 0)
{
CFSocketInvalidate(socket);
CFRelease(socket);
socket = nil;
NSLog(@"Unable to set socket options.");
return 0;
}
struct sockaddr_in address;
memset(&address, 0, sizeof(address));
address.sin_len = sizeof(address);
address.sin_family = AF_INET;
address.sin_addr.s_addr = htonl(INADDR_ANY);
CFDataRef addressData = nil;
int resultPort = 0;
for(int i=startPort;i<=endPort;i++)
{
address.sin_port = htons(i);
addressData =
CFDataCreate(NULL, (const UInt8 *)&address, sizeof(address));
[(id)addressData autorelease];
if (CFSocketSetAddress(socket, addressData) == kCFSocketSuccess)
{
resultPort = i;
NSLog(@"port %d is free", i);
break;
}
NSLog(@"port %d is busy", i);
}
CFSocketInvalidate(socket);
CFRelease(socket);
socket = nil;
if (resultPort == 0)
NSLog(@"Unable to bind socket to address.");
return resultPort;
}
- (void) checkSettings:(BOOL) checkSSH checkSCGI:(BOOL) checkSCGI handler:(void (^)(BOOL success))handler;
{
[[self window] makeFirstResponder: nil];
[self setErrorMessage: nil];
[pc closeProcessForIndex:currentProcessIndex];
NIHostPort *scgiHostPort = [NIHostPort parseHostPort:scgiHost==nil?@"":scgiHost defaultPort:5000];
NIHostPort *sshHostPort = [NIHostPort parseHostPort:sshHost==nil?@"":sshHost defaultPort:22];
//test connection with only one reconnect
int maxReconnects = ([pc maxReconnectsForIndex:currentProcessIndex] == 0?10:[pc maxReconnectsForIndex:currentProcessIndex]);
[pc setMaxReconnects:0 forIndex:currentProcessIndex];
[pc setHost:scgiHostPort.host forIndex:currentProcessIndex];
[pc setPort:scgiHostPort.port forIndex:currentProcessIndex];
[pc setConnectionType:checkSSH?@"SSH":@"Local" forIndex:currentProcessIndex];
[pc setSshHost:sshHostPort.host forIndex:currentProcessIndex];
[pc setSshPort:sshHostPort.port forIndex:currentProcessIndex];
[pc setSshLocalPort:sshLocalPort forIndex:currentProcessIndex];
[pc setSshUser:sshUsername forIndex:currentProcessIndex];
[pc setSshPassword:sshPassword forIndex:currentProcessIndex];
[pc setSshUseKeyLogin:useSSHKeyLogin forIndex:currentProcessIndex];
[pc setSshUseV2:NO forIndex:currentProcessIndex];
[pc setSshCompressionLevel:0 forIndex:currentProcessIndex];
[pc setGroupsField:1 forIndex:currentProcessIndex];
[pc setLocalDownloadsFolder:localDownloadsFolder forIndex:currentProcessIndex];
[self setChecking:YES];
[pc openProcessForIndex:currentProcessIndex handler:^(NSString *error){
[pc setMaxReconnects:maxReconnects forIndex:currentProcessIndex];
[self showError:error];
if (checkSCGI)
{
[[pc processForIndex:currentProcessIndex] list:^(NSArray *array, NSString* error){
[self setChecking:NO];
[self showError:error];
[self closeTestConnection];
if (handler)
handler(error == nil);
}];
}
else
{
[self setChecking:NO];
[self closeTestConnection];
if (handler)
handler(error == nil);
}
}];
}
- (void) downloadsPathClosed: (NSOpenPanel *) openPanel returnCode: (int) code contextInfo: (void *) info
{
if (code == NSOKButton)
{
NSString * folder = [[openPanel filenames] objectAtIndex: 0];
[self setLocalDownloadsFolder:folder];
[_downloadsPathPopUp removeItemAtIndex:0];
if (localDownloadsFolder == nil)
[_downloadsPathPopUp insertItemWithTitle:@"" atIndex:0];
else
{
[_downloadsPathPopUp insertItemWithTitle:[[NSFileManager defaultManager] displayNameAtPath: localDownloadsFolder] atIndex:0];
NSString * path = [localDownloadsFolder stringByExpandingTildeInPath];
NSImage * icon;
//show a folder icon if the folder doesn't exist
if ([[path pathExtension] isEqualToString: @""] && ![[NSFileManager defaultManager] fileExistsAtPath: path])
icon = [[NSWorkspace sharedWorkspace] iconForFileType: NSFileTypeForHFSTypeCode('fldr')];
else
icon = [[NSWorkspace sharedWorkspace] iconForFile: path];
[icon setSize: NSMakeSize(16.0, 16.0)];
NSMenuItem* menuItem = [_downloadsPathPopUp itemAtIndex:0];
[menuItem setImage:icon];
}
[_downloadsPathPopUp selectItemAtIndex: 0];
}
}
- (void) showError:(NSString *) error
{
if (![NSThread isMainThread])
{
[self performSelectorOnMainThread:@selector(showError:) withObject:error waitUntilDone:NO];
return;
}
NSLog(@"error: %@", error);
[self setChecking:NO];
[self setErrorMessage: error];
}
- (void) closeTestConnection
{
if (![NSThread isMainThread])
{
[self performSelectorOnMainThread:@selector(closeTestConnection) withObject:nil waitUntilDone:NO];
return;
}
[pc closeProcessForIndex:currentProcessIndex];
}
@end