Skip to content

Commit a295fef

Browse files
committed
Working config
1 parent 93e1624 commit a295fef

File tree

14 files changed

+187
-63
lines changed

14 files changed

+187
-63
lines changed

src/main/kotlin/com/valaphee/blit/MainView.kt

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,9 @@ import com.valaphee.blit.data.locale.Locale
2424
import com.valaphee.blit.data.manifest.IconManifest
2525
import com.valaphee.blit.source.Entry
2626
import com.valaphee.blit.source.Source
27+
import com.valaphee.blit.source.SourceConfig
2728
import javafx.beans.property.SimpleObjectProperty
2829
import javafx.beans.property.SimpleStringProperty
29-
import javafx.collections.ObservableList
3030
import javafx.scene.control.ContextMenu
3131
import javafx.scene.control.SelectionMode
3232
import javafx.scene.control.TableColumnBase
@@ -86,7 +86,7 @@ import java.util.concurrent.CompletableFuture
8686
class MainView : View("Blit") {
8787
private val locale by di<Locale>()
8888
private val iconManifest by di<IconManifest>()
89-
private val configModel by di<Config.Model>()
89+
private val _config by di<Config>()
9090

9191
private val taskManager = TaskManager().apply {
9292
val version = System.getProperty("os.version").toFloatOrNull()
@@ -145,6 +145,14 @@ class MainView : View("Blit") {
145145
}
146146

147147
inner class Pane<T : Entry<T>> : VBox() {
148+
private val sourceConfig = SimpleObjectProperty<SourceConfig>().apply {
149+
onChange {
150+
it?.let {
151+
@Suppress("UNCHECKED_CAST")
152+
source.value = it.newSource() as Source<T>
153+
}
154+
}
155+
}
148156
private val source = SimpleObjectProperty<Source<T>>().apply {
149157
onChange {
150158
it?.let {
@@ -162,9 +170,9 @@ class MainView : View("Blit") {
162170
hgrow = Priority.ALWAYS
163171

164172
hbox {
165-
combobox(source) {
173+
combobox(sourceConfig) {
166174
@Suppress("UNCHECKED_CAST")
167-
items = configModel.sources as ObservableList<Source<T>>
175+
items = _config.sources
168176
}
169177
add(CustomTextField().apply {
170178
bind(name)
@@ -245,7 +253,7 @@ class MainView : View("Blit") {
245253
}
246254
column(locale["main.tree.column.size.title"], Entry<T>::self) {
247255
tableColumnBaseSetWidth(this, 75.0)
248-
cellFormat { text = if (it.directory) "" else configModel.dataSizeUnit.value.format(it.size) }
256+
cellFormat { text = if (it.directory) "" else _config.dataSizeUnit.format(it.size) }
249257
setComparator { a, b -> a.size.compareTo(b.size) }
250258
}
251259
column(locale["main.tree.column.modified.title"], Entry<T>::modifyTime) {
@@ -261,9 +269,9 @@ class MainView : View("Blit") {
261269
setContent {
262270
taskManager.runBlocking(locale["main.tree.task.download.name"]) {
263271
suspend fun flatten(entry: Entry<T>, path: String? = null): List<File> = if (entry.directory) {
264-
File(configModel.temporaryPath.value, entry.name).mkdir()
272+
File(_config.temporaryPath, entry.name).mkdir()
265273
entry.list().flatMap { flatten(it, "${path?.let { "$path/" } ?: ""}${entry.name}") }
266-
} else listOf(File(configModel.temporaryPath.value, "${path?.let { "$path/" } ?: ""}${entry.name}").apply { FileOutputStream(this).use { entry.transferTo(it) } })
274+
} else listOf(File(_config.temporaryPath, "${path?.let { "$path/" } ?: ""}${entry.name}").apply { FileOutputStream(this).use { entry.transferTo(it) } })
267275

268276
putFiles(selectionModel.selectedItems.flatMap { flatten(it.value) })
269277
}
@@ -302,9 +310,9 @@ class MainView : View("Blit") {
302310
KeyCode.C -> if (it.isControlDown) Clipboard.getSystemClipboard().setContent {
303311
taskManager.runBlocking(locale["main.tree.task.download.name"]) {
304312
suspend fun flatten(entry: Entry<T>, path: String? = null): List<File> = if (entry.directory) {
305-
File(configModel.temporaryPath.value, entry.name).mkdir()
313+
File(_config.temporaryPath, entry.name).mkdir()
306314
entry.list().flatMap { flatten(it, "${path?.let { "$path/" } ?: ""}${entry.name}") }
307-
} else listOf(File(configModel.temporaryPath.value, "${path?.let { "$path/" } ?: ""}${entry.name}").apply { FileOutputStream(this).use { entry.transferTo(it) } })
315+
} else listOf(File(_config.temporaryPath, "${path?.let { "$path/" } ?: ""}${entry.name}").apply { FileOutputStream(this).use { entry.transferTo(it) } })
308316

309317
putFiles(selectionModel.selectedItems.flatMap { flatten(it.value) })
310318

@@ -333,7 +341,7 @@ class MainView : View("Blit") {
333341
private fun open(item: TreeItem<Entry<T>>) {
334342
if (Desktop.isDesktopSupported()) {
335343
val entry = item.value
336-
taskManager.launch(locale["main.tree.task.download.name", entry]) { Desktop.getDesktop().open(File(configModel.temporaryPath.value, entry.name).apply { FileOutputStream(this).use { entry.transferTo(it) } }) } // TODO: Desktop.open throws IOException (No application is associated with the specific file for this operation.)
344+
taskManager.launch(locale["main.tree.task.download.name", entry]) { Desktop.getDesktop().open(File(_config.temporaryPath, entry.name).apply { FileOutputStream(this).use { entry.transferTo(it) } }) } // TODO: Desktop.open throws IOException (No application is associated with the specific file for this operation.)
337345
}
338346
}
339347

src/main/kotlin/com/valaphee/blit/data/DataModule.kt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,8 @@ class DataModule(
6161
}
6262
else -> null
6363
}
64-
} catch (_: Throwable) {
64+
} catch (e: Throwable) {
65+
e.printStackTrace()
6566
}
6667
}
6768
.partition { it is KeyedData }

src/main/kotlin/com/valaphee/blit/data/config/Config.kt

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,25 +16,34 @@
1616

1717
package com.valaphee.blit.data.config
1818

19+
import com.fasterxml.jackson.annotation.JsonIgnore
1920
import com.fasterxml.jackson.annotation.JsonValue
21+
import com.google.inject.Singleton
22+
import com.valaphee.blit.data.Data
23+
import com.valaphee.blit.data.DataType
2024
import com.valaphee.blit.source.SourceConfig
2125
import javafx.beans.property.SimpleListProperty
2226
import javafx.beans.property.SimpleObjectProperty
2327
import javafx.beans.property.SimpleStringProperty
28+
import javafx.collections.ObservableList
2429
import tornadofx.ItemViewModel
2530
import tornadofx.asObservable
31+
import tornadofx.getValue
32+
import tornadofx.setValue
2633
import java.text.StringCharacterIterator
2734
import kotlin.math.abs
2835

2936
/**
3037
* @author Kevin Ludwig
3138
*/
39+
@Singleton
40+
@DataType("config")
3241
class Config(
3342
locale: String,
3443
dataSizeUnit: DataSizeUnit,
3544
temporaryPath: String,
3645
sources: List<SourceConfig>
37-
) {
46+
) : Data {
3847
enum class DataSizeUnit(
3948
@get:JsonValue val key: String,
4049
val format: (Long) -> String
@@ -68,10 +77,17 @@ class Config(
6877
})
6978
}
7079

71-
val localeProperty = SimpleStringProperty(locale)
72-
val dataSizeUnitProperty = SimpleObjectProperty(dataSizeUnit)
73-
val temporaryPathProperty = SimpleStringProperty(temporaryPath)
74-
val sourcesProperty = SimpleListProperty(sources.asObservable())
80+
@get:JsonIgnore internal val localeProperty = SimpleStringProperty(locale)
81+
var locale: String by localeProperty
82+
83+
@get:JsonIgnore internal val dataSizeUnitProperty = SimpleObjectProperty(dataSizeUnit)
84+
var dataSizeUnit: DataSizeUnit by dataSizeUnitProperty
85+
86+
@get:JsonIgnore internal val temporaryPathProperty = SimpleStringProperty(temporaryPath)
87+
var temporaryPath: String by temporaryPathProperty
88+
89+
@get:JsonIgnore internal val sourcesProperty = SimpleListProperty(sources.asObservable())
90+
var sources: ObservableList<SourceConfig> by sourcesProperty
7591

7692
class Model : ItemViewModel<Config>() {
7793
val locale = bind(Config::localeProperty)

src/main/kotlin/com/valaphee/blit/data/config/ConfigView.kt

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,6 @@ import tornadofx.View
3030
import tornadofx.action
3131
import tornadofx.button
3232
import tornadofx.buttonbar
33-
import tornadofx.enableWhen
3433
import tornadofx.select
3534
import tornadofx.tab
3635
import tornadofx.tabpane
@@ -43,7 +42,7 @@ import kotlin.reflect.KClass
4342
*/
4443
class ConfigView : View("Configure Blit") {
4544
private val locale by di<Locale>()
46-
private val configModel by di<Config.Model>()
45+
private val _config by di<Config>()
4746

4847
private var tabs = mutableMapOf<KClass<out Component>, Tab>()
4948

@@ -64,19 +63,19 @@ class ConfigView : View("Configure Blit") {
6463
buttonbar {
6564
button(locale["config.ok.text"]) {
6665
action {
67-
configModel.commit()
66+
//_config.commit()
6867
(scene.window as Stage).close()
6968
}
7069
}
7170
button(locale["config.cancel.text"]) {
7271
action {
73-
configModel.rollback()
72+
//_config.rollback()
7473
(scene.window as Stage).close()
7574
}
7675
}
7776
button(locale["config.apply.text"]) {
78-
enableWhen(configModel.dirty)
79-
action(configModel::commit)
77+
//enableWhen(_config.dirty)
78+
//action(_config::commit)
8079
}
8180
}
8281
}

src/main/kotlin/com/valaphee/blit/data/config/ConfigViewGeneral.kt

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -33,15 +33,15 @@ import java.io.File
3333
*/
3434
class ConfigViewGeneral : Fragment("General") {
3535
private val locale by di<Locale>()
36-
private val configModel by di<Config.Model>()
36+
private val _config by di<Config>()
3737
private val injector by di<Injector>()
3838

3939
override val root = form {
4040
fieldset {
4141
val locales = injector.getInstance(object : Key<Map<String, @JvmSuppressWildcards Locale>>() {})
42-
field(locale["config.general.locale.text"]) { combobox(configModel.locale, locales.keys.toList()) { cellFormat { text = locales[it]!!["name"] } } }
43-
field(locale["config.general.data_size_unit.text"]) { combobox(configModel.dataSizeUnit, Config.DataSizeUnit.values().toList()) { cellFormat { text = locale["config.general.data_size_unit.${it.key}"] } } }
44-
field(locale["config.general.temporary_path.text"]) { textfield(configModel.temporaryPath) { validator { if (it.isNullOrBlank() || !File(it).isDirectory) error(locale["config.general.temporary_path.invalid"]) else null } } }
42+
field(locale["config.general.locale.text"]) { combobox(_config.localeProperty, locales.keys.toList()) { cellFormat { text = locales[it]!!["name"] } } }
43+
field(locale["config.general.data_size_unit.text"]) { combobox(_config.dataSizeUnitProperty, Config.DataSizeUnit.values().toList()) { cellFormat { text = locale["config.general.data_size_unit.${it.key}"] } } }
44+
field(locale["config.general.temporary_path.text"]) { textfield(_config.temporaryPathProperty) { validator { if (it.isNullOrBlank() || !File(it).isDirectory) error(locale["config.general.temporary_path.invalid"]) else null } } }
4545
}
4646
}
4747
}

src/main/kotlin/com/valaphee/blit/data/config/ConfigViewSources.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,13 +38,13 @@ import tornadofx.vgrow
3838
*/
3939
class ConfigViewSources : Fragment("Sources") {
4040
private val locale by di<Locale>()
41-
private val configModel by di<Config.Model>()
41+
private val _config by di<Config>()
4242

4343
private val source = SimpleObjectProperty<SourceConfig>()
4444

4545
override val root = hbox {
4646
vbox {
47-
add(listview(configModel.sources) {
47+
add(listview(_config.sources) {
4848
bindSelected(source)
4949

5050
vgrow = Priority.ALWAYS

src/main/kotlin/com/valaphee/blit/source/SourceConfig.kt

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,16 +16,39 @@
1616

1717
package com.valaphee.blit.source
1818

19+
import com.fasterxml.jackson.annotation.JsonIgnore
20+
import com.fasterxml.jackson.annotation.JsonSubTypes
21+
import com.fasterxml.jackson.annotation.JsonTypeInfo
22+
import com.valaphee.blit.source.dav.DavSourceConfig
23+
import com.valaphee.blit.source.k8scp.K8scpSourceConfig
24+
import com.valaphee.blit.source.local.LocalSourceConfig
25+
import com.valaphee.blit.source.scp.ScpSourceConfig
26+
import com.valaphee.blit.source.sftp.SftpSourceConfig
1927
import javafx.beans.property.SimpleStringProperty
2028
import javafx.event.EventTarget
29+
import tornadofx.getValue
30+
import tornadofx.setValue
2131

2232
/**
2333
* @author Kevin Ludwig
2434
*/
35+
@JsonTypeInfo(
36+
use = JsonTypeInfo.Id.NAME,
37+
include = JsonTypeInfo.As.PROPERTY,
38+
property = "type"
39+
)
40+
@JsonSubTypes(
41+
JsonSubTypes.Type(DavSourceConfig::class),
42+
JsonSubTypes.Type(K8scpSourceConfig::class),
43+
JsonSubTypes.Type(LocalSourceConfig::class),
44+
JsonSubTypes.Type(ScpSourceConfig::class),
45+
JsonSubTypes.Type(SftpSourceConfig::class)
46+
)
2547
abstract class SourceConfig(
2648
name: String
2749
) {
28-
val nameProperty = SimpleStringProperty(name)
50+
@get:JsonIgnore protected val nameProperty = SimpleStringProperty(name)
51+
var name by nameProperty
2952

3053
abstract fun newUi(eventTarget: EventTarget)
3154

src/main/kotlin/com/valaphee/blit/source/TransferInputStream.kt

Lines changed: 36 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -23,23 +23,48 @@ import java.io.InputStream
2323
* @author Kevin Ludwig
2424
*/
2525
class TransferInputStream(
26-
stream: InputStream,
27-
private val read: (Long) -> Unit
26+
private val stream: InputStream,
27+
private val onRead: (Long) -> Unit
2828
) : FilterInputStream(stream) {
2929
private var readSum = 0L
30+
private var mark = -1L
3031

31-
override fun read() = super.read().also {
32-
readSum++
33-
read(readSum)
32+
override fun read(): Int {
33+
val result = stream.read()
34+
if (result != -1) {
35+
readSum++
36+
onRead(readSum)
37+
}
38+
return result
3439
}
3540

36-
override fun read(b: ByteArray, off: Int, len: Int) = super.read(b, off, len).also {
37-
readSum += it
38-
read(readSum)
41+
override fun read(b: ByteArray, off: Int, len: Int): Int {
42+
val result = stream.read(b, off, len)
43+
if (result != -1) {
44+
readSum += result
45+
onRead(readSum)
46+
}
47+
return result
3948
}
4049

41-
override fun skip(n: Long) = super.skip(n).also {
42-
readSum += it
43-
read(readSum)
50+
override fun skip(n: Long): Long {
51+
val result = stream.skip(n)
52+
readSum += result
53+
return result
54+
}
55+
56+
override fun mark(readlimit: Int) {
57+
stream.mark(readlimit)
58+
mark = readSum
59+
}
60+
61+
override fun reset() {
62+
require(stream.markSupported())
63+
require(mark != -1L)
64+
65+
stream.reset()
66+
67+
readSum = mark
68+
onRead(readSum)
4469
}
4570
}

src/main/kotlin/com/valaphee/blit/source/dav/DavSource.kt

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -44,11 +44,11 @@ import javax.net.ssl.X509TrustManager
4444
*/
4545
class DavSource(
4646
name: String,
47-
private val url: String,
48-
private val username: String,
47+
internal val url: String,
48+
internal val username: String,
4949
private val password: String,
50-
private val nextcloud: Boolean,
51-
private val nextcloudUploadChunkSize: Long
50+
internal val nextcloud: Boolean,
51+
internal val nextcloudUploadChunkSize: Long
5252
) : AbstractSource<DavEntry>(name) {
5353
internal val httpClient by lazy {
5454
HttpClient(OkHttp) {

0 commit comments

Comments
 (0)