]> gitweb.fperrin.net Git - GpsPrune.git/blob - tim/prune/ExternalTools.java
Version 8, September 2009
[GpsPrune.git] / tim / prune / ExternalTools.java
1 package tim.prune;
2
3 import java.io.IOException;
4
5
6 /**
7  * Class to manage interfaces to external tools, like exiftool
8  */
9 public abstract class ExternalTools
10 {
11         /** Constant for Exiftool */
12         public static final int TOOL_EXIFTOOL = 0;
13         /** Constant for Gpsbabel */
14         public static final int TOOL_GPSBABEL = 1;
15         /** Constant for Gnuplot */
16         public static final int TOOL_GNUPLOT  = 2;
17         /** Config keys in order that the tools are defined above */
18         private static final String[] CONFIG_KEYS = {Config.KEY_EXIFTOOL_PATH, Config.KEY_GPSBABEL_PATH, Config.KEY_GNUPLOT_PATH};
19         /** Verification flags for the tools in the order defined above */
20         private static final String[] VERIFY_FLAGS = {"-v", "-V", "-V"};
21
22
23         /**
24          * Check if the selected tool is installed
25          * @param inToolNum number of tool, from constants
26          * @return true if selected tool is installed
27          */
28         public static boolean isToolInstalled(int inToolNum)
29         {
30                 switch (inToolNum) {
31                         case TOOL_EXIFTOOL:
32                         case TOOL_GPSBABEL:
33                         case TOOL_GNUPLOT:
34                                 String toolPath = Config.getConfigString(CONFIG_KEYS[inToolNum]);
35                                 if (toolPath != null && toolPath.length() > 0) {
36                                         return check(toolPath + " " + VERIFY_FLAGS[inToolNum]);
37                                 }
38                 }
39                 // Not found
40                 return false;
41         }
42
43         /**
44          * Check if the selected tool is installed using the given path
45          * @param inToolNum number of tool, from constants
46          * @param inPath selected path to use instead of configured one
47          * @return true if selected tool is installed
48          */
49         public static boolean isToolInstalled(int inToolNum, String inPath)
50         {
51                 if (inPath == null || inPath.equals("")) {return false;}
52                 switch (inToolNum) {
53                         case TOOL_EXIFTOOL:
54                         case TOOL_GPSBABEL:
55                         case TOOL_GNUPLOT:
56                                 return check(inPath + " " + VERIFY_FLAGS[inToolNum]);
57                 }
58                 // Not found
59                 return false;
60         }
61
62         /**
63          * Attempt to call the specified command
64          * @return true if found, false otherwise
65          */
66         private static boolean check(String inCommand)
67         {
68                 // System.out.println("Checking tool '" + inCommand + "'");
69                 try
70                 {
71                         Runtime.getRuntime().exec(inCommand);
72                         return true;
73                 }
74                 catch (IOException ioe)
75                 {
76                         // exception thrown, command not found
77                         return false;
78                 }
79         }
80 }