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

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

see #8616 - Attempt to determine OS detailed version on some non LSB-compliant Linux systems

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