Updated copyright message.
[meviz.git] / src / main / java / eu / svjatoslav / meviz / encoder / EncodingTask.java
index 9db5d92..ae7e8a0 100755 (executable)
@@ -1,7 +1,7 @@
 /*
  * Meviz - Various tools collection to work with multimedia.
- * Copyright (C) 2012, Svjatoslav Agejenko, svjatoslav@svjatoslav.eu
- * 
+ * Copyright (C) 2012 -- 2018, Svjatoslav Agejenko, svjatoslav@svjatoslav.eu
+ *
  * This program is free software; you can redistribute it and/or
  * modify it under the terms of version 2 of the GNU General Public License
  * as published by the Free Software Foundation.
@@ -9,9 +9,9 @@
 
 package eu.svjatoslav.meviz.encoder;
 
-import java.io.File;
+import eu.svjatoslav.meviz.encoder.converters.AbstractConverter;
 
-import eu.svjatoslav.meviz.encoder.converters.Converter;
+import java.io.File;
 
 public class EncodingTask {
 
@@ -25,33 +25,67 @@ public class EncodingTask {
      */
     private final File target;
 
-    private final Converter converter;
-
+    private final AbstractConverter converter;
+    private final String targetFormat;
     private boolean useTerminal;
 
-    public EncodingTask(final File source, final File destination,
-            final eu.svjatoslav.meviz.encoder.converters.Converter converter) {
+    public EncodingTask(final File source, final File target, final AbstractConverter converter,
+                        final String targetFormat) {
 
         this.source = source;
-        target = destination;
+        this.target = target;
         this.converter = converter;
-
+        this.targetFormat = targetFormat;
     }
 
     /**
      * @return the useTerminal
      */
-    public boolean doUseTerminal() {
+    private boolean doUseTerminal() {
         return useTerminal;
     }
 
-    public String getCommand(final EncodingOptions encodingOptions) {
-        return converter.getCommand(source, target, encodingOptions);
+    private String encodeApostrophes(final String input) {
+        final StringBuilder result = new StringBuilder();
+
+        for (final char c : input.toCharArray()) {
+            if (c == '\'') {
+                result.append("'\\''");
+                continue;
+            }
+            result.append(c);
+        }
+
+        return result.toString();
+    }
+
+    public void execute(final EncodingOptions encodingOptions) {
+        try {
+            String command = getCommand(encodingOptions);
+
+            if (doUseTerminal())
+                command = "xterm -e '" + encodeApostrophes(command) + "'";
+
+            System.out.println("Executing command: " + command);
+
+            final Runtime runtime = Runtime.getRuntime();
+            final Process process = runtime.exec(new String[]{"/bin/bash", "-c", command});
+
+            process.waitFor();
+
+        } catch (final Exception e) {
+            System.out.println(e.toString());
+            e.printStackTrace();
+        }
+
+    }
+
+    private String getCommand(final EncodingOptions encodingOptions) {
+        return converter.getCommand(source, target, encodingOptions, targetFormat);
     }
 
     /**
-     * @param useTerminal
-     *            the useTerminal to set
+     * @param useTerminal the useTerminal to set
      */
     public void setUseTerminal(final boolean useTerminal) {
         this.useTerminal = useTerminal;