PageRenderTime 58ms CodeModel.GetById 32ms RepoModel.GetById 0ms app.codeStats 0ms

/backport12/backport12-util/src/com/intellij/backport12/openapi/util/SystemInfo.java

https://github.com/code-orchestra/code-orchestra-core
Java | 282 lines | 180 code | 41 blank | 61 comment | 23 complexity | 5e912035a0cde1764081b3173d521167 MD5 | raw file
Possible License(s): Apache-2.0
  1. /*
  2. * Copyright 2000-2013 JetBrains s.r.o.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. package com.intellij.backport12.openapi.util;
  17. import com.intellij.openapi.util.AtomicNotNullLazyValue;
  18. import com.intellij.openapi.util.NotNullLazyValue;
  19. import com.intellij.openapi.util.text.StringUtil;
  20. import com.intellij.backport12.util.SystemProperties;
  21. import org.jetbrains.annotations.NotNull;
  22. import java.io.File;
  23. import java.util.List;
  24. @SuppressWarnings({"HardCodedStringLiteral", "UtilityClassWithoutPrivateConstructor", "UnusedDeclaration"})
  25. public class SystemInfo extends SystemInfoRt {
  26. public static final String OS_NAME = SystemInfoRt.OS_NAME;
  27. public static final String OS_VERSION = SystemInfoRt.OS_VERSION;
  28. public static final String OS_ARCH = System.getProperty("os.arch");
  29. public static final String JAVA_VERSION = System.getProperty("java.version");
  30. public static final String JAVA_RUNTIME_VERSION = System.getProperty("java.runtime.version");
  31. public static final String ARCH_DATA_MODEL = System.getProperty("sun.arch.data.model");
  32. public static final String SUN_DESKTOP = System.getProperty("sun.desktop", "");
  33. public static final boolean isWindows = SystemInfoRt.isWindows;
  34. public static final boolean isMac = SystemInfoRt.isMac;
  35. public static final boolean isOS2 = SystemInfoRt.isOS2;
  36. public static final boolean isLinux = SystemInfoRt.isLinux;
  37. public static final boolean isFreeBSD = SystemInfoRt._OS_NAME.startsWith("freebsd");
  38. public static final boolean isSolaris = SystemInfoRt._OS_NAME.startsWith("sunos");
  39. public static final boolean isUnix = SystemInfoRt.isUnix;
  40. public static boolean isOsVersionAtLeast(@NotNull String version) {
  41. return StringUtil.compareVersionNumbers(OS_VERSION, version) >= 0;
  42. }
  43. // version numbers from http://msdn.microsoft.com/en-us/library/windows/desktop/ms724832.aspx
  44. public static final boolean isWin2kOrNewer = isWindows && isOsVersionAtLeast("5.0");
  45. public static final boolean isWinVistaOrNewer = isWindows && isOsVersionAtLeast("6.0");
  46. public static final boolean isWin7OrNewer = isWindows && isOsVersionAtLeast("6.1");
  47. /** @deprecated unsupported (to remove in IDEA 13) */
  48. public static final boolean isWindows9x = SystemInfoRt._OS_NAME.startsWith("windows 9") || SystemInfoRt._OS_NAME.startsWith("windows me");
  49. /** @deprecated unsupported (to remove in IDEA 13) */
  50. public static final boolean isWindowsNT = SystemInfoRt._OS_NAME.startsWith("windows nt");
  51. /** @deprecated use {@linkplain #OS_VERSION} (to remove in IDEA 13) */
  52. public static final boolean isWindows2000 = SystemInfoRt._OS_NAME.startsWith("windows 2000");
  53. /** @deprecated use {@linkplain #OS_VERSION} (to remove in IDEA 13) */
  54. public static final boolean isWindows2003 = SystemInfoRt._OS_NAME.startsWith("windows 2003");
  55. /** @deprecated use {@linkplain #OS_VERSION} (to remove in IDEA 13) */
  56. public static final boolean isWindowsXP = SystemInfoRt._OS_NAME.startsWith("windows xp");
  57. /** @deprecated use {@linkplain #OS_VERSION} (to remove in IDEA 13) */
  58. public static final boolean isWindowsVista = SystemInfoRt._OS_NAME.startsWith("windows vista");
  59. /** @deprecated use {@linkplain #OS_VERSION} (to remove in IDEA 13) */
  60. public static final boolean isWindows7 = SystemInfoRt._OS_NAME.startsWith("windows 7");
  61. /** @deprecated inaccurate (to remove in IDEA 13) */
  62. public static final boolean isKDE = SUN_DESKTOP.toLowerCase().contains("kde");
  63. /** @deprecated inaccurate (to remove in IDEA 13) */
  64. public static final boolean isGnome = SUN_DESKTOP.toLowerCase().contains("gnome");
  65. public static final boolean isXWindow = isUnix && !isMac;
  66. public static final boolean isMacSystemMenu = isMac && "true".equals(System.getProperty("apple.laf.useScreenMenuBar"));
  67. public static final boolean isFileSystemCaseSensitive = SystemInfoRt.isFileSystemCaseSensitive;
  68. public static final boolean areSymLinksSupported = isUnix || isWinVistaOrNewer;
  69. public static final boolean is32Bit = ARCH_DATA_MODEL == null || ARCH_DATA_MODEL.equals("32");
  70. public static final boolean is64Bit = !is32Bit;
  71. public static final boolean isAMD64 = "amd64".equals(OS_ARCH);
  72. public static final boolean isMacIntel64 = isMac && "x86_64".equals(OS_ARCH);
  73. /** @deprecated use {@linkplain #hasXdgOpen()} (to remove in IDEA 13) */
  74. public static final boolean hasXdgOpen = isXWindow;
  75. private static final NotNullLazyValue<Boolean> ourHasXdgOpen = new AtomicNotNullLazyValue<Boolean>() {
  76. @NotNull
  77. @Override
  78. protected Boolean compute() {
  79. return isUnix && new File("/usr/bin/xdg-open").canExecute();
  80. }
  81. };
  82. public static boolean hasXdgOpen() {
  83. return ourHasXdgOpen.getValue();
  84. }
  85. private static final NotNullLazyValue<Boolean> ourHasXdgMime = new AtomicNotNullLazyValue<Boolean>() {
  86. @NotNull
  87. @Override
  88. protected Boolean compute() {
  89. return isUnix && new File("/usr/bin/xdg-mime").canExecute();
  90. }
  91. };
  92. public static boolean hasXdgMime() {
  93. return ourHasXdgOpen.getValue();
  94. }
  95. private static final NotNullLazyValue<Boolean> hasNautilus = new AtomicNotNullLazyValue<Boolean>() {
  96. @NotNull
  97. @Override
  98. protected Boolean compute() {
  99. return isUnix && new File("/usr/bin/nautilus").canExecute();
  100. }
  101. };
  102. /** @deprecated implementation details (to remove in IDEA 13) */
  103. public static boolean hasNautilus() {
  104. return hasNautilus.getValue();
  105. }
  106. /** @deprecated implementation details (to remove in IDEA 13) */
  107. public static final String nativeFileManagerName = "File Manager";
  108. private static final NotNullLazyValue<String> ourFileManagerName = new AtomicNotNullLazyValue<String>() {
  109. @NotNull
  110. @Override
  111. protected String compute() {
  112. return isMac ? "Finder" :
  113. isWindows ? "Explorer" :
  114. "File Manager";
  115. }
  116. };
  117. /** @deprecated implementation details (to remove in IDEA 13) */
  118. public static String getFileManagerName() {
  119. return ourFileManagerName.getValue();
  120. }
  121. /**
  122. * Whether IDEA is running under MacOS X version 10.4 or later.
  123. *
  124. * @since 5.0.2
  125. */
  126. public static final boolean isMacOSTiger = isTiger();
  127. /**
  128. * Whether IDEA is running under MacOS X on an Intel Machine
  129. *
  130. * @since 5.0.2
  131. */
  132. public static final boolean isIntelMac = isIntelMac();
  133. /**
  134. * Running under MacOS X version 10.5 or later;
  135. *
  136. * @since 7.0.2
  137. */
  138. public static final boolean isMacOSLeopard = isLeopard();
  139. /**
  140. * Running under MacOS X version 10.6 or later;
  141. *
  142. * @since 9.0
  143. */
  144. public static final boolean isMacOSSnowLeopard = isSnowLeopard();
  145. /**
  146. * Running under MacOS X version 10.7 or later;
  147. *
  148. * @since 11.0
  149. */
  150. public static final boolean isMacOSLion = isLion();
  151. /**
  152. * Running under MacOS X version 10.8 or later;
  153. *
  154. * @since 11.1
  155. */
  156. public static final boolean isMacOSMountainLion = isMountainLion();
  157. /** @deprecated use {@linkplain #isXWindow} (to remove in IDEA 13) */
  158. public static boolean X11PasteEnabledSystem = isXWindow;
  159. private static boolean isIntelMac() {
  160. return isMac && "i386".equals(OS_ARCH);
  161. }
  162. private static boolean isTiger() {
  163. return isMac &&
  164. !OS_VERSION.startsWith("10.0") &&
  165. !OS_VERSION.startsWith("10.1") &&
  166. !OS_VERSION.startsWith("10.2") &&
  167. !OS_VERSION.startsWith("10.3");
  168. }
  169. private static boolean isLeopard() {
  170. return isMac && isTiger() && !OS_VERSION.startsWith("10.4");
  171. }
  172. private static boolean isSnowLeopard() {
  173. return isMac && isLeopard() && !OS_VERSION.startsWith("10.5");
  174. }
  175. private static boolean isLion() {
  176. return isMac && isSnowLeopard() && !OS_VERSION.startsWith("10.6");
  177. }
  178. private static boolean isMountainLion() {
  179. return isMac && isLion() && !OS_VERSION.startsWith("10.7");
  180. }
  181. @NotNull
  182. public static String getMacOSMajorVersion() {
  183. return getMacOSMajorVersion(OS_VERSION);
  184. }
  185. public static String getMacOSMajorVersion(String version) {
  186. int[] parts = getMacOSVersionParts(version);
  187. return String.format("%d.%d", parts[0], parts[1]);
  188. }
  189. @NotNull
  190. public static String getMacOSVersionCode() {
  191. return getMacOSVersionCode(OS_VERSION);
  192. }
  193. @NotNull
  194. public static String getMacOSMajorVersionCode() {
  195. return getMacOSMajorVersionCode(OS_VERSION);
  196. }
  197. @NotNull
  198. public static String getMacOSMinorVersionCode() {
  199. return getMacOSMinorVersionCode(OS_VERSION);
  200. }
  201. @NotNull
  202. public static String getMacOSVersionCode(@NotNull String version) {
  203. int[] parts = getMacOSVersionParts(version);
  204. return String.format("%02d%d%d", parts[0], normalize(parts[1]), normalize(parts[2]));
  205. }
  206. @NotNull
  207. public static String getMacOSMajorVersionCode(@NotNull String version) {
  208. int[] parts = getMacOSVersionParts(version);
  209. return String.format("%02d%d%d", parts[0], normalize(parts[1]), 0);
  210. }
  211. @NotNull
  212. public static String getMacOSMinorVersionCode(@NotNull String version) {
  213. int[] parts = getMacOSVersionParts(version);
  214. return String.format("%02d%02d", parts[1], parts[2]);
  215. }
  216. private static int[] getMacOSVersionParts(@NotNull String version) {
  217. List<String> parts = StringUtil.split(version, ".");
  218. while (parts.size() < 3) {
  219. parts.add("0");
  220. }
  221. return new int[]{toInt(parts.get(0)), toInt(parts.get(1)), toInt(parts.get(2))};
  222. }
  223. private static int normalize(int number) {
  224. return number > 9 ? 9 : number;
  225. }
  226. private static int toInt(String string) {
  227. try {
  228. return Integer.valueOf(string);
  229. }
  230. catch (NumberFormatException e) {
  231. return 0;
  232. }
  233. }
  234. public static boolean isJavaVersionAtLeast(String v) {
  235. return StringUtil.compareVersionNumbers(JAVA_RUNTIME_VERSION, v) >= 0;
  236. }
  237. /** @deprecated use {@linkplain com.intellij.backport12.util.SystemProperties#getIntProperty(String, int)} (to remove in IDEA 13) */
  238. public static int getIntProperty(@NotNull final String key, final int defaultValue) {
  239. return SystemProperties.getIntProperty(key, defaultValue);
  240. }
  241. }