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

Last change on this file since 2828 was 2801, checked in by stoecker, 14 years ago

fixed line endings of recent checkins

File size: 8.1 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 * Replies true if the current JOSM user is fully identified.
128 *
129 * @return true if the current JOSM user is fully identified.
130 */
131 public boolean isFullyIdentified() {
132 return userName != null && userInfo != null;
133 }
134
135 /**
136 * Replies the user name of the current JOSM user. null, if {@see #isAnonymous()} is true.
137 *
138 * @return the user name of the current JOSM user
139 */
140 public String getUserName() {
141 return userName;
142 }
143
144 /**
145 * Replies the user id of the current JOSM user. 0, if {@see #isAnonymous()} or
146 * {@see #isPartiallyIdentified()} is true.
147 *
148 * @return the user id of the current JOSM user
149 */
150 public int getUserId() {
151 if (userInfo == null) return 0;
152 return userInfo.getId();
153 }
154
155 /**
156 * Replies verified additional information about the current user if the user is
157 * {@see #isFullyIdentified()}.
158 *
159 * @return verified additional information about the current user
160 */
161 public UserInfo getUserInfo() {
162 return userInfo;
163 }
164 /**
165 * Initializes the user identity manager from values in the {@see org.openstreetmap.josm.data.Preferences}
166 */
167 public void initFromPreferences() {
168 String userName = Main.pref.get("osm-server.username");
169 if (isAnonymous()) {
170 if (userName != null && ! userName.trim().equals("")) {
171 setPartiallyIdentified(userName);
172 }
173 } else {
174 if (!userName.equals(this.userName)) {
175 setPartiallyIdentified(userName);
176 } else {
177 // same name in the preferences as JOSM already knows about;
178 // keep the state, be it partially or fully identified
179 }
180 }
181 }
182
183 /**
184 * Replies true if the user with name <code>username</code> is the current
185 * user
186 *
187 * @param username the user name
188 * @return true if the user with name <code>username</code> is the current
189 * user
190 */
191 public boolean isCurrentUser(String username) {
192 if (username == null || this.userName == null) return false;
193 return this.userName.equals(username);
194 }
195
196 /* ------------------------------------------------------------------- */
197 /* interface PreferenceChangeListener */
198 /* ------------------------------------------------------------------- */
199 public void preferenceChanged(PreferenceChangeEvent evt) {
200 if (evt.getKey().equals("osm-server.username")) {
201 String newValue = evt.getNewValue();
202 if (newValue == null || newValue.trim().length() == 0) {
203 setAnonymous();
204 } else {
205 if (! newValue.equals(userName)) {
206 setPartiallyIdentified(newValue);
207 }
208 }
209 return;
210 }
211
212 if (evt.getKey().equals("osm-server.url")) {
213 String newValue = evt.getNewValue();
214 if (newValue == null || newValue.trim().equals("")) {
215 setAnonymous();
216 } else if (isFullyIdentified()) {
217 setPartiallyIdentified(getUserName());
218 }
219 }
220 }
221}
Note: See TracBrowser for help on using the repository browser.