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