mirror of
https://github.com/fabianonline/telegram_backup.git
synced 2024-11-23 01:06:17 +00:00
Added Settings.
This commit is contained in:
parent
de354f548d
commit
42dc500514
@ -539,7 +539,27 @@ class Database private constructor(var client: TelegramClient) {
|
|||||||
e.printStackTrace()
|
e.printStackTrace()
|
||||||
throw RuntimeException("Exception shown above happened.")
|
throw RuntimeException("Exception shown above happened.")
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fun settingsSetValue(key: String, value: String?) {
|
||||||
|
val ps = conn!!.prepareStatement("INSERT OR REPLACE INTO settings (key, value) VALUES (?, ?)")
|
||||||
|
ps.setString(1, key)
|
||||||
|
if (value==null) {
|
||||||
|
ps.setNull(2, Types.VARCHAR)
|
||||||
|
} else {
|
||||||
|
ps.setString(2, value)
|
||||||
|
}
|
||||||
|
ps.execute()
|
||||||
|
}
|
||||||
|
|
||||||
|
fun settingsGetSettings(): Map<String, String?> {
|
||||||
|
val rs = stmt!!.executeQuery("SELECT key, value FROM settings")
|
||||||
|
val result = mutableMapOf<String, String?>()
|
||||||
|
while (rs.next()) {
|
||||||
|
result.set(rs.getString(1), rs.getString(2))
|
||||||
|
}
|
||||||
|
rs.close()
|
||||||
|
return result
|
||||||
}
|
}
|
||||||
|
|
||||||
fun getIdsFromQuery(query: String): LinkedList<Int> {
|
fun getIdsFromQuery(query: String): LinkedList<Int> {
|
||||||
|
@ -32,6 +32,7 @@ class DatabaseUpdates(protected var conn: Connection, protected var db: Database
|
|||||||
register(DB_Update_7(conn, db))
|
register(DB_Update_7(conn, db))
|
||||||
register(DB_Update_8(conn, db))
|
register(DB_Update_8(conn, db))
|
||||||
register(DB_Update_9(conn, db))
|
register(DB_Update_9(conn, db))
|
||||||
|
register(DB_Update_10(conn, db))
|
||||||
}
|
}
|
||||||
|
|
||||||
fun doUpdates() {
|
fun doUpdates() {
|
||||||
@ -398,3 +399,14 @@ internal class DB_Update_9(conn: Connection, db: Database) : DatabaseUpdate(conn
|
|||||||
execute("DELETE FROM messages WHERE id IN (" + messages_to_delete.joinToString() + ")")
|
execute("DELETE FROM messages WHERE id IN (" + messages_to_delete.joinToString() + ")")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
internal class DB_Update_10(conn: Connection, db: Database) : DatabaseUpdate(conn, db) {
|
||||||
|
override val version: Int
|
||||||
|
get() = 10
|
||||||
|
|
||||||
|
@Throws(SQLException::class)
|
||||||
|
override fun _doUpdate() {
|
||||||
|
val logger = LoggerFactory.getLogger(DB_Update_10::class.java)
|
||||||
|
execute("CREATE TABLE settings (key TEXT PRIMARY KEY, value TEXT)")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
60
src/main/kotlin/de/fabianonline/telegram_backup/Settings.kt
Normal file
60
src/main/kotlin/de/fabianonline/telegram_backup/Settings.kt
Normal file
@ -0,0 +1,60 @@
|
|||||||
|
package de.fabianonline.telegram_backup
|
||||||
|
|
||||||
|
class Settings() {
|
||||||
|
|
||||||
|
val options = arrayOf(
|
||||||
|
Option("gmaps_api_key", "abc", "This contains the api key bla foo bar.")
|
||||||
|
)
|
||||||
|
|
||||||
|
init {
|
||||||
|
var all_settings = Database.getInstance().settingsGetSettings()
|
||||||
|
for ((key, value) in all_settings) {
|
||||||
|
val opt = options.find{it.key == key}
|
||||||
|
if (opt==null) throw IllegalArgumentException("Setting with key ${key} is unknown.")
|
||||||
|
if (value!=null) opt.value = value
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fun print() {
|
||||||
|
val modifiedSettings = options.filter{!it.isDefaultValue()}
|
||||||
|
val defaultSettings = options.filter{it.isDefaultValue()}
|
||||||
|
|
||||||
|
if (modifiedSettings.count() > 0) {
|
||||||
|
println("Modified settings:")
|
||||||
|
modifiedSettings.forEach{it.print()}
|
||||||
|
println("\n")
|
||||||
|
}
|
||||||
|
|
||||||
|
if (defaultSettings.count() > 0) {
|
||||||
|
println("Settings with default value:")
|
||||||
|
defaultSettings.forEach{it.print()}
|
||||||
|
println("\n")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
inner open class HiddenOption(val key: String, val default: String) {
|
||||||
|
var value: String? = null
|
||||||
|
|
||||||
|
fun save() {
|
||||||
|
if (isDefaultValue()) {
|
||||||
|
Database.getInstance().settingsSetValue(key, null)
|
||||||
|
} else {
|
||||||
|
Database.getInstance().settingsSetValue(key, value)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
open fun print() {
|
||||||
|
println("%-30s %-30s".format(key, value))
|
||||||
|
}
|
||||||
|
|
||||||
|
fun isDefaultValue(): Boolean = (value==null || value==default)
|
||||||
|
|
||||||
|
}
|
||||||
|
inner class Option(key: String, default: String, val descr: String): HiddenOption(key, default) {
|
||||||
|
override fun print() {
|
||||||
|
println("%-30s %-30s %s".format(key, value, descr))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue
Block a user