blob: e09d8d099fd805e52755a27dbb6f916eaddffdf8 (
plain) (
tree)
|
|
package org.openslx.dozmod.util;
import java.awt.Desktop;
import java.io.File;
import java.net.URI;
import java.net.URLEncoder;
import org.apache.log4j.Logger;
public class OpenLinks {
/**
* Logger instance for this class
*/
private final static Logger LOGGER = Logger.getLogger(OpenLinks.class);
private static final Desktop desktop = Desktop.isDesktopSupported() ? Desktop.getDesktop() : null;
public static enum Link {
FAQ("http://bwlehrpool.hs-offenburg.de"),
VMWARE("https://my.vmware.com/web/vmware/free#desktop_end_user_computing/vmware_workstation_player/12_0"),
INTRO("http://www.hs-offenburg.de/fileadmin/Einrichtungen/hrz/Projekte/bwLehrpool/3_bwLehrpool_-_Image_einbinden_und_starten.pdf"),
DOZMOD("http://bwlehrpool.hs-offenburg.de/#jfmulticontent_c25490-4");
public final URI uri;
private Link(String uri) {
this.uri = URI.create(uri);
}
}
/**
* Open the URI corresponding to the given enum constant in the user's
* default web browser.
*
* @param location web site to open, passed as a member from the
* {@link Link} enum.
* @return success on true
*/
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 {
desktop.browse(uri);
return true;
} catch (Exception e) {
LOGGER.error("Got exception in openWebpage: ", e);
}
}
return false;
}
public static boolean openLocal(File path) {
try {
desktop.open(path);
return true;
} 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;
}
/**
* Send an email to the given email address, with an optional subject. This
* will open the user's default mail application.
*
* @param address receiver's address
* @param subject default subject to use, <code>null</code> for no subject
* @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));
}
return true;
} catch (Exception e) {
LOGGER.error("Got exception in sendMail: ", e);
}
}
return false;
}
}
|