2 * Sone - Preferences.kt - Copyright © 2013–2020 David Roden
4 * This program is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation, either version 3 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program. If not, see <http://www.gnu.org/licenses/>.
18 package net.pterodactylus.sone.core
20 import com.google.common.eventbus.EventBus
21 import net.pterodactylus.sone.core.event.InsertionDelayChangedEvent
22 import net.pterodactylus.sone.core.event.StrictFilteringActivatedEvent
23 import net.pterodactylus.sone.core.event.StrictFilteringDeactivatedEvent
24 import net.pterodactylus.sone.fcp.FcpInterface.FullAccessRequired
25 import net.pterodactylus.sone.fcp.FcpInterface.FullAccessRequired.ALWAYS
26 import net.pterodactylus.sone.fcp.event.FcpInterfaceActivatedEvent
27 import net.pterodactylus.sone.fcp.event.FcpInterfaceDeactivatedEvent
28 import net.pterodactylus.sone.fcp.event.FullAccessRequiredChanged
29 import net.pterodactylus.sone.utils.DefaultOption
30 import net.pterodactylus.util.config.Configuration
31 import net.pterodactylus.util.config.ConfigurationException
32 import java.lang.Integer.MAX_VALUE
35 * Convenience interface for external classes that want to access the core’s
38 class Preferences(private val eventBus: EventBus) {
40 private val _insertionDelay = DefaultOption(60) { it in 0..MAX_VALUE }
41 val insertionDelay: Int get() = _insertionDelay.get()
42 var newInsertionDelay: Int?
45 _insertionDelay.set(value)
46 eventBus.post(InsertionDelayChangedEvent(insertionDelay))
47 eventBus.post(PreferenceChangedEvent("InsertionDelay", insertionDelay))
50 private val _postsPerPage = DefaultOption(10) { it in 1..MAX_VALUE }
51 val postsPerPage: Int get() = _postsPerPage.get()
52 var newPostsPerPage: Int?
55 _postsPerPage.set(value)
56 eventBus.post(PreferenceChangedEvent("PostsPerPage", postsPerPage))
59 private val _imagesPerPage = DefaultOption(9) { it in 1..MAX_VALUE }
60 val imagesPerPage: Int get() = _imagesPerPage.get()
61 var newImagesPerPage: Int?
63 set(value: Int?) = _imagesPerPage.set(value)
65 private val _charactersPerPost = DefaultOption(400) { it == -1 || it in 50..MAX_VALUE }
66 val charactersPerPost: Int get() = _charactersPerPost.get()
67 var newCharactersPerPost: Int?
69 set(value) = _charactersPerPost.set(value)
71 private val _postCutOffLength = DefaultOption(200) { it in 50..MAX_VALUE }
72 val postCutOffLength: Int get() = _postCutOffLength.get()
73 var newPostCutOffLength: Int?
75 set(value) = _postCutOffLength.set(value)
77 private val _requireFullAccess = DefaultOption(false)
78 val requireFullAccess: Boolean get() = _requireFullAccess.get()
79 var newRequireFullAccess: Boolean?
81 set(value) = _requireFullAccess.set(value)
83 private val _fcpInterfaceActive = DefaultOption(false)
84 val fcpInterfaceActive: Boolean get() = _fcpInterfaceActive.get()
85 var newFcpInterfaceActive: Boolean?
88 _fcpInterfaceActive.set(value)
90 true -> eventBus.post(FcpInterfaceActivatedEvent())
91 else -> eventBus.post(FcpInterfaceDeactivatedEvent())
95 private val _fcpFullAccessRequired = DefaultOption(ALWAYS)
96 val fcpFullAccessRequired: FullAccessRequired get() = _fcpFullAccessRequired.get()
97 var newFcpFullAccessRequired: FullAccessRequired?
100 _fcpFullAccessRequired.set(value)
101 eventBus.post(FullAccessRequiredChanged(fcpFullAccessRequired))
104 private val _strictFiltering = DefaultOption(false)
105 val strictFiltering: Boolean get() = _strictFiltering.get()
106 var newStrictFiltering: Boolean? = false
108 _strictFiltering.set(value)
109 when (strictFiltering) {
110 true -> eventBus.post(StrictFilteringActivatedEvent())
111 else -> eventBus.post(StrictFilteringDeactivatedEvent())
115 @Throws(ConfigurationException::class)
116 fun saveTo(configuration: Configuration) {
117 configuration.getIntValue("Option/ConfigurationVersion").value = 0
118 configuration.getIntValue("Option/InsertionDelay").value = _insertionDelay.real
119 configuration.getIntValue("Option/PostsPerPage").value = _postsPerPage.real
120 configuration.getIntValue("Option/ImagesPerPage").value = _imagesPerPage.real
121 configuration.getIntValue("Option/CharactersPerPost").value = _charactersPerPost.real
122 configuration.getIntValue("Option/PostCutOffLength").value = _postCutOffLength.real
123 configuration.getBooleanValue("Option/RequireFullAccess").value = _requireFullAccess.real
124 configuration.getBooleanValue("Option/ActivateFcpInterface").value = _fcpInterfaceActive.real
125 configuration.getIntValue("Option/FcpFullAccessRequired").value = toInt(_fcpFullAccessRequired.real)
126 configuration.getBooleanValue("Option/StrictFiltering").value = _strictFiltering.real
129 private fun toInt(fullAccessRequired: FullAccessRequired?): Int? {
130 return fullAccessRequired?.ordinal
135 private val unsupported: Nothing get() = throw UnsupportedOperationException()