update to follow fanfix
authorNiki Roo <niki@nikiroo.be>
Tue, 5 May 2020 16:48:18 +0000 (18:48 +0200)
committerNiki Roo <niki@nikiroo.be>
Tue, 5 May 2020 16:48:18 +0000 (18:48 +0200)
src/be/nikiroo/fanfix_jexer/Main.java
src/be/nikiroo/fanfix_jexer/reader/TuiReader.java
src/be/nikiroo/fanfix_jexer/reader/TuiReaderApplication.java
src/be/nikiroo/fanfix_jexer/reader/TuiReaderMainWindow.java

index f5c53a32ffb30e08d8d21a182a6ad7b68e2f18d3..d540b4742b754922f82abbdf557049236241e184 100644 (file)
@@ -1,9 +1,6 @@
 package be.nikiroo.fanfix_jexer;
 
-import java.io.IOException;
-
 import be.nikiroo.fanfix.Instance;
-import be.nikiroo.fanfix.reader.Reader.ReaderType;
 import be.nikiroo.fanfix_jexer.reader.TuiReader;
 
 /**
@@ -32,8 +29,6 @@ public class Main {
                }
 
                Instance.init();
-
-               TuiReader.setDefaultReaderType(ReaderType.TUI);
-               new TuiReader().browse(null);
+               new TuiReader().start(null);
        }
 }
index 7eb0f25d1aa1cc89ba594e313737296cf73df6b4..beee855567ab2d7ee0bc9c42bfa442bef4df6c58 100644 (file)
@@ -1,13 +1,11 @@
 package be.nikiroo.fanfix_jexer.reader;
 
-import java.io.IOException;
+import java.io.Reader;
 
-import jexer.TApplication;
-import jexer.TApplication.BackendType;
 import be.nikiroo.fanfix.Instance;
 import be.nikiroo.fanfix.reader.BasicReader;
-import be.nikiroo.fanfix.reader.Reader;
-import be.nikiroo.fanfix.supported.SupportType;
+import jexer.TApplication;
+import jexer.TApplication.BackendType;
 
 /**
  * This {@link Reader}is based upon the TUI widget library 'jexer'
@@ -50,26 +48,7 @@ public class TuiReader extends BasicReader {
                return backendType;
        }
 
-       @Override
-       public void read(boolean sync) throws IOException {
-               // TODO
-               if (!sync) {
-                       // How could you do a not-sync in TUI mode?
-                       throw new java.lang.IllegalStateException(
-                                       "Async mode not implemented yet.");
-               }
-
-               try {
-                       TuiReaderApplication app = new TuiReaderApplication(this,
-                                       guessBackendType());
-                       app.run();
-               } catch (Exception e) {
-                       Instance.getInstance().getTraceHandler().error(e);
-               }
-       }
-
-       @Override
-       public void browse(String source) {
+       public void start(String source) {
                try {
                        TuiReaderApplication app = new TuiReaderApplication(this, source,
                                        guessBackendType());
@@ -78,30 +57,4 @@ public class TuiReader extends BasicReader {
                        Instance.getInstance().getTraceHandler().error(e);
                }
        }
-
-       @Override
-       public void search(boolean sync) throws IOException {
-               // TODO
-               if (sync) {
-                       throw new java.lang.IllegalStateException("Not implemented yet.");
-               }
-       }
-
-       @Override
-       public void search(SupportType searchOn, String keywords, int page,
-                       int item, boolean sync) {
-               // TODO
-               if (sync) {
-                       throw new java.lang.IllegalStateException("Not implemented yet.");
-               }
-       }
-
-       @Override
-       public void searchTag(SupportType searchOn, int page, int item,
-                       boolean sync, Integer... tags) {
-               // TODO
-               if (sync) {
-                       throw new java.lang.IllegalStateException("Not implemented yet.");
-               }
-       }
 }
index f6213096358919518ee54eeb83da3791304c12fe..3f8b4e30d6d9fa2bc1dbb84737017d5042a0abc4 100644 (file)
@@ -3,9 +3,17 @@ package be.nikiroo.fanfix_jexer.reader;
 import java.awt.Toolkit;
 import java.awt.datatransfer.DataFlavor;
 import java.io.IOException;
+import java.io.Reader;
 import java.net.URL;
 import java.net.UnknownHostException;
 
+import be.nikiroo.fanfix.Instance;
+import be.nikiroo.fanfix.data.MetaData;
+import be.nikiroo.fanfix.data.Story;
+import be.nikiroo.fanfix.library.BasicLibrary;
+import be.nikiroo.fanfix.reader.BasicReader;
+import be.nikiroo.fanfix.supported.BasicSupport;
+import be.nikiroo.fanfix_jexer.reader.TuiReaderMainWindow.Mode;
 import jexer.TApplication;
 import jexer.TCommand;
 import jexer.TKeypress;
@@ -18,15 +26,6 @@ import jexer.TWindow;
 import jexer.event.TCommandEvent;
 import jexer.event.TMenuEvent;
 import jexer.menu.TMenu;
-import be.nikiroo.fanfix.Instance;
-import be.nikiroo.fanfix.data.MetaData;
-import be.nikiroo.fanfix.data.Story;
-import be.nikiroo.fanfix.library.BasicLibrary;
-import be.nikiroo.fanfix.reader.BasicReader;
-import be.nikiroo.fanfix.reader.Reader;
-import be.nikiroo.fanfix.supported.SupportType;
-import be.nikiroo.fanfix_jexer.reader.TuiReaderMainWindow.Mode;
-import be.nikiroo.utils.Progress;
 
 /**
  * Manages the TUI general mode and links and manages the {@link TWindow}s.
@@ -36,7 +35,7 @@ import be.nikiroo.utils.Progress;
  * 
  * @author niki
  */
-class TuiReaderApplication extends TApplication implements Reader {
+class TuiReaderApplication extends TApplication {
        public static final int MENU_FILE_OPEN = 1025;
        public static final int MENU_FILE_IMPORT_URL = 1026;
        public static final int MENU_FILE_IMPORT_FILE = 1027;
@@ -52,21 +51,10 @@ class TuiReaderApplication extends TApplication implements Reader {
        public static final TCommand CMD_EXIT = new TCommand(MENU_FILE_EXIT) {
        };
 
-       private Reader reader;
+       private TuiReader reader;
        private TuiReaderMainWindow main;
 
-       // start reading if meta present
-       public TuiReaderApplication(Reader reader, BackendType backend)
-                       throws Exception {
-               super(backend);
-               init(reader);
-
-               if (getMeta() != null) {
-                       read(false);
-               }
-       }
-
-       public TuiReaderApplication(Reader reader, String source,
+       public TuiReaderApplication(TuiReader reader, String source,
                        TApplication.BackendType backend) throws Exception {
                super(backend);
                init(reader);
@@ -75,80 +63,17 @@ class TuiReaderApplication extends TApplication implements Reader {
                main.setMode(Mode.SOURCE, source);
        }
 
-       @Override
-       public void read(boolean sync) throws IOException {
-               read(getStory(null), sync);
-       }
-
-       @Override
-       public MetaData getMeta() {
-               return reader.getMeta();
-       }
-
-       @Override
-       public Story getStory(Progress pg) throws IOException {
-               return reader.getStory(pg);
-       }
-
-       @Override
+       /**
+        * @deprecated use {@link Instance} instead
+        */
+       @Deprecated
        public BasicLibrary getLibrary() {
-               return reader.getLibrary();
-       }
-
-       @Override
-       public void setLibrary(BasicLibrary lib) {
-               reader.setLibrary(lib);
+               return Instance.getInstance().getLibrary();
        }
-
-       @Override
-       public void setMeta(MetaData meta) throws IOException {
-               reader.setMeta(meta);
-       }
-
-       @Override
-       public void setMeta(String luid) throws IOException {
-               reader.setMeta(luid);
-       }
-
-       @Override
-       public void setMeta(URL source, Progress pg) throws IOException {
-               reader.setMeta(source, pg);
-       }
-
-       @Override
-       public void browse(String source) {
-               try {
-                       reader.browse(source);
-               } catch (IOException e) {
-                       Instance.getInstance().getTraceHandler().error(e);
-               }
-       }
-
-       @Override
-       public int getChapter() {
-               return reader.getChapter();
-       }
-
-       @Override
-       public void setChapter(int chapter) {
-               reader.setChapter(chapter);
-       }
-
-       @Override
-       public void search(boolean sync) throws IOException {
-               reader.search(sync);
-       }
-
-       @Override
-       public void search(SupportType searchOn, String keywords, int page,
-                       int item, boolean sync) throws IOException {
-               reader.search(searchOn, keywords, page, item, sync);
-       }
-
-       @Override
-       public void searchTag(SupportType searchOn, int page, int item,
-                       boolean sync, Integer... tags) throws IOException {
-               reader.searchTag(searchOn, page, item, sync, tags);
+       
+       public void read(MetaData meta, int chap) throws IOException {
+               Story story = getLibrary().getStory(meta.getLuid(), null);
+               read(story, chap);
        }
 
        /**
@@ -157,25 +82,22 @@ class TuiReaderApplication extends TApplication implements Reader {
         * 
         * @param story
         *            the {@link Story} to read
-        * @param sync
-        *            execute the process synchronously (wait until it is terminated
-        *            before returning)
         * 
         * @throws IOException
         *             in case of I/O errors
         */
-       public void read(Story story, boolean sync) throws IOException {
+       public void read(Story story, int chap) throws IOException {
                if (story == null) {
                        throw new IOException("No story to read");
                }
 
                // TODO: open in editor + external option
                if (!story.getMeta().isImageDocument()) {
-                       TWindow window = new TuiReaderStoryWindow(this, story, getChapter());
+                       TWindow window = new TuiReaderStoryWindow(this, story, chap);
                        window.maximize();
                } else {
                        try {
-                               openExternal(getLibrary(), story.getMeta().getLuid(), sync);
+                               openExternal(getLibrary(), story.getMeta().getLuid(), false);
                        } catch (IOException e) {
                                messageBox("Error when trying to open the story",
                                                e.getMessage(), TMessageBox.Type.OK);
@@ -215,7 +137,7 @@ class TuiReaderApplication extends TApplication implements Reader {
                }
        }
 
-       private void init(Reader reader) {
+       private void init(TuiReader reader) {
                this.reader = reader;
 
                // TODO: traces/errors?
@@ -272,8 +194,21 @@ class TuiReaderApplication extends TApplication implements Reader {
                        String openfile = null;
                        try {
                                openfile = fileOpenBox(".");
-                               reader.setMeta(BasicReader.getUrl(openfile), null);
-                               read(false);
+                               URL url = BasicReader.getUrl(openfile);
+                               
+                               try {
+                                       BasicSupport support = BasicSupport
+                                                       .getSupport(BasicReader.getUrl(openfile));
+                                       if (support == null) {
+                                               Instance.getInstance().getTraceHandler()
+                                                               .error("URL not supported: " + openfile);
+                                       }
+
+                                       read(support.process(null), -1);
+                               } catch (IOException e) {
+                                       Instance.getInstance().getTraceHandler()
+                                                       .error(new IOException("Failed to read book", e));
+                               }
                        } catch (IOException e) {
                                // TODO: i18n
                                error("Fail to open file"
@@ -299,7 +234,7 @@ class TuiReaderApplication extends TApplication implements Reader {
                                        + story + "\"", Type.OKCANCEL);
                        if (mbox.getResult() == Result.OK) {
                                try {
-                                       reader.getLibrary().delete(luid);
+                                       getLibrary().delete(luid);
                                        if (main != null) {
                                                main.refreshStories();
                                        }
@@ -385,7 +320,7 @@ class TuiReaderApplication extends TApplication implements Reader {
         */
        private boolean imprt(String url) throws IOException {
                try {
-                       reader.getLibrary().imprt(BasicReader.getUrl(url), null);
+                       getLibrary().imprt(BasicReader.getUrl(url), null);
                        main.refreshStories();
                        return true;
                } catch (UnknownHostException e) {
@@ -393,7 +328,6 @@ class TuiReaderApplication extends TApplication implements Reader {
                }
        }
 
-       @Override
        public void openExternal(BasicLibrary lib, String luid, boolean sync)
                        throws IOException {
                reader.openExternal(lib, luid, sync);
index 2429583ed6109330b3e3bcb4ea69a10c19c0f56f..b8edb5a19fded49d21846e0771213c58326208c0 100644 (file)
@@ -312,9 +312,7 @@ class TuiReaderMainWindow extends TWindow {
 
        public void readStory(MetaData meta) {
                try {
-                       reader.setChapter(-1);
-                       reader.setMeta(meta);
-                       reader.read(false);
+                       reader.read(meta, -1);
                } catch (IOException e) {
                        Instance.getInstance().getTraceHandler().error(e);
                }