| 1 | package be.nikiroo.fanfix; |
| 2 | |
| 3 | import java.io.File; |
| 4 | import java.io.IOException; |
| 5 | import java.util.Date; |
| 6 | |
| 7 | import be.nikiroo.fanfix.bundles.Config; |
| 8 | import be.nikiroo.fanfix.bundles.ConfigBundle; |
| 9 | import be.nikiroo.fanfix.bundles.StringId; |
| 10 | import be.nikiroo.fanfix.bundles.StringIdBundle; |
| 11 | import be.nikiroo.fanfix.bundles.UiConfig; |
| 12 | import be.nikiroo.fanfix.bundles.UiConfigBundle; |
| 13 | import be.nikiroo.fanfix.library.BasicLibrary; |
| 14 | import be.nikiroo.fanfix.library.CacheLibrary; |
| 15 | import be.nikiroo.fanfix.library.LocalLibrary; |
| 16 | import be.nikiroo.fanfix.library.RemoteLibrary; |
| 17 | import be.nikiroo.utils.Cache; |
| 18 | import be.nikiroo.utils.IOUtils; |
| 19 | import be.nikiroo.utils.TraceHandler; |
| 20 | import be.nikiroo.utils.resources.Bundles; |
| 21 | |
| 22 | /** |
| 23 | * Global state for the program (services and singletons). |
| 24 | * |
| 25 | * @author niki |
| 26 | */ |
| 27 | public class Instance { |
| 28 | private static ConfigBundle config; |
| 29 | private static UiConfigBundle uiconfig; |
| 30 | private static StringIdBundle trans; |
| 31 | private static DataLoader cache; |
| 32 | private static BasicLibrary lib; |
| 33 | private static File coverDir; |
| 34 | private static File readerTmp; |
| 35 | private static File remoteDir; |
| 36 | private static String configDir; |
| 37 | private static TraceHandler tracer; |
| 38 | |
| 39 | static { |
| 40 | // Before we can configure it: |
| 41 | tracer = new TraceHandler(true, checkEnv("DEBUG"), checkEnv("DEBUG")); |
| 42 | |
| 43 | // Most of the rest is dependent upon this: |
| 44 | config = new ConfigBundle(); |
| 45 | |
| 46 | configDir = System.getProperty("CONFIG_DIR"); |
| 47 | if (configDir == null) { |
| 48 | configDir = System.getenv("CONFIG_DIR"); |
| 49 | } |
| 50 | |
| 51 | if (configDir == null) { |
| 52 | configDir = new File(getHome(), ".fanfix").getPath(); |
| 53 | } |
| 54 | |
| 55 | if (!new File(configDir).exists()) { |
| 56 | new File(configDir).mkdirs(); |
| 57 | } else { |
| 58 | Bundles.setDirectory(configDir); |
| 59 | } |
| 60 | |
| 61 | try { |
| 62 | config = new ConfigBundle(); |
| 63 | config.updateFile(configDir); |
| 64 | } catch (IOException e) { |
| 65 | tracer.error(e); |
| 66 | } |
| 67 | try { |
| 68 | uiconfig = new UiConfigBundle(); |
| 69 | uiconfig.updateFile(configDir); |
| 70 | } catch (IOException e) { |
| 71 | tracer.error(e); |
| 72 | } |
| 73 | |
| 74 | // No updateFile for this one! (we do not want the user to have custom |
| 75 | // translations that won't accept updates from newer versions) |
| 76 | trans = new StringIdBundle(getLang()); |
| 77 | |
| 78 | // Fix an old bug (we used to store custom translation files by |
| 79 | // default): |
| 80 | if (trans.getString(StringId.INPUT_DESC_CBZ) == null) { |
| 81 | trans.deleteFile(configDir); |
| 82 | } |
| 83 | |
| 84 | Bundles.setDirectory(configDir); |
| 85 | |
| 86 | uiconfig = new UiConfigBundle(); |
| 87 | trans = new StringIdBundle(getLang()); |
| 88 | |
| 89 | boolean debug = Instance.getConfig() |
| 90 | .getBoolean(Config.DEBUG_ERR, false); |
| 91 | boolean trace = Instance.getConfig().getBoolean(Config.DEBUG_TRACE, |
| 92 | false); |
| 93 | coverDir = getFile(Config.DEFAULT_COVERS_DIR); |
| 94 | File tmp = getFile(Config.CACHE_DIR); |
| 95 | readerTmp = getFile(UiConfig.CACHE_DIR_LOCAL_READER); |
| 96 | remoteDir = new File(configDir, "remote"); |
| 97 | |
| 98 | if (checkEnv("NOUTF")) { |
| 99 | trans.setUnicode(false); |
| 100 | } |
| 101 | |
| 102 | if (checkEnv("DEBUG")) { |
| 103 | debug = true; |
| 104 | trace = true; |
| 105 | } |
| 106 | |
| 107 | tracer = new TraceHandler(true, debug, trace); |
| 108 | |
| 109 | String remoteLib = config.getString(Config.DEFAULT_LIBRARY); |
| 110 | if (remoteLib == null || remoteLib.trim().isEmpty()) { |
| 111 | String libDir = System.getProperty("fanfix.libdir"); |
| 112 | if (libDir == null || libDir.isEmpty()) { |
| 113 | config.getString(Config.LIBRARY_DIR); |
| 114 | } |
| 115 | try { |
| 116 | lib = new LocalLibrary(getFile(libDir)); |
| 117 | } catch (Exception e) { |
| 118 | tracer.error(new IOException( |
| 119 | "Cannot create library for directory: " |
| 120 | + getFile(libDir), e)); |
| 121 | } |
| 122 | } else { |
| 123 | int pos = remoteLib.lastIndexOf(":"); |
| 124 | if (pos >= 0) { |
| 125 | String port = remoteLib.substring(pos + 1).trim(); |
| 126 | remoteLib = remoteLib.substring(0, pos); |
| 127 | pos = remoteLib.lastIndexOf(":"); |
| 128 | if (pos >= 0) { |
| 129 | String host = remoteLib.substring(pos + 1).trim(); |
| 130 | String key = remoteLib.substring(0, pos).trim(); |
| 131 | |
| 132 | try { |
| 133 | tracer.trace("Selecting remote library " + host + ":" |
| 134 | + port); |
| 135 | lib = new RemoteLibrary(key, host, |
| 136 | Integer.parseInt(port)); |
| 137 | lib = new CacheLibrary(getRemoteDir(host), lib); |
| 138 | |
| 139 | } catch (Exception e) { |
| 140 | } |
| 141 | } |
| 142 | } |
| 143 | |
| 144 | if (lib == null) { |
| 145 | tracer.error(new IOException( |
| 146 | "Cannot create remote library for: " + remoteLib)); |
| 147 | } |
| 148 | } |
| 149 | |
| 150 | // Could have used: System.getProperty("java.io.tmpdir") |
| 151 | if (tmp == null) { |
| 152 | tmp = new File(configDir, "tmp"); |
| 153 | } |
| 154 | if (readerTmp == null) { |
| 155 | readerTmp = new File(configDir, "tmp-reader"); |
| 156 | } |
| 157 | // |
| 158 | |
| 159 | if (coverDir != null && !coverDir.exists()) { |
| 160 | tracer.error(new IOException( |
| 161 | "The 'default covers' directory does not exists: " |
| 162 | + coverDir)); |
| 163 | coverDir = null; |
| 164 | } |
| 165 | |
| 166 | String ua = config.getString(Config.USER_AGENT); |
| 167 | try { |
| 168 | int hours = config.getInteger(Config.CACHE_MAX_TIME_CHANGING, -1); |
| 169 | int hoursLarge = config |
| 170 | .getInteger(Config.CACHE_MAX_TIME_STABLE, -1); |
| 171 | cache = new DataLoader(tmp, ua, hours, hoursLarge); |
| 172 | } catch (IOException e) { |
| 173 | tracer.error(new IOException( |
| 174 | "Cannot create cache (will continue without cache)", e)); |
| 175 | cache = new DataLoader(ua); |
| 176 | } |
| 177 | |
| 178 | cache.setTraceHandler(tracer); |
| 179 | } |
| 180 | |
| 181 | /** |
| 182 | * The traces handler for this {@link Cache}. |
| 183 | * <p> |
| 184 | * It is never NULL. |
| 185 | * |
| 186 | * @return the traces handler (never NULL) |
| 187 | */ |
| 188 | public static TraceHandler getTraceHandler() { |
| 189 | return tracer; |
| 190 | } |
| 191 | |
| 192 | /** |
| 193 | * The traces handler for this {@link Cache}. |
| 194 | * |
| 195 | * @param tracer |
| 196 | * the new traces handler or NULL |
| 197 | */ |
| 198 | public static void setTraceHandler(TraceHandler tracer) { |
| 199 | if (tracer == null) { |
| 200 | tracer = new TraceHandler(false, false, false); |
| 201 | } |
| 202 | |
| 203 | Instance.tracer = tracer; |
| 204 | cache.setTraceHandler(tracer); |
| 205 | } |
| 206 | |
| 207 | /** |
| 208 | * Get the (unique) configuration service for the program. |
| 209 | * |
| 210 | * @return the configuration service |
| 211 | */ |
| 212 | public static ConfigBundle getConfig() { |
| 213 | return config; |
| 214 | } |
| 215 | |
| 216 | /** |
| 217 | * Get the (unique) UI configuration service for the program. |
| 218 | * |
| 219 | * @return the configuration service |
| 220 | */ |
| 221 | public static UiConfigBundle getUiConfig() { |
| 222 | return uiconfig; |
| 223 | } |
| 224 | |
| 225 | /** |
| 226 | * Reset the configuration. |
| 227 | * |
| 228 | * @param resetTrans |
| 229 | * also reset the translation files |
| 230 | */ |
| 231 | public static void resetConfig(boolean resetTrans) { |
| 232 | String dir = Bundles.getDirectory(); |
| 233 | Bundles.setDirectory(null); |
| 234 | try { |
| 235 | try { |
| 236 | ConfigBundle config = new ConfigBundle(); |
| 237 | config.updateFile(configDir); |
| 238 | } catch (IOException e) { |
| 239 | tracer.error(e); |
| 240 | } |
| 241 | try { |
| 242 | UiConfigBundle uiconfig = new UiConfigBundle(); |
| 243 | uiconfig.updateFile(configDir); |
| 244 | } catch (IOException e) { |
| 245 | tracer.error(e); |
| 246 | } |
| 247 | |
| 248 | if (resetTrans) { |
| 249 | try { |
| 250 | StringIdBundle trans = new StringIdBundle(null); |
| 251 | trans.updateFile(configDir); |
| 252 | } catch (IOException e) { |
| 253 | tracer.error(e); |
| 254 | } |
| 255 | } |
| 256 | } finally { |
| 257 | Bundles.setDirectory(dir); |
| 258 | } |
| 259 | } |
| 260 | |
| 261 | /** |
| 262 | * Get the (unique) {@link DataLoader} for the program. |
| 263 | * |
| 264 | * @return the {@link DataLoader} |
| 265 | */ |
| 266 | public static DataLoader getCache() { |
| 267 | return cache; |
| 268 | } |
| 269 | |
| 270 | /** |
| 271 | * Get the (unique) {link StringIdBundle} for the program. |
| 272 | * |
| 273 | * @return the {link StringIdBundle} |
| 274 | */ |
| 275 | public static StringIdBundle getTrans() { |
| 276 | return trans; |
| 277 | } |
| 278 | |
| 279 | /** |
| 280 | * Get the (unique) {@link LocalLibrary} for the program. |
| 281 | * |
| 282 | * @return the {@link LocalLibrary} |
| 283 | */ |
| 284 | public static BasicLibrary getLibrary() { |
| 285 | if (lib == null) { |
| 286 | throw new NullPointerException("We don't have a library to return"); |
| 287 | } |
| 288 | |
| 289 | return lib; |
| 290 | } |
| 291 | |
| 292 | /** |
| 293 | * Return the directory where to look for default cover pages. |
| 294 | * |
| 295 | * @return the default covers directory |
| 296 | */ |
| 297 | public static File getCoverDir() { |
| 298 | return coverDir; |
| 299 | } |
| 300 | |
| 301 | /** |
| 302 | * Return the directory where to store temporary files for the local reader. |
| 303 | * |
| 304 | * @return the directory |
| 305 | */ |
| 306 | public static File getReaderDir() { |
| 307 | return readerTmp; |
| 308 | } |
| 309 | |
| 310 | /** |
| 311 | * Return the directory where to store temporary files for the remote |
| 312 | * {@link LocalLibrary}. |
| 313 | * |
| 314 | * @param host |
| 315 | * the remote for this host |
| 316 | * |
| 317 | * @return the directory |
| 318 | */ |
| 319 | public static File getRemoteDir(String host) { |
| 320 | remoteDir.mkdirs(); |
| 321 | |
| 322 | if (host != null) { |
| 323 | return new File(remoteDir, host); |
| 324 | } |
| 325 | |
| 326 | return remoteDir; |
| 327 | } |
| 328 | |
| 329 | /** |
| 330 | * Check if we need to check that a new version of Fanfix is available. |
| 331 | * |
| 332 | * @return TRUE if we need to |
| 333 | */ |
| 334 | public static boolean isVersionCheckNeeded() { |
| 335 | try { |
| 336 | long wait = config.getInteger(Config.UPDATE_INTERVAL, 1) * 24 * 60 |
| 337 | * 60 * 1000; |
| 338 | if (wait >= 0) { |
| 339 | String lastUpString = IOUtils.readSmallFile(new File(configDir, |
| 340 | "LAST_UPDATE")); |
| 341 | long delay = new Date().getTime() |
| 342 | - Long.parseLong(lastUpString); |
| 343 | if (delay > wait) { |
| 344 | return true; |
| 345 | } |
| 346 | } else { |
| 347 | return false; |
| 348 | } |
| 349 | } catch (Exception e) { |
| 350 | // No file or bad file: |
| 351 | return true; |
| 352 | } |
| 353 | |
| 354 | return false; |
| 355 | } |
| 356 | |
| 357 | /** |
| 358 | * Notify that we checked for a new version of Fanfix. |
| 359 | */ |
| 360 | public static void setVersionChecked() { |
| 361 | try { |
| 362 | IOUtils.writeSmallFile(new File(configDir), "LAST_UPDATE", |
| 363 | Long.toString(new Date().getTime())); |
| 364 | } catch (IOException e) { |
| 365 | tracer.error(e); |
| 366 | } |
| 367 | } |
| 368 | |
| 369 | /** |
| 370 | * Return a path, but support the special $HOME variable. |
| 371 | * |
| 372 | * @return the path |
| 373 | */ |
| 374 | private static File getFile(Config id) { |
| 375 | return getFile(config.getString(id)); |
| 376 | } |
| 377 | |
| 378 | /** |
| 379 | * Return a path, but support the special $HOME variable. |
| 380 | * |
| 381 | * @return the path |
| 382 | */ |
| 383 | private static File getFile(UiConfig id) { |
| 384 | return getFile(uiconfig.getString(id)); |
| 385 | } |
| 386 | |
| 387 | /** |
| 388 | * Return a path, but support the special $HOME variable. |
| 389 | * |
| 390 | * @return the path |
| 391 | */ |
| 392 | private static File getFile(String path) { |
| 393 | File file = null; |
| 394 | if (path != null && !path.isEmpty()) { |
| 395 | path = path.replace('/', File.separatorChar); |
| 396 | if (path.contains("$HOME")) { |
| 397 | path = path.replace("$HOME", getHome()); |
| 398 | } |
| 399 | |
| 400 | file = new File(path); |
| 401 | } |
| 402 | |
| 403 | return file; |
| 404 | } |
| 405 | |
| 406 | /** |
| 407 | * Return the home directory from the system properties. |
| 408 | * |
| 409 | * @return the home |
| 410 | */ |
| 411 | private static String getHome() { |
| 412 | String home = System.getProperty("fanfix.home"); |
| 413 | if (home != null && new File(home).isFile()) { |
| 414 | home = null; |
| 415 | } |
| 416 | |
| 417 | if (home == null || home.trim().isEmpty()) { |
| 418 | home = System.getProperty("user.home"); |
| 419 | if (!new File(home).isDirectory()) { |
| 420 | home = null; |
| 421 | } |
| 422 | } |
| 423 | |
| 424 | if (home == null || home.trim().isEmpty()) { |
| 425 | home = System.getProperty("java.io.tmpdir"); |
| 426 | if (!new File(home).isDirectory()) { |
| 427 | home = null; |
| 428 | } |
| 429 | } |
| 430 | |
| 431 | if (home == null) { |
| 432 | home = ""; |
| 433 | } |
| 434 | |
| 435 | return home; |
| 436 | } |
| 437 | |
| 438 | /** |
| 439 | * The language to use for the application (NULL = default system language). |
| 440 | * |
| 441 | * @return the language |
| 442 | */ |
| 443 | private static String getLang() { |
| 444 | String lang = config.getString(Config.LANG); |
| 445 | |
| 446 | if (System.getenv("LANG") != null && !System.getenv("LANG").isEmpty()) { |
| 447 | lang = System.getenv("LANG"); |
| 448 | } |
| 449 | |
| 450 | if (lang != null && lang.isEmpty()) { |
| 451 | lang = null; |
| 452 | } |
| 453 | |
| 454 | return lang; |
| 455 | } |
| 456 | |
| 457 | /** |
| 458 | * Check that the given environment variable is "enabled". |
| 459 | * |
| 460 | * @param key |
| 461 | * the variable to check |
| 462 | * |
| 463 | * @return TRUE if it is |
| 464 | */ |
| 465 | private static boolean checkEnv(String key) { |
| 466 | String value = System.getenv(key); |
| 467 | if (value != null) { |
| 468 | value = value.trim().toLowerCase(); |
| 469 | if ("yes".equals(value) || "true".equals(value) |
| 470 | || "on".equals(value) || "1".equals(value) |
| 471 | || "y".equals(value)) { |
| 472 | return true; |
| 473 | } |
| 474 | } |
| 475 | |
| 476 | return false; |
| 477 | } |
| 478 | } |