KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > columba > chat > command > ConnectCommand


1 // The contents of this file are subject to the Mozilla Public License Version
2
// 1.1
3
//(the "License"); you may not use this file except in compliance with the
4
//License. You may obtain a copy of the License at http://www.mozilla.org/MPL/
5
//
6
//Software distributed under the License is distributed on an "AS IS" basis,
7
//WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
8
//for the specific language governing rights and
9
//limitations under the License.
10
//
11
//The Original Code is "The Columba Project"
12
//
13
//The Initial Developers of the Original Code are Frederik Dietz and Timo
14
// Stich.
15
//Portions created by Frederik Dietz and Timo Stich are Copyright (C) 2003.
16
//
17
//All Rights Reserved.
18
package org.columba.chat.command;
19
20 import javax.swing.JOptionPane JavaDoc;
21
22 import org.columba.api.command.IWorkerStatusController;
23 import org.columba.chat.Connection;
24 import org.columba.chat.MainInterface;
25 import org.columba.chat.config.api.IAccount;
26 import org.columba.chat.conn.api.IConnection.STATUS;
27 import org.columba.chat.ui.frame.api.IChatFrameMediator;
28 import org.columba.core.command.Command;
29 import org.columba.core.gui.frame.FrameManager;
30 import org.columba.mail.gui.util.PasswordDialog;
31 import org.jivesoftware.smack.Roster;
32 import org.jivesoftware.smack.SSLXMPPConnection;
33 import org.jivesoftware.smack.XMPPConnection;
34 import org.jivesoftware.smack.XMPPException;
35
36 public class ConnectCommand extends Command {
37
38     private boolean success;
39
40     private IChatFrameMediator mediator;
41
42     private PopulateRoasterCommand populateCommand;
43
44     public ConnectCommand(IChatFrameMediator mediator, ChatCommandReference ref) {
45         super(ref);
46
47         this.mediator = mediator;
48
49         populateCommand = new PopulateRoasterCommand(mediator, ref);
50     }
51
52     /**
53      * @see org.columba.api.command.Command#execute(org.columba.api.command.IWorkerStatusController)
54      */

55     public void execute(IWorkerStatusController worker) throws Exception JavaDoc {
56         IAccount account = MainInterface.config.getAccount();
57
58         try {
59
60             if (account.isEnableSSL())
61                 Connection.XMPPConnection = new SSLXMPPConnection(account
62                         .getHost(), account.getPort());
63             else
64                 Connection.XMPPConnection = new XMPPConnection(account
65                         .getHost(), account.getPort());
66
67         } catch (XMPPException e) {
68
69             JOptionPane.showMessageDialog(FrameManager.getInstance()
70                     .getActiveFrame(), e.getMessage());
71
72             e.printStackTrace();
73
74             return;
75
76         }
77
78         char[] password = account.getPassword();
79
80         PasswordDialog dialog = new PasswordDialog();
81
82         success = false;
83
84         while (!success) {
85
86             if ((password == null) || (password.length == 0)) {
87                 dialog.showDialog("<html>Enter password for <b>"
88                         + account.getId() + "</b> at <b>" + account.getHost()
89                         + "</b></html>", "", false);
90
91                 if (dialog.success()) {
92                     password = dialog.getPassword();
93
94                     if (dialog.getSave())
95                         account.setPassword(dialog.getPassword());
96                 }
97             }
98
99             try {
100                 Connection.XMPPConnection.login(account.getId(),
101                         new String JavaDoc(password));
102                 success = true;
103             } catch (XMPPException e) {
104                 JOptionPane.showMessageDialog(null, e.getMessage());
105                 e.printStackTrace();
106             }
107
108         }
109
110         if (!success)
111             return;
112
113
114
115         populateCommand.execute(worker);
116
117         Connection.XMPPConnection.getRoster().setSubscriptionMode(
118                 Roster.SUBSCRIPTION_MANUAL);
119
120
121
122     }
123
124     /**
125      * @see org.columba.api.command.Command#updateGUI()
126      */

127     public void updateGUI() throws Exception JavaDoc {
128
129         if (!success)
130             return;
131
132         MainInterface.connection.setStatus(STATUS.ONLINE);
133
134         populateCommand.updateGUI();
135
136         mediator.getRoasterTree().setEnabled(true);
137
138     }
139 }
Popular Tags