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

Last change on this file since 10747 was 10657, checked in by Don-vip, 8 years ago

see #11390, see #12890 - use Java 8 Predicates

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