Merged with plupload branch
[pithos-web-client] / src / gr / grnet / pithos / web / client / FileUploadDialog.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.File;
38 import gr.grnet.pithos.web.client.foldertree.Folder;
39
40 import com.google.gwt.core.client.GWT;
41 import com.google.gwt.core.client.Scheduler;
42 import com.google.gwt.dom.client.NativeEvent;
43 import com.google.gwt.event.dom.client.ClickEvent;
44 import com.google.gwt.event.dom.client.ClickHandler;
45 import com.google.gwt.event.dom.client.KeyCodes;
46 import com.google.gwt.user.client.Command;
47 import com.google.gwt.user.client.Cookies;
48 import com.google.gwt.user.client.Event.NativePreviewEvent;
49 import com.google.gwt.user.client.ui.Anchor;
50 import com.google.gwt.user.client.ui.Button;
51 import com.google.gwt.user.client.ui.DialogBox;
52 import com.google.gwt.user.client.ui.FileUpload;
53 import com.google.gwt.user.client.ui.FormPanel;
54 import com.google.gwt.user.client.ui.FormPanel.SubmitCompleteEvent;
55 import com.google.gwt.user.client.ui.FormPanel.SubmitCompleteHandler;
56 import com.google.gwt.user.client.ui.FormPanel.SubmitEvent;
57 import com.google.gwt.user.client.ui.FormPanel.SubmitHandler;
58 import com.google.gwt.user.client.ui.Anchor;
59 import com.google.gwt.user.client.ui.FlowPanel;
60 import com.google.gwt.user.client.ui.Grid;
61 import com.google.gwt.user.client.ui.HasHorizontalAlignment;
62 import com.google.gwt.user.client.ui.Hidden;
63 import com.google.gwt.user.client.ui.HorizontalPanel;
64 import com.google.gwt.user.client.ui.Label;
65 import com.google.gwt.user.client.ui.VerticalPanel;
66
67 /**
68  * The 'File upload' dialog box implementation.
69  */
70 public class FileUploadDialog extends DialogBox {
71
72     public static final boolean DONE = true;
73
74         /**
75          * The Form element that performs the file upload.
76          */
77     protected final FormPanel form = new FormPanel();
78
79         private final FileUpload upload = new FileUpload();
80
81         private final Label filenameLabel = new Label();
82
83     private final Label foldernameLabel = new Label();
84
85     private Button submit;
86
87         protected Folder folder;
88
89     protected Pithos app;
90
91         /**
92          * The widget's constructor.
93          */
94         public FileUploadDialog() {
95                 Anchor close = new Anchor();
96                 close.addStyleName("close");
97                 close.addClickHandler(new ClickHandler() {
98                         
99                         @Override
100                         public void onClick(ClickEvent event) {
101                                 hide();
102                         }
103                 });
104                 // Set the dialog's caption.
105                 setText("File upload");
106                 setAnimationEnabled(true);
107                 setGlassEnabled(true);
108                 setStyleName("pithos-DialogBox");
109                 // Since we're going to add a FileUpload widget, we'll need to set the
110                 // form to use the POST method, and multipart MIME encoding.
111                 form.setEncoding(FormPanel.ENCODING_MULTIPART);
112                 form.setMethod(FormPanel.METHOD_POST);
113
114                 // Create a panel to hold all of the form widgets.
115                 VerticalPanel panel = new VerticalPanel();
116                 panel.add(close);
117                 form.setWidget(panel);
118
119                 VerticalPanel inner = new VerticalPanel();
120                 inner.addStyleName("inner");
121
122         final Hidden auth = new Hidden("X-Auth-Token");
123         inner.add(auth);
124                 upload.setName("X-Object-Data");
125                 filenameLabel.setText("");
126                 filenameLabel.setVisible(false);
127                 filenameLabel.setStyleName("props-labels");
128                 HorizontalPanel fileUploadPanel = new HorizontalPanel();
129                 fileUploadPanel.add(filenameLabel);
130                 fileUploadPanel.add(upload);
131                 Grid generalTable = new Grid(2, 2);
132                 generalTable.setText(0, 0, "Folder");
133         generalTable.setWidget(0, 1, foldernameLabel);
134                 generalTable.setText(1, 0, "File");
135                 generalTable.setWidget(1, 1, fileUploadPanel);
136                 generalTable.getCellFormatter().setStyleName(0, 0, "props-labels");
137         generalTable.getCellFormatter().setStyleName(0, 1, "props-values");
138                 generalTable.getCellFormatter().setStyleName(1, 0, "props-labels");
139                 generalTable.getCellFormatter().setStyleName(1, 1, "props-values");
140                 generalTable.setCellSpacing(4);
141
142                 inner.add(generalTable);
143
144                 // Create the 'upload' button, along with a listener that submits the
145                 // form.
146                 submit = new Button("Upload", new ClickHandler() {
147                         @Override
148                         public void onClick(ClickEvent event) {
149                                 prepareAndSubmit();
150                         }
151                 });
152                 submit.addStyleName("button");
153                 inner.add(submit);
154
155                 // Add an event handler to the form.
156                 form.addSubmitHandler(new SubmitHandler() {
157
158                         @Override
159                         public void onSubmit(SubmitEvent event) {
160                 auth.setValue(app.getToken()); //This is done here because the app object is not available in the constructor
161                 Cookies.setCookie("X-Auth-Token", app.getToken(), null, "", "/", false);
162                         }
163                 });
164                 form.addSubmitCompleteHandler(new SubmitCompleteHandler() {
165
166                         @Override
167                         public void onSubmitComplete(SubmitCompleteEvent event) {
168                                 // When the form submission is successfully completed, this
169                                 // event is fired. Assuming the service returned a response
170                                 // of type text/html, we can get the result text here (see
171                                 // the FormPanel documentation for further explanation).
172                                 String results = event.getResults();
173
174                                 // Unfortunately the results are never empty, even in
175                                 // the absense of errors, so we have to check for '<pre></pre>'.
176                                 if (results != null && results.length() > 0 && !results.equalsIgnoreCase("<pre></pre>")) {
177                                         GWT.log(results, null);
178                                         app.displayError(results);
179                                 }
180                                 if (app.getSelectedTree().equals(app.getFolderTreeView()))
181                                         app.updateFolder(folder, true, new Command() {
182                                                 
183                                                 @Override
184                                                 public void execute() {
185                                                         app.updateStatistics();
186                                                 }
187                                         });
188                                 else
189                                         app.updateOtherSharedFolder(folder, true);
190                                 hide();
191                         }
192                 });
193
194                 FlowPanel uploader = new FlowPanel();
195                 uploader.getElement().setId("uploader");
196                 inner.add(uploader);
197                 
198                 panel.add(inner);
199                 panel.setCellHorizontalAlignment(inner, HasHorizontalAlignment.ALIGN_CENTER);
200                 
201                 
202                 Scheduler.get().scheduleDeferred(new Command() {
203                         
204                         @Override
205                         public void execute() {
206                                 String path = app.getApiPath() + folder.getOwner() + folder.getUri();
207                                 setupUpload(path, app.getToken());
208                         }
209                 });
210                 setWidget(form);
211         }
212
213         native void setupUpload(String path, String token) /*-{
214                 $wnd.$("#uploader").pluploadQueue({
215                         // General settings
216                         runtimes : 'html5, flash, gears, silverlight, browserplus',
217                         url : 'upload.php',
218                         max_file_size : '10mb',
219                         chunk_size : '1mb',
220                         unique_names : true,
221         
222                         // Resize images on clientside if we can
223                         resize : {width : 320, height : 240, quality : 90},
224         
225                         // Flash settings
226                         flash_swf_url : 'plupload/js/plupload.flash.swf',
227         
228                         // Silverlight settings
229                         silverlight_xap_url : 'plupload/js/plupload.silverlight.xap',
230                         
231                         preinit: {
232                                 UploadFile: function(up, file) {
233                                         up.settings.url = path + "/" + file.name + "?X-Auth-Token=" + token;
234                                         up.settings.file_data_name = "X-Object-Data";                           
235                                 }
236                         }
237                 });
238         
239                 // Client side form validation
240                 $wnd.$('form').submit(function(e) {
241                 var uploader = $wnd.$('#uploader').pluploadQueue();
242         
243                 // Files in queue upload them first
244                 if (uploader.files.length > 0) {
245                     // When all files are uploaded submit form
246                     uploader.bind('StateChanged', function() {
247                         if (uploader.files.length === (uploader.total.uploaded + uploader.total.failed)) {
248                             $wnd.$('form')[0].submit();
249                         }
250                     });
251                         
252                     uploader.start();
253                 } else {
254                     alert('You must queue at least one file.');
255                 }
256         
257                 return false;
258             });
259         }-*/;
260         
261         @Override
262         protected void onPreviewNativeEvent(NativePreviewEvent preview) {
263                 super.onPreviewNativeEvent(preview);
264
265                 NativeEvent evt = preview.getNativeEvent();
266                 if (evt.getType().equals("keydown"))
267                         // Use the popup's key preview hooks to close the dialog when either
268                         // enter or escape is pressed.
269                         switch (evt.getKeyCode()) {
270                                 case KeyCodes.KEY_ENTER:
271                                         prepareAndSubmit();
272                                         break;
273                                 case KeyCodes.KEY_ESCAPE:
274                                         hide();
275                                         break;
276                         }
277         }
278
279         /**
280          * Make any last minute checks and start the upload.
281          */
282         protected void prepareAndSubmit() {
283         if (upload.getFilename().length() == 0) {
284             app.displayError("You must select a file!");
285             return;
286         }
287         final String fname = getFilename(upload.getFilename());
288         String apath = app.getApiPath() + folder.getOwner() + folder.getUri() + "/" + fname + "?X-Auth-Token=" + app.getToken();
289         form.setAction(apath);
290         submit.setEnabled(false);
291         upload.setVisible(false);
292         filenameLabel.setText(fname);
293         filenameLabel.setVisible(true);
294
295                 if (getFileForName(fname) == null) {
296             form.submit();
297                 }
298                 else {
299                         // We are going to update an existing file, so show a confirmation dialog.
300                         ConfirmationDialog confirm = new ConfirmationDialog("Are you sure " +
301                                         "you want to update " + fname + "?", "Update") {
302
303                                 @Override
304                                 public void cancel() {
305                                         FileUploadDialog.this.hide();
306                                 }
307
308                                 @Override
309                                 public void confirm() {
310                                         form.submit();
311                                 }
312
313                         };
314                         confirm.center();
315                 }
316         }
317
318     /**
319          * Returns the file name from a potential full path argument. Apparently IE
320          * insists on sending the full path name of a file when uploading, forcing
321          * us to trim the extra path info. Since this is only observed on Windows we
322          * get to check for a single path separator value.
323          *
324          * @param name the potentially full path name of a file
325          * @return the file name without extra path information
326          */
327         protected String getFilename(String name) {
328                 int pathSepIndex = name.lastIndexOf("\\");
329                 if (pathSepIndex == -1) {
330                         pathSepIndex = name.lastIndexOf("/");
331                         if (pathSepIndex == -1)
332                                 return name;
333                 }
334                 return name.substring(pathSepIndex + 1);
335         }
336
337         protected File getFileForName(String name){
338                 for (File f : folder.getFiles())
339                         if (f.getName().equals(name))
340                                 return f;
341                 return null;
342         }
343
344     public void setApp(Pithos app) {
345         this.app = app;
346     }
347
348     public void setFolder(Folder folder) {
349         this.folder = folder;
350         foldernameLabel.setText(folder.getName());
351     }
352 }