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.GraphicsEnvironment;
|
---|
9 | import java.awt.event.KeyEvent;
|
---|
10 | import java.io.BufferedReader;
|
---|
11 | import java.io.File;
|
---|
12 | import java.io.FileInputStream;
|
---|
13 | import java.io.IOException;
|
---|
14 | import java.io.InputStreamReader;
|
---|
15 | import java.net.URI;
|
---|
16 | import java.net.URISyntaxException;
|
---|
17 | import java.util.Arrays;
|
---|
18 |
|
---|
19 | import javax.swing.JOptionPane;
|
---|
20 |
|
---|
21 | import org.openstreetmap.josm.Main;
|
---|
22 | import org.openstreetmap.josm.gui.ExtendedDialog;
|
---|
23 | import org.openstreetmap.josm.gui.util.GuiHelper;
|
---|
24 |
|
---|
25 | /**
|
---|
26 | * see PlatformHook.java
|
---|
27 | *
|
---|
28 | * BTW: THIS IS A STUB. See comments below for details.
|
---|
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 | }
|
---|
40 |
|
---|
41 | @Override
|
---|
42 | public void startupHook() {
|
---|
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 | // TODO: Insert system shortcuts here. See Windows and especially OSX to see how to.
|
---|
68 | for(int i = KeyEvent.VK_F1; i <= KeyEvent.VK_F12; ++i)
|
---|
69 | Shortcut.registerSystemShortcut("screen:toogle"+i, tr("reserved"), i, KeyEvent.CTRL_DOWN_MASK | KeyEvent.ALT_DOWN_MASK).setAutomatic();
|
---|
70 | Shortcut.registerSystemShortcut("system:reset", tr("reserved"), KeyEvent.VK_DELETE, KeyEvent.CTRL_DOWN_MASK | KeyEvent.ALT_DOWN_MASK).setAutomatic();
|
---|
71 | Shortcut.registerSystemShortcut("system:resetX", tr("reserved"), KeyEvent.VK_BACK_SPACE, KeyEvent.CTRL_DOWN_MASK | KeyEvent.ALT_DOWN_MASK).setAutomatic();
|
---|
72 | }
|
---|
73 |
|
---|
74 | /**
|
---|
75 | * This should work for all platforms. Yeah, should.
|
---|
76 | * See PlatformHook.java for a list of reasons why
|
---|
77 | * this is implemented here...
|
---|
78 | */
|
---|
79 | @Override
|
---|
80 | public String makeTooltip(String name, Shortcut sc) {
|
---|
81 | String result = "";
|
---|
82 | result += "<html>";
|
---|
83 | result += name;
|
---|
84 | if (sc != null && sc.getKeyText().length() != 0) {
|
---|
85 | result += " ";
|
---|
86 | result += "<font size='-2'>";
|
---|
87 | result += "("+sc.getKeyText()+")";
|
---|
88 | result += "</font>";
|
---|
89 | }
|
---|
90 | result += " </html>";
|
---|
91 | return result;
|
---|
92 | }
|
---|
93 |
|
---|
94 | @Override
|
---|
95 | public String getDefaultStyle() {
|
---|
96 | return "javax.swing.plaf.metal.MetalLookAndFeel";
|
---|
97 | }
|
---|
98 |
|
---|
99 | @Override
|
---|
100 | public boolean canFullscreen() {
|
---|
101 | return GraphicsEnvironment.getLocalGraphicsEnvironment()
|
---|
102 | .getDefaultScreenDevice().isFullScreenSupported();
|
---|
103 | }
|
---|
104 |
|
---|
105 | @Override
|
---|
106 | public boolean rename(File from, File to) {
|
---|
107 | return from.renameTo(to);
|
---|
108 | }
|
---|
109 |
|
---|
110 | /**
|
---|
111 | * Determines if the distribution is Debian or Ubuntu, or a derivative.
|
---|
112 | * @return {@code true} if the distribution is Debian, Ubuntu or Mint, {@code false} otherwise
|
---|
113 | */
|
---|
114 | public static boolean isDebianOrUbuntu() {
|
---|
115 | try {
|
---|
116 | String dist = Utils.execOutput(Arrays.asList("lsb_release", "-i", "-s"));
|
---|
117 | return "Debian".equalsIgnoreCase(dist) || "Ubuntu".equalsIgnoreCase(dist) || "Mint".equalsIgnoreCase(dist);
|
---|
118 | } catch (IOException e) {
|
---|
119 | Main.warn(e);
|
---|
120 | return false;
|
---|
121 | }
|
---|
122 | }
|
---|
123 |
|
---|
124 | /**
|
---|
125 | * Determines if the JVM is OpenJDK-based.
|
---|
126 | * @return {@code true} if {@code java.home} contains "openjdk", {@code false} otherwise
|
---|
127 | * @since 6951
|
---|
128 | */
|
---|
129 | public static boolean isOpenJDK() {
|
---|
130 | String javaHome = System.getProperty("java.home");
|
---|
131 | return javaHome != null && javaHome.contains("openjdk");
|
---|
132 | }
|
---|
133 |
|
---|
134 | /**
|
---|
135 | * Get the package name including detailed version.
|
---|
136 | * @param packageName The package name
|
---|
137 | * @return The package name and package version if it can be identified, null otherwise
|
---|
138 | */
|
---|
139 | public static String getPackageDetails(String packageName) {
|
---|
140 | try {
|
---|
141 | String version = Utils.execOutput(Arrays.asList(
|
---|
142 | "dpkg-query", "--show", "--showformat", "${Architecture}-${Version}", packageName));
|
---|
143 | if (version != null) {
|
---|
144 | return packageName + ":" + version;
|
---|
145 | }
|
---|
146 | } catch (IOException e) {
|
---|
147 | Main.warn(e);
|
---|
148 | }
|
---|
149 | return null;
|
---|
150 | }
|
---|
151 |
|
---|
152 | /**
|
---|
153 | * Get the Java package name including detailed version.
|
---|
154 | *
|
---|
155 | * Some Java bugs are specific to a certain security update, so in addition
|
---|
156 | * to the Java version, we also need the exact package version.
|
---|
157 | *
|
---|
158 | * This was originally written for #8921, so only Debian based distributions
|
---|
159 | * are covered at the moment. This can be extended to other distributions
|
---|
160 | * if needed.
|
---|
161 | *
|
---|
162 | * @return The package name and package version if it can be identified, null
|
---|
163 | * otherwise
|
---|
164 | */
|
---|
165 | public String getJavaPackageDetails() {
|
---|
166 | if (isDebianOrUbuntu()) {
|
---|
167 | switch(System.getProperty("java.home")) {
|
---|
168 | case "/usr/lib/jvm/java-7-openjdk-amd64/jre":
|
---|
169 | case "/usr/lib/jvm/java-7-openjdk-i386/jre":
|
---|
170 | return getPackageDetails("openjdk-7-jre");
|
---|
171 | }
|
---|
172 | }
|
---|
173 | return null;
|
---|
174 | }
|
---|
175 |
|
---|
176 | /**
|
---|
177 | * Get the Web Start package name including detailed version.
|
---|
178 | *
|
---|
179 | * Debian and Ubuntu OpenJDK packages are shipped with icedtea-web package,
|
---|
180 | * but its version does not match main java package version.
|
---|
181 | *
|
---|
182 | * Only Debian based distributions are covered at the moment.
|
---|
183 | * This can be extended to other distributions if needed.
|
---|
184 | *
|
---|
185 | * Simply return {@code null} if there's no separate package for Java WebStart.
|
---|
186 | *
|
---|
187 | * @return The package name and package version if it can be identified, null otherwise
|
---|
188 | */
|
---|
189 | public String getWebStartPackageDetails() {
|
---|
190 | if (isDebianOrUbuntu() && isOpenJDK()) {
|
---|
191 | return getPackageDetails("icedtea-netx");
|
---|
192 | }
|
---|
193 | return null;
|
---|
194 | }
|
---|
195 |
|
---|
196 | protected String buildOSDescription() {
|
---|
197 | String osName = System.getProperty("os.name");
|
---|
198 | if ("Linux".equalsIgnoreCase(osName)) {
|
---|
199 | try {
|
---|
200 | // Try lsb_release (only available on LSB-compliant Linux systems, see https://www.linuxbase.org/lsb-cert/productdir.php?by_prod )
|
---|
201 | Process p = Runtime.getRuntime().exec("lsb_release -ds");
|
---|
202 | BufferedReader input = new BufferedReader(new InputStreamReader(p.getInputStream(), Utils.UTF_8));
|
---|
203 | String line = Utils.strip(input.readLine());
|
---|
204 | Utils.close(input);
|
---|
205 | if (line != null && !line.isEmpty()) {
|
---|
206 | line = line.replaceAll("\"+","");
|
---|
207 | line = line.replaceAll("NAME=",""); // strange code for some Gentoo's
|
---|
208 | if(line.startsWith("Linux ")) // e.g. Linux Mint
|
---|
209 | return line;
|
---|
210 | else if(!line.isEmpty())
|
---|
211 | return "Linux " + line;
|
---|
212 | }
|
---|
213 | } catch (IOException e) {
|
---|
214 | // Non LSB-compliant Linux system. List of common fallback release files: http://linuxmafia.com/faq/Admin/release-files.html
|
---|
215 | for (LinuxReleaseInfo info : new LinuxReleaseInfo[]{
|
---|
216 | new LinuxReleaseInfo("/etc/lsb-release", "DISTRIB_DESCRIPTION", "DISTRIB_ID", "DISTRIB_RELEASE"),
|
---|
217 | new LinuxReleaseInfo("/etc/os-release", "PRETTY_NAME", "NAME", "VERSION"),
|
---|
218 | new LinuxReleaseInfo("/etc/arch-release"),
|
---|
219 | new LinuxReleaseInfo("/etc/debian_version", "Debian GNU/Linux "),
|
---|
220 | new LinuxReleaseInfo("/etc/fedora-release"),
|
---|
221 | new LinuxReleaseInfo("/etc/gentoo-release"),
|
---|
222 | new LinuxReleaseInfo("/etc/redhat-release")
|
---|
223 | }) {
|
---|
224 | String description = info.extractDescription();
|
---|
225 | if (description != null && !description.isEmpty()) {
|
---|
226 | return "Linux " + description;
|
---|
227 | }
|
---|
228 | }
|
---|
229 | }
|
---|
230 | }
|
---|
231 | return osName;
|
---|
232 | }
|
---|
233 |
|
---|
234 | @Override
|
---|
235 | public String getOSDescription() {
|
---|
236 | if (osDescription == null) {
|
---|
237 | osDescription = buildOSDescription();
|
---|
238 | }
|
---|
239 | return osDescription;
|
---|
240 | }
|
---|
241 |
|
---|
242 | protected static class LinuxReleaseInfo {
|
---|
243 | private final String path;
|
---|
244 | private final String descriptionField;
|
---|
245 | private final String idField;
|
---|
246 | private final String releaseField;
|
---|
247 | private final boolean plainText;
|
---|
248 | private final String prefix;
|
---|
249 |
|
---|
250 | public LinuxReleaseInfo(String path, String descriptionField, String idField, String releaseField) {
|
---|
251 | this(path, descriptionField, idField, releaseField, false, null);
|
---|
252 | }
|
---|
253 |
|
---|
254 | public LinuxReleaseInfo(String path) {
|
---|
255 | this(path, null, null, null, true, null);
|
---|
256 | }
|
---|
257 |
|
---|
258 | public LinuxReleaseInfo(String path, String prefix) {
|
---|
259 | this(path, null, null, null, true, prefix);
|
---|
260 | }
|
---|
261 |
|
---|
262 | private LinuxReleaseInfo(String path, String descriptionField, String idField, String releaseField, boolean plainText, String prefix) {
|
---|
263 | this.path = path;
|
---|
264 | this.descriptionField = descriptionField;
|
---|
265 | this.idField = idField;
|
---|
266 | this.releaseField = releaseField;
|
---|
267 | this.plainText = plainText;
|
---|
268 | this.prefix = prefix;
|
---|
269 | }
|
---|
270 |
|
---|
271 | @Override public String toString() {
|
---|
272 | return "ReleaseInfo [path=" + path + ", descriptionField=" + descriptionField +
|
---|
273 | ", idField=" + idField + ", releaseField=" + releaseField + "]";
|
---|
274 | }
|
---|
275 |
|
---|
276 | /**
|
---|
277 | * Extracts OS detailed information from a Linux release file (/etc/xxx-release)
|
---|
278 | * @return The OS detailed information, or {@code null}
|
---|
279 | */
|
---|
280 | public String extractDescription() {
|
---|
281 | String result = null;
|
---|
282 | if (path != null) {
|
---|
283 | File file = new File(path);
|
---|
284 | if (file.exists()) {
|
---|
285 | BufferedReader reader = null;
|
---|
286 | try {
|
---|
287 | reader = new BufferedReader(new InputStreamReader(new FileInputStream(file), Utils.UTF_8));
|
---|
288 | String id = null;
|
---|
289 | String release = null;
|
---|
290 | String line;
|
---|
291 | while (result == null && (line = reader.readLine()) != null) {
|
---|
292 | if (line.contains("=")) {
|
---|
293 | String[] tokens = line.split("=");
|
---|
294 | if (tokens.length >= 2) {
|
---|
295 | // Description, if available, contains exactly what we need
|
---|
296 | if (descriptionField != null && descriptionField.equalsIgnoreCase(tokens[0])) {
|
---|
297 | result = Utils.strip(tokens[1]);
|
---|
298 | } else if (idField != null && idField.equalsIgnoreCase(tokens[0])) {
|
---|
299 | id = Utils.strip(tokens[1]);
|
---|
300 | } else if (releaseField != null && releaseField.equalsIgnoreCase(tokens[0])) {
|
---|
301 | release = Utils.strip(tokens[1]);
|
---|
302 | }
|
---|
303 | }
|
---|
304 | } else if (plainText && !line.isEmpty()) {
|
---|
305 | // Files composed of a single line
|
---|
306 | result = Utils.strip(line);
|
---|
307 | }
|
---|
308 | }
|
---|
309 | // If no description has been found, try to rebuild it with "id" + "release" (i.e. "name" + "version")
|
---|
310 | if (result == null && id != null && release != null) {
|
---|
311 | result = id + " " + release;
|
---|
312 | }
|
---|
313 | } catch (IOException e) {
|
---|
314 | // Ignore
|
---|
315 | } finally {
|
---|
316 | Utils.close(reader);
|
---|
317 | }
|
---|
318 | }
|
---|
319 | }
|
---|
320 | // Append prefix if any
|
---|
321 | if (result != null && !result.isEmpty() && prefix != null && !prefix.isEmpty()) {
|
---|
322 | result = prefix + result;
|
---|
323 | }
|
---|
324 | if(result != null)
|
---|
325 | result = result.replaceAll("\"+","");
|
---|
326 | return result;
|
---|
327 | }
|
---|
328 | }
|
---|
329 |
|
---|
330 | protected void askUpdateJava(String version) {
|
---|
331 | askUpdateJava(version, "https://www.java.com/download");
|
---|
332 | }
|
---|
333 |
|
---|
334 | // Method kept because strings have already been translated. To enable for Java 8 migration somewhere in 2016
|
---|
335 | protected void askUpdateJava(final String version, final String url) {
|
---|
336 | GuiHelper.runInEDTAndWait(new Runnable() {
|
---|
337 | @Override
|
---|
338 | public void run() {
|
---|
339 | ExtendedDialog ed = new ExtendedDialog(
|
---|
340 | Main.parent,
|
---|
341 | tr("Outdated Java version"),
|
---|
342 | new String[]{tr("Update Java"), tr("Cancel")});
|
---|
343 | // Check if the dialog has not already been permanently hidden by user
|
---|
344 | if (!ed.toggleEnable("askUpdateJava8").toggleCheckState()) {
|
---|
345 | ed.setButtonIcons(new String[]{"java.png", "cancel.png"}).setCancelButton(2);
|
---|
346 | ed.setMinimumSize(new Dimension(480, 300));
|
---|
347 | ed.setIcon(JOptionPane.WARNING_MESSAGE);
|
---|
348 | String content = tr("You are running version {0} of Java.", "<b>"+version+"</b>")+"<br><br>";
|
---|
349 | if ("Sun Microsystems Inc.".equals(System.getProperty("java.vendor")) && !isOpenJDK()) {
|
---|
350 | content += "<b>"+tr("This version is no longer supported by {0} since {1} and is not recommended for use.",
|
---|
351 | "Oracle", tr("April 2015"))+"</b><br><br>";
|
---|
352 | }
|
---|
353 | content += "<b>"+tr("JOSM will soon stop working with this version; we highly recommend you to update to Java {0}.", "8")+"</b><br><br>"+
|
---|
354 | tr("Would you like to update now ?");
|
---|
355 | ed.setContent(content);
|
---|
356 |
|
---|
357 | if (ed.showDialog().getValue() == 1) {
|
---|
358 | try {
|
---|
359 | openUrl(url);
|
---|
360 | } catch (IOException e) {
|
---|
361 | Main.warn(e);
|
---|
362 | }
|
---|
363 | }
|
---|
364 | }
|
---|
365 | }
|
---|
366 | });
|
---|
367 | }
|
---|
368 | }
|
---|