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

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

see #11390 - sonar - squid:S1609 - Java 8: @FunctionalInterface annotation should be used to flag Single Abstract Method interfaces

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