001: // The contents of this file are subject to the Mozilla Public License Version
002: // 1.1
003: //(the "License"); you may not use this file except in compliance with the
004: //License. You may obtain a copy of the License at http://www.mozilla.org/MPL/
005: //
006: //Software distributed under the License is distributed on an "AS IS" basis,
007: //WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
008: //for the specific language governing rights and
009: //limitations under the License.
010: //
011: //The Original Code is "The Columba Project"
012: //
013: //The Initial Developers of the Original Code are Frederik Dietz and Timo
014: // Stich.
015: //Portions created by Frederik Dietz and Timo Stich are Copyright (C) 2003.
016: //
017: //All Rights Reserved.
018: package org.columba.chat.command;
019:
020: import javax.swing.JOptionPane;
021:
022: import org.columba.api.command.IWorkerStatusController;
023: import org.columba.chat.Connection;
024: import org.columba.chat.MainInterface;
025: import org.columba.chat.config.api.IAccount;
026: import org.columba.chat.conn.api.IConnection.STATUS;
027: import org.columba.chat.ui.frame.api.IChatFrameMediator;
028: import org.columba.core.command.Command;
029: import org.columba.core.gui.frame.FrameManager;
030: import org.columba.mail.gui.util.PasswordDialog;
031: import org.jivesoftware.smack.Roster;
032: import org.jivesoftware.smack.SSLXMPPConnection;
033: import org.jivesoftware.smack.XMPPConnection;
034: import org.jivesoftware.smack.XMPPException;
035:
036: public class ConnectCommand extends Command {
037:
038: private boolean success;
039:
040: private IChatFrameMediator mediator;
041:
042: private PopulateRoasterCommand populateCommand;
043:
044: public ConnectCommand(IChatFrameMediator mediator,
045: ChatCommandReference ref) {
046: super (ref);
047:
048: this .mediator = mediator;
049:
050: populateCommand = new PopulateRoasterCommand(mediator, ref);
051: }
052:
053: /**
054: * @see org.columba.api.command.Command#execute(org.columba.api.command.IWorkerStatusController)
055: */
056: public void execute(IWorkerStatusController worker)
057: throws Exception {
058: IAccount account = MainInterface.config.getAccount();
059:
060: try {
061:
062: if (account.isEnableSSL())
063: Connection.XMPPConnection = new SSLXMPPConnection(
064: account.getHost(), account.getPort());
065: else
066: Connection.XMPPConnection = new XMPPConnection(account
067: .getHost(), account.getPort());
068:
069: } catch (XMPPException e) {
070:
071: JOptionPane.showMessageDialog(FrameManager.getInstance()
072: .getActiveFrame(), e.getMessage());
073:
074: e.printStackTrace();
075:
076: return;
077:
078: }
079:
080: char[] password = account.getPassword();
081:
082: PasswordDialog dialog = new PasswordDialog();
083:
084: success = false;
085:
086: while (!success) {
087:
088: if ((password == null) || (password.length == 0)) {
089: dialog.showDialog("<html>Enter password for <b>"
090: + account.getId() + "</b> at <b>"
091: + account.getHost() + "</b></html>", "", false);
092:
093: if (dialog.success()) {
094: password = dialog.getPassword();
095:
096: if (dialog.getSave())
097: account.setPassword(dialog.getPassword());
098: }
099: }
100:
101: try {
102: Connection.XMPPConnection.login(account.getId(),
103: new String(password));
104: success = true;
105: } catch (XMPPException e) {
106: JOptionPane.showMessageDialog(null, e.getMessage());
107: e.printStackTrace();
108: }
109:
110: }
111:
112: if (!success)
113: return;
114:
115: populateCommand.execute(worker);
116:
117: Connection.XMPPConnection.getRoster().setSubscriptionMode(
118: Roster.SUBSCRIPTION_MANUAL);
119:
120: }
121:
122: /**
123: * @see org.columba.api.command.Command#updateGUI()
124: */
125: public void updateGUI() throws Exception {
126:
127: if (!success)
128: return;
129:
130: MainInterface.connection.setStatus(STATUS.ONLINE);
131:
132: populateCommand.updateGUI();
133:
134: mediator.getRoasterTree().setEnabled(true);
135:
136: }
137: }
|