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
.fanfix
.bundles
.UiConfig
;
10 import be
.nikiroo
.fanfix
.bundles
.UiConfigBundle
;
11 import be
.nikiroo
.fanfix
.output
.BasicOutput
.OutputType
;
12 import be
.nikiroo
.utils
.resources
.Bundles
;
15 * Global state for the program (services and singletons).
19 public class Instance
{
20 private static ConfigBundle config
;
21 private static UiConfigBundle uiconfig
;
22 private static StringIdBundle trans
;
23 private static Cache cache
;
24 private static Library lib
;
25 private static boolean debug
;
26 private static File coverDir
;
27 private static File readerTmp
;
30 // Most of the rest is dependent upon this:
31 config
= new ConfigBundle();
33 String configDir
= System
.getenv("CONFIG_DIR");
34 if (configDir
== null) {
35 configDir
= new File(System
.getProperty("user.home"), ".fanfix")
38 if (configDir
!= null) {
39 if (!new File(configDir
).exists()) {
40 new File(configDir
).mkdirs();
42 Bundles
.setDirectory(configDir
);
46 config
= new ConfigBundle();
47 config
.updateFile(configDir
);
48 } catch (IOException e
) {
52 uiconfig
= new UiConfigBundle();
53 uiconfig
.updateFile(configDir
);
54 } catch (IOException e
) {
58 trans
= new StringIdBundle(getLang());
59 trans
.updateFile(configDir
);
60 } catch (IOException e
) {
64 Bundles
.setDirectory(configDir
);
67 uiconfig
= new UiConfigBundle();
68 trans
= new StringIdBundle(getLang());
70 lib
= new Library(getFile(Config
.LIBRARY_DIR
),
71 OutputType
.INFO_TEXT
, OutputType
.CBZ
);
72 } catch (Exception e
) {
73 syserr(new IOException("Cannot create library for directory: "
74 + getFile(Config
.LIBRARY_DIR
), e
));
77 debug
= Instance
.getConfig().getBoolean(Config
.DEBUG_ERR
, false);
78 coverDir
= getFile(Config
.DEFAULT_COVERS_DIR
);
79 File tmp
= getFile(Config
.CACHE_DIR
);
80 readerTmp
= getFile(UiConfig
.CACHE_DIR_LOCAL_READER
);
82 if (checkEnv("NOUTF")) {
83 trans
.setUnicode(false);
86 if (checkEnv("DEBUG")) {
90 if (tmp
== null || readerTmp
== null) {
91 String tmpDir
= System
.getProperty("java.io.tmpdir");
94 tmp
= new File(tmpDir
, "fanfic-tmp");
96 if (readerTmp
== null) {
97 readerTmp
= new File(tmpDir
, "fanfic-reader");
100 syserr(new IOException(
101 "The system does not have a default temporary directory"));
105 if (coverDir
!= null && !coverDir
.exists()) {
106 syserr(new IOException(
107 "The 'default covers' directory does not exists: "
113 String ua
= config
.getString(Config
.USER_AGENT
);
114 int hours
= config
.getInteger(Config
.CACHE_MAX_TIME_CHANGING
, -1);
115 int hoursLarge
= config
116 .getInteger(Config
.CACHE_MAX_TIME_STABLE
, -1);
118 cache
= new Cache(tmp
, ua
, hours
, hoursLarge
);
119 } catch (IOException e
) {
120 syserr(new IOException(
121 "Cannot create cache (will continue without cache)", e
));
126 * Get the (unique) configuration service for the program.
128 * @return the configuration service
130 public static ConfigBundle
getConfig() {
135 * Get the (unique) UI configuration service for the program.
137 * @return the configuration service
139 public static UiConfigBundle
getUiConfig() {
144 * Get the (unique) {@link Cache} for the program.
146 * @return the {@link Cache}
148 public static Cache
getCache() {
153 * Get the (unique) {link StringIdBundle} for the program.
155 * @return the {link StringIdBundle}
157 public static StringIdBundle
getTrans() {
162 * Get the (unique) {@link Library} for the program.
164 * @return the {@link Library}
166 public static Library
getLibrary() {
171 * Return the directory where to look for default cover pages.
173 * @return the default covers directory
175 public static File
getCoverDir() {
180 * Return the directory where to store temporary files for the local reader.
182 * @return the directory
184 public static File
getReaderDir() {
189 * Report an error to the user
192 * the {@link Exception} to report
194 public static void syserr(Exception e
) {
198 System
.err
.println(e
.getMessage());
203 * Return a path, but support the special $HOME variable.
207 private static File
getFile(Config id
) {
208 return getFile(config
.getString(id
));
212 * Return a path, but support the special $HOME variable.
216 private static File
getFile(UiConfig id
) {
217 return getFile(uiconfig
.getString(id
));
221 * Return a path, but support the special $HOME variable.
225 private static File
getFile(String path
) {
227 if (path
!= null && !path
.isEmpty()) {
228 path
= path
.replace('/', File
.separatorChar
);
229 if (path
.contains("$HOME")) {
230 path
= path
.replace("$HOME",
231 "" + System
.getProperty("user.home"));
234 file
= new File(path
);
241 * The language to use for the application (NULL = default system language).
243 * @return the language
245 private static String
getLang() {
246 String lang
= config
.getString(Config
.LANG
);
248 if (System
.getenv("LANG") != null && !System
.getenv("LANG").isEmpty()) {
249 lang
= System
.getenv("LANG");
252 if (lang
!= null && lang
.isEmpty()) {
260 * Check that the given environment variable is "enabled".
263 * the variable to check
265 * @return TRUE if it is
267 private static boolean checkEnv(String key
) {
268 String value
= System
.getenv(key
);
270 value
= value
.trim().toLowerCase();
271 if ("yes".equals(value
) || "true".equals(value
)
272 || "on".equals(value
) || "1".equals(value
)
273 || "y".equals(value
)) {