2 * Copyright 2011 GRNET S.A. All rights reserved.
4 * Redistribution and use in source and binary forms, with or
5 * without modification, are permitted provided that the following
8 * 1. Redistributions of source code must retain the above
9 * copyright notice, this list of conditions and the following
12 * 2. Redistributions in binary form must reproduce the above
13 * copyright notice, this list of conditions and the following
14 * disclaimer in the documentation and/or other materials
15 * provided with the distribution.
17 * THIS SOFTWARE IS PROVIDED BY GRNET S.A. ``AS IS'' AND ANY EXPRESS
18 * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
19 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
20 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL GRNET S.A OR
21 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
22 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
23 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
24 * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
25 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
27 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28 * POSSIBILITY OF SUCH DAMAGE.
30 * The views and conclusions contained in the software and
31 * documentation are those of the authors and should not be
32 * interpreted as representing official policies, either expressed
33 * or implied, of GRNET S.A.
35 package gr.grnet.pithos.web.client;
37 import gr.grnet.pithos.web.client.rest.GetCommand;
38 import gr.grnet.pithos.web.client.rest.PostCommand;
39 import gr.grnet.pithos.web.client.rest.RestCommand;
40 import gr.grnet.pithos.web.client.rest.RestException;
41 import gr.grnet.pithos.web.client.rest.resource.FileResource;
42 import gr.grnet.pithos.web.client.rest.resource.FolderResource;
43 import gr.grnet.pithos.web.client.rest.resource.RestResourceWrapper;
44 import gr.grnet.pithos.web.client.rest.resource.UploadStatusResource;
46 import java.util.ArrayList;
47 import java.util.List;
49 import com.google.gwt.core.client.GWT;
50 import com.google.gwt.dom.client.NativeEvent;
51 import com.google.gwt.event.dom.client.ClickEvent;
52 import com.google.gwt.event.dom.client.ClickHandler;
53 import com.google.gwt.event.dom.client.KeyCodes;
54 import com.google.gwt.http.client.URL;
55 import com.google.gwt.json.client.JSONObject;
56 import com.google.gwt.json.client.JSONString;
57 import com.google.gwt.user.client.DeferredCommand;
58 import com.google.gwt.user.client.Event.NativePreviewEvent;
59 import com.google.gwt.user.client.Timer;
60 import com.google.gwt.user.client.ui.Button;
61 import com.google.gwt.user.client.ui.DialogBox;
62 import com.google.gwt.user.client.ui.FileUpload;
63 import com.google.gwt.user.client.ui.FormPanel;
64 import com.google.gwt.user.client.ui.FormPanel.SubmitCompleteEvent;
65 import com.google.gwt.user.client.ui.FormPanel.SubmitCompleteHandler;
66 import com.google.gwt.user.client.ui.FormPanel.SubmitEvent;
67 import com.google.gwt.user.client.ui.FormPanel.SubmitHandler;
68 import com.google.gwt.user.client.ui.Grid;
69 import com.google.gwt.user.client.ui.HTML;
70 import com.google.gwt.user.client.ui.HasHorizontalAlignment;
71 import com.google.gwt.user.client.ui.Hidden;
72 import com.google.gwt.user.client.ui.HorizontalPanel;
73 import com.google.gwt.user.client.ui.Label;
74 import com.google.gwt.user.client.ui.VerticalPanel;
77 * The 'File upload' dialog box implementation.
79 public class FileUploadDialog extends DialogBox implements Updateable {
81 protected int prgBarInterval = 1500;
83 private ProgressBar progressBar;
85 protected RepeatingTimer repeater = new RepeatingTimer(this, prgBarInterval);
87 public static final boolean DONE = true;
90 * The Form element that performs the file upload.
92 private final FormPanel form = new FormPanel();
94 private final FileUpload upload = new FileUpload();
96 protected final Label filenameLabel = new Label("");
98 protected List<FileResource> files;
100 protected boolean cancelEvent = false;
102 protected String fileNameToUse;
104 protected FolderResource folder;
107 * The widget's constructor.
109 public FileUploadDialog() {
110 // Set the dialog's caption.
111 setText("File upload");
112 setAnimationEnabled(true);
113 // Since we're going to add a FileUpload widget, we'll need to set the
114 // form to use the POST method, and multipart MIME encoding.
115 form.setEncoding(FormPanel.ENCODING_MULTIPART);
116 form.setMethod(FormPanel.METHOD_POST);
118 // Create a panel to hold all of the form widgets.
119 VerticalPanel panel = new VerticalPanel();
120 form.setWidget(panel);
121 final HTML info = new HTML("You may select a file to upload. Install" +
122 " <a href='http://gears.google.com/' target='_blank'>Google " +
123 "Gears</a><br> for uploading multiple files simultaneously.");
124 info.addStyleName("pithos-uploadNote");
126 final Hidden date = new Hidden("Date", "");
128 final Hidden auth = new Hidden("Authorization", "");
130 // Add an informative label with the folder name.
131 Object selection = GSS.get().getTreeView().getSelection();
132 folder = ((RestResourceWrapper) selection).getResource();
133 upload.setName("file");
134 filenameLabel.setText("");
135 filenameLabel.setVisible(false);
136 filenameLabel.setStyleName("props-labels");
137 HorizontalPanel fileUloadPanel = new HorizontalPanel();
138 fileUloadPanel.add(filenameLabel);
139 fileUloadPanel.add(upload);
140 upload.getElement().setId("fileUploadDiallog.uploadPanel");
141 Grid generalTable = new Grid(2, 2);
142 generalTable.setText(0, 0, "Folder");
143 generalTable.setText(1, 0, "File");
144 generalTable.setText(0, 1, folder.getName());
145 generalTable.setWidget(1, 1, fileUloadPanel);
146 generalTable.getCellFormatter().setStyleName(0, 0, "props-labels");
147 generalTable.getCellFormatter().setStyleName(1, 0, "props-labels");
148 generalTable.getCellFormatter().setStyleName(0, 1, "props-values");
149 generalTable.getCellFormatter().setStyleName(1, 1, "props-values");
150 generalTable.setCellSpacing(4);
152 panel.add(generalTable);
154 // Create a panel to hold the buttons.
155 HorizontalPanel buttons = new HorizontalPanel();
157 // Create the 'upload' button, along with a listener that submits the
159 final Button submit = new Button("Upload", new ClickHandler() {
161 public void onClick(ClickEvent event) {
165 submit.getElement().setId("fileUploadDialog.button.upload");
167 buttons.setCellHorizontalAlignment(submit, HasHorizontalAlignment.ALIGN_CENTER);
168 // Create the 'Cancel' button, along with a listener that hides the
169 // dialog when the button is clicked.
170 final Button cancel = new Button("Cancel", new ClickHandler() {
172 public void onClick(ClickEvent event) {
177 cancel.getElement().setId("fileUploadDialog.button.cancel");
179 buttons.setCellHorizontalAlignment(cancel, HasHorizontalAlignment.ALIGN_CENTER);
180 buttons.setSpacing(8);
181 buttons.addStyleName("pithos-DialogBox");
183 // Add an event handler to the form.
184 form.addSubmitHandler(new SubmitHandler() {
187 public void onSubmit(SubmitEvent event) {
189 // This event is fired just before the form is submitted. We can
190 // take this opportunity to perform validation.
191 if (upload.getFilename().length() == 0) {
192 app.displayError("You must select a file!");
198 GWT.log("Cancel:" + cancelEvent, null);
201 app.displayError("The specified file name already exists in this folder");
206 fileNameToUse = getFilename(upload.getFilename());
208 FileResource selectedFile = getFileForName(fileNameToUse);
209 if (selectedFile == null ) {
210 //we are going to create a file
211 apath = folder.getUri();
212 if (!apath.endsWith("/"))
214 apath = apath + encodeComponent(fileNameToUse);
216 apath = selectedFile.getUri();
217 form.setAction(apath);
218 String dateString = RestCommand.getDate();
219 String resource = apath.substring(app.getApiPath().length() - 1, apath.length());
220 String sig = RestCommand.calculateSig("POST", dateString, resource, RestCommand.base64decode(app.getToken()));
221 date.setValue(dateString);
222 auth.setValue(app.getCurrentUserResource().getUsername() + " " + sig);
223 GWT.log("FolderPATH:" + folder.getUri(), null);
224 submit.setEnabled(false);
225 upload.setVisible(false);
226 filenameLabel.setText(fileNameToUse);
227 filenameLabel.setVisible(true);
229 progressBar.setVisible(true);
235 form.addSubmitCompleteHandler(new SubmitCompleteHandler() {
238 public void onSubmitComplete(SubmitCompleteEvent event) {
239 // When the form submission is successfully completed, this
240 // event is fired. Assuming the service returned a response
241 // of type text/html, we can get the result text here (see
242 // the FormPanel documentation for further explanation).
243 String results = event.getResults();
245 // Unfortunately the results are never empty, even in
246 // the absense of errors, so we have to check for '<pre></pre>'.
247 if (!results.equalsIgnoreCase("<pre></pre>")) {
248 GWT.log(results, null);
249 GSS.get().displayError(results);
251 progressBar.setProgress(100);
253 GSS.get().getTreeView().updateNode(GSS.get().getTreeView().getSelection());
254 GSS.get().getStatusPanel().updateStats();
261 progressBar = new ProgressBar(50, ProgressBar.SHOW_TIME_REMAINING);
262 panel.add(progressBar);
263 progressBar.setVisible(false);
264 panel.setCellHorizontalAlignment(buttons, HasHorizontalAlignment.ALIGN_CENTER);
265 panel.setCellHorizontalAlignment(progressBar, HasHorizontalAlignment.ALIGN_CENTER);
266 panel.addStyleName("pithos-DialogBox");
267 addStyleName("pithos-DialogBox");
272 protected void onPreviewNativeEvent(NativePreviewEvent preview) {
273 super.onPreviewNativeEvent(preview);
275 NativeEvent evt = preview.getNativeEvent();
276 if (evt.getType().equals("keydown"))
277 // Use the popup's key preview hooks to close the dialog when either
278 // enter or escape is pressed.
279 switch (evt.getKeyCode()) {
280 case KeyCodes.KEY_ENTER:
283 case KeyCodes.KEY_ESCAPE:
292 * Cancels the file upload.
294 private void cancelUpload() {
300 * Make any last minute checks and start the upload.
302 public void prepareAndSubmit() {
303 final String fname = getFilename(upload.getFilename());
304 if (getFileForName(fname) == null) {
305 //we are going to create a file, so we check to see if there is a trashed file with the same name
306 FileResource same = null;
307 for (FileResource fres : folder.getFiles())
308 if (fres.isDeleted() && fres.getName().equals(fname))
313 final FileResource sameFile = same;
314 GWT.log("Same deleted file", null);
315 ConfirmationDialog confirm = new ConfirmationDialog("A file with " +
316 "the same name exists in the trash. If you continue,<br/>the trashed " +
317 "file '" + fname + "' will be renamed automatically for you.", "Continue") {
320 public void cancel() {
321 FileUploadDialog.this.hide();
325 public void confirm() {
326 updateTrashedFile(getBackupFilename(fname), sameFile);
334 // We are going to update an existing file, so show a confirmation dialog.
335 ConfirmationDialog confirm = new ConfirmationDialog("Are you sure " +
336 "you want to update " + fname + "?", "Update") {
339 public void cancel() {
340 FileUploadDialog.this.hide();
344 public void confirm() {
354 * Returns the file name from a potential full path argument. Apparently IE
355 * insists on sending the full path name of a file when uploading, forcing
356 * us to trim the extra path info. Since this is only observed on Windows we
357 * get to check for a single path separator value.
359 * @param name the potentially full path name of a file
360 * @return the file name without extra path information
362 protected String getFilename(String name) {
363 int pathSepIndex = name.lastIndexOf("\\");
364 if (pathSepIndex == -1) {
365 pathSepIndex = name.lastIndexOf("/");
366 if (pathSepIndex == -1)
369 return name.substring(pathSepIndex + 1);
373 * Check whether the file name exists in selected folder.
377 private boolean canContinue() {
380 String fileName = getFilename(upload.getFilename());
381 if (getFileForName(fileName) == null) {
382 // For file creation, check to see if the file already exists.
383 GWT.log("filename to upload:" + fileName, null);
384 for (FileResource dto : files) {
385 GWT.log("Check:" + dto.getName() + "/" + fileName, null);
386 if (!dto.isDeleted() && dto.getName().equals(fileName)) {
395 class RepeatingTimer extends Timer {
397 private Updateable updateable;
399 private int interval = 1500;
401 private boolean running = true;
403 RepeatingTimer(Updateable _updateable, int _interval) {
404 updateable = _updateable;
405 interval = _interval;
413 public void start() {
416 scheduleRepeating(interval);
419 public void finish() {
424 public int getInterval() {
428 public void setInterval(int anInterval) {
429 if (interval != anInterval) {
430 interval = anInterval;
440 public void update() {
441 String apath = folder.getUri();
442 if (!apath.endsWith("/"))
444 apath = apath + encodeComponent(fileNameToUse) + "?progress=" + encodeComponent(fileNameToUse);
445 GetCommand eg = new GetCommand<UploadStatusResource>(UploadStatusResource.class, apath, false, null) {
448 public void onComplete() {
449 UploadStatusResource res = getResult();
450 progressBar.setProgress(res.percent());
454 public void onError(Throwable t) {
459 DeferredCommand.addCommand(eg);
462 protected String getBackupFilename(String filename) {
463 List<FileResource> filesInSameFolder = new ArrayList<FileResource>();
464 for (FileResource deleted : folder.getFiles())
465 if (deleted.isDeleted())
466 filesInSameFolder.add(deleted);
468 for (FileResource same : filesInSameFolder)
469 if (same.getName().startsWith(filename)) {
470 String toCheck = same.getName().substring(filename.length(), same.getName().length());
471 if (toCheck.startsWith(" ")) {
474 test = Integer.valueOf(toCheck.replace(" ", ""));
475 } catch (NumberFormatException e) {
476 // Do nothing since string is not a number.
483 return filename + " " + i;
487 * Rename the conflicting trashed file with the supplied new name.
489 private void updateTrashedFile(String newName, FileResource trashedFile) {
490 JSONObject json = new JSONObject();
491 json.put("name", new JSONString(newName));
492 PostCommand cf = new PostCommand(trashedFile.getUri() + "?update=", json.toString(), 200) {
495 public void onComplete() {
500 public void onError(Throwable t) {
503 if (t instanceof RestException) {
504 int statusCode = ((RestException) t).getHttpStatusCode();
505 if (statusCode == 405)
506 app.displayError("You don't have the necessary permissions");
507 else if (statusCode == 404)
508 app.displayError("User in permissions does not exist");
509 else if (statusCode == 409)
510 app.displayError("A file with the same name already exists");
511 else if (statusCode == 413)
512 app.displayError("Your quota has been exceeded");
514 app.displayError("Unable to modify file:" + ((RestException) t).getHttpStatusText());
516 app.displayError("System error modifying file:" + t.getMessage());
520 DeferredCommand.addCommand(cf);
523 protected FileResource getFileForName(String name){
524 for (FileResource f : folder.getFiles())
525 if (!f.isDeleted() && f.getName().equals(name))
532 * Same as URL.encodeComponent, but also
533 * encode apostrophe since browsers aren't consistent about it
534 * (FF encodes, IE does not).
536 private String encodeComponent(String decodedURLComponent) {
537 String retv = URL.encodeComponent(decodedURLComponent);
538 retv = retv.replaceAll("'", "%27");
545 * @param newFiles the files to set
547 public void setFiles(List<FileResource> newFiles) {