Ticket #15694: confirmations.patch

File confirmations.patch, 3.8 KB (added by george-hopkins, 8 months ago)
  • src/org/openstreetmap/josm/io/remotecontrol/handler/RequestHandler.java

    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; 
    2121import org.openstreetmap.josm.io.remotecontrol.PermissionPrefWithDefault;
    2222import org.openstreetmap.josm.spi.preferences.Config;
    2323import org.openstreetmap.josm.tools.Logging;
     24import org.openstreetmap.josm.tools.Pair;
    2425import org.openstreetmap.josm.tools.Utils;
    2526
    2627/**
    public abstract class RequestHandler { 
    3536    public static final String loadInNewLayerKey = "remotecontrol.new-layer";
    3637    public static final boolean loadInNewLayerDefault = false;
    3738
     39    /** past confirmations */
     40    protected static PermissionCache PERMISSIONS = new PermissionCache();
     41
    3842    /** The GET request arguments */
    3943    protected Map<String, String> args;
    4044
    public abstract class RequestHandler { 
    154158            throw new RequestHandlerForbiddenException(err);
    155159        }
    156160
     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
    157169        /* Does the user want to confirm everything?
    158170         * If yes, display specific confirmation message.
    159171         */
    public abstract class RequestHandler { 
    166178            if (label.getPreferredSize().width > maxWidth) {
    167179                label.setText(message.replaceFirst("<div>", "<div style=\"width:" + maxWidth + "px;\">"));
    168180            }
    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);
    174189            }
    175190        }
    176191    }
    public abstract class RequestHandler { 
    390405            this.args = args;
    391406        }
    392407    }
     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    }
    393424}