PageRenderTime 46ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

/platform/util/src/com/intellij/openapi/util/SystemInfo.java

https://bitbucket.org/nbargnesi/idea
Java | 256 lines | 164 code | 37 blank | 55 comment | 19 complexity | a637a9a1311dfab937463213823b163f MD5 | raw file
Possible License(s): Apache-2.0, BSD-3-Clause, MPL-2.0-no-copyleft-exception, AGPL-1.0
  1. /*
  2. * Copyright 2000-2012 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.openapi.util;
  17. import com.intellij.openapi.util.text.StringUtil;
  18. import com.intellij.util.SystemProperties;
  19. import org.jetbrains.annotations.NotNull;
  20. import java.io.File;
  21. import java.util.List;
  22. @SuppressWarnings({"HardCodedStringLiteral", "UtilityClassWithoutPrivateConstructor", "UnusedDeclaration"})
  23. public class SystemInfo extends SystemInfoRt {
  24. public static final String OS_NAME = SystemInfoRt.OS_NAME;
  25. public static final String OS_VERSION = SystemInfoRt.OS_VERSION;
  26. public static final String OS_ARCH = System.getProperty("os.arch");
  27. public static final String JAVA_VERSION = System.getProperty("java.version");
  28. public static final String JAVA_RUNTIME_VERSION = System.getProperty("java.runtime.version");
  29. public static final String ARCH_DATA_MODEL = System.getProperty("sun.arch.data.model");
  30. public static final String SUN_DESKTOP = System.getProperty("sun.desktop", "");
  31. public static final boolean isWindows = SystemInfoRt.isWindows;
  32. public static final boolean isWindowsNT = _OS_NAME.startsWith("windows nt");
  33. public static final boolean isWindows2000 = _OS_NAME.startsWith("windows 2000");
  34. public static final boolean isWindows2003 = _OS_NAME.startsWith("windows 2003");
  35. public static final boolean isWindowsXP = _OS_NAME.startsWith("windows xp");
  36. public static final boolean isWindowsVista = _OS_NAME.startsWith("windows vista");
  37. public static final boolean isWindows7 = _OS_NAME.startsWith("windows 7");
  38. public static final boolean isWindows9x = _OS_NAME.startsWith("windows 9") || _OS_NAME.startsWith("windows me");
  39. public static final boolean isOS2 = SystemInfoRt.isOS2;
  40. public static final boolean isMac = SystemInfoRt.isMac;
  41. public static final boolean isLinux = SystemInfoRt.isLinux;
  42. public static final boolean isFreeBSD = _OS_NAME.startsWith("freebsd");
  43. public static final boolean isSolaris = _OS_NAME.startsWith("sunos");
  44. public static final boolean isUnix = SystemInfoRt.isUnix;
  45. /** @deprecated inaccurate (to remove in IDEA 13) */
  46. public static final boolean isKDE = SUN_DESKTOP.toLowerCase().contains("kde");
  47. /** @deprecated inaccurate (to remove in IDEA 13) */
  48. public static final boolean isGnome = SUN_DESKTOP.toLowerCase().contains("gnome");
  49. public static final boolean isMacSystemMenu = isMac && "true".equals(System.getProperty("apple.laf.useScreenMenuBar"));
  50. public static final boolean isFileSystemCaseSensitive = SystemInfoRt.isFileSystemCaseSensitive;
  51. public static final boolean areSymLinksSupported = isUnix ||
  52. isWindows && OS_VERSION.compareTo("6.0") >= 0;
  53. public static final boolean is32Bit = ARCH_DATA_MODEL == null || ARCH_DATA_MODEL.equals("32");
  54. public static final boolean is64Bit = !is32Bit;
  55. public static final boolean isAMD64 = "amd64".equals(OS_ARCH);
  56. public static final boolean isMacIntel64 = isMac && "x86_64".equals(OS_ARCH);
  57. /** @deprecated use {@linkplain #hasXdgOpen()} (to remove in IDEA 13) */
  58. public static final boolean hasXdgOpen = isLinux;
  59. private static final NotNullLazyValue<Boolean> ourHasXdgOpen = new AtomicNotNullLazyValue<Boolean>() {
  60. @NotNull
  61. @Override
  62. protected Boolean compute() {
  63. return isUnix && new File("/usr/bin/xdg-open").canExecute();
  64. }
  65. };
  66. public static boolean hasXdgOpen() {
  67. return ourHasXdgOpen.getValue();
  68. }
  69. private static final NotNullLazyValue<Boolean> hasNautilus = new AtomicNotNullLazyValue<Boolean>() {
  70. @NotNull
  71. @Override
  72. protected Boolean compute() {
  73. return isUnix && new File("/usr/bin/nautilus").canExecute();
  74. }
  75. };
  76. public static boolean hasNautilus() {
  77. return hasNautilus.getValue();
  78. }
  79. /** @deprecated use {@linkplain #getFileManagerName()} (to remove in IDEA 13) */
  80. public static final String nativeFileManagerName = "File Manager";
  81. private static final NotNullLazyValue<String> ourFileManagerName = new AtomicNotNullLazyValue<String>() {
  82. @NotNull
  83. @Override
  84. protected String compute() {
  85. return isMac ? "Finder" :
  86. isWindows ? "Explorer" :
  87. hasNautilus() ? "Nautilus" :
  88. "File Manager";
  89. }
  90. };
  91. @NotNull
  92. public static String getFileManagerName() {
  93. return ourFileManagerName.getValue();
  94. }
  95. /**
  96. * Whether IDEA is running under MacOS X version 10.4 or later.
  97. *
  98. * @since 5.0.2
  99. */
  100. public static final boolean isMacOSTiger = isTiger();
  101. /**
  102. * Whether IDEA is running under MacOS X on an Intel Machine
  103. *
  104. * @since 5.0.2
  105. */
  106. public static final boolean isIntelMac = isIntelMac();
  107. /**
  108. * Running under MacOS X version 10.5 or later;
  109. *
  110. * @since 7.0.2
  111. */
  112. public static final boolean isMacOSLeopard = isLeopard();
  113. /**
  114. * Running under MacOS X version 10.6 or later;
  115. *
  116. * @since 9.0
  117. */
  118. public static final boolean isMacOSSnowLeopard = isSnowLeopard();
  119. /**
  120. * Running under MacOS X version 10.7 or later;
  121. *
  122. * @since 11.0
  123. */
  124. public static final boolean isMacOSLion = isLion();
  125. /**
  126. * Running under MacOS X version 10.8 or later;
  127. *
  128. * @since 11.1
  129. */
  130. public static final boolean isMacOSMountainLion = isMountainLion();
  131. /**
  132. * Operating system is supposed to have middle mouse button click occupied by paste action.
  133. *
  134. * @since 6.0
  135. */
  136. public static boolean X11PasteEnabledSystem = isUnix && !isMac;
  137. private static boolean isIntelMac() {
  138. return isMac && "i386".equals(OS_ARCH);
  139. }
  140. private static boolean isTiger() {
  141. return isMac &&
  142. !OS_VERSION.startsWith("10.0") &&
  143. !OS_VERSION.startsWith("10.1") &&
  144. !OS_VERSION.startsWith("10.2") &&
  145. !OS_VERSION.startsWith("10.3");
  146. }
  147. private static boolean isLeopard() {
  148. return isMac && isTiger() && !OS_VERSION.startsWith("10.4");
  149. }
  150. private static boolean isSnowLeopard() {
  151. return isMac && isLeopard() && !OS_VERSION.startsWith("10.5");
  152. }
  153. private static boolean isLion() {
  154. return isMac && isSnowLeopard() && !OS_VERSION.startsWith("10.6");
  155. }
  156. private static boolean isMountainLion() {
  157. return isMac && isLion() && !OS_VERSION.startsWith("10.7");
  158. }
  159. @NotNull
  160. public static String getMacOSMajorVersion() {
  161. return getMacOSMajorVersion(OS_VERSION);
  162. }
  163. public static String getMacOSMajorVersion(String version) {
  164. int[] parts = getMacOSVersionParts(version);
  165. return String.format("%d.%d", parts[0], parts[1]);
  166. }
  167. @NotNull
  168. public static String getMacOSVersionCode() {
  169. return getMacOSVersionCode(OS_VERSION);
  170. }
  171. @NotNull
  172. public static String getMacOSMajorVersionCode() {
  173. return getMacOSMajorVersionCode(OS_VERSION);
  174. }
  175. @NotNull
  176. public static String getMacOSMinorVersionCode() {
  177. return getMacOSMinorVersionCode(OS_VERSION);
  178. }
  179. @NotNull
  180. public static String getMacOSVersionCode(@NotNull String version) {
  181. int[] parts = getMacOSVersionParts(version);
  182. return String.format("%02d%d%d", parts[0], normalize(parts[1]), normalize(parts[2]));
  183. }
  184. @NotNull
  185. public static String getMacOSMajorVersionCode(@NotNull String version) {
  186. int[] parts = getMacOSVersionParts(version);
  187. return String.format("%02d%d%d", parts[0], normalize(parts[1]), 0);
  188. }
  189. @NotNull
  190. public static String getMacOSMinorVersionCode(@NotNull String version) {
  191. int[] parts = getMacOSVersionParts(version);
  192. return String.format("%02d%02d", parts[1], parts[2]);
  193. }
  194. private static int[] getMacOSVersionParts(@NotNull String version) {
  195. List<String> parts = StringUtil.split(version, ".");
  196. while (parts.size() < 3) {
  197. parts.add("0");
  198. }
  199. return new int[]{toInt(parts.get(0)), toInt(parts.get(1)), toInt(parts.get(2))};
  200. }
  201. private static int normalize(int number) {
  202. return number > 9 ? 9 : number;
  203. }
  204. private static int toInt(String string) {
  205. try {
  206. return Integer.valueOf(string);
  207. }
  208. catch (NumberFormatException e) {
  209. return 0;
  210. }
  211. }
  212. public static boolean isJavaVersionAtLeast(String v) {
  213. return StringUtil.compareVersionNumbers(JAVA_RUNTIME_VERSION, v) >= 0;
  214. }
  215. /** @deprecated use {@linkplain SystemProperties#getIntProperty(String, int)} (to remove in IDEA 13) */
  216. public static int getIntProperty(@NotNull final String key, final int defaultValue) {
  217. return SystemProperties.getIntProperty(key, defaultValue);
  218. }
  219. }