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

Last change on this file since 6798 was 6682, checked in by simon04, 10 years ago

fix #5629 #5108 #9568 - Make Unix web browsers configurable via browser.unix property

Default is ["xdg-open", "#DESKTOP#", "$BROWSER", "gnome-open", "kfmclient openURL", "firefox"].

  • Property svn:eol-style set to native
File size: 12.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.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 * Get the Java package name including detailed version.
113 *
114 * Some Java bugs are specific to a certain security update, so in addition
115 * to the Java version, we also need the exact package version.
116 *
117 * This was originally written for #8921, so only Debian based distributions
118 * are covered at the moment. This can be extended to other distributions
119 * if needed.
120 *
121 * @return The package name and package version if it can be identified, null
122 * otherwise
123 */
124 public String getJavaPackageDetails() {
125 try {
126 String dist = Utils.execOutput(Arrays.asList("lsb_release", "-i", "-s"));
127 if ("Debian".equalsIgnoreCase(dist) || "Ubuntu".equalsIgnoreCase(dist)) {
128 String javaHome = System.getProperty("java.home");
129 if ("/usr/lib/jvm/java-6-openjdk-amd64/jre".equals(javaHome) ||
130 "/usr/lib/jvm/java-6-openjdk-i386/jre".equals(javaHome) ||
131 "/usr/lib/jvm/java-6-openjdk/jre".equals(javaHome)) {
132 String version = Utils.execOutput(Arrays.asList("dpkg-query", "--show", "--showformat", "${Architecture}-${Version}", "openjdk-6-jre"));
133 return "openjdk-6-jre:" + version;
134 }
135 if ("/usr/lib/jvm/java-7-openjdk-amd64/jre".equals(javaHome) ||
136 "/usr/lib/jvm/java-7-openjdk-i386/jre".equals(javaHome)) {
137 String version = Utils.execOutput(Arrays.asList("dpkg-query", "--show", "--showformat", "${Architecture}-${Version}", "openjdk-7-jre"));
138 return "openjdk-7-jre:" + version;
139 }
140 }
141 } catch (IOException e) {
142 Main.warn(e);
143 }
144 return null;
145 }
146
147 protected String buildOSDescription() {
148 String osName = System.getProperty("os.name");
149 if ("Linux".equalsIgnoreCase(osName)) {
150 try {
151 // Try lsb_release (only available on LSB-compliant Linux systems, see https://www.linuxbase.org/lsb-cert/productdir.php?by_prod )
152 Process p = Runtime.getRuntime().exec("lsb_release -ds");
153 BufferedReader input = new BufferedReader(new InputStreamReader(p.getInputStream()));
154 String line = Utils.strip(input.readLine());
155 Utils.close(input);
156 if (line != null && !line.isEmpty()) {
157 line = line.replaceAll("\"+","");
158 line = line.replaceAll("NAME=",""); // strange code for some Gentoo's
159 if(line.startsWith("Linux ")) // e.g. Linux Mint
160 return line;
161 else if(!line.isEmpty())
162 return "Linux " + line;
163 }
164 } catch (IOException e) {
165 // Non LSB-compliant Linux system. List of common fallback release files: http://linuxmafia.com/faq/Admin/release-files.html
166 for (LinuxReleaseInfo info : new LinuxReleaseInfo[]{
167 new LinuxReleaseInfo("/etc/lsb-release", "DISTRIB_DESCRIPTION", "DISTRIB_ID", "DISTRIB_RELEASE"),
168 new LinuxReleaseInfo("/etc/os-release", "PRETTY_NAME", "NAME", "VERSION"),
169 new LinuxReleaseInfo("/etc/arch-release"),
170 new LinuxReleaseInfo("/etc/debian_version", "Debian GNU/Linux "),
171 new LinuxReleaseInfo("/etc/fedora-release"),
172 new LinuxReleaseInfo("/etc/gentoo-release"),
173 new LinuxReleaseInfo("/etc/redhat-release")
174 }) {
175 String description = info.extractDescription();
176 if (description != null && !description.isEmpty()) {
177 return "Linux " + description;
178 }
179 }
180 }
181 }
182 return osName;
183 }
184
185 @Override
186 public String getOSDescription() {
187 if (osDescription == null) {
188 osDescription = buildOSDescription();
189 }
190 return osDescription;
191 }
192
193 protected static class LinuxReleaseInfo {
194 private final String path;
195 private final String descriptionField;
196 private final String idField;
197 private final String releaseField;
198 private final boolean plainText;
199 private final String prefix;
200
201 public LinuxReleaseInfo(String path, String descriptionField, String idField, String releaseField) {
202 this(path, descriptionField, idField, releaseField, false, null);
203 }
204
205 public LinuxReleaseInfo(String path) {
206 this(path, null, null, null, true, null);
207 }
208
209 public LinuxReleaseInfo(String path, String prefix) {
210 this(path, null, null, null, true, prefix);
211 }
212
213 private LinuxReleaseInfo(String path, String descriptionField, String idField, String releaseField, boolean plainText, String prefix) {
214 this.path = path;
215 this.descriptionField = descriptionField;
216 this.idField = idField;
217 this.releaseField = releaseField;
218 this.plainText = plainText;
219 this.prefix = prefix;
220 }
221
222 @Override public String toString() {
223 return "ReleaseInfo [path=" + path + ", descriptionField=" + descriptionField +
224 ", idField=" + idField + ", releaseField=" + releaseField + "]";
225 }
226
227 /**
228 * Extracts OS detailed information from a Linux release file (/etc/xxx-release)
229 * @return The OS detailed information, or {@code null}
230 */
231 public String extractDescription() {
232 String result = null;
233 if (path != null) {
234 File file = new File(path);
235 if (file.exists()) {
236 BufferedReader reader = null;
237 try {
238 reader = new BufferedReader(new FileReader(file));
239 String id = null;
240 String release = null;
241 String line;
242 while (result == null && (line = reader.readLine()) != null) {
243 if (line.contains("=")) {
244 String[] tokens = line.split("=");
245 if (tokens.length >= 2) {
246 // Description, if available, contains exactly what we need
247 if (descriptionField != null && descriptionField.equalsIgnoreCase(tokens[0])) {
248 result = Utils.strip(tokens[1]);
249 } else if (idField != null && idField.equalsIgnoreCase(tokens[0])) {
250 id = Utils.strip(tokens[1]);
251 } else if (releaseField != null && releaseField.equalsIgnoreCase(tokens[0])) {
252 release = Utils.strip(tokens[1]);
253 }
254 }
255 } else if (plainText && !line.isEmpty()) {
256 // Files composed of a single line
257 result = Utils.strip(line);
258 }
259 }
260 // If no description has been found, try to rebuild it with "id" + "release" (i.e. "name" + "version")
261 if (result == null && id != null && release != null) {
262 result = id + " " + release;
263 }
264 } catch (IOException e) {
265 // Ignore
266 } finally {
267 Utils.close(reader);
268 }
269 }
270 }
271 // Append prefix if any
272 if (result != null && !result.isEmpty() && prefix != null && !prefix.isEmpty()) {
273 result = prefix + result;
274 }
275 if(result != null)
276 result = result.replaceAll("\"+","");
277 return result;
278 }
279 }
280
281 protected void askUpdateJava(String version) {
282 try {
283 ExtendedDialog ed = new ExtendedDialog(
284 Main.parent,
285 tr("Outdated Java version"),
286 new String[]{tr("Update Java"), tr("Cancel")});
287 // Check if the dialog has not already been permanently hidden by user
288 if (!ed.toggleEnable("askUpdateJava7").toggleCheckState()) {
289 ed.setButtonIcons(new String[]{"java.png", "cancel.png"}).setCancelButton(2);
290 ed.setMinimumSize(new Dimension(460, 260));
291 ed.setIcon(JOptionPane.WARNING_MESSAGE);
292 ed.setContent(tr("You are running version {0} of Java.", "<b>"+version+"</b>")+"<br><br>"+
293 "<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>"+
294 "<b>"+tr("JOSM will soon stop working with this version; we highly recommend you to update to Java {0}.", "7")+"</b><br><br>"+
295 tr("Would you like to update now ?"));
296
297 if (ed.showDialog().getValue() == 1) {
298 openUrl("http://www.java.com/download");
299 }
300 }
301 } catch (IOException e) {
302 Main.warn(e);
303 }
304 }
305}
Note: See TracBrowser for help on using the repository browser.