X-Git-Url: http://git.nikiroo.be/?a=blobdiff_plain;f=src%2Fbe%2Fnikiroo%2Ffanfix%2Freader%2FBasicReader.java;h=71f19a101bc8b5b3216b66bc2fea24bcde24f7ee;hb=350bc060516184774f8116e61696a8c3c45ba85d;hp=a261c04b4db36c2bd9a7c366b24b25ff0b09e2eb;hpb=211f7ddb50f68aa8a999023ef6d63d5756bdace6;p=fanfix.git diff --git a/src/be/nikiroo/fanfix/reader/BasicReader.java b/src/be/nikiroo/fanfix/reader/BasicReader.java index a261c04..71f19a1 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; @@ -72,7 +71,7 @@ public abstract class BasicReader implements Reader { } @Override - public MetaData getMeta() { + public synchronized MetaData getMeta() { return meta; } @@ -92,18 +91,17 @@ public abstract class BasicReader implements Reader { } @Override - public synchronized void setMeta(URL source, Progress pg) - throws IOException { - BasicSupport support = BasicSupport.getSupport(source); + public synchronized void setMeta(URL url, Progress pg) throws IOException { + BasicSupport support = BasicSupport.getSupport(url); if (support == null) { - throw new IOException("URL not supported: " + source.toString()); + throw new IOException("URL not supported: " + url.toString()); } - story = support.process(source, pg); + story = support.process(pg); if (story == null) { throw new IOException( "Cannot retrieve story from external source: " - + source.toString()); + + url.toString()); } meta = story.getMeta(); @@ -133,8 +131,9 @@ public abstract class BasicReader implements Reader { .getTypeName()); } } catch (Exception e) { - Instance.syserr(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; @@ -208,16 +207,20 @@ public abstract class BasicReader implements Reader { * 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) * * @throws IOException * in case of I/O error */ - public static void openExternal(BasicLibrary lib, String luid) + @Override + public void openExternal(BasicLibrary lib, String luid, boolean sync) throws IOException { MetaData meta = lib.getInfo(luid); - File target = lib.getFile(luid); + File target = lib.getFile(luid, null); - openExternal(meta, target); + openExternal(meta, target, sync); } /** @@ -228,11 +231,14 @@ public abstract class BasicReader implements Reader { * 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 */ - protected static void openExternal(MetaData meta, File target) + protected void openExternal(MetaData meta, File target, boolean sync) throws IOException { String program = null; if (meta.isImageDocument()) { @@ -247,17 +253,58 @@ 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, sync); + } + /** + * 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 + * @param sync + * execute the process synchronously (wait until it is terminated + * before returning) + * + * @throws IOException + * in case of I/O error + */ + 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.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 { - Runtime.getRuntime().exec( + Instance.getTraceHandler().trace("starting external program"); + proc = Runtime.getRuntime().exec( new String[] { program, target.getAbsolutePath() }); } + + if (proc != null && sync) { + while (proc.isAlive()) { + try { + Thread.sleep(100); + } catch (InterruptedException e) { + } + } + } } }