5a7a46497833a80627ab6624c21f25b9d2b86a74
[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.fcp.FcpInterface.FullAccessRequired
23 import net.pterodactylus.sone.fcp.FcpInterface.FullAccessRequired.ALWAYS
24 import net.pterodactylus.sone.fcp.event.FcpInterfaceActivatedEvent
25 import net.pterodactylus.sone.fcp.event.FcpInterfaceDeactivatedEvent
26 import net.pterodactylus.sone.fcp.event.FullAccessRequiredChanged
27 import net.pterodactylus.sone.utils.DefaultOption
28 import net.pterodactylus.util.config.Configuration
29 import net.pterodactylus.util.config.ConfigurationException
30 import java.lang.Integer.MAX_VALUE
31
32 /**
33  * Convenience interface for external classes that want to access the core’s
34  * configuration.
35  */
36 class Preferences(private val eventBus: EventBus) {
37
38         private val _insertionDelay = DefaultOption(60) { it in 0..MAX_VALUE }
39         val insertionDelay: Int get() = _insertionDelay.get()
40         var newInsertionDelay: Int?
41                 get() = unsupported
42                 set(value) {
43                         _insertionDelay.set(value)
44                         eventBus.post(InsertionDelayChangedEvent(insertionDelay))
45                         eventBus.post(PreferenceChangedEvent("InsertionDelay", insertionDelay))
46                 }
47
48         private val _postsPerPage = DefaultOption(10) { it in 1..MAX_VALUE }
49         val postsPerPage: Int get() = _postsPerPage.get()
50         var newPostsPerPage: Int?
51                 get() = unsupported
52                 set(value) {
53                         _postsPerPage.set(value)
54                         eventBus.post(PreferenceChangedEvent("PostsPerPage", postsPerPage))
55                 }
56
57         private val _imagesPerPage = DefaultOption(9) { it in 1..MAX_VALUE }
58         val imagesPerPage: Int get() = _imagesPerPage.get()
59         var newImagesPerPage: Int?
60                 get() = unsupported
61                 set(value: Int?) = _imagesPerPage.set(value)
62
63         private val _charactersPerPost = DefaultOption(400) { it == -1 || it in 50..MAX_VALUE }
64         val charactersPerPost: Int get() = _charactersPerPost.get()
65         var newCharactersPerPost: Int?
66                 get() = unsupported
67                 set(value) = _charactersPerPost.set(value)
68
69         private val _postCutOffLength = DefaultOption(200) { it in 50..MAX_VALUE }
70         val postCutOffLength: Int get() = _postCutOffLength.get()
71         var newPostCutOffLength: Int?
72                 get() = unsupported
73                 set(value) = _postCutOffLength.set(value)
74
75         private val _requireFullAccess = DefaultOption(false)
76         val requireFullAccess: Boolean get() = _requireFullAccess.get()
77         var newRequireFullAccess: Boolean?
78                 get() = unsupported
79                 set(value) = _requireFullAccess.set(value)
80
81         private val _fcpInterfaceActive = DefaultOption(false)
82         val fcpInterfaceActive: Boolean get() = _fcpInterfaceActive.get()
83         var newFcpInterfaceActive: Boolean?
84                 get() = unsupported
85                 set(value) {
86                         _fcpInterfaceActive.set(value)
87                         when (value) {
88                                 true -> eventBus.post(FcpInterfaceActivatedEvent())
89                                 else -> eventBus.post(FcpInterfaceDeactivatedEvent())
90                         }
91                 }
92
93         private val _fcpFullAccessRequired = DefaultOption(ALWAYS)
94         val fcpFullAccessRequired: FullAccessRequired get() = _fcpFullAccessRequired.get()
95         var newFcpFullAccessRequired: FullAccessRequired?
96                 get() = unsupported
97                 set(value) {
98                         _fcpFullAccessRequired.set(value)
99                         eventBus.post(FullAccessRequiredChanged(fcpFullAccessRequired))
100                 }
101
102         @Throws(ConfigurationException::class)
103         fun saveTo(configuration: Configuration) {
104                 configuration.getIntValue("Option/ConfigurationVersion").value = 0
105                 configuration.getIntValue("Option/InsertionDelay").value = _insertionDelay.real
106                 configuration.getIntValue("Option/PostsPerPage").value = _postsPerPage.real
107                 configuration.getIntValue("Option/ImagesPerPage").value = _imagesPerPage.real
108                 configuration.getIntValue("Option/CharactersPerPost").value = _charactersPerPost.real
109                 configuration.getIntValue("Option/PostCutOffLength").value = _postCutOffLength.real
110                 configuration.getBooleanValue("Option/RequireFullAccess").value = _requireFullAccess.real
111                 configuration.getBooleanValue("Option/ActivateFcpInterface").value = _fcpInterfaceActive.real
112                 configuration.getIntValue("Option/FcpFullAccessRequired").value = toInt(_fcpFullAccessRequired.real)
113         }
114
115         private fun toInt(fullAccessRequired: FullAccessRequired?): Int? {
116                 return fullAccessRequired?.ordinal
117         }
118
119 }
120
121 private val unsupported: Nothing get() = throw UnsupportedOperationException()