1 import whisper.*; 2 import java.awt.*; 3 import java.awt.event.*; 4 import java.io.File ; 5 import java.io.FileOutputStream ; 6 import java.security.SecureRandom ; 7 import javax.swing.*; 8 import javax.swing.border.*; 9 import org.jivesoftware.smack.*; 10 11 12 public class NewAccountWindow{ 13 14 private String helpTopic=new String ("new.html"); private static final int GAP=10; private static final int NO_OF_PANELS=4; 17 18 private final SecureRandom sr=new SecureRandom (); 19 20 final JFrame frame=new JFrame(Lang.gs("new account title")); 21 final JPanel btnPanel=new JPanel(new FlowLayout(FlowLayout.TRAILING,GAP,GAP)); 22 final JPanel titlePanel=new JPanel(); 23 final CardLayout cardLayout=new CardLayout(GAP,GAP); 24 final JPanel layersPanel=new JPanel(cardLayout); final JPanel panels[]=new JPanel[NO_OF_PANELS]; 27 int current=0; 28 Cursor normal; 29 30 final JLabel accountNameLbl=new JLabel(System.getProperties().getProperty("user.name")); final JTextField accountNameField=new JTextField(8); final JLabel accountLbl=new JLabel(Lang.gs("account")); 34 final JPanel jabberPanel=new JPanel(new GridLayout(7,2,GAP,GAP)); 36 final JLabel jabberUsernameLbl=new JLabel(Lang.gs("jabber_username")); 37 final JTextField jabberUsername=new JTextField(15); 38 final JLabel jabberServerLbl=new JLabel(Lang.gs("jabber_server")); 39 final JTextField jabberServer=new JTextField(15); 40 final JLabel jabberPasswordLbl=new JLabel(Lang.gs("jabber_password")); 41 final JPasswordField jabberPassword=new JPasswordField(15); 42 final JLabel jabberRememberPassLbl=new JLabel(Lang.gs("remember password")); 43 final JCheckBox jabberRememberPass=new JCheckBox("",true); 44 final JLabel jabberResourceLbl=new JLabel(Lang.gs("jabber_resource")); 45 final JTextField jabberResource=new JTextField("here"); 46 final JLabel jabberSSLLbl=new JLabel(Lang.gs("jabber_ssl")); 47 final JCheckBox jabberSSL=new JCheckBox("",true); 48 final JLabel blank=new JLabel(); 49 final JButton jabberNewBtn=new JButton(Lang.gs("jabber_new")); 50 final JLabel generationInfoLbl=new JLabel("<html><p>"+Lang.gs("generation info")+"</p></html>"); 52 final JPanel passphrasePanel=new JPanel(new SpringLayout()); 53 final JPasswordField passphrase1=new JPasswordField(30); 54 final JLabel passphraseLbl1=new JLabel(Lang.gs("passphrase")); 55 final JPasswordField passphrase2=new JPasswordField(30); 56 final JLabel passphraseLbl2=new JLabel(Lang.gs("reenter passphrase")); 57 final JProgressBar keyProgress=new JProgressBar(0,1); 58 final JLabel readyLbl=new JLabel("<html><p>"+Lang.gs("ready")+"</p><p><ul><li>"+Lang.gs("account")); 60 final JLabel lastLbl=new JLabel("<html><p>"+Lang.gs("done")+"</p></html>"); 62 63 final JButton backBtn=new JButton(Lang.gs("back"),Icons.BACK); 65 final JButton nextBtn=new JButton(Lang.gs("next"),Icons.NEXT); 66 final JButton cancelBtn=new JButton(Lang.gs("cancel"),Icons.CANCEL); 67 final JButton helpBtn=new JButton(Lang.gs("help"),Icons.HELP); 68 69 final JLabel title=new JLabel(Lang.gs("title0")); 70 final BtnClick click=new BtnClick(); 71 NewKeyPairThread newKeyPairThread=new NewKeyPairThread(); 72 final JWaitDialog wait=new JWaitDialog(frame,"talk with server"); 73 74 77 public NewAccountWindow(){ 78 frame.setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE); 79 frame.setIconImage(Icons.NEW_ACCOUNT.getImage()); 80 normal=frame.getCursor(); 81 Container cp=frame.getContentPane(); 82 cp.setLayout(new BorderLayout()); 83 nextBtn.setMnemonic(Lang.s2k("next_mn")); 85 backBtn.setMnemonic(Lang.s2k("back_mn")); 86 backBtn.setEnabled(false); cancelBtn.setMnemonic(Lang.s2k("cancel_mn")); 88 helpBtn.setMnemonic(Lang.s2k("help_mn")); 89 btnPanel.add(helpBtn); 90 btnPanel.add(backBtn); 91 btnPanel.add(nextBtn); 92 btnPanel.add(cancelBtn); 93 nextBtn.setDefaultCapable(true); 94 frame.getRootPane().setDefaultButton(nextBtn); 95 helpBtn.addActionListener(click); 96 backBtn.addActionListener(click); 97 nextBtn.addActionListener(click); 98 cancelBtn.addActionListener(click); 99 btnPanel.setPreferredSize(btnPanel.getMinimumSize()); 100 title.setHorizontalAlignment(SwingConstants.CENTER); 102 titlePanel.add(title); 103 titlePanel.setPreferredSize(titlePanel.getMinimumSize()); 104 panels[0]=new JPanel(new SpringLayout()); 106 accountLbl.setToolTipText(Lang.gs("account_tt")); 107 GUI.connect(accountLbl,accountNameField,"account_mn","account_tt"); 108 if(WhisperIM.sharedMachine){ 109 accountNameField.setMaximumSize(accountNameField.getPreferredSize()); 110 accountLbl.setLabelFor(accountNameField); 111 panels[0].add(GUI.panel(accountLbl,accountNameField,GAP)); 112 } 113 else{ 114 accountNameLbl.setToolTipText(Lang.gs("account_tt")); 115 accountNameLbl.setMaximumSize(accountNameLbl.getPreferredSize()); 116 panels[0].add(GUI.panel(accountLbl,accountNameLbl,GAP)); 117 } 118 jabberPanel.setBorder(new TitledBorder(new LineBorder(Color.BLACK),Lang.gs("jabber_settings"))); 120 GUI.connect(jabberUsernameLbl,jabberUsername,"jabber_username_mn","jabber_username_tt"); 121 GUI.connect(jabberServerLbl,jabberServer,"jabber_server_mn","jabber_server_tt"); 122 GUI.connect(jabberPasswordLbl,jabberPassword,"jabber_password_mn","jabber_password_tt"); 123 GUI.connect(jabberRememberPassLbl,jabberRememberPass,null,"remember password_tt"); 124 GUI.connect(jabberResourceLbl,jabberResource,"jabber_resource_mn","jabber_resource_tt"); 125 GUI.connect(jabberSSLLbl,jabberSSL,null,"jabber_ssl_tt"); 126 jabberNewBtn.setMnemonic(Lang.s2k("jabber_new_mn")); 127 jabberNewBtn.setToolTipText(Lang.gs("jabber_new_tt")); 128 jabberSSLLbl.setIcon(Icons.PADLOCK); 129 jabberPanel.add(jabberUsernameLbl); 130 jabberPanel.add(jabberUsername); 131 jabberPanel.add(jabberServerLbl); 132 jabberPanel.add(jabberServer); 133 jabberPanel.add(jabberPasswordLbl); 134 jabberPanel.add(jabberPassword); 135 jabberPanel.add(jabberRememberPassLbl); 136 jabberPanel.add(jabberRememberPass); 137 jabberPanel.add(jabberResourceLbl); 138 jabberPanel.add(jabberResource); 139 jabberPanel.add(jabberSSLLbl); 140 jabberPanel.add(jabberSSL); 141 jabberPanel.add(blank); 142 jabberPanel.add(jabberNewBtn); 143 jabberNewBtn.addActionListener(click); 144 jabberUsername.setText(System.getProperties().getProperty("user.name")); 146 jabberServer.setText(WhisperIM.Config.getProperty("jabberDefaultServer")); 147 jabberResource.setText(WhisperIM.Config.getProperty("jabberDefaultResource")); 148 if(WhisperIM.Config.getProperty("jabberLockToUserName").equals("true")){ 150 jabberUsername.setEnabled(false); 151 } 152 if(WhisperIM.Config.getProperty("jabberLockToDefaultServer").equals("true")){ 153 jabberServer.setEnabled(false); 154 } 155 if(WhisperIM.Config.getProperty("jabberLockToDefaultResource").equals("true")){ 156 jabberResource.setEnabled(false); 157 } 158 if(WhisperIM.Config.getProperty("jabberAllowNewAccounts").equals("false")){ 159 jabberNewBtn.setVisible(false); 160 } 161 if(WhisperIM.Config.getProperty("jabberAllowNonSSL").equals("false")){ 162 jabberSSL.setEnabled(false); 163 } 164 165 panels[0].add(jabberPanel); 166 SpringUtilities.makeCompactGrid(panels[0], 167 2, 1, GAP, GAP, GAP,GAP); panels[1]=new JPanel(new BorderLayout(GAP,GAP)); 172 panels[1].add(generationInfoLbl,BorderLayout.NORTH); 173 Box passphraseBox=new Box(BoxLayout.Y_AXIS); 174 GUI.connect(passphraseLbl1,passphrase1,"passphrase_mn",null); 175 GUI.connect(passphraseLbl2,passphrase2,"reenter passphrase_mn",null); 176 passphrasePanel.add(passphraseLbl1); 177 passphrasePanel.add(passphrase1); 178 passphrasePanel.add(passphraseLbl2); 179 passphrasePanel.add(passphrase2); 180 SpringUtilities.makeCompactGrid(passphrasePanel,2,2,GAP,GAP,GAP,GAP); 181 passphrasePanel.setMaximumSize(passphrasePanel.getPreferredSize()); 182 passphraseBox.add(passphrasePanel); 183 passphraseBox.add(Box.createGlue()); 184 panels[1].add(passphraseBox,BorderLayout.CENTER); 185 keyProgress.setStringPainted(true); 186 keyProgress.setString(Lang.gs("generating key")); 187 keyProgress.setIndeterminate(true); 188 panels[1].add(keyProgress,BorderLayout.SOUTH); 189 panels[2]=new JPanel(new BorderLayout(GAP,GAP)); 191 panels[2].add(readyLbl,BorderLayout.NORTH); 192 panels[3]=new JPanel(new BorderLayout(GAP,GAP)); 194 panels[3].add(lastLbl,BorderLayout.NORTH); 195 for(int i=0;i<NO_OF_PANELS;i++){ 197 layersPanel.add(panels[i],i+""); 198 } 199 layersPanel.setSize(layersPanel.getMinimumSize()); 200 cp.add(titlePanel, BorderLayout.NORTH); 202 cp.add(layersPanel, BorderLayout.CENTER); 203 cp.add(btnPanel,BorderLayout.SOUTH); 204 frame.pack(); 205 frame.getRootPane().getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(KeyStroke.getKeyStroke(WhisperIM.HELP_KEY,0),"help"); frame.getRootPane().getActionMap().put("help",new HelpAction(frame,helpTopic)); 207 frame.setSize(frame.getMinimumSize()); 208 frame.setLocationRelativeTo(null); 209 } 210 211 212 public void show(){ 213 WhisperIM.KEYPAIR=null; 214 cardLayout.first(layersPanel); 215 title.setText(Lang.gs("title0")); 216 current=0; 217 backBtn.setEnabled(false); 218 cancelBtn.setEnabled(true); 219 nextBtn.setText(Lang.gs("next")); 220 nextBtn.setMnemonic(Lang.s2k("next_mn")); 221 nextBtn.setEnabled(true); 222 nextBtn.setIcon(Icons.NEXT); 223 frame.setCursor(normal); 224 frame.show(); 225 } 226 227 228 public void dispose(){ 229 keyProgress.setIndeterminate(false); 232 if(newKeyPairThread.isAlive()){ 233 newKeyPairThread.interrupt(); 234 } 235 frame.dispose(); 236 } 237 238 240 private void doEnter(){ 241 if(current==0){ backBtn.setEnabled(false); 243 nextBtn.setEnabled(true); 244 frame.setCursor(GUI.NORMAL); 245 return; 246 } 247 if(current==1){ keyProgress.setVisible(false); 249 passphrase1.setEnabled(true); 250 passphrase2.setEnabled(true); 251 passphrase1.setText(""); 252 passphrase2.setText(""); 253 passphrase1.requestFocus(); 254 nextBtn.setEnabled(true); 255 return; 256 } 257 if(current==2){ 258 if(WhisperIM.sharedMachine){ 259 readyLbl.setText(readyLbl.getText()+" "+accountNameField.getText()+"</li><li>"+Lang.gs("jabber_username")+" "+jabberUsername.getText()+"@"+jabberServer.getText()+"</li></ul></p></html>"); 260 } 261 else{ 262 readyLbl.setText(readyLbl.getText()+" "+accountNameLbl.getText()+"</li><li>"+Lang.gs("jabber_username")+" "+jabberUsername.getText()+"@"+jabberServer.getText()+"</li></ul></p></html>"); 263 } 264 } 265 if(current==NO_OF_PANELS-1){ backBtn.setEnabled(false); 267 cancelBtn.setEnabled(false); 268 nextBtn.setText(Lang.gs("finish")); 269 nextBtn.setMnemonic(Lang.s2k("finish_mn")); 270 nextBtn.setIcon(Icons.FINISHED); 271 } 272 } 273 274 275 private void doNext(){ 276 if(current==0){ if(WhisperIM.sharedMachine){ 280 if(accountNameField.getText().compareTo("")==0){ 281 showInfo("you must enter a account name"); 282 accountNameField.requestFocus(); 283 return; 284 } 285 String accountName=accountNameField.getText().trim(); 287 if(accountName.length()==0){ 288 showInfo("you must enter a account name"); 289 accountNameField.setText(""); 290 accountNameField.requestFocus(); 291 return; 292 } 293 accountNameField.setText(accountName); 294 if(accountName.length()>8){ 295 accountName=accountName.substring(0,8).trim(); 296 accountNameField.setText(accountName); 297 if(accountName.length()==0){ 298 showInfo("you must enter a account name"); 299 accountNameField.setText(""); 300 accountNameField.requestFocus(); 301 return; 302 } 303 } 304 File tmp=new File (WhisperIM.accountDir,accountName); 305 if(tmp.exists() && tmp.isDirectory()){ 306 showInfo("account name already exists"); 307 accountNameField.requestFocus(); 308 return; 309 } 310 311 } 312 if(!jabberOk()){ 313 return; 314 } 315 316 backBtn.setEnabled(true); 317 } 318 319 if(current==1){ 321 String p1=new String (passphrase1.getPassword()); 323 String p2=new String (passphrase2.getPassword()); 324 if(p1.length()<20){ 325 showInfo("passphrase too short"); 326 passphrase1.requestFocus(); 327 return; 328 } 329 if(!p1.equals(p2)){ 330 showInfo("passphrase mismatch"); 331 passphrase2.requestFocus(); 332 return; 333 } 334 p1=null; 335 p2=null; 336 nextBtn.setEnabled(false); 337 keyProgress.setIndeterminate(true); 338 keyProgress.setVisible(true); 339 frame.setCursor(GUI.WAIT); 340 passphrase1.setEnabled(false); 341 passphrase2.setEnabled(false); 342 newKeyPairThread=new NewKeyPairThread(); 343 newKeyPairThread.start(); 344 return; 345 } 346 if(current==2){ 347 frame.setCursor(GUI.WAIT); 348 if(WhisperIM.sharedMachine){ 349 WhisperIM.userDir=new File (WhisperIM.accountDir,accountNameField.getText()); 350 } 351 if(!WhisperIM.userDir.exists()){ if(WhisperIM.userDir.mkdir()==false){ 354 frame.setCursor(GUI.NORMAL); 355 try{ 356 GUI.showError(frame,"new account title","cannot create userdir",WhisperIM.userDir.getCanonicalPath()); 357 } 358 catch(Exception e){ 359 GUI.showError(frame,"new account title","cannot create userdir"); 360 } 361 return; 362 } 363 } 364 365 try{ 367 if(WhisperIM.isMac){ 368 WhisperIM.UserPref.setProperty("laf","native"); 369 } 370 WhisperIM.UserPref.store(new FileOutputStream (new File (WhisperIM.userDir,WhisperIM.USER_PREF_FILE_NAME),false),"Whisper preferences"); 371 } 372 catch(Exception e2){ 373 frame.setCursor(GUI.NORMAL); 374 GUI.showError(frame,"new account title","cannot save",e2.getMessage()); 375 return; 376 } 377 try{ 379 byte salt[]=new byte[64]; 380 WhisperIM.SR.nextBytes(salt); 381 WhisperOutputStream wos=new WhisperOutputStream(new File (WhisperIM.userDir,WhisperIM.KEYPAIR_FILENAME), Util.passphrase2Key(passphrase1.getPassword(),salt), WhisperIM.SR, salt); 382 wos.write(WhisperIM.KEYPAIR.publicKey()); 383 wos.write(WhisperIM.KEYPAIR.privateKey()); 384 wos.close(); 385 } 386 catch(Exception e3){ 387 frame.setCursor(GUI.NORMAL); 388 GUI.showError(frame,"new account title","cannot save keypair",e3.getMessage()); 389 return; 390 } 391 WhisperIM.SR.nextBytes(WhisperIM.MainWindow.keycachekey); 394 byte[] encryptedkck=RSA.encryptSessionKey(WhisperIM.MainWindow.keycachekey,WhisperIM.KEYPAIR.publicKey().getN(),RSAKeyPair.E17 ,WhisperIM.SR); 396 try{ 398 FileOutputStream kckos=new FileOutputStream (new File (WhisperIM.userDir,WhisperIM.KCK_FILE_NAME),false); 399 kckos.write(Util.arrayLength(encryptedkck)); kckos.write(encryptedkck); 401 kckos.flush(); 402 kckos.close(); 403 Util.wipe(encryptedkck); 404 } 405 catch(Exception e4){ 406 frame.setCursor(GUI.NORMAL); 407 GUI.showError(frame,"new account title","cannot save keypair",e4.getMessage()); 408 return; 409 } 410 WhisperIM.accountSettings.JabberUserName=jabberUsername.getText(); 412 WhisperIM.accountSettings.JabberServer=jabberServer.getText(); 413 WhisperIM.accountSettings.JabberPassword=new String (jabberPassword.getPassword()); 414 WhisperIM.accountSettings.JabberResource=jabberResource.getText(); 415 WhisperIM.accountSettings.JabberRememberPassword=jabberRememberPass.isSelected(); 416 try{ 417 WhisperIM.accountSettings.Save(); 418 } 419 catch (Exception e){ 420 frame.setCursor(GUI.NORMAL); 421 GUI.showError(frame,"new account title","cannot save",e.getMessage()); 422 return; 423 } 424 try{ 426 BackUp.backUp(new File (WhisperIM.userDir,AccountSettings.FILENAME)); 427 BackUp.backUp(new File (WhisperIM.userDir,WhisperIM.KEYPAIR_FILENAME)); 428 BackUp.backUp(new File (WhisperIM.userDir,WhisperIM.KCK_FILE_NAME)); 429 } 430 catch(Exception bue){ 431 bue.printStackTrace(); 432 } 433 frame.setCursor(GUI.NORMAL); 434 passphrase1.setText(""); 435 passphrase2.setText(""); 436 } 437 if(current==NO_OF_PANELS-1){ 439 frame.hide(); 441 WhisperIM.Login_Window.dispose(); 442 WhisperIM.Login_Window=null; 443 if(WhisperIM.Config_Window!=null){ 444 WhisperIM.Config_Window.dispose(); 445 WhisperIM.Config_Window=null; 446 } 447 if(WhisperIM.sharedMachine){ 449 WhisperIM.MainWindow.show_new(accountNameField.getText()); 450 } 451 else{ 452 WhisperIM.MainWindow.show_new(accountNameLbl.getText()); 453 } 454 frame.dispose(); 456 WhisperIM.New_Account_Window=null; 457 return; 458 } 459 showNext(); 460 doEnter(); 461 } 462 463 464 private void showNext(){ 465 current++; 466 title.setText(Lang.gs("title"+current)); 467 cardLayout.next(layersPanel); 468 } 469 470 471 private void showInfo(String message){ 472 GUI.showInfo(frame,"new account title",message); 473 } 474 475 477 private boolean jabberOk(){ 478 if(jabberUsername.getText().equals("")){ 479 showInfo("enter a user name"); 480 jabberUsername.requestFocus(); 481 return false; 482 } 483 if(jabberServer.getText().equals("")){ 484 showInfo("you must enter servername"); 485 jabberServer.requestFocus(); 486 return false; 487 } 488 if(jabberPassword.getPassword().length==0){ 489 showInfo("you must enter password"); 490 jabberPassword.requestFocus(); 491 return false; 492 } 493 return true; 494 } 495 496 497 public void doKeyGenerated(){ 498 if(current!=1){return;} 499 frame.show(); 500 frame.toFront(); 501 Toolkit.getDefaultToolkit().beep(); 502 frame.setCursor(GUI.NORMAL); 503 nextBtn.setEnabled(true); 504 showNext(); 505 doEnter(); 506 } 507 508 509 final class BtnClick implements ActionListener{ 510 public void actionPerformed(ActionEvent ae){ 511 String b=ae.getActionCommand(); 513 if(b.equals(cancelBtn.getText())){ 515 frame.hide(); 516 return; 517 } 518 if(b.equals(helpBtn.getText())){ 519 WhisperIM.Help_Window.show(frame,helpTopic); 520 return; 521 } 522 if(b.equals(jabberNewBtn.getText())){ 523 if(jabberServer.getText().equals("")){ 524 showInfo("you must enter servername"); 525 jabberServer.requestFocus(); 526 return; 527 } 528 wait.show(); 529 final SwingWorker worker = new SwingWorker() { 530 JNewJabberDialog newjabberdialog; 531 532 public Object construct(){ 533 newjabberdialog=new JNewJabberDialog(frame,jabberServer.getText(),jabberUsername.getText(),(new String (jabberPassword.getPassword())),jabberSSL.isSelected()); 534 return null; 535 } 536 537 public void finished() { 539 wait.dispose(); 540 newjabberdialog.show(); 541 if(newjabberdialog.created()){ 542 jabberUsername.setText(newjabberdialog.getUsername()); 543 jabberPassword.setText(newjabberdialog.getPassword()); 544 } 545 } 546 }; 547 548 worker.start(); 549 return; 550 } 551 if(b.equals(nextBtn.getText())){ 552 doNext(); 553 } 554 if(b.equals(backBtn.getText())){ 555 current--; 556 title.setText(Lang.gs("title"+current)); 557 cardLayout.previous(layersPanel); 558 doEnter(); 559 } 560 } 561 } 562 563 564 final class NewKeyPairThread extends Thread { 565 public NewKeyPairThread(){ 566 super(); 567 setDaemon(true); setPriority(Thread.MIN_PRIORITY); } 570 571 public void run(){ 572 WhisperIM.KEYPAIR=new KeyPair("<jabber>"+jabberUsername.getText()+"@"+jabberServer.getText()+"</jabber>",WhisperIM.SR); 573 javax.swing.SwingUtilities.invokeLater(new Runnable (){ 574 public void run(){ 575 doKeyGenerated(); } 577 }); 578 } 579 } 580 581 582 } | Popular Tags |