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.foldertree.Folder;
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;
42 import java.util.ArrayList;
43 import java.util.Arrays;
44 import java.util.HashMap;
45 import java.util.List;
48 import com.google.gwt.core.client.GWT;
49 import com.google.gwt.event.dom.client.ClickEvent;
50 import com.google.gwt.event.dom.client.ClickHandler;
51 import com.google.gwt.gears.client.Factory;
52 import com.google.gwt.gears.client.desktop.Desktop;
53 import com.google.gwt.gears.client.desktop.File;
54 import com.google.gwt.gears.client.desktop.OpenFilesHandler;
55 import com.google.gwt.gears.client.httprequest.HttpRequest;
56 import com.google.gwt.gears.client.httprequest.ProgressEvent;
57 import com.google.gwt.gears.client.httprequest.ProgressHandler;
58 import com.google.gwt.gears.client.httprequest.RequestCallback;
59 import com.google.gwt.http.client.URL;
60 import com.google.gwt.json.client.JSONObject;
61 import com.google.gwt.json.client.JSONString;
62 import com.google.gwt.user.client.DeferredCommand;
63 import com.google.gwt.user.client.ui.Button;
64 import com.google.gwt.user.client.ui.FlexTable;
65 import com.google.gwt.user.client.ui.HTML;
66 import com.google.gwt.user.client.ui.HasHorizontalAlignment;
67 import com.google.gwt.user.client.ui.HorizontalPanel;
68 import com.google.gwt.user.client.ui.VerticalPanel;
71 * The 'File upload' dialog box implementation with Google Gears support.
73 public class FileUploadGearsDialog extends FileUploadDialog {
75 protected final Factory factory = Factory.getInstance();
78 * The array of files to upload.
80 private File[] fileObjects;
83 * A list of files to upload, created from files array. Used to signal
84 * finished state when empty.
86 protected List<File> selectedFiles = new ArrayList<File>();
89 * The list of progress bars for individual files.
91 protected List<ProgressBar> progressBars = new ArrayList<ProgressBar>();
93 private Button browse;
95 private Button submit;
97 private FlexTable generalTable;
99 private Map<String, gr.grnet.pithos.web.client.foldertree.File> toRename;
101 protected List<HttpRequest> requests = new ArrayList<HttpRequest>();
103 private boolean canContinue = true;
105 protected FileUploadGearsDialog() {
109 * The widget's constructor.
111 public FileUploadGearsDialog(Pithos _app, Folder _folder) {
112 this.folder = _folder;
114 // Set the dialog's caption.
115 setText("File upload");
116 setAnimationEnabled(true);
117 // Create a panel to hold all of the dialog widgets.
118 VerticalPanel panel = new VerticalPanel();
119 final HTML info = new HTML("You may select one or more files to upload.");
120 info.addStyleName("pithos-uploadNote");
122 // Add an informative label with the folder name.
123 Object selection = app.getTreeView().getSelection();
125 browse = new Button("Browse...");
127 HorizontalPanel fileUploadPanel = new HorizontalPanel();
128 fileUploadPanel.add(browse);
130 generalTable = new FlexTable();
131 generalTable.setText(0, 0, "Folder");
132 generalTable.setText(1, 0, "File");
133 generalTable.setText(0, 1, folder.getName());
134 generalTable.setWidget(1, 1, fileUploadPanel);
135 generalTable.getCellFormatter().setStyleName(0, 0, "props-labels");
136 generalTable.getCellFormatter().setStyleName(1, 0, "props-labels");
137 generalTable.getCellFormatter().setStyleName(0, 1, "props-values");
138 generalTable.getCellFormatter().setStyleName(1, 1, "props-values");
139 generalTable.setCellSpacing(4);
141 panel.add(generalTable);
143 // Create a panel to hold the buttons.
144 HorizontalPanel buttons = new HorizontalPanel();
146 submit = new Button("Upload");
147 submit.addClickHandler(new ClickHandler() {
149 public void onClick(ClickEvent event) {
153 submit.setEnabled(false);
155 buttons.setCellHorizontalAlignment(submit, HasHorizontalAlignment.ALIGN_CENTER);
156 // Create the 'Cancel' button, along with a listener that hides the
157 // dialog when the button is clicked.
158 Button cancel = new Button("Cancel", new ClickHandler() {
160 public void onClick(ClickEvent event) {
163 app.showFileList(true);
167 buttons.setCellHorizontalAlignment(cancel, HasHorizontalAlignment.ALIGN_CENTER);
168 buttons.setSpacing(8);
169 buttons.addStyleName("pithos-DialogBox");
171 browse.addClickHandler(new ClickHandler() {
173 public void onClick(ClickEvent event) {
174 Desktop desktop = factory.createDesktop();
175 desktop.openFiles(new OpenFilesHandler() {
178 public void onOpenFiles(OpenFilesEvent ofevent) {
179 fileObjects = ofevent.getFiles();
180 selectedFiles.addAll(Arrays.asList(fileObjects));
181 for (int i = 0; i< selectedFiles.size(); i++) {
182 generalTable.setText(i+1, 0, "File");
183 generalTable.setText(i+1, 1, selectedFiles.get(i).getName());
184 ProgressBar progress = new ProgressBar(20, 0);
185 generalTable.setWidget(i+1, 2, progress);
186 progressBars.add(progress);
187 generalTable.getCellFormatter().setStyleName(i+1, 0, "props-labels");
188 generalTable.getCellFormatter().setStyleName(i+1, 1, "props-values");
190 submit.setEnabled(true);
197 panel.setCellHorizontalAlignment(buttons, HasHorizontalAlignment.ALIGN_CENTER);
198 panel.addStyleName("pithos-DialogBox");
199 addStyleName("pithos-DialogBox");
204 * Cancels the file upload.
206 private void cancelUpload() {
207 for (HttpRequest request: requests)
213 public void prepareAndSubmit() {
214 if (selectedFiles.size() == 0) {
215 app.displayError("You must select a file!");
219 submit.setEnabled(false);
220 browse.setVisible(false);
221 List<String> toUpdate = new ArrayList<String>();
222 toRename = new HashMap<String, gr.grnet.pithos.web.client.foldertree.File>();
223 for (File file: selectedFiles) {
224 String fname = getFilename(file.getName());
225 if (getFileForName(fname) == null) {
226 // We are going to create a file, so we check to see if there is a
227 // trashed file with the same name.
228 gr.grnet.pithos.web.client.foldertree.File same = null;
229 for (gr.grnet.pithos.web.client.foldertree.File fres : folder.getFiles())
230 if (fres.isInTrash() && fres.getName().equals(fname))
232 // In that case add it to the list of files to rename.
234 toRename.put(getBackupFilename(fname), same);
236 // If we are updating a file add it to the list of files to update.
240 if (!toUpdate.isEmpty()) {
241 StringBuffer sb = new StringBuffer();
242 for (String name: toUpdate)
243 sb.append(name).append("<br/>");
244 // We are going to update existing files, so show a confirmation dialog.
245 ConfirmationDialog confirm = new ConfirmationDialog("Are you sure " +
246 "you want to update the following files?<br/><i>" + sb +
250 public void cancel() {
255 public void confirm() {
266 * Confirm the renames of synonymous files already in the trash.
268 private void confirmRename() {
269 if (!toRename.isEmpty()) {
270 StringBuffer sb = new StringBuffer();
271 for (gr.grnet.pithos.web.client.foldertree.File file: toRename.values())
272 sb.append(file.getName()).append("<br/>");
273 ConfirmationDialog confirm = new ConfirmationDialog("Files " +
274 "with the following names already exist in the trash. If" +
275 " you continue,<br/>the trashed files will be renamed " +
276 "automatically for you:<br/><i>" + sb + "</i>", "Continue") {
279 public void cancel() {
284 public void confirm() {
285 updateTrashedFiles();
295 * Rename the conflicting trashed files with the supplied new names.
297 private void updateTrashedFiles() {
298 for (final String name: toRename.keySet()) {
299 JSONObject json = new JSONObject();
300 json.put("name", new JSONString(name));
301 PostCommand cf = new PostCommand(app, toRename.get(name).getUri() + "?update=", json.toString(), 200) {
304 public void onComplete() {
305 toRename.remove(name);
310 public void onError(Throwable t) {
312 if (t instanceof RestException) {
313 int statusCode = ((RestException) t).getHttpStatusCode();
314 if (statusCode == 405)
315 app.displayError("You don't have the necessary permissions");
316 else if (statusCode == 404)
317 app.displayError("User in permissions does not exist");
318 else if (statusCode == 409)
319 app.displayError("A file with the same name already exists");
320 else if (statusCode == 413)
321 app.displayError("Your quota has been exceeded");
323 app.displayError("Unable to modify file:" + ((RestException) t).getHttpStatusText());
325 app.displayError("System error modifying file:" + t.getMessage());
329 DeferredCommand.addCommand(cf);
334 * Checks if the renaming step for already trashed files is complete and
335 * starts file uploads.
337 private void uploadFiles() {
338 if (!toRename.isEmpty()) return;
340 doSend(selectedFiles);
345 * Perform the HTTP request to upload the specified file.
347 protected void doSend(final List<File> filesRemaining) {
348 HttpRequest request = factory.createHttpRequest();
349 requests.add(request);
350 String method = "PUT";
353 final String filename = getFilename(filesRemaining.get(0).getName());
354 path = folder.getUri();
355 if (!path.endsWith("/"))
357 path = path + encode(filename);
359 String token = app.getToken();
360 String resource = path.substring(app.getApiPath().length()-1, path.length());
361 String date = RestCommand.getDate();
362 String sig = RestCommand.calculateSig(method, date, resource, RestCommand.base64decode(token));
363 request.open(method, path);
364 request.setRequestHeader("Authorization", app.getCurrentUserResource().getUsername() + " " + sig);
365 request.setRequestHeader("Accept", "application/json; charset=utf-8");
366 request.setCallback(new RequestCallback() {
368 public void onResponseReceived(HttpRequest req) {
369 int state = req.getReadyState();
370 if (state != 4) return;
371 switch(req.getStatus()) {
372 case 201: // Created falls through to updated.
374 filesRemaining.remove(0);
375 if(filesRemaining.isEmpty()){
379 doSend(filesRemaining);
382 SessionExpiredDialog dlg = new SessionExpiredDialog(app);
386 app.displayError("You don't have permission to " +
387 "upload file " + filename);
390 app.displayError("A folder with the name " + filename +
391 " already exists at this level");
394 app.displayError("There is not enough free space " +
395 "available for uploading " + filename);
398 app.displayError("Error uploading file " + filename +
399 ": " + req.getStatus());
403 request.getUpload().setProgressHandler(new ProgressHandler() {
405 public void onProgress(ProgressEvent event) {
406 double pcnt = (double) event.getLoaded() / event.getTotal();
407 progressBars.get(0).setProgress((int) Math.floor(pcnt * 100));
409 progressBars.remove(0);
412 request.send(filesRemaining.get(0).getBlob());
416 * Perform the final actions after the files are uploaded.
418 protected void finish() {
420 //app.showFileList(true);
421 app.getTreeView().updateNode(app.getTreeView().getSelection());//showFileList(true);
422 app.getStatusPanel().updateStats();
426 * Same as URL.encode, but also encode apostrophe since browsers aren't
427 * consistent about it (FF encodes, IE does not).
429 protected String encode(String decodedURL) {
430 String retv = decodedURL.replaceAll("@", "_"); // Replace bad character
431 retv = URL.encodeComponent(retv);
432 retv = retv.replaceAll("'", "%27");
436 protected String getBackupFilename(String filename) {
437 List<gr.grnet.pithos.web.client.foldertree.File> filesInSameFolder = new ArrayList<gr.grnet.pithos.web.client.foldertree.File>();
438 for (gr.grnet.pithos.web.client.foldertree.File deleted : folder.getFiles())
439 if (deleted.isInTrash())
440 filesInSameFolder.add(deleted);
442 for (gr.grnet.pithos.web.client.foldertree.File same : filesInSameFolder)
443 if (same.getName().startsWith(filename)) {
444 String toCheck = same.getName().substring(filename.length(), same.getName().length());
445 if (toCheck.startsWith(" ")) {
448 test = Integer.valueOf(toCheck.replace(" ", ""));
449 } catch (NumberFormatException e) {
450 // Do nothing since string is not a number.
457 return filename + " " + i;