Replace reply-specific interfaces with Kotlin versions
[Sone.git] / src / main / java / net / pterodactylus / sone / core / Preferences.java
1 /*
2  * Sone - Preferences.java - Copyright © 2013–2016 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 static com.google.common.base.Predicates.equalTo;
21 import static java.lang.Integer.MAX_VALUE;
22 import static net.pterodactylus.sone.fcp.FcpInterface.FullAccessRequired.ALWAYS;
23 import static net.pterodactylus.sone.utils.IntegerRangePredicate.range;
24
25 import net.pterodactylus.sone.core.event.InsertionDelayChangedEvent;
26 import net.pterodactylus.sone.fcp.FcpInterface;
27 import net.pterodactylus.sone.fcp.FcpInterface.FullAccessRequired;
28 import net.pterodactylus.sone.fcp.event.FcpInterfaceActivatedEvent;
29 import net.pterodactylus.sone.fcp.event.FcpInterfaceDeactivatedEvent;
30 import net.pterodactylus.sone.fcp.event.FullAccessRequiredChanged;
31 import net.pterodactylus.sone.utils.DefaultOption;
32 import net.pterodactylus.sone.utils.Option;
33 import net.pterodactylus.util.config.Configuration;
34 import net.pterodactylus.util.config.ConfigurationException;
35
36 import com.google.common.base.Predicates;
37 import com.google.common.eventbus.EventBus;
38
39 /**
40  * Convenience interface for external classes that want to access the core’s
41  * configuration.
42  *
43  * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
44  */
45 public class Preferences {
46
47         private final EventBus eventBus;
48         private final Option<Integer> insertionDelay =
49                         new DefaultOption<Integer>(60, range(0, MAX_VALUE));
50         private final Option<Integer> postsPerPage =
51                         new DefaultOption<Integer>(10, range(1, MAX_VALUE));
52         private final Option<Integer> imagesPerPage =
53                         new DefaultOption<Integer>(9, range(1, MAX_VALUE));
54         private final Option<Integer> charactersPerPost =
55                         new DefaultOption<Integer>(400, Predicates.<Integer>or(
56                                         range(50, MAX_VALUE), equalTo(-1)));
57         private final Option<Integer> postCutOffLength =
58                         new DefaultOption<Integer>(200, range(50, MAX_VALUE));
59         private final Option<Boolean> requireFullAccess =
60                         new DefaultOption<Boolean>(false);
61         private final Option<Integer> positiveTrust =
62                         new DefaultOption<Integer>(75, range(0, 100));
63         private final Option<Integer> negativeTrust =
64                         new DefaultOption<Integer>(-25, range(-100, 100));
65         private final Option<String> trustComment =
66                         new DefaultOption<String>("Set from Sone Web Interface");
67         private final Option<Boolean> activateFcpInterface =
68                         new DefaultOption<Boolean>(false);
69         private final Option<FullAccessRequired> fcpFullAccessRequired =
70                         new DefaultOption<FullAccessRequired>(ALWAYS);
71
72         public Preferences(EventBus eventBus) {
73                 this.eventBus = eventBus;
74         }
75
76         /**
77          * Returns the insertion delay.
78          *
79          * @return The insertion delay
80          */
81         public int getInsertionDelay() {
82                 return insertionDelay.get();
83         }
84
85         /**
86          * Validates the given insertion delay.
87          *
88          * @param insertionDelay
89          *            The insertion delay to validate
90          * @return {@code true} if the given insertion delay was valid,
91          *         {@code false} otherwise
92          */
93         public boolean validateInsertionDelay(Integer insertionDelay) {
94                 return this.insertionDelay.validate(insertionDelay);
95         }
96
97         /**
98          * Sets the insertion delay
99          *
100          * @param insertionDelay
101          *            The new insertion delay, or {@code null} to restore it to
102          *            the default value
103          * @return This preferences
104          */
105         public Preferences setInsertionDelay(Integer insertionDelay) {
106                 this.insertionDelay.set(insertionDelay);
107                 eventBus.post(new InsertionDelayChangedEvent(getInsertionDelay()));
108                 return this;
109         }
110
111         /**
112          * Returns the number of posts to show per page.
113          *
114          * @return The number of posts to show per page
115          */
116         public int getPostsPerPage() {
117                 return postsPerPage.get();
118         }
119
120         /**
121          * Validates the number of posts per page.
122          *
123          * @param postsPerPage
124          *            The number of posts per page
125          * @return {@code true} if the number of posts per page was valid,
126          *         {@code false} otherwise
127          */
128         public boolean validatePostsPerPage(Integer postsPerPage) {
129                 return this.postsPerPage.validate(postsPerPage);
130         }
131
132         /**
133          * Sets the number of posts to show per page.
134          *
135          * @param postsPerPage
136          *            The number of posts to show per page
137          * @return This preferences object
138          */
139         public Preferences setPostsPerPage(Integer postsPerPage) {
140                 this.postsPerPage.set(postsPerPage);
141                 return this;
142         }
143
144         /**
145          * Returns the number of images to show per page.
146          *
147          * @return The number of images to show per page
148          */
149         public int getImagesPerPage() {
150                 return imagesPerPage.get();
151         }
152
153         /**
154          * Validates the number of images per page.
155          *
156          * @param imagesPerPage
157          *            The number of images per page
158          * @return {@code true} if the number of images per page was valid,
159          *         {@code false} otherwise
160          */
161         public boolean validateImagesPerPage(Integer imagesPerPage) {
162                 return this.imagesPerPage.validate(imagesPerPage);
163         }
164
165         /**
166          * Sets the number of images per page.
167          *
168          * @param imagesPerPage
169          *            The number of images per page
170          * @return This preferences object
171          */
172         public Preferences setImagesPerPage(Integer imagesPerPage) {
173                 this.imagesPerPage.set(imagesPerPage);
174                 return this;
175         }
176
177         /**
178          * Returns the number of characters per post, or <code>-1</code> if the
179          * posts should not be cut off.
180          *
181          * @return The numbers of characters per post
182          */
183         public int getCharactersPerPost() {
184                 return charactersPerPost.get();
185         }
186
187         /**
188          * Validates the number of characters per post.
189          *
190          * @param charactersPerPost
191          *            The number of characters per post
192          * @return {@code true} if the number of characters per post was valid,
193          *         {@code false} otherwise
194          */
195         public boolean validateCharactersPerPost(Integer charactersPerPost) {
196                 return this.charactersPerPost.validate(charactersPerPost);
197         }
198
199         /**
200          * Sets the number of characters per post.
201          *
202          * @param charactersPerPost
203          *            The number of characters per post, or <code>-1</code> to
204          *            not cut off the posts
205          * @return This preferences objects
206          */
207         public Preferences setCharactersPerPost(Integer charactersPerPost) {
208                 this.charactersPerPost.set(charactersPerPost);
209                 return this;
210         }
211
212         /**
213          * Returns the number of characters the shortened post should have.
214          *
215          * @return The number of characters of the snippet
216          */
217         public int getPostCutOffLength() {
218                 return postCutOffLength.get();
219         }
220
221         /**
222          * Validates the number of characters after which to cut off the post.
223          *
224          * @param postCutOffLength
225          *            The number of characters of the snippet
226          * @return {@code true} if the number of characters of the snippet is
227          *         valid, {@code false} otherwise
228          */
229         public boolean validatePostCutOffLength(Integer postCutOffLength) {
230                 return this.postCutOffLength.validate(postCutOffLength);
231         }
232
233         /**
234          * Sets the number of characters the shortened post should have.
235          *
236          * @param postCutOffLength
237          *            The number of characters of the snippet
238          * @return This preferences
239          */
240         public Preferences setPostCutOffLength(Integer postCutOffLength) {
241                 this.postCutOffLength.set(postCutOffLength);
242                 return this;
243         }
244
245         /**
246          * Returns whether Sone requires full access to be even visible.
247          *
248          * @return {@code true} if Sone requires full access, {@code false}
249          *         otherwise
250          */
251         public boolean isRequireFullAccess() {
252                 return requireFullAccess.get();
253         }
254
255         /**
256          * Sets whether Sone requires full access to be even visible.
257          *
258          * @param requireFullAccess
259          *            {@code true} if Sone requires full access, {@code false}
260          *            otherwise
261          */
262         public void setRequireFullAccess(Boolean requireFullAccess) {
263                 this.requireFullAccess.set(requireFullAccess);
264         }
265
266         /**
267          * Returns the positive trust.
268          *
269          * @return The positive trust
270          */
271         public int getPositiveTrust() {
272                 return positiveTrust.get();
273         }
274
275         /**
276          * Validates the positive trust.
277          *
278          * @param positiveTrust
279          *            The positive trust to validate
280          * @return {@code true} if the positive trust was valid, {@code false}
281          *         otherwise
282          */
283         public boolean validatePositiveTrust(Integer positiveTrust) {
284                 return this.positiveTrust.validate(positiveTrust);
285         }
286
287         /**
288          * Sets the positive trust.
289          *
290          * @param positiveTrust
291          *            The new positive trust, or {@code null} to restore it to
292          *            the default vlaue
293          * @return This preferences
294          */
295         public Preferences setPositiveTrust(Integer positiveTrust) {
296                 this.positiveTrust.set(positiveTrust);
297                 return this;
298         }
299
300         /**
301          * Returns the negative trust.
302          *
303          * @return The negative trust
304          */
305         public int getNegativeTrust() {
306                 return negativeTrust.get();
307         }
308
309         /**
310          * Validates the negative trust.
311          *
312          * @param negativeTrust
313          *            The negative trust to validate
314          * @return {@code true} if the negative trust was valid, {@code false}
315          *         otherwise
316          */
317         public boolean validateNegativeTrust(Integer negativeTrust) {
318                 return this.negativeTrust.validate(negativeTrust);
319         }
320
321         /**
322          * Sets the negative trust.
323          *
324          * @param negativeTrust
325          *            The negative trust, or {@code null} to restore it to the
326          *            default value
327          * @return The preferences
328          */
329         public Preferences setNegativeTrust(Integer negativeTrust) {
330                 this.negativeTrust.set(negativeTrust);
331                 return this;
332         }
333
334         /**
335          * Returns the trust comment. This is the comment that is set in the web
336          * of trust when a trust value is assigned to an identity.
337          *
338          * @return The trust comment
339          */
340         public String getTrustComment() {
341                 return trustComment.get();
342         }
343
344         /**
345          * Sets the trust comment.
346          *
347          * @param trustComment
348          *            The trust comment, or {@code null} to restore it to the
349          *            default value
350          * @return This preferences
351          */
352         public Preferences setTrustComment(String trustComment) {
353                 this.trustComment.set(trustComment);
354                 return this;
355         }
356
357         /**
358          * Returns whether the {@link FcpInterface FCP interface} is currently
359          * active.
360          *
361          * @see FcpInterface#setActive(boolean)
362          * @return {@code true} if the FCP interface is currently active,
363          *         {@code false} otherwise
364          */
365         public boolean isFcpInterfaceActive() {
366                 return activateFcpInterface.get();
367         }
368
369         /**
370          * Sets whether the {@link FcpInterface FCP interface} is currently
371          * active.
372          *
373          * @see FcpInterface#setActive(boolean)
374          * @param fcpInterfaceActive
375          *            {@code true} to activate the FCP interface, {@code false}
376          *            to deactivate the FCP interface
377          * @return This preferences object
378          */
379         public Preferences setFcpInterfaceActive(Boolean fcpInterfaceActive) {
380                 this.activateFcpInterface.set(fcpInterfaceActive);
381                 if (isFcpInterfaceActive()) {
382                         eventBus.post(new FcpInterfaceActivatedEvent());
383                 } else {
384                         eventBus.post(new FcpInterfaceDeactivatedEvent());
385                 }
386                 return this;
387         }
388
389         /**
390          * Returns the action level for which full access to the FCP interface
391          * is required.
392          *
393          * @return The action level for which full access to the FCP interface
394          *         is required
395          */
396         public FullAccessRequired getFcpFullAccessRequired() {
397                 return fcpFullAccessRequired.get();
398         }
399
400         /**
401          * Sets the action level for which full access to the FCP interface is
402          * required
403          *
404          * @param fcpFullAccessRequired
405          *            The action level
406          * @return This preferences
407          */
408         public Preferences setFcpFullAccessRequired(
409                         FullAccessRequired fcpFullAccessRequired) {
410                 this.fcpFullAccessRequired.set(fcpFullAccessRequired);
411                 eventBus.post(new FullAccessRequiredChanged(getFcpFullAccessRequired()));
412                 return this;
413         }
414
415         public void saveTo(Configuration configuration) throws ConfigurationException {
416                 configuration.getIntValue("Option/ConfigurationVersion").setValue(0);
417                 configuration.getIntValue("Option/InsertionDelay").setValue(insertionDelay.getReal());
418                 configuration.getIntValue("Option/PostsPerPage").setValue(postsPerPage.getReal());
419                 configuration.getIntValue("Option/ImagesPerPage").setValue(imagesPerPage.getReal());
420                 configuration.getIntValue("Option/CharactersPerPost").setValue(charactersPerPost.getReal());
421                 configuration.getIntValue("Option/PostCutOffLength").setValue(postCutOffLength.getReal());
422                 configuration.getBooleanValue("Option/RequireFullAccess").setValue(requireFullAccess.getReal());
423                 configuration.getIntValue("Option/PositiveTrust").setValue(positiveTrust.getReal());
424                 configuration.getIntValue("Option/NegativeTrust").setValue(negativeTrust.getReal());
425                 configuration.getStringValue("Option/TrustComment").setValue(trustComment.getReal());
426                 configuration.getBooleanValue("Option/ActivateFcpInterface").setValue(activateFcpInterface.getReal());
427                 configuration.getIntValue("Option/FcpFullAccessRequired").setValue(toInt(fcpFullAccessRequired.getReal()));
428         }
429
430         private Integer toInt(FullAccessRequired fullAccessRequired) {
431                 return (fullAccessRequired == null) ? null : fullAccessRequired.ordinal();
432         }
433
434 }