forked from bububa/MongoHub-Mac
-
Notifications
You must be signed in to change notification settings - Fork 0
/
EditConnectionController.m
290 lines (268 loc) · 11.5 KB
/
EditConnectionController.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
//
// EditConnectionController.m
// MongoHub
//
// Created by Syd on 10-4-25.
// Copyright 2010 MusicPeace.ORG. All rights reserved.
//
#import "Configure.h"
#import "EditConnectionController.h"
#import "ConnectionsArrayController.h"
#import "Connection.h"
@implementation EditConnectionController
@synthesize hostTextField;
@synthesize hostportTextField;
@synthesize usereplCheckBox;
@synthesize serversTextField;
@synthesize replnameTextField;
@synthesize aliasTextField;
@synthesize adminuserTextField;
@synthesize adminpassTextField;
@synthesize defaultdbTextField;
@synthesize usesshCheckBox;
@synthesize bindaddressTextField;
@synthesize bindportTextField;
@synthesize sshhostTextField;
@synthesize sshportTextField;
@synthesize sshuserTextField;
@synthesize sshpasswordTextField;
@synthesize sshkeyfileTextField;
@synthesize connection;
@synthesize connectionsArrayController;
@synthesize managedObjectContext;
- (id)init {
if (![super initWithWindowNibName:@"EditConnection"]) return nil;
return self;
}
- (void)dealloc {
[hostTextField release];
[hostportTextField release];
[usereplCheckBox release];
[serversTextField release];
[replnameTextField release];
[aliasTextField release];
[adminuserTextField release];
[adminpassTextField release];
[defaultdbTextField release];
[usesshCheckBox release];
[bindaddressTextField release];
[bindportTextField release];
[sshhostTextField release];
[sshportTextField release];
[sshuserTextField release];
[sshpasswordTextField release];
[sshkeyfileTextField release];
[connection release];
[connectionsArrayController release];
[managedObjectContext release];
[super dealloc];
}
- (void)windowDidLoad {
[super windowDidLoad];
}
- (IBAction)showWindow:(id)sender {
[super showWindow:sender];
[hostTextField bind:@"value" toObject:connection withKeyPath:@"host" options:nil];
[hostportTextField bind:@"value" toObject:connection withKeyPath:@"hostport" options:nil];
[serversTextField bind:@"value" toObject:connection withKeyPath:@"servers" options:nil];
[replnameTextField bind:@"value" toObject:connection withKeyPath:@"repl_name" options:nil];
[usereplCheckBox bind:@"value" toObject:connection withKeyPath:@"userepl" options:nil];
[aliasTextField bind:@"value" toObject:connection withKeyPath:@"alias" options:nil];
[adminuserTextField bind:@"value" toObject:connection withKeyPath:@"adminuser" options:nil];
[adminpassTextField bind:@"value" toObject:connection withKeyPath:@"adminpass" options:nil];
[defaultdbTextField bind:@"value" toObject:connection withKeyPath:@"defaultdb" options:nil];
[bindaddressTextField bind:@"value" toObject:connection withKeyPath:@"bindaddress" options:nil];
[bindportTextField bind:@"value" toObject:connection withKeyPath:@"bindport" options:nil];
[sshhostTextField bind:@"value" toObject:connection withKeyPath:@"sshhost" options:nil];
[sshportTextField bind:@"value" toObject:connection withKeyPath:@"sshport" options:nil];
[sshuserTextField bind:@"value" toObject:connection withKeyPath:@"sshuser" options:nil];
[sshpasswordTextField bind:@"value" toObject:connection withKeyPath:@"sshpassword" options:nil];
[sshkeyfileTextField bind:@"value" toObject:connection withKeyPath:@"sshkeyfile" options:nil];
[usesshCheckBox bind:@"value" toObject:connection withKeyPath:@"usessh" options:nil];
[self enableSSH:nil];
[self enableRepl:nil];
}
- (IBAction)cancel:(id)sender {
[self close];
}
- (IBAction)save:(id)sender {
NSString *host;
NSUInteger hostport;
NSString *servers;
NSString *repl_name;
NSUInteger userepl = 0;
NSString *alias;
NSString *adminuser = [[NSString alloc] initWithString:[adminuserTextField stringValue]];
NSString *adminpass = [[NSString alloc] initWithString:[adminpassTextField stringValue]];
NSString *defaultdb = [[NSString alloc] initWithString:[defaultdbTextField stringValue]];
NSUInteger usessh = 0;
NSString *bindaddress;
NSUInteger bindport;
NSString *sshhost;
NSUInteger sshport;
NSString *sshuser;
NSString *sshpassword;
NSString *sshkeyfile;
if ([ [hostTextField stringValue] length] == 0) {
host = [[NSString alloc] initWithString:@"localhost"];
}else{
host = [[NSString alloc] initWithString:[hostTextField stringValue]];
}
if ([hostportTextField intValue] == 0) {
hostport = 27017;
}else{
hostport = [hostportTextField intValue];
}
servers = [[NSString alloc] initWithString:[serversTextField stringValue]];
repl_name = [[NSString alloc] initWithString:[replnameTextField stringValue]];
if ([usereplCheckBox state])
{
userepl = 1;
}
if ([ [aliasTextField stringValue] length] == 0) {
alias = [[NSString alloc] initWithString:@"localhost"];
}else{
alias = [[NSString alloc] initWithString:[aliasTextField stringValue]];
}
if ([ [bindaddressTextField stringValue] length] == 0) {
bindaddress = [[NSString alloc] initWithString:@"127.0.0.1"];
}else{
bindaddress = [[NSString alloc] initWithString:[bindaddressTextField stringValue]];
}
if ([ [bindportTextField stringValue] length] == 0) {
bindport = 8888;
}else{
bindport = [bindportTextField intValue];
}
sshhost = [[NSString alloc] initWithString:[sshhostTextField stringValue]];
if ([ [sshportTextField stringValue] length] == 0) {
sshport = 22;
}else{
sshport = [sshportTextField intValue];
}
sshuser = [[NSString alloc] initWithString:[sshuserTextField stringValue]];
sshpassword = [[NSString alloc] initWithString:[sshpasswordTextField stringValue]];
sshkeyfile = [[NSString alloc] initWithString:[sshkeyfileTextField stringValue]];
if ([usesshCheckBox state])
{
usessh = 1;
}
NSArray *keys = [[NSArray alloc] initWithObjects:@"host", @"hostport", @"userepl", @"servers", @"repl_name", @"alias", @"adminuser", @"adminpass", @"defaultdb", @"usessh", @"bindaddress", @"bindport", @"sshhost", @"sshport", @"sshuser", @"sshpassword", @"sshkeyfile", nil];
NSArray *objs = [[NSArray alloc] initWithObjects:host, [NSNumber numberWithInt:hostport], [NSNumber numberWithInt:userepl], servers, repl_name, alias, adminuser, adminpass, defaultdb, [NSNumber numberWithInt:usessh], bindaddress, [NSNumber numberWithInt:bindport], sshhost, [NSNumber numberWithInt:sshport], sshuser, sshpassword, sshkeyfile, nil];
NSDictionary *connectionInfo = [[NSDictionary alloc] initWithObjects:objs forKeys:keys];
[keys release];
[objs release];
[host release];
[servers release];
[repl_name release];
[alias release];
[adminuser release];
[adminpass release];
[defaultdb release];
[sshhost release];
[sshuser release];
[sshpassword release];
[sshkeyfile release];
[bindaddress release];
if ([self validateConnection:connectionInfo]) {
connection.host = [connectionInfo objectForKey:@"host"];
connection.hostport = [connectionInfo objectForKey:@"hostport"];
connection.servers = [connectionInfo objectForKey:@"servers"];
connection.repl_name = [connectionInfo objectForKey:@"repl_name"];
connection.userepl = [connectionInfo objectForKey:@"userepl"];
connection.alias = [connectionInfo objectForKey:@"alias"];
connection.adminuser = [connectionInfo objectForKey:@"adminuser"];
connection.adminpass = [connectionInfo objectForKey:@"adminpass"];
connection.defaultdb = [connectionInfo objectForKey:@"defaultdb"];
connection.usessh = [connectionInfo objectForKey:@"usessh"];
connection.bindaddress = [connectionInfo objectForKey:@"bindaddress"];
connection.bindport = [connectionInfo objectForKey:@"bindport"];
connection.sshhost = [connectionInfo objectForKey:@"sshhost"];
connection.sshport = [connectionInfo objectForKey:@"sshport"];
connection.sshuser = [connectionInfo objectForKey:@"sshuser"];
connection.sshpassword = [connectionInfo objectForKey:@"sshpassword"];
connection.sshkeyfile = [connectionInfo objectForKey:@"sshkeyfile"];
[self close];
}
[connectionInfo release];
}
- (BOOL)validateConnection:(NSDictionary *)connectionInfo
{
if ([[connectionInfo objectForKey:@"host"] length] == 0) {
NSRunAlertPanel(@"Error", @"Connection host should not be empty", @"OK", nil, nil);
return NO;
}
if ([[connectionInfo objectForKey:@"host"] isEqualToString:@"flame.mongohq.com"] && [[connectionInfo objectForKey:@"defaultdb"] length] == 0) {
NSRunAlertPanel(@"Error", @"DB should not be empty if you are using mongohq", @"OK", nil, nil);
return NO;
}
if ([[connectionInfo objectForKey:@"alias"] length]<3) {
NSRunAlertPanel(@"Error", @"Connection name should not be less than 3 charaters", @"OK", nil, nil);
return NO;
}
if (![[connectionInfo objectForKey:@"alias"] isEqualToString:connection.alias] && [connectionsArrayController checkDuplicate:[connectionInfo objectForKey:@"alias"]]) {
NSRunAlertPanel(@"Error", @"Connection alias name has been existed!", @"OK", nil, nil);
return NO;
}
if ([usesshCheckBox state] == 1 && ([[connectionInfo objectForKey:@"bindaddress"] length] == 0 || [[connectionInfo objectForKey:@"sshhost"] length] == 0)) {
NSRunAlertPanel(@"Error", @"Please full fill ssh information!", @"OK", nil, nil);
return NO;
}
if ([usereplCheckBox state] == 1 && ([[connectionInfo objectForKey:@"servers"] length] == 0 || [[connectionInfo objectForKey:@"repl_name"] length] == 0)) {
NSRunAlertPanel(@"Error", @"Please full fill replica-set information!", @"OK", nil, nil);
return NO;
}
return YES;
}
- (IBAction)enableSSH:(id)sender
{
if ([usesshCheckBox state] == 1)
{
[bindaddressTextField setEnabled:YES];
[bindportTextField setEnabled:YES];
[sshhostTextField setEnabled:YES];
[sshuserTextField setEnabled:YES];
[sshportTextField setEnabled:YES];
[sshpasswordTextField setEnabled:YES];
[sshkeyfileTextField setEnabled:YES];
}else {
[bindaddressTextField setEnabled:NO];
[bindportTextField setEnabled:NO];
[sshhostTextField setEnabled:NO];
[sshportTextField setEnabled:NO];
[sshuserTextField setEnabled:NO];
[sshpasswordTextField setEnabled:NO];
[sshkeyfileTextField setEnabled:NO];
}
}
- (IBAction)enableRepl:(id)sender
{
if ([usereplCheckBox state] == 1)
{
[serversTextField setEnabled:YES];
[replnameTextField setEnabled:YES];
}else {
[serversTextField setEnabled:NO];
[replnameTextField setEnabled:NO];
}
}
- (IBAction)chooseKeyPath:(id)sender
{
NSOpenPanel *tvarNSOpenPanelObj = [NSOpenPanel openPanel];
NSInteger tvarNSInteger = [tvarNSOpenPanelObj runModalForTypes:nil];
if(tvarNSInteger == NSOKButton){
NSLog(@"doOpen we have an OK button");
//NSString * tvarDirectory = [tvarNSOpenPanelObj directory];
//NSLog(@"doOpen directory = %@",tvarDirectory);
NSString * tvarFilename = [tvarNSOpenPanelObj filename];
NSLog(@"doOpen filename = %@",tvarFilename);
[sshkeyfileTextField setStringValue:tvarFilename];
} else if(tvarNSInteger == NSCancelButton) {
NSLog(@"doOpen we have a Cancel button");
return;
} else {
NSLog(@"doOpen tvarInt not equal 1 or zero = %3d",tvarNSInteger);
return;
} // end if
}
@end