diff --git a/src/main/kotlin/de/fabianonline/telegram_backup/Database.kt b/src/main/kotlin/de/fabianonline/telegram_backup/Database.kt index 8c35302..6f0a971 100644 --- a/src/main/kotlin/de/fabianonline/telegram_backup/Database.kt +++ b/src/main/kotlin/de/fabianonline/telegram_backup/Database.kt @@ -539,7 +539,27 @@ class Database private constructor(var client: TelegramClient) { e.printStackTrace() 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 { + val rs = stmt!!.executeQuery("SELECT key, value FROM settings") + val result = mutableMapOf() + while (rs.next()) { + result.set(rs.getString(1), rs.getString(2)) + } + rs.close() + return result } fun getIdsFromQuery(query: String): LinkedList { diff --git a/src/main/kotlin/de/fabianonline/telegram_backup/DatabaseUpdates.kt b/src/main/kotlin/de/fabianonline/telegram_backup/DatabaseUpdates.kt index c448681..3e6d780 100644 --- a/src/main/kotlin/de/fabianonline/telegram_backup/DatabaseUpdates.kt +++ b/src/main/kotlin/de/fabianonline/telegram_backup/DatabaseUpdates.kt @@ -32,6 +32,7 @@ class DatabaseUpdates(protected var conn: Connection, protected var db: Database register(DB_Update_7(conn, db)) register(DB_Update_8(conn, db)) register(DB_Update_9(conn, db)) + register(DB_Update_10(conn, db)) } 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() + ")") } } + +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)") + } +} diff --git a/src/main/kotlin/de/fabianonline/telegram_backup/Settings.kt b/src/main/kotlin/de/fabianonline/telegram_backup/Settings.kt new file mode 100644 index 0000000..ddd31de --- /dev/null +++ b/src/main/kotlin/de/fabianonline/telegram_backup/Settings.kt @@ -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)) + } + } +} + +