From: Niki Roo Date: Sun, 3 Dec 2017 03:11:48 +0000 (+0100) Subject: Remove or move java.awt dependencies X-Git-Tag: fanfix-swing-0.0.1~12^2~377 X-Git-Url: https://git.nikiroo.be/?a=commitdiff_plain;h=16a81ef7656c5c692fb831927e75edde25dd77a0;p=fanfix-swing.git Remove or move java.awt dependencies --- diff --git a/configure.sh b/configure.sh index 520afba9..eb20f7d3 100755 --- a/configure.sh +++ b/configure.sh @@ -4,9 +4,9 @@ PREFIX=/usr/local PROGS="java javac jar make sed" -CLI=be/nikiroo/fanfix/reader/CliReader +CLI=be/nikiroo/fanfix/reader/cli/CliReader TUI= -GUI=be/nikiroo/fanfix/reader/GuiReader +GUI=be/nikiroo/fanfix/reader/ui/GuiReader JCLI= JTUI= JGUI= @@ -35,7 +35,7 @@ while [ "$*" != "" ]; do --tui) #=yes Enable TUI support (Jexer, experimental) [ "$val" = no -o "$val" = false ] && TUI= && JTUI= if [ "$val" = yes -o "$val" = true ]; then - TUI=be/nikiroo/fanfix/reader/TuiReader + TUI=be/nikiroo/fanfix/reader/tui/TuiReader JTUI="-C bin/ jexer" fi ;; diff --git a/libs/nikiroo-utils-3.1.6-sources.jar b/libs/nikiroo-utils-4.0.0-sources.jar similarity index 87% rename from libs/nikiroo-utils-3.1.6-sources.jar rename to libs/nikiroo-utils-4.0.0-sources.jar index 5f2dbae1..72ad2349 100644 Binary files a/libs/nikiroo-utils-3.1.6-sources.jar and b/libs/nikiroo-utils-4.0.0-sources.jar differ diff --git a/src/be/nikiroo/fanfix/DataLoader.java b/src/be/nikiroo/fanfix/DataLoader.java index 395f6831..222c6146 100644 --- a/src/be/nikiroo/fanfix/DataLoader.java +++ b/src/be/nikiroo/fanfix/DataLoader.java @@ -1,18 +1,16 @@ package be.nikiroo.fanfix; -import java.awt.image.BufferedImage; import java.io.File; import java.io.IOException; import java.io.InputStream; import java.net.URL; import java.util.Map; -import javax.imageio.ImageIO; - import be.nikiroo.fanfix.bundles.Config; import be.nikiroo.fanfix.supported.BasicSupport; import be.nikiroo.utils.Cache; import be.nikiroo.utils.Downloader; +import be.nikiroo.utils.Image; import be.nikiroo.utils.ImageUtils; /** @@ -50,7 +48,9 @@ public class DataLoader { public DataLoader(File dir, String UA, int hoursChanging, int hoursStable) throws IOException { cache = new Cache(dir, hoursChanging, hoursStable); + cache.setTraceHandler(Instance.getTraceHandler()); downloader = new Downloader(UA); + downloader.setTraceHandler(Instance.getTraceHandler()); } /** @@ -216,64 +216,49 @@ public class DataLoader { /** * Save the given resource as an image on disk using the default image - * format for content. + * format for content or cover -- will automatically add the extension, too. * - * @param url + * @param img * the resource * @param target - * the target file + * the target file without extension + * @param cover + * use the cover image format instead of the content image format * * @throws IOException * in case of I/O error */ - public void saveAsImage(URL url, File target) throws IOException { - InputStream in = open(url, null, true); - try { - saveAsImage(ImageUtils.fromStream(in), target); - } finally { - in.close(); + public void saveAsImage(Image img, File target, boolean cover) + throws IOException { + String format; + if (cover) { + format = Instance.getConfig().getString(Config.IMAGE_FORMAT_COVER) + .toLowerCase(); + } else { + format = Instance.getConfig() + .getString(Config.IMAGE_FORMAT_CONTENT).toLowerCase(); } + saveAsImage(img, new File(target.toString() + "." + format), format); } /** - * Save the given resource as an image on disk using the default image - * format for content. + * Save the given resource as an image on disk using the given image format + * for content, or with "png" format if it fails. * - * @param image + * @param img * the resource * @param target * the target file + * @param format + * the file format ("png", "jpeg", "bmp"...) * * @throws IOException * in case of I/O error */ - public void saveAsImage(BufferedImage image, File target) + public void saveAsImage(Image img, File target, String format) throws IOException { - try { - String format = Instance.getConfig() - .getString(Config.IMAGE_FORMAT_CONTENT).toLowerCase(); - - boolean ok = false; - try { - ok = ImageIO.write(image, format, target); - } catch (IOException e) { - ok = false; - } - - // Some formats are not reliable - // Second change: PNG - if (!ok && !format.equals("png")) { - ok = ImageIO.write(image, "png", target); - } + ImageUtils.getInstance().saveAsImage(img, target, format); - if (!ok) { - throw new IOException( - "Cannot find a writer for this image and format: " - + format); - } - } catch (IOException e) { - throw new IOException("Cannot write image to " + target, e); - } } /** diff --git a/src/be/nikiroo/fanfix/data/MetaData.java b/src/be/nikiroo/fanfix/data/MetaData.java index 8185ddd2..d72b5573 100644 --- a/src/be/nikiroo/fanfix/data/MetaData.java +++ b/src/be/nikiroo/fanfix/data/MetaData.java @@ -1,9 +1,10 @@ package be.nikiroo.fanfix.data; -import java.awt.image.BufferedImage; import java.util.ArrayList; import java.util.List; +import be.nikiroo.utils.Image; + /** * The meta data associated to a {@link Story} object. * @@ -15,7 +16,7 @@ public class MetaData implements Cloneable, Comparable { private String date; private Chapter resume; private List tags; - private BufferedImage cover; + private Image cover; private String subject; private String source; private String url; @@ -129,7 +130,7 @@ public class MetaData implements Cloneable, Comparable { * * @return the cover */ - public BufferedImage getCover() { + public Image getCover() { return cover; } @@ -139,7 +140,7 @@ public class MetaData implements Cloneable, Comparable { * @param cover * the cover to set */ - public void setCover(BufferedImage cover) { + public void setCover(Image cover) { this.cover = cover; } diff --git a/src/be/nikiroo/fanfix/data/Paragraph.java b/src/be/nikiroo/fanfix/data/Paragraph.java index 7a1a1c44..91e95f8b 100644 --- a/src/be/nikiroo/fanfix/data/Paragraph.java +++ b/src/be/nikiroo/fanfix/data/Paragraph.java @@ -1,6 +1,6 @@ package be.nikiroo.fanfix.data; -import java.awt.image.BufferedImage; +import be.nikiroo.utils.Image; /** * A paragraph in a chapter of the story. @@ -28,7 +28,7 @@ public class Paragraph implements Cloneable { private ParagraphType type; private String content; - private BufferedImage contentImage; + private Image contentImage; private long words; /** @@ -45,7 +45,7 @@ public class Paragraph implements Cloneable { * @param contentImage * the image */ - public Paragraph(BufferedImage contentImage) { + public Paragraph(Image contentImage) { this(ParagraphType.IMAGE, null, 1); this.contentImage = contentImage; } @@ -109,7 +109,7 @@ public class Paragraph implements Cloneable { * * @return the content */ - public BufferedImage getContentImage() { + public Image getContentImage() { return contentImage; } diff --git a/src/be/nikiroo/fanfix/data/Story.java b/src/be/nikiroo/fanfix/data/Story.java index 7e585712..fd4f2a8d 100644 --- a/src/be/nikiroo/fanfix/data/Story.java +++ b/src/be/nikiroo/fanfix/data/Story.java @@ -92,9 +92,22 @@ public class Story implements Iterable, Cloneable { resume += "\n"; } - String cover = (meta == null || meta.getCover() == null) ? "none" - : meta.getCover().getWidth() + "x" - + meta.getCover().getHeight(); + String cover = "none"; + if (meta != null && meta.getCover() != null) { + cover = " bytes"; + + int size = meta.getCover().getData().length; + if (size > 1000) { + size /= 1000; + cover = " kb"; + if (size > 1000) { + size /= 1000; + cover = " mb"; + } + } + + cover = size + cover; + } return String.format( "Title: [%s]\nAuthor: [%s]\nDate: [%s]\nTags: [%s]\n" + "Resume: [%s]\nCover: [%s]", title, meta == null ? "" diff --git a/src/be/nikiroo/fanfix/library/BasicLibrary.java b/src/be/nikiroo/fanfix/library/BasicLibrary.java index 9e7d4644..8c86fc90 100644 --- a/src/be/nikiroo/fanfix/library/BasicLibrary.java +++ b/src/be/nikiroo/fanfix/library/BasicLibrary.java @@ -1,6 +1,5 @@ package be.nikiroo.fanfix.library; -import java.awt.image.BufferedImage; import java.io.File; import java.io.IOException; import java.net.URL; @@ -16,6 +15,7 @@ import be.nikiroo.fanfix.output.BasicOutput; import be.nikiroo.fanfix.output.BasicOutput.OutputType; import be.nikiroo.fanfix.supported.BasicSupport; import be.nikiroo.fanfix.supported.BasicSupport.SupportType; +import be.nikiroo.utils.Image; import be.nikiroo.utils.Progress; /** @@ -89,7 +89,7 @@ abstract public class BasicLibrary { * * @return the cover image */ - public abstract BufferedImage getCover(String luid); + public abstract Image getCover(String luid); /** * Return the cover image associated to this source. @@ -101,7 +101,7 @@ abstract public class BasicLibrary { * * @return the cover image or NULL */ - public BufferedImage getSourceCover(String source) { + public Image getSourceCover(String source) { List metas = getListBySource(source); if (metas.size() > 0) { return getCover(metas.get(0).getLuid()); diff --git a/src/be/nikiroo/fanfix/library/CacheLibrary.java b/src/be/nikiroo/fanfix/library/CacheLibrary.java index 7f4bb370..26204156 100644 --- a/src/be/nikiroo/fanfix/library/CacheLibrary.java +++ b/src/be/nikiroo/fanfix/library/CacheLibrary.java @@ -1,6 +1,5 @@ package be.nikiroo.fanfix.library; -import java.awt.image.BufferedImage; import java.io.File; import java.io.IOException; import java.net.URL; @@ -10,6 +9,7 @@ import be.nikiroo.fanfix.Instance; import be.nikiroo.fanfix.bundles.UiConfig; import be.nikiroo.fanfix.data.MetaData; import be.nikiroo.fanfix.data.Story; +import be.nikiroo.utils.Image; import be.nikiroo.utils.Progress; /** @@ -101,7 +101,7 @@ public class CacheLibrary extends BasicLibrary { } @Override - public BufferedImage getCover(final String luid) { + public Image getCover(final String luid) { if (isCached(luid)) { return cacheLib.getCover(luid); } @@ -111,7 +111,7 @@ public class CacheLibrary extends BasicLibrary { } @Override - public BufferedImage getSourceCover(String source) { + public Image getSourceCover(String source) { // no cache for the source cover return lib.getSourceCover(source); } diff --git a/src/be/nikiroo/fanfix/library/LocalLibrary.java b/src/be/nikiroo/fanfix/library/LocalLibrary.java index 92676b35..041db101 100644 --- a/src/be/nikiroo/fanfix/library/LocalLibrary.java +++ b/src/be/nikiroo/fanfix/library/LocalLibrary.java @@ -1,6 +1,5 @@ package be.nikiroo.fanfix.library; -import java.awt.image.BufferedImage; import java.io.File; import java.io.FileFilter; import java.io.FileInputStream; @@ -11,8 +10,6 @@ import java.util.HashMap; import java.util.List; import java.util.Map; -import javax.imageio.ImageIO; - import be.nikiroo.fanfix.Instance; import be.nikiroo.fanfix.bundles.Config; import be.nikiroo.fanfix.data.MetaData; @@ -22,7 +19,7 @@ import be.nikiroo.fanfix.output.BasicOutput.OutputType; import be.nikiroo.fanfix.output.InfoCover; import be.nikiroo.fanfix.supported.InfoReader; import be.nikiroo.utils.IOUtils; -import be.nikiroo.utils.ImageUtils; +import be.nikiroo.utils.Image; import be.nikiroo.utils.MarkableFileInputStream; import be.nikiroo.utils.Progress; @@ -34,7 +31,7 @@ import be.nikiroo.utils.Progress; public class LocalLibrary extends BasicLibrary { private int lastId; private Map stories; // Files: [ infoFile, TargetFile ] - private Map sourceCovers; + private Map sourceCovers; private File baseDir; private OutputType text; @@ -90,7 +87,7 @@ public class LocalLibrary extends BasicLibrary { this.lastId = 0; this.stories = null; - this.sourceCovers = new HashMap(); + this.sourceCovers = new HashMap(); baseDir.mkdirs(); } @@ -111,7 +108,7 @@ public class LocalLibrary extends BasicLibrary { } @Override - public BufferedImage getCover(String luid) { + public Image getCover(String luid) { MetaData meta = getInfo(luid); if (meta != null) { File[] files = getStories(null).get(meta); @@ -133,7 +130,7 @@ public class LocalLibrary extends BasicLibrary { @Override protected void invalidateInfo(String luid) { stories = null; - sourceCovers = new HashMap(); + sourceCovers = new HashMap(); } @Override @@ -197,7 +194,7 @@ public class LocalLibrary extends BasicLibrary { } @Override - public BufferedImage getSourceCover(String source) { + public Image getSourceCover(String source) { if (!sourceCovers.containsKey(source)) { sourceCovers.put(source, super.getSourceCover(source)); } @@ -210,7 +207,8 @@ public class LocalLibrary extends BasicLibrary { sourceCovers.put(source, getCover(luid)); File cover = new File(getExpectedDir(source), ".cover.png"); try { - ImageIO.write(sourceCovers.get(source), "png", cover); + Instance.getCache().saveAsImage(sourceCovers.get(source), cover, + true); } catch (IOException e) { Instance.getTraceHandler().error(e); sourceCovers.remove(source); @@ -487,10 +485,9 @@ public class LocalLibrary extends BasicLibrary { File cover = new File(dir, ".cover.png"); if (cover.exists()) { try { - InputStream in = new MarkableFileInputStream( - new FileInputStream(cover)); + InputStream in = new FileInputStream(cover); try { - sourceCovers.put(source, ImageUtils.fromStream(in)); + sourceCovers.put(source, new Image(in)); } finally { in.close(); } @@ -516,11 +513,12 @@ public class LocalLibrary extends BasicLibrary { * @param coverImage * the cover image */ - void setSourceCover(String source, BufferedImage coverImage) { + void setSourceCover(String source, Image coverImage) { sourceCovers.put(source, coverImage); File cover = new File(getExpectedDir(source), ".cover.png"); try { - ImageIO.write(sourceCovers.get(source), "png", cover); + Instance.getCache().saveAsImage(sourceCovers.get(source), cover, + true); } catch (IOException e) { Instance.getTraceHandler().error(e); sourceCovers.remove(source); diff --git a/src/be/nikiroo/fanfix/library/RemoteLibrary.java b/src/be/nikiroo/fanfix/library/RemoteLibrary.java index 326a488b..5052fa27 100644 --- a/src/be/nikiroo/fanfix/library/RemoteLibrary.java +++ b/src/be/nikiroo/fanfix/library/RemoteLibrary.java @@ -1,6 +1,5 @@ package be.nikiroo.fanfix.library; -import java.awt.image.BufferedImage; import java.io.File; import java.io.IOException; import java.net.URL; @@ -11,6 +10,7 @@ import java.util.List; import be.nikiroo.fanfix.Instance; import be.nikiroo.fanfix.data.MetaData; import be.nikiroo.fanfix.data.Story; +import be.nikiroo.utils.Image; import be.nikiroo.utils.Progress; import be.nikiroo.utils.StringUtils; import be.nikiroo.utils.Version; @@ -95,15 +95,15 @@ public class RemoteLibrary extends BasicLibrary { } @Override - public BufferedImage getCover(final String luid) { - final BufferedImage[] result = new BufferedImage[1]; + public Image getCover(final String luid) { + final Image[] result = new Image[1]; try { new ConnectActionClientObject(host, port, true) { @Override public void action(Version serverVersion) throws Exception { Object rep = send(new Object[] { md5, "GET_COVER", luid }); - result[0] = (BufferedImage) rep; + result[0] = (Image) rep; } @Override @@ -119,8 +119,8 @@ public class RemoteLibrary extends BasicLibrary { } @Override - public BufferedImage getSourceCover(final String source) { - final BufferedImage[] result = new BufferedImage[1]; + public Image getSourceCover(final String source) { + final Image[] result = new Image[1]; try { new ConnectActionClientObject(host, port, true) { @@ -128,7 +128,7 @@ public class RemoteLibrary extends BasicLibrary { public void action(Version serverVersion) throws Exception { Object rep = send(new Object[] { md5, "GET_SOURCE_COVER", source }); - result[0] = (BufferedImage) rep; + result[0] = (Image) rep; } @Override diff --git a/src/be/nikiroo/fanfix/output/BasicOutput.java b/src/be/nikiroo/fanfix/output/BasicOutput.java index 97920cbe..5109cce6 100644 --- a/src/be/nikiroo/fanfix/output/BasicOutput.java +++ b/src/be/nikiroo/fanfix/output/BasicOutput.java @@ -376,13 +376,13 @@ public abstract class BasicOutput { int num = 0; String paragraphNumber = String.format("%04d", num++); - imageName = chapterNameNum + "_" + paragraphNumber + ".png"; + imageName = chapterNameNum + "_" + paragraphNumber; writeChapterHeader(chap); int i = 1; for (Paragraph para : chap) { paragraphNumber = String.format("%04d", num++); - imageName = chapterNameNum + "_" + paragraphNumber + ".png"; + imageName = chapterNameNum + "_" + paragraphNumber; writeParagraph(para); if (chapPg != null) { chapPg.setProgress(i++); diff --git a/src/be/nikiroo/fanfix/output/Epub.java b/src/be/nikiroo/fanfix/output/Epub.java index 48da65a2..09a9944d 100644 --- a/src/be/nikiroo/fanfix/output/Epub.java +++ b/src/be/nikiroo/fanfix/output/Epub.java @@ -7,8 +7,6 @@ import java.io.IOException; import java.io.InputStream; import java.io.OutputStreamWriter; -import javax.imageio.ImageIO; - import be.nikiroo.fanfix.Instance; import be.nikiroo.fanfix.bundles.Config; import be.nikiroo.fanfix.bundles.StringId; @@ -26,6 +24,7 @@ class Epub extends BasicOutput { private boolean inDialogue = false; private boolean inNormal = false; private File images; + private boolean nextParaIsCover = true; @Override public File process(Story story, File targetDir, String targetName) @@ -104,7 +103,8 @@ class Epub extends BasicOutput { String format = Instance.getConfig() .getString(Config.IMAGE_FORMAT_COVER).toLowerCase(); File file = new File(images, "cover." + format); - ImageIO.write(story.getMeta().getCover(), format, file); + Instance.getCache().saveAsImage(story.getMeta().getCover(), file, + true); } // OPS/* except chapters @@ -209,11 +209,14 @@ class Epub extends BasicOutput { break; case IMAGE: File file = new File(images, getCurrentImageBestName(false)); - Instance.getCache().saveAsImage(para.getContentImage(), file); + Instance.getCache().saveAsImage(para.getContentImage(), file, + nextParaIsCover); writer.write(" "); break; } + + nextParaIsCover = false; } @Override diff --git a/src/be/nikiroo/fanfix/output/Html.java b/src/be/nikiroo/fanfix/output/Html.java index d1559b40..e91c6f46 100644 --- a/src/be/nikiroo/fanfix/output/Html.java +++ b/src/be/nikiroo/fanfix/output/Html.java @@ -7,8 +7,6 @@ import java.io.IOException; import java.io.InputStream; import java.io.OutputStreamWriter; -import javax.imageio.ImageIO; - import be.nikiroo.fanfix.Instance; import be.nikiroo.fanfix.bundles.Config; import be.nikiroo.fanfix.data.Chapter; @@ -51,11 +49,9 @@ class Html extends BasicOutput { writer = null; } - String format = Instance.getConfig() - .getString(Config.IMAGE_FORMAT_COVER).toLowerCase(); if (story.getMeta().getCover() != null) { - ImageIO.write(story.getMeta().getCover(), format, new File(dir, - "cover." + format)); + Instance.getCache().saveAsImage(story.getMeta().getCover(), + new File(dir, "cover"), true); } return target; diff --git a/src/be/nikiroo/fanfix/output/InfoCover.java b/src/be/nikiroo/fanfix/output/InfoCover.java index a1b37913..1e6ccd33 100644 --- a/src/be/nikiroo/fanfix/output/InfoCover.java +++ b/src/be/nikiroo/fanfix/output/InfoCover.java @@ -6,8 +6,6 @@ import java.io.FileOutputStream; import java.io.IOException; import java.io.OutputStreamWriter; -import javax.imageio.ImageIO; - import be.nikiroo.fanfix.Instance; import be.nikiroo.fanfix.bundles.Config; import be.nikiroo.fanfix.data.MetaData; @@ -69,10 +67,8 @@ public class InfoCover { MetaData meta) { if (meta != null && meta.getCover() != null) { try { - String format = Instance.getConfig() - .getString(Config.IMAGE_FORMAT_COVER).toLowerCase(); - ImageIO.write(meta.getCover(), format, new File(targetDir, - targetName + "." + format)); + Instance.getCache().saveAsImage(meta.getCover(), + new File(targetDir, targetName), true); } catch (IOException e) { // Allow to continue without cover Instance.getTraceHandler().error( diff --git a/src/be/nikiroo/fanfix/output/Text.java b/src/be/nikiroo/fanfix/output/Text.java index ee72385b..b45a5128 100644 --- a/src/be/nikiroo/fanfix/output/Text.java +++ b/src/be/nikiroo/fanfix/output/Text.java @@ -17,6 +17,7 @@ import be.nikiroo.fanfix.data.Story; class Text extends BasicOutput { protected BufferedWriter writer; protected File targetDir; + private boolean nextParaIsCover = true; @Override public File process(Story story, File targetDir, String targetName) @@ -104,8 +105,11 @@ class Text extends BasicOutput { protected void writeParagraphHeader(Paragraph para) throws IOException { if (para.getType() == ParagraphType.IMAGE) { File file = new File(targetDir, getCurrentImageBestName(true)); - Instance.getCache().saveAsImage(para.getContentImage(), file); + Instance.getCache().saveAsImage(para.getContentImage(), file, + nextParaIsCover); } + + nextParaIsCover = false; } @Override diff --git a/src/be/nikiroo/fanfix/reader/BasicReader.java b/src/be/nikiroo/fanfix/reader/BasicReader.java index 0ea403c4..4b444940 100644 --- a/src/be/nikiroo/fanfix/reader/BasicReader.java +++ b/src/be/nikiroo/fanfix/reader/BasicReader.java @@ -1,6 +1,5 @@ package be.nikiroo.fanfix.reader; -import java.awt.Desktop; import java.io.File; import java.io.IOException; import java.net.MalformedURLException; @@ -133,8 +132,9 @@ public abstract class BasicReader implements Reader { .getTypeName()); } } catch (Exception e) { - Instance.getTraceHandler().error(new Exception("Cannot create a reader of type: " - + defaultType + " (Not compiled in?)", e)); + Instance.getTraceHandler().error( + new Exception("Cannot create a reader of type: " + + defaultType + " (Not compiled in?)", e)); } return null; @@ -212,8 +212,8 @@ public abstract class BasicReader implements Reader { * @throws IOException * in case of I/O error */ - public static void openExternal(BasicLibrary lib, String luid) - throws IOException { + @Override + public void openExternal(BasicLibrary lib, String luid) throws IOException { MetaData meta = lib.getInfo(luid); File target = lib.getFile(luid, null); @@ -232,8 +232,7 @@ public abstract class BasicReader implements Reader { * @throws IOException * in case of I/O error */ - protected static void openExternal(MetaData meta, File target) - throws IOException { + protected void openExternal(MetaData meta, File target) throws IOException { String program = null; if (meta.isImageDocument()) { program = Instance.getUiConfig().getString( @@ -247,13 +246,35 @@ public abstract class BasicReader implements Reader { program = null; } - if (program == null) { - try { - Desktop.getDesktop().browse(target.toURI()); - } catch (UnsupportedOperationException e) { - Runtime.getRuntime().exec( - new String[] { "xdg-open", target.getAbsolutePath() }); + start(target, program); + } + /** + * Start a file and open it with the given program if given or the first + * default system starter we can find. + * + * @param target + * the target to open + * @param program + * the program to use or NULL for the default system starter + * + * @throws IOException + * in case of I/O error + */ + protected void start(File target, String program) throws IOException { + if (program == null) { + boolean ok = false; + for (String starter : new String[] { "xdg-open", "start", "run" }) { + try { + 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 { Runtime.getRuntime().exec( diff --git a/src/be/nikiroo/fanfix/reader/Reader.java b/src/be/nikiroo/fanfix/reader/Reader.java index d540d39f..f98f12c5 100644 --- a/src/be/nikiroo/fanfix/reader/Reader.java +++ b/src/be/nikiroo/fanfix/reader/Reader.java @@ -40,11 +40,11 @@ public interface Reader { String pkg = "be.nikiroo.fanfix.reader."; switch (this) { case CLI: - return pkg + "CliReader"; + return pkg + "cli.CliReader"; case TUI: - return pkg + "TuiReader"; + return pkg + "tui.TuiReader"; case GUI: - return pkg + "GuiReader"; + return pkg + "ui.GuiReader"; } return null; @@ -158,4 +158,18 @@ public interface Reader { * all */ public void browse(String source); + + /** + * 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 + * + * @throws IOException + * in case of I/O error + */ + public void openExternal(BasicLibrary lib, String luid) throws IOException; } diff --git a/src/be/nikiroo/fanfix/reader/CliReader.java b/src/be/nikiroo/fanfix/reader/cli/CliReader.java similarity index 96% rename from src/be/nikiroo/fanfix/reader/CliReader.java rename to src/be/nikiroo/fanfix/reader/cli/CliReader.java index 2f0b8cee..b28d4d4e 100644 --- a/src/be/nikiroo/fanfix/reader/CliReader.java +++ b/src/be/nikiroo/fanfix/reader/cli/CliReader.java @@ -1,4 +1,4 @@ -package be.nikiroo.fanfix.reader; +package be.nikiroo.fanfix.reader.cli; import java.io.IOException; import java.util.List; @@ -9,6 +9,7 @@ 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.BasicReader; /** * Command line {@link Story} reader. diff --git a/src/be/nikiroo/fanfix/reader/TuiReader.java b/src/be/nikiroo/fanfix/reader/tui/TuiReader.java similarity index 93% rename from src/be/nikiroo/fanfix/reader/TuiReader.java rename to src/be/nikiroo/fanfix/reader/tui/TuiReader.java index 30b9dce5..7192854e 100644 --- a/src/be/nikiroo/fanfix/reader/TuiReader.java +++ b/src/be/nikiroo/fanfix/reader/tui/TuiReader.java @@ -1,10 +1,12 @@ -package be.nikiroo.fanfix.reader; +package be.nikiroo.fanfix.reader.tui; import java.io.IOException; import jexer.TApplication; import jexer.TApplication.BackendType; import be.nikiroo.fanfix.Instance; +import be.nikiroo.fanfix.reader.BasicReader; +import be.nikiroo.fanfix.reader.Reader; /** * This {@link Reader}is based upon the TUI widget library 'jexer' diff --git a/src/be/nikiroo/fanfix/reader/TuiReaderApplication.java b/src/be/nikiroo/fanfix/reader/tui/TuiReaderApplication.java similarity index 94% rename from src/be/nikiroo/fanfix/reader/TuiReaderApplication.java rename to src/be/nikiroo/fanfix/reader/tui/TuiReaderApplication.java index c1bba5a8..9e991e4c 100644 --- a/src/be/nikiroo/fanfix/reader/TuiReaderApplication.java +++ b/src/be/nikiroo/fanfix/reader/tui/TuiReaderApplication.java @@ -1,4 +1,4 @@ -package be.nikiroo.fanfix.reader; +package be.nikiroo.fanfix.reader.tui; import java.awt.Toolkit; import java.awt.datatransfer.DataFlavor; @@ -17,6 +17,8 @@ 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.utils.Progress; /** @@ -77,7 +79,7 @@ class TuiReaderApplication extends TApplication implements Reader { getChapter()); } else { try { - BasicReader.openExternal(getLibrary(), meta.getLuid()); + openExternal(getLibrary(), meta.getLuid()); } catch (IOException e) { messageBox("Error when trying to open the story", e.getMessage(), TMessageBox.Type.OK); @@ -225,4 +227,9 @@ class TuiReaderApplication extends TApplication implements Reader { return false; } } + + @Override + public void openExternal(BasicLibrary lib, String luid) throws IOException { + reader.openExternal(lib, luid); + } } diff --git a/src/be/nikiroo/fanfix/reader/TuiReaderMainWindow.java b/src/be/nikiroo/fanfix/reader/tui/TuiReaderMainWindow.java similarity index 98% rename from src/be/nikiroo/fanfix/reader/TuiReaderMainWindow.java rename to src/be/nikiroo/fanfix/reader/tui/TuiReaderMainWindow.java index 10c6466c..0fccfafa 100644 --- a/src/be/nikiroo/fanfix/reader/TuiReaderMainWindow.java +++ b/src/be/nikiroo/fanfix/reader/tui/TuiReaderMainWindow.java @@ -1,4 +1,4 @@ -package be.nikiroo.fanfix.reader; +package be.nikiroo.fanfix.reader.tui; import java.io.IOException; import java.util.ArrayList; @@ -15,6 +15,7 @@ import be.nikiroo.fanfix.Instance; import be.nikiroo.fanfix.data.MetaData; import be.nikiroo.fanfix.library.BasicLibrary; import be.nikiroo.fanfix.output.BasicOutput.OutputType; +import be.nikiroo.fanfix.reader.Reader; /** * The library window, that will list all the (filtered) stories available in diff --git a/src/be/nikiroo/fanfix/reader/TuiReaderStoryWindow.java b/src/be/nikiroo/fanfix/reader/tui/TuiReaderStoryWindow.java similarity index 99% rename from src/be/nikiroo/fanfix/reader/TuiReaderStoryWindow.java rename to src/be/nikiroo/fanfix/reader/tui/TuiReaderStoryWindow.java index e8dafc3f..e4587e75 100644 --- a/src/be/nikiroo/fanfix/reader/TuiReaderStoryWindow.java +++ b/src/be/nikiroo/fanfix/reader/tui/TuiReaderStoryWindow.java @@ -1,4 +1,4 @@ -package be.nikiroo.fanfix.reader; +package be.nikiroo.fanfix.reader.tui; import java.util.ArrayList; import java.util.List; diff --git a/src/be/nikiroo/fanfix/reader/GuiReader.java b/src/be/nikiroo/fanfix/reader/ui/GuiReader.java similarity index 92% rename from src/be/nikiroo/fanfix/reader/GuiReader.java rename to src/be/nikiroo/fanfix/reader/ui/GuiReader.java index b9d59548..576ac9cc 100644 --- a/src/be/nikiroo/fanfix/reader/GuiReader.java +++ b/src/be/nikiroo/fanfix/reader/ui/GuiReader.java @@ -1,4 +1,4 @@ -package be.nikiroo.fanfix.reader; +package be.nikiroo.fanfix.reader.ui; import java.awt.Desktop; import java.awt.EventQueue; @@ -18,6 +18,7 @@ import be.nikiroo.fanfix.data.MetaData; import be.nikiroo.fanfix.data.Story; import be.nikiroo.fanfix.library.BasicLibrary; import be.nikiroo.fanfix.library.CacheLibrary; +import be.nikiroo.fanfix.reader.BasicReader; import be.nikiroo.utils.Progress; import be.nikiroo.utils.Version; import be.nikiroo.utils.ui.UIUtils; @@ -147,6 +148,19 @@ class GuiReader extends BasicReader { }); } + @Override + public void start(File target, String program) throws IOException { + if (program == null) { + try { + Desktop.getDesktop().browse(target.toURI()); + } catch (UnsupportedOperationException e) { + super.start(target, program); + } + } else { + super.start(target, program); + } + } + // delete from local reader library void clearLocalReaderCache(String luid) { try { diff --git a/src/be/nikiroo/fanfix/reader/GuiReaderBook.java b/src/be/nikiroo/fanfix/reader/ui/GuiReaderBook.java similarity index 96% rename from src/be/nikiroo/fanfix/reader/GuiReaderBook.java rename to src/be/nikiroo/fanfix/reader/ui/GuiReaderBook.java index c70f30a8..4343ca52 100644 --- a/src/be/nikiroo/fanfix/reader/GuiReaderBook.java +++ b/src/be/nikiroo/fanfix/reader/ui/GuiReaderBook.java @@ -1,4 +1,4 @@ -package be.nikiroo.fanfix.reader; +package be.nikiroo.fanfix.reader.ui; import java.awt.BorderLayout; import java.awt.Color; @@ -27,7 +27,9 @@ import javax.swing.JPanel; import be.nikiroo.fanfix.Instance; import be.nikiroo.fanfix.data.MetaData; import be.nikiroo.fanfix.data.Story; -import be.nikiroo.utils.ImageUtils; +import be.nikiroo.fanfix.reader.Reader; +import be.nikiroo.utils.Image; +import be.nikiroo.utils.ui.ImageUtilsAwt; import be.nikiroo.utils.ui.UIUtils; /** @@ -380,7 +382,7 @@ class GuiReaderBook extends JPanel { InputStream in = Instance.getCache().getFromCache(id); if (in != null) { try { - resizedImage = ImageUtils.fromStream(in); + resizedImage = ImageUtilsAwt.fromImage(new Image(in)); in.close(); in = null; } catch (IOException e) { @@ -390,7 +392,7 @@ class GuiReaderBook extends JPanel { if (resizedImage == null) { try { - BufferedImage cover = null; + Image cover = null; if (meta.getLuid() == null) { cover = reader.getLibrary() .getSourceCover(meta.getSource()); @@ -398,6 +400,8 @@ class GuiReaderBook extends JPanel { cover = reader.getLibrary().getCover(meta.getLuid()); } + BufferedImage coverb = ImageUtilsAwt.fromImage(cover); + resizedImage = new BufferedImage(SPINE_WIDTH + COVER_WIDTH, SPINE_HEIGHT + COVER_HEIGHT + HOFFSET, BufferedImage.TYPE_4BYTE_ABGR); @@ -405,7 +409,7 @@ class GuiReaderBook extends JPanel { g.setColor(Color.white); g.fillRect(0, HOFFSET, COVER_WIDTH, COVER_HEIGHT); if (cover != null) { - g.drawImage(cover, 0, HOFFSET, COVER_WIDTH, COVER_HEIGHT, + g.drawImage(coverb, 0, HOFFSET, COVER_WIDTH, COVER_HEIGHT, null); } else { g.setColor(Color.black); @@ -430,7 +434,11 @@ class GuiReaderBook extends JPanel { } } - return new ImageIcon(resizedImage); + if (resizedImage != null) { + return new ImageIcon(resizedImage); + } + + return null; } /** diff --git a/src/be/nikiroo/fanfix/reader/GuiReaderFrame.java b/src/be/nikiroo/fanfix/reader/ui/GuiReaderFrame.java similarity index 98% rename from src/be/nikiroo/fanfix/reader/GuiReaderFrame.java rename to src/be/nikiroo/fanfix/reader/ui/GuiReaderFrame.java index 99091d37..b98bed22 100644 --- a/src/be/nikiroo/fanfix/reader/GuiReaderFrame.java +++ b/src/be/nikiroo/fanfix/reader/ui/GuiReaderFrame.java @@ -1,4 +1,4 @@ -package be.nikiroo.fanfix.reader; +package be.nikiroo.fanfix.reader.ui; import java.awt.BorderLayout; import java.awt.Color; @@ -45,7 +45,8 @@ import be.nikiroo.fanfix.library.BasicLibrary; import be.nikiroo.fanfix.library.BasicLibrary.Status; import be.nikiroo.fanfix.library.LocalLibrary; import be.nikiroo.fanfix.output.BasicOutput.OutputType; -import be.nikiroo.fanfix.reader.GuiReaderBook.BookActionListener; +import be.nikiroo.fanfix.reader.BasicReader; +import be.nikiroo.fanfix.reader.ui.GuiReaderBook.BookActionListener; import be.nikiroo.utils.Progress; import be.nikiroo.utils.Version; import be.nikiroo.utils.ui.ConfigEditor; @@ -107,8 +108,10 @@ class GuiReaderFrame extends JFrame { pane = new JPanel(); pane.setLayout(new BoxLayout(pane, BoxLayout.PAGE_AXIS)); - color = Instance.getUiConfig().getColor(UiConfig.BACKGROUND_COLOR); - if (color != null) { + Integer icolor = Instance.getUiConfig().getColor( + UiConfig.BACKGROUND_COLOR); + if (icolor != null) { + color = new Color(icolor); setBackground(color); pane.setBackground(color); } diff --git a/src/be/nikiroo/fanfix/reader/GuiReaderGroup.java b/src/be/nikiroo/fanfix/reader/ui/GuiReaderGroup.java similarity index 97% rename from src/be/nikiroo/fanfix/reader/GuiReaderGroup.java rename to src/be/nikiroo/fanfix/reader/ui/GuiReaderGroup.java index a087c4c1..c96b02df 100644 --- a/src/be/nikiroo/fanfix/reader/GuiReaderGroup.java +++ b/src/be/nikiroo/fanfix/reader/ui/GuiReaderGroup.java @@ -1,4 +1,4 @@ -package be.nikiroo.fanfix.reader; +package be.nikiroo.fanfix.reader.ui; import java.awt.BorderLayout; import java.awt.Color; @@ -12,7 +12,7 @@ import javax.swing.JLabel; import javax.swing.JPanel; import be.nikiroo.fanfix.data.MetaData; -import be.nikiroo.fanfix.reader.GuiReaderBook.BookActionListener; +import be.nikiroo.fanfix.reader.ui.GuiReaderBook.BookActionListener; import be.nikiroo.utils.ui.WrapLayout; /** diff --git a/src/be/nikiroo/fanfix/supported/BasicSupport.java b/src/be/nikiroo/fanfix/supported/BasicSupport.java index 66ba64d6..f16cdc7b 100644 --- a/src/be/nikiroo/fanfix/supported/BasicSupport.java +++ b/src/be/nikiroo/fanfix/supported/BasicSupport.java @@ -1,6 +1,5 @@ package be.nikiroo.fanfix.supported; -import java.awt.image.BufferedImage; import java.io.BufferedReader; import java.io.ByteArrayInputStream; import java.io.File; @@ -25,7 +24,7 @@ import be.nikiroo.fanfix.data.MetaData; import be.nikiroo.fanfix.data.Paragraph; import be.nikiroo.fanfix.data.Paragraph.ParagraphType; import be.nikiroo.fanfix.data.Story; -import be.nikiroo.utils.ImageUtils; +import be.nikiroo.utils.Image; import be.nikiroo.utils.Progress; import be.nikiroo.utils.StringUtils; @@ -750,7 +749,7 @@ public abstract class BasicSupport { * @return the {@link Paragraph} */ private Paragraph makeParagraph(URL source, String line) { - BufferedImage image = null; + Image image = null; if (line.startsWith("[") && line.endsWith("]")) { image = getImage(this, source, line.substring(1, line.length() - 1) .trim()); @@ -817,7 +816,7 @@ public abstract class BasicSupport { * * @return the cover if any, or NULL */ - static BufferedImage getDefaultCover(String subject) { + static Image getDefaultCover(String subject) { if (subject != null && !subject.isEmpty() && Instance.getCoverDir() != null) { try { @@ -860,13 +859,18 @@ public abstract class BasicSupport { * @return the image if found, or NULL * */ - static BufferedImage getImage(BasicSupport support, URL source, String line) { + static Image getImage(BasicSupport support, URL source, String line) { URL url = getImageUrl(support, source, line); if (url != null) { + if ("file".equals(url.getProtocol())) { + if (new File(url.getPath()).isDirectory()) { + return null; + } + } InputStream in = null; try { in = Instance.getCache().open(url, getSupport(url), true); - return ImageUtils.fromStream(in); + return new Image(in); } catch (IOException e) { } finally { if (in != null) { @@ -918,11 +922,14 @@ public abstract class BasicSupport { } for (String ext : getImageExt(true)) { - if (absPath != null && new File(absPath + ext).exists()) { - url = new File(absPath + ext).toURI().toURL(); - } else if (relPath != null - && new File(relPath + ext).exists()) { - url = new File(relPath + ext).toURI().toURL(); + File absFile = new File(absPath + ext); + File relFile = new File(relPath + ext); + if (absPath != null && absFile.exists() + && absFile.isFile()) { + url = absFile.toURI().toURL(); + } else if (relPath != null && relFile.exists() + && relFile.isFile()) { + url = relFile.toURI().toURL(); } } } catch (Exception e) { diff --git a/src/be/nikiroo/fanfix/supported/Cbz.java b/src/be/nikiroo/fanfix/supported/Cbz.java index 77a40080..b6524aca 100644 --- a/src/be/nikiroo/fanfix/supported/Cbz.java +++ b/src/be/nikiroo/fanfix/supported/Cbz.java @@ -1,6 +1,5 @@ package be.nikiroo.fanfix.supported; -import java.awt.image.BufferedImage; import java.io.IOException; import java.io.InputStream; import java.net.URL; @@ -16,7 +15,7 @@ import be.nikiroo.fanfix.Instance; import be.nikiroo.fanfix.data.Chapter; import be.nikiroo.fanfix.data.Paragraph; import be.nikiroo.fanfix.data.Story; -import be.nikiroo.utils.ImageUtils; +import be.nikiroo.utils.Image; import be.nikiroo.utils.Progress; /** @@ -76,7 +75,7 @@ class Cbz extends Epub { ZipInputStream zipIn = new ZipInputStream(getInput()); - Map images = new HashMap(); + Map images = new HashMap(); for (ZipEntry entry = zipIn.getNextEntry(); entry != null; entry = zipIn .getNextEntry()) { if (!entry.isDirectory() @@ -92,7 +91,7 @@ class Cbz extends Epub { if (imageEntry) { String uuid = meta.getUuid() + "_" + entry.getName(); try { - images.put(uuid, ImageUtils.fromStream(zipIn)); + images.put(uuid, new Image(zipIn)); } catch (Exception e) { Instance.getTraceHandler().error(e); } diff --git a/src/be/nikiroo/fanfix/supported/E621.java b/src/be/nikiroo/fanfix/supported/E621.java index 1baa6201..72c68b1a 100644 --- a/src/be/nikiroo/fanfix/supported/E621.java +++ b/src/be/nikiroo/fanfix/supported/E621.java @@ -1,6 +1,5 @@ package be.nikiroo.fanfix.supported; -import java.awt.image.BufferedImage; import java.io.IOException; import java.io.InputStream; import java.net.URL; @@ -13,6 +12,7 @@ import be.nikiroo.fanfix.Instance; import be.nikiroo.fanfix.data.Chapter; import be.nikiroo.fanfix.data.MetaData; import be.nikiroo.fanfix.data.Story; +import be.nikiroo.utils.Image; import be.nikiroo.utils.Progress; import be.nikiroo.utils.StringUtils; @@ -87,7 +87,7 @@ class E621 extends BasicSupport { return true; } - private BufferedImage getCover(URL source) throws IOException { + private Image getCover(URL source) throws IOException { InputStream in = Instance.getCache().open(source, this, true); String images = getChapterContent(new URL(source.toString() + "?page=" + 1), in, 1, null); diff --git a/src/be/nikiroo/fanfix/supported/EHentai.java b/src/be/nikiroo/fanfix/supported/EHentai.java index a25a9949..c082b914 100644 --- a/src/be/nikiroo/fanfix/supported/EHentai.java +++ b/src/be/nikiroo/fanfix/supported/EHentai.java @@ -1,6 +1,5 @@ package be.nikiroo.fanfix.supported; -import java.awt.image.BufferedImage; import java.io.IOException; import java.io.InputStream; import java.net.MalformedURLException; @@ -14,6 +13,7 @@ import be.nikiroo.fanfix.Instance; import be.nikiroo.fanfix.data.Chapter; import be.nikiroo.fanfix.data.MetaData; import be.nikiroo.fanfix.data.Story; +import be.nikiroo.utils.Image; import be.nikiroo.utils.Progress; import be.nikiroo.utils.StringUtils; @@ -78,8 +78,8 @@ class EHentai extends BasicSupport { return true; } - private BufferedImage getCover(URL source, InputStream in) { - BufferedImage author = null; + private Image getCover(URL source, InputStream in) { + Image author = null; String coverLine = getKeyLine(in, "
> chaps = getChapters(source, in, null); if (!chaps.isEmpty()) {