001// License: GPL. For details, see LICENSE file.
002package org.openstreetmap.josm.io.remotecontrol;
003
004import static org.openstreetmap.josm.tools.I18n.tr;
005
006import java.util.ArrayList;
007import java.util.Collections;
008import java.util.List;
009import java.util.stream.Collectors;
010
011import org.openstreetmap.josm.spi.preferences.Config;
012
013/**
014 * Contains a preference name to control permission for the operation
015 * implemented by the RequestHandler, and an error message to be displayed if
016 * not permitted.
017 *
018 * @author Bodo Meissner
019 */
020public class PermissionPrefWithDefault {
021    private static final List<PermissionPrefWithDefault> PREFS = new ArrayList<>();
022
023    /** Load data from API */
024    public static final PermissionPrefWithDefault LOAD_DATA =
025            new PermissionPrefWithDefault("remotecontrol.permission.load-data", true, tr("Load data from API"));
026    /** Import data from URL */
027    public static final PermissionPrefWithDefault IMPORT_DATA =
028            new PermissionPrefWithDefault("remotecontrol.permission.import", true, tr("Import data from URL"));
029    /** Open local files */
030    public static final PermissionPrefWithDefault OPEN_FILES =
031            new PermissionPrefWithDefault("remotecontrol.permission.open-files", false, tr("Open local files"));
032    /** Open web files */
033    public static final PermissionPrefWithDefault ALLOW_WEB_RESOURCES =
034            new PermissionPrefWithDefault("remotecontrol.permission.open-remote-files", false, tr("Open remote files"));
035    /** Load imagery layers */
036    public static final PermissionPrefWithDefault LOAD_IMAGERY =
037            new PermissionPrefWithDefault("remotecontrol.permission.imagery", true, tr("Load imagery layers"));
038    /** Change the selection */
039    public static final PermissionPrefWithDefault CHANGE_SELECTION =
040            new PermissionPrefWithDefault("remotecontrol.permission.change-selection", true, tr("Change the selection"));
041    /** Change the viewport */
042    public static final PermissionPrefWithDefault CHANGE_VIEWPORT =
043            new PermissionPrefWithDefault("remotecontrol.permission.change-viewport", true, tr("Change the viewport"));
044    /** Create new objects */
045    public static final PermissionPrefWithDefault CREATE_OBJECTS =
046            new PermissionPrefWithDefault("remotecontrol.permission.create-objects", true, tr("Create new objects"));
047    /** Read protocol version */
048    public static final PermissionPrefWithDefault READ_PROTOCOL_VERSION =
049            new PermissionPrefWithDefault("remotecontrol.permission.read-protocolversion", true, tr("Read protocol version"));
050
051    /**
052     * name of the preference setting to permit the remote operation
053     */
054    public final String pref;
055    /**
056     * default preference setting
057     */
058    public final boolean defaultVal;
059    /**
060     * text for the preference dialog checkbox
061     */
062    public final String preferenceText;
063
064    /**
065     * Create a new {@code PermissionPrefWithDefault}
066     *
067     * @param pref           The preference key for the permission
068     * @param defaultVal     The default value of the preference
069     * @param preferenceText The text to show in UI objects
070     */
071    public PermissionPrefWithDefault(String pref, boolean defaultVal, String preferenceText) {
072        this.pref = pref;
073        this.defaultVal = defaultVal;
074        this.preferenceText = preferenceText;
075    }
076
077    /**
078     * Determines if the action is allowed.
079     * @return true if the action is allowed
080     */
081    public boolean isAllowed() {
082        return Config.getPref().getBoolean(pref, defaultVal);
083    }
084
085    /**
086     * Returns a non-modifiable list of permission preferences for Remote Control.
087     * @return A non-modifiable list of permission preferences for Remote Control
088     */
089    public static List<PermissionPrefWithDefault> getPermissionPrefs() {
090        if (PREFS.isEmpty())
091            RequestProcessor.initialize();
092        return Collections.unmodifiableList(PREFS);
093    }
094
095    /**
096     * Adds a permission preference.
097     * @param pref The preference to add to the list returned by
098     *             {@link PermissionPrefWithDefault#getPermissionPrefs}
099     * @since 15500
100     */
101    public static void addPermissionPref(PermissionPrefWithDefault pref) {
102        if (pref.pref != null && PREFS.parallelStream().noneMatch(tPref -> pref.pref.equals(tPref.pref)))
103            PREFS.add(pref);
104    }
105
106    /**
107     * Removes a permission preference.
108     * @param pref The preference to remove from the list returned by
109     *             {@link PermissionPrefWithDefault#getPermissionPrefs}
110     *
111     * @return see {@link List#removeAll}
112     * @since 15500
113     */
114    public static boolean removePermissionPref(PermissionPrefWithDefault pref) {
115        List<PermissionPrefWithDefault> toRemove = Collections.emptyList();
116        if (pref.pref != null)
117            toRemove = PREFS.parallelStream().filter(tPref -> pref.pref.equals(tPref.pref))
118                    .collect(Collectors.toList());
119        return PREFS.removeAll(toRemove);
120    }
121}