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