001// License: GPL. For details, see LICENSE file.
002package org.openstreetmap.josm.data.imagery;
003
004import static java.util.function.Predicate.not;
005import static org.openstreetmap.josm.tools.I18n.tr;
006
007import java.io.IOException;
008import java.util.ArrayList;
009import java.util.Arrays;
010import java.util.Collection;
011import java.util.Collections;
012import java.util.HashMap;
013import java.util.HashSet;
014import java.util.List;
015import java.util.Map;
016import java.util.Objects;
017import java.util.Set;
018import java.util.TreeSet;
019import java.util.concurrent.ExecutorService;
020import java.util.stream.Collectors;
021
022import org.openstreetmap.josm.data.StructUtils;
023import org.openstreetmap.josm.data.imagery.ImageryInfo.ImageryPreferenceEntry;
024import org.openstreetmap.josm.gui.PleaseWaitRunnable;
025import org.openstreetmap.josm.io.CachedFile;
026import org.openstreetmap.josm.io.NetworkManager;
027import org.openstreetmap.josm.io.imagery.ImageryReader;
028import org.openstreetmap.josm.spi.preferences.Config;
029import org.openstreetmap.josm.tools.Logging;
030import org.openstreetmap.josm.tools.Utils;
031import org.xml.sax.SAXException;
032
033/**
034 * Manages the list of imagery entries that are shown in the imagery menu.
035 */
036public class ImageryLayerInfo {
037
038    /** Unique instance */
039    public static final ImageryLayerInfo instance = new ImageryLayerInfo();
040    /** List of all usable layers */
041    private final List<ImageryInfo> layers = new ArrayList<>();
042    /** List of layer ids of all usable layers */
043    private final Map<String, ImageryInfo> layerIds = new HashMap<>();
044    /** List of all available default layers */
045    static final List<ImageryInfo> defaultLayers = new ArrayList<>();
046    /** List of all available default layers (including mirrors) */
047    static final List<ImageryInfo> allDefaultLayers = new ArrayList<>();
048    /** List of all layer ids of available default layers (including mirrors) */
049    static final Map<String, ImageryInfo> defaultLayerIds = new HashMap<>();
050
051    private static final String[] DEFAULT_LAYER_SITES = {
052            Config.getUrls().getJOSMWebsite()+"/maps%<?ids=>"
053    };
054
055    /**
056     * Returns the list of imagery layers sites.
057     * @return the list of imagery layers sites
058     * @since 7434
059     */
060    public static Collection<String> getImageryLayersSites() {
061        return Config.getPref().getList("imagery.layers.sites", Arrays.asList(DEFAULT_LAYER_SITES));
062    }
063
064    private ImageryLayerInfo() {
065    }
066
067    /**
068     * Constructs a new {@code ImageryLayerInfo} from an existing one.
069     * @param info info to copy
070     */
071    public ImageryLayerInfo(ImageryLayerInfo info) {
072        layers.addAll(info.layers);
073    }
074
075    /**
076     * Clear the lists of layers.
077     */
078    public void clear() {
079        layers.clear();
080        layerIds.clear();
081    }
082
083    /**
084     * Loads the custom as well as default imagery entries.
085     * @param fastFail whether opening HTTP connections should fail fast, see {@link ImageryReader#setFastFail(boolean)}
086     */
087    public void load(boolean fastFail) {
088        clear();
089        List<ImageryPreferenceEntry> entries = StructUtils.getListOfStructs(
090                Config.getPref(), "imagery.entries", null, ImageryPreferenceEntry.class);
091        if (entries != null) {
092            for (ImageryPreferenceEntry prefEntry : entries) {
093                try {
094                    ImageryInfo i = new ImageryInfo(prefEntry);
095                    add(i);
096                } catch (IllegalArgumentException e) {
097                    Logging.warn("Unable to load imagery preference entry:"+e);
098                }
099            }
100            Collections.sort(layers);
101        }
102        loadDefaults(false, null, fastFail);
103    }
104
105    /**
106     * Loads the available imagery entries.
107     *
108     * The data is downloaded from the JOSM website (or loaded from cache).
109     * Entries marked as "default" are added to the user selection, if not already present.
110     *
111     * @param clearCache if true, clear the cache and start a fresh download.
112     * @param worker executor service which will perform the loading.
113     * If null, it should be performed using a {@link PleaseWaitRunnable} in the background
114     * @param fastFail whether opening HTTP connections should fail fast, see {@link ImageryReader#setFastFail(boolean)}
115     * @since 12634
116     */
117    public void loadDefaults(boolean clearCache, ExecutorService worker, boolean fastFail) {
118        final DefaultEntryLoader loader = new DefaultEntryLoader(clearCache, fastFail);
119        if (worker == null) {
120            loader.realRun();
121            loader.finish();
122        } else {
123            worker.execute(loader);
124        }
125    }
126
127    /**
128     * Loader/updater of the available imagery entries
129     */
130    class DefaultEntryLoader extends PleaseWaitRunnable {
131
132        private final boolean clearCache;
133        private final boolean fastFail;
134        private final List<ImageryInfo> newLayers = new ArrayList<>();
135        private ImageryReader reader;
136        private boolean canceled;
137        private boolean loadError;
138
139        DefaultEntryLoader(boolean clearCache, boolean fastFail) {
140            super(tr("Update default entries"));
141            this.clearCache = clearCache;
142            this.fastFail = fastFail;
143        }
144
145        @Override
146        protected void cancel() {
147            canceled = true;
148            Utils.close(reader);
149        }
150
151        @Override
152        protected void realRun() {
153            for (String source : getImageryLayersSites()) {
154                if (canceled) {
155                    return;
156                }
157                loadSource(source);
158            }
159        }
160
161        protected void loadSource(String source) {
162            boolean online = !NetworkManager.isOffline(source);
163            if (clearCache && online) {
164                CachedFile.cleanup(source);
165            }
166            try {
167                reader = new ImageryReader(source);
168                reader.setFastFail(fastFail);
169                Collection<ImageryInfo> result = reader.parse();
170                // See #23485 (remove invalid source entries)
171                result.stream().filter(not(ImageryInfo::isValid)).forEach(info -> Logging.error("Not adding invalid imagery: {0}", info));
172                result.removeIf(not(ImageryInfo::isValid));
173                newLayers.addAll(result);
174            } catch (IOException ex) {
175                loadError = true;
176                Logging.log(Logging.LEVEL_ERROR, ex);
177            } catch (SAXException ex) {
178                loadError = true;
179                Logging.error(ex);
180            }
181        }
182
183        @Override
184        protected void finish() {
185            defaultLayers.clear();
186            allDefaultLayers.clear();
187            defaultLayers.addAll(newLayers);
188            for (ImageryInfo layer : newLayers) {
189                allDefaultLayers.add(layer);
190                allDefaultLayers.addAll(layer.getMirrors());
191            }
192            defaultLayerIds.clear();
193            Collections.sort(defaultLayers);
194            Collections.sort(allDefaultLayers);
195            buildIdMap(allDefaultLayers, defaultLayerIds);
196            updateEntriesFromDefaults(!loadError);
197            buildIdMap(layers, layerIds);
198            if (!loadError && !defaultLayerIds.isEmpty()) {
199                dropOldEntries();
200            }
201        }
202    }
203
204    /**
205     * Build the mapping of unique ids to {@link ImageryInfo}s.
206     * @param lst input list
207     * @param idMap output map
208     */
209    private static void buildIdMap(List<ImageryInfo> lst, Map<String, ImageryInfo> idMap) {
210        idMap.clear();
211        Set<String> notUnique = new HashSet<>();
212        for (ImageryInfo i : lst) {
213            if (i.getId() != null) {
214                if (idMap.containsKey(i.getId())) {
215                    notUnique.add(i.getId());
216                    Logging.error("Id ''{0}'' is not unique - used by ''{1}'' and ''{2}''!",
217                            i.getId(), i.getName(), idMap.get(i.getId()).getName());
218                    continue;
219                }
220                idMap.put(i.getId(), i);
221                Collection<String> old = i.getOldIds();
222                if (old != null) {
223                    for (String id : old) {
224                        if (idMap.containsKey(id)) {
225                            Logging.error("Old Id ''{0}'' is not unique - used by ''{1}'' and ''{2}''!",
226                                    i.getId(), i.getName(), idMap.get(i.getId()).getName());
227                        } else {
228                            idMap.put(id, i);
229                        }
230                    }
231                }
232            }
233        }
234        for (String i : notUnique) {
235            idMap.remove(i);
236        }
237    }
238
239    /**
240     * Update user entries according to the list of default entries.
241     * @param dropold if <code>true</code> old entries should be removed
242     * @since 11706
243     */
244    public void updateEntriesFromDefaults(boolean dropold) {
245        // add new default entries to the user selection
246        boolean changed = false;
247        Collection<String> knownDefaults = new TreeSet<>(Config.getPref().getList("imagery.layers.default"));
248        Collection<String> newKnownDefaults = new TreeSet<>();
249        for (ImageryInfo def : defaultLayers) {
250            if (def.isDefaultEntry()) {
251                boolean isKnownDefault = false;
252                for (String entry : knownDefaults) {
253                    if (entry.equals(def.getId())) {
254                        isKnownDefault = true;
255                        newKnownDefaults.add(entry);
256                        knownDefaults.remove(entry);
257                        break;
258                    } else if (isSimilar(entry, def.getUrl())) {
259                        isKnownDefault = true;
260                        if (def.getId() != null) {
261                            newKnownDefaults.add(def.getId());
262                        }
263                        knownDefaults.remove(entry);
264                        break;
265                    }
266                }
267                boolean isInUserList = false;
268                if (!isKnownDefault) {
269                    if (def.getId() != null) {
270                        newKnownDefaults.add(def.getId());
271                        isInUserList = layers.stream().anyMatch(i -> isSimilar(def, i));
272                    } else {
273                        Logging.error("Default imagery ''{0}'' has no id. Skipping.", def.getName());
274                    }
275                }
276                if (!isKnownDefault && !isInUserList) {
277                    add(new ImageryInfo(def));
278                    changed = true;
279                }
280            }
281        }
282        if (!dropold && !knownDefaults.isEmpty()) {
283            newKnownDefaults.addAll(knownDefaults);
284        }
285        Config.getPref().putList("imagery.layers.default", new ArrayList<>(newKnownDefaults));
286
287        // automatically update user entries with same id as a default entry
288        for (int i = 0; i < layers.size(); i++) {
289            ImageryInfo info = layers.get(i);
290            if (info.getId() == null) {
291                continue;
292            }
293            ImageryInfo matchingDefault = defaultLayerIds.get(info.getId());
294            if (matchingDefault != null && !matchingDefault.equalsPref(info)) {
295                layers.set(i, matchingDefault);
296                Logging.info(tr("Update imagery ''{0}''", info.getName()));
297                changed = true;
298            }
299        }
300
301        if (changed) {
302            save();
303        }
304    }
305
306    /**
307     * Drop entries with Id which do no longer exist (removed from defaults).
308     * @since 11527
309     */
310    public void dropOldEntries() {
311        List<String> drop = new ArrayList<>();
312
313        for (Map.Entry<String, ImageryInfo> info : layerIds.entrySet()) {
314            if (!defaultLayerIds.containsKey(info.getKey())) {
315                remove(info.getValue());
316                drop.add(info.getKey());
317                Logging.info(tr("Drop old imagery ''{0}''", info.getValue().getName()));
318            }
319        }
320
321        if (!drop.isEmpty()) {
322            for (String id : drop) {
323                layerIds.remove(id);
324            }
325            save();
326        }
327    }
328
329    private static boolean isSimilar(ImageryInfo iiA, ImageryInfo iiB) {
330        if (iiA == null || iiA.getImageryType() != iiB.getImageryType())
331            return false;
332        if (iiA.getId() != null && iiB.getId() != null)
333            return iiA.getId().equals(iiB.getId());
334        return isSimilar(iiA.getUrl(), iiB.getUrl());
335    }
336
337    // some additional checks to respect extended URLs in preferences (legacy workaround)
338    private static boolean isSimilar(String a, String b) {
339        return Objects.equals(a, b) || (!Utils.isEmpty(a) && !Utils.isEmpty(b) && (a.contains(b) || b.contains(a)));
340    }
341
342    /**
343     * Add a new imagery entry.
344     * @param info imagery entry to add
345     */
346    public void add(ImageryInfo info) {
347        layers.add(info);
348    }
349
350    /**
351     * Remove an imagery entry.
352     * @param info imagery entry to remove
353     */
354    public void remove(ImageryInfo info) {
355        layers.remove(info);
356    }
357
358    /**
359     * Save the list of imagery entries to preferences.
360     */
361    public void save() {
362        List<ImageryPreferenceEntry> entries = layers.stream()
363                .map(ImageryPreferenceEntry::new)
364                .collect(Collectors.toList());
365        StructUtils.putListOfStructs(Config.getPref(), "imagery.entries", entries, ImageryPreferenceEntry.class);
366    }
367
368    /**
369     * List of usable layers
370     * @return unmodifiable list containing usable layers
371     */
372    public List<ImageryInfo> getLayers() {
373        return Collections.unmodifiableList(layers);
374    }
375
376    /**
377     * List of available default layers
378     * @return unmodifiable list containing available default layers
379     */
380    public List<ImageryInfo> getDefaultLayers() {
381        return Collections.unmodifiableList(defaultLayers);
382    }
383
384    /**
385     * List of all available default layers (including mirrors)
386     * @return unmodifiable list containing available default layers
387     * @since 11570
388     */
389    public List<ImageryInfo> getAllDefaultLayers() {
390        return Collections.unmodifiableList(allDefaultLayers);
391    }
392
393    public static void addLayer(ImageryInfo info) {
394        instance.add(info);
395        instance.save();
396    }
397
398    public static void addLayers(Collection<ImageryInfo> infos) {
399        for (ImageryInfo i : infos) {
400            instance.add(i);
401        }
402        instance.save();
403        Collections.sort(instance.layers);
404    }
405
406    /**
407     * Get unique id for ImageryInfo.
408     *
409     * This takes care, that no id is used twice (due to a user error)
410     * @param info the ImageryInfo to look up
411     * @return null, if there is no id or the id is used twice,
412     * the corresponding id otherwise
413     */
414    public String getUniqueId(ImageryInfo info) {
415        if (info.getId() != null && layerIds.get(info.getId()) == info) {
416            return info.getId();
417        }
418        return null;
419    }
420
421    /**
422     * Returns imagery layer info for the given id.
423     * @param id imagery layer id.
424     * @return imagery layer info for the given id, or {@code null}
425     * @since 13797
426     */
427    public ImageryInfo getLayer(String id) {
428        return layerIds.get(id);
429    }
430}