001 /*
002 Copyright (C) 2003 Adam Olsen
003
004 This program is free software; you can redistribute it and/or modify
005 it under the terms of the GNU General Public License as published by
006 the Free Software Foundation; either version 1, or (at your option)
007 any later version.
008
009 This program is distributed in the hope that it will be useful,
010 but WITHOUT ANY WARRANTY; without even the implied warranty of
011 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
012 GNU General Public License for more details.
013
014 You should have received a copy of the GNU General Public License
015 along with this program; if not, write to the Free Software
016 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
017 */
018
019 package com.valhalla.jbother;
020
021 import java.util.Hashtable;
022 import java.util.Iterator;
023
024 import javax.swing.JTextField;
025 import javax.swing.SwingUtilities;
026
027 import org.jivesoftware.smack.AccountManager;
028 import org.jivesoftware.smack.SSLXMPPConnection;
029 import org.jivesoftware.smack.XMPPConnection;
030 import org.jivesoftware.smack.XMPPException;
031
032 import com.valhalla.gui.DialogTracker;
033 import com.valhalla.gui.Standard;
034 import com.valhalla.gui.WaitDialog;
035 import com.valhalla.gui.WaitDialogListener;
036 import com.valhalla.misc.*;
037
038 /**
039 * Special <code>RegistrationForm</code> that allows you to register for a
040 * jabber account
041 *
042 * @author Adam Olsen
043 * @version 1.0
044 */
045 public class NewAccountDialog extends RegistrationForm {
046 private XMPPConnection connection;
047
048 private AccountManager manager;
049
050 private String username, password, server;
051
052 private boolean ssl = false;
053
054 private int port = 5222;
055
056 private ProfileEditorDialog profDialog = null;
057
058 private WaitDialog wait;
059
060 /**
061 * Default constructor
062 *
063 * @param dialog
064 * the <code>LoginDialog</code> that called this form
065 * @param server
066 * the server to register for
067 */
068 public NewAccountDialog(ProfileEditorDialog dialog, String server,
069 String username, String password, int port, boolean useSSL) {
070 super(dialog.getDialogParent(),server);
071
072 this.port = port;
073 this.ssl = useSSL;
074 this.username = username;
075 this.server = server;
076 this.password = password;
077 this.profDialog = dialog;
078 setTitle(resources.getString("createNewAccount"));
079 }
080
081 /**
082 * Collects the required registration fields from the server
083 */
084 public void getRegistrationInfo() {
085 GetRegistrationThread thread = new GetRegistrationThread();
086 wait = new WaitDialog(this, thread, "Gathering registration form...");
087 wait.setVisible(true);
088
089 thread.start();
090 }
091
092 /**
093 * Sends the registration information to the server
094 */
095 public void register() {
096 RegistrationThread thread = new RegistrationThread();
097
098 wait = new WaitDialog(this, thread, "Submitting registration...");
099 wait.setVisible(true);
100
101 thread.start();
102 }
103
104 /**
105 * Closes the NewAccountDialog
106 */
107 public void closeHandler() {
108 DialogTracker.removeDialog(this);
109 }
110
111 /**
112 * Sends the registration information to the server
113 *
114 * @author Adam Olsen
115 * @version 1.0
116 */
117 class RegistrationThread extends Thread implements WaitDialogListener {
118 private boolean stopped = false;
119
120 public void cancel() {
121 stopped = true;
122 interrupt();
123 }
124
125 public void run() {
126 NewAccountDialog.this.setVisible(false);
127 String errorMessage = null;
128
129 String username = null;
130 String password = null;
131
132 Hashtable map = new Hashtable();
133 for (int i = 0; i < fieldListNames.size(); i++) {
134 String name = (String) fieldListNames.get(i);
135 JTextField field = (JTextField) fieldListFields.get(i);
136
137 if (name.equals("password"))
138 password = field.getText();
139 else if (name.equals("username"))
140 username = field.getText();
141 else
142 map.put(name, field.getText());
143 }
144
145 try {
146 if (!connection.isConnected()) {
147 if (!ssl) {
148 connection = new XMPPConnection(server, port);
149 } else {
150 connection = new SSLXMPPConnection(server, port);
151 }
152 }
153
154 manager.createAccount(username, password, map);
155 } catch (XMPPException e) {
156 if (stopped)
157 return;
158 if (e.getXMPPError() == null)
159 errorMessage = e.getMessage();
160 else
161 errorMessage = resources.getString("xmppError"
162 + e.getXMPPError().getCode());
163 } catch (IllegalStateException e) {
164 if (stopped)
165 return;
166 errorMessage = e.getMessage();
167 }
168
169 if (stopped)
170 return;
171
172 connection.close();
173
174 final String tempMessage = errorMessage;
175 final String tempUsername = username;
176 final String tempPassword = password;
177
178 /**
179 * displays an error if there is one or close the registration
180 * dialog if the registration was successful
181 *
182 * @author Adam Olsen
183 * @version 1.0
184 */
185 SwingUtilities.invokeLater(new Runnable() {
186 public void run() {
187 wait.dispose();
188 if (tempMessage == null) {
189 Standard.noticeMessage(null, resources
190 .getString("createNewAccount"), resources
191 .getString("accountHasBeenCreated"));
192 profDialog.setUsername(tempUsername);
193 profDialog.setPassword(tempPassword);
194 profDialog.setServer(server);
195
196 DialogTracker.removeDialog(NewAccountDialog.this);
197 } else {
198 Standard.warningMessage(BuddyList.getInstance(),
199 resources.getString("error"), tempMessage);
200 setVisible(true);
201 }
202 }
203 });
204 }
205 }
206
207 /**
208 * Thread to get the required fields from the server. Also builds the
209 * dynamic registration form
210 *
211 * @author Adam Olsen
212 * @version 1.0
213 */
214 class GetRegistrationThread extends Thread implements WaitDialogListener {
215 private String errorMessage;
216
217 private boolean stopped = false;
218
219 public void cancel() {
220 stopped = true;
221 interrupt();
222 }
223
224 /**
225 * Called by the enclosing thread
226 */
227 public void run() {
228
229 try {
230 if (!ssl) {
231 connection = new XMPPConnection(server, port);
232 } else {
233 connection = new SSLXMPPConnection(server, port);
234 }
235 } catch (XMPPException e) {
236 if (stopped)
237 return;
238 if (e.getXMPPError() == null)
239 errorMessage = e.getMessage();
240 else
241 errorMessage = resources.getString("xmppError"
242 + e.getXMPPError().getCode());
243 }
244
245 if (errorMessage == null) {
246 manager = connection.getAccountManager();
247
248 instructions
249 .setText("<html><table width='300' border='0'><tr><td align='center'> "
250 + manager.getAccountInstructions()
251 + "</td></tr></table></html>");
252 Iterator iterator = manager.getAccountAttributes();
253
254 while (iterator.hasNext()) {
255 String key = (String) iterator.next();
256 if (key.equals("username"))
257 createInputBox(key, "");
258 }
259
260 iterator = manager.getAccountAttributes();
261 while (iterator.hasNext()) {
262 String key = (String) iterator.next();
263 if (!key.equals("username"))
264 createInputBox(key, "");
265 }
266 }
267
268 if (stopped)
269 return;
270
271 /**
272 * Displays the error if there is one, or displays the new
273 * RegistrationForm
274 *
275 * @author Adam Olsen
276 * @version 1.0
277 */
278 SwingUtilities.invokeLater(new Runnable() {
279 public void run() {
280 wait.dispose();
281 if (errorMessage != null) {
282 Standard.warningMessage(null, resources
283 .getString("registration"), errorMessage);
284 DialogTracker.removeDialog(NewAccountDialog.this);
285 } else {
286 pack();
287
288 setLocationRelativeTo(null);
289 setVisible(true);
290 }
291 }
292 });
293 }
294 }
295 }