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