Advertisement
Guest User

Untitled

a guest
Oct 24th, 2016
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.50 KB | None | 0 0
  1. package llepsxov.application;
  2.  
  3. import java.awt.Desktop;
  4. import java.io.File;
  5. import java.io.IOException;
  6. import java.net.URI;
  7. import java.util.ArrayList;
  8. import java.util.List;
  9.  
  10. /**
  11. * DesktopApi created by mightypork (http://stackoverflow.com/users/2180189/mightypork)
  12. * used in VoxSpell to manage file usage with the linux operating system
  13. *
  14. * To use this class, run the following command in your Bash terminal
  15. * $ apt-get install libgnome2-0
  16. */
  17. public class DesktopApi {
  18.  
  19. public static boolean browse(URI uri) {
  20.  
  21. if (openSystemSpecific(uri.toString())) return true;
  22.  
  23. if (browseDESKTOP(uri)) return true;
  24.  
  25. return false;
  26. }
  27.  
  28.  
  29. public static boolean open(File file) {
  30.  
  31. if (openSystemSpecific(file.getPath())) return true;
  32.  
  33. if (openDESKTOP(file)) return true;
  34.  
  35. return false;
  36. }
  37.  
  38.  
  39. public static boolean edit(File file) {
  40.  
  41. // you can try something like
  42. // runCommand("gimp", "%s", file.getPath())
  43. // based on user preferences.
  44.  
  45. if (openSystemSpecific(file.getPath())) return true;
  46.  
  47. if (editDESKTOP(file)) return true;
  48.  
  49. return false;
  50. }
  51.  
  52.  
  53. private static boolean openSystemSpecific(String what) {
  54.  
  55. EnumOS os = getOs();
  56.  
  57. if (os.isLinux()) {
  58. if (runCommand("kde-open", "%s", what)) return true;
  59. if (runCommand("gnome-open", "%s", what)) return true;
  60. if (runCommand("xdg-open", "%s", what)) return true;
  61. }
  62.  
  63. if (os.isMac()) {
  64. if (runCommand("open", "%s", what)) return true;
  65. }
  66.  
  67. if (os.isWindows()) {
  68. if (runCommand("explorer", "%s", what)) return true;
  69. }
  70.  
  71. return false;
  72. }
  73.  
  74.  
  75. private static boolean browseDESKTOP(URI uri) {
  76.  
  77. logOut("Trying to use Desktop.getDesktop().browse() with " + uri.toString());
  78. try {
  79. if (!Desktop.isDesktopSupported()) {
  80. logErr("Platform is not supported.");
  81. return false;
  82. }
  83.  
  84. if (!Desktop.getDesktop().isSupported(Desktop.Action.BROWSE)) {
  85. logErr("BROWSE is not supported.");
  86. return false;
  87. }
  88.  
  89. Desktop.getDesktop().browse(uri);
  90.  
  91. return true;
  92. } catch (Throwable t) {
  93. logErr("Error using desktop browse.", t);
  94. return false;
  95. }
  96. }
  97.  
  98.  
  99. private static boolean openDESKTOP(File file) {
  100.  
  101. logOut("Trying to use Desktop.getDesktop().open() with " + file.toString());
  102. try {
  103. if (!Desktop.isDesktopSupported()) {
  104. logErr("Platform is not supported.");
  105. return false;
  106. }
  107.  
  108. if (!Desktop.getDesktop().isSupported(Desktop.Action.OPEN)) {
  109. logErr("OPEN is not supported.");
  110. return false;
  111. }
  112.  
  113. Desktop.getDesktop().open(file);
  114.  
  115. return true;
  116. } catch (Throwable t) {
  117. logErr("Error using desktop open.", t);
  118. return false;
  119. }
  120. }
  121.  
  122.  
  123. private static boolean editDESKTOP(File file) {
  124.  
  125. logOut("Trying to use Desktop.getDesktop().edit() with " + file);
  126. try {
  127. if (!Desktop.isDesktopSupported()) {
  128. logErr("Platform is not supported.");
  129. return false;
  130. }
  131.  
  132. if (!Desktop.getDesktop().isSupported(Desktop.Action.EDIT)) {
  133. logErr("EDIT is not supported.");
  134. return false;
  135. }
  136.  
  137. Desktop.getDesktop().edit(file);
  138.  
  139. return true;
  140. } catch (Throwable t) {
  141. logErr("Error using desktop edit.", t);
  142. return false;
  143. }
  144. }
  145.  
  146.  
  147. private static boolean runCommand(String command, String args, String file) {
  148.  
  149. logOut("Trying to exec:\n cmd = " + command + "\n args = " + args + "\n %s = " + file);
  150.  
  151. String[] parts = prepareCommand(command, args, file);
  152.  
  153. try {
  154. Process p = Runtime.getRuntime().exec(parts);
  155. if (p == null) return false;
  156.  
  157. try {
  158. int retval = p.exitValue();
  159. if (retval == 0) {
  160. logErr("Process ended immediately.");
  161. return false;
  162. } else {
  163. logErr("Process crashed.");
  164. return false;
  165. }
  166. } catch (IllegalThreadStateException itse) {
  167. logErr("Process is running.");
  168. return true;
  169. }
  170. } catch (IOException e) {
  171. logErr("Error running command.", e);
  172. return false;
  173. }
  174. }
  175.  
  176.  
  177. private static String[] prepareCommand(String command, String args, String file) {
  178.  
  179. List<String> parts = new ArrayList<String>();
  180. parts.add(command);
  181.  
  182. if (args != null) {
  183. for (String s : args.split(" ")) {
  184. s = String.format(s, file); // put in the filename thing
  185.  
  186. parts.add(s.trim());
  187. }
  188. }
  189.  
  190. return parts.toArray(new String[parts.size()]);
  191. }
  192.  
  193. private static void logErr(String msg, Throwable t) {
  194. System.err.println(msg);
  195. t.printStackTrace();
  196. }
  197.  
  198. private static void logErr(String msg) {
  199. System.err.println(msg);
  200. }
  201.  
  202. private static void logOut(String msg) {
  203. System.out.println(msg);
  204. }
  205.  
  206. public static enum EnumOS {
  207. linux, macos, solaris, unknown, windows;
  208.  
  209. public boolean isLinux() {
  210.  
  211. return this == linux || this == solaris;
  212. }
  213.  
  214.  
  215. public boolean isMac() {
  216.  
  217. return this == macos;
  218. }
  219.  
  220.  
  221. public boolean isWindows() {
  222.  
  223. return this == windows;
  224. }
  225. }
  226.  
  227.  
  228. public static EnumOS getOs() {
  229.  
  230. String s = System.getProperty("os.name").toLowerCase();
  231.  
  232. if (s.contains("win")) {
  233. return EnumOS.windows;
  234. }
  235.  
  236. if (s.contains("mac")) {
  237. return EnumOS.macos;
  238. }
  239.  
  240. if (s.contains("solaris")) {
  241. return EnumOS.solaris;
  242. }
  243.  
  244. if (s.contains("sunos")) {
  245. return EnumOS.solaris;
  246. }
  247.  
  248. if (s.contains("linux")) {
  249. return EnumOS.linux;
  250. }
  251.  
  252. if (s.contains("unix")) {
  253. return EnumOS.linux;
  254. } else {
  255. return EnumOS.unknown;
  256. }
  257. }
  258. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement