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