🔀 Merge next
[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 public class PreferencesLoader {
12
13         private final Preferences preferences;
14
15         public PreferencesLoader(Preferences preferences) {
16                 this.preferences = preferences;
17         }
18
19         public void loadFrom(Configuration configuration) {
20                 loadInsertionDelay(configuration);
21                 loadPostsPerPage(configuration);
22                 loadImagesPerPage(configuration);
23                 loadCharactersPerPost(configuration);
24                 loadPostCutOffLength(configuration);
25                 loadRequireFullAccess(configuration);
26                 loadPositiveTrust(configuration);
27                 loadNegativeTrust(configuration);
28                 loadTrustComment(configuration);
29                 loadFcpInterfaceActive(configuration);
30                 loadFcpFullAccessRequired(configuration);
31         }
32
33         private void loadInsertionDelay(Configuration configuration) {
34                 preferences.setNewInsertionDelay(configuration.getIntValue(
35                                 "Option/InsertionDelay").getValue(null));
36         }
37
38         private void loadPostsPerPage(Configuration configuration) {
39                 preferences.setNewPostsPerPage(
40                                 configuration.getIntValue("Option/PostsPerPage")
41                                                 .getValue(null));
42         }
43
44         private void loadImagesPerPage(Configuration configuration) {
45                 preferences.setNewImagesPerPage(
46                                 configuration.getIntValue("Option/ImagesPerPage")
47                                                 .getValue(null));
48         }
49
50         private void loadCharactersPerPost(Configuration configuration) {
51                 preferences.setNewCharactersPerPost(
52                                 configuration.getIntValue("Option/CharactersPerPost")
53                                                 .getValue(null));
54         }
55
56         private void loadPostCutOffLength(Configuration configuration) {
57                 try {
58                         preferences.setNewPostCutOffLength(
59                                         configuration.getIntValue("Option/PostCutOffLength")
60                                                         .getValue(null));
61                 } catch (IllegalArgumentException iae1) {
62                         /* previous versions allowed -1, ignore and use default. */
63                 }
64         }
65
66         private void loadRequireFullAccess(Configuration configuration) {
67                 preferences.setNewRequireFullAccess(
68                                 configuration.getBooleanValue("Option/RequireFullAccess")
69                                                 .getValue(null));
70         }
71
72         private void loadPositiveTrust(Configuration configuration) {
73                 preferences.setNewPositiveTrust(
74                                 configuration.getIntValue("Option/PositiveTrust")
75                                                 .getValue(null));
76         }
77
78         private void loadNegativeTrust(Configuration configuration) {
79                 preferences.setNewNegativeTrust(
80                                 configuration.getIntValue("Option/NegativeTrust")
81                                                 .getValue(null));
82         }
83
84         private void loadTrustComment(Configuration configuration) {
85                 preferences.setNewTrustComment(
86                                 configuration.getStringValue("Option/TrustComment")
87                                                 .getValue(null));
88         }
89
90         private void loadFcpInterfaceActive(Configuration configuration) {
91                 preferences.setNewFcpInterfaceActive(configuration.getBooleanValue(
92                                 "Option/ActivateFcpInterface").getValue(null));
93         }
94
95         private void loadFcpFullAccessRequired(Configuration configuration) {
96                 Integer fullAccessRequiredInteger = configuration
97                                 .getIntValue("Option/FcpFullAccessRequired").getValue(null);
98                 preferences.setNewFcpFullAccessRequired(
99                                 (fullAccessRequiredInteger == null) ? null :
100                                                 FullAccessRequired.values()[fullAccessRequiredInteger]);
101         }
102
103 }