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