summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJonathan Bauer2018-04-26 18:19:48 +0200
committerJonathan Bauer2018-04-26 18:19:48 +0200
commit1bfa51971154f6f851c33746aac3d9a37d00b5cf (patch)
treea18abe2381323e5e69c22cf82f5ae7fec0db4687
parent[client] Better warning for streamOptimized Images (diff)
downloadtutor-module-1bfa51971154f6f851c33746aac3d9a37d00b5cf.tar.gz
tutor-module-1bfa51971154f6f851c33746aac3d9a37d00b5cf.tar.xz
tutor-module-1bfa51971154f6f851c33746aac3d9a37d00b5cf.zip
fix broken links on some desktop
Introduce OSHelper class that should be used when trying to detect the type of OS we are on. This is now used when deciding the path to save the config to and when trying to execute the fallback command if opening the link via java's desktop API fails.
-rwxr-xr-xdozentenmodul/src/main/java/org/openslx/dozmod/Config.java5
-rw-r--r--dozentenmodul/src/main/java/org/openslx/dozmod/util/OSHelper.java13
-rw-r--r--dozentenmodul/src/main/java/org/openslx/dozmod/util/OpenLinks.java67
3 files changed, 59 insertions, 26 deletions
diff --git a/dozentenmodul/src/main/java/org/openslx/dozmod/Config.java b/dozentenmodul/src/main/java/org/openslx/dozmod/Config.java
index ba21544d..7a3db999 100755
--- a/dozentenmodul/src/main/java/org/openslx/dozmod/Config.java
+++ b/dozentenmodul/src/main/java/org/openslx/dozmod/Config.java
@@ -10,6 +10,7 @@ import java.util.Properties;
import org.apache.log4j.Logger;
import org.openslx.dozmod.gui.window.DisclaimerWindow;
+import org.openslx.dozmod.util.OSHelper;
import org.openslx.util.QuickTimer;
import org.openslx.util.QuickTimer.Task;
import org.openslx.util.Util;
@@ -69,7 +70,7 @@ public class Config {
// Determine OS
String osName = System.getProperty("os.name").toLowerCase();
LOGGER.info("Machine's OS: " + osName);
- if (osName.contains("windows")) {
+ if (OSHelper.isWindows()) {
// Windows machine. Use the environment variable 'APPDATA' which
// should point to a path similar to:
// C:\Users\<user>\AppData\Roaming
@@ -81,7 +82,7 @@ public class Config {
LOGGER.warn("APPDATA is empty.");
configPath = System.getProperty("user.home") + "\\AppData\\Roaming";
}
- } else if (osName.contains("linux")) {
+ } else if (OSHelper.isLinux()) {
configPath = System.getProperty("user.home") + "/.config";
}
if (configPath == null || configPath.isEmpty()) {
diff --git a/dozentenmodul/src/main/java/org/openslx/dozmod/util/OSHelper.java b/dozentenmodul/src/main/java/org/openslx/dozmod/util/OSHelper.java
new file mode 100644
index 00000000..a3860dfa
--- /dev/null
+++ b/dozentenmodul/src/main/java/org/openslx/dozmod/util/OSHelper.java
@@ -0,0 +1,13 @@
+package org.openslx.dozmod.util;
+
+public final class OSHelper {
+
+ public final static String osName = System.getProperty("os.name").toLowerCase();
+
+ public final static boolean isWindows() {
+ return osName.contains("windows");
+ }
+ public final static boolean isLinux() {
+ return osName.contains("linux");
+ }
+}
diff --git a/dozentenmodul/src/main/java/org/openslx/dozmod/util/OpenLinks.java b/dozentenmodul/src/main/java/org/openslx/dozmod/util/OpenLinks.java
index 2a181a34..dc34922b 100644
--- a/dozentenmodul/src/main/java/org/openslx/dozmod/util/OpenLinks.java
+++ b/dozentenmodul/src/main/java/org/openslx/dozmod/util/OpenLinks.java
@@ -2,6 +2,7 @@ package org.openslx.dozmod.util;
import java.awt.Desktop;
import java.io.File;
+import java.io.IOException;
import java.net.URI;
import java.net.URLEncoder;
@@ -41,7 +42,7 @@ public class OpenLinks {
public static boolean openWebpage(Link location) {
return openWebpageUri(location.uri);
}
-
+
public static boolean openWebpageUri(URI uri) {
if (desktop != null && desktop.isSupported(Desktop.Action.BROWSE)) {
try {
@@ -51,7 +52,7 @@ public class OpenLinks {
LOGGER.error("Got exception in openWebpage: ", e);
}
}
- return false;
+ return fallbackExec(uri.toString());
}
public static boolean openLocal(File path) {
@@ -61,17 +62,7 @@ public class OpenLinks {
} catch (Exception e) {
LOGGER.error("Got exception in openLocal: ", e);
}
- try {
- int exitCode = Runtime.getRuntime()
- .exec(new String[] { "xdg-open", path.getAbsolutePath() })
- .waitFor();
- if (exitCode == 0)
- return true;
- LOGGER.error("xdg-open returned " + exitCode);
- } catch (Exception e) {
- LOGGER.error("xdg-open fallback failed", e);
- }
- return false;
+ return fallbackExec(path.getAbsolutePath());
}
/**
@@ -83,20 +74,48 @@ public class OpenLinks {
* @return false on not-success
*/
public static boolean sendMail(String address, String subject) {
- if (desktop != null && desktop.isSupported(Desktop.Action.MAIL)) {
- try {
- if (subject != null) {
- desktop.mail(new URI("mailto:" + address + "?subject="
- + URLEncoder.encode(subject, "UTF-8")));
- } else {
- desktop.mail(new URI("mailto:" + address));
- }
+ URI mailToUri = null;
+ try {
+ if (subject != null) {
+ mailToUri = new URI("mailto:" + address + "?subject=" + URLEncoder.encode(subject, "UTF-8"));
+ } else {
+ mailToUri = new URI("mailto:" + address);
+ }
+ if (desktop != null && desktop.isSupported(Desktop.Action.MAIL)) {
+ desktop.mail(mailToUri);
return true;
- } catch (Exception e) {
- LOGGER.error("Got exception in sendMail: ", e);
}
+ } catch (Exception e) {
+ LOGGER.error("Got exception in sendMail: ", e);
}
- return false;
+ return mailToUri != null && fallbackExec(mailToUri.toString());
}
+ /**
+ * Opens the given link using the system's preferred application. This makes
+ * use of OSHelper to create a OS-dependent command to execute.
+ *
+ * @param link to open
+ * @return true if the external command executed successfully, false
+ * otherwise.
+ */
+ public static boolean fallbackExec(String link) {
+ String[] execCommand;
+ if (OSHelper.isLinux()) {
+ execCommand = new String[] { "xdg-open", link };
+ } else if (OSHelper.isWindows()) {
+ execCommand = new String[] { "cmd.exe", "/c", "start", link };
+ } else {
+ LOGGER.error("Could not set command for unknown operating system: " + OSHelper.osName);
+ return false;
+ }
+ int exitCode = -1;
+ try {
+ exitCode = Runtime.getRuntime().exec(execCommand).waitFor();
+ } catch (InterruptedException | IOException e) {
+ LOGGER.debug("Failed to execute: " + execCommand + "\nTrace: ", e);
+ return false;
+ }
+ return exitCode == 0;
+ }
}