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