| 1 |
package com.github.markusbernhardt.proxy.util; |
| 2 |
|
| 3 |
import com.github.markusbernhardt.proxy.util.Logger.LogLevel; |
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
public class PlatformUtil { |
| 13 |
|
| 14 |
public static final String OVERRIDE_HOME_DIR = "com.btr.proxy.user.home"; |
| 15 |
|
| 16 |
public enum Platform { |
| 17 |
WIN, LINUX, MAC_OS, SOLARIS, OTHER |
| 18 |
} |
| 19 |
|
| 20 |
public enum Desktop { |
| 21 |
WIN, KDE, GNOME, MAC_OS, OTHER |
| 22 |
} |
| 23 |
|
| 24 |
public enum Browser { |
| 25 |
IE, FIREFOX |
| 26 |
} |
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
public static Platform getCurrentPlattform() { |
| 35 |
String osName = System.getProperty("os.name"); |
| 36 |
Logger.log(PlatformUtil.class, LogLevel.TRACE, "Detecting platform. Name is: {0}", osName); |
| 37 |
|
| 38 |
if (osName.toLowerCase().contains("windows")) { |
| 39 |
Logger.log(PlatformUtil.class, LogLevel.TRACE, "Detected Windows platform: {0}", osName); |
| 40 |
return Platform.WIN; |
| 41 |
} |
| 42 |
if (osName.toLowerCase().contains("linux")) { |
| 43 |
Logger.log(PlatformUtil.class, LogLevel.TRACE, "Detected Linux platform: {0}", osName); |
| 44 |
return Platform.LINUX; |
| 45 |
} |
| 46 |
if (osName.startsWith("Mac OS")) { |
| 47 |
Logger.log(PlatformUtil.class, LogLevel.TRACE, "Detected Mac OS platform: {0}", osName); |
| 48 |
return Platform.MAC_OS; |
| 49 |
} |
| 50 |
if (osName.startsWith("SunOS")) { |
| 51 |
Logger.log(PlatformUtil.class, LogLevel.TRACE, "Detected Solaris platform: {0}", osName); |
| 52 |
return Platform.SOLARIS; |
| 53 |
} |
| 54 |
|
| 55 |
return Platform.OTHER; |
| 56 |
} |
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
public static Browser getDefaultBrowser() { |
| 65 |
|
| 66 |
if (getCurrentPlattform() == Platform.WIN) { |
| 67 |
Logger.log(PlatformUtil.class, LogLevel.TRACE, "Detected Browser is InternetExplorer"); |
| 68 |
return Browser.IE; |
| 69 |
} else { |
| 70 |
Logger.log(PlatformUtil.class, LogLevel.TRACE, "Detected Browser Firefox. Fallback?"); |
| 71 |
return Browser.FIREFOX; |
| 72 |
} |
| 73 |
} |
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
public static Desktop getCurrentDesktop() { |
| 82 |
Platform platf = getCurrentPlattform(); |
| 83 |
|
| 84 |
if (platf == Platform.WIN) { |
| 85 |
Logger.log(PlatformUtil.class, LogLevel.TRACE, "Detected Windows desktop"); |
| 86 |
return Desktop.WIN; |
| 87 |
} |
| 88 |
if (platf == Platform.MAC_OS) { |
| 89 |
Logger.log(PlatformUtil.class, LogLevel.TRACE, "Detected Mac OS desktop"); |
| 90 |
return Desktop.MAC_OS; |
| 91 |
} |
| 92 |
|
| 93 |
if (platf == Platform.LINUX || platf == Platform.SOLARIS || platf == Platform.OTHER) { |
| 94 |
|
| 95 |
if (isKDE()) { |
| 96 |
Logger.log(PlatformUtil.class, LogLevel.TRACE, "Detected KDE desktop"); |
| 97 |
return Desktop.KDE; |
| 98 |
} |
| 99 |
if (isGnome()) { |
| 100 |
Logger.log(PlatformUtil.class, LogLevel.TRACE, "Detected Gnome desktop"); |
| 101 |
return Desktop.GNOME; |
| 102 |
} |
| 103 |
} |
| 104 |
Logger.log(PlatformUtil.class, LogLevel.TRACE, "Detected Unknown desktop"); |
| 105 |
return Desktop.OTHER; |
| 106 |
} |
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
private static boolean isGnome() { |
| 115 |
return System.getenv("GNOME_DESKTOP_SESSION_ID") != null; |
| 116 |
} |
| 117 |
|
| 118 |
|
| 119 |
|
| 120 |
|
| 121 |
|
| 122 |
|
| 123 |
|
| 124 |
private static boolean isKDE() { |
| 125 |
return System.getenv("KDE_SESSION_VERSION") != null; |
| 126 |
} |
| 127 |
|
| 128 |
|
| 129 |
|
| 130 |
|
| 131 |
|
| 132 |
|
| 133 |
|
| 134 |
public static String getUserHomeDir() { |
| 135 |
String result = System.getProperty("user.home"); |
| 136 |
String override = System.getProperty(OVERRIDE_HOME_DIR); |
| 137 |
if (override != null && override.trim().length() > 0) { |
| 138 |
result = override; |
| 139 |
} |
| 140 |
return result; |
| 141 |
} |
| 142 |
|
| 143 |
} |