1
0
mirror of https://github.com/fabianonline/telegram_backup.git synced 2024-11-22 16:56:16 +00:00

Rewritten Settings.

This commit is contained in:
Fabian Schlenz 2018-04-10 06:12:02 +02:00
parent e5da546386
commit 5c466131d3
2 changed files with 55 additions and 66 deletions

View File

@ -352,9 +352,9 @@ class DownloadManager(val client: TelegramClient, val prog: DownloadProgressInte
if (tl_peer_channel == null) continue if (tl_peer_channel == null) continue
var download = true var download = true
if (settings.whitelist_channels != null) { if (settings.whitelist_channels.isNotEmpty()) {
download = settings.whitelist_channels.contains(tl_channel.getId().toString()) download = settings.whitelist_channels.contains(tl_channel.getId().toString())
} else if (settings.blacklist_channels != null) { } else if (settings.blacklist_channels.isNotEmpty()) {
download = !settings.blacklist_channels.contains(tl_channel.getId().toString()) download = !settings.blacklist_channels.contains(tl_channel.getId().toString())
} }
val channel = Channel(id=tl_channel.getId(), access_hash=tl_channel.getAccessHash(), title=tl_channel.getTitle(), obj=tl_peer_channel, download=download) val channel = Channel(id=tl_channel.getId(), access_hash=tl_channel.getAccessHash(), title=tl_channel.getTitle(), obj=tl_peer_channel, download=download)

View File

@ -1,6 +1,7 @@
package de.fabianonline.telegram_backup package de.fabianonline.telegram_backup
import java.io.File import java.io.File
import java.util.LinkedList
import org.slf4j.LoggerFactory import org.slf4j.LoggerFactory
class Settings(val file_base: String, val database: Database, val cli_settings: CommandLineOptions) { class Settings(val file_base: String, val database: Database, val cli_settings: CommandLineOptions) {
@ -16,27 +17,16 @@ class Settings(val file_base: String, val database: Database, val cli_settings:
copy_sample_ini("config.sample.ini") copy_sample_ini("config.sample.ini")
} }
// Merging CLI and INI settings // Merging CLI and INI settings
val gmaps_key = get_setting_string("gmaps_key", default=Config.SECRET_GMAPS)
val pagination = get_setting_boolean("pagination", default=true)
val pagination_size = get_setting_int("pagination_size", default=Config.DEFAULT_PAGINATION)
val download_media = get_setting_boolean("download_media", default=true)
val download_channels = get_setting_boolean("download_channels", default=false)
val download_supergroups = get_setting_boolean("download_supergroups", default=false)
val whitelist_channels = get_setting_list("whitelist_channels")
val blacklist_channels = get_setting_list("blacklist_channels")
val sf = SettingsFactory(ini_settings, cli_settings)
private fun get_setting_string(name: String, default: String): String { val gmaps_key = sf.getString("gmaps_key", default=Config.SECRET_GMAPS)
return ini_settings[name]?.last() ?: cli_settings.values[name] ?: default val pagination = sf.getBoolean("pagination", default=true)
} val pagination_size = sf.getInt("pagination_size", default=Config.DEFAULT_PAGINATION)
val download_media = sf.getBoolean("download_media", default=true)
private fun get_setting_int(name: String, default: Int): Int { val download_channels = sf.getBoolean("download_channels", default=false)
return ini_settings[name]?.last()?.toInt() ?: cli_settings.values[name]?.toInt() ?: default val download_supergroups = sf.getBoolean("download_supergroups", default=false)
} val whitelist_channels = sf.getStringList("whitelist_channels", default=LinkedList<String>())
val blacklist_channels = sf.getStringList("blacklist_channels", default=LinkedList<String>())
private fun get_setting_boolean(name: String, default: Boolean): Boolean {
return ini_settings[name]?.last()?.toBoolean() ?: cli_settings.values[name]?.toBoolean() ?: default
}
private fun get_setting_list(name: String): List<String>? { private fun get_setting_list(name: String): List<String>? {
return ini_settings[name] return ini_settings[name]
@ -79,57 +69,56 @@ class Settings(val file_base: String, val database: Database, val cli_settings:
File(filename).outputStream().use { stream.copyTo(it) } File(filename).outputStream().use { stream.copyTo(it) }
stream.close() stream.close()
} }
}
/*
class DbSettings(val database: Database) {
private fun fetchValue(name: String): String? = database.fetchSetting(name)
private fun saveValue(name: String, value: String?) = database.saveSetting(name, value)
var pts: String?
get() = fetchValue("pts")
set(x: String?) = saveValue("pts", x)
} }
class SettingsFactory(val ini: Map<String, List<String>>, val cli: CommandLineOptions) {
fun getInt(name: String, default: Int) = getSetting(name, listOf(default.toString())).get().toInt()
fun getBoolean(name: String, default: Boolean) = getSetting(name, listOf(default.toString())).get().toBoolean()
fun getString(name: String, default: String) = getSetting(name, listOf(default)).get()
fun getStringList(name: String, default: List<String>) = getSetting(name, default).getList()
package de.fabianonline.telegram_backup fun getSetting(name: String, default: List<String>) = Setting(ini, cli, name, default)
}
class Setting(val ini: Map<String, List<String>>, val cli: CommandLineOptions, val name: String, val default: List<String>) {
val values: List<String>
class Settings(val file_base: String) { val source: SettingSource
val logger = LoggerFactory.getLogger(Settings::class.java) val logger = LoggerFactory.getLogger(Setting::class.java)
var settings = mutableMapOf<String, MutableList<String>>()
init { init {
loadIni(file_base + "config.ini") if (getIni(name) != null) {
copySampleIni(file_base + "config.sample.ini") values = getIni(name)!!
source = SettingSource.INI
} else if (getCli(name) != null) {
values = listOf(getCli(name)!!)
source = SettingSource.CLI
} else {
values = default
source = SettingSource.DEFAULT
}
logger.debug("Setting ${name} loaded. Source: ${source}. Value: ${values.toString().anonymize()}")
all_settings.add(this)
}
fun get(): String = values.last()
fun getList(): List<String> = values
fun getIni(name: String): List<String>? {
return ini[name]
} }
fun getCli(name: String): String? {
return cli.values[name]
fun println() = println(settings)
fun getString(key: String, default: String? = null): String? = settings.get(key)?.last() ?: default
fun getStringList(key: String): List<String>? = settings.get(key)
fun getInt(key: String, default: Int? = null): Int? = try { settings.get(key)?.last()?.toInt() } catch (e: NumberFormatException) { null } ?: default
fun getBoolean(key: String, default: Boolean = false): Boolean {
val value = settings.get(key)?.last()
if (value==null) return default
return value=="true"
} }
fun getArray(key: String): List<String> = settings.get(key) ?: listOf<String>()
val gmaps_key = getString("gmaps_key", default=Config.SECRET_GMAPS)!! companion object {
val pagination = getBoolean("pagination", default=true) val all_settings = LinkedList<Setting>()
val pagination_size = getInt("pagination_size", default=Config.DEFAULT_PAGINATION)!! }
val download_media = getBoolean("download_media", default=true) }
val download_channels = getBoolean("download_channels", default=false)
val download_supergroups = getBoolean("download_supergroups", default=false) enum class SettingSource {
val whitelist_channels = getStringList("whitelist_channels") INI,
val blacklist_channels = getStringList("blacklist_channels") CLI,
DEFAULT
} }
*/