Merge branch 'packaging' into debian
[pithos-web-client] / src / gr / grnet / pithos / web / client / FileUploadDialog.java
index abae4da..924c70f 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright 2011 GRNET S.A. All rights reserved.
+ * Copyright 2011-2012 GRNET S.A. All rights reserved.
  *
  * Redistribution and use in source and binary forms, with or
  * without modification, are permitted provided that the following
  */
 package gr.grnet.pithos.web.client;
 
-import gr.grnet.pithos.web.client.foldertree.File;
 import gr.grnet.pithos.web.client.foldertree.Folder;
 
-import com.google.gwt.core.client.Scheduler;
+import com.google.gwt.dom.client.NativeEvent;
 import com.google.gwt.event.dom.client.ClickEvent;
 import com.google.gwt.event.dom.client.ClickHandler;
+import com.google.gwt.event.dom.client.KeyCodes;
 import com.google.gwt.user.client.Command;
+import com.google.gwt.user.client.Event.NativePreviewEvent;
 import com.google.gwt.user.client.ui.Anchor;
 import com.google.gwt.user.client.ui.Button;
 import com.google.gwt.user.client.ui.DialogBox;
@@ -81,17 +82,15 @@ public class FileUploadDialog extends DialogBox {
        /**
         * The widget's constructor.
         */
-       public FileUploadDialog(Pithos _app, Folder _folder) {
+       public FileUploadDialog(Pithos _app) {
                app = _app;
-               folder = _folder;
-               foldernameLabel.setText(folder.getName());
-               Anchor close = new Anchor();
+               Anchor close = new Anchor("close");
                close.addStyleName("close");
                close.addClickHandler(new ClickHandler() {
                        
                        @Override
                        public void onClick(ClickEvent event) {
-                               hide();
+                               close();
                        }
                });
                // Set the dialog's caption.
@@ -99,6 +98,8 @@ public class FileUploadDialog extends DialogBox {
                setAnimationEnabled(true);
                setGlassEnabled(true);
                setStyleName("pithos-DialogBox");
+               setVisible(false);
+               
                // Since we're going to add a FileUpload widget, we'll need to set the
                // form to use the POST method, and multipart MIME encoding.
                form.setEncoding(FormPanel.ENCODING_MULTIPART);
@@ -106,6 +107,7 @@ public class FileUploadDialog extends DialogBox {
 
                // Create a panel to hold all of the form widgets.
                VerticalPanel panel = new VerticalPanel();
+               panel.setWidth("470px");
                panel.add(close);
                form.setWidget(panel);
 
@@ -141,15 +143,6 @@ public class FileUploadDialog extends DialogBox {
                panel.add(inner);
                panel.setCellHorizontalAlignment(inner, HasHorizontalAlignment.ALIGN_CENTER);
                
-               
-               Scheduler.get().scheduleDeferred(new Command() {
-                       
-                       @Override
-                       public void execute() {
-                               String path = app.getApiPath() + folder.getOwner() + folder.getUri();
-                               setupUpload(FileUploadDialog.this, path, app.getToken());
-                       }
-               });
                setWidget(form);
        }
 
@@ -161,131 +154,151 @@ public class FileUploadDialog extends DialogBox {
                                public void execute() {
                                        app.updateStatistics();
                                }
-                       });
+                       }, true);
                else
                        app.updateOtherSharedFolder(folder, true);
        }
        
        native void setupUpload(FileUploadDialog dlg, String path, String token) /*-{
-               $wnd.$("#uploader").pluploadQueue({
-                       // General settings
-                       runtimes : 'html5, silverlight, flash, gears, browserplus, html4',
-                       unique_names : true,
-       
-                       // Flash settings
-                       flash_swf_url : 'plupload/js/plupload.flash.swf',
-       
-                       // Silverlight settings
-                       silverlight_xap_url : 'plupload/js/plupload.silverlight.xap',
-                       
-                       preinit: {
-                               Init: function(up, info) {
-                                       up.settings.file_data_name = "X-Object-Data";                           
-                               },
+               var uploader = $wnd.$('#uploader').pluploadQueue();
+               var createUploader = function() {
+                       $wnd.$("#uploader").pluploadQueue({
+                               // General settings
+                               runtimes : 'html5, flash, gears, silverlight, browserplus, html4',
+                               unique_names : true,
+               
+                               // Flash settings
+                               flash_swf_url : 'plupload/js/plupload.flash.swf',
+               
+                               // Silverlight settings
+                               silverlight_xap_url : 'plupload/js/plupload.silverlight.xap',
                                
-                               UploadFile: function(up, file) {
-                                       $wnd.console.log('About to upload ' + file.name);
-                                       up.settings.url = path + "/" + file.name + "?X-Auth-Token=" + token;
-                               }
-                       },
-                       
-                       init: {
-                               FileUploaded: function(up, file, response) {
-                                       $wnd.console.log('File ' + file.name + ' uploaded');
-                                       $wnd.console.log('Response: ' + response);
+                               multiple_queues: true,
+                               
+                               preinit: {
+                                       Init: function(up, info) {
+                                               if ($wnd.console && $wnd.console.log)
+                                                       $wnd.console.log("Init fired");
+                                               up.settings.file_data_name = "X-Object-Data";                           
+                                       }
+                                       
                                },
                                
-                               UploadComplete: function(up, files) {
-                                       $wnd.console.log('All files finished');
-                                       dlg.@gr.grnet.pithos.web.client.FileUploadDialog::refreshFolder()();
+                               init: {
+                                       FilesAdded: function(up, files) {
+                                               for (var j=0; j<files.length; j++)
+                                                       files[j].url = up.path + "/" + files[j].name + "?X-Auth-Token=" + encodeURIComponent(token);
+                                       },
+                                       
+                                       BeforeUpload: function(up, file) {
+                                               if ($wnd.console && $wnd.console.log)
+                                                       $wnd.console.log('About to upload ' + file.url);
+                                               up.settings.url = file.url;
+                                       },
+                                       
+                                       FileUploaded: function(up, file, response) {
+                                               if ($wnd.console && $wnd.console.log) {
+                                                       $wnd.console.log('File ' + file.name + ' uploaded');
+                                                       $wnd.console.log('Response: ' + response);
+                                               }
+                                       },
+                                       
+                                       UploadComplete: function(up, files) {
+                                               if ($wnd.console && $wnd.console.log)
+                                                       $wnd.console.log('All files finished');
+                                               dlg.@gr.grnet.pithos.web.client.FileUploadDialog::hideUploadIndicator()();
+                                               dlg.@gr.grnet.pithos.web.client.FileUploadDialog::refreshFolder()();
+                                       },
+                                       
+                                       Error: function(up, error) {
+                                               if ($wnd.console && $wnd.console.log)
+                                                       $wnd.console.log("Error occured:" + error);
+                                       }
+                               }
+                       });
+                       return $wnd.$('#uploader').pluploadQueue();
+               };
+               
+               if ($wnd.console && $wnd.console.log)
+                       $wnd.console.log(uploader);
+               if (!uploader) {
+                       uploader = createUploader();
+               }
+               else {
+                       var removeAll = true;
+                       var files = uploader.files;
+                       if ($wnd.console && $wnd.console.log)
+                               $wnd.console.log('About to check ' + files.length + ' files');
+                       for (var i=0; i<files.length; i++) {
+                               var f = files[i];
+                               if (f.status != $wnd.plupload.DONE && f.status != $wnd.plupload.FAILED) {
+                                       removeAll = false;
+                                       break;
                                }
                        }
-               });
-       
-               // Client side form validation
-               $wnd.$('form').submit(function(e) {
-               var uploader = $wnd.$('#uploader').pluploadQueue();
-       
-               // Files in queue upload them first
-               if (uploader.files.length > 0) {
-                   // When all files are uploaded submit form
-                   uploader.bind('StateChanged', function() {
-                       if (uploader.files.length === (uploader.total.uploaded + uploader.total.failed)) {
-                           $wnd.$('form')[0].submit();
-                       }
-                   });
-                       
-                   uploader.start();
-               } else {
-                   alert('You must queue at least one file.');
-               }
-       
-               return false;
-           });
+                       if (removeAll) {
+                               if ($wnd.console && $wnd.console.log)
+                                       $wnd.console.log('About to remove ' + files.length + ' files');
+                               uploader.destroy();
+                               uploader = createUploader();
+                               if ($wnd.console && $wnd.console.log)
+                                       $wnd.console.log(uploader);
+                       }
+               }
+               uploader.path = path;
        }-*/;
        
-       /**
-        * Make any last minute checks and start the upload.
-        */
-       protected void prepareAndSubmit() {
-        if (upload.getFilename().length() == 0) {
-            app.displayError("You must select a file!");
-            return;
-        }
-        final String fname = getFilename(upload.getFilename());
-        String apath = app.getApiPath() + folder.getOwner() + folder.getUri() + "/" + fname + "?X-Auth-Token=" + app.getToken();
-        form.setAction(apath);
-        submit.setEnabled(false);
-        upload.setVisible(false);
-        filenameLabel.setText(fname);
-        filenameLabel.setVisible(true);
-
-               if (getFileForName(fname) == null) {
-            form.submit();
+       native boolean isUploading()/*-{
+               var uploader = $wnd.$("#uploader").pluploadQueue();
+               var files = uploader.files;
+               for (var i=0; i<files.length; i++) {
+                       var f = files[i];
+                       if (f.status == $wnd.plupload.UPLOADING) {
+                               return true;
+                       }
                }
-               else {
-                       // We are going to update an existing file, so show a confirmation dialog.
-                       ConfirmationDialog confirm = new ConfirmationDialog("Are you sure " +
-                                       "you want to update " + fname + "?", "Update") {
-
-                               @Override
-                               public void cancel() {
-                                       FileUploadDialog.this.hide();
-                               }
-
-                               @Override
-                               public void confirm() {
-                                       form.submit();
-                               }
+               return false;
+       }-*/;
+       
+       @Override
+       protected void onPreviewNativeEvent(NativePreviewEvent event) {
+               super.onPreviewNativeEvent(event);
 
-                       };
-                       confirm.center();
-               }
+               NativeEvent evt = event.getNativeEvent();
+               if (evt.getType().equals("keydown"))
+                       // Use the popup's key preview hooks to close the dialog when
+                       // escape is pressed.
+                       switch (evt.getKeyCode()) {
+                               case KeyCodes.KEY_ESCAPE:
+                                       close();
+                                       break;
+                       }
        }
 
-    /**
-        * Returns the file name from a potential full path argument. Apparently IE
-        * insists on sending the full path name of a file when uploading, forcing
-        * us to trim the extra path info. Since this is only observed on Windows we
-        * get to check for a single path separator value.
-        *
-        * @param name the potentially full path name of a file
-        * @return the file name without extra path information
-        */
-       protected String getFilename(String name) {
-               int pathSepIndex = name.lastIndexOf("\\");
-               if (pathSepIndex == -1) {
-                       pathSepIndex = name.lastIndexOf("/");
-                       if (pathSepIndex == -1)
-                               return name;
-               }
-               return name.substring(pathSepIndex + 1);
+       public void setFolder(Folder folder) {
+               this.folder = folder;
+               foldernameLabel.setText(folder.getName());
        }
 
-       protected File getFileForName(String name){
-               for (File f : folder.getFiles())
-                       if (f.getName().equals(name))
-                               return f;
-               return null;
+       @Override
+       public void center() {
+               app.hideUploadIndicator();
+               setVisible(true);
+               setModal(true);
+               super.center();
+               String path = app.getApiPath() + folder.getOwner() + folder.getUri();
+               setupUpload(this, path, app.getToken());
+               super.center();
+       }
+       
+       private void hideUploadIndicator() {
+               app.hideUploadIndicator();
+       }
+       
+       void close() {
+               setVisible(false);
+               setModal(false);
+               if (isUploading())
+                       app.showUploadIndicator();
        }
 }