🔀 Merge branch 'release/v82'
[Sone.git] / src / main / kotlin / net / pterodactylus / sone / core / Preferences.kt
1 /*
2  * Sone - Preferences.kt - Copyright Â© 2013–2020 David Roden
3  *
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.
8  *
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.
13  *
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/>.
16  */
17
18 package net.pterodactylus.sone.core
19
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
33
34 /**
35  * Convenience interface for external classes that want to access the core’s
36  * configuration.
37  */
38 class Preferences(private val eventBus: EventBus) {
39
40         private val _insertionDelay = DefaultOption(60) { it in 0..MAX_VALUE }
41         val insertionDelay: Int get() = _insertionDelay.get()
42         var newInsertionDelay: Int?
43                 get() = unsupported
44                 set(value) {
45                         _insertionDelay.set(value)
46                         eventBus.post(InsertionDelayChangedEvent(insertionDelay))
47                         eventBus.post(PreferenceChangedEvent("InsertionDelay", insertionDelay))
48                 }
49
50         private val _postsPerPage = DefaultOption(10) { it in 1..MAX_VALUE }
51         val postsPerPage: Int get() = _postsPerPage.get()
52         var newPostsPerPage: Int?
53                 get() = unsupported
54                 set(value) {
55                         _postsPerPage.set(value)
56                         eventBus.post(PreferenceChangedEvent("PostsPerPage", postsPerPage))
57                 }
58
59         private val _imagesPerPage = DefaultOption(9) { it in 1..MAX_VALUE }
60         val imagesPerPage: Int get() = _imagesPerPage.get()
61         var newImagesPerPage: Int?
62                 get() = unsupported
63                 set(value: Int?) = _imagesPerPage.set(value)
64
65         private val _charactersPerPost = DefaultOption(400) { it == -1 || it in 50..MAX_VALUE }
66         val charactersPerPost: Int get() = _charactersPerPost.get()
67         var newCharactersPerPost: Int?
68                 get() = unsupported
69                 set(value) = _charactersPerPost.set(value)
70
71         private val _postCutOffLength = DefaultOption(200) { it in 50..MAX_VALUE }
72         val postCutOffLength: Int get() = _postCutOffLength.get()
73         var newPostCutOffLength: Int?
74                 get() = unsupported
75                 set(value) = _postCutOffLength.set(value)
76
77         private val _requireFullAccess = DefaultOption(false)
78         val requireFullAccess: Boolean get() = _requireFullAccess.get()
79         var newRequireFullAccess: Boolean?
80                 get() = unsupported
81                 set(value) = _requireFullAccess.set(value)
82
83         private val _fcpInterfaceActive = DefaultOption(false)
84         val fcpInterfaceActive: Boolean get() = _fcpInterfaceActive.get()
85         var newFcpInterfaceActive: Boolean?
86                 get() = unsupported
87                 set(value) {
88                         _fcpInterfaceActive.set(value)
89                         when (value) {
90                                 true -> eventBus.post(FcpInterfaceActivatedEvent())
91                                 else -> eventBus.post(FcpInterfaceDeactivatedEvent())
92                         }
93                 }
94
95         private val _fcpFullAccessRequired = DefaultOption(ALWAYS)
96         val fcpFullAccessRequired: FullAccessRequired get() = _fcpFullAccessRequired.get()
97         var newFcpFullAccessRequired: FullAccessRequired?
98                 get() = unsupported
99                 set(value) {
100                         _fcpFullAccessRequired.set(value)
101                         eventBus.post(FullAccessRequiredChanged(fcpFullAccessRequired))
102                 }
103
104         private val _strictFiltering = DefaultOption(false)
105         val strictFiltering: Boolean get() = _strictFiltering.get()
106         var newStrictFiltering: Boolean? = false
107                 set(value) {
108                         _strictFiltering.set(value)
109                         when (strictFiltering) {
110                                 true -> eventBus.post(StrictFilteringActivatedEvent())
111                                 else -> eventBus.post(StrictFilteringDeactivatedEvent())
112                         }
113                 }
114
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
127         }
128
129         private fun toInt(fullAccessRequired: FullAccessRequired?): Int? {
130                 return fullAccessRequired?.ordinal
131         }
132
133 }
134
135 private val unsupported: Nothing get() = throw UnsupportedOperationException()