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.base;
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.StringUtils;
023: import net.sf.jftp.system.logging.Log;
024: import net.sf.jftp.util.*;
025:
026: import java.awt.*;
027: import java.awt.event.*;
028:
029: import java.io.*;
030:
031: import java.util.*;
032:
033: import javax.swing.*;
034: import javax.swing.JComponent.*;
035: import javax.swing.event.*;
036:
037: public class DownloadQueue extends HPanel implements ActionListener {
038: private static final String SEP = "--> ";
039:
040: // Number of Retry
041: int NumRetry = 5;
042:
043: //private JTextArea text = new JTextArea();
044: private DefaultListModel liststr = new DefaultListModel();
045: private JList list = new JList(liststr);
046: private ArrayList queue = new ArrayList();
047: private queueDownloader thread = new queueDownloader();
048: private QueueRecord lastDownload;
049: private BasicConnection con;
050:
051: // private Vector listeners = new Vector();
052: private HImageButton start = new HImageButton(Settings.resumeImage,
053: "start", "Start queue download...", this );
054: private HImageButton stop = new HImageButton(Settings.pauseImage,
055: "stop", "Stop queue download...", this );
056: private HImageButton save = new HImageButton(Settings.saveImage,
057: "save", "Save queue list to file...", this );
058: private HImageButton load = new HImageButton(Settings.cdImage,
059: "load", "Load queue list from...", this );
060: private HImageButton up = new HImageButton(Settings.downloadImage,
061: "up", "Change order of queue", this );
062: private HImageButton down = new HImageButton(Settings.uploadImage,
063: "down", "Change order of queue", this );
064: private HImageButton delete = new HImageButton(
065: Settings.deleteImage, "del", "Delete item in queue", this );
066:
067: //private HImageButton rotate = new HImageButton(Settings.cmdImage,"rotate","Toggle selected transfer...",this);
068: // connection established?
069: private boolean isThere = false;
070: private boolean downloading = false;
071: private ConnectionHandler handler = new ConnectionHandler();
072: private JLabel statuslabel;
073:
074: public DownloadQueue() {
075: setLayout(new BorderLayout());
076:
077: // list.setCellRenderer(new DirCellRenderer());
078: HPanel cmdP = new HPanel();
079:
080: cmdP.add(start);
081:
082: cmdP.add(stop);
083:
084: cmdP.add(new JLabel(" "));
085:
086: cmdP.add(up);
087:
088: cmdP.add(down);
089:
090: cmdP.add(delete);
091:
092: cmdP.add(new JLabel(" "));
093:
094: cmdP.add(save);
095:
096: cmdP.add(load);
097:
098: start.setSize(24, 24);
099: start.setBorder(true);
100: stop.setSize(24, 24);
101: stop.setBorder(true);
102: up.setSize(24, 24);
103: up.setBorder(true);
104: down.setSize(24, 24);
105: down.setBorder(true);
106: delete.setSize(24, 24);
107: delete.setBorder(true);
108: save.setSize(24, 24);
109: save.setBorder(true);
110: load.setSize(24, 24);
111: load.setBorder(true);
112:
113: JScrollPane dP = new JScrollPane(list);
114:
115: //add("South",cmdP);
116: //add("North",dP);
117: add(cmdP, BorderLayout.SOUTH);
118: add(dP, BorderLayout.CENTER);
119:
120: HPanel status = new HPanel();
121: statuslabel = new JLabel("");
122: statuslabel.setSize(100, 100);
123:
124: status.add(statuslabel);
125: add(status, BorderLayout.NORTH);
126:
127: //*** MY ADDITIONS
128: start.setToolTipText("Start queue download...");
129: stop.setToolTipText("Stop queue download...");
130: save.setToolTipText("Save queue list to file...");
131: load.setToolTipText("Load queue list from...");
132: up.setToolTipText("Change order of queue");
133: down.setToolTipText("Change order of queue");
134: delete.setToolTipText("Delete item in queue");
135:
136: //***
137: }
138:
139: /*
140: public void fresh()
141: {
142: }
143: */
144: public void addFtp(String file) {
145: Log.debug("Remote File" + JFtp.remoteDir.getPath() + file
146: + "Local File" + JFtp.localDir.getPath() + file
147: + "HostName" + JFtp.hostinfo.hostname);
148:
149: QueueRecord rec = new QueueRecord();
150: rec.type = "ftp";
151: rec.hostname = JFtp.hostinfo.hostname;
152: rec.username = JFtp.hostinfo.username;
153: rec.password = JFtp.hostinfo.password;
154: rec.port = JFtp.hostinfo.port;
155: rec.local = JFtp.localDir.getPath();
156: rec.remote = JFtp.remoteDir.getPath();
157: rec.file = file;
158: queue.add(rec);
159: liststr
160: .addElement(rec.hostname + " : " + rec.remote
161: + rec.file);
162: }
163:
164: public void actionPerformed(ActionEvent e) {
165: if (e.getActionCommand().equals("start")) {
166: if (thread != null) {
167: if (!thread.isAlive()) {
168: thread = new queueDownloader();
169: thread.start();
170:
171: //listeners.add(thread);
172: }
173: } else {
174: thread = new queueDownloader();
175: thread.start();
176:
177: //listeners.add(thread);
178: }
179: }
180:
181: else if (e.getActionCommand().equals("stop")) {
182: if (thread != null) {
183: thread.block = true;
184:
185: //QueueRecord rec = (QueueRecord)queue.get(0);
186: //Transfer d = (Transfer)handler.getConnections().get(rec.file);
187: //DataConnection dcon = d.getDataConnection();
188: FtpConnection ftpcon = (FtpConnection) con;
189: DataConnection dcon = ftpcon.getDataConnection();
190: dcon.getCon().work = false;
191:
192: try {
193: dcon.sock.close();
194: } catch (Exception ex) {
195: ex.printStackTrace();
196: }
197: }
198: }
199:
200: else if (e.getActionCommand().equals("down")) {
201: if (list.getSelectedIndex() != -1) {
202: int a = list.getSelectedIndex();
203: int b = a + 1;
204:
205: QueueRecord qa = (QueueRecord) queue.get(b);
206: queue.remove(b);
207: queue.add(a, qa);
208:
209: String sa = (String) liststr.get(b);
210: liststr.remove(b);
211: liststr.add(a, sa);
212:
213: list.setSelectedIndex(b);
214: }
215: } else if (e.getActionCommand().equals("up")) {
216: if (list.getSelectedIndex() != -1) {
217: int a = list.getSelectedIndex() - 1;
218: int b = a + 1;
219:
220: QueueRecord qa = (QueueRecord) queue.get(b);
221: queue.remove(b);
222: queue.add(a, qa);
223:
224: String sa = (String) liststr.get(b);
225: liststr.remove(b);
226: liststr.add(a, sa);
227:
228: list.setSelectedIndex(a);
229: }
230: } else if (e.getActionCommand().equals("del")) {
231: if (list.getSelectedIndex() != -1) {
232: queue.remove(list.getSelectedIndex());
233: liststr.remove(list.getSelectedIndex());
234: list.setSelectedIndex(0);
235: }
236: }
237:
238: else if (e.getActionCommand().equals("save")) {
239: JFileChooser chooser = new JFileChooser();
240: chooser.setDialogTitle("Save file");
241: chooser.setDialogType(JFileChooser.SAVE_DIALOG);
242:
243: int returnVal = chooser.showSaveDialog(new JDialog());
244:
245: if (returnVal == JFileChooser.APPROVE_OPTION) {
246: File f = chooser.getSelectedFile();
247: saveList(f);
248: }
249: }
250:
251: else if (e.getActionCommand().equals("load")) {
252: JFileChooser chooser = new JFileChooser();
253: chooser.setDialogTitle("Open file");
254: chooser.setDialogType(JFileChooser.OPEN_DIALOG);
255:
256: int returnVal = chooser.showOpenDialog(new JDialog());
257:
258: if (returnVal == JFileChooser.APPROVE_OPTION) {
259: File f = chooser.getSelectedFile();
260: loadList(f);
261: }
262: }
263: }
264:
265: private void saveList(File file) {
266: try {
267: if (file.exists()) {
268: file.delete();
269: }
270:
271: FileOutputStream fos;
272: PrintStream f = new PrintStream(
273: (fos = new FileOutputStream(file)));
274:
275: for (int i = 0; i <= queue.size(); i++) {
276: QueueRecord rec = (QueueRecord) queue.get(i);
277: f.println(rec.type);
278: f.println(rec.hostname);
279: f.println(rec.username);
280: f.println(rec.password);
281: f.println(rec.port);
282: f.println(rec.file);
283: f.println(rec.local);
284: f.println(rec.remote);
285: f.println(rec.localip);
286: f.println(rec.domain);
287: }
288:
289: fos.close();
290: } catch (Exception ex) {
291: ex.printStackTrace();
292: }
293: }
294:
295: private void loadList(File file) {
296: try {
297: BufferedReader breader = new BufferedReader(new FileReader(
298: file));
299: boolean Eof = false;
300: queue.clear();
301: liststr.clear();
302:
303: while (!Eof) {
304: QueueRecord rec = new QueueRecord();
305: rec.type = breader.readLine();
306: rec.hostname = breader.readLine();
307: rec.username = breader.readLine();
308: rec.password = breader.readLine();
309: rec.port = breader.readLine();
310: rec.file = breader.readLine();
311: rec.local = breader.readLine();
312: rec.remote = breader.readLine();
313: rec.localip = breader.readLine();
314: rec.domain = breader.readLine();
315:
316: if (rec.hostname == null) {
317: Eof = true;
318: } else {
319: queue.add(rec);
320: liststr.addElement(rec.hostname + " : "
321: + rec.remote + rec.file);
322: }
323: }
324: } catch (Exception ex) {
325: }
326: }
327:
328: // ------------ needed by Logger interface --------------
329: // main log method
330: public void debug(String msg) {
331: System.out.println(msg);
332: }
333:
334: // rarely used
335: public void debugRaw(String msg) {
336: System.out.print(msg);
337: }
338:
339: // methods below are not used yet.
340: public void debug(String msg, Throwable throwable) {
341: }
342:
343: public void warn(String msg) {
344: }
345:
346: public void warn(String msg, Throwable throwable) {
347: }
348:
349: public void error(String msg) {
350: }
351:
352: public void error(String msg, Throwable throwable) {
353: }
354:
355: public void info(String msg) {
356: }
357:
358: public void info(String msg, Throwable throwable) {
359: }
360:
361: public void fatal(String msg) {
362: }
363:
364: public void fatal(String msg, Throwable throwable) {
365: }
366:
367: class QueueRecord {
368: public String type;
369:
370: // parameter used for ftp
371: public String hostname;
372: public String username;
373: public String password;
374: public String port;
375: public String file;
376:
377: // local path
378: public String local;
379:
380: // remote path
381: public String remote;
382:
383: // used only for smb
384: public String localip;
385: public String domain;
386:
387: public QueueRecord() {
388: }
389: }
390:
391: class queueDownloader extends Thread implements ConnectionListener {
392: public boolean block = false;
393: public boolean connected = false;
394: public QueueRecord last;
395: private FtpConnection conFtp;
396:
397: public void run() {
398: try {
399: while ((queue.size() >= 1) && !block) {
400: QueueRecord rec = (QueueRecord) queue.get(0);
401: last = rec;
402:
403: if (!connected) {
404: con = new FtpConnection(rec.hostname, Integer
405: .parseInt(rec.port), "/");
406: conFtp = (FtpConnection) con;
407: conFtp.addConnectionListener(this );
408: conFtp.setConnectionHandler(handler);
409: conFtp.login(rec.username, rec.password);
410: connected = true;
411: }
412:
413: conFtp.chdirNoRefresh(rec.remote);
414:
415: conFtp.setLocalPath(rec.local);
416:
417: int i = 0;
418:
419: while (!isThere) {
420: i++;
421:
422: if (i > NumRetry) {
423: return;
424: }
425:
426: try {
427: Thread.sleep(10);
428: } catch (Exception ex) {
429: ex.printStackTrace();
430: }
431: }
432:
433: conFtp.download(rec.file);
434:
435: statuslabel.setText("");
436:
437: if (queue.size() >= 1) {
438: rec = (QueueRecord) queue.get(0);
439:
440: if ((rec.hostname.compareTo(last.hostname) == 0)
441: && (rec.port.compareTo(last.port) == 0)
442: && (rec.username
443: .compareTo(last.username) == 0)
444: && (rec.password
445: .compareTo(last.password) == 0)) {
446: connected = true;
447: } else {
448: conFtp.disconnect();
449: connected = false;
450: }
451: } else {
452: conFtp.disconnect();
453: connected = false;
454: }
455:
456: Thread.sleep(100);
457: }
458: } catch (InterruptedException e) {
459: }
460:
461: if (connected) {
462: con.disconnect();
463: }
464: }
465:
466: // ------------------ needed by ConnectionListener interface -----------------
467: // called if the remote directory has changed
468: public void updateRemoteDirectory(BasicConnection con) {
469: }
470:
471: // called if a connection has been established
472: public void connectionInitialized(BasicConnection con) {
473: isThere = true;
474: }
475:
476: // called every few kb by DataConnection during the trnsfer (interval can be changed in Settings)
477: public void updateProgress(String file, String type, long bytes) {
478: String strtmp;
479:
480: if (StringUtils.getFile(file).compareTo("") == 0) {
481: strtmp = "directory " + file;
482: } else {
483: strtmp = "file " + StringUtils.getFile(file);
484: }
485:
486: statuslabel.setText("Downloading " + strtmp + " - kbyte "
487: + (bytes / 1024));
488:
489: String tmp;
490:
491: if (type.length() >= 10) {
492: tmp = type.substring(0, 9);
493: } else {
494: tmp = " ";
495: }
496:
497: if (((type.compareTo(DataConnection.FINISHED) == 0) || (tmp
498: .compareTo(DataConnection.DFINISHED) == 0))
499: && !block) {
500: queue.remove(0);
501: liststr.remove(0);
502: }
503: }
504:
505: // called if connection fails
506: public void connectionFailed(BasicConnection con, String why) {
507: System.out.println("connection failed!");
508: }
509:
510: // up- or download has finished
511: public void actionFinished(BasicConnection con) {
512: }
513: }
514: }
|