source: josm/trunk/src/org/openstreetmap/josm/gui/JosmUserIdentityManager.java@ 2689

Last change on this file since 2689 was 2689, checked in by Gubaer, 17 years ago

new: Changeset Cache Manager for querying, downloading, browsing, and managing changesets within JOSM. See also Changeset Manager and Changeset Query Dialog

File size: 8.4 KB
Line 
1// License: GPL. For details, see LICENSE file.
2package org.openstreetmap.josm.gui;
3
4import static org.openstreetmap.josm.tools.I18n.tr;
5
6import org.openstreetmap.josm.Main;
7import org.openstreetmap.josm.data.Preferences.PreferenceChangeEvent;
8import org.openstreetmap.josm.data.Preferences.PreferenceChangedListener;
9import org.openstreetmap.josm.data.osm.UserInfo;
10import org.openstreetmap.josm.tools.CheckParameterUtil;
11
12/**
13 * JosmUserStateManager is a global object which keeps track of what JOSM knows about
14 * the identity of the current user.
15 *
16 * JOSM can be operated anonymously provided the current user never invokes an operation
17 * on the OSM server which required authentication. In this case JOSM neither knows
18 * the user name of the OSM account of the current user nor its unique id. Perhaps the
19 * user doesn't have one.
20 *
21 * If the current user supplies a user name and a password in the JOSM preferences JOSM
22 * can partially identify the user.
23 *
24 * The current user is fully identified if JOSM knows both the user name and the unique
25 * id of the users OSM account. The later is retrieved from the OSM server with a
26 * <tt>GET /api/0.6/user/details</tt> request, submitted with the user name and password
27 * of the current user.
28 *
29 * The global JosmUserStateManager listens to {@see PreferenceChangeEvent}s and keeps track
30 * of what the current JOSM instance knows about the current user. Other subsystems can
31 * let the global JosmUserStateManager know in case they fully identify the current user, see
32 * {@see #setFullyIdentified(String, long)}.
33 *
34 * The information kept by the JosmUserStateManager can be used to
35 * <ul>
36 * <li>safely query changesets owned by the current user based on its user id, not on its user name</li>
37 * <li>safely search for objects last touched by the current user based on its user id, not on its user name</li>
38 * </ul>
39 *
40 */
41public class JosmUserIdentityManager implements PreferenceChangedListener{
42
43 static private JosmUserIdentityManager instance;
44
45 /**
46 * Replies the unique instance of teh JOSM user identity manager
47 *
48 * @return the unique instance of teh JOSM user identity manager
49 */
50 static public JosmUserIdentityManager getInstance() {
51 if (instance == null) {
52 instance = new JosmUserIdentityManager();
53 instance.initFromPreferences();
54 Main.pref.addPreferenceChangeListener(instance);
55 }
56 return instance;
57 }
58
59 private String userName;
60 private UserInfo userInfo;
61
62 private JosmUserIdentityManager() {
63 }
64
65 /**
66 * Remembers the fact that the current JOSM user is anonymous.
67 */
68 public void setAnonymous() {
69 userName = null;
70 userInfo = null;
71 }
72
73 /**
74 * Remebers the fact that the current JOSM user is partially identified
75 * by the user name of its OSM account.
76 *
77 * @param userName the user name. Must not be null. Must not be empty (whitespace only).
78 * @throws IllegalArgumentException thrown if userName is null
79 * @throws IllegalArgumentException thrown if userName is empty
80 */
81 public void setPartiallyIdentified(String userName) throws IllegalArgumentException {
82 CheckParameterUtil.ensureParameterNotNull(userName, "userName");
83 if (userName.trim().equals(""))
84 throw new IllegalArgumentException(tr("Expected non-empty value for parameter ''{0}'', got ''{1}''", "userName", userName));
85 this.userName = userName;
86 userInfo = null;
87 }
88
89 /**
90 * Remembers the fact that the current JOSM user is fully identified with a
91 * verified pair of user name and user id.
92 *
93 * @param userName the user name. Must not be null. Must not be empty.
94 * @param userinfo additional information about the user, retrieved from the OSM server and including the user id
95 * @throws IllegalArgumentException thrown if userName is null
96 * @throws IllegalArgumentException thrown if userName is empty
97 * @throws IllegalArgumentException thrown if userinfo is null
98 */
99 public void setFullyIdentified(String username, UserInfo userinfo) throws IllegalArgumentException {
100 CheckParameterUtil.ensureParameterNotNull(username, "username");
101 if (username.trim().equals(""))
102 throw new IllegalArgumentException(tr("Expected non-empty value for parameter ''{0}'', got ''{1}''", "userName", userName));
103 CheckParameterUtil.ensureParameterNotNull(userinfo, "userinfo");
104 this.userName = username;
105 this.userInfo = userinfo;
106 }
107
108 /**
109 * Replies true if the current JOSM user is anonymous.
110 *
111 * @return true if the current user is anonymous.
112 */
113 public boolean isAnonymous() {
114 return userName == null && userInfo == null;
115 }
116
117 /**
118 * Replies true if the current JOSM user is partially identified.
119 *
120 * @return true if the current JOSM user is partially identified.
121 */
122 public boolean isPartiallyIdentified() {
123 return userName != null && userInfo == null;
124 }
125
126
127 /**
128 * Replies true if the current JOSM user is fully identified.
129 *
130 * @return true if the current JOSM user is fully identified.
131 */
132 public boolean isFullyIdentified() {
133 return userName != null && userInfo != null;
134 }
135
136 /**
137 * Replies the user name of the current JOSM user. null, if {@see #isAnonymous()} is true.
138 *
139 * @return the user name of the current JOSM user
140 */
141 public String getUserName() {
142 return userName;
143 }
144
145 /**
146 * Replies the user id of the current JOSM user. 0, if {@see #isAnonymous()} or
147 * {@see #isPartiallyIdentified()} is true.
148 *
149 * @return the user id of the current JOSM user
150 */
151 public int getUserId() {
152 if (userInfo == null) return 0;
153 return userInfo.getId();
154 }
155
156 /**
157 * Replies verified additional information about the current user if the user is
158 * {@see #isFullyIdentified()}.
159 *
160 * @return verified additional information about the current user
161 */
162 public UserInfo getUserInfo() {
163 return userInfo;
164 }
165 /**
166 * Initializes the user identity manager from values in the {@see org.openstreetmap.josm.data.Preferences}
167 */
168 public void initFromPreferences() {
169 String userName = Main.pref.get("osm-server.username");
170 if (isAnonymous()) {
171 if (userName != null && ! userName.trim().equals("")) {
172 setPartiallyIdentified(userName);
173 }
174 } else {
175 if (!userName.equals(this.userName)) {
176 setPartiallyIdentified(userName);
177 } else {
178 // same name in the preferences as JOSM already knows about;
179 // keep the state, be it partially or fully identified
180 }
181 }
182 }
183
184 /**
185 * Replies true if the user with name <code>username</code> is the current
186 * user
187 *
188 * @param username the user name
189 * @return true if the user with name <code>username</code> is the current
190 * user
191 */
192 public boolean isCurrentUser(String username) {
193 if (username == null || this.userName == null) return false;
194 return this.userName.equals(username);
195 }
196
197 /* ------------------------------------------------------------------- */
198 /* interface PreferenceChangeListener */
199 /* ------------------------------------------------------------------- */
200 public void preferenceChanged(PreferenceChangeEvent evt) {
201 if (evt.getKey().equals("osm-server.username")) {
202 String newValue = evt.getNewValue();
203 if (newValue == null || newValue.trim().length() == 0) {
204 setAnonymous();
205 } else {
206 if (! newValue.equals(userName)) {
207 setPartiallyIdentified(newValue);
208 }
209 }
210 return;
211 }
212
213 if (evt.getKey().equals("osm-server.url")) {
214 String newValue = evt.getNewValue();
215 if (newValue == null || newValue.trim().equals("")) {
216 setAnonymous();
217 } else if (isFullyIdentified()) {
218 setPartiallyIdentified(getUserName());
219 }
220 }
221 }
222}
Note: See TracBrowser for help on using the repository browser.