From 2d5cb003305106f8663042b6dff547c0edefc1b6 Mon Sep 17 00:00:00 2001
From: George Hopkins <george-hopkins@null.net>
Date: Mon, 25 Dec 2017 17:29:59 +0100
Subject: [PATCH] Remember confirmations of command actions
---
.../io/remotecontrol/handler/RequestHandler.java | 41 +++++++++++++++++++---
1 file changed, 36 insertions(+), 5 deletions(-)
diff --git a/src/org/openstreetmap/josm/io/remotecontrol/handler/RequestHandler.java b/src/org/openstreetmap/josm/io/remotecontrol/handler/RequestHandler.java
index 6849dd2..7183e35 100644
|
a
|
b
|
import org.openstreetmap.josm.Main;
|
| 21 | 21 | import org.openstreetmap.josm.io.remotecontrol.PermissionPrefWithDefault; |
| 22 | 22 | import org.openstreetmap.josm.spi.preferences.Config; |
| 23 | 23 | import org.openstreetmap.josm.tools.Logging; |
| | 24 | import org.openstreetmap.josm.tools.Pair; |
| 24 | 25 | import org.openstreetmap.josm.tools.Utils; |
| 25 | 26 | |
| 26 | 27 | /** |
| … |
… |
public abstract class RequestHandler {
|
| 35 | 36 | public static final String loadInNewLayerKey = "remotecontrol.new-layer"; |
| 36 | 37 | public static final boolean loadInNewLayerDefault = false; |
| 37 | 38 | |
| | 39 | /** past confirmations */ |
| | 40 | protected static PermissionCache PERMISSIONS = new PermissionCache(); |
| | 41 | |
| 38 | 42 | /** The GET request arguments */ |
| 39 | 43 | protected Map<String, String> args; |
| 40 | 44 | |
| … |
… |
public abstract class RequestHandler {
|
| 154 | 158 | throw new RequestHandlerForbiddenException(err); |
| 155 | 159 | } |
| 156 | 160 | |
| | 161 | /* |
| | 162 | * Did the user confirm this action previously? |
| | 163 | * If yes, skip the global confirmation dialog. |
| | 164 | */ |
| | 165 | if (PERMISSIONS.isAllowed(myCommand, sender)) { |
| | 166 | return; |
| | 167 | } |
| | 168 | |
| 157 | 169 | /* Does the user want to confirm everything? |
| 158 | 170 | * If yes, display specific confirmation message. |
| 159 | 171 | */ |
| … |
… |
public abstract class RequestHandler {
|
| 166 | 178 | if (label.getPreferredSize().width > maxWidth) { |
| 167 | 179 | label.setText(message.replaceFirst("<div>", "<div style=\"width:" + maxWidth + "px;\">")); |
| 168 | 180 | } |
| 169 | | if (JOptionPane.showConfirmDialog(Main.parent, label, |
| 170 | | tr("Confirm Remote Control action"), |
| 171 | | JOptionPane.YES_NO_OPTION) != JOptionPane.YES_OPTION) { |
| 172 | | String err = MessageFormat.format("RemoteControl: ''{0}'' forbidden by user''s choice", myCommand); |
| 173 | | throw new RequestHandlerForbiddenException(err); |
| | 181 | Object[] choices = new Object[] { tr("Yes, always"), tr("Yes, once"), tr("No") }; |
| | 182 | int choice = JOptionPane.showOptionDialog(Main.parent, label, tr("Confirm Remote Control action"), |
| | 183 | JOptionPane.YES_NO_CANCEL_OPTION, JOptionPane.QUESTION_MESSAGE, null, choices, choices[1]); |
| | 184 | if (choice != JOptionPane.YES_OPTION && choice != JOptionPane.NO_OPTION) { // Yes/no refer to always/once |
| | 185 | String err = MessageFormat.format("RemoteControl: ''{0}'' forbidden by user''s choice", myCommand); |
| | 186 | throw new RequestHandlerForbiddenException(err); |
| | 187 | } else if (choice == JOptionPane.YES_OPTION) { |
| | 188 | PERMISSIONS.allow(myCommand, sender); |
| 174 | 189 | } |
| 175 | 190 | } |
| 176 | 191 | } |
| … |
… |
public abstract class RequestHandler {
|
| 390 | 405 | this.args = args; |
| 391 | 406 | } |
| 392 | 407 | } |
| | 408 | |
| | 409 | public static class PermissionCache { |
| | 410 | private final HashSet<Pair<String, String>> allowed = new HashSet<Pair<String, String>>(); |
| | 411 | |
| | 412 | public void allow(String command, String sender) { |
| | 413 | allowed.add(Pair.create(command, sender)); |
| | 414 | } |
| | 415 | |
| | 416 | public boolean isAllowed(String command, String sender) { |
| | 417 | return allowed.contains(Pair.create(command, sender)); |
| | 418 | } |
| | 419 | |
| | 420 | public void clear() { |
| | 421 | allowed.clear(); |
| | 422 | } |
| | 423 | } |
| 393 | 424 | } |