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.awt.*;
022    import java.util.regex.Pattern;
023    import java.util.*;
024    import org.jivesoftware.smack.packet.*;
025    import org.jivesoftware.smack.*;
026    
027    import javax.swing.*;
028    
029    import com.valhalla.gui.*;
030    import com.valhalla.jbother.*;
031    
032    /**
033     * Special <code>RegistrationForm</code> that allows you to register for a jabber account
034     * @author Adam Olsen
035     * @version 1.0
036    */
037    public class NewAccountDialog extends RegistrationForm
038    {
039            private NewAccountDialog thisPointer = this;
040            private XMPPConnection connection;
041            private AccountManager manager;
042            private String username, password;
043            private boolean ssl = false;
044            private int port = 5222;
045            private ProfileEditorDialog profDialog = null;
046    
047            /**
048             * Default constructor
049             * @param dialog the <code>LoginDialog</code> that called this form
050             * @param server the server to register for
051            */
052            public NewAccountDialog( ProfileEditorDialog dialog, String server, String username, String password, int port, boolean useSSL )
053            {
054                    super( server );
055    
056                    this.port = port;
057                    this.ssl = useSSL;
058                    this.username = username;
059                    this.password = password;
060                    this.profDialog = dialog;
061                    setTitle( resources.getString( "createNewAccount" ) );
062            }
063    
064            /**
065             * Collects the required registration fields from the server
066            **/
067            public void getRegistrationInfo()
068            {
069                    Thread thread = new Thread( new GetRegistrationThread() );
070                    thread.start();
071            }
072    
073            /**
074             * Sends the registration information to the server
075            */
076            public void register()
077            {
078                    Thread thread = new Thread( new RegistrationThread() );
079                    thread.start();
080            }
081    
082            /**
083             * Closes the NewAccountDialog
084            */
085            public void closeHandler()
086            {
087                    DialogTracker.removeDialog( this );
088            }
089    
090            /**
091             * Sends the registration information to the server
092             * @author Adam Olsen
093             * @version 1.0
094            */
095            class RegistrationThread implements Runnable
096            {
097                    public void run()
098                    {
099                            final WaitDialog wait = new WaitDialog( "Please Wait...", "Submitting registration information..." );
100                            wait.show();
101                            thisPointer.setVisible( false );
102                            String errorMessage = null;
103    
104                            String username = null;
105                            String password = null;
106    
107                            Hashtable map = new Hashtable();
108                            for( int i = 0; i < fieldListNames.size(); i++ )
109                            {
110                                    String name = (String)fieldListNames.get( i );
111                                    JTextField field = (JTextField)fieldListFields.get( i );
112    
113                                    if( name.equals( "password" ) ) password = field.getText();
114                                    else if( name.equals( "username" ) ) username = field.getText();
115                                    else map.put( name, field.getText() );
116                            }
117    
118                            try {
119                                    if( !connection.isConnected() )
120                                    {
121                                            if( !ssl )
122                                            {
123                                                    connection = new XMPPConnection( server, port );
124                                            }
125                                            else {
126                                                    connection = new SSLXMPPConnection( server, port );
127                                            }
128                                    }
129    
130                                    manager.createAccount( username, password, map );
131                            }
132                            catch( XMPPException e )
133                            {
134                                    errorMessage = e.getMessage();
135                            }
136                            catch( IllegalStateException e )
137                            {
138                                    errorMessage = e.getMessage();
139                            }
140    
141                            connection.close();
142    
143                            final String tempMessage = errorMessage;
144                            final String tempUsername = username;
145                            final String tempPassword = password;
146    
147                            /**
148                             * displays an error if there is one or close the registration dialog
149                             * if the registration was successful
150                             * @author Adam Olsen
151                             * @version 1.0
152                            */
153                            SwingUtilities.invokeLater( new Runnable()
154                            {
155                                    public void run()
156                                    {
157                                            wait.dispose();
158                                            if( tempMessage == null )
159                                            {
160                                                    Standard.noticeMessage( null, resources.getString( "createNewAccount" ),
161                                                            resources.getString( "accountHasBeenCreated" ) );
162                                                    profDialog.setUsername( tempUsername );
163                                                    profDialog.setPassword( tempPassword );
164    
165                                                    DialogTracker.removeDialog( thisPointer );
166                                            }
167                                            else {
168                                                    Standard.warningMessage( BuddyList.getInstance(), resources.getString( "error" ), tempMessage );
169                                                    setVisible( true );
170                                            }
171                                    }
172                            } );
173                    }
174            }
175    
176            /**
177             * Thread to get the required fields from the server.
178             * Also builds the dynamic registration form
179             * @author Adam Olsen
180             * @version 1.0
181            */
182            class GetRegistrationThread implements Runnable
183            {
184                    private String errorMessage;
185    
186                    /**
187                     * Called by the enclosing thread
188                    **/
189                    public void run()
190                    {
191                            final WaitDialog wait = new WaitDialog( "Please Wait...", "Collecting registration form..." );
192                            wait.show();
193    
194                            try {
195                                    if( !ssl )
196                                    {
197                                            connection = new XMPPConnection( server, port );
198                                    }
199                                    else {
200                                            connection = new SSLXMPPConnection( server, port );
201                                    }
202                            }
203                            catch( XMPPException e )
204                            {
205                                    errorMessage = e.getMessage();
206                            }
207    
208                            if( errorMessage == null )
209                            {
210                                    manager = connection.getAccountManager();
211    
212                                    instructions.setText( "<html><table width='300' border='0'><tr><td align='center'> " +
213                                                                    manager.getAccountInstructions() + "</td></tr></table></html>" );
214                                    Iterator iterator = manager.getAccountAttributes();
215    
216                                    if( username != null )
217                                    {
218                                            ((JTextField)fieldListFields.get( 0 ) ).setText( username );
219                                    }
220    
221                                    if( password != null )
222                                    {
223                                            ((JTextField)fieldListFields.get( 1 ) ).setText( password );
224                                    }
225    
226                                    while( iterator.hasNext() )
227                                    {
228                                            String key = (String)iterator.next();
229                                            createInputBox( key );
230                                    }
231                            }
232    
233                            /**
234                             * Displays the error if there is one, or displays the new RegistrationForm
235                             * @author Adam Olsen
236                             * @version 1.0
237                            */
238                            SwingUtilities.invokeLater( new Runnable()
239                            {
240                                    public void run()
241                                    {
242                                            wait.dispose();
243                                            if( errorMessage != null )
244                                            {
245                                                    Standard.warningMessage( null, resources.getString( "registration" ), errorMessage );
246                                                    DialogTracker.removeDialog( thisPointer );
247                                            }
248                                            else {
249                                                    pack();
250    
251                                                    setLocationRelativeTo( null );
252                                                    setVisible( true );
253                                            }
254                                    }
255                            } );
256                    }
257            }
258    }