📄 Update copyright lines with correct filenames
[Sone.git] / src / main / kotlin / net / pterodactylus / sone / core / Preferences.kt
1 /*
2  * Sone - Preferences.kt - Copyright Â© 2013–2019 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.base.Predicates.*
21 import com.google.common.eventbus.*
22 import net.pterodactylus.sone.core.event.*
23 import net.pterodactylus.sone.fcp.FcpInterface.*
24 import net.pterodactylus.sone.fcp.FcpInterface.FullAccessRequired.*
25 import net.pterodactylus.sone.fcp.event.*
26 import net.pterodactylus.sone.utils.*
27 import net.pterodactylus.sone.utils.IntegerRangePredicate.*
28 import net.pterodactylus.util.config.*
29 import java.lang.Integer.*
30
31 /**
32  * Convenience interface for external classes that want to access the core’s
33  * configuration.
34  */
35 class Preferences(private val eventBus: EventBus) {
36
37         private val _insertionDelay = DefaultOption(60, range(0, MAX_VALUE))
38         val insertionDelay: Int get() = _insertionDelay.get()
39         var newInsertionDelay: Int?
40                 get() = unsupported
41                 set(value) {
42                         _insertionDelay.set(value)
43                         eventBus.post(InsertionDelayChangedEvent(insertionDelay))
44                         eventBus.post(PreferenceChangedEvent("InsertionDelay", insertionDelay))
45                 }
46
47         private val _postsPerPage = DefaultOption(10, range(1, MAX_VALUE))
48         val postsPerPage: Int get() = _postsPerPage.get()
49         var newPostsPerPage: Int?
50                 get() = unsupported
51                 set(value) {
52                         _postsPerPage.set(value)
53                         eventBus.post(PreferenceChangedEvent("PostsPerPage", postsPerPage))
54                 }
55
56         private val _imagesPerPage = DefaultOption(9, range(1, MAX_VALUE))
57         val imagesPerPage: Int get() = _imagesPerPage.get()
58         var newImagesPerPage: Int?
59                 get() = unsupported
60                 set (value: Int?) = _imagesPerPage.set(value)
61
62         private val _charactersPerPost = DefaultOption(400, or(range(50, MAX_VALUE), equalTo(-1)))
63         val charactersPerPost: Int get() = _charactersPerPost.get()
64         var newCharactersPerPost: Int?
65                 get() = unsupported
66                 set(value) = _charactersPerPost.set(value)
67
68         private val _postCutOffLength = DefaultOption(200, range(50, MAX_VALUE))
69         val postCutOffLength: Int get() = _postCutOffLength.get()
70         var newPostCutOffLength: Int?
71                 get() = unsupported
72                 set(value) = _postCutOffLength.set(value)
73
74         private val _requireFullAccess = DefaultOption(false)
75         val requireFullAccess: Boolean get() = _requireFullAccess.get()
76         var newRequireFullAccess: Boolean?
77                 get() = unsupported
78                 set(value) = _requireFullAccess.set(value)
79
80         private val _positiveTrust = DefaultOption(75, range(0, 100))
81         val positiveTrust: Int get() = _positiveTrust.get()
82         var newPositiveTrust: Int?
83                 get() = unsupported
84                 set(value) = _positiveTrust.set(value)
85
86         private val _negativeTrust = DefaultOption(-25, range(-100, 100))
87         val negativeTrust: Int get() = _negativeTrust.get()
88         var newNegativeTrust: Int?
89                 get() = unsupported
90                 set(value) = _negativeTrust.set(value)
91
92         private val _trustComment = DefaultOption("Set from Sone Web Interface")
93         val trustComment: String get() = _trustComment.get()
94         var newTrustComment: String?
95                 get() = unsupported
96                 set(value) = _trustComment.set(value)
97
98         private val _fcpInterfaceActive = DefaultOption(false)
99         val fcpInterfaceActive: Boolean get() = _fcpInterfaceActive.get()
100         var newFcpInterfaceActive: Boolean?
101                 get() = unsupported
102                 set(value) {
103                         _fcpInterfaceActive.set(value)
104                         when (value) {
105                                 true -> eventBus.post(FcpInterfaceActivatedEvent())
106                                 else -> eventBus.post(FcpInterfaceDeactivatedEvent())
107                         }
108                 }
109
110         private val _fcpFullAccessRequired = DefaultOption(ALWAYS)
111         val fcpFullAccessRequired: FullAccessRequired get() = _fcpFullAccessRequired.get()
112         var newFcpFullAccessRequired: FullAccessRequired?
113                 get() = unsupported
114                 set(value) {
115                         _fcpFullAccessRequired.set(value)
116                         eventBus.post(FullAccessRequiredChanged(fcpFullAccessRequired))
117                 }
118
119         @Throws(ConfigurationException::class)
120         fun saveTo(configuration: Configuration) {
121                 configuration.getIntValue("Option/ConfigurationVersion").value = 0
122                 configuration.getIntValue("Option/InsertionDelay").value = _insertionDelay.real
123                 configuration.getIntValue("Option/PostsPerPage").value = _postsPerPage.real
124                 configuration.getIntValue("Option/ImagesPerPage").value = _imagesPerPage.real
125                 configuration.getIntValue("Option/CharactersPerPost").value = _charactersPerPost.real
126                 configuration.getIntValue("Option/PostCutOffLength").value = _postCutOffLength.real
127                 configuration.getBooleanValue("Option/RequireFullAccess").value = _requireFullAccess.real
128                 configuration.getIntValue("Option/PositiveTrust").value = _positiveTrust.real
129                 configuration.getIntValue("Option/NegativeTrust").value = _negativeTrust.real
130                 configuration.getStringValue("Option/TrustComment").value = _trustComment.real
131                 configuration.getBooleanValue("Option/ActivateFcpInterface").value = _fcpInterfaceActive.real
132                 configuration.getIntValue("Option/FcpFullAccessRequired").value = toInt(_fcpFullAccessRequired.real)
133         }
134
135         private fun toInt(fullAccessRequired: FullAccessRequired?): Int? {
136                 return fullAccessRequired?.ordinal
137         }
138
139 }
140
141 private val unsupported: Nothing get() = throw UnsupportedOperationException()