1 package be
.nikiroo
.fanfix
;
4 import java
.io
.IOException
;
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
;
12 * Global state for the program (services and singletons).
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
;
23 private static File readerTmp
;
26 // Most of the rest is dependent upon this:
27 config
= new ConfigBundle();
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
);
33 File tmp
= getFile(Config
.CACHE_DIR
);
34 readerTmp
= getFile(Config
.CACHE_DIR_LOCAL_READER
);
36 if (checkEnv("NOUTF")) {
37 trans
.setUnicode(false);
40 if (checkEnv("DEBUG")) {
44 if (tmp
== null || readerTmp
== null) {
45 String tmpDir
= System
.getProperty("java.io.tmpdir");
48 tmp
= new File(tmpDir
, "fanfic-tmp");
50 if (readerTmp
== null) {
51 readerTmp
= new File(tmpDir
, "fanfic-reader");
54 syserr(new IOException(
55 "The system does not have a default temporary directory"));
59 if (coverDir
!= null && !coverDir
.exists()) {
60 syserr(new IOException(
61 "The 'default covers' directory does not exists: "
66 String configDir
= System
.getenv("CONFIG_DIR");
67 if (configDir
== null) {
68 configDir
= new File(System
.getProperty("user.home"), ".fanfix")
71 if (configDir
!= null) {
72 if (!new File(configDir
).exists()) {
73 new File(configDir
).mkdirs();
75 Bundles
.setDirectory(configDir
);
79 config
= new ConfigBundle();
80 config
.updateFile(configDir
);
81 } catch (IOException e
) {
85 trans
= new StringIdBundle(getLang());
86 trans
.updateFile(configDir
);
87 } catch (IOException e
) {
91 Bundles
.setDirectory(configDir
);
95 String ua
= config
.getString(Config
.USER_AGENT
);
96 int hours
= config
.getInteger(Config
.CACHE_MAX_TIME_CHANGING
, -1);
97 int hoursLarge
= config
98 .getInteger(Config
.CACHE_MAX_TIME_STABLE
, -1);
100 cache
= new Cache(tmp
, ua
, hours
, hoursLarge
);
101 } catch (IOException e
) {
102 syserr(new IOException(
103 "Cannot create cache (will continue without cache)", e
));
108 * Get the (unique) configuration service for the program.
110 * @return the configuration service
112 public static ConfigBundle
getConfig() {
117 * Get the (unique) {@link Cache} for the program.
119 * @return the {@link Cache}
121 public static Cache
getCache() {
126 * Get the (unique) {link StringIdBundle} for the program.
128 * @return the {link StringIdBundle}
130 public static StringIdBundle
getTrans() {
135 * Get the (unique) {@link Library} for the program.
137 * @return the {@link Library}
139 public static Library
getLibrary() {
144 * Return the directory where to look for default cover pages.
146 * @return the default covers directory
148 public static File
getCoverDir() {
153 * Return the directory where to store temporary files for the local reader.
155 * @return the directory
157 public static File
getReaderDir() {
162 * Report an error to the user
165 * the {@link Exception} to report
167 public static void syserr(Exception e
) {
171 System
.err
.println(e
.getMessage());
176 * Return a path, but support the special $HOME variable.
180 private static File
getFile(Config id
) {
182 String path
= config
.getString(id
);
183 if (path
!= null && !path
.isEmpty()) {
184 path
= path
.replace('/', File
.separatorChar
);
185 if (path
.contains("$HOME")) {
186 path
= path
.replace("$HOME",
187 "" + System
.getProperty("user.home"));
190 file
= new File(path
);
197 * The language to use for the application (NULL = default system language).
199 * @return the language
201 private static String
getLang() {
202 String lang
= config
.getString(Config
.LANG
);
204 if (System
.getenv("LANG") != null && !System
.getenv("LANG").isEmpty()) {
205 lang
= System
.getenv("LANG");
208 if (lang
!= null && lang
.isEmpty()) {
216 * Check that the given environment variable is "enabled".
219 * the variable to check
221 * @return TRUE if it is
223 private static boolean checkEnv(String key
) {
224 String value
= System
.getenv(key
);
226 value
= value
.trim().toLowerCase();
227 if ("yes".equals(value
) || "true".equals(value
)
228 || "on".equals(value
) || "1".equals(value
)
229 || "y".equals(value
)) {