1 package ch
.cyberduck
.connection
;
11 * Copyright (c) 2003 David Kocher. All rights reserved.
12 * http://icu.unizh.ch/~dkocher/
14 * This program is free software; you can redistribute it and/or modify
15 * it under the terms of the GNU General Public License as published by
16 * the Free Software Foundation; either version 2 of the License, or
17 * (at your option) any later version.
19 * This program is distributed in the hope that it will be useful,
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 * GNU General Public License for more details.
24 * Bug fixes, suggestions and comments should be sent to:
25 * dkocher@cyberduck.ch
28 import javax
.swing
.JOptionPane
;
30 import java
.io
.IOException
;
32 import ch
.cyberduck
.Cyberduck
;
33 import ch
.cyberduck
.Preferences
;
36 * Used to validate a requested download (ie initial, resume) action.
40 private Bookmark transfer
;
41 private Status status
;
42 private JOptionPane dialog
;
44 public Check(Bookmark transfer
, Status status
) {
45 Cyberduck
.DEBUG("[Check] new Check()");
46 this.transfer
= transfer
;
51 * @return boolean Return false if validation fails for whatever reason
53 public boolean validate(Message handler
) throws IOException
{
54 status
.setResume(false);
55 if(handler
.equals(Status
.RELOAD
)) {
56 Cyberduck
.DEBUG("[Check] Reload.");
57 return this.validateOverwrite();
59 if(! this.validateOverwrite())
60 throw new IOException("Bookmark canceled by user.");
64 if(handler
.equals(Status
.RESUME
)) {
65 Cyberduck
.DEBUG("[Check] Resume.");
66 return this.validateResume();
68 if(! this.validateResume())
69 throw new IOException("Bookmark canceled by user.");
73 if(handler
.equals(Status
.INITIAL
)) {
74 Cyberduck
.DEBUG("[Check] Initial.");
75 return this.validateInitial();
77 if(! this.validateInitial())
78 throw new IOException("Bookmark canceled by user.");
83 throw new IOException("Unknown handler.");
87 private boolean validateInitial() {
88 if(status
.isComplete()) {
89 Cyberduck
.DEBUG("[Check] Bookmark already complete.");
90 String
[] options
= {"Cancel", "Overwrite", "Similar Name"};
91 int value
= dialog
.showOptionDialog (
93 "The download of '" + transfer
.getLocalFilename() + "' has already \n" +
96 dialog
.DEFAULT_OPTION
,
97 dialog
.QUESTION_MESSAGE
,
106 this.prepareOverwrite();
109 this.prepareSimilarName();
116 if(transfer.getLocalTempPath() == null || !transfer.getLocalTempPath().exists()) {
117 this.prepareInitial();
121 if(! transfer
.getLocalPath().exists() && !transfer
.getLocalTempPath().exists()) {
122 this.prepareInitial();
125 else { // transfer.getLocalTempPath().exists()
127 if(! (transfer.getLocalTempPath().length() > 0)) {
128 this.prepareOverwrite();
133 if(Preferences
.instance().getProperty("duplicate.ask").equals("true")) {
134 Object
[] values
= {"Resume", "Similar Name", "Overwrite", "Cancel"};
135 int option
= dialog
.showOptionDialog(
137 "The file '" + transfer
.getLocalFilename() + "' already exists\n" +
138 "in your download directory.",
140 dialog
.DEFAULT_OPTION
,
141 dialog
.QUESTION_MESSAGE
,
148 this.prepareResume();
151 this.prepareSimilarName();
154 this.prepareOverwrite();
162 if(Preferences
.instance().getProperty("duplicate.similar").equals("true")) {
163 this.prepareSimilarName();
166 if(Preferences
.instance().getProperty("duplicate.resume").equals("true")) {
167 this.prepareResume();
170 if(Preferences
.instance().getProperty("duplicate.overwrite").equals("true")) {
171 this.prepareOverwrite();
174 System
.err
.println("[Check] Fatal error: dupliate property not set");
180 private boolean validateResume() {
181 if(transfer
.getLocalTempPath() != null && transfer
.getLocalTempPath().exists()) {
182 this.prepareResume();
185 else { //if(!transfer.getLocalTempPath().exists())
186 this.prepareInitial();
191 private boolean validateSimilar() {
192 this.prepareSimilarName();
196 private boolean validateOverwrite() {
197 this.prepareOverwrite();
201 private void prepareResume() {
202 Cyberduck
.DEBUG("[Check] prepareResume()");
203 status
.setResume(true);
204 status
.setCurrent(new Long(transfer
.getLocalTempPath().length()).intValue());
207 private void prepareSimilarName() {
208 Cyberduck
.DEBUG("[Check] prepareSimilarName()");
209 status
.setResume(false);
210 status
.setCurrent(0);
213 String filename
= transfer
.getLocalFilename();
215 int index
= filename
.lastIndexOf(".");
217 fn
= filename
.substring(0, index
) + "_" + no
+ filename
.substring(index
);
218 transfer
.setLocalPath(new File(transfer
.getLocalDirectory(), fn
));
221 while (transfer
.getLocalTempPath().exists() || transfer
.getLocalPath().exists());
224 private void prepareOverwrite() {
225 Cyberduck
.DEBUG("[Check] prepareOverwrite()");
226 status
.setResume(false);
227 status
.setCurrent(0);
230 private void prepareInitial() {
231 Cyberduck
.DEBUG("[Check] prepareInitial()");
232 status
.setResume(false);
233 status
.setCurrent(0);