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

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

fix #8770 - Call lsb-release only once

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