Removed all static references to the Pithos class
[pithos] / web_client / src / gr / grnet / pithos / web / client / FileUploadGearsDialog.java
1 /*
2  * Copyright 2011 GRNET S.A. All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or
5  * without modification, are permitted provided that the following
6  * conditions are met:
7  *
8  *   1. Redistributions of source code must retain the above
9  *      copyright notice, this list of conditions and the following
10  *      disclaimer.
11  *
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.
16  *
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.
29  *
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.
34  */
35 package gr.grnet.pithos.web.client;
36
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;
41
42 import java.util.ArrayList;
43 import java.util.Arrays;
44 import java.util.HashMap;
45 import java.util.List;
46 import java.util.Map;
47
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;
69
70 /**
71  * The 'File upload' dialog box implementation with Google Gears support.
72  */
73 public class FileUploadGearsDialog extends FileUploadDialog {
74
75         protected final Factory factory = Factory.getInstance();
76
77         /**
78          * The array of files to upload.
79          */
80         private File[] fileObjects;
81
82         /**
83          * A list of files to upload, created from files array. Used to signal
84          * finished state when empty.
85          */
86         protected List<File> selectedFiles = new ArrayList<File>();
87
88         /**
89          * The list of progress bars for individual files.
90          */
91         protected List<ProgressBar> progressBars = new ArrayList<ProgressBar>();
92
93         private Button browse;
94
95         private Button submit;
96
97         private FlexTable generalTable;
98
99         private Map<String, gr.grnet.pithos.web.client.foldertree.File> toRename;
100
101         protected List<HttpRequest> requests = new ArrayList<HttpRequest>();
102         
103         private boolean canContinue = true;
104
105     protected FileUploadGearsDialog() {
106     }
107
108         /**
109          * The widget's constructor.
110          */
111         public FileUploadGearsDialog(Pithos _app, Folder _folder) {
112         this.folder = _folder;
113         this.app = _app;
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");
121                 panel.add(info);
122                 // Add an informative label with the folder name.
123                 Object selection = app.getTreeView().getSelection();
124
125                 browse = new Button("Browse...");
126
127                 HorizontalPanel fileUploadPanel = new HorizontalPanel();
128                 fileUploadPanel.add(browse);
129
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);
140
141                 panel.add(generalTable);
142
143                 // Create a panel to hold the buttons.
144                 HorizontalPanel buttons = new HorizontalPanel();
145
146                 submit = new Button("Upload");
147                 submit.addClickHandler(new ClickHandler() {
148                         @Override
149                         public void onClick(ClickEvent event) {
150                                 prepareAndSubmit();
151                         }
152                 });
153                 submit.setEnabled(false);
154                 buttons.add(submit);
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() {
159                         @Override
160                         public void onClick(ClickEvent event) {
161                                 canContinue = false;                            
162                                 cancelUpload();                         
163                                 app.showFileList(true);
164                         }
165                 });
166                 buttons.add(cancel);
167                 buttons.setCellHorizontalAlignment(cancel, HasHorizontalAlignment.ALIGN_CENTER);
168                 buttons.setSpacing(8);
169                 buttons.addStyleName("pithos-DialogBox");
170
171                 browse.addClickHandler(new ClickHandler() {
172                         @Override
173                         public void onClick(ClickEvent event) {
174                                 Desktop desktop = factory.createDesktop();
175                                 desktop.openFiles(new OpenFilesHandler() {
176
177                                         @Override
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");
189                                                 }
190                                                 submit.setEnabled(true);
191                                         }
192                                 });
193                         }
194                 });
195
196                 panel.add(buttons);
197                 panel.setCellHorizontalAlignment(buttons, HasHorizontalAlignment.ALIGN_CENTER);
198                 panel.addStyleName("pithos-DialogBox");
199                 addStyleName("pithos-DialogBox");
200                 setWidget(panel);
201         }
202
203         /**
204          * Cancels the file upload.
205          */
206         private void cancelUpload() {
207                 for (HttpRequest request: requests)
208                         request.abort();
209                 hide();         
210         }
211
212         @Override
213         public void prepareAndSubmit() {
214                 if (selectedFiles.size() == 0) {
215                         app.displayError("You must select a file!");
216                         hide();
217                         return;
218                 }
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))
231                                                 same = fres;
232                                 // In that case add it to the list of files to rename.
233                                 if (same != null)
234                                         toRename.put(getBackupFilename(fname), same);
235                         } else
236                                 // If we are updating a file add it to the list of files to update.
237                                 toUpdate.add(fname);
238                 }
239
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 +
247                                         "</i>", "Update") {
248
249                                 @Override
250                                 public void cancel() {
251                                         hide();
252                                 }
253
254                                 @Override
255                                 public void confirm() {
256                                         confirmRename();
257                                 }
258
259                         };
260                         confirm.center();
261                 } else
262                         confirmRename();
263         }
264
265         /**
266          * Confirm the renames of synonymous files already in the trash.
267          */
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") {
277
278                                 @Override
279                                 public void cancel() {
280                                         hide();
281                                 }
282
283                                 @Override
284                                 public void confirm() {
285                                         updateTrashedFiles();
286                                 }
287
288                         };
289                         confirm.center();
290                 } else
291                         uploadFiles();
292         }
293
294         /**
295          * Rename the conflicting trashed files with the supplied new names.
296          */
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) {
302
303                                 @Override
304                                 public void onComplete() {
305                                         toRename.remove(name);
306                                         uploadFiles();
307                                 }
308
309                                 @Override
310                                 public void onError(Throwable t) {
311                                         GWT.log("", 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");
322                                                 else
323                                                         app.displayError("Unable to modify file:" + ((RestException) t).getHttpStatusText());
324                                         } else
325                                                 app.displayError("System error modifying file:" + t.getMessage());
326                                 }
327
328                         };
329                         DeferredCommand.addCommand(cf);
330                 }
331         }
332
333         /**
334          * Checks if the renaming step for already trashed files is complete and
335          * starts file uploads.
336          */
337         private void uploadFiles() {            
338                 if (!toRename.isEmpty()) return;
339                 if (canContinue){                                               
340                         doSend(selectedFiles);
341                 }
342         }
343
344         /**
345          * Perform the HTTP request to upload the specified file.
346          */
347         protected void doSend(final List<File> filesRemaining) {
348                 HttpRequest request = factory.createHttpRequest();
349                 requests.add(request);
350                 String method = "PUT";
351
352                 String path;
353                 final String filename = getFilename(filesRemaining.get(0).getName());
354                 path = folder.getUri();
355                 if (!path.endsWith("/"))
356                         path = path + "/";
357                 path = path + encode(filename);
358
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() {
367                         @Override
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.
373                                         case 204:
374                                                 filesRemaining.remove(0);
375                                                 if(filesRemaining.isEmpty()){                                                   
376                                                         finish();
377                                                         break;
378                                                 }                                               
379                                                 doSend(filesRemaining);                         
380                                                 break;
381                                         case 403:
382                                                 SessionExpiredDialog dlg = new SessionExpiredDialog(app);
383                                                 dlg.center();
384                                                 break;
385                                         case 405:
386                                                 app.displayError("You don't have permission to " +
387                                                                 "upload file " + filename);
388                                                 break;
389                                         case 409:
390                                                 app.displayError("A folder with the name " + filename +
391                                                                 " already exists at this level");
392                                                 break;
393                                         case 413:
394                                                 app.displayError("There is not enough free space " +
395                                                                 "available for uploading " + filename);
396                                                 break;
397                                         default:
398                                                 app.displayError("Error uploading file " + filename +
399                                                                         ": " + req.getStatus());
400                                 }
401                         }
402                 });
403                 request.getUpload().setProgressHandler(new ProgressHandler() {
404                         @Override
405                         public void onProgress(ProgressEvent event) {
406                                 double pcnt = (double) event.getLoaded() / event.getTotal();
407                                 progressBars.get(0).setProgress((int) Math.floor(pcnt * 100));
408                                 if(pcnt*100 == 100)
409                                         progressBars.remove(0);
410                         }
411                 });
412                 request.send(filesRemaining.get(0).getBlob());
413         }
414
415         /**
416          * Perform the final actions after the files are uploaded.
417          */
418         protected void finish() {
419                 hide();
420                 //app.showFileList(true);
421                 app.getTreeView().updateNode(app.getTreeView().getSelection());//showFileList(true);
422                 app.getStatusPanel().updateStats();
423         }
424
425         /**
426          * Same as URL.encode, but also encode apostrophe since browsers aren't
427          * consistent about it (FF encodes, IE does not).
428          */
429         protected String encode(String decodedURL) {
430                 String retv = decodedURL.replaceAll("@", "_"); // Replace bad character
431                 retv = URL.encodeComponent(retv);
432                 retv = retv.replaceAll("'", "%27");
433                 return retv;
434         }
435
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);
441         int i = 1;
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(" ")) {
446                     int test = -1;
447                     try {
448                         test = Integer.valueOf(toCheck.replace(" ", ""));
449                     } catch (NumberFormatException e) {
450                         // Do nothing since string is not a number.
451                     }
452                     if (test >= i)
453                         i = test + 1;
454                 }
455             }
456
457         return filename + " " + i;
458     }
459 }