Debian Codegraph @

arduino-core/src/processing/app/PreferencesData.java

raw symbols SCIP
1 package processing.app;
2
3 import cc.arduino.Constants;
4 import cc.arduino.i18n.Languages;
5 import org.apache.commons.compress.utils.IOUtils;
6 import processing.app.helpers.PreferencesHelper;
7 import processing.app.helpers.PreferencesMap;
8 import processing.app.legacy.PApplet;
9 import processing.app.legacy.PConstants;
10
11 import java.awt.*;
12 import java.io.File;
13 import java.io.IOException;
14 import java.io.PrintWriter;
15 import java.util.Arrays;
16 import java.util.Collection;
17 import java.util.Iterator;
18 import java.util.MissingResourceException;
19 import java.util.stream.Collectors;
20
21 import static processing.app.I18n.format;
22 import static processing.app.I18n.tr;
23
24
25 public class PreferencesData {
26
27 private static final String PREFS_FILE = "preferences.txt";
28
29 // data model
30
31 static PreferencesMap defaults;
32 static PreferencesMap prefs = new PreferencesMap();
33 static File preferencesFile;
34 static boolean doSave = true;
35
36
37 static public void init(File file) throws Exception {
38 if (file == null) {
39 BaseNoGui.getPlatform().fixSettingsLocation();
40 }
41 if (file != null) {
42 preferencesFile = file;
43 } else {
44 preferencesFile = BaseNoGui.getSettingsFile(PREFS_FILE);
45 }
46
47 try {
48 BaseNoGui.getPlatform().fixPrefsFilePermissions(preferencesFile);
49 } catch (Exception e) {
50 //ignore
51 }
52
53 // Start with a clean slate
54 prefs = new PreferencesMap();
55
56 // start by loading the defaults, in case something
57 // important was deleted from the user prefs
58 try {
59 prefs.load(new File(BaseNoGui.getContentFile("lib"), PREFS_FILE));
60 } catch (IOException e) {
61 BaseNoGui.showError(null, tr("Could not read default settings.\n" +
62 "You'll need to reinstall Arduino."), e);
63 }
64
65 // set some runtime constants (not saved on preferences file)
66 File hardwareFolder = BaseNoGui.getHardwareFolder();
67 prefs.put("runtime.ide.path", hardwareFolder.getParentFile().getAbsolutePath());
68 prefs.put("runtime.ide.version", "" + BaseNoGui.REVISION);
69
70 // clone the hash table
71 defaults = new PreferencesMap(prefs);
72
73 if (preferencesFile.exists()) {
74 // load the previous preferences file
75 try {
76 prefs.load(preferencesFile);
77 } catch (IOException ex) {
78 BaseNoGui.showError(tr("Error reading preferences"),
79 I18n.format(tr("Error reading the preferences file. "
80 + "Please delete (or move)\n"
81 + "{0} and restart Arduino."),
82 preferencesFile.getAbsolutePath()), ex);
83 }
84 }
85
86 // load the I18n module for internationalization
87 String lang = get("editor.languages.current");
88 if (lang == null || !Languages.have(lang)) {
89 lang = "";
90 set("editor.languages.current", "");
91 }
92 try {
93 I18n.init(lang);
94 } catch (MissingResourceException e) {
95 I18n.init("en");
96 set("editor.languages.current", "en");
97 }
98
99 // set some other runtime constants (not saved on preferences file)
100 set("runtime.os", PConstants.platformNames[PApplet.platform]);
101
102 fixPreferences();
103 }
104
105 public static File getPreferencesFile() {
106 return preferencesFile;
107 }
108
109 private static void fixPreferences() {
110 String baud = get("serial.debug_rate");
111 if ("14400".equals(baud) || "28800".equals(baud)) {
112 set("serial.debug_rate", "9600");
113 }
114 }
115
116
117 static protected void save() {
118 if (!doSave)
119 return;
120
121 if (getBoolean("preferences.readonly"))
122 return;
123
124 // on startup, don't worry about it
125 // this is trying to update the prefs for who is open
126 // before Preferences.init() has been called.
127 if (preferencesFile == null) return;
128
129 // Fix for 0163 to properly use Unicode when writing preferences.txt
130 PrintWriter writer = null;
131 try {
132 writer = PApplet.createWriter(preferencesFile);
133
134 String[] keys = prefs.keySet().toArray(new String[0]);
135 Arrays.sort(keys);
136 for (String key : keys) {
137 if (key.startsWith("runtime."))
138 continue;
139 writer.println(key + "=" + prefs.get(key));
140 }
141
142 writer.flush();
143 } catch (Throwable e) {
144 System.err.println(format(tr("Could not write preferences file: {0}"), e.getMessage()));
145 return;
146 } finally {
147 IOUtils.closeQuietly(writer);
148 }
149
150 try {
151 BaseNoGui.getPlatform().fixPrefsFilePermissions(preferencesFile);
152 } catch (Exception e) {
153 //ignore
154 }
155 }
156
157
158 // .................................................................
159
160 static public String get(String attribute) {
161 return prefs.get(attribute);
162 }
163
164 static public String get(String attribute, String defaultValue) {
165 String value = get(attribute);
166 return (value == null) ? defaultValue : value;
167 }
168
169 static public String getNonEmpty(String attribute, String defaultValue) {
170 String value = get(attribute, defaultValue);
171 return ("".equals(value)) ? defaultValue : value;
172 }
173
174 public static boolean has(String key) {
175 return prefs.containsKey(key);
176 }
177
178 public static void remove(String key) {
179 prefs.remove(key);
180 }
181
182 static public String getDefault(String attribute) {
183 return defaults.get(attribute);
184 }
185
186
187 static public void set(String attribute, String value) {
188 prefs.put(attribute, value);
189 }
190
191
192 static public void unset(String attribute) {
193 prefs.remove(attribute);
194 }
195
196 static public boolean getBoolean(String attribute, boolean defaultValue) {
197 if (has(attribute)) {
198 return getBoolean(attribute);
199 }
200
201 return defaultValue;
202 }
203
204 static public boolean getBoolean(String attribute) {
205 return prefs.getBoolean(attribute);
206 }
207
208
209 static public void setBoolean(String attribute, boolean value) {
210 prefs.putBoolean(attribute, value);
211 }
212
213
214 static public int getInteger(String attribute) {
215 return Integer.parseInt(get(attribute));
216 }
217
218 static public int getInteger(String attribute, int defaultValue) {
219 if (has(attribute)) {
220 return getInteger(attribute);
221 }
222
223 return defaultValue;
224 }
225
226 static public void setInteger(String key, int value) {
227 set(key, String.valueOf(value));
228 }
229
230 static public float getFloat(String attribute, float defaultValue) {
231 if (has(attribute)) {
232 return getFloat(attribute);
233 }
234
235 return defaultValue;
236 }
237
238 static public float getFloat(String attribute) {
239 return Float.parseFloat(get(attribute));
240 }
241
242 // get a copy of the Preferences
243 static public PreferencesMap getMap() {
244 return new PreferencesMap(prefs);
245 }
246
247 static public void removeAllKeysWithPrefix(String prefix) {
248 Iterator<String> keys = prefs.keySet().iterator();
249 while (keys.hasNext())
250 if (keys.next().startsWith(prefix))
251 keys.remove();
252 }
253
254 // Decide wether changed preferences will be saved. When value is
255 // false, Preferences.save becomes a no-op.
256 static public void setDoSave(boolean value) {
257 doSave = value;
258 }
259
260 static public Font getFont(String attr) {
261 Font font = PreferencesHelper.getFont(prefs, attr);
262 if (font == null) {
263 String value = defaults.get(attr);
264 prefs.put(attr, value);
265 font = PreferencesHelper.getFont(prefs, attr);
266 }
267 return font;
268 }
269
270 public static Collection<String> getCollection(String key) {
271 return Arrays.stream(get(key, "").split(","))
272 // Remove empty strings from the collection
273 .filter((v) -> !v.trim().isEmpty())
274 .collect(Collectors.toList());
275 }
276
277 public static void setCollection(String key, Collection<String> values) {
278 String value = values.stream().collect(Collectors.joining(","));
279 set(key, value);
280 }
281
282 public static boolean areInsecurePackagesAllowed() {
283 if (getBoolean(Constants.ALLOW_INSECURE_PACKAGES, false)) {
284 return true;
285 }
286 return getBoolean(Constants.PREF_CONTRIBUTIONS_TRUST_ALL, false);
287 }
288 }