TextureSync/client/src/main/kotlin/org/hso/texturesyncclient/controller/Controller.kt

139 lines
4.3 KiB
Kotlin

package org.hso.texturesyncclient.controller
import javafx.collections.ObservableList
import javafx.stage.DirectoryChooser
import org.hso.texturesyncclient.controller.net.Connection
import org.hso.texturesyncclient.model.GUIModel
import org.hso.texturesyncclient.model.Sha256
import org.hso.texturesyncclient.model.Texture
import org.hso.texturesyncclient.model.TextureFormat
import org.hso.texturesyncclient.view.importView.ImportViewController
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.Controller
import tornadofx.observable
import tornadofx.observableList
import java.net.InetAddress
import java.util.Calendar
import java.io.File
import javax.imageio.ImageIO
import java.util.UUID
import java.nio.file.Files
class RootController : Controller() {
private val mvc: MainViewController by inject()
private val svc: StartupViewController by inject()
private val ivc: ImportViewController by inject()
private lateinit var con: Connection
init {
/*var data = Texture()
var img = con.getTexturePreview(data.textureHash)
var test = GUIModel(data, img)
test.exportItem.setOnAction {
}
mvc.addElement(test)
data = Texture()
img = con.getTexturePreview(data.textureHash)
test = GUIModel(data, img)
test.exportItem.setOnAction {
}
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
* @param name the file name
* @param tags all tags for the file
*/
fun importTexture(path: String, name: String, tags: ObservableList<String>) {
val data = Files.readAllBytes(File(path).toPath()) // this is the image as byte array
val uuid = UUID.randomUUID()
val format = if (File(path).extension.toLowerCase() == "png") TextureFormat.PNG else TextureFormat.JPEG
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)
//Todo free image
val newTexture = Texture(uuid, name, tags.toTypedArray(), format, resolution, cal, hash)
try {
con.uploadTexture(newTexture, data)
println("Texture upload successful")
} catch (e: Exception) {
println(e)
}
}
/**
* Initialize connection to server
* @param name server name as IP or domain
*/
fun initConnection(name: String) {
try {
con = Connection(InetAddress.getByName(name))
println("ausgabe")
con.ping()
println("Connection successful")
// TODO store server ip for next start
} catch (e: Exception) {
println(e)
}
}
fun search(tags: ObservableList<String>): ArrayList<GUIModel> {
val previewList = arrayListOf<GUIModel>()
try {
con.query(tags.toTypedArray()).forEach {
previewList.add(GUIModel(it, con.getTexturePreview(it.textureHash)))
}
} catch (e: Exception) {
println(e)
}
println(previewList.size)
return previewList
}
fun switchToMainView(){
find(StartupView::class).replaceWith(MainView::class, sizeToScene = true, centerOnScreen = true)
}
fun exportTexture(data: Texture) {
val directoryChooser = DirectoryChooser()
directoryChooser.title = "Export Verzeichnis wählen"
directoryChooser.initialDirectory = File(System.getProperty("user.home"))
val dir = directoryChooser.showDialog(primaryStage)
if (dir != null) {
// TODO copy data (bytesarray) with name and extension to dir
}
}
fun showDetail(data: Texture) {
mvc.setPreview3DTexture(con.getTexturePreview(data.textureHash))
mvc.setMeta(data.name, data.resolution.toString(), "")
mvc.setTags(data.tags.toList().observable())
}
}