2 * Copyright 2007, 2008, 2009 Electronic Business Systems Ltd.
4 * This file is part of GSS.
6 * GSS is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
11 * GSS is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with GSS. If not, see <http://www.gnu.org/licenses/>.
19 package gr.ebs.gss.client;
21 import gr.ebs.gss.client.rest.GetCommand;
22 import gr.ebs.gss.client.rest.PostCommand;
23 import gr.ebs.gss.client.rest.RestCommand;
24 import gr.ebs.gss.client.rest.RestException;
25 import gr.ebs.gss.client.rest.resource.FileResource;
26 import gr.ebs.gss.client.rest.resource.FolderResource;
27 import gr.ebs.gss.client.rest.resource.RestResourceWrapper;
28 import gr.ebs.gss.client.rest.resource.UploadStatusResource;
30 import java.util.ArrayList;
31 import java.util.List;
33 import com.google.gwt.core.client.GWT;
34 import com.google.gwt.dom.client.NativeEvent;
35 import com.google.gwt.event.dom.client.ClickEvent;
36 import com.google.gwt.event.dom.client.ClickHandler;
37 import com.google.gwt.event.dom.client.KeyCodes;
38 import com.google.gwt.http.client.URL;
39 import com.google.gwt.json.client.JSONObject;
40 import com.google.gwt.json.client.JSONString;
41 import com.google.gwt.user.client.DeferredCommand;
42 import com.google.gwt.user.client.Event.NativePreviewEvent;
43 import com.google.gwt.user.client.Timer;
44 import com.google.gwt.user.client.ui.Button;
45 import com.google.gwt.user.client.ui.DialogBox;
46 import com.google.gwt.user.client.ui.FileUpload;
47 import com.google.gwt.user.client.ui.FormPanel;
48 import com.google.gwt.user.client.ui.FormPanel.SubmitCompleteEvent;
49 import com.google.gwt.user.client.ui.FormPanel.SubmitCompleteHandler;
50 import com.google.gwt.user.client.ui.FormPanel.SubmitEvent;
51 import com.google.gwt.user.client.ui.FormPanel.SubmitHandler;
52 import com.google.gwt.user.client.ui.Grid;
53 import com.google.gwt.user.client.ui.HTML;
54 import com.google.gwt.user.client.ui.HasHorizontalAlignment;
55 import com.google.gwt.user.client.ui.Hidden;
56 import com.google.gwt.user.client.ui.HorizontalPanel;
57 import com.google.gwt.user.client.ui.Label;
58 import com.google.gwt.user.client.ui.VerticalPanel;
61 * The 'File upload' dialog box implementation.
63 public class FileUploadDialog extends DialogBox implements Updateable {
65 protected int prgBarInterval = 1500;
67 private ProgressBar progressBar;
69 protected RepeatingTimer repeater = new RepeatingTimer(this, prgBarInterval);
71 public static final boolean DONE = true;
74 * The Form element that performs the file upload.
76 private final FormPanel form = new FormPanel();
78 private final FileUpload upload = new FileUpload();
80 protected final Label filenameLabel = new Label("");
82 protected List<FileResource> files;
84 protected boolean cancelEvent = false;
86 protected String fileNameToUse;
88 protected FolderResource folder;
91 * The widget's constructor.
93 public FileUploadDialog() {
94 // Set the dialog's caption.
95 setText("File upload");
96 setAnimationEnabled(true);
97 // Since we're going to add a FileUpload widget, we'll need to set the
98 // form to use the POST method, and multipart MIME encoding.
99 form.setEncoding(FormPanel.ENCODING_MULTIPART);
100 form.setMethod(FormPanel.METHOD_POST);
102 // Create a panel to hold all of the form widgets.
103 VerticalPanel panel = new VerticalPanel();
104 form.setWidget(panel);
105 final HTML info = new HTML("You may select a file to upload. Install" +
106 " <a href='http://gears.google.com/' target='_blank'>Google " +
107 "Gears</a><br> for uploading multiple files simultaneously.");
108 info.addStyleName("gss-uploadNote");
110 final Hidden date = new Hidden("Date", "");
112 final Hidden auth = new Hidden("Authorization", "");
114 // Add an informative label with the folder name.
115 Object selection = GSS.get().getTreeView().getSelection();
116 folder = ((RestResourceWrapper) selection).getResource();
117 upload.setName("file");
118 filenameLabel.setText("");
119 filenameLabel.setVisible(false);
120 filenameLabel.setStyleName("props-labels");
121 HorizontalPanel fileUloadPanel = new HorizontalPanel();
122 fileUloadPanel.add(filenameLabel);
123 fileUloadPanel.add(upload);
124 Grid generalTable = new Grid(2, 2);
125 generalTable.setText(0, 0, "Folder");
126 generalTable.setText(1, 0, "File");
127 generalTable.setText(0, 1, folder.getName());
128 generalTable.setWidget(1, 1, fileUloadPanel);
129 generalTable.getCellFormatter().setStyleName(0, 0, "props-labels");
130 generalTable.getCellFormatter().setStyleName(1, 0, "props-labels");
131 generalTable.getCellFormatter().setStyleName(0, 1, "props-values");
132 generalTable.getCellFormatter().setStyleName(1, 1, "props-values");
133 generalTable.setCellSpacing(4);
135 panel.add(generalTable);
137 // Create a panel to hold the buttons.
138 HorizontalPanel buttons = new HorizontalPanel();
140 // Create the 'upload' button, along with a listener that submits the
142 final Button submit = new Button("Upload", new ClickHandler() {
144 public void onClick(ClickEvent event) {
149 buttons.setCellHorizontalAlignment(submit, HasHorizontalAlignment.ALIGN_CENTER);
150 // Create the 'Cancel' button, along with a listener that hides the
151 // dialog when the button is clicked.
152 final Button cancel = new Button("Cancel", new ClickHandler() {
154 public void onClick(ClickEvent event) {
160 buttons.setCellHorizontalAlignment(cancel, HasHorizontalAlignment.ALIGN_CENTER);
161 buttons.setSpacing(8);
162 buttons.addStyleName("gss-DialogBox");
164 // Add an event handler to the form.
165 form.addSubmitHandler(new SubmitHandler() {
168 public void onSubmit(SubmitEvent event) {
170 // This event is fired just before the form is submitted. We can
171 // take this opportunity to perform validation.
172 if (upload.getFilename().length() == 0) {
173 app.displayError("You must select a file!");
179 GWT.log("Cancel:" + cancelEvent, null);
182 app.displayError("The specified file name already exists in this folder");
187 fileNameToUse = getFilename(upload.getFilename());
189 FileResource selectedFile = getFileForName(fileNameToUse);
190 if (selectedFile == null ) {
191 //we are going to create a file
192 apath = folder.getUri();
193 if (!apath.endsWith("/"))
195 apath = apath + encodeComponent(fileNameToUse);
197 apath = selectedFile.getUri();
198 form.setAction(apath);
199 String dateString = RestCommand.getDate();
200 String resource = apath.substring(app.getApiPath().length() - 1, apath.length());
201 String sig = RestCommand.calculateSig("POST", dateString, resource, RestCommand.base64decode(app.getToken()));
202 date.setValue(dateString);
203 auth.setValue(app.getCurrentUserResource().getUsername() + " " + sig);
204 GWT.log("FolderPATH:" + folder.getUri(), null);
205 submit.setEnabled(false);
206 upload.setVisible(false);
207 filenameLabel.setText(fileNameToUse);
208 filenameLabel.setVisible(true);
210 progressBar.setVisible(true);
216 form.addSubmitCompleteHandler(new SubmitCompleteHandler() {
219 public void onSubmitComplete(SubmitCompleteEvent event) {
220 // When the form submission is successfully completed, this
221 // event is fired. Assuming the service returned a response
222 // of type text/html, we can get the result text here (see
223 // the FormPanel documentation for further explanation).
224 String results = event.getResults();
226 // Unfortunately the results are never empty, even in
227 // the absense of errors, so we have to check for '<pre></pre>'.
228 if (!results.equalsIgnoreCase("<pre></pre>")) {
229 GWT.log(results, null);
230 GSS.get().displayError(results);
232 progressBar.setProgress(100);
234 GSS.get().getTreeView().updateNode(GSS.get().getTreeView().getSelection());
235 GSS.get().getStatusPanel().updateStats();
242 progressBar = new ProgressBar(50, ProgressBar.SHOW_TIME_REMAINING);
243 panel.add(progressBar);
244 progressBar.setVisible(false);
245 panel.setCellHorizontalAlignment(buttons, HasHorizontalAlignment.ALIGN_CENTER);
246 panel.setCellHorizontalAlignment(progressBar, HasHorizontalAlignment.ALIGN_CENTER);
247 panel.addStyleName("gss-DialogBox");
248 addStyleName("gss-DialogBox");
253 protected void onPreviewNativeEvent(NativePreviewEvent preview) {
254 super.onPreviewNativeEvent(preview);
256 NativeEvent evt = preview.getNativeEvent();
257 if (evt.getType().equals("keydown"))
258 // Use the popup's key preview hooks to close the dialog when either
259 // enter or escape is pressed.
260 switch (evt.getKeyCode()) {
261 case KeyCodes.KEY_ENTER:
264 case KeyCodes.KEY_ESCAPE:
273 * Cancels the file upload.
275 private void cancelUpload() {
281 * Make any last minute checks and start the upload.
283 public void prepareAndSubmit() {
284 final String fname = getFilename(upload.getFilename());
285 if (getFileForName(fname) == null) {
286 //we are going to create a file, so we check to see if there is a trashed file with the same name
287 FileResource same = null;
288 for (FileResource fres : folder.getFiles())
289 if (fres.isDeleted() && fres.getName().equals(fname))
294 final FileResource sameFile = same;
295 GWT.log("Same deleted file", null);
296 ConfirmationDialog confirm = new ConfirmationDialog("A file with " +
297 "the same name exists in the trash. If you continue,<br/>the trashed " +
298 "file '" + fname + "' will be renamed automatically for you.", "Continue") {
301 public void cancel() {
302 FileUploadDialog.this.hide();
306 public void confirm() {
307 updateTrashedFile(getBackupFilename(fname), sameFile);
315 // We are going to update an existing file, so show a confirmation dialog.
316 ConfirmationDialog confirm = new ConfirmationDialog("Are you sure " +
317 "you want to update " + fname + "?", "Update") {
320 public void cancel() {
321 FileUploadDialog.this.hide();
325 public void confirm() {
335 * Returns the file name from a potential full path argument. Apparently IE
336 * insists on sending the full path name of a file when uploading, forcing
337 * us to trim the extra path info. Since this is only observed on Windows we
338 * get to check for a single path separator value.
340 * @param name the potentially full path name of a file
341 * @return the file name without extra path information
343 protected String getFilename(String name) {
344 int pathSepIndex = name.lastIndexOf("\\");
345 if (pathSepIndex == -1) {
346 pathSepIndex = name.lastIndexOf("/");
347 if (pathSepIndex == -1)
350 return name.substring(pathSepIndex + 1);
354 * Check whether the file name exists in selected folder.
358 private boolean canContinue() {
361 String fileName = getFilename(upload.getFilename());
362 if (getFileForName(fileName) == null) {
363 // For file creation, check to see if the file already exists.
364 GWT.log("filename to upload:" + fileName, null);
365 for (FileResource dto : files) {
366 GWT.log("Check:" + dto.getName() + "/" + fileName, null);
367 if (!dto.isDeleted() && dto.getName().equals(fileName)) {
376 class RepeatingTimer extends Timer {
378 private Updateable updateable;
380 private int interval = 1500;
382 private boolean running = true;
384 RepeatingTimer(Updateable _updateable, int _interval) {
385 updateable = _updateable;
386 interval = _interval;
394 public void start() {
397 scheduleRepeating(interval);
400 public void finish() {
405 public int getInterval() {
409 public void setInterval(int anInterval) {
410 if (interval != anInterval) {
411 interval = anInterval;
421 public void update() {
422 String apath = folder.getUri();
423 if (!apath.endsWith("/"))
425 apath = apath + encodeComponent(fileNameToUse) + "?progress=" + encodeComponent(fileNameToUse);
426 GetCommand eg = new GetCommand<UploadStatusResource>(UploadStatusResource.class, apath, false, null) {
429 public void onComplete() {
430 UploadStatusResource res = getResult();
431 progressBar.setProgress(res.percent());
435 public void onError(Throwable t) {
440 DeferredCommand.addCommand(eg);
443 protected String getBackupFilename(String filename) {
444 List<FileResource> filesInSameFolder = new ArrayList<FileResource>();
445 for (FileResource deleted : folder.getFiles())
446 if (deleted.isDeleted())
447 filesInSameFolder.add(deleted);
449 for (FileResource same : filesInSameFolder)
450 if (same.getName().startsWith(filename)) {
451 String toCheck = same.getName().substring(filename.length(), same.getName().length());
452 if (toCheck.startsWith(" ")) {
455 test = Integer.valueOf(toCheck.replace(" ", ""));
456 } catch (NumberFormatException e) {
457 // Do nothing since string is not a number.
464 return filename + " " + i;
468 * Rename the conflicting trashed file with the supplied new name.
470 private void updateTrashedFile(String newName, FileResource trashedFile) {
471 JSONObject json = new JSONObject();
472 json.put("name", new JSONString(newName));
473 PostCommand cf = new PostCommand(trashedFile.getUri() + "?update=", json.toString(), 200) {
476 public void onComplete() {
481 public void onError(Throwable t) {
484 if (t instanceof RestException) {
485 int statusCode = ((RestException) t).getHttpStatusCode();
486 if (statusCode == 405)
487 app.displayError("You don't have the necessary permissions");
488 else if (statusCode == 404)
489 app.displayError("User in permissions does not exist");
490 else if (statusCode == 409)
491 app.displayError("A file with the same name already exists");
492 else if (statusCode == 413)
493 app.displayError("Your quota has been exceeded");
495 app.displayError("Unable to modify file:" + ((RestException) t).getHttpStatusText());
497 app.displayError("System error modifying file:" + t.getMessage());
501 DeferredCommand.addCommand(cf);
504 protected FileResource getFileForName(String name){
505 for (FileResource f : folder.getFiles())
506 if (!f.isDeleted() && f.getName().equals(name))
513 * Same as URL.encodeComponent, but also
514 * encode apostrophe since browsers aren't consistent about it
515 * (FF encodes, IE does not).
517 private String encodeComponent(String decodedURLComponent) {
518 String retv = URL.encodeComponent(decodedURLComponent);
519 retv = retv.replaceAll("'", "%27");
526 * @param newFiles the files to set
528 public void setFiles(List<FileResource> newFiles) {