2 * Copyright (c) 2011 Greek Research and Technology Network
4 package gr.grnet.pithos.web.client;
6 import gr.grnet.pithos.web.client.rest.GetCommand;
7 import gr.grnet.pithos.web.client.rest.PostCommand;
8 import gr.grnet.pithos.web.client.rest.RestCommand;
9 import gr.grnet.pithos.web.client.rest.RestException;
10 import gr.grnet.pithos.web.client.rest.resource.FileResource;
11 import gr.grnet.pithos.web.client.rest.resource.FolderResource;
12 import gr.grnet.pithos.web.client.rest.resource.RestResourceWrapper;
13 import gr.grnet.pithos.web.client.rest.resource.UploadStatusResource;
15 import java.util.ArrayList;
16 import java.util.List;
18 import com.google.gwt.core.client.GWT;
19 import com.google.gwt.dom.client.NativeEvent;
20 import com.google.gwt.event.dom.client.ClickEvent;
21 import com.google.gwt.event.dom.client.ClickHandler;
22 import com.google.gwt.event.dom.client.KeyCodes;
23 import com.google.gwt.http.client.URL;
24 import com.google.gwt.json.client.JSONObject;
25 import com.google.gwt.json.client.JSONString;
26 import com.google.gwt.user.client.DeferredCommand;
27 import com.google.gwt.user.client.Event.NativePreviewEvent;
28 import com.google.gwt.user.client.Timer;
29 import com.google.gwt.user.client.ui.Button;
30 import com.google.gwt.user.client.ui.DialogBox;
31 import com.google.gwt.user.client.ui.FileUpload;
32 import com.google.gwt.user.client.ui.FormPanel;
33 import com.google.gwt.user.client.ui.FormPanel.SubmitCompleteEvent;
34 import com.google.gwt.user.client.ui.FormPanel.SubmitCompleteHandler;
35 import com.google.gwt.user.client.ui.FormPanel.SubmitEvent;
36 import com.google.gwt.user.client.ui.FormPanel.SubmitHandler;
37 import com.google.gwt.user.client.ui.Grid;
38 import com.google.gwt.user.client.ui.HTML;
39 import com.google.gwt.user.client.ui.HasHorizontalAlignment;
40 import com.google.gwt.user.client.ui.Hidden;
41 import com.google.gwt.user.client.ui.HorizontalPanel;
42 import com.google.gwt.user.client.ui.Label;
43 import com.google.gwt.user.client.ui.VerticalPanel;
46 * The 'File upload' dialog box implementation.
48 public class FileUploadDialog extends DialogBox implements Updateable {
50 protected int prgBarInterval = 1500;
52 private ProgressBar progressBar;
54 protected RepeatingTimer repeater = new RepeatingTimer(this, prgBarInterval);
56 public static final boolean DONE = true;
59 * The Form element that performs the file upload.
61 private final FormPanel form = new FormPanel();
63 private final FileUpload upload = new FileUpload();
65 protected final Label filenameLabel = new Label("");
67 protected List<FileResource> files;
69 protected boolean cancelEvent = false;
71 protected String fileNameToUse;
73 protected FolderResource folder;
76 * The widget's constructor.
78 public FileUploadDialog() {
79 // Set the dialog's caption.
80 setText("File upload");
81 setAnimationEnabled(true);
82 // Since we're going to add a FileUpload widget, we'll need to set the
83 // form to use the POST method, and multipart MIME encoding.
84 form.setEncoding(FormPanel.ENCODING_MULTIPART);
85 form.setMethod(FormPanel.METHOD_POST);
87 // Create a panel to hold all of the form widgets.
88 VerticalPanel panel = new VerticalPanel();
89 form.setWidget(panel);
90 final HTML info = new HTML("You may select a file to upload. Install" +
91 " <a href='http://gears.google.com/' target='_blank'>Google " +
92 "Gears</a><br> for uploading multiple files simultaneously.");
93 info.addStyleName("pithos-uploadNote");
95 final Hidden date = new Hidden("Date", "");
97 final Hidden auth = new Hidden("Authorization", "");
99 // Add an informative label with the folder name.
100 Object selection = GSS.get().getTreeView().getSelection();
101 folder = ((RestResourceWrapper) selection).getResource();
102 upload.setName("file");
103 filenameLabel.setText("");
104 filenameLabel.setVisible(false);
105 filenameLabel.setStyleName("props-labels");
106 HorizontalPanel fileUloadPanel = new HorizontalPanel();
107 fileUloadPanel.add(filenameLabel);
108 fileUloadPanel.add(upload);
109 upload.getElement().setId("fileUploadDiallog.uploadPanel");
110 Grid generalTable = new Grid(2, 2);
111 generalTable.setText(0, 0, "Folder");
112 generalTable.setText(1, 0, "File");
113 generalTable.setText(0, 1, folder.getName());
114 generalTable.setWidget(1, 1, fileUloadPanel);
115 generalTable.getCellFormatter().setStyleName(0, 0, "props-labels");
116 generalTable.getCellFormatter().setStyleName(1, 0, "props-labels");
117 generalTable.getCellFormatter().setStyleName(0, 1, "props-values");
118 generalTable.getCellFormatter().setStyleName(1, 1, "props-values");
119 generalTable.setCellSpacing(4);
121 panel.add(generalTable);
123 // Create a panel to hold the buttons.
124 HorizontalPanel buttons = new HorizontalPanel();
126 // Create the 'upload' button, along with a listener that submits the
128 final Button submit = new Button("Upload", new ClickHandler() {
130 public void onClick(ClickEvent event) {
134 submit.getElement().setId("fileUploadDialog.button.upload");
136 buttons.setCellHorizontalAlignment(submit, HasHorizontalAlignment.ALIGN_CENTER);
137 // Create the 'Cancel' button, along with a listener that hides the
138 // dialog when the button is clicked.
139 final Button cancel = new Button("Cancel", new ClickHandler() {
141 public void onClick(ClickEvent event) {
146 cancel.getElement().setId("fileUploadDialog.button.cancel");
148 buttons.setCellHorizontalAlignment(cancel, HasHorizontalAlignment.ALIGN_CENTER);
149 buttons.setSpacing(8);
150 buttons.addStyleName("pithos-DialogBox");
152 // Add an event handler to the form.
153 form.addSubmitHandler(new SubmitHandler() {
156 public void onSubmit(SubmitEvent event) {
158 // This event is fired just before the form is submitted. We can
159 // take this opportunity to perform validation.
160 if (upload.getFilename().length() == 0) {
161 app.displayError("You must select a file!");
167 GWT.log("Cancel:" + cancelEvent, null);
170 app.displayError("The specified file name already exists in this folder");
175 fileNameToUse = getFilename(upload.getFilename());
177 FileResource selectedFile = getFileForName(fileNameToUse);
178 if (selectedFile == null ) {
179 //we are going to create a file
180 apath = folder.getUri();
181 if (!apath.endsWith("/"))
183 apath = apath + encodeComponent(fileNameToUse);
185 apath = selectedFile.getUri();
186 form.setAction(apath);
187 String dateString = RestCommand.getDate();
188 String resource = apath.substring(app.getApiPath().length() - 1, apath.length());
189 String sig = RestCommand.calculateSig("POST", dateString, resource, RestCommand.base64decode(app.getToken()));
190 date.setValue(dateString);
191 auth.setValue(app.getCurrentUserResource().getUsername() + " " + sig);
192 GWT.log("FolderPATH:" + folder.getUri(), null);
193 submit.setEnabled(false);
194 upload.setVisible(false);
195 filenameLabel.setText(fileNameToUse);
196 filenameLabel.setVisible(true);
198 progressBar.setVisible(true);
204 form.addSubmitCompleteHandler(new SubmitCompleteHandler() {
207 public void onSubmitComplete(SubmitCompleteEvent event) {
208 // When the form submission is successfully completed, this
209 // event is fired. Assuming the service returned a response
210 // of type text/html, we can get the result text here (see
211 // the FormPanel documentation for further explanation).
212 String results = event.getResults();
214 // Unfortunately the results are never empty, even in
215 // the absense of errors, so we have to check for '<pre></pre>'.
216 if (!results.equalsIgnoreCase("<pre></pre>")) {
217 GWT.log(results, null);
218 GSS.get().displayError(results);
220 progressBar.setProgress(100);
222 GSS.get().getTreeView().updateNode(GSS.get().getTreeView().getSelection());
223 GSS.get().getStatusPanel().updateStats();
230 progressBar = new ProgressBar(50, ProgressBar.SHOW_TIME_REMAINING);
231 panel.add(progressBar);
232 progressBar.setVisible(false);
233 panel.setCellHorizontalAlignment(buttons, HasHorizontalAlignment.ALIGN_CENTER);
234 panel.setCellHorizontalAlignment(progressBar, HasHorizontalAlignment.ALIGN_CENTER);
235 panel.addStyleName("pithos-DialogBox");
236 addStyleName("pithos-DialogBox");
241 protected void onPreviewNativeEvent(NativePreviewEvent preview) {
242 super.onPreviewNativeEvent(preview);
244 NativeEvent evt = preview.getNativeEvent();
245 if (evt.getType().equals("keydown"))
246 // Use the popup's key preview hooks to close the dialog when either
247 // enter or escape is pressed.
248 switch (evt.getKeyCode()) {
249 case KeyCodes.KEY_ENTER:
252 case KeyCodes.KEY_ESCAPE:
261 * Cancels the file upload.
263 private void cancelUpload() {
269 * Make any last minute checks and start the upload.
271 public void prepareAndSubmit() {
272 final String fname = getFilename(upload.getFilename());
273 if (getFileForName(fname) == null) {
274 //we are going to create a file, so we check to see if there is a trashed file with the same name
275 FileResource same = null;
276 for (FileResource fres : folder.getFiles())
277 if (fres.isDeleted() && fres.getName().equals(fname))
282 final FileResource sameFile = same;
283 GWT.log("Same deleted file", null);
284 ConfirmationDialog confirm = new ConfirmationDialog("A file with " +
285 "the same name exists in the trash. If you continue,<br/>the trashed " +
286 "file '" + fname + "' will be renamed automatically for you.", "Continue") {
289 public void cancel() {
290 FileUploadDialog.this.hide();
294 public void confirm() {
295 updateTrashedFile(getBackupFilename(fname), sameFile);
303 // We are going to update an existing file, so show a confirmation dialog.
304 ConfirmationDialog confirm = new ConfirmationDialog("Are you sure " +
305 "you want to update " + fname + "?", "Update") {
308 public void cancel() {
309 FileUploadDialog.this.hide();
313 public void confirm() {
323 * Returns the file name from a potential full path argument. Apparently IE
324 * insists on sending the full path name of a file when uploading, forcing
325 * us to trim the extra path info. Since this is only observed on Windows we
326 * get to check for a single path separator value.
328 * @param name the potentially full path name of a file
329 * @return the file name without extra path information
331 protected String getFilename(String name) {
332 int pathSepIndex = name.lastIndexOf("\\");
333 if (pathSepIndex == -1) {
334 pathSepIndex = name.lastIndexOf("/");
335 if (pathSepIndex == -1)
338 return name.substring(pathSepIndex + 1);
342 * Check whether the file name exists in selected folder.
346 private boolean canContinue() {
349 String fileName = getFilename(upload.getFilename());
350 if (getFileForName(fileName) == null) {
351 // For file creation, check to see if the file already exists.
352 GWT.log("filename to upload:" + fileName, null);
353 for (FileResource dto : files) {
354 GWT.log("Check:" + dto.getName() + "/" + fileName, null);
355 if (!dto.isDeleted() && dto.getName().equals(fileName)) {
364 class RepeatingTimer extends Timer {
366 private Updateable updateable;
368 private int interval = 1500;
370 private boolean running = true;
372 RepeatingTimer(Updateable _updateable, int _interval) {
373 updateable = _updateable;
374 interval = _interval;
382 public void start() {
385 scheduleRepeating(interval);
388 public void finish() {
393 public int getInterval() {
397 public void setInterval(int anInterval) {
398 if (interval != anInterval) {
399 interval = anInterval;
409 public void update() {
410 String apath = folder.getUri();
411 if (!apath.endsWith("/"))
413 apath = apath + encodeComponent(fileNameToUse) + "?progress=" + encodeComponent(fileNameToUse);
414 GetCommand eg = new GetCommand<UploadStatusResource>(UploadStatusResource.class, apath, false, null) {
417 public void onComplete() {
418 UploadStatusResource res = getResult();
419 progressBar.setProgress(res.percent());
423 public void onError(Throwable t) {
428 DeferredCommand.addCommand(eg);
431 protected String getBackupFilename(String filename) {
432 List<FileResource> filesInSameFolder = new ArrayList<FileResource>();
433 for (FileResource deleted : folder.getFiles())
434 if (deleted.isDeleted())
435 filesInSameFolder.add(deleted);
437 for (FileResource same : filesInSameFolder)
438 if (same.getName().startsWith(filename)) {
439 String toCheck = same.getName().substring(filename.length(), same.getName().length());
440 if (toCheck.startsWith(" ")) {
443 test = Integer.valueOf(toCheck.replace(" ", ""));
444 } catch (NumberFormatException e) {
445 // Do nothing since string is not a number.
452 return filename + " " + i;
456 * Rename the conflicting trashed file with the supplied new name.
458 private void updateTrashedFile(String newName, FileResource trashedFile) {
459 JSONObject json = new JSONObject();
460 json.put("name", new JSONString(newName));
461 PostCommand cf = new PostCommand(trashedFile.getUri() + "?update=", json.toString(), 200) {
464 public void onComplete() {
469 public void onError(Throwable t) {
472 if (t instanceof RestException) {
473 int statusCode = ((RestException) t).getHttpStatusCode();
474 if (statusCode == 405)
475 app.displayError("You don't have the necessary permissions");
476 else if (statusCode == 404)
477 app.displayError("User in permissions does not exist");
478 else if (statusCode == 409)
479 app.displayError("A file with the same name already exists");
480 else if (statusCode == 413)
481 app.displayError("Your quota has been exceeded");
483 app.displayError("Unable to modify file:" + ((RestException) t).getHttpStatusText());
485 app.displayError("System error modifying file:" + t.getMessage());
489 DeferredCommand.addCommand(cf);
492 protected FileResource getFileForName(String name){
493 for (FileResource f : folder.getFiles())
494 if (!f.isDeleted() && f.getName().equals(name))
501 * Same as URL.encodeComponent, but also
502 * encode apostrophe since browsers aren't consistent about it
503 * (FF encodes, IE does not).
505 private String encodeComponent(String decodedURLComponent) {
506 String retv = URL.encodeComponent(decodedURLComponent);
507 retv = retv.replaceAll("'", "%27");
514 * @param newFiles the files to set
516 public void setFiles(List<FileResource> newFiles) {