From e32766e58102c6a27be441358c628e791488ceb0 Mon Sep 17 00:00:00 2001 From: localhorst Date: Wed, 5 Jun 2019 16:05:10 +0200 Subject: [PATCH] save and load server address --- .../org/hso/texturesyncclient/app/Main.kt | 17 +++- .../controller/Controller.kt | 23 ++++-- .../controller/SettingsController.kt | 79 +++++++++++++++++++ .../view/startupView/StartupViewController.kt | 6 +- 4 files changed, 114 insertions(+), 11 deletions(-) create mode 100644 client/src/main/kotlin/org/hso/texturesyncclient/controller/SettingsController.kt diff --git a/client/src/main/kotlin/org/hso/texturesyncclient/app/Main.kt b/client/src/main/kotlin/org/hso/texturesyncclient/app/Main.kt index 3d0584c..c6292e8 100644 --- a/client/src/main/kotlin/org/hso/texturesyncclient/app/Main.kt +++ b/client/src/main/kotlin/org/hso/texturesyncclient/app/Main.kt @@ -1,18 +1,31 @@ package org.hso.texturesyncclient.app import org.hso.texturesyncclient.controller.RootController +import org.hso.texturesyncclient.controller.SettingsController import org.hso.texturesyncclient.view.importView.ImportView import org.hso.texturesyncclient.view.mainView.MainView import org.hso.texturesyncclient.view.mainView.MainViewController import org.hso.texturesyncclient.view.startupView.StartupView +import org.hso.texturesyncclient.view.startupView.StartupViewController import tornadofx.App -class Main: App(MainView::class){ +class Main: App(StartupView::class){ val controller = RootController() + private val svc: StartupViewController by inject() + init { - // TODO get saved IP address, if found try to connect, else show StartupView + SettingsController.init() + + if (SettingsController.serverAddressIsSet()) { + //load settings in ui and try to connect + println("serverAddress is set") + svc.setServerAddress(SettingsController.getServerAddress()) + svc.btnConnectAction(SettingsController.getServerAddress()) + } else { + println("serverAddress is not set") + } } } \ No newline at end of file diff --git a/client/src/main/kotlin/org/hso/texturesyncclient/controller/Controller.kt b/client/src/main/kotlin/org/hso/texturesyncclient/controller/Controller.kt index 2124bc3..88d25a9 100644 --- a/client/src/main/kotlin/org/hso/texturesyncclient/controller/Controller.kt +++ b/client/src/main/kotlin/org/hso/texturesyncclient/controller/Controller.kt @@ -1,5 +1,7 @@ package org.hso.texturesyncclient.controller +import com.sun.scenario.Settings +import javafx.application.Platform import javafx.collections.ObservableList import javafx.stage.DirectoryChooser import org.hso.texturesyncclient.controller.net.Connection @@ -30,6 +32,7 @@ class RootController : Controller() { private lateinit var con: Connection + init { /*var data = Texture() var img = con.getTexturePreview(data.textureHash) @@ -46,9 +49,9 @@ class RootController : Controller() { } mvc.addElement(test)*/ - } + /** * calculate the resolution, get today's date -> upload to server * @param path the absolute path of the file on the client's system @@ -63,7 +66,7 @@ class RootController : Controller() { val bimg = ImageIO.read(File(path)) //image for obtaining resolution val resolution = Pair(bimg.height, bimg.width) val cal = Calendar.getInstance() //calendar obj with current time - val hash = Sha256(data) + val hash = Sha256(data) //Todo free image @@ -79,18 +82,20 @@ class RootController : Controller() { } /** - * Initialize connection to server + * Initialize connection to server and switch to MainView if connected * @param name server name as IP or domain */ fun initConnection(name: String) { try { con = Connection(InetAddress.getByName(name)) con.ping() - println("Connection successful") - - // TODO store server ip for next start + println("Connection to Server successful") + SettingsController.setServerAddress(name) + switchToMainView() + println("swithing to MainView @ initCon") } catch (e: Exception) { println(e) + println("Connection to Server NOT successful") } } @@ -112,8 +117,10 @@ class RootController : Controller() { } - fun switchToMainView(){ - find(StartupView::class).replaceWith(MainView::class, sizeToScene = true, centerOnScreen = true) + fun switchToMainView() { + Platform.runLater { //avoid the exception that occurs then this is called from an not tornadoFx thread + find(StartupView::class).replaceWith(MainView::class, sizeToScene = true, centerOnScreen = true) + } } fun exportTexture(data: Texture) { diff --git a/client/src/main/kotlin/org/hso/texturesyncclient/controller/SettingsController.kt b/client/src/main/kotlin/org/hso/texturesyncclient/controller/SettingsController.kt new file mode 100644 index 0000000..5b90e65 --- /dev/null +++ b/client/src/main/kotlin/org/hso/texturesyncclient/controller/SettingsController.kt @@ -0,0 +1,79 @@ +package org.hso.texturesyncclient.controller + +import java.io.* +import java.util.Properties; + + +class SettingsController { + + companion object { + + private lateinit var serverAddress: String + + private lateinit var props: Properties + + private val userHome = System.getProperty("user.home") + private val osName = System.getProperty("os.name") + + private lateinit var dirPath: String //path to settings file + private lateinit var settingsFile: File //settings file + + private val defautAddressValue: String = " " + + fun init() { + props = Properties() + + if (osName.contains("Windows")) { + dirPath = userHome + "/Documents/TextureSync"; + } else { + dirPath = userHome + "/.TextureSync"; + } + + settingsFile = File(dirPath + "/config.xml"); //open Settings file + + if (!settingsFile.exists()) { + println("settings not found! Will create new one") + File(dirPath).mkdir() + settingsFile.createNewFile() + serverAddress = defautAddressValue //load default value + saveSettings() + } else { + println("settings found") + loadSettings() + } + } + + fun serverAddressIsSet(): Boolean { + if (serverAddress == defautAddressValue) { + return false + } + return true + } + + private fun loadSettings() { + val inputStream: InputStream + inputStream = FileInputStream(settingsFile); + props.loadFromXML(inputStream); + serverAddress = props.getProperty("serverAddress") + inputStream.close(); + } + + private fun saveSettings() { + val outputStream: OutputStream + props.setProperty("serverAddress", serverAddress); + outputStream = FileOutputStream(settingsFile); + props.storeToXML(outputStream, "TextureSync settings"); + outputStream.close(); + println("settings saved") + } + + fun getServerAddress(): String { + return serverAddress; + } + + fun setServerAddress(serverAddress: String) { + this.serverAddress = serverAddress + saveSettings() + } + } +} \ No newline at end of file diff --git a/client/src/main/kotlin/org/hso/texturesyncclient/view/startupView/StartupViewController.kt b/client/src/main/kotlin/org/hso/texturesyncclient/view/startupView/StartupViewController.kt index 11a3e5e..71e0234 100644 --- a/client/src/main/kotlin/org/hso/texturesyncclient/view/startupView/StartupViewController.kt +++ b/client/src/main/kotlin/org/hso/texturesyncclient/view/startupView/StartupViewController.kt @@ -10,6 +10,11 @@ class StartupViewController : Controller() { private val sv = find(StartupView::class) private val rootc = find(RootController::class) + fun setServerAddress(address: String){ + sv.tfServerIP.text = address + sv.tfServerIP.isFocusTraversable = false + } + fun btnConnectAction(name: String) { sv.labelStatus.text = "Verbinden ..." sv.tfServerIP.isEditable = false @@ -25,7 +30,6 @@ class StartupViewController : Controller() { sv.tfServerIP.isEditable = true sv.btnConnect.isDisable = false sv.tfServerIP.clear() - rootc.switchToMainView() } }