2 * Sone - Preferences.java - Copyright © 2013 David Roden
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.
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.
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/>.
18 package net.pterodactylus.sone.core;
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;
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;
36 import com.google.common.base.Predicates;
37 import com.google.common.eventbus.EventBus;
40 * Convenience interface for external classes that want to access the core’s
43 * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
45 public class Preferences {
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);
73 public Preferences(EventBus eventBus) {
74 this.eventBus = eventBus;
78 * Returns the insertion delay.
80 * @return The insertion delay
82 public int getInsertionDelay() {
83 return insertionDelay.get();
87 * Validates the given insertion delay.
89 * @param insertionDelay
90 * The insertion delay to validate
91 * @return {@code true} if the given insertion delay was valid,
92 * {@code false} otherwise
94 public boolean validateInsertionDelay(Integer insertionDelay) {
95 return this.insertionDelay.validate(insertionDelay);
99 * Sets the insertion delay
101 * @param insertionDelay
102 * The new insertion delay, or {@code null} to restore it to
104 * @return This preferences
106 public Preferences setInsertionDelay(Integer insertionDelay) {
107 this.insertionDelay.set(insertionDelay);
108 eventBus.post(new InsertionDelayChangedEvent(getInsertionDelay()));
113 * Returns the number of posts to show per page.
115 * @return The number of posts to show per page
117 public int getPostsPerPage() {
118 return postsPerPage.get();
122 * Validates the number of posts per page.
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
129 public boolean validatePostsPerPage(Integer postsPerPage) {
130 return this.postsPerPage.validate(postsPerPage);
134 * Sets the number of posts to show per page.
136 * @param postsPerPage
137 * The number of posts to show per page
138 * @return This preferences object
140 public Preferences setPostsPerPage(Integer postsPerPage) {
141 this.postsPerPage.set(postsPerPage);
146 * Returns the number of images to show per page.
148 * @return The number of images to show per page
150 public int getImagesPerPage() {
151 return imagesPerPage.get();
155 * Validates the number of images per page.
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
162 public boolean validateImagesPerPage(Integer imagesPerPage) {
163 return this.imagesPerPage.validate(imagesPerPage);
167 * Sets the number of images per page.
169 * @param imagesPerPage
170 * The number of images per page
171 * @return This preferences object
173 public Preferences setImagesPerPage(Integer imagesPerPage) {
174 this.imagesPerPage.set(imagesPerPage);
179 * Returns the number of characters per post, or <code>-1</code> if the
180 * posts should not be cut off.
182 * @return The numbers of characters per post
184 public int getCharactersPerPost() {
185 return charactersPerPost.get();
189 * Validates the number of characters per post.
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
196 public boolean validateCharactersPerPost(Integer charactersPerPost) {
197 return this.charactersPerPost.validate(charactersPerPost);
201 * Sets the number of characters per post.
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
208 public Preferences setCharactersPerPost(Integer charactersPerPost) {
209 this.charactersPerPost.set(charactersPerPost);
214 * Returns the number of characters the shortened post should have.
216 * @return The number of characters of the snippet
218 public int getPostCutOffLength() {
219 return postCutOffLength.get();
223 * Validates the number of characters after which to cut off the post.
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
230 public boolean validatePostCutOffLength(Integer postCutOffLength) {
231 return this.postCutOffLength.validate(postCutOffLength);
235 * Sets the number of characters the shortened post should have.
237 * @param postCutOffLength
238 * The number of characters of the snippet
239 * @return This preferences
241 public Preferences setPostCutOffLength(Integer postCutOffLength) {
242 this.postCutOffLength.set(postCutOffLength);
247 * Returns whether Sone requires full access to be even visible.
249 * @return {@code true} if Sone requires full access, {@code false}
252 public boolean isRequireFullAccess() {
253 return requireFullAccess.get();
257 * Sets whether Sone requires full access to be even visible.
259 * @param requireFullAccess
260 * {@code true} if Sone requires full access, {@code false}
263 public void setRequireFullAccess(Boolean requireFullAccess) {
264 this.requireFullAccess.set(requireFullAccess);
268 * Returns the positive trust.
270 * @return The positive trust
272 public int getPositiveTrust() {
273 return positiveTrust.get();
277 * Validates the positive trust.
279 * @param positiveTrust
280 * The positive trust to validate
281 * @return {@code true} if the positive trust was valid, {@code false}
284 public boolean validatePositiveTrust(Integer positiveTrust) {
285 return this.positiveTrust.validate(positiveTrust);
289 * Sets the positive trust.
291 * @param positiveTrust
292 * The new positive trust, or {@code null} to restore it to
294 * @return This preferences
296 public Preferences setPositiveTrust(Integer positiveTrust) {
297 this.positiveTrust.set(positiveTrust);
302 * Returns the negative trust.
304 * @return The negative trust
306 public int getNegativeTrust() {
307 return negativeTrust.get();
311 * Validates the negative trust.
313 * @param negativeTrust
314 * The negative trust to validate
315 * @return {@code true} if the negative trust was valid, {@code false}
318 public boolean validateNegativeTrust(Integer negativeTrust) {
319 return this.negativeTrust.validate(negativeTrust);
323 * Sets the negative trust.
325 * @param negativeTrust
326 * The negative trust, or {@code null} to restore it to the
328 * @return The preferences
330 public Preferences setNegativeTrust(Integer negativeTrust) {
331 this.negativeTrust.set(negativeTrust);
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.
339 * @return The trust comment
341 public String getTrustComment() {
342 return trustComment.get();
346 * Sets the trust comment.
348 * @param trustComment
349 * The trust comment, or {@code null} to restore it to the
351 * @return This preferences
353 public Preferences setTrustComment(String trustComment) {
354 this.trustComment.set(trustComment);
359 * Returns whether the {@link FcpInterface FCP interface} is currently
362 * @see FcpInterface#setActive(boolean)
363 * @return {@code true} if the FCP interface is currently active,
364 * {@code false} otherwise
366 public boolean isFcpInterfaceActive() {
367 return activateFcpInterface.get();
371 * Sets whether the {@link FcpInterface FCP interface} is currently
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
380 public Preferences setFcpInterfaceActive(Boolean fcpInterfaceActive) {
381 this.activateFcpInterface.set(fcpInterfaceActive);
382 if (isFcpInterfaceActive()) {
383 eventBus.post(new FcpInterfaceActivatedEvent());
385 eventBus.post(new FcpInterfaceDeactivatedEvent());
391 * Returns the action level for which full access to the FCP interface
394 * @return The action level for which full access to the FCP interface
397 public FullAccessRequired getFcpFullAccessRequired() {
398 return fcpFullAccessRequired.get();
402 * Sets the action level for which full access to the FCP interface is
405 * @param fcpFullAccessRequired
407 * @return This preferences
409 public Preferences setFcpFullAccessRequired(
410 FullAccessRequired fcpFullAccessRequired) {
411 this.fcpFullAccessRequired.set(fcpFullAccessRequired);
412 eventBus.post(new FullAccessRequiredChanged(getFcpFullAccessRequired()));
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()));
431 private Integer toInt(FullAccessRequired fullAccessRequired) {
432 return (fullAccessRequired == null) ? null : fullAccessRequired.ordinal();