Refactored to remove code duplication.
[meviz.git] / src / main / java / eu / svjatoslav / meviz / encoder / EncodingTask.java
1 /*
2  * Meviz - Various tools collection to work with multimedia.
3  * Copyright (C) 2012, Svjatoslav Agejenko, svjatoslav@svjatoslav.eu
4  * 
5  * This program is free software; you can redistribute it and/or
6  * modify it under the terms of version 2 of the GNU General Public License
7  * as published by the Free Software Foundation.
8  */
9
10 package eu.svjatoslav.meviz.encoder;
11
12 import java.io.File;
13
14 import eu.svjatoslav.meviz.encoder.converters.AbstractConverter;
15
16 public class EncodingTask {
17
18         /**
19          * Source file to encode
20          */
21         private final File source;
22
23         /**
24          * Target file.
25          */
26         private final File target;
27
28         private final AbstractConverter converter;
29
30         private boolean useTerminal;
31
32         public EncodingTask(final File source, final File destination,
33                         final eu.svjatoslav.meviz.encoder.converters.AbstractConverter converter) {
34
35                 this.source = source;
36                 target = destination;
37                 this.converter = converter;
38
39         }
40
41         /**
42          * @return the useTerminal
43          */
44         public boolean doUseTerminal() {
45                 return useTerminal;
46         }
47
48         public String encodeApostrophes(final String input) {
49                 final StringBuffer result = new StringBuffer();
50
51                 for (final char c : input.toCharArray()) {
52                         if (c == '\'') {
53                                 result.append("'\\''");
54                                 continue;
55                         }
56                         result.append(c);
57                 }
58
59                 return result.toString();
60         }
61
62         public void execute(final EncodingOptions encodingOptions) {
63                 try {
64                         String command = getCommand(encodingOptions);
65
66                         if (doUseTerminal())
67                                 command = "xterm -e '" + encodeApostrophes(command) + "'";
68
69                         System.out.println("Executing command: " + command);
70
71                         final Runtime run = Runtime.getRuntime();
72                         Process pr;
73                         pr = run.exec(new String[] { "/bin/bash", "-c", command });
74
75                         pr.waitFor();
76
77                 } catch (final Exception e) {
78                         System.out.println(e.toString());
79                         e.printStackTrace();
80                 }
81
82         }
83
84         public String getCommand(final EncodingOptions encodingOptions) {
85                 return converter.getCommand(source, target, encodingOptions);
86         }
87
88         /**
89          * @param useTerminal
90          *            the useTerminal to set
91          */
92         public void setUseTerminal(final boolean useTerminal) {
93                 this.useTerminal = useTerminal;
94         }
95 }