Commit | Line | Data |
---|---|---|
08fe2e33 NR |
1 | package be.nikiroo.fanfix; |
2 | ||
3 | import java.io.File; | |
4 | import java.io.IOException; | |
5 | ||
6 | import be.nikiroo.fanfix.bundles.Config; | |
7 | import be.nikiroo.fanfix.bundles.ConfigBundle; | |
8 | import be.nikiroo.fanfix.bundles.StringIdBundle; | |
9 | import be.nikiroo.utils.resources.Bundles; | |
10 | ||
11 | /** | |
12 | * Global state for the program (services and singletons). | |
13 | * | |
14 | * @author niki | |
15 | */ | |
16 | public class Instance { | |
17 | private static ConfigBundle config; | |
18 | private static StringIdBundle trans; | |
19 | private static Cache cache; | |
20 | private static Library lib; | |
21 | private static boolean debug; | |
22 | private static File coverDir; | |
3727aae2 | 23 | private static File readerTmp; |
08fe2e33 NR |
24 | |
25 | static { | |
26 | config = new ConfigBundle(); | |
27 | ||
28 | // config dependent: | |
29 | trans = new StringIdBundle(getLang()); | |
30 | lib = new Library(getFile(Config.LIBRARY_DIR)); | |
31 | debug = Instance.getConfig().getBoolean(Config.DEBUG_ERR, false); | |
32 | coverDir = getFile(Config.DEFAULT_COVERS_DIR); | |
3727aae2 NR |
33 | File tmp = getFile(Config.CACHE_DIR); |
34 | readerTmp = getFile(Config.CACHE_DIR_LOCAL_READER); | |
35 | ||
36 | if (tmp == null || readerTmp == null) { | |
37 | String tmpDir = System.getProperty("java.io.tmpdir"); | |
38 | if (tmpDir != null) { | |
39 | if (tmp == null) { | |
40 | tmp = new File(tmpDir, "fanfic-tmp"); | |
41 | } | |
42 | if (readerTmp == null) { | |
43 | tmp = new File(tmpDir, "fanfic-reader"); | |
44 | } | |
45 | } else { | |
46 | syserr(new IOException( | |
47 | "The system does not have a default temporary directory")); | |
48 | } | |
49 | } | |
08fe2e33 NR |
50 | |
51 | if (coverDir != null && !coverDir.exists()) { | |
52 | syserr(new IOException( | |
53 | "The 'default covers' directory does not exists: " | |
54 | + coverDir)); | |
55 | coverDir = null; | |
56 | } | |
57 | // | |
58 | ||
59 | String noutf = System.getenv("NOUTF"); | |
60 | if (noutf != null) { | |
61 | noutf = noutf.trim().toLowerCase(); | |
62 | if ("yes".equals(noutf) || "true".equals(noutf) | |
63 | || "on".equals(noutf) || "1".equals(noutf) | |
64 | || "y".equals(noutf)) { | |
65 | trans.setUnicode(false); | |
66 | } | |
67 | } | |
68 | ||
69 | String configDir = System.getenv("CONFIG_DIR"); | |
70 | if (configDir != null) { | |
71 | if (new File(configDir).isDirectory()) { | |
72 | Bundles.setDirectory(configDir); | |
73 | try { | |
74 | config = new ConfigBundle(); | |
75 | config.updateFile(configDir); | |
76 | } catch (IOException e) { | |
77 | syserr(e); | |
78 | } | |
79 | try { | |
80 | trans = new StringIdBundle(getLang()); | |
81 | trans.updateFile(configDir); | |
82 | } catch (IOException e) { | |
83 | syserr(e); | |
84 | } | |
85 | } else { | |
86 | syserr(new IOException("Configuration directory not found: " | |
87 | + configDir)); | |
88 | } | |
89 | } | |
90 | ||
91 | try { | |
3727aae2 | 92 | |
08fe2e33 NR |
93 | String ua = config.getString(Config.USER_AGENT); |
94 | int hours = config.getInteger(Config.CACHE_MAX_TIME_CHANGING, -1); | |
95 | int hoursLarge = config | |
96 | .getInteger(Config.CACHE_MAX_TIME_STABLE, -1); | |
97 | ||
08fe2e33 NR |
98 | cache = new Cache(tmp, ua, hours, hoursLarge); |
99 | } catch (IOException e) { | |
100 | syserr(new IOException( | |
101 | "Cannot create cache (will continue without cache)", e)); | |
102 | } | |
103 | } | |
104 | ||
105 | /** | |
106 | * Get the (unique) configuration service for the program. | |
107 | * | |
108 | * @return the configuration service | |
109 | */ | |
110 | public static ConfigBundle getConfig() { | |
111 | return config; | |
112 | } | |
113 | ||
114 | /** | |
115 | * Get the (unique) {@link Cache} for the program. | |
116 | * | |
117 | * @return the {@link Cache} | |
118 | */ | |
119 | public static Cache getCache() { | |
120 | return cache; | |
121 | } | |
122 | ||
123 | /** | |
124 | * Get the (unique) {link StringIdBundle} for the program. | |
125 | * | |
126 | * @return the {link StringIdBundle} | |
127 | */ | |
128 | public static StringIdBundle getTrans() { | |
129 | return trans; | |
130 | } | |
131 | ||
132 | /** | |
133 | * Get the (unique) {@link Library} for the program. | |
134 | * | |
135 | * @return the {@link Library} | |
136 | */ | |
137 | public static Library getLibrary() { | |
138 | return lib; | |
139 | } | |
140 | ||
141 | /** | |
142 | * Return the directory where to look for default cover pages. | |
143 | * | |
144 | * @return the default covers directory | |
145 | */ | |
146 | public static File getCoverDir() { | |
147 | return coverDir; | |
148 | } | |
149 | ||
3727aae2 NR |
150 | /** |
151 | * Return the directory where to store temporary files for the local reader. | |
152 | * | |
153 | * @return the directory | |
154 | */ | |
155 | public static File getReaderDir() { | |
156 | return readerTmp; | |
157 | } | |
158 | ||
08fe2e33 NR |
159 | /** |
160 | * Report an error to the user | |
161 | * | |
162 | * @param e | |
163 | * the {@link Exception} to report | |
164 | */ | |
165 | public static void syserr(Exception e) { | |
166 | if (debug) { | |
167 | e.printStackTrace(); | |
168 | } else { | |
169 | System.err.println(e.getMessage()); | |
170 | } | |
171 | } | |
172 | ||
173 | /** | |
174 | * Return a path, but support the special $HOME variable. | |
175 | * | |
176 | * @return the path | |
177 | */ | |
178 | private static File getFile(Config id) { | |
179 | File file = null; | |
180 | String path = config.getString(id); | |
181 | if (path != null && !path.isEmpty()) { | |
182 | path = path.replace('/', File.separatorChar); | |
183 | if (path.contains("$HOME")) { | |
184 | path = path.replace("$HOME", | |
185 | "" + System.getProperty("user.home")); | |
186 | } | |
187 | ||
188 | file = new File(path); | |
189 | } | |
190 | ||
191 | return file; | |
192 | } | |
193 | ||
194 | /** | |
195 | * The language to use for the application (NULL = default system language). | |
196 | * | |
197 | * @return the language | |
198 | */ | |
199 | private static String getLang() { | |
200 | String lang = config.getString(Config.LANG); | |
201 | ||
202 | if (System.getenv("LANG") != null && !System.getenv("LANG").isEmpty()) { | |
203 | lang = System.getenv("LANG"); | |
204 | } | |
205 | ||
206 | if (lang != null && lang.isEmpty()) { | |
207 | lang = null; | |
208 | } | |
209 | ||
210 | return lang; | |
211 | } | |
212 | } |