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 upload.getElement().setId("fileUploadDiallog.uploadPanel");
125 Grid generalTable = new Grid(2, 2);
126 generalTable.setText(0, 0, "Folder");
127 generalTable.setText(1, 0, "File");
128 generalTable.setText(0, 1, folder.getName());
129 generalTable.setWidget(1, 1, fileUloadPanel);
130 generalTable.getCellFormatter().setStyleName(0, 0, "props-labels");
131 generalTable.getCellFormatter().setStyleName(1, 0, "props-labels");
132 generalTable.getCellFormatter().setStyleName(0, 1, "props-values");
133 generalTable.getCellFormatter().setStyleName(1, 1, "props-values");
134 generalTable.setCellSpacing(4);
136 panel.add(generalTable);
138 // Create a panel to hold the buttons.
139 HorizontalPanel buttons = new HorizontalPanel();
141 // Create the 'upload' button, along with a listener that submits the
143 final Button submit = new Button("Upload", new ClickHandler() {
145 public void onClick(ClickEvent event) {
149 submit.getElement().setId("fileUploadDialog.button.upload");
151 buttons.setCellHorizontalAlignment(submit, HasHorizontalAlignment.ALIGN_CENTER);
152 // Create the 'Cancel' button, along with a listener that hides the
153 // dialog when the button is clicked.
154 final Button cancel = new Button("Cancel", new ClickHandler() {
156 public void onClick(ClickEvent event) {
161 cancel.getElement().setId("fileUploadDialog.button.cancel");
163 buttons.setCellHorizontalAlignment(cancel, HasHorizontalAlignment.ALIGN_CENTER);
164 buttons.setSpacing(8);
165 buttons.addStyleName("gss-DialogBox");
167 // Add an event handler to the form.
168 form.addSubmitHandler(new SubmitHandler() {
171 public void onSubmit(SubmitEvent event) {
173 // This event is fired just before the form is submitted. We can
174 // take this opportunity to perform validation.
175 if (upload.getFilename().length() == 0) {
176 app.displayError("You must select a file!");
182 GWT.log("Cancel:" + cancelEvent, null);
185 app.displayError("The specified file name already exists in this folder");
190 fileNameToUse = getFilename(upload.getFilename());
192 FileResource selectedFile = getFileForName(fileNameToUse);
193 if (selectedFile == null ) {
194 //we are going to create a file
195 apath = folder.getUri();
196 if (!apath.endsWith("/"))
198 apath = apath + encodeComponent(fileNameToUse);
200 apath = selectedFile.getUri();
201 form.setAction(apath);
202 String dateString = RestCommand.getDate();
203 String resource = apath.substring(app.getApiPath().length() - 1, apath.length());
204 String sig = RestCommand.calculateSig("POST", dateString, resource, RestCommand.base64decode(app.getToken()));
205 date.setValue(dateString);
206 auth.setValue(app.getCurrentUserResource().getUsername() + " " + sig);
207 GWT.log("FolderPATH:" + folder.getUri(), null);
208 submit.setEnabled(false);
209 upload.setVisible(false);
210 filenameLabel.setText(fileNameToUse);
211 filenameLabel.setVisible(true);
213 progressBar.setVisible(true);
219 form.addSubmitCompleteHandler(new SubmitCompleteHandler() {
222 public void onSubmitComplete(SubmitCompleteEvent event) {
223 // When the form submission is successfully completed, this
224 // event is fired. Assuming the service returned a response
225 // of type text/html, we can get the result text here (see
226 // the FormPanel documentation for further explanation).
227 String results = event.getResults();
229 // Unfortunately the results are never empty, even in
230 // the absense of errors, so we have to check for '<pre></pre>'.
231 if (!results.equalsIgnoreCase("<pre></pre>")) {
232 GWT.log(results, null);
233 GSS.get().displayError(results);
235 progressBar.setProgress(100);
237 GSS.get().getTreeView().updateNode(GSS.get().getTreeView().getSelection());
238 GSS.get().getStatusPanel().updateStats();
245 progressBar = new ProgressBar(50, ProgressBar.SHOW_TIME_REMAINING);
246 panel.add(progressBar);
247 progressBar.setVisible(false);
248 panel.setCellHorizontalAlignment(buttons, HasHorizontalAlignment.ALIGN_CENTER);
249 panel.setCellHorizontalAlignment(progressBar, HasHorizontalAlignment.ALIGN_CENTER);
250 panel.addStyleName("gss-DialogBox");
251 addStyleName("gss-DialogBox");
256 protected void onPreviewNativeEvent(NativePreviewEvent preview) {
257 super.onPreviewNativeEvent(preview);
259 NativeEvent evt = preview.getNativeEvent();
260 if (evt.getType().equals("keydown"))
261 // Use the popup's key preview hooks to close the dialog when either
262 // enter or escape is pressed.
263 switch (evt.getKeyCode()) {
264 case KeyCodes.KEY_ENTER:
267 case KeyCodes.KEY_ESCAPE:
276 * Cancels the file upload.
278 private void cancelUpload() {
284 * Make any last minute checks and start the upload.
286 public void prepareAndSubmit() {
287 final String fname = getFilename(upload.getFilename());
288 if (getFileForName(fname) == null) {
289 //we are going to create a file, so we check to see if there is a trashed file with the same name
290 FileResource same = null;
291 for (FileResource fres : folder.getFiles())
292 if (fres.isDeleted() && fres.getName().equals(fname))
297 final FileResource sameFile = same;
298 GWT.log("Same deleted file", null);
299 ConfirmationDialog confirm = new ConfirmationDialog("A file with " +
300 "the same name exists in the trash. If you continue,<br/>the trashed " +
301 "file '" + fname + "' will be renamed automatically for you.", "Continue") {
304 public void cancel() {
305 FileUploadDialog.this.hide();
309 public void confirm() {
310 updateTrashedFile(getBackupFilename(fname), sameFile);
318 // We are going to update an existing file, so show a confirmation dialog.
319 ConfirmationDialog confirm = new ConfirmationDialog("Are you sure " +
320 "you want to update " + fname + "?", "Update") {
323 public void cancel() {
324 FileUploadDialog.this.hide();
328 public void confirm() {
338 * Returns the file name from a potential full path argument. Apparently IE
339 * insists on sending the full path name of a file when uploading, forcing
340 * us to trim the extra path info. Since this is only observed on Windows we
341 * get to check for a single path separator value.
343 * @param name the potentially full path name of a file
344 * @return the file name without extra path information
346 protected String getFilename(String name) {
347 int pathSepIndex = name.lastIndexOf("\\");
348 if (pathSepIndex == -1) {
349 pathSepIndex = name.lastIndexOf("/");
350 if (pathSepIndex == -1)
353 return name.substring(pathSepIndex + 1);
357 * Check whether the file name exists in selected folder.
361 private boolean canContinue() {
364 String fileName = getFilename(upload.getFilename());
365 if (getFileForName(fileName) == null) {
366 // For file creation, check to see if the file already exists.
367 GWT.log("filename to upload:" + fileName, null);
368 for (FileResource dto : files) {
369 GWT.log("Check:" + dto.getName() + "/" + fileName, null);
370 if (!dto.isDeleted() && dto.getName().equals(fileName)) {
379 class RepeatingTimer extends Timer {
381 private Updateable updateable;
383 private int interval = 1500;
385 private boolean running = true;
387 RepeatingTimer(Updateable _updateable, int _interval) {
388 updateable = _updateable;
389 interval = _interval;
397 public void start() {
400 scheduleRepeating(interval);
403 public void finish() {
408 public int getInterval() {
412 public void setInterval(int anInterval) {
413 if (interval != anInterval) {
414 interval = anInterval;
424 public void update() {
425 String apath = folder.getUri();
426 if (!apath.endsWith("/"))
428 apath = apath + encodeComponent(fileNameToUse) + "?progress=" + encodeComponent(fileNameToUse);
429 GetCommand eg = new GetCommand<UploadStatusResource>(UploadStatusResource.class, apath, false, null) {
432 public void onComplete() {
433 UploadStatusResource res = getResult();
434 progressBar.setProgress(res.percent());
438 public void onError(Throwable t) {
443 DeferredCommand.addCommand(eg);
446 protected String getBackupFilename(String filename) {
447 List<FileResource> filesInSameFolder = new ArrayList<FileResource>();
448 for (FileResource deleted : folder.getFiles())
449 if (deleted.isDeleted())
450 filesInSameFolder.add(deleted);
452 for (FileResource same : filesInSameFolder)
453 if (same.getName().startsWith(filename)) {
454 String toCheck = same.getName().substring(filename.length(), same.getName().length());
455 if (toCheck.startsWith(" ")) {
458 test = Integer.valueOf(toCheck.replace(" ", ""));
459 } catch (NumberFormatException e) {
460 // Do nothing since string is not a number.
467 return filename + " " + i;
471 * Rename the conflicting trashed file with the supplied new name.
473 private void updateTrashedFile(String newName, FileResource trashedFile) {
474 JSONObject json = new JSONObject();
475 json.put("name", new JSONString(newName));
476 PostCommand cf = new PostCommand(trashedFile.getUri() + "?update=", json.toString(), 200) {
479 public void onComplete() {
484 public void onError(Throwable t) {
487 if (t instanceof RestException) {
488 int statusCode = ((RestException) t).getHttpStatusCode();
489 if (statusCode == 405)
490 app.displayError("You don't have the necessary permissions");
491 else if (statusCode == 404)
492 app.displayError("User in permissions does not exist");
493 else if (statusCode == 409)
494 app.displayError("A file with the same name already exists");
495 else if (statusCode == 413)
496 app.displayError("Your quota has been exceeded");
498 app.displayError("Unable to modify file:" + ((RestException) t).getHttpStatusText());
500 app.displayError("System error modifying file:" + t.getMessage());
504 DeferredCommand.addCommand(cf);
507 protected FileResource getFileForName(String name){
508 for (FileResource f : folder.getFiles())
509 if (!f.isDeleted() && f.getName().equals(name))
516 * Same as URL.encodeComponent, but also
517 * encode apostrophe since browsers aren't consistent about it
518 * (FF encodes, IE does not).
520 private String encodeComponent(String decodedURLComponent) {
521 String retv = URL.encodeComponent(decodedURLComponent);
522 retv = retv.replaceAll("'", "%27");
529 * @param newFiles the files to set
531 public void setFiles(List<FileResource> newFiles) {