Add test for distrust ajax page
[Sone.git] / src / main / java / net / pterodactylus / sone / core / PreferencesLoader.java
1 package net.pterodactylus.sone.core;
2
3 import net.pterodactylus.sone.fcp.FcpInterface.FullAccessRequired;
4 import net.pterodactylus.util.config.Configuration;
5 import net.pterodactylus.util.config.ConfigurationException;
6
7 /**
8  * Loads preferences stored in a {@link Configuration} into a {@link
9  * Preferences} object.
10  *
11  * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
12  */
13 public class PreferencesLoader {
14
15         private final Preferences preferences;
16
17         public PreferencesLoader(Preferences preferences) {
18                 this.preferences = preferences;
19         }
20
21         public void loadFrom(Configuration configuration) {
22                 loadInsertionDelay(configuration);
23                 loadPostsPerPage(configuration);
24                 loadImagesPerPage(configuration);
25                 loadCharactersPerPost(configuration);
26                 loadPostCutOffLength(configuration);
27                 loadRequireFullAccess(configuration);
28                 loadPositiveTrust(configuration);
29                 loadNegativeTrust(configuration);
30                 loadTrustComment(configuration);
31                 loadFcpInterfaceActive(configuration);
32                 loadFcpFullAccessRequired(configuration);
33         }
34
35         private void loadInsertionDelay(Configuration configuration) {
36                 preferences.setInsertionDelay(configuration.getIntValue(
37                                 "Option/InsertionDelay").getValue(null));
38         }
39
40         private void loadPostsPerPage(Configuration configuration) {
41                 preferences.setPostsPerPage(
42                                 configuration.getIntValue("Option/PostsPerPage")
43                                                 .getValue(null));
44         }
45
46         private void loadImagesPerPage(Configuration configuration) {
47                 preferences.setImagesPerPage(
48                                 configuration.getIntValue("Option/ImagesPerPage")
49                                                 .getValue(null));
50         }
51
52         private void loadCharactersPerPost(Configuration configuration) {
53                 preferences.setCharactersPerPost(
54                                 configuration.getIntValue("Option/CharactersPerPost")
55                                                 .getValue(null));
56         }
57
58         private void loadPostCutOffLength(Configuration configuration) {
59                 try {
60                         preferences.setPostCutOffLength(
61                                         configuration.getIntValue("Option/PostCutOffLength")
62                                                         .getValue(null));
63                 } catch (IllegalArgumentException iae1) {
64                         /* previous versions allowed -1, ignore and use default. */
65                 }
66         }
67
68         private void loadRequireFullAccess(Configuration configuration) {
69                 preferences.setRequireFullAccess(
70                                 configuration.getBooleanValue("Option/RequireFullAccess")
71                                                 .getValue(null));
72         }
73
74         private void loadPositiveTrust(Configuration configuration) {
75                 preferences.setPositiveTrust(
76                                 configuration.getIntValue("Option/PositiveTrust")
77                                                 .getValue(null));
78         }
79
80         private void loadNegativeTrust(Configuration configuration) {
81                 preferences.setNegativeTrust(
82                                 configuration.getIntValue("Option/NegativeTrust")
83                                                 .getValue(null));
84         }
85
86         private void loadTrustComment(Configuration configuration) {
87                 preferences.setTrustComment(
88                                 configuration.getStringValue("Option/TrustComment")
89                                                 .getValue(null));
90         }
91
92         private void loadFcpInterfaceActive(Configuration configuration) {
93                 preferences.setFcpInterfaceActive(configuration.getBooleanValue(
94                                 "Option/ActivateFcpInterface").getValue(null));
95         }
96
97         private void loadFcpFullAccessRequired(Configuration configuration) {
98                 Integer fullAccessRequiredInteger = configuration
99                                 .getIntValue("Option/FcpFullAccessRequired").getValue(null);
100                 preferences.setFcpFullAccessRequired(
101                                 (fullAccessRequiredInteger == null) ? null :
102                                                 FullAccessRequired.values()[fullAccessRequiredInteger]);
103         }
104
105 }