source: josm/trunk/src/org/openstreetmap/josm/data/Preferences.java@ 11296

Last change on this file since 11296 was 11288, checked in by simon04, 7 years ago

see #13376 - Use TimeUnit instead of combinations of 1000/60/60/24

  • Property svn:eol-style set to native
File size: 62.1 KB
Line 
1// License: GPL. For details, see LICENSE file.
2package org.openstreetmap.josm.data;
3
4import static org.openstreetmap.josm.tools.I18n.marktr;
5import static org.openstreetmap.josm.tools.I18n.tr;
6
7import java.awt.Color;
8import java.awt.GraphicsEnvironment;
9import java.awt.Toolkit;
10import java.io.File;
11import java.io.IOException;
12import java.io.PrintWriter;
13import java.io.Reader;
14import java.io.StringReader;
15import java.io.StringWriter;
16import java.lang.annotation.Retention;
17import java.lang.annotation.RetentionPolicy;
18import java.lang.reflect.Field;
19import java.nio.charset.StandardCharsets;
20import java.util.AbstractMap;
21import java.util.ArrayList;
22import java.util.Collection;
23import java.util.Collections;
24import java.util.HashMap;
25import java.util.HashSet;
26import java.util.Iterator;
27import java.util.LinkedHashMap;
28import java.util.LinkedList;
29import java.util.List;
30import java.util.Map;
31import java.util.Map.Entry;
32import java.util.MissingResourceException;
33import java.util.Objects;
34import java.util.ResourceBundle;
35import java.util.Set;
36import java.util.SortedMap;
37import java.util.TreeMap;
38import java.util.concurrent.TimeUnit;
39import java.util.function.Predicate;
40import java.util.regex.Matcher;
41import java.util.regex.Pattern;
42import java.util.stream.Collectors;
43import java.util.stream.Stream;
44
45import javax.json.Json;
46import javax.json.JsonArray;
47import javax.json.JsonArrayBuilder;
48import javax.json.JsonObject;
49import javax.json.JsonObjectBuilder;
50import javax.json.JsonReader;
51import javax.json.JsonString;
52import javax.json.JsonValue;
53import javax.json.JsonWriter;
54import javax.swing.JOptionPane;
55import javax.xml.stream.XMLStreamException;
56
57import org.openstreetmap.josm.Main;
58import org.openstreetmap.josm.data.preferences.BooleanProperty;
59import org.openstreetmap.josm.data.preferences.ColorProperty;
60import org.openstreetmap.josm.data.preferences.DoubleProperty;
61import org.openstreetmap.josm.data.preferences.IntegerProperty;
62import org.openstreetmap.josm.data.preferences.ListListSetting;
63import org.openstreetmap.josm.data.preferences.ListSetting;
64import org.openstreetmap.josm.data.preferences.LongProperty;
65import org.openstreetmap.josm.data.preferences.MapListSetting;
66import org.openstreetmap.josm.data.preferences.PreferencesReader;
67import org.openstreetmap.josm.data.preferences.PreferencesWriter;
68import org.openstreetmap.josm.data.preferences.Setting;
69import org.openstreetmap.josm.data.preferences.StringSetting;
70import org.openstreetmap.josm.io.OfflineAccessException;
71import org.openstreetmap.josm.io.OnlineResource;
72import org.openstreetmap.josm.tools.CheckParameterUtil;
73import org.openstreetmap.josm.tools.ColorHelper;
74import org.openstreetmap.josm.tools.I18n;
75import org.openstreetmap.josm.tools.ListenerList;
76import org.openstreetmap.josm.tools.MultiMap;
77import org.openstreetmap.josm.tools.Utils;
78import org.xml.sax.SAXException;
79
80/**
81 * This class holds all preferences for JOSM.
82 *
83 * Other classes can register their beloved properties here. All properties will be
84 * saved upon set-access.
85 *
86 * Each property is a key=setting pair, where key is a String and setting can be one of
87 * 4 types:
88 * string, list, list of lists and list of maps.
89 * In addition, each key has a unique default value that is set when the value is first
90 * accessed using one of the get...() methods. You can use the same preference
91 * key in different parts of the code, but the default value must be the same
92 * everywhere. A default value of null means, the setting has been requested, but
93 * no default value was set. This is used in advanced preferences to present a list
94 * off all possible settings.
95 *
96 * At the moment, you cannot put the empty string for string properties.
97 * put(key, "") means, the property is removed.
98 *
99 * @author imi
100 * @since 74
101 */
102public class Preferences {
103
104 private static final String[] OBSOLETE_PREF_KEYS = {
105 "hdop.factor" /* remove entry after April 2017 */
106 };
107
108 private static final long MAX_AGE_DEFAULT_PREFERENCES = TimeUnit.DAYS.toSeconds(50);
109
110 /**
111 * Internal storage for the preference directory.
112 * Do not access this variable directly!
113 * @see #getPreferencesDirectory()
114 */
115 private File preferencesDir;
116
117 /**
118 * Internal storage for the cache directory.
119 */
120 private File cacheDir;
121
122 /**
123 * Internal storage for the user data directory.
124 */
125 private File userdataDir;
126
127 /**
128 * Determines if preferences file is saved each time a property is changed.
129 */
130 private boolean saveOnPut = true;
131
132 /**
133 * Maps the setting name to the current value of the setting.
134 * The map must not contain null as key or value. The mapped setting objects
135 * must not have a null value.
136 */
137 protected final SortedMap<String, Setting<?>> settingsMap = new TreeMap<>();
138
139 /**
140 * Maps the setting name to the default value of the setting.
141 * The map must not contain null as key or value. The value of the mapped
142 * setting objects can be null.
143 */
144 protected final SortedMap<String, Setting<?>> defaultsMap = new TreeMap<>();
145
146 private final Predicate<Entry<String, Setting<?>>> NO_DEFAULT_SETTINGS_ENTRY =
147 e -> !e.getValue().equals(defaultsMap.get(e.getKey()));
148
149 /**
150 * Maps color keys to human readable color name
151 */
152 protected final SortedMap<String, String> colornames = new TreeMap<>();
153
154 /**
155 * Indicates whether {@link #init(boolean)} completed successfully.
156 * Used to decide whether to write backup preference file in {@link #save()}
157 */
158 protected boolean initSuccessful;
159
160 /**
161 * Event triggered when a preference entry value changes.
162 */
163 public interface PreferenceChangeEvent {
164 /**
165 * Returns the preference key.
166 * @return the preference key
167 */
168 String getKey();
169
170 /**
171 * Returns the old preference value.
172 * @return the old preference value
173 */
174 Setting<?> getOldValue();
175
176 /**
177 * Returns the new preference value.
178 * @return the new preference value
179 */
180 Setting<?> getNewValue();
181 }
182
183 /**
184 * Listener to preference change events.
185 * @since 10600 (functional interface)
186 */
187 @FunctionalInterface
188 public interface PreferenceChangedListener {
189 /**
190 * Trigerred when a preference entry value changes.
191 * @param e the preference change event
192 */
193 void preferenceChanged(PreferenceChangeEvent e);
194 }
195
196 private static class DefaultPreferenceChangeEvent implements PreferenceChangeEvent {
197 private final String key;
198 private final Setting<?> oldValue;
199 private final Setting<?> newValue;
200
201 DefaultPreferenceChangeEvent(String key, Setting<?> oldValue, Setting<?> newValue) {
202 this.key = key;
203 this.oldValue = oldValue;
204 this.newValue = newValue;
205 }
206
207 @Override
208 public String getKey() {
209 return key;
210 }
211
212 @Override
213 public Setting<?> getOldValue() {
214 return oldValue;
215 }
216
217 @Override
218 public Setting<?> getNewValue() {
219 return newValue;
220 }
221 }
222
223 private final ListenerList<PreferenceChangedListener> listeners = ListenerList.create();
224
225 private final HashMap<String, ListenerList<PreferenceChangedListener>> keyListeners = new HashMap<>();
226
227 /**
228 * Adds a new preferences listener.
229 * @param listener The listener to add
230 */
231 public void addPreferenceChangeListener(PreferenceChangedListener listener) {
232 if (listener != null) {
233 listeners.addListener(listener);
234 }
235 }
236
237 /**
238 * Removes a preferences listener.
239 * @param listener The listener to remove
240 */
241 public void removePreferenceChangeListener(PreferenceChangedListener listener) {
242 listeners.removeListener(listener);
243 }
244
245 /**
246 * Adds a listener that only listens to changes in one preference
247 * @param key The preference key to listen to
248 * @param listener The listener to add.
249 * @since 10824
250 */
251 public void addKeyPreferenceChangeListener(String key, PreferenceChangedListener listener) {
252 listenersForKey(key).addListener(listener);
253 }
254
255 /**
256 * Adds a weak listener that only listens to changes in one preference
257 * @param key The preference key to listen to
258 * @param listener The listener to add.
259 * @since 10824
260 */
261 public void addWeakKeyPreferenceChangeListener(String key, PreferenceChangedListener listener) {
262 listenersForKey(key).addWeakListener(listener);
263 }
264
265 private ListenerList<PreferenceChangedListener> listenersForKey(String key) {
266 ListenerList<PreferenceChangedListener> keyListener = keyListeners.get(key);
267 if (keyListener == null) {
268 keyListener = ListenerList.create();
269 keyListeners.put(key, keyListener);
270 }
271 return keyListener;
272 }
273
274 /**
275 * Removes a listener that only listens to changes in one preference
276 * @param key The preference key to listen to
277 * @param listener The listener to add.
278 */
279 public void removeKeyPreferenceChangeListener(String key, PreferenceChangedListener listener) {
280 ListenerList<PreferenceChangedListener> keyListener = keyListeners.get(key);
281 if (keyListener == null) {
282 throw new IllegalArgumentException("There are no listeners registered for " + key);
283 }
284 keyListener.removeListener(listener);
285 }
286
287 protected void firePreferenceChanged(String key, Setting<?> oldValue, Setting<?> newValue) {
288 final PreferenceChangeEvent evt = new DefaultPreferenceChangeEvent(key, oldValue, newValue);
289 listeners.fireEvent(listener -> listener.preferenceChanged(evt));
290
291 ListenerList<PreferenceChangedListener> forKey = keyListeners.get(key);
292 if (forKey != null) {
293 forKey.fireEvent(listener -> listener.preferenceChanged(evt));
294 }
295 }
296
297 /**
298 * Get the base name of the JOSM directories for preferences, cache and
299 * user data.
300 * Default value is "JOSM", unless overridden by system property "josm.dir.name".
301 * @return the base name of the JOSM directories for preferences, cache and
302 * user data
303 */
304 public String getJOSMDirectoryBaseName() {
305 String name = System.getProperty("josm.dir.name");
306 if (name != null)
307 return name;
308 else
309 return "JOSM";
310 }
311
312 /**
313 * Returns the user defined preferences directory, containing the preferences.xml file
314 * @return The user defined preferences directory, containing the preferences.xml file
315 * @since 7834
316 */
317 public File getPreferencesDirectory() {
318 if (preferencesDir != null)
319 return preferencesDir;
320 String path;
321 path = System.getProperty("josm.pref");
322 if (path != null) {
323 preferencesDir = new File(path).getAbsoluteFile();
324 } else {
325 path = System.getProperty("josm.home");
326 if (path != null) {
327 preferencesDir = new File(path).getAbsoluteFile();
328 } else {
329 preferencesDir = Main.platform.getDefaultPrefDirectory();
330 }
331 }
332 return preferencesDir;
333 }
334
335 /**
336 * Returns the user data directory, containing autosave, plugins, etc.
337 * Depending on the OS it may be the same directory as preferences directory.
338 * @return The user data directory, containing autosave, plugins, etc.
339 * @since 7834
340 */
341 public File getUserDataDirectory() {
342 if (userdataDir != null)
343 return userdataDir;
344 String path;
345 path = System.getProperty("josm.userdata");
346 if (path != null) {
347 userdataDir = new File(path).getAbsoluteFile();
348 } else {
349 path = System.getProperty("josm.home");
350 if (path != null) {
351 userdataDir = new File(path).getAbsoluteFile();
352 } else {
353 userdataDir = Main.platform.getDefaultUserDataDirectory();
354 }
355 }
356 return userdataDir;
357 }
358
359 /**
360 * Returns the user preferences file (preferences.xml).
361 * @return The user preferences file (preferences.xml)
362 */
363 public File getPreferenceFile() {
364 return new File(getPreferencesDirectory(), "preferences.xml");
365 }
366
367 /**
368 * Returns the cache file for default preferences.
369 * @return the cache file for default preferences
370 */
371 public File getDefaultsCacheFile() {
372 return new File(getCacheDirectory(), "default_preferences.xml");
373 }
374
375 /**
376 * Returns the user plugin directory.
377 * @return The user plugin directory
378 */
379 public File getPluginsDirectory() {
380 return new File(getUserDataDirectory(), "plugins");
381 }
382
383 /**
384 * Get the directory where cached content of any kind should be stored.
385 *
386 * If the directory doesn't exist on the file system, it will be created by this method.
387 *
388 * @return the cache directory
389 */
390 public File getCacheDirectory() {
391 if (cacheDir != null)
392 return cacheDir;
393 String path = System.getProperty("josm.cache");
394 if (path != null) {
395 cacheDir = new File(path).getAbsoluteFile();
396 } else {
397 path = System.getProperty("josm.home");
398 if (path != null) {
399 cacheDir = new File(path, "cache");
400 } else {
401 path = get("cache.folder", null);
402 if (path != null) {
403 cacheDir = new File(path).getAbsoluteFile();
404 } else {
405 cacheDir = Main.platform.getDefaultCacheDirectory();
406 }
407 }
408 }
409 if (!cacheDir.exists() && !cacheDir.mkdirs()) {
410 Main.warn(tr("Failed to create missing cache directory: {0}", cacheDir.getAbsoluteFile()));
411 JOptionPane.showMessageDialog(
412 Main.parent,
413 tr("<html>Failed to create missing cache directory: {0}</html>", cacheDir.getAbsoluteFile()),
414 tr("Error"),
415 JOptionPane.ERROR_MESSAGE
416 );
417 }
418 return cacheDir;
419 }
420
421 private static void addPossibleResourceDir(Set<String> locations, String s) {
422 if (s != null) {
423 if (!s.endsWith(File.separator)) {
424 s += File.separator;
425 }
426 locations.add(s);
427 }
428 }
429
430 /**
431 * Returns a set of all existing directories where resources could be stored.
432 * @return A set of all existing directories where resources could be stored.
433 */
434 public Collection<String> getAllPossiblePreferenceDirs() {
435 Set<String> locations = new HashSet<>();
436 addPossibleResourceDir(locations, getPreferencesDirectory().getPath());
437 addPossibleResourceDir(locations, getUserDataDirectory().getPath());
438 addPossibleResourceDir(locations, System.getenv("JOSM_RESOURCES"));
439 addPossibleResourceDir(locations, System.getProperty("josm.resources"));
440 if (Main.isPlatformWindows()) {
441 String appdata = System.getenv("APPDATA");
442 if (System.getenv("ALLUSERSPROFILE") != null && appdata != null
443 && appdata.lastIndexOf(File.separator) != -1) {
444 appdata = appdata.substring(appdata.lastIndexOf(File.separator));
445 locations.add(new File(new File(System.getenv("ALLUSERSPROFILE"),
446 appdata), "JOSM").getPath());
447 }
448 } else {
449 locations.add("/usr/local/share/josm/");
450 locations.add("/usr/local/lib/josm/");
451 locations.add("/usr/share/josm/");
452 locations.add("/usr/lib/josm/");
453 }
454 return locations;
455 }
456
457 /**
458 * Get settings value for a certain key.
459 * @param key the identifier for the setting
460 * @return "" if there is nothing set for the preference key, the corresponding value otherwise. The result is not null.
461 */
462 public synchronized String get(final String key) {
463 String value = get(key, null);
464 return value == null ? "" : value;
465 }
466
467 /**
468 * Get settings value for a certain key and provide default a value.
469 * @param key the identifier for the setting
470 * @param def the default value. For each call of get() with a given key, the default value must be the same.
471 * @return the corresponding value if the property has been set before, {@code def} otherwise
472 */
473 public synchronized String get(final String key, final String def) {
474 return getSetting(key, new StringSetting(def), StringSetting.class).getValue();
475 }
476
477 public synchronized Map<String, String> getAllPrefix(final String prefix) {
478 final Map<String, String> all = new TreeMap<>();
479 for (final Entry<String, Setting<?>> e : settingsMap.entrySet()) {
480 if (e.getKey().startsWith(prefix) && (e.getValue() instanceof StringSetting)) {
481 all.put(e.getKey(), ((StringSetting) e.getValue()).getValue());
482 }
483 }
484 return all;
485 }
486
487 public synchronized List<String> getAllPrefixCollectionKeys(final String prefix) {
488 final List<String> all = new LinkedList<>();
489 for (Map.Entry<String, Setting<?>> entry : settingsMap.entrySet()) {
490 if (entry.getKey().startsWith(prefix) && entry.getValue() instanceof ListSetting) {
491 all.add(entry.getKey());
492 }
493 }
494 return all;
495 }
496
497 public synchronized Map<String, String> getAllColors() {
498 final Map<String, String> all = new TreeMap<>();
499 for (final Entry<String, Setting<?>> e : defaultsMap.entrySet()) {
500 if (e.getKey().startsWith("color.") && e.getValue() instanceof StringSetting) {
501 StringSetting d = (StringSetting) e.getValue();
502 if (d.getValue() != null) {
503 all.put(e.getKey().substring(6), d.getValue());
504 }
505 }
506 }
507 for (final Entry<String, Setting<?>> e : settingsMap.entrySet()) {
508 if (e.getKey().startsWith("color.") && (e.getValue() instanceof StringSetting)) {
509 all.put(e.getKey().substring(6), ((StringSetting) e.getValue()).getValue());
510 }
511 }
512 return all;
513 }
514
515 public synchronized boolean getBoolean(final String key) {
516 String s = get(key, null);
517 return s != null && Boolean.parseBoolean(s);
518 }
519
520 public synchronized boolean getBoolean(final String key, final boolean def) {
521 return Boolean.parseBoolean(get(key, Boolean.toString(def)));
522 }
523
524 public synchronized boolean getBoolean(final String key, final String specName, final boolean def) {
525 boolean generic = getBoolean(key, def);
526 String skey = key+'.'+specName;
527 Setting<?> prop = settingsMap.get(skey);
528 if (prop instanceof StringSetting)
529 return Boolean.parseBoolean(((StringSetting) prop).getValue());
530 else
531 return generic;
532 }
533
534 /**
535 * Set a value for a certain setting.
536 * @param key the unique identifier for the setting
537 * @param value the value of the setting. Can be null or "" which both removes the key-value entry.
538 * @return {@code true}, if something has changed (i.e. value is different than before)
539 */
540 public boolean put(final String key, String value) {
541 return putSetting(key, value == null || value.isEmpty() ? null : new StringSetting(value));
542 }
543
544 /**
545 * Set a boolean value for a certain setting.
546 * @param key the unique identifier for the setting
547 * @param value The new value
548 * @return {@code true}, if something has changed (i.e. value is different than before)
549 * @see BooleanProperty
550 */
551 public boolean put(final String key, final boolean value) {
552 return put(key, Boolean.toString(value));
553 }
554
555 /**
556 * Set a boolean value for a certain setting.
557 * @param key the unique identifier for the setting
558 * @param value The new value
559 * @return {@code true}, if something has changed (i.e. value is different than before)
560 * @see IntegerProperty
561 */
562 public boolean putInteger(final String key, final Integer value) {
563 return put(key, Integer.toString(value));
564 }
565
566 /**
567 * Set a boolean value for a certain setting.
568 * @param key the unique identifier for the setting
569 * @param value The new value
570 * @return {@code true}, if something has changed (i.e. value is different than before)
571 * @see DoubleProperty
572 */
573 public boolean putDouble(final String key, final Double value) {
574 return put(key, Double.toString(value));
575 }
576
577 /**
578 * Set a boolean value for a certain setting.
579 * @param key the unique identifier for the setting
580 * @param value The new value
581 * @return {@code true}, if something has changed (i.e. value is different than before)
582 * @see LongProperty
583 */
584 public boolean putLong(final String key, final Long value) {
585 return put(key, Long.toString(value));
586 }
587
588 /**
589 * Called after every put. In case of a problem, do nothing but output the error in log.
590 * @throws IOException if any I/O error occurs
591 */
592 public synchronized void save() throws IOException {
593 save(getPreferenceFile(), settingsMap.entrySet().stream().filter(NO_DEFAULT_SETTINGS_ENTRY), false);
594 }
595
596 public synchronized void saveDefaults() throws IOException {
597 save(getDefaultsCacheFile(), defaultsMap.entrySet().stream(), true);
598 }
599
600 protected void save(File prefFile, Stream<Entry<String, Setting<?>>> settings, boolean defaults) throws IOException {
601 if (!defaults) {
602 /* currently unused, but may help to fix configuration issues in future */
603 putInteger("josm.version", Version.getInstance().getVersion());
604
605 updateSystemProperties();
606 }
607
608 File backupFile = new File(prefFile + "_backup");
609
610 // Backup old preferences if there are old preferences
611 if (prefFile.exists() && prefFile.length() > 0 && initSuccessful) {
612 Utils.copyFile(prefFile, backupFile);
613 }
614
615 try (PreferencesWriter writer = new PreferencesWriter(
616 new PrintWriter(new File(prefFile + "_tmp"), StandardCharsets.UTF_8.name()), false, defaults)) {
617 writer.write(settings);
618 }
619
620 File tmpFile = new File(prefFile + "_tmp");
621 Utils.copyFile(tmpFile, prefFile);
622 Utils.deleteFile(tmpFile, marktr("Unable to delete temporary file {0}"));
623
624 setCorrectPermissions(prefFile);
625 setCorrectPermissions(backupFile);
626 }
627
628 private static void setCorrectPermissions(File file) {
629 if (!file.setReadable(false, false) && Main.isDebugEnabled()) {
630 Main.debug(tr("Unable to set file non-readable {0}", file.getAbsolutePath()));
631 }
632 if (!file.setWritable(false, false) && Main.isDebugEnabled()) {
633 Main.debug(tr("Unable to set file non-writable {0}", file.getAbsolutePath()));
634 }
635 if (!file.setExecutable(false, false) && Main.isDebugEnabled()) {
636 Main.debug(tr("Unable to set file non-executable {0}", file.getAbsolutePath()));
637 }
638 if (!file.setReadable(true, true) && Main.isDebugEnabled()) {
639 Main.debug(tr("Unable to set file readable {0}", file.getAbsolutePath()));
640 }
641 if (!file.setWritable(true, true) && Main.isDebugEnabled()) {
642 Main.debug(tr("Unable to set file writable {0}", file.getAbsolutePath()));
643 }
644 }
645
646 /**
647 * Loads preferences from settings file.
648 * @throws IOException if any I/O error occurs while reading the file
649 * @throws SAXException if the settings file does not contain valid XML
650 * @throws XMLStreamException if an XML error occurs while parsing the file (after validation)
651 */
652 protected void load() throws IOException, SAXException, XMLStreamException {
653 File pref = getPreferenceFile();
654 PreferencesReader.validateXML(pref);
655 PreferencesReader reader = new PreferencesReader(pref, false);
656 reader.parse();
657 settingsMap.clear();
658 settingsMap.putAll(reader.getSettings());
659 updateSystemProperties();
660 removeObsolete(reader.getVersion());
661 }
662
663 /**
664 * Loads default preferences from default settings cache file.
665 *
666 * Discards entries older than {@link #MAX_AGE_DEFAULT_PREFERENCES}.
667 *
668 * @throws IOException if any I/O error occurs while reading the file
669 * @throws SAXException if the settings file does not contain valid XML
670 * @throws XMLStreamException if an XML error occurs while parsing the file (after validation)
671 */
672 protected void loadDefaults() throws IOException, XMLStreamException, SAXException {
673 File def = getDefaultsCacheFile();
674 PreferencesReader.validateXML(def);
675 PreferencesReader reader = new PreferencesReader(def, true);
676 reader.parse();
677 defaultsMap.clear();
678 long minTime = System.currentTimeMillis() / 1000 - MAX_AGE_DEFAULT_PREFERENCES;
679 for (Entry<String, Setting<?>> e : reader.getSettings().entrySet()) {
680 if (e.getValue().getTime() >= minTime) {
681 defaultsMap.put(e.getKey(), e.getValue());
682 }
683 }
684 }
685
686 /**
687 * Loads preferences from XML reader.
688 * @param in XML reader
689 * @throws XMLStreamException if any XML stream error occurs
690 * @throws IOException if any I/O error occurs
691 */
692 public void fromXML(Reader in) throws XMLStreamException, IOException {
693 PreferencesReader reader = new PreferencesReader(in, false);
694 reader.parse();
695 settingsMap.clear();
696 settingsMap.putAll(reader.getSettings());
697 }
698
699 /**
700 * Initializes preferences.
701 * @param reset if {@code true}, current settings file is replaced by the default one
702 */
703 public void init(boolean reset) {
704 initSuccessful = false;
705 // get the preferences.
706 File prefDir = getPreferencesDirectory();
707 if (prefDir.exists()) {
708 if (!prefDir.isDirectory()) {
709 Main.warn(tr("Failed to initialize preferences. Preference directory ''{0}'' is not a directory.",
710 prefDir.getAbsoluteFile()));
711 JOptionPane.showMessageDialog(
712 Main.parent,
713 tr("<html>Failed to initialize preferences.<br>Preference directory ''{0}'' is not a directory.</html>",
714 prefDir.getAbsoluteFile()),
715 tr("Error"),
716 JOptionPane.ERROR_MESSAGE
717 );
718 return;
719 }
720 } else {
721 if (!prefDir.mkdirs()) {
722 Main.warn(tr("Failed to initialize preferences. Failed to create missing preference directory: {0}",
723 prefDir.getAbsoluteFile()));
724 JOptionPane.showMessageDialog(
725 Main.parent,
726 tr("<html>Failed to initialize preferences.<br>Failed to create missing preference directory: {0}</html>",
727 prefDir.getAbsoluteFile()),
728 tr("Error"),
729 JOptionPane.ERROR_MESSAGE
730 );
731 return;
732 }
733 }
734
735 File preferenceFile = getPreferenceFile();
736 try {
737 if (!preferenceFile.exists()) {
738 Main.info(tr("Missing preference file ''{0}''. Creating a default preference file.", preferenceFile.getAbsoluteFile()));
739 resetToDefault();
740 save();
741 } else if (reset) {
742 File backupFile = new File(prefDir, "preferences.xml.bak");
743 Main.platform.rename(preferenceFile, backupFile);
744 Main.warn(tr("Replacing existing preference file ''{0}'' with default preference file.", preferenceFile.getAbsoluteFile()));
745 resetToDefault();
746 save();
747 }
748 } catch (IOException e) {
749 Main.error(e);
750 JOptionPane.showMessageDialog(
751 Main.parent,
752 tr("<html>Failed to initialize preferences.<br>Failed to reset preference file to default: {0}</html>",
753 getPreferenceFile().getAbsoluteFile()),
754 tr("Error"),
755 JOptionPane.ERROR_MESSAGE
756 );
757 return;
758 }
759 try {
760 load();
761 initSuccessful = true;
762 } catch (IOException | SAXException | XMLStreamException e) {
763 Main.error(e);
764 File backupFile = new File(prefDir, "preferences.xml.bak");
765 JOptionPane.showMessageDialog(
766 Main.parent,
767 tr("<html>Preferences file had errors.<br> Making backup of old one to <br>{0}<br> " +
768 "and creating a new default preference file.</html>",
769 backupFile.getAbsoluteFile()),
770 tr("Error"),
771 JOptionPane.ERROR_MESSAGE
772 );
773 Main.platform.rename(preferenceFile, backupFile);
774 try {
775 resetToDefault();
776 save();
777 } catch (IOException e1) {
778 Main.error(e1);
779 Main.warn(tr("Failed to initialize preferences. Failed to reset preference file to default: {0}", getPreferenceFile()));
780 }
781 }
782 File def = getDefaultsCacheFile();
783 if (def.exists()) {
784 try {
785 loadDefaults();
786 } catch (IOException | XMLStreamException | SAXException e) {
787 Main.error(e);
788 Main.warn(tr("Failed to load defaults cache file: {0}", def));
789 defaultsMap.clear();
790 if (!def.delete()) {
791 Main.warn(tr("Failed to delete faulty defaults cache file: {0}", def));
792 }
793 }
794 }
795 }
796
797 /**
798 * Resets the preferences to their initial state. This resets all values and file associations.
799 * The default values and listeners are not removed.
800 * <p>
801 * It is meant to be called before {@link #init(boolean)}
802 * @since 10876
803 */
804 public void resetToInitialState() {
805 resetToDefault();
806 preferencesDir = null;
807 cacheDir = null;
808 userdataDir = null;
809 saveOnPut = true;
810 initSuccessful = false;
811 }
812
813 /**
814 * Reset all values stored in this map to the default values. This clears the preferences.
815 */
816 public final void resetToDefault() {
817 settingsMap.clear();
818 }
819
820 /**
821 * Convenience method for accessing colour preferences.
822 * <p>
823 * To be removed: end of 2016
824 *
825 * @param colName name of the colour
826 * @param def default value
827 * @return a Color object for the configured colour, or the default value if none configured.
828 * @deprecated Use a {@link ColorProperty} instead.
829 */
830 @Deprecated
831 public synchronized Color getColor(String colName, Color def) {
832 return getColor(colName, null, def);
833 }
834
835 /* only for preferences */
836 public synchronized String getColorName(String o) {
837 Matcher m = Pattern.compile("mappaint\\.(.+?)\\.(.+)").matcher(o);
838 if (m.matches()) {
839 return tr("Paint style {0}: {1}", tr(I18n.escape(m.group(1))), tr(I18n.escape(m.group(2))));
840 }
841 m = Pattern.compile("layer (.+)").matcher(o);
842 if (m.matches()) {
843 return tr("Layer: {0}", tr(I18n.escape(m.group(1))));
844 }
845 return tr(I18n.escape(colornames.containsKey(o) ? colornames.get(o) : o));
846 }
847
848 /**
849 * Convenience method for accessing colour preferences.
850 * <p>
851 * To be removed: end of 2016
852 * @param colName name of the colour
853 * @param specName name of the special colour settings
854 * @param def default value
855 * @return a Color object for the configured colour, or the default value if none configured.
856 * @deprecated Use a {@link ColorProperty} instead.
857 * You can replace this by: <code>new ColorProperty(colName, def).getChildColor(specName)</code>
858 */
859 @Deprecated
860 public synchronized Color getColor(String colName, String specName, Color def) {
861 String colKey = ColorProperty.getColorKey(colName);
862 registerColor(colKey, colName);
863 String colStr = specName != null ? get("color."+specName) : "";
864 if (colStr.isEmpty()) {
865 colStr = get(colKey, ColorHelper.color2html(def, true));
866 }
867 if (colStr != null && !colStr.isEmpty()) {
868 return ColorHelper.html2color(colStr);
869 } else {
870 return def;
871 }
872 }
873
874 /**
875 * Registers a color name conversion for the global color registry.
876 * @param colKey The key
877 * @param colName The name of the color.
878 * @since 10824
879 */
880 public void registerColor(String colKey, String colName) {
881 if (!colKey.equals(colName)) {
882 colornames.put(colKey, colName);
883 }
884 }
885
886 public synchronized Color getDefaultColor(String colKey) {
887 StringSetting col = Utils.cast(defaultsMap.get("color."+colKey), StringSetting.class);
888 String colStr = col == null ? null : col.getValue();
889 return colStr == null || colStr.isEmpty() ? null : ColorHelper.html2color(colStr);
890 }
891
892 public synchronized boolean putColor(String colKey, Color val) {
893 return put("color."+colKey, val != null ? ColorHelper.color2html(val, true) : null);
894 }
895
896 public synchronized int getInteger(String key, int def) {
897 String v = get(key, Integer.toString(def));
898 if (v.isEmpty())
899 return def;
900
901 try {
902 return Integer.parseInt(v);
903 } catch (NumberFormatException e) {
904 // fall out
905 Main.trace(e);
906 }
907 return def;
908 }
909
910 public synchronized int getInteger(String key, String specName, int def) {
911 String v = get(key+'.'+specName);
912 if (v.isEmpty())
913 v = get(key, Integer.toString(def));
914 if (v.isEmpty())
915 return def;
916
917 try {
918 return Integer.parseInt(v);
919 } catch (NumberFormatException e) {
920 // fall out
921 Main.trace(e);
922 }
923 return def;
924 }
925
926 public synchronized long getLong(String key, long def) {
927 String v = get(key, Long.toString(def));
928 if (null == v)
929 return def;
930
931 try {
932 return Long.parseLong(v);
933 } catch (NumberFormatException e) {
934 // fall out
935 Main.trace(e);
936 }
937 return def;
938 }
939
940 public synchronized double getDouble(String key, double def) {
941 String v = get(key, Double.toString(def));
942 if (null == v)
943 return def;
944
945 try {
946 return Double.parseDouble(v);
947 } catch (NumberFormatException e) {
948 // fall out
949 Main.trace(e);
950 }
951 return def;
952 }
953
954 /**
955 * Get a list of values for a certain key
956 * @param key the identifier for the setting
957 * @param def the default value.
958 * @return the corresponding value if the property has been set before, {@code def} otherwise
959 */
960 public Collection<String> getCollection(String key, Collection<String> def) {
961 return getSetting(key, ListSetting.create(def), ListSetting.class).getValue();
962 }
963
964 /**
965 * Get a list of values for a certain key
966 * @param key the identifier for the setting
967 * @return the corresponding value if the property has been set before, an empty collection otherwise.
968 */
969 public Collection<String> getCollection(String key) {
970 Collection<String> val = getCollection(key, null);
971 return val == null ? Collections.<String>emptyList() : val;
972 }
973
974 public synchronized void removeFromCollection(String key, String value) {
975 List<String> a = new ArrayList<>(getCollection(key, Collections.<String>emptyList()));
976 a.remove(value);
977 putCollection(key, a);
978 }
979
980 /**
981 * Set a value for a certain setting. The changed setting is saved to the preference file immediately.
982 * Due to caching mechanisms on modern operating systems and hardware, this shouldn't be a performance problem.
983 * @param key the unique identifier for the setting
984 * @param setting the value of the setting. In case it is null, the key-value entry will be removed.
985 * @return {@code true}, if something has changed (i.e. value is different than before)
986 */
987 public boolean putSetting(final String key, Setting<?> setting) {
988 CheckParameterUtil.ensureParameterNotNull(key);
989 if (setting != null && setting.getValue() == null)
990 throw new IllegalArgumentException("setting argument must not have null value");
991 Setting<?> settingOld;
992 Setting<?> settingCopy = null;
993 synchronized (this) {
994 if (setting == null) {
995 settingOld = settingsMap.remove(key);
996 if (settingOld == null)
997 return false;
998 } else {
999 settingOld = settingsMap.get(key);
1000 if (setting.equals(settingOld))
1001 return false;
1002 if (settingOld == null && setting.equals(defaultsMap.get(key)))
1003 return false;
1004 settingCopy = setting.copy();
1005 settingsMap.put(key, settingCopy);
1006 }
1007 if (saveOnPut) {
1008 try {
1009 save();
1010 } catch (IOException e) {
1011 Main.warn(e, tr("Failed to persist preferences to ''{0}''", getPreferenceFile().getAbsoluteFile()));
1012 }
1013 }
1014 }
1015 // Call outside of synchronized section in case some listener wait for other thread that wait for preference lock
1016 firePreferenceChanged(key, settingOld, settingCopy);
1017 return true;
1018 }
1019
1020 public synchronized Setting<?> getSetting(String key, Setting<?> def) {
1021 return getSetting(key, def, Setting.class);
1022 }
1023
1024 /**
1025 * Get settings value for a certain key and provide default a value.
1026 * @param <T> the setting type
1027 * @param key the identifier for the setting
1028 * @param def the default value. For each call of getSetting() with a given key, the default value must be the same.
1029 * <code>def</code> must not be null, but the value of <code>def</code> can be null.
1030 * @param klass the setting type (same as T)
1031 * @return the corresponding value if the property has been set before, {@code def} otherwise
1032 */
1033 @SuppressWarnings("unchecked")
1034 public synchronized <T extends Setting<?>> T getSetting(String key, T def, Class<T> klass) {
1035 CheckParameterUtil.ensureParameterNotNull(key);
1036 CheckParameterUtil.ensureParameterNotNull(def);
1037 Setting<?> oldDef = defaultsMap.get(key);
1038 if (oldDef != null && oldDef.isNew() && oldDef.getValue() != null && def.getValue() != null && !def.equals(oldDef)) {
1039 Main.info("Defaults for " + key + " differ: " + def + " != " + defaultsMap.get(key));
1040 }
1041 if (def.getValue() != null || oldDef == null) {
1042 Setting<?> defCopy = def.copy();
1043 defCopy.setTime(System.currentTimeMillis() / 1000);
1044 defCopy.setNew(true);
1045 defaultsMap.put(key, defCopy);
1046 }
1047 Setting<?> prop = settingsMap.get(key);
1048 if (klass.isInstance(prop)) {
1049 return (T) prop;
1050 } else {
1051 return def;
1052 }
1053 }
1054
1055 /**
1056 * Put a collection.
1057 * @param key key
1058 * @param value value
1059 * @return {@code true}, if something has changed (i.e. value is different than before)
1060 */
1061 public boolean putCollection(String key, Collection<String> value) {
1062 return putSetting(key, value == null ? null : ListSetting.create(value));
1063 }
1064
1065 /**
1066 * Saves at most {@code maxsize} items of collection {@code val}.
1067 * @param key key
1068 * @param maxsize max number of items to save
1069 * @param val value
1070 * @return {@code true}, if something has changed (i.e. value is different than before)
1071 */
1072 public boolean putCollectionBounded(String key, int maxsize, Collection<String> val) {
1073 Collection<String> newCollection = new ArrayList<>(Math.min(maxsize, val.size()));
1074 for (String i : val) {
1075 if (newCollection.size() >= maxsize) {
1076 break;
1077 }
1078 newCollection.add(i);
1079 }
1080 return putCollection(key, newCollection);
1081 }
1082
1083 /**
1084 * Used to read a 2-dimensional array of strings from the preference file.
1085 * If not a single entry could be found, <code>def</code> is returned.
1086 * @param key preference key
1087 * @param def default array value
1088 * @return array value
1089 */
1090 @SuppressWarnings({ "unchecked", "rawtypes" })
1091 public synchronized Collection<Collection<String>> getArray(String key, Collection<Collection<String>> def) {
1092 ListListSetting val = getSetting(key, ListListSetting.create(def), ListListSetting.class);
1093 return (Collection) val.getValue();
1094 }
1095
1096 public Collection<Collection<String>> getArray(String key) {
1097 Collection<Collection<String>> res = getArray(key, null);
1098 return res == null ? Collections.<Collection<String>>emptyList() : res;
1099 }
1100
1101 /**
1102 * Put an array.
1103 * @param key key
1104 * @param value value
1105 * @return {@code true}, if something has changed (i.e. value is different than before)
1106 */
1107 public boolean putArray(String key, Collection<Collection<String>> value) {
1108 return putSetting(key, value == null ? null : ListListSetting.create(value));
1109 }
1110
1111 public Collection<Map<String, String>> getListOfStructs(String key, Collection<Map<String, String>> def) {
1112 return getSetting(key, new MapListSetting(def == null ? null : new ArrayList<>(def)), MapListSetting.class).getValue();
1113 }
1114
1115 public boolean putListOfStructs(String key, Collection<Map<String, String>> value) {
1116 return putSetting(key, value == null ? null : new MapListSetting(new ArrayList<>(value)));
1117 }
1118
1119 /**
1120 * Annotation used for converting objects to String Maps and vice versa.
1121 * Indicates that a certain field should be considered in the conversion process. Otherwise it is ignored.
1122 *
1123 * @see #serializeStruct(java.lang.Object, java.lang.Class)
1124 * @see #deserializeStruct(java.util.Map, java.lang.Class)
1125 */
1126 @Retention(RetentionPolicy.RUNTIME) // keep annotation at runtime
1127 public @interface pref { }
1128
1129 /**
1130 * Annotation used for converting objects to String Maps.
1131 * Indicates that a certain field should be written to the map, even if the value is the same as the default value.
1132 *
1133 * @see #serializeStruct(java.lang.Object, java.lang.Class)
1134 */
1135 @Retention(RetentionPolicy.RUNTIME) // keep annotation at runtime
1136 public @interface writeExplicitly { }
1137
1138 /**
1139 * Get a list of hashes which are represented by a struct-like class.
1140 * Possible properties are given by fields of the class klass that have the @pref annotation.
1141 * Default constructor is used to initialize the struct objects, properties then override some of these default values.
1142 * @param <T> klass type
1143 * @param key main preference key
1144 * @param klass The struct class
1145 * @return a list of objects of type T or an empty list if nothing was found
1146 */
1147 public <T> List<T> getListOfStructs(String key, Class<T> klass) {
1148 List<T> r = getListOfStructs(key, null, klass);
1149 if (r == null)
1150 return Collections.emptyList();
1151 else
1152 return r;
1153 }
1154
1155 /**
1156 * same as above, but returns def if nothing was found
1157 * @param <T> klass type
1158 * @param key main preference key
1159 * @param def default value
1160 * @param klass The struct class
1161 * @return a list of objects of type T or {@code def} if nothing was found
1162 */
1163 public <T> List<T> getListOfStructs(String key, Collection<T> def, Class<T> klass) {
1164 Collection<Map<String, String>> prop =
1165 getListOfStructs(key, def == null ? null : serializeListOfStructs(def, klass));
1166 if (prop == null)
1167 return def == null ? null : new ArrayList<>(def);
1168 List<T> lst = new ArrayList<>();
1169 for (Map<String, String> entries : prop) {
1170 T struct = deserializeStruct(entries, klass);
1171 lst.add(struct);
1172 }
1173 return lst;
1174 }
1175
1176 /**
1177 * Convenience method that saves a MapListSetting which is provided as a collection of objects.
1178 *
1179 * Each object is converted to a <code>Map&lt;String, String&gt;</code> using the fields with {@link pref} annotation.
1180 * The field name is the key and the value will be converted to a string.
1181 *
1182 * Considers only fields that have the @pref annotation.
1183 * In addition it does not write fields with null values. (Thus they are cleared)
1184 * Default values are given by the field values after default constructor has been called.
1185 * Fields equal to the default value are not written unless the field has the @writeExplicitly annotation.
1186 * @param <T> the class,
1187 * @param key main preference key
1188 * @param val the list that is supposed to be saved
1189 * @param klass The struct class
1190 * @return true if something has changed
1191 */
1192 public <T> boolean putListOfStructs(String key, Collection<T> val, Class<T> klass) {
1193 return putListOfStructs(key, serializeListOfStructs(val, klass));
1194 }
1195
1196 private static <T> Collection<Map<String, String>> serializeListOfStructs(Collection<T> l, Class<T> klass) {
1197 if (l == null)
1198 return null;
1199 Collection<Map<String, String>> vals = new ArrayList<>();
1200 for (T struct : l) {
1201 if (struct == null) {
1202 continue;
1203 }
1204 vals.add(serializeStruct(struct, klass));
1205 }
1206 return vals;
1207 }
1208
1209 @SuppressWarnings("rawtypes")
1210 private static String mapToJson(Map map) {
1211 StringWriter stringWriter = new StringWriter();
1212 try (JsonWriter writer = Json.createWriter(stringWriter)) {
1213 JsonObjectBuilder object = Json.createObjectBuilder();
1214 for (Object o: map.entrySet()) {
1215 Entry e = (Entry) o;
1216 Object evalue = e.getValue();
1217 object.add(e.getKey().toString(), evalue.toString());
1218 }
1219 writer.writeObject(object.build());
1220 }
1221 return stringWriter.toString();
1222 }
1223
1224 @SuppressWarnings({ "rawtypes", "unchecked" })
1225 private static Map mapFromJson(String s) {
1226 Map ret = null;
1227 try (JsonReader reader = Json.createReader(new StringReader(s))) {
1228 JsonObject object = reader.readObject();
1229 ret = new HashMap(object.size());
1230 for (Entry<String, JsonValue> e: object.entrySet()) {
1231 JsonValue value = e.getValue();
1232 if (value instanceof JsonString) {
1233 // in some cases, when JsonValue.toString() is called, then additional quotation marks are left in value
1234 ret.put(e.getKey(), ((JsonString) value).getString());
1235 } else {
1236 ret.put(e.getKey(), e.getValue().toString());
1237 }
1238 }
1239 }
1240 return ret;
1241 }
1242
1243 @SuppressWarnings("rawtypes")
1244 private static String multiMapToJson(MultiMap map) {
1245 StringWriter stringWriter = new StringWriter();
1246 try (JsonWriter writer = Json.createWriter(stringWriter)) {
1247 JsonObjectBuilder object = Json.createObjectBuilder();
1248 for (Object o: map.entrySet()) {
1249 Entry e = (Entry) o;
1250 Set evalue = (Set) e.getValue();
1251 JsonArrayBuilder a = Json.createArrayBuilder();
1252 for (Object evo: evalue) {
1253 a.add(evo.toString());
1254 }
1255 object.add(e.getKey().toString(), a.build());
1256 }
1257 writer.writeObject(object.build());
1258 }
1259 return stringWriter.toString();
1260 }
1261
1262 @SuppressWarnings({ "rawtypes", "unchecked" })
1263 private static MultiMap multiMapFromJson(String s) {
1264 MultiMap ret = null;
1265 try (JsonReader reader = Json.createReader(new StringReader(s))) {
1266 JsonObject object = reader.readObject();
1267 ret = new MultiMap(object.size());
1268 for (Entry<String, JsonValue> e: object.entrySet()) {
1269 JsonValue value = e.getValue();
1270 if (value instanceof JsonArray) {
1271 for (JsonString js: ((JsonArray) value).getValuesAs(JsonString.class)) {
1272 ret.put(e.getKey(), js.getString());
1273 }
1274 } else if (value instanceof JsonString) {
1275 // in some cases, when JsonValue.toString() is called, then additional quotation marks are left in value
1276 ret.put(e.getKey(), ((JsonString) value).getString());
1277 } else {
1278 ret.put(e.getKey(), e.getValue().toString());
1279 }
1280 }
1281 }
1282 return ret;
1283 }
1284
1285 /**
1286 * Convert an object to a String Map, by using field names and values as map key and value.
1287 *
1288 * The field value is converted to a String.
1289 *
1290 * Only fields with annotation {@link pref} are taken into account.
1291 *
1292 * Fields will not be written to the map if the value is null or unchanged
1293 * (compared to an object created with the no-arg-constructor).
1294 * The {@link writeExplicitly} annotation overrides this behavior, i.e. the default value will also be written.
1295 *
1296 * @param <T> the class of the object <code>struct</code>
1297 * @param struct the object to be converted
1298 * @param klass the class T
1299 * @return the resulting map (same data content as <code>struct</code>)
1300 */
1301 public static <T> Map<String, String> serializeStruct(T struct, Class<T> klass) {
1302 T structPrototype;
1303 try {
1304 structPrototype = klass.getConstructor().newInstance();
1305 } catch (ReflectiveOperationException ex) {
1306 throw new IllegalArgumentException(ex);
1307 }
1308
1309 Map<String, String> hash = new LinkedHashMap<>();
1310 for (Field f : klass.getDeclaredFields()) {
1311 if (f.getAnnotation(pref.class) == null) {
1312 continue;
1313 }
1314 Utils.setObjectsAccessible(f);
1315 try {
1316 Object fieldValue = f.get(struct);
1317 Object defaultFieldValue = f.get(structPrototype);
1318 if (fieldValue != null && (f.getAnnotation(writeExplicitly.class) != null || !Objects.equals(fieldValue, defaultFieldValue))) {
1319 String key = f.getName().replace('_', '-');
1320 if (fieldValue instanceof Map) {
1321 hash.put(key, mapToJson((Map<?, ?>) fieldValue));
1322 } else if (fieldValue instanceof MultiMap) {
1323 hash.put(key, multiMapToJson((MultiMap<?, ?>) fieldValue));
1324 } else {
1325 hash.put(key, fieldValue.toString());
1326 }
1327 }
1328 } catch (IllegalAccessException ex) {
1329 throw new RuntimeException(ex);
1330 }
1331 }
1332 return hash;
1333 }
1334
1335 /**
1336 * Converts a String-Map to an object of a certain class, by comparing map keys to field names of the class and assigning
1337 * map values to the corresponding fields.
1338 *
1339 * The map value (a String) is converted to the field type. Supported types are: boolean, Boolean, int, Integer, double,
1340 * Double, String, Map&lt;String, String&gt; and Map&lt;String, List&lt;String&gt;&gt;.
1341 *
1342 * Only fields with annotation {@link pref} are taken into account.
1343 * @param <T> the class
1344 * @param hash the string map with initial values
1345 * @param klass the class T
1346 * @return an object of class T, initialized as described above
1347 */
1348 public static <T> T deserializeStruct(Map<String, String> hash, Class<T> klass) {
1349 T struct = null;
1350 try {
1351 struct = klass.getConstructor().newInstance();
1352 } catch (ReflectiveOperationException ex) {
1353 throw new IllegalArgumentException(ex);
1354 }
1355 for (Entry<String, String> key_value : hash.entrySet()) {
1356 Object value;
1357 Field f;
1358 try {
1359 f = klass.getDeclaredField(key_value.getKey().replace('-', '_'));
1360 } catch (NoSuchFieldException ex) {
1361 Main.trace(ex);
1362 continue;
1363 }
1364 if (f.getAnnotation(pref.class) == null) {
1365 continue;
1366 }
1367 Utils.setObjectsAccessible(f);
1368 if (f.getType() == Boolean.class || f.getType() == boolean.class) {
1369 value = Boolean.valueOf(key_value.getValue());
1370 } else if (f.getType() == Integer.class || f.getType() == int.class) {
1371 try {
1372 value = Integer.valueOf(key_value.getValue());
1373 } catch (NumberFormatException nfe) {
1374 continue;
1375 }
1376 } else if (f.getType() == Double.class || f.getType() == double.class) {
1377 try {
1378 value = Double.valueOf(key_value.getValue());
1379 } catch (NumberFormatException nfe) {
1380 continue;
1381 }
1382 } else if (f.getType() == String.class) {
1383 value = key_value.getValue();
1384 } else if (f.getType().isAssignableFrom(Map.class)) {
1385 value = mapFromJson(key_value.getValue());
1386 } else if (f.getType().isAssignableFrom(MultiMap.class)) {
1387 value = multiMapFromJson(key_value.getValue());
1388 } else
1389 throw new RuntimeException("unsupported preference primitive type");
1390
1391 try {
1392 f.set(struct, value);
1393 } catch (IllegalArgumentException ex) {
1394 throw new AssertionError(ex);
1395 } catch (IllegalAccessException ex) {
1396 throw new RuntimeException(ex);
1397 }
1398 }
1399 return struct;
1400 }
1401
1402 public Map<String, Setting<?>> getAllSettings() {
1403 return new TreeMap<>(settingsMap);
1404 }
1405
1406 public Map<String, Setting<?>> getAllDefaults() {
1407 return new TreeMap<>(defaultsMap);
1408 }
1409
1410 /**
1411 * Updates system properties with the current values in the preferences.
1412 *
1413 */
1414 public void updateSystemProperties() {
1415 if ("true".equals(get("prefer.ipv6", "auto")) && !"true".equals(Utils.updateSystemProperty("java.net.preferIPv6Addresses", "true"))) {
1416 // never set this to false, only true!
1417 Main.info(tr("Try enabling IPv6 network, prefering IPv6 over IPv4 (only works on early startup)."));
1418 }
1419 Utils.updateSystemProperty("http.agent", Version.getInstance().getAgentString());
1420 Utils.updateSystemProperty("user.language", get("language"));
1421 // Workaround to fix a Java bug. This ugly hack comes from Sun bug database: https://bugs.openjdk.java.net/browse/JDK-6292739
1422 // Force AWT toolkit to update its internal preferences (fix #6345).
1423 if (!GraphicsEnvironment.isHeadless()) {
1424 try {
1425 Field field = Toolkit.class.getDeclaredField("resources");
1426 Utils.setObjectsAccessible(field);
1427 field.set(null, ResourceBundle.getBundle("sun.awt.resources.awt"));
1428 } catch (ReflectiveOperationException | MissingResourceException e) {
1429 Main.warn(e);
1430 }
1431 }
1432 // Possibility to disable SNI (not by default) in case of misconfigured https servers
1433 // See #9875 + http://stackoverflow.com/a/14884941/2257172
1434 // then https://josm.openstreetmap.de/ticket/12152#comment:5 for details
1435 if (getBoolean("jdk.tls.disableSNIExtension", false)) {
1436 Utils.updateSystemProperty("jsse.enableSNIExtension", "false");
1437 }
1438 }
1439
1440 /**
1441 * Replies the collection of plugin site URLs from where plugin lists can be downloaded.
1442 * @return the collection of plugin site URLs
1443 * @see #getOnlinePluginSites
1444 */
1445 public Collection<String> getPluginSites() {
1446 return getCollection("pluginmanager.sites", Collections.singleton(Main.getJOSMWebsite()+"/pluginicons%<?plugins=>"));
1447 }
1448
1449 /**
1450 * Returns the list of plugin sites available according to offline mode settings.
1451 * @return the list of available plugin sites
1452 * @since 8471
1453 */
1454 public Collection<String> getOnlinePluginSites() {
1455 Collection<String> pluginSites = new ArrayList<>(getPluginSites());
1456 for (Iterator<String> it = pluginSites.iterator(); it.hasNext();) {
1457 try {
1458 OnlineResource.JOSM_WEBSITE.checkOfflineAccess(it.next(), Main.getJOSMWebsite());
1459 } catch (OfflineAccessException ex) {
1460 Main.warn(ex, false);
1461 it.remove();
1462 }
1463 }
1464 return pluginSites;
1465 }
1466
1467 /**
1468 * Sets the collection of plugin site URLs.
1469 *
1470 * @param sites the site URLs
1471 */
1472 public void setPluginSites(Collection<String> sites) {
1473 putCollection("pluginmanager.sites", sites);
1474 }
1475
1476 /**
1477 * Returns XML describing these preferences.
1478 * @param nopass if password must be excluded
1479 * @return XML
1480 */
1481 public String toXML(boolean nopass) {
1482 return toXML(settingsMap.entrySet(), nopass, false);
1483 }
1484
1485 /**
1486 * Returns XML describing the given preferences.
1487 * @param settings preferences settings
1488 * @param nopass if password must be excluded
1489 * @param defaults true, if default values are converted to XML, false for
1490 * regular preferences
1491 * @return XML
1492 */
1493 public String toXML(Collection<Entry<String, Setting<?>>> settings, boolean nopass, boolean defaults) {
1494 try (
1495 StringWriter sw = new StringWriter();
1496 PreferencesWriter prefWriter = new PreferencesWriter(new PrintWriter(sw), nopass, defaults)
1497 ) {
1498 prefWriter.write(settings);
1499 sw.flush();
1500 return sw.toString();
1501 } catch (IOException e) {
1502 Main.error(e);
1503 return null;
1504 }
1505 }
1506
1507 /**
1508 * Removes obsolete preference settings. If you throw out a once-used preference
1509 * setting, add it to the list here with an expiry date (written as comment). If you
1510 * see something with an expiry date in the past, remove it from the list.
1511 * @param loadedVersion JOSM version when the preferences file was written
1512 */
1513 private void removeObsolete(int loadedVersion) {
1514 /* drop in October 2016 */
1515 if (loadedVersion < 9715) {
1516 Setting<?> setting = settingsMap.get("imagery.entries");
1517 if (setting instanceof MapListSetting) {
1518 List<Map<String, String>> l = new LinkedList<>();
1519 boolean modified = false;
1520 for (Map<String, String> map: ((MapListSetting) setting).getValue()) {
1521 Map<String, String> newMap = new HashMap<>();
1522 for (Entry<String, String> entry: map.entrySet()) {
1523 String value = entry.getValue();
1524 if ("noTileHeaders".equals(entry.getKey())) {
1525 value = value.replaceFirst("\":(\".*\")\\}", "\":[$1]}");
1526 if (!value.equals(entry.getValue())) {
1527 modified = true;
1528 }
1529 }
1530 newMap.put(entry.getKey(), value);
1531 }
1532 l.add(newMap);
1533 }
1534 if (modified) {
1535 putListOfStructs("imagery.entries", l);
1536 }
1537 }
1538 }
1539 // drop in November 2016
1540 removeUrlFromEntries(loadedVersion, 9965,
1541 "mappaint.style.entries",
1542 "josm.openstreetmap.de/josmfile?page=Styles/LegacyStandard");
1543 // drop in December 2016
1544 removeUrlFromEntries(loadedVersion, 10063,
1545 "validator.org.openstreetmap.josm.data.validation.tests.MapCSSTagChecker.entries",
1546 "resource://data/validator/power.mapcss");
1547 // drop in March 2017
1548 if (loadedVersion < 11058) {
1549 migrateOldColorKeys();
1550 }
1551
1552 for (String key : OBSOLETE_PREF_KEYS) {
1553 if (settingsMap.containsKey(key)) {
1554 settingsMap.remove(key);
1555 Main.info(tr("Preference setting {0} has been removed since it is no longer used.", key));
1556 }
1557 }
1558 }
1559
1560 private void migrateOldColorKeys() {
1561 settingsMap.keySet().stream()
1562 .filter(key -> key.startsWith("color."))
1563 .flatMap(key -> {
1564 final String newKey = ColorProperty.getColorKey(key.substring("color.".length()));
1565 return key.equals(newKey) || settingsMap.containsKey(newKey)
1566 ? Stream.empty()
1567 : Stream.of(new AbstractMap.SimpleImmutableEntry<>(key, newKey));
1568 })
1569 .collect(Collectors.toList()) // to avoid ConcurrentModificationException
1570 .forEach(entry -> {
1571 final String oldKey = entry.getKey();
1572 final String newKey = entry.getValue();
1573 Main.info("Migrating old color key {0} => {1}", oldKey, newKey);
1574 put(newKey, get(oldKey));
1575 put(oldKey, null);
1576 });
1577 }
1578
1579 private void removeUrlFromEntries(int loadedVersion, int versionMax, String key, String urlPart) {
1580 if (loadedVersion < versionMax) {
1581 Setting<?> setting = settingsMap.get(key);
1582 if (setting instanceof MapListSetting) {
1583 List<Map<String, String>> l = new LinkedList<>();
1584 boolean modified = false;
1585 for (Map<String, String> map: ((MapListSetting) setting).getValue()) {
1586 String url = map.get("url");
1587 if (url != null && url.contains(urlPart)) {
1588 modified = true;
1589 } else {
1590 l.add(map);
1591 }
1592 }
1593 if (modified) {
1594 putListOfStructs(key, l);
1595 }
1596 }
1597 }
1598 }
1599
1600 /**
1601 * Enables or not the preferences file auto-save mechanism (save each time a setting is changed).
1602 * This behaviour is enabled by default.
1603 * @param enable if {@code true}, makes JOSM save preferences file each time a setting is changed
1604 * @since 7085
1605 */
1606 public final void enableSaveOnPut(boolean enable) {
1607 synchronized (this) {
1608 saveOnPut = enable;
1609 }
1610 }
1611}
Note: See TracBrowser for help on using the repository browser.