X-Git-Url: https://git.pterodactylus.net/?p=Sone.git;a=blobdiff_plain;f=src%2Fmain%2Fkotlin%2Fnet%2Fpterodactylus%2Fsone%2Fcore%2FPreferences.kt;fp=src%2Fmain%2Fkotlin%2Fnet%2Fpterodactylus%2Fsone%2Fcore%2FPreferences.kt;h=783552e9a0b3acc7384b06fefc93835a25f82526;hp=0000000000000000000000000000000000000000;hb=03cec6a6772c2d836d94864adddaf544cbe9d72f;hpb=6f1f26e3998cfef155b0cf59152827accea70d30 diff --git a/src/main/kotlin/net/pterodactylus/sone/core/Preferences.kt b/src/main/kotlin/net/pterodactylus/sone/core/Preferences.kt new file mode 100644 index 0000000..783552e --- /dev/null +++ b/src/main/kotlin/net/pterodactylus/sone/core/Preferences.kt @@ -0,0 +1,141 @@ +/* + * Sone - Preferences.kt - Copyright © 2013–2019 David Roden + * + * 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, see . + */ + +package net.pterodactylus.sone.core + +import com.google.common.base.Predicates.* +import com.google.common.eventbus.* +import net.pterodactylus.sone.core.event.* +import net.pterodactylus.sone.fcp.FcpInterface.* +import net.pterodactylus.sone.fcp.FcpInterface.FullAccessRequired.* +import net.pterodactylus.sone.fcp.event.* +import net.pterodactylus.sone.utils.* +import net.pterodactylus.sone.utils.IntegerRangePredicate.* +import net.pterodactylus.util.config.* +import java.lang.Integer.* + +/** + * Convenience interface for external classes that want to access the core’s + * configuration. + */ +class Preferences(private val eventBus: EventBus) { + + private val _insertionDelay = DefaultOption(60, range(0, MAX_VALUE)) + val insertionDelay: Int get() = _insertionDelay.get() + var newInsertionDelay: Int? + get() = unsupported + set(value) { + _insertionDelay.set(value) + eventBus.post(InsertionDelayChangedEvent(insertionDelay)) + eventBus.post(PreferenceChangedEvent("InsertionDelay", insertionDelay)) + } + + private val _postsPerPage = DefaultOption(10, range(1, MAX_VALUE)) + val postsPerPage: Int get() = _postsPerPage.get() + var newPostsPerPage: Int? + get() = unsupported + set(value) { + _postsPerPage.set(value) + eventBus.post(PreferenceChangedEvent("PostsPerPage", postsPerPage)) + } + + private val _imagesPerPage = DefaultOption(9, range(1, MAX_VALUE)) + val imagesPerPage: Int get() = _imagesPerPage.get() + var newImagesPerPage: Int? + get() = unsupported + set (value: Int?) = _imagesPerPage.set(value) + + private val _charactersPerPost = DefaultOption(400, or(range(50, MAX_VALUE), equalTo(-1))) + val charactersPerPost: Int get() = _charactersPerPost.get() + var newCharactersPerPost: Int? + get() = unsupported + set(value) = _charactersPerPost.set(value) + + private val _postCutOffLength = DefaultOption(200, range(50, MAX_VALUE)) + val postCutOffLength: Int get() = _postCutOffLength.get() + var newPostCutOffLength: Int? + get() = unsupported + set(value) = _postCutOffLength.set(value) + + private val _requireFullAccess = DefaultOption(false) + val requireFullAccess: Boolean get() = _requireFullAccess.get() + var newRequireFullAccess: Boolean? + get() = unsupported + set(value) = _requireFullAccess.set(value) + + private val _positiveTrust = DefaultOption(75, range(0, 100)) + val positiveTrust: Int get() = _positiveTrust.get() + var newPositiveTrust: Int? + get() = unsupported + set(value) = _positiveTrust.set(value) + + private val _negativeTrust = DefaultOption(-25, range(-100, 100)) + val negativeTrust: Int get() = _negativeTrust.get() + var newNegativeTrust: Int? + get() = unsupported + set(value) = _negativeTrust.set(value) + + private val _trustComment = DefaultOption("Set from Sone Web Interface") + val trustComment: String get() = _trustComment.get() + var newTrustComment: String? + get() = unsupported + set(value) = _trustComment.set(value) + + private val _fcpInterfaceActive = DefaultOption(false) + val fcpInterfaceActive: Boolean get() = _fcpInterfaceActive.get() + var newFcpInterfaceActive: Boolean? + get() = unsupported + set(value) { + _fcpInterfaceActive.set(value) + when (value) { + true -> eventBus.post(FcpInterfaceActivatedEvent()) + else -> eventBus.post(FcpInterfaceDeactivatedEvent()) + } + } + + private val _fcpFullAccessRequired = DefaultOption(ALWAYS) + val fcpFullAccessRequired: FullAccessRequired get() = _fcpFullAccessRequired.get() + var newFcpFullAccessRequired: FullAccessRequired? + get() = unsupported + set(value) { + _fcpFullAccessRequired.set(value) + eventBus.post(FullAccessRequiredChanged(fcpFullAccessRequired)) + } + + @Throws(ConfigurationException::class) + fun saveTo(configuration: Configuration) { + configuration.getIntValue("Option/ConfigurationVersion").value = 0 + configuration.getIntValue("Option/InsertionDelay").value = _insertionDelay.real + configuration.getIntValue("Option/PostsPerPage").value = _postsPerPage.real + configuration.getIntValue("Option/ImagesPerPage").value = _imagesPerPage.real + configuration.getIntValue("Option/CharactersPerPost").value = _charactersPerPost.real + configuration.getIntValue("Option/PostCutOffLength").value = _postCutOffLength.real + configuration.getBooleanValue("Option/RequireFullAccess").value = _requireFullAccess.real + configuration.getIntValue("Option/PositiveTrust").value = _positiveTrust.real + configuration.getIntValue("Option/NegativeTrust").value = _negativeTrust.real + configuration.getStringValue("Option/TrustComment").value = _trustComment.real + configuration.getBooleanValue("Option/ActivateFcpInterface").value = _fcpInterfaceActive.real + configuration.getIntValue("Option/FcpFullAccessRequired").value = toInt(_fcpFullAccessRequired.real) + } + + private fun toInt(fullAccessRequired: FullAccessRequired?): Int? { + return fullAccessRequired?.ordinal + } + +} + +private val unsupported: Nothing get() = throw UnsupportedOperationException()