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