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