| 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.StringIdBundle; |
| 10 | import be.nikiroo.fanfix.bundles.UiConfig; |
| 11 | import be.nikiroo.fanfix.bundles.UiConfigBundle; |
| 12 | import be.nikiroo.fanfix.output.BasicOutput.OutputType; |
| 13 | import be.nikiroo.utils.IOUtils; |
| 14 | import be.nikiroo.utils.resources.Bundles; |
| 15 | |
| 16 | /** |
| 17 | * Global state for the program (services and singletons). |
| 18 | * |
| 19 | * @author niki |
| 20 | */ |
| 21 | public class Instance { |
| 22 | private static ConfigBundle config; |
| 23 | private static UiConfigBundle uiconfig; |
| 24 | private static StringIdBundle trans; |
| 25 | private static Cache cache; |
| 26 | private static LocalLibrary lib; |
| 27 | private static boolean debug; |
| 28 | private static File coverDir; |
| 29 | private static File readerTmp; |
| 30 | private static File remoteDir; |
| 31 | private static String configDir; |
| 32 | |
| 33 | static { |
| 34 | // Most of the rest is dependent upon this: |
| 35 | config = new ConfigBundle(); |
| 36 | |
| 37 | configDir = System.getProperty("CONFIG_DIR"); |
| 38 | if (configDir == null) { |
| 39 | configDir = System.getenv("CONFIG_DIR"); |
| 40 | } |
| 41 | |
| 42 | if (configDir == null) { |
| 43 | configDir = new File(System.getProperty("user.home"), ".fanfix") |
| 44 | .getPath(); |
| 45 | } |
| 46 | |
| 47 | if (!new File(configDir).exists()) { |
| 48 | new File(configDir).mkdirs(); |
| 49 | } else { |
| 50 | Bundles.setDirectory(configDir); |
| 51 | } |
| 52 | |
| 53 | try { |
| 54 | config = new ConfigBundle(); |
| 55 | config.updateFile(configDir); |
| 56 | } catch (IOException e) { |
| 57 | syserr(e); |
| 58 | } |
| 59 | try { |
| 60 | uiconfig = new UiConfigBundle(); |
| 61 | uiconfig.updateFile(configDir); |
| 62 | } catch (IOException e) { |
| 63 | syserr(e); |
| 64 | } |
| 65 | try { |
| 66 | trans = new StringIdBundle(getLang()); |
| 67 | trans.updateFile(configDir); |
| 68 | } catch (IOException e) { |
| 69 | syserr(e); |
| 70 | } |
| 71 | |
| 72 | Bundles.setDirectory(configDir); |
| 73 | |
| 74 | uiconfig = new UiConfigBundle(); |
| 75 | trans = new StringIdBundle(getLang()); |
| 76 | try { |
| 77 | lib = new LocalLibrary(getFile(Config.LIBRARY_DIR), |
| 78 | OutputType.INFO_TEXT, OutputType.CBZ); |
| 79 | } catch (Exception e) { |
| 80 | syserr(new IOException("Cannot create library for directory: " |
| 81 | + getFile(Config.LIBRARY_DIR), e)); |
| 82 | } |
| 83 | |
| 84 | debug = Instance.getConfig().getBoolean(Config.DEBUG_ERR, false); |
| 85 | coverDir = getFile(Config.DEFAULT_COVERS_DIR); |
| 86 | File tmp = getFile(Config.CACHE_DIR); |
| 87 | readerTmp = getFile(UiConfig.CACHE_DIR_LOCAL_READER); |
| 88 | remoteDir = new File(getFile(Config.LIBRARY_DIR), "remote"); |
| 89 | |
| 90 | if (checkEnv("NOUTF")) { |
| 91 | trans.setUnicode(false); |
| 92 | } |
| 93 | |
| 94 | if (checkEnv("DEBUG")) { |
| 95 | debug = true; |
| 96 | } |
| 97 | |
| 98 | // Could have used: System.getProperty("java.io.tmpdir") |
| 99 | if (tmp == null) { |
| 100 | tmp = new File(configDir, "tmp"); |
| 101 | } |
| 102 | if (readerTmp == null) { |
| 103 | readerTmp = new File(configDir, "tmp-reader"); |
| 104 | } |
| 105 | // |
| 106 | |
| 107 | if (coverDir != null && !coverDir.exists()) { |
| 108 | syserr(new IOException( |
| 109 | "The 'default covers' directory does not exists: " |
| 110 | + coverDir)); |
| 111 | coverDir = null; |
| 112 | } |
| 113 | |
| 114 | try { |
| 115 | String ua = config.getString(Config.USER_AGENT); |
| 116 | int hours = config.getInteger(Config.CACHE_MAX_TIME_CHANGING, -1); |
| 117 | int hoursLarge = config |
| 118 | .getInteger(Config.CACHE_MAX_TIME_STABLE, -1); |
| 119 | |
| 120 | cache = new Cache(tmp, ua, hours, hoursLarge); |
| 121 | } catch (IOException e) { |
| 122 | syserr(new IOException( |
| 123 | "Cannot create cache (will continue without cache)", e)); |
| 124 | } |
| 125 | } |
| 126 | |
| 127 | /** |
| 128 | * Get the (unique) configuration service for the program. |
| 129 | * |
| 130 | * @return the configuration service |
| 131 | */ |
| 132 | public static ConfigBundle getConfig() { |
| 133 | return config; |
| 134 | } |
| 135 | |
| 136 | /** |
| 137 | * Get the (unique) UI configuration service for the program. |
| 138 | * |
| 139 | * @return the configuration service |
| 140 | */ |
| 141 | public static UiConfigBundle getUiConfig() { |
| 142 | return uiconfig; |
| 143 | } |
| 144 | |
| 145 | /** |
| 146 | * Get the (unique) {@link Cache} for the program. |
| 147 | * |
| 148 | * @return the {@link Cache} |
| 149 | */ |
| 150 | public static Cache getCache() { |
| 151 | return cache; |
| 152 | } |
| 153 | |
| 154 | /** |
| 155 | * Get the (unique) {link StringIdBundle} for the program. |
| 156 | * |
| 157 | * @return the {link StringIdBundle} |
| 158 | */ |
| 159 | public static StringIdBundle getTrans() { |
| 160 | return trans; |
| 161 | } |
| 162 | |
| 163 | /** |
| 164 | * Get the (unique) {@link LocalLibrary} for the program. |
| 165 | * |
| 166 | * @return the {@link LocalLibrary} |
| 167 | */ |
| 168 | public static BasicLibrary getLibrary() { |
| 169 | return lib; |
| 170 | } |
| 171 | |
| 172 | /** |
| 173 | * Return the directory where to look for default cover pages. |
| 174 | * |
| 175 | * @return the default covers directory |
| 176 | */ |
| 177 | public static File getCoverDir() { |
| 178 | return coverDir; |
| 179 | } |
| 180 | |
| 181 | /** |
| 182 | * Return the directory where to store temporary files for the local reader. |
| 183 | * |
| 184 | * @return the directory |
| 185 | */ |
| 186 | public static File getReaderDir() { |
| 187 | return readerTmp; |
| 188 | } |
| 189 | |
| 190 | /** |
| 191 | * Return the directory where to store temporary files for the remote |
| 192 | * {@link LocalLibrary}. |
| 193 | * |
| 194 | * @param host |
| 195 | * the remote for this host |
| 196 | * |
| 197 | * @return the directory |
| 198 | */ |
| 199 | public static File getRemoteDir(String host) { |
| 200 | remoteDir.mkdirs(); |
| 201 | |
| 202 | if (host != null) { |
| 203 | return new File(remoteDir, host); |
| 204 | } |
| 205 | |
| 206 | return remoteDir; |
| 207 | } |
| 208 | |
| 209 | /** |
| 210 | * Check if we need to check that a new version of Fanfix is available. |
| 211 | * |
| 212 | * @return TRUE if we need to |
| 213 | */ |
| 214 | public static boolean isVersionCheckNeeded() { |
| 215 | try { |
| 216 | long wait = config.getInteger(Config.UPDATE_INTERVAL, 1) * 24 * 60 |
| 217 | * 60 * 1000; |
| 218 | if (wait >= 0) { |
| 219 | String lastUpString = IOUtils.readSmallFile(new File(configDir, |
| 220 | "LAST_UPDATE")); |
| 221 | long delay = new Date().getTime() |
| 222 | - Long.parseLong(lastUpString); |
| 223 | if (delay > wait) { |
| 224 | return true; |
| 225 | } |
| 226 | } else { |
| 227 | return false; |
| 228 | } |
| 229 | } catch (Exception e) { |
| 230 | // No file or bad file: |
| 231 | return true; |
| 232 | } |
| 233 | |
| 234 | return false; |
| 235 | } |
| 236 | |
| 237 | /** |
| 238 | * Notify that we checked for a new version of Fanfix. |
| 239 | */ |
| 240 | public static void setVersionChecked() { |
| 241 | try { |
| 242 | IOUtils.writeSmallFile(new File(configDir), "LAST_UPDATE", |
| 243 | Long.toString(new Date().getTime())); |
| 244 | } catch (IOException e) { |
| 245 | syserr(e); |
| 246 | } |
| 247 | } |
| 248 | |
| 249 | /** |
| 250 | * Report an error to the user |
| 251 | * |
| 252 | * @param e |
| 253 | * the {@link Exception} to report |
| 254 | */ |
| 255 | public static void syserr(Exception e) { |
| 256 | if (debug) { |
| 257 | e.printStackTrace(); |
| 258 | } else { |
| 259 | System.err.println(e.getMessage()); |
| 260 | } |
| 261 | } |
| 262 | |
| 263 | /** |
| 264 | * Return a path, but support the special $HOME variable. |
| 265 | * |
| 266 | * @return the path |
| 267 | */ |
| 268 | private static File getFile(Config id) { |
| 269 | return getFile(config.getString(id)); |
| 270 | } |
| 271 | |
| 272 | /** |
| 273 | * Return a path, but support the special $HOME variable. |
| 274 | * |
| 275 | * @return the path |
| 276 | */ |
| 277 | private static File getFile(UiConfig id) { |
| 278 | return getFile(uiconfig.getString(id)); |
| 279 | } |
| 280 | |
| 281 | /** |
| 282 | * Return a path, but support the special $HOME variable. |
| 283 | * |
| 284 | * @return the path |
| 285 | */ |
| 286 | private static File getFile(String path) { |
| 287 | File file = null; |
| 288 | if (path != null && !path.isEmpty()) { |
| 289 | path = path.replace('/', File.separatorChar); |
| 290 | if (path.contains("$HOME")) { |
| 291 | path = path.replace("$HOME", |
| 292 | "" + System.getProperty("user.home")); |
| 293 | } |
| 294 | |
| 295 | file = new File(path); |
| 296 | } |
| 297 | |
| 298 | return file; |
| 299 | } |
| 300 | |
| 301 | /** |
| 302 | * The language to use for the application (NULL = default system language). |
| 303 | * |
| 304 | * @return the language |
| 305 | */ |
| 306 | private static String getLang() { |
| 307 | String lang = config.getString(Config.LANG); |
| 308 | |
| 309 | if (System.getenv("LANG") != null && !System.getenv("LANG").isEmpty()) { |
| 310 | lang = System.getenv("LANG"); |
| 311 | } |
| 312 | |
| 313 | if (lang != null && lang.isEmpty()) { |
| 314 | lang = null; |
| 315 | } |
| 316 | |
| 317 | return lang; |
| 318 | } |
| 319 | |
| 320 | /** |
| 321 | * Check that the given environment variable is "enabled". |
| 322 | * |
| 323 | * @param key |
| 324 | * the variable to check |
| 325 | * |
| 326 | * @return TRUE if it is |
| 327 | */ |
| 328 | private static boolean checkEnv(String key) { |
| 329 | String value = System.getenv(key); |
| 330 | if (value != null) { |
| 331 | value = value.trim().toLowerCase(); |
| 332 | if ("yes".equals(value) || "true".equals(value) |
| 333 | || "on".equals(value) || "1".equals(value) |
| 334 | || "y".equals(value)) { |
| 335 | return true; |
| 336 | } |
| 337 | } |
| 338 | |
| 339 | return false; |
| 340 | } |
| 341 | } |