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    public static final PermissionPrefWithDefault LOAD_DATA =
024            new PermissionPrefWithDefault("remotecontrol.permission.load-data", true, tr("Load data from API"));
025    public static final PermissionPrefWithDefault IMPORT_DATA =
026            new PermissionPrefWithDefault("remotecontrol.permission.import", true, tr("Import data from URL"));
027    public static final PermissionPrefWithDefault OPEN_FILES =
028            new PermissionPrefWithDefault("remotecontrol.permission.open-files", false, tr("Open local files"));
029    public static final PermissionPrefWithDefault LOAD_IMAGERY =
030            new PermissionPrefWithDefault("remotecontrol.permission.imagery", true, tr("Load imagery layers"));
031    public static final PermissionPrefWithDefault CHANGE_SELECTION =
032            new PermissionPrefWithDefault("remotecontrol.permission.change-selection", true, tr("Change the selection"));
033    public static final PermissionPrefWithDefault CHANGE_VIEWPORT =
034            new PermissionPrefWithDefault("remotecontrol.permission.change-viewport", true, tr("Change the viewport"));
035    public static final PermissionPrefWithDefault CREATE_OBJECTS =
036            new PermissionPrefWithDefault("remotecontrol.permission.create-objects", true, tr("Create new objects"));
037    public static final PermissionPrefWithDefault READ_PROTOCOL_VERSION =
038            new PermissionPrefWithDefault("remotecontrol.permission.read-protocolversion", true, tr("Read protocol version"));
039    /**
040     * name of the preference setting to permit the remote operation
041     */
042    public final String pref;
043    /**
044     * default preference setting
045     */
046    public final boolean defaultVal;
047    /**
048     * text for the preference dialog checkbox
049     */
050    public final String preferenceText;
051
052    /**
053     * Create a new {@code PermissionPrefWithDefault}
054     *
055     * @param pref           The preference key for the permission
056     * @param defaultVal     The default value of the preference
057     * @param preferenceText The text to show in UI objects
058     */
059    public PermissionPrefWithDefault(String pref, boolean defaultVal, String preferenceText) {
060        this.pref = pref;
061        this.defaultVal = defaultVal;
062        this.preferenceText = preferenceText;
063    }
064
065    /**
066     * Determines if the action is allowed.
067     * @return true if the action is allowed
068     */
069    public boolean isAllowed() {
070        return Config.getPref().getBoolean(pref, defaultVal);
071    }
072
073    /**
074     * Returns a non-modifiable list of permission preferences for Remote Control.
075     * @return A non-modifiable list of permission preferences for Remote Control
076     */
077    public static List<PermissionPrefWithDefault> getPermissionPrefs() {
078        if (PREFS.isEmpty())
079            RequestProcessor.initialize();
080        return Collections.unmodifiableList(PREFS);
081    }
082
083    /**
084     * Adds a permission preference.
085     * @param pref The preference to add to the list returned by
086     *             {@link PermissionPrefWithDefault#getPermissionPrefs}
087     * @since 15500
088     */
089    public static void addPermissionPref(PermissionPrefWithDefault pref) {
090        if (pref.pref != null && PREFS.parallelStream().noneMatch(tPref -> pref.pref.equals(tPref.pref)))
091            PREFS.add(pref);
092    }
093
094    /**
095     * Removes a permission preference.
096     * @param pref The preference to remove from the list returned by
097     *             {@link PermissionPrefWithDefault#getPermissionPrefs}
098     *
099     * @return see {@link List#removeAll}
100     * @since 15500
101     */
102    public static boolean removePermissionPref(PermissionPrefWithDefault pref) {
103        List<PermissionPrefWithDefault> toRemove = Collections.emptyList();
104        if (pref.pref != null)
105            toRemove = PREFS.parallelStream().filter(tPref -> pref.pref.equals(tPref.pref))
106                    .collect(Collectors.toList());
107        return PREFS.removeAll(toRemove);
108    }
109}