1 | // License: GPL. For details, see LICENSE file.
|
---|
2 | package org.openstreetmap.josm.io.remotecontrol;
|
---|
3 |
|
---|
4 | import static org.openstreetmap.josm.tools.I18n.tr;
|
---|
5 |
|
---|
6 | import java.util.ArrayList;
|
---|
7 | import java.util.Collections;
|
---|
8 | import java.util.List;
|
---|
9 | import java.util.stream.Collectors;
|
---|
10 |
|
---|
11 | import org.openstreetmap.josm.spi.preferences.Config;
|
---|
12 |
|
---|
13 | /**
|
---|
14 | * Contains a preference name to control permission for the operation
|
---|
15 | * implemented by the RequestHandler, and an error message to be displayed if
|
---|
16 | * not permitted.
|
---|
17 | *
|
---|
18 | * @author Bodo Meissner
|
---|
19 | */
|
---|
20 | public class PermissionPrefWithDefault {
|
---|
21 | private static final List<PermissionPrefWithDefault> PREFS = new ArrayList<>();
|
---|
22 |
|
---|
23 | /** Load data from API */
|
---|
24 | public static final PermissionPrefWithDefault LOAD_DATA =
|
---|
25 | new PermissionPrefWithDefault("remotecontrol.permission.load-data", true, tr("Load data from API"));
|
---|
26 | /** Import data from URL */
|
---|
27 | public static final PermissionPrefWithDefault IMPORT_DATA =
|
---|
28 | new PermissionPrefWithDefault("remotecontrol.permission.import", true, tr("Import data from URL"));
|
---|
29 | /** Open local files */
|
---|
30 | public static final PermissionPrefWithDefault OPEN_FILES =
|
---|
31 | new PermissionPrefWithDefault("remotecontrol.permission.open-files", false, tr("Open local files"));
|
---|
32 | /** Load imagery layers */
|
---|
33 | public static final PermissionPrefWithDefault LOAD_IMAGERY =
|
---|
34 | new PermissionPrefWithDefault("remotecontrol.permission.imagery", true, tr("Load imagery layers"));
|
---|
35 | /** Change the selection */
|
---|
36 | public static final PermissionPrefWithDefault CHANGE_SELECTION =
|
---|
37 | new PermissionPrefWithDefault("remotecontrol.permission.change-selection", true, tr("Change the selection"));
|
---|
38 | /** Change the viewport */
|
---|
39 | public static final PermissionPrefWithDefault CHANGE_VIEWPORT =
|
---|
40 | new PermissionPrefWithDefault("remotecontrol.permission.change-viewport", true, tr("Change the viewport"));
|
---|
41 | /** Create new objects */
|
---|
42 | public static final PermissionPrefWithDefault CREATE_OBJECTS =
|
---|
43 | new PermissionPrefWithDefault("remotecontrol.permission.create-objects", true, tr("Create new objects"));
|
---|
44 | /** Read protocol version */
|
---|
45 | public static final PermissionPrefWithDefault READ_PROTOCOL_VERSION =
|
---|
46 | new PermissionPrefWithDefault("remotecontrol.permission.read-protocolversion", true, tr("Read protocol version"));
|
---|
47 | /**
|
---|
48 | * name of the preference setting to permit the remote operation
|
---|
49 | */
|
---|
50 | public final String pref;
|
---|
51 | /**
|
---|
52 | * default preference setting
|
---|
53 | */
|
---|
54 | public final boolean defaultVal;
|
---|
55 | /**
|
---|
56 | * text for the preference dialog checkbox
|
---|
57 | */
|
---|
58 | public final String preferenceText;
|
---|
59 |
|
---|
60 | /**
|
---|
61 | * Create a new {@code PermissionPrefWithDefault}
|
---|
62 | *
|
---|
63 | * @param pref The preference key for the permission
|
---|
64 | * @param defaultVal The default value of the preference
|
---|
65 | * @param preferenceText The text to show in UI objects
|
---|
66 | */
|
---|
67 | public PermissionPrefWithDefault(String pref, boolean defaultVal, String preferenceText) {
|
---|
68 | this.pref = pref;
|
---|
69 | this.defaultVal = defaultVal;
|
---|
70 | this.preferenceText = preferenceText;
|
---|
71 | }
|
---|
72 |
|
---|
73 | /**
|
---|
74 | * Determines if the action is allowed.
|
---|
75 | * @return true if the action is allowed
|
---|
76 | */
|
---|
77 | public boolean isAllowed() {
|
---|
78 | return Config.getPref().getBoolean(pref, defaultVal);
|
---|
79 | }
|
---|
80 |
|
---|
81 | /**
|
---|
82 | * Returns a non-modifiable list of permission preferences for Remote Control.
|
---|
83 | * @return A non-modifiable list of permission preferences for Remote Control
|
---|
84 | */
|
---|
85 | public static List<PermissionPrefWithDefault> getPermissionPrefs() {
|
---|
86 | if (PREFS.isEmpty())
|
---|
87 | RequestProcessor.initialize();
|
---|
88 | return Collections.unmodifiableList(PREFS);
|
---|
89 | }
|
---|
90 |
|
---|
91 | /**
|
---|
92 | * Adds a permission preference.
|
---|
93 | * @param pref The preference to add to the list returned by
|
---|
94 | * {@link PermissionPrefWithDefault#getPermissionPrefs}
|
---|
95 | * @since 15500
|
---|
96 | */
|
---|
97 | public static void addPermissionPref(PermissionPrefWithDefault pref) {
|
---|
98 | if (pref.pref != null && PREFS.parallelStream().noneMatch(tPref -> pref.pref.equals(tPref.pref)))
|
---|
99 | PREFS.add(pref);
|
---|
100 | }
|
---|
101 |
|
---|
102 | /**
|
---|
103 | * Removes a permission preference.
|
---|
104 | * @param pref The preference to remove from the list returned by
|
---|
105 | * {@link PermissionPrefWithDefault#getPermissionPrefs}
|
---|
106 | *
|
---|
107 | * @return see {@link List#removeAll}
|
---|
108 | * @since 15500
|
---|
109 | */
|
---|
110 | public static boolean removePermissionPref(PermissionPrefWithDefault pref) {
|
---|
111 | List<PermissionPrefWithDefault> toRemove = Collections.emptyList();
|
---|
112 | if (pref.pref != null)
|
---|
113 | toRemove = PREFS.parallelStream().filter(tPref -> pref.pref.equals(tPref.pref))
|
---|
114 | .collect(Collectors.toList());
|
---|
115 | return PREFS.removeAll(toRemove);
|
---|
116 | }
|
---|
117 | }
|
---|