TheCitadelofRicks/src/main/kotlin/org/mosad/thecitadelofricks/controller/StartupController.kt

136 lines
4.0 KiB
Kotlin

/**
* TheCitadelofRicks
*
* Copyright 2019 <seil0@mosad.xyz>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
* MA 02110-1301, USA.
*
*/
package org.mosad.thecitadelofricks.controller
import org.slf4j.Logger
import org.slf4j.LoggerFactory
import java.io.File
import java.io.FileInputStream
import java.io.FileOutputStream
import java.util.*
class StartupController {
private val logger: Logger = LoggerFactory.getLogger(CacheController::class.java)
private val userHome = System.getProperty("user.home")
private val tcorHome = "$userHome/.tcor"
private val dirTcorHome = File(tcorHome)
private val dirTcorCache = File("$tcorHome/cache")
private val fileConfig = File("$tcorHome/config.xml")
init {
// if the tcor directory doesn't exist, create it
if (!dirTcorHome.exists()) {
dirTcorHome.mkdir()
}
// if the cache directory doesn't exist, create it
if (!dirTcorCache.exists()) {
dirTcorCache.mkdir()
}
// check if the config file exist, if so load it
if (fileConfig.exists()) {
loadConfig()
} else {
createConfig()
}
}
private fun loadConfig() = try {
val properties = Properties()
properties.loadFromXML(FileInputStream(fileConfig))
cachetAPIKey = try {
properties.getProperty("cachetAPIKey")
} catch (ex: Exception) {
"0"
}
cachetBaseURL = try {
properties.getProperty("cachetBaseURL")
} catch (ex: Exception) {
"https://status.mosad.xyz"
}
mensaMenuURL = try {
properties.getProperty("mensaMenuURL")
} catch (ex: Exception) {
"https://www.swfr.de/de/essen-trinken/speiseplaene/mensa-offenburg/"
}
mensaName = try {
properties.getProperty("mensaName")
} catch (ex: Exception) {
"Offenburg"
}
} catch (ex: Exception) {
logger.error("error while loading config", ex)
}
private fun createConfig() = try {
val properties = Properties()
properties.setProperty("cachetAPIKey", "0")
properties.setProperty("cachetBaseURL", "https://status.mosad.xyz")
properties.setProperty("mensaMenuURL", "https://www.swfr.de/de/essen-trinken/speiseplaene/mensa-offenburg/")
properties.setProperty("mensaName", "Offenburg")
val outputStream = FileOutputStream(fileConfig)
properties.storeToXML(outputStream, "tcor configuration")
outputStream.close()
} catch (ex: Exception) {
logger.error("error while creating config", ex)
}
companion object {
private var cachetAPIKey = "0"
private var cachetBaseURL = "https://status.mosad.xyz"
private var courseListURL = "https://www.hs-offenburg.de/studium/vorlesungsplaene/"
private var mensaMenuURL = "https://www.swfr.de/de/essen-trinken/speiseplaene/mensa-offenburg/"
private var mensaName = "Offenburg"
fun getCachetAPIKey(): String {
return cachetAPIKey
}
fun getCachetBaseURL(): String {
return cachetBaseURL
}
fun getCourseListURL(): String {
return courseListURL
}
fun getMensaMenuURL(): String {
return mensaMenuURL
}
fun getMensaName(): String {
return mensaName
}
}
}