Mercurial > 510Connectbot
annotate src/com/five_ten_sg/connectbot/util/HostDatabase.java @ 92:e5259eab903e
remove 5250 configuration
author | Carl Byington <carl@five-ten-sg.com> |
---|---|
date | Mon, 16 Jun 2014 16:22:34 -0700 |
parents | 07c7055cc124 |
children | 446dbcf606eb |
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.util; | |
19 | |
20 import java.nio.charset.Charset; | |
21 import java.util.Iterator; | |
22 import java.util.LinkedList; | |
23 import java.util.List; | |
24 import java.util.Map; | |
25 import java.util.Map.Entry; | |
26 | |
27 import com.five_ten_sg.connectbot.bean.HostBean; | |
28 import com.five_ten_sg.connectbot.bean.PortForwardBean; | |
29 import android.content.ContentValues; | |
30 import android.content.Context; | |
31 import android.database.Cursor; | |
32 import android.database.sqlite.SQLiteDatabase; | |
33 import android.database.sqlite.SQLiteException; | |
34 import android.util.Log; | |
35 | |
36 import com.trilead.ssh2.KnownHosts; | |
37 | |
38 /** | |
39 * Contains information about various SSH hosts, include public hostkey if known | |
40 * from previous sessions. | |
41 * | |
42 * @author jsharkey | |
43 */ | |
44 public class HostDatabase extends RobustSQLiteOpenHelper { | |
45 | |
46 public final static String TAG = "ConnectBot.HostDatabase"; | |
47 | |
48 public final static String DB_NAME = "hosts"; | |
62
aac8e0496ef9
add 5250 config entries
Carl Byington <carl@five-ten-sg.com>
parents:
61
diff
changeset
|
49 public final static int DB_VERSION = 26; |
0 | 50 |
51 public final static String TABLE_HOSTS = "hosts"; | |
52 public final static String FIELD_HOST_NICKNAME = "nickname"; | |
53 public final static String FIELD_HOST_PROTOCOL = "protocol"; | |
54 public final static String FIELD_HOST_USERNAME = "username"; | |
55 public final static String FIELD_HOST_HOSTNAME = "hostname"; | |
56 public final static String FIELD_HOST_PORT = "port"; | |
57 public final static String FIELD_HOST_HOSTKEYALGO = "hostkeyalgo"; | |
58 public final static String FIELD_HOST_HOSTKEY = "hostkey"; | |
59 public final static String FIELD_HOST_LASTCONNECT = "lastconnect"; | |
60 public final static String FIELD_HOST_COLOR = "color"; | |
61 public final static String FIELD_HOST_USEKEYS = "usekeys"; | |
62 public final static String FIELD_HOST_USEAUTHAGENT = "useauthagent"; | |
63 public final static String FIELD_HOST_POSTLOGIN = "postlogin"; | |
64 public final static String FIELD_HOST_PUBKEYID = "pubkeyid"; | |
65 public final static String FIELD_HOST_WANTSESSION = "wantsession"; | |
66 public final static String FIELD_HOST_DELKEY = "delkey"; | |
67 public final static String FIELD_HOST_FONTSIZE = "fontsize"; | |
68 public final static String FIELD_HOST_COMPRESSION = "compression"; | |
69 public final static String FIELD_HOST_HTTPPROXY = "httpproxy"; | |
70 public final static String FIELD_HOST_ENCODING = "encoding"; | |
71 public final static String FIELD_HOST_STAYCONNECTED = "stayconnected"; | |
72 public final static String FIELD_HOST_WANTX11FORWARD = "wantx11forward"; | |
73 public final static String FIELD_HOST_X11HOST = "x11host"; | |
74 public final static String FIELD_HOST_X11PORT = "x11port"; | |
75 public final static String FIELD_HOST_MONITOR = "monitor"; | |
61 | 76 public final static String FIELD_HOST_EMULATION = "emulation"; |
77 public final static String FIELD_HOST_ENCRYPTION5250 = "encryption5250"; | |
0 | 78 |
79 public final static String TABLE_PORTFORWARDS = "portforwards"; | |
80 public final static String FIELD_PORTFORWARD_HOSTID = "hostid"; | |
81 public final static String FIELD_PORTFORWARD_NICKNAME = "nickname"; | |
82 public final static String FIELD_PORTFORWARD_TYPE = "type"; | |
83 public final static String FIELD_PORTFORWARD_SOURCEPORT = "sourceport"; | |
84 public final static String FIELD_PORTFORWARD_DESTADDR = "destaddr"; | |
85 public final static String FIELD_PORTFORWARD_DESTPORT = "destport"; | |
86 | |
87 public final static String TABLE_COLORS = "colors"; | |
88 public final static String FIELD_COLOR_SCHEME = "scheme"; | |
89 public final static String FIELD_COLOR_NUMBER = "number"; | |
90 public final static String FIELD_COLOR_VALUE = "value"; | |
91 | |
92 public final static String TABLE_COLOR_DEFAULTS = "colorDefaults"; | |
93 public final static String FIELD_COLOR_FG = "fg"; | |
94 public final static String FIELD_COLOR_BG = "bg"; | |
95 | |
96 public final static int DEFAULT_FG_COLOR = 7; | |
97 public final static int DEFAULT_BG_COLOR = 0; | |
98 | |
99 public final static String COLOR_RED = "red"; | |
100 public final static String COLOR_GREEN = "green"; | |
101 public final static String COLOR_BLUE = "blue"; | |
102 public final static String COLOR_GRAY = "gray"; | |
103 | |
104 public final static String PORTFORWARD_LOCAL = "local"; | |
105 public final static String PORTFORWARD_REMOTE = "remote"; | |
106 public final static String PORTFORWARD_DYNAMIC4 = "dynamic4"; | |
107 public final static String PORTFORWARD_DYNAMIC5 = "dynamic5"; | |
108 | |
109 public final static String DELKEY_DEL = "del"; | |
110 public final static String DELKEY_BACKSPACE = "backspace"; | |
111 | |
112 public final static String AUTHAGENT_NO = "no"; | |
113 public final static String AUTHAGENT_CONFIRM = "confirm"; | |
114 public final static String AUTHAGENT_YES = "yes"; | |
115 | |
116 public final static String ENCODING_DEFAULT = Charset.defaultCharset().name(); | |
117 | |
118 public final static String X11HOST_DEFAULT = "localhost"; | |
119 public final static int X11PORT_DEFAULT = 6000; | |
120 | |
121 public final static long PUBKEYID_NEVER = -2; | |
122 public final static long PUBKEYID_ANY = -1; | |
123 | |
124 public static final int DEFAULT_COLOR_SCHEME = 0; | |
125 | |
126 // Table creation strings | |
127 public static final String CREATE_TABLE_COLOR_DEFAULTS = | |
128 "CREATE TABLE " + TABLE_COLOR_DEFAULTS | |
129 + " (" + FIELD_COLOR_SCHEME + " INTEGER NOT NULL, " | |
130 + FIELD_COLOR_FG + " INTEGER NOT NULL DEFAULT " + DEFAULT_FG_COLOR + ", " | |
131 + FIELD_COLOR_BG + " INTEGER NOT NULL DEFAULT " + DEFAULT_BG_COLOR + ")"; | |
132 public static final String CREATE_TABLE_COLOR_DEFAULTS_INDEX = | |
133 "CREATE INDEX " + TABLE_COLOR_DEFAULTS + FIELD_COLOR_SCHEME + "index ON " | |
134 + TABLE_COLOR_DEFAULTS + " (" + FIELD_COLOR_SCHEME + ");"; | |
135 | |
136 private static final String WHERE_SCHEME_AND_COLOR = FIELD_COLOR_SCHEME + " = ? AND " | |
137 + FIELD_COLOR_NUMBER + " = ?"; | |
138 | |
139 static { | |
140 addTableName(TABLE_HOSTS); | |
141 addTableName(TABLE_PORTFORWARDS); | |
142 addIndexName(TABLE_PORTFORWARDS + FIELD_PORTFORWARD_HOSTID + "index"); | |
143 addTableName(TABLE_COLORS); | |
144 addIndexName(TABLE_COLORS + FIELD_COLOR_SCHEME + "index"); | |
145 addTableName(TABLE_COLOR_DEFAULTS); | |
146 addIndexName(TABLE_COLOR_DEFAULTS + FIELD_COLOR_SCHEME + "index"); | |
147 } | |
148 | |
149 public static final Object[] dbLock = new Object[0]; | |
150 | |
151 public HostDatabase(Context context) { | |
152 super(context, DB_NAME, null, DB_VERSION); | |
153 getWritableDatabase().close(); | |
154 } | |
155 | |
156 @Override | |
157 public void onCreate(SQLiteDatabase db) { | |
158 super.onCreate(db); | |
159 db.execSQL("CREATE TABLE " + TABLE_HOSTS | |
160 + " (_id INTEGER PRIMARY KEY, " | |
161 + FIELD_HOST_NICKNAME + " TEXT, " | |
162 + FIELD_HOST_PROTOCOL + " TEXT DEFAULT 'ssh', " | |
163 + FIELD_HOST_USERNAME + " TEXT, " | |
164 + FIELD_HOST_HOSTNAME + " TEXT, " | |
165 + FIELD_HOST_PORT + " INTEGER, " | |
166 + FIELD_HOST_HOSTKEYALGO + " TEXT, " | |
167 + FIELD_HOST_HOSTKEY + " BLOB, " | |
168 + FIELD_HOST_LASTCONNECT + " INTEGER, " | |
169 + FIELD_HOST_COLOR + " TEXT, " | |
170 + FIELD_HOST_USEKEYS + " TEXT, " | |
171 + FIELD_HOST_USEAUTHAGENT + " TEXT, " | |
172 + FIELD_HOST_POSTLOGIN + " TEXT, " | |
173 + FIELD_HOST_PUBKEYID + " INTEGER DEFAULT " + PUBKEYID_ANY + ", " | |
174 + FIELD_HOST_DELKEY + " TEXT DEFAULT '" + DELKEY_DEL + "', " | |
175 + FIELD_HOST_FONTSIZE + " INTEGER, " | |
176 + FIELD_HOST_WANTSESSION + " TEXT DEFAULT '" + Boolean.toString(true) + "', " | |
177 + FIELD_HOST_COMPRESSION + " TEXT DEFAULT '" + Boolean.toString(false) + "', " | |
178 + FIELD_HOST_HTTPPROXY + " TEXT, " | |
179 + FIELD_HOST_ENCODING + " TEXT DEFAULT '" + ENCODING_DEFAULT + "', " | |
180 + FIELD_HOST_STAYCONNECTED + " TEXT, " | |
181 + FIELD_HOST_WANTX11FORWARD + " TEXT DEFAULT '" + Boolean.toString(false) + "', " | |
182 + FIELD_HOST_X11HOST + " TEXT DEFAULT '" + X11HOST_DEFAULT + "', " | |
183 + FIELD_HOST_X11PORT + " INTEGER DEFAULT " + X11PORT_DEFAULT + ", " | |
61 | 184 + FIELD_HOST_MONITOR + " TEXT, " |
185 + FIELD_HOST_EMULATION + " TEXT, " | |
186 + FIELD_HOST_ENCRYPTION5250 + " TEXT)"); | |
0 | 187 db.execSQL("CREATE TABLE " + TABLE_PORTFORWARDS |
188 + " (_id INTEGER PRIMARY KEY, " | |
189 + FIELD_PORTFORWARD_HOSTID + " INTEGER, " | |
190 + FIELD_PORTFORWARD_NICKNAME + " TEXT, " | |
191 + FIELD_PORTFORWARD_TYPE + " TEXT NOT NULL DEFAULT " + PORTFORWARD_LOCAL + ", " | |
192 + FIELD_PORTFORWARD_SOURCEPORT + " INTEGER NOT NULL DEFAULT 8080, " | |
193 + FIELD_PORTFORWARD_DESTADDR + " TEXT, " | |
194 + FIELD_PORTFORWARD_DESTPORT + " TEXT)"); | |
195 db.execSQL("CREATE INDEX " + TABLE_PORTFORWARDS + FIELD_PORTFORWARD_HOSTID + "index ON " | |
196 + TABLE_PORTFORWARDS + " (" + FIELD_PORTFORWARD_HOSTID + ");"); | |
197 db.execSQL("CREATE TABLE " + TABLE_COLORS | |
198 + " (_id INTEGER PRIMARY KEY, " | |
199 + FIELD_COLOR_NUMBER + " INTEGER, " | |
200 + FIELD_COLOR_VALUE + " INTEGER, " | |
201 + FIELD_COLOR_SCHEME + " INTEGER)"); | |
202 db.execSQL("CREATE INDEX " + TABLE_COLORS + FIELD_COLOR_SCHEME + "index ON " | |
203 + TABLE_COLORS + " (" + FIELD_COLOR_SCHEME + ");"); | |
204 db.execSQL(CREATE_TABLE_COLOR_DEFAULTS); | |
205 db.execSQL(CREATE_TABLE_COLOR_DEFAULTS_INDEX); | |
206 } | |
207 | |
208 @Override | |
209 public void onRobustUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) throws SQLiteException { | |
210 // Versions of the database before the Android Market release will be | |
211 // shot without warning. | |
212 if (oldVersion <= 9) { | |
213 db.execSQL("DROP TABLE IF EXISTS " + TABLE_HOSTS); | |
214 onCreate(db); | |
215 return; | |
216 } | |
217 | |
218 switch (oldVersion) { | |
219 case 10: | |
220 db.execSQL("ALTER TABLE " + TABLE_HOSTS | |
221 + " ADD COLUMN " + FIELD_HOST_PUBKEYID + " INTEGER DEFAULT " + PUBKEYID_ANY); | |
222 | |
223 case 11: | |
224 db.execSQL("CREATE TABLE " + TABLE_PORTFORWARDS | |
225 + " (_id INTEGER PRIMARY KEY, " | |
226 + FIELD_PORTFORWARD_HOSTID + " INTEGER, " | |
227 + FIELD_PORTFORWARD_NICKNAME + " TEXT, " | |
228 + FIELD_PORTFORWARD_TYPE + " TEXT NOT NULL DEFAULT " + PORTFORWARD_LOCAL + ", " | |
229 + FIELD_PORTFORWARD_SOURCEPORT + " INTEGER NOT NULL DEFAULT 8080, " | |
230 + FIELD_PORTFORWARD_DESTADDR + " TEXT, " | |
231 + FIELD_PORTFORWARD_DESTPORT + " INTEGER)"); | |
232 | |
233 case 12: | |
234 db.execSQL("ALTER TABLE " + TABLE_HOSTS | |
235 + " ADD COLUMN " + FIELD_HOST_WANTSESSION + " TEXT DEFAULT '" + Boolean.toString(true) + "'"); | |
236 | |
237 case 13: | |
238 db.execSQL("ALTER TABLE " + TABLE_HOSTS | |
239 + " ADD COLUMN " + FIELD_HOST_COMPRESSION + " TEXT DEFAULT '" + Boolean.toString(false) + "'"); | |
240 | |
241 case 14: | |
242 db.execSQL("ALTER TABLE " + TABLE_HOSTS | |
243 + " ADD COLUMN " + FIELD_HOST_ENCODING + " TEXT DEFAULT '" + ENCODING_DEFAULT + "'"); | |
244 | |
245 case 15: | |
246 db.execSQL("ALTER TABLE " + TABLE_HOSTS | |
247 + " ADD COLUMN " + FIELD_HOST_PROTOCOL + " TEXT DEFAULT 'ssh'"); | |
248 | |
249 case 16: | |
250 db.execSQL("ALTER TABLE " + TABLE_HOSTS | |
251 + " ADD COLUMN " + FIELD_HOST_DELKEY + " TEXT DEFAULT '" + DELKEY_DEL + "'"); | |
252 | |
253 case 17: | |
254 db.execSQL("CREATE INDEX " + TABLE_PORTFORWARDS + FIELD_PORTFORWARD_HOSTID + "index ON " | |
255 + TABLE_PORTFORWARDS + " (" + FIELD_PORTFORWARD_HOSTID + ");"); | |
256 // Add colors | |
257 db.execSQL("CREATE TABLE " + TABLE_COLORS | |
258 + " (_id INTEGER PRIMARY KEY, " | |
259 + FIELD_COLOR_NUMBER + " INTEGER, " | |
260 + FIELD_COLOR_VALUE + " INTEGER, " | |
261 + FIELD_COLOR_SCHEME + " INTEGER)"); | |
262 db.execSQL("CREATE INDEX " + TABLE_COLORS + FIELD_COLOR_SCHEME + "index ON " | |
263 + TABLE_COLORS + " (" + FIELD_COLOR_SCHEME + ");"); | |
264 | |
265 case 18: | |
266 db.execSQL("ALTER TABLE " + TABLE_HOSTS | |
267 + " ADD COLUMN " + FIELD_HOST_USEAUTHAGENT + " TEXT DEFAULT '" + AUTHAGENT_NO + "'"); | |
268 | |
269 case 19: | |
270 db.execSQL("ALTER TABLE " + TABLE_HOSTS | |
271 + " ADD COLUMN " + FIELD_HOST_STAYCONNECTED + " TEXT"); | |
272 | |
273 case 20: | |
274 db.execSQL("ALTER TABLE " + TABLE_HOSTS | |
275 + " ADD COLUMN " + FIELD_HOST_FONTSIZE + " INTEGER"); | |
276 | |
277 case 21: | |
278 db.execSQL("DROP TABLE " + TABLE_COLOR_DEFAULTS); | |
279 db.execSQL(CREATE_TABLE_COLOR_DEFAULTS); | |
280 db.execSQL(CREATE_TABLE_COLOR_DEFAULTS_INDEX); | |
281 | |
282 case 22: | |
283 db.execSQL("ALTER TABLE " + TABLE_HOSTS | |
284 + " ADD COLUMN " + FIELD_HOST_WANTX11FORWARD + " TEXT DEFAULT '" + Boolean.toString(false) + "'"); | |
285 db.execSQL("ALTER TABLE " + TABLE_HOSTS | |
286 + " ADD COLUMN " + FIELD_HOST_X11HOST + " TEXT DEFAULT '" + X11HOST_DEFAULT + "'"); | |
287 db.execSQL("ALTER TABLE " + TABLE_HOSTS | |
288 + " ADD COLUMN " + FIELD_HOST_X11PORT + " INTEGER DEFAULT " + X11PORT_DEFAULT); | |
289 | |
290 case 23: | |
291 db.execSQL("ALTER TABLE " + TABLE_HOSTS | |
292 + " ADD COLUMN " + FIELD_HOST_HTTPPROXY + " TEXT"); | |
293 | |
294 case 24: | |
295 db.execSQL("ALTER TABLE " + TABLE_HOSTS | |
296 + " ADD COLUMN " + FIELD_HOST_MONITOR + " TEXT"); | |
61 | 297 |
298 case 25: | |
299 db.execSQL("ALTER TABLE " + TABLE_HOSTS | |
300 + " ADD COLUMN " + FIELD_HOST_EMULATION + " TEXT"); | |
301 db.execSQL("ALTER TABLE " + TABLE_HOSTS | |
302 + " ADD COLUMN " + FIELD_HOST_ENCRYPTION5250 + " TEXT"); | |
0 | 303 } |
304 } | |
305 | |
306 /** | |
307 * Touch a specific host to update its "last connected" field. | |
308 * @param nickname Nickname field of host to update | |
309 */ | |
310 public void touchHost(HostBean host) { | |
311 long now = System.currentTimeMillis() / 1000; | |
312 ContentValues values = new ContentValues(); | |
313 values.put(FIELD_HOST_LASTCONNECT, now); | |
314 | |
315 synchronized (dbLock) { | |
316 SQLiteDatabase db = this.getWritableDatabase(); | |
317 db.update(TABLE_HOSTS, values, "_id = ?", new String[] { String.valueOf(host.getId()) }); | |
318 } | |
319 } | |
320 | |
321 /** | |
322 * Create a new host using the given parameters. | |
323 */ | |
324 public HostBean saveHost(HostBean host) { | |
325 long id; | |
326 | |
327 synchronized (dbLock) { | |
328 SQLiteDatabase db = this.getWritableDatabase(); | |
329 id = db.insert(TABLE_HOSTS, null, host.getValues()); | |
330 } | |
331 | |
332 host.setId(id); | |
333 return host; | |
334 } | |
335 | |
336 /** | |
337 * Update a field in a host record. | |
338 */ | |
339 public boolean updateFontSize(HostBean host) { | |
340 long id = host.getId(); | |
341 | |
342 if (id < 0) | |
343 return false; | |
344 | |
345 ContentValues updates = new ContentValues(); | |
346 updates.put(FIELD_HOST_FONTSIZE, host.getFontSize()); | |
347 | |
348 synchronized (dbLock) { | |
349 SQLiteDatabase db = getWritableDatabase(); | |
350 db.update(TABLE_HOSTS, updates, "_id = ?", | |
351 new String[] { String.valueOf(id) }); | |
352 } | |
353 | |
354 return true; | |
355 } | |
356 | |
357 /** | |
358 * Delete a specific host by its <code>_id</code> value. | |
359 */ | |
360 public void deleteHost(HostBean host) { | |
361 if (host.getId() < 0) | |
362 return; | |
363 | |
364 synchronized (dbLock) { | |
365 SQLiteDatabase db = this.getWritableDatabase(); | |
366 db.delete(TABLE_HOSTS, "_id = ?", new String[] { String.valueOf(host.getId()) }); | |
367 } | |
368 } | |
369 | |
370 /** | |
371 * Return a cursor that contains information about all known hosts. | |
372 * @param sortColors If true, sort by color, otherwise sort by nickname. | |
373 */ | |
374 public List<HostBean> getHosts(boolean sortColors) { | |
375 String sortField = sortColors ? FIELD_HOST_COLOR : FIELD_HOST_NICKNAME; | |
376 List<HostBean> hosts; | |
377 | |
378 synchronized (dbLock) { | |
379 SQLiteDatabase db = this.getReadableDatabase(); | |
380 Cursor c = db.query(TABLE_HOSTS, null, null, null, null, null, sortField + " ASC"); | |
381 hosts = createHostBeans(c); | |
382 c.close(); | |
383 } | |
384 | |
385 return hosts; | |
386 } | |
387 | |
388 /** | |
389 * @param hosts | |
390 * @param c | |
391 */ | |
392 private List<HostBean> createHostBeans(Cursor c) { | |
393 List<HostBean> hosts = new LinkedList<HostBean>(); | |
394 final int COL_ID = c.getColumnIndexOrThrow("_id"), | |
395 COL_NICKNAME = c.getColumnIndexOrThrow(FIELD_HOST_NICKNAME), | |
396 COL_PROTOCOL = c.getColumnIndexOrThrow(FIELD_HOST_PROTOCOL), | |
397 COL_USERNAME = c.getColumnIndexOrThrow(FIELD_HOST_USERNAME), | |
398 COL_HOSTNAME = c.getColumnIndexOrThrow(FIELD_HOST_HOSTNAME), | |
399 COL_PORT = c.getColumnIndexOrThrow(FIELD_HOST_PORT), | |
400 COL_LASTCONNECT = c.getColumnIndexOrThrow(FIELD_HOST_LASTCONNECT), | |
401 COL_COLOR = c.getColumnIndexOrThrow(FIELD_HOST_COLOR), | |
402 COL_USEKEYS = c.getColumnIndexOrThrow(FIELD_HOST_USEKEYS), | |
403 COL_USEAUTHAGENT = c.getColumnIndexOrThrow(FIELD_HOST_USEAUTHAGENT), | |
404 COL_POSTLOGIN = c.getColumnIndexOrThrow(FIELD_HOST_POSTLOGIN), | |
405 COL_PUBKEYID = c.getColumnIndexOrThrow(FIELD_HOST_PUBKEYID), | |
406 COL_WANTSESSION = c.getColumnIndexOrThrow(FIELD_HOST_WANTSESSION), | |
407 COL_DELKEY = c.getColumnIndexOrThrow(FIELD_HOST_DELKEY), | |
408 COL_FONTSIZE = c.getColumnIndexOrThrow(FIELD_HOST_FONTSIZE), | |
409 COL_COMPRESSION = c.getColumnIndexOrThrow(FIELD_HOST_COMPRESSION), | |
410 COL_HTTPPROXY = c.getColumnIndexOrThrow(FIELD_HOST_HTTPPROXY), | |
411 COL_ENCODING = c.getColumnIndexOrThrow(FIELD_HOST_ENCODING), | |
412 COL_STAYCONNECTED = c.getColumnIndexOrThrow(FIELD_HOST_STAYCONNECTED), | |
413 COL_WANTX11FORWARD = c.getColumnIndexOrThrow(FIELD_HOST_WANTX11FORWARD), | |
414 COL_X11HOST = c.getColumnIndexOrThrow(FIELD_HOST_X11HOST), | |
415 COL_X11PORT = c.getColumnIndexOrThrow(FIELD_HOST_X11PORT), | |
61 | 416 COL_MONITOR = c.getColumnIndexOrThrow(FIELD_HOST_MONITOR), |
417 COL_EMULATION = c.getColumnIndexOrThrow(FIELD_HOST_EMULATION), | |
418 COL_ENCRYPTION5250 = c.getColumnIndexOrThrow(FIELD_HOST_ENCRYPTION5250); | |
0 | 419 |
420 while (c.moveToNext()) { | |
421 HostBean host = new HostBean(); | |
422 host.setId(c.getLong(COL_ID)); | |
423 host.setNickname(c.getString(COL_NICKNAME)); | |
424 host.setProtocol(c.getString(COL_PROTOCOL)); | |
425 host.setUsername(c.getString(COL_USERNAME)); | |
426 host.setHostname(c.getString(COL_HOSTNAME)); | |
427 host.setPort(c.getInt(COL_PORT)); | |
428 host.setLastConnect(c.getLong(COL_LASTCONNECT)); | |
429 host.setColor(c.getString(COL_COLOR)); | |
430 host.setUseKeys(Boolean.valueOf(c.getString(COL_USEKEYS))); | |
431 host.setUseAuthAgent(c.getString(COL_USEAUTHAGENT)); | |
432 host.setPostLogin(c.getString(COL_POSTLOGIN)); | |
433 host.setPubkeyId(c.getLong(COL_PUBKEYID)); | |
434 host.setWantSession(Boolean.valueOf(c.getString(COL_WANTSESSION))); | |
435 host.setDelKey(c.getString(COL_DELKEY)); | |
436 host.setFontSize(c.getInt(COL_FONTSIZE)); | |
437 host.setCompression(Boolean.valueOf(c.getString(COL_COMPRESSION))); | |
438 host.setHttpproxy(c.getString(COL_HTTPPROXY)); | |
439 host.setEncoding(c.getString(COL_ENCODING)); | |
440 host.setStayConnected(Boolean.valueOf(c.getString(COL_STAYCONNECTED))); | |
441 host.setWantX11Forward(Boolean.valueOf(c.getString(COL_WANTX11FORWARD))); | |
442 host.setX11Host(c.getString(COL_X11HOST)); | |
443 host.setX11Port(c.getInt(COL_X11PORT)); | |
444 host.setMonitor(c.getString(COL_MONITOR)); | |
63
07c7055cc124
add 5250 config entries
Carl Byington <carl@five-ten-sg.com>
parents:
62
diff
changeset
|
445 host.setHostEmulation(c.getString(COL_EMULATION)); |
61 | 446 host.setEncryption5250(c.getString(COL_ENCRYPTION5250)); |
0 | 447 hosts.add(host); |
448 } | |
449 | |
450 return hosts; | |
451 } | |
452 | |
453 /** | |
454 * @param c | |
455 * @return | |
456 */ | |
457 private HostBean getFirstHostBean(Cursor c) { | |
458 HostBean host = null; | |
459 List<HostBean> hosts = createHostBeans(c); | |
460 | |
461 if (hosts.size() > 0) | |
462 host = hosts.get(0); | |
463 | |
464 c.close(); | |
465 return host; | |
466 } | |
467 | |
468 /** | |
469 * @param nickname | |
470 * @param protocol | |
471 * @param username | |
472 * @param hostname | |
473 * @param hostname2 | |
474 * @param port | |
475 * @return | |
476 */ | |
477 public HostBean findHost(Map<String, String> selection) { | |
478 StringBuilder selectionBuilder = new StringBuilder(); | |
479 Iterator<Entry<String, String>> i = selection.entrySet().iterator(); | |
480 List<String> selectionValuesList = new LinkedList<String>(); | |
481 int n = 0; | |
482 | |
483 while (i.hasNext()) { | |
484 Entry<String, String> entry = i.next(); | |
485 | |
486 if (entry.getValue() == null) | |
487 continue; | |
488 | |
489 if (n++ > 0) | |
490 selectionBuilder.append(" AND "); | |
491 | |
492 selectionBuilder.append(entry.getKey()) | |
493 .append(" = ?"); | |
494 selectionValuesList.add(entry.getValue()); | |
495 } | |
496 | |
497 String selectionValues[] = new String[selectionValuesList.size()]; | |
498 selectionValuesList.toArray(selectionValues); | |
499 selectionValuesList = null; | |
500 HostBean host; | |
501 | |
502 synchronized (dbLock) { | |
503 SQLiteDatabase db = getReadableDatabase(); | |
504 Cursor c = db.query(TABLE_HOSTS, null, | |
505 selectionBuilder.toString(), | |
506 selectionValues, | |
507 null, null, null); | |
508 host = getFirstHostBean(c); | |
509 } | |
510 | |
511 return host; | |
512 } | |
513 | |
514 /** | |
515 * @param hostId | |
516 * @return | |
517 */ | |
518 public HostBean findHostById(long hostId) { | |
519 HostBean host; | |
520 | |
521 synchronized (dbLock) { | |
522 SQLiteDatabase db = getReadableDatabase(); | |
523 Cursor c = db.query(TABLE_HOSTS, null, | |
524 "_id = ?", new String[] { String.valueOf(hostId) }, | |
525 null, null, null); | |
526 host = getFirstHostBean(c); | |
527 } | |
528 | |
529 return host; | |
530 } | |
531 | |
532 /** | |
533 * Record the given hostkey into database under this nickname. | |
534 * @param hostname | |
535 * @param port | |
536 * @param hostkeyalgo | |
537 * @param hostkey | |
538 */ | |
539 public void saveKnownHost(String hostname, int port, String hostkeyalgo, byte[] hostkey) { | |
540 ContentValues values = new ContentValues(); | |
541 values.put(FIELD_HOST_HOSTKEYALGO, hostkeyalgo); | |
542 values.put(FIELD_HOST_HOSTKEY, hostkey); | |
543 | |
544 synchronized (dbLock) { | |
545 SQLiteDatabase db = getReadableDatabase(); | |
546 db.update(TABLE_HOSTS, values, | |
547 FIELD_HOST_HOSTNAME + " = ? AND " + FIELD_HOST_PORT + " = ?", | |
548 new String[] { hostname, String.valueOf(port) }); | |
549 Log.d(TAG, String.format("Finished saving hostkey information for '%s'", hostname)); | |
550 } | |
551 } | |
552 | |
553 /** | |
554 * Build list of known hosts for Trilead library. | |
555 * @return | |
556 */ | |
557 public KnownHosts getKnownHosts() { | |
558 KnownHosts known = new KnownHosts(); | |
559 | |
560 synchronized (dbLock) { | |
561 SQLiteDatabase db = this.getReadableDatabase(); | |
562 Cursor c = db.query(TABLE_HOSTS, new String[] { FIELD_HOST_HOSTNAME, | |
563 FIELD_HOST_PORT, FIELD_HOST_HOSTKEYALGO, FIELD_HOST_HOSTKEY | |
564 }, | |
565 null, null, null, null, null); | |
566 | |
567 if (c != null) { | |
568 int COL_HOSTNAME = c.getColumnIndexOrThrow(FIELD_HOST_HOSTNAME), | |
569 COL_PORT = c.getColumnIndexOrThrow(FIELD_HOST_PORT), | |
570 COL_HOSTKEYALGO = c.getColumnIndexOrThrow(FIELD_HOST_HOSTKEYALGO), | |
571 COL_HOSTKEY = c.getColumnIndexOrThrow(FIELD_HOST_HOSTKEY); | |
572 | |
573 while (c.moveToNext()) { | |
574 String hostname = c.getString(COL_HOSTNAME), | |
575 hostkeyalgo = c.getString(COL_HOSTKEYALGO); | |
576 int port = c.getInt(COL_PORT); | |
577 byte[] hostkey = c.getBlob(COL_HOSTKEY); | |
578 | |
579 if (hostkeyalgo == null || hostkeyalgo.length() == 0) continue; | |
580 | |
581 if (hostkey == null || hostkey.length == 0) continue; | |
582 | |
583 try { | |
584 known.addHostkey(new String[] { String.format("%s:%d", hostname, port) }, hostkeyalgo, hostkey); | |
585 } | |
586 catch (Exception e) { | |
587 Log.e(TAG, "Problem while adding a known host from database", e); | |
588 } | |
589 } | |
590 | |
591 c.close(); | |
592 } | |
593 } | |
594 | |
595 return known; | |
596 } | |
597 | |
598 /** | |
599 * Unset any hosts using a pubkey ID that has been deleted. | |
600 * @param pubkeyId | |
601 */ | |
602 public void stopUsingPubkey(long pubkeyId) { | |
603 if (pubkeyId < 0) return; | |
604 | |
605 ContentValues values = new ContentValues(); | |
606 values.put(FIELD_HOST_PUBKEYID, PUBKEYID_ANY); | |
607 | |
608 synchronized (dbLock) { | |
609 SQLiteDatabase db = this.getWritableDatabase(); | |
610 db.update(TABLE_HOSTS, values, FIELD_HOST_PUBKEYID + " = ?", new String[] { String.valueOf(pubkeyId) }); | |
611 } | |
612 | |
613 Log.d(TAG, String.format("Set all hosts using pubkey id %d to -1", pubkeyId)); | |
614 } | |
615 | |
616 /* | |
617 * Methods for dealing with port forwards attached to hosts | |
618 */ | |
619 | |
620 /** | |
621 * Returns a list of all the port forwards associated with a particular host ID. | |
622 * @param host the host for which we want the port forward list | |
623 * @return port forwards associated with host ID | |
624 */ | |
625 public List<PortForwardBean> getPortForwardsForHost(HostBean host) { | |
626 List<PortForwardBean> portForwards = new LinkedList<PortForwardBean>(); | |
627 | |
628 synchronized (dbLock) { | |
629 SQLiteDatabase db = this.getReadableDatabase(); | |
630 Cursor c = db.query(TABLE_PORTFORWARDS, new String[] { | |
631 "_id", FIELD_PORTFORWARD_NICKNAME, FIELD_PORTFORWARD_TYPE, FIELD_PORTFORWARD_SOURCEPORT, | |
632 FIELD_PORTFORWARD_DESTADDR, FIELD_PORTFORWARD_DESTPORT | |
633 }, | |
634 FIELD_PORTFORWARD_HOSTID + " = ?", new String[] { String.valueOf(host.getId()) }, | |
635 null, null, null); | |
636 | |
637 while (c.moveToNext()) { | |
638 PortForwardBean pfb = new PortForwardBean( | |
639 c.getInt(0), | |
640 host.getId(), | |
641 c.getString(1), | |
642 c.getString(2), | |
643 c.getInt(3), | |
644 c.getString(4), | |
645 c.getInt(5)); | |
646 portForwards.add(pfb); | |
647 } | |
648 | |
649 c.close(); | |
650 } | |
651 | |
652 return portForwards; | |
653 } | |
654 | |
655 /** | |
656 * Update the parameters of a port forward in the database. | |
657 * @param pfb {@link PortForwardBean} to save | |
658 * @return true on success | |
659 */ | |
660 public boolean savePortForward(PortForwardBean pfb) { | |
661 boolean success = false; | |
662 | |
663 synchronized (dbLock) { | |
664 SQLiteDatabase db = getWritableDatabase(); | |
665 | |
666 if (pfb.getId() < 0) { | |
667 long id = db.insert(TABLE_PORTFORWARDS, null, pfb.getValues()); | |
668 pfb.setId(id); | |
669 success = true; | |
670 } | |
671 else { | |
672 if (db.update(TABLE_PORTFORWARDS, pfb.getValues(), "_id = ?", new String[] { String.valueOf(pfb.getId()) }) > 0) | |
673 success = true; | |
674 } | |
675 } | |
676 | |
677 return success; | |
678 } | |
679 | |
680 /** | |
681 * Deletes a port forward from the database. | |
682 * @param pfb {@link PortForwardBean} to delete | |
683 */ | |
684 public void deletePortForward(PortForwardBean pfb) { | |
685 if (pfb.getId() < 0) | |
686 return; | |
687 | |
688 synchronized (dbLock) { | |
689 SQLiteDatabase db = this.getWritableDatabase(); | |
690 db.delete(TABLE_PORTFORWARDS, "_id = ?", new String[] { String.valueOf(pfb.getId()) }); | |
691 } | |
692 } | |
693 | |
694 public Integer[] getColorsForScheme(int scheme) { | |
695 Integer[] colors = Colors.defaults.clone(); | |
696 | |
697 synchronized (dbLock) { | |
698 SQLiteDatabase db = getReadableDatabase(); | |
699 Cursor c = db.query(TABLE_COLORS, new String[] { | |
700 FIELD_COLOR_NUMBER, FIELD_COLOR_VALUE | |
701 }, | |
702 FIELD_COLOR_SCHEME + " = ?", | |
703 new String[] { String.valueOf(scheme) }, | |
704 null, null, null); | |
705 | |
706 while (c.moveToNext()) { | |
707 colors[c.getInt(0)] = Integer.valueOf(c.getInt(1)); | |
708 } | |
709 | |
710 c.close(); | |
711 } | |
712 | |
713 return colors; | |
714 } | |
715 | |
716 public void setColorForScheme(int scheme, int number, int value) { | |
717 final SQLiteDatabase db; | |
718 final String[] whereArgs = new String[] { String.valueOf(scheme), String.valueOf(number) }; | |
719 | |
720 if (value == Colors.defaults[number]) { | |
721 synchronized (dbLock) { | |
722 db = getWritableDatabase(); | |
723 db.delete(TABLE_COLORS, | |
724 WHERE_SCHEME_AND_COLOR, whereArgs); | |
725 } | |
726 } | |
727 else { | |
728 final ContentValues values = new ContentValues(); | |
729 values.put(FIELD_COLOR_VALUE, value); | |
730 | |
731 synchronized (dbLock) { | |
732 db = getWritableDatabase(); | |
733 final int rowsAffected = db.update(TABLE_COLORS, values, | |
734 WHERE_SCHEME_AND_COLOR, whereArgs); | |
735 | |
736 if (rowsAffected == 0) { | |
737 values.put(FIELD_COLOR_SCHEME, scheme); | |
738 values.put(FIELD_COLOR_NUMBER, number); | |
739 db.insert(TABLE_COLORS, null, values); | |
740 } | |
741 } | |
742 } | |
743 } | |
744 | |
745 public void setGlobalColor(int number, int value) { | |
746 setColorForScheme(DEFAULT_COLOR_SCHEME, number, value); | |
747 } | |
748 | |
749 public int[] getDefaultColorsForScheme(int scheme) { | |
750 int[] colors = new int[] { DEFAULT_FG_COLOR, DEFAULT_BG_COLOR }; | |
751 | |
752 synchronized (dbLock) { | |
753 SQLiteDatabase db = getReadableDatabase(); | |
754 Cursor c = db.query(TABLE_COLOR_DEFAULTS, | |
755 new String[] { FIELD_COLOR_FG, FIELD_COLOR_BG }, | |
756 FIELD_COLOR_SCHEME + " = ?", | |
757 new String[] { String.valueOf(scheme) }, | |
758 null, null, null); | |
759 | |
760 if (c.moveToFirst()) { | |
761 colors[0] = c.getInt(0); | |
762 colors[1] = c.getInt(1); | |
763 } | |
764 | |
765 c.close(); | |
766 } | |
767 | |
768 return colors; | |
769 } | |
770 | |
771 public int[] getGlobalDefaultColors() { | |
772 return getDefaultColorsForScheme(DEFAULT_COLOR_SCHEME); | |
773 } | |
774 | |
775 public void setDefaultColorsForScheme(int scheme, int fg, int bg) { | |
776 SQLiteDatabase db; | |
777 String schemeWhere = null; | |
778 String[] whereArgs; | |
779 schemeWhere = FIELD_COLOR_SCHEME + " = ?"; | |
780 whereArgs = new String[] { String.valueOf(scheme) }; | |
781 ContentValues values = new ContentValues(); | |
782 values.put(FIELD_COLOR_FG, fg); | |
783 values.put(FIELD_COLOR_BG, bg); | |
784 | |
785 synchronized (dbLock) { | |
786 db = getWritableDatabase(); | |
787 int rowsAffected = db.update(TABLE_COLOR_DEFAULTS, values, | |
788 schemeWhere, whereArgs); | |
789 | |
790 if (rowsAffected == 0) { | |
791 values.put(FIELD_COLOR_SCHEME, scheme); | |
792 db.insert(TABLE_COLOR_DEFAULTS, null, values); | |
793 } | |
794 } | |
795 } | |
796 } |