summaryrefslogblamecommitdiffstats
path: root/dozentenmodul/src/main/java/org/openslx/dozmod/util/OpenLinks.java
blob: 2a181a3472039e885d4bdd58e9fb64336d7573d7 (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.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("https://www.bwlehrpool.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("https://bwlp-masterserver.ruf.uni-freiburg.de/dozmod/"),
		REGISTER_BWIDM("https://bwlp-masterserver.ruf.uni-freiburg.de/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 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;
	}

}