1 | // License: GPL. For details, see LICENSE file.
|
---|
2 | package org.openstreetmap.josm.tools;
|
---|
3 |
|
---|
4 | import static org.openstreetmap.josm.tools.I18n.tr;
|
---|
5 |
|
---|
6 | import java.awt.Desktop;
|
---|
7 | import java.awt.Dimension;
|
---|
8 | import java.awt.event.KeyEvent;
|
---|
9 | import java.io.BufferedReader;
|
---|
10 | import java.io.File;
|
---|
11 | import java.io.IOException;
|
---|
12 | import java.io.InputStreamReader;
|
---|
13 | import java.net.URI;
|
---|
14 | import java.net.URISyntaxException;
|
---|
15 | import java.nio.charset.StandardCharsets;
|
---|
16 | import java.nio.file.Files;
|
---|
17 | import java.nio.file.Path;
|
---|
18 | import java.nio.file.Paths;
|
---|
19 | import java.util.Arrays;
|
---|
20 |
|
---|
21 | import javax.swing.JOptionPane;
|
---|
22 |
|
---|
23 | import org.openstreetmap.josm.Main;
|
---|
24 | import org.openstreetmap.josm.gui.ExtendedDialog;
|
---|
25 | import org.openstreetmap.josm.gui.util.GuiHelper;
|
---|
26 |
|
---|
27 | /**
|
---|
28 | * {@code PlatformHook} base implementation.
|
---|
29 | *
|
---|
30 | * Don't write (Main.platform instanceof PlatformHookUnixoid) because other platform
|
---|
31 | * hooks are subclasses of this class.
|
---|
32 | */
|
---|
33 | public class PlatformHookUnixoid implements PlatformHook {
|
---|
34 |
|
---|
35 | private String osDescription;
|
---|
36 |
|
---|
37 | @Override
|
---|
38 | public void preStartupHook() {
|
---|
39 | // See #12022 - Disable GNOME ATK Java wrapper as it causes a lot of serious trouble
|
---|
40 | if ("org.GNOME.Accessibility.AtkWrapper".equals(System.getProperty("assistive_technologies"))) {
|
---|
41 | System.clearProperty("assistive_technologies");
|
---|
42 | }
|
---|
43 | }
|
---|
44 |
|
---|
45 | @Override
|
---|
46 | public void openUrl(String url) throws IOException {
|
---|
47 | for (String program : Main.pref.getCollection("browser.unix",
|
---|
48 | Arrays.asList("xdg-open", "#DESKTOP#", "$BROWSER", "gnome-open", "kfmclient openURL", "firefox"))) {
|
---|
49 | try {
|
---|
50 | if ("#DESKTOP#".equals(program)) {
|
---|
51 | Desktop.getDesktop().browse(new URI(url));
|
---|
52 | } else if (program.startsWith("$")) {
|
---|
53 | program = System.getenv().get(program.substring(1));
|
---|
54 | Runtime.getRuntime().exec(new String[]{program, url});
|
---|
55 | } else {
|
---|
56 | Runtime.getRuntime().exec(new String[]{program, url});
|
---|
57 | }
|
---|
58 | return;
|
---|
59 | } catch (IOException | URISyntaxException e) {
|
---|
60 | Main.warn(e);
|
---|
61 | }
|
---|
62 | }
|
---|
63 | }
|
---|
64 |
|
---|
65 | @Override
|
---|
66 | public void initSystemShortcuts() {
|
---|
67 | // CHECKSTYLE.OFF: LineLength
|
---|
68 | // TODO: Insert system shortcuts here. See Windows and especially OSX to see how to.
|
---|
69 | for (int i = KeyEvent.VK_F1; i <= KeyEvent.VK_F12; ++i) {
|
---|
70 | Shortcut.registerSystemShortcut("screen:toogle"+i, tr("reserved"), i, KeyEvent.CTRL_DOWN_MASK | KeyEvent.ALT_DOWN_MASK)
|
---|
71 | .setAutomatic();
|
---|
72 | }
|
---|
73 | Shortcut.registerSystemShortcut("system:reset", tr("reserved"), KeyEvent.VK_DELETE, KeyEvent.CTRL_DOWN_MASK | KeyEvent.ALT_DOWN_MASK)
|
---|
74 | .setAutomatic();
|
---|
75 | Shortcut.registerSystemShortcut("system:resetX", tr("reserved"), KeyEvent.VK_BACK_SPACE, KeyEvent.CTRL_DOWN_MASK | KeyEvent.ALT_DOWN_MASK)
|
---|
76 | .setAutomatic();
|
---|
77 | // CHECKSTYLE.ON: LineLength
|
---|
78 | }
|
---|
79 |
|
---|
80 | @Override
|
---|
81 | public String getDefaultStyle() {
|
---|
82 | return "javax.swing.plaf.metal.MetalLookAndFeel";
|
---|
83 | }
|
---|
84 |
|
---|
85 | /**
|
---|
86 | * Determines if the distribution is Debian or Ubuntu, or a derivative.
|
---|
87 | * @return {@code true} if the distribution is Debian, Ubuntu or Mint, {@code false} otherwise
|
---|
88 | */
|
---|
89 | public static boolean isDebianOrUbuntu() {
|
---|
90 | try {
|
---|
91 | String dist = Utils.execOutput(Arrays.asList("lsb_release", "-i", "-s"));
|
---|
92 | return "Debian".equalsIgnoreCase(dist) || "Ubuntu".equalsIgnoreCase(dist) || "Mint".equalsIgnoreCase(dist);
|
---|
93 | } catch (IOException e) {
|
---|
94 | // lsb_release is not available on all Linux systems, so don't log at warning level
|
---|
95 | Main.debug(e);
|
---|
96 | return false;
|
---|
97 | }
|
---|
98 | }
|
---|
99 |
|
---|
100 | /**
|
---|
101 | * Determines if the JVM is OpenJDK-based.
|
---|
102 | * @return {@code true} if {@code java.home} contains "openjdk", {@code false} otherwise
|
---|
103 | * @since 6951
|
---|
104 | */
|
---|
105 | public static boolean isOpenJDK() {
|
---|
106 | String javaHome = System.getProperty("java.home");
|
---|
107 | return javaHome != null && javaHome.contains("openjdk");
|
---|
108 | }
|
---|
109 |
|
---|
110 | /**
|
---|
111 | * Get the package name including detailed version.
|
---|
112 | * @param packageNames The possible package names (when a package can have different names on different distributions)
|
---|
113 | * @return The package name and package version if it can be identified, null otherwise
|
---|
114 | * @since 7314
|
---|
115 | */
|
---|
116 | public static String getPackageDetails(String ... packageNames) {
|
---|
117 | try {
|
---|
118 | // CHECKSTYLE.OFF: SingleSpaceSeparator
|
---|
119 | boolean dpkg = Paths.get("/usr/bin/dpkg-query").toFile().exists();
|
---|
120 | boolean eque = Paths.get("/usr/bin/equery").toFile().exists();
|
---|
121 | boolean rpm = Paths.get("/bin/rpm").toFile().exists();
|
---|
122 | // CHECKSTYLE.ON: SingleSpaceSeparator
|
---|
123 | if (dpkg || rpm || eque) {
|
---|
124 | for (String packageName : packageNames) {
|
---|
125 | String[] args;
|
---|
126 | if (dpkg) {
|
---|
127 | args = new String[] {"dpkg-query", "--show", "--showformat", "${Architecture}-${Version}", packageName};
|
---|
128 | } else if (eque) {
|
---|
129 | args = new String[] {"equery", "-q", "list", "-e", "--format=$fullversion", packageName};
|
---|
130 | } else {
|
---|
131 | args = new String[] {"rpm", "-q", "--qf", "%{arch}-%{version}", packageName};
|
---|
132 | }
|
---|
133 | String version = Utils.execOutput(Arrays.asList(args));
|
---|
134 | if (version != null && !version.contains("not installed")) {
|
---|
135 | return packageName + ':' + version;
|
---|
136 | }
|
---|
137 | }
|
---|
138 | }
|
---|
139 | } catch (IOException e) {
|
---|
140 | Main.warn(e);
|
---|
141 | }
|
---|
142 | return null;
|
---|
143 | }
|
---|
144 |
|
---|
145 | /**
|
---|
146 | * Get the Java package name including detailed version.
|
---|
147 | *
|
---|
148 | * Some Java bugs are specific to a certain security update, so in addition
|
---|
149 | * to the Java version, we also need the exact package version.
|
---|
150 | *
|
---|
151 | * @return The package name and package version if it can be identified, null otherwise
|
---|
152 | */
|
---|
153 | public String getJavaPackageDetails() {
|
---|
154 | String home = System.getProperty("java.home");
|
---|
155 | if (home.contains("java-8-openjdk") || home.contains("java-1.8.0-openjdk")) {
|
---|
156 | return getPackageDetails("openjdk-8-jre", "java-1_8_0-openjdk", "java-1.8.0-openjdk");
|
---|
157 | } else if (home.contains("java-9-openjdk") || home.contains("java-1.9.0-openjdk")) {
|
---|
158 | return getPackageDetails("openjdk-9-jre", "java-1_9_0-openjdk", "java-1.9.0-openjdk");
|
---|
159 | } else if (home.contains("icedtea")) {
|
---|
160 | return getPackageDetails("icedtea-bin");
|
---|
161 | } else if (home.contains("oracle")) {
|
---|
162 | return getPackageDetails("oracle-jdk-bin", "oracle-jre-bin");
|
---|
163 | }
|
---|
164 | return null;
|
---|
165 | }
|
---|
166 |
|
---|
167 | /**
|
---|
168 | * Get the Web Start package name including detailed version.
|
---|
169 | *
|
---|
170 | * OpenJDK packages are shipped with icedtea-web package,
|
---|
171 | * but its version generally does not match main java package version.
|
---|
172 | *
|
---|
173 | * Simply return {@code null} if there's no separate package for Java WebStart.
|
---|
174 | *
|
---|
175 | * @return The package name and package version if it can be identified, null otherwise
|
---|
176 | */
|
---|
177 | public String getWebStartPackageDetails() {
|
---|
178 | if (isOpenJDK()) {
|
---|
179 | return getPackageDetails("icedtea-netx", "icedtea-web");
|
---|
180 | }
|
---|
181 | return null;
|
---|
182 | }
|
---|
183 |
|
---|
184 | /**
|
---|
185 | * Get the Gnome ATK wrapper package name including detailed version.
|
---|
186 | *
|
---|
187 | * Debian and Ubuntu derivatives come with a pre-enabled accessibility software
|
---|
188 | * completely buggy that makes Swing crash in a lot of different ways.
|
---|
189 | *
|
---|
190 | * Simply return {@code null} if it's not found.
|
---|
191 | *
|
---|
192 | * @return The package name and package version if it can be identified, null otherwise
|
---|
193 | */
|
---|
194 | public String getAtkWrapperPackageDetails() {
|
---|
195 | if (isOpenJDK() && isDebianOrUbuntu()) {
|
---|
196 | return getPackageDetails("libatk-wrapper-java");
|
---|
197 | }
|
---|
198 | return null;
|
---|
199 | }
|
---|
200 |
|
---|
201 | protected String buildOSDescription() {
|
---|
202 | String osName = System.getProperty("os.name");
|
---|
203 | if ("Linux".equalsIgnoreCase(osName)) {
|
---|
204 | try {
|
---|
205 | // Try lsb_release (only available on LSB-compliant Linux systems,
|
---|
206 | // see https://www.linuxbase.org/lsb-cert/productdir.php?by_prod )
|
---|
207 | Process p = Runtime.getRuntime().exec("lsb_release -ds");
|
---|
208 | try (BufferedReader input = new BufferedReader(new InputStreamReader(p.getInputStream(), StandardCharsets.UTF_8))) {
|
---|
209 | String line = Utils.strip(input.readLine());
|
---|
210 | if (line != null && !line.isEmpty()) {
|
---|
211 | line = line.replaceAll("\"+", "");
|
---|
212 | line = line.replaceAll("NAME=", ""); // strange code for some Gentoo's
|
---|
213 | if (line.startsWith("Linux ")) // e.g. Linux Mint
|
---|
214 | return line;
|
---|
215 | else if (!line.isEmpty())
|
---|
216 | return "Linux " + line;
|
---|
217 | }
|
---|
218 | }
|
---|
219 | } catch (IOException e) {
|
---|
220 | Main.debug(e);
|
---|
221 | // Non LSB-compliant Linux system. List of common fallback release files: http://linuxmafia.com/faq/Admin/release-files.html
|
---|
222 | for (LinuxReleaseInfo info : new LinuxReleaseInfo[]{
|
---|
223 | new LinuxReleaseInfo("/etc/lsb-release", "DISTRIB_DESCRIPTION", "DISTRIB_ID", "DISTRIB_RELEASE"),
|
---|
224 | new LinuxReleaseInfo("/etc/os-release", "PRETTY_NAME", "NAME", "VERSION"),
|
---|
225 | new LinuxReleaseInfo("/etc/arch-release"),
|
---|
226 | new LinuxReleaseInfo("/etc/debian_version", "Debian GNU/Linux "),
|
---|
227 | new LinuxReleaseInfo("/etc/fedora-release"),
|
---|
228 | new LinuxReleaseInfo("/etc/gentoo-release"),
|
---|
229 | new LinuxReleaseInfo("/etc/redhat-release"),
|
---|
230 | new LinuxReleaseInfo("/etc/SuSE-release")
|
---|
231 | }) {
|
---|
232 | String description = info.extractDescription();
|
---|
233 | if (description != null && !description.isEmpty()) {
|
---|
234 | return "Linux " + description;
|
---|
235 | }
|
---|
236 | }
|
---|
237 | }
|
---|
238 | }
|
---|
239 | return osName;
|
---|
240 | }
|
---|
241 |
|
---|
242 | @Override
|
---|
243 | public String getOSDescription() {
|
---|
244 | if (osDescription == null) {
|
---|
245 | osDescription = buildOSDescription();
|
---|
246 | }
|
---|
247 | return osDescription;
|
---|
248 | }
|
---|
249 |
|
---|
250 | protected static class LinuxReleaseInfo {
|
---|
251 | private final String path;
|
---|
252 | private final String descriptionField;
|
---|
253 | private final String idField;
|
---|
254 | private final String releaseField;
|
---|
255 | private final boolean plainText;
|
---|
256 | private final String prefix;
|
---|
257 |
|
---|
258 | public LinuxReleaseInfo(String path, String descriptionField, String idField, String releaseField) {
|
---|
259 | this(path, descriptionField, idField, releaseField, false, null);
|
---|
260 | }
|
---|
261 |
|
---|
262 | public LinuxReleaseInfo(String path) {
|
---|
263 | this(path, null, null, null, true, null);
|
---|
264 | }
|
---|
265 |
|
---|
266 | public LinuxReleaseInfo(String path, String prefix) {
|
---|
267 | this(path, null, null, null, true, prefix);
|
---|
268 | }
|
---|
269 |
|
---|
270 | private LinuxReleaseInfo(String path, String descriptionField, String idField, String releaseField, boolean plainText, String prefix) {
|
---|
271 | this.path = path;
|
---|
272 | this.descriptionField = descriptionField;
|
---|
273 | this.idField = idField;
|
---|
274 | this.releaseField = releaseField;
|
---|
275 | this.plainText = plainText;
|
---|
276 | this.prefix = prefix;
|
---|
277 | }
|
---|
278 |
|
---|
279 | @Override public String toString() {
|
---|
280 | return "ReleaseInfo [path=" + path + ", descriptionField=" + descriptionField +
|
---|
281 | ", idField=" + idField + ", releaseField=" + releaseField + ']';
|
---|
282 | }
|
---|
283 |
|
---|
284 | /**
|
---|
285 | * Extracts OS detailed information from a Linux release file (/etc/xxx-release)
|
---|
286 | * @return The OS detailed information, or {@code null}
|
---|
287 | */
|
---|
288 | public String extractDescription() {
|
---|
289 | String result = null;
|
---|
290 | if (path != null) {
|
---|
291 | Path p = Paths.get(path);
|
---|
292 | if (p.toFile().exists()) {
|
---|
293 | try (BufferedReader reader = Files.newBufferedReader(p, StandardCharsets.UTF_8)) {
|
---|
294 | String id = null;
|
---|
295 | String release = null;
|
---|
296 | String line;
|
---|
297 | while (result == null && (line = reader.readLine()) != null) {
|
---|
298 | if (line.contains("=")) {
|
---|
299 | String[] tokens = line.split("=");
|
---|
300 | if (tokens.length >= 2) {
|
---|
301 | // Description, if available, contains exactly what we need
|
---|
302 | if (descriptionField != null && descriptionField.equalsIgnoreCase(tokens[0])) {
|
---|
303 | result = Utils.strip(tokens[1]);
|
---|
304 | } else if (idField != null && idField.equalsIgnoreCase(tokens[0])) {
|
---|
305 | id = Utils.strip(tokens[1]);
|
---|
306 | } else if (releaseField != null && releaseField.equalsIgnoreCase(tokens[0])) {
|
---|
307 | release = Utils.strip(tokens[1]);
|
---|
308 | }
|
---|
309 | }
|
---|
310 | } else if (plainText && !line.isEmpty()) {
|
---|
311 | // Files composed of a single line
|
---|
312 | result = Utils.strip(line);
|
---|
313 | }
|
---|
314 | }
|
---|
315 | // If no description has been found, try to rebuild it with "id" + "release" (i.e. "name" + "version")
|
---|
316 | if (result == null && id != null && release != null) {
|
---|
317 | result = id + ' ' + release;
|
---|
318 | }
|
---|
319 | } catch (IOException e) {
|
---|
320 | // Ignore
|
---|
321 | Main.trace(e);
|
---|
322 | }
|
---|
323 | }
|
---|
324 | }
|
---|
325 | // Append prefix if any
|
---|
326 | if (result != null && !result.isEmpty() && prefix != null && !prefix.isEmpty()) {
|
---|
327 | result = prefix + result;
|
---|
328 | }
|
---|
329 | if (result != null)
|
---|
330 | result = result.replaceAll("\"+", "");
|
---|
331 | return result;
|
---|
332 | }
|
---|
333 | }
|
---|
334 |
|
---|
335 | // Method unused, but kept for translation already done. To reuse during Java 9 migration
|
---|
336 | protected void askUpdateJava(final String version, final String url) {
|
---|
337 | GuiHelper.runInEDTAndWait(() -> {
|
---|
338 | ExtendedDialog ed = new ExtendedDialog(
|
---|
339 | Main.parent,
|
---|
340 | tr("Outdated Java version"),
|
---|
341 | new String[]{tr("OK"), tr("Update Java"), tr("Cancel")});
|
---|
342 | // Check if the dialog has not already been permanently hidden by user
|
---|
343 | if (!ed.toggleEnable("askUpdateJava9").toggleCheckState()) {
|
---|
344 | ed.setButtonIcons(new String[]{"ok", "java", "cancel"}).setCancelButton(3);
|
---|
345 | ed.setMinimumSize(new Dimension(480, 300));
|
---|
346 | ed.setIcon(JOptionPane.WARNING_MESSAGE);
|
---|
347 | StringBuilder content = new StringBuilder(tr("You are running version {0} of Java.", "<b>"+version+"</b>"))
|
---|
348 | .append("<br><br>");
|
---|
349 | if ("Sun Microsystems Inc.".equals(System.getProperty("java.vendor")) && !isOpenJDK()) {
|
---|
350 | content.append("<b>").append(tr("This version is no longer supported by {0} since {1} and is not recommended for use.",
|
---|
351 | "Oracle", tr("April 2015"))).append("</b><br><br>"); // TODO: change date once Java 8 EOL is announced
|
---|
352 | }
|
---|
353 | content.append("<b>")
|
---|
354 | .append(tr("JOSM will soon stop working with this version; we highly recommend you to update to Java {0}.", "8"))
|
---|
355 | .append("</b><br><br>")
|
---|
356 | .append(tr("Would you like to update now ?"));
|
---|
357 | ed.setContent(content.toString());
|
---|
358 |
|
---|
359 | if (ed.showDialog().getValue() == 2) {
|
---|
360 | try {
|
---|
361 | openUrl(url);
|
---|
362 | } catch (IOException e) {
|
---|
363 | Main.warn(e);
|
---|
364 | }
|
---|
365 | }
|
---|
366 | }
|
---|
367 | });
|
---|
368 | }
|
---|
369 |
|
---|
370 | @Override
|
---|
371 | public File getDefaultCacheDirectory() {
|
---|
372 | return new File(Main.pref.getUserDataDirectory(), "cache");
|
---|
373 | }
|
---|
374 |
|
---|
375 | @Override
|
---|
376 | public File getDefaultPrefDirectory() {
|
---|
377 | return new File(System.getProperty("user.home"), ".josm");
|
---|
378 | }
|
---|
379 |
|
---|
380 | @Override
|
---|
381 | public File getDefaultUserDataDirectory() {
|
---|
382 | // Use preferences directory by default
|
---|
383 | return Main.pref.getPreferencesDirectory();
|
---|
384 | }
|
---|
385 |
|
---|
386 | }
|
---|