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.tools;
017:
018: import net.sf.jftp.*;
019: import net.sf.jftp.config.*;
020: import net.sf.jftp.gui.*;
021: import net.sf.jftp.gui.base.dir.DirCellRenderer;
022: import net.sf.jftp.gui.base.dir.DirEntry;
023: import net.sf.jftp.gui.framework.*;
024: import net.sf.jftp.net.*;
025: import net.sf.jftp.system.logging.Log;
026: import net.sf.jftp.util.*;
027:
028: import java.awt.*;
029: import java.awt.event.*;
030:
031: import java.io.*;
032:
033: import java.net.*;
034:
035: import java.util.*;
036:
037: import javax.swing.*;
038: import javax.swing.event.*;
039:
040: public class InsomniacClient extends HPanel implements Runnable,
041: ActionListener {
042: private HTextField host = new HTextField("URL:",
043: "http://canofsleep.com:8180/Insomniac/test.jsp?keywords=",
044: 40);
045: private HTextField search = new HTextField("Search term:", "mp3",
046: 20);
047: private HTextField user = new HTextField("User:", "guest", 10);
048: private HPasswordField pass = new HPasswordField("Password:",
049: "none");
050: private HTextField dir = new HTextField("Download to:",
051: Settings.defaultWorkDir, 20);
052: private JPanel p1 = new JPanel();
053: private JPanel p2 = new JPanel();
054: private JButton ok = new JButton("Search");
055: private JList list = new JList();
056: private JButton load = new JButton("Download file");
057: private Vector files;
058: private Vector sizes;
059: private Thread runner;
060: private boolean doSearch;
061: private Vector fileNames;
062:
063: public InsomniacClient() {
064: setLayout(new BorderLayout());
065:
066: JPanel x1 = new JPanel();
067: x1.setLayout(new FlowLayout(FlowLayout.LEFT));
068: x1.add(host);
069: x1.add(search);
070:
071: JPanel x2 = new JPanel();
072: x2.setLayout(new FlowLayout(FlowLayout.LEFT));
073: x2.add(search);
074: x2.add(dir);
075:
076: JPanel x3 = new JPanel();
077: x3.setLayout(new FlowLayout(FlowLayout.LEFT));
078: x3.add(user);
079: x3.add(pass);
080:
081: p1.setLayout(new BoxLayout(p1, BoxLayout.Y_AXIS));
082: p1.add(x1);
083: p1.add(x2);
084: p1.add(x3);
085:
086: JPanel okP = new JPanel();
087: okP.setLayout(new FlowLayout(FlowLayout.CENTER));
088: okP.add(ok);
089: okP.add(new JLabel(" "));
090: okP.add(load);
091:
092: //p1.add(okP);
093: //list.setHeight(300);
094: list.setFixedCellWidth(720);
095: list.setVisibleRowCount(12);
096: list.setCellRenderer(new DirCellRenderer());
097:
098: JScrollPane pane = new JScrollPane(list);
099: p2.add(new JScrollPane(pane));
100:
101: add("North", p1);
102: add("Center", p2);
103: add("South", okP);
104:
105: //JPanel p3 = new JPanel();
106: //p3.setLayout(new FlowLayout(FlowLayout.CENTER));
107: //p3.add(load);
108: load.addActionListener(this );
109: load.setEnabled(false);
110:
111: //add("South", p3);
112: ok.addActionListener(this );
113:
114: setVisible(true);
115: validate();
116: doLayout();
117: }
118:
119: public void actionPerformed(ActionEvent e) {
120: if ((e.getSource() == ok) || e.getSource() instanceof DirEntry) {
121: doSearch = true;
122: runner = new Thread(this );
123: runner.start();
124: } else if (e.getSource() == load) {
125: doSearch = false;
126: runner = new Thread(this );
127: runner.start();
128: }
129: }
130:
131: public void run() {
132: if (doSearch) {
133: search.setEnabled(false);
134:
135: String url = host.getText().trim()
136: + search.getText().trim();
137: search(url);
138: load.setEnabled(true);
139: search.setEnabled(true);
140: } else {
141: download((String) fileNames.elementAt(list
142: .getSelectedIndex()));
143: }
144: }
145:
146: private void download(String url) {
147: try {
148: Log.debug("Insomniac: Download started: " + url);
149:
150: String host = url.substring(6);
151: host = host.substring(0, host.indexOf("/"));
152: Log.debug("Insomniac: Trying to connect to remote host: "
153: + host);
154: JFtp.statusP.jftp.ensureLogging();
155:
156: SmbConnection con = new SmbConnection(host, user.getText()
157: .trim(), pass.getText().trim(), "WORKGROUP",
158: ((ConnectionListener) JFtp.remoteDir));
159: Log.debug("Insomniac: Connected, downloading to: "
160: + Settings.defaultWorkDir);
161: JFtp.statusP.jftp.ensureLogging();
162: con.setLocalPath(dir.getText().trim());
163:
164: if (con.download(url) >= 0) {
165: Log.debug("Insomniac: Finished download.");
166: } else {
167: Log.debug("Insomniac: Download failed.");
168: }
169:
170: JFtp.statusP.jftp.ensureLogging();
171: } catch (Exception ex) {
172: ex.printStackTrace();
173: Log.debug("Insomniac: Error: " + ex);
174: }
175: }
176:
177: private void search(String url) {
178: files = new Vector();
179: sizes = new Vector();
180: fileNames = new Vector();
181:
182: try {
183: URL u = new URL(url);
184: DataInputStream in = new DataInputStream(u.openStream());
185:
186: String tmp = null;
187:
188: while (true) {
189: tmp = in.readLine();
190:
191: if (tmp == null) {
192: return;
193: } else {
194: if (tmp.indexOf("X-File") >= 0) {
195: tmp = parse(tmp);
196: fileNames.add(tmp);
197:
198: DirEntry entry = new DirEntry(tmp.substring(tmp
199: .lastIndexOf("/") + 1), this );
200: entry.setFile();
201:
202: tmp = in.readLine();
203:
204: if (tmp.indexOf("X-Size") >= 0) {
205: tmp = parse(tmp);
206: entry.setFileSize(Long.parseLong(tmp));
207: } else if (tmp == null) {
208: return;
209: }
210:
211: files.add(entry);
212:
213: list.setListData(files);
214: }
215: }
216: }
217: } catch (Exception ex) {
218: ex.printStackTrace();
219: Log.debug("Insomniac: Error: " + ex);
220: }
221: }
222:
223: private String parse(String tmp) {
224: if (tmp == null) {
225: return "-1";
226: }
227:
228: tmp.trim();
229: tmp = tmp.substring(8);
230: tmp = tmp.substring(0, tmp.length() - 1);
231:
232: return tmp;
233: }
234:
235: public Insets getInsets() {
236: Insets in = super .getInsets();
237:
238: return new Insets(in.top + 5, in.left + 5, in.bottom + 5,
239: in.right + 5);
240: }
241: }
|