001// License: GPL. For details, see LICENSE file.
002package org.openstreetmap.josm.io;
003
004import static org.openstreetmap.josm.tools.I18n.tr;
005
006import org.openstreetmap.josm.Main;
007
008/**
009 * Online resources directly used by JOSM.
010 * This does not include websites where user can sometimes be redirected through its web browser,
011 * but only those to we establish a connection.
012 *
013 * @since 7434
014 */
015public enum OnlineResource {
016
017    /** The OSM API, used for download, upload, history, etc. */
018    OSM_API(tr("OSM API")),
019    /** The JOSM website, used for startup page, imagery/presets/styles/rules entries, help, etc. */
020    JOSM_WEBSITE(tr("JOSM website")),
021    /** Value used to represent all online resources */
022    ALL(tr("All"));
023
024    private final String locName;
025
026    OnlineResource(String locName) {
027        this.locName = locName;
028    }
029
030    /**
031     * Replies the localized name.
032     * @return the localized name
033     */
034    public final String getLocName() {
035        return locName;
036    }
037
038    /**
039     * Ensures resource is not accessed in offline mode.
040     * @param downloadString The attempted download string
041     * @param resourceString The resource download string that should not be accessed
042     * @throws OfflineAccessException if resource is accessed in offline mode, in any protocol
043     */
044    public final void checkOfflineAccess(String downloadString, String resourceString) {
045        if (Main.isOffline(this) && downloadString.substring(downloadString.indexOf("://"))
046                .startsWith(resourceString.substring(resourceString.indexOf("://")))) {
047            throw new OfflineAccessException(tr("Unable to access ''{0}'': {1} not available (offline mode)", downloadString, getLocName()));
048        }
049    }
050}