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