*** empty log message ***
[cyberduck.git] / source / ch / cyberduck / Attic / connection / Check.java
blobd7a0af3117b24831d243b1c5bb929678d09750ac
1 package ch.cyberduck.connection;
3 /*
4 * Check.java
5 * Cyberduck
7 * $Header$
8 * $Revision$
9 * $Date$
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;
29 import java.io.File;
30 import java.io.IOException;
32 import ch.cyberduck.Cyberduck;
33 import ch.cyberduck.Preferences;
35 /**
36 * Used to validate a requested download (ie initial, resume) action.
37 * @version $Id$
39 public class Check {
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;
47 this.status = status;
50 /**
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.");
61 return;
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.");
70 return;
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.");
79 return;
82 else {
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 (
92 null,
93 "The download of '" + transfer.getLocalFilename() + "' has already \n" +
94 "been completed.",
95 "Restart transfer",
96 dialog.DEFAULT_OPTION,
97 dialog.QUESTION_MESSAGE,
98 null,
99 options,
100 options[0]
102 switch(value) {
103 case 0:
104 return false;
105 case 1:
106 this.prepareOverwrite();
107 return true;
108 case 2:
109 this.prepareSimilarName();
110 return true;
111 default:
112 return false;
116 if(transfer.getLocalTempPath() == null || !transfer.getLocalTempPath().exists()) {
117 this.prepareInitial();
118 return true;
121 if(! transfer.getLocalPath().exists() && !transfer.getLocalTempPath().exists()) {
122 this.prepareInitial();
123 return true;
125 else { // transfer.getLocalTempPath().exists()
127 if(! (transfer.getLocalTempPath().length() > 0)) {
128 this.prepareOverwrite();
129 return true;
132 //else {
133 if(Preferences.instance().getProperty("duplicate.ask").equals("true")) {
134 Object[] values = {"Resume", "Similar Name", "Overwrite", "Cancel"};
135 int option = dialog.showOptionDialog(
136 null,
137 "The file '" + transfer.getLocalFilename() + "' already exists\n" +
138 "in your download directory.",
139 "File exists",
140 dialog.DEFAULT_OPTION,
141 dialog.QUESTION_MESSAGE,
142 null,
143 values,
144 values[0]
146 switch(option) {
147 case 0:
148 this.prepareResume();
149 return true;
150 case 1:
151 this.prepareSimilarName();
152 return true;
153 case 2:
154 this.prepareOverwrite();
155 return true;
156 case 3:
157 return false;
158 default:
159 return false;
162 if(Preferences.instance().getProperty("duplicate.similar").equals("true")) {
163 this.prepareSimilarName();
164 return true;
166 if(Preferences.instance().getProperty("duplicate.resume").equals("true")) {
167 this.prepareResume();
168 return true;
170 if(Preferences.instance().getProperty("duplicate.overwrite").equals("true")) {
171 this.prepareOverwrite();
172 return true;
174 System.err.println("[Check] Fatal error: dupliate property not set");
177 return false;
180 private boolean validateResume() {
181 if(transfer.getLocalTempPath() != null && transfer.getLocalTempPath().exists()) {
182 this.prepareResume();
183 return true;
185 else { //if(!transfer.getLocalTempPath().exists())
186 this.prepareInitial();
187 return true;
191 private boolean validateSimilar() {
192 this.prepareSimilarName();
193 return true;
196 private boolean validateOverwrite() {
197 this.prepareOverwrite();
198 return true;
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);
212 String fn = null;
213 String filename = transfer.getLocalFilename();
214 int no = 1;
215 int index = filename.lastIndexOf(".");
216 do {
217 fn = filename.substring(0, index) + "_" + no + filename.substring(index);
218 transfer.setLocalPath(new File(transfer.getLocalDirectory(), fn));
219 no++;
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);