Ignore:
Timestamp:
2013-04-17T20:14:32+02:00 (11 years ago)
Author:
akks
Message:

Remote control: allow adding tags without confirmation for current session (add_tags), see #8612
added parsing of request headers and detecting request sender by IP and "referer" HTTP header

File:
1 edited

Legend:

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

    r5845 r5876  
    1111import java.awt.event.KeyEvent;
    1212import java.awt.event.MouseEvent;
     13import java.io.UnsupportedEncodingException;
     14import java.net.URLDecoder;
    1315import java.util.Collection;
    1416import java.util.HashMap;
     17import java.util.HashSet;
     18import java.util.Map;
     19import java.util.Set;
    1520import javax.swing.AbstractAction;
     21import javax.swing.JCheckBox;
    1622
    1723import javax.swing.JPanel;
    1824import javax.swing.JTable;
    1925import javax.swing.KeyStroke;
    20 import javax.swing.event.CellEditorListener;
    21 import javax.swing.event.ChangeEvent;
    2226import javax.swing.table.DefaultTableModel;
    2327import javax.swing.table.TableCellEditor;
     
    3135import org.openstreetmap.josm.data.osm.OsmPrimitive;
    3236import org.openstreetmap.josm.gui.ExtendedDialog;
     37import org.openstreetmap.josm.gui.util.GuiHelper;
    3338import org.openstreetmap.josm.gui.util.TableHelper;
    3439import org.openstreetmap.josm.tools.GBC;
     
    4651
    4752
     53    /** initially given tags  **/
     54    String[][] tags;
     55   
    4856    private final JTable propertyTable;
    4957    private Collection<? extends OsmPrimitive> sel;
    5058    int[] count;
    5159
     60    String sender;
     61    static Set<String> trustedSenders = new HashSet<String>();
     62   
    5263    /**
    5364     * Class for displaying "delete from ... objects" in the table
     
    110121    }
    111122           
    112     public AddTagsDialog(String[][] tags) {
     123    public AddTagsDialog(String[][] tags, String senderName) {
    113124        super(Main.parent, tr("Add tags to selected objects"), new String[] { tr("Add selected tags"), tr("Add all tags"),  tr("Cancel")},
    114125                false,
    115126                true);
    116127        setToolTipTexts(new String[]{tr("Add checked tags to selected objects"), tr("Shift+Enter: Add all tags to selected objects"), ""});
    117      
     128
     129        this.sender = senderName;
     130       
    118131        DataSet.addSelectionListener(this);
    119132
     
    211224        tablePanel.add(propertyTable.getTableHeader(), GBC.eol().fill(GBC.HORIZONTAL));
    212225        tablePanel.add(propertyTable, GBC.eol().fill(GBC.BOTH));
     226        if (!trustedSenders.contains(sender)) {
     227            final JCheckBox c = new JCheckBox();
     228            c.setAction(new AbstractAction(tr("Accept all tags from {0} for this session", sender) ) {
     229                @Override public void actionPerformed(ActionEvent e) {
     230                    if (c.isSelected())
     231                        trustedSenders.add(sender);
     232                    else
     233                        trustedSenders.remove(sender);
     234                }
     235            } );
     236            tablePanel.add(c , GBC.eol().insets(20,10,0,0));
     237        }
    213238        setContent(tablePanel);
    214239        setDefaultButton(2);
     
    251276                }
    252277            }
     278        }
     279        if (buttonIndex == 2) {
     280            trustedSenders.remove(sender);
    253281        }
    254282        setVisible(false);
     
    260288        findExistingTags();
    261289    }
    262 
     290   
     291     /*
     292     * parse addtags parameters Example URL (part):
     293     * addtags=wikipedia:de%3DResidenzschloss Dresden|name:en%3DDresden Castle
     294     */
     295    public static void addTags(final Map<String, String> args, final String sender) {
     296        if (args.containsKey("addtags")) {
     297            GuiHelper.executeByMainWorkerInEDT(new Runnable() {
     298
     299                public void run() {
     300                    String[] tags = null;
     301                    try {
     302                        tags = URLDecoder.decode(args.get("addtags"), "UTF-8").split("\\|");
     303                    } catch (UnsupportedEncodingException e) {
     304                        throw new RuntimeException();
     305                    }
     306                    Set<String> tagSet = new HashSet<String>();
     307                    for (String tag : tags) {
     308                        if (!tag.trim().isEmpty() && tag.contains("=")) {
     309                            tagSet.add(tag.trim());
     310                        }
     311                    }
     312                    if (!tagSet.isEmpty()) {
     313                        String[][] keyValue = new String[tagSet.size()][2];
     314                        int i = 0;
     315                        for (String tag : tagSet) {
     316                            // support a  =   b===c as "a"="b===c"
     317                            String [] pair = tag.split("\\s*=\\s*",2);
     318                            keyValue[i][0] = pair[0];
     319                            keyValue[i][1] = pair.length<2 ? "": pair[1];
     320                            i++;
     321                        }
     322                        addTagsIfNeeded(keyValue, sender);
     323                    }
     324                }
     325
     326               
     327            });
     328        }
     329    }
     330   
     331    private static void addTagsIfNeeded(String[][] keyValue, String sender) {
     332        if (trustedSenders.contains(sender)) {
     333            if (Main.main.getCurrentDataSet() != null) {
     334                Collection<OsmPrimitive> s = Main.main.getCurrentDataSet().getSelected();
     335                for (int j = 0; j < keyValue.length; j++) {
     336                    Main.main.undoRedo.add(new ChangePropertyCommand(s, keyValue[j][0], keyValue[j][1]));
     337                }
     338            }
     339        } else {
     340            new AddTagsDialog(keyValue, sender).showDialog();
     341        }
     342    }
    263343}
Note: See TracChangeset for help on using the changeset viewer.