Changeset 13241 in josm


Ignore:
Timestamp:
2017-12-25T18:08:44+01:00 (4 weeks ago)
Author:
Don-vip
Message:

fix #15694 - Remember confirmations of command actions (patch by george-hopkins, minor changes)

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/org/openstreetmap/josm/io/remotecontrol/handler/RequestHandler.java

    r12854 r13241  
    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
     
    3536    public static final String loadInNewLayerKey = "remotecontrol.new-layer";
    3637    public static final boolean loadInNewLayerDefault = false;
     38
     39    /** past confirmations */
     40    protected static PermissionCache PERMISSIONS = new PermissionCache();
    3741
    3842    /** The GET request arguments */
     
    153157            Logging.info(err);
    154158            throw new RequestHandlerForbiddenException(err);
     159        }
     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;
    155167        }
    156168
     
    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        }
     
    391406        }
    392407    }
     408
     409    static class PermissionCache {
     410        private final Set<Pair<String, String>> allowed = new HashSet<>();
     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}
Note: See TracChangeset for help on using the changeset viewer.