From 022c67ea90c014b1bb5e270323b12224901dcfbc Mon Sep 17 00:00:00 2001 From: Kevin Whitaker Date: Wed, 21 Sep 2016 01:14:14 -0400 Subject: [PATCH] Fixed flaw in safe code that made it unsafe. Make sure to check for actual functionality, since each action can have different available states. --- .../org/multibit/hd/ui/utils/SafeDesktop.java | 64 +++++++++++-------- 1 file changed, 37 insertions(+), 27 deletions(-) diff --git a/mbhd-swing/src/main/java/org/multibit/hd/ui/utils/SafeDesktop.java b/mbhd-swing/src/main/java/org/multibit/hd/ui/utils/SafeDesktop.java index a6981e30b..6874d2937 100644 --- a/mbhd-swing/src/main/java/org/multibit/hd/ui/utils/SafeDesktop.java +++ b/mbhd-swing/src/main/java/org/multibit/hd/ui/utils/SafeDesktop.java @@ -40,14 +40,15 @@ public static boolean browse(final URI uri) { try { Desktop desktop = Desktop.getDesktop(); - if (desktop != null) { + if (desktop != null && desktop.isSupported(Desktop.Action.BROWSE)) { Desktop.getDesktop().browse(uri); // Assume success return true; + } else { + // Consider using xdg + return tryAltBrowse(uri); } - // Consider using xdg - } catch (RuntimeException | IOException e) { // Probably a weird setup - just stop now log.warn("Failed to open browser.", e); @@ -55,37 +56,46 @@ public static boolean browse(final URI uri) { } else { - // GNOME libraries are probably not available check OS - if (!OSUtils.isLinux()) { - // This is Windows/OS X so probably a weird setup - just stop now - log.warn("No default browser configured."); - return false; - } + return tryAltBrowse(uri); + } - // Might have a chance with KDE libraries since we're on Linux - log.warn("Attempting to open browser with xdg-open"); - try { - new ProcessBuilder( - "xdg-open", - uri.toString() - ).start(); + // Must have failed to be here + return false; + + } + + /** + *

A method to try and use alternative ways to use the browse action on platforms where java standard code gives problems.

+ * @param uri The URI to pass to the browser + * @return True if the browser opened successfully (i.e. no visible error to the JVM) + */ + private static boolean tryAltBrowse(URI uri) { + // GNOME libraries are probably not available check OS + if (!OSUtils.isLinux()) { + // This is Windows/OS X so probably a weird setup - just stop now + log.warn("No default browser configured."); + return false; + } - // Need to include a call to waitFor() and examine the exit code to - // accurately determine success. This requires wrapping the code in - // a timeout executor which is more complex than it's worth at present + // Might have a chance with KDE libraries since we're on Linux + log.warn("Attempting to open browser with xdg-open"); + try { + new ProcessBuilder( + "xdg-open", + uri.toString() + ).start(); - // Assume success - return true; + // Need to include a call to waitFor() and examine the exit code to + // accurately determine success. This requires wrapping the code in + // a timeout executor which is more complex than it's worth at present - } catch (IOException e) { - log.warn("Failed to open external browser through 'xdg-open {}'", uri.toString(), e); - } + // Assume success + return true; + } catch (IOException e) { + log.warn("Failed to open external browser through 'xdg-open {}'", uri.toString(), e); } - - // Must have failed to be here return false; - } } \ No newline at end of file