KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > sf > jftp > tools > InsomniacClient


1 /*
2  * This program is free software; you can redistribute it and/or
3  * modify it under the terms of the GNU General Public License
4  * as published by the Free Software Foundation; either version 2
5  * of the License, or (at your option) any later version.
6  *
7  * This program is distributed in the hope that it will be useful,
8  * but WITHOUT ANY WARRANTY; without even the implied warranty of
9  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10  * GNU General Public License for more details.
11
12  * You should have received a copy of the GNU General Public License
13  * along with this program; if not, write to the Free Software
14  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
15  */

16 package net.sf.jftp.tools;
17
18 import net.sf.jftp.*;
19 import net.sf.jftp.config.*;
20 import net.sf.jftp.gui.*;
21 import net.sf.jftp.gui.base.dir.DirCellRenderer;
22 import net.sf.jftp.gui.base.dir.DirEntry;
23 import net.sf.jftp.gui.framework.*;
24 import net.sf.jftp.net.*;
25 import net.sf.jftp.system.logging.Log;
26 import net.sf.jftp.util.*;
27
28 import java.awt.*;
29 import java.awt.event.*;
30
31 import java.io.*;
32
33 import java.net.*;
34
35 import java.util.*;
36
37 import javax.swing.*;
38 import javax.swing.event.*;
39
40
41 public class InsomniacClient extends HPanel implements Runnable JavaDoc, ActionListener
42 {
43     private HTextField host = new HTextField("URL:",
44                                              "http://canofsleep.com:8180/Insomniac/test.jsp?keywords=",
45                                              40);
46     private HTextField search = new HTextField("Search term:", "mp3", 20);
47     private HTextField user = new HTextField("User:", "guest", 10);
48     private HPasswordField pass = new HPasswordField("Password:", "none");
49     private HTextField dir = new HTextField("Download to:",
50                                             Settings.defaultWorkDir, 20);
51     private JPanel p1 = new JPanel();
52     private JPanel p2 = new JPanel();
53     private JButton ok = new JButton("Search");
54     private JList list = new JList();
55     private JButton load = new JButton("Download file");
56     private Vector files;
57     private Vector sizes;
58     private Thread JavaDoc runner;
59     private boolean doSearch;
60     private Vector fileNames;
61
62     public InsomniacClient()
63     {
64         setLayout(new BorderLayout());
65
66         JPanel x1 = new JPanel();
67         x1.setLayout(new FlowLayout(FlowLayout.LEFT));
68         x1.add(host);
69         x1.add(search);
70
71         JPanel x2 = new JPanel();
72         x2.setLayout(new FlowLayout(FlowLayout.LEFT));
73         x2.add(search);
74         x2.add(dir);
75
76         JPanel x3 = new JPanel();
77         x3.setLayout(new FlowLayout(FlowLayout.LEFT));
78         x3.add(user);
79         x3.add(pass);
80
81         p1.setLayout(new BoxLayout(p1, BoxLayout.Y_AXIS));
82         p1.add(x1);
83         p1.add(x2);
84         p1.add(x3);
85
86         JPanel okP = new JPanel();
87         okP.setLayout(new FlowLayout(FlowLayout.CENTER));
88         okP.add(ok);
89         okP.add(new JLabel(" "));
90         okP.add(load);
91
92         //p1.add(okP);
93
//list.setHeight(300);
94
list.setFixedCellWidth(720);
95         list.setVisibleRowCount(12);
96         list.setCellRenderer(new DirCellRenderer());
97
98         JScrollPane pane = new JScrollPane(list);
99         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     {
121         if((e.getSource() == ok) || e.getSource() instanceof DirEntry)
122         {
123             doSearch = true;
124             runner = new Thread JavaDoc(this);
125             runner.start();
126         }
127         else if(e.getSource() == load)
128         {
129             doSearch = false;
130             runner = new Thread JavaDoc(this);
131             runner.start();
132         }
133     }
134
135     public void run()
136     {
137         if(doSearch)
138         {
139             search.setEnabled(false);
140
141             String JavaDoc url = host.getText().trim() + search.getText().trim();
142             search(url);
143             load.setEnabled(true);
144             search.setEnabled(true);
145         }
146         else
147         {
148             download((String JavaDoc) fileNames.elementAt(list.getSelectedIndex()));
149         }
150     }
151
152     private void download(String JavaDoc url)
153     {
154         try
155         {
156             Log.debug("Insomniac: Download started: " + url);
157
158             String JavaDoc host = url.substring(6);
159             host = host.substring(0, host.indexOf("/"));
160             Log.debug("Insomniac: Trying to connect to remote host: " + host);
161             JFtp.statusP.jftp.ensureLogging();
162
163             SmbConnection con = new SmbConnection(host, user.getText().trim(),
164                                                   pass.getText().trim(),
165                                                   "WORKGROUP",
166                                                   ((ConnectionListener) JFtp.remoteDir));
167             Log.debug("Insomniac: Connected, downloading to: " +
168                       Settings.defaultWorkDir);
169             JFtp.statusP.jftp.ensureLogging();
170             con.setLocalPath(dir.getText().trim());
171
172             if(con.download(url) >= 0)
173             {
174                 Log.debug("Insomniac: Finished download.");
175             }
176             else
177             {
178                 Log.debug("Insomniac: Download failed.");
179             }
180
181             JFtp.statusP.jftp.ensureLogging();
182         }
183         catch(Exception JavaDoc ex)
184         {
185             ex.printStackTrace();
186             Log.debug("Insomniac: Error: " + ex);
187         }
188     }
189
190     private void search(String JavaDoc url)
191     {
192         files = new Vector();
193         sizes = new Vector();
194         fileNames = new Vector();
195
196         try
197         {
198             URL u = new URL(url);
199             DataInputStream in = new DataInputStream(u.openStream());
200
201             String JavaDoc tmp = null;
202
203             while(true)
204             {
205                 tmp = in.readLine();
206
207                 if(tmp == null)
208                 {
209                     return;
210                 }
211                 else
212                 {
213                     if(tmp.indexOf("X-File") >= 0)
214                     {
215                         tmp = parse(tmp);
216                         fileNames.add(tmp);
217
218                         DirEntry entry = new DirEntry(tmp.substring(tmp.lastIndexOf("/") +
219                                                                     1), this);
220                         entry.setFile();
221
222                         tmp = in.readLine();
223
224                         if(tmp.indexOf("X-Size") >= 0)
225                         {
226                             tmp = parse(tmp);
227                             entry.setFileSize(Long.parseLong(tmp));
228                         }
229                         else if(tmp == null)
230                         {
231                             return;
232                         }
233
234                         files.add(entry);
235
236                         list.setListData(files);
237                     }
238                 }
239             }
240         }
241         catch(Exception JavaDoc ex)
242         {
243             ex.printStackTrace();
244             Log.debug("Insomniac: Error: " + ex);
245         }
246     }
247
248     private String JavaDoc parse(String JavaDoc tmp)
249     {
250         if(tmp == null)
251         {
252             return "-1";
253         }
254
255         tmp.trim();
256         tmp = tmp.substring(8);
257         tmp = tmp.substring(0, tmp.length() - 1);
258
259         return tmp;
260     }
261
262     public Insets getInsets()
263     {
264         Insets in = super.getInsets();
265
266         return new Insets(in.top + 5, in.left + 5, in.bottom + 5, in.right + 5);
267     }
268 }
269
Popular Tags