5fa33e4ccebb31a9995ee2217b458bdfa2b71b72
[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                 loadFcpInterfaceActive(configuration);
27                 loadFcpFullAccessRequired(configuration);
28         }
29
30         private void loadInsertionDelay(Configuration configuration) {
31                 preferences.setNewInsertionDelay(configuration.getIntValue(
32                                 "Option/InsertionDelay").getValue(null));
33         }
34
35         private void loadPostsPerPage(Configuration configuration) {
36                 preferences.setNewPostsPerPage(
37                                 configuration.getIntValue("Option/PostsPerPage")
38                                                 .getValue(null));
39         }
40
41         private void loadImagesPerPage(Configuration configuration) {
42                 preferences.setNewImagesPerPage(
43                                 configuration.getIntValue("Option/ImagesPerPage")
44                                                 .getValue(null));
45         }
46
47         private void loadCharactersPerPost(Configuration configuration) {
48                 preferences.setNewCharactersPerPost(
49                                 configuration.getIntValue("Option/CharactersPerPost")
50                                                 .getValue(null));
51         }
52
53         private void loadPostCutOffLength(Configuration configuration) {
54                 try {
55                         preferences.setNewPostCutOffLength(
56                                         configuration.getIntValue("Option/PostCutOffLength")
57                                                         .getValue(null));
58                 } catch (IllegalArgumentException iae1) {
59                         /* previous versions allowed -1, ignore and use default. */
60                 }
61         }
62
63         private void loadRequireFullAccess(Configuration configuration) {
64                 preferences.setNewRequireFullAccess(
65                                 configuration.getBooleanValue("Option/RequireFullAccess")
66                                                 .getValue(null));
67         }
68
69         private void loadFcpInterfaceActive(Configuration configuration) {
70                 preferences.setNewFcpInterfaceActive(configuration.getBooleanValue(
71                                 "Option/ActivateFcpInterface").getValue(null));
72         }
73
74         private void loadFcpFullAccessRequired(Configuration configuration) {
75                 Integer fullAccessRequiredInteger = configuration
76                                 .getIntValue("Option/FcpFullAccessRequired").getValue(null);
77                 preferences.setNewFcpFullAccessRequired(
78                                 (fullAccessRequiredInteger == null) ? null :
79                                                 FullAccessRequired.values()[fullAccessRequiredInteger]);
80         }
81
82 }