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

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

update license/copyright information

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