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

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

see #8465 - replace Utils.UTF_8 by StandardCharsets.UTF_8, new in Java 7

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