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

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

see #8888 - Add icedtea-web package version in status report for Debian/Ubuntu

  • Property svn:eol-style set to native
File size: 14.2 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.FileReader;
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;
23
24/**
25 * see PlatformHook.java
26 *
27 * BTW: THIS IS A STUB. See comments below for details.
28 *
29 * Don't write (Main.platform instanceof PlatformHookUnixoid) because other platform
30 * hooks are subclasses of this class.
31 */
32public class PlatformHookUnixoid implements PlatformHook {
33
34 private String osDescription;
35
36 @Override
37 public void preStartupHook() {
38 }
39
40 @Override
41 public void startupHook() {
42 }
43
44 @Override
45 public void openUrl(String url) throws IOException {
46 for (String program : Main.pref.getCollection("browser.unix",
47 Arrays.asList("xdg-open", "#DESKTOP#", "$BROWSER", "gnome-open", "kfmclient openURL", "firefox"))) {
48 try {
49 if ("#DESKTOP#".equals(program)) {
50 Desktop.getDesktop().browse(new URI(url));
51 } else if (program.startsWith("$")) {
52 program = System.getenv().get(program.substring(1));
53 Runtime.getRuntime().exec(new String[]{program, url});
54 } else {
55 Runtime.getRuntime().exec(new String[]{program, url});
56 }
57 return;
58 } catch (IOException e) {
59 Main.warn(e);
60 } catch (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.getLocalGraphicsEnvironment()
103 .getDefaultScreenDevice().isFullScreenSupported();
104 }
105
106 @Override
107 public boolean rename(File from, File to) {
108 return from.renameTo(to);
109 }
110
111 /**
112 * Determines if the distribution is Debian or Ubuntu.
113 * @return {@code true} if the distribution is Debian or Ubuntu, {@code false} otherwise
114 */
115 public static boolean isDebianOrUbuntu() {
116 try {
117 String dist = Utils.execOutput(Arrays.asList("lsb_release", "-i", "-s"));
118 return "Debian".equalsIgnoreCase(dist) || "Ubuntu".equalsIgnoreCase(dist);
119 } catch (IOException e) {
120 Main.warn(e);
121 return false;
122 }
123 }
124
125 /**
126 * Get the package name including detailed version.
127 * @param packageName The package name
128 * @return The package name and package version if it can be identified, null otherwise
129 */
130 public String getPackageDetails(String packageName) {
131 try {
132 String version = Utils.execOutput(Arrays.asList("dpkg-query", "--show", "--showformat", "${Architecture}-${Version}", packageName));
133 return packageName + ":" + version;
134 } catch (IOException e) {
135 Main.warn(e);
136 return null;
137 }
138 }
139
140 /**
141 * Get the Java package name including detailed version.
142 *
143 * Some Java bugs are specific to a certain security update, so in addition
144 * to the Java version, we also need the exact package version.
145 *
146 * This was originally written for #8921, so only Debian based distributions
147 * are covered at the moment. This can be extended to other distributions
148 * if needed.
149 *
150 * @return The package name and package version if it can be identified, null
151 * otherwise
152 */
153 public String getJavaPackageDetails() {
154 if (isDebianOrUbuntu()) {
155 String javaHome = System.getProperty("java.home");
156 if ("/usr/lib/jvm/java-6-openjdk-amd64/jre".equals(javaHome) ||
157 "/usr/lib/jvm/java-6-openjdk-i386/jre".equals(javaHome) ||
158 "/usr/lib/jvm/java-6-openjdk/jre".equals(javaHome)) {
159 return getPackageDetails("openjdk-6-jre");
160 }
161 if ("/usr/lib/jvm/java-7-openjdk-amd64/jre".equals(javaHome) ||
162 "/usr/lib/jvm/java-7-openjdk-i386/jre".equals(javaHome)) {
163 return getPackageDetails("openjdk-7-jre");
164 }
165 }
166 return null;
167 }
168
169 /**
170 * Get the Web Start package name including detailed version.
171 *
172 * Debian and Ubuntu OpenJDK packages are shipped with icedtea-web package,
173 * but its version does not match main java package version.
174 *
175 * Only Debian based distributions are covered at the moment.
176 * This can be extended to other distributions if needed.
177 *
178 * Simply return {@code null} if there's no separate package for Java WebStart.
179 *
180 * @return The package name and package version if it can be identified, null otherwise
181 */
182 public String getWebStartPackageDetails() {
183 if (isDebianOrUbuntu()) {
184 String javaHome = System.getProperty("java.home");
185 if (javaHome != null && javaHome.contains("openjdk")) {
186 return getPackageDetails("icedtea-netx");
187 }
188 }
189 return null;
190 }
191
192 protected String buildOSDescription() {
193 String osName = System.getProperty("os.name");
194 if ("Linux".equalsIgnoreCase(osName)) {
195 try {
196 // Try lsb_release (only available on LSB-compliant Linux systems, see https://www.linuxbase.org/lsb-cert/productdir.php?by_prod )
197 Process p = Runtime.getRuntime().exec("lsb_release -ds");
198 BufferedReader input = new BufferedReader(new InputStreamReader(p.getInputStream()));
199 String line = Utils.strip(input.readLine());
200 Utils.close(input);
201 if (line != null && !line.isEmpty()) {
202 line = line.replaceAll("\"+","");
203 line = line.replaceAll("NAME=",""); // strange code for some Gentoo's
204 if(line.startsWith("Linux ")) // e.g. Linux Mint
205 return line;
206 else if(!line.isEmpty())
207 return "Linux " + line;
208 }
209 } catch (IOException e) {
210 // Non LSB-compliant Linux system. List of common fallback release files: http://linuxmafia.com/faq/Admin/release-files.html
211 for (LinuxReleaseInfo info : new LinuxReleaseInfo[]{
212 new LinuxReleaseInfo("/etc/lsb-release", "DISTRIB_DESCRIPTION", "DISTRIB_ID", "DISTRIB_RELEASE"),
213 new LinuxReleaseInfo("/etc/os-release", "PRETTY_NAME", "NAME", "VERSION"),
214 new LinuxReleaseInfo("/etc/arch-release"),
215 new LinuxReleaseInfo("/etc/debian_version", "Debian GNU/Linux "),
216 new LinuxReleaseInfo("/etc/fedora-release"),
217 new LinuxReleaseInfo("/etc/gentoo-release"),
218 new LinuxReleaseInfo("/etc/redhat-release")
219 }) {
220 String description = info.extractDescription();
221 if (description != null && !description.isEmpty()) {
222 return "Linux " + description;
223 }
224 }
225 }
226 }
227 return osName;
228 }
229
230 @Override
231 public String getOSDescription() {
232 if (osDescription == null) {
233 osDescription = buildOSDescription();
234 }
235 return osDescription;
236 }
237
238 protected static class LinuxReleaseInfo {
239 private final String path;
240 private final String descriptionField;
241 private final String idField;
242 private final String releaseField;
243 private final boolean plainText;
244 private final String prefix;
245
246 public LinuxReleaseInfo(String path, String descriptionField, String idField, String releaseField) {
247 this(path, descriptionField, idField, releaseField, false, null);
248 }
249
250 public LinuxReleaseInfo(String path) {
251 this(path, null, null, null, true, null);
252 }
253
254 public LinuxReleaseInfo(String path, String prefix) {
255 this(path, null, null, null, true, prefix);
256 }
257
258 private LinuxReleaseInfo(String path, String descriptionField, String idField, String releaseField, boolean plainText, String prefix) {
259 this.path = path;
260 this.descriptionField = descriptionField;
261 this.idField = idField;
262 this.releaseField = releaseField;
263 this.plainText = plainText;
264 this.prefix = prefix;
265 }
266
267 @Override public String toString() {
268 return "ReleaseInfo [path=" + path + ", descriptionField=" + descriptionField +
269 ", idField=" + idField + ", releaseField=" + releaseField + "]";
270 }
271
272 /**
273 * Extracts OS detailed information from a Linux release file (/etc/xxx-release)
274 * @return The OS detailed information, or {@code null}
275 */
276 public String extractDescription() {
277 String result = null;
278 if (path != null) {
279 File file = new File(path);
280 if (file.exists()) {
281 BufferedReader reader = null;
282 try {
283 reader = new BufferedReader(new FileReader(file));
284 String id = null;
285 String release = null;
286 String line;
287 while (result == null && (line = reader.readLine()) != null) {
288 if (line.contains("=")) {
289 String[] tokens = line.split("=");
290 if (tokens.length >= 2) {
291 // Description, if available, contains exactly what we need
292 if (descriptionField != null && descriptionField.equalsIgnoreCase(tokens[0])) {
293 result = Utils.strip(tokens[1]);
294 } else if (idField != null && idField.equalsIgnoreCase(tokens[0])) {
295 id = Utils.strip(tokens[1]);
296 } else if (releaseField != null && releaseField.equalsIgnoreCase(tokens[0])) {
297 release = Utils.strip(tokens[1]);
298 }
299 }
300 } else if (plainText && !line.isEmpty()) {
301 // Files composed of a single line
302 result = Utils.strip(line);
303 }
304 }
305 // If no description has been found, try to rebuild it with "id" + "release" (i.e. "name" + "version")
306 if (result == null && id != null && release != null) {
307 result = id + " " + release;
308 }
309 } catch (IOException e) {
310 // Ignore
311 } finally {
312 Utils.close(reader);
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 try {
328 ExtendedDialog ed = new ExtendedDialog(
329 Main.parent,
330 tr("Outdated Java version"),
331 new String[]{tr("Update Java"), tr("Cancel")});
332 // Check if the dialog has not already been permanently hidden by user
333 if (!ed.toggleEnable("askUpdateJava7").toggleCheckState()) {
334 ed.setButtonIcons(new String[]{"java.png", "cancel.png"}).setCancelButton(2);
335 ed.setMinimumSize(new Dimension(460, 260));
336 ed.setIcon(JOptionPane.WARNING_MESSAGE);
337 ed.setContent(tr("You are running version {0} of Java.", "<b>"+version+"</b>")+"<br><br>"+
338 "<b>"+tr("This version is no longer supported by {0} since {1} and is not recommended for use.", "Oracle", tr("February 2013"))+"</b><br><br>"+
339 "<b>"+tr("JOSM will soon stop working with this version; we highly recommend you to update to Java {0}.", "7")+"</b><br><br>"+
340 tr("Would you like to update now ?"));
341
342 if (ed.showDialog().getValue() == 1) {
343 openUrl("http://www.java.com/download");
344 }
345 }
346 } catch (IOException e) {
347 Main.warn(e);
348 }
349 }
350}
Note: See TracBrowser for help on using the repository browser.