From 3727aae2b0516c5ed5366cde29544565f1d6c6b0 Mon Sep 17 00:00:00 2001 From: Niki Roo Date: Sun, 12 Feb 2017 00:21:24 +0100 Subject: [PATCH] Prepare for LocalReader This will allow HTML or TEXT conversion first if configured --- src/be/nikiroo/fanfix/Instance.java | 39 +++++++--- src/be/nikiroo/fanfix/Main.java | 7 +- src/be/nikiroo/fanfix/bundles/Config.java | 4 + .../nikiroo/fanfix/bundles/config.properties | 6 ++ .../{FanfixReader.java => BasicReader.java} | 76 ++++++++++++++++++- .../fanfix/reader/{cli => }/CliReader.java | 20 +---- 6 files changed, 118 insertions(+), 34 deletions(-) rename src/be/nikiroo/fanfix/reader/{FanfixReader.java => BasicReader.java} (57%) rename src/be/nikiroo/fanfix/reader/{cli => }/CliReader.java (84%) diff --git a/src/be/nikiroo/fanfix/Instance.java b/src/be/nikiroo/fanfix/Instance.java index 5c198ee..94e854a 100644 --- a/src/be/nikiroo/fanfix/Instance.java +++ b/src/be/nikiroo/fanfix/Instance.java @@ -20,6 +20,7 @@ public class Instance { private static Library lib; private static boolean debug; private static File coverDir; + private static File readerTmp; static { config = new ConfigBundle(); @@ -29,6 +30,23 @@ public class Instance { lib = new Library(getFile(Config.LIBRARY_DIR)); debug = Instance.getConfig().getBoolean(Config.DEBUG_ERR, false); coverDir = getFile(Config.DEFAULT_COVERS_DIR); + File tmp = getFile(Config.CACHE_DIR); + readerTmp = getFile(Config.CACHE_DIR_LOCAL_READER); + + if (tmp == null || readerTmp == null) { + String tmpDir = System.getProperty("java.io.tmpdir"); + if (tmpDir != null) { + if (tmp == null) { + tmp = new File(tmpDir, "fanfic-tmp"); + } + if (readerTmp == null) { + tmp = new File(tmpDir, "fanfic-reader"); + } + } else { + syserr(new IOException( + "The system does not have a default temporary directory")); + } + } if (coverDir != null && !coverDir.exists()) { syserr(new IOException( @@ -71,22 +89,12 @@ public class Instance { } try { - File tmp = getFile(Config.CACHE_DIR); + String ua = config.getString(Config.USER_AGENT); int hours = config.getInteger(Config.CACHE_MAX_TIME_CHANGING, -1); int hoursLarge = config .getInteger(Config.CACHE_MAX_TIME_STABLE, -1); - if (tmp == null) { - String tmpDir = System.getProperty("java.io.tmpdir"); - if (tmpDir != null) { - tmp = new File(tmpDir, "fanfic-tmp"); - } else { - syserr(new IOException( - "The system does not have a default temporary directory")); - } - } - cache = new Cache(tmp, ua, hours, hoursLarge); } catch (IOException e) { syserr(new IOException( @@ -139,6 +147,15 @@ public class Instance { return coverDir; } + /** + * Return the directory where to store temporary files for the local reader. + * + * @return the directory + */ + public static File getReaderDir() { + return readerTmp; + } + /** * Report an error to the user * diff --git a/src/be/nikiroo/fanfix/Main.java b/src/be/nikiroo/fanfix/Main.java index 3ecc612..30febd3 100644 --- a/src/be/nikiroo/fanfix/Main.java +++ b/src/be/nikiroo/fanfix/Main.java @@ -10,8 +10,7 @@ import be.nikiroo.fanfix.data.Chapter; import be.nikiroo.fanfix.data.Story; import be.nikiroo.fanfix.output.BasicOutput; import be.nikiroo.fanfix.output.BasicOutput.OutputType; -import be.nikiroo.fanfix.reader.FanfixReader; -import be.nikiroo.fanfix.reader.cli.CliReader; +import be.nikiroo.fanfix.reader.BasicReader; import be.nikiroo.fanfix.supported.BasicSupport; import be.nikiroo.fanfix.supported.BasicSupport.SupportType; @@ -192,7 +191,7 @@ public class Main { return 1; } - new CliReader().start(type); + BasicReader.getReader().start(type); return 0; } @@ -214,7 +213,7 @@ public class Main { */ private static int read(String story, String chap, boolean library) { try { - FanfixReader reader = new CliReader(); + BasicReader reader = BasicReader.getReader(); if (library) { reader.setStory(story); } else { diff --git a/src/be/nikiroo/fanfix/bundles/Config.java b/src/be/nikiroo/fanfix/bundles/Config.java index 969eb27..7a663e4 100644 --- a/src/be/nikiroo/fanfix/bundles/Config.java +++ b/src/be/nikiroo/fanfix/bundles/Config.java @@ -10,8 +10,12 @@ import be.nikiroo.utils.resources.Meta; public enum Config { @Meta(what = "language", where = "", format = "language (example: en-GB) or nothing for default system language", info = "Force the language (can be overwritten again with the env variable $LANG)") LANG, // + @Meta(what = "reader type", where = "", format = "CLI or LOCAL", info = "Select the reader to use to read stories (CLI = simple output to console, LOCAL = use local system file handler)") + READER_TYPE, // @Meta(what = "directory", where = "", format = "absolute path, $HOME variable supported, / is always accepted as dir separator", info = "The directory where to store temporary files, defaults to a directory 'fanfic-tmp' in the system default temporary directory") CACHE_DIR, // + @Meta(what = "directory", where = "", format = "absolute path, $HOME variable supported, / is always accepted as dir separator", info = "The directory where to store temporary files, defaults to a directory 'fanfic-reader' in the system default temporary directory") + CACHE_DIR_LOCAL_READER, // @Meta(what = "delay in hours", where = "", format = "integer | 0: no cache | -1: infinite time cache which is default", info = "The delay after which a cached resource that is thought to change ~often is considered too old and triggers a refresh") CACHE_MAX_TIME_CHANGING, // @Meta(what = "delay in hours", where = "", format = "integer | 0: no cache | -1: infinite time cache which is default", info = "The delay after which a cached resource that is thought to change rarely is considered too old and triggers a refresh") diff --git a/src/be/nikiroo/fanfix/bundles/config.properties b/src/be/nikiroo/fanfix/bundles/config.properties index 8a1d6c1..79f2ac9 100644 --- a/src/be/nikiroo/fanfix/bundles/config.properties +++ b/src/be/nikiroo/fanfix/bundles/config.properties @@ -5,9 +5,15 @@ # (WHAT: language, FORMAT: language (example: en-GB) or nothing for default system language) # Force the language (can be overwritten again with the env variable $LANG) LANG = +# (WHAT: reader type, FORMAT: CLI or LOCAL) +# Select the reader to use to read stories (CLI = simple output to console, LOCAL = use local system file handler) +READER_TYPE = # (WHAT: directory, FORMAT: absolute path, $HOME variable supported, / is always accepted as dir separator) # The directory where to store temporary files, defaults to a directory 'fanfic-tmp' in the system default temporary directory CACHE_DIR = +# (WHAT: directory, FORMAT: absolute path, $HOME variable supported, / is always accepted as dir separator) +# The directory where to store temporary files, defaults to a directory 'fanfic-reader' in the system default temporary directory +CACHE_DIR_LOCAL_READER = # (WHAT: delay in hours, FORMAT: integer | 0: no cache | -1: infinite time cache which is default) # The delay after which a cached resource that is thought to change ~often is considered too old and triggers a refresh CACHE_MAX_TIME_CHANGING = 24 diff --git a/src/be/nikiroo/fanfix/reader/FanfixReader.java b/src/be/nikiroo/fanfix/reader/BasicReader.java similarity index 57% rename from src/be/nikiroo/fanfix/reader/FanfixReader.java rename to src/be/nikiroo/fanfix/reader/BasicReader.java index 009271e..eeb00af 100644 --- a/src/be/nikiroo/fanfix/reader/FanfixReader.java +++ b/src/be/nikiroo/fanfix/reader/BasicReader.java @@ -16,8 +16,41 @@ import be.nikiroo.fanfix.supported.BasicSupport.SupportType; * * @author niki */ -public abstract class FanfixReader { +public abstract class BasicReader { + public enum ReaderType { + /** Simple reader that outputs everything on the console */ + CLI, + /** Reader that starts local programs to handle the stories */ + LOCAL + } + + private static ReaderType defaultType; private Story story; + private ReaderType type; + + static { + // TODO: default type from config + } + + /** + * The type of this reader. + * + * @return the type + */ + public ReaderType getType() { + return type; + } + + /** + * The type of this reader. + * + * @param type + * the new type + */ + protected BasicReader setType(ReaderType type) { + this.type = type; + return this; + } /** * Return the current {@link Story}. @@ -29,7 +62,7 @@ public abstract class FanfixReader { } /** - * Create a new {@link FanfixReader} for a {@link Story} in the + * Create a new {@link BasicReader} for a {@link Story} in the * {@link Library} . * * @param luid @@ -45,7 +78,7 @@ public abstract class FanfixReader { } /** - * Create a new {@link FanfixReader} for an external {@link Story}. + * Create a new {@link BasicReader} for an external {@link Story}. * * @param source * the {@link Story} {@link URL} @@ -93,4 +126,41 @@ public abstract class FanfixReader { * all */ public abstract void start(SupportType type); + + /** + * Return a new {@link BasicReader} ready for use. + * + * @return a {@link BasicReader} + */ + public static BasicReader getReader() { + switch (defaultType) { + // case LOCAL: + // return new LocalReader().setType(ReaderType.LOCAL); + case CLI: + return new CliReader().setType(ReaderType.CLI); + } + + return null; + } + + /** + * The default {@link ReaderType} used when calling + * {@link BasicReader#getReader()}. + * + * @return the default type + */ + public static ReaderType getDefaultReaderType() { + return defaultType; + } + + /** + * The default {@link ReaderType} used when calling + * {@link BasicReader#getReader()}. + * + * @param defaultType + * the new default type + */ + public static void setDefaultReaderType(ReaderType defaultType) { + BasicReader.defaultType = defaultType; + } } diff --git a/src/be/nikiroo/fanfix/reader/cli/CliReader.java b/src/be/nikiroo/fanfix/reader/CliReader.java similarity index 84% rename from src/be/nikiroo/fanfix/reader/cli/CliReader.java rename to src/be/nikiroo/fanfix/reader/CliReader.java index e040a21..b3dd3ed 100644 --- a/src/be/nikiroo/fanfix/reader/cli/CliReader.java +++ b/src/be/nikiroo/fanfix/reader/CliReader.java @@ -1,4 +1,4 @@ -package be.nikiroo.fanfix.reader.cli; +package be.nikiroo.fanfix.reader; import java.io.IOException; import java.util.List; @@ -9,7 +9,6 @@ import be.nikiroo.fanfix.data.Chapter; import be.nikiroo.fanfix.data.MetaData; import be.nikiroo.fanfix.data.Paragraph; import be.nikiroo.fanfix.data.Story; -import be.nikiroo.fanfix.reader.FanfixReader; import be.nikiroo.fanfix.supported.BasicSupport.SupportType; /** @@ -19,14 +18,8 @@ import be.nikiroo.fanfix.supported.BasicSupport.SupportType; * * @author niki */ -public class CliReader extends FanfixReader { - /** - * Start the {@link Story} Reading. - * - * @throws IOException - * in case of I/O error or if the {@link Story} was not - * previously set - */ +class CliReader extends BasicReader { + @Override public void read() throws IOException { if (getStory() == null) { throw new IOException("No story to read"); @@ -65,12 +58,7 @@ public class CliReader extends FanfixReader { } } - /** - * Read the selected chapter (starting at 1). - * - * @param chapter - * the chapter - */ + @Override public void read(int chapter) { if (chapter > getStory().getChapters().size()) { System.err.println("Chapter " + chapter + ": no such chapter"); -- 2.27.0