correctly update filelist and tree nodes
[pithos] / src / gr / ebs / gss / client / FileUploadDialog.java
1 /*
2  * Copyright 2007, 2008, 2009 Electronic Business Systems Ltd.
3  *
4  * This file is part of GSS.
5  *
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.
10  *
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.
15  *
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/>.
18  */
19 package gr.ebs.gss.client;
20
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;
29
30 import java.util.ArrayList;
31 import java.util.List;
32
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;
59
60 /**
61  * The 'File upload' dialog box implementation.
62  */
63 public class FileUploadDialog extends DialogBox implements Updateable {
64
65         protected int prgBarInterval = 1500;
66
67         private ProgressBar progressBar;
68
69         protected RepeatingTimer repeater = new RepeatingTimer(this, prgBarInterval);
70
71         public static final boolean DONE = true;
72
73         /**
74          * The Form element that performs the file upload.
75          */
76         private final FormPanel form = new FormPanel();
77
78         private final FileUpload upload = new FileUpload();
79
80         protected final Label filenameLabel = new Label("");
81
82         protected List<FileResource> files;
83
84         protected boolean cancelEvent = false;
85
86         protected String fileNameToUse;
87
88         protected FolderResource folder;
89
90         /**
91          * The widget's constructor.
92          */
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);
101
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");
109                 panel.add(info);
110                 final Hidden date = new Hidden("Date", "");
111                 panel.add(date);
112                 final Hidden auth = new Hidden("Authorization", "");
113                 panel.add(auth);
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);
134
135                 panel.add(generalTable);
136
137                 // Create a panel to hold the buttons.
138                 HorizontalPanel buttons = new HorizontalPanel();
139
140                 // Create the 'upload' button, along with a listener that submits the
141                 // form.
142                 final Button submit = new Button("Upload", new ClickHandler() {
143                         @Override
144                         public void onClick(ClickEvent event) {
145                                 prepareAndSubmit();
146                         }
147                 });
148                 buttons.add(submit);
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() {
153                         @Override
154                         public void onClick(ClickEvent event) {
155                                 repeater.finish();
156                                 hide();
157                         }
158                 });
159                 buttons.add(cancel);
160                 buttons.setCellHorizontalAlignment(cancel, HasHorizontalAlignment.ALIGN_CENTER);
161                 buttons.setSpacing(8);
162                 buttons.addStyleName("gss-DialogBox");
163
164                 // Add an event handler to the form.
165                 form.addSubmitHandler(new SubmitHandler() {
166
167                         @Override
168                         public void onSubmit(SubmitEvent event) {
169                                 GSS app = GSS.get();
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!");
174                                         event.cancel();
175                                         hide();
176                                 } else {
177
178                                         canContinue();
179                                         GWT.log("Cancel:" + cancelEvent, null);
180                                         if (cancelEvent) {
181                                                 cancelEvent = false;
182                                                 app.displayError("The specified file name already exists in this folder");
183                                                 event.cancel();
184                                                 hide();
185                                         } else {
186
187                                                 fileNameToUse = getFilename(upload.getFilename());
188                                                 String apath;
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("/"))
194                                                                 apath = apath + "/";
195                                                         apath = apath + encodeComponent(fileNameToUse);
196                                                 } else
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);
209                                                 repeater.start();
210                                                 progressBar.setVisible(true);
211                                         }
212                                 }
213
214                         }
215                 });
216                 form.addSubmitCompleteHandler(new SubmitCompleteHandler() {
217
218                         @Override
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();
225
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);
231                                 }
232                                 progressBar.setProgress(100);
233                                 cancelUpload();
234                                 GSS.get().getTreeView().updateNode(GSS.get().getTreeView().getSelection());
235                                 GSS.get().getStatusPanel().updateStats();
236
237                         }
238                 });
239
240
241                 panel.add(buttons);
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");
249                 setWidget(form);
250         }
251
252         @Override
253         protected void onPreviewNativeEvent(NativePreviewEvent preview) {
254                 super.onPreviewNativeEvent(preview);
255
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:
262                                         prepareAndSubmit();
263                                         break;
264                                 case KeyCodes.KEY_ESCAPE:
265                                         cancelUpload();
266                                         break;
267                         }
268         }
269
270
271
272         /**
273          * Cancels the file upload.
274          */
275         private void cancelUpload() {
276                 repeater.finish();
277                 hide();
278         }
279
280         /**
281          * Make any last minute checks and start the upload.
282          */
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))
290                                         same = fres;
291                         if (same == null)
292                                 form.submit();
293                         else {
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") {
299
300                                         @Override
301                                         public void cancel() {
302                                                 FileUploadDialog.this.hide();
303                                         }
304
305                                         @Override
306                                         public void confirm() {
307                                                 updateTrashedFile(getBackupFilename(fname), sameFile);
308                                         }
309
310                                 };
311                                 confirm.center();
312                         }
313                 }
314                 else {
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") {
318
319                                 @Override
320                                 public void cancel() {
321                                         FileUploadDialog.this.hide();
322                                 }
323
324                                 @Override
325                                 public void confirm() {
326                                         form.submit();
327                                 }
328
329                         };
330                         confirm.center();
331                 }
332         }
333
334         /**
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.
339          *
340          * @param name the potentially full path name of a file
341          * @return the file name without extra path information
342          */
343         protected String getFilename(String name) {
344                 int pathSepIndex = name.lastIndexOf("\\");
345                 if (pathSepIndex == -1) {
346                         pathSepIndex = name.lastIndexOf("/");
347                         if (pathSepIndex == -1)
348                                 return name;
349                 }
350                 return name.substring(pathSepIndex + 1);
351         }
352
353         /**
354          * Check whether the file name exists in selected folder.
355          *
356          * @return
357          */
358         private boolean canContinue() {
359                 if (files == null)
360                         return false;
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)) {
368                                         cancelEvent = true;
369                                         return true;
370                                 }
371                         }
372                 }
373                 return true;
374         }
375
376         class RepeatingTimer extends Timer {
377
378                 private Updateable updateable;
379
380                 private int interval = 1500;
381
382                 private boolean running = true;
383
384                 RepeatingTimer(Updateable _updateable, int _interval) {
385                         updateable = _updateable;
386                         interval = _interval;
387                 }
388
389                 @Override
390                 public void run() {
391                         updateable.update();
392                 }
393
394                 public void start() {
395                         running = true;
396
397                         scheduleRepeating(interval);
398                 }
399
400                 public void finish() {
401                         running = false;
402                         cancel();
403                 }
404
405                 public int getInterval() {
406                         return interval;
407                 }
408
409                 public void setInterval(int anInterval) {
410                         if (interval != anInterval) {
411                                 interval = anInterval;
412                                 if (running) {
413                                         finish();
414                                         start();
415                                 }
416                         }
417                 }
418         }
419
420         @Override
421         public void update() {
422                 String apath = folder.getUri();
423                 if (!apath.endsWith("/"))
424                         apath = apath + "/";
425                 apath = apath + encodeComponent(fileNameToUse) + "?progress=" + encodeComponent(fileNameToUse);
426                 GetCommand eg = new GetCommand<UploadStatusResource>(UploadStatusResource.class, apath, false, null) {
427
428                         @Override
429                         public void onComplete() {
430                                 UploadStatusResource res = getResult();
431                                 progressBar.setProgress(res.percent());
432                         }
433
434                         @Override
435                         public void onError(Throwable t) {
436                                 GWT.log("", t);
437                         }
438
439                 };
440                 DeferredCommand.addCommand(eg);
441         }
442
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);
448                 int i = 1;
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(" ")) {
453                                         int test = -1;
454                                         try {
455                                                 test = Integer.valueOf(toCheck.replace(" ", ""));
456                                         } catch (NumberFormatException e) {
457                                                 // Do nothing since string is not a number.
458                                         }
459                                         if (test >= i)
460                                                 i = test + 1;
461                                 }
462                         }
463
464                 return filename + " " + i;
465         }
466
467         /**
468          * Rename the conflicting trashed file with the supplied new name.
469          */
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) {
474
475                         @Override
476                         public void onComplete() {
477                                 form.submit();
478                         }
479
480                         @Override
481                         public void onError(Throwable t) {
482                                 GSS app = GSS.get();
483                                 GWT.log("", 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");
494                                         else
495                                                 app.displayError("Unable to modify file:" + ((RestException) t).getHttpStatusText());
496                                 } else
497                                         app.displayError("System error modifying file:" + t.getMessage());
498                         }
499
500                 };
501                 DeferredCommand.addCommand(cf);
502         }
503
504         protected FileResource getFileForName(String name){
505                 for (FileResource f : folder.getFiles())
506                         if (!f.isDeleted() && f.getName().equals(name))
507                                 return f;
508                 return null;
509         }
510
511
512         /**
513          * Same as URL.encodeComponent, but also
514          * encode apostrophe since browsers aren't consistent about it
515          * (FF encodes, IE does not).
516          */
517         private String encodeComponent(String decodedURLComponent) {
518                 String retv = URL.encodeComponent(decodedURLComponent);
519                 retv = retv.replaceAll("'", "%27");
520                 return retv;
521         }
522
523         /**
524          * Modify the files.
525          *
526          * @param newFiles the files to set
527          */
528         public void setFiles(List<FileResource> newFiles) {
529                 files = newFiles;
530         }
531 }