/
AddConnectionController.m
190 lines (177 loc) · 6.16 KB
/
AddConnectionController.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
//
// AddConnectionController.m
// MongoHub
//
// Created by Syd on 10-4-24.
// Copyright 2010 MusicPeace.ORG. All rights reserved.
//
#import "Configure.h"
#import "AddConnectionController.h"
#import "ConnectionsArrayController.h"
@implementation AddConnectionController
@synthesize hostTextField;
@synthesize hostportTextField;
@synthesize aliasTextField;
@synthesize adminuserTextField;
@synthesize adminpassTextField;
@synthesize usesshCheckBox;
@synthesize bindaddressTextField;
@synthesize bindportTextField;
@synthesize sshhostTextField;
@synthesize sshportTextField;
@synthesize sshuserTextField;
@synthesize sshpasswordTextField;
@synthesize connectionInfo;
@synthesize connectionsArrayController;
@synthesize managedObjectContext;
- (id)init {
if (![super initWithWindowNibName:@"NewConnection"]) return nil;
return self;
}
- (void)dealloc {
[hostTextField release];
[hostportTextField release];
[aliasTextField release];
[adminuserTextField release];
[adminpassTextField release];
[usesshCheckBox release];
[bindaddressTextField release];
[bindportTextField release];
[sshhostTextField release];
[sshportTextField release];
[sshuserTextField release];
[sshpasswordTextField release];
[connectionInfo release];
[connectionsArrayController release];
[managedObjectContext release];
[super dealloc];
}
- (void)windowDidLoad {
//NSLog(@"New Connection Window Loaded");
[super windowDidLoad];
}
- (void)windowWillClose:(NSNotification *)notification {
[[NSNotificationCenter defaultCenter] postNotificationName:kNewConnectionWindowWillClose object:connectionInfo];
}
- (IBAction)cancel:(id)sender {
connectionInfo = nil;
[self close];
}
- (IBAction)add:(id)sender {
NSString *host;
NSUInteger hostport;
NSString *alias;
NSString *adminuser = [adminuserTextField stringValue];
NSString *adminpass = [adminpassTextField stringValue];
NSUInteger usessh = 0;
NSString *bindaddress;
NSUInteger bindport;
NSString *sshhost;
NSUInteger sshport;
NSString *sshuser;
NSString *sshpassword;
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];
}
if ([ [aliasTextField stringValue] length] == 0) {
alias = [[NSString alloc] initWithString:@"localhost"];
}else{
alias = [[NSString alloc] initWithString:[aliasTextField stringValue]];
}
if ([usesshCheckBox state])
{
usessh = 1;
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]];
}else{
bindaddress = @"127.0.0.1";
bindport = 8888;
sshhost = @"";
sshport = 22;
sshuser = @"";
sshpassword = @"";
}
NSArray *keys = [[NSArray alloc] initWithObjects:@"host", @"hostport", @"alias", @"adminuser", @"adminpass", @"usessh", @"bindaddress", @"bindport", @"sshhost", @"sshport", @"sshuser", @"sshpassword", nil];
NSArray *objs = [[NSArray alloc] initWithObjects:host, [NSNumber numberWithInt:hostport], alias, adminuser, adminpass, [NSNumber numberWithInt:usessh], bindaddress, [NSNumber numberWithInt:bindport], sshhost, [NSNumber numberWithInt:sshport], sshuser, sshpassword, nil];
if (!connectionInfo) {
connectionInfo = [[NSMutableDictionary alloc] initWithCapacity:12];
}
connectionInfo = [NSMutableDictionary dictionaryWithObjects:objs forKeys:keys];
[keys release];
[objs release];
[host release];
[alias release];
if (usessh == 1)
{
[sshhost release];
[sshuser release];
[sshpassword release];
[bindaddress release];
}
if ([self validateConnection]) {
[self close];
}
}
- (BOOL)validateConnection
{
if ([[connectionInfo objectForKey:@"host"] length] == 0) {
NSRunAlertPanel(@"Error", @"Connection host should not be empty", @"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 ([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;
}
return YES;
}
- (IBAction)enableSSH:(id)sender
{
if ([usesshCheckBox state] == 1)
{
[bindaddressTextField setEnabled:YES];
[bindportTextField setEnabled:YES];
[sshhostTextField setEnabled:YES];
[sshuserTextField setEnabled:YES];
[sshpasswordTextField setEnabled:YES];
[sshportTextField setEnabled:YES];
}else {
[bindaddressTextField setEnabled:NO];
[bindportTextField setEnabled:NO];
[sshhostTextField setEnabled:NO];
[sshuserTextField setEnabled:NO];
[sshpasswordTextField setEnabled:NO];
[sshportTextField setEnabled:NO];
}
}
@end