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
.output
.BasicOutput
.OutputType
;
10 import be
.nikiroo
.utils
.resources
.Bundles
;
13 * Global state for the program (services and singletons).
17 public class Instance
{
18 private static ConfigBundle config
;
19 private static StringIdBundle trans
;
20 private static Cache cache
;
21 private static Library lib
;
22 private static boolean debug
;
23 private static File coverDir
;
24 private static File readerTmp
;
27 // Most of the rest is dependent upon this:
28 config
= new ConfigBundle();
30 String configDir
= System
.getenv("CONFIG_DIR");
31 if (configDir
== null) {
32 configDir
= new File(System
.getProperty("user.home"), ".fanfix")
35 if (configDir
!= null) {
36 if (!new File(configDir
).exists()) {
37 new File(configDir
).mkdirs();
39 Bundles
.setDirectory(configDir
);
43 config
= new ConfigBundle();
44 config
.updateFile(configDir
);
45 } catch (IOException e
) {
49 trans
= new StringIdBundle(getLang());
50 System
.out
.println("UPDATING");
51 trans
.updateFile(configDir
);
52 System
.out
.println("UPDATED");
53 } catch (IOException e
) {
57 Bundles
.setDirectory(configDir
);
60 trans
= new StringIdBundle(getLang());
62 lib
= new Library(getFile(Config
.LIBRARY_DIR
),
63 OutputType
.INFO_TEXT
, OutputType
.CBZ
);
64 } catch (Exception e
) {
65 syserr(new IOException("Cannot create library for directory: "
66 + getFile(Config
.LIBRARY_DIR
), e
));
69 debug
= Instance
.getConfig().getBoolean(Config
.DEBUG_ERR
, false);
70 coverDir
= getFile(Config
.DEFAULT_COVERS_DIR
);
71 File tmp
= getFile(Config
.CACHE_DIR
);
72 readerTmp
= getFile(Config
.CACHE_DIR_LOCAL_READER
);
74 if (checkEnv("NOUTF")) {
75 trans
.setUnicode(false);
78 if (checkEnv("DEBUG")) {
82 if (tmp
== null || readerTmp
== null) {
83 String tmpDir
= System
.getProperty("java.io.tmpdir");
86 tmp
= new File(tmpDir
, "fanfic-tmp");
88 if (readerTmp
== null) {
89 readerTmp
= new File(tmpDir
, "fanfic-reader");
92 syserr(new IOException(
93 "The system does not have a default temporary directory"));
97 if (coverDir
!= null && !coverDir
.exists()) {
98 syserr(new IOException(
99 "The 'default covers' directory does not exists: "
105 String ua
= config
.getString(Config
.USER_AGENT
);
106 int hours
= config
.getInteger(Config
.CACHE_MAX_TIME_CHANGING
, -1);
107 int hoursLarge
= config
108 .getInteger(Config
.CACHE_MAX_TIME_STABLE
, -1);
110 cache
= new Cache(tmp
, ua
, hours
, hoursLarge
);
111 } catch (IOException e
) {
112 syserr(new IOException(
113 "Cannot create cache (will continue without cache)", e
));
118 * Get the (unique) configuration service for the program.
120 * @return the configuration service
122 public static ConfigBundle
getConfig() {
127 * Get the (unique) {@link Cache} for the program.
129 * @return the {@link Cache}
131 public static Cache
getCache() {
136 * Get the (unique) {link StringIdBundle} for the program.
138 * @return the {link StringIdBundle}
140 public static StringIdBundle
getTrans() {
145 * Get the (unique) {@link Library} for the program.
147 * @return the {@link Library}
149 public static Library
getLibrary() {
154 * Return the directory where to look for default cover pages.
156 * @return the default covers directory
158 public static File
getCoverDir() {
163 * Return the directory where to store temporary files for the local reader.
165 * @return the directory
167 public static File
getReaderDir() {
172 * Report an error to the user
175 * the {@link Exception} to report
177 public static void syserr(Exception e
) {
181 System
.err
.println(e
.getMessage());
186 * Return a path, but support the special $HOME variable.
190 private static File
getFile(Config id
) {
192 String path
= config
.getString(id
);
193 if (path
!= null && !path
.isEmpty()) {
194 path
= path
.replace('/', File
.separatorChar
);
195 if (path
.contains("$HOME")) {
196 path
= path
.replace("$HOME",
197 "" + System
.getProperty("user.home"));
200 file
= new File(path
);
207 * The language to use for the application (NULL = default system language).
209 * @return the language
211 private static String
getLang() {
212 String lang
= config
.getString(Config
.LANG
);
214 if (System
.getenv("LANG") != null && !System
.getenv("LANG").isEmpty()) {
215 lang
= System
.getenv("LANG");
218 if (lang
!= null && lang
.isEmpty()) {
226 * Check that the given environment variable is "enabled".
229 * the variable to check
231 * @return TRUE if it is
233 private static boolean checkEnv(String key
) {
234 String value
= System
.getenv(key
);
236 value
= value
.trim().toLowerCase();
237 if ("yes".equals(value
) || "true".equals(value
)
238 || "on".equals(value
) || "1".equals(value
)
239 || "y".equals(value
)) {