1 | // License: GPL. For details, see LICENSE file.
|
---|
2 | package org.openstreetmap.josm.gui.io;
|
---|
3 |
|
---|
4 | import static org.openstreetmap.josm.tools.I18n.tr;
|
---|
5 |
|
---|
6 | import java.awt.Component;
|
---|
7 | import java.io.IOException;
|
---|
8 | import java.util.List;
|
---|
9 |
|
---|
10 | import javax.swing.JOptionPane;
|
---|
11 | import javax.swing.SwingUtilities;
|
---|
12 |
|
---|
13 | import org.openstreetmap.josm.Main;
|
---|
14 | import org.openstreetmap.josm.data.osm.Changeset;
|
---|
15 | import org.openstreetmap.josm.data.osm.ChangesetCache;
|
---|
16 | import org.openstreetmap.josm.data.osm.UserInfo;
|
---|
17 | import org.openstreetmap.josm.gui.ExceptionDialogUtil;
|
---|
18 | import org.openstreetmap.josm.gui.JosmUserIdentityManager;
|
---|
19 | import org.openstreetmap.josm.gui.PleaseWaitRunnable;
|
---|
20 | import org.openstreetmap.josm.gui.util.GuiHelper;
|
---|
21 | import org.openstreetmap.josm.io.ChangesetQuery;
|
---|
22 | import org.openstreetmap.josm.io.OsmServerChangesetReader;
|
---|
23 | import org.openstreetmap.josm.io.OsmServerUserInfoReader;
|
---|
24 | import org.openstreetmap.josm.io.OsmTransferException;
|
---|
25 | import org.xml.sax.SAXException;
|
---|
26 |
|
---|
27 | /**
|
---|
28 | * This is a task for downloading the open changesets of the current user
|
---|
29 | * from the OSM server.
|
---|
30 | */
|
---|
31 | public class DownloadOpenChangesetsTask extends PleaseWaitRunnable {
|
---|
32 |
|
---|
33 | private boolean canceled;
|
---|
34 | private OsmServerChangesetReader reader;
|
---|
35 | private List<Changeset> changesets;
|
---|
36 | private Exception lastException;
|
---|
37 | private final Component parent;
|
---|
38 |
|
---|
39 | /**
|
---|
40 | * Constructs the task
|
---|
41 | * @param parent is a component to show error messages
|
---|
42 | */
|
---|
43 | public DownloadOpenChangesetsTask(Component parent) {
|
---|
44 | super(parent, tr("Downloading open changesets ..."), false /* don't ignore exceptions */);
|
---|
45 | this.parent = parent;
|
---|
46 | }
|
---|
47 |
|
---|
48 | @Override
|
---|
49 | protected void cancel() {
|
---|
50 | this.canceled = true;
|
---|
51 | synchronized (this) {
|
---|
52 | if (reader != null) {
|
---|
53 | reader.cancel();
|
---|
54 | }
|
---|
55 | }
|
---|
56 | }
|
---|
57 |
|
---|
58 | @Override
|
---|
59 | protected void finish() {
|
---|
60 | if (JosmUserIdentityManager.getInstance().isAnonymous()) {
|
---|
61 | JOptionPane.showMessageDialog(
|
---|
62 | GuiHelper.getFrameForComponent(parent),
|
---|
63 | "<html>" + tr("Could not retrieve the list of your open changesets because<br>"
|
---|
64 | + "JOSM does not know your identity.<br>"
|
---|
65 | + "You have either chosen to work anonymously or you are not entitled<br>"
|
---|
66 | + "to know the identity of the user on whose behalf you are working.")
|
---|
67 | + "</html>",
|
---|
68 | tr("Missing user identity"),
|
---|
69 | JOptionPane.ERROR_MESSAGE
|
---|
70 | );
|
---|
71 | return;
|
---|
72 | }
|
---|
73 | if (canceled) return;
|
---|
74 | if (lastException != null) {
|
---|
75 | ExceptionDialogUtil.explainException(lastException);
|
---|
76 | return;
|
---|
77 | }
|
---|
78 | if (changesets.isEmpty()) {
|
---|
79 | JOptionPane.showMessageDialog(
|
---|
80 | Main.parent,
|
---|
81 | tr("There are no open changesets"),
|
---|
82 | tr("No open changesets"),
|
---|
83 | JOptionPane.INFORMATION_MESSAGE
|
---|
84 | );
|
---|
85 | return;
|
---|
86 | }
|
---|
87 | SwingUtilities.invokeLater(() -> ChangesetCache.getInstance().update(changesets));
|
---|
88 | }
|
---|
89 |
|
---|
90 | /**
|
---|
91 | * Refreshes the user info from the server. This is necessary if we don't know the users id yet.
|
---|
92 | */
|
---|
93 | protected void refreshUserIdentity() {
|
---|
94 | JosmUserIdentityManager im = JosmUserIdentityManager.getInstance();
|
---|
95 | try {
|
---|
96 | OsmServerUserInfoReader infoReader = new OsmServerUserInfoReader();
|
---|
97 | UserInfo info = infoReader.fetchUserInfo(getProgressMonitor().createSubTaskMonitor(1, false));
|
---|
98 | im.setFullyIdentified(info.getDisplayName(), info);
|
---|
99 | } catch (OsmTransferException e) {
|
---|
100 | // retrieving the user info can fail if the current user is not authorised to
|
---|
101 | // retrieve it, i.e. if he is working with an OAuth Access Token which doesn't
|
---|
102 | // have the respective privileges or if he didn't or he can't authenticate with
|
---|
103 | // a username/password-pair.
|
---|
104 | //
|
---|
105 | // Downgrade your knowlege about its identity if we've assumed that he was fully
|
---|
106 | // identified. Otherwise, if he is anonymous or partially identified, keep our level
|
---|
107 | // of knowlege.
|
---|
108 | //
|
---|
109 | if (im.isFullyIdentified()) {
|
---|
110 | im.setPartiallyIdentified(im.getUserName());
|
---|
111 | }
|
---|
112 | Main.warn(tr("Failed to retrieve user infos for the current JOSM user. Exception was: {0}", e.toString()));
|
---|
113 | }
|
---|
114 | }
|
---|
115 |
|
---|
116 | @Override
|
---|
117 | protected void realRun() throws SAXException, IOException, OsmTransferException {
|
---|
118 | try {
|
---|
119 | JosmUserIdentityManager im = JosmUserIdentityManager.getInstance();
|
---|
120 | if (im.isAnonymous()) {
|
---|
121 | refreshUserIdentity();
|
---|
122 | } else if (im.isFullyIdentified()) {
|
---|
123 | // do nothing
|
---|
124 | } else if (im.isPartiallyIdentified()) {
|
---|
125 | refreshUserIdentity();
|
---|
126 | }
|
---|
127 | if (canceled) return;
|
---|
128 | synchronized (this) {
|
---|
129 | reader = new OsmServerChangesetReader();
|
---|
130 | }
|
---|
131 | ChangesetQuery query = new ChangesetQuery().beingOpen(true);
|
---|
132 | if (im.isAnonymous())
|
---|
133 | // we still don't know anything about the current user. Can't retrieve
|
---|
134 | // its changesets
|
---|
135 | return;
|
---|
136 | else if (im.isFullyIdentified()) {
|
---|
137 | query = query.forUser(im.getUserId());
|
---|
138 | } else {
|
---|
139 | // we only know the users name, not its id. Nevermind, try to read
|
---|
140 | // its open changesets anyway.
|
---|
141 | //
|
---|
142 | query = query.forUser(im.getUserName());
|
---|
143 | }
|
---|
144 | changesets = reader.queryChangesets(
|
---|
145 | query,
|
---|
146 | getProgressMonitor().createSubTaskMonitor(1, false /* not internal */)
|
---|
147 | );
|
---|
148 | } catch (OsmTransferException e) {
|
---|
149 | if (canceled)
|
---|
150 | return;
|
---|
151 | lastException = e;
|
---|
152 | }
|
---|
153 | }
|
---|
154 |
|
---|
155 | public boolean isCanceled() {
|
---|
156 | return canceled;
|
---|
157 | }
|
---|
158 | }
|
---|