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 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 (tmp
== null || readerTmp
== null) {
37 String tmpDir
= System
.getProperty("java.io.tmpdir");
40 tmp
= new File(tmpDir
, "fanfic-tmp");
42 if (readerTmp
== null) {
43 tmp
= new File(tmpDir
, "fanfic-reader");
46 syserr(new IOException(
47 "The system does not have a default temporary directory"));
51 if (coverDir
!= null && !coverDir
.exists()) {
52 syserr(new IOException(
53 "The 'default covers' directory does not exists: "
59 String noutf
= System
.getenv("NOUTF");
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);
69 String configDir
= System
.getenv("CONFIG_DIR");
70 if (configDir
!= null) {
71 if (new File(configDir
).isDirectory()) {
72 Bundles
.setDirectory(configDir
);
74 config
= new ConfigBundle();
75 config
.updateFile(configDir
);
76 } catch (IOException e
) {
80 trans
= new StringIdBundle(getLang());
81 trans
.updateFile(configDir
);
82 } catch (IOException e
) {
86 syserr(new IOException("Configuration directory not found: "
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);
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
));
106 * Get the (unique) configuration service for the program.
108 * @return the configuration service
110 public static ConfigBundle
getConfig() {
115 * Get the (unique) {@link Cache} for the program.
117 * @return the {@link Cache}
119 public static Cache
getCache() {
124 * Get the (unique) {link StringIdBundle} for the program.
126 * @return the {link StringIdBundle}
128 public static StringIdBundle
getTrans() {
133 * Get the (unique) {@link Library} for the program.
135 * @return the {@link Library}
137 public static Library
getLibrary() {
142 * Return the directory where to look for default cover pages.
144 * @return the default covers directory
146 public static File
getCoverDir() {
151 * Return the directory where to store temporary files for the local reader.
153 * @return the directory
155 public static File
getReaderDir() {
160 * Report an error to the user
163 * the {@link Exception} to report
165 public static void syserr(Exception e
) {
169 System
.err
.println(e
.getMessage());
174 * Return a path, but support the special $HOME variable.
178 private static File
getFile(Config id
) {
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"));
188 file
= new File(path
);
195 * The language to use for the application (NULL = default system language).
197 * @return the language
199 private static String
getLang() {
200 String lang
= config
.getString(Config
.LANG
);
202 if (System
.getenv("LANG") != null && !System
.getenv("LANG").isEmpty()) {
203 lang
= System
.getenv("LANG");
206 if (lang
!= null && lang
.isEmpty()) {