summaryrefslogtreecommitdiffstats
path: root/dozentenmodul/src/main/java/org/openslx/dozmod/util/DesktopEnvironment.java
blob: f08ef14b6e09c4399c975772c8a8ab965359fdf8 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
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;
import java.util.Arrays;

import org.apache.log4j.Logger;
import org.openslx.dozmod.App;
import org.openslx.dozmod.Branding;

public class DesktopEnvironment {

	/**
	 * Logger instance for this class
	 */
	private final static Logger LOGGER = Logger.getLogger(DesktopEnvironment.class);

	private static final Desktop desktop = Desktop.isDesktopSupported() ? Desktop.getDesktop() : null;
	public static enum Link {
		FAQ(Branding.getServiceFAQWebsite()),
		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("https://" + App.getMasterServerAddress() + "/dozmod/"),
		REGISTER_BWIDM("https://" + App.getMasterServerAddress() + "/webif/");

		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 fallbackExec(uri.toString());
	}

	public static boolean openLocal(File path) {
		try {
			desktop.open(path);
			return true;
		} catch (Exception e) {
			LOGGER.error("Got exception in openLocal: ", e);
		}
		return fallbackExec(path.getAbsolutePath());
	}

	/**
	 * 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) {
		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);
		}
		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: " + Arrays.toString(execCommand) + " -- Trace: ", e);
			return false;
		}
		return exitCode == 0;
	}
}