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 // Could have used: System.getProperty("java.io.tmpdir")
92 tmp
= new File(configDir
, "tmp");
94 if (readerTmp
== null) {
95 readerTmp
= new File(configDir
, "tmp-reader");
99 if (coverDir
!= null && !coverDir
.exists()) {
100 syserr(new IOException(
101 "The 'default covers' directory does not exists: "
107 String ua
= config
.getString(Config
.USER_AGENT
);
108 int hours
= config
.getInteger(Config
.CACHE_MAX_TIME_CHANGING
, -1);
109 int hoursLarge
= config
110 .getInteger(Config
.CACHE_MAX_TIME_STABLE
, -1);
112 cache
= new Cache(tmp
, ua
, hours
, hoursLarge
);
113 } catch (IOException e
) {
114 syserr(new IOException(
115 "Cannot create cache (will continue without cache)", e
));
120 * Get the (unique) configuration service for the program.
122 * @return the configuration service
124 public static ConfigBundle
getConfig() {
129 * Get the (unique) UI configuration service for the program.
131 * @return the configuration service
133 public static UiConfigBundle
getUiConfig() {
138 * Get the (unique) {@link Cache} for the program.
140 * @return the {@link Cache}
142 public static Cache
getCache() {
147 * Get the (unique) {link StringIdBundle} for the program.
149 * @return the {link StringIdBundle}
151 public static StringIdBundle
getTrans() {
156 * Get the (unique) {@link Library} for the program.
158 * @return the {@link Library}
160 public static Library
getLibrary() {
165 * Return the directory where to look for default cover pages.
167 * @return the default covers directory
169 public static File
getCoverDir() {
174 * Return the directory where to store temporary files for the local reader.
176 * @return the directory
178 public static File
getReaderDir() {
183 * Report an error to the user
186 * the {@link Exception} to report
188 public static void syserr(Exception e
) {
192 System
.err
.println(e
.getMessage());
197 * Return a path, but support the special $HOME variable.
201 private static File
getFile(Config id
) {
202 return getFile(config
.getString(id
));
206 * Return a path, but support the special $HOME variable.
210 private static File
getFile(UiConfig id
) {
211 return getFile(uiconfig
.getString(id
));
215 * Return a path, but support the special $HOME variable.
219 private static File
getFile(String path
) {
221 if (path
!= null && !path
.isEmpty()) {
222 path
= path
.replace('/', File
.separatorChar
);
223 if (path
.contains("$HOME")) {
224 path
= path
.replace("$HOME",
225 "" + System
.getProperty("user.home"));
228 file
= new File(path
);
235 * The language to use for the application (NULL = default system language).
237 * @return the language
239 private static String
getLang() {
240 String lang
= config
.getString(Config
.LANG
);
242 if (System
.getenv("LANG") != null && !System
.getenv("LANG").isEmpty()) {
243 lang
= System
.getenv("LANG");
246 if (lang
!= null && lang
.isEmpty()) {
254 * Check that the given environment variable is "enabled".
257 * the variable to check
259 * @return TRUE if it is
261 private static boolean checkEnv(String key
) {
262 String value
= System
.getenv(key
);
264 value
= value
.trim().toLowerCase();
265 if ("yes".equals(value
) || "true".equals(value
)
266 || "on".equals(value
) || "1".equals(value
)
267 || "y".equals(value
)) {