Use event bus to activate and deactivate the FCP interface.
[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 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
26 import com.google.common.eventbus.EventBus;
27
28 /**
29  * Convenience interface for external classes that want to access the core’s
30  * configuration.
31  *
32  * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
33  */
34 public class Preferences {
35
36         private final EventBus eventBus;
37         private final Options options;
38
39         public Preferences(EventBus eventBus, Options options) {
40                 this.eventBus = eventBus;
41                 this.options = options;
42         }
43
44         /**
45          * Returns the insertion delay.
46          *
47          * @return The insertion delay
48          */
49         public int getInsertionDelay() {
50                 return options.getIntegerOption("InsertionDelay").get();
51         }
52
53         /**
54          * Validates the given insertion delay.
55          *
56          * @param insertionDelay
57          *            The insertion delay to validate
58          * @return {@code true} if the given insertion delay was valid,
59          *         {@code false} otherwise
60          */
61         public boolean validateInsertionDelay(Integer insertionDelay) {
62                 return options.getIntegerOption("InsertionDelay").validate(insertionDelay);
63         }
64
65         /**
66          * Sets the insertion delay
67          *
68          * @param insertionDelay
69          *            The new insertion delay, or {@code null} to restore it to
70          *            the default value
71          * @return This preferences
72          */
73         public Preferences setInsertionDelay(Integer insertionDelay) {
74                 options.getIntegerOption("InsertionDelay").set(insertionDelay);
75                 eventBus.post(new InsertionDelayChangedEvent(getInsertionDelay()));
76                 return this;
77         }
78
79         /**
80          * Returns the number of posts to show per page.
81          *
82          * @return The number of posts to show per page
83          */
84         public int getPostsPerPage() {
85                 return options.getIntegerOption("PostsPerPage").get();
86         }
87
88         /**
89          * Validates the number of posts per page.
90          *
91          * @param postsPerPage
92          *            The number of posts per page
93          * @return {@code true} if the number of posts per page was valid,
94          *         {@code false} otherwise
95          */
96         public boolean validatePostsPerPage(Integer postsPerPage) {
97                 return options.getIntegerOption("PostsPerPage").validate(postsPerPage);
98         }
99
100         /**
101          * Sets the number of posts to show per page.
102          *
103          * @param postsPerPage
104          *            The number of posts to show per page
105          * @return This preferences object
106          */
107         public Preferences setPostsPerPage(Integer postsPerPage) {
108                 options.getIntegerOption("PostsPerPage").set(postsPerPage);
109                 return this;
110         }
111
112         /**
113          * Returns the number of images to show per page.
114          *
115          * @return The number of images to show per page
116          */
117         public int getImagesPerPage() {
118                 return options.getIntegerOption("ImagesPerPage").get();
119         }
120
121         /**
122          * Validates the number of images per page.
123          *
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
128          */
129         public boolean validateImagesPerPage(Integer imagesPerPage) {
130                 return options.getIntegerOption("ImagesPerPage").validate(imagesPerPage);
131         }
132
133         /**
134          * Sets the number of images per page.
135          *
136          * @param imagesPerPage
137          *            The number of images per page
138          * @return This preferences object
139          */
140         public Preferences setImagesPerPage(Integer imagesPerPage) {
141                 options.getIntegerOption("ImagesPerPage").set(imagesPerPage);
142                 return this;
143         }
144
145         /**
146          * Returns the number of characters per post, or <code>-1</code> if the
147          * posts should not be cut off.
148          *
149          * @return The numbers of characters per post
150          */
151         public int getCharactersPerPost() {
152                 return options.getIntegerOption("CharactersPerPost").get();
153         }
154
155         /**
156          * Validates the number of characters per post.
157          *
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
162          */
163         public boolean validateCharactersPerPost(Integer charactersPerPost) {
164                 return options.getIntegerOption("CharactersPerPost").validate(charactersPerPost);
165         }
166
167         /**
168          * Sets the number of characters per post.
169          *
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
174          */
175         public Preferences setCharactersPerPost(Integer charactersPerPost) {
176                 options.getIntegerOption("CharactersPerPost").set(charactersPerPost);
177                 return this;
178         }
179
180         /**
181          * Returns the number of characters the shortened post should have.
182          *
183          * @return The number of characters of the snippet
184          */
185         public int getPostCutOffLength() {
186                 return options.getIntegerOption("PostCutOffLength").get();
187         }
188
189         /**
190          * Validates the number of characters after which to cut off the post.
191          *
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
196          */
197         public boolean validatePostCutOffLength(Integer postCutOffLength) {
198                 return options.getIntegerOption("PostCutOffLength").validate(postCutOffLength);
199         }
200
201         /**
202          * Sets the number of characters the shortened post should have.
203          *
204          * @param postCutOffLength
205          *            The number of characters of the snippet
206          * @return This preferences
207          */
208         public Preferences setPostCutOffLength(Integer postCutOffLength) {
209                 options.getIntegerOption("PostCutOffLength").set(postCutOffLength);
210                 return this;
211         }
212
213         /**
214          * Returns whether Sone requires full access to be even visible.
215          *
216          * @return {@code true} if Sone requires full access, {@code false}
217          *         otherwise
218          */
219         public boolean isRequireFullAccess() {
220                 return options.getBooleanOption("RequireFullAccess").get();
221         }
222
223         /**
224          * Sets whether Sone requires full access to be even visible.
225          *
226          * @param requireFullAccess
227          *            {@code true} if Sone requires full access, {@code false}
228          *            otherwise
229          */
230         public void setRequireFullAccess(Boolean requireFullAccess) {
231                 options.getBooleanOption("RequireFullAccess").set(requireFullAccess);
232         }
233
234         /**
235          * Returns the positive trust.
236          *
237          * @return The positive trust
238          */
239         public int getPositiveTrust() {
240                 return options.getIntegerOption("PositiveTrust").get();
241         }
242
243         /**
244          * Validates the positive trust.
245          *
246          * @param positiveTrust
247          *            The positive trust to validate
248          * @return {@code true} if the positive trust was valid, {@code false}
249          *         otherwise
250          */
251         public boolean validatePositiveTrust(Integer positiveTrust) {
252                 return options.getIntegerOption("PositiveTrust").validate(positiveTrust);
253         }
254
255         /**
256          * Sets the positive trust.
257          *
258          * @param positiveTrust
259          *            The new positive trust, or {@code null} to restore it to
260          *            the default vlaue
261          * @return This preferences
262          */
263         public Preferences setPositiveTrust(Integer positiveTrust) {
264                 options.getIntegerOption("PositiveTrust").set(positiveTrust);
265                 return this;
266         }
267
268         /**
269          * Returns the negative trust.
270          *
271          * @return The negative trust
272          */
273         public int getNegativeTrust() {
274                 return options.getIntegerOption("NegativeTrust").get();
275         }
276
277         /**
278          * Validates the negative trust.
279          *
280          * @param negativeTrust
281          *            The negative trust to validate
282          * @return {@code true} if the negative trust was valid, {@code false}
283          *         otherwise
284          */
285         public boolean validateNegativeTrust(Integer negativeTrust) {
286                 return options.getIntegerOption("NegativeTrust").validate(negativeTrust);
287         }
288
289         /**
290          * Sets the negative trust.
291          *
292          * @param negativeTrust
293          *            The negative trust, or {@code null} to restore it to the
294          *            default value
295          * @return The preferences
296          */
297         public Preferences setNegativeTrust(Integer negativeTrust) {
298                 options.getIntegerOption("NegativeTrust").set(negativeTrust);
299                 return this;
300         }
301
302         /**
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.
305          *
306          * @return The trust comment
307          */
308         public String getTrustComment() {
309                 return options.getStringOption("TrustComment").get();
310         }
311
312         /**
313          * Sets the trust comment.
314          *
315          * @param trustComment
316          *            The trust comment, or {@code null} to restore it to the
317          *            default value
318          * @return This preferences
319          */
320         public Preferences setTrustComment(String trustComment) {
321                 options.getStringOption("TrustComment").set(trustComment);
322                 return this;
323         }
324
325         /**
326          * Returns whether the {@link FcpInterface FCP interface} is currently
327          * active.
328          *
329          * @see FcpInterface#setActive(boolean)
330          * @return {@code true} if the FCP interface is currently active,
331          *         {@code false} otherwise
332          */
333         public boolean isFcpInterfaceActive() {
334                 return options.getBooleanOption("ActivateFcpInterface").get();
335         }
336
337         /**
338          * Sets whether the {@link FcpInterface FCP interface} is currently
339          * active.
340          *
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
346          */
347         public Preferences setFcpInterfaceActive(boolean fcpInterfaceActive) {
348                 options.getBooleanOption("ActivateFcpInterface").set(fcpInterfaceActive);
349                 if (fcpInterfaceActive) {
350                         eventBus.post(new FcpInterfaceActivatedEvent());
351                 } else {
352                         eventBus.post(new FcpInterfaceDeactivatedEvent());
353                 }
354                 return this;
355         }
356
357         /**
358          * Returns the action level for which full access to the FCP interface
359          * is required.
360          *
361          * @return The action level for which full access to the FCP interface
362          *         is required
363          */
364         public FullAccessRequired getFcpFullAccessRequired() {
365                 return FullAccessRequired.values()[options.getIntegerOption("FcpFullAccessRequired").get()];
366         }
367
368         /**
369          * Sets the action level for which full access to the FCP interface is
370          * required
371          *
372          * @param fcpFullAccessRequired
373          *            The action level
374          * @return This preferences
375          */
376         public Preferences setFcpFullAccessRequired(FullAccessRequired fcpFullAccessRequired) {
377                 options.getIntegerOption("FcpFullAccessRequired").set((fcpFullAccessRequired != null) ? fcpFullAccessRequired.ordinal() : null);
378                 return this;
379         }
380
381 }