source: josm/trunk/src/org/openstreetmap/josm/tools/PlatformHookUnixoid.java@ 7060

Last change on this file since 7060 was 7037, checked in by Don-vip, 10 years ago

see #8465 - last batch of try-with-resources

  • Property svn:eol-style set to native
File size: 14.9 KB
Line 
1// License: GPL. For details, see LICENSE file.
2package org.openstreetmap.josm.tools;
3
4import static org.openstreetmap.josm.tools.I18n.tr;
5
6import java.awt.Desktop;
7import java.awt.Dimension;
8import java.awt.GraphicsEnvironment;
9import java.awt.event.KeyEvent;
10import java.io.BufferedReader;
11import java.io.File;
12import java.io.FileInputStream;
13import java.io.IOException;
14import java.io.InputStreamReader;
15import java.net.URI;
16import java.net.URISyntaxException;
17import java.util.Arrays;
18
19import javax.swing.JOptionPane;
20
21import org.openstreetmap.josm.Main;
22import org.openstreetmap.josm.gui.ExtendedDialog;
23import 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 */
33public 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 += "&nbsp;</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 try (BufferedReader input = new BufferedReader(new InputStreamReader(p.getInputStream(), Utils.UTF_8))) {
203 String line = Utils.strip(input.readLine());
204 if (line != null && !line.isEmpty()) {
205 line = line.replaceAll("\"+","");
206 line = line.replaceAll("NAME=",""); // strange code for some Gentoo's
207 if(line.startsWith("Linux ")) // e.g. Linux Mint
208 return line;
209 else if(!line.isEmpty())
210 return "Linux " + line;
211 }
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 try (BufferedReader reader = new BufferedReader(new InputStreamReader(new FileInputStream(file), Utils.UTF_8))) {
286 String id = null;
287 String release = null;
288 String line;
289 while (result == null && (line = reader.readLine()) != null) {
290 if (line.contains("=")) {
291 String[] tokens = line.split("=");
292 if (tokens.length >= 2) {
293 // Description, if available, contains exactly what we need
294 if (descriptionField != null && descriptionField.equalsIgnoreCase(tokens[0])) {
295 result = Utils.strip(tokens[1]);
296 } else if (idField != null && idField.equalsIgnoreCase(tokens[0])) {
297 id = Utils.strip(tokens[1]);
298 } else if (releaseField != null && releaseField.equalsIgnoreCase(tokens[0])) {
299 release = Utils.strip(tokens[1]);
300 }
301 }
302 } else if (plainText && !line.isEmpty()) {
303 // Files composed of a single line
304 result = Utils.strip(line);
305 }
306 }
307 // If no description has been found, try to rebuild it with "id" + "release" (i.e. "name" + "version")
308 if (result == null && id != null && release != null) {
309 result = id + " " + release;
310 }
311 } catch (IOException e) {
312 // Ignore
313 }
314 }
315 }
316 // Append prefix if any
317 if (result != null && !result.isEmpty() && prefix != null && !prefix.isEmpty()) {
318 result = prefix + result;
319 }
320 if(result != null)
321 result = result.replaceAll("\"+","");
322 return result;
323 }
324 }
325
326 protected void askUpdateJava(String version) {
327 askUpdateJava(version, "https://www.java.com/download");
328 }
329
330 // Method kept because strings have already been translated. To enable for Java 8 migration somewhere in 2016
331 protected void askUpdateJava(final String version, final String url) {
332 GuiHelper.runInEDTAndWait(new Runnable() {
333 @Override
334 public void run() {
335 ExtendedDialog ed = new ExtendedDialog(
336 Main.parent,
337 tr("Outdated Java version"),
338 new String[]{tr("Update Java"), tr("Cancel")});
339 // Check if the dialog has not already been permanently hidden by user
340 if (!ed.toggleEnable("askUpdateJava8").toggleCheckState()) {
341 ed.setButtonIcons(new String[]{"java.png", "cancel.png"}).setCancelButton(2);
342 ed.setMinimumSize(new Dimension(480, 300));
343 ed.setIcon(JOptionPane.WARNING_MESSAGE);
344 String content = tr("You are running version {0} of Java.", "<b>"+version+"</b>")+"<br><br>";
345 if ("Sun Microsystems Inc.".equals(System.getProperty("java.vendor")) && !isOpenJDK()) {
346 content += "<b>"+tr("This version is no longer supported by {0} since {1} and is not recommended for use.",
347 "Oracle", tr("April 2015"))+"</b><br><br>";
348 }
349 content += "<b>"+tr("JOSM will soon stop working with this version; we highly recommend you to update to Java {0}.", "8")+"</b><br><br>"+
350 tr("Would you like to update now ?");
351 ed.setContent(content);
352
353 if (ed.showDialog().getValue() == 1) {
354 try {
355 openUrl(url);
356 } catch (IOException e) {
357 Main.warn(e);
358 }
359 }
360 }
361 }
362 });
363 }
364}
Note: See TracBrowser for help on using the repository browser.