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 net.pterodactylus.sone.core.event.InsertionDelayChangedEvent;
21 import net.pterodactylus.sone.fcp.FcpInterface;
22 import net.pterodactylus.sone.fcp.FcpInterface.FullAccessRequired;
23 import net.pterodactylus.sone.fcp.event.FcpInterfaceActivatedEvent;
24 import net.pterodactylus.sone.fcp.event.FcpInterfaceDeactivatedEvent;
26 import com.google.common.eventbus.EventBus;
29 * Convenience interface for external classes that want to access the core’s
32 * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
34 public class Preferences {
36 private final EventBus eventBus;
37 private final Options options;
39 public Preferences(EventBus eventBus, Options options) {
40 this.eventBus = eventBus;
41 this.options = options;
45 * Returns the insertion delay.
47 * @return The insertion delay
49 public int getInsertionDelay() {
50 return options.getIntegerOption("InsertionDelay").get();
54 * Validates the given insertion delay.
56 * @param insertionDelay
57 * The insertion delay to validate
58 * @return {@code true} if the given insertion delay was valid,
59 * {@code false} otherwise
61 public boolean validateInsertionDelay(Integer insertionDelay) {
62 return options.getIntegerOption("InsertionDelay").validate(insertionDelay);
66 * Sets the insertion delay
68 * @param insertionDelay
69 * The new insertion delay, or {@code null} to restore it to
71 * @return This preferences
73 public Preferences setInsertionDelay(Integer insertionDelay) {
74 options.getIntegerOption("InsertionDelay").set(insertionDelay);
75 eventBus.post(new InsertionDelayChangedEvent(getInsertionDelay()));
80 * Returns the number of posts to show per page.
82 * @return The number of posts to show per page
84 public int getPostsPerPage() {
85 return options.getIntegerOption("PostsPerPage").get();
89 * Validates the number of posts per page.
92 * The number of posts per page
93 * @return {@code true} if the number of posts per page was valid,
94 * {@code false} otherwise
96 public boolean validatePostsPerPage(Integer postsPerPage) {
97 return options.getIntegerOption("PostsPerPage").validate(postsPerPage);
101 * Sets the number of posts to show per page.
103 * @param postsPerPage
104 * The number of posts to show per page
105 * @return This preferences object
107 public Preferences setPostsPerPage(Integer postsPerPage) {
108 options.getIntegerOption("PostsPerPage").set(postsPerPage);
113 * Returns the number of images to show per page.
115 * @return The number of images to show per page
117 public int getImagesPerPage() {
118 return options.getIntegerOption("ImagesPerPage").get();
122 * Validates the number of images per page.
124 * @param imagesPerPage
125 * The number of images per page
126 * @return {@code true} if the number of images per page was valid,
127 * {@code false} otherwise
129 public boolean validateImagesPerPage(Integer imagesPerPage) {
130 return options.getIntegerOption("ImagesPerPage").validate(imagesPerPage);
134 * Sets the number of images per page.
136 * @param imagesPerPage
137 * The number of images per page
138 * @return This preferences object
140 public Preferences setImagesPerPage(Integer imagesPerPage) {
141 options.getIntegerOption("ImagesPerPage").set(imagesPerPage);
146 * Returns the number of characters per post, or <code>-1</code> if the
147 * posts should not be cut off.
149 * @return The numbers of characters per post
151 public int getCharactersPerPost() {
152 return options.getIntegerOption("CharactersPerPost").get();
156 * Validates the number of characters per post.
158 * @param charactersPerPost
159 * The number of characters per post
160 * @return {@code true} if the number of characters per post was valid,
161 * {@code false} otherwise
163 public boolean validateCharactersPerPost(Integer charactersPerPost) {
164 return options.getIntegerOption("CharactersPerPost").validate(charactersPerPost);
168 * Sets the number of characters per post.
170 * @param charactersPerPost
171 * The number of characters per post, or <code>-1</code> to
172 * not cut off the posts
173 * @return This preferences objects
175 public Preferences setCharactersPerPost(Integer charactersPerPost) {
176 options.getIntegerOption("CharactersPerPost").set(charactersPerPost);
181 * Returns the number of characters the shortened post should have.
183 * @return The number of characters of the snippet
185 public int getPostCutOffLength() {
186 return options.getIntegerOption("PostCutOffLength").get();
190 * Validates the number of characters after which to cut off the post.
192 * @param postCutOffLength
193 * The number of characters of the snippet
194 * @return {@code true} if the number of characters of the snippet is
195 * valid, {@code false} otherwise
197 public boolean validatePostCutOffLength(Integer postCutOffLength) {
198 return options.getIntegerOption("PostCutOffLength").validate(postCutOffLength);
202 * Sets the number of characters the shortened post should have.
204 * @param postCutOffLength
205 * The number of characters of the snippet
206 * @return This preferences
208 public Preferences setPostCutOffLength(Integer postCutOffLength) {
209 options.getIntegerOption("PostCutOffLength").set(postCutOffLength);
214 * Returns whether Sone requires full access to be even visible.
216 * @return {@code true} if Sone requires full access, {@code false}
219 public boolean isRequireFullAccess() {
220 return options.getBooleanOption("RequireFullAccess").get();
224 * Sets whether Sone requires full access to be even visible.
226 * @param requireFullAccess
227 * {@code true} if Sone requires full access, {@code false}
230 public void setRequireFullAccess(Boolean requireFullAccess) {
231 options.getBooleanOption("RequireFullAccess").set(requireFullAccess);
235 * Returns the positive trust.
237 * @return The positive trust
239 public int getPositiveTrust() {
240 return options.getIntegerOption("PositiveTrust").get();
244 * Validates the positive trust.
246 * @param positiveTrust
247 * The positive trust to validate
248 * @return {@code true} if the positive trust was valid, {@code false}
251 public boolean validatePositiveTrust(Integer positiveTrust) {
252 return options.getIntegerOption("PositiveTrust").validate(positiveTrust);
256 * Sets the positive trust.
258 * @param positiveTrust
259 * The new positive trust, or {@code null} to restore it to
261 * @return This preferences
263 public Preferences setPositiveTrust(Integer positiveTrust) {
264 options.getIntegerOption("PositiveTrust").set(positiveTrust);
269 * Returns the negative trust.
271 * @return The negative trust
273 public int getNegativeTrust() {
274 return options.getIntegerOption("NegativeTrust").get();
278 * Validates the negative trust.
280 * @param negativeTrust
281 * The negative trust to validate
282 * @return {@code true} if the negative trust was valid, {@code false}
285 public boolean validateNegativeTrust(Integer negativeTrust) {
286 return options.getIntegerOption("NegativeTrust").validate(negativeTrust);
290 * Sets the negative trust.
292 * @param negativeTrust
293 * The negative trust, or {@code null} to restore it to the
295 * @return The preferences
297 public Preferences setNegativeTrust(Integer negativeTrust) {
298 options.getIntegerOption("NegativeTrust").set(negativeTrust);
303 * Returns the trust comment. This is the comment that is set in the web
304 * of trust when a trust value is assigned to an identity.
306 * @return The trust comment
308 public String getTrustComment() {
309 return options.getStringOption("TrustComment").get();
313 * Sets the trust comment.
315 * @param trustComment
316 * The trust comment, or {@code null} to restore it to the
318 * @return This preferences
320 public Preferences setTrustComment(String trustComment) {
321 options.getStringOption("TrustComment").set(trustComment);
326 * Returns whether the {@link FcpInterface FCP interface} is currently
329 * @see FcpInterface#setActive(boolean)
330 * @return {@code true} if the FCP interface is currently active,
331 * {@code false} otherwise
333 public boolean isFcpInterfaceActive() {
334 return options.getBooleanOption("ActivateFcpInterface").get();
338 * Sets whether the {@link FcpInterface FCP interface} is currently
341 * @see FcpInterface#setActive(boolean)
342 * @param fcpInterfaceActive
343 * {@code true} to activate the FCP interface, {@code false}
344 * to deactivate the FCP interface
345 * @return This preferences object
347 public Preferences setFcpInterfaceActive(boolean fcpInterfaceActive) {
348 options.getBooleanOption("ActivateFcpInterface").set(fcpInterfaceActive);
349 if (fcpInterfaceActive) {
350 eventBus.post(new FcpInterfaceActivatedEvent());
352 eventBus.post(new FcpInterfaceDeactivatedEvent());
358 * Returns the action level for which full access to the FCP interface
361 * @return The action level for which full access to the FCP interface
364 public FullAccessRequired getFcpFullAccessRequired() {
365 return FullAccessRequired.values()[options.getIntegerOption("FcpFullAccessRequired").get()];
369 * Sets the action level for which full access to the FCP interface is
372 * @param fcpFullAccessRequired
374 * @return This preferences
376 public Preferences setFcpFullAccessRequired(FullAccessRequired fcpFullAccessRequired) {
377 options.getIntegerOption("FcpFullAccessRequired").set((fcpFullAccessRequired != null) ? fcpFullAccessRequired.ordinal() : null);