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

Last change on this file since 4120 was 3083, checked in by bastiK, 14 years ago

added svn:eol-style=native to source files

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