2014-04-06 00:29:20 +02:00
|
|
|
package processing.app;
|
|
|
|
|
2015-06-01 17:08:10 +02:00
|
|
|
import com.google.common.base.Joiner;
|
|
|
|
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;
|
2014-04-06 00:29:20 +02:00
|
|
|
|
2015-05-05 10:02:12 +02:00
|
|
|
import java.awt.*;
|
2014-04-06 00:29:20 +02:00
|
|
|
import java.io.File;
|
|
|
|
import java.io.IOException;
|
|
|
|
import java.io.PrintWriter;
|
|
|
|
import java.util.Arrays;
|
2015-06-01 17:08:10 +02:00
|
|
|
import java.util.Collection;
|
2015-02-16 12:42:48 +01:00
|
|
|
import java.util.Iterator;
|
2014-04-06 00:29:20 +02:00
|
|
|
import java.util.MissingResourceException;
|
|
|
|
|
2015-06-01 17:08:10 +02:00
|
|
|
import static processing.app.I18n._;
|
2014-04-06 00:29:20 +02:00
|
|
|
|
|
|
|
|
|
|
|
public class PreferencesData {
|
|
|
|
|
2015-03-17 15:48:25 +01:00
|
|
|
private static final String PREFS_FILE = "preferences.txt";
|
2014-04-06 00:29:20 +02:00
|
|
|
|
|
|
|
// data model
|
|
|
|
|
|
|
|
static PreferencesMap defaults;
|
|
|
|
static PreferencesMap prefs = new PreferencesMap();
|
|
|
|
static File preferencesFile;
|
|
|
|
static boolean doSave = true;
|
|
|
|
|
|
|
|
|
2015-06-23 15:58:11 +02:00
|
|
|
static public void init(File file) throws IOException {
|
|
|
|
if (file == null) {
|
|
|
|
BaseNoGui.getPlatform().fixSettingsLocation();
|
|
|
|
}
|
2015-03-17 15:48:25 +01:00
|
|
|
if (file != null) {
|
2014-04-06 00:29:20 +02:00
|
|
|
preferencesFile = file;
|
2015-03-17 15:48:25 +01:00
|
|
|
} else {
|
2014-08-21 19:47:33 +02:00
|
|
|
preferencesFile = BaseNoGui.getSettingsFile(PREFS_FILE);
|
2015-03-17 15:48:25 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
|
|
|
BaseNoGui.getPlatform().fixPrefsFilePermissions(preferencesFile);
|
|
|
|
} catch (Exception e) {
|
|
|
|
//ignore
|
|
|
|
}
|
2014-04-06 00:29:20 +02:00
|
|
|
|
|
|
|
// start by loading the defaults, in case something
|
|
|
|
// important was deleted from the user prefs
|
|
|
|
try {
|
2015-05-04 15:44:34 +02:00
|
|
|
prefs.load(new File(BaseNoGui.getContentFile("lib"), PREFS_FILE));
|
2014-04-06 00:29:20 +02:00
|
|
|
} catch (IOException e) {
|
2014-08-21 19:47:33 +02:00
|
|
|
BaseNoGui.showError(null, _("Could not read default settings.\n" +
|
2015-06-01 17:08:10 +02:00
|
|
|
"You'll need to reinstall Arduino."), e);
|
2014-04-06 00:29:20 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// set some runtime constants (not saved on preferences file)
|
2014-08-21 19:47:33 +02:00
|
|
|
File hardwareFolder = BaseNoGui.getHardwareFolder();
|
2014-04-06 00:29:20 +02:00
|
|
|
prefs.put("runtime.ide.path", hardwareFolder.getParentFile().getAbsolutePath());
|
2014-08-21 19:47:33 +02:00
|
|
|
prefs.put("runtime.ide.version", "" + BaseNoGui.REVISION);
|
2015-06-01 17:08:10 +02:00
|
|
|
|
2014-04-06 00:29:20 +02:00
|
|
|
// clone the hash table
|
|
|
|
defaults = new PreferencesMap(prefs);
|
|
|
|
|
|
|
|
if (preferencesFile.exists()) {
|
|
|
|
// load the previous preferences file
|
|
|
|
try {
|
|
|
|
prefs.load(preferencesFile);
|
|
|
|
} catch (IOException ex) {
|
2014-08-21 19:47:33 +02:00
|
|
|
BaseNoGui.showError(_("Error reading preferences"),
|
2015-06-01 17:08:10 +02:00
|
|
|
I18n.format(_("Error reading the preferences file. "
|
|
|
|
+ "Please delete (or move)\n"
|
|
|
|
+ "{0} and restart Arduino."),
|
|
|
|
preferencesFile.getAbsolutePath()), ex);
|
2014-04-06 00:29:20 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// load the I18n module for internationalization
|
|
|
|
try {
|
|
|
|
I18n.init(get("editor.languages.current"));
|
|
|
|
} 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();
|
|
|
|
}
|
|
|
|
|
2015-05-15 15:23:23 +02:00
|
|
|
public static File getPreferencesFile() {
|
|
|
|
return preferencesFile;
|
|
|
|
}
|
|
|
|
|
2014-04-06 00:29:20 +02:00
|
|
|
private static void fixPreferences() {
|
|
|
|
String baud = get("serial.debug_rate");
|
2014-12-04 13:10:01 +01:00
|
|
|
if ("14400".equals(baud) || "28800".equals(baud)) {
|
2014-04-06 00:29:20 +02:00
|
|
|
set("serial.debug_rate", "9600");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static protected void save() {
|
|
|
|
if (!doSave)
|
|
|
|
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
|
2015-05-04 15:44:34 +02:00
|
|
|
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));
|
|
|
|
}
|
2014-04-06 00:29:20 +02:00
|
|
|
|
2015-05-04 15:44:34 +02:00
|
|
|
writer.flush();
|
|
|
|
} finally {
|
2015-05-21 16:47:50 +02:00
|
|
|
IOUtils.closeQuietly(writer);
|
2015-05-04 15:44:34 +02:00
|
|
|
}
|
2015-03-17 15:48:25 +01:00
|
|
|
|
|
|
|
try {
|
|
|
|
BaseNoGui.getPlatform().fixPrefsFilePermissions(preferencesFile);
|
|
|
|
} catch (Exception e) {
|
|
|
|
//ignore
|
|
|
|
}
|
2014-04-06 00:29:20 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// .................................................................
|
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2015-06-22 11:00:50 +02:00
|
|
|
static public String getNonEmpty(String attribute, String defaultValue) {
|
|
|
|
String value = get(attribute, defaultValue);
|
|
|
|
return ("".equals(value)) ? defaultValue : value;
|
|
|
|
}
|
|
|
|
|
2014-04-06 00:29:20 +02:00
|
|
|
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) {
|
|
|
|
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));
|
|
|
|
}
|
|
|
|
|
2015-05-05 10:02:12 +02:00
|
|
|
static public int getInteger(String attribute, int defaultValue) {
|
|
|
|
if (has(attribute)) {
|
|
|
|
return getInteger(attribute);
|
|
|
|
}
|
|
|
|
|
|
|
|
return defaultValue;
|
|
|
|
}
|
2014-04-06 00:29:20 +02:00
|
|
|
|
|
|
|
static public void setInteger(String key, int value) {
|
|
|
|
set(key, String.valueOf(value));
|
|
|
|
}
|
|
|
|
|
2015-06-22 10:35:28 +02:00
|
|
|
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));
|
|
|
|
}
|
|
|
|
|
2014-04-06 00:29:20 +02:00
|
|
|
// get a copy of the Preferences
|
2015-06-01 17:08:10 +02:00
|
|
|
static public PreferencesMap getMap() {
|
2014-04-06 00:29:20 +02:00
|
|
|
return new PreferencesMap(prefs);
|
|
|
|
}
|
|
|
|
|
2015-02-16 12:42:48 +01:00
|
|
|
static public void removeAllKeysWithPrefix(String prefix) {
|
|
|
|
Iterator<String> keys = prefs.keySet().iterator();
|
|
|
|
while (keys.hasNext())
|
|
|
|
if (keys.next().startsWith(prefix))
|
|
|
|
keys.remove();
|
|
|
|
}
|
|
|
|
|
2014-04-06 00:29:20 +02:00
|
|
|
// Decide wether changed preferences will be saved. When value is
|
|
|
|
// false, Preferences.save becomes a no-op.
|
2015-06-01 17:08:10 +02:00
|
|
|
static public void setDoSave(boolean value) {
|
2014-04-06 00:29:20 +02:00
|
|
|
doSave = value;
|
|
|
|
}
|
2015-05-05 10:02:12 +02:00
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
2015-06-01 17:08:10 +02:00
|
|
|
|
|
|
|
public static Collection<String> getCollection(String key) {
|
|
|
|
return Arrays.asList(get(key, "").split(","));
|
|
|
|
}
|
|
|
|
|
|
|
|
public static void setCollection(String key, Collection<String> values) {
|
|
|
|
String value = Joiner.on(',').join(values);
|
|
|
|
set(key, value);
|
|
|
|
}
|
2014-04-06 00:29:20 +02:00
|
|
|
}
|