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