Instance: use getInstance()
[nikiroo-utils.git] / src / be / nikiroo / fanfix / reader / BasicReader.java
index eeb00af1c6ff2f5439af6c4bdfbe9bd8669142e4..7f79da3a8ec62b854b7bcdba62838a23779fcaf4 100644 (file)
 package be.nikiroo.fanfix.reader;
 
+import java.io.File;
 import java.io.IOException;
+import java.net.MalformedURLException;
 import java.net.URL;
+import java.text.ParseException;
+import java.text.SimpleDateFormat;
+import java.util.Date;
+import java.util.Map;
+import java.util.TreeMap;
 
 import be.nikiroo.fanfix.Instance;
-import be.nikiroo.fanfix.Library;
+import be.nikiroo.fanfix.bundles.Config;
+import be.nikiroo.fanfix.bundles.UiConfig;
+import be.nikiroo.fanfix.data.MetaData;
 import be.nikiroo.fanfix.data.Story;
+import be.nikiroo.fanfix.library.BasicLibrary;
+import be.nikiroo.fanfix.library.LocalLibrary;
 import be.nikiroo.fanfix.supported.BasicSupport;
-import be.nikiroo.fanfix.supported.BasicSupport.SupportType;
+import be.nikiroo.utils.Progress;
+import be.nikiroo.utils.StringUtils;
+import be.nikiroo.utils.serial.SerialUtils;
 
 /**
- * Command line {@link Story} reader.
+ * The class that handles the different {@link Story} readers you can use.
  * <p>
- * Will output stories to the console.
+ * All the readers should be accessed via {@link BasicReader#getReader()}.
  * 
  * @author niki
  */
-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
-       }
+public abstract class BasicReader implements Reader {
+       private static BasicLibrary defaultLibrary = Instance.getInstance().getLibrary();
+       private static ReaderType defaultType = ReaderType.GUI;
 
-       private static ReaderType defaultType;
+       private BasicLibrary lib;
+       private MetaData meta;
        private Story story;
-       private ReaderType type;
+       private int chapter;
 
+       /**
+        * Take the default reader type configuration from the config file.
+        */
        static {
-               // TODO: default type from config
+               String typeString = Instance.getInstance().getConfig().getString(Config.READER_TYPE);
+               if (typeString != null && !typeString.isEmpty()) {
+                       try {
+                               ReaderType type = ReaderType.valueOf(typeString.toUpperCase());
+                               defaultType = type;
+                       } catch (IllegalArgumentException e) {
+                               // Do nothing
+                       }
+               }
+       }
+
+       @Override
+       public synchronized Story getStory(Progress pg) throws IOException {
+               if (story == null) {
+                       story = getLibrary().getStory(meta.getLuid(), pg);
+               }
+
+               return story;
+       }
+
+       @Override
+       public BasicLibrary getLibrary() {
+               if (lib == null) {
+                       lib = defaultLibrary;
+               }
+
+               return lib;
+       }
+
+       @Override
+       public void setLibrary(BasicLibrary lib) {
+               this.lib = lib;
+       }
+
+       @Override
+       public synchronized MetaData getMeta() {
+               return meta;
+       }
+
+       @Override
+       public synchronized void setMeta(MetaData meta) throws IOException {
+               setMeta(meta == null ? null : meta.getLuid()); // must check the library
+       }
+
+       @Override
+       public synchronized void setMeta(String luid) throws IOException {
+               story = null;
+               meta = getLibrary().getInfo(luid);
+
+               if (meta == null) {
+                       throw new IOException("Cannot retrieve story from library: " + luid);
+               }
+       }
+
+       @Override
+       public synchronized void setMeta(URL url, Progress pg) throws IOException {
+               BasicSupport support = BasicSupport.getSupport(url);
+               if (support == null) {
+                       throw new IOException("URL not supported: " + url.toString());
+               }
+
+               story = support.process(pg);
+               if (story == null) {
+                       throw new IOException(
+                                       "Cannot retrieve story from external source: "
+                                                       + url.toString());
+               }
+
+               meta = story.getMeta();
+       }
+
+       @Override
+       public int getChapter() {
+               return chapter;
+       }
+
+       @Override
+       public void setChapter(int chapter) {
+               this.chapter = chapter;
        }
 
        /**
-        * The type of this reader.
+        * Return a new {@link BasicReader} ready for use if one is configured.
+        * <p>
+        * Can return NULL if none are configured.
         * 
-        * @return the type
+        * @return a {@link BasicReader}, or NULL if none configured
         */
-       public ReaderType getType() {
-               return type;
+       public static Reader getReader() {
+               try {
+                       if (defaultType != null) {
+                               return (Reader) SerialUtils.createObject(defaultType
+                                               .getTypeName());
+                       }
+               } catch (Exception e) {
+                       Instance.getInstance().getTraceHandler()
+                                       .error(new Exception("Cannot create a reader of type: " + defaultType + " (Not compiled in?)", e));
+               }
+
+               return null;
        }
 
        /**
-        * The type of this reader.
+        * The default {@link Reader.ReaderType} used when calling
+        * {@link BasicReader#getReader()}.
         * 
-        * @param type
-        *            the new type
+        * @return the default type
         */
-       protected BasicReader setType(ReaderType type) {
-               this.type = type;
-               return this;
+       public static ReaderType getDefaultReaderType() {
+               return defaultType;
        }
 
        /**
-        * Return the current {@link Story}.
+        * The default {@link Reader.ReaderType} used when calling
+        * {@link BasicReader#getReader()}.
         * 
-        * @return the {@link Story}
+        * @param defaultType
+        *            the new default type
         */
-       public Story getStory() {
-               return story;
+       public static void setDefaultReaderType(ReaderType defaultType) {
+               BasicReader.defaultType = defaultType;
        }
 
        /**
-        * Create a new {@link BasicReader} for a {@link Story} in the
-        * {@link Library} .
+        * Change the default {@link LocalLibrary} to open with the
+        * {@link BasicReader}s.
         * 
-        * @param luid
-        *            the {@link Story} ID
-        * @throws IOException
-        *             in case of I/O error
+        * @param lib
+        *            the new {@link LocalLibrary}
         */
-       public void setStory(String luid) throws IOException {
-               story = Instance.getLibrary().getStory(luid);
-               if (story == null) {
-                       throw new IOException("Cannot retrieve story from library: " + luid);
-               }
+       public static void setDefaultLibrary(BasicLibrary lib) {
+               BasicReader.defaultLibrary = lib;
        }
 
        /**
-        * Create a new {@link BasicReader} for an external {@link Story}.
+        * Return an {@link URL} from this {@link String}, be it a file path or an
+        * actual {@link URL}.
         * 
-        * @param source
-        *            the {@link Story} {@link URL}
-        * @throws IOException
-        *             in case of I/O error
+        * @param sourceString
+        *            the source
+        * 
+        * @return the corresponding {@link URL}
+        * 
+        * @throws MalformedURLException
+        *             if this is neither a file nor a conventional {@link URL}
         */
-       public void setStory(URL source) throws IOException {
-               BasicSupport support = BasicSupport.getSupport(source);
-               if (support == null) {
-                       throw new IOException("URL not supported: " + source.toString());
+       public static URL getUrl(String sourceString) throws MalformedURLException {
+               if (sourceString == null || sourceString.isEmpty()) {
+                       throw new MalformedURLException("Empty url");
                }
 
-               story = support.process(source);
-               if (story == null) {
-                       throw new IOException(
-                                       "Cannot retrieve story from external source: "
-                                                       + source.toString());
-
+               URL source = null;
+               try {
+                       source = new URL(sourceString);
+               } catch (MalformedURLException e) {
+                       File sourceFile = new File(sourceString);
+                       source = sourceFile.toURI().toURL();
                }
+
+               return source;
        }
 
        /**
-        * Start the {@link Story} Reading.
+        * Describe a {@link Story} from its {@link MetaData} and return a list of
+        * title/value that represent this {@link Story}.
         * 
-        * @throws IOException
-        *             in case of I/O error or if the {@link Story} was not
-        *             previously set
-        */
-       public abstract void read() throws IOException;
-
-       /**
-        * Read the selected chapter (starting at 1).
+        * @param meta
+        *            the {@link MetaData} to represent
         * 
-        * @param chapter
-        *            the chapter
+        * @return the information
         */
-       public abstract void read(int chapter);
+       public static Map<String, String> getMetaDesc(MetaData meta) {
+               Map<String, String> metaDesc = new TreeMap<String, String>();
+
+               // TODO: i18n
+
+               StringBuilder tags = new StringBuilder();
+               for (String tag : meta.getTags()) {
+                       if (tags.length() > 0) {
+                               tags.append(", ");
+                       }
+                       tags.append(tag);
+               }
+
+               // TODO: i18n
+               metaDesc.put("Author", meta.getAuthor());
+               metaDesc.put("Publication date", formatDate(meta.getDate()));
+               metaDesc.put("Published on", meta.getPublisher());
+               metaDesc.put("URL", meta.getUrl());
+               String count = "";
+               if (meta.getWords() > 0) {
+                       count = StringUtils.formatNumber(meta.getWords());
+               }
+               if (meta.isImageDocument()) {
+                       metaDesc.put("Number of images", count);
+               } else {
+                       metaDesc.put("Number of words", count);
+               }
+               metaDesc.put("Source", meta.getSource());
+               metaDesc.put("Subject", meta.getSubject());
+               metaDesc.put("Language", meta.getLang());
+               metaDesc.put("Tags", tags.toString());
+
+               return metaDesc;
+       }
 
        /**
-        * Start the reader in browse mode for the given type (or pass NULL for all
-        * types).
+        * Open the {@link Story} with an external reader (the program will be
+        * passed the main file associated with this {@link Story}).
+        * 
+        * @param lib
+        *            the {@link BasicLibrary} to select the {@link Story} from
+        * @param luid
+        *            the {@link Story} LUID
+        * @param sync
+        *            execute the process synchronously (wait until it is terminated
+        *            before returning)
         * 
-        * @param type
-        *            the type of {@link Story} to take into account, or NULL for
-        *            all
+        * @throws IOException
+        *             in case of I/O error
         */
-       public abstract void start(SupportType type);
+       @Override
+       public void openExternal(BasicLibrary lib, String luid, boolean sync)
+                       throws IOException {
+               MetaData meta = lib.getInfo(luid);
+               File target = lib.getFile(luid, null);
+
+               openExternal(meta, target, sync);
+       }
 
        /**
-        * Return a new {@link BasicReader} ready for use.
+        * Open the {@link Story} with an external reader (the program will be
+        * passed the given target file).
         * 
-        * @return a {@link BasicReader}
+        * @param meta
+        *            the {@link Story} to load
+        * @param target
+        *            the target {@link File}
+        * @param sync
+        *            execute the process synchronously (wait until it is terminated
+        *            before returning)
+        * 
+        * @throws IOException
+        *             in case of I/O error
         */
-       public static BasicReader getReader() {
-               switch (defaultType) {
-               // case LOCAL:
-               // return new LocalReader().setType(ReaderType.LOCAL);
-               case CLI:
-                       return new CliReader().setType(ReaderType.CLI);
+       protected void openExternal(MetaData meta, File target, boolean sync)
+                       throws IOException {
+               String program = null;
+               if (meta.isImageDocument()) {
+                       program = Instance.getInstance().getUiConfig().getString(UiConfig.IMAGES_DOCUMENT_READER);
+               } else {
+                       program = Instance.getInstance().getUiConfig().getString(UiConfig.NON_IMAGES_DOCUMENT_READER);
                }
 
-               return null;
+               if (program != null && program.trim().isEmpty()) {
+                       program = null;
+               }
+
+               start(target, program, sync);
        }
 
        /**
-        * The default {@link ReaderType} used when calling
-        * {@link BasicReader#getReader()}.
+        * Start a file and open it with the given program if given or the first
+        * default system starter we can find.
         * 
-        * @return the default type
+        * @param target
+        *            the target to open
+        * @param program
+        *            the program to use or NULL for the default system starter
+        * @param sync
+        *            execute the process synchronously (wait until it is terminated
+        *            before returning)
+        * 
+        * @throws IOException
+        *             in case of I/O error
         */
-       public static ReaderType getDefaultReaderType() {
-               return defaultType;
+       protected void start(File target, String program, boolean sync)
+                       throws IOException {
+
+               Process proc = null;
+               if (program == null) {
+                       boolean ok = false;
+                       for (String starter : new String[] { "xdg-open", "open", "see",
+                                       "start", "run" }) {
+                               try {
+                                       Instance.getInstance().getTraceHandler().trace("starting external program");
+                                       proc = Runtime.getRuntime().exec(new String[] { starter, target.getAbsolutePath() });
+                                       ok = true;
+                                       break;
+                               } catch (IOException e) {
+                               }
+                       }
+                       if (!ok) {
+                               throw new IOException("Cannot find a program to start the file");
+                       }
+               } else {
+                       Instance.getInstance().getTraceHandler().trace("starting external program");
+                       proc = Runtime.getRuntime().exec(
+                                       new String[] { program, target.getAbsolutePath() });
+               }
+
+               if (proc != null && sync) {
+                       try {
+                               proc.waitFor();
+                       } catch (InterruptedException e) {
+                       }
+               }
        }
 
-       /**
-        * 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;
+       static private String formatDate(String date) {
+               long ms = 0;
+
+               if (date != null && !date.isEmpty()) {
+                       try {
+                               ms = StringUtils.toTime(date);
+                       } catch (ParseException e) {
+                       }
+
+                       if (ms <= 0) {
+                               SimpleDateFormat sdf = new SimpleDateFormat(
+                                               "yyyy-MM-dd'T'HH:mm:ssSSS");
+                               try {
+                                       ms = sdf.parse(date).getTime();
+                               } catch (ParseException e) {
+                               }
+                       }
+
+                       if (ms > 0) {
+                               SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
+                               return sdf.format(new Date(ms));
+                       }
+               }
+
+               if (date == null) {
+                       date = "";
+               }
+
+               // :(
+               return date;
        }
 }