summaryrefslogblamecommitdiffstats
path: root/dozentenmodul/src/main/java/org/openslx/dozmod/util/DesktopEnvironment.java
blob: 81125f624103e2db9f3d825295f7a7767e45e7ac (plain) (tree)
1
2
3
4
5
6
7
8
9
                                
 
                        
                    
                           
                    
                           
                        
 
                               

                                   
 
                                 
 


                                         
                                                                                        
 
                                                                                                          
                                 
                                                     
                                                                                      
                                                                                                                                                     

                                                                                      
 
                                     
 
                                          












                                                                              

                                                    
 
                                                       
                                                                                    
                             
                                                    


                                                                                  
                         
                 
                                                    

         






                                                                        
                                                            

         








                                                                                   








                                                                                                                             
                                            
                         

                                                                       
                 
                                                                               
         
 









                                                                                    
                                         
                                                                        
                                                  

                                                                                      
                                                                                                               





                                                                                    
                                                                                                              



                                     
 
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://www.bwlehrpool.de/doku.php/allgemein/virtualisierer"),
		//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;
	}
}