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;
25 import net.pterodactylus.sone.fcp.event.FullAccessRequiredChanged;
27 import com.google.common.eventbus.EventBus;
30 * Convenience interface for external classes that want to access the core’s
33 * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
35 public class Preferences {
37 private final EventBus eventBus;
38 private final Options options;
40 public Preferences(EventBus eventBus, Options options) {
41 this.eventBus = eventBus;
42 this.options = options;
46 * Returns the insertion delay.
48 * @return The insertion delay
50 public int getInsertionDelay() {
51 return options.getIntegerOption("InsertionDelay").get();
55 * Validates the given insertion delay.
57 * @param insertionDelay
58 * The insertion delay to validate
59 * @return {@code true} if the given insertion delay was valid,
60 * {@code false} otherwise
62 public boolean validateInsertionDelay(Integer insertionDelay) {
63 return options.getIntegerOption("InsertionDelay").validate(insertionDelay);
67 * Sets the insertion delay
69 * @param insertionDelay
70 * The new insertion delay, or {@code null} to restore it to
72 * @return This preferences
74 public Preferences setInsertionDelay(Integer insertionDelay) {
75 options.getIntegerOption("InsertionDelay").set(insertionDelay);
76 eventBus.post(new InsertionDelayChangedEvent(getInsertionDelay()));
81 * Returns the number of posts to show per page.
83 * @return The number of posts to show per page
85 public int getPostsPerPage() {
86 return options.getIntegerOption("PostsPerPage").get();
90 * Validates the number of posts per page.
93 * The number of posts per page
94 * @return {@code true} if the number of posts per page was valid,
95 * {@code false} otherwise
97 public boolean validatePostsPerPage(Integer postsPerPage) {
98 return options.getIntegerOption("PostsPerPage").validate(postsPerPage);
102 * Sets the number of posts to show per page.
104 * @param postsPerPage
105 * The number of posts to show per page
106 * @return This preferences object
108 public Preferences setPostsPerPage(Integer postsPerPage) {
109 options.getIntegerOption("PostsPerPage").set(postsPerPage);
114 * Returns the number of images to show per page.
116 * @return The number of images to show per page
118 public int getImagesPerPage() {
119 return options.getIntegerOption("ImagesPerPage").get();
123 * Validates the number of images per page.
125 * @param imagesPerPage
126 * The number of images per page
127 * @return {@code true} if the number of images per page was valid,
128 * {@code false} otherwise
130 public boolean validateImagesPerPage(Integer imagesPerPage) {
131 return options.getIntegerOption("ImagesPerPage").validate(imagesPerPage);
135 * Sets the number of images per page.
137 * @param imagesPerPage
138 * The number of images per page
139 * @return This preferences object
141 public Preferences setImagesPerPage(Integer imagesPerPage) {
142 options.getIntegerOption("ImagesPerPage").set(imagesPerPage);
147 * Returns the number of characters per post, or <code>-1</code> if the
148 * posts should not be cut off.
150 * @return The numbers of characters per post
152 public int getCharactersPerPost() {
153 return options.getIntegerOption("CharactersPerPost").get();
157 * Validates the number of characters per post.
159 * @param charactersPerPost
160 * The number of characters per post
161 * @return {@code true} if the number of characters per post was valid,
162 * {@code false} otherwise
164 public boolean validateCharactersPerPost(Integer charactersPerPost) {
165 return options.getIntegerOption("CharactersPerPost").validate(charactersPerPost);
169 * Sets the number of characters per post.
171 * @param charactersPerPost
172 * The number of characters per post, or <code>-1</code> to
173 * not cut off the posts
174 * @return This preferences objects
176 public Preferences setCharactersPerPost(Integer charactersPerPost) {
177 options.getIntegerOption("CharactersPerPost").set(charactersPerPost);
182 * Returns the number of characters the shortened post should have.
184 * @return The number of characters of the snippet
186 public int getPostCutOffLength() {
187 return options.getIntegerOption("PostCutOffLength").get();
191 * Validates the number of characters after which to cut off the post.
193 * @param postCutOffLength
194 * The number of characters of the snippet
195 * @return {@code true} if the number of characters of the snippet is
196 * valid, {@code false} otherwise
198 public boolean validatePostCutOffLength(Integer postCutOffLength) {
199 return options.getIntegerOption("PostCutOffLength").validate(postCutOffLength);
203 * Sets the number of characters the shortened post should have.
205 * @param postCutOffLength
206 * The number of characters of the snippet
207 * @return This preferences
209 public Preferences setPostCutOffLength(Integer postCutOffLength) {
210 options.getIntegerOption("PostCutOffLength").set(postCutOffLength);
215 * Returns whether Sone requires full access to be even visible.
217 * @return {@code true} if Sone requires full access, {@code false}
220 public boolean isRequireFullAccess() {
221 return options.getBooleanOption("RequireFullAccess").get();
225 * Sets whether Sone requires full access to be even visible.
227 * @param requireFullAccess
228 * {@code true} if Sone requires full access, {@code false}
231 public void setRequireFullAccess(Boolean requireFullAccess) {
232 options.getBooleanOption("RequireFullAccess").set(requireFullAccess);
236 * Returns the positive trust.
238 * @return The positive trust
240 public int getPositiveTrust() {
241 return options.getIntegerOption("PositiveTrust").get();
245 * Validates the positive trust.
247 * @param positiveTrust
248 * The positive trust to validate
249 * @return {@code true} if the positive trust was valid, {@code false}
252 public boolean validatePositiveTrust(Integer positiveTrust) {
253 return options.getIntegerOption("PositiveTrust").validate(positiveTrust);
257 * Sets the positive trust.
259 * @param positiveTrust
260 * The new positive trust, or {@code null} to restore it to
262 * @return This preferences
264 public Preferences setPositiveTrust(Integer positiveTrust) {
265 options.getIntegerOption("PositiveTrust").set(positiveTrust);
270 * Returns the negative trust.
272 * @return The negative trust
274 public int getNegativeTrust() {
275 return options.getIntegerOption("NegativeTrust").get();
279 * Validates the negative trust.
281 * @param negativeTrust
282 * The negative trust to validate
283 * @return {@code true} if the negative trust was valid, {@code false}
286 public boolean validateNegativeTrust(Integer negativeTrust) {
287 return options.getIntegerOption("NegativeTrust").validate(negativeTrust);
291 * Sets the negative trust.
293 * @param negativeTrust
294 * The negative trust, or {@code null} to restore it to the
296 * @return The preferences
298 public Preferences setNegativeTrust(Integer negativeTrust) {
299 options.getIntegerOption("NegativeTrust").set(negativeTrust);
304 * Returns the trust comment. This is the comment that is set in the web
305 * of trust when a trust value is assigned to an identity.
307 * @return The trust comment
309 public String getTrustComment() {
310 return options.getStringOption("TrustComment").get();
314 * Sets the trust comment.
316 * @param trustComment
317 * The trust comment, or {@code null} to restore it to the
319 * @return This preferences
321 public Preferences setTrustComment(String trustComment) {
322 options.getStringOption("TrustComment").set(trustComment);
327 * Returns whether the {@link FcpInterface FCP interface} is currently
330 * @see FcpInterface#setActive(boolean)
331 * @return {@code true} if the FCP interface is currently active,
332 * {@code false} otherwise
334 public boolean isFcpInterfaceActive() {
335 return options.getBooleanOption("ActivateFcpInterface").get();
339 * Sets whether the {@link FcpInterface FCP interface} is currently
342 * @see FcpInterface#setActive(boolean)
343 * @param fcpInterfaceActive
344 * {@code true} to activate the FCP interface, {@code false}
345 * to deactivate the FCP interface
346 * @return This preferences object
348 public Preferences setFcpInterfaceActive(boolean fcpInterfaceActive) {
349 options.getBooleanOption("ActivateFcpInterface").set(fcpInterfaceActive);
350 if (fcpInterfaceActive) {
351 eventBus.post(new FcpInterfaceActivatedEvent());
353 eventBus.post(new FcpInterfaceDeactivatedEvent());
359 * Returns the action level for which full access to the FCP interface
362 * @return The action level for which full access to the FCP interface
365 public FullAccessRequired getFcpFullAccessRequired() {
366 return FullAccessRequired.values()[options.getIntegerOption("FcpFullAccessRequired").get()];
370 * Sets the action level for which full access to the FCP interface is
373 * @param fcpFullAccessRequired
375 * @return This preferences
377 public Preferences setFcpFullAccessRequired(FullAccessRequired fcpFullAccessRequired) {
378 options.getIntegerOption("FcpFullAccessRequired").set((fcpFullAccessRequired != null) ? fcpFullAccessRequired.ordinal() : null);
379 eventBus.post(new FullAccessRequiredChanged(fcpFullAccessRequired));