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

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

see #11390 - sonar - squid:S1604 - Java 8: Anonymous inner classes containing only one method should become lambdas

  • Property svn:eol-style set to native
File size: 57.0 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 =
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 if (Main.isTraceEnabled()) {
796 Main.trace(e.getMessage());
797 }
798 }
799 return def;
800 }
801
802 public synchronized int getInteger(String key, String specName, int def) {
803 String v = get(key+'.'+specName);
804 if (v.isEmpty())
805 v = get(key, Integer.toString(def));
806 if (v.isEmpty())
807 return def;
808
809 try {
810 return Integer.parseInt(v);
811 } catch (NumberFormatException e) {
812 // fall out
813 if (Main.isTraceEnabled()) {
814 Main.trace(e.getMessage());
815 }
816 }
817 return def;
818 }
819
820 public synchronized long getLong(String key, long def) {
821 String v = get(key, Long.toString(def));
822 if (null == v)
823 return def;
824
825 try {
826 return Long.parseLong(v);
827 } catch (NumberFormatException e) {
828 // fall out
829 if (Main.isTraceEnabled()) {
830 Main.trace(e.getMessage());
831 }
832 }
833 return def;
834 }
835
836 public synchronized double getDouble(String key, double def) {
837 String v = get(key, Double.toString(def));
838 if (null == v)
839 return def;
840
841 try {
842 return Double.parseDouble(v);
843 } catch (NumberFormatException e) {
844 // fall out
845 if (Main.isTraceEnabled()) {
846 Main.trace(e.getMessage());
847 }
848 }
849 return def;
850 }
851
852 /**
853 * Get a list of values for a certain key
854 * @param key the identifier for the setting
855 * @param def the default value.
856 * @return the corresponding value if the property has been set before, {@code def} otherwise
857 */
858 public Collection<String> getCollection(String key, Collection<String> def) {
859 return getSetting(key, ListSetting.create(def), ListSetting.class).getValue();
860 }
861
862 /**
863 * Get a list of values for a certain key
864 * @param key the identifier for the setting
865 * @return the corresponding value if the property has been set before, an empty collection otherwise.
866 */
867 public Collection<String> getCollection(String key) {
868 Collection<String> val = getCollection(key, null);
869 return val == null ? Collections.<String>emptyList() : val;
870 }
871
872 public synchronized void removeFromCollection(String key, String value) {
873 List<String> a = new ArrayList<>(getCollection(key, Collections.<String>emptyList()));
874 a.remove(value);
875 putCollection(key, a);
876 }
877
878 /**
879 * Set a value for a certain setting. The changed setting is saved to the preference file immediately.
880 * Due to caching mechanisms on modern operating systems and hardware, this shouldn't be a performance problem.
881 * @param key the unique identifier for the setting
882 * @param setting the value of the setting. In case it is null, the key-value entry will be removed.
883 * @return {@code true}, if something has changed (i.e. value is different than before)
884 */
885 public boolean putSetting(final String key, Setting<?> setting) {
886 CheckParameterUtil.ensureParameterNotNull(key);
887 if (setting != null && setting.getValue() == null)
888 throw new IllegalArgumentException("setting argument must not have null value");
889 Setting<?> settingOld;
890 Setting<?> settingCopy = null;
891 synchronized (this) {
892 if (setting == null) {
893 settingOld = settingsMap.remove(key);
894 if (settingOld == null)
895 return false;
896 } else {
897 settingOld = settingsMap.get(key);
898 if (setting.equals(settingOld))
899 return false;
900 if (settingOld == null && setting.equals(defaultsMap.get(key)))
901 return false;
902 settingCopy = setting.copy();
903 settingsMap.put(key, settingCopy);
904 }
905 if (saveOnPut) {
906 try {
907 save();
908 } catch (IOException e) {
909 Main.warn(e, tr("Failed to persist preferences to ''{0}''", getPreferenceFile().getAbsoluteFile()));
910 }
911 }
912 }
913 // Call outside of synchronized section in case some listener wait for other thread that wait for preference lock
914 firePreferenceChanged(key, settingOld, settingCopy);
915 return true;
916 }
917
918 public synchronized Setting<?> getSetting(String key, Setting<?> def) {
919 return getSetting(key, def, Setting.class);
920 }
921
922 /**
923 * Get settings value for a certain key and provide default a value.
924 * @param <T> the setting type
925 * @param key the identifier for the setting
926 * @param def the default value. For each call of getSetting() with a given key, the default value must be the same.
927 * <code>def</code> must not be null, but the value of <code>def</code> can be null.
928 * @param klass the setting type (same as T)
929 * @return the corresponding value if the property has been set before, {@code def} otherwise
930 */
931 @SuppressWarnings("unchecked")
932 public synchronized <T extends Setting<?>> T getSetting(String key, T def, Class<T> klass) {
933 CheckParameterUtil.ensureParameterNotNull(key);
934 CheckParameterUtil.ensureParameterNotNull(def);
935 Setting<?> oldDef = defaultsMap.get(key);
936 if (oldDef != null && oldDef.isNew() && oldDef.getValue() != null && def.getValue() != null && !def.equals(oldDef)) {
937 Main.info("Defaults for " + key + " differ: " + def + " != " + defaultsMap.get(key));
938 }
939 if (def.getValue() != null || oldDef == null) {
940 Setting<?> defCopy = def.copy();
941 defCopy.setTime(System.currentTimeMillis() / 1000);
942 defCopy.setNew(true);
943 defaultsMap.put(key, defCopy);
944 }
945 Setting<?> prop = settingsMap.get(key);
946 if (klass.isInstance(prop)) {
947 return (T) prop;
948 } else {
949 return def;
950 }
951 }
952
953 /**
954 * Put a collection.
955 * @param key key
956 * @param value value
957 * @return {@code true}, if something has changed (i.e. value is different than before)
958 */
959 public boolean putCollection(String key, Collection<String> value) {
960 return putSetting(key, value == null ? null : ListSetting.create(value));
961 }
962
963 /**
964 * Saves at most {@code maxsize} items of collection {@code val}.
965 * @param key key
966 * @param maxsize max number of items to save
967 * @param val value
968 * @return {@code true}, if something has changed (i.e. value is different than before)
969 */
970 public boolean putCollectionBounded(String key, int maxsize, Collection<String> val) {
971 Collection<String> newCollection = new ArrayList<>(Math.min(maxsize, val.size()));
972 for (String i : val) {
973 if (newCollection.size() >= maxsize) {
974 break;
975 }
976 newCollection.add(i);
977 }
978 return putCollection(key, newCollection);
979 }
980
981 /**
982 * Used to read a 2-dimensional array of strings from the preference file.
983 * If not a single entry could be found, <code>def</code> is returned.
984 * @param key preference key
985 * @param def default array value
986 * @return array value
987 */
988 @SuppressWarnings({ "unchecked", "rawtypes" })
989 public synchronized Collection<Collection<String>> getArray(String key, Collection<Collection<String>> def) {
990 ListListSetting val = getSetting(key, ListListSetting.create(def), ListListSetting.class);
991 return (Collection) val.getValue();
992 }
993
994 public Collection<Collection<String>> getArray(String key) {
995 Collection<Collection<String>> res = getArray(key, null);
996 return res == null ? Collections.<Collection<String>>emptyList() : res;
997 }
998
999 /**
1000 * Put an array.
1001 * @param key key
1002 * @param value value
1003 * @return {@code true}, if something has changed (i.e. value is different than before)
1004 */
1005 public boolean putArray(String key, Collection<Collection<String>> value) {
1006 return putSetting(key, value == null ? null : ListListSetting.create(value));
1007 }
1008
1009 public Collection<Map<String, String>> getListOfStructs(String key, Collection<Map<String, String>> def) {
1010 return getSetting(key, new MapListSetting(def == null ? null : new ArrayList<>(def)), MapListSetting.class).getValue();
1011 }
1012
1013 public boolean putListOfStructs(String key, Collection<Map<String, String>> value) {
1014 return putSetting(key, value == null ? null : new MapListSetting(new ArrayList<>(value)));
1015 }
1016
1017 /**
1018 * Annotation used for converting objects to String Maps and vice versa.
1019 * Indicates that a certain field should be considered in the conversion process. Otherwise it is ignored.
1020 *
1021 * @see #serializeStruct(java.lang.Object, java.lang.Class)
1022 * @see #deserializeStruct(java.util.Map, java.lang.Class)
1023 */
1024 @Retention(RetentionPolicy.RUNTIME) // keep annotation at runtime
1025 public @interface pref { }
1026
1027 /**
1028 * Annotation used for converting objects to String Maps.
1029 * Indicates that a certain field should be written to the map, even if the value is the same as the default value.
1030 *
1031 * @see #serializeStruct(java.lang.Object, java.lang.Class)
1032 */
1033 @Retention(RetentionPolicy.RUNTIME) // keep annotation at runtime
1034 public @interface writeExplicitly { }
1035
1036 /**
1037 * Get a list of hashes which are represented by a struct-like class.
1038 * Possible properties are given by fields of the class klass that have the @pref annotation.
1039 * Default constructor is used to initialize the struct objects, properties then override some of these default values.
1040 * @param <T> klass type
1041 * @param key main preference key
1042 * @param klass The struct class
1043 * @return a list of objects of type T or an empty list if nothing was found
1044 */
1045 public <T> List<T> getListOfStructs(String key, Class<T> klass) {
1046 List<T> r = getListOfStructs(key, null, klass);
1047 if (r == null)
1048 return Collections.emptyList();
1049 else
1050 return r;
1051 }
1052
1053 /**
1054 * same as above, but returns def if nothing was found
1055 * @param <T> klass type
1056 * @param key main preference key
1057 * @param def default value
1058 * @param klass The struct class
1059 * @return a list of objects of type T or {@code def} if nothing was found
1060 */
1061 public <T> List<T> getListOfStructs(String key, Collection<T> def, Class<T> klass) {
1062 Collection<Map<String, String>> prop =
1063 getListOfStructs(key, def == null ? null : serializeListOfStructs(def, klass));
1064 if (prop == null)
1065 return def == null ? null : new ArrayList<>(def);
1066 List<T> lst = new ArrayList<>();
1067 for (Map<String, String> entries : prop) {
1068 T struct = deserializeStruct(entries, klass);
1069 lst.add(struct);
1070 }
1071 return lst;
1072 }
1073
1074 /**
1075 * Convenience method that saves a MapListSetting which is provided as a collection of objects.
1076 *
1077 * Each object is converted to a <code>Map&lt;String, String&gt;</code> using the fields with {@link pref} annotation.
1078 * The field name is the key and the value will be converted to a string.
1079 *
1080 * Considers only fields that have the @pref annotation.
1081 * In addition it does not write fields with null values. (Thus they are cleared)
1082 * Default values are given by the field values after default constructor has been called.
1083 * Fields equal to the default value are not written unless the field has the @writeExplicitly annotation.
1084 * @param <T> the class,
1085 * @param key main preference key
1086 * @param val the list that is supposed to be saved
1087 * @param klass The struct class
1088 * @return true if something has changed
1089 */
1090 public <T> boolean putListOfStructs(String key, Collection<T> val, Class<T> klass) {
1091 return putListOfStructs(key, serializeListOfStructs(val, klass));
1092 }
1093
1094 private static <T> Collection<Map<String, String>> serializeListOfStructs(Collection<T> l, Class<T> klass) {
1095 if (l == null)
1096 return null;
1097 Collection<Map<String, String>> vals = new ArrayList<>();
1098 for (T struct : l) {
1099 if (struct == null) {
1100 continue;
1101 }
1102 vals.add(serializeStruct(struct, klass));
1103 }
1104 return vals;
1105 }
1106
1107 @SuppressWarnings("rawtypes")
1108 private static String mapToJson(Map map) {
1109 StringWriter stringWriter = new StringWriter();
1110 try (JsonWriter writer = Json.createWriter(stringWriter)) {
1111 JsonObjectBuilder object = Json.createObjectBuilder();
1112 for (Object o: map.entrySet()) {
1113 Entry e = (Entry) o;
1114 Object evalue = e.getValue();
1115 object.add(e.getKey().toString(), evalue.toString());
1116 }
1117 writer.writeObject(object.build());
1118 }
1119 return stringWriter.toString();
1120 }
1121
1122 @SuppressWarnings({ "rawtypes", "unchecked" })
1123 private static Map mapFromJson(String s) {
1124 Map ret = null;
1125 try (JsonReader reader = Json.createReader(new StringReader(s))) {
1126 JsonObject object = reader.readObject();
1127 ret = new HashMap(object.size());
1128 for (Entry<String, JsonValue> e: object.entrySet()) {
1129 JsonValue value = e.getValue();
1130 if (value instanceof JsonString) {
1131 // in some cases, when JsonValue.toString() is called, then additional quotation marks are left in value
1132 ret.put(e.getKey(), ((JsonString) value).getString());
1133 } else {
1134 ret.put(e.getKey(), e.getValue().toString());
1135 }
1136 }
1137 }
1138 return ret;
1139 }
1140
1141 @SuppressWarnings("rawtypes")
1142 private static String multiMapToJson(MultiMap map) {
1143 StringWriter stringWriter = new StringWriter();
1144 try (JsonWriter writer = Json.createWriter(stringWriter)) {
1145 JsonObjectBuilder object = Json.createObjectBuilder();
1146 for (Object o: map.entrySet()) {
1147 Entry e = (Entry) o;
1148 Set evalue = (Set) e.getValue();
1149 JsonArrayBuilder a = Json.createArrayBuilder();
1150 for (Object evo: evalue) {
1151 a.add(evo.toString());
1152 }
1153 object.add(e.getKey().toString(), a.build());
1154 }
1155 writer.writeObject(object.build());
1156 }
1157 return stringWriter.toString();
1158 }
1159
1160 @SuppressWarnings({ "rawtypes", "unchecked" })
1161 private static MultiMap multiMapFromJson(String s) {
1162 MultiMap ret = null;
1163 try (JsonReader reader = Json.createReader(new StringReader(s))) {
1164 JsonObject object = reader.readObject();
1165 ret = new MultiMap(object.size());
1166 for (Entry<String, JsonValue> e: object.entrySet()) {
1167 JsonValue value = e.getValue();
1168 if (value instanceof JsonArray) {
1169 for (JsonString js: ((JsonArray) value).getValuesAs(JsonString.class)) {
1170 ret.put(e.getKey(), js.getString());
1171 }
1172 } else if (value instanceof JsonString) {
1173 // in some cases, when JsonValue.toString() is called, then additional quotation marks are left in value
1174 ret.put(e.getKey(), ((JsonString) value).getString());
1175 } else {
1176 ret.put(e.getKey(), e.getValue().toString());
1177 }
1178 }
1179 }
1180 return ret;
1181 }
1182
1183 /**
1184 * Convert an object to a String Map, by using field names and values as map key and value.
1185 *
1186 * The field value is converted to a String.
1187 *
1188 * Only fields with annotation {@link pref} are taken into account.
1189 *
1190 * Fields will not be written to the map if the value is null or unchanged
1191 * (compared to an object created with the no-arg-constructor).
1192 * The {@link writeExplicitly} annotation overrides this behavior, i.e. the default value will also be written.
1193 *
1194 * @param <T> the class of the object <code>struct</code>
1195 * @param struct the object to be converted
1196 * @param klass the class T
1197 * @return the resulting map (same data content as <code>struct</code>)
1198 */
1199 public static <T> Map<String, String> serializeStruct(T struct, Class<T> klass) {
1200 T structPrototype;
1201 try {
1202 structPrototype = klass.getConstructor().newInstance();
1203 } catch (ReflectiveOperationException ex) {
1204 throw new IllegalArgumentException(ex);
1205 }
1206
1207 Map<String, String> hash = new LinkedHashMap<>();
1208 for (Field f : klass.getDeclaredFields()) {
1209 if (f.getAnnotation(pref.class) == null) {
1210 continue;
1211 }
1212 Utils.setObjectsAccessible(f);
1213 try {
1214 Object fieldValue = f.get(struct);
1215 Object defaultFieldValue = f.get(structPrototype);
1216 if (fieldValue != null && (f.getAnnotation(writeExplicitly.class) != null || !Objects.equals(fieldValue, defaultFieldValue))) {
1217 String key = f.getName().replace('_', '-');
1218 if (fieldValue instanceof Map) {
1219 hash.put(key, mapToJson((Map<?, ?>) fieldValue));
1220 } else if (fieldValue instanceof MultiMap) {
1221 hash.put(key, multiMapToJson((MultiMap<?, ?>) fieldValue));
1222 } else {
1223 hash.put(key, fieldValue.toString());
1224 }
1225 }
1226 } catch (IllegalAccessException ex) {
1227 throw new RuntimeException(ex);
1228 }
1229 }
1230 return hash;
1231 }
1232
1233 /**
1234 * Converts a String-Map to an object of a certain class, by comparing map keys to field names of the class and assigning
1235 * map values to the corresponding fields.
1236 *
1237 * The map value (a String) is converted to the field type. Supported types are: boolean, Boolean, int, Integer, double,
1238 * Double, String, Map&lt;String, String&gt; and Map&lt;String, List&lt;String&gt;&gt;.
1239 *
1240 * Only fields with annotation {@link pref} are taken into account.
1241 * @param <T> the class
1242 * @param hash the string map with initial values
1243 * @param klass the class T
1244 * @return an object of class T, initialized as described above
1245 */
1246 public static <T> T deserializeStruct(Map<String, String> hash, Class<T> klass) {
1247 T struct = null;
1248 try {
1249 struct = klass.getConstructor().newInstance();
1250 } catch (ReflectiveOperationException ex) {
1251 throw new IllegalArgumentException(ex);
1252 }
1253 for (Entry<String, String> key_value : hash.entrySet()) {
1254 Object value;
1255 Field f;
1256 try {
1257 f = klass.getDeclaredField(key_value.getKey().replace('-', '_'));
1258 } catch (NoSuchFieldException ex) {
1259 Main.trace(ex);
1260 continue;
1261 }
1262 if (f.getAnnotation(pref.class) == null) {
1263 continue;
1264 }
1265 Utils.setObjectsAccessible(f);
1266 if (f.getType() == Boolean.class || f.getType() == boolean.class) {
1267 value = Boolean.valueOf(key_value.getValue());
1268 } else if (f.getType() == Integer.class || f.getType() == int.class) {
1269 try {
1270 value = Integer.valueOf(key_value.getValue());
1271 } catch (NumberFormatException nfe) {
1272 continue;
1273 }
1274 } else if (f.getType() == Double.class || f.getType() == double.class) {
1275 try {
1276 value = Double.valueOf(key_value.getValue());
1277 } catch (NumberFormatException nfe) {
1278 continue;
1279 }
1280 } else if (f.getType() == String.class) {
1281 value = key_value.getValue();
1282 } else if (f.getType().isAssignableFrom(Map.class)) {
1283 value = mapFromJson(key_value.getValue());
1284 } else if (f.getType().isAssignableFrom(MultiMap.class)) {
1285 value = multiMapFromJson(key_value.getValue());
1286 } else
1287 throw new RuntimeException("unsupported preference primitive type");
1288
1289 try {
1290 f.set(struct, value);
1291 } catch (IllegalArgumentException ex) {
1292 throw new AssertionError(ex);
1293 } catch (IllegalAccessException ex) {
1294 throw new RuntimeException(ex);
1295 }
1296 }
1297 return struct;
1298 }
1299
1300 public Map<String, Setting<?>> getAllSettings() {
1301 return new TreeMap<>(settingsMap);
1302 }
1303
1304 public Map<String, Setting<?>> getAllDefaults() {
1305 return new TreeMap<>(defaultsMap);
1306 }
1307
1308 /**
1309 * Updates system properties with the current values in the preferences.
1310 *
1311 */
1312 public void updateSystemProperties() {
1313 if ("true".equals(get("prefer.ipv6", "auto")) && !"true".equals(Utils.updateSystemProperty("java.net.preferIPv6Addresses", "true"))) {
1314 // never set this to false, only true!
1315 Main.info(tr("Try enabling IPv6 network, prefering IPv6 over IPv4 (only works on early startup)."));
1316 }
1317 Utils.updateSystemProperty("http.agent", Version.getInstance().getAgentString());
1318 Utils.updateSystemProperty("user.language", get("language"));
1319 // Workaround to fix a Java bug. This ugly hack comes from Sun bug database: https://bugs.openjdk.java.net/browse/JDK-6292739
1320 // Force AWT toolkit to update its internal preferences (fix #6345).
1321 if (!GraphicsEnvironment.isHeadless()) {
1322 try {
1323 Field field = Toolkit.class.getDeclaredField("resources");
1324 Utils.setObjectsAccessible(field);
1325 field.set(null, ResourceBundle.getBundle("sun.awt.resources.awt"));
1326 } catch (ReflectiveOperationException | MissingResourceException e) {
1327 Main.warn(e);
1328 }
1329 }
1330 // Possibility to disable SNI (not by default) in case of misconfigured https servers
1331 // See #9875 + http://stackoverflow.com/a/14884941/2257172
1332 // then https://josm.openstreetmap.de/ticket/12152#comment:5 for details
1333 if (getBoolean("jdk.tls.disableSNIExtension", false)) {
1334 Utils.updateSystemProperty("jsse.enableSNIExtension", "false");
1335 }
1336 }
1337
1338 /**
1339 * Replies the collection of plugin site URLs from where plugin lists can be downloaded.
1340 * @return the collection of plugin site URLs
1341 * @see #getOnlinePluginSites
1342 */
1343 public Collection<String> getPluginSites() {
1344 return getCollection("pluginmanager.sites", Collections.singleton(Main.getJOSMWebsite()+"/pluginicons%<?plugins=>"));
1345 }
1346
1347 /**
1348 * Returns the list of plugin sites available according to offline mode settings.
1349 * @return the list of available plugin sites
1350 * @since 8471
1351 */
1352 public Collection<String> getOnlinePluginSites() {
1353 Collection<String> pluginSites = new ArrayList<>(getPluginSites());
1354 for (Iterator<String> it = pluginSites.iterator(); it.hasNext();) {
1355 try {
1356 OnlineResource.JOSM_WEBSITE.checkOfflineAccess(it.next(), Main.getJOSMWebsite());
1357 } catch (OfflineAccessException ex) {
1358 Main.warn(ex, false);
1359 it.remove();
1360 }
1361 }
1362 return pluginSites;
1363 }
1364
1365 /**
1366 * Sets the collection of plugin site URLs.
1367 *
1368 * @param sites the site URLs
1369 */
1370 public void setPluginSites(Collection<String> sites) {
1371 putCollection("pluginmanager.sites", sites);
1372 }
1373
1374 /**
1375 * Returns XML describing these preferences.
1376 * @param nopass if password must be excluded
1377 * @return XML
1378 */
1379 public String toXML(boolean nopass) {
1380 return toXML(settingsMap.entrySet(), nopass, false);
1381 }
1382
1383 /**
1384 * Returns XML describing the given preferences.
1385 * @param settings preferences settings
1386 * @param nopass if password must be excluded
1387 * @param defaults true, if default values are converted to XML, false for
1388 * regular preferences
1389 * @return XML
1390 */
1391 public String toXML(Collection<Entry<String, Setting<?>>> settings, boolean nopass, boolean defaults) {
1392 try (
1393 StringWriter sw = new StringWriter();
1394 PreferencesWriter prefWriter = new PreferencesWriter(new PrintWriter(sw), nopass, defaults);
1395 ) {
1396 prefWriter.write(settings);
1397 sw.flush();
1398 return sw.toString();
1399 } catch (IOException e) {
1400 Main.error(e);
1401 return null;
1402 }
1403 }
1404
1405 /**
1406 * Removes obsolete preference settings. If you throw out a once-used preference
1407 * setting, add it to the list here with an expiry date (written as comment). If you
1408 * see something with an expiry date in the past, remove it from the list.
1409 * @param loadedVersion JOSM version when the preferences file was written
1410 */
1411 private void removeObsolete(int loadedVersion) {
1412 /* drop in October 2016 */
1413 if (loadedVersion < 9715) {
1414 Setting<?> setting = settingsMap.get("imagery.entries");
1415 if (setting instanceof MapListSetting) {
1416 List<Map<String, String>> l = new LinkedList<>();
1417 boolean modified = false;
1418 for (Map<String, String> map: ((MapListSetting) setting).getValue()) {
1419 Map<String, String> newMap = new HashMap<>();
1420 for (Entry<String, String> entry: map.entrySet()) {
1421 String value = entry.getValue();
1422 if ("noTileHeaders".equals(entry.getKey())) {
1423 value = value.replaceFirst("\":(\".*\")\\}", "\":[$1]}");
1424 if (!value.equals(entry.getValue())) {
1425 modified = true;
1426 }
1427 }
1428 newMap.put(entry.getKey(), value);
1429 }
1430 l.add(newMap);
1431 }
1432 if (modified) {
1433 putListOfStructs("imagery.entries", l);
1434 }
1435 }
1436 }
1437 // drop in November 2016
1438 removeUrlFromEntries(loadedVersion, 9965,
1439 "mappaint.style.entries",
1440 "josm.openstreetmap.de/josmfile?page=Styles/LegacyStandard");
1441 // drop in December 2016
1442 removeUrlFromEntries(loadedVersion, 10063,
1443 "validator.org.openstreetmap.josm.data.validation.tests.MapCSSTagChecker.entries",
1444 "resource://data/validator/power.mapcss");
1445
1446 for (String key : OBSOLETE_PREF_KEYS) {
1447 if (settingsMap.containsKey(key)) {
1448 settingsMap.remove(key);
1449 Main.info(tr("Preference setting {0} has been removed since it is no longer used.", key));
1450 }
1451 }
1452 }
1453
1454 private void removeUrlFromEntries(int loadedVersion, int versionMax, String key, String urlPart) {
1455 if (loadedVersion < versionMax) {
1456 Setting<?> setting = settingsMap.get(key);
1457 if (setting instanceof MapListSetting) {
1458 List<Map<String, String>> l = new LinkedList<>();
1459 boolean modified = false;
1460 for (Map<String, String> map: ((MapListSetting) setting).getValue()) {
1461 String url = map.get("url");
1462 if (url != null && url.contains(urlPart)) {
1463 modified = true;
1464 } else {
1465 l.add(map);
1466 }
1467 }
1468 if (modified) {
1469 putListOfStructs(key, l);
1470 }
1471 }
1472 }
1473 }
1474
1475 /**
1476 * Enables or not the preferences file auto-save mechanism (save each time a setting is changed).
1477 * This behaviour is enabled by default.
1478 * @param enable if {@code true}, makes JOSM save preferences file each time a setting is changed
1479 * @since 7085
1480 */
1481 public final void enableSaveOnPut(boolean enable) {
1482 synchronized (this) {
1483 saveOnPut = enable;
1484 }
1485 }
1486}
Note: See TracBrowser for help on using the repository browser.