1 19 package org.columba.mail.gui.config.account; 20 21 import java.awt.Font ; 22 import java.awt.GridBagConstraints ; 23 import java.awt.GridBagLayout ; 24 import java.awt.Insets ; 25 import java.awt.event.ActionEvent ; 26 import java.awt.event.ActionListener ; 27 import java.io.File ; 28 import java.text.MessageFormat ; 29 import java.util.Iterator ; 30 import java.util.LinkedList ; 31 import java.util.List ; 32 import java.util.logging.Logger ; 33 import java.util.regex.Matcher ; 34 import java.util.regex.Pattern ; 35 36 import javax.swing.BorderFactory ; 37 import javax.swing.JButton ; 38 import javax.swing.JCheckBox ; 39 import javax.swing.JComboBox ; 40 import javax.swing.JDialog ; 41 import javax.swing.JLabel ; 42 import javax.swing.JOptionPane ; 43 import javax.swing.JPanel ; 44 import javax.swing.JSpinner ; 45 import javax.swing.JTextField ; 46 import javax.swing.SpinnerNumberModel ; 47 import javax.swing.SwingUtilities ; 48 49 import org.columa.core.config.IDefaultItem; 50 import org.columba.core.config.DefaultConfigDirectory; 51 import org.columba.core.gui.base.ButtonWithMnemonic; 52 import org.columba.core.gui.base.CheckBoxWithMnemonic; 53 import org.columba.core.gui.base.LabelWithMnemonic; 54 import org.columba.core.gui.exception.ExceptionHandler; 55 import org.columba.mail.config.AccountItem; 56 import org.columba.mail.config.ImapItem; 57 import org.columba.mail.config.IncomingItem; 58 import org.columba.mail.config.MailConfig; 59 import org.columba.mail.config.PopItem; 60 import org.columba.mail.imap.IImapServer; 61 import org.columba.mail.imap.IMAPServer; 62 import org.columba.mail.pop3.POP3Store; 63 import org.columba.mail.util.MailResourceLoader; 64 import org.columba.ristretto.imap.IMAPProtocol; 65 import org.columba.ristretto.pop3.POP3Protocol; 66 67 import com.jgoodies.forms.builder.DefaultFormBuilder; 68 import com.jgoodies.forms.layout.FormLayout; 69 70 73 public class IncomingServerPanel extends DefaultPanel implements ActionListener { 74 75 76 private static final Logger LOG = Logger 77 .getLogger("org.columba.mail.gui.config.account"); 78 79 private static final Pattern AUTH_MODE_TOKENIZE_PATTERN = Pattern 80 .compile("(\\d+);?"); 81 82 private JLabel loginLabel; 83 84 private JTextField loginTextField; 85 86 private JTextField passwordTextField; 87 88 private JLabel hostLabel; 89 90 private JTextField hostTextField; 91 92 private JLabel portLabel; 93 94 private JSpinner portSpinner; 95 96 private JLabel typeLabel; 97 98 private JComboBox typeComboBox; 99 100 private JCheckBox storePasswordCheckBox; 101 102 private JCheckBox secureCheckBox; 103 104 private JLabel authenticationLabel; 105 106 private JComboBox authenticationComboBox; 107 108 private JLabel typeDescriptionLabel; 109 110 private IDefaultItem serverItem = null; 111 112 private AccountItem accountItem; 113 114 private JCheckBox defaultAccountCheckBox; 115 116 private ReceiveOptionsPanel receiveOptionsPanel; 117 118 private JButton checkAuthMethods; 119 120 private JComboBox sslComboBox; 121 122 private JDialog dialog; 124 125 public IncomingServerPanel(JDialog dialog, AccountItem account, 126 ReceiveOptionsPanel receiveOptionsPanel) { 127 super(); 128 129 this.dialog = dialog; 130 131 this.accountItem = account; 132 this.receiveOptionsPanel = receiveOptionsPanel; 133 134 if (account.isPopAccount()) { 135 serverItem = account.getPopItem(); 136 } else { 137 serverItem = account.getImapItem(); 138 } 139 140 initComponents(); 141 142 updateComponents(true); 143 } 144 145 public String getHost() { 146 return hostTextField.getText(); 147 } 148 149 public String getLogin() { 150 return loginTextField.getText(); 151 } 152 153 public boolean isPopAccount() { 154 return accountItem.isPopAccount(); 155 } 156 157 protected void updateComponents(boolean b) { 158 if (b) { 159 loginTextField.setText(serverItem.get(IncomingItem.USER)); 160 passwordTextField.setText(serverItem.get(IncomingItem.PASSWORD)); 161 hostTextField.setText(serverItem.get(IncomingItem.HOST)); 162 String port = serverItem.get(IncomingItem.PORT); 163 portSpinner.setValue(new Integer (port)); 164 165 storePasswordCheckBox.setSelected(serverItem 166 .getBoolean(IncomingItem.SAVE_PASSWORD)); 167 168 defaultAccountCheckBox.setSelected(serverItem 169 .getBoolean(IncomingItem.USE_DEFAULT_ACCOUNT)); 170 171 try { 172 authenticationComboBox.setSelectedItem(new Integer (serverItem 173 .get(IncomingItem.LOGIN_METHOD))); 174 } catch (NumberFormatException e) { 175 } 176 177 secureCheckBox.removeActionListener(this); 180 sslComboBox.removeActionListener(this); 181 182 secureCheckBox.setSelected(serverItem.getBooleanWithDefault( 183 IncomingItem.ENABLE_SSL, false)); 184 185 sslComboBox.setSelectedIndex(serverItem.getIntegerWithDefault( 186 IncomingItem.SSL_TYPE, 1)); 187 sslComboBox.setEnabled(secureCheckBox.isSelected()); 188 secureCheckBox.addActionListener(this); 190 sslComboBox.addActionListener(this); 191 192 defaultAccountCheckBox.setEnabled(MailConfig.getInstance() 193 .getAccountList().getDefaultAccountUid() != accountItem 194 .getInteger(IncomingItem.UID)); 195 196 if (defaultAccountCheckBox.isEnabled() 197 && defaultAccountCheckBox.isSelected()) { 198 showDefaultAccountWarning(); 199 } else { 200 layoutComponents(); 201 } 202 } else { 203 serverItem.setString(IncomingItem.USER, loginTextField.getText()); 204 serverItem.setString(IncomingItem.HOST, hostTextField.getText()); 205 serverItem.setString(IncomingItem.PASSWORD, passwordTextField 206 .getText()); 207 serverItem.setString(IncomingItem.PORT, ((Integer ) portSpinner 208 .getValue()).toString()); 209 210 serverItem.setBoolean(IncomingItem.SAVE_PASSWORD, 211 storePasswordCheckBox.isSelected()); 212 213 serverItem.setBoolean(IncomingItem.ENABLE_SSL, secureCheckBox 214 .isSelected()); 215 serverItem.setInteger(IncomingItem.SSL_TYPE, sslComboBox 216 .getSelectedIndex()); 217 218 serverItem.setString(IncomingItem.LOGIN_METHOD, 220 authenticationComboBox.getSelectedItem().toString()); 221 222 serverItem.setBoolean(IncomingItem.USE_DEFAULT_ACCOUNT, 223 defaultAccountCheckBox.isSelected()); 224 225 serverItem.getRoot().notifyObservers(); 226 } 227 } 228 229 protected void showDefaultAccountWarning() { 230 setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5)); 231 232 GridBagLayout mainLayout = new GridBagLayout (); 233 GridBagConstraints mainConstraints = new GridBagConstraints (); 234 235 setLayout(mainLayout); 236 237 mainConstraints.gridwidth = GridBagConstraints.REMAINDER; 238 mainConstraints.anchor = GridBagConstraints.NORTHWEST; 239 mainConstraints.weightx = 1.0; 240 mainConstraints.insets = new Insets (0, 10, 5, 0); 241 mainLayout.setConstraints(defaultAccountCheckBox, mainConstraints); 242 add(defaultAccountCheckBox); 243 244 mainConstraints = new GridBagConstraints (); 245 mainConstraints.weighty = 1.0; 246 mainConstraints.gridwidth = GridBagConstraints.REMAINDER; 247 248 254 JLabel label = new JLabel (MailResourceLoader.getString("dialog", 255 "account", "using_default_account_settings")); 256 Font newFont = label.getFont().deriveFont(Font.BOLD); 257 label.setFont(newFont); 258 mainLayout.setConstraints(label, mainConstraints); 259 add(label); 260 } 261 262 protected void layoutComponents() { 263 FormLayout layout = new FormLayout( 265 "10dlu, max(70dlu;default), 3dlu, fill:max(150dlu;default):grow, 3dlu, default, 3dlu, default", 266 267 ""); 271 DefaultFormBuilder builder = new DefaultFormBuilder(layout, this); 273 274 builder.setDefaultDialogBorder(); 276 277 builder.setLeadingColumnOffset(1); 279 280 284 builder.appendSeparator(MailResourceLoader.getString("dialog", 285 "account", "configuration")); 286 builder.nextLine(); 287 288 builder.append(loginLabel, 1); 289 builder.append(loginTextField, 5); 290 builder.nextLine(); 291 292 builder.append(hostLabel, 1); 293 builder.append(hostTextField); 294 296 builder.append(portLabel); 297 builder.append(portSpinner); 298 builder.nextLine(); 299 300 builder.appendSeparator(MailResourceLoader.getString("dialog", 301 "account", "security")); 302 builder.nextLine(); 303 304 JPanel panel = new JPanel (); 305 FormLayout l = new FormLayout( 306 "default, 3dlu, fill:pref:grow, 3dlu, fill:pref:grow", 307 308 "fill:default:grow"); 313 DefaultFormBuilder b = new DefaultFormBuilder(l, panel); 315 b.append(authenticationLabel, authenticationComboBox, checkAuthMethods); 316 builder.append(panel, 3); 317 builder.nextLine(); 318 319 builder.append(secureCheckBox, 3); 320 builder.nextLine(); 321 322 JPanel panel2 = new JPanel (); 323 FormLayout l2 = new FormLayout("default, 3dlu, left:pref", 324 325 "fill:default:grow"); 330 DefaultFormBuilder b2 = new DefaultFormBuilder(l2, panel2); 332 b2.setRowGroupingEnabled(true); 333 b2.append(secureCheckBox, sslComboBox); 334 builder.append(panel2, 3); 335 builder.nextLine(); 336 337 builder.append(storePasswordCheckBox, 3); 338 builder.nextLine(); 339 340 349 } 350 351 protected void initComponents() { 352 defaultAccountCheckBox = new CheckBoxWithMnemonic(MailResourceLoader 353 .getString("dialog", "account", "use_default_account_settings")); 354 355 defaultAccountCheckBox.setActionCommand("DEFAULT_ACCOUNT"); 356 defaultAccountCheckBox.addActionListener(this); 357 358 typeLabel = new LabelWithMnemonic(MailResourceLoader.getString( 360 "dialog", "account", "server_type")); 361 362 typeComboBox = new JComboBox (); 363 typeComboBox.addItem("POP3"); 364 typeComboBox.addItem("IMAP4"); 365 366 if (accountItem.isPopAccount()) { 367 typeComboBox.setSelectedIndex(0); 368 } else { 369 typeComboBox.setSelectedIndex(1); 370 } 371 372 typeLabel.setLabelFor(typeComboBox); 373 typeComboBox.setEnabled(false); 374 375 typeDescriptionLabel = new JLabel ( 377 "Description: To connect to and fetch new messages from a POP3-server."); 378 typeDescriptionLabel.setEnabled(false); 379 380 loginLabel = new LabelWithMnemonic(MailResourceLoader.getString( 381 "dialog", "account", "login")); 382 383 loginTextField = new JTextField (); 384 loginLabel.setLabelFor(loginTextField); 385 386 passwordTextField = new JTextField (); 387 388 hostLabel = new LabelWithMnemonic(MailResourceLoader.getString( 389 "dialog", "account", IncomingItem.HOST)); 390 391 hostTextField = new JTextField (); 392 hostLabel.setLabelFor(hostTextField); 393 394 portLabel = new LabelWithMnemonic(MailResourceLoader.getString( 395 "dialog", "account", IncomingItem.PORT)); 396 397 portSpinner = new JSpinner (new SpinnerNumberModel (100, 1, 65535, 1)); 398 portSpinner.setEditor(new JSpinner.NumberEditor (portSpinner, "#####")); 399 portLabel.setLabelFor(portSpinner); 400 401 storePasswordCheckBox = new CheckBoxWithMnemonic(MailResourceLoader 402 .getString("dialog", "account", 403 "store_password_in_configuration_file")); 404 405 storePasswordCheckBox.setActionCommand("SAVE"); 406 storePasswordCheckBox.addActionListener(this); 407 408 secureCheckBox = new CheckBoxWithMnemonic(MailResourceLoader.getString( 409 "dialog", "account", "use_SSL_for_secure_connection")); 410 secureCheckBox.setActionCommand("SSL"); 411 secureCheckBox.addActionListener(this); 412 413 authenticationLabel = new LabelWithMnemonic(MailResourceLoader 414 .getString("dialog", "account", "authentication_type")); 415 416 authenticationComboBox = new JComboBox (); 417 authenticationComboBox 418 .setRenderer(new AuthenticationListCellRenderer()); 419 authenticationLabel.setLabelFor(authenticationComboBox); 420 421 updateAuthenticationComboBox(); 422 423 checkAuthMethods = new ButtonWithMnemonic(MailResourceLoader.getString( 424 "dialog", "account", "authentication_checkout_methods")); 425 checkAuthMethods.setActionCommand("CHECK_AUTHMETHODS"); 426 checkAuthMethods.addActionListener(this); 427 428 sslComboBox = new JComboBox (); 429 if (isPopAccount()) { 430 sslComboBox.addItem(MailResourceLoader.getString("dialog", 431 "account", "pop3s_in_checkbox")); 432 } else { 433 sslComboBox.addItem(MailResourceLoader.getString("dialog", 434 "account", "imaps_in_checkbox")); 435 } 436 sslComboBox.addItem(MailResourceLoader.getString("dialog", "account", 437 "tls_in_checkbox")); 438 sslComboBox.setActionCommand("SSL"); 439 sslComboBox.addActionListener(this); 440 } 441 442 private void updateAuthenticationComboBox() { 443 authenticationComboBox.removeAllItems(); 444 445 authenticationComboBox.addItem(new Integer (0)); 446 447 String authMethods; 448 if (isPopAccount()) { 449 authMethods = accountItem.getString(IncomingItem.POPSERVER, 450 "authentication_methods"); 451 } else { 452 authMethods = accountItem.getString("imapserver", 453 "authentication_methods"); 454 } 455 if (authMethods != null) { 457 Matcher matcher = AUTH_MODE_TOKENIZE_PATTERN.matcher(authMethods); 458 459 while (matcher.find()) { 460 authenticationComboBox.addItem(new Integer (matcher.group(1))); 461 } 462 } 463 } 464 465 public void actionPerformed(ActionEvent e) { 466 String action = e.getActionCommand(); 467 468 if (action.equals("SERVER")) { LOG.info("selection changed"); 470 } else if (action.equals("DEFAULT_ACCOUNT")) { 471 removeAll(); 472 receiveOptionsPanel.removeAll(); 473 474 if (defaultAccountCheckBox.isSelected()) { 475 showDefaultAccountWarning(); 476 receiveOptionsPanel.showDefaultAccountWarning(); 477 } else { 478 layoutComponents(); 479 receiveOptionsPanel.layoutComponents(); 480 } 481 482 revalidate(); 483 receiveOptionsPanel.revalidate(); 484 } else if (action.equals("CHECK_AUTHMETHODS")) { 485 fetchAuthMechanisms(); 486 } else if (action.equals("SSL")) { 487 sslComboBox.setEnabled(secureCheckBox.isSelected()); 488 489 if (secureCheckBox.isSelected()) { 490 if (sslComboBox.getSelectedIndex() == IncomingItem.TLS) { 492 if (isPopAccount()) { 494 if (((Integer ) portSpinner.getValue()).intValue() != POP3Protocol.DEFAULT_PORT) { 495 portSpinner.setValue(new Integer ( 496 POP3Protocol.DEFAULT_PORT)); 497 showPortChangeMessageBox(); 498 } 499 } else { 500 if (((Integer ) portSpinner.getValue()).intValue() != IMAPProtocol.DEFAULT_PORT) { 501 portSpinner.setValue(new Integer ( 502 IMAPProtocol.DEFAULT_PORT)); 503 showPortChangeMessageBox(); 504 } 505 } 506 } else { 507 if (isPopAccount()) { 509 if (((Integer ) portSpinner.getValue()).intValue() != POP3Protocol.DEFAULT_SSL_PORT) { 510 portSpinner.setValue(new Integer ( 511 POP3Protocol.DEFAULT_SSL_PORT)); 512 showPortChangeMessageBox(); 513 } 514 } else { 515 if (((Integer ) portSpinner.getValue()).intValue() != IMAPProtocol.DEFAULT_SSL_PORT) { 516 portSpinner.setValue(new Integer ( 517 IMAPProtocol.DEFAULT_SSL_PORT)); 518 showPortChangeMessageBox(); 519 } 520 } 521 } 522 } else { 523 if (isPopAccount()) { 525 if (((Integer ) portSpinner.getValue()).intValue() != POP3Protocol.DEFAULT_PORT) { 526 portSpinner.setValue(new Integer ( 527 POP3Protocol.DEFAULT_PORT)); 528 showPortChangeMessageBox(); 529 } 530 } else { 531 if (((Integer ) portSpinner.getValue()).intValue() != IMAPProtocol.DEFAULT_PORT) { 532 portSpinner.setValue(new Integer ( 533 IMAPProtocol.DEFAULT_PORT)); 534 showPortChangeMessageBox(); 535 } 536 } 537 } 538 } else if (action.equals("SAVE")) { 539 if (!storePasswordCheckBox.isSelected()) { 540 return; 541 } else { 542 File configPath = DefaultConfigDirectory.getInstance().getCurrentPath(); 543 File defaultConfigPath = DefaultConfigDirectory.getDefaultPath(); 544 while (!configPath.equals(defaultConfigPath)) { 545 configPath = configPath.getParentFile(); 546 if (configPath == null) { 547 JOptionPane 548 .showMessageDialog(dialog, MailResourceLoader 549 .getString("dialog", 550 IncomingItem.PASSWORD, 551 "warn_save_msg"), 552 MailResourceLoader.getString("dialog", 553 IncomingItem.PASSWORD, 554 "warn_save_title"), 555 JOptionPane.WARNING_MESSAGE); 556 return; 557 } 558 } 559 } 560 } 561 } 562 563 566 private void showPortChangeMessageBox() { 567 Runnable doHelloWorld = new Runnable () { 568 public void run() { 569 JOptionPane.showMessageDialog(dialog, MailResourceLoader 570 .getString("dialog", "account", "change_port_ssl"), 571 "Information", JOptionPane.INFORMATION_MESSAGE); 572 } 573 }; 574 575 SwingUtilities.invokeLater(doHelloWorld); 576 } 577 578 private IDefaultItem getCurrentDialogSettings() { 579 IDefaultItem server = null; 580 581 if (accountItem.isPopAccount()) { 582 server = (IDefaultItem) accountItem.getPopItem().clone(); 583 } else { 584 server = (IDefaultItem) accountItem.getImapItem().clone(); 585 } 586 587 server.setString(IncomingItem.USER, loginTextField.getText()); 588 server.setString(IncomingItem.HOST, hostTextField.getText()); 589 server.setString(IncomingItem.PASSWORD, passwordTextField.getText()); 590 server.setString(IncomingItem.PORT, ((Integer ) portSpinner.getValue()) 591 .toString()); 592 593 server.setBoolean(IncomingItem.ENABLE_SSL, secureCheckBox.isSelected()); 594 server 595 .setInteger(IncomingItem.SSL_TYPE, sslComboBox 596 .getSelectedIndex()); 597 598 return server; 599 } 600 601 private void fetchAuthMechanisms() { 602 List list = new LinkedList (); 603 IDefaultItem serverItem = getCurrentDialogSettings(); 604 605 if (isPopAccount()) { 606 609 POP3Store store = new POP3Store((PopItem) serverItem); 610 611 try { 612 list = store.checkSupportedAuthenticationMethods(); 613 } catch (Exception e) { 614 new ExceptionHandler().processException(e); 616 } 617 } else { 618 IImapServer server = new IMAPServer((ImapItem) serverItem); 619 620 try { 621 list = server.checkSupportedAuthenticationMethods(); 622 } catch (Exception e) { 623 new ExceptionHandler().processException(e); 625 } 626 627 } 628 629 if (list.size() > 0) { 631 StringBuffer authMethods = new StringBuffer (); 632 Iterator it = list.iterator(); 633 authMethods.append(it.next()); 634 635 while (it.hasNext()) { 636 authMethods.append(';'); 637 authMethods.append(it.next()); 638 } 639 640 if (isPopAccount()) { 641 accountItem.setString(IncomingItem.POPSERVER, 642 "authentication_methods", authMethods.toString()); 643 } else { 644 accountItem.setString("imapserver", "authentication_methods", 645 authMethods.toString()); 646 } 647 648 } 649 650 updateAuthenticationComboBox(); 651 } 652 653 public boolean isFinished() { 654 String host = getHost(); 655 String login = getLogin(); 656 657 if (host.length() == 0) { 658 JOptionPane.showMessageDialog(this, MailResourceLoader.getString( 659 "dialog", "account", "You_have_to_enter_a_host_name")); 660 661 return false; 663 } else if (login.length() == 0) { 664 JOptionPane.showMessageDialog(this, MailResourceLoader.getString( 665 "dialog", "account", "You_have_to_enter_a_login_name")); 666 667 return false; 669 } else if (defaultAccountCheckBox.isSelected()) { 670 AccountItem defaultAccount = MailConfig.getInstance() 671 .getAccountList().getDefaultAccount(); 672 673 if (defaultAccount.getAccountType() != accountItem.getAccountType()) { 674 675 String errorMessage = MailResourceLoader.getString("dialog", 676 "account", "cannot_use_default_account"); 677 678 Object [] accountType = new Object [] { defaultAccount 679 .getAccountTypeDescription() }; 680 681 errorMessage = MessageFormat.format(errorMessage, accountType); 682 683 JOptionPane.showMessageDialog(null, errorMessage); 684 685 return false; 686 } 687 } 688 689 return true; 690 } 691 } 692 | Popular Tags |