001: /*
002: * This program is free software; you can redistribute it and/or
003: * modify it under the terms of the GNU General Public License
004: * as published by the Free Software Foundation; either version 2
005: * of the License, or (at your option) any later version.
006: *
007: * This program is distributed in the hope that it will be useful,
008: * but WITHOUT ANY WARRANTY; without even the implied warranty of
009: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
010: * GNU General Public License for more details.
011:
012: * You should have received a copy of the GNU General Public License
013: * along with this program; if not, write to the Free Software
014: * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
015: */
016: package net.sf.jftp.gui.hostchooser;
017:
018: import net.sf.jftp.*;
019: import net.sf.jftp.config.*;
020: import net.sf.jftp.gui.framework.*;
021: import net.sf.jftp.net.*;
022: import net.sf.jftp.system.logging.Log;
023: import net.sf.jftp.util.*;
024:
025: import java.awt.*;
026: import java.awt.event.*;
027:
028: import java.io.*;
029:
030: import java.net.*;
031:
032: import java.util.*;
033:
034: import javax.swing.*;
035:
036: public class SmbHostChooser extends HFrame implements ActionListener,
037: WindowListener {
038: public static HTextField host = new HTextField("URL:",
039: "smb://localhost/");
040: public static HTextField user = new HTextField("Username:", "guest");
041:
042: //public static HTextField pass = new HTextField("Password:","none@nowhere.no");
043: public static HPasswordField pass = new HPasswordField("Password:",
044: "nopasswd");
045: JCheckBox lan = new JCheckBox("Browse LAN", true);
046: public HTextField domain = new HTextField("Domain: ",
047: "WORKGROUP");
048: public HTextField broadcast = new HTextField("Broadcast IP: ",
049: "AUTO");
050: public HTextField wins = new HTextField("WINS Server IP: ",
051: "NONE");
052:
053: //public HTextField ip = new HTextField("Local IP: ","<default>");
054: public JComboBox ip = new JComboBox();
055: private HPanel okP = new HPanel();
056: private HButton ok = new HButton("Connect");
057: private ComponentListener listener = null;
058: private boolean useLocal = false;
059:
060: public SmbHostChooser(ComponentListener l, boolean local) {
061: listener = l;
062: useLocal = local;
063: init();
064: }
065:
066: public SmbHostChooser(ComponentListener l) {
067: listener = l;
068: init();
069: }
070:
071: public SmbHostChooser() {
072: init();
073: }
074:
075: public void init() {
076: //setSize(500, 320);
077: setLocation(100, 150);
078: setTitle("Smb Connection...");
079: setBackground(okP.getBackground());
080: getContentPane().setLayout(new GridLayout(5, 2, 5, 3));
081:
082: //*** MY CHANGES
083: try {
084: File f = new File(Settings.appHomeDir);
085: f.mkdir();
086:
087: File f1 = new File(Settings.login);
088: f1.createNewFile();
089:
090: File f2 = new File(Settings.login_def_smb);
091: f2.createNewFile();
092: } catch (IOException ex) {
093: ex.printStackTrace();
094: }
095:
096: String[] login = LoadSet.loadSet(Settings.login_def_smb);
097:
098: if ((login[0] != null) && (login.length > 1)) {
099: host.setText(login[0]);
100: user.setText(login[1]);
101:
102: //if (login[3] != null)
103: //lan.setText(login[3]);
104: //if (login[4] != null)
105: //ip.setText(login[4]);
106: //if (login[5] != null)
107: domain.setText(login[5]);
108: }
109:
110: /*
111: else {
112: host.setText("smb://localhost/");
113: user.setText("guest");
114: domain.setText("WORKGROUP");
115: }
116: */
117: if (Settings.getStorePasswords()) {
118: if ((login != null) && (login.length > 2)
119: && (login[2] != null)) {
120: pass.setText(login[2]);
121: }
122: } else {
123: pass.setText("");
124: }
125:
126: //***end of my changes (for this section)
127: ip.setEditable(true);
128:
129: getContentPane().add(host);
130: getContentPane().add(lan);
131: getContentPane().add(user);
132: getContentPane().add(pass);
133:
134: getContentPane().add(ip);
135: getContentPane().add(domain);
136:
137: getContentPane().add(broadcast);
138: getContentPane().add(okP);
139:
140: getContentPane().add(wins);
141:
142: JTextArea t = new JTextArea();
143: t.setLineWrap(true);
144: t.setText("Note: URL is in form \"smb://host/\"\n"
145: + "and most people do not need WINS.");
146:
147: getContentPane().add(t);
148:
149: okP.add(ok);
150: ok.addActionListener(this );
151:
152: host.setEnabled(!lan.isSelected());
153: setDefaultCloseOperation(JDialog.DO_NOTHING_ON_CLOSE);
154:
155: lan.addActionListener(this );
156: pass.text.addActionListener(this );
157:
158: setModal(false);
159: setVisible(false);
160: addWindowListener(this );
161:
162: ip.addItem("<default>");
163:
164: try {
165: Enumeration e = NetworkInterface.getNetworkInterfaces();
166:
167: while (e.hasMoreElements()) {
168: Enumeration f = ((NetworkInterface) e.nextElement())
169: .getInetAddresses();
170:
171: while (f.hasMoreElements()) {
172: ;
173: ip.addItem(((InetAddress) f.nextElement())
174: .getHostAddress());
175: }
176: }
177: } catch (Exception ex) {
178: Log.debug("Error determining default network interface: "
179: + ex);
180:
181: //ex.printStackTrace();
182: }
183:
184: //setBCast();
185: domain.setEnabled(false);
186: broadcast.setEnabled(false);
187: wins.setEnabled(false);
188:
189: ip.addActionListener(this );
190:
191: pack();
192: setVisible(true);
193: }
194:
195: private void setBCast() {
196: try {
197: String tmp = ((String) ip.getSelectedItem()).trim();
198: String x = tmp.substring(0, tmp.lastIndexOf(".") + 1)
199: + "255";
200: broadcast.setText(x);
201: } catch (Exception ex) {
202: Log.out("Error (SMBHostChooser): " + ex);
203: }
204: }
205:
206: public void update() {
207: fixLocation();
208: setVisible(true);
209: toFront();
210: host.requestFocus();
211: }
212:
213: public void actionPerformed(ActionEvent e) {
214: if (e.getSource() == lan) {
215: host.setEnabled(!lan.isSelected());
216: } else if (e.getSource() == ip) {
217: if (ip.getSelectedItem().equals("<default>")) {
218: domain.setEnabled(false);
219: broadcast.setEnabled(false);
220: wins.setEnabled(false);
221: } else {
222: domain.setEnabled(true);
223: broadcast.setEnabled(true);
224: wins.setEnabled(true);
225: setBCast();
226: }
227: } else if ((e.getSource() == ok)
228: || (e.getSource() == pass.text)) {
229: // Switch windows
230: //this.setVisible(false);
231: setCursor(new Cursor(Cursor.WAIT_CURSOR));
232:
233: SmbConnection con = null;
234:
235: //System.out.println(jcifs.Config.getProperty("jcifs.smb.client.laddr"));
236: String tmp = ((String) ip.getSelectedItem()).trim();
237:
238: if (!tmp.equals("") && !tmp.equals("<default>")) {
239: String x = tmp.trim().substring(0,
240: tmp.lastIndexOf(".") + 1)
241: + "255";
242: String bcast = broadcast.getText().trim();
243:
244: if (!bcast.equals("AUTO")) {
245: x = bcast;
246: }
247:
248: Log.debug("Setting LAN interface to: " + tmp + "/" + x);
249: jcifs.Config.setProperty("jcifs.netbios.laddr", tmp);
250: jcifs.Config.setProperty("jcifs.smb.client.laddr", tmp);
251: jcifs.Config.setProperty("jcifs.netbios.baddr", x);
252:
253: String y = wins.getText().trim();
254:
255: if (!y.equals("NONE")) {
256: Log.debug("Setting WINS server IP to: " + y);
257: jcifs.Config.setProperty("jcifs.netbios.wins", y);
258: }
259: }
260:
261: //System.out.println(jcifs.Config.getProperty("jcifs.smb.client.laddr"));
262: //JFtp.setHost(host.getText());
263: String htmp = host.getText().trim();
264: String utmp = user.getText().trim();
265: String ptmp = pass.getText();
266: String dtmp = domain.getText().trim();
267:
268: //***
269: //if(dtmp.equals("")) dtmp = null;
270: if (dtmp.equals("")) {
271: dtmp = "NONE";
272: }
273:
274: //if(lan.isSelected()) htmp = null;
275: if (lan.isSelected()) {
276: htmp = "(LAN)";
277: }
278:
279: //***save the set of selected data
280: SaveSet s = new SaveSet(Settings.login_def_smb, htmp, utmp,
281: ptmp, "", "", dtmp);
282:
283: //*** Now make the function call to the methos for starting
284: //connections
285: boolean status;
286: int potmp = 0; //*** port number: unlikely to be needed in the future
287:
288: status = StartConnection.startCon("SMB", htmp, utmp, ptmp,
289: potmp, dtmp, useLocal);
290:
291: /*
292: try
293: {
294: con = new SmbConnection(htmp,dtmp,utmp,ptmp, ((ConnectionListener)JFtp.remoteDir));
295:
296: //JFtp.statusP.jftp.addConnection(htmp, con);
297: if(useLocal)
298: {
299: JFtp.statusP.jftp.addLocalConnection(htmp, con);
300: JFtp.localDir.fresh();
301: }
302: else
303: {
304: JFtp.statusP.jftp.addConnection(htmp, con);
305: JFtp.remoteDir.fresh();
306: }
307:
308: //JFtp.remoteDir.setCon(con);
309: //con.setLocalPath(JFtp.localDir.getCon().getPWD());
310: //con.addConnectionListener((ConnectionListener) JFtp.localDir);
311: //con.addConnectionListener((ConnectionListener) JFtp.remoteDir);
312: //JFtp.remoteDir.fresh();
313: }
314: catch(Exception ex)
315: {
316: Log.debug("Could not create SMBConnection, does this distribution come with jcifs?");
317: } */
318: setCursor(new Cursor(Cursor.DEFAULT_CURSOR));
319: this .dispose();
320: JFtp.mainFrame.setVisible(true);
321: JFtp.mainFrame.toFront();
322:
323: if (listener != null) {
324: listener.componentResized(new ComponentEvent(this , 0));
325: }
326: }
327: }
328:
329: public void windowClosing(WindowEvent e) {
330: //System.exit(0);
331: this .dispose();
332: }
333:
334: public void windowClosed(WindowEvent e) {
335: }
336:
337: public void windowActivated(WindowEvent e) {
338: }
339:
340: public void windowDeactivated(WindowEvent e) {
341: }
342:
343: public void windowIconified(WindowEvent e) {
344: }
345:
346: public void windowDeiconified(WindowEvent e) {
347: }
348:
349: public void windowOpened(WindowEvent e) {
350: }
351:
352: public Insets getInsets() {
353: Insets std = super .getInsets();
354:
355: return new Insets(std.top + 10, std.left + 10, std.bottom + 10,
356: std.right + 10);
357: }
358:
359: public void pause(int time) {
360: try {
361: Thread.sleep(time);
362: } catch (Exception ex) {
363: }
364: }
365: }
|