| 1 | // License: GPL. For details, see LICENSE file.
|
|---|
| 2 | package org.openstreetmap.josm.io.remotecontrol.handler;
|
|---|
| 3 |
|
|---|
| 4 | import static org.openstreetmap.josm.tools.I18n.tr;
|
|---|
| 5 |
|
|---|
| 6 | import java.io.File;
|
|---|
| 7 | import java.util.Arrays;
|
|---|
| 8 | import java.util.EnumSet;
|
|---|
| 9 |
|
|---|
| 10 | import org.openstreetmap.josm.actions.OpenFileAction;
|
|---|
| 11 | import org.openstreetmap.josm.gui.io.importexport.Options;
|
|---|
| 12 | import org.openstreetmap.josm.gui.util.GuiHelper;
|
|---|
| 13 | import org.openstreetmap.josm.io.remotecontrol.PermissionPrefWithDefault;
|
|---|
| 14 |
|
|---|
| 15 | /**
|
|---|
| 16 | * Opens a local file
|
|---|
| 17 | */
|
|---|
| 18 | public class OpenFileHandler extends RequestHandler {
|
|---|
| 19 |
|
|---|
| 20 | /**
|
|---|
| 21 | * The remote control command name used to open a local file.
|
|---|
| 22 | */
|
|---|
| 23 | public static final String command = "open_file";
|
|---|
| 24 |
|
|---|
| 25 | @Override
|
|---|
| 26 | public String[] getMandatoryParams() {
|
|---|
| 27 | return new String[]{"filename"};
|
|---|
| 28 | }
|
|---|
| 29 |
|
|---|
| 30 | @Override
|
|---|
| 31 | public String getUsage() {
|
|---|
| 32 | return "opens a local file in JOSM";
|
|---|
| 33 | }
|
|---|
| 34 |
|
|---|
| 35 | @Override
|
|---|
| 36 | public String[] getUsageExamples() {
|
|---|
| 37 | return new String[] {"/open_file?filename=/tmp/test.osm"};
|
|---|
| 38 | }
|
|---|
| 39 |
|
|---|
| 40 | @Override
|
|---|
| 41 | public PermissionPrefWithDefault getPermissionPref() {
|
|---|
| 42 | return PermissionPrefWithDefault.OPEN_FILES;
|
|---|
| 43 | }
|
|---|
| 44 |
|
|---|
| 45 | @Override
|
|---|
| 46 | protected void handleRequest() throws RequestHandlerErrorException, RequestHandlerBadRequestException {
|
|---|
| 47 | EnumSet<Options> options = EnumSet.noneOf(Options.class);
|
|---|
| 48 | if (PermissionPrefWithDefault.ALLOW_WEB_RESOURCES.isAllowed()) {
|
|---|
| 49 | options.add(Options.ALLOW_WEB_RESOURCES);
|
|---|
| 50 | }
|
|---|
| 51 | GuiHelper.runInEDT(() ->
|
|---|
| 52 | OpenFileAction.openFiles(Arrays.asList(new File(args.get("filename"))), options.toArray(new Options[0])));
|
|---|
| 53 | }
|
|---|
| 54 |
|
|---|
| 55 | @Override
|
|---|
| 56 | public String getPermissionMessage() {
|
|---|
| 57 | return tr("Remote Control has been asked to open a local file.");
|
|---|
| 58 | }
|
|---|
| 59 |
|
|---|
| 60 | @Override
|
|---|
| 61 | protected void validateRequest() throws RequestHandlerBadRequestException {
|
|---|
| 62 | // Nothing to do
|
|---|
| 63 | }
|
|---|
| 64 | }
|
|---|