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
;
25 config
= new ConfigBundle();
28 trans
= new StringIdBundle(getLang());
29 lib
= new Library(getFile(Config
.LIBRARY_DIR
));
30 debug
= Instance
.getConfig().getBoolean(Config
.DEBUG_ERR
, false);
31 coverDir
= getFile(Config
.DEFAULT_COVERS_DIR
);
33 if (coverDir
!= null && !coverDir
.exists()) {
34 syserr(new IOException(
35 "The 'default covers' directory does not exists: "
41 String noutf
= System
.getenv("NOUTF");
43 noutf
= noutf
.trim().toLowerCase();
44 if ("yes".equals(noutf
) || "true".equals(noutf
)
45 || "on".equals(noutf
) || "1".equals(noutf
)
46 || "y".equals(noutf
)) {
47 trans
.setUnicode(false);
51 String configDir
= System
.getenv("CONFIG_DIR");
52 if (configDir
!= null) {
53 if (new File(configDir
).isDirectory()) {
54 Bundles
.setDirectory(configDir
);
56 config
= new ConfigBundle();
57 config
.updateFile(configDir
);
58 } catch (IOException e
) {
62 trans
= new StringIdBundle(getLang());
63 trans
.updateFile(configDir
);
64 } catch (IOException e
) {
68 syserr(new IOException("Configuration directory not found: "
74 File tmp
= getFile(Config
.CACHE_DIR
);
75 String ua
= config
.getString(Config
.USER_AGENT
);
76 int hours
= config
.getInteger(Config
.CACHE_MAX_TIME_CHANGING
, -1);
77 int hoursLarge
= config
78 .getInteger(Config
.CACHE_MAX_TIME_STABLE
, -1);
81 String tmpDir
= System
.getProperty("java.io.tmpdir");
83 tmp
= new File(tmpDir
, "fanfic-tmp");
85 syserr(new IOException(
86 "The system does not have a default temporary directory"));
90 cache
= new Cache(tmp
, ua
, hours
, hoursLarge
);
91 } catch (IOException e
) {
92 syserr(new IOException(
93 "Cannot create cache (will continue without cache)", e
));
98 * Get the (unique) configuration service for the program.
100 * @return the configuration service
102 public static ConfigBundle
getConfig() {
107 * Get the (unique) {@link Cache} for the program.
109 * @return the {@link Cache}
111 public static Cache
getCache() {
116 * Get the (unique) {link StringIdBundle} for the program.
118 * @return the {link StringIdBundle}
120 public static StringIdBundle
getTrans() {
125 * Get the (unique) {@link Library} for the program.
127 * @return the {@link Library}
129 public static Library
getLibrary() {
134 * Return the directory where to look for default cover pages.
136 * @return the default covers directory
138 public static File
getCoverDir() {
143 * Report an error to the user
146 * the {@link Exception} to report
148 public static void syserr(Exception e
) {
152 System
.err
.println(e
.getMessage());
157 * Return a path, but support the special $HOME variable.
161 private static File
getFile(Config id
) {
163 String path
= config
.getString(id
);
164 if (path
!= null && !path
.isEmpty()) {
165 path
= path
.replace('/', File
.separatorChar
);
166 if (path
.contains("$HOME")) {
167 path
= path
.replace("$HOME",
168 "" + System
.getProperty("user.home"));
171 file
= new File(path
);
178 * The language to use for the application (NULL = default system language).
180 * @return the language
182 private static String
getLang() {
183 String lang
= config
.getString(Config
.LANG
);
185 if (System
.getenv("LANG") != null && !System
.getenv("LANG").isEmpty()) {
186 lang
= System
.getenv("LANG");
189 if (lang
!= null && lang
.isEmpty()) {