Mercurial > 510Connectbot
annotate src/com/five_ten_sg/connectbot/bean/HostBean.java @ 107:afe6962769b9
use hash map rather than linear search thru arrays
author | Carl Byington <carl@five-ten-sg.com> |
---|---|
date | Wed, 18 Jun 2014 11:43:08 -0700 |
parents | 1c22e6a7efff |
children | 6969d6cebcd7 |
rev | line source |
---|---|
0 | 1 /* |
2 * ConnectBot: simple, powerful, open-source SSH client for Android | |
3 * Copyright 2007 Kenny Root, Jeffrey Sharkey | |
4 * | |
5 * Licensed under the Apache License, Version 2.0 (the "License"); | |
6 * you may not use this file except in compliance with the License. | |
7 * You may obtain a copy of the License at | |
8 * | |
9 * http://www.apache.org/licenses/LICENSE-2.0 | |
10 * | |
11 * Unless required by applicable law or agreed to in writing, software | |
12 * distributed under the License is distributed on an "AS IS" BASIS, | |
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
14 * See the License for the specific language governing permissions and | |
15 * limitations under the License. | |
16 */ | |
17 | |
18 package com.five_ten_sg.connectbot.bean; | |
19 | |
20 import com.five_ten_sg.connectbot.util.HostDatabase; | |
21 import android.content.ContentValues; | |
22 import android.net.Uri; | |
23 | |
24 /** | |
25 * @author Kenny Root | |
26 * | |
27 */ | |
28 public class HostBean extends AbstractBean { | |
29 public static final String BEAN_NAME = "host"; | |
30 | |
31 /* Database fields */ | |
61 | 32 private long id = -1; |
33 private String nickname = null; | |
34 private String username = null; | |
35 private String hostname = null; | |
36 private int port = 22; | |
37 private String protocol = "ssh"; | |
38 private String hostKeyAlgo = null; | |
39 private byte[] hostKey = null; | |
40 private long lastConnect = -1; | |
41 private String color; | |
0 | 42 private boolean useKeys = true; |
61 | 43 private String useAuthAgent = HostDatabase.AUTHAGENT_NO; |
44 private String postLogin = null; | |
45 private long pubkeyId = -1; | |
0 | 46 private boolean wantSession = true; |
61 | 47 private String delKey = HostDatabase.DELKEY_DEL; |
48 private int fontSize = -1; | |
0 | 49 private boolean compression = false; |
61 | 50 private String httpproxy = null; |
51 private String encoding = HostDatabase.ENCODING_DEFAULT; | |
0 | 52 private boolean stayConnected = false; |
53 private boolean wantX11Forward = false; | |
61 | 54 private String x11Host = "localhost"; |
55 private int x11Port = 6000; | |
56 private String monitor = null; | |
57 private String hostemulation = null; | |
58 private String encryption5250 = null; | |
0 | 59 |
60 public HostBean() { | |
61 } | |
62 | |
63 @Override | |
64 public String getBeanName() { | |
65 return BEAN_NAME; | |
66 } | |
67 | |
68 public HostBean(String nickname, String protocol, String username, String hostname, int port) { | |
69 this.nickname = nickname; | |
70 this.protocol = protocol; | |
71 this.username = username; | |
72 this.hostname = hostname; | |
73 this.port = port; | |
74 } | |
75 | |
83
4ccfde0bc506
disable host config items that are not applicable to the host protocol
Carl Byington <carl@five-ten-sg.com>
parents:
63
diff
changeset
|
76 public boolean isSSH() { |
88
1c22e6a7efff
convert ctrl keys to virtual keys; use proper android home directory
Carl Byington <carl@five-ten-sg.com>
parents:
83
diff
changeset
|
77 return protocol.equals("ssh"); |
83
4ccfde0bc506
disable host config items that are not applicable to the host protocol
Carl Byington <carl@five-ten-sg.com>
parents:
63
diff
changeset
|
78 } |
4ccfde0bc506
disable host config items that are not applicable to the host protocol
Carl Byington <carl@five-ten-sg.com>
parents:
63
diff
changeset
|
79 |
4ccfde0bc506
disable host config items that are not applicable to the host protocol
Carl Byington <carl@five-ten-sg.com>
parents:
63
diff
changeset
|
80 public boolean is5250() { |
88
1c22e6a7efff
convert ctrl keys to virtual keys; use proper android home directory
Carl Byington <carl@five-ten-sg.com>
parents:
83
diff
changeset
|
81 return protocol.equals("tn5250"); |
83
4ccfde0bc506
disable host config items that are not applicable to the host protocol
Carl Byington <carl@five-ten-sg.com>
parents:
63
diff
changeset
|
82 } |
4ccfde0bc506
disable host config items that are not applicable to the host protocol
Carl Byington <carl@five-ten-sg.com>
parents:
63
diff
changeset
|
83 |
4ccfde0bc506
disable host config items that are not applicable to the host protocol
Carl Byington <carl@five-ten-sg.com>
parents:
63
diff
changeset
|
84 public boolean isTelnet() { |
88
1c22e6a7efff
convert ctrl keys to virtual keys; use proper android home directory
Carl Byington <carl@five-ten-sg.com>
parents:
83
diff
changeset
|
85 return protocol.equals("telnet"); |
83
4ccfde0bc506
disable host config items that are not applicable to the host protocol
Carl Byington <carl@five-ten-sg.com>
parents:
63
diff
changeset
|
86 } |
4ccfde0bc506
disable host config items that are not applicable to the host protocol
Carl Byington <carl@five-ten-sg.com>
parents:
63
diff
changeset
|
87 |
4ccfde0bc506
disable host config items that are not applicable to the host protocol
Carl Byington <carl@five-ten-sg.com>
parents:
63
diff
changeset
|
88 public boolean isAsync() { |
4ccfde0bc506
disable host config items that are not applicable to the host protocol
Carl Byington <carl@five-ten-sg.com>
parents:
63
diff
changeset
|
89 return isSSH() || isTelnet(); |
4ccfde0bc506
disable host config items that are not applicable to the host protocol
Carl Byington <carl@five-ten-sg.com>
parents:
63
diff
changeset
|
90 } |
4ccfde0bc506
disable host config items that are not applicable to the host protocol
Carl Byington <carl@five-ten-sg.com>
parents:
63
diff
changeset
|
91 |
0 | 92 public void setId(long id) { |
93 this.id = id; | |
94 } | |
95 public long getId() { | |
96 return id; | |
97 } | |
98 public void setNickname(String nickname) { | |
99 this.nickname = nickname; | |
100 } | |
101 public String getNickname() { | |
102 return nickname; | |
103 } | |
104 public void setUsername(String username) { | |
105 this.username = username; | |
106 } | |
107 public String getUsername() { | |
108 return username; | |
109 } | |
110 public void setHostname(String hostname) { | |
111 this.hostname = hostname; | |
112 } | |
113 public String getHostname() { | |
114 return hostname; | |
115 } | |
116 public void setPort(int port) { | |
117 this.port = port; | |
118 } | |
119 public int getPort() { | |
120 return port; | |
121 } | |
122 | |
123 public void setProtocol(String protocol) { | |
124 this.protocol = protocol; | |
125 } | |
126 | |
127 public String getProtocol() { | |
128 return protocol; | |
129 } | |
130 | |
131 public void setHostKeyAlgo(String hostKeyAlgo) { | |
132 this.hostKeyAlgo = hostKeyAlgo; | |
133 } | |
134 public String getHostKeyAlgo() { | |
135 return hostKeyAlgo; | |
136 } | |
137 public void setHostKey(byte[] hostKey) { | |
138 if (hostKey == null) | |
139 this.hostKey = null; | |
140 else | |
141 this.hostKey = hostKey.clone(); | |
142 } | |
143 public byte[] getHostKey() { | |
144 if (hostKey == null) | |
145 return null; | |
146 else | |
147 return hostKey.clone(); | |
148 } | |
149 public void setLastConnect(long lastConnect) { | |
150 this.lastConnect = lastConnect; | |
151 } | |
152 public long getLastConnect() { | |
153 return lastConnect; | |
154 } | |
155 public void setColor(String color) { | |
156 this.color = color; | |
157 } | |
158 public String getColor() { | |
159 return color; | |
160 } | |
161 public void setUseKeys(boolean useKeys) { | |
162 this.useKeys = useKeys; | |
163 } | |
164 public boolean getUseKeys() { | |
165 return useKeys; | |
166 } | |
167 public void setUseAuthAgent(String useAuthAgent) { | |
168 this.useAuthAgent = useAuthAgent; | |
169 } | |
170 public String getUseAuthAgent() { | |
171 return useAuthAgent; | |
172 } | |
173 public void setPostLogin(String postLogin) { | |
174 this.postLogin = postLogin; | |
175 } | |
176 public String getPostLogin() { | |
177 return postLogin; | |
178 } | |
179 public void setPubkeyId(long pubkeyId) { | |
180 this.pubkeyId = pubkeyId; | |
181 } | |
182 public long getPubkeyId() { | |
183 return pubkeyId; | |
184 } | |
185 public void setWantSession(boolean wantSession) { | |
186 this.wantSession = wantSession; | |
187 } | |
188 public boolean getWantSession() { | |
189 return wantSession; | |
190 } | |
191 public void setDelKey(String delKey) { | |
192 this.delKey = delKey; | |
193 } | |
194 public String getDelKey() { | |
195 return delKey; | |
196 } | |
197 public void setFontSize(int fontSize) { | |
198 this.fontSize = fontSize; | |
199 } | |
200 public int getFontSize() { | |
201 return fontSize; | |
202 } | |
203 public void setCompression(boolean compression) { | |
204 this.compression = compression; | |
205 } | |
206 public boolean getCompression() { | |
207 return compression; | |
208 } | |
209 public void setHttpproxy(String httpproxy) { | |
210 this.httpproxy = httpproxy; | |
211 } | |
212 public String getHttpproxy() { | |
213 return httpproxy; | |
214 } | |
215 | |
216 public void setEncoding(String encoding) { | |
217 this.encoding = encoding; | |
218 } | |
219 | |
220 public String getEncoding() { | |
221 return this.encoding; | |
222 } | |
223 | |
224 public void setStayConnected(boolean stayConnected) { | |
225 this.stayConnected = stayConnected; | |
226 } | |
227 | |
228 public boolean getStayConnected() { | |
229 return stayConnected; | |
230 } | |
231 | |
232 public String getDescription() { | |
233 String description = String.format("%s@%s", username, hostname); | |
234 | |
235 if (port != 22) | |
236 description += String.format(":%d", port); | |
237 | |
238 return description; | |
239 } | |
240 | |
241 public boolean getWantX11Forward() { | |
242 return this.wantX11Forward; | |
243 } | |
244 | |
245 public void setWantX11Forward(boolean wantX11Forward) { | |
246 this.wantX11Forward = wantX11Forward; | |
247 } | |
248 | |
249 public String getX11Host() { | |
250 return this.x11Host; | |
251 } | |
252 | |
253 public void setX11Host(String x11Host) { | |
254 this.x11Host = x11Host; | |
255 } | |
256 | |
257 public int getX11Port() { | |
258 return this.x11Port; | |
259 } | |
260 | |
261 public void setX11Port(int x11Port) { | |
262 this.x11Port = x11Port; | |
263 } | |
264 | |
265 public String getMonitor() { | |
266 return this.monitor; | |
267 } | |
268 | |
269 public void setMonitor(String monitor) { | |
270 this.monitor = monitor; | |
271 } | |
272 | |
61 | 273 public String getHostEmulation() { |
274 return this.hostemulation; | |
275 } | |
276 | |
277 public void setHostEmulation(String hostemulation) { | |
278 this.hostemulation = hostemulation; | |
279 } | |
280 | |
281 public String getEncryption5250() { | |
282 return this.encryption5250; | |
283 } | |
284 | |
285 public void setEncryption5250(String encryption5250) { | |
286 this.encryption5250 = encryption5250; | |
287 } | |
288 | |
0 | 289 @Override |
290 public ContentValues getValues() { | |
291 ContentValues values = new ContentValues(); | |
292 values.put(HostDatabase.FIELD_HOST_NICKNAME, nickname); | |
293 values.put(HostDatabase.FIELD_HOST_PROTOCOL, protocol); | |
294 values.put(HostDatabase.FIELD_HOST_USERNAME, username); | |
295 values.put(HostDatabase.FIELD_HOST_HOSTNAME, hostname); | |
296 values.put(HostDatabase.FIELD_HOST_PORT, port); | |
297 values.put(HostDatabase.FIELD_HOST_HOSTKEYALGO, hostKeyAlgo); | |
298 values.put(HostDatabase.FIELD_HOST_HOSTKEY, hostKey); | |
299 values.put(HostDatabase.FIELD_HOST_LASTCONNECT, lastConnect); | |
300 values.put(HostDatabase.FIELD_HOST_COLOR, color); | |
301 values.put(HostDatabase.FIELD_HOST_USEKEYS, Boolean.toString(useKeys)); | |
302 values.put(HostDatabase.FIELD_HOST_USEAUTHAGENT, useAuthAgent); | |
303 values.put(HostDatabase.FIELD_HOST_POSTLOGIN, postLogin); | |
304 values.put(HostDatabase.FIELD_HOST_PUBKEYID, pubkeyId); | |
305 values.put(HostDatabase.FIELD_HOST_WANTSESSION, Boolean.toString(wantSession)); | |
306 values.put(HostDatabase.FIELD_HOST_DELKEY, delKey); | |
307 values.put(HostDatabase.FIELD_HOST_FONTSIZE, fontSize); | |
308 values.put(HostDatabase.FIELD_HOST_COMPRESSION, Boolean.toString(compression)); | |
309 values.put(HostDatabase.FIELD_HOST_HTTPPROXY, httpproxy); | |
310 values.put(HostDatabase.FIELD_HOST_ENCODING, encoding); | |
311 values.put(HostDatabase.FIELD_HOST_STAYCONNECTED, stayConnected); | |
312 values.put(HostDatabase.FIELD_HOST_WANTX11FORWARD, wantX11Forward); | |
313 values.put(HostDatabase.FIELD_HOST_X11HOST, x11Host); | |
314 values.put(HostDatabase.FIELD_HOST_X11PORT, x11Port); | |
315 values.put(HostDatabase.FIELD_HOST_MONITOR, monitor); | |
63
07c7055cc124
add 5250 config entries
Carl Byington <carl@five-ten-sg.com>
parents:
61
diff
changeset
|
316 values.put(HostDatabase.FIELD_HOST_EMULATION, hostemulation); |
61 | 317 values.put(HostDatabase.FIELD_HOST_ENCRYPTION5250, encryption5250); |
0 | 318 return values; |
319 } | |
320 | |
321 @Override | |
322 public boolean equals(Object o) { | |
323 if (o == null || !(o instanceof HostBean)) | |
324 return false; | |
325 | |
326 HostBean host = (HostBean)o; | |
327 | |
328 if (id != -1 && host.getId() != -1) | |
329 return host.getId() == id; | |
330 | |
331 if (nickname == null) { | |
332 if (host.getNickname() != null) | |
333 return false; | |
334 } | |
335 else if (!nickname.equals(host.getNickname())) | |
336 return false; | |
337 | |
338 if (protocol == null) { | |
339 if (host.getProtocol() != null) | |
340 return false; | |
341 } | |
342 else if (!protocol.equals(host.getProtocol())) | |
343 return false; | |
344 | |
345 if (username == null) { | |
346 if (host.getUsername() != null) | |
347 return false; | |
348 } | |
349 else if (!username.equals(host.getUsername())) | |
350 return false; | |
351 | |
352 if (hostname == null) { | |
353 if (host.getHostname() != null) | |
354 return false; | |
355 } | |
356 else if (!hostname.equals(host.getHostname())) | |
357 return false; | |
358 | |
359 if (port != host.getPort()) | |
360 return false; | |
361 | |
362 return true; | |
363 } | |
364 | |
365 @Override | |
366 public int hashCode() { | |
367 int hash = 7; | |
368 | |
369 if (id != -1) | |
370 return (int)id; | |
371 | |
372 hash = 31 * hash + (null == nickname ? 0 : nickname.hashCode()); | |
373 hash = 31 * hash + (null == protocol ? 0 : protocol.hashCode()); | |
374 hash = 31 * hash + (null == username ? 0 : username.hashCode()); | |
375 hash = 31 * hash + (null == hostname ? 0 : hostname.hashCode()); | |
376 hash = 31 * hash + port; | |
377 return hash; | |
378 } | |
379 | |
380 /** | |
381 * @return URI identifying this HostBean | |
382 */ | |
383 public Uri getUri() { | |
384 StringBuilder sb = new StringBuilder(); | |
385 sb.append(protocol) | |
386 .append("://"); | |
387 | |
388 if (username != null) | |
389 sb.append(Uri.encode(username)) | |
390 .append('@'); | |
391 | |
392 sb.append(Uri.encode(hostname)) | |
393 .append(':') | |
394 .append(port) | |
395 .append("/#") | |
396 .append(nickname); | |
397 return Uri.parse(sb.toString()); | |
398 } | |
399 | |
400 } |