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

Last change on this file since 6340 was 6310, checked in by Don-vip, 11 years ago

Sonar/FindBugs - Nested blocks of code should not be left empty

  • Property svn:eol-style set to native
File size: 10.9 KB
Line 
1// License: GPL. Copyright 2007 by Immanuel Scholz and others
2package org.openstreetmap.josm.tools;
3
4import static org.openstreetmap.josm.tools.I18n.tr;
5
6import java.awt.GraphicsEnvironment;
7import java.awt.event.KeyEvent;
8import java.io.BufferedReader;
9import java.io.File;
10import java.io.FileReader;
11import java.io.IOException;
12import java.io.InputStreamReader;
13import java.util.Arrays;
14import java.util.List;
15
16import org.openstreetmap.josm.Main;
17
18/**
19 * see PlatformHook.java
20 *
21 * BTW: THIS IS A STUB. See comments below for details.
22 *
23 * Don't write (Main.platform instanceof PlatformHookUnixoid) because other platform
24 * hooks are subclasses of this class.
25 */
26public class PlatformHookUnixoid implements PlatformHook {
27
28 private String osDescription;
29
30 @Override
31 public void preStartupHook(){
32 }
33
34 @Override
35 public void startupHook() {
36 }
37
38 @Override
39 public void openUrl(String url) throws IOException {
40 String[] programs = {"gnome-open", "kfmclient openURL", "firefox"};
41 for (String program : programs) {
42 try {
43 Runtime.getRuntime().exec(program+" "+url);
44 return;
45 } catch (IOException e) {
46 Main.warn(e);
47 }
48 }
49 }
50
51 @Override
52 public void initSystemShortcuts() {
53 // TODO: Insert system shortcuts here. See Windows and especially OSX to see how to.
54 for(int i = KeyEvent.VK_F1; i <= KeyEvent.VK_F12; ++i)
55 Shortcut.registerSystemShortcut("screen:toogle"+i, tr("reserved"), i, KeyEvent.CTRL_DOWN_MASK | KeyEvent.ALT_DOWN_MASK).setAutomatic();
56 Shortcut.registerSystemShortcut("system:reset", tr("reserved"), KeyEvent.VK_DELETE, KeyEvent.CTRL_DOWN_MASK | KeyEvent.ALT_DOWN_MASK).setAutomatic();
57 Shortcut.registerSystemShortcut("system:resetX", tr("reserved"), KeyEvent.VK_BACK_SPACE, KeyEvent.CTRL_DOWN_MASK | KeyEvent.ALT_DOWN_MASK).setAutomatic();
58 }
59 /**
60 * This should work for all platforms. Yeah, should.
61 * See PlatformHook.java for a list of reasons why
62 * this is implemented here...
63 */
64 @Override
65 public String makeTooltip(String name, Shortcut sc) {
66 String result = "";
67 result += "<html>";
68 result += name;
69 if (sc != null && sc.getKeyText().length() != 0) {
70 result += " ";
71 result += "<font size='-2'>";
72 result += "("+sc.getKeyText()+")";
73 result += "</font>";
74 }
75 result += "&nbsp;</html>";
76 return result;
77 }
78
79 @Override
80 public String getDefaultStyle() {
81 return "javax.swing.plaf.metal.MetalLookAndFeel";
82 }
83
84 @Override
85 public boolean canFullscreen()
86 {
87 return GraphicsEnvironment.getLocalGraphicsEnvironment()
88 .getDefaultScreenDevice().isFullScreenSupported();
89 }
90
91 @Override
92 public boolean rename(File from, File to)
93 {
94 return from.renameTo(to);
95 }
96
97 /**
98 * Get the Java package name including detailed version.
99 *
100 * Some Java bugs are specific to a certain security update, so in addition
101 * to the Java version, we also need the exact package version.
102 *
103 * This was originally written for #8921, so only Debian based distributions
104 * are covered at the moment. This can be extended to other distributions
105 * if needed.
106 *
107 * @return The package name and package version if it can be identified, null
108 * otherwise
109 */
110 public String getJavaPackageDetails() {
111 try {
112 String dist = Utils.execOutput(Arrays.asList("lsb_release", "-i", "-s"));
113 if ("Debian".equalsIgnoreCase(dist) || "Ubuntu".equalsIgnoreCase(dist)) {
114 String javaHome = System.getProperty("java.home");
115 if ("/usr/lib/jvm/java-6-openjdk-amd64/jre".equals(javaHome) ||
116 "/usr/lib/jvm/java-6-openjdk-i386/jre".equals(javaHome) ||
117 "/usr/lib/jvm/java-6-openjdk/jre".equals(javaHome)) {
118 String version = Utils.execOutput(Arrays.asList("dpkg-query", "--show", "--showformat", "${Architecture}-${Version}", "openjdk-6-jre"));
119 return "openjdk-6-jre:" + version;
120 }
121 if ("/usr/lib/jvm/java-7-openjdk-amd64/jre".equals(javaHome) ||
122 "/usr/lib/jvm/java-7-openjdk-i386/jre".equals(javaHome)) {
123 String version = Utils.execOutput(Arrays.asList("dpkg-query", "--show", "--showformat", "${Architecture}-${Version}", "openjdk-7-jre"));
124 return "openjdk-7-jre:" + version;
125 }
126 }
127 } catch (IOException e) {
128 Main.warn(e);
129 }
130 return null;
131 }
132
133 protected String buildOSDescription() {
134 String osName = System.getProperty("os.name");
135 if ("Linux".equalsIgnoreCase(osName)) {
136 try {
137 // Try lsb_release (only available on LSB-compliant Linux systems, see https://www.linuxbase.org/lsb-cert/productdir.php?by_prod )
138 Process p = Runtime.getRuntime().exec("lsb_release -ds");
139 BufferedReader input = new BufferedReader(new InputStreamReader(p.getInputStream()));
140 String line = Utils.strip(input.readLine());
141 Utils.close(input);
142 if (line != null && !line.isEmpty()) {
143 line = line.replaceAll("\"+","");
144 line = line.replaceAll("NAME=",""); // strange code for some Gentoo's
145 if(line.startsWith("Linux ")) // e.g. Linux Mint
146 return line;
147 else if(!line.isEmpty())
148 return "Linux " + line;
149 }
150 } catch (IOException e) {
151 // Non LSB-compliant Linux system. List of common fallback release files: http://linuxmafia.com/faq/Admin/release-files.html
152 for (LinuxReleaseInfo info : new LinuxReleaseInfo[]{
153 new LinuxReleaseInfo("/etc/lsb-release", "DISTRIB_DESCRIPTION", "DISTRIB_ID", "DISTRIB_RELEASE"),
154 new LinuxReleaseInfo("/etc/os-release", "PRETTY_NAME", "NAME", "VERSION"),
155 new LinuxReleaseInfo("/etc/arch-release"),
156 new LinuxReleaseInfo("/etc/debian_version", "Debian GNU/Linux "),
157 new LinuxReleaseInfo("/etc/fedora-release"),
158 new LinuxReleaseInfo("/etc/gentoo-release"),
159 new LinuxReleaseInfo("/etc/redhat-release")
160 }) {
161 String description = info.extractDescription();
162 if (description != null && !description.isEmpty()) {
163 return "Linux " + description;
164 }
165 }
166 }
167 }
168 return osName;
169 }
170
171 @Override
172 public String getOSDescription() {
173 if (osDescription == null) {
174 osDescription = buildOSDescription();
175 }
176 return osDescription;
177 }
178
179 protected static class LinuxReleaseInfo {
180 private final String path;
181 private final String descriptionField;
182 private final String idField;
183 private final String releaseField;
184 private final boolean plainText;
185 private final String prefix;
186
187 public LinuxReleaseInfo(String path, String descriptionField, String idField, String releaseField) {
188 this(path, descriptionField, idField, releaseField, false, null);
189 }
190
191 public LinuxReleaseInfo(String path) {
192 this(path, null, null, null, true, null);
193 }
194
195 public LinuxReleaseInfo(String path, String prefix) {
196 this(path, null, null, null, true, prefix);
197 }
198
199 private LinuxReleaseInfo(String path, String descriptionField, String idField, String releaseField, boolean plainText, String prefix) {
200 this.path = path;
201 this.descriptionField = descriptionField;
202 this.idField = idField;
203 this.releaseField = releaseField;
204 this.plainText = plainText;
205 this.prefix = prefix;
206 }
207
208 @Override public String toString() {
209 return "ReleaseInfo [path=" + path + ", descriptionField=" + descriptionField +
210 ", idField=" + idField + ", releaseField=" + releaseField + "]";
211 }
212
213 /**
214 * Extracts OS detailed information from a Linux release file (/etc/xxx-release)
215 * @return The OS detailed information, or {@code null}
216 */
217 public String extractDescription() {
218 String result = null;
219 if (path != null) {
220 File file = new File(path);
221 if (file.exists()) {
222 BufferedReader reader = null;
223 try {
224 reader = new BufferedReader(new FileReader(file));
225 String id = null;
226 String release = null;
227 String line;
228 while (result == null && (line = reader.readLine()) != null) {
229 if (line.contains("=")) {
230 String[] tokens = line.split("=");
231 if (tokens.length >= 2) {
232 // Description, if available, contains exactly what we need
233 if (descriptionField != null && descriptionField.equalsIgnoreCase(tokens[0])) {
234 result = Utils.strip(tokens[1]);
235 } else if (idField != null && idField.equalsIgnoreCase(tokens[0])) {
236 id = Utils.strip(tokens[1]);
237 } else if (releaseField != null && releaseField.equalsIgnoreCase(tokens[0])) {
238 release = Utils.strip(tokens[1]);
239 }
240 }
241 } else if (plainText && !line.isEmpty()) {
242 // Files composed of a single line
243 result = Utils.strip(line);
244 }
245 }
246 // If no description has been found, try to rebuild it with "id" + "release" (i.e. "name" + "version")
247 if (result == null && id != null && release != null) {
248 result = id + " " + release;
249 }
250 } catch (IOException e) {
251 // Ignore
252 } finally {
253 Utils.close(reader);
254 }
255 }
256 }
257 // Append prefix if any
258 if (result != null && !result.isEmpty() && prefix != null && !prefix.isEmpty()) {
259 result = prefix + result;
260 }
261 if(result != null)
262 result = result.replaceAll("\"+","");
263 return result;
264 }
265 }
266}
Note: See TracBrowser for help on using the repository browser.